@getstrata/bootstrap 2.2.47 → 2.2.49

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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # @getstrata/bootstrap changelog
2
2
 
3
+ ## 2.2.49
4
+
5
+ - Supply the real shared conditional cache operation to API and web module contexts, preserving tags, ETags, invalidation and error recovery.
6
+ - Keep provider phase admission inside lifecycle ownership, retaining reverse cleanup, drain/flush/close ordering and shared application identity.
7
+ - Preserve native callback arguments and cloned Response routes, apply CSRF correctly to mixed method maps, and pair normalized route aliases with the winning handler's own metadata. Existing whole-path override precedence remains intact.
8
+ - Align core requirements with 2.2.49. Opaque native file/directory/HTML routes remain a known adapter limitation; this patch does not bypass request admission or introduce a public-assets API.
9
+
10
+ ## 2.2.48
11
+
12
+ - Align the core peer requirement with 2.2.48 for strict environment validation, compatible repository contracts and dependency-cycle diagnostics. Existing HTTP kernel, module-context and provider contracts remain unchanged.
13
+
3
14
  ## 2.2.47
4
15
 
5
16
  - Align the package version with the starter/CLI correction batch; no bootstrap runtime behavior changes.
@@ -1,11 +1,10 @@
1
- import type { OpenApiRouteMap } from "@getstrata/core/openapi/registeredRoute";
2
1
  import type { AppDependencies, AppModule, AppRouteMap } from "./contracts";
2
+ import { registerOpenApiRouteMap } from "./routeMetadata";
3
3
  interface BuildModuleRoutesOptions {
4
4
  apiPrefix?: string;
5
5
  modules?: AppModule[];
6
6
  clearRegistry?: boolean;
7
7
  }
8
- declare function registerOpenApiRouteMap(routes: Record<string, unknown>, middleware: string[], metadata?: OpenApiRouteMap): Record<string, unknown>;
9
8
  declare function buildModuleRoutes(dependencies: AppDependencies, options?: BuildModuleRoutesOptions): AppRouteMap;
10
9
  export type { BuildModuleRoutesOptions };
11
10
  export { buildModuleRoutes, registerOpenApiRouteMap };
@@ -0,0 +1,3 @@
1
+ import type { AppDependencies, CachedJson } from "./contracts";
2
+ declare function createCachedJson(dependencies: Pick<AppDependencies, "cache">): CachedJson;
3
+ export { createCachedJson };
@@ -0,0 +1,4 @@
1
+ import { type Middleware } from "@getstrata/core/http/middleware";
2
+ /** Module callbacks must receive middleware even when inert fields share their method map. */
3
+ declare function applyModuleRouteMiddleware(routes: Record<string, unknown>, middleware: Middleware[]): Record<string, unknown>;
4
+ export { applyModuleRouteMiddleware };
@@ -1,2 +1,2 @@
1
- declare function prefixRouteMap(prefix: string, routes: Record<string, unknown>): Record<string, unknown>;
1
+ declare function prefixRouteMap<Value = unknown>(prefix: string, routes: Record<string, Value>): Record<string, Value>;
2
2
  export { prefixRouteMap };
@@ -0,0 +1,3 @@
1
+ import type { OpenApiRouteMap } from "@getstrata/core/openapi/registeredRoute";
2
+ declare function registerOpenApiRouteMap(routes: Record<string, unknown>, middleware: string[], metadata?: OpenApiRouteMap): Record<string, unknown>;
3
+ export { registerOpenApiRouteMap };
@@ -1,7 +1,12 @@
1
1
  // @bun
2
- // ../../src/bootstrap/buildModuleRoutes.ts
2
+ // ../../src/bootstrap/cachedJson.ts
3
3
  import { conditionalJsonResponse } from "@getstrata/core/http/conditionalResponse";
4
- import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
4
+ function createCachedJson(dependencies) {
5
+ return async (cacheKey, loader, tags = [], request) => {
6
+ const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
7
+ return conditionalJsonResponse(request, data);
8
+ };
9
+ }
5
10
 
6
11
  // ../../src/bootstrap/httpKernel.ts
7
12
  import {
@@ -362,6 +367,44 @@ function createHttpKernel(dependencies) {
362
367
  return new HttpKernel(dependencies);
363
368
  }
364
369
 
370
+ // ../../src/bootstrap/moduleRouteMiddleware.ts
371
+ import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
372
+ function wrapCallback(path, callback, middleware) {
373
+ return applyMiddlewareToRoutes({ [path]: callback }, middleware)[path];
374
+ }
375
+ function applyModuleRouteMiddleware(routes, middleware) {
376
+ const wrapped = {};
377
+ for (const [path, value] of Object.entries(routes)) {
378
+ if (value instanceof Response) {
379
+ const methods = {};
380
+ for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
381
+ methods[method] = wrapCallback(path, () => value.clone(), middleware);
382
+ }
383
+ wrapped[path] = methods;
384
+ continue;
385
+ }
386
+ if (typeof value === "function") {
387
+ wrapped[path] = wrapCallback(path, value, middleware);
388
+ continue;
389
+ }
390
+ if (value && typeof value === "object" && !Array.isArray(value)) {
391
+ const entries = Object.entries(value);
392
+ if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
393
+ wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
394
+ const callback = handler instanceof Response ? () => handler.clone() : handler;
395
+ return [
396
+ method,
397
+ typeof callback === "function" ? wrapCallback(path, callback, middleware) : callback
398
+ ];
399
+ }));
400
+ continue;
401
+ }
402
+ }
403
+ wrapped[path] = value;
404
+ }
405
+ return wrapped;
406
+ }
407
+
365
408
  // ../../src/bootstrap/discoverModules.ts
