@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/config.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { load } from 'js-yaml';
|
|
2
2
|
import type { LoggerConfig } from '../logging/logger';
|
|
3
3
|
import { JSONValue } from 'hono/utils/types';
|
|
4
|
-
import {
|
|
4
|
+
import { resolveTypedConfig } from './env-resolver';
|
|
5
5
|
|
|
6
6
|
// Configuration interfaces
|
|
7
7
|
export interface CorsConfig {
|
|
@@ -122,6 +122,7 @@ export function flattenHyperdriveConfigs(postgresConfig: HyperdrivePostgresConfi
|
|
|
122
122
|
export interface PgEdgeConfig {
|
|
123
123
|
enabled: boolean;
|
|
124
124
|
locations: string[];
|
|
125
|
+
connection_fallback?: boolean;
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
export interface CloudflareRoute {
|
|
@@ -131,6 +132,13 @@ export interface CloudflareRoute {
|
|
|
131
132
|
zone_name?: string;
|
|
132
133
|
}
|
|
133
134
|
|
|
135
|
+
export interface AssetsConfig {
|
|
136
|
+
directory?: string;
|
|
137
|
+
run_worker_first?: boolean | string[];
|
|
138
|
+
not_found_handling?: 'single-page-application' | '404-page' | 'none';
|
|
139
|
+
html_handling?: 'auto-trailing-slash' | 'force-trailing-slash' | 'drop-trailing-slash' | 'none';
|
|
140
|
+
}
|
|
141
|
+
|
|
134
142
|
export interface WorkerConfig {
|
|
135
143
|
name: string;
|
|
136
144
|
port?: number;
|
|
@@ -156,6 +164,7 @@ export interface WorkerConfig {
|
|
|
156
164
|
};
|
|
157
165
|
vars?: Record<string, string | number | boolean | JSONValue>;
|
|
158
166
|
cf_routes?: CloudflareRoute[];
|
|
167
|
+
assets?: AssetsConfig;
|
|
159
168
|
observability?: {
|
|
160
169
|
logs?: {
|
|
161
170
|
enabled?: boolean;
|
|
@@ -184,6 +193,7 @@ export interface RouterConfig {
|
|
|
184
193
|
kv_namespaces?: WorkerKvNamespaceConfig[];
|
|
185
194
|
vars?: Record<string, string | number | boolean | JSONValue>;
|
|
186
195
|
cf_routes?: CloudflareRoute[];
|
|
196
|
+
assets?: AssetsConfig;
|
|
187
197
|
observability?: {
|
|
188
198
|
logs?: {
|
|
189
199
|
enabled?: boolean;
|
|
@@ -225,6 +235,7 @@ export const defaultConfig: AppConfig = {
|
|
|
225
235
|
pgEdge: {
|
|
226
236
|
enabled: false,
|
|
227
237
|
locations: [],
|
|
238
|
+
connection_fallback: true,
|
|
228
239
|
},
|
|
229
240
|
db_engine: 'postgres',
|
|
230
241
|
vars: {},
|
|
@@ -244,7 +255,7 @@ export const defaultConfig: AppConfig = {
|
|
|
244
255
|
};
|
|
245
256
|
|
|
246
257
|
// Load configuration from YAML content
|
|
247
|
-
export function parseConfig(yamlContent: string, env?: Record<string,
|
|
258
|
+
export function parseConfig(yamlContent: string, env?: Record<string, unknown>): AppConfig {
|
|
248
259
|
try {
|
|
249
260
|
const parsed = load(yamlContent) as Partial<AppConfig>;
|
|
250
261
|
|
|
@@ -259,7 +270,10 @@ export function parseConfig(yamlContent: string, env?: Record<string, any>): App
|
|
|
259
270
|
...parsed.server,
|
|
260
271
|
},
|
|
261
272
|
db_engine: parsed.db_engine || defaultConfig.db_engine,
|
|
262
|
-
pgEdge:
|
|
273
|
+
pgEdge: {
|
|
274
|
+
...defaultConfig.pgEdge,
|
|
275
|
+
...parsed.pgEdge,
|
|
276
|
+
},
|
|
263
277
|
logging: parsed.logging,
|
|
264
278
|
vars: parsed.vars || {},
|
|
265
279
|
workers: parsed.workers || {},
|
|
@@ -272,8 +286,7 @@ export function parseConfig(yamlContent: string, env?: Record<string, any>): App
|
|
|
272
286
|
// Resolve environment variables if env is provided
|
|
273
287
|
if (env) {
|
|
274
288
|
// For worker runtime: env already contains all vars from wrangler (.dev.vars + secrets)
|
|
275
|
-
|
|
276
|
-
config = resolved as AppConfig;
|
|
289
|
+
config = resolveTypedConfig(config, env);
|
|
277
290
|
}
|
|
278
291
|
|
|
279
292
|
return config;
|
|
@@ -287,7 +300,7 @@ export function parseConfig(yamlContent: string, env?: Record<string, any>): App
|
|
|
287
300
|
let configCache: { content: string; config: AppConfig } | null = null;
|
|
288
301
|
|
|
289
302
|
// Load configuration from environment or use default
|
|
290
|
-
export async function loadConfig(env?: { CONFIG_CONTENT?: string;[key: string]:
|
|
303
|
+
export async function loadConfig(env?: { CONFIG_CONTENT?: string; [key: string]: unknown }): Promise<AppConfig> {
|
|
291
304
|
const configContent = env?.CONFIG_CONTENT;
|
|
292
305
|
|
|
293
306
|
if (configContent) {
|
|
@@ -325,7 +338,7 @@ export async function loadConfig(env?: { CONFIG_CONTENT?: string;[key: string]:
|
|
|
325
338
|
// If the content looks like Base64-gzipped data, a plain-text YAML parse will never succeed.
|
|
326
339
|
if (configContent.startsWith('H4sI')) {
|
|
327
340
|
const message = 'Failed to decompress Base64-gzipped CONFIG_CONTENT (invalid Base64/gzip data or missing DecompressionStream support).';
|
|
328
|
-
|
|
341
|
+
throw new Error(message, { cause: e });
|
|
329
342
|
}
|
|
330
343
|
return parseConfig(configContent, env);
|
|
331
344
|
}
|
|
@@ -525,11 +538,11 @@ export function validateConfig(config: AppConfig): void {
|
|
|
525
538
|
|
|
526
539
|
// Helper function to get service binding with direct call fallback
|
|
527
540
|
export function getServiceBindingWithFallback(
|
|
528
|
-
env:
|
|
541
|
+
env: Record<string, unknown>,
|
|
529
542
|
bindingName: string,
|
|
530
543
|
config: AppConfig,
|
|
531
544
|
workerName: string
|
|
532
|
-
): { binding:
|
|
545
|
+
): { binding: unknown; externalUrl?: string } {
|
|
533
546
|
const workerConfig = config.workers?.[workerName];
|
|
534
547
|
const serviceConfig = workerConfig?.services?.find(s => s.binding === bindingName);
|
|
535
548
|
|
|
@@ -549,12 +562,17 @@ export interface ServiceDiscoveryResult {
|
|
|
549
562
|
allWorkers: Record<string, { handlers: string[]; services: Array<{ binding: string; service: string; external_url?: string }> }>;
|
|
550
563
|
}
|
|
551
564
|
|
|
565
|
+
type WorkerRegistryLike = {
|
|
566
|
+
getHandlers?: (workerName: string) => Array<{ name: string }>;
|
|
567
|
+
getHandlerNames?: (workerName: string) => string[];
|
|
568
|
+
};
|
|
569
|
+
|
|
552
570
|
// Get service/handler discovery information for current worker
|
|
553
571
|
export function getServiceDiscovery(
|
|
554
|
-
env:
|
|
572
|
+
env: Record<string, unknown>,
|
|
555
573
|
config: AppConfig,
|
|
556
574
|
workerName: string,
|
|
557
|
-
workerRegistry:
|
|
575
|
+
workerRegistry: WorkerRegistryLike
|
|
558
576
|
): ServiceDiscoveryResult {
|
|
559
577
|
const routerWorkerName = getRouterWorkerName(config);
|
|
560
578
|
const workerConfig = config.workers?.[workerName];
|
|
@@ -564,7 +582,7 @@ export function getServiceDiscovery(
|
|
|
564
582
|
|
|
565
583
|
// Get actually initialized handlers from the registry
|
|
566
584
|
const initializedHandlers = workerRegistry.getHandlers
|
|
567
|
-
? workerRegistry.getHandlers(workerName).map((h
|
|
585
|
+
? workerRegistry.getHandlers(workerName).map((h) => h.name)
|
|
568
586
|
: workerRegistry.getHandlerNames
|
|
569
587
|
? workerRegistry.getHandlerNames(workerName)
|
|
570
588
|
: [];
|
|
@@ -637,8 +655,8 @@ export function getServiceDiscovery(
|
|
|
637
655
|
export function getFetchInstance(
|
|
638
656
|
target: string,
|
|
639
657
|
discovery: ServiceDiscoveryResult,
|
|
640
|
-
env:
|
|
641
|
-
):
|
|
658
|
+
env: Record<string, unknown>
|
|
659
|
+
): FetchInstance | null {
|
|
642
660
|
// First check if it's a handler on current worker
|
|
643
661
|
if (discovery.initializedHandlers.includes(target)) {
|
|
644
662
|
return { type: 'handler', name: target };
|
|
@@ -685,3 +703,11 @@ export function getFetchInstance(
|
|
|
685
703
|
|
|
686
704
|
return null;
|
|
687
705
|
}
|
|
706
|
+
|
|
707
|
+
export interface FetchInstance {
|
|
708
|
+
type: 'handler' | 'service';
|
|
709
|
+
name: string;
|
|
710
|
+
handler?: Function;
|
|
711
|
+
binding?: unknown;
|
|
712
|
+
external_url?: string;
|
|
713
|
+
}
|
package/core/discovery.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import type { Hono } from 'hono';
|
|
2
2
|
import type { LoggerConfig } from '../logging/logger';
|
|
3
3
|
|
|
4
|
+
type HandlerEnv = Record<string, unknown>;
|
|
5
|
+
type HonoEnv = { Bindings: HandlerEnv; Variables?: Record<string, unknown> };
|
|
6
|
+
type QueueBatch = unknown;
|
|
7
|
+
type ScheduledEvent = unknown;
|
|
8
|
+
type ExecutionCtx = unknown;
|
|
9
|
+
|
|
4
10
|
/**
|
|
5
11
|
* Represents a handler that can be registered with an AppWorker.
|
|
6
12
|
* Each handler is a self-contained unit of business logic with its own routes.
|
|
@@ -8,29 +14,29 @@ import type { LoggerConfig } from '../logging/logger';
|
|
|
8
14
|
export interface AppHandler {
|
|
9
15
|
name: string;
|
|
10
16
|
version: string;
|
|
11
|
-
routes: Hono<
|
|
12
|
-
componentSchemas?: Record<string,
|
|
17
|
+
routes: Hono<HonoEnv>;
|
|
18
|
+
componentSchemas?: Record<string, unknown>;
|
|
13
19
|
/** Additional security schemes to declare in the handler's OpenAPI spec */
|
|
14
|
-
securitySchemes?: Record<string,
|
|
20
|
+
securitySchemes?: Record<string, unknown>;
|
|
15
21
|
// Allow handlers to initialize their own logger with the worker's config
|
|
16
22
|
initLogger?: (config: Partial<LoggerConfig>, context?: string, worker?: string, handler?: string) => void;
|
|
17
23
|
// Allow handlers to perform async initialization (e.g., database connections)
|
|
18
|
-
init?: (env:
|
|
24
|
+
init?: (env: HandlerEnv) => Promise<void>;
|
|
19
25
|
// Queue processing generic callback
|
|
20
|
-
queue?: (batch:
|
|
26
|
+
queue?: (batch: QueueBatch, env: HandlerEnv, ctx: ExecutionCtx) => Promise<void>;
|
|
21
27
|
// Determine if this handler handles the specific queue batch
|
|
22
28
|
handlesQueue?: (queueName: string, configuredQueues: string[]) => boolean;
|
|
23
29
|
// Let the handler mount directly on root path
|
|
24
30
|
ignoreWorkerBasePath?: boolean;
|
|
25
31
|
// Optional scheduled handler
|
|
26
|
-
scheduled?: (event:
|
|
32
|
+
scheduled?: (event: ScheduledEvent, env: HandlerEnv, ctx: ExecutionCtx) => Promise<void> | void;
|
|
27
33
|
}
|
|
28
34
|
|
|
29
35
|
/**
|
|
30
36
|
* Represents a registered worker instance with its associated Hono apps.
|
|
31
37
|
*/
|
|
32
38
|
export interface WorkerInstance {
|
|
33
|
-
mainApp: Hono<
|
|
39
|
+
mainApp: Hono<HonoEnv> | null;
|
|
34
40
|
handlers: Map<string, AppHandler>;
|
|
35
41
|
}
|
|
36
42
|
|
|
@@ -42,7 +48,7 @@ export class AppWorkerRegistry {
|
|
|
42
48
|
private workers: Map<string, WorkerInstance> = new Map();
|
|
43
49
|
|
|
44
50
|
// Register a worker's main app
|
|
45
|
-
registerMainApp(workerName: string, app: Hono<
|
|
51
|
+
registerMainApp(workerName: string, app: Hono<HonoEnv>): void {
|
|
46
52
|
const instance = this.getOrCreateWorkerInstance(workerName);
|
|
47
53
|
instance.mainApp = app;
|
|
48
54
|
}
|
|
@@ -70,7 +76,7 @@ export class AppWorkerRegistry {
|
|
|
70
76
|
}
|
|
71
77
|
|
|
72
78
|
// Get main app for a worker
|
|
73
|
-
getMainApp(workerName: string): Hono<
|
|
79
|
+
getMainApp(workerName: string): Hono<HonoEnv> | undefined | null {
|
|
74
80
|
return this.workers.get(workerName)?.mainApp;
|
|
75
81
|
}
|
|
76
82
|
|
|
@@ -86,7 +92,7 @@ export class AppWorkerRegistry {
|
|
|
86
92
|
return Array.from(this.workers.keys());
|
|
87
93
|
}
|
|
88
94
|
|
|
89
|
-
// Check if a handler exists in
|
|
95
|
+
// Check if a handler exists in at least one worker
|
|
90
96
|
hasHandler(handlerName: string): boolean {
|
|
91
97
|
for (const instance of this.workers.values()) {
|
|
92
98
|
if (instance.handlers.has(handlerName)) return true;
|
package/core/env-resolver.ts
CHANGED
|
@@ -17,6 +17,12 @@
|
|
|
17
17
|
* Parse .dev.vars file format (key=value, one per line, shell-style comments)
|
|
18
18
|
* Returns Map for efficient lookup
|
|
19
19
|
*/
|
|
20
|
+
type EnvMap = Record<string, unknown>;
|
|
21
|
+
|
|
22
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
23
|
+
return typeof value === 'object' && value !== null;
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
export function parseDevVars(content: string): Map<string, string> {
|
|
21
27
|
const vars = new Map<string, string>();
|
|
22
28
|
const lines = content.split('\n');
|
|
@@ -74,7 +80,7 @@ export function extractVariableNames(value: string): string[] {
|
|
|
74
80
|
*/
|
|
75
81
|
export function resolveVariable(
|
|
76
82
|
varName: string,
|
|
77
|
-
env:
|
|
83
|
+
env: EnvMap,
|
|
78
84
|
devVars: Map<string, string>
|
|
79
85
|
): string | undefined {
|
|
80
86
|
// 1. Check env bindings (Cloudflare secrets at runtime)
|
|
@@ -97,7 +103,7 @@ export function resolveVariable(
|
|
|
97
103
|
*/
|
|
98
104
|
export function resolveStringValue(
|
|
99
105
|
value: string,
|
|
100
|
-
env:
|
|
106
|
+
env: EnvMap,
|
|
101
107
|
devVars: Map<string, string>
|
|
102
108
|
): string {
|
|
103
109
|
// Pattern matches both $VAR and ${VAR}
|
|
@@ -112,10 +118,10 @@ export function resolveStringValue(
|
|
|
112
118
|
* Handles: strings, numbers, booleans, objects, arrays
|
|
113
119
|
*/
|
|
114
120
|
export function resolveValue(
|
|
115
|
-
value:
|
|
116
|
-
env:
|
|
121
|
+
value: unknown,
|
|
122
|
+
env: EnvMap,
|
|
117
123
|
devVars: Map<string, string>
|
|
118
|
-
):
|
|
124
|
+
): unknown {
|
|
119
125
|
if (value === null || value === undefined) {
|
|
120
126
|
return value;
|
|
121
127
|
}
|
|
@@ -132,8 +138,8 @@ export function resolveValue(
|
|
|
132
138
|
return value.map(item => resolveValue(item, env, devVars));
|
|
133
139
|
}
|
|
134
140
|
|
|
135
|
-
if (
|
|
136
|
-
const resolved: Record<string,
|
|
141
|
+
if (isRecord(value)) {
|
|
142
|
+
const resolved: Record<string, unknown> = {};
|
|
137
143
|
for (const [key, val] of Object.entries(value)) {
|
|
138
144
|
resolved[key] = resolveValue(val, env, devVars);
|
|
139
145
|
}
|
|
@@ -152,9 +158,17 @@ export function resolveValue(
|
|
|
152
158
|
* @param devVars - Optional file-based vars (for Node.js scripts only, defaults to empty)
|
|
153
159
|
*/
|
|
154
160
|
export function resolveConfig(
|
|
155
|
-
config: Record<string,
|
|
156
|
-
env:
|
|
161
|
+
config: Record<string, unknown>,
|
|
162
|
+
env: EnvMap,
|
|
163
|
+
devVars: Map<string, string> = new Map()
|
|
164
|
+
): Record<string, unknown> {
|
|
165
|
+
return resolveValue(config, env, devVars) as Record<string, unknown>;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function resolveTypedConfig<TConfig>(
|
|
169
|
+
config: TConfig,
|
|
170
|
+
env: EnvMap,
|
|
157
171
|
devVars: Map<string, string> = new Map()
|
|
158
|
-
):
|
|
159
|
-
return resolveValue(config, env, devVars);
|
|
172
|
+
): TConfig {
|
|
173
|
+
return resolveValue(config, env, devVars) as TConfig;
|
|
160
174
|
}
|