Skip to content

Commit

Permalink
Reverted fix for #17 as it was breaking other libraries.
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Jul 11, 2019
1 parent 6bd8c3f commit af97209
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
40 changes: 21 additions & 19 deletions cometd-nodejs-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ module.exports = {
var httpc = require('http');
var https = require('https');

var runtime = {};
global.cometdRuntime = runtime;
var window = global['window'];
if (!window) {
window = global['window'] = {};
}

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

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

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

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

this.status = 0;
this.statusText = '';
this.readyState = runtime.XMLHttpRequest.UNSENT;
this.readyState = window.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 = runtime.XMLHttpRequest.OPENED;
this.readyState = window.XMLHttpRequest.OPENED;
};

this.setRequestHeader = function(name, value) {
Expand Down Expand Up @@ -104,7 +106,7 @@ module.exports = {
var success = false;
self.status = response.statusCode;
self.statusText = response.statusMessage;
self.readyState = runtime.XMLHttpRequest.HEADERS_RECEIVED;
self.readyState = window.XMLHttpRequest.HEADERS_RECEIVED;
var headers = response.headers;
for (var name in headers) {
if (headers.hasOwnProperty(name)) {
Expand All @@ -120,19 +122,19 @@ module.exports = {
}
}
response.on('data', function(chunk) {
self.readyState = runtime.XMLHttpRequest.LOADING;
self.readyState = window.XMLHttpRequest.LOADING;
self.responseText += chunk;
});
response.on('end', function() {
success = true;
self.readyState = runtime.XMLHttpRequest.DONE;
self.readyState = window.XMLHttpRequest.DONE;
if (self.onload) {
self.onload();
}
});
response.on('close', function() {
if (!success) {
self.readyState = runtime.XMLHttpRequest.DONE;
self.readyState = window.XMLHttpRequest.DONE;
if (self.onerror) {
self.onerror();
}
Expand All @@ -141,7 +143,7 @@ module.exports = {
});
['abort', 'aborted', 'error'].forEach(function(event) {
_request.on(event, function(x) {
self.readyState = runtime.XMLHttpRequest.DONE;
self.readyState = window.XMLHttpRequest.DONE;
if (x) {
var error = x.message;
if (error) {
Expand Down Expand Up @@ -169,10 +171,10 @@ module.exports = {
return _config;
};
};
runtime.XMLHttpRequest.UNSENT = 0;
runtime.XMLHttpRequest.OPENED = 1;
runtime.XMLHttpRequest.HEADERS_RECEIVED = 2;
runtime.XMLHttpRequest.LOADING = 3;
runtime.XMLHttpRequest.DONE = 4;
window.XMLHttpRequest.UNSENT = 0;
window.XMLHttpRequest.OPENED = 1;
window.XMLHttpRequest.HEADERS_RECEIVED = 2;
window.XMLHttpRequest.LOADING = 3;
window.XMLHttpRequest.DONE = 4;
}
};
4 changes: 2 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.3",
"version": "1.0.4",
"description": "Adapter code to run the CometD JavaScript library in a NodeJS environment",
"keywords": [
"node",
Expand All @@ -21,7 +21,7 @@
"test": "mocha --exit"
},
"dependencies": {
"cometd": ">=3.1.9 <4.0.0 || >=4.0.4"
"cometd": ">=3.1.2"
},
"devDependencies": {
"mocha": "*"
Expand Down
2 changes: 1 addition & 1 deletion test/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('cookies', function() {

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

afterEach(function() {
Expand Down
2 changes: 1 addition & 1 deletion test/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('https', function() {

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

afterEach(function() {
Expand Down

0 comments on commit af97209

Please sign in to comment.