366
409
  import { readdir as readdir2 } from "fs/promises";
367
410
  import { join as join2, resolve } from "path";
@@ -574,7 +617,7 @@ function readSharedRouteRegistry() {
574
617
  }
575
618
  var routeRegistry = readSharedRouteRegistry();
576
619
 
577
- // ../../src/bootstrap/buildModuleRoutes.ts
620
+ // ../../src/bootstrap/routeMetadata.ts
578
621
  function registerOpenApiRoute(method, path, middleware, openApi) {
579
622
  routeRegistry.register({ method, path, middleware, ...openApi ? { openApi } : {} });
580
623
  }
@@ -594,11 +637,18 @@ function registerOpenApiRouteMap(routes, middleware, metadata = {}) {
594
637
  }
595
638
  return registered;
596
639
  }
597
- function createCachedJson(dependencies) {
598
- return async (cacheKey, loader, tags = [], request) => {
599
- const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
600
- return conditionalJsonResponse(request, data);
601
- };
640
+
641
+ // ../../src/bootstrap/buildModuleRoutes.ts
642
+ function prefixApiModuleRoutes(prefix, routes, metadata) {
643
+ const entries = Object.fromEntries(Object.entries(routes).map(([path, handler]) => [path, { handler, openApi: metadata[path] }]));
644
+ const prefixedRoutes = {};
645
+ const prefixedMetadata = {};
646
+ for (const [path, entry] of Object.entries(prefixRouteMap(prefix, entries))) {
647
+ prefixedRoutes[path] = entry.handler;
648
+ if (entry.openApi)
649
+ prefixedMetadata[path] = entry.openApi;
650
+ }
651
+ return { routes: prefixedRoutes, metadata: prefixedMetadata };
602
652
  }
603
653
  function buildModuleRoutes(dependencies, options = {}) {
604
654
  const { apiPrefix = "", modules = discoverModules(), clearRegistry = true } = options;
@@ -630,8 +680,8 @@ function buildModuleRoutes(dependencies, options = {}) {
630
680
  }
631
681
  Object.assign(moduleRoutes, routes);
632
682
  }
633
- const prefixedModuleRoutes = prefixRouteMap(apiPrefix, moduleRoutes);
634
- return applyMiddlewareToRoutes(registerOpenApiRouteMap(prefixedModuleRoutes, ["global", "api"], prefixRouteMap(apiPrefix, metadata)), middleware);
683
+ const prefixed = prefixApiModuleRoutes(apiPrefix, moduleRoutes, metadata);
684
+ return applyModuleRouteMiddleware(registerOpenApiRouteMap(prefixed.routes, ["global", "api"], prefixed.metadata), middleware);
635
685
  }
636
686
  export {
637
687
  buildModuleRoutes,
@@ -1,10 +1,12 @@
1
1
  // @bun
2
- // ../../src/bootstrap/buildWebModuleRoutes.ts
3
- import { applyMiddlewareToRoutes as applyMiddlewareToRoutes2 } from "@getstrata/core/http/middleware";
4
-
5
- // ../../src/bootstrap/buildModuleRoutes.ts
2
+ // ../../src/bootstrap/cachedJson.ts
6
3
  import { conditionalJsonResponse } from "@getstrata/core/http/conditionalResponse";
7
- import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
4
+ function createCachedJson(dependencies) {
5
+ return async (cacheKey, loader, tags = [], request) => {
6
+ const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
7
+ return conditionalJsonResponse(request, data);
8
+ };
9
+ }
8
10
 
9
11
  // ../../src/bootstrap/httpKernel.ts
10
12
  import {
@@ -365,6 +367,44 @@ function createHttpKernel(dependencies) {
365
367
  return new HttpKernel(dependencies);
366
368
  }
367
369
 
370
+ // ../../src/bootstrap/moduleRouteMiddleware.ts
371
+ import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
372
+ function wrapCallback(path, callback, middleware) {
373
+ return applyMiddlewareToRoutes({ [path]: callback }, middleware)[path];
374
+ }
375
+ function applyModuleRouteMiddleware(routes, middleware) {
376
+ const wrapped = {};
377
+ for (const [path, value] of Object.entries(routes)) {
378
+ if (value instanceof Response) {
379
+ const methods = {};
380
+ for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
381
+ methods[method] = wrapCallback(path, () => value.clone(), middleware);
382
+ }
383
+ wrapped[path] = methods;
384
+ continue;
385
+ }
386
+ if (typeof value === "function") {
387
+ wrapped[path] = wrapCallback(path, value, middleware);
388
+ continue;
389
+ }
390
+ if (value && typeof value === "object" && !Array.isArray(value)) {
391
+ const entries = Object.entries(value);
392
+ if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
393
+ wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
394
+ const callback = handler instanceof Response ? () => handler.clone() : handler;
395
+ return [
396
+ method,
397
+ typeof callback === "function" ? wrapCallback(path, callback, middleware) : callback
398
+ ];
399
+ }));
400
+ continue;
401
+ }
402
+ }
403
+ wrapped[path] = value;
404
+ }
405
+ return wrapped;
406
+ }
407
+
368
408
  // ../../src/bootstrap/discoverModules.ts
