Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Delete processStringChunk
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.
  • Loading branch information
sebmarkbage committed Jun 3, 2023
commit 18ff594d5486d6478d5474317e6cd3de86a38b19
25 changes: 1 addition & 24 deletions packages/react-client/src/ReactFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
dispatchHint,
readPartialStringChunk,
readFinalStringChunk,
supportsBinaryStreams,
createStringDecoder,
} from './ReactFlightClientConfig';

Expand Down Expand Up @@ -667,12 +666,9 @@ export function createResponse(
_callServer: callServer !== undefined ? callServer : missingCall,
_chunks: chunks,
_partialRow: '',
_stringDecoder: (null: any),
_stringDecoder: createStringDecoder(),
_fromJSON: (null: any),
};
if (supportsBinaryStreams) {
response._stringDecoder = createStringDecoder();
}
// Don't inline this call because it causes closure to outline the call above.
response._fromJSON = createFromJSONCallback(response);
return response;
Expand Down Expand Up @@ -854,29 +850,10 @@ function processFullRow(response: Response, row: string): void {
}
}

export function processStringChunk(
response: Response,
chunk: string,
offset: number,
): void {
let linebreak = chunk.indexOf('\n', offset);
while (linebreak > -1) {
const fullrow = response._partialRow + chunk.slice(offset, linebreak);
processFullRow(response, fullrow);
response._partialRow = '';
offset = linebreak + 1;
linebreak = chunk.indexOf('\n', offset);
}
response._partialRow += chunk.slice(offset);
}

export function processBinaryChunk(
response: Response,
chunk: Uint8Array,
): void {
if (!supportsBinaryStreams) {
throw new Error("This environment don't support binary chunks.");
}
const stringDecoder = response._stringDecoder;
let linebreak = chunk.indexOf(10); // newline
while (linebreak > -1) {
Expand Down
2 changes: 0 additions & 2 deletions packages/react-client/src/ReactFlightClientConfigBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

export type StringDecoder = TextDecoder;

export const supportsBinaryStreams = true;

export function createStringDecoder(): StringDecoder {
return new TextDecoder();
}
Expand Down
2 changes: 0 additions & 2 deletions packages/react-client/src/ReactFlightClientConfigNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import {TextDecoder} from 'util';

export type StringDecoder = TextDecoder;

export const supportsBinaryStreams = true;

export function createStringDecoder(): StringDecoder {
return new TextDecoder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export opaque type Source = mixed;

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

export const supportsBinaryStreams = $$$config.supportsBinaryStreams;
export const createStringDecoder = $$$config.createStringDecoder;
export const readPartialStringChunk = $$$config.readPartialStringChunk;
export const readFinalStringChunk = $$$config.readFinalStringChunk;
18 changes: 14 additions & 4 deletions packages/react-noop-renderer/src/ReactNoopFlightClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,20 @@ import {readModule} from 'react-noop-renderer/flight-modules';

import ReactFlightClient from 'react-client/flight';

type Source = Array<string>;
type Source = Array<Uint8Array>;

const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
supportsBinaryStreams: false,
const decoderOptions = {stream: true};

const {createResponse, processBinaryChunk, getRoot, close} = ReactFlightClient({
createStringDecoder() {
return new TextDecoder();
},
readPartialStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
return decoder.decode(buffer, decoderOptions);
},
readFinalStringChunk(decoder: TextDecoder, buffer: Uint8Array): string {
return decoder.decode(buffer);
},
resolveClientReference(bundlerConfig: null, idx: string) {
return idx;
},
Expand All @@ -37,7 +47,7 @@ const {createResponse, processStringChunk, getRoot, close} = ReactFlightClient({
function read<T>(source: Source): Thenable<T> {
const response = createResponse(source, null);
for (let i = 0; i < source.length; i++) {
processStringChunk(response, source[i], 0);
processBinaryChunk(response, source[i], 0);
}
close(response);
return getRoot(response);
Expand Down
14 changes: 8 additions & 6 deletions packages/react-noop-renderer/src/ReactNoopFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import {saveModule} from 'react-noop-renderer/flight-modules';

import ReactFlightServer from 'react-server/flight';

type Destination = Array<string>;
type Destination = Array<Uint8Array>;

const textEncoder = new TextEncoder();

const ReactNoopFlightServer = ReactFlightServer({
scheduleWork(callback: () => void) {
Expand All @@ -39,13 +41,13 @@ const ReactNoopFlightServer = ReactFlightServer({
close(destination: Destination): void {},
closeWithError(destination: Destination, error: mixed): void {},
flushBuffered(destination: Destination): void {},
stringToChunk(content: string): string {
return content;
stringToChunk(content: string): Uint8Array {
return textEncoder.encode(content);
},
stringToPrecomputedChunk(content: string): string {
return content;
stringToPrecomputedChunk(content: string): Uint8Array {
return textEncoder.encode(content);
},
clonePrecomputedChunk(chunk: string): string {
clonePrecomputedChunk(chunk: Uint8Array): Uint8Array {
return chunk;
},
isClientReference(reference: Object): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
processBinaryChunk,
close,
} from 'react-client/src/ReactFlightClient';
import {processStringChunk} from '../../react-client/src/ReactFlightClient';

function noServerCall() {
throw new Error(
Expand All @@ -44,11 +43,7 @@ function createFromNodeStream<T>(
): Thenable<T> {
const response: Response = createResponse(moduleRootPath, noServerCall);
stream.on('data', chunk => {
if (typeof chunk === 'string') {
processStringChunk(response, chunk, 0);
} else {
processBinaryChunk(response, chunk);
}
processBinaryChunk(response, chunk);
});
stream.on('error', error => {
reportGlobalError(response, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
processBinaryChunk,
close,
} from 'react-client/src/ReactFlightClient';
import {processStringChunk} from '../../react-client/src/ReactFlightClient';

function noServerCall() {
throw new Error(
Expand All @@ -45,11 +44,7 @@ function createFromNodeStream<T>(
): Thenable<T> {
const response: Response = createResponse(moduleMap, noServerCall);
stream.on('data', chunk => {
if (typeof chunk === 'string') {
processStringChunk(response, chunk, 0);
} else {
processBinaryChunk(response, chunk);
}
processBinaryChunk(response, chunk);
});
stream.on('error', error => {
reportGlobalError(response, error);
Expand Down