Skip to content

Commit 5367fe6

Browse files
authored
* doc update * handle corrupted location header during redirect
1 parent d1ca2df commit 5367fe6

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ Changelog
55

66
# 2.x release
77

8-
## master
8+
## v2.3.0
99

10-
- Fix: update `browser.js` to support react-native, where `self` isn't available.
10+
- New: `AbortSignal` support, with README example.
11+
- Enhance: handle invalid `Location` header during redirect by rejecting them explicitly with `FetchError`.
12+
- Fix: update `browser.js` to support react-native environment, where `self` isn't available globally.
1113

1214
## v2.2.1
1315

README.md

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ A light-weight module that brings `window.fetch` to Node.js
3131
- [Accessing Headers and other Meta data](#accessing-headers-and-other-meta-data)
3232
- [Post data using a file stream](#post-data-using-a-file-stream)
3333
- [Post with form-data (detect multipart)](#post-with-form-data-detect-multipart)
34+
- [Request cancellation with AbortSignal](#request-cancellation-with-abortsignal)
3435
- [API](#api)
3536
- [fetch(url[, options])](#fetchurl-options)
3637
- [Options](#options)
@@ -248,6 +249,40 @@ fetch('https://httpbin.org/post', options)
248249
.then(json => console.log(json));
249250
```
250251

252+
#### Request cancellation with AbortSignal
253+
254+
> NOTE: You may only cancel streamed requests on Node >= v8.0.0
255+
256+
You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).
257+
258+
An example of timing out a request after 150ms could be achieved as follows:
259+
260+
```js
261+
import AbortContoller from 'abort-controller';
262+
263+
const controller = new AbortController();
264+
const timeout = setTimeout(
265+
() => { controller.abort(); },
266+
150,
267+
);
268+
269+
fetch(url, { signal: controller.signal })
270+
.then(res => res.json())
271+
.then(
272+
data => {
273+
useData(data)
274+
},
275+
err => {
276+
if (err.name === 'AbortError') {
277+
// request was aborted
278+
}
279+
},
280+
)
281+
.finally(() => {
282+
clearTimeout(timeout);
283+
});
284+
```
285+
251286
See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.
252287

253288

@@ -286,40 +321,6 @@ The default values are shown after each option key.
286321
}
287322
```
288323

289-
#### Request cancellation with AbortController:
290-
291-
> NOTE: You may only cancel streamed requests on Node >= v8.0.0
292-
293-
You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).
294-
295-
An example of timing out a request after 150ms could be achieved as follows:
296-
297-
```js
298-
import AbortContoller from 'abort-controller';
299-
300-
const controller = new AbortController();
301-
const timeout = setTimeout(
302-
() => { controller.abort(); },
303-
150,
304-
);
305-
306-
fetch(url, { signal: controller.signal })
307-
.then(res => res.json())
308-
.then(
309-
data => {
310-
useData(data)
311-
},
312-
err => {
313-
if (err.name === 'AbortError') {
314-
// request was aborted
315-
}
316-
},
317-
)
318-
.finally(() => {
319-
clearTimeout(timeout);
320-
});
321-
```
322-
323324
##### Default Headers
324325

325326
If no values are set, the following request headers will be sent automatically:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fetch",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "A light-weight module that brings window.fetch to node.js",
55
"main": "lib/index",
66
"browser": "./browser.js",

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ export default function fetch(url, opts) {
120120
case 'manual':
121121
// node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
122122
if (locationURL !== null) {
123-
headers.set('Location', locationURL);
123+
// handle corrupted header
124+
try {
125+
headers.set('Location', locationURL);
126+
} catch (err) {
127+
// istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
128+
reject(err);
129+
}
124130
}
125131
break;
126132
case 'follow':

0 commit comments

Comments
 (0)