More cleanup

This commit is contained in:
Reinier Criel 2026-04-01 14:57:24 -07:00
parent e29c11546c
commit 1a811edc95
5 changed files with 201 additions and 78 deletions

View file

@ -2,7 +2,7 @@ import https from "https";
import { generateCertForHost } from "./certUtils.js";
import { HttpsProxyAgent } from "https-proxy-agent";
import { ui } from "../environment/userInteraction.js";
import { gunzipSync, gzipSync } from "zlib";
import { gunzipSync } from "zlib";
/**
* @typedef {import("./interceptors/interceptorBuilder.js").Interceptor} Interceptor
@ -107,6 +107,23 @@ function getRequestPathAndQuery(url) {
return url;
}
/**
* @param {NodeJS.Dict<string | string[]>} headers
* @returns {void}
*/
function normalizeRewrittenResponseHeaders(headers) {
for (const headerName of Object.keys(headers)) {
const lowerHeaderName = headerName.toLowerCase();
if (
lowerHeaderName === "content-length" ||
lowerHeaderName === "transfer-encoding" ||
lowerHeaderName === "content-encoding"
) {
delete headers[headerName];
}
}
}
/**
* @param {import("http").IncomingMessage} req
* @param {string} hostname
@ -218,17 +235,7 @@ 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.
for (const headerName of Object.keys(headers)) {
const lowerHeaderName = headerName.toLowerCase();
if (
lowerHeaderName === "content-length" ||
lowerHeaderName === "transfer-encoding" ||
lowerHeaderName === "content-encoding"
) {
delete headers[headerName];
}
}
normalizeRewrittenResponseHeaders(headers);
headers["content-length"] = String(buffer.byteLength);
res.writeHead(statusCode, headers);
res.end(buffer);