Skip to content

Commit

Permalink
fix: only apply some polyfills below node 18.11
Browse files Browse the repository at this point in the history
related to #10918
  • Loading branch information
dummdidumm committed Oct 25, 2023
1 parent 000c193 commit 6cfd8c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/mean-cheetahs-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: only apply some polyfills below node 18.11
22 changes: 16 additions & 6 deletions packages/kit/src/exports/node/polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ import buffer from 'node:buffer';
import { webcrypto as crypto } from 'node:crypto';
import { fetch, Response, Request, Headers, FormData, File as UndiciFile } from 'undici';

/** @type {Record<string, any>} */
const globals_post_node_18_11 = {
crypto
};

// @ts-expect-error
const File = buffer.File ?? UndiciFile;

/** @type {Record<string, any>} */
const globals = {
const globals_pre_node_18_11 = {
crypto,
fetch,
Response,
Expand All @@ -21,16 +26,21 @@ const globals = {
};

// exported for dev/preview and node environments
// TODO: remove this once we only support Node 18.11+ (the version multipart/form-data was added)
/**
* Make various web APIs available as globals:
* - `crypto`
* - `fetch`
* - `Headers`
* - `Request`
* - `Response`
* - `fetch` (only in node 18.11-)
* - `Headers` (only in node 18.11-)
* - `Request` (only in node 18.11-)
* - `Response` (only in node 18.11-)
*/
export function installPolyfills() {
const version = process.versions.node.split('.').map((n) => parseInt(n, 10));
const globals =
(version[0] === 18 && version[1] >= 11) || version[0] > 18
? globals_post_node_18_11
: globals_pre_node_18_11;

for (const name in globals) {
Object.defineProperty(globalThis, name, {
enumerable: true,
Expand Down

0 comments on commit 6cfd8c4

Please sign in to comment.