Skip to content

Commit

Permalink
fix: use globalThis polyfill instead of self/global
Browse files Browse the repository at this point in the history
In order to fix the "self is not defined" issues in Android 8 and React
Native.

Backported from master: 3f3a6f9
  • Loading branch information
darrachequesne committed Apr 17, 2020
1 parent ccc9337 commit 357f01d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
9 changes: 9 additions & 0 deletions lib/globalThis.browser.js
@@ -0,0 +1,9 @@
module.exports = (function () {
if (typeof self !== 'undefined') {
return self;
} else if (typeof window !== 'undefined') {
return window;
} else {
return Function('return this')(); // eslint-disable-line no-new-func
}
})();
1 change: 1 addition & 0 deletions lib/globalThis.js
@@ -0,0 +1 @@
module.exports = global;
13 changes: 2 additions & 11 deletions lib/transports/polling-jsonp.js
Expand Up @@ -4,6 +4,7 @@

var Polling = require('./polling');
var inherit = require('component-inherit');
var globalThis = require('../globalThis');

/**
* Module exports.
Expand All @@ -30,15 +31,6 @@ var callbacks;

function empty () { }

/**
* Until https://github.com/tc39/proposal-global is shipped.
*/
function glob () {
return typeof self !== 'undefined' ? self
: typeof window !== 'undefined' ? window
: typeof global !== 'undefined' ? global : {};
}

/**
* JSONP Polling constructor.
*
Expand All @@ -55,8 +47,7 @@ function JSONPPolling (opts) {
// we do this here (lazily) to avoid unneeded global pollution
if (!callbacks) {
// we need to consider multiple engines in the same page
var global = glob();
callbacks = global.___eio = (global.___eio || []);
callbacks = globalThis.___eio = (globalThis.___eio || []);
}

// callback identifier
Expand Down
3 changes: 2 additions & 1 deletion lib/transports/polling-xhr.js
Expand Up @@ -9,6 +9,7 @@ var Polling = require('./polling');
var Emitter = require('component-emitter');
var inherit = require('component-inherit');
var debug = require('debug')('engine.io-client:polling-xhr');
var globalThis = require('../globalThis');

/**
* Module exports.
Expand Down Expand Up @@ -403,7 +404,7 @@ if (typeof document !== 'undefined') {
if (typeof attachEvent === 'function') {
attachEvent('onunload', unloadHandler);
} else if (typeof addEventListener === 'function') {
var terminationEvent = 'onpagehide' in self ? 'pagehide' : 'unload';
var terminationEvent = 'onpagehide' in globalThis ? 'pagehide' : 'unload';
addEventListener(terminationEvent, unloadHandler, false);
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/xmlhttprequest.js
@@ -1,6 +1,7 @@
// browser shim for xmlhttprequest module

var hasCORS = require('has-cors');
var globalThis = require('./globalThis');

module.exports = function (opts) {
var xdomain = opts.xdomain;
Expand Down Expand Up @@ -31,7 +32,7 @@ module.exports = function (opts) {

if (!xdomain) {
try {
return new self[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
return new globalThis[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
} catch (e) { }
}
};
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -69,7 +69,8 @@
},
"browser": {
"ws": false,
"xmlhttprequest-ssl": "./lib/xmlhttprequest.js"
"xmlhttprequest-ssl": "./lib/xmlhttprequest.js",
"./lib/globalThis.js": "./lib/globalThis.browser.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 357f01d

Please sign in to comment.