-
-
Notifications
You must be signed in to change notification settings - Fork 1k
revamp Headers module #834
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
Conversation
NotMoni
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lgtm
xxczaki
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
LinusU
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat! 👍
|
Before merging this, I think we should discuss the last points from the PR description. Here are my thoughts:
cc @node-fetch/core @tinovyatkin |
|
My personal thoughts:
|
|
@xxczaki I believe with 5 approvals and no opposing comments this PR can be merged and nuances 1 and 2 attended/discussed in separate PRs. |

What is the purpose of this pull request?
This is an almost complete rewrite of the
Headersmodule (so, diff is looking crazy - it's better just to read new code completely).The current implementation of
Headersis somewhere painful to read (particularly iterator implementation). It's nowhere close to being lightweight too with inventing some wheels and avoiding modern language features.What changes did you make? (provide an overview)
The only change to test file is the replacement of originally broken test for
createHeadersLenient- it was testing for falseness properties onHeadersobject that was always false.Change for other files is renaming of internal methods
createHeadersLenient(tofromRawHeaders) andexportNodeCompatibleHeaders(to build-in symbol).The approach used in this refactoring is to take advantage of the fact, that there is almost completely identical by shape built-in object in Node >= 10.0 -
URLSearchParams(I do believe it was copied fromHeaderswhen introduced browser-side, so, now we can reuse it back). We extend hereURLSearchParamsattending minor differences in functionality (case insensitivity, parameters validation, and keys iteration) via lightweight Proxy, and major functionality differences (getand values iteration) as overloaded methods.I'd moved
exportNodeCompatibleHeadersexport into Node.JS built-in symbol, so, it also now serves for everyone secret debug powerhouses -console.log(response.headers)I've replaced
createHeadersLenientand creatingHeadersfromrawHeaders, that fixes #783.With the use of Proxy, constructor return value and generator functions in the source is also a fun weekend reading for everybody who loves modern JavaScript 😄
Is there anything you'd like reviewers to know?
This PR purposely doesn't touch any tests to showcase that module is in equal functionality, however, there are some points to discuss:
Headers now, by side effect, exposing
getAllmethod (as it exists onURLSearchParams) that was removed from client-side spec due to security concern and then removed fromnode-fetchtoo, thenrawwas introduced when we realized than on the server-side, indeed, we need to be able to get all values.It's easy to hide this method completely (or make non-enumerable), or leave it undocumented (all other modules as Request and Response are exposing some non-spec methods and we have
rawhere anyway) or to document it. I do believe it's an extremely useful server-side and better fit for Cookies thanraw. So @node-fetch/core probably should decide here.See: Use case for Headers getAll whatwg/fetch#973 (comment)
There is no current test for
new Headers(null)and it passes in both new and original versions of Headers. However, it throws at Chrome. Should behavior be adjusted and test added?3. There is a subtle difference in how headers are returned concatenated -node-fetchis using,to join them while browsers use just coma. Should it be adjusted?