Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Commit 02c444c

Browse files
[19.2.x][FlightReply] Don't drop FormData entries in decodeReplyFromBusboy (#36566)
Co-authored-by: Hendrik Liebau <mail@hendrik-liebau.de>
1 parent 750848e commit 02c444c

8 files changed

Lines changed: 617 additions & 105 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"art": "0.10.1",
5454
"babel-plugin-syntax-hermes-parser": "^0.32.0",
5555
"babel-plugin-syntax-trailing-function-commas": "^6.5.0",
56+
"busboy": "^1.6.0",
5657
"chalk": "^3.0.0",
5758
"cli-table": "^0.3.1",
5859
"coffee-script": "^1.12.7",

‎packages/react-server-dom-esm/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
}from'react-client/src/ReactFlightClientStreamConfigNode';
6363

6464
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
65+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
6566

6667
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
6768

@@ -329,6 +330,17 @@ function prerenderToNodeStream(
329330
});
330331
}
331332

333+
typePendingFile={
334+
name: string,
335+
file: FileHandle,
336+
complete: boolean,
337+
// Lazily allocated when a text field arrives after this file's 'file'
338+
// event but before its (deferred) 'end' event. Stored as flat
339+
// [name1, value1, name2, value2, ...] pairs.
340+
queuedFields: null|Array<string>,
341+
next: null|PendingFile,
342+
};
343+
332344
functiondecodeReplyFromBusboy<T>(
333345
busboyStream: Busboy,
334346
moduleBasePath: ServerManifest,
@@ -344,14 +356,55 @@ function decodeReplyFromBusboy<T>(
344356
undefined,
345357
options ? options.arraySizeLimit : undefined,
346358
);
347-
letpendingFiles=0;
348-
constqueuedFields: Array<string>=[];
359+
360+
// Linked list of pending files in arrival (payload) order. Text fields that
361+
// arrive while a file is in flight are queued on the tail file's
362+
// `queuedFields` so they can be resolved together when that file completes.
363+
// Fields that arrive while the list is empty bypass it and resolve
364+
// immediately. This makes the backing FormData's insertion order match the
365+
// payload's entry order.
366+
lethead: null|PendingFile=null;
367+
lettail: null|PendingFile=null;
368+
letbodyFinished=false;
369+
letclosed=false;
370+
371+
functionflush(){
372+
while(head!==null){
373+
constcurrent=head;
374+
if(!current.complete){
375+
// This file is still streaming. Hold later files and fields until it
376+
// completes so the backing FormData reflects payload order.
377+
return;
378+
}
379+
try{
380+
resolveFileComplete(response,current.name,current.file);
381+
constqueuedFields=current.queuedFields;
382+
if(queuedFields!==null){
383+
for(leti=0;i<queuedFields.length;i+=2){
384+
resolveField(response,queuedFields[i],queuedFields[i+1]);
385+
}
386+
}
387+
}catch(error){
388+
busboyStream.destroy(error);
389+
return;
390+
}
391+
head=current.next;
392+
}
393+
tail=null;
394+
if(bodyFinished&&!closed){
395+
closed=true;
396+
close(response);
397+
}
398+
}
399+
349400
busboyStream.on('field',(name,value)=>{
350-
if(pendingFiles>0){
351-
// Because the 'end' event fires two microtasks after the next 'field'
352-
// we would resolve files and fields out of order. To handle this properly
353-
// we queue any fields we receive until the previous file is done.
354-
queuedFields.push(name,value);
401+
if(tail!==null){
402+
// A file is in flight; queue the field on the tail (most recent) pending
403+
// file so it resolves after that file, preserving payload order.
404+
if(tail.queuedFields===null){
405+
tail.queuedFields=[];
406+
}
407+
tail.queuedFields.push(name,value);
355408
}else{
356409
try{
357410
resolveField(response,name,value);
@@ -371,29 +424,46 @@ function decodeReplyFromBusboy<T>(
371424
);
372425
return;
373426
}
374-
pendingFiles++;
375427
constfile=resolveFileInfo(response,name,filename,mimeType);
428+
constpendingFile: PendingFile={
429+
name,
430+
file,
431+
complete: false,
432+
queuedFields: null,
433+
next: null,
434+
};
435+
if(tail===null){
436+
head=pendingFile;
437+
}else{
438+
tail.next=pendingFile;
439+
}
440+
tail=pendingFile;
376441
value.on('data',chunk=>{
377-
resolveFileChunk(response,file,chunk);
378-
});
379-
value.on('end',()=>{
380442
try{
381-
resolveFileComplete(response,name,file);
382-
pendingFiles--;
383-
if(pendingFiles===0){
384-
// Release any queued fields
385-
for(leti=0;i<queuedFields.length;i+=2){
386-
resolveField(response,queuedFields[i],queuedFields[i+1]);
387-
}
388-
queuedFields.length=0;
389-
}
443+
resolveFileChunk(response,file,chunk);
390444
}catch(error){
391445
busboyStream.destroy(error);
392446
}
393447
});
448+
value.on('error',error=>{
449+
busboyStream.destroy(error);
450+
});
451+
value.on('end',()=>{
452+
pendingFile.complete=true;
453+
flush();
454+
});
394455
});
395456
busboyStream.on('finish',()=>{
396-
close(response);
457+
bodyFinished=true;
458+
flush();
459+
if(!closed){
460+
// Invariant: busboy delays 'finish' until every file's 'end' event has
461+
// fired, so the flush above should always close the response.
462+
reportGlobalError(
463+
response,
464+
newError('Reply finished with incomplete file part.'),
465+
);
466+
}
397467
});
398468
busboyStream.on('error',err=>{
399469
reportGlobalError(

‎packages/react-server-dom-parcel/src/server/ReactFlightDOMServerNode.js‎

Lines changed: 91 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import {
7575
import{textEncoder}from'react-server/src/ReactServerStreamConfigNode';
7676

7777
importtype{TemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
78+
importtype{FileHandle}from'react-server/src/ReactFlightReplyServer';
7879

7980
export{createTemporaryReferenceSet}from'react-server/src/ReactFlightServerTemporaryReferences';
8081

@@ -560,6 +561,17 @@ export function registerServerActions(manifest: ServerManifest) {
560561
serverManifest=manifest;
561562
}
562563

564+
typePendingFile={
565+
name: string,
566+
file: FileHandle,
567+
complete: boolean,
568+
// Lazily allocated when a text field arrives after this file's 'file'
569+
// event but before its (deferred) 'end' event. Stored as flat
570+
// [name1, value1, name2, value2, ...] pairs.
571+
queuedFields: null|Array<string>,
572+
next: null|PendingFile,
573+
};
574+
563575
exportfunctiondecodeReplyFromBusboy<T>(
564576
busboyStream: Busboy,
565577
options?: {
@@ -574,14 +586,55 @@ export function decodeReplyFromBusboy<T>(
574586
undefined,
575587
options ? options.arraySizeLimit : undefined,
576588
);
577-
letpendingFiles=0;
578-
constqueuedFields: Array<string>=[];
589+
590+
// Linked list of pending files in arrival (payload) order. Text fields that
591+
// arrive while a file is in flight are queued on the tail file's
592+
// `queuedFields` so they can be resolved together when that file completes.
593+
// Fields that arrive while the list is empty bypass it and resolve
594+
// immediately. This makes the backing FormData's insertion order match the
595+
// payload's entry order.
596+
lethead: null|PendingFile=null;
597+
lettail: null|PendingFile=null;
598+
letbodyFinished=false;
599+
letclosed=false;
600+
601+
functionflush(){
602+
while(head!==null){
603+
constcurrent=head;
604+
if(!current.complete){
605+
// This file is still streaming. Hold later files and fields until it
606+
// completes so the backing FormData reflects payload order.
607+
return;
608+
}
609+
try{
610+
resolveFileComplete(response,current.name,current.file);
611+
constqueuedFields=current.queuedFields;
612+
if(queuedFields!==null){
613+
for(leti=0;i<queuedFields.length;i+=2){
614+
resolveField(response,queuedFields[i],queuedFields[i+1]);
615+
}
616+
}
617+
}catch(error){
618+
busboyStream.destroy(error);
619+
return;
620+
}
621+
head=current.next;
622+
}
623+
tail=null;
624+
if(bodyFinished&&!closed){
625+
closed=true;
626+
close(response);
627+
}
628+
}
629+
579630
busboyStream.on('field',(name,value)=>{
580-
if(pendingFiles>0){
581-
// Because the 'end' event fires two microtasks after the next 'field'
582-
// we would resolve files and fields out of order. To handle this properly
583-
// we queue any fields we receive until the previous file is done.
584-
queuedFields.push(name,value);
631+
if(tail!==null){
632+
// A file is in flight; queue the field on the tail (most recent) pending
633+
// file so it resolves after that file, preserving payload order.
634+
if(tail.queuedFields===null){
635+
tail.queuedFields=[];
636+
}
637+
tail.queuedFields.push(name,value);
585638
}else{
586639
try{
587640
resolveField(response,name,value);
@@ -601,29 +654,46 @@ export function decodeReplyFromBusboy<T>(
601654
);
602655
return;
603656
}
604-
pendingFiles++;
605657
constfile=resolveFileInfo(response,name,filename,mimeType);
658+
constpendingFile: PendingFile={
659+
name,
660+
file,
661+
complete: false,
662+
queuedFields: null,
663+
next: null,
664+
};
665+
if(tail===null){
666+
head=pendingFile;
667+
}else{
668+
tail.next=pendingFile;
669+
}
670+
tail=pendingFile;
606671
value.on('data',chunk=>{
607-
resolveFileChunk(response,file,chunk);
608-
});
609-
value.on('end',()=>{
610672
try{
611-
resolveFileComplete(response,name,file);
612-
pendingFiles--;
613-
if(pendingFiles===0){
614-
// Release any queued fields
615-
for(leti=0;i<queuedFields.length;i+=2){
616-
resolveField(response,queuedFields[i],queuedFields[i+1]);
617-
}
618-
queuedFields.length=0;
619-
}
673+
resolveFileChunk(response,file,chunk);
620674
}catch(error){
621675
busboyStream.destroy(error);
622676
}
623677
});
678+
value.on('error',error=>{
679+
busboyStream.destroy(error);
680+
});
681+
value.on('end',()=>{
682+
pendingFile.complete=true;
683+
flush();
684+
});
624685
});
625686
busboyStream.on('finish',()=>{
626-
close(response);
687+
bodyFinished=true;
688+
flush();
689+
if(!closed){
690+
// Invariant: busboy delays 'finish' until every file's 'end' event has
691+
// fired, so the flush above should always close the response.
692+
reportGlobalError(
693+
response,
694+
newError('Reply finished with incomplete file part.'),
695+
);
696+
}
627697
});
628698
busboyStream.on('error',err=>{
629699
reportGlobalError(

0 commit comments

Comments
 (0)