@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,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API route definition from the generated registry
|
|
3
|
+
*/
|
|
4
|
+
export type ApiRouteEntry = {
|
|
5
|
+
path: string;
|
|
6
|
+
modulePath: string;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* API Route Handler
|
|
10
|
+
* Manages loading and executing API routes
|
|
11
|
+
*/
|
|
12
|
+
export declare class ApiRouteHandler {
|
|
13
|
+
private loaded;
|
|
14
|
+
/**
|
|
15
|
+
* Load the API routes of a build from its generated registry,
|
|
16
|
+
* <distDir>/server/api-routes.ts. A build without route.ts files writes no
|
|
17
|
+
* registry, so it has no API routes. Route modules load on their first
|
|
18
|
+
* request, by URLs that carry the build id: after a rebuild, loading the
|
|
19
|
+
* new build makes requests import the rebuilt route modules.
|
|
20
|
+
*
|
|
21
|
+
* @param distDir - Build output directory
|
|
22
|
+
* @param buildId - Id of the build to load
|
|
23
|
+
*/
|
|
24
|
+
loadRoutes(distDir: string, buildId: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Find the first route whose path matches the pathname. API routes use the
|
|
27
|
+
* page router's matcher, so their params are percent-decoded and a
|
|
28
|
+
* catch-all matches its bare prefix the same way.
|
|
29
|
+
*/
|
|
30
|
+
private findRoute;
|
|
31
|
+
/**
|
|
32
|
+
* Handle an API request
|
|
33
|
+
* Returns null if no matching route found
|
|
34
|
+
*/
|
|
35
|
+
handleRequest(req: Request, pathname: string): Promise<Response | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Get allowed methods for a module
|
|
38
|
+
*/
|
|
39
|
+
private getAllowedMethods;
|
|
40
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
// API Route Handler for laroux.js
|
|
3
|
+
// Handles file-based API routes (route.ts files)
|
|
4
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
5
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
6
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
7
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
return path;
|
|
11
|
+
};
|
|
12
|
+
import * as logging from "@eserstack/logging";
|
|
13
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
14
|
+
import * as router from "@eserstack/laroux/router";
|
|
15
|
+
import * as buildModule from "./build-module.js";
|
|
16
|
+
const apiLogger = logging.logger.getLogger(["laroux-server", "api-handler"]);
|
|
17
|
+
/**
|
|
18
|
+
* API Route Handler
|
|
19
|
+
* Manages loading and executing API routes
|
|
20
|
+
*/
|
|
21
|
+
export class ApiRouteHandler {
|
|
22
|
+
loaded = {
|
|
23
|
+
routes: [],
|
|
24
|
+
registryDir: "",
|
|
25
|
+
buildId: "",
|
|
26
|
+
moduleCache: new Map(),
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Load the API routes of a build from its generated registry,
|
|
30
|
+
* <distDir>/server/api-routes.ts. A build without route.ts files writes no
|
|
31
|
+
* registry, so it has no API routes. Route modules load on their first
|
|
32
|
+
* request, by URLs that carry the build id: after a rebuild, loading the
|
|
33
|
+
* new build makes requests import the rebuilt route modules.
|
|
34
|
+
*
|
|
35
|
+
* @param distDir - Build output directory
|
|
36
|
+
* @param buildId - Id of the build to load
|
|
37
|
+
*/
|
|
38
|
+
async loadRoutes(distDir, buildId) {
|
|
39
|
+
const registryDir = runtime.path.resolve(distDir, "server");
|
|
40
|
+
const registry = await buildModule.importOptionalBuildModule(runtime.path.join(registryDir, "api-routes.ts"), buildId);
|
|
41
|
+
const entries = registry?.["apiRoutes"];
|
|
42
|
+
const routes = Array.isArray(entries)
|
|
43
|
+
? entries
|
|
44
|
+
: [];
|
|
45
|
+
// Replaced in one step: a request that started before keeps the build
|
|
46
|
+
// it started with, so it never mixes the modules of two builds.
|
|
47
|
+
this.loaded = { routes, registryDir, buildId, moduleCache: new Map() };
|
|
48
|
+
apiLogger.debug(`Loaded ${routes.length} API route(s)`);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Find the first route whose path matches the pathname. API routes use the
|
|
52
|
+
* page router's matcher, so their params are percent-decoded and a
|
|
53
|
+
* catch-all matches its bare prefix the same way.
|
|
54
|
+
*/
|
|
55
|
+
findRoute(routes, pathname) {
|
|
56
|
+
for (const route of routes) {
|
|
57
|
+
const params = router.matchRoute(pathname, route);
|
|
58
|
+
if (params !== null) {
|
|
59
|
+
return { route, params };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Handle an API request
|
|
66
|
+
* Returns null if no matching route found
|
|
67
|
+
*/
|
|
68
|
+
async handleRequest(req, pathname) {
|
|
69
|
+
const loaded = this.loaded;
|
|
70
|
+
const match = this.findRoute(loaded.routes, pathname);
|
|
71
|
+
if (match === null) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const { route, params } = match;
|
|
75
|
+
const url = new URL(req.url);
|
|
76
|
+
const method = req.method;
|
|
77
|
+
apiLogger.debug(`API route matched: ${method} ${pathname} -> ${route.path}`);
|
|
78
|
+
try {
|
|
79
|
+
// Resolve module path relative to the registry directory
|
|
80
|
+
const resolvedPath = runtime.path.resolve(loaded.registryDir, route.modulePath);
|
|
81
|
+
// Load module (with caching)
|
|
82
|
+
let module = loaded.moduleCache.get(resolvedPath);
|
|
83
|
+
if (module === undefined) {
|
|
84
|
+
module = (await import(__rewriteRelativeImportExtension(buildModule.toBuildModuleUrl(resolvedPath, loaded.buildId))));
|
|
85
|
+
loaded.moduleCache.set(resolvedPath, module);
|
|
86
|
+
}
|
|
87
|
+
// Get handler for this method
|
|
88
|
+
const handler = module[method];
|
|
89
|
+
if (handler === undefined) {
|
|
90
|
+
apiLogger.debug(`Method ${method} not allowed for ${route.path}`);
|
|
91
|
+
return new Response("Method Not Allowed", {
|
|
92
|
+
status: 405,
|
|
93
|
+
headers: {
|
|
94
|
+
Allow: this.getAllowedMethods(module).join(", "),
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Execute handler
|
|
99
|
+
return await handler({
|
|
100
|
+
request: req,
|
|
101
|
+
params,
|
|
102
|
+
searchParams: url.searchParams,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
// Handled here: logged once with the route, and the client gets a
|
|
107
|
+
// fixed message without the error's details.
|
|
108
|
+
await apiLogger
|
|
109
|
+
.with({ route: route.path, method, error })
|
|
110
|
+
.error("API route handler failed to load or threw; answered 500");
|
|
111
|
+
return new Response(JSON.stringify({
|
|
112
|
+
error: "Internal Server Error",
|
|
113
|
+
}), {
|
|
114
|
+
status: 500,
|
|
115
|
+
headers: { "Content-Type": "application/json" },
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get allowed methods for a module
|
|
121
|
+
*/
|
|
122
|
+
getAllowedMethods(module) {
|
|
123
|
+
const methods = [
|
|
124
|
+
"GET",
|
|
125
|
+
"POST",
|
|
126
|
+
"PUT",
|
|
127
|
+
"DELETE",
|
|
128
|
+
"PATCH",
|
|
129
|
+
"HEAD",
|
|
130
|
+
"OPTIONS",
|
|
131
|
+
];
|
|
132
|
+
return methods.filter((method) => module[method] !== undefined);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Server Modules
|
|
3
|
+
*
|
|
4
|
+
* The modules a laroux server loads from its build output: the page routes
|
|
5
|
+
* with the layout and the not-found page, the API routes, the proxies and the
|
|
6
|
+
* server actions. Each one is imported by a URL that carries the build id
|
|
7
|
+
* (see build-module.ts), so loading the next build after a dev rebuild makes
|
|
8
|
+
* every later request use the rebuilt modules without a restart.
|
|
9
|
+
*/
|
|
10
|
+
import * as middlewareDispatcher from "./middleware-dispatcher.ts";
|
|
11
|
+
import * as routeDispatcher from "./route-dispatcher.ts";
|
|
12
|
+
/**
|
|
13
|
+
* Result of loading app components for a given pathname
|
|
14
|
+
*/
|
|
15
|
+
export type AppComponents = {
|
|
16
|
+
Layout: any;
|
|
17
|
+
Page: any;
|
|
18
|
+
params: Record<string, string | string[]>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The server modules of one app. A server keeps one instance, so servers for
|
|
22
|
+
* different apps in one process never share routes.
|
|
23
|
+
*/
|
|
24
|
+
export declare class ServerModules {
|
|
25
|
+
#private;
|
|
26
|
+
/** The API routes of the loaded build */
|
|
27
|
+
readonly apiRoutes: routeDispatcher.ApiRouteHandler;
|
|
28
|
+
/** The proxies of the loaded build */
|
|
29
|
+
readonly proxies: middlewareDispatcher.MiddlewareDispatcher;
|
|
30
|
+
/**
|
|
31
|
+
* Create the server modules of the app built into distDir. Nothing loads
|
|
32
|
+
* until {@link load} is called.
|
|
33
|
+
*
|
|
34
|
+
* @param distDir - Absolute path of the build output directory
|
|
35
|
+
*/
|
|
36
|
+
constructor(distDir: string);
|
|
37
|
+
/**
|
|
38
|
+
* Load a build: its page routes, which import every page module, the
|
|
39
|
+
* layout, the server action modules, the API route registry and the proxy
|
|
40
|
+
* registry. API route and proxy modules load on their first request.
|
|
41
|
+
* Requests that start after this resolves use the new build; when the page
|
|
42
|
+
* routes or the layout fail to load, every module stays on the build loaded
|
|
43
|
+
* before. When the API route or proxy registry fails to load after that,
|
|
44
|
+
* load rejects and the pages stay on the build loaded before, while a
|
|
45
|
+
* registry that did load already serves the new build.
|
|
46
|
+
*
|
|
47
|
+
* @param buildId - Id of the build in distDir, from its chunk manifest or
|
|
48
|
+
* its BuildResult
|
|
49
|
+
*/
|
|
50
|
+
load(buildId: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Id of the loaded build. Server action modules are imported with it.
|
|
53
|
+
*
|
|
54
|
+
* @throws {Error} When no build has been loaded yet
|
|
55
|
+
*/
|
|
56
|
+
loadedBuildId(): string;
|
|
57
|
+
/**
|
|
58
|
+
* Load the layout and the page for a pathname from the loaded build; a
|
|
59
|
+
* pathname no route matches gets the not-found page.
|
|
60
|
+
*
|
|
61
|
+
* @param pathname - Request pathname, such as "/stories/hello"
|
|
62
|
+
* @throws {Error} When no build has been loaded yet
|
|
63
|
+
*/
|
|
64
|
+
getApp(pathname: string): Promise<AppComponents>;
|
|
65
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Server Modules
|
|
4
|
+
*
|
|
5
|
+
* The modules a laroux server loads from its build output: the page routes
|
|
6
|
+
* with the layout and the not-found page, the API routes, the proxies and the
|
|
7
|
+
* server actions. Each one is imported by a URL that carries the build id
|
|
8
|
+
* (see build-module.ts), so loading the next build after a dev rebuild makes
|
|
9
|
+
* every later request use the rebuilt modules without a restart.
|
|
10
|
+
*/
|
|
11
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
12
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
13
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
14
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return path;
|
|
18
|
+
};
|
|
19
|
+
import * as logging from "@eserstack/logging";
|
|
20
|
+
import { runtime } from "@eserstack/standards/cross-runtime";
|
|
21
|
+
import * as patterns from "@eserstack/standards/patterns";
|
|
22
|
+
import * as router from "@eserstack/laroux/router";
|
|
23
|
+
import * as buildModule from "./build-module.js";
|
|
24
|
+
import * as middlewareDispatcher from "./middleware-dispatcher.js";
|
|
25
|
+
import * as routeDispatcher from "./route-dispatcher.js";
|
|
26
|
+
const modulesLogger = logging.logger.getLogger([
|
|
27
|
+
"laroux-server",
|
|
28
|
+
"server-modules",
|
|
29
|
+
]);
|
|
30
|
+
/** Directory under distDir that holds the server output of a build. */
|
|
31
|
+
const SERVER_DIR = "server";
|
|
32
|
+
/**
|
|
33
|
+
* The server modules of one app. A server keeps one instance, so servers for
|
|
34
|
+
* different apps in one process never share routes.
|
|
35
|
+
*/
|
|
36
|
+
export class ServerModules {
|
|
37
|
+
/** The API routes of the loaded build */
|
|
38
|
+
apiRoutes = new routeDispatcher.ApiRouteHandler();
|
|
39
|
+
/** The proxies of the loaded build */
|
|
40
|
+
proxies = new middlewareDispatcher.MiddlewareDispatcher();
|
|
41
|
+
#distDir;
|
|
42
|
+
#loaded = null;
|
|
43
|
+
/**
|
|
44
|
+
* Create the server modules of the app built into distDir. Nothing loads
|
|
45
|
+
* until {@link load} is called.
|
|
46
|
+
*
|
|
47
|
+
* @param distDir - Absolute path of the build output directory
|
|
48
|
+
*/
|
|
49
|
+
constructor(distDir) {
|
|
50
|
+
this.#distDir = distDir;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Load a build: its page routes, which import every page module, the
|
|
54
|
+
* layout, the server action modules, the API route registry and the proxy
|
|
55
|
+
* registry. API route and proxy modules load on their first request.
|
|
56
|
+
* Requests that start after this resolves use the new build; when the page
|
|
57
|
+
* routes or the layout fail to load, every module stays on the build loaded
|
|
58
|
+
* before. When the API route or proxy registry fails to load after that,
|
|
59
|
+
* load rejects and the pages stay on the build loaded before, while a
|
|
60
|
+
* registry that did load already serves the new build.
|
|
61
|
+
*
|
|
62
|
+
* @param buildId - Id of the build in distDir, from its chunk manifest or
|
|
63
|
+
* its BuildResult
|
|
64
|
+
*/
|
|
65
|
+
async load(buildId) {
|
|
66
|
+
// Loaded first, so a page or layout that fails to import leaves the
|
|
67
|
+
// server on the previous build
|
|
68
|
+
const [pageRoutes, layoutModule] = await Promise.all([
|
|
69
|
+
loadPageRoutes(this.#distDir, buildId),
|
|
70
|
+
importServerComponent(this.#distDir, "layout", buildId),
|
|
71
|
+
]);
|
|
72
|
+
await Promise.all([
|
|
73
|
+
this.apiRoutes.loadRoutes(this.#distDir, buildId),
|
|
74
|
+
this.proxies.loadProxies(this.#distDir, buildId),
|
|
75
|
+
loadServerActions(this.#distDir, buildId),
|
|
76
|
+
]);
|
|
77
|
+
this.#loaded = { buildId, pageRoutes, layoutModule };
|
|
78
|
+
modulesLogger.debug(`Loaded build ${buildId}: ${pageRoutes.length} page route(s)`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Id of the loaded build. Server action modules are imported with it.
|
|
82
|
+
*
|
|
83
|
+
* @throws {Error} When no build has been loaded yet
|
|
84
|
+
*/
|
|
85
|
+
loadedBuildId() {
|
|
86
|
+
return this.#requireLoaded("loadedBuildId").buildId;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Load the layout and the page for a pathname from the loaded build; a
|
|
90
|
+
* pathname no route matches gets the not-found page.
|
|
91
|
+
*
|
|
92
|
+
* @param pathname - Request pathname, such as "/stories/hello"
|
|
93
|
+
* @throws {Error} When no build has been loaded yet
|
|
94
|
+
*/
|
|
95
|
+
async getApp(pathname) {
|
|
96
|
+
const { buildId, pageRoutes, layoutModule } = this.#requireLoaded("getApp");
|
|
97
|
+
const match = router.findMatchingRoute(pathname, pageRoutes);
|
|
98
|
+
if (match === null) {
|
|
99
|
+
const notFoundModule = await importServerComponent(this.#distDir, "not-found", buildId);
|
|
100
|
+
return {
|
|
101
|
+
Layout: layoutModule["Layout"],
|
|
102
|
+
Page: notFoundModule["NotFound"],
|
|
103
|
+
params: {},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
Layout: layoutModule["Layout"],
|
|
108
|
+
Page: match.route.component,
|
|
109
|
+
params: match.params,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
#requireLoaded(operation) {
|
|
113
|
+
if (this.#loaded === null) {
|
|
114
|
+
throw new Error(`ServerModules.${operation}: no build of ${this.#distDir} is loaded; call load(buildId) first`);
|
|
115
|
+
}
|
|
116
|
+
return this.#loaded;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Import the page routes the build generated into
|
|
121
|
+
* <distDir>/server/_generated-routes.ts. The file imports the page modules
|
|
122
|
+
* with its own build id, so a new build id loads the rebuilt pages.
|
|
123
|
+
*/
|
|
124
|
+
async function loadPageRoutes(distDir, buildId) {
|
|
125
|
+
const routesPath = runtime.path.join(distDir, SERVER_DIR, "_generated-routes.ts");
|
|
126
|
+
const routesModule = await import(__rewriteRelativeImportExtension(buildModule.toBuildModuleUrl(routesPath, buildId)));
|
|
127
|
+
const generatedRoutes = routesModule["generatedRoutes"];
|
|
128
|
+
if (!Array.isArray(generatedRoutes)) {
|
|
129
|
+
throw new Error(`load page routes from ${routesPath}: the file exports no generatedRoutes array; rebuild the app`);
|
|
130
|
+
}
|
|
131
|
+
return generatedRoutes;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Import the server action modules the build lists in
|
|
135
|
+
* <distDir>/server/actions-manifest.json. An action module that fails to
|
|
136
|
+
* import is logged and skipped, so the other actions still load.
|
|
137
|
+
*/
|
|
138
|
+
async function loadServerActions(distDir, buildId) {
|
|
139
|
+
const manifestPath = runtime.path.resolve(distDir, SERVER_DIR, "actions-manifest.json");
|
|
140
|
+
if (!(await runtime.fs.exists(manifestPath))) {
|
|
141
|
+
modulesLogger.debug("No actions manifest found (optional)");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const manifest = JSON.parse(await runtime.fs.readTextFile(manifestPath));
|
|
145
|
+
let loadedCount = 0;
|
|
146
|
+
for (const actionPath of manifest.actions) {
|
|
147
|
+
// Action paths keep the source layout, such as "src/app/actions.js",
|
|
148
|
+
// under dist/server
|
|
149
|
+
const fullPath = runtime.path.resolve(distDir, SERVER_DIR, actionPath);
|
|
150
|
+
try {
|
|
151
|
+
// Action modules register themselves on import, so they load one at a
|
|
152
|
+
// time to keep the registration order equal to the manifest order.
|
|
153
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
154
|
+
const loaded = await buildModule.importOptionalBuildModule(fullPath, buildId);
|
|
155
|
+
if (loaded !== null) {
|
|
156
|
+
loadedCount++;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
// Logged before the next module loads, so the entries keep the
|
|
161
|
+
// manifest order of the imports above.
|
|
162
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
163
|
+
await modulesLogger
|
|
164
|
+
.with({ actionPath, buildId, error })
|
|
165
|
+
.warn("load server action module: importing it failed, so its actions answer 500 until it is fixed");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (loadedCount > 0) {
|
|
169
|
+
modulesLogger.debug(`Server actions loaded: ${loadedCount} file(s)`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Import a server component of the app root, such as the layout. The bundled
|
|
174
|
+
* file at dist/server/src/app/<baseName>.js is preferred; a source file next
|
|
175
|
+
* to it is the fallback.
|
|
176
|
+
*/
|
|
177
|
+
async function importServerComponent(distDir, baseName, buildId) {
|
|
178
|
+
const componentPath = await resolveServerComponentPath(distDir, baseName);
|
|
179
|
+
return await import(__rewriteRelativeImportExtension(buildModule.toBuildModuleUrl(componentPath, buildId)));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Resolve server component import path.
|
|
183
|
+
* Bundled .js files are at dist/server/src/app/*.js (both bundlers output here).
|
|
184
|
+
* Falls back to source files for compatibility.
|
|
185
|
+
*
|
|
186
|
+
* @param distDir - Build output directory
|
|
187
|
+
* @param baseName - Base file name without extension (e.g., "layout", "not-found")
|
|
188
|
+
* @returns Resolved path to the component file
|
|
189
|
+
*/
|
|
190
|
+
async function resolveServerComponentPath(distDir, baseName) {
|
|
191
|
+
const appDir = runtime.path.resolve(distDir, SERVER_DIR, "src", "app");
|
|
192
|
+
// Try bundled .js file in src/app (both bundlers output here)
|
|
193
|
+
const bundledPath = runtime.path.join(appDir, `${baseName}.js`);
|
|
194
|
+
if (await runtime.fs.exists(bundledPath)) {
|
|
195
|
+
return bundledPath;
|
|
196
|
+
}
|
|
197
|
+
// Fall back to source file - try each extension
|
|
198
|
+
for (const ext of patterns.JS_FILE_EXTENSIONS) {
|
|
199
|
+
const sourcePath = runtime.path.join(appDir, `${baseName}.${ext}`);
|
|
200
|
+
// Extensions are tried in preference order and the first match wins.
|
|
201
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
202
|
+
if (await runtime.fs.exists(sourcePath)) {
|
|
203
|
+
return sourcePath;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// Return bundled path as default (will fail with clear error)
|
|
207
|
+
return bundledPath;
|
|
208
|
+
}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Laroux Server
|
|
3
|
+
* Server orchestration for React Server Components
|
|
4
|
+
*
|
|
5
|
+
* The build runs in @eserstack/laroux-bundler; the CLI commands are in
|
|
6
|
+
* ./commands and mount under `eser laroux` through ./module.ts.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import type { ServerOptions } from "./options.ts";
|
|
11
|
+
export type { ServerOptions } from "./options.ts";
|
|
12
|
+
export { VALID_LOG_LEVELS } from "./options.ts";
|
|
13
|
+
export type { AppComponents } from "./domain/server-modules.ts";
|
|
14
|
+
export type { RenderContext, Renderer, RenderResult, } from "./domain/renderer.ts";
|
|
15
|
+
export type { HtmlShellBuilder, HtmlShellOptions, } from "./domain/html-shell.ts";
|
|
16
|
+
export type { BundleData, Bundler, ChunkManifest, ClientComponent, ClientComponentCache, ClientComponentCacheEntry, ComponentChunkInfo, CriticalCssResult, CssPlugin, CssPluginContext, CssProcessor, CssProcessorOptions, FileInfo, FontOptimizationOptions, FontOptimizer, FrameworkPlugin, ImageManifest, ImageOptimizationRequest, ImageOptimizer, ImageVariant, ModuleMap, ModuleMapEntry, OptimizedFonts, OptimizedImage, TransformResult, UniversalCssResult, } from "@eserstack/laroux-bundler";
|
|
17
|
+
/**
|
|
18
|
+
* Thrown by startServer when the build it runs before serving fails, so dev
|
|
19
|
+
* and serve never start on a stale or empty dist/. The bundler logs the
|
|
20
|
+
* failing step and its error before this is thrown.
|
|
21
|
+
*/
|
|
22
|
+
export declare class BuildFailedError extends Error {
|
|
23
|
+
/** Root of the app whose build failed */
|
|
24
|
+
readonly projectRoot: string;
|
|
25
|
+
/**
|
|
26
|
+
* Create the error for the app at projectRoot.
|
|
27
|
+
*
|
|
28
|
+
* @param projectRoot - Root of the app whose build failed
|
|
29
|
+
* @param options - Standard error options, such as a `cause`
|
|
30
|
+
*/
|
|
31
|
+
constructor(projectRoot: string, options?: ErrorOptions);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Start the development or production server
|
|
35
|
+
*
|
|
36
|
+
* @param options - Server options (with optional plugin injection)
|
|
37
|
+
* @throws {BuildFailedError} When the build that runs before serving fails;
|
|
38
|
+
* the server is not started.
|
|
39
|
+
*/
|
|
40
|
+
export declare function startServer(options: ServerOptions): Promise<void>;
|