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

chore: replace node-fetch with undici #348

Merged
merged 1 commit into from
Jan 31, 2022
Merged

chore: replace node-fetch with undici #348

merged 1 commit into from
Jan 31, 2022

Conversation

threepointone
Copy link
Contributor

There are several reasons to replace node-fetch with undici:

  • undici's fetch() implementation is set to become node's standard fetch() implementation, which means we can just remove the dependency in the future (or optionally load it depending on which version of node is being used)
  • node-fetch pollutes the global type space with a number of standard types
  • we already bundle undici via miniflare/pages, so this means our bundle size could ostensibly become smaller.

This replaces node-fetch with undici.

  • All instances of import fetch from "node-fetch" are replaced with import {fetch} from "undici"
  • undici also comes with spec compliant forms of FormData and File, so we could also remove formdata-node in form_data.ts
  • All the global types that were injected by node-fetch are now imported from undici (as well as some mistaken ones from node:url)
  • NOTE: this also turns on skipLibCheck in tsconfig.json. Some dependencies oddly depend on browser globals like Request, Response (like @miniflare/core, jest-fetch-mock, etc), which now fail because node-fetch isn't injecting those globals anymore. So we enable skipLibCheck to bypass them. (I'd thought skipLibCheck completely ignores 'third party' types, but that's not true - it still uses the module graph to scan types. So we're still typesafe. We should enable strict sometime to avoid anys, but that's for later.)
  • The bundle size isn't smaller because we're bundling 2 different versions of undici, but we'll fix that by separately upping the version of undici that miniflare bundles.

@changeset-bot
Copy link

changeset-bot bot commented Jan 31, 2022

🦋 Changeset detected

Latest commit: be2c20d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@threepointone threepointone force-pushed the add-undici branch 2 times, most recently from 6b4a5e6 to 1ac3642 Compare January 31, 2022 09:20
@threepointone threepointone changed the title WIP chore: replace node-fetch with undici chore: replace node-fetch with undici Jan 31, 2022
@threepointone threepointone marked this pull request as ready for review January 31, 2022 09:21
@threepointone
Copy link
Contributor Author

I get a little nervous with reviews like this because pages doesn't have test coverage. @GregBrimble I don't expect anything to break by upping undici, but could you have a look? Also, could you send a PR that adds a simple pages project as example-pages-app so we can occasionally test manually? Thank you!

@@ -12,7 +12,13 @@
"jsx": "react",
"resolveJsonModule": true,
"incremental": true,
"strictNullChecks": true
"strictNullChecks": true,
"skipLibCheck": true
Copy link
Contributor Author

@threepointone threepointone Jan 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this will be the controversial part of this PR, so I'm calling it out right now. Have I misunderstood how this flag works, and does it make our codebase worse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/wrangler/src/__tests__/mock-cfetch.ts Outdated Show resolved Hide resolved
packages/wrangler/src/api/preview.ts Outdated Show resolved Hide resolved
packages/wrangler/src/api/preview.ts Show resolved Hide resolved
There are several reasons to replace `node-fetch` with `undici`:

- `undici`'s `fetch()` implementation is set to become node's standard `fetch()` implementation, which means we can just remove the dependency in the future (or optionally load it depending on which version of node is being used)
- `node-fetch` pollutes the global type space with a number of standard types
- we already bundle `undici` via `miniflare`/pages, so this means our bundle size could ostensibly become smaller.

This replaces `node-fetch` with `undici`.

- All instances of `import fetch from "node-fetch"` are replaced with `import {fetch} from "undici"`
- `undici` also comes with spec compliant forms of `FormData` and `File`, so we could also remove `formdata-node` in `form_data.ts`
- All the global types that were injected by `node-fetch` are now imported from `undici` (as well as some mistaken ones from `node:url`)
- NOTE: this also turns on `skipLibCheck` in `tsconfig.json`. Some dependencies oddly depend on browser globals like `Request`, `Response` (like `@miniflare/core`, `jest-fetch-mock`, etc), which now fail because `node-fetch` isn't injecting those globals anymore. So we enable `skipLibCheck` to bypass them. (I'd thought `skipLibCheck` completely ignores 'third party' types, but that's not true - it still uses the module graph to scan types. So we're still typesafe. We should enable `strict` sometime to avoid `any`s, but that's for later.)
- The bundle size isn't smaller because we're bundling 2 different versions of `undici`, but we'll fix that by separately upping the version of `undici` that miniflare bundles.
@threepointone threepointone merged commit b8e3b01 into main Jan 31, 2022
@threepointone threepointone deleted the add-undici branch January 31, 2022 21:12
@github-actions github-actions bot mentioned this pull request Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants