2019-10-30 05:45:47 +08:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
2020-03-07 08:20:42 +08:00
|
|
|
import type {ReactModel} from 'react-server/src/ReactFlightServer';
|
2022-03-08 20:55:32 +08:00
|
|
|
import type {ServerContextJSONValue} from 'shared/ReactTypes';
|
2020-03-19 03:18:34 +08:00
|
|
|
import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig';
|
2019-10-30 05:45:47 +08:00
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
createRequest,
|
|
|
|
|
startWork,
|
|
|
|
|
startFlowing,
|
2022-06-19 23:05:41 +08:00
|
|
|
abort,
|
2020-03-07 08:20:42 +08:00
|
|
|
} from 'react-server/src/ReactFlightServer';
|
2019-10-30 05:45:47 +08:00
|
|
|
|
2022-09-10 04:03:48 +08:00
|
|
|
type Options = {
|
2022-06-01 05:53:32 +08:00
|
|
|
identifierPrefix?: string,
|
2022-06-19 23:05:41 +08:00
|
|
|
signal?: AbortSignal,
|
|
|
|
|
context?: Array<[string, ServerContextJSONValue]>,
|
|
|
|
|
onError?: (error: mixed) => void,
|
2022-09-10 04:03:48 +08:00
|
|
|
};
|
2021-03-30 10:36:16 +08:00
|
|
|
|
2020-03-19 03:18:34 +08:00
|
|
|
function renderToReadableStream(
|
|
|
|
|
model: ReactModel,
|
|
|
|
|
webpackMap: BundlerConfig,
|
2021-03-30 10:36:16 +08:00
|
|
|
options?: Options,
|
2020-03-19 03:18:34 +08:00
|
|
|
): ReadableStream {
|
2021-09-29 06:32:09 +08:00
|
|
|
const request = createRequest(
|
|
|
|
|
model,
|
|
|
|
|
webpackMap,
|
|
|
|
|
options ? options.onError : undefined,
|
2022-06-01 05:53:32 +08:00
|
|
|
options ? options.context : undefined,
|
|
|
|
|
options ? options.identifierPrefix : undefined,
|
2021-09-29 06:32:09 +08:00
|
|
|
);
|
2022-06-19 23:05:41 +08:00
|
|
|
if (options && options.signal) {
|
|
|
|
|
const signal = options.signal;
|
|
|
|
|
if (signal.aborted) {
|
|
|
|
|
abort(request, (signal: any).reason);
|
|
|
|
|
} else {
|
|
|
|
|
const listener = () => {
|
|
|
|
|
abort(request, (signal: any).reason);
|
|
|
|
|
signal.removeEventListener('abort', listener);
|
|
|
|
|
};
|
|
|
|
|
signal.addEventListener('abort', listener);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-01 04:20:36 +08:00
|
|
|
const stream = new ReadableStream(
|
|
|
|
|
{
|
|
|
|
|
type: 'bytes',
|
2022-09-08 23:46:07 +08:00
|
|
|
start(controller): ?Promise<void> {
|
2022-06-01 04:20:36 +08:00
|
|
|
startWork(request);
|
|
|
|
|
},
|
2022-09-08 23:46:07 +08:00
|
|
|
pull(controller): ?Promise<void> {
|
2022-06-01 04:20:36 +08:00
|
|
|
startFlowing(request, controller);
|
|
|
|
|
},
|
2022-09-08 23:46:07 +08:00
|
|
|
cancel(reason): ?Promise<void> {},
|
2019-10-30 05:45:47 +08:00
|
|
|
},
|
2022-06-01 04:20:36 +08:00
|
|
|
// $FlowFixMe size() methods are not allowed on byte streams.
|
|
|
|
|
{highWaterMark: 0},
|
|
|
|
|
);
|
2021-09-28 08:47:56 +08:00
|
|
|
return stream;
|
2019-10-30 05:45:47 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 09:18:55 +08:00
|
|
|
export {renderToReadableStream};
|