cometd-nodejs-client
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -7,9 +7,10 @@ module.exports = { | ||
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; | ||
@@ -23,3 +24,3 @@ // Fields shared by all XMLHttpRequest instances. | ||
}); | ||
var _cookieStore = {}; | ||
var _globalCookies = {}; | ||
@@ -31,9 +32,42 @@ function _secure(uri) { | ||
// Bare minimum XMLHttpRequest implementation that works with CometD. | ||
window.XMLHttpRequest = function() { | ||
runtime.XMLHttpRequest = function() { | ||
var _localCookies = {}; | ||
var _config; | ||
var _request; | ||
function _storeCookie(cookieStore, value) { | ||
var host = _config.hostname; | ||
var jar = cookieStore[host]; | ||
if (jar === undefined) { | ||
cookieStore[host] = jar = {}; | ||
} | ||
var cookies = value.split(';'); | ||
for (var i = 0; i < cookies.length; ++i) { | ||
var cookie = cookies[i].trim(); | ||
var equal = cookie.indexOf('='); | ||
if (equal > 0) { | ||
jar[cookie.substring(0, equal)] = cookie; | ||
} | ||
} | ||
} | ||
function _concatCookies(cookieStore) { | ||
var cookies = ''; | ||
var jar = cookieStore[_config.hostname]; | ||
if (jar) { | ||
for (var name in jar) { | ||
if (jar.hasOwnProperty(name)) { | ||
if (cookies) { | ||
cookies += '; '; | ||
} | ||
cookies += jar[name]; | ||
} | ||
} | ||
} | ||
return cookies; | ||
} | ||
this.status = 0; | ||
this.statusText = ''; | ||
this.readyState = window.XMLHttpRequest.UNSENT; | ||
this.readyState = runtime.XMLHttpRequest.UNSENT; | ||
this.responseText = ''; | ||
@@ -46,28 +80,24 @@ | ||
_config.headers = {}; | ||
this.readyState = window.XMLHttpRequest.OPENED; | ||
this.readyState = runtime.XMLHttpRequest.OPENED; | ||
}; | ||
this.setRequestHeader = function(name, value) { | ||
_config.headers[name] = value; | ||
if (/^cookie$/i.test(name)) { | ||
_storeCookie(_localCookies, value) | ||
} else { | ||
_config.headers[name] = value; | ||
} | ||
}; | ||
this.send = function(data) { | ||
var cookieStore = this.context && this.context.cookieStore; | ||
if (!cookieStore) { | ||
cookieStore = _cookieStore; | ||
var globalCookies = this.context && this.context.cookieStore; | ||
if (!globalCookies) { | ||
globalCookies = _globalCookies; | ||
} | ||
var jar = cookieStore[_config.hostname]; | ||
if (jar) { | ||
var cookies = ''; | ||
for (var name in jar) { | ||
if (jar.hasOwnProperty(name)) { | ||
if (cookies) { | ||
cookies += '; '; | ||
} | ||
cookies += jar[name]; | ||
} | ||
} | ||
if (cookies) { | ||
_config.headers['Cookie'] = cookies; | ||
} | ||
var cookies1 = _concatCookies(globalCookies); | ||
var cookies2 = _concatCookies(_localCookies); | ||
var delim = (cookies1 && cookies2) ? '; ' : ''; | ||
var cookies = cookies1 + delim + cookies2; | ||
if (cookies) { | ||
_config.headers['Cookie'] = cookies; | ||
} | ||
@@ -81,3 +111,3 @@ | ||
self.statusText = response.statusMessage; | ||
self.readyState = window.XMLHttpRequest.HEADERS_RECEIVED; | ||
self.readyState = runtime.XMLHttpRequest.HEADERS_RECEIVED; | ||
var headers = response.headers; | ||
@@ -92,13 +122,3 @@ for (var name in headers) { | ||
var cookie = parts[0]; | ||
var host = _config.hostname; | ||
var jar = cookieStore[host]; | ||
if (jar === undefined) { | ||
cookieStore[host] = jar = {}; | ||
} | ||
var equal = cookie.indexOf('='); | ||
if (equal > 0) { | ||
jar[cookie.substring(0, equal)] = cookie; | ||
} | ||
_storeCookie(globalCookies, cookie); | ||
} | ||
@@ -109,3 +129,3 @@ } | ||
response.on('data', function(chunk) { | ||
self.readyState = window.XMLHttpRequest.LOADING; | ||
self.readyState = runtime.XMLHttpRequest.LOADING; | ||
self.responseText += chunk; | ||
@@ -115,3 +135,3 @@ }); | ||
success = true; | ||
self.readyState = window.XMLHttpRequest.DONE; | ||
self.readyState = runtime.XMLHttpRequest.DONE; | ||
if (self.onload) { | ||
@@ -123,3 +143,3 @@ self.onload(); | ||
if (!success) { | ||
self.readyState = window.XMLHttpRequest.DONE; | ||
self.readyState = runtime.XMLHttpRequest.DONE; | ||
if (self.onerror) { | ||
@@ -133,3 +153,3 @@ self.onerror(); | ||
_request.on(event, function(x) { | ||
self.readyState = window.XMLHttpRequest.DONE; | ||
self.readyState = runtime.XMLHttpRequest.DONE; | ||
if (x) { | ||
@@ -162,8 +182,8 @@ var error = x.message; | ||
}; | ||
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; | ||
} | ||
}; |
{ | ||
"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", | ||
@@ -21,4 +21,7 @@ "keywords": [ | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha --exit" | ||
}, | ||
"dependencies": { | ||
"cometd": ">=3.1.9 <4.0.0 || >=4.0.4" | ||
}, | ||
"devDependencies": { | ||
@@ -25,0 +28,0 @@ "mocha": "*" |
@@ -6,2 +6,3 @@ var assert = require('assert'); | ||
describe('cookies', function() { | ||
var _runtime; | ||
var _server; | ||
@@ -11,2 +12,3 @@ | ||
cometd.adapt(); | ||
_runtime = global.cometdRuntime; | ||
}); | ||
@@ -36,7 +38,7 @@ | ||
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'); | ||
@@ -74,11 +76,11 @@ xhr2.onload = function() { | ||
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'); | ||
@@ -123,7 +125,7 @@ xhr3.onload = function() { | ||
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'); | ||
@@ -133,7 +135,7 @@ xhrA2.onload = function() { | ||
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'); | ||
@@ -171,11 +173,11 @@ xhrB2.onload = function() { | ||
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'); | ||
@@ -193,2 +195,47 @@ xhr3.onload = function() { | ||
}); | ||
it('handles cookies as request headers', function(done) { | ||
_server = http.createServer(function(request, response) { | ||
var cookies = request.headers['cookie']; | ||
if (/\/1$/.test(request.url)) { | ||
response.setHeader('Set-Cookie', 'a=b'); | ||
response.end(); | ||
} else if (/\/2$/.test(request.url)) { | ||
assert.ok(cookies.indexOf('a=b') >= 0); | ||
assert.ok(cookies.indexOf('c=d') >= 0); | ||
assert.ok(cookies.indexOf('e=f') >= 0); | ||
response.end(); | ||
} else if (/\/3$/.test(request.url)) { | ||
assert.ok(cookies.indexOf('a=b') >= 0); | ||
assert.ok(cookies.indexOf('c=d') < 0); | ||
assert.ok(cookies.indexOf('e=f') < 0); | ||
response.end(); | ||
} | ||
}); | ||
_server.listen(0, 'localhost', function() { | ||
var port = _server.address().port; | ||
console.log('listening on localhost:' + port); | ||
var xhr1 = new _runtime.XMLHttpRequest(); | ||
xhr1.open('GET', 'http://localhost:' + port + '/1'); | ||
xhr1.onload = function() { | ||
assert.strictEqual(xhr1.status, 200); | ||
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 _runtime.XMLHttpRequest(); | ||
xhr3.open('GET', 'http://localhost:' + port + '/3'); | ||
xhr3.onload = function() { | ||
assert.strictEqual(xhr3.status, 200); | ||
done(); | ||
}; | ||
xhr3.send(); | ||
}; | ||
xhr2.send(); | ||
}; | ||
xhr1.send(); | ||
}); | ||
}); | ||
}); |
@@ -7,2 +7,3 @@ var assert = require('assert'); | ||
describe('https', function() { | ||
var _runtime; | ||
var _server; | ||
@@ -12,2 +13,3 @@ | ||
cometd.adapt(); | ||
_runtime = global.cometdRuntime; | ||
}); | ||
@@ -33,3 +35,3 @@ | ||
var uri = 'https://localhost:' + port; | ||
var xhr = new window.XMLHttpRequest(); | ||
var xhr = new _runtime.XMLHttpRequest(); | ||
xhr.open('GET', uri + '/'); | ||
@@ -36,0 +38,0 @@ // Allow self-signed certificates. |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
480
75882
1
5
+ Addedcometd@8.0.6(transitive)