2020-03-11 05:55:04 +08:00
|
|
|
/**
|
2022-10-18 23:19:24 +08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2020-03-11 05:55:04 +08:00
|
|
|
*
|
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
|
*
|
|
|
|
|
* @flow
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// This file is an intermediate layer to translate between Flight
|
|
|
|
|
// calls to stream output over a binary stream.
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
FLIGHT PROTOCOL GRAMMAR
|
|
|
|
|
|
|
|
|
|
Response
|
2020-10-30 08:57:31 +08:00
|
|
|
- RowSequence
|
2020-03-11 05:55:04 +08:00
|
|
|
|
|
|
|
|
RowSequence
|
|
|
|
|
- Row RowSequence
|
|
|
|
|
- Row
|
|
|
|
|
|
|
|
|
|
Row
|
|
|
|
|
- "J" RowID JSONData
|
2020-10-30 08:57:31 +08:00
|
|
|
- "M" RowID JSONModuleData
|
2020-03-11 05:55:04 +08:00
|
|
|
- "H" RowID HTMLData
|
|
|
|
|
- "B" RowID BlobData
|
|
|
|
|
- "U" RowID URLData
|
|
|
|
|
- "E" RowID ErrorData
|
|
|
|
|
|
|
|
|
|
RowID
|
|
|
|
|
- HexDigits ":"
|
|
|
|
|
|
|
|
|
|
HexDigits
|
|
|
|
|
- HexDigit HexDigits
|
|
|
|
|
- HexDigit
|
|
|
|
|
|
|
|
|
|
HexDigit
|
|
|
|
|
- 0-F
|
|
|
|
|
|
|
|
|
|
URLData
|
|
|
|
|
- (UTF8 encoded URL) "\n"
|
|
|
|
|
|
|
|
|
|
ErrorData
|
|
|
|
|
- (UTF8 encoded JSON: {message: "...", stack: "..."}) "\n"
|
|
|
|
|
|
|
|
|
|
JSONData
|
|
|
|
|
- (UTF8 encoded JSON) "\n"
|
|
|
|
|
- String values that begin with $ are escaped with a "$" prefix.
|
|
|
|
|
- References to other rows are encoding as JSONReference strings.
|
|
|
|
|
|
|
|
|
|
JSONReference
|
|
|
|
|
- "$" HexDigits
|
|
|
|
|
|
|
|
|
|
HTMLData
|
|
|
|
|
- ByteSize (UTF8 encoded HTML)
|
|
|
|
|
|
|
|
|
|
BlobData
|
|
|
|
|
- ByteSize (Binary Data)
|
|
|
|
|
|
|
|
|
|
ByteSize
|
|
|
|
|
- (unsigned 32-bit integer)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// TODO: Implement HTMLData, BlobData and URLData.
|
|
|
|
|
|
2023-03-09 12:45:55 +08:00
|
|
|
import type {
|
|
|
|
|
Request,
|
|
|
|
|
ReactClientValue,
|
|
|
|
|
} from 'react-server/src/ReactFlightServer';
|
2020-03-11 05:55:04 +08:00
|
|
|
|
2021-03-16 01:36:23 +08:00
|
|
|
import {stringToChunk} from './ReactServerStreamConfig';
|
2020-03-11 05:55:04 +08:00
|
|
|
|
2021-03-16 01:36:23 +08:00
|
|
|
import type {Chunk} from './ReactServerStreamConfig';
|
2020-03-11 05:55:04 +08:00
|
|
|
|
2021-03-16 01:36:23 +08:00
|
|
|
export type {Destination, Chunk} from './ReactServerStreamConfig';
|
2020-03-11 05:55:04 +08:00
|
|
|
|
|
|
|
|
const stringify = JSON.stringify;
|
|
|
|
|
|
|
|
|
|
function serializeRowHeader(tag: string, id: number) {
|
2023-02-01 01:41:36 +08:00
|
|
|
return id.toString(16) + ':' + tag;
|
2020-03-11 05:55:04 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-24 04:19:29 +08:00
|
|
|
export function processErrorChunkProd(
|
2020-03-11 05:55:04 +08:00
|
|
|
request: Request,
|
|
|
|
|
id: number,
|
2022-09-24 04:19:29 +08:00
|
|
|
digest: string,
|
|
|
|
|
): Chunk {
|
|
|
|
|
if (__DEV__) {
|
|
|
|
|
// These errors should never make it into a build so we don't need to encode them in codes.json
|
|
|
|
|
// eslint-disable-next-line react-internal/prod-error-codes
|
|
|
|
|
throw new Error(
|
|
|
|
|
'processErrorChunkProd should never be called while in development mode. Use processErrorChunkDev instead. This is a bug in React.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const errorInfo: any = {digest};
|
|
|
|
|
const row = serializeRowHeader('E', id) + stringify(errorInfo) + '\n';
|
|
|
|
|
return stringToChunk(row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function processErrorChunkDev(
|
|
|
|
|
request: Request,
|
|
|
|
|
id: number,
|
|
|
|
|
digest: string,
|
2020-03-11 05:55:04 +08:00
|
|
|
message: string,
|
|
|
|
|
stack: string,
|
|
|
|
|
): Chunk {
|
2022-09-24 04:19:29 +08:00
|
|
|
if (!__DEV__) {
|
|
|
|
|
// These errors should never make it into a build so we don't need to encode them in codes.json
|
|
|
|
|
// eslint-disable-next-line react-internal/prod-error-codes
|
|
|
|
|
throw new Error(
|
|
|
|
|
'processErrorChunkDev should never be called while in production mode. Use processErrorChunkProd instead. This is a bug in React.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const errorInfo: any = {digest, message, stack};
|
2020-04-02 03:35:52 +08:00
|
|
|
const row = serializeRowHeader('E', id) + stringify(errorInfo) + '\n';
|
2021-03-16 01:36:23 +08:00
|
|
|
return stringToChunk(row);
|
2020-03-11 05:55:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function processModelChunk(
|
|
|
|
|
request: Request,
|
|
|
|
|
id: number,
|
2023-03-09 12:45:55 +08:00
|
|
|
model: ReactClientValue,
|
2020-03-11 05:55:04 +08:00
|
|
|
): Chunk {
|
2023-03-06 23:33:22 +08:00
|
|
|
// $FlowFixMe[incompatible-type] stringify can return null
|
2022-09-08 23:46:07 +08:00
|
|
|
const json: string = stringify(model, request.toJSON);
|
2023-02-01 01:41:36 +08:00
|
|
|
const row = id.toString(16) + ':' + json + '\n';
|
2021-03-16 01:36:23 +08:00
|
|
|
return stringToChunk(row);
|
2020-10-30 08:57:31 +08:00
|
|
|
}
|
|
|
|
|
|
2022-06-19 23:05:41 +08:00
|
|
|
export function processReferenceChunk(
|
|
|
|
|
request: Request,
|
|
|
|
|
id: number,
|
|
|
|
|
reference: string,
|
|
|
|
|
): Chunk {
|
|
|
|
|
const json = stringify(reference);
|
2023-02-01 01:41:36 +08:00
|
|
|
const row = id.toString(16) + ':' + json + '\n';
|
2022-06-19 23:05:41 +08:00
|
|
|
return stringToChunk(row);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 08:45:05 +08:00
|
|
|
export function processImportChunk(
|
2020-10-30 08:57:31 +08:00
|
|
|
request: Request,
|
|
|
|
|
id: number,
|
2023-03-09 12:45:55 +08:00
|
|
|
clientReferenceMetadata: ReactClientValue,
|
2020-10-30 08:57:31 +08:00
|
|
|
): Chunk {
|
2023-03-06 23:33:22 +08:00
|
|
|
// $FlowFixMe[incompatible-type] stringify can return null
|
2023-02-10 08:45:05 +08:00
|
|
|
const json: string = stringify(clientReferenceMetadata);
|
2023-02-01 01:41:36 +08:00
|
|
|
const row = serializeRowHeader('I', id) + json + '\n';
|
2021-03-16 01:36:23 +08:00
|
|
|
return stringToChunk(row);
|
2020-11-11 11:56:50 +08:00
|
|
|
}
|
|
|
|
|
|
2023-04-22 11:45:51 +08:00
|
|
|
export function processHintChunk(
|
|
|
|
|
request: Request,
|
|
|
|
|
id: number,
|
|
|
|
|
code: string,
|
|
|
|
|
model: JSONValue,
|
|
|
|
|
): Chunk {
|
|
|
|
|
const json: string = stringify(model);
|
|
|
|
|
const row = serializeRowHeader('H' + code, id) + json + '\n';
|
|
|
|
|
return stringToChunk(row);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 05:55:04 +08:00
|
|
|
export {
|
|
|
|
|
scheduleWork,
|
|
|
|
|
flushBuffered,
|
|
|
|
|
beginWriting,
|
|
|
|
|
writeChunk,
|
2022-02-24 00:35:21 +08:00
|
|
|
writeChunkAndReturn,
|
2020-03-11 05:55:04 +08:00
|
|
|
completeWriting,
|
|
|
|
|
close,
|
2021-03-30 10:36:16 +08:00
|
|
|
closeWithError,
|
2020-03-11 05:55:04 +08:00
|
|
|
} from './ReactServerStreamConfig';
|