Skip to content

Commit 2cc4128

Browse files
sebmarkbageAndyPengc12
authored andcommitted
Delete processStringChunk (facebook#26896)
Follow up to facebook#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.
1 parent 345d995 commit 2cc4128

File tree

8 files changed

+25
-51
lines changed

8 files changed

+25
-51
lines changed

packages/react-client/src/ReactFlightClient.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
dispatchHint,
2929
readPartialStringChunk,
3030
readFinalStringChunk,
31-
supportsBinaryStreams,
3231
createStringDecoder,
3332
} from './ReactFlightClientConfig';
3433

@@ -667,12 +666,9 @@ export function createResponse(
667666
_callServer: callServer !== undefined ? callServer : missingCall,
668667
_chunks: chunks,
669668
_partialRow: '',
670-
_stringDecoder: (null: any),
669+
_stringDecoder: createStringDecoder(),
671670
_fromJSON: (null: any),
672671
};
673-
if (supportsBinaryStreams) {
674-
response._stringDecoder = createStringDecoder();
675-
}
676672
// Don't inline this call because it causes closure to outline the call above.
677673
response._fromJSON = createFromJSONCallback(response);
678674
return response;
@@ -854,29 +850,10 @@ function processFullRow(response: Response, row: string): void {
854850
}
855851
}
856852

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-
873853
export function processBinaryChunk(
874854
response: Response,
875855
chunk: Uint8Array,
876856
): void {
877-
if (!supportsBinaryStreams) {
878-
throw new Error("This environment don't support binary chunks.");
879-
}
880857
const stringDecoder = response._stringDecoder;
881858
let linebreak = chunk.indexOf(10); // newline
882859
while (linebreak > -1) {

packages/react-client/src/ReactFlightClientConfigBrowser.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
export type StringDecoder = TextDecoder;
1111

12-
export const supportsBinaryStreams = true;
13-
1412
export function createStringDecoder(): StringDecoder {
1513
return new TextDecoder();
1614
}

packages/react-client/src/ReactFlightClientConfigNode.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import {TextDecoder} from 'util';
1111

1212
export type StringDecoder = TextDecoder;
1313

14-
export const supportsBinaryStreams = true;
15-
1614
export function createStringDecoder(): StringDecoder {
1715
return new TextDecoder();
1816
}

packages/react-client/src/forks/ReactFlightClientConfig.custom.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export opaque type Source = mixed;
4040

4141
export opaque type StringDecoder = mixed; // eslint-disable-line no-undef
4242

43-
export const supportsBinaryStreams = $$$config.supportsBinaryStreams;
4443
export const createStringDecoder = $$$config.createStringDecoder;
4544
export const readPartialStringChunk = $$$config.readPartialStringChunk;
4645
export const readFinalStringChunk = $$$config.readFinalStringChunk;

packages/react-noop-renderer/src/ReactNoopFlightClient.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ import {readModule} from 'react-noop-renderer/flight-modules';
1818

1919
import ReactFlightClient from 'react-client/flight';
2020

21-
type Source = Array<string>;
21+
type Source = Array<Uint8Array>;
2222

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+
},
2535
resolveClientReference(bundlerConfig: null, idx: string) {
2636
return idx;
2737
},
@@ -37,7 +47,7 @@ const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
3747
function read<T>(source: Source): Thenable<T> {
3848
const response = createResponse(source, null);
3949
for (let i = 0; i < source.length; i++) {
40-
processStringChunk(response, source[i], 0);
50+
processBinaryChunk(response, source[i], 0);
4151
}
4252
close(response);
4353
return getRoot(response);

packages/react-noop-renderer/src/ReactNoopFlightServer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import {saveModule} from 'react-noop-renderer/flight-modules';
2121

2222
import ReactFlightServer from 'react-server/flight';
2323

24-
type Destination = Array<string>;
24+
type Destination = Array<Uint8Array>;
25+
26+
const textEncoder = new TextEncoder();
2527

2628
const ReactNoopFlightServer = ReactFlightServer({
2729
scheduleWork(callback: () => void) {
@@ -39,13 +41,13 @@ const ReactNoopFlightServer = ReactFlightServer({
3941
close(destination: Destination): void {},
4042
closeWithError(destination: Destination, error: mixed): void {},
4143
flushBuffered(destination: Destination): void {},
42-
stringToChunk(content: string): string {
43-
return content;
44+
stringToChunk(content: string): Uint8Array {
45+
return textEncoder.encode(content);
4446
},
45-
stringToPrecomputedChunk(content: string): string {
46-
return content;
47+
stringToPrecomputedChunk(content: string): Uint8Array {
48+
return textEncoder.encode(content);
4749
},
48-
clonePrecomputedChunk(chunk: string): string {
50+
clonePrecomputedChunk(chunk: Uint8Array): Uint8Array {
4951
return chunk;
5052
},
5153
isClientReference(reference: Object): boolean {

packages/react-server-dom-esm/src/ReactFlightDOMClientNode.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
processBinaryChunk,
2121
close,
2222
} from 'react-client/src/ReactFlightClient';
23-
import {processStringChunk} from '../../react-client/src/ReactFlightClient';
2423

2524
function noServerCall() {
2625
throw new Error(
@@ -44,11 +43,7 @@ function createFromNodeStream<T>(
4443
): Thenable<T> {
4544
const response: Response = createResponse(moduleRootPath, noServerCall);
4645
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);
5247
});
5348
stream.on('error', error => {
5449
reportGlobalError(response, error);

packages/react-server-dom-webpack/src/ReactFlightDOMClientNode.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
processBinaryChunk,
2323
close,
2424
} from 'react-client/src/ReactFlightClient';
25-
import {processStringChunk} from '../../react-client/src/ReactFlightClient';
2625

2726
function noServerCall() {
2827
throw new Error(
@@ -45,11 +44,7 @@ function createFromNodeStream<T>(
4544
): Thenable<T> {
4645
const response: Response = createResponse(moduleMap, noServerCall);
4746
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);
5348
});
5449
stream.on('error', error => {
5550
reportGlobalError(response, error);

0 commit comments

Comments
 (0)