Skip to content

Commit

Permalink
using module/path#export syntax for the reference IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
nksaraf committed Jun 11, 2023
1 parent 6a226c9 commit d5a7af9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
6 changes: 4 additions & 2 deletions fixtures/flight-vite/server/region.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ async function createApp() {

loadModule = async entry => {
return await viteServer.ssrLoadModule(
path.resolve(viteServer.config.root, entry)
path.isAbsolute(entry)
? entry
: path.join(viteServer.config.root, entry)
);
};
} else {
Expand Down Expand Up @@ -124,7 +126,7 @@ async function createApp() {
const serverReference = req.get('rsc-action');
if (serverReference) {
// This is the client-side case
const [filepath, name] = JSON.parse(serverReference);
const [filepath, name] = serverReference.split('#');
const action = (await loadModule(filepath))[name];
// Validate that this is actually a function we intended to expose and
// not the client trying to invoke arbitrary functions. In a real app,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/flight-vite/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function callServer(id, args) {
method: 'POST',
headers: {
Accept: 'text/x-component',
'rsc-action': JSON.stringify(id),
'rsc-action': id,
},
body: await encodeReply(args),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,9 @@ export type SSRManifest = string; // Module root path

export type ServerManifest = string; // Module root path

export type ServerReferenceId =
| [
string, // module path
string, // export name
]
| string;
export type ServerReferenceId = string;

export opaque type ClientReferenceMetadata = [
string, // module path
string, // export name
];
export opaque type ClientReferenceMetadata = string;

// eslint-disable-next-line no-unused-vars
export opaque type ClientReference<T> = {
Expand All @@ -39,26 +31,21 @@ export function resolveClientReference<T>(
bundlerConfig: SSRManifest,
metadata: ClientReferenceMetadata,
): ClientReference<T> {
const [specifier, name] = metadata.split('#');
return {
specifier: metadata[0],
name: metadata[1],
specifier,
name,
};
}

export function resolveServerReference<T>(
config: ServerManifest,
id: ServerReferenceId,
): ClientReference<T> {
if (typeof id === 'string') {
const [specifier, name] = id.split(',');
return {
specifier,
name,
};
}
const [specifier, name] = id.split('#');
return {
specifier: id[0],
name: id[1],
specifier,
name,
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react-server-dom-vite/src/ReactFlightViteRuntime.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function createClientReference(
const clientReference = Object.defineProperties(({}: any), {
$$typeof: {value: CLIENT_REFERENCE},
// Represents the whole Module object instead of a particular import.
$$id: {value: [moduleId, name], configurable: true},
$$id: {value: `${moduleId}#${name}`},
$$async: {value: false},
});
return new Proxy(clientReference, proxyHandlers);
Expand All @@ -211,7 +211,7 @@ export function createServerReference(
const serverReference = Object.defineProperties(fn, {
$$typeof: {value: SERVER_REFERENCE},
// Represents the whole Module object instead of a particular import.
$$id: {value: [moduleId, name], configurable: true},
$$id: {value: `${moduleId}#${name}`},
$$bound: {value: null},
});
return serverReference;
Expand Down

0 comments on commit d5a7af9

Please sign in to comment.