/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @flow */ import type {ReactModel} from 'react-server/src/ReactFlightServer'; import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig'; import type {Writable} from 'stream'; import { createRequest, startWork, startFlowing, } from 'react-server/src/ReactFlightServer'; function createDrainHandler(destination, request) { return () => startFlowing(request, destination); } type Options = { onError?: (error: mixed) => void, }; type Controls = {| pipe(destination: T): T, |}; function renderToPipeableStream( model: ReactModel, webpackMap: BundlerConfig, options?: Options, ): Controls { const request = createRequest( model, webpackMap, options ? options.onError : undefined, ); let hasStartedFlowing = false; startWork(request); return { pipe(destination: T): T { if (hasStartedFlowing) { throw new Error( 'React currently only supports piping to one writable stream.', ); } hasStartedFlowing = true; startFlowing(request, destination); destination.on('drain', createDrainHandler(destination, request)); return destination; }, }; } export {renderToPipeableStream};