// QuickJS ships the ECMAScript standard library only, none of the web platform // globals that React's browser build assumes exist. This file is imported first // from the server entry so these shims are installed before React evaluates. if (typeof globalThis.TextEncoder === "undefined") { globalThis.TextEncoder = class TextEncoder { get encoding() { return "utf-8"; } encode(input = "") { const bytes = []; for (let i = 0; i < input.length; i++) { let code = input.codePointAt(i); if (code > 0xffff) i++; if (code < 0x80) { bytes.push(code); } else if (code < 0x800) { bytes.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f)); } else if (code < 0x10000) { bytes.push( 0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f), ); } else { bytes.push( 0xf0 | (code >> 18), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f), ); } } return new Uint8Array(bytes); } }; } if (typeof globalThis.TextDecoder === "undefined") { globalThis.TextDecoder = class TextDecoder { decode(bytes = new Uint8Array()) { let out = ""; for (let i = 0; i < bytes.length;) { const b = bytes[i]; let code; if (b < 0x80) { code = b; i += 1; } else if (b < 0xe0) { code = ((b & 0x1f) << 6) | (bytes[i + 1] & 0x3f); i += 2; } else if (b < 0xf0) { code = ((b & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f); i += 3; } else { code = ((b & 0x07) << 18) | ((bytes[i + 1] & 0x3f) << 12) | ((bytes[i + 2] & 0x3f) << 6) | (bytes[i + 3] & 0x3f); i += 4; } out += String.fromCodePoint(code); } return out; } }; } // `renderToString` is synchronous, so timers only need to exist, not be // accurate. Deferring to a microtask is enough for anything React schedules // during a string render. if (typeof globalThis.setTimeout === "undefined") { globalThis.setTimeout = (cb, _delay, ...args) => { Promise.resolve().then(() => cb(...args)); return 0; }; globalThis.clearTimeout = () => {}; } if (typeof globalThis.setImmediate === "undefined") { globalThis.setImmediate = (cb, ...args) => globalThis.setTimeout(cb, 0, ...args); globalThis.clearImmediate = () => {}; } // The `scheduler` package (pulled in via react-dom) constructs a MessageChannel // at module scope to schedule work. Nothing posts to it during a synchronous // string render, but it needs to exist. if (typeof globalThis.MessageChannel === "undefined") { class MessagePort { onmessage = null; _other = null; postMessage(data) { const target = this._other; Promise.resolve().then(() => target.onmessage?.({ data })); } start() {} close() {} } globalThis.MessageChannel = class MessageChannel { constructor() { this.port1 = new MessagePort(); this.port2 = new MessagePort(); this.port1._other = this.port2; this.port2._other = this.port1; } }; } if (typeof globalThis.queueMicrotask === "undefined") { globalThis.queueMicrotask = cb => { Promise.resolve().then(cb); }; }