@danylohotvianskyi/bobra-framework 0.1.9 → 0.2.1

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 (102) hide show
  1. package/batteries/auth/hash.ts +102 -36
  2. package/batteries/auth/token-metadata.ts +25 -8
  3. package/batteries/auth/token.ts +5 -5
  4. package/batteries/openapi/index.ts +62 -41
  5. package/batteries/openapi/merge-openapi.ts +125 -33
  6. package/core/config.ts +40 -14
  7. package/core/discovery.ts +16 -10
  8. package/core/env-resolver.ts +25 -11
  9. package/core/factory.ts +150 -77
  10. package/core/spa.ts +14 -8
  11. package/db/client.ts +80 -415
  12. package/db/drizzle-clients.ts +65 -0
  13. package/db/failover.ts +318 -0
  14. package/db/index.ts +1 -0
  15. package/db/migrate.ts +7 -6
  16. package/db/pgedge.ts +307 -0
  17. package/db/test/client.test.ts +346 -0
  18. package/db/token-auth.ts +40 -0
  19. package/db/types.ts +96 -0
  20. package/dist/batteries/auth/hash.d.ts +10 -2
  21. package/dist/batteries/auth/hash.js +78 -43
  22. package/dist/batteries/auth/hash.js.map +1 -1
  23. package/dist/batteries/auth/token-metadata.d.ts +3 -3
  24. package/dist/batteries/auth/token-metadata.js +14 -6
  25. package/dist/batteries/auth/token-metadata.js.map +1 -1
  26. package/dist/batteries/auth/token.d.ts +3 -3
  27. package/dist/batteries/auth/token.js +2 -2
  28. package/dist/batteries/auth/token.js.map +1 -1
  29. package/dist/batteries/openapi/index.d.ts +51 -13
  30. package/dist/batteries/openapi/index.js +25 -15
  31. package/dist/batteries/openapi/index.js.map +1 -1
  32. package/dist/batteries/openapi/merge-openapi.d.ts +26 -2
  33. package/dist/batteries/openapi/merge-openapi.js +83 -26
  34. package/dist/batteries/openapi/merge-openapi.js.map +1 -1
  35. package/dist/core/config.d.ts +26 -9
  36. package/dist/core/config.js +8 -7
  37. package/dist/core/config.js.map +1 -1
  38. package/dist/core/discovery.d.ts +18 -9
  39. package/dist/core/discovery.js +1 -1
  40. package/dist/core/discovery.js.map +1 -1
  41. package/dist/core/env-resolver.d.ts +7 -4
  42. package/dist/core/env-resolver.js +7 -5
  43. package/dist/core/env-resolver.js.map +1 -1
  44. package/dist/core/factory.d.ts +36 -16
  45. package/dist/core/factory.js +70 -38
  46. package/dist/core/factory.js.map +1 -1
  47. package/dist/core/spa.js +9 -4
  48. package/dist/core/spa.js.map +1 -1
  49. package/dist/db/client.d.ts +16 -66
  50. package/dist/db/client.js +40 -311
  51. package/dist/db/client.js.map +1 -1
  52. package/dist/db/drizzle-clients.d.ts +6 -0
  53. package/dist/db/drizzle-clients.js +42 -0
  54. package/dist/db/drizzle-clients.js.map +1 -0
  55. package/dist/db/failover.d.ts +39 -0
  56. package/dist/db/failover.js +267 -0
  57. package/dist/db/failover.js.map +1 -0
  58. package/dist/db/index.d.ts +1 -0
  59. package/dist/db/index.js +1 -0
  60. package/dist/db/index.js.map +1 -1
  61. package/dist/db/migrate.js +4 -4
  62. package/dist/db/migrate.js.map +1 -1
  63. package/dist/db/pgedge.d.ts +14 -0
  64. package/dist/db/pgedge.js +218 -0
  65. package/dist/db/pgedge.js.map +1 -0
  66. package/dist/db/token-auth.d.ts +34 -0
  67. package/dist/db/token-auth.js +15 -0
  68. package/dist/db/token-auth.js.map +1 -0
  69. package/dist/db/types.d.ts +82 -0
  70. package/dist/db/types.js +2 -0
  71. package/dist/db/types.js.map +1 -0
  72. package/dist/logging/logger.d.ts +13 -13
  73. package/dist/logging/logger.js +11 -2
  74. package/dist/logging/logger.js.map +1 -1
  75. package/dist/logging/logging-middleware.d.ts +1 -1
  76. package/dist/logging/logging-middleware.js +6 -6
  77. package/dist/logging/logging-middleware.js.map +1 -1
  78. package/dist/middleware/ensure-api-token.d.ts +33 -4
  79. package/dist/middleware/ensure-api-token.js.map +1 -1
  80. package/dist/middleware/utils.d.ts +5 -1
  81. package/dist/middleware/utils.js +11 -7
  82. package/dist/middleware/utils.js.map +1 -1
  83. package/dist/network/router.d.ts +9 -6
  84. package/dist/network/router.js +3 -1
  85. package/dist/network/router.js.map +1 -1
  86. package/dist/network/service-discovery.d.ts +1 -1
  87. package/dist/network/service-discovery.js.map +1 -1
  88. package/dist/network/service-fetch.d.ts +6 -9
  89. package/dist/network/service-fetch.js +14 -4
  90. package/dist/network/service-fetch.js.map +1 -1
  91. package/dist/node/generate-wrangler-config.d.ts +28 -4
  92. package/dist/node/generate-wrangler-config.js +75 -20
  93. package/dist/node/generate-wrangler-config.js.map +1 -1
  94. package/logging/logger.ts +31 -21
  95. package/logging/logging-middleware.ts +12 -9
  96. package/middleware/ensure-api-token.ts +8 -6
  97. package/middleware/utils.ts +26 -8
  98. package/network/router.ts +17 -13
  99. package/network/service-discovery.ts +1 -1
  100. package/network/service-fetch.ts +26 -17
  101. package/node/generate-wrangler-config.ts +160 -24
  102. package/package.json +10 -7
