Skip to content

Commit 060f47e

Browse files
dummdidummRich-Harrisbenmccann
authored
fix: only apply some polyfills below node 18.11 (#10920)
* fix: only apply some polyfills below node 18.11 related to #10918 * Update packages/kit/src/exports/node/polyfills.js Co-authored-by: Ben McCann <[email protected]> * be more defensive * be more defensive --------- Co-authored-by: Rich Harris <[email protected]> Co-authored-by: Ben McCann <[email protected]>
1 parent 000c193 commit 060f47e

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

.changeset/mean-cheetahs-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: only apply some polyfills below node 18.11

packages/kit/src/exports/node/polyfills.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import buffer from 'node:buffer';
33
import { webcrypto as crypto } from 'node:crypto';
44
import { fetch, Response, Request, Headers, FormData, File as UndiciFile } from 'undici';
55

6+
/** @type {Record<string, any>} */
7+
const globals_post_node_18_11 = {
8+
crypto
9+
};
10+
611
// @ts-expect-error
712
const File = buffer.File ?? UndiciFile;
813

914
/** @type {Record<string, any>} */
10-
const globals = {
15+
// TODO: remove this once we only support Node 18.11+ (the version multipart/form-data was added)
16+
const globals_pre_node_18_11 = {
1117
crypto,
1218
fetch,
1319
Response,
@@ -21,16 +27,26 @@ const globals = {
2127
};
2228

2329
// exported for dev/preview and node environments
24-
// TODO: remove this once we only support Node 18.11+ (the version multipart/form-data was added)
2530
/**
2631
* Make various web APIs available as globals:
2732
* - `crypto`
28-
* - `fetch`
29-
* - `Headers`
30-
* - `Request`
31-
* - `Response`
33+
* - `fetch` (only in node < 18.11)
34+
* - `Headers` (only in node < 18.11)
35+
* - `Request` (only in node < 18.11)
36+
* - `Response` (only in node < 18.11)
3237
*/
3338
export function installPolyfills() {
39+
// Be defensive (we don't know in which environments this is called) and always apply if something goes wrong
40+
let globals = globals_pre_node_18_11;
41+
try {
42+
const version = process.versions.node.split('.').map((n) => parseInt(n, 10));
43+
if ((version[0] === 18 && version[1] >= 11) || version[0] > 18) {
44+
globals = globals_post_node_18_11;
45+
}
46+
} catch (e) {
47+
// ignore
48+
}
49+
3450
for (const name in globals) {
3551
Object.defineProperty(globalThis, name, {
3652
enumerable: true,

0 commit comments

Comments
 (0)