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
13 changes: 13 additions & 0 deletions index.js
Expand Up @@ -2,6 +2,15 @@
const fetch = require('node-fetch');
const AbortController = require('abort-controller');

// Extracted from https://github.com/Richienb/try-import/blob/master/index.js
function tryImport(name) {
try {
return require(name);
} catch (_) { }
}

const streams = tryImport('web-streams-polyfill/ponyfill/es2018');

if (!global.fetch) {
global.fetch = fetch;
}
Expand All @@ -22,4 +31,8 @@ if (!global.AbortController) {
global.AbortController = AbortController;
}

if (!global.ReadableStream && streams) {
global.ReadableStream = streams.ReadableStream;
Richienb marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = require('ky/umd');
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -64,6 +64,9 @@
"peerDependencies": {
"ky": ">=0.17.0"
},
"optionalDependencies": {
"web-streams-polyfill": ">=2.0.0"
Richienb marked this conversation as resolved.
Show resolved Hide resolved
},
"browser": "browser.js",
"ava": {
"register": [
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -23,6 +23,8 @@ $ npm install ky ky-universal

*Note that you also need to install `ky`.*

For `ReadableStream` support, also install [`web-streams-polyfill`](https://github.com/MattiasBuelens/web-streams-polyfill).
Richienb marked this conversation as resolved.
Show resolved Hide resolved

## Usage

```js
Expand Down