@eserstack/laroux-server 0.0.0-bootstrap.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +411 -0
- package/dist/adapters/react/error-chunk.d.ts +11 -0
- package/dist/adapters/react/error-chunk.js +12 -0
- package/dist/adapters/react/html-shell.d.ts +15 -0
- package/dist/adapters/react/html-shell.js +109 -0
- package/dist/adapters/react/inline-rsc-emitter.d.ts +62 -0
- package/dist/adapters/react/inline-rsc-emitter.js +120 -0
- package/dist/adapters/react/mod.d.ts +30 -0
- package/dist/adapters/react/mod.js +36 -0
- package/dist/adapters/react/render-app.d.ts +46 -0
- package/dist/adapters/react/render-app.js +44 -0
- package/dist/adapters/react/renderer.d.ts +15 -0
- package/dist/adapters/react/renderer.js +86 -0
- package/dist/adapters/react/rsc-flight-renderer.d.ts +64 -0
- package/dist/adapters/react/rsc-flight-renderer.js +623 -0
- package/dist/adapters/react/rsc-handler.d.ts +47 -0
- package/dist/adapters/react/rsc-handler.js +114 -0
- package/dist/adapters/react/script-json.d.ts +12 -0
- package/dist/adapters/react/script-json.js +18 -0
- package/dist/adapters/react/ssr-renderer.d.ts +63 -0
- package/dist/adapters/react/ssr-renderer.js +625 -0
- package/dist/bin-main.d.ts +1 -0
- package/dist/bin-main.js +18 -0
- package/dist/cli.d.ts +26 -0
- package/dist/cli.js +48 -0
- package/dist/commands/build.d.ts +78 -0
- package/dist/commands/build.js +221 -0
- package/dist/commands/dev.d.ts +50 -0
- package/dist/commands/dev.js +133 -0
- package/dist/commands/init.d.ts +29 -0
- package/dist/commands/init.js +228 -0
- package/dist/commands/serve.d.ts +47 -0
- package/dist/commands/serve.js +124 -0
- package/dist/config/load-config.d.ts +14 -0
- package/dist/config/load-config.js +67 -0
- package/dist/domain/action-registry.d.ts +83 -0
- package/dist/domain/action-registry.js +124 -0
- package/dist/domain/build-module.d.ts +30 -0
- package/dist/domain/build-module.js +50 -0
- package/dist/domain/hmr-manager.d.ts +51 -0
- package/dist/domain/hmr-manager.js +90 -0
- package/dist/domain/html-shell.d.ts +56 -0
- package/dist/domain/html-shell.js +15 -0
- package/dist/domain/middleware-dispatcher.d.ts +42 -0
- package/dist/domain/middleware-dispatcher.js +150 -0
- package/dist/domain/rebuild-handler.d.ts +52 -0
- package/dist/domain/rebuild-handler.js +52 -0
- package/dist/domain/renderer.d.ts +62 -0
- package/dist/domain/renderer.js +15 -0
- package/dist/domain/route-dispatcher.d.ts +40 -0
- package/dist/domain/route-dispatcher.js +134 -0
- package/dist/domain/server-modules.d.ts +65 -0
- package/dist/domain/server-modules.js +208 -0
- package/dist/main.d.ts +40 -0
- package/dist/main.js +256 -0
- package/dist/module.d.ts +24 -0
- package/dist/module.js +140 -0
- package/dist/options.d.ts +53 -0
- package/dist/options.js +14 -0
- package/dist/package.json +98 -0
- package/dist/proxy/types.d.ts +19 -0
- package/dist/proxy/types.js +3 -0
- package/dist/runtime/action-guard.d.ts +49 -0
- package/dist/runtime/action-guard.js +115 -0
- package/dist/runtime/html-shell.d.ts +66 -0
- package/dist/runtime/html-shell.js +246 -0
- package/dist/runtime/media-types.d.ts +13 -0
- package/dist/runtime/media-types.js +83 -0
- package/dist/runtime/path-containment.d.ts +11 -0
- package/dist/runtime/path-containment.js +27 -0
- package/dist/runtime/seo/generate-meta.d.ts +16 -0
- package/dist/runtime/seo/generate-meta.js +121 -0
- package/dist/runtime/seo/index.d.ts +5 -0
- package/dist/runtime/seo/index.js +5 -0
- package/dist/runtime/seo/types.d.ts +25 -0
- package/dist/runtime/seo/types.js +5 -0
- package/dist/runtime/server.d.ts +58 -0
- package/dist/runtime/server.js +699 -0
- package/package.json +84 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Inline RSC Emitter
|
|
4
|
+
*
|
|
5
|
+
* Transforms RSC wire format into inline <script> tags that can be
|
|
6
|
+
* embedded directly in the HTML stream. This allows true progressive
|
|
7
|
+
* RSC streaming without a separate /rsc fetch.
|
|
8
|
+
*
|
|
9
|
+
* @see https://github.com/facebook/react/tree/main/packages/react-server-dom-webpack
|
|
10
|
+
*/
|
|
11
|
+
import * as scriptJson from "./script-json.js";
|
|
12
|
+
import * as protocol from "@eserstack/laroux-react/protocol";
|
|
13
|
+
import * as rscFlightRenderer from "./rsc-flight-renderer.js";
|
|
14
|
+
import * as logging from "@eserstack/logging";
|
|
15
|
+
const inlineLogger = logging.logger.getLogger([
|
|
16
|
+
"laroux-server",
|
|
17
|
+
"react",
|
|
18
|
+
"inline-rsc",
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Transform RSC wire format to inline script format
|
|
22
|
+
*
|
|
23
|
+
* Input (wire format): J0:{"value":"test"}\n
|
|
24
|
+
* Output (inline): <script>self.__RSC_CHUNK__({type:"J",id:0,value:{"value":"test"}})</script>\n
|
|
25
|
+
*/
|
|
26
|
+
export function chunkToInlineScript(line) {
|
|
27
|
+
const chunk = protocol.parseChunk(line.trim());
|
|
28
|
+
if (!chunk)
|
|
29
|
+
return "";
|
|
30
|
+
// Create the inline script that calls the global chunk handler
|
|
31
|
+
// The chunk is passed as a structured object for easier client-side processing
|
|
32
|
+
// Chunk values carry rendered strings, so escape for script context: a
|
|
33
|
+
// value containing </script or <!-- must not end or re-parse the element.
|
|
34
|
+
const chunkJson = scriptJson.escapeJsonForScript(JSON.stringify(chunk));
|
|
35
|
+
return `<script>self.__RSC_CHUNK__(${chunkJson})</script>\n`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a TransformStream that converts RSC wire format to inline scripts
|
|
39
|
+
*
|
|
40
|
+
* This transforms the raw RSC stream into HTML-embeddable script tags.
|
|
41
|
+
* Each chunk becomes a <script> that calls self.__RSC_CHUNK__()
|
|
42
|
+
*/
|
|
43
|
+
export function createInlineTransformStream() {
|
|
44
|
+
const decoder = new TextDecoder();
|
|
45
|
+
const encoder = new TextEncoder();
|
|
46
|
+
let buffer = "";
|
|
47
|
+
return new TransformStream({
|
|
48
|
+
transform(chunk, controller) {
|
|
49
|
+
// Decode and add to buffer
|
|
50
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
51
|
+
// Process complete lines
|
|
52
|
+
const lines = buffer.split("\n");
|
|
53
|
+
// Keep the last potentially incomplete line in buffer
|
|
54
|
+
buffer = lines.pop() ?? "";
|
|
55
|
+
for (const line of lines) {
|
|
56
|
+
if (!line.trim())
|
|
57
|
+
continue;
|
|
58
|
+
const inlineScript = chunkToInlineScript(line);
|
|
59
|
+
if (inlineScript) {
|
|
60
|
+
controller.enqueue(encoder.encode(inlineScript));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
flush(controller) {
|
|
65
|
+
// Process any remaining content
|
|
66
|
+
if (buffer.trim()) {
|
|
67
|
+
const inlineScript = chunkToInlineScript(buffer);
|
|
68
|
+
if (inlineScript) {
|
|
69
|
+
controller.enqueue(encoder.encode(inlineScript));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Create an inline RSC stream from a React element
|
|
77
|
+
*
|
|
78
|
+
* This function:
|
|
79
|
+
* 1. Renders the React element using the existing RSC Flight renderer
|
|
80
|
+
* 2. Transforms the wire format output to inline <script> tags
|
|
81
|
+
* 3. Returns a stream that can be piped into HTML response
|
|
82
|
+
*
|
|
83
|
+
* @param element - The root React element to render
|
|
84
|
+
* @param bundlerConfig - Module map for client component resolution
|
|
85
|
+
* @returns ReadableStream of inline script tags
|
|
86
|
+
*/
|
|
87
|
+
export function createInlineRSCStream(element, bundlerConfig) {
|
|
88
|
+
inlineLogger.debug("Creating inline RSC stream...");
|
|
89
|
+
// Use the existing RSC Flight renderer
|
|
90
|
+
const rscStream = rscFlightRenderer.renderToReadableStream(element, bundlerConfig);
|
|
91
|
+
// Transform wire format to inline scripts
|
|
92
|
+
const transformStream = createInlineTransformStream();
|
|
93
|
+
return rscStream.pipeThrough(transformStream);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Generate the bootstrap script that buffers inline RSC chunks
|
|
97
|
+
*
|
|
98
|
+
* This script must be placed BEFORE any RSC chunk scripts in the HTML.
|
|
99
|
+
* It sets up:
|
|
100
|
+
* 1. A buffer array for chunks that arrive before JS loads
|
|
101
|
+
* 2. A handler function that stores chunks in the buffer
|
|
102
|
+
*
|
|
103
|
+
* The client entry.ts will later process buffered chunks and
|
|
104
|
+
* replace the handler with a direct processor.
|
|
105
|
+
*/
|
|
106
|
+
export function generateInlineBootstrapScript() {
|
|
107
|
+
return `<script id="__RSC_INLINE_BOOTSTRAP__">
|
|
108
|
+
self.__RSC_CHUNKS_BUFFER__=[];
|
|
109
|
+
self.__RSC_CHUNK__=function(c){self.__RSC_CHUNKS_BUFFER__.push(c)};
|
|
110
|
+
</script>`;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Generate the completion marker script
|
|
114
|
+
*
|
|
115
|
+
* This script is placed AFTER all RSC chunks to signal completion.
|
|
116
|
+
* The client uses this to know when all chunks have been received.
|
|
117
|
+
*/
|
|
118
|
+
export function generateInlineCompletionScript() {
|
|
119
|
+
return `<script>self.__RSC_STREAMING_COMPLETE__=true</script>`;
|
|
120
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Adapter for @eserstack/laroux-server
|
|
3
|
+
*
|
|
4
|
+
* This module provides React-specific implementations for the server's
|
|
5
|
+
* rendering port interfaces. It includes:
|
|
6
|
+
* - RSC (React Server Components) Flight renderer
|
|
7
|
+
* - SSR (Server-Side Rendering) with RSC payload embedding
|
|
8
|
+
* - HTML shell builder for React applications
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import * as larouxServer from "@eserstack/laroux-server";
|
|
13
|
+
* import * as reactAdapter from "@eserstack/laroux-server/adapters/react";
|
|
14
|
+
*
|
|
15
|
+
* await larouxServer.startServer({
|
|
16
|
+
* mode: "dev",
|
|
17
|
+
* renderer: reactAdapter.reactRenderer,
|
|
18
|
+
* htmlShell: reactAdapter.reactHtmlShellBuilder,
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type { ModuleReference, RSCChunk, ServerActionReference, } from "@eserstack/laroux-react/protocol";
|
|
23
|
+
export type { LayoutProps, PageProps, PageRequest, RouteParams, } from "@eserstack/laroux";
|
|
24
|
+
export { createReactRenderer, reactRenderer } from "./renderer.ts";
|
|
25
|
+
export { createReactHtmlShellBuilder, reactHtmlShellBuilder, } from "./html-shell.ts";
|
|
26
|
+
export { type BundlerConfig, renderToReadableStream, renderValueToReadableStream, } from "./rsc-flight-renderer.ts";
|
|
27
|
+
export { loadModuleMap, renderRSC, renderRSCResponse, streamToResponse, } from "./rsc-handler.ts";
|
|
28
|
+
export { generateRSCPayloadScript, renderSSR, serializeRSCPayload, type SSROptions, type SSRResult, } from "./ssr-renderer.ts";
|
|
29
|
+
export { chunkToInlineScript, createInlineRSCStream, createInlineTransformStream, generateInlineBootstrapScript, generateInlineCompletionScript, type InlineChunkCallback, } from "./inline-rsc-emitter.ts";
|
|
30
|
+
export { renderApp, type RenderAppProps, type RenderAppRequestContext, } from "./render-app.ts";
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* React Adapter for @eserstack/laroux-server
|
|
4
|
+
*
|
|
5
|
+
* This module provides React-specific implementations for the server's
|
|
6
|
+
* rendering port interfaces. It includes:
|
|
7
|
+
* - RSC (React Server Components) Flight renderer
|
|
8
|
+
* - SSR (Server-Side Rendering) with RSC payload embedding
|
|
9
|
+
* - HTML shell builder for React applications
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import * as larouxServer from "@eserstack/laroux-server";
|
|
14
|
+
* import * as reactAdapter from "@eserstack/laroux-server/adapters/react";
|
|
15
|
+
*
|
|
16
|
+
* await larouxServer.startServer({
|
|
17
|
+
* mode: "dev",
|
|
18
|
+
* renderer: reactAdapter.reactRenderer,
|
|
19
|
+
* htmlShell: reactAdapter.reactHtmlShellBuilder,
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
// Renderer port implementation
|
|
24
|
+
export { createReactRenderer, reactRenderer } from "./renderer.js";
|
|
25
|
+
// HTML shell port implementation
|
|
26
|
+
export { createReactHtmlShellBuilder, reactHtmlShellBuilder, } from "./html-shell.js";
|
|
27
|
+
// RSC Flight renderer (low-level)
|
|
28
|
+
export { renderToReadableStream, renderValueToReadableStream, } from "./rsc-flight-renderer.js";
|
|
29
|
+
// RSC handler (HTTP layer)
|
|
30
|
+
export { loadModuleMap, renderRSC, renderRSCResponse, streamToResponse, } from "./rsc-handler.js";
|
|
31
|
+
// SSR renderer
|
|
32
|
+
export { generateRSCPayloadScript, renderSSR, serializeRSCPayload, } from "./ssr-renderer.js";
|
|
33
|
+
// Inline RSC emitter (for streaming)
|
|
34
|
+
export { chunkToInlineScript, createInlineRSCStream, createInlineTransformStream, generateInlineBootstrapScript, generateInlineCompletionScript, } from "./inline-rsc-emitter.js";
|
|
35
|
+
// App composition helper
|
|
36
|
+
export { renderApp, } from "./render-app.js";
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper to render the App with Layout and Page components
|
|
3
|
+
* Separated into .tsx file to avoid JSX in .ts files for JSR compatibility
|
|
4
|
+
*/
|
|
5
|
+
import * as React from "react";
|
|
6
|
+
import * as larouxRouter from "@eserstack/laroux/router";
|
|
7
|
+
/**
|
|
8
|
+
* Raw data of the request being rendered, which renderApp turns into the
|
|
9
|
+
* `request` prop (a PageRequest from `@eserstack/laroux`).
|
|
10
|
+
*/
|
|
11
|
+
export type RenderAppRequestContext = {
|
|
12
|
+
/** The Cookie header, or null when the request has none. */
|
|
13
|
+
readonly cookieHeader?: string | null;
|
|
14
|
+
/** The Host header, or null when the request has none. */
|
|
15
|
+
readonly host?: string | null;
|
|
16
|
+
/** The `locale` search parameter of the page's URL, or null. */
|
|
17
|
+
readonly localeParam?: string | null;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Props passed to the render function
|
|
21
|
+
*/
|
|
22
|
+
export type RenderAppProps = {
|
|
23
|
+
/** Pathname of the page being rendered. */
|
|
24
|
+
readonly pathname: string;
|
|
25
|
+
/** Values of the route's dynamic segments; empty when left out. */
|
|
26
|
+
readonly params?: larouxRouter.RouteParams;
|
|
27
|
+
/**
|
|
28
|
+
* Data of the request being rendered. It is passed with each render and
|
|
29
|
+
* never kept in module scope, which concurrent requests share.
|
|
30
|
+
*/
|
|
31
|
+
readonly requestContext?: RenderAppRequestContext;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Renders the Layout with Page as children.
|
|
35
|
+
*
|
|
36
|
+
* The page receives `params`, `pathname` and `request` (PageProps from
|
|
37
|
+
* `@eserstack/laroux`), and the layout `children` and `request`. A layout or
|
|
38
|
+
* page that is a client component gets no `request`: its props are sent to
|
|
39
|
+
* the browser, and the cookies would go with them, HttpOnly ones included.
|
|
40
|
+
*
|
|
41
|
+
* @param Layout - The Layout component to wrap the page
|
|
42
|
+
* @param Page - The Page component for the matched route
|
|
43
|
+
* @param props - Props including pathname, params, and request context; when
|
|
44
|
+
* left out, the page renders for "/" with no params and an empty request
|
|
45
|
+
*/
|
|
46
|
+
export declare function renderApp(Layout: React.ComponentType<larouxRouter.LayoutProps<React.ReactNode>>, Page: React.ComponentType<larouxRouter.PageProps>, props?: RenderAppProps): React.ReactElement;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Helper to render the App with Layout and Page components
|
|
4
|
+
* Separated into .tsx file to avoid JSX in .ts files for JSR compatibility
|
|
5
|
+
*/
|
|
6
|
+
import * as React from "react";
|
|
7
|
+
import * as larouxRouter from "@eserstack/laroux/router";
|
|
8
|
+
import * as protocol from "@eserstack/laroux-react/protocol";
|
|
9
|
+
/**
|
|
10
|
+
* Renders the Layout with Page as children.
|
|
11
|
+
*
|
|
12
|
+
* The page receives `params`, `pathname` and `request` (PageProps from
|
|
13
|
+
* `@eserstack/laroux`), and the layout `children` and `request`. A layout or
|
|
14
|
+
* page that is a client component gets no `request`: its props are sent to
|
|
15
|
+
* the browser, and the cookies would go with them, HttpOnly ones included.
|
|
16
|
+
*
|
|
17
|
+
* @param Layout - The Layout component to wrap the page
|
|
18
|
+
* @param Page - The Page component for the matched route
|
|
19
|
+
* @param props - Props including pathname, params, and request context; when
|
|
20
|
+
* left out, the page renders for "/" with no params and an empty request
|
|
21
|
+
*/
|
|
22
|
+
export function renderApp(Layout, Page, props) {
|
|
23
|
+
const requestContext = props?.requestContext;
|
|
24
|
+
const request = larouxRouter.createPageRequest({
|
|
25
|
+
cookieHeader: requestContext?.cookieHeader,
|
|
26
|
+
host: requestContext?.host,
|
|
27
|
+
locale: requestContext?.localeParam,
|
|
28
|
+
});
|
|
29
|
+
const pageProps = {
|
|
30
|
+
params: props?.params ?? {},
|
|
31
|
+
pathname: props?.pathname ?? "/",
|
|
32
|
+
};
|
|
33
|
+
// A client reference is not called here: the flight renderer sends the
|
|
34
|
+
// props it is given to the browser, so it gets them without request. Its
|
|
35
|
+
// declared props require request, hence the cast through unknown.
|
|
36
|
+
const pageElement = protocol.isClientReference(Page)
|
|
37
|
+
? React.createElement(Page, pageProps)
|
|
38
|
+
: React.createElement(Page, { ...pageProps, request });
|
|
39
|
+
return protocol.isClientReference(Layout)
|
|
40
|
+
? React.createElement(Layout, null, pageElement)
|
|
41
|
+
: React.createElement(
|
|
42
|
+
// children is the third argument, which the props type does not see
|
|
43
|
+
Layout, { request }, pageElement);
|
|
44
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Renderer Implementation
|
|
3
|
+
* Implements the Renderer port interface using React SSR and RSC
|
|
4
|
+
*/
|
|
5
|
+
import type { Renderer } from "../../domain/renderer.ts";
|
|
6
|
+
/**
|
|
7
|
+
* Create a React renderer adapter
|
|
8
|
+
*
|
|
9
|
+
* @returns Renderer implementation for React
|
|
10
|
+
*/
|
|
11
|
+
export declare function createReactRenderer(): Renderer;
|
|
12
|
+
/**
|
|
13
|
+
* Default React renderer instance
|
|
14
|
+
*/
|
|
15
|
+
export declare const reactRenderer: Renderer;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* React Renderer Implementation
|
|
4
|
+
* Implements the Renderer port interface using React SSR and RSC
|
|
5
|
+
*/
|
|
6
|
+
import * as ssrRenderer from "./ssr-renderer.js";
|
|
7
|
+
import * as rscHandler from "./rsc-handler.js";
|
|
8
|
+
import * as renderApp from "./render-app.js";
|
|
9
|
+
import * as inlineRscEmitter from "./inline-rsc-emitter.js";
|
|
10
|
+
import * as logging from "@eserstack/logging";
|
|
11
|
+
const rendererLogger = logging.logger.getLogger([
|
|
12
|
+
"laroux-server",
|
|
13
|
+
"react",
|
|
14
|
+
"renderer",
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* Read the data pages receive as their `request` prop from the request.
|
|
18
|
+
*/
|
|
19
|
+
function readRequestContext(request) {
|
|
20
|
+
return {
|
|
21
|
+
cookieHeader: request.headers.get("cookie"),
|
|
22
|
+
host: request.headers.get("host"),
|
|
23
|
+
localeParam: new URL(request.url).searchParams.get("locale"),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a React renderer adapter
|
|
28
|
+
*
|
|
29
|
+
* @returns Renderer implementation for React
|
|
30
|
+
*/
|
|
31
|
+
export function createReactRenderer() {
|
|
32
|
+
// Cache module map across requests
|
|
33
|
+
let moduleMapCache = null;
|
|
34
|
+
return {
|
|
35
|
+
name: "react",
|
|
36
|
+
async renderPage(Layout, Page, context) {
|
|
37
|
+
rendererLogger.debug(`Rendering page: ${context.pathname}`);
|
|
38
|
+
// Load or use cached module map
|
|
39
|
+
if (moduleMapCache === null) {
|
|
40
|
+
moduleMapCache = await rscHandler.loadModuleMap(context.config);
|
|
41
|
+
}
|
|
42
|
+
// Create the React element tree. Request data travels with this
|
|
43
|
+
// render only, as the request prop of the page and the layout.
|
|
44
|
+
const element = renderApp.renderApp(Layout, Page, {
|
|
45
|
+
pathname: context.pathname,
|
|
46
|
+
params: context.params,
|
|
47
|
+
requestContext: readRequestContext(context.request),
|
|
48
|
+
});
|
|
49
|
+
// Render to HTML with RSC payload
|
|
50
|
+
const result = await ssrRenderer.renderSSR(element, moduleMapCache, {
|
|
51
|
+
streamMode: "await-all",
|
|
52
|
+
});
|
|
53
|
+
// Generate RSC payload script
|
|
54
|
+
const rscPayloadScript = ssrRenderer.generateRSCPayloadScript(result.rscPayload);
|
|
55
|
+
rendererLogger.debug(`Page rendered, HTML length: ${result.html.length}`);
|
|
56
|
+
return {
|
|
57
|
+
html: result.html,
|
|
58
|
+
rscPayload: rscPayloadScript,
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
async renderRSC(Layout, Page, context) {
|
|
62
|
+
rendererLogger.debug(`Rendering RSC: ${context.pathname}`);
|
|
63
|
+
// Load or use cached module map
|
|
64
|
+
if (moduleMapCache === null) {
|
|
65
|
+
moduleMapCache = await rscHandler.loadModuleMap(context.config);
|
|
66
|
+
}
|
|
67
|
+
// Create the React element tree. Request data travels with this
|
|
68
|
+
// render only, as the request prop of the page and the layout.
|
|
69
|
+
const element = renderApp.renderApp(Layout, Page, {
|
|
70
|
+
pathname: context.pathname,
|
|
71
|
+
params: context.params,
|
|
72
|
+
requestContext: readRequestContext(context.request),
|
|
73
|
+
});
|
|
74
|
+
// Render to RSC streaming response
|
|
75
|
+
return await rscHandler.renderRSCResponse(context.config, element, moduleMapCache);
|
|
76
|
+
},
|
|
77
|
+
generateBootstrapScript(_context) {
|
|
78
|
+
// Generate the inline RSC bootstrap script
|
|
79
|
+
return inlineRscEmitter.generateInlineBootstrapScript();
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Default React renderer instance
|
|
85
|
+
*/
|
|
86
|
+
export const reactRenderer = createReactRenderer();
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React Flight Server - RSC Renderer
|
|
3
|
+
*
|
|
4
|
+
* This module implements the server-side RSC renderer that converts
|
|
5
|
+
* React component trees into the RSC wire format for streaming to clients.
|
|
6
|
+
*
|
|
7
|
+
* The RSC wire format is a newline-delimited sequence of chunks:
|
|
8
|
+
* - J chunks: JSON data (elements, props, primitives)
|
|
9
|
+
* - M chunks: Module references (client components)
|
|
10
|
+
* - E chunks: Errors from server rendering
|
|
11
|
+
*
|
|
12
|
+
* Each chunk is assigned an ID, and chunks can reference each other using
|
|
13
|
+
* the $ syntax (e.g., "$42" references chunk ID 42).
|
|
14
|
+
*
|
|
15
|
+
* @see https://github.com/facebook/react/tree/main/packages/react-server-dom-webpack
|
|
16
|
+
*/
|
|
17
|
+
import type { ReactElement } from "react";
|
|
18
|
+
import type { ModuleReference } from "@eserstack/laroux-react/protocol";
|
|
19
|
+
/**
|
|
20
|
+
* The module map of the client bundle: for each client component's module id,
|
|
21
|
+
* the chunks that hold it and its export name. The renderer writes these
|
|
22
|
+
* references into M chunks, so the browser loads a client component from its
|
|
23
|
+
* own chunk.
|
|
24
|
+
*/
|
|
25
|
+
export type BundlerConfig = {
|
|
26
|
+
[moduleId: string]: ModuleReference;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Render a React tree to RSC wire format stream
|
|
30
|
+
*
|
|
31
|
+
* This is the main server-side entry point. It:
|
|
32
|
+
* 1. Creates a ReadableStream for the RSC wire format
|
|
33
|
+
* 2. Starts rendering the React tree (which emits chunks)
|
|
34
|
+
* 3. Waits for all async components to complete
|
|
35
|
+
* 4. Closes the stream when everything is done
|
|
36
|
+
*
|
|
37
|
+
* Chunks are emitted as they're created for true streaming behavior.
|
|
38
|
+
* The stream stays open until all async components (Suspense boundaries) resolve.
|
|
39
|
+
*
|
|
40
|
+
* @param element The root React element (or primitive value, array, etc.) to render
|
|
41
|
+
* @param bundlerConfig Mapping of client component IDs to module references
|
|
42
|
+
* @returns ReadableStream of RSC wire format (newline-delimited chunks)
|
|
43
|
+
*/
|
|
44
|
+
export declare function renderToReadableStream(element: ReactElement | any, bundlerConfig: BundlerConfig): ReadableStream<Uint8Array>;
|
|
45
|
+
/**
|
|
46
|
+
* Render a value to the RSC wire format, as laroux-server sends a server
|
|
47
|
+
* action's result.
|
|
48
|
+
*
|
|
49
|
+
* Chunk 0 holds the value. Unlike client component props, which keep JSON
|
|
50
|
+
* semantics, it is encoded so that Dates, Maps, Sets, BigInts, `undefined`
|
|
51
|
+
* and non-finite numbers arrive as themselves; the React elements in it
|
|
52
|
+
* render like a page tree, and server actions arrive callable.
|
|
53
|
+
* createValueFromReadableStream in `@eserstack/laroux-react/client` decodes
|
|
54
|
+
* it.
|
|
55
|
+
*
|
|
56
|
+
* A value the client cannot receive (a symbol, a promise, a function that is
|
|
57
|
+
* not a server action, or an object that contains itself) is logged, and
|
|
58
|
+
* chunk 0 becomes an error chunk whose message names where it sits.
|
|
59
|
+
*
|
|
60
|
+
* @param value The value to send
|
|
61
|
+
* @param bundlerConfig Mapping of client component IDs to module references
|
|
62
|
+
* @returns ReadableStream of RSC wire format (newline-delimited chunks)
|
|
63
|
+
*/
|
|
64
|
+
export declare function renderValueToReadableStream(value: unknown, bundlerConfig: BundlerConfig): ReadableStream<Uint8Array>;
|