@getstrata/bootstrap 2.0.5 → 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,9 @@
1
1
  # @getstrata/bootstrap changelog
2
2
 
3
+ ## 2.0.6
4
+
5
+ - Dispatch registered Bun route preflights through CORS
6
+
3
7
  ## 2.0.5
4
8
 
5
9
  - Allow explicitly approved application headers in CORS
@@ -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 };
@@ -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
@@ -1579,6 +1579,7 @@ function createRouteKernel(dependencies) {
1579
1579
  return createHttpKernel(dependencies);
1580
1580
  }
1581
1581
  // ../../src/bootstrap/web/server.ts
1582
+ import { createCorsMiddleware as createCorsMiddleware2 } from "@getstrata/core/http/corsMiddleware";
1582
1583
  import { currentRequestMeta as currentRequestMeta2, runWithRequestMeta } from "@getstrata/core/http/requestMetaContext";
1583
1584
  import { assertUrlPathUnderRoot as assertUrlPathUnderRoot2 } from "@getstrata/core/security/safePath";
1584
1585
  import { notFoundHtmlResponse as notFoundHtmlResponse2 } from "@getstrata/core/view";
@@ -1603,11 +1604,16 @@ function wrapRouteHandler(handler, path) {
1603
1604
  });
1604
1605
  };
1605
1606
  }
1606
- function convertAppRoutesToBunRoutes(routes) {
1607
+ function convertAppRoutesToBunRoutes(routes, cors = {}) {
1607
1608
  const bunRoutes = {};
1609
+ const middleware = createCorsMiddleware2(cors);
1610
+ const preflight = (request) => middleware(request, async () => new Response(null, { status: 405 }));
1608
1611
  for (const [path, handler] of Object.entries(routes)) {
1609
1612
  if (typeof handler === "function") {
1610
- bunRoutes[path] = { GET: wrapRouteHandler(handler, path) };
1613
+ bunRoutes[path] = {
1614
+ GET: wrapRouteHandler(handler, path),
1615
+ OPTIONS: wrapRouteHandler(preflight, path)
1616
+ };
1611
1617
  continue;
1612
1618
  }
1613
1619
  if (handler && typeof handler === "object" && !Array.isArray(handler)) {
@@ -1619,6 +1625,7 @@ function convertAppRoutesToBunRoutes(routes) {
1619
1625
  methods[method.toUpperCase()] = wrapRouteHandler(methodHandler, path);
1620
1626
  }
1621
1627
  if (Object.keys(methods).length > 0) {
1628
+ methods.OPTIONS ??= wrapRouteHandler(preflight, path);
1622
1629
  bunRoutes[path] = methods;
1623
1630
  }
1624
1631
  }
@@ -1627,7 +1634,7 @@ function convertAppRoutesToBunRoutes(routes) {
1627
1634
  }
1628
1635
  function createWebServer(options) {
1629
1636
  const publicDir = options.publicDir ?? "./public";
1630
- const bunRoutes = options.routes ? convertAppRoutesToBunRoutes(options.routes) : undefined;
1637
+ const bunRoutes = options.routes ? convertAppRoutesToBunRoutes(options.routes, options.cors) : undefined;
1631
1638
  return Bun.serve({
1632
1639
  port: options.port,
1633
1640
  ...bunRoutes ? { routes: bunRoutes } : {},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getstrata/bootstrap",
3
- "version": "2.0.5",
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.5",
188
+ "@getstrata/core": "^2.0.6",
189
189
  "typescript": "^5.9.0 || ^6.0.0 || ^7.0.0"
190
190
  },
191
191
  "engines": {