diff --git a/src/platform/deno.ts b/src/platform/deno.ts index bec09d0a..82a39425 100644 --- a/src/platform/deno.ts +++ b/src/platform/deno.ts @@ -105,7 +105,8 @@ Platform.load({ Headers: globalThis.Headers, FormData: globalThis.FormData, File: globalThis.File, - ReadableStream: globalThis.ReadableStream + ReadableStream: globalThis.ReadableStream, + CustomEvent: globalThis.CustomEvent }); export * from './lib.js'; diff --git a/src/platform/node.ts b/src/platform/node.ts index 4ba8414c..5b95e018 100644 --- a/src/platform/node.ts +++ b/src/platform/node.ts @@ -17,6 +17,7 @@ import os from 'os'; import fs from 'fs/promises'; import { readFileSync } from 'fs'; import DOMParser from './polyfills/server-dom.js'; +import CustomEvent from './polyfills/node-custom-event.js'; import { fileURLToPath } from 'url'; import evaluate from './jsruntime/jinter.js'; @@ -123,7 +124,8 @@ Platform.load({ Headers: Headers as unknown as typeof globalThis.Headers, FormData: FormData as unknown as typeof globalThis.FormData, File: File as unknown as typeof globalThis.File, - ReadableStream: ReadableStream as unknown as typeof globalThis.ReadableStream + ReadableStream: ReadableStream as unknown as typeof globalThis.ReadableStream, + CustomEvent: CustomEvent as unknown as typeof globalThis.CustomEvent }); export * from './lib.js'; diff --git a/src/platform/polyfills/node-custom-event.ts b/src/platform/polyfills/node-custom-event.ts new file mode 100644 index 00000000..879eee17 --- /dev/null +++ b/src/platform/polyfills/node-custom-event.ts @@ -0,0 +1,13 @@ +// See https://github.com/nodejs/node/issues/40678#issuecomment-1126944677 +export default class CustomEvent extends Event { + #detail; + + constructor(type: string, options?: CustomEventInit) { + super(type, options); + this.#detail = options?.detail ?? null; + } + + get detail() { + return this.#detail; + } +} \ No newline at end of file diff --git a/src/platform/web.ts b/src/platform/web.ts index f1727d66..e43d66be 100644 --- a/src/platform/web.ts +++ b/src/platform/web.ts @@ -115,7 +115,8 @@ Platform.load({ Headers: globalThis.Headers, FormData: globalThis.FormData, File: globalThis.File, - ReadableStream: globalThis.ReadableStream + ReadableStream: globalThis.ReadableStream, + CustomEvent: globalThis.CustomEvent }); export * from './lib.js'; diff --git a/src/types/PlatformShim.ts b/src/types/PlatformShim.ts index 22b14e41..901053f2 100644 --- a/src/types/PlatformShim.ts +++ b/src/types/PlatformShim.ts @@ -27,6 +27,7 @@ interface PlatformShim { FormData: typeof FormData; File: typeof File; ReadableStream: typeof ReadableStream; + CustomEvent: typeof CustomEvent; } export default PlatformShim; \ No newline at end of file diff --git a/src/utils/EventEmitterLike.ts b/src/utils/EventEmitterLike.ts index 56926c02..90c74c95 100644 --- a/src/utils/EventEmitterLike.ts +++ b/src/utils/EventEmitterLike.ts @@ -1,22 +1,4 @@ -// Polyfill CustomEvents on node -if (!Reflect.has(globalThis, 'CustomEvent')) { - - // See https://github.com/nodejs/node/issues/40678#issuecomment-1126944677 - class CustomEvent extends Event { - #detail; - - constructor(type: string, options?: CustomEventInit) { - super(type, options); - this.#detail = options?.detail ?? null; - } - - get detail() { - return this.#detail; - } - } - - Reflect.set(globalThis, 'CustomEvent', CustomEvent); -} +import { Platform } from './Utils.js'; export default class EventEmitterLike extends EventTarget { #legacy_listeners = new Map<(...args: any[]) => void, EventListener>(); @@ -26,13 +8,13 @@ export default class EventEmitterLike extends EventTarget { } emit(type: string, ...args: any[]) { - const event = new CustomEvent(type, { detail: args }); + const event = new Platform.shim.CustomEvent(type, { detail: args }); this.dispatchEvent(event); } on(type: string, listener: (...args: any[]) => void) { const wrapper: EventListener = (ev) => { - if (ev instanceof CustomEvent) { + if (ev instanceof Platform.shim.CustomEvent) { listener(...ev.detail); } else { listener(ev); @@ -44,7 +26,7 @@ export default class EventEmitterLike extends EventTarget { once(type: string, listener: (...args: any[]) => void) { const wrapper: EventListener = (ev) => { - if (ev instanceof CustomEvent) { + if (ev instanceof Platform.shim.CustomEvent) { listener(...ev.detail); } else { listener(ev);