Skip to content

Commit

Permalink
Merge 95a9b52 into 670b563
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot committed Dec 23, 2018
2 parents 670b563 + 95a9b52 commit 5528ab0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
22 changes: 12 additions & 10 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,7 @@ Request.prototype.start = function () {
if (isConnecting) {
var onReqSockConnect = function () {
socket.removeListener('connect', onReqSockConnect)
clearTimeout(self.timeoutTimer)
self.timeoutTimer = null
self.clearTimeout()
setReqTimeout()
}

Expand Down Expand Up @@ -874,10 +873,7 @@ Request.prototype.onRequestError = function (error) {
self.req.end()
return
}
if (self.timeout && self.timeoutTimer) {
clearTimeout(self.timeoutTimer)
self.timeoutTimer = null
}
self.clearTimeout()
self.emit('error', error)
}

Expand Down Expand Up @@ -964,10 +960,7 @@ Request.prototype.onRequestResponse = function (response) {
if (self.setHost) {
self.removeHeader('host')
}
if (self.timeout && self.timeoutTimer) {
clearTimeout(self.timeoutTimer)
self.timeoutTimer = null
}
self.clearTimeout()

var targetCookieJar = (self._jar && self._jar.setCookie) ? self._jar : globalCookieJar
var addCookie = function (cookie) {
Expand Down Expand Up @@ -1172,6 +1165,7 @@ Request.prototype.abort = function () {
self.response.destroy()
}

self.clearTimeout()
self.emit('abort')
}

Expand Down Expand Up @@ -1532,13 +1526,21 @@ Request.prototype.resume = function () {
}
Request.prototype.destroy = function () {
var self = this
this.clearTimeout()
if (!self._ended) {
self.end()
} else if (self.response) {
self.response.destroy()
}
}

Request.prototype.clearTimeout = function () {
if (this.timeoutTimer) {
clearTimeout(this.timeoutTimer)
this.timeoutTimer = null
}
}

Request.defaultProxyHeaderWhiteList =
Tunnel.defaultProxyHeaderWhiteList.slice()

Expand Down
9 changes: 9 additions & 0 deletions tests/test-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ tape('request timeout with keep-alive connection', function (t) {
})
})

tape('calling abort clears the timeout', function (t) {
const req = request({ url: s.url + '/timeout', timeout: 2500 })
setTimeout(function () {
req.abort()
t.equal(req.timeoutTimer, null)
t.end()
}, 5)
})

tape('cleanup', function (t) {
s.close(function () {
t.end()
Expand Down

0 comments on commit 5528ab0

Please sign in to comment.