@jango-blockchained/hoox-shared 1.0.9 → 1.1.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 (74) hide show
  1. package/README.md +17 -0
  2. package/dist/analytics.d.ts +23 -1
  3. package/dist/analytics.d.ts.map +1 -1
  4. package/dist/analytics.js +14 -3
  5. package/dist/api-client.d.ts +12 -2
  6. package/dist/api-client.d.ts.map +1 -1
  7. package/dist/api-client.js +74 -7
  8. package/dist/colors.d.ts +63 -24
  9. package/dist/colors.d.ts.map +1 -1
  10. package/dist/colors.js +60 -19
  11. package/dist/config.d.ts +34 -1
  12. package/dist/config.d.ts.map +1 -1
  13. package/dist/config.js +61 -6
  14. package/dist/cron-handler.d.ts +2 -2
  15. package/dist/cron-handler.d.ts.map +1 -1
  16. package/dist/d1/index.js +1 -0
  17. package/dist/d1/repository.js +297 -0
  18. package/dist/d1/schemas.js +81 -0
  19. package/dist/exchanges/base-exchange-client.js +120 -0
  20. package/dist/exchanges/types.js +1 -0
  21. package/dist/index.d.ts +21 -11
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +580 -60
  24. package/dist/middleware/auth.d.ts +42 -6
  25. package/dist/middleware/auth.d.ts.map +1 -1
  26. package/dist/middleware/auth.js +151 -0
  27. package/dist/middleware/cors.d.ts +28 -2
  28. package/dist/middleware/cors.d.ts.map +1 -1
  29. package/dist/middleware/cors.js +77 -0
  30. package/dist/middleware/index.d.ts +2 -2
  31. package/dist/middleware/index.d.ts.map +1 -1
  32. package/dist/middleware/index.js +192 -107
  33. package/dist/middleware/logger.js +202 -0
  34. package/dist/middleware/rate-limit.d.ts.map +1 -1
  35. package/dist/middleware/rate-limit.js +181 -0
  36. package/dist/middleware/security-headers.js +58 -0
  37. package/dist/middleware/validate.js +56 -0
  38. package/dist/operator-transport.d.ts +61 -0
  39. package/dist/operator-transport.d.ts.map +1 -0
  40. package/dist/operator-transport.js +87 -0
  41. package/dist/path-utils.d.ts +59 -1
  42. package/dist/path-utils.d.ts.map +1 -1
  43. package/dist/path-utils.js +89 -2
  44. package/dist/queue-handler.d.ts +3 -3
  45. package/dist/queue-handler.d.ts.map +1 -1
  46. package/dist/router.d.ts +2 -2
  47. package/dist/router.d.ts.map +1 -1
  48. package/dist/schemas/index.js +7 -4
  49. package/dist/schemas/registry.js +387 -0
  50. package/dist/schemas/types.js +1 -0
  51. package/dist/schemas/validators.d.ts.map +1 -1
  52. package/dist/schemas/validators.js +290 -0
  53. package/dist/service-bindings.d.ts +65 -0
  54. package/dist/service-bindings.d.ts.map +1 -1
  55. package/dist/service-bindings.js +216 -1
  56. package/dist/session.d.ts.map +1 -1
  57. package/dist/session.js +97 -2
  58. package/dist/sse.d.ts +12 -3
  59. package/dist/sse.d.ts.map +1 -1
  60. package/dist/sse.js +70 -7
  61. package/dist/stores/config-store.d.ts.map +1 -1
  62. package/dist/stores/config-store.js +129 -6
  63. package/dist/stores/service-store.d.ts.map +1 -1
  64. package/dist/stores/service-store.js +96 -25
  65. package/dist/types.d.ts +66 -1
  66. package/dist/types.d.ts.map +1 -1
  67. package/dist/types.js +11 -0
  68. package/dist/wizard/engine.js +501 -0
  69. package/dist/wizard/index.js +2 -1
  70. package/dist/wizard/persistence.js +31 -0
  71. package/dist/wizard/presets.js +199 -0
  72. package/dist/wizard/provisioner.js +1 -0
  73. package/dist/wizard/types.js +1 -0
  74. package/package.json +38 -12
@@ -6,14 +6,47 @@ import type { Env } from "../types";
6
6
  import type { MiddlewareHandler } from "../types/router";
