@eserstack/laroux-server 0.0.0-stage → 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 +410 -2
- 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 +81 -3
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* RSC Renderer with Streaming
|
|
4
|
+
* Renders React Server Components to the RSC wire protocol format
|
|
5
|
+
*/
|
|
6
|
+
import * as rscFlightRenderer from "./rsc-flight-renderer.js";
|
|
7
|
+
import * as crossRuntime from "@eserstack/standards/cross-runtime";
|
|
8
|
+
import * as logging from "@eserstack/logging";
|
|
9
|
+
const rscRenderer = logging.logger.getLogger([
|
|
10
|
+
"laroux-server",
|
|
11
|
+
"react",
|
|
12
|
+
"rsc-handler",
|
|
13
|
+
]);
|
|
14
|
+
// Constants
|
|
15
|
+
const MODULE_MAP_FILENAME = "module-map.json";
|
|
16
|
+
/**
|
|
17
|
+
* Load the module map generated during build
|
|
18
|
+
* @param config - Application configuration
|
|
19
|
+
* @returns Bundler configuration with component mappings
|
|
20
|
+
*/
|
|
21
|
+
export async function loadModuleMap(config) {
|
|
22
|
+
try {
|
|
23
|
+
const moduleMapPath = crossRuntime.runtime.path.resolve(config.distDir, "client", MODULE_MAP_FILENAME);
|
|
24
|
+
const content = await crossRuntime.runtime.fs.readTextFile(moduleMapPath);
|
|
25
|
+
const map = JSON.parse(content);
|
|
26
|
+
rscRenderer.debug(`Loaded module map with ${Object.keys(map).length} entries`);
|
|
27
|
+
return map;
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
rscRenderer.warn("Module map not found, using empty map");
|
|
31
|
+
if (error instanceof crossRuntime.NotFoundError) {
|
|
32
|
+
rscRenderer.warn(" Run `laroux build` to generate the module map");
|
|
33
|
+
}
|
|
34
|
+
return {};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Render a React Server Component tree to RSC wire format stream
|
|
39
|
+
*
|
|
40
|
+
* This implements the React Server Components protocol:
|
|
41
|
+
* - Server components render on the server and stream as JSON
|
|
42
|
+
* - Client components are sent as references (not code)
|
|
43
|
+
* - Uses RSC wire format: J0: (JSON), M1: (Module reference), etc.
|
|
44
|
+
* - Supports progressive rendering with Suspense
|
|
45
|
+
*
|
|
46
|
+
* @param config - Application configuration
|
|
47
|
+
* @param component - Root React element to render
|
|
48
|
+
* @param moduleMap - Optional pre-loaded module map (for current mode)
|
|
49
|
+
* @returns ReadableStream of RSC wire format chunks
|
|
50
|
+
*/
|
|
51
|
+
export async function renderRSC(config, component, moduleMap) {
|
|
52
|
+
try {
|
|
53
|
+
// Use provided module map or load from disk
|
|
54
|
+
const bundlerConfig = moduleMap ?? (await loadModuleMap(config));
|
|
55
|
+
// Debug: log module map
|
|
56
|
+
rscRenderer.debug("Module map keys:", {
|
|
57
|
+
keys: Object.keys(bundlerConfig),
|
|
58
|
+
});
|
|
59
|
+
rscRenderer.debug("Module map:", { moduleMap: bundlerConfig });
|
|
60
|
+
// Render to RSC wire format stream
|
|
61
|
+
// renderToReadableStream now returns immediately (not async)
|
|
62
|
+
const stream = rscFlightRenderer.renderToReadableStream(component, bundlerConfig);
|
|
63
|
+
rscRenderer.debug("RSC stream created");
|
|
64
|
+
return stream;
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
rscRenderer
|
|
68
|
+
.with({ error })
|
|
69
|
+
.error("render RSC: start the stream; sending an error stream instead");
|
|
70
|
+
// Return error stream in RSC format
|
|
71
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
72
|
+
const errorStream = new ReadableStream({
|
|
73
|
+
start(controller) {
|
|
74
|
+
// Error chunk: E0:{"message":"..."}
|
|
75
|
+
const errorChunk = `E0:${JSON.stringify({ message: errorMessage })}\n`;
|
|
76
|
+
controller.enqueue(new TextEncoder().encode(errorChunk));
|
|
77
|
+
controller.close();
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
return errorStream;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Convert RSC stream to HTTP Response with proper headers
|
|
85
|
+
*
|
|
86
|
+
* The response carries no CORS headers: the laroux client fetches `/rsc` from
|
|
87
|
+
* its own origin. A deployment that serves the payload to another origin adds
|
|
88
|
+
* an allowlist for that origin itself.
|
|
89
|
+
*
|
|
90
|
+
* @param stream - RSC wire format stream
|
|
91
|
+
* @returns HTTP Response ready to send to client
|
|
92
|
+
*/
|
|
93
|
+
export function streamToResponse(stream) {
|
|
94
|
+
// Return the stream directly - the runtime handles Transfer-Encoding: chunked automatically
|
|
95
|
+
// Any intermediate piping or TransformStream can cause buffering issues
|
|
96
|
+
return new Response(stream, {
|
|
97
|
+
headers: {
|
|
98
|
+
"Content-Type": "text/x-component; charset=utf-8",
|
|
99
|
+
"Cache-Control": "no-cache, no-store, must-revalidate",
|
|
100
|
+
"X-Content-Type-Options": "nosniff",
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Render RSC and return as HTTP Response (convenience method)
|
|
106
|
+
* @param config - Application configuration
|
|
107
|
+
* @param component - Root React element to render
|
|
108
|
+
* @param moduleMap - Optional pre-loaded module map (for current mode)
|
|
109
|
+
* @returns HTTP Response with RSC stream
|
|
110
|
+
*/
|
|
111
|
+
export async function renderRSCResponse(config, component, moduleMap) {
|
|
112
|
+
const stream = await renderRSC(config, component, moduleMap);
|
|
113
|
+
return streamToResponse(stream);
|
|
114
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escapes serialized JSON for embedding inside an HTML <script> element.
|
|
3
|
+
*
|
|
4
|
+
* `<`, `>` and `&` become `<`, `>` and `&`, so no value can
|
|
5
|
+
* close the element (`</script`) or open an HTML comment (`<!--`) that
|
|
6
|
+
* changes how the rest of the script is parsed. U+2028 and U+2029 become
|
|
7
|
+
* escapes because they end a line in older JavaScript parsers. JSON only
|
|
8
|
+
* contains these characters inside string literals, where the escapes decode
|
|
9
|
+
* back to the same string, so the result is still valid JSON and a valid
|
|
10
|
+
* JavaScript expression.
|
|
11
|
+
*/
|
|
12
|
+
export declare const escapeJsonForScript: (json: string) => string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Escapes serialized JSON for embedding inside an HTML <script> element.
|
|
4
|
+
*
|
|
5
|
+
* `<`, `>` and `&` become `<`, `>` and `&`, so no value can
|
|
6
|
+
* close the element (`</script`) or open an HTML comment (`<!--`) that
|
|
7
|
+
* changes how the rest of the script is parsed. U+2028 and U+2029 become
|
|
8
|
+
* escapes because they end a line in older JavaScript parsers. JSON only
|
|
9
|
+
* contains these characters inside string literals, where the escapes decode
|
|
10
|
+
* back to the same string, so the result is still valid JSON and a valid
|
|
11
|
+
* JavaScript expression.
|
|
12
|
+
*/
|
|
13
|
+
export const escapeJsonForScript = (json) => json
|
|
14
|
+
.replace(/</g, "\\u003c")
|
|
15
|
+
.replace(/>/g, "\\u003e")
|
|
16
|
+
.replace(/&/g, "\\u0026")
|
|
17
|
+
.replace(/\u2028/g, "\\u2028")
|
|
18
|
+
.replace(/\u2029/g, "\\u2029");
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server-Side Rendering (SSR) Renderer
|
|
3
|
+
*
|
|
4
|
+
* This module renders React Server Components to HTML for initial page load.
|
|
5
|
+
* It generates both:
|
|
6
|
+
* 1. HTML markup for immediate display
|
|
7
|
+
* 2. RSC payload for client hydration
|
|
8
|
+
*
|
|
9
|
+
* Key concepts:
|
|
10
|
+
* - Server components are rendered to HTML directly
|
|
11
|
+
* - Client components render as placeholders with data attributes
|
|
12
|
+
* - RSC payload is embedded in the HTML for hydration without refetch
|
|
13
|
+
*/
|
|
14
|
+
import type { RSCChunk } from "@eserstack/laroux-react/protocol";
|
|
15
|
+
import type { BundlerConfig } from "./rsc-flight-renderer.ts";
|
|
16
|
+
/**
|
|
17
|
+
* SSR rendering options
|
|
18
|
+
*/
|
|
19
|
+
export type SSROptions = {
|
|
20
|
+
/** Whether to stream HTML or await all content */
|
|
21
|
+
streamMode: "streaming-classic" | "await-all";
|
|
22
|
+
/** Timeout for async components (ms) */
|
|
23
|
+
timeout?: number;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* SSR rendering result
|
|
27
|
+
*/
|
|
28
|
+
export type SSRResult = {
|
|
29
|
+
/** Server-rendered HTML content */
|
|
30
|
+
html: string;
|
|
31
|
+
/** RSC payload chunks for hydration */
|
|
32
|
+
rscPayload: RSCChunk[];
|
|
33
|
+
/** Client component modules that need to be loaded */
|
|
34
|
+
clientModules: string[];
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Render a React element tree to HTML with RSC payload
|
|
38
|
+
*
|
|
39
|
+
* @param element - The root React element to render
|
|
40
|
+
* @param bundlerConfig - Module map for client component resolution
|
|
41
|
+
* @param options - SSR rendering options
|
|
42
|
+
* @returns SSR result with HTML, RSC payload, and client modules
|
|
43
|
+
*/
|
|
44
|
+
export declare function renderSSR(element: any, bundlerConfig: BundlerConfig, options?: SSROptions): Promise<SSRResult>;
|
|
45
|
+
/**
|
|
46
|
+
* What a visitor sees when the page cannot be rendered at all. The error
|
|
47
|
+
* stays in the server log, under the correlation id shown next to this text.
|
|
48
|
+
*/
|
|
49
|
+
export declare const SSR_ERROR_MESSAGE = "This page could not be rendered. Reload it, or report this error id:";
|
|
50
|
+
/**
|
|
51
|
+
* Serialize RSC payload for embedding in HTML
|
|
52
|
+
*
|
|
53
|
+
* @param chunks - RSC chunks to serialize
|
|
54
|
+
* @returns JSON string safe for embedding in script tag
|
|
55
|
+
*/
|
|
56
|
+
export declare function serializeRSCPayload(chunks: RSCChunk[]): string;
|
|
57
|
+
/**
|
|
58
|
+
* Generate RSC payload script tag
|
|
59
|
+
*
|
|
60
|
+
* @param chunks - RSC chunks to embed
|
|
61
|
+
* @returns HTML script tag with serialized payload
|
|
62
|
+
*/
|
|
63
|
+
export declare function generateRSCPayloadScript(chunks: RSCChunk[]): string;
|