-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out Edge and Node implementations of the Flight Client (#26187)
This splits out the Edge and Node implementations of Flight Client into their own implementations. The Node implementation now takes a Node Stream as input. I removed the bundler config from the Browser variant because you're never supposed to use that in the browser since it's only for SSR. Similarly, it's required on the server. This also enables generating a SSR manifest from the Webpack plugin. This is necessary for SSR so that you can reverse look up what a client module is called on the server. I also removed the option to pass a callServer from the server. We might want to add it back in the future but basically, we don't recommend calling Server Functions from render for initial render because if that happened client-side it would be a client-side waterfall. If it's never called in initial render, then it also shouldn't ever happen during SSR. This might be considered too restrictive. ~This also compiles the unbundled packages as ESM. This isn't strictly necessary because we only need access to dynamic import to load the modules but we don't have any other build options that leave `import(...)` intact, and seems appropriate that this would also be an ESM module.~ Went with `import(...)` in CJS instead.
- Loading branch information
1 parent
70b0bbd
commit 60144a0
Showing
28 changed files
with
657 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/react-client/src/ReactFlightClientHostConfigNode.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import {TextDecoder} from 'util'; | ||
|
||
export type StringDecoder = TextDecoder; | ||
|
||
export const supportsBinaryStreams = true; | ||
|
||
export function createStringDecoder(): StringDecoder { | ||
return new TextDecoder(); | ||
} | ||
|
||
const decoderOptions = {stream: true}; | ||
|
||
export function readPartialStringChunk( | ||
decoder: StringDecoder, | ||
buffer: Uint8Array, | ||
): string { | ||
return decoder.decode(buffer, decoderOptions); | ||
} | ||
|
||
export function readFinalStringChunk( | ||
decoder: StringDecoder, | ||
buffer: Uint8Array, | ||
): string { | ||
return decoder.decode(buffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
* @flow | ||
*/ | ||
|
||
export * from './src/ReactFlightDOMClient'; | ||
export * from './src/ReactFlightDOMClientBrowser'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
* @flow | ||
*/ | ||
|
||
export * from './src/ReactFlightDOMClient'; | ||
export * from './src/ReactFlightDOMClientEdge'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
* @flow | ||
*/ | ||
|
||
export * from './src/ReactFlightDOMClient'; | ||
export * from './src/ReactFlightDOMClientNode'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
* @flow | ||
*/ | ||
|
||
export * from './src/ReactFlightDOMClient'; | ||
export * from './src/ReactFlightDOMClientNode'; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/react-server-dom-webpack/src/ReactFlightClientNodeBundlerConfig.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type { | ||
Thenable, | ||
FulfilledThenable, | ||
RejectedThenable, | ||
} from 'shared/ReactTypes'; | ||
|
||
export type WebpackSSRMap = { | ||
[clientId: string]: { | ||
[clientExportName: string]: ClientReference<any>, | ||
}, | ||
}; | ||
|
||
export type BundlerConfig = WebpackSSRMap; | ||
|
||
export opaque type ClientReferenceMetadata = { | ||
id: string, | ||
chunks: Array<string>, | ||
name: string, | ||
async: boolean, | ||
}; | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
export opaque type ClientReference<T> = { | ||
specifier: string, | ||
name: string, | ||
}; | ||
|
||
export function resolveClientReference<T>( | ||
bundlerConfig: BundlerConfig, | ||
metadata: ClientReferenceMetadata, | ||
): ClientReference<T> { | ||
const resolvedModuleData = bundlerConfig[metadata.id][metadata.name]; | ||
return resolvedModuleData; | ||
} | ||
|
||
const asyncModuleCache: Map<string, Thenable<any>> = new Map(); | ||
|
||
export function preloadModule<T>( | ||
metadata: ClientReference<T>, | ||
): null | Thenable<any> { | ||
const existingPromise = asyncModuleCache.get(metadata.specifier); | ||
if (existingPromise) { | ||
if (existingPromise.status === 'fulfilled') { | ||
return null; | ||
} | ||
return existingPromise; | ||
} else { | ||
// $FlowFixMe[unsupported-syntax] | ||
const modulePromise: Thenable<T> = import(metadata.specifier); | ||
modulePromise.then( | ||
value => { | ||
const fulfilledThenable: FulfilledThenable<mixed> = | ||
(modulePromise: any); | ||
fulfilledThenable.status = 'fulfilled'; | ||
fulfilledThenable.value = value; | ||
}, | ||
reason => { | ||
const rejectedThenable: RejectedThenable<mixed> = (modulePromise: any); | ||
rejectedThenable.status = 'rejected'; | ||
rejectedThenable.reason = reason; | ||
}, | ||
); | ||
asyncModuleCache.set(metadata.specifier, modulePromise); | ||
return modulePromise; | ||
} | ||
} | ||
|
||
export function requireModule<T>(metadata: ClientReference<T>): T { | ||
let moduleExports; | ||
// We assume that preloadModule has been called before, which | ||
// should have added something to the module cache. | ||
const promise: any = asyncModuleCache.get(metadata.specifier); | ||
if (promise.status === 'fulfilled') { | ||
moduleExports = promise.value; | ||
} else { | ||
throw promise.reason; | ||
} | ||
if (metadata.name === '*') { | ||
// This is a placeholder value that represents that the caller imported this | ||
// as a CommonJS module as is. | ||
return moduleExports; | ||
} | ||
if (metadata.name === '') { | ||
// This is a placeholder value that represents that the caller accessed the | ||
// default property of this if it was an ESM interop module. | ||
return moduleExports.default; | ||
} | ||
return moduleExports[metadata.name]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
packages/react-server-dom-webpack/src/ReactFlightDOMClientEdge.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Thenable} from 'shared/ReactTypes.js'; | ||
|
||
import type {Response as FlightResponse} from 'react-client/src/ReactFlightClientStream'; | ||
|
||
import type {BundlerConfig} from './ReactFlightClientWebpackBundlerConfig'; | ||
|
||
import { | ||
createResponse, | ||
getRoot, | ||
reportGlobalError, | ||
processBinaryChunk, | ||
close, | ||
} from 'react-client/src/ReactFlightClientStream'; | ||
|
||
function noServerCall() { | ||
throw new Error( | ||
'Server Functions cannot be called during initial render. ' + | ||
'This would create a fetch waterfall. Try to use a Server Component ' + | ||
'to pass data to Client Components instead.', | ||
); | ||
} | ||
|
||
export type Options = { | ||
moduleMap?: BundlerConfig, | ||
}; | ||
|
||
function createResponseFromOptions(options: void | Options) { | ||
return createResponse( | ||
options && options.moduleMap ? options.moduleMap : null, | ||
noServerCall, | ||
); | ||
} | ||
|
||
function startReadingFromStream( | ||
response: FlightResponse, | ||
stream: ReadableStream, | ||
): void { | ||
const reader = stream.getReader(); | ||
function progress({ | ||
done, | ||
value, | ||
}: { | ||
done: boolean, | ||
value: ?any, | ||
... | ||
}): void | Promise<void> { | ||
if (done) { | ||
close(response); | ||
return; | ||
} | ||
const buffer: Uint8Array = (value: any); | ||
processBinaryChunk(response, buffer); | ||
return reader.read().then(progress).catch(error); | ||
} | ||
function error(e: any) { | ||
reportGlobalError(response, e); | ||
} | ||
reader.read().then(progress).catch(error); | ||
} | ||
|
||
function createFromReadableStream<T>( | ||
stream: ReadableStream, | ||
options?: Options, | ||
): Thenable<T> { | ||
const response: FlightResponse = createResponseFromOptions(options); | ||
startReadingFromStream(response, stream); | ||
return getRoot(response); | ||
} | ||
|
||
function createFromFetch<T>( | ||
promiseForResponse: Promise<Response>, | ||
options?: Options, | ||
): Thenable<T> { | ||
const response: FlightResponse = createResponseFromOptions(options); | ||
promiseForResponse.then( | ||
function (r) { | ||
startReadingFromStream(response, (r.body: any)); | ||
}, | ||
function (e) { | ||
reportGlobalError(response, e); | ||
}, | ||
); | ||
return getRoot(response); | ||
} | ||
|
||
export {createFromFetch, createFromReadableStream}; |
Oops, something went wrong.