@umituz/web-cloudflare 1.0.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +621 -0
  3. package/package.json +87 -0
  4. package/src/config/patterns.ts +469 -0
  5. package/src/config/types.ts +648 -0
  6. package/src/domain/entities/analytics.entity.ts +47 -0
  7. package/src/domain/entities/d1.entity.ts +37 -0
  8. package/src/domain/entities/image.entity.ts +48 -0
  9. package/src/domain/entities/index.ts +11 -0
  10. package/src/domain/entities/kv.entity.ts +34 -0
  11. package/src/domain/entities/r2.entity.ts +55 -0
  12. package/src/domain/entities/worker.entity.ts +35 -0
  13. package/src/domain/index.ts +7 -0
  14. package/src/domain/interfaces/index.ts +6 -0
  15. package/src/domain/interfaces/services.interface.ts +82 -0
  16. package/src/index.ts +53 -0
  17. package/src/infrastructure/constants/index.ts +13 -0
  18. package/src/infrastructure/domain/ai-gateway.entity.ts +169 -0
  19. package/src/infrastructure/domain/workflows.entity.ts +108 -0
  20. package/src/infrastructure/middleware/index.ts +405 -0
  21. package/src/infrastructure/router/index.ts +549 -0
  22. package/src/infrastructure/services/ai-gateway/index.ts +416 -0
  23. package/src/infrastructure/services/analytics/analytics.service.ts +189 -0
  24. package/src/infrastructure/services/analytics/index.ts +7 -0
  25. package/src/infrastructure/services/d1/d1.service.ts +191 -0
  26. package/src/infrastructure/services/d1/index.ts +7 -0
  27. package/src/infrastructure/services/images/images.service.ts +227 -0
  28. package/src/infrastructure/services/images/index.ts +7 -0
  29. package/src/infrastructure/services/kv/index.ts +7 -0
  30. package/src/infrastructure/services/kv/kv.service.ts +116 -0
  31. package/src/infrastructure/services/r2/index.ts +7 -0
  32. package/src/infrastructure/services/r2/r2.service.ts +164 -0
  33. package/src/infrastructure/services/workers/index.ts +7 -0
  34. package/src/infrastructure/services/workers/workers.service.ts +164 -0
  35. package/src/infrastructure/services/workflows/index.ts +437 -0
  36. package/src/infrastructure/utils/helpers.ts +732 -0
  37. package/src/infrastructure/utils/index.ts +6 -0
  38. package/src/infrastructure/utils/utils.util.ts +150 -0
  39. package/src/presentation/hooks/cloudflare.hooks.ts +314 -0
  40. package/src/presentation/hooks/index.ts +6 -0
  41. package/src/worker.example.ts +41 -0
