mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
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.
103 lines
3.0 KiB
JavaScript
103 lines
3.0 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
type JSONValue =
|
|
| string
|
|
| boolean
|
|
| number
|
|
| null
|
|
| {+[key: string]: JSONValue}
|
|
| $ReadOnlyArray<JSONValue>;
|
|
|
|
declare module 'JSResourceReference' {
|
|
declare export interface JSResourceReference<T> {
|
|
getModuleId(): string;
|
|
getModuleIdAsRef(): $Flow$ModuleRef<T>;
|
|
getModuleIfRequired(): ?T;
|
|
load(): Promise<T>;
|
|
preload(): void;
|
|
}
|
|
}
|
|
|
|
declare module 'JSResourceReferenceImpl' {
|
|
declare export default class JSResourceReferenceImpl<T> {
|
|
getModuleId(): string;
|
|
getModuleIdAsRef(): $Flow$ModuleRef<T>;
|
|
getModuleIfRequired(): ?T;
|
|
load(): Promise<T>;
|
|
preload(): void;
|
|
}
|
|
}
|
|
|
|
declare module 'ReactFlightDOMRelayServerIntegration' {
|
|
import type {JSResourceReference} from 'JSResourceReference';
|
|
|
|
declare export opaque type Destination;
|
|
declare export opaque type BundlerConfig;
|
|
declare export function emitRow(
|
|
destination: Destination,
|
|
json: JSONValue,
|
|
): void;
|
|
declare export function close(destination: Destination): void;
|
|
|
|
declare export type ClientReferenceMetadata = JSONValue;
|
|
declare export function resolveClientReferenceMetadata<T>(
|
|
config: BundlerConfig,
|
|
resourceReference: JSResourceReference<T>,
|
|
): ClientReferenceMetadata;
|
|
}
|
|
|
|
declare module 'ReactFlightDOMRelayClientIntegration' {
|
|
import type {JSResourceReference} from 'JSResourceReference';
|
|
|
|
declare export opaque type ClientReferenceMetadata;
|
|
declare export function resolveClientReference<T>(
|
|
moduleData: ClientReferenceMetadata,
|
|
): JSResourceReference<T>;
|
|
declare export function preloadModule<T>(
|
|
moduleReference: JSResourceReference<T>,
|
|
): null | Promise<void>;
|
|
declare export function requireModule<T>(
|
|
moduleReference: JSResourceReference<T>,
|
|
): T;
|
|
}
|
|
|
|
declare module 'ReactFlightNativeRelayServerIntegration' {
|
|
import type {JSResourceReference} from 'JSResourceReference';
|
|
|
|
declare export opaque type Destination;
|
|
declare export opaque type BundlerConfig;
|
|
declare export function emitRow(
|
|
destination: Destination,
|
|
json: JSONValue,
|
|
): void;
|
|
declare export function close(destination: Destination): void;
|
|
|
|
declare export type ClientReferenceMetadata = JSONValue;
|
|
declare export function resolveClientReferenceMetadata<T>(
|
|
config: BundlerConfig,
|
|
resourceReference: JSResourceReference<T>,
|
|
): ClientReferenceMetadata;
|
|
}
|
|
|
|
declare module 'ReactFlightNativeRelayClientIntegration' {
|
|
import type {JSResourceReference} from 'JSResourceReference';
|
|
|
|
declare export opaque type ClientReferenceMetadata;
|
|
declare export function resolveClientReference<T>(
|
|
moduleData: ClientReferenceMetadata,
|
|
): JSResourceReference<T>;
|
|
declare export function preloadModule<T>(
|
|
moduleReference: JSResourceReference<T>,
|
|
): null | Promise<void>;
|
|
declare export function requireModule<T>(
|
|
moduleReference: JSResourceReference<T>,
|
|
): T;
|
|
}
|