Files
react/packages/react-server/src/ReactFlightServerConfigStream.js
Sebastian Markbåge 76a6dbcb9a [Flight] Encode Symbols as special rows that can be referenced by models … (#20171)
* Encode Symbols as special rows that can be referenced by models

If a symbol was extracted from Symbol.for(...) then we can reliably
recreate the same symbol on the client.

S123:"react.suspense"
M456:{mySymbol: '$123'}

This doesn't suffer from the XSS problem because you have to write actual
code to create one of these symbols. That problem is only a problem because
values pass through common other usages of JSON which are not secure.

Since React encodes its built-ins as symbols, we can now use them as long
as its props are serializable. Like Suspense.

* Refactor resolution to avoid memo hack

Going through createElement isn't quite equivalent for ref and key in props.

* Reuse symbol ids that have already been written earlier in the stream
2020-11-10 19:56:50 -08:00

130 lines
2.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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
- RowSequence
RowSequence
- Row RowSequence
- Row
Row
- "J" RowID JSONData
- "M" RowID JSONModuleData
- "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.
import type {Request, ReactModel} from 'react-server/src/ReactFlightServer';
import {convertStringToBuffer} from './ReactServerStreamConfig';
export type {Destination} from './ReactServerStreamConfig';
export type Chunk = Uint8Array;
const stringify = JSON.stringify;
function serializeRowHeader(tag: string, id: number) {
return tag + id.toString(16) + ':';
}
export function processErrorChunk(
request: Request,
id: number,
message: string,
stack: string,
): Chunk {
const errorInfo = {message, stack};
const row = serializeRowHeader('E', id) + stringify(errorInfo) + '\n';
return convertStringToBuffer(row);
}
export function processModelChunk(
request: Request,
id: number,
model: ReactModel,
): Chunk {
const json = stringify(model, request.toJSON);
const row = serializeRowHeader('J', id) + json + '\n';
return convertStringToBuffer(row);
}
export function processModuleChunk(
request: Request,
id: number,
moduleMetaData: ReactModel,
): Chunk {
const json = stringify(moduleMetaData);
const row = serializeRowHeader('M', id) + json + '\n';
return convertStringToBuffer(row);
}
export function processSymbolChunk(
request: Request,
id: number,
name: string,
): Chunk {
const json = stringify(name);
const row = serializeRowHeader('S', id) + json + '\n';
return convertStringToBuffer(row);
}
export {
scheduleWork,
flushBuffered,
beginWriting,
writeChunk,
completeWriting,
close,
} from './ReactServerStreamConfig';