diff options
author | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2018-09-20 02:56:13 +0200 |
commit | bbff7403fbf46f9ad92240ac213df8d30ef31b64 (patch) | |
tree | c58400ec5124da1c7d56b01aea83309f80a56c3b /node_modules/stream-http/lib/request.js | |
parent | 003fb34971cf63466184351b4db5f7c67df4f444 (diff) |
update packages
Diffstat (limited to 'node_modules/stream-http/lib/request.js')
-rw-r--r-- | node_modules/stream-http/lib/request.js | 47 |
1 files changed, 34 insertions, 13 deletions
diff --git a/node_modules/stream-http/lib/request.js b/node_modules/stream-http/lib/request.js index 36ad12db9..4f097dfa3 100644 --- a/node_modules/stream-http/lib/request.js +++ b/node_modules/stream-http/lib/request.js @@ -38,9 +38,8 @@ var ClientRequest = module.exports = function (opts) { var preferBinary var useFetch = true - if (opts.mode === 'disable-fetch' || 'timeout' in opts) { - // If the use of XHR should be preferred and includes preserving the 'content-type' header. - // Force XHR to be used since the Fetch API does not yet support timeouts. + if (opts.mode === 'disable-fetch' || ('requestTimeout' in opts && !capability.abortController)) { + // If the use of XHR should be preferred. Not typically needed. useFetch = false preferBinary = true } else if (opts.mode === 'prefer-streaming') { @@ -57,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() @@ -102,7 +102,9 @@ ClientRequest.prototype._onFinish = function () { var headersObj = self._headers var body = null if (opts.method !== 'GET' && opts.method !== 'HEAD') { - if (capability.blobConstructor) { + if (capability.arraybuffer) { + body = toArrayBuffer(Buffer.concat(self._body)) + } else if (capability.blobConstructor) { body = new global.Blob(self._body.map(function (buffer) { return toArrayBuffer(buffer) }), { @@ -129,17 +131,36 @@ 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) { + self._fetchTimer = global.setTimeout(function () { + self.emit('requestTimeout') + if (self._fetchAbortController) + self._fetchAbortController.abort() + }, opts.requestTimeout) + } + } + global.fetch(self._opts.url, { method: self._opts.method, headers: headersList, body: body || undefined, mode: 'cors', - credentials: opts.withCredentials ? 'include' : 'same-origin' + credentials: opts.withCredentials ? 'include' : 'same-origin', + signal: signal }).then(function (response) { 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() @@ -162,10 +183,10 @@ ClientRequest.prototype._onFinish = function () { if (self._mode === 'text' && 'overrideMimeType' in xhr) xhr.overrideMimeType('text/plain; charset=x-user-defined') - if ('timeout' in opts) { - xhr.timeout = opts.timeout + if ('requestTimeout' in opts) { + xhr.timeout = opts.requestTimeout xhr.ontimeout = function () { - self.emit('timeout') + self.emit('requestTimeout') } } @@ -239,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) }) @@ -257,12 +278,13 @@ 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) self._xhr.abort() - // Currently, there isn't a way to truly abort a fetch. - // If you like bikeshedding, see https://github.com/whatwg/fetch/issues/27 + else if (self._fetchAbortController) + self._fetchAbortController.abort() } ClientRequest.prototype.end = function (data, encoding, cb) { @@ -301,6 +323,5 @@ var unsafeHeaders = [ 'trailer', 'transfer-encoding', 'upgrade', - 'user-agent', 'via' ] |