main
js 79 lines 1.93 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import type {ReactClientValue} from 'react-server/src/ReactFlightServer';
11
12 import type {
13 ClientReference,
14 ServerReference,
15 } from '../ReactFlightESMReferences';
16
17 export type {ClientReference, ServerReference};
18
19 export type ClientManifest = string; // base URL on the file system
20
21 export type ServerReferenceId = string;
22
23 export type ClientReferenceMetadata = [
24 string, // module path
25 string, // export name
26 ];
27
28 export type ClientReferenceKey = string;
29
30 export {
31 isClientReference,
32 isServerReference,
33 } from '../ReactFlightESMReferences';
34
35 export function getClientReferenceKey(
36 reference: ClientReference<any>,
37 ): ClientReferenceKey {
38 return reference.$$id;
39 }
40
41 export function resolveClientReferenceMetadata<T>(
42 config: ClientManifest,
43 clientReference: ClientReference<T>,
44 ): ClientReferenceMetadata {
45 const baseURL: string = config;
46 const id = clientReference.$$id;
47 const idx = id.lastIndexOf('#');
48 const exportName = id.slice(idx + 1);
49 const fullURL = id.slice(0, idx);
50 if (!fullURL.startsWith(baseURL)) {
51 throw new Error(
52 'Attempted to load a Client Module outside the hosted root.',
53 );
54 }
55 // Relative URL
56 const modulePath = fullURL.slice(baseURL.length);
57 return [modulePath, exportName];
58 }
59
60 export function getServerReferenceId<T>(
61 config: ClientManifest,
62 serverReference: ServerReference<T>,
63 ): ServerReferenceId {
64 return serverReference.$$id;
65 }
66
67 export function getServerReferenceBoundArguments<T>(
68 config: ClientManifest,
69 serverReference: ServerReference<T>,
70 ): null | Array<ReactClientValue> {
71 return serverReference.$$bound;
72 }
73
74 export function getServerReferenceLocation<T>(
75 config: ClientManifest,
76 serverReference: ServerReference<T>,
77 ): void | Error {
78 return serverReference.$$location;
79 }