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

BREAKING CHANGE: remove node-fetch in favor of global #580

Merged
merged 11 commits into from
May 21, 2023

Conversation

styfle
Copy link
Contributor

@styfle styfle commented May 13, 2023


Behavior

Before the change?

Previously you could run this on Node.js 14 and 16 without manually polyfilling fetch.

After the change?

With this PR it will work in Node.js 18, browsers, and any JS runtime that provides fetch. If you want to run in Node.js 14 or 16, you can polyfill globalThis.fetch or pass in the request options.

Other information

This improves compatibility with many runtimes because node-fetch is not the same as the standard fetch global.


Additional info

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Added the appropriate label for the given change

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes (Please add the Type: Breaking change label)

If Yes, what's the impact:

If you want to run in Node.js 14 or 16, you must polyfill globalThis.fetch or pass in the request options.

Pull request type

Please add the corresponding label for change this PR introduces:

  • Feature/model/API additions: Type: Feature

@kroese
Copy link

kroese commented May 13, 2023

You can also add this to package.json instead:

"overrides": {
    "node-fetch": "npm:node-fetch-native@latest"
  },

That way it doesnt require any changes to the existing code, and it will not be a breaking change anymore.

@wolfy1339 wolfy1339 added Type: Feature New feature or request Type: Breaking change Used to note any change that requires a major version bump labels May 13, 2023
@wolfy1339 wolfy1339 changed the title BREAKING CHANGE: remove node-fetch in favor of global feat: remove node-fetch in favor of global May 13, 2023
@wolfy1339
Copy link
Member

Can you fix the tests for Node 14 and 16?

@wolfy1339
Copy link
Member

I think adding a simple patch like so will work:

(async function () {
  if (parseInt(process.versions.node.split(".")[0]) <= 18) {
    // @ts-expect-error
    globalThis.fetch = await import("node-fetch");
  }
})();

@kroese
Copy link

kroese commented May 13, 2023

@wolfy1339 I think that way bundlers will still include node-fetch, because they cannot evaluate the if-expression during bundling.

With the solution I suggested I reduced the size of my app that requires octokit significantly.

@wolfy1339
Copy link
Member

The patch shouldn't go in any production code, just for the test files.

@kroese
Copy link

kroese commented May 13, 2023

The patch shouldn't go in any production code, just for the test files.

Okay, sorry I misunderstood. But I still think nodefetch-native would be an easier solution. It uses the native fetch when available (node 18+) and uses nodefetch 3.0 as a fallback for lower versions. This would allow request.js to transition to native fetch without breaking anything for older versions.

I am using request.js with it without any problems.

@styfle
Copy link
Contributor Author

styfle commented May 13, 2023

@wolfy1339 I used a similar solution but with require() so its synchronous a little less code.

I confirmed the tests are now passing on node 16 and 18.

@styfle styfle changed the title feat: remove node-fetch in favor of global BREAKING CHANGE: remove node-fetch in favor of global May 13, 2023
@wolfy1339
Copy link
Member

Even with node-fetch-native it wouldn't be a single change.

As node-fetch v3 drops the timeout option since it isn't in the spec, the types and code need updating.

@kroese
Copy link

kroese commented May 13, 2023

Even with node-fetch-native it wouldn't be a single change.

As node-fetch v3 drops the timeout option since it isn't in the spec, the types and code need updating.

I didn't notice anything about timeouts, all I can say is that I didn't notice any change in behaviour when using node-fetch-native but maybe there are some certain edge-cases where it doesn't behave the same.

@wolfy1339
Copy link
Member

We are using node-fetch v2 at the moment which had the timeout option.
In v3 it was removed.

https://github.com/node-fetch/node-fetch/blob/main/docs/v3-UPGRADE-GUIDE.md#the-timeout-option-was-removed

@gr2m
Copy link
Contributor

gr2m commented May 13, 2023

I would remove the dependency on node-fetch altogether. That's what we already did in octokit-next:
https://github.com/octokit/octokit-next.js/pull/62/files

A custom fetch function can be configured already. For users of older Node versions, we can document how to use node-fetch instead.