7
7
  /**
8
8
  * Constant-time string comparison to prevent timing attacks.
9
- * Uses crypto.timingSafeEqual for edge-native constant-time comparison.
9
+ *
10
+ * Length is NOT short-circuited: both strings are hashed to fixed-size
11
+ * digests via a simple XOR-fold into equal-length buffers so that
12
+ * unequal lengths do not leak via early return (length oracle).
13
+ *
14
+ * Note: true crypto.subtle.timingSafeEqual needs equal-length ArrayBuffers;
15
+ * we pad both encodings to the same max length before XOR comparison.
10
16
  */
11
17
  export declare function timingSafeEqual(a: string, b: string): boolean;
18
+ /**
19
+ * Environment bindings accepted for operator Bearer auth.
20
+ * Prefer OPERATOR_API_KEY; INTERNAL_API_KEY remains a legacy alias.
21
+ */
22
+ export interface OperatorAuthEnv {
23
+ OPERATOR_API_KEY?: string;
24
+ INTERNAL_API_KEY?: string;
25
+ [key: string]: unknown;
26
+ }
27
+ /**
28
+ * Resolve the operator API secret from Worker env.
29
+ * Order: OPERATOR_API_KEY → INTERNAL_API_KEY (legacy).
30
+ */
31
+ export declare function resolveOperatorApiKey(env: OperatorAuthEnv | Env): string | undefined;
12
32
  /**
13
33
  * Require Bearer token authentication via Authorization header.
14
- * Use for external API endpoints that need token-based auth.
34
+ * Use for external operator / management API endpoints.
35
+ *
36
+ * Secret resolution: `OPERATOR_API_KEY` (preferred) or `INTERNAL_API_KEY` (legacy).
37
+ * Client should send the same value as `HOOX_API_TOKEN`.
38
+ */
39
+ export declare function requireAuth(request: Request, env: Env | OperatorAuthEnv): Promise<Response | null>;
40
+ /**
41
+ * Explicit operator-plane auth (alias of requireAuth with clearer naming).
42
+ * Fail-closed when no operator key is configured.
43
+ */
44
+ export declare function requireOperatorAuth(request: Request, env: OperatorAuthEnv | Env): Promise<Response | null>;
45
+ /**
46
+ * Router middleware that enforces operator Bearer auth.
47
+ * Usage: router.get("/v1/workers", handler, [createOperatorAuthMiddleware()])
15
48
  */
16
- export declare function requireAuth(request: Request, env: Env): Promise<Response | null>;
49
+ export declare function createOperatorAuthMiddleware(): MiddlewareHandler<OperatorAuthEnv>;
17
50
  /**
18
51
  * Environment with an internal auth key binding.
19
52
  * Workers that accept internal service-to-service requests should extend this.
@@ -24,6 +57,9 @@ export declare function requireAuth(request: Request, env: Env): Promise<Respons
24
57
  export interface InternalAuthEnv {
25
58
  [key: string]: unknown;
26
59
  }
60
+ export type InternalAuthKeyName = string | readonly string[];
61
+ /** Collect configured secrets for one or more env binding names (first wins for callers). */
62
+ export declare function collectInternalAuthKeys(env: InternalAuthEnv, keyNames: InternalAuthKeyName): string[];
27
63
  /**
28
64
  * Require internal service-to-service authentication via X-Internal-Auth-Key header.
29
65
  * Use for endpoints called by other workers via Service Bindings.
@@ -36,7 +72,7 @@ export interface InternalAuthEnv {
36
72
  * @param keyName - The env binding name for the internal key (default: 'INTERNAL_KEY_BINDING')
37
73
  * @returns Response if unauthorized, null if authorized
38
74
  */
39
- export declare function requireInternalAuth(request: Request, env: InternalAuthEnv, keyName?: string): Response | null;
75
+ export declare function requireInternalAuth(request: Request, env: InternalAuthEnv, keyName?: InternalAuthKeyName): Response | null;
40
76
  /**
41
77
  * Create a middleware function for the router that enforces internal auth.
42
78
  * Returns a Response (401) when unauthorized, or void when authorized.
@@ -45,7 +81,7 @@ export declare function requireInternalAuth(request: Request, env: InternalAuthE
45
81
  *
46
82
  * @param keyName - The env binding name for the internal key (default: 'INTERNAL_KEY_BINDING')
47
83
  */
