better-https-proxy-agent
Advanced tools
Comparing version 1.0.6 to 1.0.7
62
index.js
@@ -54,5 +54,7 @@ 'use strict'; | ||
const stream = this._createSurrogateStream(); | ||
const stream = this._createSurrogateStream(() => { | ||
request.abort(); | ||
}); | ||
this._createProxyConnection(options, (err, socket) => { | ||
const request = this._createProxyConnection(options, (err, socket) => { | ||
if (err) { | ||
@@ -67,7 +69,7 @@ stream.emit('error', err); | ||
const tlsSocket = tls.connect(options, () => { | ||
this._connectSurrogateStream(stream, tlsSocket); | ||
if (options._agentKey) this._cacheSession(options._agentKey, tlsSocket.getSession()); | ||
}); | ||
this._connectSurrogateStream(stream, tlsSocket); | ||
tlsSocket.once('error', (err) => { | ||
@@ -92,2 +94,6 @@ if (!stream.surrogateConnectedStream) stream.emit('error', err); | ||
request.on('timeout', () => { | ||
stream.emit('timeout'); | ||
}); | ||
return stream; | ||
@@ -109,3 +115,3 @@ }; | ||
Agent.prototype._createSurrogateStream = function _createSurrogateStream() { | ||
Agent.prototype._createSurrogateStream = function _createSurrogateStream(destroyer) { | ||
const stream = duplexify(); | ||
@@ -118,2 +124,3 @@ | ||
stream.surrogateReffed = true; | ||
stream.surrogateDestroy = destroyer; | ||
@@ -128,2 +135,3 @@ /* | ||
stream.unref = surrogateUnref; | ||
stream.destroy = surrogateDestroy; | ||
@@ -147,2 +155,4 @@ /* | ||
tlsSocket.surrogateDestroy = null; | ||
/* | ||
@@ -173,9 +183,9 @@ * Apply 'buffered' side effects. | ||
stream.unref = connectedUnref; | ||
stream.destroy = connectedDestroy; | ||
/* | ||
* Forward the 'timeout' event, as it is not a standard stream event. | ||
* Neither is the 'connect' event, but since the stream is already 'connected' | ||
* when it is returned, we don't need that. | ||
* Forward the 'timeout' and 'connect' events, as they are not standard stream events. | ||
*/ | ||
tlsSocket.on('timeout', connectedOnTimeout); | ||
tlsSocket.on('connect', connectedOnConnect); | ||
@@ -190,2 +200,5 @@ /* | ||
* no 'close' is emitted at all. Oops. We work around that problem here. | ||
* | ||
* It also doesn't set writable to false when the stream is closed, even | ||
* though it is no longer safe to call write(). We deal with that, too. | ||
*/ | ||
@@ -209,3 +222,15 @@ tlsSocket.once('end', connectedOnEnd); | ||
request.on('connect', function(res, socket, head) { | ||
callback(null, socket); | ||
if (res.statusCode === 200) { | ||
callback(null, socket); | ||
} else { | ||
const error = new Error(res.statusMessage); | ||
error.code = res.statusCode; | ||
callback(error); | ||
/* | ||
* There is no expectation of reuse of a socket when using CONNECT, so although | ||
* we theoretically could reuse it, we don't bother. It's simpler to destroy it. | ||
*/ | ||
socket.destroy(); | ||
} | ||
}); | ||
@@ -216,2 +241,4 @@ | ||
request.end(); | ||
return request; | ||
}; | ||
@@ -256,2 +283,8 @@ | ||
function surrogateDestroy() { | ||
this.surrogateDestroy(); | ||
return this; | ||
} | ||
function setTimeoutListener(timeout, callback) { | ||
@@ -297,2 +330,8 @@ if (timeout) { | ||
function connectedDestroy() { | ||
this.surrogateConnectedStream.destroy(); | ||
return this; | ||
} | ||
function connectedOnTimeout() { | ||
@@ -302,2 +341,6 @@ this.surrogateStream.emit('timeout') | ||
function connectedOnConnect() { | ||
this.surrogateStream.emit('connect') | ||
} | ||
function connectedOnEnd() { | ||
@@ -308,2 +351,3 @@ this.surrogateSeenEnd = true; | ||
function connectedOnClose() { | ||
this.surrogateStream.writable = false; | ||
if (this.surrogateSeenEnd) this.surrogateStream.emit('close'); | ||
@@ -310,0 +354,0 @@ } |
{ | ||
"name": "better-https-proxy-agent", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "An agent for HTTPS through an HTTP(S) proxy server using the CONNECT method", | ||
@@ -55,4 +55,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"duplexify": "^4.0.0" | ||
"duplexify": "4.0.0" | ||
} | ||
} |
16059
250
+ Addedduplexify@4.0.0(transitive)
- Removedduplexify@4.1.3(transitive)
Updatedduplexify@4.0.0