"use strict";
/* Socket Lib - Built with rolldown */
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../_virtual/_rolldown/runtime.js');
const require_primordials_buffer = require('../primordials/buffer.js');
const require_primordials_error = require('../primordials/error.js');
const require_primordials_string = require('../primordials/string.js');
const require_primordials_map_set = require('../primordials/map-set.js');
const require_fs_safe = require('../fs/safe.js');
const require_compression__internal = require('./_internal.js');
let node_fs = require("node:fs");
let node_path = require("node:path");
node_path = require_runtime.__toESM(node_path);
let node_util = require("node:util");
let node_stream_promises = require("node:stream/promises");
let node_zlib = require("node:zlib");

//#region src/compression/gzip.ts
const gzipAsync = (0, node_util.promisify)(node_zlib.gzip);
const gunzipAsync = (0, node_util.promisify)(node_zlib.gunzip);
/**
* Translate `CompressOptions` into the `ZlibOptions` zlib expects. Returns an
* empty options object when no `level` is given (zlib uses its default, level
* 6). Exposed for parity with `resolveBrotliOptions` and for unit-test
* coverage.
*/
function resolveGzipOptions(options) {
	const level = options?.level;
	if (level === void 0) return { __proto__: null };
	return {
		__proto__: null,
		level
	};
}
/**
* Compress a string or Buffer with gzip. Strings are encoded as UTF-8 before
* compression. Default level is 6 (zlib default).
*/
async function compressGzip(input, options) {
	return await gzipAsync(typeof input === "string" ? require_primordials_buffer.BufferFrom(input, "utf8") : input, resolveGzipOptions(options));
}
/**
* Decompress a gzip-compressed Buffer.
*/
async function decompressGzip(input) {
	return await gunzipAsync(input);
}
async function compressGzipFile(srcPath, destOrOptions, maybeOptions) {
	const { destPath, options, inPlace } = require_compression__internal.resolveFileArgs("compressGzipFile", srcPath, destOrOptions, maybeOptions, (p) => `${p}.gz`);
	await (0, node_stream_promises.pipeline)((0, node_fs.createReadStream)(srcPath), (0, node_zlib.createGzip)(resolveGzipOptions(options)), (0, node_fs.createWriteStream)(destPath));
	if (inPlace) await require_fs_safe.safeDelete(srcPath);
	return destPath;
}
async function decompressGzipFile(srcPath, destOrOptions) {
	const { destPath, inPlace } = require_compression__internal.resolveFileArgs("decompressGzipFile", srcPath, destOrOptions, void 0, (p) => {
		if (!hasGzipExt(p)) throw new require_primordials_error.ErrorCtor(`decompressGzipFile: ${p} has no .gz/.gzip/.tgz extension; can't infer destination`);
		const stripped = require_compression__internal.stripExt(p, GZIP_EXTS);
		return require_primordials_string.StringPrototypeToLowerCase(node_path.default.extname(p)) === ".tgz" ? `${stripped}.tar` : stripped;
	});
	await (0, node_stream_promises.pipeline)((0, node_fs.createReadStream)(srcPath), (0, node_zlib.createGunzip)(), (0, node_fs.createWriteStream)(destPath));
	if (inPlace) await require_fs_safe.safeDelete(srcPath);
	return destPath;
}
/**
* Create a gzip compress transform stream.
*/
function createGzipCompressor(options) {
	return (0, node_zlib.createGzip)(resolveGzipOptions(options));
}
/**
* Create a gzip decompress transform stream.
*/
function createGzipDecompressor() {
	return (0, node_zlib.createGunzip)();
}
const GZIP_MAGIC_0 = 31;
const GZIP_MAGIC_1 = 139;
/**
* Magic-byte check for gzip. Reads the first two bytes and matches the gzip
* spec's 0x1f 0x8b signature. Authoritative.
*/
function isGzipCompressed(input) {
	return require_primordials_buffer.BufferIsBuffer(input) && input.byteLength >= 2 && input[0] === GZIP_MAGIC_0 && input[1] === GZIP_MAGIC_1;
}
const GZIP_EXTS = new require_primordials_map_set.SetCtor([
	".gz",
	".gzip",
	".tgz"
]);
/**
* Extension check for gzip paths — matches `.gz` / `.gzip` / `.tgz`
* (case-insensitive). Naming follows node:path's `extname`.
*/
function hasGzipExt(filePath) {
	return GZIP_EXTS.has(require_primordials_string.StringPrototypeToLowerCase(node_path.default.extname(filePath)));
}

//#endregion
exports.GZIP_EXTS = GZIP_EXTS;
exports.compressGzip = compressGzip;
exports.compressGzipFile = compressGzipFile;
exports.createGzipCompressor = createGzipCompressor;
exports.createGzipDecompressor = createGzipDecompressor;
exports.decompressGzip = decompressGzip;
exports.decompressGzipFile = decompressGzipFile;
exports.hasGzipExt = hasGzipExt;
exports.isGzipCompressed = isGzipCompressed;
exports.resolveGzipOptions = resolveGzipOptions;