Compare commits

...

12 Commits

Author SHA1 Message Date
John Hiesey 663f88323c 2.8.2 2018-05-09 19:26:31 -07:00
John Hiesey 12a0b3fd2a npm update 2018-05-09 16:37:22 -07:00
John Hiesey 520cc451e8 Clear fetch timeout after request finishes
Instead of right when response is created, which misses timeouts
that occur later
2018-05-08 21:52:39 -07:00
John Hiesey 5c3b919dab Fix edge tests 2018-05-08 21:52:05 -07:00
John Hiesey 5bc3a7205e Add (disabled) test for timeout after success
Unfortunately, reasonable timeout lengths tend to cause
spurious failures, so this test shouldn't be enabled
normally.
2018-05-08 20:37:36 -07:00
John Hiesey 80cbe3c5fe Improve fetch error handling 2018-05-08 17:36:01 -07:00
John Hiesey c3ab1f7bf0 Update package-lock.json 2018-05-08 16:18:22 -07:00
John Hiesey 98095e85eb Bump airtap version 2018-05-08 16:18:11 -07:00
John Hiesey 6547015c3d Also clear fetch timeout on error 2018-05-08 16:13:41 -07:00
John Hiesey 4b8abf52f3 Fetch clearTimeout fixup 2018-05-08 16:07:46 -07:00
John Hiesey 472ed03ea7 Merge pull request #87 from luozhang002/fetchmode
fix:add clearTimeout issue#85
2018-05-08 16:04:48 -07:00
罗章 16ec2ee996 fix:add clearTimeout 2018-05-03 13:24:32 +08:00
6 changed files with 1888 additions and 1292 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ browsers:
version: 34..latest
- name: safari
version: 8..latest
- name: microsoftedge
- name: MicrosoftEdge
version: 13..latest
- name: ie
version: 9..latest
+8 -3
View File
@@ -56,6 +56,7 @@ var ClientRequest = module.exports = function (opts) {
throw new Error('Invalid value for opts.mode')
}
self._mode = decideMode(preferBinary, useFetch)
self._fetchTimer = null
self.on('finish', function () {
self._onFinish()
@@ -131,13 +132,14 @@ ClientRequest.prototype._onFinish = function () {
if (self._mode === 'fetch') {
var signal = null
var fetchTimer = null
if (capability.abortController) {
var controller = new AbortController()
signal = controller.signal
self._fetchAbortController = controller
if ('requestTimeout' in opts && opts.requestTimeout !== 0) {
global.setTimeout(function () {
self._fetchTimer = global.setTimeout(function () {
self.emit('requestTimeout')
if (self._fetchAbortController)
self._fetchAbortController.abort()
@@ -156,7 +158,9 @@ ClientRequest.prototype._onFinish = function () {
self._fetchResponse = response
self._connect()
}, function (reason) {
self.emit('error', reason)
global.clearTimeout(self._fetchTimer)
if (!self._destroyed)
self.emit('error', reason)
})
} else {
var xhr = self._xhr = new global.XMLHttpRequest()
@@ -256,7 +260,7 @@ ClientRequest.prototype._connect = function () {
if (self._destroyed)
return
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode, self._fetchTimer)
self._response.on('error', function(err) {
self.emit('error', err)
})
@@ -274,6 +278,7 @@ ClientRequest.prototype._write = function (chunk, encoding, cb) {
ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function () {
var self = this
self._destroyed = true
global.clearTimeout(self._fetchTimer)
if (self._response)
self._response._destroyed = true
if (self._xhr)
+11 -4
View File
@@ -10,7 +10,7 @@ var rStates = exports.readyStates = {
DONE: 4
}
var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode, fetchTimer) {
var self = this
stream.Readable.call(self)
@@ -45,7 +45,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
write: function (chunk) {
return new Promise(function (resolve, reject) {
if (self._destroyed) {
return
reject()
} else if(self.push(new Buffer(chunk))) {
resolve()
} else {
@@ -54,6 +54,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
})
},
close: function () {
global.clearTimeout(fetchTimer)
if (!self._destroyed)
self.push(null)
},
@@ -64,7 +65,11 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
})
try {
response.body.pipeTo(writable)
response.body.pipeTo(writable).catch(function (err) {
global.clearTimeout(fetchTimer)
if (!self._destroyed)
self.emit('error', err)
})
return
} catch (e) {} // pipeTo method isn't defined. Can't find a better way to feature test this
}
@@ -75,12 +80,14 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
if (self._destroyed)
return
if (result.done) {
global.clearTimeout(fetchTimer)
self.push(null)
return
}
self.push(new Buffer(result.value))
read()
}).catch(function(err) {
}).catch(function (err) {
global.clearTimeout(fetchTimer)
if (!self._destroyed)
self.emit('error', err)
})
+1840 -1277
View File
File diff suppressed because it is too large Load Diff
+7 -7
View File
@@ -1,6 +1,6 @@
{
"name": "stream-http",
"version": "2.8.1",
"version": "2.8.2",
"description": "Streaming http in the browser",
"main": "index.js",
"repository": {
@@ -29,18 +29,18 @@
"dependencies": {
"builtin-status-codes": "^3.0.0",
"inherits": "^2.0.1",
"readable-stream": "^2.3.3",
"readable-stream": "^2.3.6",
"to-arraybuffer": "^1.0.0",
"xtend": "^4.0.0"
},
"devDependencies": {
"airtap": "^0.0.3",
"airtap": "^0.0.5",
"basic-auth": "^2.0.0",
"brfs": "^1.4.0",
"brfs": "^1.6.1",
"cookie-parser": "^1.4.3",
"express": "^4.16.2",
"tape": "^4.8.0",
"ua-parser-js": "^0.7.17",
"express": "^4.16.3",
"tape": "^4.9.0",
"ua-parser-js": "^0.7.18",
"webworkify": "^1.5.0"
}
}
+21
View File
@@ -20,3 +20,24 @@ test('timeout', function (t) {
t.end()
})
})
// TODO: reenable this if there's a way to make it simultaneously
// fast and reliable
test.skip('no timeout after success', function (t) {
var req = http.get({
path: '/basic.txt',
requestTimeout: 50000 // ms
}, function (res) {
res.on('data', function (data) {
})
res.on('end', function () {
t.pass('success')
global.setTimeout(function () {
t.end()
}, 50000)
})
})
req.on('requestTimeout', function () {
t.fail('unexpected timeout')
})
})