Skip to content

Commit 1285d5c

Browse files
committed
Make byteLengthOfChunk Optional
This lets a server that can't encode strings but just pass them along able to emit RSC - albeit a less optimal format. The only build we have that does that today is react-html but the FB version of Flight had a similar constraint. It's still possible to support binary data as long as byteLengthOfBinaryChunk is implemented which doesn't require a text encoder.
1 parent 67bab16 commit 1285d5c

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Diff for: packages/react-dom-bindings/src/server/ReactDOMLegacyServerStreamConfig.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ export function typedArrayToBinaryChunk(
6666
throw new Error('Not implemented.');
6767
}
6868

69-
export function byteLengthOfChunk(chunk: Chunk | PrecomputedChunk): number {
70-
throw new Error('Not implemented.');
71-
}
69+
export const byteLengthOfChunk:
70+
| null
71+
| ((chunk: Chunk | PrecomputedChunk) => number) = null;
7272

7373
export function byteLengthOfBinaryChunk(chunk: BinaryChunk): number {
7474
throw new Error('Not implemented.');

Diff for: packages/react-server/src/ReactFlightServer.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -2541,7 +2541,7 @@ function renderModelDestructive(
25412541
return serializeDateFromDateJSON(value);
25422542
}
25432543
}
2544-
if (value.length >= 1024) {
2544+
if (value.length >= 1024 && byteLengthOfChunk !== null) {
25452545
// For large strings, we encode them outside the JSON payload so that we
25462546
// don't have to double encode and double parse the strings. This can also
25472547
// be more compact in case the string has a lot of escaped characters.
@@ -2892,6 +2892,12 @@ function emitTypedArrayChunk(
28922892
}
28932893

28942894
function emitTextChunk(request: Request, id: number, text: string): void {
2895+
if (byteLengthOfChunk === null) {
2896+
// eslint-disable-next-line react-internal/prod-error-codes
2897+
throw new Error(
2898+
'Existence of byteLengthOfChunk should have already been checked. This is a bug in React.',
2899+
);
2900+
}
28952901
request.pendingChunks++; // Extra chunk for the header.
28962902
const textChunk = stringToChunk(text);
28972903
const binaryLength = byteLengthOfChunk(textChunk);
@@ -3289,7 +3295,7 @@ function emitChunk(
32893295
const id = task.id;
32903296
// For certain types we have special types, we typically outlined them but
32913297
// we can emit them directly for this row instead of through an indirection.
3292-
if (typeof value === 'string') {
3298+
if (typeof value === 'string' && byteLengthOfChunk !== null) {
32933299
if (enableTaint) {
32943300
const tainted = TaintRegistryValues.get(value);
32953301
if (tainted !== undefined) {

0 commit comments

Comments
 (0)