Skip to content

fix: support objects with null prototype as HeadersInit#81

Merged
kettanaito merged 4 commits intomswjs:mainfrom
tmisirpash:tmisirpash/undefined_constructor
Mar 5, 2026
Merged

fix: support objects with null prototype as HeadersInit#81
kettanaito merged 4 commits intomswjs:mainfrom
tmisirpash:tmisirpash/undefined_constructor

Conversation

@tmisirpash
Copy link
Copy Markdown
Contributor

@tmisirpash tmisirpash commented Oct 10, 2024

Currently, if an object is created with the null prototype, e.g.

const headers = Object.create(null);

attempting to instantiate a Headers object with this polyfill throws an error:

TypeError: Cannot read properties of undefined (reading 'name')

because headers.constructor is undefined.

@kettanaito

@tmisirpash
Copy link
Copy Markdown
Contributor Author

To provide more context, I would like to use this polyfill in an environment that lacks a lot of the web standards and get the axios library working. To that end, I've also set up a shim fetch function that uses this polyfill.

When axios accepts a set of headers, it internally converts that to an object and constructs it with the null prototype here, before passing that object into the shim fetch function. The fetch function then wraps the object in a Headers instance. This is where I get the above error.

I can work around this for now by changing the shim fetch function implementation, but @kettanaito I'd like to hear your thoughts on whether you'd accept this contribution here, because wrapping an object with the null prototype in a Headers instance appears to work fine in the browser and Node.js.

@kettanaito kettanaito changed the title Allow passing headers object with null prototype fix: support object with null prototype as HeadersInit Mar 5, 2026
Copy link
Copy Markdown
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.

Looks good to me. Thanks for adding this, @tmisirpash.

@kettanaito kettanaito changed the title fix: support object with null prototype as HeadersInit fix: support objects with null prototype as HeadersInit Mar 5, 2026
@kettanaito kettanaito merged commit c95b0bc into mswjs:main Mar 5, 2026
0 of 3 checks passed
@kettanaito
Copy link
Copy Markdown
Member

Released: v5.0.1 🎉

This has been released in v5.0.1.

Get these changes by running the following command:

npm i headers-polyfill@latest

Predictable release automation by Release.

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.

2 participants