cometd-nodejs-client
Advanced tools
Comparing version 1.3.0 to 1.4.0
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2020 the original author or authors. | ||
* | ||
@@ -22,7 +22,13 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
export interface Cookies { | ||
storeCookie?(uri: any, header: string, callback: (failure: Error | null, cookie: any) => void): void; | ||
retrieveCookies?(context: any, uri: any, callback: (failure: Error | null, cookies: string[]) => void): void; | ||
} | ||
export interface Options { | ||
logLevel?: 'debug' | 'info'; | ||
httpProxy?: HttpProxy; | ||
cookies?: Cookies; | ||
} | ||
export function adapt(options?: Options): void; |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2017 the original author or authors. | ||
* | ||
@@ -59,2 +59,67 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
function _storeServerCookie(uri, header, callback) { | ||
if (options && options.cookies && options.cookies.storeCookie) { | ||
options.cookies.storeCookie(uri, header, callback); | ||
} else { | ||
const host = uri.hostname; | ||
let jar = _globalCookies[host]; | ||
if (jar === undefined) { | ||
_globalCookies[host] = jar = {}; | ||
} | ||
const parts = header.split(';'); | ||
// Ignore domain, path, expiration, etc. | ||
const nameValue = parts[0].trim(); | ||
const equal = nameValue.indexOf('='); | ||
if (equal > 0) { | ||
const name = nameValue.substring(0, equal); | ||
jar[name] = nameValue; | ||
} | ||
callback(); | ||
} | ||
} | ||
function _retrieveServerCookies(context, uri, callback) { | ||
if (options && options.cookies && options.cookies.retrieveCookies) { | ||
options.cookies.retrieveCookies(context, uri, callback); | ||
} else { | ||
let globalCookies = context && context.cookieStore; | ||
if (!globalCookies) { | ||
globalCookies = _globalCookies; | ||
} | ||
let cookies = []; | ||
const jar = globalCookies[uri.hostname]; | ||
if (jar) { | ||
for (let name in jar) { | ||
if (jar.hasOwnProperty(name)) { | ||
cookies.push(jar[name]); | ||
} | ||
} | ||
} | ||
callback(null, cookies); | ||
} | ||
} | ||
function _asyncForEach(array, index, operation, callback) { | ||
while (index < array.length) { | ||
let complete = false; | ||
let proceed = false; | ||
operation(array[index], () => { | ||
complete = true; | ||
if (proceed) { | ||
++index; | ||
_asyncForEach(array, index, operation, callback); | ||
} | ||
}); | ||
if (complete) { | ||
++index; | ||
} else { | ||
proceed = true; | ||
break; | ||
} | ||
} | ||
if (index === array.length) { | ||
callback(); | ||
} | ||
} | ||
// Bare minimum XMLHttpRequest implementation that works with CometD. | ||
@@ -66,7 +131,7 @@ window.XMLHttpRequest = function() { | ||
function _storeCookie(cookieStore, value) { | ||
function _storeCookie(value) { | ||
const host = _config.hostname; | ||
let jar = cookieStore[host]; | ||
let jar = _localCookies[host]; | ||
if (jar === undefined) { | ||
cookieStore[host] = jar = {}; | ||
_localCookies[host] = jar = {}; | ||
} | ||
@@ -83,12 +148,9 @@ const cookies = value.split(';'); | ||
function _concatCookies(cookieStore) { | ||
let cookies = ''; | ||
const jar = cookieStore[_config.hostname]; | ||
function _retrieveCookies() { | ||
const cookies = []; | ||
const jar = _localCookies[_config.hostname]; | ||
if (jar) { | ||
for (let name in jar) { | ||
if (jar.hasOwnProperty(name)) { | ||
if (cookies) { | ||
cookies += '; '; | ||
} | ||
cookies += jar[name]; | ||
cookies.push(jar[name]); | ||
} | ||
@@ -150,3 +212,3 @@ } | ||
if (/^cookie$/i.test(name)) { | ||
_storeCookie(_localCookies, value) | ||
_storeCookie(value); | ||
} else { | ||
@@ -158,72 +220,70 @@ _config.headers[name] = value; | ||
this.send = data => { | ||
let globalCookies = this.context && this.context.cookieStore; | ||
if (!globalCookies) { | ||
globalCookies = _globalCookies; | ||
} | ||
const cookies1 = _concatCookies(globalCookies); | ||
const cookies2 = _concatCookies(_localCookies); | ||
const delim = (cookies1 && cookies2) ? '; ' : ''; | ||
const cookies = cookies1 + delim + cookies2; | ||
if (cookies) { | ||
_config.headers['Cookie'] = cookies; | ||
} | ||
_retrieveServerCookies(this.context, _config, (x, cookies) => { | ||
const cookies1 = x ? '' : cookies.join('; '); | ||
const cookies2 = _retrieveCookies().join('; '); | ||
const delim = (cookies1 && cookies2) ? '; ' : ''; | ||
const allCookies = cookies1 + delim + cookies2; | ||
if (allCookies) { | ||
_config.headers['Cookie'] = allCookies; | ||
} | ||
const http = _secure(_config) ? https : httpc; | ||
_request = http.request(_config, response => { | ||
let success = false; | ||
this.status = response.statusCode; | ||
this.statusText = response.statusMessage; | ||
this.readyState = window.XMLHttpRequest.HEADERS_RECEIVED; | ||
const headers = response.headers; | ||
for (let name in headers) { | ||
if (headers.hasOwnProperty(name)) { | ||
if (/^set-cookie$/i.test(name)) { | ||
const header = headers[name]; | ||
for (let i = 0; i < header.length; ++i) { | ||
const whole = header[i]; | ||
const parts = whole.split(';'); | ||
const cookie = parts[0]; | ||
_storeCookie(globalCookies, cookie); | ||
const http = _secure(_config) ? https : httpc; | ||
_request = http.request(_config, response => { | ||
let success = false; | ||
this.status = response.statusCode; | ||
this.statusText = response.statusMessage; | ||
this.readyState = window.XMLHttpRequest.HEADERS_RECEIVED; | ||
const setCookies = []; | ||
const headers = response.headers; | ||
for (let name in headers) { | ||
if (headers.hasOwnProperty(name)) { | ||
if (/^set-cookie$/i.test(name)) { | ||
const header = headers[name]; | ||
setCookies.push.apply(setCookies, header); | ||
} | ||
} | ||
} | ||
} | ||
response.on('data', chunk => { | ||
this.readyState = window.XMLHttpRequest.LOADING; | ||
this.responseText += chunk; | ||
_asyncForEach(setCookies, 0, (element, callback) => { | ||
_storeServerCookie(_config, element, callback); | ||
}, () => { | ||
response.on('data', chunk => { | ||
this.readyState = window.XMLHttpRequest.LOADING; | ||
this.responseText += chunk; | ||
}); | ||
response.on('end', () => { | ||
success = true; | ||
this.readyState = window.XMLHttpRequest.DONE; | ||
if (this.onload) { | ||
this.onload(); | ||
} | ||
}); | ||
response.on('close', () => { | ||
if (!success) { | ||
this.readyState = window.XMLHttpRequest.DONE; | ||
if (this.onerror) { | ||
this.onerror(); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
response.on('end', () => { | ||
success = true; | ||
this.readyState = window.XMLHttpRequest.DONE; | ||
if (this.onload) { | ||
this.onload(); | ||
} | ||
}); | ||
response.on('close', () => { | ||
if (!success) { | ||
['abort', 'aborted', 'error'].forEach(event => { | ||
_request.on(event, x => { | ||
this.readyState = window.XMLHttpRequest.DONE; | ||
if (x) { | ||
const error = x.message; | ||
if (error) { | ||
this.statusText = error; | ||
} | ||
} | ||
if (this.onerror) { | ||
this.onerror(); | ||
this.onerror(x); | ||
} | ||
} | ||
}); | ||
}); | ||
if (data) { | ||
_request.write(data); | ||
} | ||
_request.end(); | ||
}); | ||
['abort', 'aborted', 'error'].forEach(event => { | ||
_request.on(event, x => { | ||
this.readyState = window.XMLHttpRequest.DONE; | ||
if (x) { | ||
const error = x.message; | ||
if (error) { | ||
this.statusText = error; | ||
} | ||
} | ||
if (this.onerror) { | ||
this.onerror(x); | ||
} | ||
}); | ||
}); | ||
if (data) { | ||
_request.write(data); | ||
} | ||
_request.end(); | ||
}; | ||
@@ -230,0 +290,0 @@ |
{ | ||
"name": "cometd-nodejs-client", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Adapter code to run the CometD JavaScript library in a NodeJS environment", | ||
@@ -22,3 +22,3 @@ "keywords": [ | ||
"scripts": { | ||
"test": "tsc && mocha --exit" | ||
"test": "mocha --exit --require ts-node/register --extension ts --extension js" | ||
}, | ||
@@ -32,7 +32,10 @@ "dependencies": { | ||
"devDependencies": { | ||
"@types/mocha": "latest", | ||
"@types/node": "latest", | ||
"@types/tough-cookie": "^4.0.1", | ||
"mocha": "latest", | ||
"typescript": "latest", | ||
"@types/node": "latest", | ||
"@types/mocha": "latest" | ||
"tough-cookie": "latest", | ||
"ts-node": "latest", | ||
"typescript": "latest" | ||
} | ||
} |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2019 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2017 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2017 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2019 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2020 the original author or authors. | ||
* | ||
@@ -16,8 +16,8 @@ * Licensed under the Apache License, Version 2.0 (the "License"); | ||
*/ | ||
import nodeCometD = require('..'); | ||
import http = require('http'); | ||
import {AddressInfo} from "net"; | ||
import * as nodeCometD from '..'; | ||
import * as jsCometD from 'cometd'; | ||
import * as http from 'http'; | ||
import {AddressInfo} from 'net'; | ||
describe('typescript client', () => { | ||
let _cometd: any; | ||
let _server: http.Server; | ||
@@ -27,3 +27,2 @@ | ||
nodeCometD.adapt(); | ||
_cometd = require('cometd'); | ||
}); | ||
@@ -62,3 +61,3 @@ | ||
const cometd = new _cometd.CometD(); | ||
const cometd: jsCometD.CometD = new jsCometD.CometD(); | ||
cometd.websocketEnabled = false; | ||
@@ -69,3 +68,3 @@ cometd.configure({ | ||
}); | ||
cometd.handshake((r: any) => { | ||
cometd.handshake((r: jsCometD.Message) => { | ||
if (r.successful) { | ||
@@ -72,0 +71,0 @@ done(); |
/* | ||
* Copyright (c) 2017-2020 the original author or authors. | ||
* Copyright (c) 2019 the original author or authors. | ||
* | ||
@@ -4,0 +4,0 @@ * Licensed under the Apache License, Version 2.0 (the "License"); |
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
69575
1231
7
7