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} from 'shared/ReactTypes';
    
  11. import type {BootstrapScriptDescriptor} from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
    
  12. import type {PostponedState} from 'react-server/src/ReactFizzServer';
    
  13. import type {ImportMap} from '../shared/ReactDOMTypes';
    
  14. 
    
  15. import ReactVersion from 'shared/ReactVersion';
    
  16. 
    
  17. import {
    
  18.   createPrerenderRequest,
    
  19.   startWork,
    
  20.   startFlowing,
    
  21.   stopFlowing,
    
  22.   abort,
    
  23.   getPostponedState,
    
  24. } from 'react-server/src/ReactFizzServer';
    
  25. 
    
  26. import {
    
  27.   createResumableState,
    
  28.   createRenderState,
    
  29.   createRootFormatContext,
    
  30. } from 'react-dom-bindings/src/server/ReactFizzConfigDOM';
    
  31. 
    
  32. type Options = {
    
  33.   identifierPrefix?: string,
    
  34.   namespaceURI?: string,
    
  35.   bootstrapScriptContent?: string,
    
  36.   bootstrapScripts?: Array<string | BootstrapScriptDescriptor>,
    
  37.   bootstrapModules?: Array<string | BootstrapScriptDescriptor>,
    
  38.   progressiveChunkSize?: number,
    
  39.   signal?: AbortSignal,
    
  40.   onError?: (error: mixed) => ?string,
    
  41.   onPostpone?: (reason: string) => void,
    
  42.   unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor,
    
  43.   importMap?: ImportMap,
    
  44. };
    
  45. 
    
  46. type StaticResult = {
    
  47.   postponed: null | PostponedState,
    
  48.   prelude: ReadableStream,
    
  49. };
    
  50. 
    
  51. function prerender(
    
  52.   children: ReactNodeList,
    
  53.   options?: Options,
    
  54. ): Promise<StaticResult> {
    
  55.   return new Promise((resolve, reject) => {
    
  56.     const onFatalError = reject;
    
  57. 
    
  58.     function onAllReady() {
    
  59.       const stream = new ReadableStream(
    
  60.         {
    
  61.           type: 'bytes',
    
  62.           pull: (controller): ?Promise<void> => {
    
  63.             startFlowing(request, controller);
    
  64.           },
    
  65.           cancel: (reason): ?Promise<void> => {
    
  66.             stopFlowing(request);
    
  67.             abort(request, reason);
    
  68.           },
    
  69.         },
    
  70.         // $FlowFixMe[prop-missing] size() methods are not allowed on byte streams.
    
  71.         {highWaterMark: 0},
    
  72.       );
    
  73. 
    
  74.       const result = {
    
  75.         postponed: getPostponedState(request),
    
  76.         prelude: stream,
    
  77.       };
    
  78.       resolve(result);
    
  79.     }
    
  80.     const resources = createResumableState(
    
  81.       options ? options.identifierPrefix : undefined,
    
  82.       options ? options.unstable_externalRuntimeSrc : undefined,
    
  83.     );
    
  84.     const request = createPrerenderRequest(
    
  85.       children,
    
  86.       resources,
    
  87.       createRenderState(
    
  88.         resources,
    
  89.         undefined, // nonce is not compatible with prerendered bootstrap scripts
    
  90.         options ? options.bootstrapScriptContent : undefined,
    
  91.         options ? options.bootstrapScripts : undefined,
    
  92.         options ? options.bootstrapModules : undefined,
    
  93.         options ? options.unstable_externalRuntimeSrc : undefined,
    
  94.         options ? options.importMap : undefined,
    
  95.       ),
    
  96.       createRootFormatContext(options ? options.namespaceURI : undefined),
    
  97.       options ? options.progressiveChunkSize : undefined,
    
  98.       options ? options.onError : undefined,
    
  99.       onAllReady,
    
  100.       undefined,
    
  101.       undefined,
    
  102.       onFatalError,
    
  103.       options ? options.onPostpone : undefined,
    
  104.     );
    
  105.     if (options && options.signal) {
    
  106.       const signal = options.signal;
    
  107.       if (signal.aborted) {
    
  108.         abort(request, (signal: any).reason);
    
  109.       } else {
    
  110.         const listener = () => {
    
  111.           abort(request, (signal: any).reason);
    
  112.           signal.removeEventListener('abort', listener);
    
  113.         };
    
  114.         signal.addEventListener('abort', listener);
    
  115.       }
    
  116.     }
    
  117.     startWork(request);
    
  118.   });
    
  119. }
    
  120. 
    
  121. export {prerender, ReactVersion as version};