Adapt per review

This commit is contained in:
Reinier Criel 2026-04-02 08:56:20 -07:00
parent 06ef0c3990
commit 0aabba668e
3 changed files with 75 additions and 41 deletions

View file

@ -3,6 +3,7 @@ import { generateCertForHost } from "./certUtils.js";
import { HttpsProxyAgent } from "https-proxy-agent";
import { ui } from "../environment/userInteraction.js";
import { gunzipSync } from "zlib";
import { omitHeaders } from "./http-utils.js";
/**
* @typedef {import("./interceptors/interceptorBuilder.js").Interceptor} Interceptor
@ -107,28 +108,6 @@ function getRequestPathAndQuery(url) {
return url;
}
/**
* @param {NodeJS.Dict<string | string[]>} headers
* @returns {NodeJS.Dict<string | string[]>}
*/
function normalizeRewrittenResponseHeaders(headers) {
/** @type {NodeJS.Dict<string | string[]>} */
const normalizedHeaders = { ...headers };
for (const headerName of Object.keys(headers)) {
const lowerHeaderName = headerName.toLowerCase();
if (
lowerHeaderName === "content-length" ||
lowerHeaderName === "transfer-encoding" ||
lowerHeaderName === "content-encoding"
) {
delete normalizedHeaders[headerName];
}
}
return normalizedHeaders;
}
/**
* @param {import("http").IncomingMessage} req
* @param {string} hostname
@ -240,7 +219,11 @@ function createProxyRequest(hostname, port, req, res, requestHandler) {
// For rewritten responses, send the final body uncompressed.
// This avoids mismatches between upstream compression metadata and the
// rewritten payload on the wire.
const rewrittenHeaders = normalizeRewrittenResponseHeaders(headers);
const rewrittenHeaders = omitHeaders(
headers,
["content-length", "transfer-encoding", "content-encoding"],
{ caseInsensitive: true }
) || {};
rewrittenHeaders["content-length"] = String(buffer.byteLength);
res.writeHead(statusCode, rewrittenHeaders);
res.end(buffer);