Skip to content

Commit 357f01d

Browse files
fix: use globalThis polyfill instead of self/global
In order to fix the "self is not defined" issues in Android 8 and React Native. Backported from master: 3f3a6f9
1 parent ccc9337 commit 357f01d

File tree

6 files changed

+18
-14
lines changed

6 files changed

+18
-14
lines changed

lib/globalThis.browser.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = (function () {
2+
if (typeof self !== 'undefined') {
3+
return self;
4+
} else if (typeof window !== 'undefined') {
5+
return window;
6+
} else {
7+
return Function('return this')(); // eslint-disable-line no-new-func
8+
}
9+
})();

lib/globalThis.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = global;

lib/transports/polling-jsonp.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
var Polling = require('./polling');
66
var inherit = require('component-inherit');
7+
var globalThis = require('../globalThis');
78

89
/**
910
* Module exports.
@@ -30,15 +31,6 @@ var callbacks;
3031

3132
function empty () { }
3233

33-
/**
34-
* Until https://github.com/tc39/proposal-global is shipped.
35-
*/
36-
function glob () {
37-
return typeof self !== 'undefined' ? self
38-
: typeof window !== 'undefined' ? window
39-
: typeof global !== 'undefined' ? global : {};
40-
}
41-
4234
/**
4335
* JSONP Polling constructor.
4436
*
@@ -55,8 +47,7 @@ function JSONPPolling (opts) {
5547
// we do this here (lazily) to avoid unneeded global pollution
5648
if (!callbacks) {
5749
// we need to consider multiple engines in the same page
58-
var global = glob();
59-
callbacks = global.___eio = (global.___eio || []);
50+
callbacks = globalThis.___eio = (globalThis.___eio || []);
6051
}
6152

6253
// callback identifier

lib/transports/polling-xhr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Polling = require('./polling');
99
var Emitter = require('component-emitter');
1010
var inherit = require('component-inherit');
1111
var debug = require('debug')('engine.io-client:polling-xhr');
12+
var globalThis = require('../globalThis');
1213

1314
/**
1415
* Module exports.
@@ -403,7 +404,7 @@ if (typeof document !== 'undefined') {
403404
if (typeof attachEvent === 'function') {
404405
attachEvent('onunload', unloadHandler);
405406
} else if (typeof addEventListener === 'function') {
406-
var terminationEvent = 'onpagehide' in self ? 'pagehide' : 'unload';
407+
var terminationEvent = 'onpagehide' in globalThis ? 'pagehide' : 'unload';
407408
addEventListener(terminationEvent, unloadHandler, false);
408409
}
409410
}

lib/xmlhttprequest.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// browser shim for xmlhttprequest module
22

33
var hasCORS = require('has-cors');
4+
var globalThis = require('./globalThis');
45

56
module.exports = function (opts) {
67
var xdomain = opts.xdomain;
@@ -31,7 +32,7 @@ module.exports = function (opts) {
3132

3233
if (!xdomain) {
3334
try {
34-
return new self[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
35+
return new globalThis[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
3536
} catch (e) { }
3637
}
3738
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
},
7070
"browser": {
7171
"ws": false,
72-
"xmlhttprequest-ssl": "./lib/xmlhttprequest.js"
72+
"xmlhttprequest-ssl": "./lib/xmlhttprequest.js",
73+
"./lib/globalThis.js": "./lib/globalThis.browser.js"
7374
},
7475
"repository": {
7576
"type": "git",

0 commit comments

Comments
 (0)