├── .github ├── dependabot.yml ├── funding.yml └── workflows │ ├── release.yml │ └── tests.yml ├── .gitignore ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── lib ├── browser.cjs ├── browser.mjs └── node.cjs ├── package.json └── tests ├── esbuild ├── esbuild-app.js ├── esbuild.test.js └── package.json ├── jest ├── jsdom.test.js ├── node.test.js └── package.json ├── node ├── node.test.cjs ├── node.test.mjs └── package.json └── rollup ├── package.json ├── rollup-app.js └── rollup.test.js /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Basic dependabot.yml file with 2 | # minimum configuration for two package managers 3 | 4 | version: 2 5 | updates: 6 | # Enable version updates for npm 7 | - package-ecosystem: "npm" 8 | # Look for `package.json` and `lock` files in the `root` directory 9 | directory: "/" 10 | # Check the npm registry for updates every day (weekdays) 11 | schedule: 12 | interval: "daily" 13 | - package-ecosystem: "npm" 14 | directory: "/tests/esbuild" 15 | schedule: 16 | interval: "daily" 17 | - package-ecosystem: "npm" 18 | directory: "/tests/jest" 19 | schedule: 20 | interval: "daily" 21 | - package-ecosystem: "npm" 22 | directory: "/tests/node" 23 | schedule: 24 | interval: "daily" 25 | - package-ecosystem: "npm" 26 | directory: "/tests/rollup" 27 | schedule: 28 | interval: "daily" 29 | # Enable updates to github actions 30 | - package-ecosystem: "github-actions" 31 | directory: "/" 32 | schedule: 33 | interval: "daily" 34 | 35 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: ['bcomnes'] 4 | custom: ['https://bret.io'] 5 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: npm bump 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | newversion: 7 | description: 'npm version {major,minor,patch}' 8 | required: true 9 | 10 | concurrency: # prevent concurrent releases 11 | group: npm-bump 12 | cancel-in-progress: true 13 | 14 | env: 15 | node_version: 'lts/*' 16 | 17 | jobs: 18 | version_and_release: 19 | runs-on: ubuntu-latest 20 | steps: 21 | - uses: actions/checkout@v4 22 | with: 23 | # fetch full history so things like auto-changelog work properly 24 | fetch-depth: 0 25 | - name: Use Node.js ${{ env.node_version }} 26 | uses: actions/setup-node@v4 27 | with: 28 | node-version: ${{ env.node_version }} 29 | # setting a registry enables the NODE_AUTH_TOKEN env variable where we can set an npm token. REQUIRED 30 | registry-url: 'https://registry.npmjs.org' 31 | - run: npm i 32 | - run: npm test 33 | - name: npm version && npm publish 34 | uses: bcomnes/npm-bump@v2 35 | with: 36 | git_email: bcomnes@gmail.com 37 | git_username: ${{ github.actor }} 38 | newversion: ${{ github.event.inputs.newversion }} 39 | github_token: ${{ secrets.GITHUB_TOKEN }} # built in actions token. Passed tp gh-release if in use. 40 | npm_token: ${{ secrets.NPM_TOKEN }} # user set secret token generated at npm 41 | 42 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: [pull_request, push] 4 | 5 | env: 6 | FORCE_COLOR: 1 7 | 8 | jobs: 9 | test: 10 | runs-on: ${{ matrix.os }} 11 | 12 | strategy: 13 | matrix: 14 | os: [ubuntu-latest] 15 | node: ['lts/*'] 16 | 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Use Node.js ${{ matrix.node }} 20 | uses: actions/setup-node@v4 21 | with: 22 | node-version: ${{ matrix.node }} 23 | - run: npm i 24 | - run: npm test 25 | 26 | automerge: 27 | needs: test 28 | runs-on: ubuntu-latest 29 | permissions: 30 | pull-requests: write 31 | contents: write 32 | steps: 33 | - uses: fastify/github-action-merge-dependabot@v3 34 | if: ${{ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request' && contains(github.head_ref, 'dependabot/github_actions') }} 35 | with: 36 | github-token: ${{secrets.github_token}} 37 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | .nyc_output 4 | package-lock.json 5 | .tap 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). 9 | 10 | ## [v5.0.0](https://github.com/bcomnes/fetch-undici/compare/v4.0.1...v5.0.0) 11 | 12 | ### Merged 13 | 14 | - Bump undici from 6.21.0 to 7.0.0 [`#89`](https://github.com/bcomnes/fetch-undici/pull/89) 15 | - Bump npm-run-all2 from 6.2.6 to 7.0.1 [`#88`](https://github.com/bcomnes/fetch-undici/pull/88) 16 | 17 | ## [v4.0.1](https://github.com/bcomnes/fetch-undici/compare/v4.0.0...v4.0.1) - 2024-09-25 18 | 19 | ### Merged 20 | 21 | - Bump tap from 20.0.3 to 21.0.0 in /tests/rollup [`#80`](https://github.com/bcomnes/fetch-undici/pull/80) 22 | - Bump tap from 20.0.3 to 21.0.0 in /tests/esbuild [`#79`](https://github.com/bcomnes/fetch-undici/pull/79) 23 | - Bump tap from 20.0.3 to 21.0.0 in /tests/node [`#78`](https://github.com/bcomnes/fetch-undici/pull/78) 24 | - Bump tap from 19.2.5 to 20.0.0 in /tests/rollup [`#75`](https://github.com/bcomnes/fetch-undici/pull/75) 25 | - Bump tap from 19.2.5 to 20.0.0 in /tests/node [`#76`](https://github.com/bcomnes/fetch-undici/pull/76) 26 | - Bump tap from 19.2.5 to 20.0.0 in /tests/esbuild [`#77`](https://github.com/bcomnes/fetch-undici/pull/77) 27 | - Bump @rollup/plugin-commonjs from 25.0.8 to 26.0.1 in /tests/rollup [`#74`](https://github.com/bcomnes/fetch-undici/pull/74) 28 | - Bump tap from 18.8.0 to 19.0.0 in /tests/rollup [`#71`](https://github.com/bcomnes/fetch-undici/pull/71) 29 | - Bump tap from 18.8.0 to 19.0.0 in /tests/esbuild [`#72`](https://github.com/bcomnes/fetch-undici/pull/72) 30 | - Bump tap from 18.8.0 to 19.0.0 in /tests/node [`#73`](https://github.com/bcomnes/fetch-undici/pull/73) 31 | - Bump jsdom from 23.2.0 to 24.0.0 in /tests/rollup [`#70`](https://github.com/bcomnes/fetch-undici/pull/70) 32 | - Bump jsdom from 23.2.0 to 24.0.0 in /tests/esbuild [`#69`](https://github.com/bcomnes/fetch-undici/pull/69) 33 | 34 | ### Fixed 35 | 36 | - Exclude tests from tarball [`#84`](https://github.com/bcomnes/fetch-undici/issues/84) 37 | 38 | ### Commits 39 | 40 | - Merge pull request #85 from bcomnes/remove-tests [`1852f46`](https://github.com/bcomnes/fetch-undici/commit/1852f461a9bdca8df6be3d8b5d32dce0a1d22349) 41 | - Bump minimum undici version [`8f91e92`](https://github.com/bcomnes/fetch-undici/commit/8f91e92494f79503ab1f5932f403188c5c1f01de) 42 | - Merge pull request #83 from bcomnes/dependabot/npm_and_yarn/tests/rollup/rollup/plugin-commonjs-28.0.0 [`b8872e1`](https://github.com/bcomnes/fetch-undici/commit/b8872e1583ea4039f03675882037786e4a290118) 43 | 44 | ## [v4.0.0](https://github.com/bcomnes/fetch-undici/compare/v3.0.2...v4.0.0) - 2023-12-07 45 | 46 | ### Merged 47 | 48 | - Bump undici from 5.28.2 to 6.0.1 [`#68`](https://github.com/bcomnes/fetch-undici/pull/68) 49 | - Bump jsdom from 22.1.0 to 23.0.0 in /tests/rollup [`#65`](https://github.com/bcomnes/fetch-undici/pull/65) 50 | - Bump jsdom from 22.1.0 to 23.0.0 in /tests/esbuild [`#66`](https://github.com/bcomnes/fetch-undici/pull/66) 51 | 52 | ## [v3.0.2](https://github.com/bcomnes/fetch-undici/compare/v3.0.1...v3.0.2) - 2023-11-23 53 | 54 | ### Merged 55 | 56 | - Bump actions/setup-node from 3 to 4 [`#64`](https://github.com/bcomnes/fetch-undici/pull/64) 57 | - Bump actions/checkout from 3 to 4 [`#45`](https://github.com/bcomnes/fetch-undici/pull/45) 58 | - Bump p-temporary-directory from 1.1.1 to 2.0.1 in /tests/rollup [`#43`](https://github.com/bcomnes/fetch-undici/pull/43) 59 | - Bump p-temporary-directory from 1.1.1 to 2.0.1 in /tests/esbuild [`#44`](https://github.com/bcomnes/fetch-undici/pull/44) 60 | - Bump @rollup/plugin-commonjs from 24.1.0 to 25.0.0 in /tests/rollup [`#42`](https://github.com/bcomnes/fetch-undici/pull/42) 61 | - Bump jsdom from 21.1.2 to 22.0.0 in /tests/rollup [`#40`](https://github.com/bcomnes/fetch-undici/pull/40) 62 | - Bump jsdom from 21.1.2 to 22.0.0 in /tests/esbuild [`#41`](https://github.com/bcomnes/fetch-undici/pull/41) 63 | - Bump bcomnes/npm-bump from 2.1.0 to 2.2.1 [`#39`](https://github.com/bcomnes/fetch-undici/pull/39) 64 | - Bump jsdom from 20.0.3 to 21.0.0 in /tests/esbuild [`#37`](https://github.com/bcomnes/fetch-undici/pull/37) 65 | - Bump jsdom from 20.0.3 to 21.0.0 in /tests/rollup [`#38`](https://github.com/bcomnes/fetch-undici/pull/38) 66 | - Bump @rollup/plugin-commonjs from 23.0.7 to 24.0.0 in /tests/rollup [`#36`](https://github.com/bcomnes/fetch-undici/pull/36) 67 | - Bump bcomnes/npm-bump from 2.0.2 to 2.1.0 [`#34`](https://github.com/bcomnes/fetch-undici/pull/34) 68 | - Bump actions/setup-node from 3.5.0 to 3.5.1 [`#33`](https://github.com/bcomnes/fetch-undici/pull/33) 69 | - Bump @rollup/plugin-node-resolve from 14.1.0 to 15.0.0 in /tests/rollup [`#32`](https://github.com/bcomnes/fetch-undici/pull/32) 70 | - Bump @rollup/plugin-commonjs from 22.0.2 to 23.0.0 in /tests/rollup [`#31`](https://github.com/bcomnes/fetch-undici/pull/31) 71 | - Bump actions/setup-node from 3.4.1 to 3.5.0 [`#30`](https://github.com/bcomnes/fetch-undici/pull/30) 72 | - Bump actions/setup-node from 3.4.0 to 3.4.1 [`#28`](https://github.com/bcomnes/fetch-undici/pull/28) 73 | - Bump actions/setup-node from 3.3.0 to 3.4.0 [`#27`](https://github.com/bcomnes/fetch-undici/pull/27) 74 | - Bump jsdom from 19.0.0 to 20.0.0 in /tests/rollup [`#26`](https://github.com/bcomnes/fetch-undici/pull/26) 75 | - Bump jsdom from 19.0.0 to 20.0.0 in /tests/esbuild [`#25`](https://github.com/bcomnes/fetch-undici/pull/25) 76 | - Bump npm-run-all2 from 5.0.2 to 6.0.0 [`#24`](https://github.com/bcomnes/fetch-undici/pull/24) 77 | - Bump actions/setup-node from 3.2.0 to 3.3.0 [`#23`](https://github.com/bcomnes/fetch-undici/pull/23) 78 | - Bump actions/setup-node from 3.1.1 to 3.2.0 [`#22`](https://github.com/bcomnes/fetch-undici/pull/22) 79 | 80 | ### Commits 81 | 82 | - Update tap [`931106d`](https://github.com/bcomnes/fetch-undici/commit/931106d228f4c9fec53116e7a3a27a5ceb66c40a) 83 | - Update versions [`a1217fd`](https://github.com/bcomnes/fetch-undici/commit/a1217fd1f45d395514a91224390f4bc31675fc0d) 84 | - Remove files filter [`646d36b`](https://github.com/bcomnes/fetch-undici/commit/646d36b635be0ea6166c11ef4b1e359dff096474) 85 | 86 | ## [v3.0.1](https://github.com/bcomnes/fetch-undici/compare/v3.0.0...v3.0.1) - 2022-05-15 87 | 88 | ### Merged 89 | 90 | - Improve node import [`#21`](https://github.com/bcomnes/fetch-undici/pull/21) 91 | 92 | ### Commits 93 | 94 | - Use a conditional import on undici in node [`eceea56`](https://github.com/bcomnes/fetch-undici/commit/eceea56b8ae7895514d15d4e357a58ff8725000c) 95 | - Fix conditional export [`385f0ec`](https://github.com/bcomnes/fetch-undici/commit/385f0ecaf3d123e05bf94f8299791ea7491e12ea) 96 | - Use updated test asserts [`5bc5aa7`](https://github.com/bcomnes/fetch-undici/commit/5bc5aa7caf3b105446bbc301b043fbc7aa06b475) 97 | 98 | ## [v3.0.0](https://github.com/bcomnes/fetch-undici/compare/v2.0.0...v3.0.0) - 2022-05-12 99 | 100 | ### Merged 101 | 102 | - Export native fetch in node and add native boolean export [`#19`](https://github.com/bcomnes/fetch-undici/pull/19) 103 | - Bump standard from 16.0.4 to 17.0.0 [`#18`](https://github.com/bcomnes/fetch-undici/pull/18) 104 | - Bump actions/setup-node from 3.1.0 to 3.1.1 [`#17`](https://github.com/bcomnes/fetch-undici/pull/17) 105 | - Bump fastify/github-action-merge-dependabot from 3.0.2 to 3.1 [`#16`](https://github.com/bcomnes/fetch-undici/pull/16) 106 | - Bump actions/setup-node from 3.0.0 to 3.1.0 [`#15`](https://github.com/bcomnes/fetch-undici/pull/15) 107 | 108 | ### Commits 109 | 110 | - **Breaking change:** Export native fetch in node and add native boolean export [`58b65eb`](https://github.com/bcomnes/fetch-undici/commit/58b65eb746332a56df53dda1df9197f3da20edb6) 111 | - Update tests.yml [`c4ffd4a`](https://github.com/bcomnes/fetch-undici/commit/c4ffd4ae2e8e9a173c8d1aa825bfd8f7abc43b27) 112 | - Fix test node versions [`0b6684d`](https://github.com/bcomnes/fetch-undici/commit/0b6684d14d1d670ded056a997d65e2473f7b7de4) 113 | 114 | ## [v2.0.0](https://github.com/bcomnes/fetch-undici/compare/v1.1.0...v2.0.0) - 2022-03-30 115 | 116 | ### Merged 117 | 118 | - Bump undici from 4.16.0 to 5.0.0 [`#14`](https://github.com/bcomnes/fetch-undici/pull/14) 119 | - Bump actions/checkout from 2.4.0 to 3 [`#13`](https://github.com/bcomnes/fetch-undici/pull/13) 120 | - Bump actions/setup-node from 2.5.1 to 3.0.0 [`#12`](https://github.com/bcomnes/fetch-undici/pull/12) 121 | 122 | ## [v1.1.0](https://github.com/bcomnes/fetch-undici/compare/v1.0.5...v1.1.0) - 2022-01-27 123 | 124 | ### Merged 125 | 126 | - Export Headers, Response, Request [`#11`](https://github.com/bcomnes/fetch-undici/pull/11) 127 | - Bump fastify/github-action-merge-dependabot from 2.7.1 to 3.0.2 [`#9`](https://github.com/bcomnes/fetch-undici/pull/9) 128 | - Bump actions/setup-node from 2.5.0 to 2.5.1 [`#10`](https://github.com/bcomnes/fetch-undici/pull/10) 129 | - Bump fastify/github-action-merge-dependabot from 2.7.0 to 2.7.1 [`#8`](https://github.com/bcomnes/fetch-undici/pull/8) 130 | - Bump fastify/github-action-merge-dependabot from 2.6.0 to 2.7.0 [`#7`](https://github.com/bcomnes/fetch-undici/pull/7) 131 | - Bump actions/setup-node from 2.4.1 to 2.5.0 [`#6`](https://github.com/bcomnes/fetch-undici/pull/6) 132 | - Bump fastify/github-action-merge-dependabot from 2.5.0 to 2.6.0 [`#5`](https://github.com/bcomnes/fetch-undici/pull/5) 133 | - Bump actions/checkout from 2.3.5 to 2.4.0 [`#4`](https://github.com/bcomnes/fetch-undici/pull/4) 134 | - Bump actions/checkout from 2.3.4 to 2.3.5 [`#3`](https://github.com/bcomnes/fetch-undici/pull/3) 135 | - Bump actions/setup-node from 2.4.0 to 2.4.1 [`#2`](https://github.com/bcomnes/fetch-undici/pull/2) 136 | - Bump fastify/github-action-merge-dependabot from 2.4.0 to 2.5.0 [`#1`](https://github.com/bcomnes/fetch-undici/pull/1) 137 | 138 | ### Commits 139 | 140 | - Fix permissions [`d18a79c`](https://github.com/bcomnes/fetch-undici/commit/d18a79c529762c519dc74e54fee37980ee2c08b9) 141 | 142 | ## [v1.0.5](https://github.com/bcomnes/fetch-undici/compare/v1.0.4...v1.0.5) - 2021-09-09 143 | 144 | ### Commits 145 | 146 | - Set up files field [`6ed677b`](https://github.com/bcomnes/fetch-undici/commit/6ed677b01483538b4491313833a7db3acb9ae3df) 147 | - Add engines field node >= 16 [`538f293`](https://github.com/bcomnes/fetch-undici/commit/538f293ad3d231acfb6c22183418a4acb5d62f17) 148 | 149 | ## [v1.0.4](https://github.com/bcomnes/fetch-undici/compare/v1.0.3...v1.0.4) - 2021-09-08 150 | 151 | ### Commits 152 | 153 | - Add more tests [`39f3963`](https://github.com/bcomnes/fetch-undici/commit/39f3963dcc5553a83cc63e6911e5cdf61faef813) 154 | - Add note test and fix cjs node import [`6447561`](https://github.com/bcomnes/fetch-undici/commit/6447561c88bce51be2005a15839d0555928a81c1) 155 | - Fix CI [`afb43fa`](https://github.com/bcomnes/fetch-undici/commit/afb43fa2b2affbd15d04cb8fbd41aa4ebc985258) 156 | 157 | ## [v1.0.3](https://github.com/bcomnes/fetch-undici/compare/v1.0.2...v1.0.3) - 2021-09-06 158 | 159 | ### Commits 160 | 161 | - Fix name agian [`7e77b1f`](https://github.com/bcomnes/fetch-undici/commit/7e77b1fbc8ee3a218a7e5649b302b4b7c5eb968c) 162 | 163 | ## [v1.0.2](https://github.com/bcomnes/fetch-undici/compare/v1.0.1...v1.0.2) - 2021-09-06 164 | 165 | ### Commits 166 | 167 | - Fix global clobbers [`ea67b68`](https://github.com/bcomnes/fetch-undici/commit/ea67b681c29943ade8fcf223d6dac71ec3db9c56) 168 | 169 | ## [v1.0.1](https://github.com/bcomnes/fetch-undici/compare/v1.0.0...v1.0.1) - 2021-09-06 170 | 171 | ### Commits 172 | 173 | - Rename [`2a5a5d4`](https://github.com/bcomnes/fetch-undici/commit/2a5a5d4dba6a1fab9790fba0abf7d88f0527ed0a) 174 | 175 | ## v1.0.0 - 2021-09-06 176 | 177 | ### Commits 178 | 179 | - Init [`5e33066`](https://github.com/bcomnes/fetch-undici/commit/5e3306609c31176f2793c7a8ed9ba64a093f9fa6) 180 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Releasing 4 | 5 | Changelog, and releasing is autmated with npm scripts and actions. To create a release: 6 | 7 | - Navigate to the actions tab 8 | - Select the `npm bump` action. 9 | - Trigger an action, specifying the semantic version bump that is needed. 10 | - Changelog, Github release and npm publish is hanlded by the action. 11 | - An in depth review of this system is documented here: [bret.io/projects/package-automation](https://bret.io/projects/package-automation/) 12 | 13 | If for some reason that isn't workoing or a local release is preferred, follow these steps: 14 | 15 | - Ensure a clean working git workspace. 16 | - Run `npm version {patch,minor,major}`. 17 | - This wills update the version number and generate the changelog. 18 | - Run `npm publish`. 19 | - This will push your local git branch and tags to the default remote, perform a [gh-release](https://ghub.io/gh-release), and create an npm publication 20 | 21 | ## Guidelines 22 | 23 | - Patches, ideas and changes welcome. 24 | - Fixes almost always welcome. 25 | - Features sometimes welcome. 26 | - Please open an issue to discuss the issue prior to spending lots of time on the problem. 27 | - It may be rejected. 28 | - If you don't want to wait around for the discussion to commence, and you really want to jump into the implementation work, be prepared for fork if the idea is respectfully declined. 29 | - Try to stay within the style of the existing code. 30 | - All tests must pass. 31 | - Additional features or code paths must be tested. 32 | - Aim for 100% coverage. 33 | - Questions are welcome, however unless there is a official support contract established between the maintainers and the requester, support is not guaranteed. 34 | - Contributors reserve the right to walk away from this project at any moment with or without notice. 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Bret Comnes 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fetch-undici 2 | [![Actions Status](https://github.com/bcomnes/fetch-undici/workflows/tests/badge.svg)](https://github.com/bcomnes/fetch-undici/actions) 3 | 4 | Isomorphic exports of `fetch`, providing [`window.fetch`][br] in the browser, and [`undici.fetch`][ud] in node.js. 5 | 6 | ``` 7 | npm install fetch-undici 8 | ``` 9 | 10 | ## Usage 11 | 12 | 13 | ``` js 14 | import { 15 | fetch, 16 | Headers, 17 | Response, 18 | Request 19 | } from 'fetch-undici'; 20 | ``` 21 | 22 | ## Why 23 | 24 | If you want to write an API client of some kind, that would be useful in the browser or in Node.js, `fetch-undici` provides the best implementation of `fetch` for the environment it's used in. 25 | 26 | ## How 27 | 28 | `fetch-undici` works by utilizing environment specific exports fields so that you can have dependency injection at the built-in node module resolver layer. 29 | It supports the following export fields: 30 | 31 | - `main` (cjs node) 32 | - `browser` (cjs browser) 33 | - `exports.node` (cjs node) 34 | - `exports.browser` (esm browser) 35 | 36 | ## License 37 | 38 | MIT 39 | 40 | [br]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API 41 | [ud]: https://github.com/nodejs/undici 42 | -------------------------------------------------------------------------------- /lib/browser.cjs: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | fetch: window.fetch, 3 | Headers: globalThis.Headers, 4 | Response: globalThis.Response, 5 | Request: globalThis.Request, 6 | type: 'browser.cjs', 7 | native: true 8 | } 9 | -------------------------------------------------------------------------------- /lib/browser.mjs: -------------------------------------------------------------------------------- 1 | const fetch = window.fetch 2 | const type = 'browser.mjs' 3 | const native = true 4 | 5 | const Headers = globalThis.Headers 6 | const Response = globalThis.Response 7 | const Request = globalThis.Request 8 | 9 | export { 10 | fetch, 11 | type, 12 | Headers, 13 | Response, 14 | Request, 15 | native 16 | } 17 | -------------------------------------------------------------------------------- /lib/node.cjs: -------------------------------------------------------------------------------- 1 | const native = globalThis?.fetch != null 2 | exports.native = native 3 | 4 | const fetchSource = native ? globalThis : require('undici') 5 | 6 | exports.fetch = fetchSource?.fetch 7 | exports.Headers = fetchSource?.Headers 8 | exports.Response = fetchSource?.Response 9 | exports.Request = fetchSource?.Request 10 | 11 | exports.type = 'node.cjs' 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fetch-undici", 3 | "description": "Isomorphic exports of fetch from window and unduci", 4 | "version": "5.0.0", 5 | "author": "Bret Comnes (https://bret.io)", 6 | "bugs": { 7 | "url": "https://github.com/bcomnes/fetch-undici/issues" 8 | }, 9 | "dependencies": { 10 | "undici": "^7.0.0" 11 | }, 12 | "devDependencies": { 13 | "auto-changelog": "^2.2.0", 14 | "gh-release": "^7.0.0", 15 | "npm-run-all2": "^8.0.1", 16 | "semver": "^7.3.7", 17 | "standard": "^17.0.0" 18 | }, 19 | "homepage": "https://github.com/bcomnes/fetch-undici", 20 | "keywords": [], 21 | "license": "MIT", 22 | "main": "./lib/node.cjs", 23 | "browser": "./lib/browser.cjs", 24 | "module": "./lib/browser.mjs", 25 | "exports": { 26 | "browser": "./lib/browser.mjs", 27 | "node": "./lib/node.cjs" 28 | }, 29 | "files": [ 30 | "lib/", 31 | "README.md", 32 | "LICENSE", 33 | "CHANGELOG.md", 34 | "CONTRIBUTING.md" 35 | ], 36 | "repository": { 37 | "type": "git", 38 | "url": "https://github.com/bcomnes/fetch-undici.git" 39 | }, 40 | "scripts": { 41 | "prepublishOnly": "git push --follow-tags && gh-release -y", 42 | "test": "run-s test:*", 43 | "test:standard": "standard", 44 | "test:esbuild": "cd tests/esbuild && npm i && npm test", 45 | "test:jest": "cd tests/jest && npm i && npm test", 46 | "test:rollup": "cd tests/rollup && npm i && npm test", 47 | "test:node": "cd tests/node && npm i && npm test", 48 | "version": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:' && git add CHANGELOG.md" 49 | }, 50 | "standard": { 51 | "ignore": [ 52 | "dist" 53 | ] 54 | }, 55 | "funding": { 56 | "type": "individual", 57 | "url": "https://github.com/sponsors/bcomnes" 58 | }, 59 | "engines": { 60 | "node": ">=16" 61 | }, 62 | "tap": { 63 | "serial": [], 64 | "typecheck": false, 65 | "allow-incomplete-coverage": true, 66 | "allow-empty-coverage": true 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/esbuild/esbuild-app.js: -------------------------------------------------------------------------------- 1 | import { type, native } from 'fetch-undici' 2 | 3 | export { type, native } 4 | -------------------------------------------------------------------------------- /tests/esbuild/esbuild.test.js: -------------------------------------------------------------------------------- 1 | import tap from 'tap' 2 | import esbuild from 'esbuild' 3 | import { join } from 'node:path' 4 | import desm from 'desm' 5 | import { JSDOM } from 'jsdom' 6 | import tmp from 'p-temporary-directory' 7 | import { writeFile } from 'node:fs/promises' 8 | 9 | const { window } = new JSDOM('', { 10 | url: 'https://example.org/', 11 | referrer: 'https://example.com/', 12 | contentType: 'text/html', 13 | includeNodeLocations: true, 14 | storageQuota: 10000000 15 | }) 16 | 17 | global.window = window 18 | 19 | const __dirname = desm(import.meta.url) 20 | 21 | tap.test('test build with esbuild', async t => { 22 | const [dir, cleanup] = await tmp() 23 | 24 | const results = await esbuild.build({ 25 | entryPoints: [join(__dirname, 'esbuild-app.js')], 26 | bundle: true, 27 | write: true, 28 | format: 'esm', 29 | splitting: true, 30 | sourcemap: true, 31 | outdir: join(dir), 32 | target: ['es2020'] 33 | }) 34 | 35 | tap.equal(results.errors.length, 0) 36 | 37 | await writeFile(join(dir, 'package.json'), '{ "type": "module" }') 38 | 39 | const { type, native } = await import(join(dir, 'esbuild-app.js')) 40 | tap.equal(type, 'browser.mjs') 41 | tap.equal(native, true) 42 | await cleanup() 43 | }) 44 | -------------------------------------------------------------------------------- /tests/esbuild/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "esbuild", 3 | "version": "0.0.0", 4 | "description": "", 5 | "main": "esbuild-app.js", 6 | "type": "module", 7 | "scripts": { 8 | "test": "tap" 9 | }, 10 | "keywords": [], 11 | "author": "Bret Comnes (https://bret.io/)", 12 | "license": "MIT", 13 | "dependencies": { 14 | "desm": "^1.2.0", 15 | "esbuild": "^0.14.39", 16 | "fetch-undici": "../../.", 17 | "jsdom": "^26.0.0", 18 | "p-temporary-directory": "^2.0.1", 19 | "tap": "^21.0.0" 20 | }, 21 | "tap": { 22 | "serial": [], 23 | "typecheck": false, 24 | "allow-incomplete-coverage": true, 25 | "allow-empty-coverage": true, 26 | "coverage-report": [ 27 | "text", 28 | "lcovonly" 29 | ] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/jest/jsdom.test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | /** 3 | * @jest-environment jsdom 4 | */ 5 | 6 | const process = require('process') 7 | const semver = require('semver') 8 | const { fetch, type, native } = require('fetch-undici') 9 | 10 | test('jsdom in mjs', () => { 11 | expect(type).toBe('node.cjs') 12 | expect(fetch).toBeDefined() 13 | 14 | const supportsFetch = semver.satisfies(process.version, '>=18.0.0') 15 | if (supportsFetch) expect(native).toBe(true) 16 | else expect(native).toBe(false) 17 | }) 18 | -------------------------------------------------------------------------------- /tests/jest/node.test.js: -------------------------------------------------------------------------------- 1 | /* eslint-env jest */ 2 | 3 | const process = require('process') 4 | const semver = require('semver') 5 | const { fetch, type, native } = require('fetch-undici') 6 | 7 | test('node in cjs', () => { 8 | expect(type).toBe('node.cjs') 9 | expect(fetch).toBeDefined() 10 | 11 | const supportsFetch = semver.satisfies(process.version, '>=18.0.0') 12 | if (supportsFetch) expect(native).toBe(true) 13 | else expect(native).toBe(false) 14 | }) 15 | -------------------------------------------------------------------------------- /tests/jest/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest", 3 | "version": "0.0.0", 4 | "description": "", 5 | "main": "jsdom.test.js", 6 | "scripts": { 7 | "test": "jest" 8 | }, 9 | "keywords": [], 10 | "author": "Bret Comnes (https://bret.io/)", 11 | "license": "MIT", 12 | "dependencies": { 13 | "fetch-undici": "../../.", 14 | "jest": "^28.1.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/node/node.test.cjs: -------------------------------------------------------------------------------- 1 | const tap = require('tap') 2 | const process = require('process') 3 | const semver = require('semver') 4 | const { type, fetch, native } = require('fetch-undici') 5 | 6 | tap.test('node gets undici cjs', async t => { 7 | t.equal(type, 'node.cjs') 8 | t.ok(fetch) 9 | 10 | const supportsFetch = semver.satisfies(process.version, '>=18.0.0') 11 | if (supportsFetch) t.ok(native, 'exported native fetch in node versions >=18') 12 | else t.false(native, 'exported undici fetch in node versions <18') 13 | }) 14 | -------------------------------------------------------------------------------- /tests/node/node.test.mjs: -------------------------------------------------------------------------------- 1 | import tap from 'tap' 2 | import process from 'process' 3 | import semver from 'semver' 4 | import { type, fetch, native } from 'fetch-undici' 5 | 6 | tap.test('node esm gets undici cjs', async t => { 7 | t.equal(type, 'node.cjs') 8 | t.ok(fetch) 9 | 10 | const supportsFetch = semver.satisfies(process.version, '>=18.0.0') 11 | if (supportsFetch) t.ok(native, 'exported native fetch in node versions >=18') 12 | else t.false(native, 'exported undici fetch in node versions <18') 13 | }) 14 | -------------------------------------------------------------------------------- /tests/node/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node", 3 | "version": "0.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "tap" 8 | }, 9 | "keywords": [], 10 | "author": "Bret Comnes (https://bret.io/)", 11 | "license": "MIT", 12 | "dependencies": { 13 | "fetch-undici": "../../.", 14 | "tap": "^21.0.0" 15 | }, 16 | "tap": { 17 | "serial": [], 18 | "typecheck": false, 19 | "allow-incomplete-coverage": true, 20 | "allow-empty-coverage": true, 21 | "coverage-report": [ 22 | "text", 23 | "lcovonly" 24 | ] 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/rollup/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup", 3 | "version": "0.0.0", 4 | "description": "", 5 | "main": "rollup-app.js", 6 | "scripts": { 7 | "test": "tap" 8 | }, 9 | "keywords": [], 10 | "author": "Bret Comnes (https://bret.io/)", 11 | "license": "MIT", 12 | "type": "module", 13 | "dependencies": { 14 | "@rollup/plugin-commonjs": "^28.0.0", 15 | "@rollup/plugin-node-resolve": "^16.0.0", 16 | "fetch-undici": "../../.", 17 | "rollup": "^2.56.3", 18 | "desm": "^1.2.0", 19 | "jsdom": "^26.0.0", 20 | "p-temporary-directory": "^2.0.1", 21 | "tap": "^21.0.0" 22 | }, 23 | "tap": { 24 | "serial": [], 25 | "typecheck": false, 26 | "allow-incomplete-coverage": true, 27 | "allow-empty-coverage": true, 28 | "coverage-report": [ 29 | "text", 30 | "lcovonly" 31 | ] 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /tests/rollup/rollup-app.js: -------------------------------------------------------------------------------- 1 | import { fetch, type, native } from 'fetch-undici' 2 | 3 | export { type, fetch, native } 4 | -------------------------------------------------------------------------------- /tests/rollup/rollup.test.js: -------------------------------------------------------------------------------- 1 | import tap from 'tap' 2 | import { join } from 'node:path' 3 | import desm from 'desm' 4 | import { JSDOM } from 'jsdom' 5 | import tmp from 'p-temporary-directory' 6 | import { writeFile } from 'node:fs/promises' 7 | import { rollup } from 'rollup' 8 | import commonjs from '@rollup/plugin-commonjs' 9 | import resolve from '@rollup/plugin-node-resolve' 10 | 11 | const { window } = new JSDOM('', { 12 | url: 'https://example.org/', 13 | referrer: 'https://example.com/', 14 | contentType: 'text/html', 15 | includeNodeLocations: true, 16 | storageQuota: 10000000 17 | }) 18 | 19 | global.window = window 20 | 21 | const __dirname = desm(import.meta.url) 22 | 23 | tap.test('test build with esbuild', async t => { 24 | const [dir, cleanup] = await tmp() 25 | console.log(dir) 26 | const inpuOpts = { 27 | input: [join(__dirname, 'rollup-app.js')], 28 | plugins: [ 29 | resolve({ browser: true }), 30 | commonjs() 31 | ] 32 | } 33 | 34 | const bundle = await rollup(inpuOpts) 35 | 36 | const outputOpts = { 37 | dir, 38 | format: 'es' 39 | } 40 | 41 | await bundle.write(outputOpts) 42 | 43 | await writeFile(join(dir, 'package.json'), '{ "type": "module" }') 44 | 45 | const { type, native } = await import(join(dir, 'rollup-app.js')) 46 | tap.equal(type, 'browser.mjs') 47 | tap.ok(native, 'native browser export in rollup') 48 | await cleanup() 49 | }) 50 | --------------------------------------------------------------------------------