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

fix: support global "_Headers" in Undici 5.26.3 #73

Merged
merged 4 commits into from
Oct 18, 2023
Merged

fix: support global "_Headers" in Undici 5.26.3 #73

merged 4 commits into from
Oct 18, 2023

Conversation

joshkel
Copy link
Contributor

@joshkel joshkel commented Oct 16, 2023

Node.js's update to Undici 5.26.3 caused its Headers class to be called _Headers:

https://github.com/nodejs/node/pull/50153/files#diff-f516ab824a7722da938a4c7c851520d39731ddeb4f7198dff4e932c5d4f8fdf7

Fixes #72.

@joshkel joshkel marked this pull request as draft October 16, 2023 23:35
@joshkel
Copy link
Contributor Author

joshkel commented Oct 16, 2023

Alternatively, would it be better to check instanceof if a global Headers class exists? Perhaps something like this?

    /**
     * @note Cannot necessarily check if the `init` is an instance of the
     * `Headers` because that class may not be defined in Node or jsdom.
     */
    if (
      ['Headers', 'HeadersPolyfill'].includes(init?.constructor.name) ||
      init instanceof Headers ||
      (typeof globalThis.Headers === 'function' && init instanceof globalThis.Headers)
    ) {

Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this, @joshkel!

@kettanaito kettanaito marked this pull request as ready for review October 17, 2023 10:54
src/Headers.ts Outdated Show resolved Hide resolved
src/Headers.ts Outdated
@@ -25,7 +25,7 @@ export class Headers {
* because that class is only defined in the browser.
*/
if (
['Headers', 'HeadersPolyfill'].includes(init?.constructor.name) ||
['Headers', 'HeadersPolyfill', '_Headers'].includes(init?.constructor.name) ||
Copy link
Member

Choose a reason for hiding this comment

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

We should also add a test for this, implicitly. Bumping the used Node.js version in the CI would be enough. I recommend adding a matrix of Node.js versions so we test both older versions and the recent ones that introduce this change.

Let me know if you need help with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kettanaito Done, I think. I have no real experience working with GitHub Actions, so I don't know if my approach is will work and is the best way of doing it.

Copy link
Member

Choose a reason for hiding this comment

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

You did it right 👌

@kettanaito kettanaito changed the title fix: Update headers-polyfill for Node.js Undici changes fix: support global "_Headers" in Undici 5.26.3 Oct 18, 2023
Copy link
Member

@kettanaito kettanaito left a comment

Choose a reason for hiding this comment

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

This looks fantastic. Thank you, @joshkel!

@kettanaito kettanaito merged commit 49a4386 into mswjs:main Oct 18, 2023
0 of 2 checks passed
@kettanaito
Copy link
Member

Welcome to contributors, @joshkel 🎉

@kettanaito
Copy link
Member

Released: v4.0.2 🎉

This has been released in v4.0.2!

Make sure to always update to the latest version (npm i headers-polyfill@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@joshkel joshkel deleted the node-undici-headers-fix branch October 18, 2023 17:39
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.

Losing headers when using Undici in latest Node 18.x
2 participants