Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fizz/Flight] Don't start writing to the stream if it's already full #22726

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export function beginWriting(destination: Destination) {}
export function beginWriting(destination: Destination): boolean {
return true;
}

let prevWasCommentSegmenter = false;
export function writeChunk(
Expand Down
4 changes: 3 additions & 1 deletion packages/react-noop-renderer/src/ReactNoopFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const ReactNoopFlightServer = ReactFlightServer({
scheduleWork(callback: () => void) {
callback();
},
beginWriting(destination: Destination): void {},
beginWriting(destination: Destination): boolean {
return true;
},
writeChunk(destination: Destination, chunk: string): void {
destination.push(chunk);
},
Expand Down
4 changes: 3 additions & 1 deletion packages/react-noop-renderer/src/ReactNoopServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ const ReactNoopServer = ReactFizzServer({
scheduleWork(callback: () => void) {
callback();
},
beginWriting(destination: Destination): void {},
beginWriting(destination: Destination): boolean {
return true;
},
writeChunk(destination: Destination, buffer: Uint8Array): void {
const stack = destination.stack;
if (buffer === POP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export function beginWriting(destination: Destination) {}
export function beginWriting(destination: Destination): boolean {
return true;
}

export function writeChunk(destination: Destination, chunk: Chunk): boolean {
emitRow(destination, chunk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export function beginWriting(destination: Destination) {}
export function beginWriting(destination: Destination): boolean {
return true;
}

export function writeChunk(
destination: Destination,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export function scheduleWork(callback: () => void) {

export function flushBuffered(destination: Destination) {}

export function beginWriting(destination: Destination) {}
export function beginWriting(destination: Destination): boolean {
return true;
}

export function writeChunk(destination: Destination, chunk: Chunk): boolean {
emitRow(destination, chunk);
Expand Down
10 changes: 8 additions & 2 deletions packages/react-server/src/ReactFizzServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,10 @@ function flushCompletedQueues(
request: Request,
destination: Destination,
): void {
beginWriting(destination);
if (!beginWriting(destination)) {
request.destination = null;
return;
}
try {
// The structure of this is to go through each queue one by one and write
// until the sink tells us to stop. When we should stop, we still finish writing
Expand Down Expand Up @@ -1872,7 +1875,10 @@ function flushCompletedQueues(
// Allow anything written so far to flush to the underlying sink before
// we continue with lower priorities.
completeWriting(destination);
beginWriting(destination);
if (!beginWriting(destination)) {
request.destination = null;
return;
}

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

Expand Down
5 changes: 4 additions & 1 deletion packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,10 @@ function flushCompletedChunks(
request: Request,
destination: Destination,
): void {
beginWriting(destination);
if (!beginWriting(destination)) {
request.destination = null;
return;
}
try {
// We emit module chunks first in the stream so that
// they can be preloaded as early as possible.
Expand Down
4 changes: 3 additions & 1 deletion packages/react-server/src/ReactServerStreamConfigBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export function flushBuffered(destination: Destination) {
// transform streams. https://github.com/whatwg/streams/issues/960
}

export function beginWriting(destination: Destination) {}
export function beginWriting(destination: Destination): boolean {
return destination.desiredSize > 0;
}

export function writeChunk(
destination: Destination,
Expand Down
18 changes: 12 additions & 6 deletions packages/react-server/src/ReactServerStreamConfigNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

import type {Writable} from 'stream';

type MightBeFlushable = {
type WritableExtensions = {
flush?: () => void,
writableNeedDrain?: boolean,
...
};

export type Destination = Writable & MightBeFlushable;
export type Destination = Writable & WritableExtensions;

export type PrecomputedChunk = Uint8Array;
export type Chunk = string;
Expand All @@ -33,11 +34,16 @@ export function flushBuffered(destination: Destination) {
}
}

export function beginWriting(destination: Destination) {
// Older Node streams like http.createServer don't have this.
if (typeof destination.cork === 'function') {
destination.cork();
export function beginWriting(destination: Destination): boolean {
// Older versions of Node don't have this.
if (destination.writableNeedDrain !== true) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about writableLength >= writableHighWaterMark? It goes back a little further in terms of Node versions. That's what write() practically returns, and what is checked before needDrain is set to true. It's also how we implement that for web streams.

// Older Node streams like http.createServer don't have this.
if (typeof destination.cork === 'function') {
destination.cork();
}
return true;
}
return false;
}

export function writeChunk(
Expand Down