├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── browser.d.ts ├── browser.js ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/ky-universal 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 20 14 | - 18 15 | - 16 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /browser.d.ts: -------------------------------------------------------------------------------- 1 | export {default} from 'ky'; 2 | export * from 'ky'; 3 | -------------------------------------------------------------------------------- /browser.js: -------------------------------------------------------------------------------- 1 | export {default} from 'ky'; 2 | export * from 'ky'; 3 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export {default} from 'ky'; 2 | export * from 'ky'; 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import fetch, {Headers, Request, Response} from 'node-fetch'; 2 | 3 | const TEN_MEGABYTES = 1000 * 1000 * 10; 4 | 5 | if (!globalThis.fetch) { 6 | globalThis.fetch = (url, options) => fetch(url, {highWaterMark: TEN_MEGABYTES, ...options}); 7 | } 8 | 9 | if (!globalThis.Headers) { 10 | globalThis.Headers = Headers; 11 | } 12 | 13 | if (!globalThis.Request) { 14 | globalThis.Request = Request; 15 | } 16 | 17 | if (!globalThis.Response) { 18 | globalThis.Response = Response; 19 | } 20 | 21 | if (!globalThis.ReadableStream) { 22 | try { 23 | globalThis.ReadableStream = await import('web-streams-polyfill/ponyfill/es2018'); 24 | } catch {} 25 | } 26 | 27 | const {default: ky, HTTPError, TimeoutError} = await import('ky'); 28 | 29 | export default ky; 30 | export {HTTPError, TimeoutError}; 31 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import ky, {type ResponsePromise} from './index.js'; 3 | 4 | expectType(ky('https://sindresorhus.com')); 5 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ky-universal", 3 | "version": "0.12.0", 4 | "description": "Use Ky in both Node.js and browsers", 5 | "license": "MIT", 6 | "repository": "sindresorhus/ky-universal", 7 | "funding": "https://github.com/sindresorhus/ky-universal?sponsor=1", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "node": "./index.js", 16 | "default": "./browser.js" 17 | }, 18 | "engines": { 19 | "node": ">=16" 20 | }, 21 | "scripts": { 22 | "test": "xo && ava && tsd" 23 | }, 24 | "files": [ 25 | "index.js", 26 | "index.d.ts", 27 | "browser.js", 28 | "browser.d.ts" 29 | ], 30 | "keywords": [ 31 | "ky", 32 | "universal", 33 | "isomorphic", 34 | "browser", 35 | "browsers", 36 | "node", 37 | "react", 38 | "vue", 39 | "ssr", 40 | "fetch", 41 | "request", 42 | "requests", 43 | "http", 44 | "https", 45 | "fetching", 46 | "get", 47 | "url", 48 | "curl", 49 | "wget", 50 | "net", 51 | "network", 52 | "ajax", 53 | "api", 54 | "rest", 55 | "xhr", 56 | "browser", 57 | "got", 58 | "axios", 59 | "node-fetch" 60 | ], 61 | "dependencies": { 62 | "node-fetch": "^3.3.2" 63 | }, 64 | "devDependencies": { 65 | "ava": "^5.3.1", 66 | "ky": "^0.33.3", 67 | "tsd": "^0.28.1", 68 | "xo": "^0.55.0" 69 | }, 70 | "peerDependencies": { 71 | "ky": ">=0.33.0", 72 | "web-streams-polyfill": ">=3.2.1" 73 | }, 74 | "peerDependenciesMeta": { 75 | "web-streams-polyfill": { 76 | "optional": true 77 | } 78 | }, 79 | "browser": "browser.js" 80 | } 81 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # ky-universal 2 | 3 | > Use Ky in both Node.js and browsers 4 | 5 | **As of [Ky 1.0.0](https://github.com/sindresorhus/ky/releases/tag/v1.0.0), it runs natively on Node.js. So this package is no longer needed.** 6 | 7 | [Ky](https://github.com/sindresorhus/ky) is made for browsers, but this package makes it possible to use it in Node.js too, by polyfilling most of the required browser APIs using [`node-fetch`](https://github.com/bitinn/node-fetch). 8 | 9 | This package can be useful for: 10 | - Isomorphic code 11 | - Web apps (React, Vue.js, etc.) that use server-side rendering (SSR) 12 | - Testing browser libraries using a Node.js test runner 13 | 14 | **Note:** Before opening an issue, make sure it's an issue with Ky and not its polyfills. Generally, if something works in the browser, but not in Node.js, it's an issue with `node-fetch`. 15 | 16 | Keep in mind that Ky targets [modern browsers](https://github.com/sindresorhus/ky#browser-support) when used in the browser. For older browsers, you will need to transpile and use a [`fetch` polyfill](https://github.com/github/fetch). 17 | 18 | ## Install 19 | 20 | ```sh 21 | npm install ky ky-universal 22 | ``` 23 | 24 | *Note that you also need to install `ky`.* 25 | 26 | ## Usage 27 | 28 | ```js 29 | import ky from 'ky-universal'; 30 | 31 | const parsed = await ky('https://httpbin.org/json').json(); 32 | 33 | // … 34 | ``` 35 | 36 | ## `ReadableStream` support 37 | 38 | 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): 39 | 40 | ``` 41 | $ npm install web-streams-polyfill 42 | ``` 43 | 44 | You can then use it normally: 45 | 46 | ```js 47 | import ky from 'ky-universal'; 48 | 49 | const {body} = await ky('https://httpbin.org/bytes/16'); 50 | const {value} = await body.getReader().read(); 51 | const result = new TextDecoder('utf-8').decode(value); 52 | 53 | // … 54 | ``` 55 | 56 | ## API 57 | 58 | The API is exactly the same as the [Ky API](https://github.com/sindresorhus/ky#api), including the [named exports](https://github.com/sindresorhus/ky#httperror). 59 | 60 | ## FAQ 61 | 62 | #### How do I use this with a web app (React, Vue.js, etc.) that uses server-side rendering (SSR)? 63 | 64 | Use it like you would use Ky: 65 | 66 | ```js 67 | import ky from 'ky-universal'; 68 | 69 | const parsed = await ky('https://httpbin.org/json').json(); 70 | 71 | // … 72 | ``` 73 | 74 | Webpack will ensure the polyfills are only included and used when the app is rendered on the server-side. 75 | 76 | #### How do I test a browser library that uses Ky in AVA? 77 | 78 | Put the following in package.json: 79 | 80 | ```json 81 | { 82 | "ava": { 83 | "require": [ 84 | "ky-universal" 85 | ] 86 | } 87 | } 88 | ``` 89 | 90 | The library that uses Ky will now *just work* in AVA tests. 91 | 92 | #### `clone()` hangs with a large response in Node - What should I do? 93 | 94 | 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. 95 | 96 | However, you can specify a custom `highWaterMark` if needed: 97 | 98 | ```js 99 | import ky from 'ky-universal'; 100 | 101 | const response = await ky('https://example.com', { 102 | // 20 MB 103 | highWaterMark: 1000 * 1000 * 20 104 | }); 105 | 106 | const data = await response.clone().buffer(); 107 | ``` 108 | 109 | ## Related 110 | 111 | - [ky](https://github.com/sindresorhus/ky) - Tiny and elegant HTTP client based on the browser Fetch API 112 | - [got](https://github.com/sindresorhus/got) - Simplified HTTP requests in Node.js 113 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import ky from './index.js'; 3 | 4 | test('main', async t => { 5 | const {slideshow} = await ky('https://httpbin.org/json').json(); 6 | t.is(slideshow.title, 'Sample Slide Show'); 7 | }); 8 | --------------------------------------------------------------------------------