Skip to content

Commit

Permalink
Fix compatibility with Node.js >= 10.0.0 (#73)
Browse files Browse the repository at this point in the history
Resume the socket after the `'socket'` event is emitted on the
`ClientRequest` object.

Fixes: #58
Refs: nodejs/node#24474 (comment)
  • Loading branch information
TooTallNate committed Oct 4, 2019
2 parents d0e3c18 + 2590f76 commit c7d8161
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -8,6 +8,8 @@ node_js:
- "6"
- "7"
- "8"
- "10"
- "12"

install:
- PATH="`npm bin`:`npm bin -g`:$PATH"
Expand Down
32 changes: 16 additions & 16 deletions index.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -155,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
Expand All @@ -174,17 +170,14 @@ 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?
throw new Error('should not happen...');
}

socket.resume();
// nullify the cached Buffer instance
buffers = null;
}
Expand All @@ -193,11 +186,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';
Expand All @@ -224,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));
}
8 changes: 4 additions & 4 deletions test/test.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit c7d8161

Please sign in to comment.