@tatara/http 0.1.2 → 0.1.3

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/dist/index.d.mts CHANGED
@@ -67,6 +67,11 @@ interface CreateAppOptions<E extends HttpEnv> {
67
67
  /** origins are exact (`https://app.example.com`) or one-label patterns (`https://*.example.com`) */
68
68
  cors: {
69
69
  origins: string[];
70
+ /**
71
+ * For origins not in `origins`: true allows it. For origins kept in the database, such as a
72
+ * game's web build address; cache the lookup, since it runs on cross-origin requests.
73
+ */
74
+ allowOrigin?: (origin: string) => boolean | Promise<boolean>;
70
75
  exposeHeaders?: string[];
71
76
  allowHeaders?: string[];
72
77
  };
package/dist/index.mjs CHANGED
@@ -145,13 +145,16 @@ function corsOrigin(origins) {
145
145
  });
146
146
  return (origin) => exact.has(origin) || patterns.some((p) => p.test(origin)) ? origin : null;
147
147
  }
148
+ function allowedOrigin(listed, check) {
149
+ return async (origin) => listed(origin) ?? (origin && check && await check(origin) ? origin : null);
150
+ }
148
151
  /** A plain OpenAPIHono app with request context, secure headers, CORS, body limit, /health and /ready. */
149
152
  function createApp(options) {
150
153
  const app = new OpenAPIHono({ defaultHook: validationHook });
151
154
  app.use(requestContext(options.logger, options.defaults));
152
155
  app.use(secureHeaders());
153
156
  app.use(cors({
154
- origin: corsOrigin(options.cors.origins),
157
+ origin: allowedOrigin(corsOrigin(options.cors.origins), options.cors.allowOrigin),
155
158
  credentials: true,
156
159
  allowHeaders: options.cors.allowHeaders ?? [
157
160
  "Content-Type",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tatara/http",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "tatara HTTP: Hono app setup, errors, envelope, OpenAPI docs and route mounting",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -31,7 +31,7 @@
31
31
  "@hono/zod-openapi": "^1.6.3",
32
32
  "@hono/node-server": "^2.1.5",
33
33
  "@scalar/hono-api-reference": "^0.12.12",
34
- "@tatara/core": "^0.1.2",
34
+ "@tatara/core": "^0.1.3",
35
35
  "pino": "^10.4.0",
36
36
  "zod": "^4.6.7"
37
37
  },
package/src/app.ts CHANGED
@@ -44,7 +44,16 @@ export function requestContext<E extends HttpEnv>(logger: CoreLogger, defaults:
44
44
  export interface CreateAppOptions<E extends HttpEnv> {
45
45
  logger: CoreLogger;
46
46
  /** origins are exact (`https://app.example.com`) or one-label patterns (`https://*.example.com`) */
47
- cors: { origins: string[]; exposeHeaders?: string[]; allowHeaders?: string[] };
47
+ cors: {
48
+ origins: string[];
49
+ /**
50
+ * For origins not in `origins`: true allows it. For origins kept in the database, such as a
51
+ * game's web build address; cache the lookup, since it runs on cross-origin requests.
52
+ */
53
+ allowOrigin?: (origin: string) => boolean | Promise<boolean>;
54
+ exposeHeaders?: string[];
55
+ allowHeaders?: string[];
56
+ };
48
57
  bodyLimitBytes: number;
49
58
  /** a limit on how long a handler may take, with longer ones for slow paths */
50
59
  timeouts?: TimeoutOptions;
@@ -66,6 +75,14 @@ export function corsOrigin(origins: string[]): (origin: string) => string | null
66
75
  return (origin) => (exact.has(origin) || patterns.some((p) => p.test(origin)) ? origin : null);
67
76
  }
68
77
 
78
+ function allowedOrigin(
79
+ listed: (origin: string) => string | null,
80
+ check?: (origin: string) => boolean | Promise<boolean>,
81
+ ): (origin: string) => Promise<string | null> {
82
+ return async (origin) =>
83
+ listed(origin) ?? (origin && check && (await check(origin)) ? origin : null);
84
+ }
85
+
69
86
  /** A plain OpenAPIHono app with request context, secure headers, CORS, body limit, /health and /ready. */
70
87
  export function createApp<E extends HttpEnv>(options: CreateAppOptions<E>): OpenAPIHono<E> {
71
88
  const app = new OpenAPIHono<E>({ defaultHook: validationHook });
@@ -73,7 +90,7 @@ export function createApp<E extends HttpEnv>(options: CreateAppOptions<E>): Open
73
90
  app.use(secureHeaders());
74
91
  app.use(
75
92
  cors({
76
- origin: corsOrigin(options.cors.origins),
93
+ origin: allowedOrigin(corsOrigin(options.cors.origins), options.cors.allowOrigin),
77
94
  credentials: true,
78
95
  allowHeaders: options.cors.allowHeaders ?? [
79
96
  "Content-Type",