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 17, 2023
1 parent 8fa41ff commit 10ce129
Show file tree
Hide file tree
Showing 30 changed files with 149 additions and 147 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 @@ -52,11 +52,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 @@ -65,8 +65,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 @@ -91,7 +91,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 @@ -151,7 +151,7 @@ Chunk.prototype.then = function <T>(
};

export type ResponseBase = {
_bundlerConfig: SSRManifest,
_ssrManifest: SSRManifest,
_callServer: CallServerCallback,
_chunks: Map<number, SomeChunk<any>>,
...
Expand Down Expand Up @@ -619,12 +619,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 @@ -657,7 +657,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 @@ -40,14 +40,14 @@ export type UninitializedModel = JSONValue;
export type Response = ResponseBase;

export function resolveClientReference<T>(
bundlerConfig: SSRManifest,
ssrManifest: SSRManifest,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
return resolveClientReferenceImpl(metadata);
}

export function resolveServerReference<T>(
bundlerConfig: ServerManifest,
serverManifest: ServerManifest,
id: ServerReferenceId,
): ClientReference<T> {
throw new Error('Not implemented.');
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 @@ -36,15 +36,15 @@ 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;
}

export function resolveServerReference<T>(
bundlerConfig: ServerManifest,
serverManifest: ServerManifest,
id: ServerReferenceId,
): ClientReference<T> {
const idx = id.lastIndexOf('#');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,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 All @@ -56,11 +56,11 @@ export function resolveClientReference<T>(
}

export function resolveServerReference<T>(
bundlerConfig: ServerManifest,
serverManifest: ServerManifest,
id: ServerReferenceId,
): ClientReference<T> {
// This needs to return async: true if it's an async module.
return bundlerConfig[id];
return serverManifest[id];
}

// The chunk cache contains all the chunks we've preloaded so far.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ function noServerCall() {
}

export type Options = {
moduleMap?: $NonMaybeType<SSRManifest>,
ssrManifest?: $NonMaybeType<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 @@ -36,12 +36,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 Expand Up @@ -77,9 +77,9 @@ function renderToReadableStream(

function decodeReply<T>(
body: string | FormData,
webpackMap: ServerManifest,
serverManifest: ServerManifest,
): Thenable<T> {
const response = createResponse(webpackMap);
const response = createResponse(serverManifest);
if (typeof body === 'string') {
resolveField(response, 0, body);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,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 Expand Up @@ -77,9 +77,9 @@ function renderToReadableStream(

function decodeReply<T>(
body: string | FormData,
webpackMap: ServerManifest,
serverManifest: ServerManifest,
): Thenable<T> {
const response = createResponse(webpackMap);
const response = createResponse(serverManifest);
if (typeof body === 'string') {
resolveField(response, 0, body);
} else {
Expand Down
Loading

0 comments on commit 10ce129

Please sign in to comment.