Skip to content

Commit f50f39b

Browse files
authored
[Flight] Better compat with http.createServer (#17289)
1 parent 3452706 commit f50f39b

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

packages/react-server/src/ReactServerHostConfigNode.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

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

12-
type MightBeFlushable = {flush?: () => void};
12+
type MightBeFlushable = {
13+
flush?: () => void,
14+
flushHeaders?: () => void, // Legacy
15+
};
1316

1417
export type Destination = Writable & MightBeFlushable;
1518

@@ -21,13 +24,20 @@ export function flushBuffered(destination: Destination) {
2124
// If we don't have any more data to send right now.
2225
// Flush whatever is in the buffer to the wire.
2326
if (typeof destination.flush === 'function') {
24-
// By convention the Zlib streams provide a flush function for this purpose.
25-
destination.flush();
27+
// http.createServer response have flush(), but it has a different meaning and
28+
// is deprecated in favor of flushHeaders(). Detect to avoid a warning.
29+
if (typeof destination.flushHeaders !== 'function') {
30+
// By convention the Zlib streams provide a flush function for this purpose.
31+
destination.flush();
32+
}
2633
}
2734
}
2835

2936
export function beginWriting(destination: Destination) {
30-
destination.cork();
37+
// Older Node streams like http.createServer don't have this.
38+
if (typeof destination.cork === 'function') {
39+
destination.cork();
40+
}
3141
}
3242

3343
export function writeChunk(destination: Destination, buffer: Uint8Array) {
@@ -36,7 +46,10 @@ export function writeChunk(destination: Destination, buffer: Uint8Array) {
3646
}
3747

3848
export function completeWriting(destination: Destination) {
39-
destination.uncork();
49+
// Older Node streams like http.createServer don't have this.
50+
if (typeof destination.uncork === 'function') {
51+
destination.uncork();
52+
}
4053
}
4154

4255
export function close(destination: Destination) {

0 commit comments

Comments
 (0)