Node 16 is the last version that doesn't have it built in, and it will be out of maintenance mode in September. After that all current JavaScript runtime environments have the global fetch method. It might make sense to remove support for both Node 14 & Node 16 at this point, and while doing that also remove the dependency on node-fetch

@wolfy1339
Copy link
Member

We'll need to remove the timeout option as well, since it doesn't exist on the fetch API or newer versions of node-fetch.

@styfle
Copy link
Contributor Author

styfle commented May 15, 2023

There is a similar solution for timeout using

AbortSignal.timeout(opts.timeout)

Although if the consumer provides their own fetch then they can provider their own options too, so we can remove the options now that I think about it.

Let me know how to proceed.

@wolfy1339
Copy link
Member

Since this is going to be a breaking change, let's just remove that option from our code altogether

@gr2m
Copy link
Contributor

gr2m commented May 17, 2023

note that this will also be a breaking change for all the packages that depend on @octokit/request. If possible, I'd try to coordinate with other breaking changes in the dependents.

@wolfy1339 wolfy1339 changed the base branch from main to beta May 20, 2023 17:26
@@ -447,7 +451,7 @@ x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
});

it("passes node-fetch options to fetch only", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Remove timeout parameter tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it from the tests in ea7c2ab

Copy link
Member

Choose a reason for hiding this comment

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

These tests don't really seem useful now. Could you remove them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! Removed in 3cfc7c8

@wolfy1339
Copy link
Member

Will this resolve #432 @gr2m as node-fetch won't be included anymore

@gr2m
Copy link
Contributor

gr2m commented May 20, 2023

Will this resolve #432 @gr2m as node-fetch won't be included anymore

yes 🚀

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

I would recommend we also remove the request.timeout option while at it. We already recommend options.request.signal today

README.md Outdated Show resolved Hide resolved
src/fetch-wrapper.ts Show resolved Hide resolved
test/request.test.ts Outdated Show resolved Hide resolved
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>
@styfle
Copy link
Contributor Author

styfle commented May 20, 2023

Looks like I have some merge conflicts now. Do you prefer git rebase or git merge to resolve?

@styfle styfle requested review from gr2m and wolfy1339 May 20, 2023 23:23
@wolfy1339
Copy link
Member

Either way is fine. We squash and merge anyways in the end.

@gr2m
Copy link
Contributor

gr2m commented May 21, 2023

Merge is easier to keep track of new changes for reviewers.

@styfle
Copy link
Contributor Author

styfle commented May 21, 2023

@wolfy1339 @gr2m I fixed the merge conflicts and addressed the remaining comments, thanks!

@wolfy1339
Copy link
Member

There are some linting issues left to fix.

Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

Great work, just some lint errors left:
https://github.com/octokit/request.js/actions/runs/5039405053/jobs/9038110405?pr=580#step:5:7

running npm run lint:fix should do the trick

@wolfy1339 wolfy1339 merged commit a683296 into octokit:beta May 21, 2023
@gr2m
Copy link
Contributor

gr2m commented May 21, 2023

Wow this is a big day for Octokit's cross plattform support. Thank you all for making this a reality ❤️

@github-actions
Copy link

🎉 This PR is included in version 7.0.0-beta.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@styfle styfle deleted the remove-node-fetch branch May 22, 2023 02:03
wolfy1339 added a commit that referenced this pull request Jun 16, 2023
* ci: stop testing against NodeJS v14, v16 (#581)

BREAKING CHANGE: Drop support for NodeJS v14, v16

* feat: remove `node-fetch` in favor of global (#580)

BREAKING CHANGE: remove `node-fetch` in favor of global

* docs: update ToC for README.md

* fix(deps): update dependency @octokit/request-error to v4 (#587)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

* fix: bump `@octokit/request-error`

* fix(deps): bump `@octokit/types`

* fix(build): bump Octokit deps

* tests: adapt for `@octokit/endpoint` v8

* fix(deps): upgrade to `@octokit/endpoint` v8 stable

---------

Co-authored-by: Steven <steven@ceriously.com>
Co-authored-by: prettier-toc-me[bot] <56236715+prettier-toc-me[bot]@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released on @beta Type: Breaking change Used to note any change that requires a major version bump Type: Feature New feature or request
Projects
Archived in project
4 participants