Skip to content

Commit

Permalink
Merge pull request #742 from streamich/fix-global
Browse files Browse the repository at this point in the history
fix: 🐛 use globalThis defensively
  • Loading branch information
streamich authored Sep 2, 2021
2 parents 69f5c49 + eed6bbf commit 50832df
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/internal/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ function makeNodeError(Base) {
};
}

class AssertionError extends globalThis.Error {
const g = typeof globalThis !== 'undefined' ? globalThis : global;

class AssertionError extends g.Error {
generatedMessage: any;
name: any;
code: any;
Expand Down Expand Up @@ -74,9 +76,9 @@ function E(sym, val) {
messages[sym] = typeof val === 'function' ? val : String(val);
}

export const Error = makeNodeError(globalThis.Error);
export const TypeError = makeNodeError(globalThis.TypeError);
export const RangeError = makeNodeError(globalThis.RangeError);
export const Error = makeNodeError(g.Error);
export const TypeError = makeNodeError(g.TypeError);
export const RangeError = makeNodeError(g.RangeError);

export {
message,
Expand Down
4 changes: 2 additions & 2 deletions src/setImmediate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type TSetImmediate = (callback: (...args) => void, args?) => void;
let _setImmediate: TSetImmediate;

if (typeof setImmediate === 'function') _setImmediate = setImmediate.bind(globalThis);
else _setImmediate = setTimeout.bind(globalThis);
if (typeof setImmediate === 'function') _setImmediate = setImmediate.bind(typeof globalThis !== 'undefined' ? globalThis : global);
else _setImmediate = setTimeout.bind(typeof globalThis !== 'undefined' ? globalThis : global);

export default _setImmediate as TSetImmediate;
2 changes: 1 addition & 1 deletion src/setTimeoutUnref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type TSetTimeout = (callback: (...args) => void, time?: number, args?: an
* only in Node's environment it will "unref" its macro task.
*/
function setTimeoutUnref(callback, time?, args?): object {
const ref = setTimeout.apply(globalThis, arguments);
const ref = setTimeout.apply(typeof globalThis !== 'undefined' ? globalThis : global, arguments);
if (ref && typeof ref === 'object' && typeof ref.unref === 'function') ref.unref();
return ref;
}
Expand Down
2 changes: 1 addition & 1 deletion src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2223,7 +2223,7 @@ export class StatWatcher extends EventEmitter {

start(path: string, persistent: boolean = true, interval: number = 5007) {
this.filename = pathToFilename(path);
this.setTimeout = persistent ? setTimeout.bind(globalThis) : setTimeoutUnref;
this.setTimeout = persistent ? setTimeout.bind(typeof globalThis !== 'undefined' ? globalThis : global) : setTimeoutUnref;
this.interval = interval;
this.prev = this.vol.statSync(this.filename);
this.loop();
Expand Down

0 comments on commit 50832df

Please sign in to comment.