@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.
Files changed (37) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/README.md +1 -1
  3. package/dist/bootstrap/app.d.ts +7 -1
  4. package/dist/bootstrap/config.d.ts +1 -1
  5. package/dist/bootstrap/context.d.ts +8 -2
  6. package/dist/bootstrap/dependencies.d.ts +1 -1
  7. package/dist/bootstrap/discoverJobs.d.ts +11 -3
  8. package/dist/bootstrap/discoverListeners.d.ts +7 -3
  9. package/dist/bootstrap/discoverModels.d.ts +8 -0
  10. package/dist/bootstrap/discoverModules.d.ts +4 -1
  11. package/dist/bootstrap/httpKernel.d.ts +5 -0
  12. package/dist/bootstrap/inProcessCron.d.ts +2 -1
  13. package/dist/bootstrap/infrastructureDiscovery.d.ts +9 -0
  14. package/dist/bootstrap/modules.d.ts +1 -1
  15. package/dist/bootstrap/providers/tracing.d.ts +3 -0
  16. package/dist/bootstrap/public-api.d.ts +9 -4
  17. package/dist/bootstrap/web/index.d.ts +1 -1
  18. package/dist/bootstrap/web/server.d.ts +15 -7
  19. package/dist/entries/buildModuleRoutes.js +212 -67
  20. package/dist/entries/buildWebModuleRoutes.js +212 -67
  21. package/dist/entries/cache/modelCacheTags.js +157 -51
  22. package/dist/entries/config.js +6 -0
  23. package/dist/entries/context.js +426 -188
  24. package/dist/entries/createWebRoutes.js +214 -69
  25. package/dist/entries/dependencies.js +428 -190
  26. package/dist/entries/discoverJobs.js +132 -50
  27. package/dist/entries/discoverListeners.js +85 -37
  28. package/dist/entries/discoverModels.js +39 -0
  29. package/dist/entries/discoverModules.js +158 -51
  30. package/dist/entries/health.js +3 -0
  31. package/dist/entries/httpKernel.js +52 -13
  32. package/dist/entries/listeners/invalidateCacheOnModelWrite.js +157 -51
  33. package/dist/entries/providers.js +307 -164
  34. package/dist/entries/web/routing.js +52 -13
  35. package/dist/entries/web/server.js +69 -14
  36. package/dist/index.js +590 -216
  37. package/package.json +8 -3
