|
| 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/ReactDOMServerFormatConfig'; |
| 12 | + |
| 13 | +import ReactVersion from 'shared/ReactVersion'; |
| 14 | + |
| 15 | +import { |
| 16 | + createRequest, |
| 17 | + startWork, |
| 18 | + startFlowing, |
| 19 | + abort, |
| 20 | +} from 'react-server/src/ReactFizzServer'; |
| 21 | + |
| 22 | +import { |
| 23 | + createResponseState, |
| 24 | + createRootFormatContext, |
| 25 | +} from 'react-dom-bindings/src/server/ReactDOMServerFormatConfig'; |
| 26 | + |
| 27 | +type Options = { |
| 28 | + identifierPrefix?: string, |
| 29 | + namespaceURI?: string, |
| 30 | + nonce?: string, |
| 31 | + bootstrapScriptContent?: string, |
| 32 | + bootstrapScripts?: Array<string | BootstrapScriptDescriptor>, |
| 33 | + bootstrapModules?: Array<string | BootstrapScriptDescriptor>, |
| 34 | + progressiveChunkSize?: number, |
| 35 | + signal?: AbortSignal, |
| 36 | + onError?: (error: mixed) => ?string, |
| 37 | + unstable_externalRuntimeSrc?: string | BootstrapScriptDescriptor, |
| 38 | +}; |
| 39 | + |
| 40 | +// TODO: Move to sub-classing ReadableStream. |
| 41 | +type ReactDOMServerReadableStream = ReadableStream & { |
| 42 | + allReady: Promise<void>, |
| 43 | +}; |
| 44 | + |
| 45 | +function renderToReadableStream( |
| 46 | + children: ReactNodeList, |
| 47 | + options?: Options, |
| 48 | +): Promise<ReactDOMServerReadableStream> { |
| 49 | + return new Promise((resolve, reject) => { |
| 50 | + let onFatalError; |
| 51 | + let onAllReady; |
| 52 | + const allReady = new Promise((res, rej) => { |
| 53 | + onAllReady = res; |
| 54 | + onFatalError = rej; |
| 55 | + }); |
| 56 | + |
| 57 | + function onShellReady() { |
| 58 | + const stream: ReactDOMServerReadableStream = (new ReadableStream( |
| 59 | + { |
| 60 | + type: 'direct', |
| 61 | + pull: (controller): ?Promise<void> => { |
| 62 | + // $FlowIgnore |
| 63 | + startFlowing(request, controller); |
| 64 | + }, |
| 65 | + cancel: (reason): ?Promise<void> => { |
| 66 | + abort(request); |
| 67 | + }, |
| 68 | + }, |
| 69 | + // $FlowFixMe size() methods are not allowed on byte streams. |
| 70 | + {highWaterMark: 2048}, |
| 71 | + ): any); |
| 72 | + // TODO: Move to sub-classing ReadableStream. |
| 73 | + stream.allReady = allReady; |
| 74 | + resolve(stream); |
| 75 | + } |
| 76 | + function onShellError(error: mixed) { |
| 77 | + // If the shell errors the caller of `renderToReadableStream` won't have access to `allReady`. |
| 78 | + // However, `allReady` will be rejected by `onFatalError` as well. |
| 79 | + // So we need to catch the duplicate, uncatchable fatal error in `allReady` to prevent a `UnhandledPromiseRejection`. |
| 80 | + allReady.catch(() => {}); |
| 81 | + reject(error); |
| 82 | + } |
| 83 | + const request = createRequest( |
| 84 | + children, |
| 85 | + createResponseState( |
| 86 | + options ? options.identifierPrefix : undefined, |
| 87 | + options ? options.nonce : undefined, |
| 88 | + options ? options.bootstrapScriptContent : undefined, |
| 89 | + options ? options.bootstrapScripts : undefined, |
| 90 | + options ? options.bootstrapModules : undefined, |
| 91 | + options ? options.unstable_externalRuntimeSrc : undefined, |
| 92 | + ), |
| 93 | + createRootFormatContext(options ? options.namespaceURI : undefined), |
| 94 | + options ? options.progressiveChunkSize : undefined, |
| 95 | + options ? options.onError : undefined, |
| 96 | + onAllReady, |
| 97 | + onShellReady, |
| 98 | + onShellError, |
| 99 | + onFatalError, |
| 100 | + ); |
| 101 | + if (options && options.signal) { |
| 102 | + const signal = options.signal; |
| 103 | + if (signal.aborted) { |
| 104 | + abort(request, (signal: any).reason); |
| 105 | + } else { |
| 106 | + const listener = () => { |
| 107 | + abort(request, (signal: any).reason); |
| 108 | + signal.removeEventListener('abort', listener); |
| 109 | + }; |
| 110 | + signal.addEventListener('abort', listener); |
| 111 | + } |
| 112 | + } |
| 113 | + startWork(request); |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +function renderToNodeStream() { |
| 118 | + throw new Error( |
| 119 | + 'ReactDOMServer.renderToNodeStream(): The Node Stream API is not available ' + |
| 120 | + 'in Bun. Use ReactDOMServer.renderToReadableStream() instead.', |
| 121 | + ); |
| 122 | +} |
| 123 | + |
| 124 | +function renderToStaticNodeStream() { |
| 125 | + throw new Error( |
| 126 | + 'ReactDOMServer.renderToStaticNodeStream(): The Node Stream API is not available ' + |
| 127 | + 'in Bun. Use ReactDOMServer.renderToReadableStream() instead.', |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +export { |
| 132 | + renderToReadableStream, |
| 133 | + renderToNodeStream, |
| 134 | + renderToStaticNodeStream, |
| 135 | + ReactVersion as version, |
| 136 | +}; |
0 commit comments