From c36a1b3b6c6505638d88aa0b97cec4d959a28e3b Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Wed, 6 Nov 2019 16:22:15 +0000 Subject: [PATCH] [Flight] Better compat with http.createServer --- .../src/ReactServerHostConfigNode.js | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/react-server/src/ReactServerHostConfigNode.js b/packages/react-server/src/ReactServerHostConfigNode.js index 2ee9ddec354d9..91eb1346a67a4 100644 --- a/packages/react-server/src/ReactServerHostConfigNode.js +++ b/packages/react-server/src/ReactServerHostConfigNode.js @@ -9,7 +9,10 @@ import type {Writable} from 'stream'; -type MightBeFlushable = {flush?: () => void}; +type MightBeFlushable = { + flush?: () => void, + flushHeaders?: () => void, // Legacy +}; export type Destination = Writable & MightBeFlushable; @@ -21,13 +24,20 @@ export function flushBuffered(destination: Destination) { // If we don't have any more data to send right now. // Flush whatever is in the buffer to the wire. if (typeof destination.flush === 'function') { - // By convention the Zlib streams provide a flush function for this purpose. - destination.flush(); + // http.createServer response have flush(), but it has a different meaning and + // is deprecated in favor of flushHeaders(). Detect to avoid a warning. + if (typeof destination.flushHeaders !== 'function') { + // By convention the Zlib streams provide a flush function for this purpose. + destination.flush(); + } } } export function beginWriting(destination: Destination) { - destination.cork(); + // Older Node streams like http.createServer don't have this. + if (typeof destination.cork === 'function') { + destination.cork(); + } } export function writeChunk(destination: Destination, buffer: Uint8Array) { @@ -36,7 +46,10 @@ export function writeChunk(destination: Destination, buffer: Uint8Array) { } export function completeWriting(destination: Destination) { - destination.uncork(); + // Older Node streams like http.createServer don't have this. + if (typeof destination.uncork === 'function') { + destination.uncork(); + } } export function close(destination: Destination) {