@samitouri / QOS-React-2 / commits / f50f39b55f

[Flight] Better compat with http.createServer (#17289)

Dan Abramov committed Nov 6, 2019 at 17:10 UTC f50f39b55fb09fc78c8fc2bd63d286c147fd30f2
1 file changed +18 -5
packages/react-server/src/ReactServerHostConfigNode.js
+18 -5
@@ -9,7 +9,10 @@
9
10 import type {Writable} from 'stream';
11
12 -type MightBeFlushable = {flush?: () => void};
12 +type MightBeFlushable = {
13 + flush?: () => void,
14 + flushHeaders?: () => void, // Legacy
15 +};
16
17 export type Destination = Writable & MightBeFlushable;
18
@@ -21,13 +24,20 @@ export function flushBuffered(destination: Destination) {
24 // If we don't have any more data to send right now.
25 // Flush whatever is in the buffer to the wire.
26 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 + }
33 }
34 }
35
36 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 + }
41 }
42
43 export function writeChunk(destination: Destination, buffer: Uint8Array) {
@@ -36,7 +46,10 @@ export function writeChunk(destination: Destination, buffer: Uint8Array) {
46 }
47
48 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 + }
53 }
54
55 export function close(destination: Destination) {