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 {isClientReference, isServerReference} from './ReactFlightESMReferences';
    
  31. 
    
  32. export function getClientReferenceKey(
    
  33.   reference: ClientReference<any>,
    
  34. ): ClientReferenceKey {
    
  35.   return reference.$$id;
    
  36. }
    
  37. 
    
  38. export function resolveClientReferenceMetadata<T>(
    
  39.   config: ClientManifest,
    
  40.   clientReference: ClientReference<T>,
    
  41. ): ClientReferenceMetadata {
    
  42.   const baseURL: string = config;
    
  43.   const id = clientReference.$$id;
    
  44.   const idx = id.lastIndexOf('#');
    
  45.   const exportName = id.slice(idx + 1);
    
  46.   const fullURL = id.slice(0, idx);
    
  47.   if (!fullURL.startsWith(baseURL)) {
    
  48.     throw new Error(
    
  49.       'Attempted to load a Client Module outside the hosted root.',
    
  50.     );
    
  51.   }
    
  52.   // Relative URL
    
  53.   const modulePath = fullURL.slice(baseURL.length);
    
  54.   return [modulePath, exportName];
    
  55. }
    
  56. 
    
  57. export function getServerReferenceId<T>(
    
  58.   config: ClientManifest,
    
  59.   serverReference: ServerReference<T>,
    
  60. ): ServerReferenceId {
    
  61.   return serverReference.$$id;
    
  62. }
    
  63. 
    
  64. export function getServerReferenceBoundArguments<T>(
    
  65.   config: ClientManifest,
    
  66.   serverReference: ServerReference<T>,
    
  67. ): null | Array<ReactClientValue> {
    
  68.   return serverReference.$$bound;
    
  69. }