Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 663f88323c | |||
| 12a0b3fd2a | |||
| 520cc451e8 | |||
| 5c3b919dab | |||
| 5bc3a7205e | |||
| 80cbe3c5fe | |||
| c3ab1f7bf0 | |||
| 98095e85eb | |||
| 6547015c3d | |||
| 4b8abf52f3 | |||
| 472ed03ea7 | |||
| 16ec2ee996 |
+1
-1
@@ -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
@@ -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
@@ -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)
|
||||
})
|
||||
|
||||
Generated
+1840
-1277
File diff suppressed because it is too large
Load Diff
+7
-7
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user