import { debug, varlockSettings, scanForLeaks, redactSensitiveConfig } from './chunk-PRASPAE4.js';
import { __name } from './chunk-IBAHCH6O.js';
import zlib from 'zlib';
import { ServerResponse } from 'http';

var patchedKey = "_patchedByVarlock";
function patchGlobalServerResponse(opts) {
  debug("\u26A1\uFE0F PATCHING global ServerResponse");
  if (Object.getOwnPropertyDescriptor(ServerResponse.prototype, patchedKey)) {
    debug("> already patched");
    return;
  }
  if (varlockSettings.preventLeaks === false) {
    debug("> disabled by settings");
    return;
  }
  Object.defineProperty(ServerResponse.prototype, patchedKey, { value: true });
  const serverResponseWrite = ServerResponse.prototype.write;
  ServerResponse.prototype.write = /* @__PURE__ */ __name(function varlockPatchedServerResponseWrite(...args) {
    const rawChunk = args[0];
    const contentType = this.getHeader("content-type")?.toString() || "";
    let runScan = contentType.startsWith("text/") || contentType.startsWith("application/json") || !contentType && typeof rawChunk === "string";
    const reqUrl = this.req.url;
    if (runScan && reqUrl && opts?.ignoreUrlPatterns?.some((pattern) => pattern.test(reqUrl))) {
      runScan = false;
    }
    if (!runScan) {
      return serverResponseWrite.apply(this, args);
    }
    const compressionType = this.getHeader("Content-Encoding");
    let chunkStr;
    let chunkType = null;
    if (typeof rawChunk === "string") {
      chunkType = "string";
      chunkStr = rawChunk;
    } else if (!compressionType) {
      chunkType = "encoded";
      const decoder = new TextDecoder();
      chunkStr = decoder.decode(rawChunk);
    } else if (compressionType === "gzip") {
      chunkType = "gzip";
      if (!this._zlibChunks) {
        this._zlibChunks = [rawChunk];
      } else {
        this._zlibChunks?.push(rawChunk);
        try {
          const unzippedChunk = zlib.unzipSync(Buffer.concat(this._zlibChunks || []), {
            flush: zlib.constants.Z_SYNC_FLUSH,
            finishFlush: zlib.constants.Z_SYNC_FLUSH
          });
          const fullUnzippedData = unzippedChunk.toString("utf-8");
          chunkStr = fullUnzippedData.substring(this._lastChunkEndIndex || 0);
          this._lastChunkEndIndex = fullUnzippedData.length;
        } catch (err) {
        }
      }
    }
    if (chunkStr) {
      try {
        scanForLeaks(chunkStr, { method: "patched ServerResponse.write", file: this.req.url });
      } catch (err) {
        if (opts?.redactInsteadOfThrow) {
          chunkStr = redactSensitiveConfig(chunkStr);
          if (chunkType === "string") {
            args[0] = chunkStr;
          } else if (chunkType === "encoded") {
            const encoder = new TextEncoder();
            args[0] = encoder.encode(chunkStr);
          } else if (chunkType === "gzip") ; else {
            throw new Error(`unable to scrub - unknown chunk type ${chunkType}`);
          }
        } else {
          throw err;
        }
      }
    }
    return serverResponseWrite.apply(this, args);
  }, "varlockPatchedServerResponseWrite");
  const serverResponseEnd = ServerResponse.prototype.end;
  ServerResponse.prototype.end = /* @__PURE__ */ __name(function patchedServerResponseEnd(...args) {
    const endChunk = args[0];
    if (endChunk && typeof endChunk === "string") {
      scanForLeaks(endChunk, { method: "patched ServerResponse.end" });
    }
    return serverResponseEnd.apply(this, args);
  }, "patchedServerResponseEnd");
}
__name(patchGlobalServerResponse, "patchGlobalServerResponse");

export { patchGlobalServerResponse };
//# sourceMappingURL=chunk-KU6Z4QI2.js.map
//# sourceMappingURL=chunk-KU6Z4QI2.js.map