Skip to content

Commit

Permalink
v2.3.0 (#548)
Browse files Browse the repository at this point in the history
* doc update
* handle corrupted location header during redirect
  • Loading branch information
bitinn committed Nov 13, 2018
1 parent d1ca2df commit 5367fe6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 38 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ Changelog

# 2.x release

## master
## v2.3.0

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

## v2.2.1

Expand Down
69 changes: 35 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A light-weight module that brings `window.fetch` to Node.js
- [Accessing Headers and other Meta data](#accessing-headers-and-other-meta-data)
- [Post data using a file stream](#post-data-using-a-file-stream)
- [Post with form-data (detect multipart)](#post-with-form-data-detect-multipart)
- [Request cancellation with AbortSignal](#request-cancellation-with-abortsignal)
- [API](#api)
- [fetch(url[, options])](#fetchurl-options)
- [Options](#options)
Expand Down Expand Up @@ -248,6 +249,40 @@ fetch('https://httpbin.org/post', options)
.then(json => console.log(json));
```

#### Request cancellation with AbortSignal

> NOTE: You may only cancel streamed requests on Node >= v8.0.0
You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).

An example of timing out a request after 150ms could be achieved as follows:

```js
import AbortContoller from 'abort-controller';

const controller = new AbortController();
const timeout = setTimeout(
() => { controller.abort(); },
150,
);

fetch(url, { signal: controller.signal })
.then(res => res.json())
.then(
data => {
useData(data)
},
err => {
if (err.name === 'AbortError') {
// request was aborted
}
},
)
.finally(() => {
clearTimeout(timeout);
});
```

See [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.


Expand Down Expand Up @@ -286,40 +321,6 @@ The default values are shown after each option key.
}
```

#### Request cancellation with AbortController:

> NOTE: You may only cancel streamed requests on Node >= v8.0.0
You may cancel requests with `AbortController`. A suggested implementation is [`abort-controller`](https://www.npmjs.com/package/abort-controller).

An example of timing out a request after 150ms could be achieved as follows:

```js
import AbortContoller from 'abort-controller';

const controller = new AbortController();
const timeout = setTimeout(
() => { controller.abort(); },
150,
);

fetch(url, { signal: controller.signal })
.then(res => res.json())
.then(
data => {
useData(data)
},
err => {
if (err.name === 'AbortError') {
// request was aborted
}
},
)
.finally(() => {
clearTimeout(timeout);
});
```

##### Default Headers

If no values are set, the following request headers will be sent automatically:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-fetch",
"version": "2.2.1",
"version": "2.3.0",
"description": "A light-weight module that brings window.fetch to node.js",
"main": "lib/index",
"browser": "./browser.js",
Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ export default function fetch(url, opts) {
case 'manual':
// node-fetch-specific step: make manual redirect a bit easier to use by setting the Location header value to the resolved URL.
if (locationURL !== null) {
headers.set('Location', locationURL);
// handle corrupted header
try {
headers.set('Location', locationURL);
} catch (err) {
// istanbul ignore next: nodejs server prevent invalid response headers, we can't test this through normal request
reject(err);
}
}
break;
case 'follow':
Expand Down

0 comments on commit 5367fe6

Please sign in to comment.