Skip to content

Commit 5c85f1f

Browse files
authored
fix(fetch): prefer native fetch to avoid node-fetch premature close (#742)
1 parent c7351cf commit 5c85f1f

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

lib/fetch-polyfill.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ const patchedFetch = (...args: Parameters<typeof crossFetch>) => {
1919
return crossFetch(...args);
2020
};
2121

22-
export default patchedFetch as unknown as typeof fetch;
22+
// node-fetch@2 (bundled by cross-fetch) throws a false ERR_STREAM_PREMATURE_CLOSE on
23+
// keep-alive responses on Node >= 22.23.0 / 24.17.0 (nodejs/node#63989, the CVE-2026-48931
24+
// http.Agent fix). Node's built-in fetch (undici, Node >= 18) is unaffected, so prefer it
25+
// when present and fall back to cross-fetch (node-fetch) only on older runtimes.
26+
const polyfillFetch =
27+
typeof globalThis.fetch === 'function'
28+
? (...args: Parameters<typeof globalThis.fetch>) => globalThis.fetch(...args)
29+
: patchedFetch;
30+
31+
export default polyfillFetch as unknown as typeof fetch;

0 commit comments

Comments
 (0)