48
- export declare function createInternalAuthMiddleware(keyName?: string): MiddlewareHandler<any>;
84
+ export declare function createInternalAuthMiddleware(keyName?: InternalAuthKeyName): MiddlewareHandler<InternalAuthEnv>;
49
85
  /**
50
86
  * Check internal auth and return a result object (non-throwing pattern).
51
87
  * Useful when you need to check auth without immediately returning a response.
@@ -55,7 +91,7 @@ export declare function createInternalAuthMiddleware(keyName?: string): Middlewa
55
91
  * @param keyName - The env binding name for the internal key (default: 'INTERNAL_KEY_BINDING')
56
92
  * @returns Object with authorized flag and optional error message
57
93
  */
58
- export declare function checkInternalAuth(request: Request, env: InternalAuthEnv, keyName?: string): {
94
+ export declare function checkInternalAuth(request: Request, env: InternalAuthEnv, keyName?: InternalAuthKeyName): {
59
95
  authorized: boolean;
60
96
  error?: string;
61
97
  };
@@ -1 +1 @@
1
- {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/middleware/auth.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,KAAK,E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OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAezC"}
@@ -0,0 +1,151 @@
1
+ // @bun
2
+ var __defProp = Object.defineProperty;
3
+ var __returnValue = (v) => v;
4
+ function __exportSetter(name, newValue) {
5
+ this[name] = __returnValue.bind(null, newValue);
6
+ }
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true,
12
+ configurable: true,
13
+ set: __exportSetter.bind(all, name)
14
+ });
15
+ };
16
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
17
+
18
+ // src/middleware/auth.ts
19
+ function timingSafeEqual(a, b) {
20
+ const encoder = new TextEncoder;
21
+ const aBuf = encoder.encode(a);
22
+ const bBuf = encoder.encode(b);
23
+ const len = Math.max(aBuf.length, bBuf.length);
24
+ let result = aBuf.length === bBuf.length ? 0 : 1;
25
+ for (let i = 0;i < len; i++) {
26
+ const av = i < aBuf.length ? aBuf[i] : 0;
27
+ const bv = i < bBuf.length ? bBuf[i] : 0;
28
+ result |= av ^ bv;
29
+ }
30
+ return result === 0;
31
+ }
32
+ function resolveOperatorApiKey(env) {
33
+ const preferred = env.OPERATOR_API_KEY;
34
+ if (typeof preferred === "string" && preferred.length > 0) {
35
+ return preferred;
36
+ }
37
+ const legacy = env.INTERNAL_API_KEY;
38
+ if (typeof legacy === "string" && legacy.length > 0) {
39
+ return legacy;
40
+ }
41
+ return;
42
+ }
43
+ async function requireAuth(request, env) {
44
+ return requireOperatorAuth(request, env);
45
+ }
46
+ async function requireOperatorAuth(request, env) {
47
+ const apiKey = resolveOperatorApiKey(env);
48
+ if (!apiKey) {
49
+ return new Response(JSON.stringify({
50
+ error: "Operator API key not configured",
51
+ hint: "Set OPERATOR_API_KEY (preferred) or INTERNAL_API_KEY on the Worker"
52
+ }), { status: 401, headers: { "Content-Type": "application/json" } });
53
+ }
54
+ const authHeader = request.headers.get("Authorization");
55
+ const expectedHeader = `Bearer ${apiKey}`;
56
+ if (!authHeader || !timingSafeEqual(authHeader, expectedHeader)) {
57
+ return new Response(JSON.stringify({ error: "Unauthorized" }), {
58
+ status: 401,
59
+ headers: { "Content-Type": "application/json" }
60
+ });
61
+ }
62
+ return null;
63
+ }
64
+ function createOperatorAuthMiddleware() {
65
+ return async (request, env, _ctx) => {
66
+ const denied = await requireOperatorAuth(request, env);
67
+ if (denied)
68
+ return denied;
69
+ return;
70
+ };
71
+ }
72
+ function normalizeKeyNames(keyName) {
73
+ if (typeof keyName === "string") {
74
+ return [keyName];
75
+ }
76
+ return [...keyName];
77
+ }
78
+ function collectInternalAuthKeys(env, keyNames) {
79
+ const keys = [];
80
+ for (const name of normalizeKeyNames(keyNames)) {
81
+ const value = env[name];
82
+ if (typeof value === "string" && value.length > 0) {
83
+ keys.push(value);
84
+ }
85
+ }
86
+ return keys;
87
+ }
88
+ function matchesAnyInternalAuthKey(providedKey, expectedKeys) {
89
+ for (const expected of expectedKeys) {
90
+ if (timingSafeEqual(providedKey, expected)) {
91
+ return true;
92
+ }
93
+ }
94
+ return false;
95
+ }
96
+ function internalAuthNotConfiguredResponse(keyNames) {
97
+ const label = normalizeKeyNames(keyNames).join(" | ");
98
+ return new Response(JSON.stringify({
99
+ success: false,
100
+ error: `Internal auth key(s) not configured: ${label}`
101
+ }), { status: 401, headers: { "Content-Type": "application/json" } });
102
+ }
103
+ function requireInternalAuth(request, env, keyName = "INTERNAL_KEY_BINDING") {
104
+ const expectedKeys = collectInternalAuthKeys(env, keyName);
105
+ if (expectedKeys.length === 0) {
106
+ return internalAuthNotConfiguredResponse(keyName);
107
+ }
108
+ const providedKey = request.headers.get("X-Internal-Auth-Key");
109
+ if (!providedKey || !matchesAnyInternalAuthKey(providedKey, expectedKeys)) {
110
+ return new Response(JSON.stringify({ success: false, error: "Unauthorized" }), { status: 401, headers: { "Content-Type": "application/json" } });
111
+ }
112
+ return null;
113
+ }
114
+ function createInternalAuthMiddleware(keyName = "INTERNAL_KEY_BINDING") {
115
+ return async (request, env, _ctx) => {
116
+ const expectedKeys = collectInternalAuthKeys(env, keyName);
117
+ if (expectedKeys.length === 0) {
118
+ return internalAuthNotConfiguredResponse(keyName);
119
+ }
120
+ const providedKey = request.headers.get("X-Internal-Auth-Key");
121
+ if (!providedKey || !matchesAnyInternalAuthKey(providedKey, expectedKeys)) {
122
+ return new Response(JSON.stringify({ success: false, error: "Unauthorized" }), { status: 401, headers: { "Content-Type": "application/json" } });
123
+ }
124
+ return;
125
+ };
126
+ }
127
+ function checkInternalAuth(request, env, keyName = "INTERNAL_KEY_BINDING") {
128
+ const expectedKeys = collectInternalAuthKeys(env, keyName);
129
+ if (expectedKeys.length === 0) {
130
+ return {
131
+ authorized: false,
132
+ error: `${normalizeKeyNames(keyName).join(" | ")} not configured`
133
+ };
134
+ }
135
+ const providedKey = request.headers.get("X-Internal-Auth-Key");
136
+ if (!providedKey || !matchesAnyInternalAuthKey(providedKey, expectedKeys)) {
137
+ return { authorized: false, error: "Unauthorized" };
138
+ }
139
+ return { authorized: true };
140
+ }
141
+ export {
142
+ timingSafeEqual,
143
+ resolveOperatorApiKey,
144
+ requireOperatorAuth,
145
+ requireInternalAuth,
146
+ requireAuth,
147
+ createOperatorAuthMiddleware,
148
+ createInternalAuthMiddleware,
149
+ collectInternalAuthKeys,
150
+ checkInternalAuth
151
+ };
@@ -2,14 +2,24 @@
2
2
  * CORS middleware for Cloudflare Workers
