Skip to content

Commit

Permalink
[fix] Remove any reference to the global variable
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Nov 7, 2018
1 parent 122111a commit 99bcc62
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 55 deletions.
9 changes: 4 additions & 5 deletions lib/socket.js
Expand Up @@ -45,7 +45,7 @@ function Socket (uri, opts) {
}

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

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

this.agent = opts.agent || false;
this.hostname = opts.hostname ||
(global.location ? location.hostname : 'localhost');
this.port = opts.port || (global.location && location.port
(typeof location !== 'undefined' ? location.hostname : 'localhost');
this.port = opts.port || (typeof location !== 'undefined' && location.port
? location.port
: (this.secure ? 443 : 80));
this.query = opts.query || {};
Expand Down Expand Up @@ -98,8 +98,7 @@ function Socket (uri, opts) {
this.isReactNative = (typeof navigator !== 'undefined' && typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative');

// other options for Node.js or ReactNative client
var freeGlobal = typeof global === 'object' && global;
if (freeGlobal.global === freeGlobal || this.isReactNative) {
if (typeof self === 'undefined' || this.isReactNative) {
if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
this.extraHeaders = opts.extraHeaders;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/transports/index.js
Expand Up @@ -27,7 +27,7 @@ function polling (opts) {
var xs = false;
var jsonp = false !== opts.jsonp;

if (global.location) {
if (typeof location !== 'undefined') {
var isSSL = 'https:' === location.protocol;
var port = location.port;

Expand Down
9 changes: 4 additions & 5 deletions lib/transports/polling-jsonp.js
@@ -1,4 +1,3 @@

/**
* Module requirements.
*/
Expand Down Expand Up @@ -47,8 +46,8 @@ 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
if (!global.___eio) global.___eio = [];
callbacks = global.___eio;
if (!window.___eio) window.___eio = [];

This comment has been minimized.

Copy link
@oliversalzburg

oliversalzburg Nov 15, 2018

This causes test failures in engine.io, because there is no window in NodeJS.

callbacks = window.___eio;
}

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

// prevent spurious errors from being emitted when the window is unloaded
if (global.document && global.addEventListener) {
global.addEventListener('beforeunload', function () {
if (typeof addEventListener === 'function') {
addEventListener('beforeunload', function () {
if (self.script) self.script.onerror = empty;
}, false);
}
Expand Down
22 changes: 12 additions & 10 deletions lib/transports/polling-xhr.js
@@ -1,3 +1,5 @@
/* global attachEvent */

/**
* Module requirements.
*/
Expand Down Expand Up @@ -33,7 +35,7 @@ function XHR (opts) {
this.requestTimeout = opts.requestTimeout;
this.extraHeaders = opts.extraHeaders;

if (global.location) {
if (typeof location !== 'undefined') {
var isSSL = 'https:' === location.protocol;
var port = location.port;

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

this.xd = opts.hostname !== global.location.hostname ||
this.xd = (typeof location !== 'undefined' && opts.hostname !== location.hostname) ||
port !== opts.port;
this.xs = opts.secure !== isSSL;
}
Expand Down Expand Up @@ -271,7 +273,7 @@ Request.prototype.create = function () {
return;
}

if (global.document) {
if (typeof document !== 'undefined') {
this.index = Request.requestsCount++;
Request.requests[this.index] = this;
}
Expand Down Expand Up @@ -333,7 +335,7 @@ Request.prototype.cleanup = function (fromError) {
} catch (e) {}
}

if (global.document) {
if (typeof document !== 'undefined') {
delete Request.requests[this.index];
}

Expand Down Expand Up @@ -373,7 +375,7 @@ Request.prototype.onLoad = function () {
*/

Request.prototype.hasXDR = function () {
return 'undefined' !== typeof global.XDomainRequest && !this.xs && this.enablesXDR;
return typeof XDomainRequest !== 'undefined' && !this.xs && this.enablesXDR;
};

/**
Expand All @@ -395,11 +397,11 @@ Request.prototype.abort = function () {
Request.requestsCount = 0;
Request.requests = {};

if (global.document) {
if (global.attachEvent) {
global.attachEvent('onunload', unloadHandler);
} else if (global.addEventListener) {
global.addEventListener('beforeunload', unloadHandler, false);
if (typeof document !== 'undefined') {
if (typeof attachEvent === 'function') {
attachEvent('onunload', unloadHandler);
} else if (typeof addEventListener === 'function') {
addEventListener('beforeunload', unloadHandler, false);
}
}

Expand Down
14 changes: 6 additions & 8 deletions lib/transports/websocket.js
Expand Up @@ -8,12 +8,13 @@ var parseqs = require('parseqs');
var inherit = require('component-inherit');
var yeast = require('yeast');
var debug = require('debug')('engine.io-client:websocket');
var BrowserWebSocket = global.WebSocket || global.MozWebSocket;
var NodeWebSocket;
if (typeof window === 'undefined') {
var BrowserWebSocket, NodeWebSocket;
if (typeof self === 'undefined') {
try {
NodeWebSocket = require('ws');
} catch (e) { }
} else {
BrowserWebSocket = self.WebSocket || self.MozWebSocket;
}

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

var WebSocket = BrowserWebSocket;
if (!WebSocket && typeof window === 'undefined') {
WebSocket = NodeWebSocket;
}
var WebSocket = BrowserWebSocket || NodeWebSocket;

/**
* Module exports.
Expand Down Expand Up @@ -176,7 +174,7 @@ WS.prototype.write = function (packets) {
}

if (self.perMessageDeflate) {
var len = 'string' === typeof data ? global.Buffer.byteLength(data) : data.length;
var len = 'string' === typeof data ? Buffer.byteLength(data) : data.length;
if (len < self.perMessageDeflate.threshold) {
opts.compress = false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/xmlhttprequest.js
Expand Up @@ -31,7 +31,7 @@ module.exports = function (opts) {

if (!xdomain) {
try {
return new global[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
return new self[['Active'].concat('Object').join('X')]('Microsoft.XMLHTTP');
} catch (e) { }
}
};
16 changes: 2 additions & 14 deletions support/webpack.config.js
Expand Up @@ -6,8 +6,8 @@ module.exports = {
library: 'eio',
libraryTarget: 'umd'
},
externals: {
global: glob()
node: {
Buffer: false
},
module: {
loaders: [{
Expand All @@ -20,15 +20,3 @@ module.exports = {
}]
}
};

/**
* Populates `global`.
*
* @api private
*/

function glob () {
return 'typeof self !== "undefined" ? self : ' +
'typeof window !== "undefined" ? window : ' +
'typeof global !== "undefined" ? global : {}';
}
2 changes: 1 addition & 1 deletion test/blob/polling.js
Expand Up @@ -18,7 +18,7 @@ describe('blob', function () {
socket.on('message', function (data) {
if (typeof data === 'string') return;

expect(data).to.be.a(global.Blob);
expect(data).to.be.a(Blob);
var fr = new FileReader();
fr.onload = function () {
var ab = this.result;
Expand Down
2 changes: 1 addition & 1 deletion test/blob/ws.js
Expand Up @@ -17,7 +17,7 @@ describe('blob', function () {
socket.on('upgrade', function () {
socket.send(binaryData);
socket.on('message', function (data) {
expect(data).to.be.a(global.Blob);
expect(data).to.be.a(Blob);
var fr = new FileReader();
fr.onload = function () {
var ab = this.result;
Expand Down
2 changes: 1 addition & 1 deletion test/connection.js
Expand Up @@ -59,7 +59,7 @@ describe('connection', function () {
});

// no `Worker` on old IE
if (global.Worker) {
if (typeof Worker !== 'undefined') {
it('should work in a worker', function (done) {
var worker = new Worker('/test/support/worker.js');
var msg = 0;
Expand Down
2 changes: 1 addition & 1 deletion test/engine.io-client.js
Expand Up @@ -2,7 +2,7 @@
var expect = require('expect.js');
var eio = require('../');

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

describe('engine.io-client', function () {
var open;
Expand Down
6 changes: 4 additions & 2 deletions test/index.js
@@ -1,7 +1,9 @@
require('./support/env');

// whitelist some globals to avoid warnings
global.___eio = null;
if (typeof window !== 'undefined') {
window.___eio = null;
}

var Blob = require('blob');

Expand All @@ -12,7 +14,7 @@ require('./connection');
require('./transports');
require('./xmlhttprequest');

if (global.ArrayBuffer) {
if (typeof ArrayBuffer !== 'undefined') {
require('./arraybuffer');
} else {
require('./binary-fallback');
Expand Down
1 change: 1 addition & 0 deletions test/socket.js
Expand Up @@ -8,6 +8,7 @@ describe('Socket', function () {
it('should return only available transports', function () {
var socket = new eio.Socket({'transports': ['polling']});
expect(socket.filterUpgrades(['polling', 'websocket'])).to.eql(['polling']);
socket.close();
});
});
});
12 changes: 7 additions & 5 deletions test/support/env.js
@@ -1,20 +1,22 @@
/* global location:true */

// WARNING this is bad practice
// we only do this in our tests because we need to test engine.io-client
// support in browsers and in node.js
// some tests do not yet work in both
exports.browser = !!global.window;
exports.wsSupport = !!(!global.window || window.WebSocket || window.MozWebSocket);
exports.browser = typeof window !== 'undefined';
exports.wsSupport = !!(typeof window === 'undefined' || window.WebSocket || window.MozWebSocket);

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

if (!global.location) {
global.location = {
if (typeof location === 'undefined') {
location = {
hostname: 'localhost',
port: 3000
};
Expand Down

0 comments on commit 99bcc62

Please sign in to comment.