Skip to content

Commit

Permalink
Use node-fetch v3 beta and require Node.js 10.16 (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Co-authored-by: Seth Holladay <me@seth-holladay.com>
  • Loading branch information
3 people committed May 27, 2020
1 parent ce5a08e commit 1631f2e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const fetch = require('node-fetch');
const AbortController = require('abort-controller');

const TEN_MEGABYTES = 1000 * 1000 * 10;

if (!global.fetch) {
global.fetch = fetch;
global.fetch = (url, options) => fetch(url, {highWaterMark: TEN_MEGABYTES, ...options});
}

if (!global.Headers) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=10"
"node": ">=10.16"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -54,7 +54,7 @@
],
"dependencies": {
"abort-controller": "^3.0.0",
"node-fetch": "^2.6.0"
"node-fetch": "^3.0.0-beta.6-exportfix"
},
"devDependencies": {
"ava": "^2.4.0",
Expand Down
21 changes: 20 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const ky = require('ky-universal');
const {body} = await ky('https://httpbin.org/bytes/16');
const {value} = await body.getReader().read();
const result = new TextDecoder('utf-8').decode(value);

//
})();
```
Expand Down Expand Up @@ -95,6 +95,25 @@ Put the following in package.json:

The library that uses Ky will now *just work* in AVA tests.

#### `clone()` hangs with a large response in Node - What should I do?

Streams in Node.js have a smaller internal buffer size (16 kB, aka `highWaterMark`) than browsers (>1 MB, not consistent across browsers). When using Ky, the default `highWaterMark` is set to 10 MB, so you shouldn't encounter many issues related to that.

However, you can specify a custom `highWaterMark` if needed:

```js
import ky from 'ky-universal';

(async () => {
const response = await ky('https://example.com', {
// 20 MB
highWaterMark: 1000 * 1000 * 20
});

const data = await response.clone().buffer();
})();
```

## Related

- [ky](https://github.com/sindresorhus/ky) - Tiny and elegant HTTP client based on the browser Fetch API
Expand Down

0 comments on commit 1631f2e

Please sign in to comment.