@umituz/web-cloudflare 1.4.5 → 1.4.6
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/package.json +2 -2
- package/src/domains/middleware/entities/index.ts +106 -0
- package/src/domains/middleware/index.ts +14 -0
- package/src/{infrastructure/middleware/auth.ts → domains/middleware/services/auth.service.ts} +2 -10
- package/src/{infrastructure/middleware/cache.ts → domains/middleware/services/cache.service.ts} +7 -9
- package/src/{infrastructure/middleware/cors.ts → domains/middleware/services/cors.service.ts} +2 -10
- package/src/domains/middleware/services/index.ts +9 -0
- package/src/{infrastructure/middleware/rate-limit.ts → domains/middleware/services/rate-limit.service.ts} +2 -14
- package/src/domains/middleware/types/index.ts +5 -0
- package/src/domains/middleware/types/service.interface.ts +122 -0
- package/src/index.ts +2 -2
- package/src/infrastructure/middleware/index.ts +5 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-cloudflare",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.6",
|
|
4
4
|
"description": "Comprehensive Cloudflare Workers integration with config-based patterns, middleware, router, workflows, and AI (Patch-only versioning: only z in x.y.z increments)",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"./ai-gateway": "./src/domains/ai-gateway/index.ts",
|
|
18
18
|
"./workers-ai": "./src/domains/ai-gateway/index.ts",
|
|
19
19
|
"./wrangler": "./src/domains/wrangler/index.ts",
|
|
20
|
+
"./middleware": "./src/domains/middleware/index.ts",
|
|
20
21
|
"./router": "./src/infrastructure/router/index.ts",
|
|
21
|
-
"./middleware": "./src/infrastructure/middleware/index.ts",
|
|
22
22
|
"./utils": "./src/infrastructure/utils/helpers.ts",
|
|
23
23
|
"./helpers": "./src/infrastructure/utils/helpers.ts",
|
|
24
24
|
"./config": "./src/config/patterns.ts",
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware Domain Entities
|
|
3
|
+
* @description Middleware configuration and types for Cloudflare Workers
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* CORS configuration
|
|
8
|
+
*/
|
|
9
|
+
export interface CORSConfig {
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
allowedOrigins: string[];
|
|
12
|
+
allowedMethods: string[];
|
|
13
|
+
allowedHeaders: string[];
|
|
14
|
+
exposedHeaders?: string[];
|
|
15
|
+
allowCredentials?: boolean;
|
|
16
|
+
maxAge?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Cache configuration
|
|
21
|
+
*/
|
|
22
|
+
export interface CacheConfig {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
defaultTTL: number;
|
|
25
|
+
paths?: Record<string, number>;
|
|
26
|
+
prefix?: string;
|
|
27
|
+
bypassPaths?: string[];
|
|
28
|
+
respectHeaders?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Rate limit configuration
|
|
33
|
+
*/
|
|
34
|
+
export interface RateLimitConfig {
|
|
35
|
+
enabled: boolean;
|
|
36
|
+
maxRequests: number;
|
|
37
|
+
window: number;
|
|
38
|
+
by?: 'ip' | 'user' | 'both';
|
|
39
|
+
customKeys?: string[];
|
|
40
|
+
whitelist?: string[];
|
|
41
|
+
response?: {
|
|
42
|
+
status: number;
|
|
43
|
+
message: string;
|
|
44
|
+
retryAfter?: number;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Authentication configuration
|
|
50
|
+
*/
|
|
51
|
+
export interface AuthConfig {
|
|
52
|
+
enabled: boolean;
|
|
53
|
+
type: 'bearer' | 'apikey' | 'basic';
|
|
54
|
+
token?: string;
|
|
55
|
+
apiKeyHeader?: string;
|
|
56
|
+
apiKeyValue?: string;
|
|
57
|
+
username?: string;
|
|
58
|
+
password?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Security headers configuration
|
|
63
|
+
*/
|
|
64
|
+
export interface SecurityHeadersConfig {
|
|
65
|
+
frameGuard?: boolean;
|
|
66
|
+
contentTypeNosniff?: boolean;
|
|
67
|
+
xssProtection?: boolean;
|
|
68
|
+
strictTransportSecurity?: boolean;
|
|
69
|
+
referrerPolicy?: string;
|
|
70
|
+
contentSecurityPolicy?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* IP filter configuration
|
|
75
|
+
*/
|
|
76
|
+
export interface IPFilterConfig {
|
|
77
|
+
mode: 'whitelist' | 'blacklist';
|
|
78
|
+
ips: string[];
|
|
79
|
+
cidrs?: string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Log configuration
|
|
84
|
+
*/
|
|
85
|
+
export interface LogConfig {
|
|
86
|
+
level: 'debug' | 'info' | 'warn' | 'error';
|
|
87
|
+
includeHeaders?: boolean;
|
|
88
|
+
includeBody?: boolean;
|
|
89
|
+
sampleRate?: number;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Health check configuration
|
|
94
|
+
*/
|
|
95
|
+
export interface HealthCheckConfig {
|
|
96
|
+
uptime: number;
|
|
97
|
+
checks: Record<string, () => Promise<boolean>>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Error handler configuration
|
|
102
|
+
*/
|
|
103
|
+
export interface ErrorHandlerConfig {
|
|
104
|
+
debug: boolean;
|
|
105
|
+
logger?: (error: Error) => void;
|
|
106
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware Domain
|
|
3
|
+
* @description Complete middleware integration for Cloudflare Workers
|
|
4
|
+
* Subpath: @umituz/web-cloudflare/middleware
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// Entities
|
|
8
|
+
export * from './entities';
|
|
9
|
+
|
|
10
|
+
// Types
|
|
11
|
+
export * from './types';
|
|
12
|
+
|
|
13
|
+
// Services
|
|
14
|
+
export * from './services';
|
package/src/{infrastructure/middleware/auth.ts → domains/middleware/services/auth.service.ts}
RENAMED
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Auth
|
|
2
|
+
* Auth Service
|
|
3
3
|
* @description Authentication middleware for Cloudflare Workers
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
enabled: boolean;
|
|
8
|
-
type: 'bearer' | 'apikey' | 'basic';
|
|
9
|
-
token?: string;
|
|
10
|
-
apiKeyHeader?: string;
|
|
11
|
-
apiKeyValue?: string;
|
|
12
|
-
username?: string;
|
|
13
|
-
password?: string;
|
|
14
|
-
}
|
|
6
|
+
import type { AuthConfig } from '../entities';
|
|
15
7
|
|
|
16
8
|
/**
|
|
17
9
|
* Require authentication
|
package/src/{infrastructure/middleware/cache.ts → domains/middleware/services/cache.service.ts}
RENAMED
|
@@ -1,18 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Cache
|
|
2
|
+
* Cache Service
|
|
3
3
|
* @description Caching middleware for Cloudflare Workers
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
bypassPaths?: string[];
|
|
12
|
-
respectHeaders?: boolean;
|
|
6
|
+
import type { CacheConfig } from '../entities';
|
|
7
|
+
|
|
8
|
+
interface CacheEntry {
|
|
9
|
+
response: Response;
|
|
10
|
+
expires: number;
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
const cacheStore = new Map<string,
|
|
13
|
+
const cacheStore = new Map<string, CacheEntry>();
|
|
16
14
|
|
|
17
15
|
/**
|
|
18
16
|
* Cache middleware
|
package/src/{infrastructure/middleware/cors.ts → domains/middleware/services/cors.service.ts}
RENAMED
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* CORS
|
|
2
|
+
* CORS Service
|
|
3
3
|
* @description Cross-Origin Resource Sharing middleware for Cloudflare Workers
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
enabled: boolean;
|
|
8
|
-
allowedOrigins: string[];
|
|
9
|
-
allowedMethods: string[];
|
|
10
|
-
allowedHeaders: string[];
|
|
11
|
-
exposedHeaders?: string[];
|
|
12
|
-
allowCredentials?: boolean;
|
|
13
|
-
maxAge?: number;
|
|
14
|
-
}
|
|
6
|
+
import type { CORSConfig } from '../entities';
|
|
15
7
|
|
|
16
8
|
/**
|
|
17
9
|
* Add CORS headers to response
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware Services
|
|
3
|
+
* @description Middleware service implementations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export { cors, addCorsHeaders } from './cors.service';
|
|
7
|
+
export { cache, setCache, invalidateCache } from './cache.service';
|
|
8
|
+
export { checkRateLimit } from './rate-limit.service';
|
|
9
|
+
export { requireAuth, addUserContext } from './auth.service';
|
|
@@ -1,21 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Rate Limit
|
|
2
|
+
* Rate Limit Service
|
|
3
3
|
* @description Rate limiting middleware for Cloudflare Workers
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
enabled: boolean;
|
|
8
|
-
maxRequests: number;
|
|
9
|
-
window: number;
|
|
10
|
-
by?: 'ip' | 'user' | 'both';
|
|
11
|
-
customKeys?: string[];
|
|
12
|
-
whitelist?: string[];
|
|
13
|
-
response?: {
|
|
14
|
-
status: number;
|
|
15
|
-
message: string;
|
|
16
|
-
retryAfter?: number;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
6
|
+
import type { RateLimitConfig } from '../entities';
|
|
19
7
|
|
|
20
8
|
interface RateLimitEntry {
|
|
21
9
|
count: number;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware Service Interface
|
|
3
|
+
* @description Defines the contract for middleware operations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
CORSConfig,
|
|
8
|
+
CacheConfig,
|
|
9
|
+
RateLimitConfig,
|
|
10
|
+
AuthConfig,
|
|
11
|
+
SecurityHeadersConfig,
|
|
12
|
+
IPFilterConfig,
|
|
13
|
+
LogConfig,
|
|
14
|
+
HealthCheckConfig,
|
|
15
|
+
ErrorHandlerConfig,
|
|
16
|
+
} from '../entities';
|
|
17
|
+
|
|
18
|
+
export interface IMiddlewareService {
|
|
19
|
+
/**
|
|
20
|
+
* CORS middleware
|
|
21
|
+
*/
|
|
22
|
+
cors(request: Request, config: CORSConfig): Promise<Response | null>;
|
|
23
|
+
addCorsHeaders(request: Request, response: Response, config: CORSConfig): Response;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Cache middleware
|
|
27
|
+
*/
|
|
28
|
+
cache(request: Request, config: CacheConfig): Promise<Response | null>;
|
|
29
|
+
setCache(request: Request, response: Response, config: CacheConfig): void;
|
|
30
|
+
invalidateCache(pattern?: string): void;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Rate limit middleware
|
|
34
|
+
*/
|
|
35
|
+
checkRateLimit(request: Request, config: RateLimitConfig): Promise<Response | null>;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Authentication middleware
|
|
39
|
+
*/
|
|
40
|
+
requireAuth(request: Request, config: AuthConfig): Promise<Response | null>;
|
|
41
|
+
addUserContext(request: Request, user: {
|
|
42
|
+
id: string;
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}): Request;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Security headers
|
|
48
|
+
*/
|
|
49
|
+
addSecurityHeaders(response: Response, config: SecurityHeadersConfig): Response;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Bot detection
|
|
53
|
+
*/
|
|
54
|
+
detectBot(request: Request): Promise<{
|
|
55
|
+
isBot: boolean;
|
|
56
|
+
botType?: string;
|
|
57
|
+
}>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Request logging
|
|
61
|
+
*/
|
|
62
|
+
logRequest(request: Request, config: LogConfig): Promise<void>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Response time tracking
|
|
66
|
+
*/
|
|
67
|
+
trackResponseTime(handler: () => Promise<Response>): Promise<{
|
|
68
|
+
response: Response;
|
|
69
|
+
duration: number;
|
|
70
|
+
}>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* IP filter
|
|
74
|
+
*/
|
|
75
|
+
checkIPFilter(request: Request, config: IPFilterConfig): Response | null;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Method override
|
|
79
|
+
*/
|
|
80
|
+
methodOverride(request: Request): Request;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Request ID
|
|
84
|
+
*/
|
|
85
|
+
addRequestID(request: Request): string;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Health check
|
|
89
|
+
*/
|
|
90
|
+
healthCheck(
|
|
91
|
+
env: import('../../router').CloudflareEnv,
|
|
92
|
+
config?: HealthCheckConfig
|
|
93
|
+
): Promise<Response>;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Error handling
|
|
97
|
+
*/
|
|
98
|
+
handleMiddlewareError(error: Error, config: ErrorHandlerConfig): Response;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Conditional middleware
|
|
102
|
+
*/
|
|
103
|
+
conditionalChainMiddleware(
|
|
104
|
+
condition: (request: Request) => boolean,
|
|
105
|
+
middleware: (request: Request) => Response | null
|
|
106
|
+
): (request: Request) => Response | null;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Chain middleware
|
|
110
|
+
*/
|
|
111
|
+
chainMiddleware(
|
|
112
|
+
...middlewares: Array<(request: Request) => Response | null>
|
|
113
|
+
): (request: Request) => Response | null;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Chain async middleware
|
|
117
|
+
*/
|
|
118
|
+
chainAsyncMiddleware(
|
|
119
|
+
request: Request,
|
|
120
|
+
...middlewares: Array<(request: Request) => Promise<Response | null>>
|
|
121
|
+
): Promise<Response | null>;
|
|
122
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -35,10 +35,10 @@ export * from "./domains/kv";
|
|
|
35
35
|
export * from "./domains/images";
|
|
36
36
|
export * from "./domains/analytics";
|
|
37
37
|
export * from "./domains/workflows";
|
|
38
|
+
export * from "./domains/middleware";
|
|
38
39
|
|
|
39
|
-
// Infrastructure - Router,
|
|
40
|
+
// Infrastructure - Router, Utils
|
|
40
41
|
export * from "./infrastructure/router";
|
|
41
|
-
export * from "./infrastructure/middleware";
|
|
42
42
|
export * from "./infrastructure/utils/helpers";
|
|
43
43
|
export * from "./infrastructure/constants";
|
|
44
44
|
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cloudflare Middleware Collection
|
|
3
3
|
* @description Comprehensive middleware for Cloudflare Workers
|
|
4
|
+
* @deprecated Import from '@umituz/web-cloudflare/middleware' instead
|
|
4
5
|
*/
|
|
5
6
|
|
|
7
|
+
// Re-export from middleware domain
|
|
8
|
+
export * from '../../domains/middleware';
|
|
9
|
+
|
|
6
10
|
// ============================================================
|
|
7
|
-
// Environment Types
|
|
11
|
+
// Environment Types (kept for backwards compatibility)
|
|
8
12
|
// ============================================================
|
|
9
13
|
|
|
10
14
|
export interface CloudflareMiddlewareEnv {
|
|
@@ -20,12 +24,6 @@ export interface CloudflareMiddlewareEnv {
|
|
|
20
24
|
// Type alias for backwards compatibility
|
|
21
25
|
export type Env = CloudflareMiddlewareEnv;
|
|
22
26
|
|
|
23
|
-
// Re-export existing middleware
|
|
24
|
-
export { cors, addCorsHeaders } from './cors';
|
|
25
|
-
export { cache, setCache, invalidateCache } from './cache';
|
|
26
|
-
export { checkRateLimit } from './rate-limit';
|
|
27
|
-
export { requireAuth, addUserContext } from './auth';
|
|
28
|
-
|
|
29
27
|
// ============================================================
|
|
30
28
|
// New Middleware
|
|
31
29
|
// ============================================================
|