@getstrata/bootstrap 2.2.60 → 3.0.5
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/CHANGELOG.md +35 -0
- package/dist/bootstrap/app.d.ts +18 -7
- package/dist/bootstrap/buildModuleRoutes.d.ts +3 -5
- package/dist/bootstrap/buildWebModuleRoutes.d.ts +2 -3
- package/dist/bootstrap/cache/modelCacheTags.d.ts +3 -2
- package/dist/bootstrap/cachedJson.d.ts +7 -2
- package/dist/bootstrap/config.d.ts +1 -1
- package/dist/bootstrap/context.d.ts +25 -7
- package/dist/bootstrap/contracts.d.ts +1 -0
- package/dist/bootstrap/createWebRoutes.d.ts +2 -1
- package/dist/bootstrap/databaseReadiness.d.ts +3 -0
- package/dist/bootstrap/dependencies.d.ts +3 -2
- package/dist/bootstrap/discoverJobs.d.ts +3 -2
- package/dist/bootstrap/discoverListeners.d.ts +10 -7
- package/dist/bootstrap/discoverModels.d.ts +3 -2
- package/dist/bootstrap/discoverModules.d.ts +7 -6
- package/dist/bootstrap/health.d.ts +3 -2
- package/dist/bootstrap/http/securedRouteModelBinding.d.ts +1 -1
- package/dist/bootstrap/httpKernel.d.ts +43 -22
- package/dist/bootstrap/httpPersistence.d.ts +4 -0
- package/dist/bootstrap/inProcessCron.d.ts +5 -5
- package/dist/bootstrap/listeners/invalidateCacheOnModelWrite.d.ts +13 -12
- package/dist/bootstrap/metricsRoutes.d.ts +6 -1
- package/dist/bootstrap/moduleRouteMiddleware.d.ts +4 -4
- package/dist/bootstrap/modules.d.ts +1 -1
- package/dist/bootstrap/ownedHttpRoute.d.ts +23 -0
- package/dist/bootstrap/persistenceContracts.d.ts +70 -0
- package/dist/bootstrap/persistenceInvocation.d.ts +4 -0
- package/dist/bootstrap/providers/auth.d.ts +21 -0
- package/dist/bootstrap/providers/cache.d.ts +9 -3
- package/dist/bootstrap/providers/cacheProvider.d.ts +3 -0
- package/dist/bootstrap/providers/config.d.ts +6 -3
- package/dist/bootstrap/providers/events.d.ts +5 -2
- package/dist/bootstrap/providers/index.d.ts +3 -2
- package/dist/bootstrap/providers/listeners.d.ts +3 -3
- package/dist/bootstrap/providers/mail.d.ts +15 -0
- package/dist/bootstrap/providers/queue.d.ts +3 -2
- package/dist/bootstrap/providers/storage.d.ts +11 -2
- package/dist/bootstrap/providers/tracing.d.ts +4 -2
- package/dist/bootstrap/providers/view.d.ts +8 -5
- package/dist/bootstrap/public-api.d.ts +10 -7
- package/dist/bootstrap/queue/defaultJobs.d.ts +4 -2
- package/dist/bootstrap/rateLimit.d.ts +13 -3
- package/dist/bootstrap/routeCompiler.d.ts +20 -0
- package/dist/bootstrap/routeMetadata.d.ts +3 -3
- package/dist/bootstrap/routePlan.d.ts +11 -0
- package/dist/bootstrap/schedule.d.ts +11 -2
- package/dist/bootstrap/secretsGuard.d.ts +2 -3
- package/dist/bootstrap/web/routing.d.ts +6 -3
- package/dist/bootstrap/web/server.d.ts +14 -4
- package/dist/bootstrap/web/session.d.ts +23 -16
- package/dist/entries/buildModuleRoutes.js +602 -384
- package/dist/entries/buildWebModuleRoutes.js +596 -347
- package/dist/entries/cache/modelCacheTags.js +8 -186
- package/dist/entries/cachedJson.js +22 -0
- package/dist/entries/config.js +1 -3
- package/dist/entries/context.js +665 -700
- package/dist/entries/createSpaRoutes.js +194 -5
- package/dist/entries/createWebRoutes.js +618 -367
- package/dist/entries/databaseReadiness.js +267 -0
- package/dist/entries/dependencies.js +666 -701
- package/dist/entries/discoverJobs.js +18 -16
- package/dist/entries/discoverListeners.js +27 -38
- package/dist/entries/discoverModels.js +4 -3
- package/dist/entries/discoverModules.js +29 -67
- package/dist/entries/health.js +14 -34
- package/dist/entries/httpKernel.js +405 -89
- package/dist/entries/listeners/invalidateCacheOnModelWrite.js +43 -289
- package/dist/entries/metricsRoutes.js +9 -5
- package/dist/entries/persistenceContracts.js +1 -0
- package/dist/entries/persistenceInvocation.js +17 -0
- package/dist/entries/providers/auth.js +114 -0
- package/dist/entries/providers/cache.js +35 -0
- package/dist/entries/providers/events.js +55 -0
- package/dist/entries/providers/view.js +34 -44
- package/dist/entries/providers.js +392 -561
- package/dist/entries/queue/defaultJobs.js +9 -6
- package/dist/entries/routePlan.js +200 -0
- package/dist/entries/schedule.js +37 -37
- package/dist/entries/secretsGuard.js +10 -13
- package/dist/entries/web/routing.js +412 -96
- package/dist/entries/web/server.js +461 -29
- package/dist/entries/web/session.js +61 -73
- package/dist/index.js +1520 -1085
- package/dist/internal/ownedHttpRoute.js +66 -0
- package/package.json +52 -15
- package/dist/bootstrap/applicationRegistry.d.ts +0 -1
- package/dist/bootstrap/routeRegistry.d.ts +0 -10
- package/dist/entries/applicationRegistry.js +0 -24
- package/dist/entries/routeRegistry.js +0 -29
|
@@ -4,6 +4,192 @@ import { join } from "path";
|
|
|
4
4
|
import { jsonResponse } from "@getstrata/core/http/response";
|
|
5
5
|
import { isSpaEnabled, isViewsEnabled, readSpaPrefix } from "@getstrata/core/runtime/frontendMode";
|
|
6
6
|
import { assertUrlPathUnderRoot } from "@getstrata/core/security/safePath";
|
|
7
|
+
|
|
8
|
+
// ../../src/bootstrap/routeCompiler.ts
|
|
9
|
+
var ROUTE_METHODS = ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"];
|
|
10
|
+
var OPENAPI_METHODS = new Set([...ROUTE_METHODS, "TRACE"]);
|
|
11
|
+
var inventoryKey = Symbol.for("@getstrata/owned-route-inventory");
|
|
12
|
+
function readRouteInventory(routes) {
|
|
13
|
+
return Object.getOwnPropertyDescriptor(routes, inventoryKey)?.value;
|
|
14
|
+
}
|
|
15
|
+
function immutableCopy(value, ancestors = new Set) {
|
|
16
|
+
if (value === null || typeof value !== "object") {
|
|
17
|
+
if (typeof value === "function" || typeof value === "symbol" || typeof value === "bigint" || typeof value === "number" && !Number.isFinite(value)) {
|
|
18
|
+
throw new Error("Route metadata must contain only JSON values");
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
if (ancestors.has(value))
|
|
23
|
+
throw new Error("Route metadata must not contain cycles");
|
|
24
|
+
if (!Array.isArray(value) && Object.getPrototypeOf(value) !== Object.prototype && Object.getPrototypeOf(value) !== null) {
|
|
25
|
+
throw new Error("Route metadata must contain only JSON objects");
|
|
26
|
+
}
|
|
27
|
+
ancestors.add(value);
|
|
28
|
+
const copy = Array.isArray(value) ? value.map((entry) => immutableCopy(entry, ancestors)) : Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, immutableCopy(entry, ancestors)]));
|
|
29
|
+
ancestors.delete(value);
|
|
30
|
+
return Object.freeze(copy);
|
|
31
|
+
}
|
|
32
|
+
function normalizeRoutePath(path, prefix = "") {
|
|
33
|
+
const normalizedPrefix = prefix.replace(/\/+$/, "");
|
|
34
|
+
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
35
|
+
const result = `${normalizedPrefix ? normalizedPrefix.startsWith("/") ? normalizedPrefix : `/${normalizedPrefix}` : ""}${normalizedPath}`;
|
|
36
|
+
if (!path || /[?#\s]/.test(result) || result.includes("//") || result.split("/").some((part) => part === "." || part === "..")) {
|
|
37
|
+
throw new Error(`Invalid route path: ${path}`);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
function routeMethods(handler) {
|
|
42
|
+
if (handler === false)
|
|
43
|
+
return { methods: [], handler };
|
|
44
|
+
if (handler instanceof Response)
|
|
45
|
+
return {
|
|
46
|
+
methods: [...ROUTE_METHODS],
|
|
47
|
+
handler: Object.freeze(Object.fromEntries(ROUTE_METHODS.map((method) => [method, handler])))
|
|
48
|
+
};
|
|
49
|
+
if (typeof handler === "function")
|
|
50
|
+
return { methods: ["*"], handler };
|
|
51
|
+
if (handler && typeof handler === "object" && !Array.isArray(handler)) {
|
|
52
|
+
const entries = Object.entries(handler);
|
|
53
|
+
if (entries.some(([method, value]) => OPENAPI_METHODS.has(method.toUpperCase()) || typeof value === "function" || value instanceof Response)) {
|
|
54
|
+
const methods = {};
|
|
55
|
+
for (const [rawMethod, value] of entries) {
|
|
56
|
+
const method = rawMethod.toUpperCase();
|
|
57
|
+
if (!ROUTE_METHODS.includes(method)) {
|
|
58
|
+
throw new Error(`Unsupported route method: ${rawMethod}`);
|
|
59
|
+
}
|
|
60
|
+
if (Object.hasOwn(methods, method))
|
|
61
|
+
throw new Error(`Duplicate route method: ${method}`);
|
|
62
|
+
if (typeof value !== "function" && !(value instanceof Response)) {
|
|
63
|
+
throw new Error(`Invalid route handler: ${method}`);
|
|
64
|
+
}
|
|
65
|
+
methods[method] = value;
|
|
66
|
+
}
|
|
67
|
+
return { methods: Object.keys(methods), handler: Object.freeze(methods) };
|
|
68
|
+
}
|
|
69
|
+
if (Object.prototype.toString.call(handler) === "[object HTMLBundle]") {
|
|
70
|
+
return { methods: ["GET", "HEAD"], handler };
|
|
71
|
+
}
|
|
72
|
+
if (handler instanceof Blob && "exists" in handler && typeof handler.exists === "function") {
|
|
73
|
+
return { methods: ["*"], handler };
|
|
74
|
+
}
|
|
75
|
+
if (Object.getPrototypeOf(handler) === Object.prototype && "dir" in handler && typeof handler.dir === "string" && handler.dir.length > 0 && entries.every(([key, value]) => key === "dir" || key === "statCache" && typeof value === "boolean")) {
|
|
76
|
+
return { methods: ["*"], handler: Object.freeze({ ...handler }) };
|
|
77
|
+
}
|
|
78
|
+
throw new Error("Invalid route method map or native route declaration");
|
|
79
|
+
}
|
|
80
|
+
throw new Error("Invalid route handler");
|
|
81
|
+
}
|
|
82
|
+
function compileRoutePlan(sources, prefix = "") {
|
|
83
|
+
const routes = Object.create(null);
|
|
84
|
+
const owners = new Map;
|
|
85
|
+
const inventory = [];
|
|
86
|
+
const canonicalPaths = new Map;
|
|
87
|
+
const implicitHeads = new Map;
|
|
88
|
+
for (const source of sources) {
|
|
89
|
+
for (const path of Object.keys(source.metadata ?? {})) {
|
|
90
|
+
if (!Object.hasOwn(source.routes, path)) {
|
|
91
|
+
throw new Error(`OpenAPI metadata has no API route: ${source.name} ${path}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const [rawPath, rawHandler] of Object.entries(source.routes)) {
|
|
95
|
+
const path = normalizeRoutePath(rawPath, prefix);
|
|
96
|
+
const { methods, handler } = routeMethods(rawHandler);
|
|
97
|
+
if (handler && typeof handler === "object" && "dir" in handler && !path.endsWith("/*")) {
|
|
98
|
+
throw new Error(`Directory route must end in /*: ${path}`);
|
|
99
|
+
}
|
|
100
|
+
const collisionPath = path.replace(/:[^/]+/g, ":parameter");
|
|
101
|
+
const canonicalPath = canonicalPaths.get(collisionPath);
|
|
102
|
+
if (canonicalPath && canonicalPath !== path) {
|
|
103
|
+
throw new Error(`Ambiguous route parameter names: ${canonicalPath} and ${path}`);
|
|
104
|
+
}
|
|
105
|
+
canonicalPaths.set(collisionPath, path);
|
|
106
|
+
const previous = owners.get(collisionPath) ?? new Map;
|
|
107
|
+
for (const method of methods) {
|
|
108
|
+
if (previous.has(method) || previous.has("*") || method === "*" && previous.size) {
|
|
109
|
+
throw new Error(`Duplicate route: ${method} ${path} (${[...previous.values()][0]} and ${source.name})`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const metadata = source.metadata?.[rawPath];
|
|
113
|
+
for (const method of Object.keys(metadata ?? {})) {
|
|
114
|
+
if (!OPENAPI_METHODS.has(method) || !methods.includes("*") && !methods.includes(method) && !(method === "HEAD" && methods.includes("GET"))) {
|
|
115
|
+
throw new Error(`OpenAPI metadata has no API handler: ${method} ${rawPath}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const inherited = readRouteInventory(source.routes)?.filter((entry) => entry.path === rawPath);
|
|
119
|
+
if (inherited) {
|
|
120
|
+
inventory.push(...inherited.filter((entry) => entry.method !== "HEAD" || !methods.includes("GET") || methods.includes("HEAD")).map((entry) => ({ ...entry, path })));
|
|
121
|
+
} else {
|
|
122
|
+
for (const method of methods) {
|
|
123
|
+
const openApi = metadata?.[method];
|
|
124
|
+
inventory.push({
|
|
125
|
+
method,
|
|
126
|
+
path,
|
|
127
|
+
middleware: source.middleware ?? [],
|
|
128
|
+
...openApi ? { openApi } : {}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (methods.includes("*")) {
|
|
132
|
+
for (const [method, openApi] of Object.entries(metadata ?? {})) {
|
|
133
|
+
inventory.push({
|
|
134
|
+
method,
|
|
135
|
+
path,
|
|
136
|
+
middleware: source.middleware ?? [],
|
|
137
|
+
...openApi ? { openApi } : {}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (methods.includes("GET") && !methods.includes("HEAD")) {
|
|
143
|
+
const openApi = metadata?.HEAD ?? inherited?.find((entry) => entry.method === "HEAD")?.openApi;
|
|
144
|
+
implicitHeads.set(path, {
|
|
145
|
+
method: "HEAD",
|
|
146
|
+
path,
|
|
147
|
+
middleware: inherited?.find((entry) => entry.method === "GET")?.middleware ?? source.middleware ?? [],
|
|
148
|
+
...openApi ? { openApi } : {}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
for (const method of methods)
|
|
152
|
+
previous.set(method, source.name);
|
|
153
|
+
owners.set(collisionPath, previous);
|
|
154
|
+
if (Object.hasOwn(routes, path)) {
|
|
155
|
+
if (routes[path] === false || handler === false || Object.prototype.toString.call(routes[path]) === "[object HTMLBundle]" || Object.prototype.toString.call(handler) === "[object HTMLBundle]") {
|
|
156
|
+
throw new Error(`Cannot merge native bundle route: ${path}`);
|
|
157
|
+
}
|
|
158
|
+
routes[path] = Object.freeze({ ...routes[path], ...handler });
|
|
159
|
+
} else {
|
|
160
|
+
routes[path] = handler;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (const [path, head] of implicitHeads) {
|
|
165
|
+
if (!inventory.some((entry) => entry.path === path && entry.method === "HEAD"))
|
|
166
|
+
inventory.push(head);
|
|
167
|
+
}
|
|
168
|
+
const snapshot = immutableCopy(inventory.sort((left, right) => left.path.localeCompare(right.path)));
|
|
169
|
+
return Object.freeze({ routes: attachRouteInventory(routes, snapshot), inventory: snapshot });
|
|
170
|
+
}
|
|
171
|
+
function attachRouteInventory(routes, inventory) {
|
|
172
|
+
for (const value of Object.values(routes)) {
|
|
173
|
+
if (value && typeof value === "object" && !(value instanceof Response) && Object.keys(value).some((method) => ROUTE_METHODS.includes(method))) {
|
|
174
|
+
Object.freeze(value);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
Object.defineProperty(routes, inventoryKey, { value: inventory, enumerable: false });
|
|
178
|
+
return Object.freeze(routes);
|
|
179
|
+
}
|
|
180
|
+
function getRouteInventory(routes) {
|
|
181
|
+
const inventory = readRouteInventory(routes);
|
|
182
|
+
if (!inventory)
|
|
183
|
+
throw new Error("Route map has not been compiled");
|
|
184
|
+
return inventory;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ../../src/bootstrap/routeMetadata.ts
|
|
188
|
+
function publishRoutePlan(plan, routes = plan.routes) {
|
|
189
|
+
return attachRouteInventory(routes, plan.inventory);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ../../src/bootstrap/createSpaRoutes.ts
|
|
7
193
|
var SPA_DIST_DIRECTORY = join(process.cwd(), "frontend/dist");
|
|
8
194
|
function relativeSpaPath(pathname, prefix) {
|
|
9
195
|
if (pathname === prefix || pathname === `${prefix}/`) {
|
|
@@ -56,16 +242,19 @@ function createSpaRoutes(_dependencies, options = {}) {
|
|
|
56
242
|
if (!isViewsEnabled()) {
|
|
57
243
|
routes["/"] = async () => Response.redirect(`${prefix}/`, 302);
|
|
58
244
|
}
|
|
59
|
-
|
|
245
|
+
const plan = compileRoutePlan([{ name: "SPA", routes }]);
|
|
246
|
+
return publishRoutePlan(plan, plan.routes);
|
|
60
247
|
}
|
|
61
248
|
function mergeSpaRoutes(dependencies, routes, options) {
|
|
62
249
|
if (!isSpaEnabled()) {
|
|
63
250
|
return routes;
|
|
64
251
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
252
|
+
const spaRoutes = createSpaRoutes(dependencies, options);
|
|
253
|
+
const plan = compileRoutePlan([
|
|
254
|
+
{ name: "SPA", routes: spaRoutes },
|
|
255
|
+
{ name: "application", routes }
|
|
256
|
+
]);
|
|
257
|
+
return publishRoutePlan(plan, plan.routes);
|
|
69
258
|
}
|
|
70
259
|
export {
|
|
71
260
|
SPA_DIST_DIRECTORY,
|