@getstrata/bootstrap 2.2.60 → 3.0.4

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.
Files changed (90) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/dist/bootstrap/app.d.ts +18 -7
  3. package/dist/bootstrap/buildModuleRoutes.d.ts +3 -5
  4. package/dist/bootstrap/buildWebModuleRoutes.d.ts +2 -3
  5. package/dist/bootstrap/cache/modelCacheTags.d.ts +3 -2
  6. package/dist/bootstrap/cachedJson.d.ts +7 -2
  7. package/dist/bootstrap/config.d.ts +1 -1
  8. package/dist/bootstrap/context.d.ts +25 -7
  9. package/dist/bootstrap/contracts.d.ts +1 -0
  10. package/dist/bootstrap/createWebRoutes.d.ts +2 -1
  11. package/dist/bootstrap/databaseReadiness.d.ts +3 -0
  12. package/dist/bootstrap/dependencies.d.ts +3 -2
  13. package/dist/bootstrap/discoverJobs.d.ts +3 -2
  14. package/dist/bootstrap/discoverListeners.d.ts +10 -7
  15. package/dist/bootstrap/discoverModels.d.ts +3 -2
  16. package/dist/bootstrap/discoverModules.d.ts +7 -6
  17. package/dist/bootstrap/health.d.ts +3 -2
  18. package/dist/bootstrap/http/securedRouteModelBinding.d.ts +1 -1
  19. package/dist/bootstrap/httpKernel.d.ts +43 -22
  20. package/dist/bootstrap/httpPersistence.d.ts +4 -0
  21. package/dist/bootstrap/inProcessCron.d.ts +5 -5
  22. package/dist/bootstrap/listeners/invalidateCacheOnModelWrite.d.ts +13 -12
  23. package/dist/bootstrap/metricsRoutes.d.ts +6 -1
  24. package/dist/bootstrap/moduleRouteMiddleware.d.ts +4 -4
  25. package/dist/bootstrap/modules.d.ts +1 -1
  26. package/dist/bootstrap/ownedHttpRoute.d.ts +23 -0
  27. package/dist/bootstrap/persistenceContracts.d.ts +70 -0
  28. package/dist/bootstrap/persistenceInvocation.d.ts +4 -0
  29. package/dist/bootstrap/providers/auth.d.ts +21 -0
  30. package/dist/bootstrap/providers/cache.d.ts +9 -3
  31. package/dist/bootstrap/providers/cacheProvider.d.ts +3 -0
  32. package/dist/bootstrap/providers/config.d.ts +6 -3
  33. package/dist/bootstrap/providers/events.d.ts +5 -2
  34. package/dist/bootstrap/providers/index.d.ts +3 -2
  35. package/dist/bootstrap/providers/listeners.d.ts +3 -3
  36. package/dist/bootstrap/providers/mail.d.ts +15 -0
  37. package/dist/bootstrap/providers/queue.d.ts +3 -2
  38. package/dist/bootstrap/providers/storage.d.ts +11 -2
  39. package/dist/bootstrap/providers/tracing.d.ts +4 -2
  40. package/dist/bootstrap/providers/view.d.ts +8 -5
  41. package/dist/bootstrap/public-api.d.ts +10 -7
  42. package/dist/bootstrap/queue/defaultJobs.d.ts +4 -2
  43. package/dist/bootstrap/rateLimit.d.ts +13 -3
  44. package/dist/bootstrap/routeCompiler.d.ts +20 -0
  45. package/dist/bootstrap/routeMetadata.d.ts +3 -3
  46. package/dist/bootstrap/routePlan.d.ts +11 -0
  47. package/dist/bootstrap/schedule.d.ts +11 -2
  48. package/dist/bootstrap/secretsGuard.d.ts +2 -3
  49. package/dist/bootstrap/web/routing.d.ts +6 -3
  50. package/dist/bootstrap/web/server.d.ts +14 -4
  51. package/dist/bootstrap/web/session.d.ts +23 -16
  52. package/dist/entries/buildModuleRoutes.js +602 -384
  53. package/dist/entries/buildWebModuleRoutes.js +596 -347
  54. package/dist/entries/cache/modelCacheTags.js +8 -186
  55. package/dist/entries/cachedJson.js +22 -0
  56. package/dist/entries/config.js +1 -3
  57. package/dist/entries/context.js +665 -700
  58. package/dist/entries/createSpaRoutes.js +194 -5
  59. package/dist/entries/createWebRoutes.js +618 -367
  60. package/dist/entries/databaseReadiness.js +267 -0
  61. package/dist/entries/dependencies.js +666 -701
  62. package/dist/entries/discoverJobs.js +18 -16
  63. package/dist/entries/discoverListeners.js +27 -38
  64. package/dist/entries/discoverModels.js +4 -3
  65. package/dist/entries/discoverModules.js +29 -67
  66. package/dist/entries/health.js +14 -34
  67. package/dist/entries/httpKernel.js +405 -89
  68. package/dist/entries/listeners/invalidateCacheOnModelWrite.js +43 -289
  69. package/dist/entries/metricsRoutes.js +9 -5
  70. package/dist/entries/persistenceContracts.js +1 -0
  71. package/dist/entries/persistenceInvocation.js +17 -0
  72. package/dist/entries/providers/auth.js +114 -0
  73. package/dist/entries/providers/cache.js +35 -0
  74. package/dist/entries/providers/events.js +55 -0
  75. package/dist/entries/providers/view.js +34 -44
  76. package/dist/entries/providers.js +392 -561
  77. package/dist/entries/queue/defaultJobs.js +9 -6
  78. package/dist/entries/routePlan.js +200 -0
  79. package/dist/entries/schedule.js +37 -37
  80. package/dist/entries/secretsGuard.js +10 -13
  81. package/dist/entries/web/routing.js +412 -96
  82. package/dist/entries/web/server.js +461 -29
  83. package/dist/entries/web/session.js +61 -73
  84. package/dist/index.js +1520 -1085
  85. package/dist/internal/ownedHttpRoute.js +66 -0
  86. package/package.json +52 -15
  87. package/dist/bootstrap/applicationRegistry.d.ts +0 -1
  88. package/dist/bootstrap/routeRegistry.d.ts +0 -10
  89. package/dist/entries/applicationRegistry.js +0 -24
  90. 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
- return routes;
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
- return {
66
- ...createSpaRoutes(dependencies, options),
67
- ...routes
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,