Skip to content

Commit

Permalink
Require Node.js 14
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 8, 2021
1 parent 24822c3 commit 2ff32a0
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 54 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ jobs:
matrix:
node-version:
- 14
- 12
- 10
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
Expand Down
4 changes: 4 additions & 0 deletions browser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ky from 'ky';

export default ky;
export * from 'ky';
4 changes: 3 additions & 1 deletion browser.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export {default} from 'ky';
import ky from 'ky';

export default ky;
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ky from 'ky';

export = ky;
export default ky;
export * from 'ky';
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
'use strict';
const fetch = require('node-fetch');
const AbortController = require('abort-controller');
import fetch, {Headers, Request, Response} from 'node-fetch';
import AbortController from 'abort-controller';
import ky from 'ky';

const TEN_MEGABYTES = 1000 * 1000 * 10;

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

if (!global.Headers) {
global.Headers = fetch.Headers;
if (!globalThis.Headers) {
globalThis.Headers = Headers;
}

if (!global.Request) {
global.Request = fetch.Request;
if (!globalThis.Request) {
globalThis.Request = Request;
}

if (!global.Response) {
global.Response = fetch.Response;
if (!globalThis.Response) {
globalThis.Response = Response;
}

if (!global.AbortController) {
global.AbortController = AbortController;
if (!globalThis.AbortController) {
globalThis.AbortController = AbortController;
}

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

module.exports = require('ky/umd');
export default ky;
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {expectType} from 'tsd';
import ky, {ResponsePromise} from '.';

expectType<ResponsePromise>(ky('https://sindresorhus.com'));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
36 changes: 28 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"import": "./browser.js",
"node": "./index.js"
},
"engines": {
"node": ">=10.17"
"node": ">=14"
},
"scripts": {
"test": "xo && ava"
"//": "XO is disabled until ESLint supports top-level await",
"//test": "xo && ava && tsd",
"test": "ava && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
"browser.js",
"browser.d.ts"
],
"keywords": [
"ky",
Expand Down Expand Up @@ -57,13 +65,14 @@
"node-fetch": "3.0.0-beta.9"
},
"devDependencies": {
"ava": "^2.4.0",
"ky": "^0.17.0",
"xo": "^0.25.3"
"ava": "^3.15.0",
"ky": "^0.26.0",
"tsd": "^0.14.0",
"xo": "^0.37.1"
},
"peerDependencies": {
"ky": ">=0.17.0",
"web-streams-polyfill": ">=2.0.0"
"ky": ">=0.26.0",
"web-streams-polyfill": ">=3.0.1"
},
"peerDependenciesMeta": {
"web-streams-polyfill": {
Expand All @@ -72,8 +81,19 @@
},
"browser": "browser.js",
"ava": {
"globals": [
"globalThis"
],
"register": [
"."
]
},
"tsd": {
"compilerOptions": {
"lib": [
"es2019",
"dom"
]
}
}
}
38 changes: 15 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ $ npm install ky ky-universal
## Usage

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

(async () => {
const parsed = await ky('https://httpbin.org/json').json();
const parsed = await ky('https://httpbin.org/json').json();

//
})();
//
```

## `ReadableStream` support
Expand All @@ -46,15 +44,13 @@ $ npm install web-streams-polyfill
You can then use it normally:

```js
const ky = require('ky-universal');
import ky from '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);
const {body} = await ky('https://httpbin.org/bytes/16');
const {value} = await body.getReader().read();
const result = new TextDecoder('utf-8').decode(value);

//
})();
//
```

## API
Expand All @@ -70,11 +66,9 @@ Use it like you would use Ky:
```js
import ky from 'ky-universal';

(async () => {
const parsed = await ky('https://httpbin.org/json').json();
const parsed = await ky('https://httpbin.org/json').json();

//
})();
//
```

Webpack will ensure the polyfills are only included and used when the app is rendered on the server-side.
Expand Down Expand Up @@ -104,14 +98,12 @@ 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 response = await ky('https://example.com', {
// 20 MB
highWaterMark: 1000 * 1000 * 20
});

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

## Related
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import ky from '.';
import ky from './index.js';

test('main', async t => {
const {slideshow} = await ky('https://httpbin.org/json').json();
Expand Down

0 comments on commit 2ff32a0

Please sign in to comment.