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 {Thenable} from 'shared/ReactTypes.js';
    
  11. 
    
  12. import type {Response} from 'react-client/src/ReactFlightClient';
    
  13. 
    
  14. import type {Readable} from 'stream';
    
  15. 
    
  16. import {
    
  17.   createResponse,
    
  18.   getRoot,
    
  19.   reportGlobalError,
    
  20.   processBinaryChunk,
    
  21.   close,
    
  22. } from 'react-client/src/ReactFlightClient';
    
  23. 
    
  24. import {createServerReference as createServerReferenceImpl} from 'react-client/src/ReactFlightReplyClient';
    
  25. 
    
  26. function noServerCall() {
    
  27.   throw new Error(
    
  28.     'Server Functions cannot be called during initial render. ' +
    
  29.       'This would create a fetch waterfall. Try to use a Server Component ' +
    
  30.       'to pass data to Client Components instead.',
    
  31.   );
    
  32. }
    
  33. 
    
  34. export function createServerReference<A: Iterable<any>, T>(
    
  35.   id: any,
    
  36.   callServer: any,
    
  37. ): (...A) => Promise<T> {
    
  38.   return createServerReferenceImpl(id, noServerCall);
    
  39. }
    
  40. 
    
  41. export type Options = {
    
  42.   nonce?: string,
    
  43. };
    
  44. 
    
  45. function createFromNodeStream<T>(
    
  46.   stream: Readable,
    
  47.   moduleRootPath: string,
    
  48.   moduleBaseURL: string,
    
  49.   options?: Options,
    
  50. ): Thenable<T> {
    
  51.   const response: Response = createResponse(
    
  52.     moduleRootPath,
    
  53.     moduleBaseURL,
    
  54.     noServerCall,
    
  55.     options && typeof options.nonce === 'string' ? options.nonce : undefined,
    
  56.   );
    
  57.   stream.on('data', chunk => {
    
  58.     processBinaryChunk(response, chunk);
    
  59.   });
    
  60.   stream.on('error', error => {
    
  61.     reportGlobalError(response, error);
    
  62.   });
    
  63.   stream.on('end', () => close(response));
    
  64.   return getRoot(response);
    
  65. }
    
  66. 
    
  67. export {createFromNodeStream};