Enable passing Server References from Server to Client (#26124)

This is the first of a series of PRs, that let you pass functions, by
reference, to the client and back. E.g. through Server Context. It's
like client references but they're opaque on the client and resolved on
the server.

To do this, for security, you must opt-in to exposing these functions to
the client using the `"use server"` directive. The `"use client"`
directive lets you enter the client from the server. The `"use server"`
directive lets you enter the server from the client.

This works by tagging those functions as Server References. We could
potentially expand this to other non-serializable or stateful objects
too like classes.

This only implements server->server CJS imports and server->server ESM
imports. We really should add a loader to the webpack plug-in for
client->server imports too. I'll leave closures as an exercise for
integrators.

You can't "call" a client reference on the server, however, you can
"call" a server reference on the client. This invokes a callback on the
Flight client options called `callServer`. This lets a router implement
calling back to the server. Effectively creating an RPC. This is using
JSON for serializing those arguments but more utils coming from
client->server serialization.
This commit is contained in:
Sebastian Markbåge
2023-02-09 19:45:05 -05:00
committed by GitHub
parent 6c75d4e009
commit ef9f6e77b8
38 changed files with 844 additions and 219 deletions
+3 -3
View File
@@ -141,12 +141,12 @@ export function processReferenceChunk(
return stringToChunk(row);
}
export function processModuleChunk(
export function processImportChunk(
request: Request,
id: number,
moduleMetaData: ReactModel,
clientReferenceMetadata: ReactModel,
): Chunk {
const json: string = stringify(moduleMetaData);
const json: string = stringify(clientReferenceMetadata);
const row = serializeRowHeader('I', id) + json + '\n';
return stringToChunk(row);
}