From b0747116d98aaee933e991453dcfff3cd54b6f4c Mon Sep 17 00:00:00 2001 From: Abanoub Ghadban Date: Tue, 15 Apr 2025 14:30:28 +0200 Subject: [PATCH 01/50] replace RSCPayloadContainer with RSC injection utils --- node_package/src/RSCClientRoot.ts | 13 +- node_package/src/RSCPayloadContainer.tsx | 128 ---------------- node_package/src/RSCPayloadGenerator.ts | 57 ++++++++ node_package/src/RSCServerRoot.tsx | 40 ++--- node_package/src/ReactOnRails.node.ts | 4 + node_package/src/injectRSCPayload.ts | 86 +++++++++++ .../src/streamServerRenderedReactComponent.ts | 17 ++- .../transformRSCStreamAndReplayConsoleLogs.ts | 43 +++--- node_package/src/types/index.ts | 31 ++++ node_package/tests/injectRSCPayload.test.ts | 138 ++++++++++++++++++ 10 files changed, 363 insertions(+), 194 deletions(-) delete mode 100644 node_package/src/RSCPayloadContainer.tsx create mode 100644 node_package/src/RSCPayloadGenerator.ts create mode 100644 node_package/src/injectRSCPayload.ts create mode 100644 node_package/tests/injectRSCPayload.test.ts diff --git a/node_package/src/RSCClientRoot.ts b/node_package/src/RSCClientRoot.ts index 08b5c2fad3..49cb294303 100644 --- a/node_package/src/RSCClientRoot.ts +++ b/node_package/src/RSCClientRoot.ts @@ -5,14 +5,14 @@ import * as ReactDOMClient from 'react-dom/client'; import { createFromReadableStream } from 'react-on-rails-rsc/client'; import { fetch } from './utils.ts'; import transformRSCStreamAndReplayConsoleLogs from './transformRSCStreamAndReplayConsoleLogs.ts'; -import { RailsContext, RenderFunction, RSCPayloadChunk } from './types/index.ts'; +import { RailsContext, RenderFunction } from './types/index.ts'; import { ensureReactUseAvailable } from './reactApis.cts'; ensureReactUseAvailable(); declare global { interface Window { - REACT_ON_RAILS_RSC_PAYLOAD?: RSCPayloadChunk[]; + REACT_ON_RAILS_RSC_PAYLOAD?: string[]; } } @@ -39,13 +39,13 @@ const fetchRSC = ({ componentName, rscPayloadGenerationUrlPath, componentProps } }; const createRSCStreamFromPage = () => { - let streamController: ReadableStreamController | undefined; - const stream = new ReadableStream({ + let streamController: ReadableStreamController | undefined; + const stream = new ReadableStream({ start(controller) { if (typeof window === 'undefined') { return; } - const handleChunk = (chunk: RSCPayloadChunk) => { + const handleChunk = (chunk: string) => { controller.enqueue(chunk); }; @@ -67,9 +67,10 @@ const createRSCStreamFromPage = () => { window.REACT_ON_RAILS_RSC_PAYLOAD = []; } window.REACT_ON_RAILS_RSC_PAYLOAD.forEach(handleChunk); + const originalPush = window.REACT_ON_RAILS_RSC_PAYLOAD.push; window.REACT_ON_RAILS_RSC_PAYLOAD.push = (...chunks) => { chunks.forEach(handleChunk); - return chunks.length; + return originalPush.call(window.REACT_ON_RAILS_RSC_PAYLOAD, ...chunks); }; streamController = controller; }, diff --git a/node_package/src/RSCPayloadContainer.tsx b/node_package/src/RSCPayloadContainer.tsx deleted file mode 100644 index d1c58fe29a..0000000000 --- a/node_package/src/RSCPayloadContainer.tsx +++ /dev/null @@ -1,128 +0,0 @@ -import * as React from 'react'; - -type StreamChunk = { - chunk: string; - isLastChunk: boolean; -}; - -type RSCPayloadContainerProps = { - RSCPayloadStream: NodeJS.ReadableStream; -}; - -type RSCPayloadContainerInnerProps = { - chunkIndex: number; - getChunkPromise: (chunkIndex: number) => Promise; -}; - -// In JavaScript, when an escape sequence with a backslash (\) is followed by a character -// that isn't a recognized escape character, the backslash is ignored, and the character -// is treated as-is. -// This behavior allows us to use the backslash to escape characters that might be -// interpreted as HTML tags, preventing them from being processed by the HTML parser. -// For example, we can escape the comment tag