369
409
  import { readdir as readdir2 } from "fs/promises";
370
410
  import { join as join2, resolve } from "path";
@@ -541,17 +581,6 @@ function resetDiscoverModulesForTests() {
541
581
  current.modulesReady = undefined;
542
582
  current.source = undefined;
543
583
  }
544
- // ../../src/bootstrap/prefixRouteMap.ts
545
- function prefixRouteMap(prefix, routes) {
546
- const normalizedPrefix = prefix.replace(/\/$/, "");
547
- const prefixed = {};
548
- for (const [path, handler] of Object.entries(routes)) {
549
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
550
- prefixed[`${normalizedPrefix}${normalizedPath}`] = handler;
551
- }
552
- return prefixed;
553
- }
554
-
555
584
  // ../../src/bootstrap/routeRegistry.ts
556
585
  class RouteRegistry {
557
586
  routes = [];
@@ -577,7 +606,7 @@ function readSharedRouteRegistry() {
577
606
  }
578
607
  var routeRegistry = readSharedRouteRegistry();
579
608
 
580
- // ../../src/bootstrap/buildModuleRoutes.ts
609
+ // ../../src/bootstrap/routeMetadata.ts
581
610
  function registerOpenApiRoute(method, path, middleware, openApi) {
582
611
  routeRegistry.register({ method, path, middleware, ...openApi ? { openApi } : {} });
583
612
  }
@@ -597,45 +626,6 @@ function registerOpenApiRouteMap(routes, middleware, metadata = {}) {
597
626
  }
598
627
  return registered;
599
628
  }
600
- function createCachedJson(dependencies) {
601
- return async (cacheKey, loader, tags = [], request) => {
602
- const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
603
- return conditionalJsonResponse(request, data);
604
- };
605
- }
606
- function buildModuleRoutes(dependencies, options = {}) {
607
- const { apiPrefix = "", modules = discoverModules(), clearRegistry = true } = options;
608
- if (clearRegistry) {
609
- routeRegistry.clear();
610
- }
611
- const kernel = createHttpKernel(dependencies);
612
- const middleware = [...kernel.globalMiddleware(), ...kernel.group("api")];
613
- const cachedJson = createCachedJson(dependencies);
614
- const moduleRoutes = {};
615
- const metadata = {};
616
- for (const module of modules) {
617
- if (!module.routes) {
618
- continue;
619
- }
620
- const routes = module.routes({ dependencies, cachedJson, kernel });
621
- for (const [path, operations] of Object.entries(module.openApi ?? {})) {
622
- if (!(path in routes))
623
- throw new Error(`OpenAPI metadata has no API route: ${module.name} ${path}`);
624
- const methods = typeof routes[path] === "function" ? ["GET"] : Object.keys(routes[path]).map((method) => method.toUpperCase());
625
- for (const method of Object.keys(operations))
626
- if (!methods.includes(method))
627
- throw new Error(`OpenAPI metadata has no API handler: ${method} ${path}`);
628
- }
629
- for (const path of Object.keys(routes)) {
630
- delete metadata[path];
631
- if (module.openApi?.[path])
632
- metadata[path] = module.openApi[path];
633
- }
634
- Object.assign(moduleRoutes, routes);
635
- }
636
- const prefixedModuleRoutes = prefixRouteMap(apiPrefix, moduleRoutes);
637
- return applyMiddlewareToRoutes(registerOpenApiRouteMap(prefixedModuleRoutes, ["global", "api"], prefixRouteMap(apiPrefix, metadata)), middleware);
638
- }
639
629
 
640
630
  // ../../src/bootstrap/buildWebModuleRoutes.ts
