"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var gzip_exports = {};
__export(gzip_exports, {
  GZIP_EXTS: () => GZIP_EXTS,
  compressGzip: () => compressGzip,
  compressGzipFile: () => compressGzipFile,
  createGzipCompressor: () => createGzipCompressor,
  createGzipDecompressor: () => createGzipDecompressor,
  decompressGzip: () => decompressGzip,
  decompressGzipFile: () => decompressGzipFile,
  hasGzipExt: () => hasGzipExt,
  isGzipCompressed: () => isGzipCompressed,
  resolveGzipOptions: () => resolveGzipOptions
});
module.exports = __toCommonJS(gzip_exports);
var import_node_buffer = require("node:buffer");
var import_node_fs = require("node:fs");
var import_node_path = __toESM(require("node:path"));
var import_promises = require("node:stream/promises");
var import_node_util = require("node:util");
var import_node_zlib = require("node:zlib");
var import_safe = require("../fs/safe");
var import_error = require("../primordials/error");
var import_string = require("../primordials/string");
var import_internal = require("./_internal");
const gzipAsync = (0, import_node_util.promisify)(import_node_zlib.gzip);
const gunzipAsync = (0, import_node_util.promisify)(import_node_zlib.gunzip);
function resolveGzipOptions(options) {
  const level = options?.level;
  if (level === void 0) {
    return { __proto__: null };
  }
  return { __proto__: null, level };
}
async function compressGzip(input, options) {
  const buf = typeof input === "string" ? import_node_buffer.Buffer.from(input, "utf8") : input;
  return await gzipAsync(buf, resolveGzipOptions(options));
}
async function decompressGzip(input) {
  return await gunzipAsync(input);
}
async function compressGzipFile(srcPath, destOrOptions, maybeOptions) {
  const { destPath, options, inPlace } = (0, import_internal.resolveFileArgs)(
    "compressGzipFile",
    srcPath,
    destOrOptions,
    maybeOptions,
    (p) => `${p}.gz`
  );
  await (0, import_promises.pipeline)(
    (0, import_node_fs.createReadStream)(srcPath),
    (0, import_node_zlib.createGzip)(resolveGzipOptions(options)),
    (0, import_node_fs.createWriteStream)(destPath)
  );
  if (inPlace) {
    await (0, import_safe.safeDelete)(srcPath);
  }
  return destPath;
}
async function decompressGzipFile(srcPath, destOrOptions) {
  const { destPath, inPlace } = (0, import_internal.resolveFileArgs)(
    "decompressGzipFile",
    srcPath,
    destOrOptions,
    void 0,
    (p) => {
      if (!hasGzipExt(p)) {
        throw new import_error.ErrorCtor(
          `decompressGzipFile: ${p} has no .gz/.gzip/.tgz extension; can't infer destination`
        );
      }
      const stripped = (0, import_internal.stripExt)(p, GZIP_EXTS);
      return (0, import_string.StringPrototypeToLowerCase)(import_node_path.default.extname(p)) === ".tgz" ? `${stripped}.tar` : stripped;
    }
  );
  await (0, import_promises.pipeline)(
    (0, import_node_fs.createReadStream)(srcPath),
    (0, import_node_zlib.createGunzip)(),
    (0, import_node_fs.createWriteStream)(destPath)
  );
  if (inPlace) {
    await (0, import_safe.safeDelete)(srcPath);
  }
  return destPath;
}
function createGzipCompressor(options) {
  return (0, import_node_zlib.createGzip)(resolveGzipOptions(options));
}
function createGzipDecompressor() {
  return (0, import_node_zlib.createGunzip)();
}
const GZIP_MAGIC_0 = 31;
const GZIP_MAGIC_1 = 139;
function isGzipCompressed(input) {
  return import_node_buffer.Buffer.isBuffer(input) && input.byteLength >= 2 && input[0] === GZIP_MAGIC_0 && input[1] === GZIP_MAGIC_1;
}
const GZIP_EXTS = /* @__PURE__ */ new Set([".gz", ".gzip", ".tgz"]);
function hasGzipExt(filePath) {
  return GZIP_EXTS.has((0, import_string.StringPrototypeToLowerCase)(import_node_path.default.extname(filePath)));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  GZIP_EXTS,
  compressGzip,
  compressGzipFile,
  createGzipCompressor,
  createGzipDecompressor,
  decompressGzip,
  decompressGzipFile,
  hasGzipExt,
  isGzipCompressed,
  resolveGzipOptions
});
