Skip to content

Commit

Permalink
fix: prevent hoisting of the undefined global variable in `browser.…
Browse files Browse the repository at this point in the history
…js` (#1534)

Because of JS hoisting `var global` to the top of the file, `typeof
global` in `getGlobal()` will always be `undefined`.

By using a different variable name like `globalObject`, we are able to
read the "real" `typeof global` and get access to the global object that
way.
  • Loading branch information
valeriangalliat authored Nov 8, 2022
1 parent e218f8d commit 8bb6e31
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ var getGlobal = function () {
throw new Error('unable to locate global object');
}

var global = getGlobal();
var globalObject = getGlobal();

module.exports = exports = global.fetch;
module.exports = exports = globalObject.fetch;

// Needed for TypeScript and Webpack.
if (global.fetch) {
exports.default = global.fetch.bind(global);
if (globalObject.fetch) {
exports.default = globalObject.fetch.bind(global);
}

exports.Headers = global.Headers;
exports.Request = global.Request;
exports.Response = global.Response;
exports.Headers = globalObject.Headers;
exports.Request = globalObject.Request;
exports.Response = globalObject.Response;

0 comments on commit 8bb6e31

Please sign in to comment.