Skip to content

Commit

Permalink
Fixes #17 - Breaks any other libraries that rely on window.
Browse files Browse the repository at this point in the history
Removed the hardcoded definition of global.window, relying
now on global.cometdRuntime.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed May 13, 2019
1 parent f1a3dc0 commit 6bd8c3f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 36 deletions.
37 changes: 19 additions & 18 deletions cometd-nodejs-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ module.exports = {
var httpc = require('http');
var https = require('https');

global.window = {};
var runtime = {};
global.cometdRuntime = runtime;

window.setTimeout = setTimeout;
window.clearTimeout = clearTimeout;
runtime.setTimeout = setTimeout;
runtime.clearTimeout = clearTimeout;

window.console = console;
window.console.debug = window.console.log;
runtime.console = console;
runtime.console.debug = runtime.console.log;

// Fields shared by all XMLHttpRequest instances.
var _agentc = new httpc.Agent({
Expand All @@ -26,7 +27,7 @@ module.exports = {
}

// Bare minimum XMLHttpRequest implementation that works with CometD.
window.XMLHttpRequest = function() {
runtime.XMLHttpRequest = function() {
var _localCookies = {};
var _config;
var _request;
Expand Down Expand Up @@ -65,15 +66,15 @@ module.exports = {

this.status = 0;
this.statusText = '';
this.readyState = window.XMLHttpRequest.UNSENT;
this.readyState = runtime.XMLHttpRequest.UNSENT;
this.responseText = '';

this.open = function(method, uri) {
_config = url.parse(uri);
_config.method = method;
_config.agent = _secure(_config) ? _agents : _agentc;
_config.headers = {};
this.readyState = window.XMLHttpRequest.OPENED;
this.readyState = runtime.XMLHttpRequest.OPENED;
};

this.setRequestHeader = function(name, value) {
Expand Down Expand Up @@ -103,7 +104,7 @@ module.exports = {
var success = false;
self.status = response.statusCode;
self.statusText = response.statusMessage;
self.readyState = window.XMLHttpRequest.HEADERS_RECEIVED;
self.readyState = runtime.XMLHttpRequest.HEADERS_RECEIVED;
var headers = response.headers;
for (var name in headers) {
if (headers.hasOwnProperty(name)) {
Expand All @@ -119,19 +120,19 @@ module.exports = {
}
}
response.on('data', function(chunk) {
self.readyState = window.XMLHttpRequest.LOADING;
self.readyState = runtime.XMLHttpRequest.LOADING;
self.responseText += chunk;
});
response.on('end', function() {
success = true;
self.readyState = window.XMLHttpRequest.DONE;
self.readyState = runtime.XMLHttpRequest.DONE;
if (self.onload) {
self.onload();
}
});
response.on('close', function() {
if (!success) {
self.readyState = window.XMLHttpRequest.DONE;
self.readyState = runtime.XMLHttpRequest.DONE;
if (self.onerror) {
self.onerror();
}
Expand All @@ -140,7 +141,7 @@ module.exports = {
});
['abort', 'aborted', 'error'].forEach(function(event) {
_request.on(event, function(x) {
self.readyState = window.XMLHttpRequest.DONE;
self.readyState = runtime.XMLHttpRequest.DONE;
if (x) {
var error = x.message;
if (error) {
Expand Down Expand Up @@ -168,10 +169,10 @@ module.exports = {
return _config;
};
};
window.XMLHttpRequest.UNSENT = 0;
window.XMLHttpRequest.OPENED = 1;
window.XMLHttpRequest.HEADERS_RECEIVED = 2;
window.XMLHttpRequest.LOADING = 3;
window.XMLHttpRequest.DONE = 4;
runtime.XMLHttpRequest.UNSENT = 0;
runtime.XMLHttpRequest.OPENED = 1;
runtime.XMLHttpRequest.HEADERS_RECEIVED = 2;
runtime.XMLHttpRequest.LOADING = 3;
runtime.XMLHttpRequest.DONE = 4;
}
};
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cometd-nodejs-client",
"version": "1.0.2",
"version": "1.0.3",
"description": "Adapter code to run the CometD JavaScript library in a NodeJS environment",
"keywords": [
"node",
Expand All @@ -18,7 +18,10 @@
"url": "https://github.com/cometd/cometd-nodejs-client.git"
},
"scripts": {
"test": "mocha"
"test": "mocha --exit"
},
"dependencies": {
"cometd": ">=3.1.9 <4.0.0 || >=4.0.4"
},
"devDependencies": {
"mocha": "*"
Expand Down
56 changes: 56 additions & 0 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var cometd = require('..');
var http = require('http');

describe('client', function() {
var _lib;
var _server;

beforeEach(function() {
cometd.adapt();
_lib = require('cometd');
});

afterEach(function() {
if (_server) {
_server.close();
}
});

it('performs handshake', function(done) {
_server = http.createServer(function(request, response) {
var content = '';
request.addListener('data', function(chunk) {
content += chunk;
});
request.addListener('end', function() {
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
var content = '[{' +
'"id":"1",' +
'"version":"1.0",' +
'"channel":"/meta/handshake",' +
'"clientId":"0123456789abcdef",' +
'"supportedConnectionTypes":["long-polling"],' +
'"advice":{"reconnect":"none"},' +
'"successful":true' +
'}]';
response.end(content, 'utf8');
});
});
_server.listen(0, 'localhost', function() {
var port = _server.address().port;
console.log('listening on localhost:' + port);

var cometd = new _lib.CometD();
cometd.configure({
url: 'http://localhost:' + port + '/cometd',
logLevel: 'info'
});
cometd.handshake(function(r) {
if (r.successful) {
done();
}
});
});
});
});
32 changes: 17 additions & 15 deletions test/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ var cometd = require('..');
var http = require('http');

describe('cookies', function() {
var _runtime;
var _server;

beforeEach(function() {
cometd.adapt();
_runtime = global.cometdRuntime;
});

afterEach(function() {
Expand All @@ -31,11 +33,11 @@ describe('cookies', function() {
console.log('listening on localhost:' + port);
var uri = 'http://localhost:' + port;

var xhr1 = new window.XMLHttpRequest();
var xhr1 = new _runtime.XMLHttpRequest();
xhr1.open('GET', uri + '/1');
xhr1.onload = function() {
assert.strictEqual(xhr1.status, 200);
var xhr2 = new window.XMLHttpRequest();
var xhr2 = new _runtime.XMLHttpRequest();
xhr2.open('GET', uri + '/2');
xhr2.onload = function() {
assert.strictEqual(xhr2.status, 200);
Expand Down Expand Up @@ -68,15 +70,15 @@ describe('cookies', function() {
console.log('listening on localhost:' + port);
var uri = 'http://localhost:' + port;

var xhr1 = new window.XMLHttpRequest();
var xhr1 = new _runtime.XMLHttpRequest();
xhr1.open('GET', uri + '/1');
xhr1.onload = function() {
assert.strictEqual(xhr1.status, 200);
var xhr2 = new window.XMLHttpRequest();
var xhr2 = new _runtime.XMLHttpRequest();
xhr2.open('GET', uri + '/2');
xhr2.onload = function() {
assert.strictEqual(xhr2.status, 200);
var xhr3 = new window.XMLHttpRequest();
var xhr3 = new _runtime.XMLHttpRequest();
xhr3.open('GET', uri + '/3');
xhr3.onload = function() {
assert.strictEqual(xhr3.status, 200);
Expand Down Expand Up @@ -116,20 +118,20 @@ describe('cookies', function() {
var port = _server.address().port;
console.log('listening on localhost:' + port);

var xhrA1 = new window.XMLHttpRequest();
var xhrA1 = new _runtime.XMLHttpRequest();
xhrA1.open('GET', 'http://localhost:' + port + '/hostA/1');
xhrA1.onload = function() {
assert.strictEqual(xhrA1.status, 200);
var xhrA2 = new window.XMLHttpRequest();
var xhrA2 = new _runtime.XMLHttpRequest();
xhrA2.open('GET', 'http://localhost:' + port + '/hostA/2');
xhrA2.onload = function() {
assert.strictEqual(xhrA2.status, 200);

var xhrB1 = new window.XMLHttpRequest();
var xhrB1 = new _runtime.XMLHttpRequest();
xhrB1.open('GET', 'http://127.0.0.1:' + port + '/hostB/1');
xhrB1.onload = function() {
assert.strictEqual(xhrB1.status, 200);
var xhrB2 = new window.XMLHttpRequest();
var xhrB2 = new _runtime.XMLHttpRequest();
xhrB2.open('GET', 'http://127.0.0.1:' + port + '/hostB/2');
xhrB2.onload = function() {
assert.strictEqual(xhrB2.status, 200);
Expand Down Expand Up @@ -162,15 +164,15 @@ describe('cookies', function() {
var port = _server.address().port;
console.log('listening on localhost:' + port);

var xhr1 = new window.XMLHttpRequest();
var xhr1 = new _runtime.XMLHttpRequest();
xhr1.open('GET', 'http://localhost:' + port + '/1');
xhr1.onload = function() {
assert.strictEqual(xhr1.status, 200);
var xhr2 = new window.XMLHttpRequest();
var xhr2 = new _runtime.XMLHttpRequest();
xhr2.open('GET', 'http://localhost:' + port + '/2');
xhr2.onload = function() {
assert.strictEqual(xhr2.status, 200);
var xhr3 = new window.XMLHttpRequest();
var xhr3 = new _runtime.XMLHttpRequest();
xhr3.open('GET', 'http://localhost:' + port + '/verify');
xhr3.onload = function() {
assert.strictEqual(xhr1.status, 200);
Expand Down Expand Up @@ -206,16 +208,16 @@ describe('cookies', function() {
var port = _server.address().port;
console.log('listening on localhost:' + port);

var xhr1 = new window.XMLHttpRequest();
var xhr1 = new _runtime.XMLHttpRequest();
xhr1.open('GET', 'http://localhost:' + port + '/1');
xhr1.onload = function() {
assert.strictEqual(xhr1.status, 200);
var xhr2 = new window.XMLHttpRequest();
var xhr2 = new _runtime.XMLHttpRequest();
xhr2.open('GET', 'http://localhost:' + port + '/2');
xhr2.setRequestHeader('cookie', 'c=d; e=f');
xhr2.onload = function() {
assert.strictEqual(xhr2.status, 200);
var xhr3 = new window.XMLHttpRequest();
var xhr3 = new _runtime.XMLHttpRequest();
xhr3.open('GET', 'http://localhost:' + port + '/3');
xhr3.onload = function() {
assert.strictEqual(xhr3.status, 200);
Expand Down
4 changes: 3 additions & 1 deletion test/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ var https = require('https');
var fs = require('fs');

describe('https', function() {
var _runtime;
var _server;

beforeEach(function() {
cometd.adapt();
_runtime = global.cometdRuntime;
});

afterEach(function() {
Expand All @@ -28,7 +30,7 @@ describe('https', function() {
var port = _server.address().port;
console.log('listening on localhost:' + port);
var uri = 'https://localhost:' + port;
var xhr = new window.XMLHttpRequest();
var xhr = new _runtime.XMLHttpRequest();
xhr.open('GET', uri + '/');
// Allow self-signed certificates.
xhr._config().rejectUnauthorized = false;
Expand Down

0 comments on commit 6bd8c3f

Please sign in to comment.