7
7
* @flow
8
8
*/
9
9
10
- import type { Destination } from 'react-server/src/ReactServerStreamConfig' ;
10
+ import type {
11
+ Destination ,
12
+ Chunk ,
13
+ PrecomputedChunk ,
14
+ } from 'react-server/src/ReactServerStreamConfig' ;
11
15
12
16
import {
13
17
writeChunk ,
14
- convertStringToBuffer ,
18
+ stringToChunk ,
19
+ stringToPrecomputedChunk ,
15
20
} from 'react-server/src/ReactServerStreamConfig' ;
16
21
17
22
import escapeTextForBrowser from './escapeTextForBrowser' ;
@@ -55,43 +60,43 @@ function encodeHTMLTextNode(text: string): string {
55
60
}
56
61
57
62
export function pushTextInstance (
58
- target : Array < Uint8Array > ,
63
+ target : Array < Chunk | PrecomputedChunk > ,
59
64
text : string ,
60
65
) : void {
61
- target . push ( convertStringToBuffer ( encodeHTMLTextNode ( text ) ) ) ;
66
+ target . push ( stringToChunk ( encodeHTMLTextNode ( text ) ) ) ;
62
67
}
63
68
64
- const startTag1 = convertStringToBuffer ( '<' ) ;
65
- const startTag2 = convertStringToBuffer ( '>' ) ;
69
+ const startTag1 = stringToPrecomputedChunk ( '<' ) ;
70
+ const startTag2 = stringToPrecomputedChunk ( '>' ) ;
66
71
67
72
export function pushStartInstance (
68
- target : Array < Uint8Array > ,
73
+ target : Array < Chunk | PrecomputedChunk > ,
69
74
type : string ,
70
75
props : Object ,
71
76
) : void {
72
77
// 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 ) ;
74
79
}
75
80
76
- const endTag1 = convertStringToBuffer ( '</' ) ;
77
- const endTag2 = convertStringToBuffer ( '>' ) ;
81
+ const endTag1 = stringToPrecomputedChunk ( '</' ) ;
82
+ const endTag2 = stringToPrecomputedChunk ( '>' ) ;
78
83
79
84
export function pushEndInstance (
80
- target : Array < Uint8Array > ,
85
+ target : Array < Chunk | PrecomputedChunk > ,
81
86
type : string ,
82
87
props : Object ,
83
88
) : void {
84
89
// TODO: Figure out if it was self closing.
85
- target . push ( endTag1 , convertStringToBuffer ( type ) , endTag2 ) ;
90
+ target . push ( endTag1 , stringToChunk ( type ) , endTag2 ) ;
86
91
}
87
92
88
93
// Structural Nodes
89
94
90
95
// A placeholder is a node inside a hidden partial tree that can be filled in later, but before
91
96
// 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>' ) ;
95
100
export function writePlaceholder (
96
101
destination : Destination ,
97
102
id : number ,
@@ -101,16 +106,18 @@ export function writePlaceholder(
101
106
writeChunk ( destination , placeholder1 ) ;
102
107
// TODO: Use the identifierPrefix option to make the prefix configurable.
103
108
writeChunk ( destination , placeholder2 ) ;
104
- const formattedID = convertStringToBuffer ( id . toString ( 16 ) ) ;
109
+ const formattedID = stringToChunk ( id . toString ( 16 ) ) ;
105
110
writeChunk ( destination , formattedID ) ;
106
111
return writeChunk ( destination , placeholder3 ) ;
107
112
}
108
113
109
114
// 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 ( '<!--/$-->' ) ;
114
121
115
122
export function writeStartCompletedSuspenseBoundary (
116
123
destination : Destination ,
@@ -134,10 +141,10 @@ export function writeEndSuspenseBoundary(destination: Destination): boolean {
134
141
return writeChunk ( destination , endSuspenseBoundary ) ;
135
142
}
136
143
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>' ) ;
141
148
export function writeStartSegment (
142
149
destination : Destination ,
143
150
id : number ,
@@ -146,7 +153,7 @@ export function writeStartSegment(
146
153
writeChunk ( destination , startSegment ) ;
147
154
// TODO: Use the identifierPrefix option to make the prefix configurable.
148
155
writeChunk ( destination , startSegment2 ) ;
149
- const formattedID = convertStringToBuffer ( id . toString ( 16 ) ) ;
156
+ const formattedID = stringToChunk ( id . toString ( 16 ) ) ;
150
157
writeChunk ( destination , formattedID ) ;
151
158
return writeChunk ( destination , startSegment3 ) ;
152
159
}
@@ -276,12 +283,14 @@ const completeBoundaryFunction =
276
283
const clientRenderFunction =
277
284
'function $RX(b){if(b=document.getElementById(b)){do b=b.previousSibling;while(8!==b.nodeType||"$?"!==b.data);b.data="$!";b._reactRetry&&b._reactRetry()}}' ;
278
285
279
- const completeSegmentScript1Full = convertStringToBuffer (
286
+ const completeSegmentScript1Full = stringToPrecomputedChunk (
280
287
'<script>' + completeSegmentFunction + ';$RS("S:' ,
281
288
) ;
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>' ) ;
285
294
286
295
export function writeCompletedSegmentInstruction (
287
296
destination : Destination ,
@@ -297,19 +306,21 @@ export function writeCompletedSegmentInstruction(
297
306
writeChunk ( destination , completeSegmentScript1Partial ) ;
298
307
}
299
308
// TODO: Use the identifierPrefix option to make the prefix configurable.
300
- const formattedID = convertStringToBuffer ( contentSegmentID . toString ( 16 ) ) ;
309
+ const formattedID = stringToChunk ( contentSegmentID . toString ( 16 ) ) ;
301
310
writeChunk ( destination , formattedID ) ;
302
311
writeChunk ( destination , completeSegmentScript2 ) ;
303
312
writeChunk ( destination , formattedID ) ;
304
313
return writeChunk ( destination , completeSegmentScript3 ) ;
305
314
}
306
315
307
- const completeBoundaryScript1Full = convertStringToBuffer (
316
+ const completeBoundaryScript1Full = stringToPrecomputedChunk (
308
317
'<script>' + completeBoundaryFunction + ';$RC("' ,
309
318
) ;
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>' ) ;
313
324
314
325
export function writeCompletedBoundaryInstruction (
315
326
destination : Destination ,
@@ -330,23 +341,21 @@ export function writeCompletedBoundaryInstruction(
330
341
boundaryID . id !== null ,
331
342
'An ID must have been assigned before we can complete the boundary.' ,
332
343
) ;
333
- const formattedBoundaryID = convertStringToBuffer (
344
+ const formattedBoundaryID = stringToChunk (
334
345
encodeHTMLIDAttribute ( boundaryID . id ) ,
335
346
) ;
336
- const formattedContentID = convertStringToBuffer (
337
- contentSegmentID . toString ( 16 ) ,
338
- ) ;
347
+ const formattedContentID = stringToChunk ( contentSegmentID . toString ( 16 ) ) ;
339
348
writeChunk ( destination , formattedBoundaryID ) ;
340
349
writeChunk ( destination , completeBoundaryScript2 ) ;
341
350
writeChunk ( destination , formattedContentID ) ;
342
351
return writeChunk ( destination , completeBoundaryScript3 ) ;
343
352
}
344
353
345
- const clientRenderScript1Full = convertStringToBuffer (
354
+ const clientRenderScript1Full = stringToPrecomputedChunk (
346
355
'<script>' + clientRenderFunction + ';$RX("' ,
347
356
) ;
348
- const clientRenderScript1Partial = convertStringToBuffer ( '<script>$RX("' ) ;
349
- const clientRenderScript2 = convertStringToBuffer ( '")</script>' ) ;
357
+ const clientRenderScript1Partial = stringToPrecomputedChunk ( '<script>$RX("' ) ;
358
+ const clientRenderScript2 = stringToPrecomputedChunk ( '")</script>' ) ;
350
359
351
360
export function writeClientRenderBoundaryInstruction (
352
361
destination : Destination ,
@@ -365,7 +374,7 @@ export function writeClientRenderBoundaryInstruction(
365
374
boundaryID . id !== null ,
366
375
'An ID must have been assigned before we can complete the boundary.' ,
367
376
) ;
368
- const formattedBoundaryID = convertStringToBuffer (
377
+ const formattedBoundaryID = stringToPrecomputedChunk (
369
378
encodeHTMLIDAttribute ( boundaryID . id ) ,
370
379
) ;
371
380
writeChunk ( destination , formattedBoundaryID ) ;
0 commit comments