|
1 | 1 | import { File } from 'node:buffer'; |
2 | 2 | import crypto from 'node:crypto'; |
| 3 | +import { |
| 4 | + ByteLengthQueuingStrategy, |
| 5 | + CountQueuingStrategy, |
| 6 | + ReadableByteStreamController, |
| 7 | + ReadableStream, |
| 8 | + ReadableStreamBYOBReader, |
| 9 | + ReadableStreamBYOBRequest, |
| 10 | + ReadableStreamDefaultController, |
| 11 | + ReadableStreamDefaultReader, |
| 12 | + TransformStream, |
| 13 | + WritableStream, |
| 14 | + WritableStreamDefaultController, |
| 15 | + WritableStreamDefaultWriter, |
| 16 | +} from 'node:stream/web'; |
| 17 | +import { FormData, Headers, Request, Response, fetch, File as undiciFile } from 'undici'; |
3 | 18 |
|
4 | 19 | // NOTE: This file does not intend to polyfill everything that exists, its main goal is to make life easier |
5 | 20 | // for users deploying to runtime that do support these features. In the future, we hope for this file to disappear. |
6 | 21 |
|
| 22 | +// HACK (2023-08-18) Stackblitz does not support Node 18 yet, so we'll fake Node 16 support for some time until it's supported |
| 23 | +// TODO: Remove when Node 18 is supported on Stackblitz |
| 24 | +const isStackblitz = process.env.SHELL === '/bin/jsh' && process.versions.webcontainer != null; |
| 25 | + |
7 | 26 | export function apply() { |
| 27 | + if (isStackblitz) { |
| 28 | + const neededPolyfills = [ |
| 29 | + ByteLengthQueuingStrategy, |
| 30 | + CountQueuingStrategy, |
| 31 | + ReadableByteStreamController, |
| 32 | + ReadableStream, |
| 33 | + ReadableStreamBYOBReader, |
| 34 | + ReadableStreamBYOBRequest, |
| 35 | + ReadableStreamDefaultController, |
| 36 | + ReadableStreamDefaultReader, |
| 37 | + TransformStream, |
| 38 | + WritableStream, |
| 39 | + WritableStreamDefaultController, |
| 40 | + WritableStreamDefaultWriter, |
| 41 | + undiciFile, |
| 42 | + FormData, |
| 43 | + Headers, |
| 44 | + Request, |
| 45 | + Response, |
| 46 | + fetch, |
| 47 | + ]; |
| 48 | + |
| 49 | + for (let polyfillName of Object.keys(neededPolyfills)) { |
| 50 | + if (Object.hasOwnProperty.call(globalThis, polyfillName)) continue; |
| 51 | + |
| 52 | + // Add polyfill to globalThis |
| 53 | + Object.defineProperty(globalThis, polyfillName, { |
| 54 | + configurable: true, |
| 55 | + enumerable: true, |
| 56 | + writable: true, |
| 57 | + value: |
| 58 | + neededPolyfills[ |
| 59 | + (polyfillName === 'undiciFile' ? 'File' : polyfillName) as keyof typeof neededPolyfills |
| 60 | + ], |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | + |
8 | 65 | // Remove when Node 18 is dropped for Node 20 |
9 | 66 | if (!globalThis.crypto) { |
10 | 67 | Object.defineProperty(globalThis, 'crypto', { |
|
0 commit comments