From 784d07a16b314f8b35f8382e4cc41197fd3f7877 Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Wed, 16 Oct 2019 19:40:02 +0200 Subject: [PATCH] Use a `Duplex` instead of a plain `EventEmitter` Fixes: https://github.com/TooTallNate/node-https-proxy-agent/issues/81 --- index.js | 9 +++++++-- test/test.js | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index aeb624db..68991e96 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ var net = require('net'); var tls = require('tls'); var url = require('url'); -var events = require('events'); +var stream = require('stream'); var Agent = require('agent-base'); var inherits = require('util').inherits; var debug = require('debug')('https-proxy-agent'); @@ -168,7 +168,12 @@ HttpsProxyAgent.prototype.callback = function connect(req, opts, fn) { // // See: https://hackerone.com/reports/541502 socket.destroy(); - socket = new events.EventEmitter(); + socket = new stream.Duplex({ + read() {}, + write(chunk, encoding, callback) { + callback(); + } + }); // save a reference to the concat'd Buffer for the `onsocket` callback buffers = buffered; diff --git a/test/test.js b/test/test.js index 513aae0a..f4ae8c1c 100644 --- a/test/test.js +++ b/test/test.js @@ -234,6 +234,24 @@ describe('HttpsProxyAgent', function() { done(); }); }); + it('should not error if the request is aborted and a fake socket is assigned to it', function(done) { + proxy.authenticate = function(req, fn) { + fn(null, false); + }; + + const proxyUri = + process.env.HTTP_PROXY || + process.env.http_proxy || + 'http://127.0.0.1:' + proxyPort; + + const req = http.get({ agent: new HttpsProxyAgent(proxyUri) }); + + req.on('socket', function() { + req.abort(); + }) + + req.on('abort', done); + }); it('should emit an "error" event on the `http.ClientRequest` if the proxy does not exist', function(done) { // port 4 is a reserved, but "unassigned" port var proxyUri = 'http://127.0.0.1:4';