-
Notifications
You must be signed in to change notification settings - Fork 130
fix: remove type: "bytes" from ReadableStream for flight data #1293
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
Open
e-chan1007
wants to merge
2
commits into
opennextjs:main
Choose a base branch
from
e-chan1007:fix/flight-readable-stream-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,10 @@ | ||
| --- | ||
| "@opennextjs/cloudflare": patch | ||
| --- | ||
|
|
||
| fix: patch the createInlinedDataReadableStream function not to use the type: "bytes" option in ReadableStream constructor | ||
|
|
||
| workerd seems not to correctly support the type: "bytes" option in ReadableStream constructor, that breaks inlined flight data streaming over 4kB. | ||
| By removing the type: "bytes" option, the inlined flight data will not be split into multiple chunks, and the streaming will work correctly. | ||
|
|
||
| (fixes #1225) |
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
37 changes: 37 additions & 0 deletions
37
packages/cloudflare/src/cli/build/patches/plugins/flight-readable-stream-type.spec.ts
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,37 @@ | ||
| import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import { rule } from "./flight-readable-stream-type.js"; | ||
|
|
||
| describe("patchFlightReadableStreamType", () => { | ||
| test("removes type property of ReadableStream constructor only in createInlinedDataReadableStream", () => { | ||
| const code = ` | ||
| new ReadableStream({type: "bytes", start(e){ e.enqueue(n); }}); | ||
|
|
||
| function tl(e,t,r){ | ||
| let n = t ? '<script nonce="">' : "<script>"; | ||
| return new ReadableStream({type: "bytes", start(e){ e.enqueue(n); }}); | ||
| } | ||
|
|
||
| function tl2(e,t,r){ | ||
| return new ReadableStream({type: "bytes", start(e){ e.enqueue(n); }}); | ||
| } | ||
| `; | ||
|
|
||
| expect(patchCode(code, rule)).toMatchInlineSnapshot( | ||
| ` | ||
| "new ReadableStream({type: "bytes", start(e){ e.enqueue(n); }}); | ||
|
|
||
| function tl(e,t,r){ | ||
| let n = t ? '<script nonce="">' : "<script>"; | ||
| return new ReadableStream({start(e){ e.enqueue(n); }}); | ||
| } | ||
|
|
||
| function tl2(e,t,r){ | ||
| return new ReadableStream({type: "bytes", start(e){ e.enqueue(n); }}); | ||
| } | ||
| " | ||
| ` | ||
| ); | ||
| }); | ||
| }); |
41 changes: 41 additions & 0 deletions
41
packages/cloudflare/src/cli/build/patches/plugins/flight-readable-stream-type.ts
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,41 @@ | ||
| /** | ||
| * This patch will remove the `type: "bytes"` option from the `ReadableStream` constructor in the | ||
| * createInlinedDataReadableStream function, which provides inline script tag chunks for writing hydration data to the client. | ||
| * Since workerd has no enough support for the `bytes` type, this patch will remove it to avoid errors related to RSC Flight streams. | ||
| */ | ||
|
|
||
| import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; | ||
| import type { CodePatcher } from "@opennextjs/aws/build/patch/codePatcher.js"; | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|
|
||
| export const patchFlightReadableStreamType: CodePatcher = { | ||
| name: "patch-flight-readable-stream-type", | ||
| patches: [ | ||
| { | ||
| pathFilter: getCrossPlatformPathRegex(String.raw`next-server/.*\.runtime\.prod\.js$`, { | ||
| escape: false, | ||
| }), | ||
| contentFilter: /new ReadableStream\(\{type:"bytes",/, | ||
| patchCode: async ({ code }) => patchCode(code, rule), | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| export const rule = ` | ||
| rule: | ||
| kind: object | ||
| pattern: "{ type: \\"bytes\\", $$$REST }" | ||
| inside: | ||
| stopBy: end | ||
| kind: new_expression | ||
| has: | ||
| field: constructor | ||
| pattern: ReadableStream | ||
| inside: | ||
| kind: function_declaration | ||
| stopBy: end | ||
| has: | ||
| regex: "<script>" | ||
|
|
||
| fix: "{$$$REST}" | ||
| `; | ||
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.
Uh oh!
There was an error while loading. Please reload this page.