"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var checksums_exports = {};
__export(checksums_exports, {
  fetchChecksums: () => fetchChecksums,
  parseChecksums: () => parseChecksums
});
module.exports = __toCommonJS(checksums_exports);
var import_error = require("../primordials/error");
var import_string = require("../primordials/string");
var import_request = require("./request");
const CHECKSUM_BSD_RE = /^SHA256\s+\((.+)\)\s+=\s+([a-fA-F0-9]{64})$/;
const CHECKSUM_GNU_RE = /^([a-fA-F0-9]{64})\s+(.+)$/;
async function fetchChecksums(url, options) {
  const {
    ca,
    headers = {},
    timeout = 3e4
  } = {
    __proto__: null,
    ...options
  };
  const response = await (0, import_request.httpRequest)(url, { ca, headers, timeout });
  if (!response.ok) {
    throw new import_error.ErrorCtor(
      `Failed to fetch checksums from ${url}: ${response.status} ${response.statusText}`
    );
  }
  return parseChecksums(response.body.toString("utf8"));
}
function parseChecksums(text) {
  const checksums = { __proto__: null };
  for (const line of (0, import_string.StringPrototypeSplit)(text, "\n")) {
    const trimmed = line.trim();
    if (!trimmed || (0, import_string.StringPrototypeStartsWith)(trimmed, "#")) {
      continue;
    }
    const bsdMatch = CHECKSUM_BSD_RE.exec(trimmed);
    if (bsdMatch) {
      checksums[bsdMatch[1]] = bsdMatch[2].toLowerCase();
      continue;
    }
    const gnuMatch = CHECKSUM_GNU_RE.exec(trimmed);
    if (gnuMatch) {
      checksums[gnuMatch[2]] = gnuMatch[1].toLowerCase();
    }
  }
  return checksums;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  fetchChecksums,
  parseChecksums
});
