From 357f01d90448d8565b650377bc7cabb351d991bd Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 17 Apr 2020 08:37:35 +0200 Subject: [PATCH] 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: https://github.com/socketio/engine.io-client/commit/3f3a6f991404ef601252193382d2d2029cff6c45 --- lib/globalThis.browser.js | 9 +++++++++ lib/globalThis.js | 1 + lib/transports/polling-jsonp.js | 13 ++----------- lib/transports/polling-xhr.js | 3 ++- lib/xmlhttprequest.js | 3 ++- package.json | 3 ++- 6 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 lib/globalThis.browser.js create mode 100644 lib/globalThis.js diff --git a/lib/globalThis.browser.js b/lib/globalThis.browser.js new file mode 100644 index 000000000..11b0f2813 --- /dev/null +++ b/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 + } +})(); diff --git a/lib/globalThis.js b/lib/globalThis.js new file mode 100644 index 000000000..e1465f205 --- /dev/null +++ b/lib/globalThis.js @@ -0,0 +1 @@ +module.exports = global; diff --git a/lib/transports/polling-jsonp.js b/lib/transports/polling-jsonp.js index 8e693375f..fe65705b9 100644 --- a/lib/transports/polling-jsonp.js +++ b/lib/transports/polling-jsonp.js @@ -4,6 +4,7 @@ var Polling = require('./polling'); var inherit = require('component-inherit'); +var globalThis = require('../globalThis'); /** * Module exports. @@ -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. * @@ -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 diff --git a/lib/transports/polling-xhr.js b/lib/transports/polling-xhr.js index 04da6d721..43f529c41 100755 --- a/lib/transports/polling-xhr.js +++ b/lib/transports/polling-xhr.js @@ -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. @@ -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); } } diff --git a/lib/xmlhttprequest.js b/lib/xmlhttprequest.js index 1413e6394..9d078c177 100644 --- a/lib/xmlhttprequest.js +++ b/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; @@ -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) { } } }; diff --git a/package.json b/package.json index 7e769f058..5ed1718ef 100644 --- a/package.json +++ b/package.json @@ -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",