@@ -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 { readdirSync } from "fs";
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
- var DISCOVER_MODULES_STATE_KEY = Symbol.for("@getstrata/discoverModulesState");
32
- function readDiscoverModulesState() {
33
- const existing = globalThis[DISCOVER_MODULES_STATE_KEY];
34
- if (existing) {
35
- return existing;
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
- const state = { appModules: [] };
38
- globalThis[DISCOVER_MODULES_STATE_KEY] = state;
39
- return state;
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 state = readDiscoverModulesState();
43
- if (state.configuredModulesDir !== modulesDir) {
44
- state.appModules.length = 0;
45
- state.modulesReady = undefined;
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
- state.configuredModulesDir = modulesDir;
99
+ current.configuredManifest = undefined;
100
+ current.configuredModulesDir = directory;
48
101
  }
49
- function resolveModulesDirectory(options) {
50
- const state = readDiscoverModulesState();
51
- if (options?.modulesDir) {
52
- return options.modulesDir;
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
- if (state.configuredModulesDir) {
55
- return state.configuredModulesDir;
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 loadDiscoveredModules(options) {
60
- const modulesDirectory = resolveModulesDirectory(options);
61
- let moduleNames;
62
- try {
63
- moduleNames = readdirSync(modulesDirectory, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name);
64
- } catch (error) {
65
- if (error.code === "ENOENT") {
66
- return [];
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 modules = await Promise.all(moduleNames.map(async (moduleName) => {
71
- const moduleUrl = pathToFileURL(join(modulesDirectory, moduleName, "index.ts")).href;
72
- const loaded = await import(moduleUrl);
73
- return loaded.default;
74
- }));
75
- return modules.filter((module) => module?.name !== undefined).sort((left, right) => (left.order ?? 100) - (right.order ?? 100));
76
- }
77
- async function ensureModulesLoaded(options) {
78
- const state = readDiscoverModulesState();
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
- state.modulesReady ??= loadDiscoveredModules(options).then((modules) => {
83
- state.appModules.splice(0, state.appModules.length, ...modules);
84
- return state.appModules;
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
- return state.modulesReady;
193
+ current.modulesReady = pending;
194
+ return pending;
87
195
  }
88
196
  function discoverModules() {
89
- return readDiscoverModulesState().appModules;
197
+ return state().appModules;
90
198
  }
91
199
  function resetDiscoverModulesForTests() {
92
- const state = readDiscoverModulesState();
93
- state.configuredModulesDir = undefined;
94
- state.appModules.length = 0;
95
- state.modulesReady = undefined;
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
- return new CacheRepository(store);
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 { existsSync, readdirSync as readdirSync2 } from "fs";
377
- import { createRequire } from "module";
378
- import { join as join2 } from "path";
379
- var requireListener = createRequire(import.meta.url);
380
- var DISCOVER_LISTENERS_STATE_KEY = Symbol.for("@getstrata/discoverListenersState");
381
- function readDiscoverListenersState() {
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 loadDiscoveredListeners() {
398
- const listenersDirectory = resolveListenersDirectory();
399
- let entries;
400
- try {
401
- entries = readdirSync2(listenersDirectory, { withFileTypes: true }).filter((entry) => entry.isFile() && /\.(ts|js)$/.test(entry.name)).map((entry) => entry.name);
402
- } catch (error) {
403
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
404
- return [];
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
- return listeners.filter((listener) => typeof listener === "function");
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
- const state = readDiscoverListenersState();
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
- registerListenerGroup("cache.invalidate-on-model-write", () => {
556
- registerInvalidateCacheOnModelWriteListeners();
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 { existsSync as existsSync2, readdirSync as readdirSync3 } from "fs";
584
- import { createRequire as createRequire2 } from "module";
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 requireJob = createRequire2(import.meta.url);
588
- var DISCOVER_JOBS_STATE_KEY = Symbol.for("@getstrata/discoverJobsState");
589
- function readDiscoverJobsState() {
590
- const existing = globalThis[DISCOVER_JOBS_STATE_KEY];
591
- if (existing) {
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 isDiscoveredJobClass(value) {
606
- if (typeof value !== "function") {
607
- return false;
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 loadDiscoveredJobClasses() {
613
- const jobsDirectory = resolveJobsDirectory();
614
- let entries;
615
- try {
616
- entries = readdirSync3(jobsDirectory, { withFileTypes: true }).filter((entry) => entry.isFile() && /\.(ts|js)$/.test(entry.name)).map((entry) => entry.name);
617
- } catch (error) {
618
- if (error instanceof Error && "code" in error && error.code === "ENOENT") {
619
- return [];
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
- return entries.flatMap((fileName) => {
624
- const filePath = join3(jobsDirectory, fileName);
625
- const loaded = requireJob(filePath);
626
- return isDiscoveredJobClass(loaded.default) ? [loaded.default] : [];
627
- });
628
- }
629
- function discoverJobs() {
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 names = [];
635
- for (const JobClass of loadDiscoveredJobClasses()) {
636
- jobRegistry.register(JobClass.jobName, () => new JobClass);
637
- names.push(JobClass.jobName);
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
- state.names = names;
640
- return names;
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
- const state = readDiscoverJobsState();
644
- state.names = undefined;
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
- container.set(CORE_QUEUE_TOKEN, createAppQueue(driver, config.get(REDIS_URL_CONFIG_KEY) ?? process.env.REDIS_URL, failedJobs, () => {
669
- registerDefaultJobs();
670
- discoverJobs();
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
- function createAppContext() {
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
- container
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 context = {
756
- container,
757
- config,
758
- dependencies
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
- const providers = collectProviders();
761
- runProviderPhase(providers, "register", context);
762
- runProviderPhase(providers, "boot", context);
763
- assertAppDependenciesComplete(dependencies);
764
- const appContext = {
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
- setActiveApplicationContext(appContext);
770
- return appContext;
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
- cachedAppContext ??= createAppContext();
775
- return cachedAppContext;
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() {