Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add optional ReadableStream polyfill (#12)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Richienb and sindresorhus committed Feb 14, 2020
1 parent dbcc379 commit c73efca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions index.js
Expand Up @@ -22,4 +22,10 @@ if (!global.AbortController) {
global.AbortController = AbortController;
}

if (!global.ReadableStream) {
try {
global.ReadableStream = require('web-streams-polyfill/ponyfill/es2018');
} catch (_) {}
}

module.exports = require('ky/umd');
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -62,7 +62,13 @@
"xo": "^0.25.3"
},
"peerDependencies": {
"ky": ">=0.17.0"
"ky": ">=0.17.0",
"web-streams-polyfill": ">=2.0.0"
},
"peerDependenciesMeta": {
"web-streams-polyfill": {
"optional": true
}
},
"browser": "browser.js",
"ava": {
Expand Down
22 changes: 22 additions & 0 deletions readme.md
Expand Up @@ -35,6 +35,28 @@ const ky = require('ky-universal');
})();
```

## `ReadableStream` support

For [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) support, also install [`web-streams-polyfill`](https://github.com/MattiasBuelens/web-streams-polyfill):

```
$ npm install web-streams-polyfill
```

You can then use it normally:

```js
const ky = require('ky-universal');

(async () => {
const {body} = await ky('https://httpbin.org/bytes/16');
const {value} = await body.getReader().read();
const result = new TextDecoder('utf-8').decode(value);

//
})();
```

## API

The API is exactly the same as the [Ky API](https://github.com/sindresorhus/ky#api).
Expand Down

0 comments on commit c73efca

Please sign in to comment.