@getstrata/bootstrap 2.1.0 → 2.2.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/CHANGELOG.md +25 -0
- package/README.md +1 -1
- package/dist/bootstrap/app.d.ts +7 -1
- package/dist/bootstrap/config.d.ts +1 -1
- package/dist/bootstrap/context.d.ts +8 -2
- package/dist/bootstrap/dependencies.d.ts +1 -1
- package/dist/bootstrap/discoverJobs.d.ts +11 -3
- package/dist/bootstrap/discoverListeners.d.ts +7 -3
- package/dist/bootstrap/discoverModels.d.ts +8 -0
- package/dist/bootstrap/discoverModules.d.ts +4 -1
- package/dist/bootstrap/httpKernel.d.ts +5 -0
- package/dist/bootstrap/inProcessCron.d.ts +2 -1
- package/dist/bootstrap/infrastructureDiscovery.d.ts +9 -0
- package/dist/bootstrap/modules.d.ts +1 -1
- package/dist/bootstrap/providers/tracing.d.ts +3 -0
- package/dist/bootstrap/public-api.d.ts +9 -4
- package/dist/bootstrap/web/index.d.ts +1 -1
- package/dist/bootstrap/web/server.d.ts +15 -7
- package/dist/entries/buildModuleRoutes.js +212 -67
- package/dist/entries/buildWebModuleRoutes.js +212 -67
- package/dist/entries/cache/modelCacheTags.js +157 -51
- package/dist/entries/config.js +6 -0
- package/dist/entries/context.js +426 -188
- package/dist/entries/createWebRoutes.js +214 -69
- package/dist/entries/dependencies.js +428 -190
- package/dist/entries/discoverJobs.js +132 -50
- package/dist/entries/discoverListeners.js +85 -37
- package/dist/entries/discoverModels.js +39 -0
- package/dist/entries/discoverModules.js +158 -51
- package/dist/entries/health.js +3 -0
- package/dist/entries/httpKernel.js +52 -13
- package/dist/entries/listeners/invalidateCacheOnModelWrite.js +157 -51
- package/dist/entries/providers.js +307 -164
- package/dist/entries/web/routing.js +52 -13
- package/dist/entries/web/server.js +69 -14
- package/dist/index.js +590 -216
- package/package.json +8 -3
package/dist/entries/context.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
// ../../src/bootstrap/context.ts
|
|
3
|
+
import { CORE_HTTP_CLEANUP_TOKEN as CORE_HTTP_CLEANUP_TOKEN2 } from "@getstrata/core/contracts/serviceTokens";
|
|
4
|
+
import { clearActiveApplicationContext } from "@getstrata/core/runtime/applicationRegistry";
|
|
5
|
+
|
|
2
6
|
// ../../src/bootstrap/applicationRegistry.ts
|
|
3
7
|
import {
|
|
4
8
|
resolveApplicationAuth,
|
|
@@ -25,74 +29,180 @@ import {
|
|
|
25
29
|
} from "@getstrata/core/contracts/di";
|
|
26
30
|
|
|
27
31
|
// ../../src/bootstrap/discoverModules.ts
|
|
28
|
-
import {
|
|
29
|
-
import { join } from "path";
|
|
32
|
+
import { readdir as readdir2 } from "fs/promises";
|
|
33
|
+
import { join as join2, resolve } from "path";
|
|
34
|
+
|
|
35
|
+
// ../../src/bootstrap/infrastructureDiscovery.ts
|
|
36
|
+
import { readdir } from "fs/promises";
|
|
37
|
+
import { join, relative } from "path";
|
|
30
38
|
import { pathToFileURL } from "url";
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
async function infrastructureFiles(directory, exclude = []) {
|
|
40
|
+
const excluded = new Set([
|
|
41
|
+
"index.ts",
|
|
42
|
+
"index.js",
|
|
43
|
+
"index.mts",
|
|
44
|
+
"index.mjs",
|
|
45
|
+
"register.ts",
|
|
46
|
+
"register.js",
|
|
47
|
+
"register.mts",
|
|
48
|
+
"register.mjs",
|
|
49
|
+
...exclude
|
|
50
|
+
]);
|
|
51
|
+
const files = [];
|
|
52
|
+
async function visit(path) {
|
|
53
|
+
const entries = await readdir(path, { withFileTypes: true });
|
|
54
|
+
entries.sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
const file = join(path, entry.name);
|
|
57
|
+
const key = relative(directory, file).replaceAll("\\", "/");
|
|
58
|
+
if (excluded.has(entry.name) || excluded.has(key))
|
|
59
|
+
continue;
|
|
60
|
+
if (entry.isDirectory())
|
|
61
|
+
await visit(file);
|
|
62
|
+
else if (entry.isFile() && /\.(ts|js|mts|mjs)$/.test(entry.name) && !/\.(d|test|spec)\.[cm]?[jt]s$/.test(entry.name))
|
|
63
|
+
files.push(file);
|
|
64
|
+
}
|
|
36
65
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
66
|
+
try {
|
|
67
|
+
await visit(directory);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (error.code !== "ENOENT")
|
|
70
|
+
throw error;
|
|
71
|
+
if (error.path !== directory)
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
return files;
|
|
75
|
+
}
|
|
76
|
+
async function importInfrastructure(file) {
|
|
77
|
+
try {
|
|
78
|
+
return await import(pathToFileURL(file).href);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
throw new Error(`Cannot load infrastructure module ${file}`, { cause: error });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// ../../src/bootstrap/discoverModules.ts
|
|
85
|
+
var KEY = Symbol.for("@getstrata/discoverModulesState");
|
|
86
|
+
function state() {
|
|
87
|
+
const globals = globalThis;
|
|
88
|
+
globals[KEY] ??= { appModules: [] };
|
|
89
|
+
return globals[KEY];
|
|
40
90
|
}
|
|
41
91
|
function configureModulesDirectory(modulesDir) {
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
92
|
+
const current = state();
|
|
93
|
+
const directory = resolve(modulesDir);
|
|
94
|
+
if (current.configuredModulesDir !== directory || current.configuredManifest) {
|
|
95
|
+
current.appModules.length = 0;
|
|
96
|
+
current.modulesReady = undefined;
|
|
97
|
+
current.source = undefined;
|
|
46
98
|
}
|
|
47
|
-
|
|
99
|
+
current.configuredManifest = undefined;
|
|
100
|
+
current.configuredModulesDir = directory;
|
|
48
101
|
}
|
|
49
|
-
function
|
|
50
|
-
const
|
|
51
|
-
if (
|
|
52
|
-
|
|
102
|
+
function configureModulesManifest(manifest) {
|
|
103
|
+
const current = state();
|
|
104
|
+
if (current.configuredManifest !== manifest) {
|
|
105
|
+
current.appModules.length = 0;
|
|
106
|
+
current.modulesReady = undefined;
|
|
107
|
+
current.source = undefined;
|
|
53
108
|
}
|
|
54
|
-
|
|
55
|
-
|
|
109
|
+
current.configuredManifest = manifest;
|
|
110
|
+
}
|
|
111
|
+
function validateModule(value, location) {
|
|
112
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
113
|
+
throw new TypeError(`Invalid application module at ${location}.`);
|
|
114
|
+
const module = value;
|
|
115
|
+
if (typeof module.name !== "string" || !module.name.trim())
|
|
116
|
+
throw new TypeError(`Invalid module name at ${location}.`);
|
|
117
|
+
if (module.order !== undefined && (typeof module.order !== "number" || !Number.isFinite(module.order)))
|
|
118
|
+
throw new TypeError(`Invalid module order at ${location}.`);
|
|
119
|
+
for (const hook of ["routes", "webRoutes"])
|
|
120
|
+
if (module[hook] !== undefined && typeof module[hook] !== "function")
|
|
121
|
+
throw new TypeError(`Invalid ${hook} at ${location}.`);
|
|
122
|
+
if (module.providers !== undefined) {
|
|
123
|
+
if (!Array.isArray(module.providers))
|
|
124
|
+
throw new TypeError(`Invalid providers at ${location}.`);
|
|
125
|
+
for (const provider of module.providers) {
|
|
126
|
+
if (!provider || typeof provider.name !== "string" || !provider.name.trim() || provider.register !== undefined && typeof provider.register !== "function" || provider.boot !== undefined && typeof provider.boot !== "function")
|
|
127
|
+
throw new TypeError(`Invalid provider at ${location}.`);
|
|
128
|
+
}
|
|
56
129
|
}
|
|
57
|
-
throw new Error("configureModulesDirectory() must be called before discovering modules. The app preload (or starter) should call it.");
|
|
58
130
|
}
|
|
59
|
-
async function
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
131
|
+
async function load(options) {
|
|
132
|
+
const manifest = options.manifest ?? (options.modulesDir === undefined ? state().configuredManifest : undefined);
|
|
133
|
+
const entries = [];
|
|
134
|
+
if (manifest)
|
|
135
|
+
for (const [index, value] of manifest.entries())
|
|
136
|
+
entries.push({ value, location: `module manifest[${index}]` });
|
|
137
|
+
else {
|
|
138
|
+
const directory = options.modulesDir ?? state().configuredModulesDir;
|
|
139
|
+
if (!directory)
|
|
140
|
+
throw new Error("configureModulesDirectory() or configureModulesManifest() must be called before discovering modules.");
|
|
141
|
+
let folders;
|
|
142
|
+
try {
|
|
143
|
+
folders = await readdir2(directory, { withFileTypes: true });
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (error.code === "ENOENT")
|
|
146
|
+
return [];
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
const excluded = new Set(options.exclude ?? []);
|
|
150
|
+
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)) {
|
|
151
|
+
const path = join2(directory, folder.name);
|
|
152
|
+
const files = await readdir2(path);
|
|
153
|
+
const candidates = files.filter((file) => /^index\.(ts|js|mts|mjs)$/.test(file));
|
|
154
|
+
if (candidates.length !== 1)
|
|
155
|
+
throw new Error(`Expected one module entrypoint at ${path}; found ${candidates.length}.`);
|
|
156
|
+
const file = join2(path, candidates[0]);
|
|
157
|
+
const exports = await importInfrastructure(file);
|
|
158
|
+
entries.push({ value: exports.default, location: file });
|
|
67
159
|
}
|
|
68
|
-
throw error;
|
|
69
160
|
}
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (state.appModules.length > 0) {
|
|
80
|
-
return state.appModules;
|
|
161
|
+
const names = new Map;
|
|
162
|
+
const modules = [];
|
|
163
|
+
for (const { value, location } of entries) {
|
|
164
|
+
validateModule(value, location);
|
|
165
|
+
const previous = names.get(value.name);
|
|
166
|
+
if (previous)
|
|
167
|
+
throw new Error(`Duplicate module "${value.name}" at ${location}; first declared at ${previous}.`);
|
|
168
|
+
names.set(value.name, location);
|
|
169
|
+
modules.push(value);
|
|
81
170
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
171
|
+
return modules.sort((left, right) => (left.order ?? 100) - (right.order ?? 100));
|
|
172
|
+
}
|
|
173
|
+
function ensureModulesLoaded(options = {}) {
|
|
174
|
+
const current = state();
|
|
175
|
+
const key = options.manifest ?? (options.modulesDir === undefined ? current.configuredManifest : undefined) ?? JSON.stringify([
|
|
176
|
+
options.modulesDir ? resolve(options.modulesDir) : current.configuredModulesDir,
|
|
177
|
+
[...options.exclude ?? []].sort()
|
|
178
|
+
]);
|
|
179
|
+
if (current.modulesReady && current.source === key)
|
|
180
|
+
return current.modulesReady;
|
|
181
|
+
current.appModules.length = 0;
|
|
182
|
+
current.source = key;
|
|
183
|
+
const pending = load(options).then((modules) => {
|
|
184
|
+
if (current.modulesReady !== pending)
|
|
185
|
+
return modules;
|
|
186
|
+
current.appModules.splice(0, current.appModules.length, ...modules);
|
|
187
|
+
return current.appModules;
|
|
188
|
+
}).catch((error) => {
|
|
189
|
+
if (current.modulesReady === pending)
|
|
190
|
+
current.modulesReady = undefined;
|
|
191
|
+
throw error;
|
|
85
192
|
});
|
|
86
|
-
|
|
193
|
+
current.modulesReady = pending;
|
|
194
|
+
return pending;
|
|
87
195
|
}
|
|
88
196
|
function discoverModules() {
|
|
89
|
-
return
|
|
197
|
+
return state().appModules;
|
|
90
198
|
}
|
|
91
199
|
function resetDiscoverModulesForTests() {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
200
|
+
const current = state();
|
|
201
|
+
current.configuredModulesDir = undefined;
|
|
202
|
+
current.configuredManifest = undefined;
|
|
203
|
+
current.appModules.length = 0;
|
|
204
|
+
current.modulesReady = undefined;
|
|
205
|
+
current.source = undefined;
|
|
96
206
|
}
|
|
97
207
|
// ../../src/bootstrap/providers/auth.ts
|
|
98
208
|
import { BasicAuthGuard } from "@getstrata/core/auth/basicAuthGuard";
|
|
@@ -120,8 +230,11 @@ import {
|
|
|
120
230
|
CORE_CACHE_TOKEN,
|
|
121
231
|
CORE_CONFIG_TOKEN,
|
|
122
232
|
CORE_EVENT_BUS_TOKEN,
|
|
233
|
+
CORE_HTTP_CLEANUP_TOKEN,
|
|
123
234
|
CORE_POLICY_GATE_TOKEN,
|
|
124
235
|
CORE_QUEUE_TOKEN,
|
|
236
|
+
CORE_TENANT_RESOLVER_TOKEN,
|
|
237
|
+
CORE_THROTTLE_QUOTA_POLICY_TOKEN,
|
|
125
238
|
CORE_TOKEN_SERVICE_TOKEN
|
|
126
239
|
} from "@getstrata/core/contracts/serviceTokens";
|
|
127
240
|
var APP_PORT_CONFIG_KEY = "app.port";
|
|
@@ -168,7 +281,7 @@ import { createCacheStore } from "@getstrata/core/cache/createCacheStore";
|
|
|
168
281
|
import { CacheRepository } from "@getstrata/core/cache/repository";
|
|
169
282
|
var cacheProvider = {
|
|
170
283
|
name: "core.cache",
|
|
171
|
-
register({ container, config, dependencies }) {
|
|
284
|
+
register({ container, config, dependencies, onCleanup }) {
|
|
172
285
|
container.singleton(CORE_CACHE_TOKEN, () => {
|
|
173
286
|
const store = createCacheStore({
|
|
174
287
|
driver: config.require(CACHE_DRIVER_CONFIG_KEY),
|
|
@@ -176,7 +289,9 @@ var cacheProvider = {
|
|
|
176
289
|
maxEntries: config.require(CACHE_MAX_ENTRIES_CONFIG_KEY),
|
|
177
290
|
redisUrl: config.get(REDIS_URL_CONFIG_KEY) || undefined
|
|
178
291
|
});
|
|
179
|
-
|
|
292
|
+
const cache = new CacheRepository(store);
|
|
293
|
+
onCleanup(() => cache.close());
|
|
294
|
+
return cache;
|
|
180
295
|
});
|
|
181
296
|
dependencies.cache = container.resolve(CORE_CACHE_TOKEN);
|
|
182
297
|
}
|
|
@@ -373,53 +488,50 @@ var eventsProvider = {
|
|
|
373
488
|
var events_default = eventsProvider;
|
|
374
489
|
|
|
375
490
|
// ../../src/bootstrap/discoverListeners.ts
|
|
376
|
-
import {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
const existing = globalThis[DISCOVER_LISTENERS_STATE_KEY];
|
|
383
|
-
if (existing) {
|
|
384
|
-
return existing;
|
|
385
|
-
}
|
|
386
|
-
const state = {};
|
|
387
|
-
globalThis[DISCOVER_LISTENERS_STATE_KEY] = state;
|
|
388
|
-
return state;
|
|
389
|
-
}
|
|
390
|
-
function resolveListenersDirectory() {
|
|
391
|
-
const fromCwd = join2(process.cwd(), "src", "listeners");
|
|
392
|
-
if (existsSync(fromCwd)) {
|
|
393
|
-
return fromCwd;
|
|
394
|
-
}
|
|
395
|
-
return join2(import.meta.dir, "../listeners");
|
|
491
|
+
import { join as join3, resolve as resolve2 } from "path";
|
|
492
|
+
var KEY2 = Symbol.for("@getstrata/discoverListenersState");
|
|
493
|
+
function state2() {
|
|
494
|
+
const globals = globalThis;
|
|
495
|
+
globals[KEY2] ??= { loads: new Map };
|
|
496
|
+
return globals[KEY2];
|
|
396
497
|
}
|
|
397
|
-
function
|
|
398
|
-
const
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
498
|
+
async function load2(options) {
|
|
499
|
+
const listeners = new Set;
|
|
500
|
+
const add = (value, location) => {
|
|
501
|
+
if (typeof value !== "function")
|
|
502
|
+
throw new TypeError(`Invalid listener registrar at ${location}: expected a default function.`);
|
|
503
|
+
listeners.add(value);
|
|
504
|
+
};
|
|
505
|
+
if (options.manifest)
|
|
506
|
+
for (const [index, value] of options.manifest.entries())
|
|
507
|
+
add(value, `listener manifest[${index}]`);
|
|
508
|
+
else
|
|
509
|
+
for (const file of await infrastructureFiles(options.directory ?? join3(process.cwd(), "src", "listeners"), options.exclude)) {
|
|
510
|
+
const exports = await importInfrastructure(file);
|
|
511
|
+
const value = exports.default;
|
|
512
|
+
add(value && typeof value === "object" && "default" in value ? value.default : value, file);
|
|
405
513
|
}
|
|
514
|
+
return [...listeners];
|
|
515
|
+
}
|
|
516
|
+
function discoverListeners(options = {}) {
|
|
517
|
+
const cache = state2().loads;
|
|
518
|
+
const key = options.manifest ?? JSON.stringify([
|
|
519
|
+
resolve2(options.directory ?? join3(process.cwd(), "src", "listeners")),
|
|
520
|
+
[...options.exclude ?? []].sort()
|
|
521
|
+
]);
|
|
522
|
+
const existing = cache.get(key);
|
|
523
|
+
if (existing)
|
|
524
|
+
return existing;
|
|
525
|
+
const pending = load2(options).catch((error) => {
|
|
526
|
+
if (cache.get(key) === pending)
|
|
527
|
+
cache.delete(key);
|
|
406
528
|
throw error;
|
|
407
|
-
}
|
|
408
|
-
const listeners = entries.map((fileName) => {
|
|
409
|
-
const filePath = join2(listenersDirectory, fileName);
|
|
410
|
-
const loaded = requireListener(filePath);
|
|
411
|
-
return loaded.default;
|
|
412
529
|
});
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
function discoverListeners() {
|
|
416
|
-
const state = readDiscoverListenersState();
|
|
417
|
-
state.appListeners ??= loadDiscoveredListeners();
|
|
418
|
-
return state.appListeners;
|
|
530
|
+
cache.set(key, pending);
|
|
531
|
+
return pending;
|
|
419
532
|
}
|
|
420
533
|
function resetDiscoverListenersForTests() {
|
|
421
|
-
|
|
422
|
-
state.appListeners = undefined;
|
|
534
|
+
state2().loads = new Map;
|
|
423
535
|
}
|
|
424
536
|
|
|
425
537
|
// ../../src/bootstrap/listeners/invalidateCacheOnModelWrite.ts
|
|
@@ -541,22 +653,12 @@ function registerInvalidateCacheOnModelWriteListeners(bus = eventBus2, options =
|
|
|
541
653
|
}
|
|
542
654
|
|
|
543
655
|
// ../../src/bootstrap/providers/listeners.ts
|
|
544
|
-
var registeredListenerGroups = new Set;
|
|
545
|
-
function registerListenerGroup(name, register) {
|
|
546
|
-
if (registeredListenerGroups.has(name)) {
|
|
547
|
-
return;
|
|
548
|
-
}
|
|
549
|
-
registeredListenerGroups.add(name);
|
|
550
|
-
register();
|
|
551
|
-
}
|
|
552
656
|
var listenersProvider = {
|
|
553
657
|
name: "core.listeners",
|
|
554
|
-
boot() {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
for (const registerListener of discoverListeners()) {
|
|
559
|
-
registerListener();
|
|
658
|
+
async boot({ onCleanup }) {
|
|
659
|
+
onCleanup(registerInvalidateCacheOnModelWriteListeners());
|
|
660
|
+
for (const registerListener of await discoverListeners()) {
|
|
661
|
+
await registerListener();
|
|
560
662
|
}
|
|
561
663
|
}
|
|
562
664
|
};
|
|
@@ -580,68 +682,99 @@ import {
|
|
|
580
682
|
} from "@getstrata/core/queue/createAppQueue";
|
|
581
683
|
|
|
582
684
|
// ../../src/bootstrap/discoverJobs.ts
|
|
583
|
-
import {
|
|
584
|
-
import {
|
|
585
|
-
import { join as join3 } from "path";
|
|
685
|
+
import { join as join4, resolve as resolve3 } from "path";
|
|
686
|
+
import { Job } from "@getstrata/core/queue";
|
|
586
687
|
import { jobRegistry } from "@getstrata/core/queue/jobRegistry";
|
|
587
|
-
var
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
return existing;
|
|
593
|
-
}
|
|
594
|
-
const state = {};
|
|
595
|
-
globalThis[DISCOVER_JOBS_STATE_KEY] = state;
|
|
596
|
-
return state;
|
|
597
|
-
}
|
|
598
|
-
function resolveJobsDirectory() {
|
|
599
|
-
const fromCwd = join3(process.cwd(), "src", "jobs");
|
|
600
|
-
if (existsSync2(fromCwd)) {
|
|
601
|
-
return fromCwd;
|
|
602
|
-
}
|
|
603
|
-
return join3(import.meta.dir, "../jobs");
|
|
688
|
+
var KEY3 = Symbol.for("@getstrata/discoverJobsState");
|
|
689
|
+
function state3() {
|
|
690
|
+
const globals = globalThis;
|
|
691
|
+
globals[KEY3] ??= { loads: new Map, registered: new Map };
|
|
692
|
+
return globals[KEY3];
|
|
604
693
|
}
|
|
605
|
-
function
|
|
606
|
-
if (typeof value !== "function")
|
|
607
|
-
|
|
608
|
-
}
|
|
609
|
-
const jobName = value.jobName;
|
|
610
|
-
return typeof jobName === "string" && jobName.length > 0;
|
|
694
|
+
function validateClass(value, location) {
|
|
695
|
+
if (typeof value !== "function" || typeof value.jobName !== "string" || !value.jobName.trim() || !(value.prototype instanceof Job) && typeof value.prototype?.handle !== "function")
|
|
696
|
+
throw new TypeError(`Invalid job at ${location}: expected a class with nonempty static jobName and handle().`);
|
|
611
697
|
}
|
|
612
|
-
function
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
if (
|
|
619
|
-
|
|
698
|
+
async function load3(options, current) {
|
|
699
|
+
const jobs = new Map;
|
|
700
|
+
const add = (name, identity, create, location) => {
|
|
701
|
+
const previous = jobs.get(name);
|
|
702
|
+
if (previous && previous.identity !== identity)
|
|
703
|
+
throw new Error(`Duplicate job "${name}" at ${location}; first declared at ${previous.location}.`);
|
|
704
|
+
if (!previous)
|
|
705
|
+
jobs.set(name, { identity, create, location });
|
|
706
|
+
};
|
|
707
|
+
if (options.manifest) {
|
|
708
|
+
for (const [index, entry] of options.manifest.entries()) {
|
|
709
|
+
const location = `job manifest[${index}]`;
|
|
710
|
+
if (typeof entry === "function") {
|
|
711
|
+
validateClass(entry, location);
|
|
712
|
+
add(entry.jobName, entry, () => new entry, location);
|
|
713
|
+
} else {
|
|
714
|
+
if (!entry || typeof entry.name !== "string" || !entry.name.trim() || typeof entry.create !== "function")
|
|
715
|
+
throw new TypeError(`Invalid job factory at ${location}.`);
|
|
716
|
+
add(entry.name, entry.create, entry.create, location);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
for (const file of await infrastructureFiles(options.directory ?? join4(process.cwd(), "src", "jobs"), options.exclude)) {
|
|
721
|
+
const exports = await importInfrastructure(file);
|
|
722
|
+
const defaultValue = exports.default;
|
|
723
|
+
const legacy = defaultValue && typeof defaultValue === "object" && "default" in defaultValue ? defaultValue.default : defaultValue;
|
|
724
|
+
if (legacy !== undefined)
|
|
725
|
+
validateClass(legacy, `${file}:default`);
|
|
726
|
+
let found = false;
|
|
727
|
+
for (const [name, value] of Object.entries({
|
|
728
|
+
...exports,
|
|
729
|
+
...legacy === undefined ? {} : { default: legacy }
|
|
730
|
+
})) {
|
|
731
|
+
if (name !== "default" && !(typeof value === "function" && ("jobName" in value)))
|
|
732
|
+
continue;
|
|
733
|
+
validateClass(value, `${file}:${name}`);
|
|
734
|
+
found = true;
|
|
735
|
+
add(value.jobName, value, () => new value, `${file}:${name}`);
|
|
736
|
+
}
|
|
737
|
+
if (!found)
|
|
738
|
+
throw new TypeError(`No job exports at ${file}; exclude helper modules explicitly.`);
|
|
620
739
|
}
|
|
621
|
-
throw error;
|
|
622
740
|
}
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
const state = readDiscoverJobsState();
|
|
631
|
-
if (state.names) {
|
|
632
|
-
return state.names;
|
|
741
|
+
if (!current())
|
|
742
|
+
return [...jobs.keys()];
|
|
743
|
+
const registered = state3().registered;
|
|
744
|
+
for (const [name, entry] of jobs) {
|
|
745
|
+
const previous = registered.get(name);
|
|
746
|
+
if (previous && previous.identity !== entry.identity)
|
|
747
|
+
throw new Error(`Duplicate discovered job "${name}" at ${entry.location}; first declared at ${previous.location}.`);
|
|
633
748
|
}
|
|
634
|
-
const
|
|
635
|
-
for (const
|
|
636
|
-
|
|
637
|
-
|
|
749
|
+
const existing = new Set(jobRegistry.names());
|
|
750
|
+
for (const [name, entry] of jobs) {
|
|
751
|
+
if (!existing.has(name)) {
|
|
752
|
+
jobRegistry.register(name, entry.create);
|
|
753
|
+
registered.set(name, entry);
|
|
754
|
+
}
|
|
638
755
|
}
|
|
639
|
-
|
|
640
|
-
|
|
756
|
+
return [...jobs.keys()];
|
|
757
|
+
}
|
|
758
|
+
function discoverJobs(options = {}) {
|
|
759
|
+
const cache = state3().loads;
|
|
760
|
+
const key = options.manifest ?? JSON.stringify([
|
|
761
|
+
resolve3(options.directory ?? join4(process.cwd(), "src", "jobs")),
|
|
762
|
+
[...options.exclude ?? []].sort()
|
|
763
|
+
]);
|
|
764
|
+
const existing = cache.get(key);
|
|
765
|
+
if (existing)
|
|
766
|
+
return existing;
|
|
767
|
+
const pending = load3(options, () => state3().loads === cache).catch((error) => {
|
|
768
|
+
if (cache.get(key) === pending)
|
|
769
|
+
cache.delete(key);
|
|
770
|
+
throw error;
|
|
771
|
+
});
|
|
772
|
+
cache.set(key, pending);
|
|
773
|
+
return pending;
|
|
641
774
|
}
|
|
642
775
|
function resetDiscoverJobsForTests() {
|
|
643
|
-
|
|
644
|
-
|
|
776
|
+
state3().loads = new Map;
|
|
777
|
+
state3().registered = new Map;
|
|
645
778
|
}
|
|
646
779
|
|
|
647
780
|
// ../../src/bootstrap/queue/defaultJobs.ts
|
|
@@ -659,16 +792,17 @@ function registerDefaultJobs() {
|
|
|
659
792
|
// ../../src/bootstrap/providers/queue.ts
|
|
660
793
|
var queueProvider = {
|
|
661
794
|
name: "core.queue",
|
|
662
|
-
register({ container, config }) {
|
|
795
|
+
async register({ container, config, onCleanup }) {
|
|
663
796
|
const configuredDriver = process.env.QUEUE_DRIVER ?? DEFAULT_QUEUE_DRIVER;
|
|
664
797
|
const driver = configuredDriver === "async" || configuredDriver === "redis" || configuredDriver === "sync" ? configuredDriver : DEFAULT_QUEUE_DRIVER;
|
|
665
798
|
config.set("queue.driver", driver);
|
|
666
799
|
const failedJobs = createFailedJobService();
|
|
667
800
|
container.set(FAILED_JOB_SERVICE_TOKEN, failedJobs);
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
801
|
+
registerDefaultJobs();
|
|
802
|
+
await discoverJobs();
|
|
803
|
+
const queue = createAppQueue(driver, config.get(REDIS_URL_CONFIG_KEY) ?? process.env.REDIS_URL, failedJobs);
|
|
804
|
+
onCleanup(() => queue.close?.(), driver === "async" ? "drain" : "close");
|
|
805
|
+
container.set(CORE_QUEUE_TOKEN, queue);
|
|
672
806
|
}
|
|
673
807
|
};
|
|
674
808
|
var queue_default = queueProvider;
|
|
@@ -683,6 +817,18 @@ var storageProvider = {
|
|
|
683
817
|
};
|
|
684
818
|
var storage_default = storageProvider;
|
|
685
819
|
|
|
820
|
+
// ../../src/bootstrap/providers/tracing.ts
|
|
821
|
+
import { acquireTracingRuntime } from "@getstrata/core/tracing/tracingMiddleware";
|
|
822
|
+
var tracingProvider = {
|
|
823
|
+
name: "core.tracing",
|
|
824
|
+
register({ onCleanup }) {
|
|
825
|
+
const ownership = acquireTracingRuntime();
|
|
826
|
+
onCleanup(() => ownership.flush(), "flush");
|
|
827
|
+
onCleanup(() => ownership.release(), "close");
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
var tracing_default = tracingProvider;
|
|
831
|
+
|
|
686
832
|
// ../../src/bootstrap/providers/view.ts
|
|
687
833
|
import { currentRequestMeta } from "@getstrata/core/http/requestMetaContext";
|
|
688
834
|
import { appDisplayName } from "@getstrata/core/runtime/appKeyPrefix";
|
|
@@ -727,6 +873,7 @@ var viewProvider = {
|
|
|
727
873
|
// ../../src/bootstrap/providers/index.ts
|
|
728
874
|
var coreProviders = [
|
|
729
875
|
config_default,
|
|
876
|
+
tracing_default,
|
|
730
877
|
cache_default,
|
|
731
878
|
storage_default,
|
|
732
879
|
auth_default,
|
|
@@ -741,38 +888,129 @@ var coreProviders = [
|
|
|
741
888
|
function collectProviders(modules = discoverModules()) {
|
|
742
889
|
return [...coreProviders, ...modules.flatMap((module) => module.providers ?? [])];
|
|
743
890
|
}
|
|
744
|
-
function runProviderPhase(providers, phase, context) {
|
|
745
|
-
for (const provider of providers)
|
|
746
|
-
provider[phase]?.(context);
|
|
747
|
-
}
|
|
891
|
+
async function runProviderPhase(providers, phase, context) {
|
|
892
|
+
for (const provider of providers)
|
|
893
|
+
await provider[phase]?.(context);
|
|
748
894
|
}
|
|
749
|
-
|
|
895
|
+
var readyContext;
|
|
896
|
+
async function createAppContext(providers) {
|
|
897
|
+
if (!providers)
|
|
898
|
+
await ensureModulesLoaded();
|
|
899
|
+
const configured = [...providers ?? collectProviders()];
|
|
750
900
|
const container = new ServiceContainer;
|
|
751
901
|
const config = new ConfigStore;
|
|
752
|
-
const dependencies = {
|
|
753
|
-
|
|
902
|
+
const dependencies = { container };
|
|
903
|
+
const cleanups = [];
|
|
904
|
+
const drains = [];
|
|
905
|
+
let draining;
|
|
906
|
+
const drain = () => {
|
|
907
|
+
draining ??= (async () => {
|
|
908
|
+
const errors = [];
|
|
909
|
+
for (const handler of drains.splice(0).reverse()) {
|
|
910
|
+
try {
|
|
911
|
+
await handler();
|
|
912
|
+
} catch (error) {
|
|
913
|
+
errors.push(error);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
if (errors.length)
|
|
917
|
+
throw new AggregateError(errors, "Provider drain failed.");
|
|
918
|
+
})();
|
|
919
|
+
return draining;
|
|
754
920
|
};
|
|
755
|
-
const
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
921
|
+
const flushes = [];
|
|
922
|
+
let flushing;
|
|
923
|
+
const flush = () => {
|
|
924
|
+
flushing ??= (async () => {
|
|
925
|
+
const errors = [];
|
|
926
|
+
for (const handler of flushes.splice(0).reverse()) {
|
|
927
|
+
try {
|
|
928
|
+
await handler();
|
|
929
|
+
} catch (error) {
|
|
930
|
+
errors.push(error);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (errors.length)
|
|
934
|
+
throw new AggregateError(errors, "Provider flush failed.");
|
|
935
|
+
})();
|
|
936
|
+
return flushing;
|
|
759
937
|
};
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
938
|
+
let disposal;
|
|
939
|
+
let disposed = false;
|
|
940
|
+
let initialized;
|
|
941
|
+
const dispose = () => {
|
|
942
|
+
disposed = true;
|
|
943
|
+
disposal ??= (async () => {
|
|
944
|
+
await drain();
|
|
945
|
+
const errors = [];
|
|
946
|
+
try {
|
|
947
|
+
await flush();
|
|
948
|
+
} catch (error) {
|
|
949
|
+
errors.push(error);
|
|
950
|
+
}
|
|
951
|
+
if (initialized) {
|
|
952
|
+
clearActiveApplicationContext(initialized);
|
|
953
|
+
if (readyContext === initialized)
|
|
954
|
+
readyContext = undefined;
|
|
955
|
+
}
|
|
956
|
+
for (const handler of cleanups.splice(0).reverse()) {
|
|
957
|
+
try {
|
|
958
|
+
await handler();
|
|
959
|
+
} catch (error) {
|
|
960
|
+
errors.push(error);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
if (errors.length)
|
|
964
|
+
throw new AggregateError(errors, "Provider cleanup failed.");
|
|
965
|
+
})();
|
|
966
|
+
return disposal;
|
|
967
|
+
};
|
|
968
|
+
const context = {
|
|
765
969
|
container,
|
|
766
970
|
config,
|
|
767
|
-
dependencies
|
|
971
|
+
dependencies,
|
|
972
|
+
onCleanup(handler, phase = "close") {
|
|
973
|
+
if (disposed)
|
|
974
|
+
throw new Error("Cannot register cleanup on a disposed application context.");
|
|
975
|
+
if (phase !== "drain" && phase !== "flush" && phase !== "close")
|
|
976
|
+
throw new TypeError("Unknown provider cleanup phase.");
|
|
977
|
+
if (phase === "drain" && draining || phase === "flush" && flushing)
|
|
978
|
+
throw new Error("Cannot register cleanup after its phase started.");
|
|
979
|
+
(phase === "drain" ? drains : phase === "flush" ? flushes : cleanups).push(handler);
|
|
980
|
+
}
|
|
768
981
|
};
|
|
769
|
-
|
|
770
|
-
|
|
982
|
+
container.set(CORE_HTTP_CLEANUP_TOKEN2, (handler) => context.onCleanup(handler));
|
|
983
|
+
try {
|
|
984
|
+
await runProviderPhase(configured, "register", context);
|
|
985
|
+
await runProviderPhase(configured, "boot", context);
|
|
986
|
+
assertAppDependenciesComplete(dependencies);
|
|
987
|
+
const appContext = {
|
|
988
|
+
container,
|
|
989
|
+
config,
|
|
990
|
+
dependencies,
|
|
991
|
+
drain,
|
|
992
|
+
flush,
|
|
993
|
+
dispose
|
|
994
|
+
};
|
|
995
|
+
initialized = appContext;
|
|
996
|
+
setActiveApplicationContext(appContext);
|
|
997
|
+
readyContext = appContext;
|
|
998
|
+
return appContext;
|
|
999
|
+
} catch (error) {
|
|
1000
|
+
try {
|
|
1001
|
+
await dispose();
|
|
1002
|
+
} catch (cleanupError) {
|
|
1003
|
+
throw new AggregateError([error, cleanupError], "Application startup and cleanup failed.", {
|
|
1004
|
+
cause: error
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
throw error;
|
|
1008
|
+
}
|
|
771
1009
|
}
|
|
772
|
-
var cachedAppContext;
|
|
773
1010
|
function getAppContext() {
|
|
774
|
-
|
|
775
|
-
|
|
1011
|
+
if (!readyContext)
|
|
1012
|
+
throw new Error("Application context is not ready. Await createAppContext() before using appContext.");
|
|
1013
|
+
return readyContext;
|
|
776
1014
|
}
|
|
777
1015
|
var appContext = {
|
|
778
1016
|
get container() {
|