@@ -24,7 +24,9 @@ import {
24
24
  import { JSONValue } from 'hono/utils/types';
25
25
  import { gzipSync } from 'zlib';
26
26
 
27
- interface WranglerConfig {
27
+ export type { AppConfig } from '../core/config.js';
28
+
29
+ export interface WranglerConfig {
28
30
  name: string;
29
31
  main: string;
30
32
  compatibility_date: string;
@@ -111,6 +113,66 @@ interface WranglerConfig {
111
113
  enabled?: boolean;
112
114
  };
113
115
  };
116
+ // observability?: {
117
+ // traces?: {
118
+ // enabled?: boolean;
119
+ // destinations?: string[];
120
+ // };
121
+ // logs?: {
122
+ // enabled?: boolean;
123
+ // destinations?: string[];
124
+ // };
125
+ // };
126
+ }
127
+
128
+ export interface WranglerConfigExtensionContext<
129
+ TConfig extends AppConfig = AppConfig,
130
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
131
+ > {
132
+ config: TConfig;
133
+ workerType: string;
134
+ workerName: string;
135
+ wranglerConfig: TWranglerConfig;
136
+ }
137
+
138
+ export interface WranglerConfigWriteContext<
139
+ TConfig extends AppConfig = AppConfig,
140
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
141
+ > extends WranglerConfigExtensionContext<TConfig, TWranglerConfig> {
142
+ configFile: string;
143
+ outputFile: string;
144
+ outputDir: string;
145
+ outputContent: string;
146
+ }
147
+
148
+ export interface WranglerConfigGeneratorExtension<
149
+ TConfig extends AppConfig = AppConfig,
150
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
151
+ > {
152
+ validateConfig?: (config: TConfig) => void;
153
+ extendWranglerConfig?: (
154
+ context: WranglerConfigExtensionContext<TConfig, TWranglerConfig>
155
+ ) => TWranglerConfig | void;
156
+ serializeOutput?: (wranglerConfig: TWranglerConfig) => string;
157
+ onBeforeWrite?: (context: WranglerConfigWriteContext<TConfig, TWranglerConfig>) => void;
158
+ onAfterWrite?: (context: WranglerConfigWriteContext<TConfig, TWranglerConfig>) => void;
159
+ }
160
+
161
+ export interface GenerateWranglerMainOptions<
162
+ TConfig extends AppConfig = AppConfig,
163
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
164
+ > {
165
+ args?: string[];
166
+ extension?: WranglerConfigGeneratorExtension<TConfig, TWranglerConfig>;
167
+ }
168
+
169
+ export function defineWranglerConfigExtension<
170
+ TConfig extends AppConfig = AppConfig,
171
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
172
+ >(
173
+ extension: WranglerConfigGeneratorExtension<TConfig, TWranglerConfig>
174
+ ): WranglerConfigGeneratorExtension<TConfig, TWranglerConfig> {
175
+ return extension;
114
176
  }
115
177
 
116
178
  function normalizePathValue(p?: string): string {
@@ -120,9 +182,17 @@ function normalizePathValue(p?: string): string {
120
182
  return v;
121
183
  }
122
184
 
123
- export function generateWranglerConfig(config: AppConfig, workerType: string, workerName: string): WranglerConfig {
185
+ export function generateWranglerConfig<
186
+ TConfig extends AppConfig = AppConfig,
187
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
188
+ >(
189
+ config: TConfig,
190
+ workerType: string,
191
+ workerName: string,
192
+ extension?: WranglerConfigGeneratorExtension<TConfig, TWranglerConfig>
193
+ ): TWranglerConfig {
124
194
  const configYaml = dump(config);
125
- const compressed = gzipSync(configYaml, { mtime: 0 } as any);
195
+ const compressed = gzipSync(configYaml);
126
196
  const configContent = compressed.toString('base64');
127
197
 
128
198
  let wranglerConfig: WranglerConfig;
@@ -142,8 +212,24 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
142
212
  PGEDGE_LOCATIONS: JSON.stringify(config.pgEdge?.locations || []),
143
213
  ...(config.deployment_context && { DEPLOYMENT_CONTEXT: config.deployment_context }),
144
214
  ...config.vars,
145
- ...routerConfig?.vars
215
+ ...routerConfig?.vars,
216
+ PGEDGE_FAILOVER_ENABLED: config.pgEdge?.connection_fallback ?? true
146
217
  },
218
+ // observability: {
219
+ // ...(routerObservability?.traces ? {
220
+ // traces: {
221
+ // enabled: routerObservability?.traces?.enabled || false,
222
+ // ...(Array.isArray(routerObservability?.traces?.destinations)
223
+ // ? { destinations: routerObservability.traces.destinations }
224
+ // : {}),
225
+ // },
226
+ // } : {}),
227
+ // logs: {
228
+ // enabled: routerObservability?.logs?.enabled || false,
229
+ // ...(Array.isArray(routerObservability?.logs?.destinations)
230
+ // ? { destinations: routerObservability.logs.destinations }
231
+ // : {}),
232
+ // }
147
233
  observability: {
148
234
  logs: {
149
235
  enabled: routerConfig?.observability?.logs?.enabled || false
@@ -151,9 +237,8 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
151
237
  }
152
238
  };
153
239
 
154
- const rcAny = routerConfig as any;
155
- const assetsDirectory: string | undefined = rcAny?.assets?.directory || 'public';
156
- let runWorkerFirstConfig: boolean | string[] | undefined = rcAny?.assets?.run_worker_first;
240
+ const assetsDirectory: string | undefined = routerConfig?.assets?.directory || 'public';
241
+ let runWorkerFirstConfig: boolean | string[] | undefined = routerConfig?.assets?.run_worker_first;
157
242
 
158
243
  const patterns = new Set<string>();
159
244
 
@@ -185,8 +270,8 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
185
270
  } else {
186
271
  runWorkerFirst = Array.from(patterns);
187
272
  }
188
- const notFoundHandling: 'single-page-application' | '404-page' | 'none' | undefined = rcAny?.assets?.not_found_handling || 'single-page-application';
189
- const htmlHandling: 'auto-trailing-slash' | 'force-trailing-slash' | 'drop-trailing-slash' | 'none' | undefined = rcAny?.assets?.html_handling;
273
+ const notFoundHandling: 'single-page-application' | '404-page' | 'none' | undefined = routerConfig?.assets?.not_found_handling || 'single-page-application';
274
+ const htmlHandling: 'auto-trailing-slash' | 'force-trailing-slash' | 'drop-trailing-slash' | 'none' | undefined = routerConfig?.assets?.html_handling;
190
275
  wranglerConfig.assets = {
191
276
  directory: assetsDirectory,
192
277
  binding: 'ASSETS',
@@ -252,8 +337,25 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
252
337
  PGEDGE_LOCATIONS: JSON.stringify(config.pgEdge?.locations || []),
253
338
  ...(config.deployment_context && { DEPLOYMENT_CONTEXT: config.deployment_context }),
254
339
  ...config.vars,
255
- ...workerConfig?.vars
340
+ ...workerConfig?.vars,
341
+ PGEDGE_FAILOVER_ENABLED: config.pgEdge?.connection_fallback ?? true
256
342
  },
343
+ // observability: {
344
+ // ...(workerObservability?.traces ? {
345
+ // traces: {
346
+ // enabled: workerObservability?.traces?.enabled || false,
347
+ // ...(Array.isArray(workerObservability?.traces?.destinations)
348
+ // ? { destinations: workerObservability.traces.destinations }
349
+ // : {}),
350
+ // },
351
+ // } : {}),
352
+ // logs: {
353
+ // enabled: workerObservability?.logs?.enabled || false,
354
+ // ...(Array.isArray(workerObservability?.logs?.destinations)
355
+ // ? { destinations: workerObservability.logs.destinations }
356
+ // : {}),
357
+ // }
358
+ // }
257
359
  observability: {
258
360
  logs: {
259
361
  enabled: workerConfig?.observability?.logs?.enabled || false
@@ -271,10 +373,9 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
271
373
  }
272
374
  }
273
375
 
274
- const workerConfigAny = workerConfig as any;
275
- if (workerConfigAny?.assets) {
276
- const assetsDirectory: string | undefined = workerConfigAny?.assets?.directory || 'public';
277
- let runWorkerFirstConfig: boolean | string[] | undefined = workerConfigAny?.assets?.run_worker_first;
376
+ if (workerConfig?.assets) {
377
+ const assetsDirectory: string | undefined = workerConfig.assets.directory || 'public';
378
+ let runWorkerFirstConfig: boolean | string[] | undefined = workerConfig.assets.run_worker_first;
278
379
 
279
380
  const patterns = new Set<string>();
280
381
  if (Array.isArray(runWorkerFirstConfig)) {
@@ -288,8 +389,8 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
288
389
  runWorkerFirst = Array.from(patterns);
289
390
  }
290
391
 
291
- const notFoundHandling: 'single-page-application' | '404-page' | 'none' | undefined = workerConfigAny?.assets?.not_found_handling || 'single-page-application';
292
- const htmlHandling: 'auto-trailing-slash' | 'force-trailing-slash' | 'drop-trailing-slash' | 'none' | undefined = workerConfigAny?.assets?.html_handling;
392
+ const notFoundHandling: 'single-page-application' | '404-page' | 'none' | undefined = workerConfig.assets.not_found_handling || 'single-page-application';
393
+ const htmlHandling: 'auto-trailing-slash' | 'force-trailing-slash' | 'drop-trailing-slash' | 'none' | undefined = workerConfig.assets.html_handling;
293
394
 
294
395
  wranglerConfig.assets = {
295
396
  directory: assetsDirectory,
@@ -304,7 +405,7 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
304
405
  try {
305
406
  const dbBinding = getDatabaseBinding(config, workerName);
306
407
  if (dbBinding.type === 'hyperdrive') {
307
- const flatHyperdriveConfigs = flattenHyperdriveConfigs(dbBinding.config as any);
408
+ const flatHyperdriveConfigs = flattenHyperdriveConfigs(dbBinding.config);
308
409
  wranglerConfig.hyperdrive = flatHyperdriveConfigs.map((hdConfig) => ({
309
410
  binding: hdConfig.binding,
310
411
  id: hdConfig.id,
@@ -381,7 +482,15 @@ export function generateWranglerConfig(config: AppConfig, workerType: string, wo
381
482
  }
382
483
  }
383
484
 
384
- return wranglerConfig;
485
+ const baseWranglerConfig = wranglerConfig as TWranglerConfig;
486
+ const extendedWranglerConfig = extension?.extendWranglerConfig?.({
487
+ config,
488
+ workerType,
489
+ workerName,
490
+ wranglerConfig: baseWranglerConfig,
491
+ });
492
+
493
+ return extendedWranglerConfig || baseWranglerConfig;
385
494
  }
386
495
 
387
496
  function copyDevVarsToWorker(workerDir: string, repoRoot: string) {
@@ -428,8 +537,13 @@ function storeHash(outputDir: string, hash: string): void {
428
537
  }
429
538
  }
430
539
 
431
- export function main() {
432
- const args = typeof process !== 'undefined' ? process.argv.slice(2) : [];
540
+ export function main<
541
+ TConfig extends AppConfig = AppConfig,
542
+ TWranglerConfig extends WranglerConfig = WranglerConfig,
543
+ >(
544
+ options: GenerateWranglerMainOptions<TConfig, TWranglerConfig> = {}
545
+ ) {
546
+ const args = options.args ?? (typeof process !== 'undefined' ? process.argv.slice(2) : []);
433
547
  const configFile = args[0] || process.env.CONFIG_PATH || 'config.yml';
434
548
  const outputFile = args[1] || 'wrangler.jsonc';
435
549
  const workerType = args[2] || 'worker';
@@ -446,7 +560,7 @@ export function main() {
446
560
 
447
561
  try {
448
562
  const yamlContent = fs.readFileSync(configFile, 'utf8');
449
- const config = load(yamlContent) as AppConfig;
563
+ const config = load(yamlContent) as TConfig;
450
564
 
451
565
  if (!config.cors || !config.server) {
452
566
  throw new Error('Config must include cors and server sections');
@@ -456,7 +570,14 @@ export function main() {
456
570
  throw new Error('Server config must include name and version');
457
571
  }
458
572
 
459
- const wranglerConfig = generateWranglerConfig(config, workerType, workerName);
573
+ options.extension?.validateConfig?.(config);
574
+
575
+ const wranglerConfig = generateWranglerConfig<TConfig, TWranglerConfig>(
576
+ config,
577
+ workerType,
578
+ workerName,
579
+ options.extension
580
+ );
460
581
 
461
582
  const outputDir = path.dirname(outputFile);
462
583
  if (!fs.existsSync(outputDir)) {
@@ -471,7 +592,8 @@ export function main() {
471
592
  } catch { }
472
593
  }
473
594
 
474
- const outputContent = JSON.stringify(wranglerConfig, null, 2);
595
+ const outputContent = options.extension?.serializeOutput?.(wranglerConfig)
596
+ ?? JSON.stringify(wranglerConfig, null, 2);
475
597
  const newHash = generateHash(outputContent);
476
598
 
477
599
  if (fs.existsSync(outputFile)) {
@@ -491,10 +613,24 @@ export function main() {
491
613
  console.log(` Worker type: ${workerType}`);
492
614
  console.log(` Worker name: ${workerName}`);
493
615
 
616
+ const writeContext: WranglerConfigWriteContext<TConfig, TWranglerConfig> = {
617
+ config,
618
+ workerType,
619
+ workerName,
620
+ wranglerConfig,
621
+ configFile,
622
+ outputFile,
623
+ outputDir,
624
+ outputContent,
625
+ };
626
+
627
+ options.extension?.onBeforeWrite?.(writeContext);
628
+
494
629
  fs.writeFileSync(outputFile, outputContent);
495
630
 
496
631
  storeHash(outputDir, newHash);
497
632
  copyDevVarsToWorker(outputDir, repoRoot);
633
+ options.extension?.onAfterWrite?.(writeContext);
498
634
 
499
635
  console.log(`✅ Generated ${outputFile} successfully`);
500
636
  console.log(` Worker name: ${wranglerConfig.name}`);
@@ -531,7 +667,7 @@ export function main() {
531
667
  const dbBinding = getDatabaseBinding(config, workerName);
532
668
  console.log(` Database: ${dbBinding.type} (worker-specific)`);
533
669
  if (dbBinding.type === 'hyperdrive') {
534
- const flatHyperdriveConfigs = flattenHyperdriveConfigs(dbBinding.config as any);
670
+ const flatHyperdriveConfigs = flattenHyperdriveConfigs(dbBinding.config);
535
671
  const bindingNames = flatHyperdriveConfigs.map((cfg) => cfg.binding);
536
672
  console.log(` Hyperdrive bindings: ${bindingNames.join(', ')}`);
537
673
  } else if (dbBinding.type === 'd1') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danylohotvianskyi/bobra-framework",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "description": "Bobra Framework - Generic Cloudflare Workers Framework",
5
5
  "author": "Danylo Hotvianskyi",
6
6
  "license": "Apache-2.0",
@@ -13,8 +13,8 @@
13
13
  "access": "public",
14
14
  "registry": "https://registry.npmjs.org/"
15
15
  },
16
- "main": "./index.ts",
17
- "types": "./index.ts",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
18
  "exports": {
19
19
  ".": {
20
20
  "types": "./dist/index.d.ts",
@@ -77,7 +77,9 @@
77
77
  "NOTICE"
78
78
  ],
79
79
  "dependencies": {
80
+ "@cloudflare/workers-types": "^4.20260307.1",
80
81
  "@hono/valibot-validator": "^0.5.2",
82
+ "@types/pg": "^8.11.2",
81
83
  "@valibot/to-json-schema": "^1.3.0",
82
84
  "dotenv": "^16.4.1",
83
85
  "drizzle-orm": "^0.44.3",
@@ -85,21 +87,22 @@
85
87
  "hono": "^4.11.9",
86
88
  "hono-openapi": "^0.4.8",
87
89
  "js-yaml": "^4.1.0",
90
+ "openapi-types": "^12.1.3",
88
91
  "pg": "^8.16.3",
89
92
  "postgres": "^3.2.0",
90
93
  "ts-node": "^10.9.2",
91
- "valibot": "^1.1.0",
92
- "@types/pg": "^8.11.2",
93
- "@cloudflare/workers-types": "^4.20260307.1"
94
+ "valibot": "^1.1.0"
94
95
  },
95
96
  "devDependencies": {
96
97
  "@types/js-yaml": "^4.0.9",
97
98
  "@types/node": "^20.19.4",
98
99
  "drizzle-kit": "^0.31.4",
99
- "typescript": "^5.6.0"
100
+ "typescript": "^5.6.0",
101
+ "vitest": "4.1.5"
100
102
  },
101
103
  "scripts": {
102
104
  "build": "tsc",
105
+ "test": "vitest run",
103
106
  "type-check": "tsc --noEmit"
104
107
  }
105
108
  }