From 11be976272f404a600ae8bc131b6b1436d96ee16 Mon Sep 17 00:00:00 2001 From: Adrian Chu Date: Fri, 26 Jul 2019 16:30:23 +0200 Subject: [PATCH 1/4] Remove unreachable code Node.js < 4.5.0 is no longer supported as per https://github.com/TooTallNate/node-https-proxy-agent/commit/a2779222. --- index.js | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 0a2fdab..7293645 100644 --- a/index.js +++ b/index.js @@ -91,7 +91,6 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { } function cleanup() { - socket.removeListener('data', ondata); socket.removeListener('end', onend); socket.removeListener('error', onerror); socket.removeListener('close', onclose); @@ -120,11 +119,7 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { if (!~str.indexOf('\r\n\r\n')) { // keep buffering debug('have not received end of HTTP headers yet...'); - if (socket.read) { - read(); - } else { - socket.once('data', ondata); - } + read(); return; } @@ -174,11 +169,7 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { function onsocket(socket) { // replay the "buffers" Buffer onto the `socket`, since at this point // the HTTP module machinery has been hooked up for the user - if ('function' == typeof socket.ondata) { - // node <= v0.11.3, the `ondata` function is set on the socket - socket.ondata(buffers, 0, buffers.length); - } else if (socket.listeners('data').length > 0) { - // node > v0.11.3, the "data" event is listened for directly + if (socket.listenerCount('data') > 0) { socket.emit('data', buffers); } else { // never? @@ -193,11 +184,7 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { socket.on('close', onclose); socket.on('end', onend); - if (socket.read) { - read(); - } else { - socket.once('data', ondata); - } + read(); var hostname = opts.host + ':' + opts.port; var msg = 'CONNECT ' + hostname + ' HTTP/1.1\r\n'; From 16c2fadc84a4605a9e457b316b34e2adf8f94c9d Mon Sep 17 00:00:00 2001 From: Adrian Chu Date: Fri, 26 Jul 2019 16:34:00 +0200 Subject: [PATCH 2/4] Fix compatibility with Node.js >= 10.0.0 Resume the socket after the `'socket'` event is emitted on the `ClientRequest` object. Refs: https://github.com/nodejs/node/issues/24474#issuecomment-511963799 Fixes: https://github.com/TooTallNate/node-https-proxy-agent/pull/58 --- index.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/index.js b/index.js index 7293645..6452663 100644 --- a/index.js +++ b/index.js @@ -150,6 +150,7 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { } cleanup(); + req.once('socket', resume); fn(null, sock); } else { // some other status code that's not 200... need to re-play the HTTP header @@ -176,6 +177,7 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { throw new Error('should not happen...'); } + socket.resume(); // nullify the cached Buffer instance buffers = null; } @@ -211,6 +213,17 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { socket.write(msg + '\r\n'); }; +/** + * Resumes a socket. + * + * @param {(net.Socket|tls.Socket)} socket The socket to resume + * @api public + */ + +function resume(socket) { + socket.resume(); +} + function isDefaultPort(port, secure) { return Boolean((!secure && port === 80) || (secure && port === 443)); } From f4e6d29adb75e8b75980ad4a58826f7fdd4841a0 Mon Sep 17 00:00:00 2001 From: Adrian Chu Date: Fri, 26 Jul 2019 16:44:53 +0200 Subject: [PATCH 3/4] Test on Node.js 10 and 12 --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 805d3d5..4472d5b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,8 @@ node_js: - "6" - "7" - "8" + - "10" + - "12" install: - PATH="`npm bin`:`npm bin -g`:$PATH" From 05734dfb5c8a044b8df9a0b9edf2b30bf98a2fc7 Mon Sep 17 00:00:00 2001 From: Adrian Chu Date: Thu, 3 Oct 2019 22:15:26 -0700 Subject: [PATCH 4/4] =?UTF-8?q?Meh=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.js b/test/test.js index b368495..6ed1e88 100644 --- a/test/test.js +++ b/test/test.js @@ -71,23 +71,23 @@ describe('HttpsProxyAgent', function () { // shut down test HTTP server after(function (done) { - server.once('close', function () { done(); }); server.close(); + done(); }); after(function (done) { - proxy.once('close', function () { done(); }); proxy.close(); + done(); }); after(function (done) { - sslServer.once('close', function () { done(); }); sslServer.close(); + done(); }); after(function (done) { - sslProxy.once('close', function () { done(); }); sslProxy.close(); + done(); }); describe('constructor', function () {