|
| 1 | +// Node.js 16 polyfills for modern web APIs |
| 2 | +// This file is loaded before anything else to ensure compatibility with Node.js 16 |
| 3 | + |
| 4 | +// Make the file work with ESM |
| 5 | +import { createRequire } from 'module'; |
| 6 | +const require = createRequire(import.meta.url); |
| 7 | + |
| 8 | +// Polyfill for DOMException |
| 9 | +if (typeof globalThis.DOMException === 'undefined') { |
| 10 | + globalThis.DOMException = class DOMException extends Error { |
| 11 | + constructor(message, name) { |
| 12 | + super(message); |
| 13 | + this.name = name || 'Error'; |
| 14 | + this.code = 0; |
| 15 | + |
| 16 | + // Common DOMException codes |
| 17 | + if (name === 'AbortError') this.code = 20; |
| 18 | + if (name === 'InvalidStateError') this.code = 11; |
| 19 | + if (name === 'NetworkError') this.code = 19; |
| 20 | + if (name === 'NotFoundError') this.code = 8; |
| 21 | + if (name === 'NotSupportedError') this.code = 9; |
| 22 | + if (name === 'SecurityError') this.code = 18; |
| 23 | + if (name === 'SyntaxError') this.code = 12; |
| 24 | + if (name === 'TypeMismatchError') this.code = 17; |
| 25 | + } |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +// Polyfill for ReadableStream |
| 30 | +if (typeof globalThis.ReadableStream === 'undefined') { |
| 31 | + const { Readable } = require('stream'); |
| 32 | + |
| 33 | + globalThis.ReadableStream = class ReadableStream { |
| 34 | + constructor(underlyingSource = {}) { |
| 35 | + this._readable = new Readable({ |
| 36 | + read: underlyingSource.pull, |
| 37 | + objectMode: true |
| 38 | + }); |
| 39 | + |
| 40 | + if (underlyingSource.start) { |
| 41 | + underlyingSource.start(this); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + getReader() { |
| 46 | + return { |
| 47 | + read: async () => { |
| 48 | + return new Promise((resolve) => { |
| 49 | + this._readable.once('data', (chunk) => { |
| 50 | + resolve({ value: chunk, done: false }); |
| 51 | + }); |
| 52 | + this._readable.once('end', () => { |
| 53 | + resolve({ value: undefined, done: true }); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }, |
| 57 | + releaseLock: () => {} |
| 58 | + }; |
| 59 | + } |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +// Polyfill for Blob |
| 64 | +if (typeof globalThis.Blob === 'undefined') { |
| 65 | + const { Buffer } = require('buffer'); |
| 66 | + |
| 67 | + globalThis.Blob = class Blob { |
| 68 | + constructor(array, options = {}) { |
| 69 | + this.parts = array; |
| 70 | + this.type = options.type || ''; |
| 71 | + |
| 72 | + // Calculate size |
| 73 | + this.size = array.reduce((acc, part) => { |
| 74 | + const buf = part instanceof Buffer ? part : Buffer.from(String(part)); |
| 75 | + return acc + buf.length; |
| 76 | + }, 0); |
| 77 | + } |
| 78 | + |
| 79 | + // Basic implementation of slice |
| 80 | + slice(start, end, contentType) { |
| 81 | + // For simplicity, we're not implementing the full logic |
| 82 | + return new Blob([], { type: contentType || this.type }); |
| 83 | + } |
| 84 | + |
| 85 | + // Add other methods as needed |
| 86 | + async text() { |
| 87 | + const buffers = []; |
| 88 | + for (const part of this.parts) { |
| 89 | + const buf = part instanceof Buffer ? part : Buffer.from(String(part)); |
| 90 | + buffers.push(buf); |
| 91 | + } |
| 92 | + return Buffer.concat(buffers).toString('utf-8'); |
| 93 | + } |
| 94 | + |
| 95 | + async arrayBuffer() { |
| 96 | + const buffers = []; |
| 97 | + for (const part of this.parts) { |
| 98 | + const buf = part instanceof Buffer ? part : Buffer.from(String(part)); |
| 99 | + buffers.push(buf); |
| 100 | + } |
| 101 | + return Buffer.concat(buffers).buffer; |
| 102 | + } |
| 103 | + }; |
| 104 | +} |
| 105 | + |
| 106 | +// Add additional web API polyfills that undici might need |
| 107 | +if (typeof globalThis.WritableStream === 'undefined') { |
| 108 | + const { Writable } = require('stream'); |
| 109 | + |
| 110 | + globalThis.WritableStream = class WritableStream { |
| 111 | + constructor(underlyingSink = {}) { |
| 112 | + this._writable = new Writable({ |
| 113 | + write: (chunk, encoding, callback) => { |
| 114 | + if (underlyingSink.write) { |
| 115 | + Promise.resolve(underlyingSink.write(chunk)) |
| 116 | + .then(() => callback(), callback); |
| 117 | + } else { |
| 118 | + callback(); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + if (underlyingSink.start) { |
| 124 | + underlyingSink.start(this); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + getWriter() { |
| 129 | + return { |
| 130 | + write: async (chunk) => { |
| 131 | + return new Promise((resolve, reject) => { |
| 132 | + this._writable.write(chunk, (err) => { |
| 133 | + if (err) reject(err); |
| 134 | + else resolve(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }, |
| 138 | + close: async () => { |
| 139 | + return new Promise((resolve) => { |
| 140 | + this._writable.end(() => resolve()); |
| 141 | + }); |
| 142 | + }, |
| 143 | + releaseLock: () => {} |
| 144 | + }; |
| 145 | + } |
| 146 | + }; |
| 147 | +} |
| 148 | + |
| 149 | +// More web polyfills |
| 150 | +if (typeof globalThis.FormData === 'undefined') { |
| 151 | + globalThis.FormData = class FormData { |
| 152 | + constructor() { |
| 153 | + this._data = new Map(); |
| 154 | + } |
| 155 | + |
| 156 | + append(name, value, filename) { |
| 157 | + this._data.set(name, { value, filename }); |
| 158 | + } |
| 159 | + |
| 160 | + delete(name) { |
| 161 | + this._data.delete(name); |
| 162 | + } |
| 163 | + |
| 164 | + get(name) { |
| 165 | + return this._data.get(name)?.value; |
| 166 | + } |
| 167 | + |
| 168 | + has(name) { |
| 169 | + return this._data.has(name); |
| 170 | + } |
| 171 | + |
| 172 | + set(name, value, filename) { |
| 173 | + this.append(name, value, filename); |
| 174 | + } |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +// Export to ensure this file is properly loaded as a module |
| 179 | +export default {}; |
0 commit comments