@samitouri / QOS-React-2 / commits / ce6842d8f5

Delete processStringChunk (#26896)

Follow up to #26827. These can't include binary data and we don't really have any use cases that really require these to already be strings. When the stream is encoded inside another protocol - such as HTML we need a different format that encode binary offsets and binary data.

Sebastian Markbåge committed Jun 10, 2023 at 16:59 UTC ce6842d8f528977119b80d969306c8475099f66e
8 files changed +25 -51
packages/react-client/src/ReactFlightClient.js
+1 -24
@@ -28,7 +28,6 @@ import {
28 dispatchHint,
29 readPartialStringChunk,
30 readFinalStringChunk,
31 - supportsBinaryStreams,
31 createStringDecoder,
32 } from './ReactFlightClientConfig';
33
@@ -667,12 +666,9 @@ export function createResponse(
666 _callServer: callServer !== undefined ? callServer : missingCall,
667 _chunks: chunks,
668 _partialRow: '',
670 - _stringDecoder: (null: any),
669 + _stringDecoder: createStringDecoder(),
670 _fromJSON: (null: any),
671 };
673 - if (supportsBinaryStreams) {
674 - response._stringDecoder = createStringDecoder();
675 - }
672 // Don't inline this call because it causes closure to outline the call above.
673 response._fromJSON = createFromJSONCallback(response);
674 return response;
@@ -854,29 +850,10 @@ function processFullRow(response: Response, row: string): void {
850 }
851 }
852
857 -export function processStringChunk(
858 - response: Response,
859 - chunk: string,
860 - offset: number,
861 -): void {
862 - let linebreak = chunk.indexOf('\n', offset);
863 - while (linebreak > -1) {
864 - const fullrow = response._partialRow + chunk.slice(offset, linebreak);
865 - processFullRow(response, fullrow);
866 - response._partialRow = '';
867 - offset = linebreak + 1;
868 - linebreak = chunk.indexOf('\n', offset);
869 - }
870 - response._partialRow += chunk.slice(offset);
871 -}
872 -
853 export function processBinaryChunk(
854 response: Response,
855 chunk: Uint8Array,
856 ): void {
877 - if (!supportsBinaryStreams) {
878 - throw new Error("This environment don't support binary chunks.");
879 - }
857 const stringDecoder = response._stringDecoder;
858 let linebreak = chunk.indexOf(10); // newline
859 while (linebreak > -1) {
packages/react-client/src/ReactFlightClientConfigBrowser.js
-2
@@ -9,8 +9,6 @@
9
10 export type StringDecoder = TextDecoder;
11
12 -export const supportsBinaryStreams = true;
13 -
12 export function createStringDecoder(): StringDecoder {
13 return new TextDecoder();
14 }
packages/react-client/src/ReactFlightClientConfigNode.js
-2
@@ -11,8 +11,6 @@ import {TextDecoder} from 'util';
11
12 export type StringDecoder = TextDecoder;
13
14 -export const supportsBinaryStreams = true;
15 -
14 export function createStringDecoder(): StringDecoder {
15 return new TextDecoder();
16 }
packages/react-client/src/forks/ReactFlightClientConfig.custom.js
-1
@@ -40,7 +40,6 @@ export opaque type Source = mixed;
40
41 export opaque type StringDecoder = mixed; // eslint-disable-line no-undef
42
43 -export const supportsBinaryStreams = $$$config.supportsBinaryStreams;
43 export const createStringDecoder = $$$config.createStringDecoder;
44 export const readPartialStringChunk = $$$config.readPartialStringChunk;
45 export const readFinalStringChunk = $$$config.readFinalStringChunk;
packages/react-noop-renderer/src/ReactNoopFlightClient.js
+14 -4
@@ -18,10 +18,20 @@ import {readModule} from 'react-noop-renderer/flight-modules';
18
19 import ReactFlightClient from 'react-client/flight';
20
21 -type Source = Array<string>;
21 +type Source = Array<Uint8Array>;
22
23 -const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
24 - supportsBinaryStreams: false,
23 +const decoderOptions = {stream: true};
24 +
25 +const {createResponse, processBinaryChunk, getRoot, close} = ReactFlightClient({
26 + createStringDecoder() {
27 + return new TextDecoder();
28 + },
29 + readPartialStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
30 + return decoder.decode(buffer, decoderOptions);
31 + },
32 + readFinalStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
33 + return decoder.decode(buffer);
34 + },
35 resolveClientReference(bundlerConfig: null, idx: string) {
36 return idx;
37 },
@@ -37,7 +47,7 @@ const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
47 function read<T>(source: Source): Thenable<T> {
48 const response = createResponse(source, null);
49 for (let i = 0; i < source.length; i++) {
40 - processStringChunk(response, source[i], 0);
50 + processBinaryChunk(response, source[i], 0);
51 }
52 close(response);
53 return getRoot(response);
packages/react-noop-renderer/src/ReactNoopFlightServer.js
+8 -6
@@ -21,7 +21,9 @@ import {saveModule} from 'react-noop-renderer/flight-modules';
21
22 import ReactFlightServer from 'react-server/flight';
23
24 -type Destination = Array<string>;
24 +type Destination = Array<Uint8Array>;
25 +
26 +const textEncoder = new TextEncoder();
27
28 const ReactNoopFlightServer = ReactFlightServer({
29 scheduleWork(callback: () => void) {
@@ -39,13 +41,13 @@ const ReactNoopFlightServer = ReactFlightServer({
41 close(destination: Destination): void {},
42 closeWithError(destination: Destination, error: mixed): void {},
43 flushBuffered(destination: Destination): void {},
42 - stringToChunk(content: string): string {
43 - return content;
44 + stringToChunk(content: string): Uint8Array {
45 + return textEncoder.encode(content);
46 },
45 - stringToPrecomputedChunk(content: string): string {
46 - return content;
47 + stringToPrecomputedChunk(content: string): Uint8Array {
48 + return textEncoder.encode(content);
49 },
48 - clonePrecomputedChunk(chunk: string): string {
50 + clonePrecomputedChunk(chunk: Uint8Array): Uint8Array {
51 return chunk;
52 },
53 isClientReference(reference: Object): boolean {
packages/react-server-dom-esm/src/ReactFlightDOMClientNode.js
+1 -6
@@ -20,7 +20,6 @@ import {
20 processBinaryChunk,
21 close,
22 } from 'react-client/src/ReactFlightClient';
23 -import {processStringChunk} from '../../react-client/src/ReactFlightClient';
23
24 function noServerCall() {
25 throw new Error(
@@ -44,11 +43,7 @@ function createFromNodeStream<T>(
43 ): Thenable<T> {
44 const response: Response = createResponse(moduleRootPath, noServerCall);
45 stream.on('data', chunk => {
47 - if (typeof chunk === 'string') {
48 - processStringChunk(response, chunk, 0);
49 - } else {
50 - processBinaryChunk(response, chunk);
51 - }
46 + processBinaryChunk(response, chunk);
47 });
48 stream.on('error', error => {
49 reportGlobalError(response, error);
packages/react-server-dom-webpack/src/ReactFlightDOMClientNode.js
+1 -6
@@ -22,7 +22,6 @@ import {
22 processBinaryChunk,
23 close,
24 } from 'react-client/src/ReactFlightClient';
25 -import {processStringChunk} from '../../react-client/src/ReactFlightClient';
25
26 function noServerCall() {
27 throw new Error(
@@ -45,11 +44,7 @@ function createFromNodeStream<T>(
44 ): Thenable<T> {
45 const response: Response = createResponse(moduleMap, noServerCall);
46 stream.on('data', chunk => {
48 - if (typeof chunk === 'string') {
49 - processStringChunk(response, chunk, 0);
50 - } else {
51 - processBinaryChunk(response, chunk);
52 - }
47 + processBinaryChunk(response, chunk);
48 });
49 stream.on('error', error => {
50 reportGlobalError(response, error);