From 6663f25efc35cb5f22da0243c8e4ab08f02c197f Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Tue, 5 Nov 2019 13:04:18 +0100 Subject: [PATCH] Fixes #20 - Support forward proxies. Small changes and added support for options.logLevel in adapt(). Signed-off-by: Simone Bordet --- cometd-nodejs-client.js | 13 +++++++++++-- package.json | 2 +- test/proxy.js | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cometd-nodejs-client.js b/cometd-nodejs-client.js index fb5d119..0cc153e 100644 --- a/cometd-nodejs-client.js +++ b/cometd-nodejs-client.js @@ -3,6 +3,8 @@ module.exports = { var url = require('url'); var httpc = require('http'); var https = require('https'); + var HttpcProxyAgent = require('http-proxy-agent'); + var HttpsProxyAgent = require('https-proxy-agent'); var window = global['window']; if (!window) { @@ -21,11 +23,17 @@ module.exports = { }; var _agentc = new httpc.Agent(_agentOptions); var _agents = new https.Agent(_agentOptions); - var HttpcProxyAgent = require('http-proxy-agent'); - var HttpsProxyAgent = require('https-proxy-agent'); var _globalCookies = {}; + var _logLevel = options && options.logLevel || 'info'; + + function _debug() { + if (_logLevel === 'debug') { + console.log.apply(this, Array.from(arguments)); + } + } + function _secure(uri) { return /^https/i.test(uri.protocol); } @@ -95,6 +103,7 @@ module.exports = { } } if (isIncluded) { + _debug('proxying', serverURI.href, 'via', proxy); var agentOpts = Object.assign(url.parse(proxy), _agentOptions); return _secure(serverURI) ? new HttpsProxyAgent(agentOpts) : new HttpcProxyAgent(agentOpts); } diff --git a/package.json b/package.json index 2be666c..fd1dbb1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cometd-nodejs-client", - "version": "1.0.4", + "version": "1.1.0", "description": "Adapter code to run the CometD JavaScript library in a NodeJS environment", "keywords": [ "node", diff --git a/test/proxy.js b/test/proxy.js index d037d24..bab0551 100644 --- a/test/proxy.js +++ b/test/proxy.js @@ -43,6 +43,7 @@ describe('proxy', function() { console.log('proxy listening on localhost:' + proxyPort); nodeCometD.adapt({ + logLevel: 'debug', httpProxy: { uri: 'http://localhost:' + proxyPort }