| 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 { |
| 11 | Request, |
| 12 | ReactClientValue, |
| 13 | } from 'react-server/src/ReactFlightServer'; |
| 14 | import type {Thenable} from 'shared/ReactTypes'; |
| 15 | import type {ClientManifest} from './ReactFlightServerConfigWebpackBundler'; |
| 16 | import type {ServerManifest} from 'react-client/src/ReactFlightClientConfig'; |
| 17 | |
| 18 | import { |
| 19 | createRequest, |
| 20 | createPrerenderRequest, |
| 21 | startWork, |
| 22 | startFlowing, |
| 23 | startFlowingDebug, |
| 24 | stopFlowing, |
| 25 | abort, |
| 26 | attachAbortSignal, |
| 27 | resolveDebugMessage, |
| 28 | closeDebugChannel, |
| 29 | } from 'react-server/src/ReactFlightServer'; |
| 30 | |
| 31 | import { |
| 32 | createResponse, |
| 33 | close, |
| 34 | getRoot, |
| 35 | } from 'react-server/src/ReactFlightReplyServer'; |
| 36 | |
| 37 | import { |
| 38 | decodeAction, |
| 39 | decodeFormState, |
| 40 | } from 'react-server/src/ReactFlightActionServer'; |
| 41 | |
| 42 | export { |
| 43 | registerServerReference, |
| 44 | registerClientReference, |
| 45 | createClientModuleProxy, |
| 46 | } from '../ReactFlightWebpackReferences'; |
| 47 | |
| 48 | import { |
| 49 | createStringDecoder, |
| 50 | readPartialStringChunk, |
| 51 | readFinalStringChunk, |
| 52 | } from 'react-client/src/ReactFlightClientStreamConfigWeb'; |
| 53 | |
| 54 | import type {TemporaryReferenceSet} from 'react-server/src/ReactFlightServerTemporaryReferences'; |
| 55 | |
| 56 | export {createTemporaryReferenceSet} from 'react-server/src/ReactFlightServerTemporaryReferences'; |
| 57 | |
| 58 | export type {TemporaryReferenceSet}; |
| 59 | |
| 60 | type Options = { |
| 61 | debugChannel?: {readable?: ReadableStream, writable?: WritableStream, ...}, |
| 62 | environmentName?: string | (() => string), |
| 63 | filterStackFrame?: (url: string, functionName: string) => boolean, |
| 64 | identifierPrefix?: string, |
| 65 | signal?: AbortSignal, |
| 66 | temporaryReferences?: TemporaryReferenceSet, |
| 67 | onError?: (error: mixed) => void, |
| 68 | startTime?: number, |
| 69 | }; |
| 70 | |
| 71 | function startReadingFromDebugChannelReadableStream( |
| 72 | request: Request, |
| 73 | stream: ReadableStream, |
| 74 | ): void { |
| 75 | const reader = stream.getReader(); |
| 76 | const stringDecoder = createStringDecoder(); |
| 77 | let stringBuffer = ''; |
| 78 | function progress({ |
| 79 | done, |
| 80 | value, |
| 81 | }: { |
| 82 | done: boolean, |
| 83 | value: ?any, |
| 84 | ... |
| 85 | }): void | Promise<void> { |
| 86 | const buffer: Uint8Array = value as any; |
| 87 | stringBuffer += done |
| 88 | ? readFinalStringChunk(stringDecoder, new Uint8Array(0)) |
| 89 | : readPartialStringChunk(stringDecoder, buffer); |
| 90 | const messages = stringBuffer.split('\n'); |
| 91 | for (let i = 0; i < messages.length - 1; i++) { |
| 92 | resolveDebugMessage(request, messages[i]); |
| 93 | } |
| 94 | stringBuffer = messages[messages.length - 1]; |
| 95 | if (done) { |
| 96 | closeDebugChannel(request); |
| 97 | return; |
| 98 | } |
| 99 | return reader.read().then(progress).catch(error); |
| 100 | } |
| 101 | function error(e: any) { |
| 102 | abort( |
| 103 | request, |
| 104 | new Error('Lost connection to the Debug Channel.', { |
| 105 | cause: e, |
| 106 | }), |
| 107 | ); |
| 108 | } |
| 109 | reader.read().then(progress).catch(error); |
| 110 | } |
| 111 | |
| 112 | function renderToReadableStream( |
| 113 | model: ReactClientValue, |
| 114 | webpackMap: ClientManifest, |
| 115 | options?: Options, |
| 116 | ): ReadableStream { |
| 117 | const debugChannelReadable = |
| 118 | __DEV__ && options && options.debugChannel |
| 119 | ? options.debugChannel.readable |
| 120 | : undefined; |
| 121 | const debugChannelWritable = |
| 122 | __DEV__ && options && options.debugChannel |
| 123 | ? options.debugChannel.writable |
| 124 | : undefined; |
| 125 | const request = createRequest( |
| 126 | model, |
| 127 | webpackMap, |
| 128 | options ? options.onError : undefined, |
| 129 | options ? options.identifierPrefix : undefined, |
| 130 | options ? options.temporaryReferences : undefined, |
| 131 | options ? options.startTime : undefined, |
| 132 | __DEV__ && options ? options.environmentName : undefined, |
| 133 | __DEV__ && options ? options.filterStackFrame : undefined, |
| 134 | debugChannelReadable !== undefined, |
| 135 | ); |
| 136 | if (options && options.signal) { |
| 137 | attachAbortSignal(request, options.signal); |
| 138 | } |
| 139 | if (debugChannelWritable !== undefined) { |
| 140 | const debugStream = new ReadableStream( |
| 141 | { |
| 142 | type: 'bytes', |
| 143 | pull: (controller): ?Promise<void> => { |
| 144 | startFlowingDebug(request, controller); |
| 145 | }, |
| 146 | }, |
| 147 | // $FlowFixMe[prop-missing] size() methods are not allowed on byte streams. |
| 148 | // $FlowFixMe[incompatible-type] |
| 149 | {highWaterMark: 0}, |
| 150 | ); |
| 151 | debugStream.pipeTo(debugChannelWritable); |
| 152 | } |
| 153 | if (debugChannelReadable !== undefined) { |
| 154 | startReadingFromDebugChannelReadableStream(request, debugChannelReadable); |
| 155 | } |
| 156 | const stream = new ReadableStream( |
| 157 | { |
| 158 | type: 'bytes', |
| 159 | start: (controller): ?Promise<void> => { |
| 160 | startWork(request); |
| 161 | }, |
| 162 | pull: (controller): ?Promise<void> => { |
| 163 | startFlowing(request, controller); |
| 164 | }, |
| 165 | cancel: (reason): ?Promise<void> => { |
| 166 | stopFlowing(request); |
| 167 | abort(request, reason); |
| 168 | }, |
| 169 | }, |
| 170 | // $FlowFixMe[prop-missing] size() methods are not allowed on byte streams. |
| 171 | // $FlowFixMe[incompatible-type] |
| 172 | {highWaterMark: 0}, |
| 173 | ); |
| 174 | return stream; |
| 175 | } |
| 176 | |
| 177 | type StaticResult = { |
| 178 | prelude: ReadableStream, |
| 179 | }; |
| 180 | |
| 181 | function prerender( |
| 182 | model: ReactClientValue, |
| 183 | webpackMap: ClientManifest, |
| 184 | options?: Options, |
| 185 | ): Promise<StaticResult> { |
| 186 | return new Promise((resolve, reject) => { |
| 187 | const onFatalError = reject; |
| 188 | function onAllReady() { |
| 189 | const stream = new ReadableStream( |
| 190 | { |
| 191 | type: 'bytes', |
| 192 | pull: (controller): ?Promise<void> => { |
| 193 | startFlowing(request, controller); |
| 194 | }, |
| 195 | cancel: (reason): ?Promise<void> => { |
| 196 | stopFlowing(request); |
| 197 | abort(request, reason); |
| 198 | }, |
| 199 | }, |
| 200 | // $FlowFixMe[prop-missing] size() methods are not allowed on byte streams. |
| 201 | // $FlowFixMe[incompatible-type] |
| 202 | {highWaterMark: 0}, |
| 203 | ); |
| 204 | resolve({prelude: stream}); |
| 205 | } |
| 206 | const request = createPrerenderRequest( |
| 207 | model, |
| 208 | webpackMap, |
| 209 | onAllReady, |
| 210 | onFatalError, |
| 211 | options ? options.onError : undefined, |
| 212 | options ? options.identifierPrefix : undefined, |
| 213 | options ? options.temporaryReferences : undefined, |
| 214 | options ? options.startTime : undefined, |
| 215 | __DEV__ && options ? options.environmentName : undefined, |
| 216 | __DEV__ && options ? options.filterStackFrame : undefined, |
| 217 | false, |
| 218 | ); |
| 219 | if (options && options.signal) { |
| 220 | attachAbortSignal(request, options.signal); |
| 221 | } |
| 222 | startWork(request); |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | function decodeReply<T>( |
| 227 | body: string | FormData, |
| 228 | webpackMap: ServerManifest, |
| 229 | options?: { |
| 230 | temporaryReferences?: TemporaryReferenceSet, |
| 231 | arraySizeLimit?: number, |
| 232 | }, |
| 233 | ): Thenable<T> { |
| 234 | if (typeof body === 'string') { |
| 235 | const form = new FormData(); |
| 236 | form.append('0', body); |
| 237 | body = form; |
| 238 | } |
| 239 | const response = createResponse( |
| 240 | webpackMap, |
| 241 | '', |
| 242 | options ? options.temporaryReferences : undefined, |
| 243 | body, |
| 244 | options ? options.arraySizeLimit : undefined, |
| 245 | ); |
| 246 | const root = getRoot<T>(response); |
| 247 | close(response); |
| 248 | return root; |
| 249 | } |
| 250 | |
| 251 | export { |
| 252 | renderToReadableStream, |
| 253 | prerender, |
| 254 | decodeReply, |
| 255 | decodeAction, |
| 256 | decodeFormState, |
| 257 | }; |