3
3
  * Provides standardized CORS headers and preflight handling
4
4
  *
5
+ * Security default: NO open origin (*). Internal service-binding workers
6
+ * should not advertise browser CORS at all. Public endpoints must pass
7
+ * an explicit allowOrigin (or use publicCorsHeaders()).
8
+ *
5
9
  * @available — Available for consumer use. Import from "@jango-blockchained/hoox-shared/middleware".
6
10
  */
11
+ export interface CorsEnv {
12
+ CORS_ALLOW_ORIGIN?: string;
13
+ }
7
14
  export interface CorsOptions {
8
- /** Allowed origins (default: "*") */
15
+ /**
16
+ * Allowed origins. Empty / omitted = no Access-Control-Allow-Origin header
17
+ * (fail-closed). Never defaults to "*".
18
+ */
9
19
  allowOrigin?: string;
10
20
  /** Allowed methods (default: "GET, POST, OPTIONS, PUT, DELETE") */
11
21
  allowMethods?: string;
12
- /** Allowed headers (default: "Content-Type, Authorization, X-Request-ID, X-Internal-Auth-Key") */
22
+ /** Allowed headers (default: "Content-Type, Authorization, X-Request-ID") */
13
23
  allowHeaders?: string;
14
24
  /** Whether to include credentials (default: false) */
15
25
  allowCredentials?: boolean;
@@ -18,8 +28,24 @@ export interface CorsOptions {
18
28
  }
19
29
  /**
20
30
  * Create CORS headers object for use in responses.
31
+ * Omits Access-Control-Allow-Origin when allowOrigin is empty.
21
32
  */
22
33
  export declare function corsHeaders(options?: CorsOptions): Record<string, string>;
34
+ /**
35
+ * CORS headers for truly public browser-facing APIs.
36
+ * Prefer a concrete origin over "*" whenever possible.
37
+ */
38
+ export declare function publicCorsHeaders(origin?: string, options?: Omit<CorsOptions, "allowOrigin">): Record<string, string>;
39
+ /**
40
+ * CORS headers for internal workers (service bindings only).
41
+ * Returns an empty object — no browser CORS surface.
42
+ */
43
+ export declare function internalCorsHeaders(): Record<string, string>;
44
+ /**
45
+ * Resolve CORS from env + request for dashboard-facing workers.
46
+ * Set CORS_ALLOW_ORIGIN to a comma-separated browser origin allowlist.
47
+ */
48
+ export declare function resolveCorsOptions(request: Request, env?: unknown): CorsOptions;
23
49
  /**
24
50
  * Handle CORS preflight (OPTIONS) requests.
25
51
  * Returns a Response for OPTIONS, or null for non-OPTIONS requests.
@@ -1 +1 @@
1
- {"version":3,"file":"cors.d.ts","sourceRoot":"","sources":["../../src/middleware/cors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,WAAW;IAC1B,qCAAqC;IACrC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,mEAAmE;IACnE,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kGAAkG;IAClG,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sDAAs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,OAAO,GACZ,WAAW,CA4Bb;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,OAAO,EAChB,OAAO,CAAC,EAAE,WAAW,GACpB,QAAQ,GAAG,IAAI,CAOjB"}
@@ -0,0 +1,77 @@
1
+ // @bun
2
+ var __defProp = Object.defineProperty;
3
+ var __returnValue = (v) => v;
4
+ function __exportSetter(name, newValue) {
5
+ this[name] = __returnValue.bind(null, newValue);
6
+ }
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true,
12
+ configurable: true,
13
+ set: __exportSetter.bind(all, name)
14
+ });
15
+ };
16
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
17
+
18
+ // src/middleware/cors.ts
19
+ var DEFAULT_OPTIONS = {
20
+ allowOrigin: "",
21
+ allowMethods: "GET, POST, OPTIONS, PUT, DELETE",
22
+ allowHeaders: "Content-Type, Authorization, X-Request-ID",
23
+ allowCredentials: false,
24
+ maxAge: 86400
25
+ };
26
+ function corsHeaders(options) {
27
+ const opts = { ...DEFAULT_OPTIONS, ...options };
28
+ const headers = {
29
+ "Access-Control-Allow-Methods": opts.allowMethods,
30
+ "Access-Control-Allow-Headers": opts.allowHeaders,
31
+ "Access-Control-Max-Age": String(opts.maxAge)
32
+ };
33
+ if (opts.allowOrigin) {
34
+ headers["Access-Control-Allow-Origin"] = opts.allowOrigin;
35
+ }
36
+ if (opts.allowCredentials) {
37
+ headers["Access-Control-Allow-Credentials"] = "true";
38
+ }
39
+ return headers;
40
+ }
41
+ function publicCorsHeaders(origin = "*", options) {
42
+ return corsHeaders({ ...options, allowOrigin: origin });
43
+ }
44
+ function internalCorsHeaders() {
45
+ return {};
46
+ }
47
+ function resolveCorsOptions(request, env) {
48
+ const raw = env && typeof env === "object" && "CORS_ALLOW_ORIGIN" in env && typeof env.CORS_ALLOW_ORIGIN === "string" ? env.CORS_ALLOW_ORIGIN : undefined;
49
+ const configured = raw?.trim();
50
+ if (!configured) {
51
+ return {};
52
+ }
53
+ const allowed = configured.split(",").map((entry) => entry.trim()).filter(Boolean);
54
+ const origin = request.headers.get("Origin");
55
+ if (origin && allowed.includes(origin)) {
56
+ return { allowOrigin: origin, allowCredentials: true };
57
+ }
58
+ if (!origin && allowed.length === 1) {
59
+ return { allowOrigin: allowed[0] };
60
+ }
61
+ return {};
62
+ }
63
+ function handleCorsPreflightRequest(request, options) {
64
+ if (request.method !== "OPTIONS")
65
+ return null;
66
+ return new Response(null, {
67
+ status: 204,
68
+ headers: corsHeaders(options)
69
+ });
70
+ }
71
+ export {
72
+ resolveCorsOptions,
73
+ publicCorsHeaders,
74
+ internalCorsHeaders,
75
+ handleCorsPreflightRequest,
76
+ corsHeaders
77
+ };
@@ -2,9 +2,9 @@
2
2
  * Barrel exports for shared middleware
3
3
  */
4
4
  export { createLogger, withRequestLog, type Logger, type LogContext, } from "./logger";
5
- export { requireAuth, requireInternalAuth, createInternalAuthMiddleware, checkInternalAuth, timingSafeEqual, type InternalAuthEnv, } from "./auth";
5
+ export { requireAuth, requireOperatorAuth, createOperatorAuthMiddleware, resolveOperatorApiKey, requireInternalAuth, createInternalAuthMiddleware, checkInternalAuth, timingSafeEqual, collectInternalAuthKeys, type InternalAuthEnv, type InternalAuthKeyName, type OperatorAuthEnv, } from "./auth";
6
6
  export { createRateLimiter, type RateLimiter, type RateLimitConfig, } from "./rate-limit";
7
7
  export { validateJson, validateJsonLegacy, requireField, optionalField, } from "./validate";
8
- export { corsHeaders, handleCorsPreflightRequest, type CorsOptions, } from "./cors";
8
+ export { corsHeaders, resolveCorsOptions, type CorsEnv, publicCorsHeaders, internalCorsHeaders, handleCorsPreflightRequest, type CorsOptions, } from "./cors";
9
9
  export { secureHeaders, wrapWithSecurityHeaders, type SecurityHeadersOptions, SECURITY_HEADERS_DEFAULTS, } from "./security-headers";
10
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,YAAY,EACZ,cAAc,EACd,KAAK,MAAM,EACX,KAAK,UAAU,GAChB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,4BAA4B,EAC5B,iBAAiB,EACjB,eAAe,EACf,KAAK,eAAe,GACrB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,iBAAiB,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,GACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,aAAa,GACd,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,0BAA0B,EAC1B,KAAK,WAAW,GACjB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,aAAa,EACb,uBAAuB,EACvB,KAAK,sBAAsB,EAC3B,yBAAyB,GAC1B,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,YAAY,EACZ,cAAc,EACd,KAAK,MAAM,EACX,KAAK,UAAU,GAChB,MAAM,UAAU,CAAC;AAClB,OAAO,EACL,WAAW,EACX,mBAAmB,EACnB,4BAA4B,EAC5B,qBAAqB,EACrB,mBAAmB,EACnB,4BAA4B,EAC5B,iBAAiB,EACjB,eAAe,EACf,uBAAuB,EACvB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,eAAe,GACrB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,iBAAiB,EACjB,KAAK,WAAW,EAChB,KAAK,eAAe,GACrB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,YAAY,EACZ,kBAAkB,EAClB,YAAY,EACZ,aAAa,GACd,MAAM,YAAY,CAAC;AACpB,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,KAAK,OAAO,EACZ,iBAAiB,EACjB,mBAAmB,EACnB,0BAA0B,EAC1B,KAAK,WAAW,GACjB,MAAM,QAAQ,CAAC;AAChB,OAAO,EACL,aAAa,EACb,uBAAuB,EACvB,KAAK,sBAAsB,EAC3B,yBAAyB,GAC1B,MAAM,oBAAoB,CAAC"}