mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
eb7f8b42c9
This lets us pass a writable on the server side and readable on the client side to send debug info through a separate channel so that it doesn't interfere with the main payload as much. The main payload refers to chunks defined in the debug info which means it's still blocked on it though. This ensures that the debug data has loaded by the time the value is rendered so that the next step can forward the data. This will be a bit fragile to race conditions until #33665 lands. Another follow up needed is the ability to skip the debug channel on the receiving side. Right now it'll block forever if you don't provide one since we're blocking on the debug data.
101 lines
2.7 KiB
JavaScript
101 lines
2.7 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, ReactCustomFormAction} from 'shared/ReactTypes.js';
|
|
|
|
import type {
|
|
Response,
|
|
FindSourceMapURLCallback,
|
|
} from 'react-client/src/ReactFlightClient';
|
|
|
|
import type {
|
|
ServerConsumerModuleMap,
|
|
ModuleLoading,
|
|
ServerManifest,
|
|
} from 'react-client/src/ReactFlightClientConfig';
|
|
|
|
type ServerConsumerManifest = {
|
|
moduleMap: ServerConsumerModuleMap,
|
|
moduleLoading: ModuleLoading,
|
|
serverModuleMap: null | ServerManifest,
|
|
};
|
|
|
|
import type {Readable} from 'stream';
|
|
|
|
import {
|
|
createResponse,
|
|
createStreamState,
|
|
getRoot,
|
|
reportGlobalError,
|
|
processStringChunk,
|
|
processBinaryChunk,
|
|
close,
|
|
} from 'react-client/src/ReactFlightClient';
|
|
|
|
export * from './ReactFlightDOMClientEdge';
|
|
|
|
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.',
|
|
);
|
|
}
|
|
|
|
type EncodeFormActionCallback = <A>(
|
|
id: any,
|
|
args: Promise<A>,
|
|
) => ReactCustomFormAction;
|
|
|
|
export type Options = {
|
|
nonce?: string,
|
|
encodeFormAction?: EncodeFormActionCallback,
|
|
findSourceMapURL?: FindSourceMapURLCallback,
|
|
replayConsoleLogs?: boolean,
|
|
environmentName?: string,
|
|
};
|
|
|
|
function createFromNodeStream<T>(
|
|
stream: Readable,
|
|
serverConsumerManifest: ServerConsumerManifest,
|
|
options?: Options,
|
|
): Thenable<T> {
|
|
const response: Response = createResponse(
|
|
serverConsumerManifest.moduleMap,
|
|
serverConsumerManifest.serverModuleMap,
|
|
serverConsumerManifest.moduleLoading,
|
|
noServerCall,
|
|
options ? options.encodeFormAction : undefined,
|
|
options && typeof options.nonce === 'string' ? options.nonce : undefined,
|
|
undefined, // TODO: If encodeReply is supported, this should support temporaryReferences
|
|
__DEV__ && options && options.findSourceMapURL
|
|
? options.findSourceMapURL
|
|
: undefined,
|
|
__DEV__ && options ? options.replayConsoleLogs === true : false, // defaults to false
|
|
__DEV__ && options && options.environmentName
|
|
? options.environmentName
|
|
: undefined,
|
|
);
|
|
const streamState = createStreamState();
|
|
stream.on('data', chunk => {
|
|
if (typeof chunk === 'string') {
|
|
processStringChunk(response, streamState, chunk);
|
|
} else {
|
|
processBinaryChunk(response, streamState, chunk);
|
|
}
|
|
});
|
|
stream.on('error', error => {
|
|
reportGlobalError(response, error);
|
|
});
|
|
stream.on('end', () => close(response));
|
|
return getRoot(response);
|
|
}
|
|
|
|
export {createFromNodeStream};
|