-
Notifications
You must be signed in to change notification settings - Fork 326
Support Node 18 by avoiding Headers.raw()
#1427
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
daa9530
Split cookies manually instead of using `Headers.raw()`
jplhomer 3b5a2fc
Add Node v18 to the testing matrix
jplhomer 628e5d8
Add changeset
jplhomer 10569ef
Fix types, logic, and bundling issues
jplhomer c6dd155
Add support for sending ReadableStream to node responses
jplhomer a27507a
Update set-cookie-parser to enhance tree-shaking in workers
frandiox b02fad0
Update packages/hydrogen/src/entry-server.tsx
jplhomer 88e0589
Use existing bufferReadableStream utility
jplhomer 6a01ab1
Don't run tests on Node 17 anymore
jplhomer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@shopify/hydrogen': patch | ||
| --- | ||
|
|
||
| Properly support Node v18 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ import {ServerAnalyticsRoute} from './foundation/Analytics/ServerAnalyticsRoute. | |
| import {getSyncSessionApi} from './foundation/session/session'; | ||
| import {parseJSON} from './utilities/parse'; | ||
| import {htmlEncode} from './utilities'; | ||
| import {splitCookiesString} from 'set-cookie-parser'; | ||
|
|
||
| declare global { | ||
| // This is provided by a Vite plugin | ||
|
|
@@ -707,22 +708,37 @@ function handleFetchResponseInNode( | |
| nodeResponse.statusCode = response.status; | ||
|
|
||
| if (response.body) { | ||
| nodeResponse.write(response.body); | ||
| if (response.body instanceof ReadableStream) { | ||
| const reader = response.body.getReader(); | ||
| reader.read().then(function write({done, value}) { | ||
| value && nodeResponse.write(value); | ||
| !done && reader.read().then(write); | ||
| done && nodeResponse.end(); | ||
| }); | ||
|
||
| } else { | ||
| nodeResponse.write(response.body); | ||
| nodeResponse.end(); | ||
| } | ||
| } else { | ||
| nodeResponse.end(); | ||
| } | ||
|
|
||
| nodeResponse.end(); | ||
| }); | ||
| } | ||
|
|
||
| return fetchResponsePromise; | ||
| } | ||
|
|
||
| // From fetch Headers to Node Response | ||
| /** | ||
| * Convert Headers to outgoing Node.js headers. | ||
| * Specifically, parse set-cookie headers to split them properly as separate | ||
| * `set-cookie` headers rather than a single, combined header. | ||
| */ | ||
| function setNodeHeaders(headers: Headers, nodeResponse: ServerResponse) { | ||
| // Headers.raw is only implemented in node-fetch, which is used by Hydrogen in dev and prod. | ||
| // It is the only way for now to access `set-cookie` header as an array. | ||
| // https://github.com/Shopify/hydrogen/issues/1228 | ||
| Object.entries((headers as any).raw()).forEach(([key, value]) => | ||
| nodeResponse.setHeader(key, value as string) | ||
| ); | ||
| for (const [key, value] of headers.entries()) { | ||
| if (key.toLowerCase() === 'set-cookie') { | ||
| nodeResponse.setHeader('set-cookie', splitCookiesString(value)); | ||
jplhomer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } else { | ||
| nodeResponse.setHeader(key, value); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2664,6 +2664,13 @@ | |
| resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.2.3.tgz#5798ecf1bec94eaa64db39ee52808ec0693315aa" | ||
| integrity sha512-KQf+QAMWKMrtBMsB8/24w53tEsxllMj6TuA80TT/5igJalLI/zm0L3oXRbIAl4Ohfc85gyHX/jhMwsVkmhLU4A== | ||
|
|
||
| "@types/set-cookie-parser@^2.4.2": | ||
| version "2.4.2" | ||
| resolved "https://registry.yarnpkg.com/@types/set-cookie-parser/-/set-cookie-parser-2.4.2.tgz#b6a955219b54151bfebd4521170723df5e13caad" | ||
| integrity sha512-fBZgytwhYAUkj/jC/FAV4RQ5EerRup1YQsXQCh8rZfiHkc4UahC192oH0smGwsXol3cL3A5oETuAHeQHmhXM4w== | ||
| dependencies: | ||
| "@types/node" "*" | ||
|
|
||
| "@types/[email protected]": | ||
| version "0.0.29" | ||
| resolved "https://registry.yarnpkg.com/@types/stack-trace/-/stack-trace-0.0.29.tgz#eb7a7c60098edb35630ed900742a5ecb20cfcb4d" | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we remove 17.x? I guess it's going to take the same time but just more chance for flaky tests or unrelated issues to appear 😅
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.
Yeah I'm down. If we support 18, we can just tell folks to upgrade to that if they're having trouble.