Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional ReadableStream polyfill #12

Merged
merged 12 commits into from Feb 14, 2020
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');
})();
```
Richienb marked this conversation as resolved.
Show resolved Hide resolved

## `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);

// …
})();
Richienb marked this conversation as resolved.
Show resolved Hide resolved
```

## API

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