@danylohotvianskyi/bobra-framework 0.1.9 → 0.2.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.
- package/batteries/auth/hash.ts +102 -36
- package/batteries/auth/token-metadata.ts +25 -8
- package/batteries/auth/token.ts +5 -5
- package/batteries/openapi/index.ts +62 -41
- package/batteries/openapi/merge-openapi.ts +125 -33
- package/core/config.ts +40 -14
- package/core/discovery.ts +16 -10
- package/core/env-resolver.ts +25 -11
- package/core/factory.ts +150 -77
- package/core/spa.ts +14 -8
- package/db/client.ts +80 -415
- package/db/drizzle-clients.ts +65 -0
- package/db/failover.ts +300 -0
- package/db/index.ts +1 -0
- package/db/migrate.ts +7 -6
- package/db/pgedge.ts +307 -0
- package/db/test/client.test.ts +297 -0
- package/db/token-auth.ts +40 -0
- package/db/types.ts +96 -0
- package/dist/batteries/auth/hash.d.ts +10 -2
- package/dist/batteries/auth/hash.js +78 -43
- package/dist/batteries/auth/hash.js.map +1 -1
- package/dist/batteries/auth/token-metadata.d.ts +3 -3
- package/dist/batteries/auth/token-metadata.js +14 -6
- package/dist/batteries/auth/token-metadata.js.map +1 -1
- package/dist/batteries/auth/token.d.ts +3 -3
- package/dist/batteries/auth/token.js +2 -2
- package/dist/batteries/auth/token.js.map +1 -1
- package/dist/batteries/openapi/index.d.ts +51 -13
- package/dist/batteries/openapi/index.js +25 -15
- package/dist/batteries/openapi/index.js.map +1 -1
- package/dist/batteries/openapi/merge-openapi.d.ts +26 -2
- package/dist/batteries/openapi/merge-openapi.js +83 -26
- package/dist/batteries/openapi/merge-openapi.js.map +1 -1
- package/dist/core/config.d.ts +26 -9
- package/dist/core/config.js +8 -7
- package/dist/core/config.js.map +1 -1
- package/dist/core/discovery.d.ts +18 -9
- package/dist/core/discovery.js +1 -1
- package/dist/core/discovery.js.map +1 -1
- package/dist/core/env-resolver.d.ts +7 -4
- package/dist/core/env-resolver.js +7 -5
- package/dist/core/env-resolver.js.map +1 -1
- package/dist/core/factory.d.ts +36 -16
- package/dist/core/factory.js +70 -38
- package/dist/core/factory.js.map +1 -1
- package/dist/core/spa.js +9 -4
- package/dist/core/spa.js.map +1 -1
- package/dist/db/client.d.ts +16 -66
- package/dist/db/client.js +40 -311
- package/dist/db/client.js.map +1 -1
- package/dist/db/drizzle-clients.d.ts +6 -0
- package/dist/db/drizzle-clients.js +42 -0
- package/dist/db/drizzle-clients.js.map +1 -0
- package/dist/db/failover.d.ts +39 -0
- package/dist/db/failover.js +251 -0
- package/dist/db/failover.js.map +1 -0
- package/dist/db/index.d.ts +1 -0
- package/dist/db/index.js +1 -0
- package/dist/db/index.js.map +1 -1
- package/dist/db/migrate.js +4 -4
- package/dist/db/migrate.js.map +1 -1
- package/dist/db/pgedge.d.ts +14 -0
- package/dist/db/pgedge.js +218 -0
- package/dist/db/pgedge.js.map +1 -0
- package/dist/db/token-auth.d.ts +34 -0
- package/dist/db/token-auth.js +15 -0
- package/dist/db/token-auth.js.map +1 -0
- package/dist/db/types.d.ts +82 -0
- package/dist/db/types.js +2 -0
- package/dist/db/types.js.map +1 -0
- package/dist/logging/logger.d.ts +13 -13
- package/dist/logging/logger.js +11 -2
- package/dist/logging/logger.js.map +1 -1
- package/dist/logging/logging-middleware.d.ts +1 -1
- package/dist/logging/logging-middleware.js +6 -6
- package/dist/logging/logging-middleware.js.map +1 -1
- package/dist/middleware/ensure-api-token.d.ts +33 -4
- package/dist/middleware/ensure-api-token.js.map +1 -1
- package/dist/middleware/utils.d.ts +5 -1
- package/dist/middleware/utils.js +11 -7
- package/dist/middleware/utils.js.map +1 -1
- package/dist/network/router.d.ts +9 -6
- package/dist/network/router.js +3 -1
- package/dist/network/router.js.map +1 -1
- package/dist/network/service-discovery.d.ts +1 -1
- package/dist/network/service-discovery.js.map +1 -1
- package/dist/network/service-fetch.d.ts +6 -9
- package/dist/network/service-fetch.js +14 -4
- package/dist/network/service-fetch.js.map +1 -1
- package/dist/node/generate-wrangler-config.d.ts +28 -4
- package/dist/node/generate-wrangler-config.js +75 -20
- package/dist/node/generate-wrangler-config.js.map +1 -1
- package/logging/logger.ts +31 -21
- package/logging/logging-middleware.ts +12 -9
- package/middleware/ensure-api-token.ts +8 -6
- package/middleware/utils.ts +26 -8
- package/network/router.ts +17 -13
- package/network/service-discovery.ts +1 -1
- package/network/service-fetch.ts +26 -17
- package/node/generate-wrangler-config.ts +160 -24
- package/package.json +10 -7
package/core/factory.ts
CHANGED
|
@@ -17,18 +17,55 @@ import {
|
|
|
17
17
|
} from './config';
|
|
18
18
|
import { serviceDiscoveryMiddleware } from '../network/service-discovery';
|
|
19
19
|
import { generateSpecs } from 'hono-openapi';
|
|
20
|
+
import type { OpenAPIV3 } from 'openapi-types';
|
|
21
|
+
import type { MessageBatch, ExecutionContext, ScheduledController } from '@cloudflare/workers-types';
|
|
22
|
+
|
|
23
|
+
const OPENAPI_METHODS = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'] as const;
|
|
24
|
+
type OpenApiMethod = typeof OPENAPI_METHODS[number];
|
|
25
|
+
type HonoBindingsEnv = { Bindings: Record<string, unknown>; Variables?: Record<string, unknown> };
|
|
26
|
+
|
|
27
|
+
type JsonNode = string | number | boolean | null | JsonNode[] | { [key: string]: JsonNode };
|
|
28
|
+
type QueuePayload = Record<string, string | number | boolean | null>;
|
|
29
|
+
type QueueBatch = MessageBatch<QueuePayload>;
|
|
30
|
+
type WorkerExecutionCtx = ExecutionContext;
|
|
31
|
+
type ScheduledEvent = ScheduledController;
|
|
32
|
+
type SchemaMap = Record<string, OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject>;
|
|
33
|
+
type SecuritySchemeMap = Record<string, OpenAPIV3.SecuritySchemeObject | OpenAPIV3.ReferenceObject>;
|
|
34
|
+
|
|
35
|
+
interface RequestWithCf extends Request {
|
|
36
|
+
cf?: Record<string, unknown> & {
|
|
37
|
+
continent?: string;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isJsonObject(node: JsonNode): node is { [key: string]: JsonNode } {
|
|
42
|
+
return typeof node === 'object' && node !== null && !Array.isArray(node);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isPathItemObject(item: OpenAPIV3.PathItemObject | OpenAPIV3.ReferenceObject): item is OpenAPIV3.PathItemObject {
|
|
46
|
+
return !('$ref' in item);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function hasOperation(pathItem: OpenAPIV3.PathItemObject): boolean {
|
|
50
|
+
return OPENAPI_METHODS.some((method) => Boolean(pathItem[method]));
|
|
51
|
+
}
|
|
20
52
|
|
|
21
53
|
// Helper: collect schema $ref names referenced anywhere in the OpenAPI spec
|
|
22
|
-
function collectReferencedSchemaNames(obj:
|
|
54
|
+
function collectReferencedSchemaNames(obj: JsonNode): Set<string> {
|
|
23
55
|
const set = new Set<string>();
|
|
24
|
-
function scan(val:
|
|
25
|
-
if (
|
|
26
|
-
for (const
|
|
27
|
-
|
|
28
|
-
|
|
56
|
+
function scan(val: JsonNode) {
|
|
57
|
+
if (Array.isArray(val)) {
|
|
58
|
+
for (const item of val) scan(item);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (isJsonObject(val)) {
|
|
63
|
+
for (const [k, value] of Object.entries(val)) {
|
|
64
|
+
if (k === '$ref' && typeof value === 'string') {
|
|
65
|
+
const m = value.match(/^#\/components\/schemas\/([A-Za-z0-9_.-]+)/);
|
|
29
66
|
if (m && m[1]) set.add(m[1]);
|
|
30
67
|
} else {
|
|
31
|
-
scan(
|
|
68
|
+
scan(value);
|
|
32
69
|
}
|
|
33
70
|
}
|
|
34
71
|
}
|
|
@@ -37,19 +74,23 @@ function collectReferencedSchemaNames(obj: any): Set<string> {
|
|
|
37
74
|
return set;
|
|
38
75
|
}
|
|
39
76
|
|
|
40
|
-
function pruneComponents(
|
|
41
|
-
|
|
42
|
-
|
|
77
|
+
function pruneComponents(
|
|
78
|
+
full: OpenAPIV3.Document,
|
|
79
|
+
paths: OpenAPIV3.PathsObject,
|
|
80
|
+
keepAlways: string[] = []
|
|
81
|
+
): OpenAPIV3.ComponentsObject {
|
|
82
|
+
const result: OpenAPIV3.ComponentsObject = { ...(full.components || {}) };
|
|
83
|
+
const schemas = (full.components?.schemas || {}) as SchemaMap;
|
|
43
84
|
|
|
44
85
|
// Start with schemas referenced directly from paths
|
|
45
|
-
const refs = collectReferencedSchemaNames(paths);
|
|
86
|
+
const refs = collectReferencedSchemaNames(paths as JsonNode);
|
|
46
87
|
|
|
47
88
|
// Always-keep schemas should be included in the starting set as well
|
|
48
89
|
for (const name of keepAlways) {
|
|
49
90
|
refs.add(name);
|
|
50
91
|
}
|
|
51
92
|
|
|
52
|
-
const kept:
|
|
93
|
+
const kept: SchemaMap = {};
|
|
53
94
|
|
|
54
95
|
// Traverse schema references transitively using a work queue to ensure
|
|
55
96
|
// nested dependencies are not pruned.
|
|
@@ -65,8 +106,8 @@ function pruneComponents(full: any, paths: any, keepAlways: string[] = []) {
|
|
|
65
106
|
// Keep this schema
|
|
66
107
|
kept[name] = schema;
|
|
67
108
|
|
|
68
|
-
// Find
|
|
69
|
-
const nestedRefs = collectReferencedSchemaNames(schema);
|
|
109
|
+
// Find referenced schemas and add them to the queue if not already seen
|
|
110
|
+
const nestedRefs = collectReferencedSchemaNames(schema as JsonNode);
|
|
70
111
|
for (const dep of nestedRefs) {
|
|
71
112
|
if (!refs.has(dep)) {
|
|
72
113
|
refs.add(dep);
|
|
@@ -83,15 +124,15 @@ export interface OpenAPISpecOptions {
|
|
|
83
124
|
/** The path where the OpenAPI spec will be served (e.g. '/openapi', '/internal/openapi') */
|
|
84
125
|
path?: string;
|
|
85
126
|
/** Return true to exclude an operation from the generated OpenAPI spec */
|
|
86
|
-
excludeEndpoint?: (path: string, method:
|
|
87
|
-
/** Exclude operations that have
|
|
127
|
+
excludeEndpoint?: (path: string, method: OpenApiMethod, operation: OpenAPIV3.OperationObject) => boolean;
|
|
128
|
+
/** Exclude operations that have one of these tags (e.g. ['internal']) */
|
|
88
129
|
excludeTags?: string[];
|
|
89
130
|
/** Whether or not to prune unreferenced OpenAPI component schemas (defaults to true if exclusions are used) */
|
|
90
131
|
pruneComponents?: boolean;
|
|
91
132
|
/** An array of schema names to always keep in the OpenAPI spec if pruneComponents is true */
|
|
92
133
|
pruneComponentsKeepAlways?: string[];
|
|
93
134
|
/** Additional security schemes to add to the OpenAPI spec */
|
|
94
|
-
securitySchemes?:
|
|
135
|
+
securitySchemes?: SecuritySchemeMap;
|
|
95
136
|
}
|
|
96
137
|
|
|
97
138
|
export interface WorkerOptions {
|
|
@@ -102,12 +143,14 @@ export interface WorkerOptions {
|
|
|
102
143
|
export interface WorkerEnv {
|
|
103
144
|
CONFIG_CONTENT?: string;
|
|
104
145
|
WORKER_NAME?: string;
|
|
105
|
-
|
|
146
|
+
__cfContinent?: string;
|
|
147
|
+
// Bindings are deployment-specific and can include D1/KV/R2/DO namespaces.
|
|
148
|
+
[key: string]: unknown;
|
|
106
149
|
}
|
|
107
150
|
|
|
108
151
|
// Create a new worker instance with common middleware
|
|
109
152
|
export async function createWorker(env?: WorkerEnv, options?: WorkerOptions): Promise<AppWorker> {
|
|
110
|
-
const app = new Hono<
|
|
153
|
+
const app = new Hono<HonoBindingsEnv>();
|
|
111
154
|
|
|
112
155
|
const config = await loadConfig(env || {});
|
|
113
156
|
validateConfig(config);
|
|
@@ -149,7 +192,7 @@ export async function createWorker(env?: WorkerEnv, options?: WorkerOptions): Pr
|
|
|
149
192
|
|
|
150
193
|
// Worker-level OpenAPI with router base path prefixing
|
|
151
194
|
(() => {
|
|
152
|
-
const specApp = new Hono<
|
|
195
|
+
const specApp = new Hono<HonoBindingsEnv>();
|
|
153
196
|
const routerBase = getRouterBasePath(config);
|
|
154
197
|
// Mount the worker app under router base path so generated paths include router base
|
|
155
198
|
specApp.route(routerBase === '/' ? '/' : routerBase, app);
|
|
@@ -162,7 +205,7 @@ export async function createWorker(env?: WorkerEnv, options?: WorkerOptions): Pr
|
|
|
162
205
|
description: config.server.description,
|
|
163
206
|
},
|
|
164
207
|
},
|
|
165
|
-
}
|
|
208
|
+
};
|
|
166
209
|
|
|
167
210
|
const specDefs = options?.openapi
|
|
168
211
|
? (Array.isArray(options.openapi) ? options.openapi : [options.openapi])
|
|
@@ -171,32 +214,47 @@ export async function createWorker(env?: WorkerEnv, options?: WorkerOptions): Pr
|
|
|
171
214
|
for (const specDef of specDefs) {
|
|
172
215
|
const specPath = specDef.path || '/openapi';
|
|
173
216
|
app.get(specPath, async (c) => {
|
|
174
|
-
const full = await generateSpecs(specApp, specOptions, undefined, c);
|
|
217
|
+
const full = await generateSpecs(specApp, specOptions, undefined, c) as OpenAPIV3.Document;
|
|
175
218
|
const { excludeEndpoint, excludeTags, pruneComponents: specPrune, securitySchemes } = specDef;
|
|
176
219
|
const hasExclusions = !!excludeEndpoint || (!!excludeTags && excludeTags.length > 0);
|
|
177
220
|
|
|
178
221
|
let finalSpec = full;
|
|
179
222
|
|
|
180
223
|
if (hasExclusions) {
|
|
181
|
-
const filtered:
|
|
182
|
-
for (const [path,
|
|
183
|
-
|
|
184
|
-
|
|
224
|
+
const filtered: OpenAPIV3.Document = { ...full, paths: {} };
|
|
225
|
+
for (const [path, pathItemOrRef] of Object.entries(full.paths ?? {}) as Array<[string, OpenAPIV3.PathItemObject | OpenAPIV3.ReferenceObject]>) {
|
|
226
|
+
if (!pathItemOrRef) continue;
|
|
227
|
+
|
|
228
|
+
if (!isPathItemObject(pathItemOrRef)) {
|
|
229
|
+
filtered.paths[path] = pathItemOrRef;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const keepPathItem: OpenAPIV3.PathItemObject = { ...pathItemOrRef };
|
|
234
|
+
|
|
235
|
+
for (const method of OPENAPI_METHODS) {
|
|
236
|
+
const operation = pathItemOrRef[method];
|
|
237
|
+
if (!operation) continue;
|
|
238
|
+
|
|
185
239
|
let shouldExclude = false;
|
|
186
|
-
if (excludeEndpoint && excludeEndpoint(path, method,
|
|
240
|
+
if (excludeEndpoint && excludeEndpoint(path, method, operation)) {
|
|
187
241
|
shouldExclude = true;
|
|
188
242
|
}
|
|
189
|
-
|
|
190
|
-
|
|
243
|
+
|
|
244
|
+
if (!shouldExclude && excludeTags && Array.isArray(operation.tags)) {
|
|
245
|
+
if (operation.tags.some((tag: string) => excludeTags.includes(tag))) {
|
|
191
246
|
shouldExclude = true;
|
|
192
247
|
}
|
|
193
248
|
}
|
|
194
249
|
|
|
195
|
-
if (
|
|
196
|
-
|
|
250
|
+
if (shouldExclude) {
|
|
251
|
+
delete keepPathItem[method];
|
|
197
252
|
}
|
|
198
253
|
}
|
|
199
|
-
|
|
254
|
+
|
|
255
|
+
if (hasOperation(keepPathItem) || Boolean(keepPathItem.parameters?.length)) {
|
|
256
|
+
filtered.paths[path] = keepPathItem;
|
|
257
|
+
}
|
|
200
258
|
}
|
|
201
259
|
|
|
202
260
|
const shouldPrune = specPrune ?? true;
|
|
@@ -231,7 +289,7 @@ export async function createWorker(env?: WorkerEnv, options?: WorkerOptions): Pr
|
|
|
231
289
|
|
|
232
290
|
// Core worker class that manages handlers and their lifecycle
|
|
233
291
|
export class AppWorker {
|
|
234
|
-
private app: Hono<
|
|
292
|
+
private app: Hono<HonoBindingsEnv>;
|
|
235
293
|
private handlers: AppHandler[] = [];
|
|
236
294
|
private config: AppConfig;
|
|
237
295
|
private workerName: string;
|
|
@@ -240,7 +298,7 @@ export class AppWorker {
|
|
|
240
298
|
private initialized = false;
|
|
241
299
|
private startupStack = new LogStack();
|
|
242
300
|
|
|
243
|
-
constructor(app: Hono<
|
|
301
|
+
constructor(app: Hono<HonoBindingsEnv>, config: AppConfig, workerName: string, basePath: string, options?: WorkerOptions) {
|
|
244
302
|
this.app = app;
|
|
245
303
|
this.config = config;
|
|
246
304
|
this.workerName = workerName;
|
|
@@ -276,7 +334,7 @@ export class AppWorker {
|
|
|
276
334
|
|
|
277
335
|
// Cloudflare Workers export a single queue() per worker, but a worker can consume multiple queues.
|
|
278
336
|
// This dispatcher routes each batch to the correct handler based on batch.queue.
|
|
279
|
-
getQueueFunction(): ((batch:
|
|
337
|
+
getQueueFunction(): ((batch: QueueBatch, env: WorkerEnv, ctx: WorkerExecutionCtx) => Promise<void>) | undefined {
|
|
280
338
|
const queueHandlers = this.handlers.filter(h => typeof h.queue === 'function');
|
|
281
339
|
|
|
282
340
|
if (queueHandlers.length === 0) {
|
|
@@ -292,15 +350,17 @@ export class AppWorker {
|
|
|
292
350
|
getLogger().warn(`Multiple handlers with queue() found but none declare handlesQueue(). Using first one: ${queueHandlers[0]!.name}`);
|
|
293
351
|
}
|
|
294
352
|
|
|
295
|
-
return async (batch:
|
|
353
|
+
return async (batch: QueueBatch, env: WorkerEnv, ctx: WorkerExecutionCtx) => {
|
|
296
354
|
await this.initialize(env);
|
|
297
355
|
|
|
298
356
|
// Build configured queue name list from config for this worker
|
|
299
357
|
let configuredQueues: string[] = [];
|
|
300
358
|
try {
|
|
301
|
-
const bindings = getWorkerQueueBindings(this.config, this.workerName)
|
|
359
|
+
const bindings = getWorkerQueueBindings(this.config, this.workerName);
|
|
302
360
|
const consumers = bindings.consumers ?? [];
|
|
303
|
-
configuredQueues = consumers
|
|
361
|
+
configuredQueues = consumers
|
|
362
|
+
.map((consumer) => consumer.queue)
|
|
363
|
+
.filter((queueName): queueName is string => typeof queueName === 'string' && queueName.length > 0);
|
|
304
364
|
} catch { }
|
|
305
365
|
|
|
306
366
|
// Try matcher-based dispatch
|
|
@@ -366,14 +426,14 @@ export class AppWorker {
|
|
|
366
426
|
};
|
|
367
427
|
}
|
|
368
428
|
|
|
369
|
-
getScheduledFunction(): ((event:
|
|
429
|
+
getScheduledFunction(): ((event: ScheduledEvent, env: WorkerEnv, ctx: WorkerExecutionCtx) => Promise<void>) | undefined {
|
|
370
430
|
const scheduledHandlers = this.handlers.filter(h => typeof h.scheduled === 'function');
|
|
371
431
|
|
|
372
432
|
if (scheduledHandlers.length === 0) {
|
|
373
433
|
return undefined;
|
|
374
434
|
}
|
|
375
435
|
|
|
376
|
-
return async (event:
|
|
436
|
+
return async (event: ScheduledEvent, env: WorkerEnv, ctx: WorkerExecutionCtx) => {
|
|
377
437
|
await this.initialize(env);
|
|
378
438
|
|
|
379
439
|
const promises = scheduledHandlers.map(async (handler) => {
|
|
@@ -431,20 +491,13 @@ export class AppWorker {
|
|
|
431
491
|
|
|
432
492
|
if (handler.name !== 'api-docs') {
|
|
433
493
|
(() => {
|
|
434
|
-
const specApp = new Hono<
|
|
494
|
+
const specApp = new Hono<HonoBindingsEnv>();
|
|
435
495
|
const routerBase = getRouterBasePath(this.config);
|
|
436
496
|
const prefix = routerBase === '/' ? handlerPath : `${routerBase}${handlerPath}`;
|
|
437
497
|
specApp.route(prefix, handler.routes);
|
|
438
498
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
componentSchemas = handler.componentSchemas;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
let handlerSecuritySchemes = {};
|
|
445
|
-
if (handler.securitySchemes) {
|
|
446
|
-
handlerSecuritySchemes = handler.securitySchemes;
|
|
447
|
-
}
|
|
499
|
+
const componentSchemas = (handler.componentSchemas || {}) as SchemaMap;
|
|
500
|
+
const handlerSecuritySchemes = (handler.securitySchemes || {}) as SecuritySchemeMap;
|
|
448
501
|
|
|
449
502
|
const specOptions = {
|
|
450
503
|
documentation: {
|
|
@@ -458,7 +511,7 @@ export class AppWorker {
|
|
|
458
511
|
securitySchemes: handlerSecuritySchemes,
|
|
459
512
|
},
|
|
460
513
|
},
|
|
461
|
-
}
|
|
514
|
+
};
|
|
462
515
|
|
|
463
516
|
const specDefs = this.options?.openapi
|
|
464
517
|
? (Array.isArray(this.options.openapi) ? this.options.openapi : [this.options.openapi])
|
|
@@ -467,32 +520,47 @@ export class AppWorker {
|
|
|
467
520
|
for (const specDef of specDefs) {
|
|
468
521
|
const specPath = specDef.path || '/openapi';
|
|
469
522
|
this.app.get(`${handlerPath}${specPath}`, async (c) => {
|
|
470
|
-
const full = await generateSpecs(specApp, specOptions, undefined, c);
|
|
523
|
+
const full = await generateSpecs(specApp, specOptions, undefined, c) as OpenAPIV3.Document;
|
|
471
524
|
const { excludeEndpoint, excludeTags, pruneComponents: specPrune, securitySchemes } = specDef;
|
|
472
525
|
const hasExclusions = !!excludeEndpoint || (!!excludeTags && excludeTags.length > 0);
|
|
473
526
|
|
|
474
527
|
let finalSpec = full;
|
|
475
528
|
|
|
476
529
|
if (hasExclusions) {
|
|
477
|
-
const filtered:
|
|
478
|
-
for (const [path,
|
|
479
|
-
|
|
480
|
-
|
|
530
|
+
const filtered: OpenAPIV3.Document = { ...full, paths: {} };
|
|
531
|
+
for (const [path, pathItemOrRef] of Object.entries(full.paths ?? {}) as Array<[string, OpenAPIV3.PathItemObject | OpenAPIV3.ReferenceObject]>) {
|
|
532
|
+
if (!pathItemOrRef) continue;
|
|
533
|
+
|
|
534
|
+
if (!isPathItemObject(pathItemOrRef)) {
|
|
535
|
+
filtered.paths[path] = pathItemOrRef;
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const keepPathItem: OpenAPIV3.PathItemObject = { ...pathItemOrRef };
|
|
540
|
+
|
|
541
|
+
for (const method of OPENAPI_METHODS) {
|
|
542
|
+
const operation = pathItemOrRef[method];
|
|
543
|
+
if (!operation) continue;
|
|
544
|
+
|
|
481
545
|
let shouldExclude = false;
|
|
482
|
-
if (excludeEndpoint && excludeEndpoint(path, method,
|
|
546
|
+
if (excludeEndpoint && excludeEndpoint(path, method, operation)) {
|
|
483
547
|
shouldExclude = true;
|
|
484
548
|
}
|
|
485
|
-
|
|
486
|
-
|
|
549
|
+
|
|
550
|
+
if (!shouldExclude && excludeTags && Array.isArray(operation.tags)) {
|
|
551
|
+
if (operation.tags.some((tag: string) => excludeTags.includes(tag))) {
|
|
487
552
|
shouldExclude = true;
|
|
488
553
|
}
|
|
489
554
|
}
|
|
490
555
|
|
|
491
|
-
if (
|
|
492
|
-
|
|
556
|
+
if (shouldExclude) {
|
|
557
|
+
delete keepPathItem[method];
|
|
493
558
|
}
|
|
494
559
|
}
|
|
495
|
-
|
|
560
|
+
|
|
561
|
+
if (hasOperation(keepPathItem) || Boolean(keepPathItem.parameters?.length)) {
|
|
562
|
+
filtered.paths[path] = keepPathItem;
|
|
563
|
+
}
|
|
496
564
|
}
|
|
497
565
|
|
|
498
566
|
const shouldPrune = specPrune ?? true;
|
|
@@ -586,7 +654,7 @@ export class AppWorker {
|
|
|
586
654
|
}
|
|
587
655
|
}
|
|
588
656
|
|
|
589
|
-
getApp(): Hono<
|
|
657
|
+
getApp(): Hono<HonoBindingsEnv> {
|
|
590
658
|
return this.app;
|
|
591
659
|
}
|
|
592
660
|
|
|
@@ -596,18 +664,22 @@ export class AppWorker {
|
|
|
596
664
|
const queueFunction = this.getQueueFunction();
|
|
597
665
|
const scheduledFunction = this.getScheduledFunction();
|
|
598
666
|
|
|
599
|
-
const workerExport:
|
|
600
|
-
|
|
667
|
+
const workerExport: {
|
|
668
|
+
fetch: (request: RequestWithCf, env: WorkerEnv, ctx: WorkerExecutionCtx) => Promise<Response>;
|
|
669
|
+
queue?: (batch: QueueBatch, env: WorkerEnv, ctx: WorkerExecutionCtx) => Promise<void>;
|
|
670
|
+
scheduled?: (event: ScheduledEvent, env: WorkerEnv, ctx: WorkerExecutionCtx) => Promise<void>;
|
|
671
|
+
} = {
|
|
672
|
+
async fetch(request: RequestWithCf, env: WorkerEnv, ctx: WorkerExecutionCtx) {
|
|
601
673
|
try {
|
|
602
674
|
// Merge global and worker-specific vars into env
|
|
603
|
-
const globalVars =
|
|
675
|
+
const globalVars = self.config.vars || {};
|
|
604
676
|
const workerVars = (self.config.workers?.[self.workerName]?.vars) || {};
|
|
605
677
|
const mergedEnv = { ...env, ...globalVars, ...workerVars } as WorkerEnv;
|
|
606
678
|
|
|
607
679
|
// Inject CF location for pgEdge geo-routing
|
|
608
680
|
// Header takes priority: request.cf reflects the worker's colo, not the original client
|
|
609
|
-
const cfContinent = request.headers?.get?.('X-CF-Continent') ||
|
|
610
|
-
if (cfContinent)
|
|
681
|
+
const cfContinent = request.headers?.get?.('X-CF-Continent') || request.cf?.continent;
|
|
682
|
+
if (cfContinent) mergedEnv.__cfContinent = cfContinent;
|
|
611
683
|
|
|
612
684
|
await self.initialize(mergedEnv);
|
|
613
685
|
return await self.app.fetch(request, mergedEnv, ctx);
|
|
@@ -628,8 +700,8 @@ export class AppWorker {
|
|
|
628
700
|
};
|
|
629
701
|
|
|
630
702
|
if (queueFunction) {
|
|
631
|
-
workerExport.queue = async (batch:
|
|
632
|
-
const globalVars =
|
|
703
|
+
workerExport.queue = async (batch: QueueBatch, env: WorkerEnv, ctx: WorkerExecutionCtx) => {
|
|
704
|
+
const globalVars = self.config.vars || {};
|
|
633
705
|
const workerVars = (self.config.workers?.[self.workerName]?.vars) || {};
|
|
634
706
|
const mergedEnv = { ...env, ...globalVars, ...workerVars } as WorkerEnv;
|
|
635
707
|
await queueFunction(batch, mergedEnv, ctx);
|
|
@@ -637,8 +709,8 @@ export class AppWorker {
|
|
|
637
709
|
}
|
|
638
710
|
|
|
639
711
|
if (scheduledFunction) {
|
|
640
|
-
workerExport.scheduled = async (event:
|
|
641
|
-
const globalVars =
|
|
712
|
+
workerExport.scheduled = async (event: ScheduledEvent, env: WorkerEnv, ctx: WorkerExecutionCtx) => {
|
|
713
|
+
const globalVars = self.config.vars || {};
|
|
642
714
|
const workerVars = (self.config.workers?.[self.workerName]?.vars) || {};
|
|
643
715
|
const mergedEnv = { ...env, ...globalVars, ...workerVars } as WorkerEnv;
|
|
644
716
|
await scheduledFunction(event, mergedEnv, ctx);
|
|
@@ -661,7 +733,7 @@ export function createCloudflareWorker(
|
|
|
661
733
|
options?: WorkerOptions
|
|
662
734
|
) {
|
|
663
735
|
return {
|
|
664
|
-
async fetch(request: Request, env: WorkerEnv, ctx:
|
|
736
|
+
async fetch(request: Request, env: WorkerEnv, ctx: WorkerExecutionCtx): Promise<Response> {
|
|
665
737
|
try {
|
|
666
738
|
const workerEnv = {
|
|
667
739
|
...env,
|
|
@@ -670,8 +742,9 @@ export function createCloudflareWorker(
|
|
|
670
742
|
|
|
671
743
|
// Inject CF location for pgEdge geo-routing
|
|
672
744
|
// Header takes priority: request.cf reflects the worker's colo, not the original client
|
|
673
|
-
const
|
|
674
|
-
|
|
745
|
+
const requestWithCf = request as RequestWithCf;
|
|
746
|
+
const cfContinent = request.headers?.get?.('X-CF-Continent') || requestWithCf.cf?.continent;
|
|
747
|
+
if (cfContinent) workerEnv.__cfContinent = cfContinent;
|
|
675
748
|
|
|
676
749
|
const worker = await createWorker(workerEnv, options);
|
|
677
750
|
|
|
@@ -695,7 +768,7 @@ export function createCloudflareWorker(
|
|
|
695
768
|
}
|
|
696
769
|
},
|
|
697
770
|
|
|
698
|
-
async queue(batch:
|
|
771
|
+
async queue(batch: QueueBatch, env: WorkerEnv, ctx: WorkerExecutionCtx): Promise<void> {
|
|
699
772
|
try {
|
|
700
773
|
const workerEnv = {
|
|
701
774
|
...env,
|
|
@@ -723,7 +796,7 @@ export function createCloudflareWorker(
|
|
|
723
796
|
}
|
|
724
797
|
},
|
|
725
798
|
|
|
726
|
-
async scheduled(event:
|
|
799
|
+
async scheduled(event: ScheduledEvent, env: WorkerEnv, ctx: WorkerExecutionCtx): Promise<void> {
|
|
727
800
|
try {
|
|
728
801
|
const workerEnv = {
|
|
729
802
|
...env,
|
package/core/spa.ts
CHANGED
|
@@ -2,9 +2,8 @@ import { Hono } from 'hono';
|
|
|
2
2
|
import { getLogger, initializeLogger } from '../logging/logger';
|
|
3
3
|
import type { AppHandler } from './discovery';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
[key: string]: any;
|
|
5
|
+
interface AssetBinding {
|
|
6
|
+
fetch(request: Request): Promise<Response>;
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
export interface SpaHandlerOptions {
|
|
@@ -44,19 +43,26 @@ export function createSpaHandler(options: SpaHandlerOptions): AppHandler {
|
|
|
44
43
|
ignoreWorkerBasePath = false
|
|
45
44
|
} = options;
|
|
46
45
|
|
|
47
|
-
const app = new Hono<{ Bindings:
|
|
46
|
+
const app = new Hono<{ Bindings: Record<string, unknown> }>();
|
|
48
47
|
|
|
49
48
|
// Handle all requests under this handler's path
|
|
50
49
|
app.all('*', async (c) => {
|
|
51
50
|
const logger = getLogger();
|
|
52
51
|
const url = new URL(c.req.url);
|
|
53
52
|
|
|
54
|
-
const
|
|
55
|
-
|
|
53
|
+
const assetBindingValue = c.env[assetsBinding];
|
|
54
|
+
const hasFetch = typeof assetBindingValue === 'object'
|
|
55
|
+
&& assetBindingValue !== null
|
|
56
|
+
&& 'fetch' in assetBindingValue
|
|
57
|
+
&& typeof (assetBindingValue as AssetBinding).fetch === 'function';
|
|
58
|
+
|
|
59
|
+
if (!hasFetch) {
|
|
56
60
|
logger.error(`${assetsBinding} binding not available within SpaHandler '${name}'`);
|
|
57
61
|
return c.text(`${assetsBinding} binding not available`, 503);
|
|
58
62
|
}
|
|
59
63
|
|
|
64
|
+
const assets = assetBindingValue as AssetBinding;
|
|
65
|
+
|
|
60
66
|
// Recursion guard: prevent infinite loops when ASSETS routes back to the handler
|
|
61
67
|
if (c.req.header('X-Recursive-Fetch') === 'true') {
|
|
62
68
|
return new Response(null, { status: 404 });
|
|
@@ -69,7 +75,7 @@ export function createSpaHandler(options: SpaHandlerOptions): AppHandler {
|
|
|
69
75
|
const assetRequest = new Request(c.req.url, c.req.raw);
|
|
70
76
|
assetRequest.headers.set('X-Recursive-Fetch', 'true');
|
|
71
77
|
|
|
72
|
-
const response = await
|
|
78
|
+
const response = await assets.fetch(assetRequest);
|
|
73
79
|
|
|
74
80
|
return response;
|
|
75
81
|
}
|
|
@@ -86,7 +92,7 @@ export function createSpaHandler(options: SpaHandlerOptions): AppHandler {
|
|
|
86
92
|
const indexRequest = new Request(indexUrl, c.req.raw);
|
|
87
93
|
indexRequest.headers.set('X-Recursive-Fetch', 'true');
|
|
88
94
|
|
|
89
|
-
const response = await
|
|
95
|
+
const response = await assets.fetch(indexRequest);
|
|
90
96
|
|
|
91
97
|
if (response.status !== 200) {
|
|
92
98
|
logger.error(`[SpaHandler:${name}] Failed to fetch ${indexPath}. Status: ${response.status}`);
|