Skip to content

Commit

Permalink
Process Flight Chunks as Strings in renderToMarkup
Browse files Browse the repository at this point in the history
This lets us avoid the dependency on TextDecoder/TextEncoder.
  • Loading branch information
sebmarkbage committed Jun 28, 2024
1 parent 2898e07 commit 1ceacb3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
14 changes: 5 additions & 9 deletions packages/react-html/src/ReactHTMLLegacyClientStreamConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@
* @flow
*/

// TODO: The legacy one should not use binary.
export type StringDecoder = null;

export type StringDecoder = TextDecoder;

export function createStringDecoder(): StringDecoder {
return new TextDecoder();
export function createStringDecoder(): null {
return null;
}

const decoderOptions = {stream: true};

export function readPartialStringChunk(
decoder: StringDecoder,
buffer: Uint8Array,
): string {
return decoder.decode(buffer, decoderOptions);
throw new Error('Not implemented.');
}

export function readFinalStringChunk(
decoder: StringDecoder,
buffer: Uint8Array,
): string {
return decoder.decode(buffer);
throw new Error('Not implemented.');
}
6 changes: 2 additions & 4 deletions packages/react-html/src/ReactHTMLServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import {
createResponse as createFlightResponse,
getRoot as getFlightRoot,
processBinaryChunk as processFlightBinaryChunk,
processStringChunk as processFlightStringChunk,
close as closeFlight,
} from 'react-client/src/ReactFlightClient';

Expand Down Expand Up @@ -75,12 +75,10 @@ export function renderToMarkup(
options?: MarkupOptions,
): Promise<string> {
return new Promise((resolve, reject) => {
const textEncoder = new TextEncoder();
const flightDestination = {
push(chunk: string | null): boolean {
if (chunk !== null) {
// TODO: Legacy should not use binary streams.
processFlightBinaryChunk(flightResponse, textEncoder.encode(chunk));
processFlightStringChunk(flightResponse, chunk);
} else {
closeFlight(flightResponse);
}
Expand Down

0 comments on commit 1ceacb3

Please sign in to comment.