Skip to content

Commit 99bcc62

Browse files
[fix] Remove any reference to the global variable
Related: socketio/socket.io-client#1166
1 parent 122111a commit 99bcc62

14 files changed

+46
-55
lines changed

Diff for: lib/socket.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function Socket (uri, opts) {
4545
}
4646

4747
this.secure = null != opts.secure ? opts.secure
48-
: (global.location && 'https:' === location.protocol);
48+
: (typeof location !== 'undefined' && 'https:' === location.protocol);
4949

5050
if (opts.hostname && !opts.port) {
5151
// if no port is specified manually, use the protocol default
@@ -54,8 +54,8 @@ function Socket (uri, opts) {
5454

5555
this.agent = opts.agent || false;
5656
this.hostname = opts.hostname ||
57-
(global.location ? location.hostname : 'localhost');
58-
this.port = opts.port || (global.location && location.port
57+
(typeof location !== 'undefined' ? location.hostname : 'localhost');
58+
this.port = opts.port || (typeof location !== 'undefined' && location.port
5959
? location.port
6060
: (this.secure ? 443 : 80));
6161
this.query = opts.query || {};
@@ -98,8 +98,7 @@ function Socket (uri, opts) {
9898
this.isReactNative = (typeof navigator !== 'undefined' && typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative');
9999

100100
// other options for Node.js or ReactNative client
101-
var freeGlobal = typeof global === 'object' && global;
102-
if (freeGlobal.global === freeGlobal || this.isReactNative) {
101+
if (typeof self === 'undefined' || this.isReactNative) {
103102
if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
104103
this.extraHeaders = opts.extraHeaders;
105104
}

Diff for: lib/transports/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function polling (opts) {
2727
var xs = false;
2828
var jsonp = false !== opts.jsonp;
2929

30-
if (global.location) {
30+
if (typeof location !== 'undefined') {
3131
var isSSL = 'https:' === location.protocol;
3232
var port = location.port;
3333

Diff for: lib/transports/polling-jsonp.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* Module requirements.
43
*/
@@ -47,8 +46,8 @@ function JSONPPolling (opts) {
4746
// we do this here (lazily) to avoid unneeded global pollution
4847
if (!callbacks) {
4948
// we need to consider multiple engines in the same page
50-
if (!global.___eio) global.___eio = [];
51-
callbacks = global.___eio;
49+
if (!window.___eio) window.___eio = [];
50+
callbacks = window.___eio;
5251
}
5352

5453
// callback identifier
@@ -64,8 +63,8 @@ function JSONPPolling (opts) {
6463
this.query.j = this.index;
6564

6665
// prevent spurious errors from being emitted when the window is unloaded
67-
if (global.document && global.addEventListener) {
68-
global.addEventListener('beforeunload', function () {
66+
if (typeof addEventListener === 'function') {
67+
addEventListener('beforeunload', function () {
6968
if (self.script) self.script.onerror = empty;
7069
}, false);
7170
}

Diff for: lib/transports/polling-xhr.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global attachEvent */
2+
13
/**
24
* Module requirements.
35
*/
@@ -33,7 +35,7 @@ function XHR (opts) {
3335
this.requestTimeout = opts.requestTimeout;
3436
this.extraHeaders = opts.extraHeaders;
3537

36-
if (global.location) {
38+
if (typeof location !== 'undefined') {
3739
var isSSL = 'https:' === location.protocol;
3840
var port = location.port;
3941

@@ -42,7 +44,7 @@ function XHR (opts) {
4244
port = isSSL ? 443 : 80;
4345
}
4446

45-
this.xd = opts.hostname !== global.location.hostname ||
47+
this.xd = (typeof location !== 'undefined' && opts.hostname !== location.hostname) ||
4648
port !== opts.port;
4749
this.xs = opts.secure !== isSSL;
4850
}
@@ -271,7 +273,7 @@ Request.prototype.create = function () {
271273
return;
272274
}
273275

274-
if (global.document) {
276+
if (typeof document !== 'undefined') {
275277
this.index = Request.requestsCount++;
276278
Request.requests[this.index] = this;
277279
}
@@ -333,7 +335,7 @@ Request.prototype.cleanup = function (fromError) {
333335
} catch (e) {}
334336
}
335337

336-
if (global.document) {
338+
if (typeof document !== 'undefined') {
337339
delete Request.requests[this.index];
338340
}
339341

@@ -373,7 +375,7 @@ Request.prototype.onLoad = function () {
373375
*/
374376

375377
Request.prototype.hasXDR = function () {
376-
return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR;
378+
return typeof XDomainRequest !== 'undefined' && !this.xs && this.enablesXDR;
377379
};
378380

379381
/**
@@ -395,11 +397,11 @@ Request.prototype.abort = function () {
395397
Request.requestsCount = 0;
396398
Request.requests = {};
397399

398-
if (global.document) {
399-
if (global.attachEvent) {
400-
global.attachEvent('onunload', unloadHandler);
401-
} else if (global.addEventListener) {
402-
global.addEventListener('beforeunload', unloadHandler, false);
400+
if (typeof document !== 'undefined') {
401+
if (typeof attachEvent === 'function') {
402+
attachEvent('onunload', unloadHandler);
403+
} else if (typeof addEventListener === 'function') {
404+
addEventListener('beforeunload', unloadHandler, false);
403405
}
404406
}
405407

Diff for: lib/transports/websocket.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ var parseqs = require('parseqs');
88
var inherit = require('component-inherit');
99
var yeast = require('yeast');
1010
var debug = require('debug')('engine.io-client:websocket');
11-
var BrowserWebSocket = global.WebSocket || global.MozWebSocket;
12-
var NodeWebSocket;
13-
if (typeof window === 'undefined') {
11+
var BrowserWebSocket, NodeWebSocket;
12+
if (typeof self === 'undefined') {
1413
try {
1514
NodeWebSocket = require('ws');
1615
} catch (e) { }
16+
} else {
17+
BrowserWebSocket = self.WebSocket || self.MozWebSocket;
1718
}
1819

1920
/**
@@ -22,10 +23,7 @@ if (typeof window === 'undefined') {
2223
* interface exposed by `ws` for Node-like environment.
2324
*/
2425

25-
var WebSocket = BrowserWebSocket;
26-
if (!WebSocket && typeof window === 'undefined') {
27-
WebSocket = NodeWebSocket;
28-
}
26+
var WebSocket = BrowserWebSocket || NodeWebSocket;
2927

3028
/**
3129
* Module exports.
@@ -176,7 +174,7 @@ WS.prototype.write = function (packets) {
176174
}
177175

178176
if (self.perMessageDeflate) {
179-
var len = 'string' === typeof data ? global.Buffer.byteLength(data) : data.length;
177+
var len = 'string' === typeof data ? Buffer.byteLength(data) : data.length;
180178
if (len < self.perMessageDeflate.threshold) {
181179
opts.compress = false;
182180
}

Diff for: lib/xmlhttprequest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = function (opts) {
3131

3232
if (!xdomain) {
3333
try {
34-
return new global[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
34+
return new self[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
3535
} catch (e) { }
3636
}
3737
};

Diff for: support/webpack.config.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module.exports = {
66
library: 'eio',
77
libraryTarget: 'umd'
88
},
9-
externals: {
10-
global: glob()
9+
node: {
10+
Buffer: false
1111
},
1212
module: {
1313
loaders: [{
@@ -20,15 +20,3 @@ module.exports = {
2020
}]
2121
}
2222
};
23-
24-
/**
25-
* Populates `global`.
26-
*
27-
* @api private
28-
*/
29-
30-
function glob () {
31-
return 'typeof self !== "undefined" ? self : ' +
32-
'typeof window !== "undefined" ? window : ' +
33-
'typeof global !== "undefined" ? global : {}';
34-
}

Diff for: test/blob/polling.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('blob', function () {
1818
socket.on('message', function (data) {
1919
if (typeof data === 'string') return;
2020

21-
expect(data).to.be.a(global.Blob);
21+
expect(data).to.be.a(Blob);
2222
var fr = new FileReader();
2323
fr.onload = function () {
2424
var ab = this.result;

Diff for: test/blob/ws.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('blob', function () {
1717
socket.on('upgrade', function () {
1818
socket.send(binaryData);
1919
socket.on('message', function (data) {
20-
expect(data).to.be.a(global.Blob);
20+
expect(data).to.be.a(Blob);
2121
var fr = new FileReader();
2222
fr.onload = function () {
2323
var ab = this.result;

Diff for: test/connection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('connection', function () {
5959
});
6060

6161
// no `Worker` on old IE
62-
if (global.Worker) {
62+
if (typeof Worker !== 'undefined') {
6363
it('should work in a worker', function (done) {
6464
var worker = new Worker('/test/support/worker.js');
6565
var msg = 0;

Diff for: test/engine.io-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var expect = require('expect.js');
33
var eio = require('../');
44

5-
var expectedPort = global.location && 'https:' === location.protocol ? '443' : '80';
5+
var expectedPort = typeof location !== 'undefined' && 'https:' === location.protocol ? '443' : '80';
66

77
describe('engine.io-client', function () {
88
var open;

Diff for: test/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
require('./support/env');
22

33
// whitelist some globals to avoid warnings
4-
global.___eio = null;
4+
if (typeof window !== 'undefined') {
5+
window.___eio = null;
6+
}
57

68
var Blob = require('blob');
79

@@ -12,7 +14,7 @@ require('./connection');
1214
require('./transports');
1315
require('./xmlhttprequest');
1416

15-
if (global.ArrayBuffer) {
17+
if (typeof ArrayBuffer !== 'undefined') {
1618
require('./arraybuffer');
1719
} else {
1820
require('./binary-fallback');

Diff for: test/socket.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe('Socket', function () {
88
it('should return only available transports', function () {
99
var socket = new eio.Socket({'transports': ['polling']});
1010
expect(socket.filterUpgrades(['polling', 'websocket'])).to.eql(['polling']);
11+
socket.close();
1112
});
1213
});
1314
});

Diff for: test/support/env.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1+
/* global location:true */
2+
13
// WARNING this is bad practice
24
// we only do this in our tests because we need to test engine.io-client
35
// support in browsers and in node.js
46
// some tests do not yet work in both
5-
exports.browser = !!global.window;
6-
exports.wsSupport = !!(!global.window || window.WebSocket || window.MozWebSocket);
7+
exports.browser = typeof window !== 'undefined';
8+
exports.wsSupport = !!(typeof window === 'undefined' || window.WebSocket || window.MozWebSocket);
79

8-
var userAgent = global.navigator ? navigator.userAgent : '';
10+
var userAgent = typeof navigator !== 'undefined' ? navigator.userAgent : '';
911
exports.isOldSimulator = ~userAgent.indexOf('iPhone OS 4') || ~userAgent.indexOf('iPhone OS 5');
1012
exports.isIE8 = /MSIE 8/.test(userAgent);
1113
exports.isIE9 = /MSIE 9/.test(userAgent);
1214
exports.isIE10 = /MSIE 10/.test(userAgent);
1315
exports.isIE11 = !!userAgent.match(/Trident.*rv[ :]*11\./); // ws doesn't work at all in sauce labs
1416
exports.isAndroid = userAgent.match(/Android/i);
1517

16-
if (!global.location) {
17-
global.location = {
18+
if (typeof location === 'undefined') {
19+
location = {
1820
hostname: 'localhost',
1921
port: 3000
2022
};

0 commit comments

Comments
 (0)