|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | +/// <reference types="react/experimental" /> |
| 5 | + |
| 6 | +import * as React from 'react'; |
| 7 | +import { PassThrough, Readable, Transform } from 'node:stream'; |
| 8 | +import { text } from 'node:stream/consumers'; |
| 9 | +import { Suspense, PropsWithChildren } from 'react'; |
| 10 | + |
| 11 | +import * as path from 'path'; |
| 12 | +import * as mock from 'mock-fs'; |
| 13 | + |
| 14 | +import ReactOnRails, { RailsContextWithServerStreamingCapabilities } from '../src/ReactOnRailsRSC'; |
| 15 | +import AsyncQueue from './AsyncQueue'; |
| 16 | +import StreamReader from './StreamReader'; |
| 17 | + |
| 18 | +const manifestFileDirectory = path.resolve(__dirname, '../src') |
| 19 | +const clientManifestPath = path.join(manifestFileDirectory, 'react-client-manifest.json'); |
| 20 | + |
| 21 | +mock({ |
| 22 | + [clientManifestPath]: JSON.stringify({ |
| 23 | + filePathToModuleMetadata: {}, |
| 24 | + moduleLoading: { prefix: '', crossOrigin: null }, |
| 25 | + }), |
| 26 | +}); |
| 27 | + |
| 28 | +afterAll(() => mock.restore()); |
| 29 | + |
| 30 | +const AsyncQueueItem = async ({ asyncQueue, children }: PropsWithChildren<{asyncQueue: AsyncQueue<string>}>) => { |
| 31 | + const value = await asyncQueue.dequeue(); |
| 32 | + |
| 33 | + return ( |
| 34 | + <> |
| 35 | + <p>Data: {value}</p> |
| 36 | + {children} |
| 37 | + </> |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +const AsyncQueueContainer = ({ asyncQueue }: { asyncQueue: AsyncQueue<string> }) => { |
| 42 | + return ( |
| 43 | + <div> |
| 44 | + <h1>Async Queue</h1> |
| 45 | + <Suspense fallback={<p>Loading Item1</p>}> |
| 46 | + <AsyncQueueItem asyncQueue={asyncQueue}> |
| 47 | + <Suspense fallback={<p>Loading Item2</p>}> |
| 48 | + <AsyncQueueItem asyncQueue={asyncQueue}> |
| 49 | + <Suspense fallback={<p>Loading Item3</p>}> |
| 50 | + <AsyncQueueItem asyncQueue={asyncQueue} /> |
| 51 | + </Suspense> |
| 52 | + </AsyncQueueItem> |
| 53 | + </Suspense> |
| 54 | + </AsyncQueueItem> |
| 55 | + </Suspense> |
| 56 | + </div> |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +ReactOnRails.register({ AsyncQueueContainer }); |
| 61 | + |
| 62 | +const renderComponent = (props: Record<string, unknown>) => { |
| 63 | + return ReactOnRails.serverRenderRSCReactComponent({ |
| 64 | + railsContext: { |
| 65 | + reactClientManifestFileName: 'react-client-manifest.json', |
| 66 | + reactServerClientManifestFileName: 'react-server-client-manifest.json', |
| 67 | + } as unknown as RailsContextWithServerStreamingCapabilities, |
| 68 | + name: 'AsyncQueueContainer', |
| 69 | + renderingReturnsPromises: true, |
| 70 | + throwJsErrors: true, |
| 71 | + domNodeId: 'dom-id', |
| 72 | + props, |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +const createParallelRenders = (size: number) => { |
| 77 | + const asyncQueues = new Array(size).fill(null).map(() => new AsyncQueue<string>()); |
| 78 | + const streams = asyncQueues.map((asyncQueue) => { |
| 79 | + return renderComponent({ asyncQueue }); |
| 80 | + }); |
| 81 | + const readers = streams.map(stream => new StreamReader(stream)); |
| 82 | + |
| 83 | + const enqueue = (value: string) => asyncQueues.forEach(asyncQueues => asyncQueues.enqueue(value)); |
| 84 | + |
| 85 | + const removeComponentJsonData = (chunk: string) => { |
| 86 | + const parsedJson = JSON.parse(chunk); |
| 87 | + const html = parsedJson.html as string; |
| 88 | + const santizedHtml = html.split('\n').map(chunkLine => { |
| 89 | + if (!chunkLine.includes('"stack":')) { |
| 90 | + return chunkLine; |
| 91 | + } |
| 92 | + |
| 93 | + const regexMatch = /(^\d+):\{/.exec(chunkLine) |
| 94 | + if (!regexMatch) { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + const chunkJsonString = chunkLine.slice(chunkLine.indexOf('{')); |
| 99 | + const chunkJson = JSON.parse(chunkJsonString); |
| 100 | + delete chunkJson.stack; |
| 101 | + return `${regexMatch[1]}:${JSON.stringify(chunkJson)}` |
| 102 | + }); |
| 103 | + |
| 104 | + return JSON.stringify({ |
| 105 | + ...parsedJson, |
| 106 | + html: santizedHtml, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + const expectNextChunk = (nextChunk: string) => Promise.all( |
| 111 | + readers.map(async (reader) => { |
| 112 | + const chunk = await reader.nextChunk(); |
| 113 | + expect(removeComponentJsonData(chunk)).toEqual(removeComponentJsonData(nextChunk)); |
| 114 | + }) |
| 115 | + ); |
| 116 | + |
| 117 | + const expectEndOfStream = () => Promise.all( |
| 118 | + readers.map(reader => expect(reader.nextChunk()).rejects.toThrow(/Queue Ended/)) |
| 119 | + ); |
| 120 | + |
| 121 | + return { enqueue, expectNextChunk, expectEndOfStream }; |
| 122 | +} |
| 123 | + |
| 124 | +test('Renders concurrent rsc streams as single rsc stream', async () => { |
| 125 | + expect.assertions(258); |
| 126 | + const asyncQueue = new AsyncQueue<string>(); |
| 127 | + const stream = renderComponent({ asyncQueue }); |
| 128 | + const reader = new StreamReader(stream); |
| 129 | + |
| 130 | + const chunks: string[] = []; |
| 131 | + let chunk = await reader.nextChunk() |
| 132 | + chunks.push(chunk); |
| 133 | + expect(chunk).toContain("Async Queue"); |
| 134 | + expect(chunk).toContain("Loading Item2"); |
| 135 | + expect(chunk).not.toContain("Random Value"); |
| 136 | + |
| 137 | + asyncQueue.enqueue("Random Value1"); |
| 138 | + chunk = await reader.nextChunk(); |
| 139 | + chunks.push(chunk); |
| 140 | + expect(chunk).toContain("Random Value1"); |
| 141 | + |
| 142 | + asyncQueue.enqueue("Random Value2"); |
| 143 | + chunk = await reader.nextChunk(); |
| 144 | + chunks.push(chunk); |
| 145 | + expect(chunk).toContain("Random Value2"); |
| 146 | + |
| 147 | + asyncQueue.enqueue("Random Value3"); |
| 148 | + chunk = await reader.nextChunk(); |
| 149 | + chunks.push(chunk); |
| 150 | + expect(chunk).toContain("Random Value3"); |
| 151 | + |
| 152 | + await expect(reader.nextChunk()).rejects.toThrow(/Queue Ended/); |
| 153 | + |
| 154 | + const { enqueue, expectNextChunk, expectEndOfStream } = createParallelRenders(50); |
| 155 | + |
| 156 | + expect(chunks).toHaveLength(4); |
| 157 | + await expectNextChunk(chunks[0]!); |
| 158 | + enqueue("Random Value1"); |
| 159 | + await expectNextChunk(chunks[1]!); |
| 160 | + enqueue("Random Value2"); |
| 161 | + await expectNextChunk(chunks[2]!); |
| 162 | + enqueue("Random Value3"); |
| 163 | + await expectNextChunk(chunks[3]!); |
| 164 | + await expectEndOfStream(); |
| 165 | +}); |
0 commit comments