Skip to content

Commit 2b003a5

Browse files
authoredMar 9, 2023
Split out ServerReferenceMetadata into Id and Bound Arguments (#26351)
This is just moving some stuff around and renaming things. This tuple is opaque to the Flight implementation and we should probably encode it separately as a single string instead of a model object. The term "Metadata" isn't the same as when used for ClientReferences so it's not really the right term anyway. I also made it optional since a bound function with no arguments bound is technically different than a raw instance of that function (it's a clone). I also renamed the type ReactModel to ReactClientValue. This is the generic serializable type for something that can pass through the serializable boundary from server to client. There will be another one for client to server. I also filled in missing classes and ensure the serializable sub-types are explicit. E.g. Array and Thenable.
1 parent 62cd5af commit 2b003a5

24 files changed

+179
-122
lines changed
 

Diff for: ‎packages/react-client/src/ReactFlightClient.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type {
1515
ClientReferenceMetadata,
1616
UninitializedModel,
1717
Response,
18-
BundlerConfig,
18+
SSRManifest,
1919
} from './ReactFlightClientHostConfig';
2020

2121
import {
@@ -149,7 +149,7 @@ Chunk.prototype.then = function <T>(
149149
};
150150

151151
export type ResponseBase = {
152-
_bundlerConfig: BundlerConfig,
152+
_bundlerConfig: SSRManifest,
153153
_callServer: CallServerCallback,
154154
_chunks: Map<number, SomeChunk<any>>,
155155
...
@@ -473,13 +473,16 @@ function createModelReject<T>(chunk: SomeChunk<T>): (error: mixed) => void {
473473

474474
function createServerReferenceProxy<A: Iterable<any>, T>(
475475
response: Response,
476-
metaData: {id: any, bound: Thenable<Array<any>>},
476+
metaData: {id: any, bound: null | Thenable<Array<any>>},
477477
): (...A) => Promise<T> {
478478
const callServer = response._callServer;
479479
const proxy = function (): Promise<T> {
480480
// $FlowFixMe[method-unbinding]
481481
const args = Array.prototype.slice.call(arguments);
482482
const p = metaData.bound;
483+
if (!p) {
484+
return callServer(metaData.id, args);
485+
}
483486
if (p.status === INITIALIZED) {
484487
const bound = p.value;
485488
return callServer(metaData.id, bound.concat(args));
@@ -608,7 +611,7 @@ function missingCall() {
608611
}
609612

610613
export function createResponse(
611-
bundlerConfig: BundlerConfig,
614+
bundlerConfig: SSRManifest,
612615
callServer: void | CallServerCallback,
613616
): ResponseBase {
614617
const chunks: Map<number, SomeChunk<any>> = new Map();

Diff for: ‎packages/react-client/src/ReactFlightClientStream.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import type {CallServerCallback} from './ReactFlightClient';
1111
import type {Response} from './ReactFlightClientHostConfigStream';
12-
import type {BundlerConfig} from './ReactFlightClientHostConfig';
12+
import type {SSRManifest} from './ReactFlightClientHostConfig';
1313

1414
import {
1515
resolveModule,
@@ -121,7 +121,7 @@ function createFromJSONCallback(response: Response) {
121121
}
122122

123123
export function createResponse(
124-
bundlerConfig: BundlerConfig,
124+
bundlerConfig: SSRManifest,
125125
callServer: void | CallServerCallback,
126126
): Response {
127127
// NOTE: CHECK THE COMPILER OUTPUT EACH TIME YOU CHANGE THIS.

Diff for: ‎packages/react-client/src/forks/ReactFlightClientHostConfig.custom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
declare var $$$hostConfig: any;
2727

2828
export type Response = any;
29-
export opaque type BundlerConfig = mixed;
29+
export opaque type SSRManifest = mixed;
3030
export opaque type ClientReferenceMetadata = mixed;
3131
export opaque type ClientReference<T> = mixed; // eslint-disable-line no-unused-vars
3232
export const resolveClientReference = $$$hostConfig.resolveClientReference;

Diff for: ‎packages/react-client/src/forks/ReactFlightClientHostConfig.dom-bun.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export * from 'react-client/src/ReactFlightClientHostConfigBrowser';
1111
export * from 'react-client/src/ReactFlightClientHostConfigStream';
1212

1313
export type Response = any;
14-
export opaque type BundlerConfig = mixed;
14+
export opaque type SSRManifest = mixed;
1515
export opaque type ClientReferenceMetadata = mixed;
1616
export opaque type ClientReference<T> = mixed; // eslint-disable-line no-unused-vars
1717
export const resolveClientReference: any = null;

Diff for: ‎packages/react-noop-renderer/src/ReactNoopFlightServer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* environment.
1515
*/
1616

17-
import type {ReactModel} from 'react-server/src/ReactFlightServer';
17+
import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
1818
import type {ServerContextJSONValue} from 'shared/ReactTypes';
1919

2020
import {saveModule} from 'react-noop-renderer/flight-modules';
@@ -71,7 +71,7 @@ type Options = {
7171
identifierPrefix?: string,
7272
};
7373

74-
function render(model: ReactModel, options?: Options): Destination {
74+
function render(model: ReactClientValue, options?: Options): Destination {
7575
const destination: Destination = [];
7676
const bundlerConfig = undefined;
7777
const request = ReactNoopFlightServer.createRequest(

Diff for: ‎packages/react-server-dom-relay/src/ReactFlightDOMRelayClientHostConfig.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import isArray from 'shared/isArray';
3131

3232
export type {ClientReferenceMetadata} from 'ReactFlightDOMRelayClientIntegration';
3333

34-
export type BundlerConfig = null;
34+
export type SSRManifest = null;
3535

3636
export type UninitializedModel = JSONValue;
3737

3838
export type Response = ResponseBase;
3939

4040
export function resolveClientReference<T>(
41-
bundlerConfig: BundlerConfig,
41+
bundlerConfig: SSRManifest,
4242
metadata: ClientReferenceMetadata,
4343
): ClientReference<T> {
4444
return resolveClientReferenceImpl(metadata);

Diff for: ‎packages/react-server-dom-relay/src/ReactFlightDOMRelayServer.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* @flow
88
*/
99

10-
import type {ReactModel} from 'react-server/src/ReactFlightServer';
10+
import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
1111
import type {
12-
BundlerConfig,
12+
ClientManifest,
1313
Destination,
1414
} from './ReactFlightDOMRelayServerHostConfig';
1515

@@ -25,9 +25,9 @@ type Options = {
2525
};
2626

2727
function render(
28-
model: ReactModel,
28+
model: ReactClientValue,
2929
destination: Destination,
30-
config: BundlerConfig,
30+
config: ClientManifest,
3131
options?: Options,
3232
): void {
3333
const request = createRequest(

Diff for: ‎packages/react-server-dom-relay/src/ReactFlightDOMRelayServerHostConfig.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
import type {RowEncoding, JSONValue} from './ReactFlightDOMRelayProtocol';
1111

12-
import type {Request, ReactModel} from 'react-server/src/ReactFlightServer';
12+
import type {
13+
Request,
14+
ReactClientValue,
15+
} from 'react-server/src/ReactFlightServer';
1316

1417
import type {JSResourceReference} from 'JSResourceReference';
1518
import JSResourceReferenceImpl from 'JSResourceReferenceImpl';
@@ -23,7 +26,7 @@ export type ServerReferenceId = {};
2326

2427
import type {
2528
Destination,
26-
BundlerConfig,
29+
BundlerConfig as ClientManifest,
2730
ClientReferenceMetadata,
2831
} from 'ReactFlightDOMRelayServerIntegration';
2932

@@ -37,7 +40,7 @@ import {
3740

3841
export type {
3942
Destination,
40-
BundlerConfig,
43+
BundlerConfig as ClientManifest,
4144
ClientReferenceMetadata,
4245
} from 'ReactFlightDOMRelayServerIntegration';
4346

@@ -60,16 +63,23 @@ export function getClientReferenceKey(
6063
}
6164

6265
export function resolveClientReferenceMetadata<T>(
63-
config: BundlerConfig,
66+
config: ClientManifest,
6467
resource: ClientReference<T>,
6568
): ClientReferenceMetadata {
6669
return resolveClientReferenceMetadataImpl(config, resource);
6770
}
6871

69-
export function resolveServerReferenceMetadata<T>(
70-
config: BundlerConfig,
72+
export function getServerReferenceId<T>(
73+
config: ClientManifest,
74+
resource: ServerReference<T>,
75+
): ServerReferenceId {
76+
throw new Error('Not implemented.');
77+
}
78+
79+
export function getServerReferenceBoundArguments<T>(
80+
config: ClientManifest,
7181
resource: ServerReference<T>,
72-
): {id: ServerReferenceId, bound: Promise<Array<any>>} {
82+
): Array<ReactClientValue> {
7383
throw new Error('Not implemented.');
7484
}
7585

@@ -125,9 +135,9 @@ export function processErrorChunkDev(
125135

126136
function convertModelToJSON(
127137
request: Request,
128-
parent: {+[key: string]: ReactModel} | $ReadOnlyArray<ReactModel>,
138+
parent: {+[key: string]: ReactClientValue} | $ReadOnlyArray<ReactClientValue>,
129139
key: string,
130-
model: ReactModel,
140+
model: ReactClientValue,
131141
): JSONValue {
132142
const json = resolveModelToJSON(request, parent, key, model);
133143
if (typeof json === 'object' && json !== null) {
@@ -160,7 +170,7 @@ function convertModelToJSON(
160170
export function processModelChunk(
161171
request: Request,
162172
id: number,
163-
model: ReactModel,
173+
model: ReactClientValue,
164174
): Chunk {
165175
// $FlowFixMe no good way to define an empty exact object
166176
const json = convertModelToJSON(request, {}, '', model);

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightClientNodeBundlerConfig.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import type {
1313
RejectedThenable,
1414
} from 'shared/ReactTypes';
1515

16-
export type WebpackSSRMap = {
16+
export type SSRManifest = {
1717
[clientId: string]: {
1818
[clientExportName: string]: ClientReference<any>,
1919
},
2020
};
2121

22-
export type BundlerConfig = WebpackSSRMap;
23-
2422
export opaque type ClientReferenceMetadata = {
2523
id: string,
2624
chunks: Array<string>,
@@ -34,7 +32,7 @@ export opaque type ClientReference<T> = {
3432
};
3533

3634
export function resolveClientReference<T>(
37-
bundlerConfig: BundlerConfig,
35+
bundlerConfig: SSRManifest,
3836
metadata: ClientReferenceMetadata,
3937
): ClientReference<T> {
4038
const resolvedModuleData = bundlerConfig[metadata.id][metadata.name];

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightClientWebpackBundlerConfig.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ import type {
1313
RejectedThenable,
1414
} from 'shared/ReactTypes';
1515

16-
export type WebpackSSRMap = {
16+
export type SSRManifest = null | {
1717
[clientId: string]: {
1818
[clientExportName: string]: ClientReferenceMetadata,
1919
},
2020
};
2121

22-
export type BundlerConfig = null | WebpackSSRMap;
23-
2422
export opaque type ClientReferenceMetadata = {
2523
id: string,
2624
chunks: Array<string>,
@@ -32,7 +30,7 @@ export opaque type ClientReferenceMetadata = {
3230
export opaque type ClientReference<T> = ClientReferenceMetadata;
3331

3432
export function resolveClientReference<T>(
35-
bundlerConfig: BundlerConfig,
33+
bundlerConfig: SSRManifest,
3634
metadata: ClientReferenceMetadata,
3735
): ClientReference<T> {
3836
if (bundlerConfig) {

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightDOMClientEdge.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {Thenable} from 'shared/ReactTypes.js';
1111

1212
import type {Response as FlightResponse} from 'react-client/src/ReactFlightClientStream';
1313

14-
import type {BundlerConfig} from './ReactFlightClientWebpackBundlerConfig';
14+
import type {SSRManifest} from './ReactFlightClientWebpackBundlerConfig';
1515

1616
import {
1717
createResponse,
@@ -30,7 +30,7 @@ function noServerCall() {
3030
}
3131

3232
export type Options = {
33-
moduleMap?: BundlerConfig,
33+
moduleMap?: SSRManifest,
3434
};
3535

3636
function createResponseFromOptions(options: void | Options) {

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightDOMClientNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {Thenable} from 'shared/ReactTypes.js';
1111

1212
import type {Response} from 'react-client/src/ReactFlightClientStream';
1313

14-
import type {BundlerConfig} from 'react-client/src/ReactFlightClientHostConfig';
14+
import type {SSRManifest} from 'react-client/src/ReactFlightClientHostConfig';
1515

1616
import type {Readable} from 'stream';
1717

@@ -34,7 +34,7 @@ function noServerCall() {
3434

3535
function createFromNodeStream<T>(
3636
stream: Readable,
37-
moduleMap: $NonMaybeType<BundlerConfig>,
37+
moduleMap: $NonMaybeType<SSRManifest>,
3838
): Thenable<T> {
3939
const response: Response = createResponse(moduleMap, noServerCall);
4040
stream.on('data', chunk => {

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightDOMServerBrowser.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* @flow
88
*/
99

10-
import type {ReactModel} from 'react-server/src/ReactFlightServer';
10+
import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
1111
import type {ServerContextJSONValue} from 'shared/ReactTypes';
12-
import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig';
12+
import type {ClientManifest} from './ReactFlightServerWebpackBundlerConfig';
1313

1414
import {
1515
createRequest,
@@ -26,8 +26,8 @@ type Options = {
2626
};
2727

2828
function renderToReadableStream(
29-
model: ReactModel,
30-
webpackMap: BundlerConfig,
29+
model: ReactClientValue,
30+
webpackMap: ClientManifest,
3131
options?: Options,
3232
): ReadableStream {
3333
const request = createRequest(

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightDOMServerEdge.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* @flow
88
*/
99

10-
import type {ReactModel} from 'react-server/src/ReactFlightServer';
10+
import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
1111
import type {ServerContextJSONValue} from 'shared/ReactTypes';
12-
import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig';
12+
import type {ClientManifest} from './ReactFlightServerWebpackBundlerConfig';
1313

1414
import {
1515
createRequest,
@@ -26,8 +26,8 @@ type Options = {
2626
};
2727

2828
function renderToReadableStream(
29-
model: ReactModel,
30-
webpackMap: BundlerConfig,
29+
model: ReactClientValue,
30+
webpackMap: ClientManifest,
3131
options?: Options,
3232
): ReadableStream {
3333
const request = createRequest(

Diff for: ‎packages/react-server-dom-webpack/src/ReactFlightDOMServerNode.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
* @flow
88
*/
99

10-
import type {Request, ReactModel} from 'react-server/src/ReactFlightServer';
10+
import type {
11+
Request,
12+
ReactClientValue,
13+
} from 'react-server/src/ReactFlightServer';
1114
import type {Destination} from 'react-server/src/ReactServerStreamConfigNode';
12-
import type {BundlerConfig} from './ReactFlightServerWebpackBundlerConfig';
15+
import type {ClientManifest} from './ReactFlightServerWebpackBundlerConfig';
1316
import type {Writable} from 'stream';
1417
import type {ServerContextJSONValue} from 'shared/ReactTypes';
1518

@@ -36,8 +39,8 @@ type PipeableStream = {
3639
};
3740

3841
function renderToPipeableStream(
39-
model: ReactModel,
40-
webpackMap: BundlerConfig,
42+
model: ReactClientValue,
43+
webpackMap: ClientManifest,
4144
options?: Options,
4245
): PipeableStream {
4346
const request = createRequest(

0 commit comments

Comments
 (0)