Skip to content

Commit

Permalink
do not apply polyfills if not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Oct 24, 2024
1 parent 7cb62b7 commit ff1cc54
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src-node/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@ import { EventEmitter } from 'events';
import { Blob } from 'buffer';
import fetch from 'node-fetch';

global.Blob = Blob;

global.fetch = fetch;
if (!('Blob' in global)) {
global.Blob = Blob;
}
if (!('fetch' in global)) {
global.fetch = fetch;
}

// mimic window events
const emitter = new EventEmitter();
global.addEventListener = emitter.addListener.bind(emitter);
global.removeEventListener = emitter.removeListener.bind(emitter);
global.dispatchEvent = (event) => emitter.emit(event.type, event);
global.CustomEvent = class {
constructor(type, payload) {
this.type = type;
Object.assign(this, payload);
}
};
if (
!('addEventListener' in global) &&
!('removeEventListener' in global) &&
!('dispatchEvent' in global)
) {
global.addEventListener = emitter.addListener.bind(emitter);
global.removeEventListener = emitter.removeListener.bind(emitter);
global.dispatchEvent = emitter.emit.bind(emitter);
}
if (!('CustomEvent' in global)) {
global.CustomEvent = class {
constructor(type, payload) {
this.type = type;
Object.assign(this, payload);
}
};
}

0 comments on commit ff1cc54

Please sign in to comment.