mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
This uses the same mechanism as [large strings](https://github.com/facebook/react/pull/26932) to encode chunks of length based binary data in the RSC payload behind a flag. I introduce a new BinaryChunk type that's specific to each stream and ways to convert into it. That's because we sometimes need all chunks to be Uint8Array for the output, even if the source is another array buffer view, and sometimes we need to clone it before transferring. Each type of typed array is its own row tag. This lets us ensure that the instance is directly in the right format in the cached entry instead of creating a wrapper at each reference. Ideally this is also how Map/Set should work but those are lazy which complicates that approach a bit. We assume both server and client use little-endian for now. If we want to support other modes, we'd convert it to/from little-endian so that the transfer protocol is always little-endian. That way the common clients can be the fastest possible. So far this only implements Server to Client. Still need to implement Client to Server for parity. NOTE: This is the first time we make RSC effectively a binary format. This is not compatible with existing SSR techniques which serialize the stream as unicode in the HTML. To be compatible, those implementations would have to use base64 or something like that. Which is what we'll do when we move this technique to be built-in to Fizz.
86 lines
2.1 KiB
JavaScript
86 lines
2.1 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
|
|
*/
|
|
|
|
export type Destination = {
|
|
buffer: string,
|
|
done: boolean,
|
|
fatal: boolean,
|
|
error: mixed,
|
|
};
|
|
|
|
export opaque type PrecomputedChunk = string;
|
|
export opaque type Chunk = string;
|
|
export opaque type BinaryChunk = string;
|
|
|
|
export function scheduleWork(callback: () => void) {
|
|
// We don't schedule work in this model, and instead expect performWork to always be called repeatedly.
|
|
}
|
|
|
|
export function flushBuffered(destination: Destination) {}
|
|
|
|
export const supportsRequestStorage = false;
|
|
export const requestStorage: AsyncLocalStorage<Request> = (null: any);
|
|
|
|
export function beginWriting(destination: Destination) {}
|
|
|
|
export function writeChunk(
|
|
destination: Destination,
|
|
chunk: Chunk | PrecomputedChunk | BinaryChunk,
|
|
): void {
|
|
destination.buffer += chunk;
|
|
}
|
|
|
|
export function writeChunkAndReturn(
|
|
destination: Destination,
|
|
chunk: Chunk | PrecomputedChunk | BinaryChunk,
|
|
): boolean {
|
|
destination.buffer += chunk;
|
|
return true;
|
|
}
|
|
|
|
export function completeWriting(destination: Destination) {}
|
|
|
|
export function close(destination: Destination) {
|
|
destination.done = true;
|
|
}
|
|
|
|
export function stringToChunk(content: string): Chunk {
|
|
return content;
|
|
}
|
|
|
|
export function stringToPrecomputedChunk(content: string): PrecomputedChunk {
|
|
return content;
|
|
}
|
|
|
|
export function typedArrayToBinaryChunk(
|
|
content: $ArrayBufferView,
|
|
): BinaryChunk {
|
|
throw new Error('Not implemented.');
|
|
}
|
|
|
|
export function clonePrecomputedChunk(
|
|
chunk: PrecomputedChunk,
|
|
): PrecomputedChunk {
|
|
return chunk;
|
|
}
|
|
|
|
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
|
|
throw new Error('Not implemented.');
|
|
}
|
|
|
|
export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
|
|
throw new Error('Not implemented.');
|
|
}
|
|
|
|
export function closeWithError(destination: Destination, error: mixed): void {
|
|
destination.done = true;
|
|
destination.fatal = true;
|
|
destination.error = error;
|
|
}
|