Skip to content

Commit

Permalink
Clean up inconsistent naming in Flight implementation, part 1
Browse files Browse the repository at this point in the history
Since `ClientManifest` and `SSRManifest` are now clearly differentiated
in their structure (via facebook#26300) and type names (via facebook#26351), this opened
up the opportunity to clean up some inconsistencies in how parameters,
variables and properties were named (e.g. `config`, `bundlerConfig`,
`moduleMap`, `webpackMap`, ...). To improve readability and avoid
confusion between the two different types of objects we now always name
them either `clientManifest` or `ssrManifest`.
  • Loading branch information
unstubbable committed Mar 9, 2023
1 parent 3706edb commit 5392e75
Show file tree
Hide file tree
Showing 29 changed files with 132 additions and 130 deletions.
6 changes: 3 additions & 3 deletions fixtures/flight/server/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ app.all('/', async function (req, res, next) {
virtualFs = fs;
buildPath = path.join(__dirname, '../build/');
}
// Read the module map from the virtual file system.
const moduleMap = JSON.parse(
// Read the SSR manifest from the virtual file system.
const ssrManifest = JSON.parse(
await virtualFs.readFile(
path.join(buildPath, 'react-ssr-manifest.json'),
'utf8'
Expand All @@ -140,7 +140,7 @@ app.all('/', async function (req, res, next) {
// For HTML, we're a "client" emulator that runs the client code,
// so we start by consuming the RSC payload. This needs a module
// map that reverse engineers the client-side path to the SSR path.
const root = await createFromNodeStream(rscResponse, moduleMap);
const root = await createFromNodeStream(rscResponse, ssrManifest);
// Render it into HTML by resolving the client components
res.set('Content-type', 'text/html');
const {pipe} = renderToPipeableStream(root, {
Expand Down
12 changes: 6 additions & 6 deletions fixtures/flight/server/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ app.get('/', async function (req, res) {
// const m = require('../src/App.js');
const m = await import('../src/App.js');

let moduleMap;
let clientManifest;
let mainCSSChunks;
if (process.env.NODE_ENV === 'development') {
// Read the module map from the HMR server in development.
moduleMap = await (
// Read the client manifest from the HMR server in development.
clientManifest = await (
await fetch('http://localhost:3000/react-client-manifest.json')
).json();
mainCSSChunks = (
Expand All @@ -64,8 +64,8 @@ app.get('/', async function (req, res) {
).json()
).main.css;
} else {
// Read the module map from the static build in production.
moduleMap = JSON.parse(
// Read the client manifest from the static build in production.
clientManifest = JSON.parse(
await readFile(
path.resolve(__dirname, `../build/react-client-manifest.json`),
'utf8'
Expand All @@ -90,7 +90,7 @@ app.get('/', async function (req, res) {
),
React.createElement(App),
];
const {pipe} = renderToPipeableStream(root, moduleMap);
const {pipe} = renderToPipeableStream(root, clientManifest);
pipe(res);
});

Expand Down
8 changes: 4 additions & 4 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Chunk.prototype.then = function <T>(
};

export type ResponseBase = {
_bundlerConfig: SSRManifest,
_ssrManifest: SSRManifest,
_callServer: CallServerCallback,
_chunks: Map<number, SomeChunk<any>>,
...
Expand Down Expand Up @@ -611,12 +611,12 @@ function missingCall() {
}

export function createResponse(
bundlerConfig: SSRManifest,
ssrManifest: SSRManifest,
callServer: void | CallServerCallback,
): ResponseBase {
const chunks: Map<number, SomeChunk<any>> = new Map();
const response = {
_bundlerConfig: bundlerConfig,
_ssrManifest: ssrManifest,
_callServer: callServer !== undefined ? callServer : missingCall,
_chunks: chunks,
};
Expand Down Expand Up @@ -649,7 +649,7 @@ export function resolveModule(
model,
);
const clientReference = resolveClientReference<$FlowFixMe>(
response._bundlerConfig,
response._ssrManifest,
clientReferenceMetadata,
);

Expand Down
4 changes: 2 additions & 2 deletions packages/react-client/src/ReactFlightClientStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ function createFromJSONCallback(response: Response) {
}

export function createResponse(
bundlerConfig: SSRManifest,
ssrManifest: SSRManifest,
callServer: void | CallServerCallback,
): Response {
// NOTE: CHECK THE COMPILER OUTPUT EACH TIME YOU CHANGE THIS.
// It should be inlined to one object literal but minor changes can break it.
const stringDecoder = supportsBinaryStreams ? createStringDecoder() : null;
const response: any = createResponseBase(bundlerConfig, callServer);
const response: any = createResponseBase(ssrManifest, callServer);
response._partialRow = '';
if (supportsBinaryStreams) {
response._stringDecoder = stringDecoder;
Expand Down
2 changes: 1 addition & 1 deletion packages/react-noop-renderer/src/ReactNoopFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Source = Array<string>;

const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
supportsBinaryStreams: false,
resolveClientReference(bundlerConfig: null, idx: string) {
resolveClientReference(ssrManifest: null, idx: string) {
return idx;
},
preloadModule(idx: string) {},
Expand Down
14 changes: 7 additions & 7 deletions packages/react-noop-renderer/src/ReactNoopFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ const ReactNoopFlightServer = ReactFlightServer({
isServerReference(reference: Object): boolean {
return reference.$$typeof === Symbol.for('react.server.reference');
},
getClientReferenceKey(reference: Object): Object {
return reference;
getClientReferenceKey(clientReference: Object): Object {
return clientReference;
},
resolveClientReferenceMetadata(
config: void,
reference: {$$typeof: symbol, value: any},
clientManifest: void,
clientReference: {$$typeof: symbol, value: any},
) {
return saveModule(reference.value);
return saveModule(clientReference.value);
},
});

Expand All @@ -73,10 +73,10 @@ type Options = {

function render(model: ReactClientValue, options?: Options): Destination {
const destination: Destination = [];
const bundlerConfig = undefined;
const clientManifest = undefined;
const request = ReactNoopFlightServer.createRequest(
model,
bundlerConfig,
clientManifest,
options ? options.onError : undefined,
options ? options.context : undefined,
options ? options.identifierPrefix : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type UninitializedModel = JSONValue;
export type Response = ResponseBase;

export function resolveClientReference<T>(
bundlerConfig: SSRManifest,
ssrManifest: SSRManifest,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
return resolveClientReferenceImpl(metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type Options = {
function render(
model: ReactClientValue,
destination: Destination,
config: ClientManifest,
clientManifest: ClientManifest,
options?: Options,
): void {
const request = createRequest(
model,
config,
clientManifest,
options ? options.onError : undefined,
undefined, // not currently set up to supply context overrides
options ? options.identifierPrefix : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type ServerReferenceId = {};

import type {
Destination,
BundlerConfig as ClientManifest,
ClientManifest,
ClientReferenceMetadata,
} from 'ReactFlightDOMRelayServerIntegration';

Expand All @@ -40,7 +40,7 @@ import {

export type {
Destination,
BundlerConfig as ClientManifest,
ClientManifest,
ClientReferenceMetadata,
} from 'ReactFlightDOMRelayServerIntegration';

Expand All @@ -55,30 +55,30 @@ export function isServerReference(reference: Object): boolean {
export type ClientReferenceKey = ClientReference<any>;

export function getClientReferenceKey(
reference: ClientReference<any>,
clientReference: ClientReference<any>,
): ClientReferenceKey {
// We use the reference object itself as the key because we assume the
// object will be cached by the bundler runtime.
return reference;
return clientReference;
}

export function resolveClientReferenceMetadata<T>(
config: ClientManifest,
resource: ClientReference<T>,
clientManifest: ClientManifest,
clientReference: ClientReference<T>,
): ClientReferenceMetadata {
return resolveClientReferenceMetadataImpl(config, resource);
return resolveClientReferenceMetadataImpl(clientManifest, clientReference);
}

export function getServerReferenceId<T>(
config: ClientManifest,
resource: ServerReference<T>,
clientManifest: ClientManifest,
serverReference: ServerReference<T>,
): ServerReferenceId {
throw new Error('Not implemented.');
}

export function getServerReferenceBoundArguments<T>(
config: ClientManifest,
resource: ServerReference<T>,
clientManifest: ClientManifest,
serverReference: ServerReference<T>,
): Array<ReactClientValue> {
throw new Error('Not implemented.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const ReactFlightDOMRelayServerIntegration = {
destination.push(json);
},
close(destination) {},
resolveClientReferenceMetadata(config, resource) {
return resource._moduleId;
resolveClientReferenceMetadata(clientManifest, clientReference) {
return clientReference._moduleId;
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export opaque type ClientReference<T> = {
};

export function resolveClientReference<T>(
bundlerConfig: SSRManifest,
ssrManifest: SSRManifest,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
const resolvedModuleData = bundlerConfig[metadata.id][metadata.name];
const resolvedModuleData = ssrManifest[metadata.id][metadata.name];
return resolvedModuleData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export opaque type ClientReferenceMetadata = {
export opaque type ClientReference<T> = ClientReferenceMetadata;

export function resolveClientReference<T>(
bundlerConfig: SSRManifest,
ssrManifest: SSRManifest,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
if (bundlerConfig) {
const resolvedModuleData = bundlerConfig[metadata.id][metadata.name];
if (ssrManifest) {
const resolvedModuleData = ssrManifest[metadata.id][metadata.name];
if (metadata.async) {
return {
id: resolvedModuleData.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ function noServerCall() {
}

export type Options = {
moduleMap?: SSRManifest,
ssrManifest?: SSRManifest,
};

function createResponseFromOptions(options: void | Options) {
return createResponse(
options && options.moduleMap ? options.moduleMap : null,
options && options.ssrManifest ? options.ssrManifest : null,
noServerCall,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ function noServerCall() {

function createFromNodeStream<T>(
stream: Readable,
moduleMap: $NonMaybeType<SSRManifest>,
ssrManifest: $NonMaybeType<SSRManifest>,
): Thenable<T> {
const response: Response = createResponse(moduleMap, noServerCall);
const response: Response = createResponse(ssrManifest, noServerCall);
stream.on('data', chunk => {
if (typeof chunk === 'string') {
processStringChunk(response, chunk, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type Options = {

function renderToReadableStream(
model: ReactClientValue,
webpackMap: ClientManifest,
clientManifest: ClientManifest,
options?: Options,
): ReadableStream {
const request = createRequest(
model,
webpackMap,
clientManifest,
options ? options.onError : undefined,
options ? options.context : undefined,
options ? options.identifierPrefix : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ type Options = {

function renderToReadableStream(
model: ReactClientValue,
webpackMap: ClientManifest,
clientManifest: ClientManifest,
options?: Options,
): ReadableStream {
const request = createRequest(
model,
webpackMap,
clientManifest,
options ? options.onError : undefined,
options ? options.context : undefined,
options ? options.identifierPrefix : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ type PipeableStream = {

function renderToPipeableStream(
model: ReactClientValue,
webpackMap: ClientManifest,
clientManifest: ClientManifest,
options?: Options,
): PipeableStream {
const request = createRequest(
model,
webpackMap,
clientManifest,
options ? options.onError : undefined,
options ? options.context : undefined,
options ? options.identifierPrefix : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ const CLIENT_REFERENCE_TAG = Symbol.for('react.client.reference');
const SERVER_REFERENCE_TAG = Symbol.for('react.server.reference');

export function getClientReferenceKey(
reference: ClientReference<any>,
clientReference: ClientReference<any>,
): ClientReferenceKey {
return reference.$$async ? reference.$$id + '#async' : reference.$$id;
return clientReference.$$async
? clientReference.$$id + '#async'
: clientReference.$$id;
}

export function isClientReference(reference: Object): boolean {
Expand All @@ -55,10 +57,10 @@ export function isServerReference(reference: Object): boolean {
}

export function resolveClientReferenceMetadata<T>(
config: ClientManifest,
clientManifest: ClientManifest,
clientReference: ClientReference<T>,
): ClientReferenceMetadata {
const resolvedModuleData = config[clientReference.$$id];
const resolvedModuleData = clientManifest[clientReference.$$id];
if (clientReference.$$async) {
return {
id: resolvedModuleData.id,
Expand All @@ -72,14 +74,14 @@ export function resolveClientReferenceMetadata<T>(
}

export function getServerReferenceId<T>(
config: ClientManifest,
clientManifest: ClientManifest,
serverReference: ServerReference<T>,
): ServerReferenceId {
return serverReference.$$id;
}

export function getServerReferenceBoundArguments<T>(
config: ClientManifest,
clientManifest: ClientManifest,
serverReference: ServerReference<T>,
): null | Array<ReactClientValue> {
return serverReference.$$bound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default class ReactFlightWebpackPlugin {
if (clientFileNameFound === false) {
compilation.warnings.push(
new WebpackError(
`Client runtime at ${clientImportName} was not found. React Server Components module map file ${_this.clientManifestFilename} was not created.`,
`Client runtime at ${clientImportName} was not found. React Server Components client manifest file ${_this.clientManifestFilename} was not created.`,
),
);
return;
Expand Down
Loading

0 comments on commit 5392e75

Please sign in to comment.