node-fetch
Advanced tools
Comparing version 2.6.9 to 2.6.7
@@ -14,13 +14,13 @@ "use strict"; | ||
var globalObject = getGlobal(); | ||
var global = getGlobal(); | ||
module.exports = exports = globalObject.fetch; | ||
module.exports = exports = global.fetch; | ||
// Needed for TypeScript and Webpack. | ||
if (globalObject.fetch) { | ||
exports.default = globalObject.fetch.bind(globalObject); | ||
if (global.fetch) { | ||
exports.default = global.fetch.bind(global); | ||
} | ||
exports.Headers = globalObject.Headers; | ||
exports.Request = globalObject.Request; | ||
exports.Response = globalObject.Response; | ||
exports.Headers = global.Headers; | ||
exports.Request = global.Request; | ||
exports.Response = global.Response; |
@@ -1417,16 +1417,2 @@ process.emitWarning("The .es.js file is deprecated. Use .mjs instead."); | ||
/** | ||
* isSameProtocol reports whether the two provided URLs use the same protocol. | ||
* | ||
* Both domains must already be in canonical form. | ||
* @param {string|URL} original | ||
* @param {string|URL} destination | ||
*/ | ||
const isSameProtocol = function isSameProtocol(destination, original) { | ||
const orig = new URL$1(original).protocol; | ||
const dest = new URL$1(destination).protocol; | ||
return orig === dest; | ||
}; | ||
/** | ||
* Fetch function | ||
@@ -1462,3 +1448,3 @@ * | ||
if (request.body && request.body instanceof Stream.Readable) { | ||
destroyStream(request.body, error); | ||
request.body.destroy(error); | ||
} | ||
@@ -1504,39 +1490,5 @@ if (!response || !response.body) return; | ||
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err)); | ||
if (response && response.body) { | ||
destroyStream(response.body, err); | ||
} | ||
finalize(); | ||
}); | ||
fixResponseChunkedTransferBadEnding(req, function (err) { | ||
if (signal && signal.aborted) { | ||
return; | ||
} | ||
if (response && response.body) { | ||
destroyStream(response.body, err); | ||
} | ||
}); | ||
/* c8 ignore next 18 */ | ||
if (parseInt(process.version.substring(1)) < 14) { | ||
// Before Node.js 14, pipeline() does not fully support async iterators and does not always | ||
// properly handle when the socket close/end events are out of order. | ||
req.on('socket', function (s) { | ||
s.addListener('close', function (hadError) { | ||
// if a data listener is still present we didn't end cleanly | ||
const hasDataListener = s.listenerCount('data') > 0; | ||
// if end happened before close but the socket didn't emit an error, do it now | ||
if (response && hasDataListener && !hadError && !(signal && signal.aborted)) { | ||
const err = new Error('Premature close'); | ||
err.code = 'ERR_STREAM_PREMATURE_CLOSE'; | ||
response.body.emit('error', err); | ||
} | ||
}); | ||
}); | ||
} | ||
req.on('response', function (res) { | ||
@@ -1613,3 +1565,3 @@ clearTimeout(reqTimeout); | ||
if (!isDomainOrSubdomain(request.url, locationURL) || !isSameProtocol(request.url, locationURL)) { | ||
if (!isDomainOrSubdomain(request.url, locationURL)) { | ||
for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) { | ||
@@ -1707,9 +1659,2 @@ requestOpts.headers.delete(name); | ||
}); | ||
raw.on('end', function () { | ||
// some old IIS servers return zero-length OK deflate responses, so 'data' is never emitted. | ||
if (!response) { | ||
response = new Response(body, response_options); | ||
resolve(response); | ||
} | ||
}); | ||
return; | ||
@@ -1734,37 +1679,2 @@ } | ||
} | ||
function fixResponseChunkedTransferBadEnding(request, errorCallback) { | ||
let socket; | ||
request.on('socket', function (s) { | ||
socket = s; | ||
}); | ||
request.on('response', function (response) { | ||
const headers = response.headers; | ||
if (headers['transfer-encoding'] === 'chunked' && !headers['content-length']) { | ||
response.once('close', function (hadError) { | ||
// if a data listener is still present we didn't end cleanly | ||
const hasDataListener = socket.listenerCount('data') > 0; | ||
if (hasDataListener && !hadError) { | ||
const err = new Error('Premature close'); | ||
err.code = 'ERR_STREAM_PREMATURE_CLOSE'; | ||
errorCallback(err); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
function destroyStream(stream, err) { | ||
if (stream.destroy) { | ||
stream.destroy(err); | ||
} else { | ||
// node < 8 | ||
stream.emit('error', err); | ||
stream.end(); | ||
} | ||
} | ||
/** | ||
@@ -1771,0 +1681,0 @@ * Redirect code matching |
@@ -1421,16 +1421,2 @@ 'use strict'; | ||
/** | ||
* isSameProtocol reports whether the two provided URLs use the same protocol. | ||
* | ||
* Both domains must already be in canonical form. | ||
* @param {string|URL} original | ||
* @param {string|URL} destination | ||
*/ | ||
const isSameProtocol = function isSameProtocol(destination, original) { | ||
const orig = new URL$1(original).protocol; | ||
const dest = new URL$1(destination).protocol; | ||
return orig === dest; | ||
}; | ||
/** | ||
* Fetch function | ||
@@ -1466,3 +1452,3 @@ * | ||
if (request.body && request.body instanceof Stream.Readable) { | ||
destroyStream(request.body, error); | ||
request.body.destroy(error); | ||
} | ||
@@ -1508,39 +1494,5 @@ if (!response || !response.body) return; | ||
reject(new FetchError(`request to ${request.url} failed, reason: ${err.message}`, 'system', err)); | ||
if (response && response.body) { | ||
destroyStream(response.body, err); | ||
} | ||
finalize(); | ||
}); | ||
fixResponseChunkedTransferBadEnding(req, function (err) { | ||
if (signal && signal.aborted) { | ||
return; | ||
} | ||
if (response && response.body) { | ||
destroyStream(response.body, err); | ||
} | ||
}); | ||
/* c8 ignore next 18 */ | ||
if (parseInt(process.version.substring(1)) < 14) { | ||
// Before Node.js 14, pipeline() does not fully support async iterators and does not always | ||
// properly handle when the socket close/end events are out of order. | ||
req.on('socket', function (s) { | ||
s.addListener('close', function (hadError) { | ||
// if a data listener is still present we didn't end cleanly | ||
const hasDataListener = s.listenerCount('data') > 0; | ||
// if end happened before close but the socket didn't emit an error, do it now | ||
if (response && hasDataListener && !hadError && !(signal && signal.aborted)) { | ||
const err = new Error('Premature close'); | ||
err.code = 'ERR_STREAM_PREMATURE_CLOSE'; | ||
response.body.emit('error', err); | ||
} | ||
}); | ||
}); | ||
} | ||
req.on('response', function (res) { | ||
@@ -1617,3 +1569,3 @@ clearTimeout(reqTimeout); | ||
if (!isDomainOrSubdomain(request.url, locationURL) || !isSameProtocol(request.url, locationURL)) { | ||
if (!isDomainOrSubdomain(request.url, locationURL)) { | ||
for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) { | ||
@@ -1711,9 +1663,2 @@ requestOpts.headers.delete(name); | ||
}); | ||
raw.on('end', function () { | ||
// some old IIS servers return zero-length OK deflate responses, so 'data' is never emitted. | ||
if (!response) { | ||
response = new Response(body, response_options); | ||
resolve(response); | ||
} | ||
}); | ||
return; | ||
@@ -1738,37 +1683,2 @@ } | ||
} | ||
function fixResponseChunkedTransferBadEnding(request, errorCallback) { | ||
let socket; | ||
request.on('socket', function (s) { | ||
socket = s; | ||
}); | ||
request.on('response', function (response) { | ||
const headers = response.headers; | ||
if (headers['transfer-encoding'] === 'chunked' && !headers['content-length']) { | ||
response.once('close', function (hadError) { | ||
// if a data listener is still present we didn't end cleanly | ||
const hasDataListener = socket.listenerCount('data') > 0; | ||
if (hasDataListener && !hadError) { | ||
const err = new Error('Premature close'); | ||
err.code = 'ERR_STREAM_PREMATURE_CLOSE'; | ||
errorCallback(err); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
function destroyStream(stream, err) { | ||
if (stream.destroy) { | ||
stream.destroy(err); | ||
} else { | ||
// node < 8 | ||
stream.emit('error', err); | ||
stream.end(); | ||
} | ||
} | ||
/** | ||
@@ -1775,0 +1685,0 @@ * Redirect code matching |
{ | ||
"name": "node-fetch", | ||
"version": "2.6.9", | ||
"version": "2.6.7", | ||
"description": "A light-weight module that brings window.fetch to node.js", | ||
@@ -42,3 +42,3 @@ "main": "lib/index.js", | ||
}, | ||
"peerDependencies": { | ||
"peerDependencies": { | ||
"encoding": "^0.1.0" | ||
@@ -57,5 +57,3 @@ }, | ||
"babel-plugin-istanbul": "^4.1.6", | ||
"babel-plugin-transform-async-generator-functions": "^6.24.1", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-preset-env": "1.4.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-register": "^6.16.3", | ||
@@ -79,14 +77,3 @@ "chai": "^3.5.0", | ||
"teeny-request": "3.7.0" | ||
}, | ||
"release": { | ||
"branches": [ | ||
"+([0-9]).x", | ||
"main", | ||
"next", | ||
{ | ||
"name": "beta", | ||
"prerelease": true | ||
} | ||
] | ||
} | ||
} |
@@ -191,45 +191,2 @@ node-fetch | ||
In Node.js 14 you can also use async iterators to read `body`; however, be careful to catch | ||
errors -- the longer a response runs, the more likely it is to encounter an error. | ||
```js | ||
const fetch = require('node-fetch'); | ||
const response = await fetch('https://httpbin.org/stream/3'); | ||
try { | ||
for await (const chunk of response.body) { | ||
console.dir(JSON.parse(chunk.toString())); | ||
} | ||
} catch (err) { | ||
console.error(err.stack); | ||
} | ||
``` | ||
In Node.js 12 you can also use async iterators to read `body`; however, async iterators with streams | ||
did not mature until Node.js 14, so you need to do some extra work to ensure you handle errors | ||
directly from the stream and wait on it response to fully close. | ||
```js | ||
const fetch = require('node-fetch'); | ||
const read = async body => { | ||
let error; | ||
body.on('error', err => { | ||
error = err; | ||
}); | ||
for await (const chunk of body) { | ||
console.dir(JSON.parse(chunk.toString())); | ||
} | ||
return new Promise((resolve, reject) => { | ||
body.on('close', () => { | ||
error ? reject(error) : resolve(); | ||
}); | ||
}); | ||
}; | ||
try { | ||
const response = await fetch('https://httpbin.org/stream/3'); | ||
await read(response.body); | ||
} catch (err) { | ||
console.error(err.stack); | ||
} | ||
``` | ||
#### Buffer | ||
@@ -236,0 +193,0 @@ If you prefer to cache binary data in full, use buffer(). (NOTE: `buffer()` is a `node-fetch`-only API) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24
152240
4396
591