-
Notifications
You must be signed in to change notification settings - Fork 51k
[FlightReply] Don't drop FormData entries in decodeReplyFromBusboy
#36468
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
unstubbable
wants to merge
5
commits into
facebook:main
Choose a base branch
from
unstubbable:fix-form-data-regression
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
5 commits
Select commit
Hold shift + click to select a range
59c282b
[FlightReply] Don't drop FormData entries in `decodeReplyFromBusboy`
unstubbable dcd8fb8
Use file-only buffer
unstubbable 5978b45
Handle error events
unstubbable 9630bae
Use a linked list
unstubbable 95599c9
Throw when not fully closed after flush on finish
unstubbable 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
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 |
|---|---|---|
|
|
@@ -62,6 +62,7 @@ import { | |
| } from 'react-client/src/ReactFlightClientStreamConfigNode'; | ||
|
|
||
| import type {TemporaryReferenceSet} from 'react-server/src/ReactFlightServerTemporaryReferences'; | ||
| import type {FileHandle} from 'react-server/src/ReactFlightReplyServer'; | ||
|
|
||
| export {createTemporaryReferenceSet} from 'react-server/src/ReactFlightServerTemporaryReferences'; | ||
|
|
||
|
|
@@ -329,6 +330,17 @@ function prerenderToNodeStream( | |
| }); | ||
| } | ||
|
|
||
| type PendingFile = { | ||
| name: string, | ||
| file: FileHandle, | ||
| complete: boolean, | ||
| // Lazily allocated when a text field arrives after this file's 'file' | ||
| // event but before its (deferred) 'end' event. Stored as flat | ||
| // [name1, value1, name2, value2, ...] pairs. | ||
| queuedFields: null | Array<string>, | ||
| next: null | PendingFile, | ||
| }; | ||
|
|
||
| function decodeReplyFromBusboy<T>( | ||
| busboyStream: Busboy, | ||
| moduleBasePath: ServerManifest, | ||
|
|
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>( | |
| undefined, | ||
| options ? options.arraySizeLimit : undefined, | ||
| ); | ||
| let pendingFiles = 0; | ||
| const queuedFields: Array<string> = []; | ||
|
|
||
| // Linked list of pending files in arrival (payload) order. Text fields that | ||
| // arrive while a file is in flight are queued on the tail file's | ||
| // `queuedFields` so they can be resolved together when that file completes. | ||
| // Fields that arrive while the list is empty bypass it and resolve | ||
| // immediately. This makes the backing FormData's insertion order match the | ||
| // payload's entry order. | ||
| let head: null | PendingFile = null; | ||
| let tail: null | PendingFile = null; | ||
| let bodyFinished = false; | ||
| let closed = false; | ||
|
|
||
| function flush() { | ||
| while (head !== null) { | ||
| const current = head; | ||
| if (!current.complete) { | ||
| // This file is still streaming. Hold later files and fields until it | ||
| // completes so the backing FormData reflects payload order. | ||
| return; | ||
| } | ||
| try { | ||
| resolveFileComplete(response, current.name, current.file); | ||
| const queuedFields = current.queuedFields; | ||
| if (queuedFields !== null) { | ||
| for (let i = 0; i < queuedFields.length; i += 2) { | ||
| resolveField(response, queuedFields[i], queuedFields[i + 1]); | ||
| } | ||
| } | ||
| } catch (error) { | ||
| busboyStream.destroy(error); | ||
| return; | ||
| } | ||
| head = current.next; | ||
| } | ||
| tail = null; | ||
| if (bodyFinished && !closed) { | ||
| closed = true; | ||
| close(response); | ||
| } | ||
| } | ||
|
|
||
| busboyStream.on('field', (name, value) => { | ||
| if (pendingFiles > 0) { | ||
| // Because the 'end' event fires two microtasks after the next 'field' | ||
| // we would resolve files and fields out of order. To handle this properly | ||
| // we queue any fields we receive until the previous file is done. | ||
| queuedFields.push(name, value); | ||
| if (tail !== null) { | ||
| // A file is in flight; queue the field on the tail (most recent) pending | ||
| // file so it resolves after that file, preserving payload order. | ||
| if (tail.queuedFields === null) { | ||
| tail.queuedFields = []; | ||
| } | ||
| tail.queuedFields.push(name, value); | ||
| } else { | ||
| try { | ||
| resolveField(response, name, value); | ||
|
|
@@ -371,29 +424,45 @@ function decodeReplyFromBusboy<T>( | |
| ); | ||
| return; | ||
| } | ||
| pendingFiles++; | ||
| const file = resolveFileInfo(response, name, filename, mimeType); | ||
| const pendingFile: PendingFile = { | ||
| name, | ||
| file, | ||
| complete: false, | ||
| queuedFields: null, | ||
| next: null, | ||
| }; | ||
| if (tail === null) { | ||
| head = pendingFile; | ||
| } else { | ||
| tail.next = pendingFile; | ||
| } | ||
| tail = pendingFile; | ||
| value.on('data', chunk => { | ||
| resolveFileChunk(response, file, chunk); | ||
| }); | ||
| value.on('end', () => { | ||
| try { | ||
| resolveFileComplete(response, name, file); | ||
| pendingFiles--; | ||
| if (pendingFiles === 0) { | ||
| // Release any queued fields | ||
| for (let i = 0; i < queuedFields.length; i += 2) { | ||
| resolveField(response, queuedFields[i], queuedFields[i + 1]); | ||
| } | ||
| queuedFields.length = 0; | ||
| } | ||
| resolveFileChunk(response, file, chunk); | ||
| } catch (error) { | ||
| busboyStream.destroy(error); | ||
| } | ||
| }); | ||
| value.on('error', error => { | ||
| busboyStream.destroy(error); | ||
| }); | ||
| value.on('end', () => { | ||
| pendingFile.complete = true; | ||
| flush(); | ||
| }); | ||
| }); | ||
| busboyStream.on('finish', () => { | ||
| close(response); | ||
| bodyFinished = true; | ||
| flush(); | ||
|
unstubbable marked this conversation as resolved.
|
||
| if (!closed) { | ||
| // Invariant: busboy delays 'finish' until every file's 'end' event has | ||
| // fired, so the flush above should always close the response. | ||
| busboyStream.destroy( | ||
| new Error('Reply finished with incomplete file part.'), | ||
| ); | ||
|
Comment on lines
+462
to
+464
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this actually bubble up through the error event? shouldn't we just directly reportGlobalError? |
||
| } | ||
| }); | ||
| busboyStream.on('error', err => { | ||
| reportGlobalError( | ||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.