Skip to content

Commit 8b6d828

Browse files
committed
Don't try writing if the stream is full
1 parent 2b77ab2 commit 8b6d828

File tree

10 files changed

+45
-16
lines changed

10 files changed

+45
-16
lines changed

packages/react-dom/src/server/ReactDOMLegacyServerStreamConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export function scheduleWork(callback: () => void) {
2222

2323
export function flushBuffered(destination: Destination) {}
2424

25-
export function beginWriting(destination: Destination) {}
25+
export function beginWriting(destination: Destination): boolean {
26+
return true;
27+
}
2628

2729
let prevWasCommentSegmenter = false;
2830
export function writeChunk(

packages/react-noop-renderer/src/ReactNoopFlightServer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ const ReactNoopFlightServer = ReactFlightServer({
2626
scheduleWork(callback: () => void) {
2727
callback();
2828
},
29-
beginWriting(destination: Destination): void {},
29+
beginWriting(destination: Destination): boolean {
30+
return true;
31+
},
3032
writeChunk(destination: Destination, chunk: string): void {
3133
destination.push(chunk);
3234
},

packages/react-noop-renderer/src/ReactNoopServer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ const ReactNoopServer = ReactFizzServer({
5757
scheduleWork(callback: () => void) {
5858
callback();
5959
},
60-
beginWriting(destination: Destination): void {},
60+
beginWriting(destination: Destination): boolean {
61+
return true;
62+
},
6163
writeChunk(destination: Destination, buffer: Uint8Array): void {
6264
const stack = destination.stack;
6365
if (buffer === POP) {

packages/react-server-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ export function scheduleWork(callback: () => void) {
139139

140140
export function flushBuffered(destination: Destination) {}
141141

142-
export function beginWriting(destination: Destination) {}
142+
export function beginWriting(destination: Destination): boolean {
143+
return true;
144+
}
143145

144146
export function writeChunk(destination: Destination, chunk: Chunk): boolean {
145147
emitRow(destination, chunk);

packages/react-server-dom-relay/src/ReactServerStreamConfigFB.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export function scheduleWork(callback: () => void) {
2323

2424
export function flushBuffered(destination: Destination) {}
2525

26-
export function beginWriting(destination: Destination) {}
26+
export function beginWriting(destination: Destination): boolean {
27+
return true;
28+
}
2729

2830
export function writeChunk(
2931
destination: Destination,

packages/react-server-native-relay/src/ReactFlightNativeRelayServerHostConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ export function scheduleWork(callback: () => void) {
136136

137137
export function flushBuffered(destination: Destination) {}
138138

139-
export function beginWriting(destination: Destination) {}
139+
export function beginWriting(destination: Destination): boolean {
140+
return true;
141+
}
140142

141143
export function writeChunk(destination: Destination, chunk: Chunk): boolean {
142144
emitRow(destination, chunk);

packages/react-server/src/ReactFizzServer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,7 +1820,10 @@ function flushCompletedQueues(
18201820
request: Request,
18211821
destination: Destination,
18221822
): void {
1823-
beginWriting(destination);
1823+
if (!beginWriting(destination)) {
1824+
request.destination = null;
1825+
return;
1826+
}
18241827
try {
18251828
// The structure of this is to go through each queue one by one and write
18261829
// until the sink tells us to stop. When we should stop, we still finish writing
@@ -1872,7 +1875,10 @@ function flushCompletedQueues(
18721875
// Allow anything written so far to flush to the underlying sink before
18731876
// we continue with lower priorities.
18741877
completeWriting(destination);
1875-
beginWriting(destination);
1878+
if (!beginWriting(destination)) {
1879+
request.destination = null;
1880+
return;
1881+
}
18761882

18771883
// TODO: Here we'll emit data used by hydration.
18781884

packages/react-server/src/ReactFlightServer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,10 @@ function flushCompletedChunks(
723723
request: Request,
724724
destination: Destination,
725725
): void {
726-
beginWriting(destination);
726+
if (!beginWriting(destination)) {
727+
request.destination = null;
728+
return;
729+
}
727730
try {
728731
// We emit module chunks first in the stream so that
729732
// they can be preloaded as early as possible.

packages/react-server/src/ReactServerStreamConfigBrowser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export function flushBuffered(destination: Destination) {
2121
// transform streams. https://github.com/whatwg/streams/issues/960
2222
}
2323

24-
export function beginWriting(destination: Destination) {}
24+
export function beginWriting(destination: Destination): boolean {
25+
return destination.desiredSize > 0;
26+
}
2527

2628
export function writeChunk(
2729
destination: Destination,

packages/react-server/src/ReactServerStreamConfigNode.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
import type {Writable} from 'stream';
1111

12-
type MightBeFlushable = {
12+
type WritableExtensions = {
1313
flush?: () => void,
14+
writableNeedDrain?: boolean,
1415
...
1516
};
1617

17-
export type Destination = Writable & MightBeFlushable;
18+
export type Destination = Writable & WritableExtensions;
1819

1920
export type PrecomputedChunk = Uint8Array;
2021
export type Chunk = string;
@@ -33,11 +34,16 @@ export function flushBuffered(destination: Destination) {
3334
}
3435
}
3536

36-
export function beginWriting(destination: Destination) {
37-
// Older Node streams like http.createServer don't have this.
38-
if (typeof destination.cork === 'function') {
39-
destination.cork();
37+
export function beginWriting(destination: Destination): boolean {
38+
// Older versions of Node don't have this.
39+
if (destination.writableNeedDrain !== true) {
40+
// Older Node streams like http.createServer don't have this.
41+
if (typeof destination.cork === 'function') {
42+
destination.cork();
43+
}
44+
return true;
4045
}
46+
return false;
4147
}
4248

4349
export function writeChunk(

0 commit comments

Comments
 (0)