77 * @flow
88 */
99
10- import type { Destination } from 'react-server/src/ReactServerStreamConfig' ;
10+ import type {
11+ Destination ,
12+ Chunk ,
13+ PrecomputedChunk ,
14+ } from 'react-server/src/ReactServerStreamConfig' ;
1115
1216import {
1317 writeChunk ,
14- convertStringToBuffer ,
18+ stringToChunk ,
19+ stringToPrecomputedChunk ,
1520} from 'react-server/src/ReactServerStreamConfig' ;
1621
1722import escapeTextForBrowser from './escapeTextForBrowser' ;
@@ -55,43 +60,43 @@ function encodeHTMLTextNode(text: string): string {
5560}
5661
5762export function pushTextInstance (
58- target : Array < Uint8Array > ,
63+ target : Array < Chunk | PrecomputedChunk > ,
5964 text : string ,
6065) : void {
61- target . push ( convertStringToBuffer ( encodeHTMLTextNode ( text ) ) ) ;
66+ target . push ( stringToChunk ( encodeHTMLTextNode ( text ) ) ) ;
6267}
6368
64- const startTag1 = convertStringToBuffer ( '<' ) ;
65- const startTag2 = convertStringToBuffer ( '>' ) ;
69+ const startTag1 = stringToPrecomputedChunk ( '<' ) ;
70+ const startTag2 = stringToPrecomputedChunk ( '>' ) ;
6671
6772export function pushStartInstance (
68- target : Array < Uint8Array > ,
73+ target : Array < Chunk | PrecomputedChunk > ,
6974 type : string ,
7075 props : Object ,
7176) : void {
7277 // TODO: Figure out if it's self closing and everything else.
73- target . push ( startTag1 , convertStringToBuffer ( type ) , startTag2 ) ;
78+ target . push ( startTag1 , stringToChunk ( type ) , startTag2 ) ;
7479}
7580
76- const endTag1 = convertStringToBuffer ( '</' ) ;
77- const endTag2 = convertStringToBuffer ( '>' ) ;
81+ const endTag1 = stringToPrecomputedChunk ( '</' ) ;
82+ const endTag2 = stringToPrecomputedChunk ( '>' ) ;
7883
7984export function pushEndInstance (
80- target : Array < Uint8Array > ,
85+ target : Array < Chunk | PrecomputedChunk > ,
8186 type : string ,
8287 props : Object ,
8388) : void {
8489 // TODO: Figure out if it was self closing.
85- target . push ( endTag1 , convertStringToBuffer ( type ) , endTag2 ) ;
90+ target . push ( endTag1 , stringToChunk ( type ) , endTag2 ) ;
8691}
8792
8893// Structural Nodes
8994
9095// A placeholder is a node inside a hidden partial tree that can be filled in later, but before
9196// display. It's never visible to users.
92- const placeholder1 = convertStringToBuffer ( '<span id="' ) ;
93- const placeholder2 = convertStringToBuffer ( 'P:' ) ;
94- const placeholder3 = convertStringToBuffer ( '"></span>' ) ;
97+ const placeholder1 = stringToPrecomputedChunk ( '<span id="' ) ;
98+ const placeholder2 = stringToPrecomputedChunk ( 'P:' ) ;
99+ const placeholder3 = stringToPrecomputedChunk ( '"></span>' ) ;
95100export function writePlaceholder (
96101 destination : Destination ,
97102 id : number ,
@@ -101,16 +106,18 @@ export function writePlaceholder(
101106 writeChunk ( destination , placeholder1 ) ;
102107 // TODO: Use the identifierPrefix option to make the prefix configurable.
103108 writeChunk ( destination , placeholder2 ) ;
104- const formattedID = convertStringToBuffer ( id . toString ( 16 ) ) ;
109+ const formattedID = stringToChunk ( id . toString ( 16 ) ) ;
105110 writeChunk ( destination , formattedID ) ;
106111 return writeChunk ( destination , placeholder3 ) ;
107112}
108113
109114// Suspense boundaries are encoded as comments.
110- const startCompletedSuspenseBoundary = convertStringToBuffer ( '<!--$-->' ) ;
111- const startPendingSuspenseBoundary = convertStringToBuffer ( '<!--$?-->' ) ;
112- const startClientRenderedSuspenseBoundary = convertStringToBuffer ( '<!--$!-->' ) ;
113- const endSuspenseBoundary = convertStringToBuffer ( '<!--/$-->' ) ;
115+ const startCompletedSuspenseBoundary = stringToPrecomputedChunk ( '<!--$-->' ) ;
116+ const startPendingSuspenseBoundary = stringToPrecomputedChunk ( '<!--$?-->' ) ;
117+ const startClientRenderedSuspenseBoundary = stringToPrecomputedChunk (
118+ '<!--$!-->' ,
119+ ) ;
120+ const endSuspenseBoundary = stringToPrecomputedChunk ( '<!--/$-->' ) ;
114121
115122export function writeStartCompletedSuspenseBoundary (
116123 destination : Destination ,
@@ -134,10 +141,10 @@ export function writeEndSuspenseBoundary(destination: Destination): boolean {
134141 return writeChunk ( destination , endSuspenseBoundary ) ;
135142}
136143
137- const startSegment = convertStringToBuffer ( '<div hidden id="' ) ;
138- const startSegment2 = convertStringToBuffer ( 'S:' ) ;
139- const startSegment3 = convertStringToBuffer ( '">' ) ;
140- const endSegment = convertStringToBuffer ( '"></div>' ) ;
144+ const startSegment = stringToPrecomputedChunk ( '<div hidden id="' ) ;
145+ const startSegment2 = stringToPrecomputedChunk ( 'S:' ) ;
146+ const startSegment3 = stringToPrecomputedChunk ( '">' ) ;
147+ const endSegment = stringToPrecomputedChunk ( '"></div>' ) ;
141148export function writeStartSegment (
142149 destination : Destination ,
143150 id : number ,
@@ -146,7 +153,7 @@ export function writeStartSegment(
146153 writeChunk ( destination , startSegment ) ;
147154 // TODO: Use the identifierPrefix option to make the prefix configurable.
148155 writeChunk ( destination , startSegment2 ) ;
149- const formattedID = convertStringToBuffer ( id . toString ( 16 ) ) ;
156+ const formattedID = stringToChunk ( id . toString ( 16 ) ) ;
150157 writeChunk ( destination , formattedID ) ;
151158 return writeChunk ( destination , startSegment3 ) ;
152159}
@@ -276,12 +283,14 @@ const completeBoundaryFunction =
276283const clientRenderFunction =
277284 'function $RX(b){if(b=document.getElementById(b)){do b=b.previousSibling;while(8!==b.nodeType||"$?"!==b.data);b.data="$!";b._reactRetry&&b._reactRetry()}}' ;
278285
279- const completeSegmentScript1Full = convertStringToBuffer (
286+ const completeSegmentScript1Full = stringToPrecomputedChunk (
280287 '<script>' + completeSegmentFunction + ';$RS("S:' ,
281288) ;
282- const completeSegmentScript1Partial = convertStringToBuffer ( '<script>$RS("S:' ) ;
283- const completeSegmentScript2 = convertStringToBuffer ( '","P:' ) ;
284- const completeSegmentScript3 = convertStringToBuffer ( '")</script>' ) ;
289+ const completeSegmentScript1Partial = stringToPrecomputedChunk (
290+ '<script>$RS("S:' ,
291+ ) ;
292+ const completeSegmentScript2 = stringToPrecomputedChunk ( '","P:' ) ;
293+ const completeSegmentScript3 = stringToPrecomputedChunk ( '")</script>' ) ;
285294
286295export function writeCompletedSegmentInstruction (
287296 destination : Destination ,
@@ -297,19 +306,21 @@ export function writeCompletedSegmentInstruction(
297306 writeChunk ( destination , completeSegmentScript1Partial ) ;
298307 }
299308 // TODO: Use the identifierPrefix option to make the prefix configurable.
300- const formattedID = convertStringToBuffer ( contentSegmentID . toString ( 16 ) ) ;
309+ const formattedID = stringToChunk ( contentSegmentID . toString ( 16 ) ) ;
301310 writeChunk ( destination , formattedID ) ;
302311 writeChunk ( destination , completeSegmentScript2 ) ;
303312 writeChunk ( destination , formattedID ) ;
304313 return writeChunk ( destination , completeSegmentScript3 ) ;
305314}
306315
307- const completeBoundaryScript1Full = convertStringToBuffer (
316+ const completeBoundaryScript1Full = stringToPrecomputedChunk (
308317 '<script>' + completeBoundaryFunction + ';$RC("' ,
309318) ;
310- const completeBoundaryScript1Partial = convertStringToBuffer ( '<script>$RC("' ) ;
311- const completeBoundaryScript2 = convertStringToBuffer ( '","S:' ) ;
312- const completeBoundaryScript3 = convertStringToBuffer ( '")</script>' ) ;
319+ const completeBoundaryScript1Partial = stringToPrecomputedChunk (
320+ '<script>$RC("' ,
321+ ) ;
322+ const completeBoundaryScript2 = stringToPrecomputedChunk ( '","S:' ) ;
323+ const completeBoundaryScript3 = stringToPrecomputedChunk ( '")</script>' ) ;
313324
314325export function writeCompletedBoundaryInstruction (
315326 destination : Destination ,
@@ -330,23 +341,21 @@ export function writeCompletedBoundaryInstruction(
330341 boundaryID . id !== null ,
331342 'An ID must have been assigned before we can complete the boundary.' ,
332343 ) ;
333- const formattedBoundaryID = convertStringToBuffer (
344+ const formattedBoundaryID = stringToChunk (
334345 encodeHTMLIDAttribute ( boundaryID . id ) ,
335346 ) ;
336- const formattedContentID = convertStringToBuffer (
337- contentSegmentID . toString ( 16 ) ,
338- ) ;
347+ const formattedContentID = stringToChunk ( contentSegmentID . toString ( 16 ) ) ;
339348 writeChunk ( destination , formattedBoundaryID ) ;
340349 writeChunk ( destination , completeBoundaryScript2 ) ;
341350 writeChunk ( destination , formattedContentID ) ;
342351 return writeChunk ( destination , completeBoundaryScript3 ) ;
343352}
344353
345- const clientRenderScript1Full = convertStringToBuffer (
354+ const clientRenderScript1Full = stringToPrecomputedChunk (
346355 '<script>' + clientRenderFunction + ';$RX("' ,
347356) ;
348- const clientRenderScript1Partial = convertStringToBuffer ( '<script>$RX("' ) ;
349- const clientRenderScript2 = convertStringToBuffer ( '")</script>' ) ;
357+ const clientRenderScript1Partial = stringToPrecomputedChunk ( '<script>$RX("' ) ;
358+ const clientRenderScript2 = stringToPrecomputedChunk ( '")</script>' ) ;
350359
351360export function writeClientRenderBoundaryInstruction (
352361 destination : Destination ,
@@ -365,7 +374,7 @@ export function writeClientRenderBoundaryInstruction(
365374 boundaryID . id !== null ,
366375 'An ID must have been assigned before we can complete the boundary.' ,
367376 ) ;
368- const formattedBoundaryID = convertStringToBuffer (
377+ const formattedBoundaryID = stringToPrecomputedChunk (
369378 encodeHTMLIDAttribute ( boundaryID . id ) ,
370379 ) ;
371380 writeChunk ( destination , formattedBoundaryID ) ;
0 commit comments