1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  3.  *
    
  4.  * This source code is licensed under the MIT license found in the
    
  5.  * LICENSE file in the root directory of this source tree.
    
  6.  *
    
  7.  * @flow
    
  8.  */
    
  9. 
    
  10. import type {ReactNodeList, ReactFormState} from 'shared/ReactTypes';
    
  11. import type {BootstrapScriptDescriptor} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
    
  12. import type {ImportMap} from '../shared/ReactDOMTypes';
    
  13. 
    
  14. import ReactVersion from 'shared/ReactVersion';
    
  15. 
    
  16. import {
    
  17.   createRequest,
    
  18.   startWork,
    
  19.   startFlowing,
    
  20.   stopFlowing,
    
  21.   abort,
    
  22. } from 'react-server/src/ReactFizzServer';
    
  23. 
    
  24. import {
    
  25.   createResumableState,
    
  26.   createRenderState,
    
  27.   createRootFormatContext,
    
  28. } from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
    
  29. 
    
  30. type Options = {
    
  31.   identifierPrefix?: string,
    
  32.   namespaceURI?: string,
    
  33.   nonce?: string,
    
  34.   bootstrapScriptContent?: string,
    
  35.   bootstrapScripts?: Array<string | BootstrapScriptDescriptor>,
    
  36.   bootstrapModules?: Array<string | BootstrapScriptDescriptor>,
    
  37.   progressiveChunkSize?: number,
    
  38.   signal?: AbortSignal,
    
  39.   onError?: (error: mixed) => ?string,
    
  40.   onPostpone?: (reason: string) => void,
    
  41.   unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor,
    
  42.   importMap?: ImportMap,
    
  43.   formState?: ReactFormState<any, any> | null,
    
  44. };
    
  45. 
    
  46. // TODO: Move to sub-classing ReadableStream.
    
  47. type ReactDOMServerReadableStream = ReadableStream & {
    
  48.   allReady: Promise<void>,
    
  49. };
    
  50. 
    
  51. function renderToReadableStream(
    
  52.   children: ReactNodeList,
    
  53.   options?: Options,
    
  54. ): Promise<ReactDOMServerReadableStream> {
    
  55.   return new Promise((resolve, reject) => {
    
  56.     let onFatalError;
    
  57.     let onAllReady;
    
  58.     const allReady = new Promise<void>((res, rej) => {
    
  59.       onAllReady = res;
    
  60.       onFatalError = rej;
    
  61.     });
    
  62. 
    
  63.     function onShellReady() {
    
  64.       const stream: ReactDOMServerReadableStream = (new ReadableStream(
    
  65.         {
    
  66.           type: 'direct',
    
  67.           pull: (controller): ?Promise<void> => {
    
  68.             // $FlowIgnore
    
  69.             startFlowing(request, controller);
    
  70.           },
    
  71.           cancel: (reason): ?Promise<void> => {
    
  72.             stopFlowing(request);
    
  73.             abort(request, reason);
    
  74.           },
    
  75.         },
    
  76.         // $FlowFixMe[prop-missing] size() methods are not allowed on byte streams.
    
  77.         {highWaterMark: 2048},
    
  78.       ): any);
    
  79.       // TODO: Move to sub-classing ReadableStream.
    
  80.       stream.allReady = allReady;
    
  81.       resolve(stream);
    
  82.     }
    
  83.     function onShellError(error: mixed) {
    
  84.       // If the shell errors the caller of `renderToReadableStream` won't have access to `allReady`.
    
  85.       // However, `allReady` will be rejected by `onFatalError` as well.
    
  86.       // So we need to catch the duplicate, uncatchable fatal error in `allReady` to prevent a `UnhandledPromiseRejection`.
    
  87.       allReady.catch(() => {});
    
  88.       reject(error);
    
  89.     }
    
  90.     const resumableState = createResumableState(
    
  91.       options ? options.identifierPrefix : undefined,
    
  92.       options ? options.unstable_externalRuntimeSrc : undefined,
    
  93.     );
    
  94.     const request = createRequest(
    
  95.       children,
    
  96.       resumableState,
    
  97.       createRenderState(
    
  98.         resumableState,
    
  99.         options ? options.nonce : undefined,
    
  100.         options ? options.bootstrapScriptContent : undefined,
    
  101.         options ? options.bootstrapScripts : undefined,
    
  102.         options ? options.bootstrapModules : undefined,
    
  103.         options ? options.unstable_externalRuntimeSrc : undefined,
    
  104.         options ? options.importMap : undefined,
    
  105.       ),
    
  106.       createRootFormatContext(options ? options.namespaceURI : undefined),
    
  107.       options ? options.progressiveChunkSize : undefined,
    
  108.       options ? options.onError : undefined,
    
  109.       onAllReady,
    
  110.       onShellReady,
    
  111.       onShellError,
    
  112.       onFatalError,
    
  113.       options ? options.onPostpone : undefined,
    
  114.       options ? options.formState : undefined,
    
  115.     );
    
  116.     if (options && options.signal) {
    
  117.       const signal = options.signal;
    
  118.       if (signal.aborted) {
    
  119.         abort(request, (signal: any).reason);
    
  120.       } else {
    
  121.         const listener = () => {
    
  122.           abort(request, (signal: any).reason);
    
  123.           signal.removeEventListener('abort', listener);
    
  124.         };
    
  125.         signal.addEventListener('abort', listener);
    
  126.       }
    
  127.     }
    
  128.     startWork(request);
    
  129.   });
    
  130. }
    
  131. 
    
  132. function renderToNodeStream() {
    
  133.   throw new Error(
    
  134.     'ReactDOMServer.renderToNodeStream(): The Node Stream API is not available ' +
    
  135.       'in Bun. Use ReactDOMServer.renderToReadableStream() instead.',
    
  136.   );
    
  137. }
    
  138. 
    
  139. function renderToStaticNodeStream() {
    
  140.   throw new Error(
    
  141.     'ReactDOMServer.renderToStaticNodeStream(): The Node Stream API is not available ' +
    
  142.       'in Bun. Use ReactDOMServer.renderToReadableStream() instead.',
    
  143.   );
    
  144. }
    
  145. 
    
  146. export {
    
  147.   renderToReadableStream,
    
  148.   renderToNodeStream,
    
  149.   renderToStaticNodeStream,
    
  150.   ReactVersion as version,
    
  151. };