Skip to content

Commit

Permalink
Use a Duplex instead of a plain EventEmitter
Browse files Browse the repository at this point in the history
Fixes: #81
  • Loading branch information
lpinca committed Oct 16, 2019
1 parent a6187ae commit b688518
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
15 changes: 13 additions & 2 deletions index.js
Expand Up @@ -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');
Expand Down Expand Up @@ -168,7 +168,16 @@ 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();
}
});

if (process.versions.modules === '48') {
socket.destroy = noop;
}

// save a reference to the concat'd Buffer for the `onsocket` callback
buffers = buffered;
Expand Down Expand Up @@ -241,3 +250,5 @@ function resume(socket) {
function isDefaultPort(port, secure) {
return Boolean((!secure && port === 80) || (secure && port === 443));
}

function noop() {}
18 changes: 18 additions & 0 deletions test/test.js
Expand Up @@ -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';
Expand Down

0 comments on commit b688518

Please sign in to comment.