@@ -0,0 +1,648 @@
1
+ /**
2
+ * Cloudflare Worker Configuration Types
3
+ * @description Comprehensive configuration types for Cloudflare Workers
4
+ */
5
+
6
+ // ============================================================
7
+ // Main Configuration
8
+ // ============================================================
9
+
10
+ export interface WorkerConfig {
11
+ /**
12
+ * Cache configuration
13
+ */
14
+ cache?: CacheConfig;
15
+
16
+ /**
17
+ * Rate limiting configuration
18
+ */
19
+ rateLimit?: RateLimitConfig;
20
+
21
+ /**
22
+ * CORS configuration
23
+ */
24
+ cors?: CORSConfig;
25
+
26
+ /**
27
+ * AI configuration
28
+ */
29
+ ai?: AIConfig;
30
+
31
+ /**
32
+ * Workflows configuration
33
+ */
34
+ workflows?: WorkflowConfig;
35
+
36
+ /**
37
+ * Analytics configuration
38
+ */
39
+ analytics?: AnalyticsConfig;
40
+
41
+ /**
42
+ * Compression configuration
43
+ */
44
+ compression?: CompressionConfig;
45
+
46
+ /**
47
+ * Image optimization configuration
48
+ */
49
+ imageOptimization?: ImageOptimizationConfig;
50
+
51
+ /**
52
+ * Queues configuration
53
+ */
54
+ queues?: QueueConfig;
55
+
56
+ /**
57
+ * Scheduled tasks configuration
58
+ */
59
+ scheduledTasks?: ScheduledTaskConfig;
60
+
61
+ /**
62
+ * Static file serving configuration
63
+ */
64
+ staticFiles?: StaticFileConfig;
65
+
66
+ /**
67
+ * Proxy configuration
68
+ */
69
+ proxy?: ProxyConfig;
70
+
71
+ /**
72
+ * Security configuration
73
+ */
74
+ security?: SecurityConfig;
75
+
76
+ /**
77
+ * Monitoring configuration
78
+ */
79
+ monitoring?: MonitoringConfig;
80
+
81
+ /**
82
+ * Feature flags
83
+ */
84
+ features?: FeatureFlags;
85
+ }
86
+
87
+ // ============================================================
88
+ // Cache Configuration
89
+ // ============================================================
90
+
91
+ export interface CacheConfig {
92
+ /**
93
+ * Enable caching
94
+ */
95
+ enabled: boolean;
96
+
97
+ /**
98
+ * Default TTL in seconds
99
+ */
100
+ defaultTTL: number;
101
+
102
+ /**
103
+ * Path-specific TTLs
104
+ */
105
+ paths?: Record<string, number>;
106
+
107
+ /**
108
+ * Cache key prefix
109
+ */
110
+ prefix?: string;
111
+
112
+ /**
113
+ * Bypass cache for these paths
114
+ */
115
+ bypassPaths?: string[];
116
+
117
+ /**
118
+ * Cache headers to respect
119
+ */
120
+ respectHeaders?: boolean;
121
+ }
122
+
123
+ // ============================================================
124
+ // Rate Limiting Configuration
125
+ // ============================================================
126
+
127
+ export interface RateLimitConfig {
128
+ /**
129
+ * Enable rate limiting
130
+ */
131
+ enabled: boolean;
132
+
133
+ /**
134
+ * Max requests per window
135
+ */
136
+ maxRequests: number;
137
+
138
+ /**
139
+ * Time window in seconds
140
+ */
141
+ window: number;
142
+
143
+ /**
144
+ * Rate limit by IP, user, or both
145
+ */
146
+ by?: 'ip' | 'user' | 'both';
147
+
148
+ /**
149
+ * Custom rate limit keys
150
+ */
151
+ customKeys?: string[];
152
+
153
+ /**
154
+ * Whitelisted IPs/IDs
155
+ */
156
+ whitelist?: string[];
157
+
158
+ /**
159
+ * Response when rate limited
160
+ */
161
+ response?: {
162
+ status: number;
163
+ message: string;
164
+ retryAfter?: number;
165
+ };
166
+ }
167
+
168
+ // ============================================================
169
+ // CORS Configuration
170
+ // ============================================================
171
+
172
+ export interface CORSConfig {
173
+ /**
174
+ * Enable CORS
175
+ */
176
+ enabled: boolean;
177
+
178
+ /**
179
+ * Allowed origins
180
+ */
181
+ allowedOrigins: string[];
182
+
183
+ /**
184
+ * Allowed methods
185
+ */
186
+ allowedMethods: string[];
187
+
188
+ /**
189
+ * Allowed headers
190
+ */
191
+ allowedHeaders: string[];
192
+
193
+ /**
194
+ * Exposed headers
195
+ */
196
+ exposedHeaders?: string[];
197
+
198
+ /**
199
+ * Allow credentials
200
+ */
201
+ allowCredentials?: boolean;
202
+
203
+ /**
204
+ * Max age for preflight
205
+ */
206
+ maxAge?: number;
207
+ }
208
+
209
+ // ============================================================
210
+ // AI Configuration
211
+ // ============================================================
212
+
213
+ export interface AIConfig {
214
+ /**
215
+ * Enable AI features
216
+ */
217
+ enabled: boolean;
218
+
219
+ /**
220
+ * AI Gateway configuration
221
+ */
222
+ gateway?: {
223
+ gatewayId?: string;
224
+ providers: Array<{
225
+ id: string;
226
+ name: string;
227
+ type: 'workers-ai' | 'openai' | 'anthropic' | 'cohere' | 'custom';
228
+ baseURL: string;
229
+ apiKey: string;
230
+ models: string[];
231
+ fallbackProvider?: string;
232
+ weight?: number;
233
+ }>;
234
+ cacheEnabled?: boolean;
235
+ cacheTTL?: number;
236
+ rateLimiting?: boolean;
237
+ analytics?: boolean;
238
+ };
239
+
240
+ /**
241
+ * Default model for text generation
242
+ */
243
+ defaultModel?: string;
244
+
245
+ /**
246
+ * Default parameters
247
+ */
248
+ defaultParams?: {
249
+ temperature?: number;
250
+ maxTokens?: number;
251
+ topP?: number;
252
+ };
253
+ }
254
+
255
+ // ============================================================
256
+ // Workflow Configuration
257
+ // ============================================================
258
+
259
+ export interface WorkflowConfig {
260
+ /**
261
+ * Enable workflows
262
+ */
263
+ enabled: boolean;
264
+
265
+ /**
266
+ * Max execution time in seconds
267
+ */
268
+ maxExecutionTime: number;
269
+
270
+ /**
271
+ * Default retry count
272
+ */
273
+ defaultRetries: number;
274
+
275
+ /**
276
+ * Workflow definitions
277
+ */
278
+ workflows?: Record<string, {
279
+ id: string;
280
+ name: string;
281
+ description?: string;
282
+ steps: Array<{
283
+ id: string;
284
+ name: string;
285
+ handler: string;
286
+ timeout?: number;
287
+ retryPolicy?: {
288
+ maxAttempts: number;
289
+ backoffMultiplier: number;
290
+ initialDelay: number;
291
+ maxDelay: number;
292
+ };
293
+ dependencies?: string[];
294
+ }>;
295
+ }>;
296
+
297
+ /**
298
+ * Storage backend for workflow state
299
+ */
300
+ storage?: 'kv' | 'd1';
301
+ }
302
+
303
+ // ============================================================
304
+ // Analytics Configuration
305
+ // ============================================================
306
+
307
+ export interface AnalyticsConfig {
308
+ /**
309
+ * Enable analytics
310
+ */
311
+ enabled: boolean;
312
+
313
+ /**
314
+ * Analytics provider
315
+ */
316
+ provider?: 'cloudflare' | 'google' | 'mixpanel' | 'custom';
317
+
318
+ /**
319
+ * Sample rate (0-1)
320
+ */
321
+ sampleRate?: number;
322
+
323
+ /**
324
+ * Events to track
325
+ */
326
+ events?: string[];
327
+
328
+ /**
329
+ * Custom analytics endpoint
330
+ */
331
+ endpoint?: string;
332
+ }
333
+
334
+ // ============================================================
335
+ // Compression Configuration
336
+ // ============================================================
337
+
338
+ export interface CompressionConfig {
339
+ /**
340
+ * Enable compression
341
+ */
342
+ enabled: boolean;
343
+
344
+ /**
345
+ * Content types to compress
346
+ */
347
+ types?: string[];
348
+
349
+ /**
350
+ * Minimum size to compress (bytes)
351
+ */
352
+ minSize?: number;
353
+
354
+ /**
355
+ * Compression level (0-9)
356
+ */
357
+ level?: number;
358
+ }
359
+
360
+ // ============================================================
361
+ // Image Optimization Configuration
362
+ // ============================================================
363
+
364
+ export interface ImageOptimizationConfig {
365
+ /**
366
+ * Enable image optimization
367
+ */
368
+ enabled: boolean;
369
+
370
+ /**
371
+ * Supported formats
372
+ */
373
+ formats?: Array<'webp' | 'avif' | 'jpeg' | 'png'>;
374
+
375
+ /**
376
+ * Default quality (1-100)
377
+ */
378
+ quality?: number;
379
+
380
+ /**
381
+ * Default resize options
382
+ */
383
+ resize?: {
384
+ maxWidth?: number;
385
+ maxHeight?: number;
386
+ fit?: 'contain' | 'cover' | 'fill';
387
+ };
388
+
389
+ /**
390
+ * Strip metadata
391
+ */
392
+ stripMetadata?: boolean;
393
+ }
394
+
395
+ // ============================================================
396
+ // Queue Configuration
397
+ // ============================================================
398
+
399
+ export interface QueueConfig {
400
+ /**
401
+ * Enable queues
402
+ */
403
+ enabled: boolean;
404
+
405
+ /**
406
+ * Queue definitions
407
+ */
408
+ queues?: Record<string, {
409
+ name: string;
410
+ batchSize?: number;
411
+ maxRetries?: number;
412
+ maxWaitTime?: number;
413
+ }>;
414
+ }
415
+
416
+ // ============================================================
417
+ // Scheduled Task Configuration
418
+ // ============================================================
419
+
420
+ export interface ScheduledTaskConfig {
421
+ /**
422
+ * Enable scheduled tasks
423
+ */
424
+ enabled: boolean;
425
+
426
+ /**
427
+ * Cron schedules
428
+ */
429
+ schedules?: Record<string, {
430
+ cron: string;
431
+ handler: string;
432
+ }>;
433
+ }
434
+
435
+ // ============================================================
436
+ // Static File Configuration
437
+ // ============================================================
438
+
439
+ export interface StaticFileConfig {
440
+ /**
441
+ * Enable static file serving
442
+ */
443
+ enabled: boolean;
444
+
445
+ /**
446
+ * Path patterns for static files
447
+ */
448
+ paths?: string[];
449
+
450
+ /**
451
+ * Cache control header
452
+ */
453
+ cacheControl?: string;
454
+
455
+ /**
456
+ * Serve index.html for directory requests
457
+ */
458
+ serveIndex?: boolean;
459
+
460
+ /**
461
+ * Custom headers
462
+ */
463
+ headers?: Record<string, string>;
464
+ }
465
+
466
+ // ============================================================
467
+ // Proxy Configuration
468
+ // ============================================================
469
+
470
+ export interface ProxyConfig {
471
+ /**
472
+ * Enable proxying
473
+ */
474
+ enabled: boolean;
475
+
476
+ /**
477
+ * Upstream URL
478
+ */
479
+ upstream: string;
480
+
481
+ /**
482
+ * Paths to proxy
483
+ */
484
+ paths?: string[];
485
+
486
+ /**
487
+ * Strip request headers
488
+ */
489
+ stripHeaders?: string[];
490
+
491
+ /**
492
+ * Add request headers
493
+ */
494
+ addHeaders?: Record<string, string>;
495
+
496
+ /**
497
+ * Timeout in seconds
498
+ */
499
+ timeout?: number;
500
+
501
+ /**
502
+ * Retry attempts
503
+ */
504
+ retries?: number;
505
+ }
506
+
507
+ // ============================================================
508
+ // Security Configuration
509
+ // ============================================================
510
+
511
+ export interface SecurityConfig {
512
+ /**
513
+ * Enable security headers
514
+ */
515
+ enabled: boolean;
516
+
517
+ /**
518
+ * Security headers
519
+ */
520
+ headers?: {
521
+ XFrameOptions?: string;
522
+ XContentTypeOptions?: string;
523
+ XSSProtection?: string;
524
+ ContentSecurityPolicy?: string;
525
+ StrictTransportSecurity?: string;
526
+ ReferrerPolicy?: string;
527
+ PermissionsPolicy?: string;
528
+ };
529
+
530
+ /**
531
+ * Bot detection
532
+ */
533
+ botDetection?: boolean;
534
+
535
+ /**
536
+ * IP whitelist/blacklist
537
+ */
538
+ ipFilter?: {
539
+ mode: 'whitelist' | 'blacklist';
540
+ ips: string[];
541
+ };
542
+ }
543
+
544
+ // ============================================================
545
+ // Monitoring Configuration
546
+ // ============================================================
547
+
548
+ export interface MonitoringConfig {
549
+ /**
550
+ * Enable monitoring
551
+ */
552
+ enabled: boolean;
553
+
554
+ /**
555
+ * Log level
556
+ */
557
+ logLevel?: 'debug' | 'info' | 'warn' | 'error';
558
+
559
+ /**
560
+ * Trace sampling (0-1)
561
+ */
562
+ traceSampleRate?: number;
563
+
564
+ /**
565
+ * Error tracking endpoint
566
+ */
567
+ errorEndpoint?: string;
568
+
569
+ /**
570
+ * Performance monitoring
571
+ */
572
+ performance?: boolean;
573
+
574
+ /**
575
+ * Alert thresholds
576
+ */
577
+ alerts?: {
578
+ errorRate?: number;
579
+ latency?: number;
580
+ };
581
+ }
582
+
583
+ // ============================================================
584
+ // Feature Flags
585
+ // ============================================================
586
+
587
+ export interface FeatureFlags {
588
+ /**
589
+ * Enable new features
590
+ */
591
+ [key: string]: boolean | string | number | undefined;
592
+ }
593
+
594
+ // ============================================================
595
+ // Environment Configuration
596
+ // ============================================================
597
+
598
+ export interface EnvConfig {
599
+ /**
600
+ * Environment name
601
+ */
602
+ environment: 'development' | 'staging' | 'production';
603
+
604
+ /**
605
+ * KV namespace bindings
606
+ */
607
+ KV?: KVNamespace;
608
+
609
+ /**
610
+ * R2 bucket bindings
611
+ */
612
+ R2?: R2Bucket;
613
+
614
+ /**
615
+ * D1 database bindings
616
+ */
617
+ D1?: D1Database;
618
+
619
+ /**
620
+ * Durable Object bindings
621
+ */
622
+ DO?: Record<string, DurableObjectNamespace>;
623
+
624
+ /**
625
+ * Queue bindings
626
+ */
627
+ QUEUE?: Record<string, Queue>;
628
+
629
+ /**
630
+ * AI bindings
631
+ */
632
+ AI?: any;
633
+
634
+ /**
635
+ * Custom environment variables
636
+ */
637
+ vars?: Record<string, string>;
638
+ }
639
+
640
+ // ============================================================
641
+ // Configuration Merging Types
642
+ // ============================================================
643
+
644
+ export type DeepPartial<T> = {
645
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
646
+ };
647
+
648
+ export type ConfigOverride = DeepPartial<WorkerConfig>;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Analytics Entity
3
+ * @description Cloudflare Web Analytics configuration and types
4
+ */
5
+
6
+ export interface AnalyticsConfig {
7
+ readonly siteId: string;
8
+ readonly scriptUrl?: string;
9
+ }
10
+
11
+ export interface AnalyticsEvent {
12
+ readonly timestamp: number;
13
+ readonly url: string;
14
+ readonly eventType: "pageview" | "custom" | "outbound-link" | "timing";
15
+ readonly eventData?: Record<string, unknown>;
16
+ }
17
+
18
+ export interface AnalyticsPageviewEvent extends AnalyticsEvent {
19
+ readonly eventType: "pageview";
20
+ readonly title: string;
21
+ readonly referrer?: string;
22
+ }
23
+
24
+ export interface AnalyticsCustomEvent extends AnalyticsEvent {
25
+ readonly eventType: "custom";
26
+ readonly eventName: string;
27
+ }
28
+
29
+ export interface AnalyticsTimingEvent extends AnalyticsEvent {
30
+ readonly eventType: "timing";
31
+ readonly name: string;
32
+ readonly value: number;
33
+ readonly label?: string;
34
+ }
35
+
36
+ export interface AnalyticsData {
37
+ readonly siteId: string;
38
+ readonly events: readonly AnalyticsEvent[];
39
+ readonly metrics?: AnalyticsMetrics;
40
+ }
41
+
42
+ export interface AnalyticsMetrics {
43
+ readonly pageviews: number;
44
+ readonly uniqueVisitors: number;
45
+ readonly bounceRate?: number;
46
+ readonly avgSessionDuration?: number;
47
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * D1 Entity
3
+ * @description Cloudflare D1 database configuration and types
4
+ */
5
+
6
+ export interface D1DatabaseConfig {
7
+ readonly database: string;
8
+ }
9
+
10
+ export interface D1QueryResult<T = unknown> {
11
+ readonly results: readonly T[];
12
+ readonly success: boolean;
13
+ readonly meta?: D1QueryMeta;
14
+ }
15
+
16
+ export interface D1QueryMeta {
17
+ readonly duration: number;
18
+ readonly rows_read: number;
19
+ readonly rows_written: number;
20
+ readonly last_row_id?: number;
21
+ readonly changes: number;
22
+ }
23
+
24
+ export interface D1PreparedStatement {
25
+ readonly statement: string;
26
+ readonly params: readonly unknown[];
27
+ }
28
+
29
+ export interface D1BatchResult {
30
+ readonly success: boolean;
31
+ readonly results: readonly D1QueryResult[];
32
+ }
33
+
34
+ export interface D1TransactionOptions {
35
+ readonly rollbackOnError?: boolean;
36
+ readonly timeout?: number;
37
+ }