@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
|
@@ -1,312 +1,66 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// ../../src/bootstrap/listeners/invalidateCacheOnModelWrite.ts
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { createTrackedJob } from "@getstrata/core/queue/createAppQueue";
|
|
6
|
-
|
|
7
|
-
// ../../src/bootstrap/applicationRegistry.ts
|
|
3
|
+
import { service } from "@getstrata/core/contracts/services";
|
|
4
|
+
import { createServiceToken } from "@getstrata/core/contracts/token";
|
|
8
5
|
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
resolveApplicationDependencies,
|
|
13
|
-
resolveApplicationEventBus,
|
|
14
|
-
resolveApplicationLogger,
|
|
15
|
-
resolveApplicationPolicyGate,
|
|
16
|
-
resolveApplicationQueue,
|
|
17
|
-
setActiveApplicationContext
|
|
18
|
-
} from "@getstrata/core/runtime/applicationRegistry";
|
|
19
|
-
|
|
20
|
-
// ../../src/bootstrap/discoverModules.ts
|
|
21
|
-
import { readdir as readdir2 } from "fs/promises";
|
|
22
|
-
import { join as join2, resolve } from "path";
|
|
23
|
-
|
|
24
|
-
// ../../src/bootstrap/infrastructureDiscovery.ts
|
|
25
|
-
import { readdir } from "fs/promises";
|
|
26
|
-
import { join, relative } from "path";
|
|
27
|
-
import { pathToFileURL } from "url";
|
|
28
|
-
async function infrastructureFiles(directory, exclude = []) {
|
|
29
|
-
const excluded = new Set([
|
|
30
|
-
"index.ts",
|
|
31
|
-
"index.js",
|
|
32
|
-
"index.mts",
|
|
33
|
-
"index.mjs",
|
|
34
|
-
"register.ts",
|
|
35
|
-
"register.js",
|
|
36
|
-
"register.mts",
|
|
37
|
-
"register.mjs",
|
|
38
|
-
...exclude
|
|
39
|
-
]);
|
|
40
|
-
const files = [];
|
|
41
|
-
async function visit(path) {
|
|
42
|
-
const entries = await readdir(path, { withFileTypes: true });
|
|
43
|
-
entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
44
|
-
for (const entry of entries) {
|
|
45
|
-
const file = join(path, entry.name);
|
|
46
|
-
const key = relative(directory, file).replaceAll("\\", "/");
|
|
47
|
-
if (excluded.has(entry.name) || excluded.has(key))
|
|
48
|
-
continue;
|
|
49
|
-
if (entry.isDirectory())
|
|
50
|
-
await visit(file);
|
|
51
|
-
else if (entry.isFile() && /\.(ts|js|mts|mjs)$/.test(entry.name) && !/\.(d|test|spec)\.[cm]?[jt]s$/.test(entry.name))
|
|
52
|
-
files.push(file);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
try {
|
|
56
|
-
await visit(directory);
|
|
57
|
-
} catch (error) {
|
|
58
|
-
if (error.code !== "ENOENT")
|
|
59
|
-
throw error;
|
|
60
|
-
if (error.path !== directory)
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
63
|
-
return files;
|
|
64
|
-
}
|
|
65
|
-
async function importInfrastructure(file) {
|
|
66
|
-
try {
|
|
67
|
-
return await import(pathToFileURL(file).href);
|
|
68
|
-
} catch (error) {
|
|
69
|
-
throw new Error(`Cannot load infrastructure module ${file}`, { cause: error });
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// ../../src/bootstrap/discoverModules.ts
|
|
74
|
-
var KEY = Symbol.for("@getstrata/discoverModulesState");
|
|
75
|
-
function state() {
|
|
76
|
-
const globals = globalThis;
|
|
77
|
-
globals[KEY] ??= { appModules: [] };
|
|
78
|
-
return globals[KEY];
|
|
79
|
-
}
|
|
80
|
-
function configureModulesDirectory(modulesDir) {
|
|
81
|
-
const current = state();
|
|
82
|
-
const directory = resolve(modulesDir);
|
|
83
|
-
if (current.configuredModulesDir !== directory || current.configuredManifest) {
|
|
84
|
-
current.appModules.length = 0;
|
|
85
|
-
current.modulesReady = undefined;
|
|
86
|
-
current.source = undefined;
|
|
87
|
-
}
|
|
88
|
-
current.configuredManifest = undefined;
|
|
89
|
-
current.configuredModulesDir = directory;
|
|
90
|
-
}
|
|
91
|
-
function configureModulesManifest(manifest) {
|
|
92
|
-
const current = state();
|
|
93
|
-
if (current.configuredManifest !== manifest) {
|
|
94
|
-
current.appModules.length = 0;
|
|
95
|
-
current.modulesReady = undefined;
|
|
96
|
-
current.source = undefined;
|
|
97
|
-
}
|
|
98
|
-
current.configuredManifest = manifest;
|
|
99
|
-
}
|
|
100
|
-
function validateModule(value, location) {
|
|
101
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
102
|
-
throw new TypeError(`Invalid application module at ${location}.`);
|
|
103
|
-
const module = value;
|
|
104
|
-
if (typeof module.name !== "string" || !module.name.trim())
|
|
105
|
-
throw new TypeError(`Invalid module name at ${location}.`);
|
|
106
|
-
if (module.order !== undefined && (typeof module.order !== "number" || !Number.isFinite(module.order)))
|
|
107
|
-
throw new TypeError(`Invalid module order at ${location}.`);
|
|
108
|
-
for (const hook of ["routes", "webRoutes"])
|
|
109
|
-
if (module[hook] !== undefined && typeof module[hook] !== "function")
|
|
110
|
-
throw new TypeError(`Invalid ${hook} at ${location}.`);
|
|
111
|
-
if (module.providers !== undefined) {
|
|
112
|
-
if (!Array.isArray(module.providers))
|
|
113
|
-
throw new TypeError(`Invalid providers at ${location}.`);
|
|
114
|
-
for (const provider of module.providers) {
|
|
115
|
-
if (!provider || typeof provider.name !== "string" || !provider.name.trim() || provider.register !== undefined && typeof provider.register !== "function" || provider.boot !== undefined && typeof provider.boot !== "function")
|
|
116
|
-
throw new TypeError(`Invalid provider at ${location}.`);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
async function load(options) {
|
|
121
|
-
const manifest = options.manifest ?? (options.modulesDir === undefined ? state().configuredManifest : undefined);
|
|
122
|
-
const entries = [];
|
|
123
|
-
if (manifest)
|
|
124
|
-
for (const [index, value] of manifest.entries())
|
|
125
|
-
entries.push({ value, location: `module manifest[${index}]` });
|
|
126
|
-
else {
|
|
127
|
-
const directory = options.modulesDir ?? state().configuredModulesDir;
|
|
128
|
-
if (!directory)
|
|
129
|
-
throw new Error("configureModulesDirectory() or configureModulesManifest() must be called before discovering modules.");
|
|
130
|
-
let folders;
|
|
131
|
-
try {
|
|
132
|
-
folders = await readdir2(directory, { withFileTypes: true });
|
|
133
|
-
} catch (error) {
|
|
134
|
-
if (error.code === "ENOENT")
|
|
135
|
-
return [];
|
|
136
|
-
throw error;
|
|
137
|
-
}
|
|
138
|
-
const excluded = new Set(options.exclude ?? []);
|
|
139
|
-
for (const folder of folders.filter((entry) => entry.isDirectory() && !excluded.has(entry.name)).sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0)) {
|
|
140
|
-
const path = join2(directory, folder.name);
|
|
141
|
-
const files = await readdir2(path);
|
|
142
|
-
const candidates = files.filter((file) => /^index\.(ts|js|mts|mjs)$/.test(file));
|
|
143
|
-
if (candidates.length !== 1)
|
|
144
|
-
throw new Error(`Expected one module entrypoint at ${path}; found ${candidates.length}.`);
|
|
145
|
-
const file = join2(path, candidates[0]);
|
|
146
|
-
const exports = await importInfrastructure(file);
|
|
147
|
-
entries.push({ value: exports.default, location: file });
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const names = new Map;
|
|
151
|
-
const modules = [];
|
|
152
|
-
for (const { value, location } of entries) {
|
|
153
|
-
validateModule(value, location);
|
|
154
|
-
const previous = names.get(value.name);
|
|
155
|
-
if (previous)
|
|
156
|
-
throw new Error(`Duplicate module "${value.name}" at ${location}; first declared at ${previous}.`);
|
|
157
|
-
names.set(value.name, location);
|
|
158
|
-
modules.push(value);
|
|
159
|
-
}
|
|
160
|
-
return modules.sort((left, right) => (left.order ?? 100) - (right.order ?? 100));
|
|
161
|
-
}
|
|
162
|
-
function ensureModulesLoaded(options = {}) {
|
|
163
|
-
const current = state();
|
|
164
|
-
const key = options.manifest ?? (options.modulesDir === undefined ? current.configuredManifest : undefined) ?? JSON.stringify([
|
|
165
|
-
options.modulesDir ? resolve(options.modulesDir) : current.configuredModulesDir,
|
|
166
|
-
[...options.exclude ?? []].sort()
|
|
167
|
-
]);
|
|
168
|
-
if (current.modulesReady && current.source === key)
|
|
169
|
-
return current.modulesReady;
|
|
170
|
-
current.appModules.length = 0;
|
|
171
|
-
current.source = key;
|
|
172
|
-
const pending = load(options).then((modules) => {
|
|
173
|
-
if (current.modulesReady !== pending)
|
|
174
|
-
return modules;
|
|
175
|
-
current.appModules.splice(0, current.appModules.length, ...modules);
|
|
176
|
-
return current.appModules;
|
|
177
|
-
}).catch((error) => {
|
|
178
|
-
if (current.modulesReady === pending)
|
|
179
|
-
current.modulesReady = undefined;
|
|
180
|
-
throw error;
|
|
181
|
-
});
|
|
182
|
-
current.modulesReady = pending;
|
|
183
|
-
return pending;
|
|
184
|
-
}
|
|
185
|
-
function discoverModules() {
|
|
186
|
-
return state().appModules;
|
|
187
|
-
}
|
|
188
|
-
function resetDiscoverModulesForTests() {
|
|
189
|
-
const current = state();
|
|
190
|
-
current.configuredModulesDir = undefined;
|
|
191
|
-
current.configuredManifest = undefined;
|
|
192
|
-
current.appModules.length = 0;
|
|
193
|
-
current.modulesReady = undefined;
|
|
194
|
-
current.source = undefined;
|
|
195
|
-
}
|
|
6
|
+
repositoryEffects
|
|
7
|
+
} from "@getstrata/core/database/definitions";
|
|
8
|
+
import { InvalidateCacheTagsJob } from "@getstrata/core/jobs/invalidateCacheTagsJob";
|
|
196
9
|
|
|
197
10
|
// ../../src/bootstrap/cache/modelCacheTags.ts
|
|
198
|
-
function cacheTagsForModelWrite(tableName, action) {
|
|
199
|
-
const module =
|
|
200
|
-
const baseTags = module?.cacheTags ?? [`${tableName}s`];
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
return [...new Set([...baseTags, ...extraTags])];
|
|
11
|
+
function cacheTagsForModelWrite(tableName, action, modules) {
|
|
12
|
+
const module = modules.find((entry) => entry.tableName === tableName);
|
|
13
|
+
const baseTags = module?.cacheTags ?? [tableName.endsWith("s") ? tableName : `${tableName}s`];
|
|
14
|
+
const extras = action === "deleted" || action === "force-deleted" ? module?.cacheDeleteExtraTags ?? [] : [];
|
|
15
|
+
return [...new Set([...baseTags, ...extras])];
|
|
204
16
|
}
|
|
205
|
-
function discoverModelTableNames() {
|
|
206
|
-
return
|
|
17
|
+
function discoverModelTableNames(modules) {
|
|
18
|
+
return modules.map((module) => module.tableName).filter((name) => name !== undefined);
|
|
207
19
|
}
|
|
208
20
|
|
|
209
21
|
// ../../src/bootstrap/listeners/invalidateCacheOnModelWrite.ts
|
|
210
|
-
var
|
|
22
|
+
var ACTIONS = ["created", "updated", "deleted", "restored", "force-deleted"];
|
|
211
23
|
var CACHE_EVENT = "strata.cache.invalidate-tags";
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
24
|
+
function createModelCacheInvalidationEffects(repositories, modules, tokens) {
|
|
25
|
+
const services = [];
|
|
26
|
+
const effects = [];
|
|
27
|
+
for (const definition of repositories) {
|
|
28
|
+
for (const action of ACTIONS) {
|
|
29
|
+
const tags = Object.freeze(cacheTagsForModelWrite(definition.table.name, action, modules));
|
|
30
|
+
if (!tags.length)
|
|
31
|
+
continue;
|
|
32
|
+
const listener = createServiceToken(`cache.${definition.name}.${action}`);
|
|
33
|
+
services.push(service(listener, {
|
|
34
|
+
lifetime: "application",
|
|
35
|
+
dependencies: [tokens.cache, tokens.queue],
|
|
36
|
+
create: (cache, queue) => async () => {
|
|
37
|
+
await queue.dispatch(new InvalidateCacheTagsJob(cache), { tags: [...tags] });
|
|
38
|
+
}
|
|
39
|
+
}));
|
|
40
|
+
effects.push(repositoryEffects(definition, [{ action, afterCommit: listener }]));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return Object.freeze({
|
|
44
|
+
services: Object.freeze(services),
|
|
45
|
+
repositoryEffects: Object.freeze(effects)
|
|
46
|
+
});
|
|
222
47
|
}
|
|
223
|
-
function createModelCacheInvalidationListener(
|
|
48
|
+
function createModelCacheInvalidationListener(cache) {
|
|
224
49
|
return {
|
|
225
50
|
name: "strata.cache.invalidate-tags.v1",
|
|
226
51
|
event: CACHE_EVENT,
|
|
227
52
|
async handle(event, { signal }) {
|
|
228
53
|
const payload = event.payload;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
54
|
+
const tags = payload?.tags;
|
|
55
|
+
if (event.version !== 1 || !Array.isArray(tags) || !tags.length || tags.length > 128 || !tags.every((tag) => typeof tag === "string" && tag.length > 0 && tag.length <= 256))
|
|
56
|
+
throw new Error("Invalid durable cache invalidation payload.");
|
|
232
57
|
signal.throwIfAborted();
|
|
233
|
-
await new InvalidateCacheTagsJob(
|
|
58
|
+
await new InvalidateCacheTagsJob(cache).handle({ tags });
|
|
234
59
|
signal.throwIfAborted();
|
|
235
60
|
}
|
|
236
61
|
};
|
|
237
62
|
}
|
|
238
|
-
function registerInvalidateCacheOnModelWriteListeners(bus = eventBus, options = {}) {
|
|
239
|
-
if (options.outbox && !options.outbox.hasListener("strata.cache.invalidate-tags.v1", CACHE_EVENT))
|
|
240
|
-
throw new Error("The application outbox must include createModelCacheInvalidationListener().");
|
|
241
|
-
const writes = discoverModelTableNames().flatMap((tableName) => MODEL_WRITE_ACTIONS.map((action) => ({
|
|
242
|
-
tableName,
|
|
243
|
-
action,
|
|
244
|
-
tags: cacheTagsForModelWrite(tableName, action)
|
|
245
|
-
})));
|
|
246
|
-
if (options.outbox) {
|
|
247
|
-
for (const write of writes)
|
|
248
|
-
if (write.tags.length)
|
|
249
|
-
requireCacheTags(write.tags);
|
|
250
|
-
}
|
|
251
|
-
const state = registrations();
|
|
252
|
-
const previous = state.get(bus);
|
|
253
|
-
if (previous?.durable && !options.outbox)
|
|
254
|
-
return () => {};
|
|
255
|
-
previous?.suspend();
|
|
256
|
-
const unsubscribe = [];
|
|
257
|
-
const subscribe = [];
|
|
258
|
-
const registration = {
|
|
259
|
-
durable: !!options.outbox,
|
|
260
|
-
suspend() {
|
|
261
|
-
for (const stop of unsubscribe.splice(0))
|
|
262
|
-
stop();
|
|
263
|
-
},
|
|
264
|
-
resume() {
|
|
265
|
-
for (const start of subscribe)
|
|
266
|
-
unsubscribe.push(start());
|
|
267
|
-
state.set(bus, registration);
|
|
268
|
-
},
|
|
269
|
-
dispose() {
|
|
270
|
-
if (state.get(bus) !== registration)
|
|
271
|
-
return;
|
|
272
|
-
registration.suspend();
|
|
273
|
-
if (previous)
|
|
274
|
-
previous.resume();
|
|
275
|
-
else
|
|
276
|
-
state.delete(bus);
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
for (const { tableName, action, tags: durableTags } of writes) {
|
|
280
|
-
const event = modelEventName(tableName, action);
|
|
281
|
-
if (options.outbox) {
|
|
282
|
-
if (!durableTags.length)
|
|
283
|
-
continue;
|
|
284
|
-
const outbox = options.outbox;
|
|
285
|
-
subscribe.push(() => bus.listenTransactional(event, async () => {
|
|
286
|
-
await outbox.publish(CACHE_EVENT, { tags: durableTags });
|
|
287
|
-
}));
|
|
288
|
-
} else {
|
|
289
|
-
subscribe.push(() => bus.listen(event, async () => {
|
|
290
|
-
const tags = cacheTagsForModelWrite(tableName, action);
|
|
291
|
-
if (!tags.length)
|
|
292
|
-
return;
|
|
293
|
-
let cache;
|
|
294
|
-
let queue;
|
|
295
|
-
try {
|
|
296
|
-
cache = resolveApplicationCache();
|
|
297
|
-
queue = resolveApplicationQueue();
|
|
298
|
-
} catch {
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
const job = createTrackedJob("cache.invalidate-tags", new InvalidateCacheTagsJob(cache));
|
|
302
|
-
await queue.dispatch(job, { tags });
|
|
303
|
-
}));
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
registration.resume();
|
|
307
|
-
return registration.dispose;
|
|
308
|
-
}
|
|
309
63
|
export {
|
|
310
|
-
|
|
311
|
-
|
|
64
|
+
createModelCacheInvalidationEffects,
|
|
65
|
+
createModelCacheInvalidationListener
|
|
312
66
|
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { timingSafeEqual } from "crypto";
|
|
4
4
|
import {
|
|
5
5
|
renderTransactionAcquisitionMetrics
|
|
6
|
-
} from "@getstrata/core/database/
|
|
6
|
+
} from "@getstrata/core/database/acquisitionMetrics";
|
|
7
7
|
import { renderOutboxMetrics } from "@getstrata/core/events/outbox";
|
|
8
8
|
import { prometheusRegistry } from "@getstrata/core/metrics/prometheus";
|
|
9
9
|
import {
|
|
@@ -53,19 +53,23 @@ function tokensMatch(left, right) {
|
|
|
53
53
|
}
|
|
54
54
|
return timingSafeEqual(leftBuffer, rightBuffer);
|
|
55
55
|
}
|
|
56
|
-
function authorizeMetrics(request) {
|
|
57
|
-
const expected =
|
|
56
|
+
function authorizeMetrics(request, policy) {
|
|
57
|
+
const expected = policy.token;
|
|
58
58
|
const authorization = request.headers.get("authorization") ?? "";
|
|
59
59
|
const presented = authorization.startsWith("Bearer ") ? authorization.slice("Bearer ".length) : "";
|
|
60
60
|
if (expected) {
|
|
61
61
|
return presented.length > 0 && tokensMatch(presented, expected);
|
|
62
62
|
}
|
|
63
|
-
if (
|
|
63
|
+
if (policy.production) {
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
66
|
return true;
|
|
67
67
|
}
|
|
68
68
|
function createMetricsRoutes(options = {}) {
|
|
69
|
+
const authorization = Object.freeze({
|
|
70
|
+
token: (options.authorization?.token ?? (options.authorization ? undefined : process.env.METRICS_TOKEN))?.trim(),
|
|
71
|
+
production: options.authorization?.production ?? isProductionEnv()
|
|
72
|
+
});
|
|
69
73
|
let collection;
|
|
70
74
|
const collect = () => {
|
|
71
75
|
if (!options.queue && !options.outbox && !options.tracing && !options.transactionAcquisition && !options.failedJobs)
|
|
@@ -87,7 +91,7 @@ strata_${name}_collector_success 0
|
|
|
87
91
|
};
|
|
88
92
|
return {
|
|
89
93
|
"/metrics": async (request) => {
|
|
90
|
-
if (!authorizeMetrics(request)) {
|
|
94
|
+
if (!authorizeMetrics(request, authorization)) {
|
|
91
95
|
return new Response("Not Found", { status: 404 });
|
|
92
96
|
}
|
|
93
97
|
return new Response(prometheusRegistry.renderMetrics() + await collect(), {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/bootstrap/persistenceInvocation.ts
|
|
3
|
+
import { CORE_EXECUTION_OWNERSHIP_TOKEN } from "@getstrata/core/contracts/executionOwnership";
|
|
4
|
+
import { DATABASE_SESSION_TOKEN } from "@getstrata/core/database/tokens";
|
|
5
|
+
import { createScopedPolicy } from "@getstrata/core/tenant/scopedDatabasePolicy";
|
|
6
|
+
function createPersistenceInvocation(application, connection) {
|
|
7
|
+
if (!connection.trim())
|
|
8
|
+
throw new TypeError("A configured persistence connection is required.");
|
|
9
|
+
return (envelope, work) => application.run(envelope, (scope) => {
|
|
10
|
+
const database = scope.resolve(DATABASE_SESSION_TOKEN).connection(connection);
|
|
11
|
+
const policy = createScopedPolicy(scope.resolve(CORE_EXECUTION_OWNERSHIP_TOKEN), scope.context, database);
|
|
12
|
+
return work(Object.freeze({ database, policy }));
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
createPersistenceInvocation
|
|
17
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/bootstrap/providers/auth.ts
|
|
3
|
+
import { establishedAuthReader } from "@getstrata/core/auth/authReader";
|
|
4
|
+
import { BasicAuthGuard } from "@getstrata/core/auth/basicAuthGuard";
|
|
5
|
+
import {
|
|
6
|
+
AuthManager,
|
|
7
|
+
CompositeGuard,
|
|
8
|
+
DatabaseTokenGuard,
|
|
9
|
+
GuestGuard,
|
|
10
|
+
requireAuthCapability
|
|
11
|
+
} from "@getstrata/core/auth/guard";
|
|
12
|
+
import { JwtGuard } from "@getstrata/core/auth/jwtGuard";
|
|
13
|
+
import { SessionGuard } from "@getstrata/core/auth/sessionGuard";
|
|
14
|
+
|
|
15
|
+
// ../../src/bootstrap/config.ts
|
|
16
|
+
import {
|
|
17
|
+
CORE_ABILITY_CHECKER_TOKEN,
|
|
18
|
+
CORE_AUTH_TOKEN,
|
|
19
|
+
CORE_AUTH_USER_DIRECTORY_TOKEN,
|
|
20
|
+
CORE_CACHE_TOKEN,
|
|
21
|
+
CORE_CONFIG_TOKEN,
|
|
22
|
+
CORE_EVENT_BUS_TOKEN,
|
|
23
|
+
CORE_HTTP_CLEANUP_TOKEN,
|
|
24
|
+
CORE_POLICY_GATE_TOKEN,
|
|
25
|
+
CORE_PUBLIC_TENANCY_TOKEN,
|
|
26
|
+
CORE_QUEUE_TOKEN,
|
|
27
|
+
CORE_TENANT_RESOLVER_TOKEN,
|
|
28
|
+
CORE_THROTTLE_QUOTA_POLICY_TOKEN
|
|
29
|
+
} from "@getstrata/core/contracts/serviceTokens";
|
|
30
|
+
var APP_PORT_CONFIG_KEY = "app.port";
|
|
31
|
+
var CACHE_TTL_MS_CONFIG_KEY = "cache.ttlMs";
|
|
32
|
+
var CACHE_MAX_ENTRIES_CONFIG_KEY = "cache.maxEntries";
|
|
33
|
+
var CACHE_DRIVER_CONFIG_KEY = "cache.driver";
|
|
34
|
+
var REDIS_URL_CONFIG_KEY = "cache.redisUrl";
|
|
35
|
+
var DATABASE_URL_CONFIG_KEY = "database.url";
|
|
36
|
+
var CSRF_TARGET_ORIGIN_CONFIG_KEY = "http.csrf.targetOrigin";
|
|
37
|
+
var AUTH_DEV_HEADERS_CONFIG_KEY = "auth.allowDevHeaders";
|
|
38
|
+
var DEFAULT_APP_PORT = 3000;
|
|
39
|
+
var DEFAULT_CACHE_TTL_MS = 3600000;
|
|
40
|
+
var DEFAULT_CACHE_MAX_ENTRIES = 100;
|
|
41
|
+
var DEFAULT_CACHE_DRIVER = "array";
|
|
42
|
+
var DEFAULT_API_TOKEN = "";
|
|
43
|
+
var DEFAULT_QUEUE_DRIVER = "sync";
|
|
44
|
+
|
|
45
|
+
// ../../src/bootstrap/providers/auth.ts
|
|
46
|
+
function createMetadataAuthManager(directory, options, persistRecovery) {
|
|
47
|
+
const enabled = new Set(options.mechanisms);
|
|
48
|
+
if ([...enabled].some((name) => !["api", "jwt", "basic", "session"].includes(name)))
|
|
49
|
+
throw new TypeError("Unknown authentication mechanism.");
|
|
50
|
+
const named = new Map;
|
|
51
|
+
const guards = [];
|
|
52
|
+
const add = (name, guard) => {
|
|
53
|
+
named.set(name, guard);
|
|
54
|
+
guards.push(guard);
|
|
55
|
+
return guard;
|
|
56
|
+
};
|
|
57
|
+
if (enabled.has("api")) {
|
|
58
|
+
const api = add("api", new DatabaseTokenGuard(requireAuthCapability(directory, ["resolveUserFromToken"], "API token")));
|
|
59
|
+
named.set("access_token", api);
|
|
60
|
+
}
|
|
61
|
+
if (enabled.has("jwt")) {
|
|
62
|
+
if (!options.jwtSecret)
|
|
63
|
+
throw new TypeError("Explicit JWT secret is required.");
|
|
64
|
+
add("jwt", new JwtGuard({
|
|
65
|
+
directory: requireAuthCapability(directory, ["findByIdOrThrow"], "JWT"),
|
|
66
|
+
secret: options.jwtSecret
|
|
67
|
+
}));
|
|
68
|
+
}
|
|
69
|
+
if (enabled.has("basic")) {
|
|
70
|
+
const accounts = requireAuthCapability(directory, ["findByIdOrThrow"], "Basic account");
|
|
71
|
+
const emailLookup = directory.findByEmail === undefined ? undefined : requireAuthCapability(directory, ["findByEmail"], "Basic email lookup");
|
|
72
|
+
const credentials = directory.verifyCredentials === undefined ? undefined : requireAuthCapability(directory, ["verifyCredentials"], "Basic credentials");
|
|
73
|
+
let basic;
|
|
74
|
+
if (emailLookup)
|
|
75
|
+
basic = {
|
|
76
|
+
accounts,
|
|
77
|
+
emailLookup,
|
|
78
|
+
credentials,
|
|
79
|
+
persistRecovery,
|
|
80
|
+
mfaSecrets: options.mfaSecrets
|
|
81
|
+
};
|
|
82
|
+
else if (credentials)
|
|
83
|
+
basic = { accounts, credentials, persistRecovery, mfaSecrets: options.mfaSecrets };
|
|
84
|
+
else
|
|
85
|
+
throw new TypeError("Basic authentication requires an email lookup or credential verifier.");
|
|
86
|
+
add("basic", new BasicAuthGuard(basic));
|
|
87
|
+
}
|
|
88
|
+
if (enabled.has("session")) {
|
|
89
|
+
const session = add("web", options.sessionGuard !== undefined ? requireAuthCapability(options.sessionGuard, ["authenticate"], "custom session") : new SessionGuard(requireAuthCapability(directory, ["findByIdOrThrow"], "session account"), {
|
|
90
|
+
read: options.readSession,
|
|
91
|
+
hasCredential: requireAuthCapability(options, ["hasSessionCredential"], "session presence").hasSessionCredential,
|
|
92
|
+
abilities: options.roleAbilities,
|
|
93
|
+
validity: directory.hasActiveBrowserSession === undefined ? null : requireAuthCapability(directory, ["hasActiveBrowserSession"], "session validity")
|
|
94
|
+
}));
|
|
95
|
+
named.set("session", session);
|
|
96
|
+
}
|
|
97
|
+
if (!options.production && options.allowDevHeaders)
|
|
98
|
+
guards.push(new GuestGuard({ ...options, abilities: options.abilities }));
|
|
99
|
+
const auth = new AuthManager(new CompositeGuard(guards));
|
|
100
|
+
for (const [name, guard] of named)
|
|
101
|
+
auth.registerGuard(name, guard);
|
|
102
|
+
return auth;
|
|
103
|
+
}
|
|
104
|
+
var authProvider = {
|
|
105
|
+
name: "core.auth",
|
|
106
|
+
register({ container }) {
|
|
107
|
+
container.set(CORE_AUTH_TOKEN, establishedAuthReader);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var auth_default = authProvider;
|
|
111
|
+
export {
|
|
112
|
+
createMetadataAuthManager,
|
|
113
|
+
auth_default as default
|
|
114
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/bootstrap/providers/cache.ts
|
|
3
|
+
import {
|
|
4
|
+
createCacheStore,
|
|
5
|
+
snapshotCacheStoreOptions
|
|
6
|
+
} from "@getstrata/core/cache/createCacheStore";
|
|
7
|
+
import { CacheRepository, createGuardedCache } from "@getstrata/core/cache/repository";
|
|
8
|
+
import { CORE_CACHE_BACKEND_TOKEN, CORE_CACHE_TOKEN } from "@getstrata/core/cache/tokens";
|
|
9
|
+
import { CORE_EXECUTION_OPERATIONS_TOKEN } from "@getstrata/core/contracts/executionOperations";
|
|
10
|
+
import {
|
|
11
|
+
resourceService,
|
|
12
|
+
service
|
|
13
|
+
} from "@getstrata/core/contracts/services";
|
|
14
|
+
function createCacheBindings(options) {
|
|
15
|
+
const snapshot = snapshotCacheStoreOptions(options, true);
|
|
16
|
+
return Object.freeze([
|
|
17
|
+
resourceService(CORE_CACHE_BACKEND_TOKEN, {
|
|
18
|
+
lifetime: "application",
|
|
19
|
+
dependencies: [],
|
|
20
|
+
create: () => ({
|
|
21
|
+
name: "core.cache.backend",
|
|
22
|
+
open: () => ({ ok: true, value: new CacheRepository(createCacheStore(snapshot)) }),
|
|
23
|
+
close: (backend) => backend.close()
|
|
24
|
+
})
|
|
25
|
+
}),
|
|
26
|
+
service(CORE_CACHE_TOKEN, {
|
|
27
|
+
lifetime: "execution",
|
|
28
|
+
dependencies: [CORE_CACHE_BACKEND_TOKEN, CORE_EXECUTION_OPERATIONS_TOKEN],
|
|
29
|
+
create: (backend, operations) => createGuardedCache(backend, operations)
|
|
30
|
+
})
|
|
31
|
+
]);
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
createCacheBindings
|
|
35
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/bootstrap/providers/events.ts
|
|
3
|
+
import { valueService } from "@getstrata/core/contracts/services";
|
|
4
|
+
import { createServiceToken } from "@getstrata/core/contracts/token";
|
|
5
|
+
import { EventBus } from "@getstrata/core/events";
|
|
6
|
+
|
|
7
|
+
// ../../src/bootstrap/config.ts
|
|
8
|
+
import {
|
|
9
|
+
CORE_ABILITY_CHECKER_TOKEN,
|
|
10
|
+
CORE_AUTH_TOKEN,
|
|
11
|
+
CORE_AUTH_USER_DIRECTORY_TOKEN,
|
|
12
|
+
CORE_CACHE_TOKEN,
|
|
13
|
+
CORE_CONFIG_TOKEN,
|
|
14
|
+
CORE_EVENT_BUS_TOKEN,
|
|
15
|
+
CORE_HTTP_CLEANUP_TOKEN,
|
|
16
|
+
CORE_POLICY_GATE_TOKEN,
|
|
17
|
+
CORE_PUBLIC_TENANCY_TOKEN,
|
|
18
|
+
CORE_QUEUE_TOKEN,
|
|
19
|
+
CORE_TENANT_RESOLVER_TOKEN,
|
|
20
|
+
CORE_THROTTLE_QUOTA_POLICY_TOKEN
|
|
21
|
+
} from "@getstrata/core/contracts/serviceTokens";
|
|
22
|
+
var APP_PORT_CONFIG_KEY = "app.port";
|
|
23
|
+
var CACHE_TTL_MS_CONFIG_KEY = "cache.ttlMs";
|
|
24
|
+
var CACHE_MAX_ENTRIES_CONFIG_KEY = "cache.maxEntries";
|
|
25
|
+
var CACHE_DRIVER_CONFIG_KEY = "cache.driver";
|
|
26
|
+
var REDIS_URL_CONFIG_KEY = "cache.redisUrl";
|
|
27
|
+
var DATABASE_URL_CONFIG_KEY = "database.url";
|
|
28
|
+
var CSRF_TARGET_ORIGIN_CONFIG_KEY = "http.csrf.targetOrigin";
|
|
29
|
+
var AUTH_DEV_HEADERS_CONFIG_KEY = "auth.allowDevHeaders";
|
|
30
|
+
var DEFAULT_APP_PORT = 3000;
|
|
31
|
+
var DEFAULT_CACHE_TTL_MS = 3600000;
|
|
32
|
+
var DEFAULT_CACHE_MAX_ENTRIES = 100;
|
|
33
|
+
var DEFAULT_CACHE_DRIVER = "array";
|
|
34
|
+
var DEFAULT_API_TOKEN = "";
|
|
35
|
+
var DEFAULT_QUEUE_DRIVER = "sync";
|
|
36
|
+
|
|
37
|
+
// ../../src/bootstrap/providers/events.ts
|
|
38
|
+
var APPLICATION_EVENT_BUS_TOKEN = createServiceToken("bootstrap.events");
|
|
39
|
+
function createEventBindings() {
|
|
40
|
+
return Object.freeze([valueService(APPLICATION_EVENT_BUS_TOKEN, new EventBus)]);
|
|
41
|
+
}
|
|
42
|
+
function createEventsProvider(bus) {
|
|
43
|
+
return {
|
|
44
|
+
name: "core.events",
|
|
45
|
+
register({ container }) {
|
|
46
|
+
container.set(CORE_EVENT_BUS_TOKEN, bus);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
APPLICATION_EVENT_BUS_TOKEN,
|
|
52
|
+
CORE_EVENT_BUS_TOKEN,
|
|
53
|
+
createEventBindings,
|
|
54
|
+
createEventsProvider
|
|
55
|
+
};
|