Files
react/packages/react-server-dom-webpack/src/ReactFlightDOMServerNode.js
T
Sebastian Markbåge 579c008a75 [Fizz/Flight] pipeToNodeWritable(..., writable).startWriting() -> renderToPipeableStream(...).pipe(writable) (#22450)
* Rename pipeToNodeWritable to renderToNodePipe

* Add startWriting API to Flight

We don't really need it in this case because there's way less reason to
delay the stream in Flight.

* Pass the destination to startWriting instead of renderToNode

* Rename startWriting to pipe

This mirrors the ReadableStream API in Node

* Error codes

* Rename to renderToPipeableStream

This mimics the renderToReadableStream API for the browser.
2021-10-06 00:31:06 -04:00

60 lines
1.4 KiB
JavaScript

/**
* 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<T: Writable>(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<T: Writable>(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};