Skip to content

Commit 3f3a6f9

Browse files
fix: use globalThis polyfill instead of self/global (#634)
In order to fix the "self is not defined" issues in Android 8 and React Native. Fixes #611, #626 Closes #627
1 parent 27fa694 commit 3f3a6f9

File tree

9 files changed

+38
-38
lines changed

9 files changed

+38
-38
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 = (() => {
2+
if (typeof self !== "undefined") {
3+
return self;
4+
} else if (typeof window !== "undefined") {
5+
return window;
6+
} else {
7+
return Function("return this")();
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Polling = require("./polling");
2+
const globalThis = require("../globalThis");
23

34
const rNewline = /\n/g;
45
const rEscapedNewline = /\\n/g;
@@ -31,7 +32,7 @@ class JSONPPolling extends Polling {
3132
// we do this here (lazily) to avoid unneeded global pollution
3233
if (!callbacks) {
3334
// we need to consider multiple engines in the same page
34-
callbacks = global.___eio = global.___eio || [];
35+
callbacks = globalThis.___eio = globalThis.___eio || [];
3536
}
3637

3738
// callback identifier

lib/transports/polling-xhr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const XMLHttpRequest = require("xmlhttprequest-ssl");
44
const Polling = require("./polling");
55
const Emitter = require("component-emitter");
66
const { pick } = require("../util");
7+
const globalThis = require("../globalThis");
78

89
const debug = require("debug")("engine.io-client:polling-xhr");
910

@@ -361,7 +362,7 @@ if (typeof document !== "undefined") {
361362
if (typeof attachEvent === "function") {
362363
attachEvent("onunload", unloadHandler);
363364
} else if (typeof addEventListener === "function") {
364-
const terminationEvent = "onpagehide" in self ? "pagehide" : "unload";
365+
const terminationEvent = "onpagehide" in globalThis ? "pagehide" : "unload";
365366
addEventListener(terminationEvent, unloadHandler, false);
366367
}
367368
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const globalThis = require("../globalThis");
2+
3+
module.exports = {
4+
WebSocket: globalThis.WebSocket || globalThis.MozWebSocket,
5+
usingBrowserWebSocket: true
6+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
WebSocket: require("ws"),
3+
usingBrowserWebSocket: false
4+
};

lib/transports/websocket.js

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,10 @@ const parser = require("engine.io-parser");
33
const parseqs = require("parseqs");
44
const yeast = require("yeast");
55
const { pick } = require("../util");
6+
const { WebSocket, usingBrowserWebSocket } = require("./websocket-constructor");
67

78
const debug = require("debug")("engine.io-client:websocket");
89

9-
let BrowserWebSocket, NodeWebSocket;
10-
11-
if (typeof WebSocket !== "undefined") {
12-
BrowserWebSocket = WebSocket;
13-
} else if (typeof self !== "undefined") {
14-
BrowserWebSocket = self.WebSocket || self.MozWebSocket;
15-
}
16-
17-
if (typeof window === "undefined") {
18-
try {
19-
NodeWebSocket = require("ws");
20-
} catch (e) {}
21-
}
22-
23-
/**
24-
* Get either the `WebSocket` or `MozWebSocket` globals
25-
* in the browser or try to resolve WebSocket-compatible
26-
* interface exposed by `ws` for Node-like environment.
27-
*/
28-
29-
let WebSocketImpl = BrowserWebSocket || NodeWebSocket;
30-
3110
class WS extends Transport {
3211
/**
3312
* WebSocket transport constructor.
@@ -42,10 +21,6 @@ class WS extends Transport {
4221
if (forceBase64) {
4322
this.supportsBinary = false;
4423
}
45-
this.usingBrowserWebSocket = BrowserWebSocket && !opts.forceNode;
46-
if (!this.usingBrowserWebSocket) {
47-
WebSocketImpl = NodeWebSocket;
48-
}
4924
// WebSockets support binary
5025
this.supportsBinary = true;
5126
}
@@ -91,11 +66,11 @@ class WS extends Transport {
9166

9267
try {
9368
this.ws =
94-
this.usingBrowserWebSocket && !this.opts.isReactNative
69+
usingBrowserWebSocket && !this.opts.isReactNative
9570
? protocols
96-
? new WebSocketImpl(uri, protocols)
97-
: new WebSocketImpl(uri)
98-
: new WebSocketImpl(uri, protocols, opts);
71+
? new WebSocket(uri, protocols)
72+
: new WebSocket(uri)
73+
: new WebSocket(uri, protocols, opts);
9974
} catch (err) {
10075
return this.emit("error", err);
10176
}
@@ -156,7 +131,7 @@ class WS extends Transport {
156131
parser.encodePacket(packet, self.supportsBinary, function(data) {
157132
// always create a new object (GH-437)
158133
const opts = {};
159-
if (!self.usingBrowserWebSocket) {
134+
if (!usingBrowserWebSocket) {
160135
if (packet.options) {
161136
opts.compress = packet.options.compress;
162137
}
@@ -176,7 +151,7 @@ class WS extends Transport {
176151
// have a chance of informing us about it yet, in that case send will
177152
// throw an error
178153
try {
179-
if (self.usingBrowserWebSocket) {
154+
if (usingBrowserWebSocket) {
180155
// TypeError is thrown when passing the second argument on Safari
181156
self.ws.send(data);
182157
} else {
@@ -278,8 +253,8 @@ class WS extends Transport {
278253
*/
279254
check() {
280255
return (
281-
!!WebSocketImpl &&
282-
!("__initialize" in WebSocketImpl && this.name === WS.prototype.name)
256+
!!WebSocket &&
257+
!("__initialize" in WebSocket && this.name === WS.prototype.name)
283258
);
284259
}
285260
}

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
const hasCORS = require("has-cors");
4+
const globalThis = require("./globalThis");
45

56
module.exports = function(opts) {
67
const 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")](
35+
return new globalThis[["Active"].concat("Object").join("X")](
3536
"Microsoft.XMLHTTP"
3637
);
3738
} catch (e) {}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@
6565
},
6666
"browser": {
6767
"ws": false,
68-
"xmlhttprequest-ssl": "./lib/xmlhttprequest.js"
68+
"xmlhttprequest-ssl": "./lib/xmlhttprequest.js",
69+
"./lib/transports/websocket-constructor.js": "./lib/transports/websocket-constructor.browser.js",
70+
"./lib/globalThis.js": "./lib/globalThis.browser.js"
6971
},
7072
"repository": {
7173
"type": "git",

0 commit comments

Comments
 (0)