fix(javascript): track shared ESM HTTP agent mutations

This commit is contained in:
RRFZ authored and Ysopwnable committed 2026-11-21 21:10:59 +01:00
1 parent f54cc2f3ac
commit d500638d5d
2 files changed
+54

No files matched your search

@@ -46,3 +46,51 @@ it("keeps browser requests and shifted arguments", () => {
),
).toHaveLength(3);
});
it.each([
[
"named HTTP",
"import { globalAgent, request } from 'node:http'; globalAgent.protocol = 'https:'; request('https://example.test/');",
],
[
"named HTTPS",
"import { globalAgent, get } from 'node:https'; globalAgent.protocol = 'http:'; get('http://example.test/');",
],
[
"namespace HTTP",
"import * as http from 'node:http'; http.globalAgent.protocol = 'https:'; http.request('https://example.test/');",
],
[
"namespace HTTPS",
"import * as https from 'node:https'; https.globalAgent.protocol = 'http:'; https.get('http://example.test/');",
],
])(
"keeps %s requests after the shared agent protocol changes",
(_import, source) => {
expect(analyzeJavaScriptSemantics(source).requestOperations).toHaveLength(
1,
);
},
);
it("tracks shared agent aliases and escapes through named exports", () => {
expect(
analyzeJavaScriptSemantics(`
import { globalAgent, request } from "node:http";
const alias = globalAgent;
configure(alias);
request("https://example.test/");
`).requestOperations,
).toHaveLength(1);
});
it("ignores later agent mutations and unrelated properties", () => {
expect(
analyzeJavaScriptSemantics(`
import { globalAgent, request } from "node:http";
globalAgent.label = "metadata";
request("https://example.test/");
globalAgent.protocol = "https:";
`).requestOperations,
).toEqual([]);
});
@@ -1972,6 +1972,12 @@ export const semanticMutableBuiltinOrigin = <Key = never>(
return { specifier, path };
if (specifier === "process" && (path[0] === "env" || path[0] === "argv"))
return { specifier, path };
// Named and namespace exports still expose the same mutable Agent object.
if (
(specifier === "http" || specifier === "https") &&
path[0] === "globalAgent"
)
return { specifier, path };
return specifier === "fs" && path[0] === "promises"
? { specifier, path }
: undefined;