mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Follow up to #26827. These can't include binary data and we don't really have any use cases that really require these to already be strings. When the stream is encoded inside another protocol - such as HTML we need a different format that encode binary offsets and binary data.
56 lines
1.4 KiB
JavaScript
56 lines
1.4 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
|
|
*/
|
|
|
|
import type {Thenable} from 'shared/ReactTypes.js';
|
|
|
|
import type {Response} from 'react-client/src/ReactFlightClient';
|
|
|
|
import type {Readable} from 'stream';
|
|
|
|
import {
|
|
createResponse,
|
|
getRoot,
|
|
reportGlobalError,
|
|
processBinaryChunk,
|
|
close,
|
|
} from 'react-client/src/ReactFlightClient';
|
|
|
|
function noServerCall() {
|
|
throw new Error(
|
|
'Server Functions cannot be called during initial render. ' +
|
|
'This would create a fetch waterfall. Try to use a Server Component ' +
|
|
'to pass data to Client Components instead.',
|
|
);
|
|
}
|
|
|
|
export function createServerReference<A: Iterable<any>, T>(
|
|
id: any,
|
|
callServer: any,
|
|
): (...A) => Promise<T> {
|
|
return noServerCall;
|
|
}
|
|
|
|
function createFromNodeStream<T>(
|
|
stream: Readable,
|
|
moduleRootPath: string,
|
|
moduleBaseURL: string, // TODO: Used for preloading hints
|
|
): Thenable<T> {
|
|
const response: Response = createResponse(moduleRootPath, noServerCall);
|
|
stream.on('data', chunk => {
|
|
processBinaryChunk(response, chunk);
|
|
});
|
|
stream.on('error', error => {
|
|
reportGlobalError(response, error);
|
|
});
|
|
stream.on('end', () => close(response));
|
|
return getRoot(response);
|
|
}
|
|
|
|
export {createFromNodeStream};
|