@getstrata/bootstrap 2.0.4 → 2.0.6

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,13 @@
1
1
  # @getstrata/bootstrap changelog
2
2
 
3
+ ## 2.0.6
4
+
5
+ - Dispatch registered Bun route preflights through CORS
6
+
7
+ ## 2.0.5
8
+
9
+ - Allow explicitly approved application headers in CORS
10
+
3
11
  ## 2.0.4
4
12
 
5
13
  - Place cache rollout guidance beside outbox docs without overlapping API additions
@@ -1,3 +1,4 @@
1
+ import { type CorsOptions } from "@getstrata/core/http/corsMiddleware";
1
2
  import type { BunRequest, Server } from "bun";
2
3
  import type { AppRouteMap } from "../contracts.ts";
3
4
  export interface WebServerOptions {
@@ -5,9 +6,11 @@ export interface WebServerOptions {
5
6
  handle?: (request: Request) => Promise<Response | null> | Response | null;
6
7
  routes?: AppRouteMap;
7
8
  publicDir?: string;
9
+ /** Additional approved headers for synthesized route preflights; origins still use framework policy. */
10
+ cors?: CorsOptions;
8
11
  onRequest?: (request: Request) => Promise<void> | void;
9
12
  }
10
13
  type BunRouteHandler = (request: BunRequest, server: Server<unknown>) => Response | Promise<Response>;
11
- declare function convertAppRoutesToBunRoutes(routes: AppRouteMap): Record<string, Record<string, BunRouteHandler>>;
14
+ declare function convertAppRoutesToBunRoutes(routes: AppRouteMap, cors?: CorsOptions): Record<string, Record<string, BunRouteHandler>>;
12
15
  export declare function createWebServer(options: WebServerOptions): Server<undefined>;
13
16
  export { convertAppRoutesToBunRoutes };
@@ -285,6 +285,9 @@ var appEnvSchema = defineEnvSchema({
285
285
  API_PREFIX: {
286
286
  default: "/api/v1"
287
287
  },
288
+ CORS_ADDITIONAL_ALLOWED_HEADERS: {
289
+ default: ""
290
+ },
288
291
  CORS_ALLOWED_ORIGINS: {
289
292
  default: "*"
290
293
  },
@@ -285,6 +285,9 @@ var appEnvSchema = defineEnvSchema({
285
285
  API_PREFIX: {
286
286
  default: "/api/v1"
287
287
  },
288
+ CORS_ADDITIONAL_ALLOWED_HEADERS: {
289
+ default: ""
290
+ },
288
291
  CORS_ALLOWED_ORIGINS: {
289
292
  default: "*"
290
293
  },
@@ -191,6 +191,9 @@ var appEnvSchema = defineEnvSchema({
191
191
  API_PREFIX: {
192
192
  default: "/api/v1"
193
193
  },
194
+ CORS_ADDITIONAL_ALLOWED_HEADERS: {
195
+ default: ""
196
+ },
194
197
  CORS_ALLOWED_ORIGINS: {
195
198
  default: "*"
196
199
  },
@@ -1,5 +1,6 @@
1
1
  // @bun
2
2
  // ../../src/bootstrap/web/server.ts
3
+ import { createCorsMiddleware } from "@getstrata/core/http/corsMiddleware";
3
4
  import { currentRequestMeta, runWithRequestMeta } from "@getstrata/core/http/requestMetaContext";
4
5
  import { assertUrlPathUnderRoot } from "@getstrata/core/security/safePath";
5
6
  import { notFoundHtmlResponse } from "@getstrata/core/view";
@@ -24,11 +25,16 @@ function wrapRouteHandler(handler, path) {
24
25
  });
25
26
  };
26
27
  }
27
- function convertAppRoutesToBunRoutes(routes) {
28
+ function convertAppRoutesToBunRoutes(routes, cors = {}) {
28
29
  const bunRoutes = {};
30
+ const middleware = createCorsMiddleware(cors);
31
+ const preflight = (request) => middleware(request, async () => new Response(null, { status: 405 }));
29
32
  for (const [path, handler] of Object.entries(routes)) {
30
33
  if (typeof handler === "function") {
31
- bunRoutes[path] = { GET: wrapRouteHandler(handler, path) };
34
+ bunRoutes[path] = {
35
+ GET: wrapRouteHandler(handler, path),
36
+ OPTIONS: wrapRouteHandler(preflight, path)
37
+ };
32
38
  continue;
33
39
  }
34
40
  if (handler && typeof handler === "object" && !Array.isArray(handler)) {
@@ -40,6 +46,7 @@ function convertAppRoutesToBunRoutes(routes) {
40
46
  methods[method.toUpperCase()] = wrapRouteHandler(methodHandler, path);
41
47
  }
42
48
  if (Object.keys(methods).length > 0) {
49
+ methods.OPTIONS ??= wrapRouteHandler(preflight, path);
43
50
  bunRoutes[path] = methods;
44
51
  }
45
52
  }
@@ -48,7 +55,7 @@ function convertAppRoutesToBunRoutes(routes) {
48
55
  }
49
56
  function createWebServer(options) {
50
57
  const publicDir = options.publicDir ?? "./public";
51
- const bunRoutes = options.routes ? convertAppRoutesToBunRoutes(options.routes) : undefined;
58
+ const bunRoutes = options.routes ? convertAppRoutesToBunRoutes(options.routes, options.cors) : undefined;
52
59
  return Bun.serve({
53
60
  port: options.port,
54
61
  ...bunRoutes ? { routes: bunRoutes } : {},
package/dist/index.js CHANGED
@@ -704,6 +704,9 @@ var appEnvSchema = defineEnvSchema({
704
704
  API_PREFIX: {
705
705
  default: "/api/v1"
706
706
  },
707
+ CORS_ADDITIONAL_ALLOWED_HEADERS: {
708
+ default: ""
709
+ },
707
710
  CORS_ALLOWED_ORIGINS: {
708
711
  default: "*"
709
712
  },
@@ -1576,6 +1579,7 @@ function createRouteKernel(dependencies) {
1576
1579
  return createHttpKernel(dependencies);
1577
1580
  }
1578
1581
  // ../../src/bootstrap/web/server.ts
1582
+ import { createCorsMiddleware as createCorsMiddleware2 } from "@getstrata/core/http/corsMiddleware";
1579
1583
  import { currentRequestMeta as currentRequestMeta2, runWithRequestMeta } from "@getstrata/core/http/requestMetaContext";
1580
1584
  import { assertUrlPathUnderRoot as assertUrlPathUnderRoot2 } from "@getstrata/core/security/safePath";
1581
1585
  import { notFoundHtmlResponse as notFoundHtmlResponse2 } from "@getstrata/core/view";
@@ -1600,11 +1604,16 @@ function wrapRouteHandler(handler, path) {
1600
1604
  });
1601
1605
  };
1602
1606
  }
1603
- function convertAppRoutesToBunRoutes(routes) {
1607
+ function convertAppRoutesToBunRoutes(routes, cors = {}) {
1604
1608
  const bunRoutes = {};
1609
+ const middleware = createCorsMiddleware2(cors);
1610
+ const preflight = (request) => middleware(request, async () => new Response(null, { status: 405 }));
1605
1611
  for (const [path, handler] of Object.entries(routes)) {
1606
1612
  if (typeof handler === "function") {
1607
- bunRoutes[path] = { GET: wrapRouteHandler(handler, path) };
1613
+ bunRoutes[path] = {
1614
+ GET: wrapRouteHandler(handler, path),
1615
+ OPTIONS: wrapRouteHandler(preflight, path)
1616
+ };
1608
1617
  continue;
1609
1618
  }
1610
1619
  if (handler && typeof handler === "object" && !Array.isArray(handler)) {
@@ -1616,6 +1625,7 @@ function convertAppRoutesToBunRoutes(routes) {
1616
1625
  methods[method.toUpperCase()] = wrapRouteHandler(methodHandler, path);
1617
1626
  }
1618
1627
  if (Object.keys(methods).length > 0) {
1628
+ methods.OPTIONS ??= wrapRouteHandler(preflight, path);
1619
1629
  bunRoutes[path] = methods;
1620
1630
  }
1621
1631
  }
@@ -1624,7 +1634,7 @@ function convertAppRoutesToBunRoutes(routes) {
1624
1634
  }
1625
1635
  function createWebServer(options) {
1626
1636
  const publicDir = options.publicDir ?? "./public";
1627
- const bunRoutes = options.routes ? convertAppRoutesToBunRoutes(options.routes) : undefined;
1637
+ const bunRoutes = options.routes ? convertAppRoutesToBunRoutes(options.routes, options.cors) : undefined;
1628
1638
  return Bun.serve({
1629
1639
  port: options.port,
1630
1640
  ...bunRoutes ? { routes: bunRoutes } : {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getstrata/bootstrap",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "Strata application bootstrap: HttpKernel, DI, web session helpers",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -185,7 +185,7 @@
185
185
  "access": "public"
186
186
  },
187
187
  "peerDependencies": {
188
- "@getstrata/core": "^2.0.4",
188
+ "@getstrata/core": "^2.0.6",
189
189
  "typescript": "^5.9.0 || ^6.0.0 || ^7.0.0"
190
190
  },
191
191
  "engines": {