641
631
  function buildWebModuleRoutes(dependencies, options = {}) {
@@ -644,6 +634,7 @@ function buildWebModuleRoutes(dependencies, options = {}) {
644
634
  routeRegistry.clear();
645
635
  }
646
636
  const kernel = createHttpKernel(dependencies);
637
+ const cachedJson = createCachedJson(dependencies);
647
638
  const middleware = kernel.globalMiddleware("web");
648
639
  const moduleRoutes = { ...seedRoutes };
649
640
  for (const module of modules) {
@@ -652,11 +643,11 @@ function buildWebModuleRoutes(dependencies, options = {}) {
652
643
  }
653
644
  Object.assign(moduleRoutes, module.webRoutes({
654
645
  dependencies,
655
- cachedJson: async () => new Response(""),
646
+ cachedJson,
656
647
  kernel
657
648
  }));
658
649
  }
659
- return applyMiddlewareToRoutes2(registerOpenApiRouteMap(moduleRoutes, ["global", "web"]), middleware);
650
+ return applyModuleRouteMiddleware(registerOpenApiRouteMap(moduleRoutes, ["global", "web"]), middleware);
660
651
  }
661
652
  export {
662
653
  buildWebModuleRoutes
@@ -924,8 +924,10 @@ async function createAppContext(providers) {
924
924
  const dependencies = { container };
925
925
  const cleanups = [];
926
926
  const drains = [];
927
+ let drainStarted = false;
927
928
  let draining;
928
929
  const drain = () => {
930
+ drainStarted = true;
929
931
  draining ??= (async () => {
930
932
  const errors = [];
931
933
  for (const handler of drains.splice(0).reverse()) {
@@ -941,8 +943,10 @@ async function createAppContext(providers) {
941
943
  return draining;
942
944
  };
943
945
  const flushes = [];
946
+ let flushStarted = false;
944
947
  let flushing;
945
948
  const flush = () => {
949
+ flushStarted = true;
946
950
  flushing ??= (async () => {
947
951
  const errors = [];
948
952
  for (const handler of flushes.splice(0).reverse()) {
@@ -996,7 +1000,7 @@ async function createAppContext(providers) {
996
1000
  throw new Error("Cannot register cleanup on a disposed application context.");
997
1001
  if (phase !== "drain" && phase !== "flush" && phase !== "close")
998
1002
  throw new TypeError("Unknown provider cleanup phase.");
999
- if (phase === "drain" && draining || phase === "flush" && flushing)
1003
+ if (phase === "drain" && drainStarted || phase === "flush" && flushStarted)
1000
1004
  throw new Error("Cannot register cleanup after its phase started.");
1001
1005
  (phase === "drain" ? drains : phase === "flush" ? flushes : cleanups).push(handler);
1002
1006
  }
@@ -5,12 +5,14 @@ import { isViewsEnabled as isViewsEnabled2 } from "@getstrata/core/runtime/front
5
5
  import { assertUrlPathUnderRoot } from "@getstrata/core/security/safePath";
6
6
  import { notFoundHtmlResponse } from "@getstrata/core/view";
7
7
 
8
- // ../../src/bootstrap/buildWebModuleRoutes.ts
9
- import { applyMiddlewareToRoutes as applyMiddlewareToRoutes2 } from "@getstrata/core/http/middleware";
10
-
11
- // ../../src/bootstrap/buildModuleRoutes.ts
8
+ // ../../src/bootstrap/cachedJson.ts
12
9
  import { conditionalJsonResponse } from "@getstrata/core/http/conditionalResponse";
13
- import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
10
+ function createCachedJson(dependencies) {
11
+ return async (cacheKey, loader, tags = [], request) => {
12
+ const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
13
+ return conditionalJsonResponse(request, data);
14
+ };
15
+ }
14
16
 
15
17
  // ../../src/bootstrap/httpKernel.ts
16
18
  import {
@@ -371,6 +373,44 @@ function createHttpKernel(dependencies) {
371
373
  return new HttpKernel(dependencies);
372
374
  }
373
375
 
376
+ // ../../src/bootstrap/moduleRouteMiddleware.ts
377
+ import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
378
+ function wrapCallback(path, callback, middleware) {
379
+ return applyMiddlewareToRoutes({ [path]: callback }, middleware)[path];
380
+ }
381
+ function applyModuleRouteMiddleware(routes, middleware) {
382
+ const wrapped = {};
383
+ for (const [path, value] of Object.entries(routes)) {
384
+ if (value instanceof Response) {
385
+ const methods = {};
386
+ for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
387
+ methods[method] = wrapCallback(path, () => value.clone(), middleware);
388
+ }
389
+ wrapped[path] = methods;
390
+ continue;
391
+ }
392
+ if (typeof value === "function") {
393
+ wrapped[path] = wrapCallback(path, value, middleware);
394
+ continue;
395
+ }
396
+ if (value && typeof value === "object" && !Array.isArray(value)) {
397
+ const entries = Object.entries(value);
398
+ if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
399
+ wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
400
+ const callback = handler instanceof Response ? () => handler.clone() : handler;
401
+ return [
402
+ method,
403
+ typeof callback === "function" ? wrapCallback(path, callback, middleware) : callback
404
+ ];
405
+ }));
406
+ continue;
407
+ }
408
+ }
409
+ wrapped[path] = value;
410
+ }
411
+ return wrapped;
412
+ }
413
+
374
414
  // ../../src/bootstrap/discoverModules.ts
375
415
  import { readdir as readdir2 } from "fs/promises";
376
416
  import { join as join2, resolve } from "path";
@@ -547,17 +587,6 @@ function resetDiscoverModulesForTests() {
547
587
  current.modulesReady = undefined;
548
588
  current.source = undefined;
549
589
  }
550
- // ../../src/bootstrap/prefixRouteMap.ts
551
- function prefixRouteMap(prefix, routes) {
552
- const normalizedPrefix = prefix.replace(/\/$/, "");
553
- const prefixed = {};
554
- for (const [path, handler] of Object.entries(routes)) {
555
- const normalizedPath = path.startsWith("/") ? path : `/${path}`;
556
- prefixed[`${normalizedPrefix}${normalizedPath}`] = handler;
557
- }
558
- return prefixed;
559
- }
560
-
561
590
  // ../../src/bootstrap/routeRegistry.ts
562
591
  class RouteRegistry {
563
592
  routes = [];
@@ -583,7 +612,7 @@ function readSharedRouteRegistry() {
583
612
  }
584
613
  var routeRegistry = readSharedRouteRegistry();
585
614
 
586
- // ../../src/bootstrap/buildModuleRoutes.ts
615
+ // ../../src/bootstrap/routeMetadata.ts
587
616
  function registerOpenApiRoute(method, path, middleware, openApi) {
588
617
  routeRegistry.register({ method, path, middleware, ...openApi ? { openApi } : {} });
589
618
  }
@@ -603,45 +632,6 @@ function registerOpenApiRouteMap(routes, middleware, metadata = {}) {
603
632
  }
604
633
  return registered;
605
634
  }
606
- function createCachedJson(dependencies) {
607
- return async (cacheKey, loader, tags = [], request) => {
608
- const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
609
- return conditionalJsonResponse(request, data);
610
- };
611
- }
612
- function buildModuleRoutes(dependencies, options = {}) {
613
- const { apiPrefix = "", modules = discoverModules(), clearRegistry = true } = options;
614
- if (clearRegistry) {
615
- routeRegistry.clear();
616
- }
617
- const kernel = createHttpKernel(dependencies);
618
- const middleware = [...kernel.globalMiddleware(), ...kernel.group("api")];
619
- const cachedJson = createCachedJson(dependencies);
620
- const moduleRoutes = {};
621
- const metadata = {};
622
- for (const module of modules) {
623
- if (!module.routes) {
624
- continue;
625
- }
626
- const routes = module.routes({ dependencies, cachedJson, kernel });
627
- for (const [path, operations] of Object.entries(module.openApi ?? {})) {
628
- if (!(path in routes))
629
- throw new Error(`OpenAPI metadata has no API route: ${module.name} ${path}`);
630
- const methods = typeof routes[path] === "function" ? ["GET"] : Object.keys(routes[path]).map((method) => method.toUpperCase());
631
- for (const method of Object.keys(operations))
632
- if (!methods.includes(method))
633
- throw new Error(`OpenAPI metadata has no API handler: ${method} ${path}`);
634
- }
635
- for (const path of Object.keys(routes)) {
636
- delete metadata[path];
637
- if (module.openApi?.[path])
638
- metadata[path] = module.openApi[path];
639
- }
640
- Object.assign(moduleRoutes, routes);
641
- }
642
- const prefixedModuleRoutes = prefixRouteMap(apiPrefix, moduleRoutes);
643
- return applyMiddlewareToRoutes(registerOpenApiRouteMap(prefixedModuleRoutes, ["global", "api"], prefixRouteMap(apiPrefix, metadata)), middleware);
644
- }
645
635
 
646
636
  // ../../src/bootstrap/buildWebModuleRoutes.ts
647
637
  function buildWebModuleRoutes(dependencies, options = {}) {
@@ -650,6 +640,7 @@ function buildWebModuleRoutes(dependencies, options = {}) {
650
640
  routeRegistry.clear();
651
641
  }
652
642
  const kernel = createHttpKernel(dependencies);
643
+ const cachedJson = createCachedJson(dependencies);
653
644
  const middleware = kernel.globalMiddleware("web");
654
645
  const moduleRoutes = { ...seedRoutes };
655
646
  for (const module of modules) {
@@ -658,11 +649,11 @@ function buildWebModuleRoutes(dependencies, options = {}) {
658
649
  }
659
650
  Object.assign(moduleRoutes, module.webRoutes({
660
651
  dependencies,
661
- cachedJson: async () => new Response(""),
652
+ cachedJson,
662
653
  kernel
663
654
  }));
664
655
  }
665
- return applyMiddlewareToRoutes2(registerOpenApiRouteMap(moduleRoutes, ["global", "web"]), middleware);
656
+ return applyModuleRouteMiddleware(registerOpenApiRouteMap(moduleRoutes, ["global", "web"]), middleware);
666
657
  }
667
658
 
668
659
  // ../../src/bootstrap/createWebRoutes.ts
@@ -924,8 +924,10 @@ async function createAppContext(providers) {
924
924
  const dependencies = { container };
925
925
  const cleanups = [];
926
926
  const drains = [];
927
+ let drainStarted = false;
927
928
  let draining;
928
929
  const drain = () => {
930
+ drainStarted = true;
929
931
  draining ??= (async () => {
930
932
  const errors = [];
931
933
  for (const handler of drains.splice(0).reverse()) {
@@ -941,8 +943,10 @@ async function createAppContext(providers) {
941
943
  return draining;
942
944
  };
943
945
  const flushes = [];
946
+ let flushStarted = false;
944
947
  let flushing;
945
948
  const flush = () => {
949
+ flushStarted = true;
946
950
  flushing ??= (async () => {
947
951
  const errors = [];
948
952
  for (const handler of flushes.splice(0).reverse()) {
@@ -996,7 +1000,7 @@ async function createAppContext(providers) {
996
1000
  throw new Error("Cannot register cleanup on a disposed application context.");
997
1001
  if (phase !== "drain" && phase !== "flush" && phase !== "close")
998
1002
  throw new TypeError("Unknown provider cleanup phase.");
999
- if (phase === "drain" && draining || phase === "flush" && flushing)
1003
+ if (phase === "drain" && drainStarted || phase === "flush" && flushStarted)
1000
1004
  throw new Error("Cannot register cleanup after its phase started.");
1001
1005
  (phase === "drain" ? drains : phase === "flush" ? flushes : cleanups).push(handler);
1002
1006
  }
@@ -25,7 +25,7 @@ function wrapRouteHandler(handler, path, onRequest, track = (work) => work()) {
25
25
  const intercepted = await onRequest?.(request, server);
26
26
  if (intercepted instanceof Response)
27
27
  return intercepted;
28
- const response = await handler(request);
28
+ const response = await handler(request, server);
29
29
  return response ?? await missingHtmlResponse();
30
30
  }));
31
31
  };
@@ -35,6 +35,15 @@ function convertAppRoutesToBunRoutes(routes, cors = {}, onRequest, track) {
35
35
  const middleware = createCorsMiddleware(cors);
36
36
  const preflight = (request) => middleware(request, async () => new Response(null, { status: 405 }));
37
37
  for (const [path, handler] of Object.entries(routes)) {
38
+ if (handler instanceof Response) {
39
+ const methods = {};
40
+ for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]) {
41
+ methods[method] = wrapRouteHandler(() => handler.clone(), path, onRequest, track);
42
+ }
43
+ methods.OPTIONS = wrapRouteHandler(preflight, path, onRequest, track);
44
+ bunRoutes[path] = methods;
45
+ continue;
46
+ }
38
47
  if (typeof handler === "function") {
39
48
  bunRoutes[path] = {
40
49
  GET: wrapRouteHandler(handler, path, onRequest, track),
@@ -45,6 +54,10 @@ function convertAppRoutesToBunRoutes(routes, cors = {}, onRequest, track) {
45
54
  if (handler && typeof handler === "object" && !Array.isArray(handler)) {
46
55
  const methods = {};
47
56
  for (const [method, methodHandler] of Object.entries(handler)) {
57
+ if (methodHandler instanceof Response) {
58
+ methods[method.toUpperCase()] = wrapRouteHandler(() => methodHandler.clone(), path, onRequest, track);
59
+ continue;
60
+ }
48
61
  if (typeof methodHandler !== "function") {
49
62
  continue;
50
63
  }
package/dist/index.js CHANGED
@@ -20,9 +20,14 @@ import {
20
20
  resolveApplicationQueue,
21
21
  setActiveApplicationContext
22
22
  } from "@getstrata/core/runtime/applicationRegistry";
23
- // ../../src/bootstrap/buildModuleRoutes.ts
23
+ // ../../src/bootstrap/cachedJson.ts
24
24
  import { conditionalJsonResponse } from "@getstrata/core/http/conditionalResponse";
25
- import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
25
+ function createCachedJson(dependencies) {
26
+ return async (cacheKey, loader, tags = [], request) => {
27
+ const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
28
+ return conditionalJsonResponse(request, data);
29
+ };
30
+ }
26
31
 
27
32
  // ../../src/bootstrap/httpKernel.ts
28
33
  import {
@@ -382,6 +387,44 @@ function createHttpKernel(dependencies) {
382
387
  return new HttpKernel(dependencies);
383
388
  }
384
389
 
390
+ // ../../src/bootstrap/moduleRouteMiddleware.ts
391
+ import { applyMiddlewareToRoutes } from "@getstrata/core/http/middleware";
392
+ function wrapCallback(path, callback, middleware) {
393
+ return applyMiddlewareToRoutes({ [path]: callback }, middleware)[path];
394
+ }
395
+ function applyModuleRouteMiddleware(routes, middleware) {
396
+ const wrapped = {};
397
+ for (const [path, value] of Object.entries(routes)) {
398
+ if (value instanceof Response) {
399
+ const methods = {};
400
+ for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
401
+ methods[method] = wrapCallback(path, () => value.clone(), middleware);
402
+ }
403
+ wrapped[path] = methods;
404
+ continue;
405
+ }
406
+ if (typeof value === "function") {
407
+ wrapped[path] = wrapCallback(path, value, middleware);
408
+ continue;
409
+ }
410
+ if (value && typeof value === "object" && !Array.isArray(value)) {
411
+ const entries = Object.entries(value);
412
+ if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
413
+ wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
414
+ const callback = handler instanceof Response ? () => handler.clone() : handler;
415
+ return [
416
+ method,
417
+ typeof callback === "function" ? wrapCallback(path, callback, middleware) : callback
418
+ ];
419
+ }));
420
+ continue;
421
+ }
422
+ }
423
+ wrapped[path] = value;
424
+ }
425
+ return wrapped;
426
+ }
427
+
385
428
  // ../../src/bootstrap/discoverModules.ts
386
429
  import { readdir as readdir2 } from "fs/promises";
387
430
  import { join as join2, resolve } from "path";
@@ -586,7 +629,7 @@ function readSharedRouteRegistry() {
586
629
  }
587
630
  var routeRegistry = readSharedRouteRegistry();
588
631
 
589
- // ../../src/bootstrap/buildModuleRoutes.ts
632
+ // ../../src/bootstrap/routeMetadata.ts
590
633
  function registerOpenApiRoute(method, path, middleware, openApi) {
591
634
  routeRegistry.register({ method, path, middleware, ...openApi ? { openApi } : {} });
592
635
  }
@@ -606,11 +649,18 @@ function registerOpenApiRouteMap(routes, middleware, metadata = {}) {
606
649
  }
607
650
  return registered;
608
651
  }
609
- function createCachedJson(dependencies) {
610
- return async (cacheKey, loader, tags = [], request) => {
611
- const data = tags.length > 0 ? await dependencies.cache.tags(...tags).remember(cacheKey, loader) : await dependencies.cache.remember(cacheKey, loader);
612
- return conditionalJsonResponse(request, data);
613
- };
652
+
653
+ // ../../src/bootstrap/buildModuleRoutes.ts
654
+ function prefixApiModuleRoutes(prefix, routes, metadata) {
655
+ const entries = Object.fromEntries(Object.entries(routes).map(([path, handler]) => [path, { handler, openApi: metadata[path] }]));
656
+ const prefixedRoutes = {};
657
+ const prefixedMetadata = {};
658
+ for (const [path, entry] of Object.entries(prefixRouteMap(prefix, entries))) {
659
+ prefixedRoutes[path] = entry.handler;
660
+ if (entry.openApi)
661
+ prefixedMetadata[path] = entry.openApi;
662
+ }
663
+ return { routes: prefixedRoutes, metadata: prefixedMetadata };
614
664
  }
615
665
  function buildModuleRoutes(dependencies, options = {}) {
616
666
  const { apiPrefix = "", modules = discoverModules(), clearRegistry = true } = options;
@@ -642,17 +692,17 @@ function buildModuleRoutes(dependencies, options = {}) {
642
692
  }
643
693
  Object.assign(moduleRoutes, routes);
644
694
  }
645
- const prefixedModuleRoutes = prefixRouteMap(apiPrefix, moduleRoutes);
646
- return applyMiddlewareToRoutes(registerOpenApiRouteMap(prefixedModuleRoutes, ["global", "api"], prefixRouteMap(apiPrefix, metadata)), middleware);
695
+ const prefixed = prefixApiModuleRoutes(apiPrefix, moduleRoutes, metadata);
696
+ return applyModuleRouteMiddleware(registerOpenApiRouteMap(prefixed.routes, ["global", "api"], prefixed.metadata), middleware);
647
697
  }
648
698
  // ../../src/bootstrap/buildWebModuleRoutes.ts
649
- import { applyMiddlewareToRoutes as applyMiddlewareToRoutes2 } from "@getstrata/core/http/middleware";
650
699
  function buildWebModuleRoutes(dependencies, options = {}) {
651
700
  const { modules = discoverModules(), clearRegistry = true, seedRoutes = {} } = options;
652
701
  if (clearRegistry) {
653
702
  routeRegistry.clear();
654
703
  }
655
704
  const kernel = createHttpKernel(dependencies);
705
+ const cachedJson = createCachedJson(dependencies);
656
706
  const middleware = kernel.globalMiddleware("web");
657
707
  const moduleRoutes = { ...seedRoutes };
658
708
  for (const module of modules) {
@@ -661,11 +711,11 @@ function buildWebModuleRoutes(dependencies, options = {}) {
661
711
  }
662
712
  Object.assign(moduleRoutes, module.webRoutes({
663
713
  dependencies,
664
- cachedJson: async () => new Response(""),
714
+ cachedJson,
665
715
  kernel
666
716
  }));
667
717
  }
668
- return applyMiddlewareToRoutes2(registerOpenApiRouteMap(moduleRoutes, ["global", "web"]), middleware);
718
+ return applyModuleRouteMiddleware(registerOpenApiRouteMap(moduleRoutes, ["global", "web"]), middleware);
669
719
  }
670
720
  // ../../src/bootstrap/cache/modelCacheTags.ts
671
721
  function cacheTagsForModelWrite(tableName, action) {
@@ -1335,8 +1385,10 @@ async function createAppContext(providers) {
1335
1385
  const dependencies = { container };
1336
1386
  const cleanups = [];
1337
1387
  const drains = [];
1388
+ let drainStarted = false;
1338
1389
  let draining;
1339
1390
  const drain = () => {
1391
+ drainStarted = true;
1340
1392
  draining ??= (async () => {
1341
1393
  const errors = [];
1342
1394
  for (const handler of drains.splice(0).reverse()) {
@@ -1352,8 +1404,10 @@ async function createAppContext(providers) {
1352
1404
  return draining;
1353
1405
  };
1354
1406
  const flushes = [];
1407
+ let flushStarted = false;
1355
1408
  let flushing;
1356
1409
  const flush = () => {
1410
+ flushStarted = true;
1357
1411
  flushing ??= (async () => {
1358
1412
  const errors = [];
1359
1413
  for (const handler of flushes.splice(0).reverse()) {
@@ -1407,7 +1461,7 @@ async function createAppContext(providers) {
1407
1461
  throw new Error("Cannot register cleanup on a disposed application context.");
1408
1462
  if (phase !== "drain" && phase !== "flush" && phase !== "close")
1409
1463
  throw new TypeError("Unknown provider cleanup phase.");
1410
- if (phase === "drain" && draining || phase === "flush" && flushing)
1464
+ if (phase === "drain" && drainStarted || phase === "flush" && flushStarted)
1411
1465
  throw new Error("Cannot register cleanup after its phase started.");
1412
1466
  (phase === "drain" ? drains : phase === "flush" ? flushes : cleanups).push(handler);
1413
1467
  }
@@ -2058,7 +2112,7 @@ function wrapRouteHandler(handler, path, onRequest, track = (work) => work()) {
2058
2112
  const intercepted = await onRequest?.(request, server);
2059
2113
  if (intercepted instanceof Response)
2060
2114
  return intercepted;
2061
- const response = await handler(request);
2115
+ const response = await handler(request, server);
2062
2116
  return response ?? await missingHtmlResponse();
2063
2117
  }));
2064
2118
  };
@@ -2068,6 +2122,15 @@ function convertAppRoutesToBunRoutes(routes, cors = {}, onRequest, track) {
2068
2122
  const middleware = createCorsMiddleware2(cors);
2069
2123
  const preflight = (request) => middleware(request, async () => new Response(null, { status: 405 }));
2070
2124
  for (const [path, handler] of Object.entries(routes)) {
2125
+ if (handler instanceof Response) {
2126
+ const methods = {};
2127
+ for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]) {
2128
+ methods[method] = wrapRouteHandler(() => handler.clone(), path, onRequest, track);
2129
+ }
2130
+ methods.OPTIONS = wrapRouteHandler(preflight, path, onRequest, track);
2131
+ bunRoutes[path] = methods;
2132
+ continue;
2133
+ }
2071
2134
  if (typeof handler === "function") {
2072
2135
  bunRoutes[path] = {
2073
2136
  GET: wrapRouteHandler(handler, path, onRequest, track),
@@ -2078,6 +2141,10 @@ function convertAppRoutesToBunRoutes(routes, cors = {}, onRequest, track) {
2078
2141
  if (handler && typeof handler === "object" && !Array.isArray(handler)) {
2079
2142
  const methods = {};
2080
2143
  for (const [method, methodHandler] of Object.entries(handler)) {
2144
+ if (methodHandler instanceof Response) {
2145
+ methods[method.toUpperCase()] = wrapRouteHandler(() => methodHandler.clone(), path, onRequest, track);
2146
+ continue;
2147
+ }
2081
2148
  if (typeof methodHandler !== "function") {
2082
2149
  continue;
2083
2150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getstrata/bootstrap",
3
- "version": "2.2.47",
3
+ "version": "2.2.49",
4
4
  "description": "Strata application bootstrap: HttpKernel, DI, web session helpers",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -190,7 +190,7 @@
190
190
  "access": "public"
191
191
  },
192
192
  "peerDependencies": {
193
- "@getstrata/core": "^2.2.47",
193
+ "@getstrata/core": "^2.2.49",
194
194
  "typescript": "^5.9.0 || ^6.0.0 || ^7.0.0"
195
195
  },
196
196
  "engines": {