ai.matey.http.core 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/LICENSE +21 -0
- package/dist/cjs/auth.js +173 -0
- package/dist/cjs/auth.js.map +1 -0
- package/dist/cjs/cors.js +140 -0
- package/dist/cjs/cors.js.map +1 -0
- package/dist/cjs/error-handler.js +147 -0
- package/dist/cjs/error-handler.js.map +1 -0
- package/dist/cjs/handler.js +335 -0
- package/dist/cjs/handler.js.map +1 -0
- package/dist/cjs/health.js +218 -0
- package/dist/cjs/health.js.map +1 -0
- package/dist/cjs/index.js +83 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/rate-limiter.js +163 -0
- package/dist/cjs/rate-limiter.js.map +1 -0
- package/dist/cjs/request-parser.js +141 -0
- package/dist/cjs/request-parser.js.map +1 -0
- package/dist/cjs/response-formatter.js +218 -0
- package/dist/cjs/response-formatter.js.map +1 -0
- package/dist/cjs/route-matcher.js +178 -0
- package/dist/cjs/route-matcher.js.map +1 -0
- package/dist/cjs/streaming-handler.js +120 -0
- package/dist/cjs/streaming-handler.js.map +1 -0
- package/dist/cjs/types.js +11 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/esm/auth.js +163 -0
- package/dist/esm/auth.js.map +1 -0
- package/dist/esm/cors.js +134 -0
- package/dist/esm/cors.js.map +1 -0
- package/dist/esm/error-handler.js +137 -0
- package/dist/esm/error-handler.js.map +1 -0
- package/dist/esm/handler.js +331 -0
- package/dist/esm/handler.js.map +1 -0
- package/dist/esm/health.js +210 -0
- package/dist/esm/health.js.map +1 -0
- package/dist/esm/index.js +30 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/rate-limiter.js +156 -0
- package/dist/esm/rate-limiter.js.map +1 -0
- package/dist/esm/request-parser.js +136 -0
- package/dist/esm/request-parser.js.map +1 -0
- package/dist/esm/response-formatter.js +206 -0
- package/dist/esm/response-formatter.js.map +1 -0
- package/dist/esm/route-matcher.js +171 -0
- package/dist/esm/route-matcher.js.map +1 -0
- package/dist/esm/streaming-handler.js +112 -0
- package/dist/esm/streaming-handler.js.map +1 -0
- package/dist/esm/types.js +10 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/types/auth.d.ts +37 -0
- package/dist/types/auth.d.ts.map +1 -0
- package/dist/types/cors.d.ts +26 -0
- package/dist/types/cors.d.ts.map +1 -0
- package/dist/types/error-handler.d.ts +38 -0
- package/dist/types/error-handler.d.ts.map +1 -0
- package/dist/types/handler.d.ts +45 -0
- package/dist/types/handler.d.ts.map +1 -0
- package/dist/types/health.d.ts +164 -0
- package/dist/types/health.d.ts.map +1 -0
- package/dist/types/index.d.ts +21 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/rate-limiter.d.ts +56 -0
- package/dist/types/rate-limiter.d.ts.map +1 -0
- package/dist/types/request-parser.d.ts +22 -0
- package/dist/types/request-parser.d.ts.map +1 -0
- package/dist/types/response-formatter.d.ts +49 -0
- package/dist/types/response-formatter.d.ts.map +1 -0
- package/dist/types/route-matcher.d.ts +45 -0
- package/dist/types/route-matcher.d.ts.map +1 -0
- package/dist/types/streaming-handler.d.ts +40 -0
- package/dist/types/streaming-handler.d.ts.map +1 -0
- package/dist/types/types.d.ts +398 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +73 -0
- package/readme.md +60 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core HTTP Handler
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic HTTP request handler that can be adapted to any framework.
|
|
5
|
+
* Contains all the core logic for CORS, auth, rate limiting, routing, and streaming.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { GenericRequest, GenericResponse, CoreHandlerOptions } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Core HTTP request handler (framework-agnostic)
|
|
12
|
+
*/
|
|
13
|
+
export declare class CoreHTTPHandler {
|
|
14
|
+
private readonly bridge;
|
|
15
|
+
private readonly options;
|
|
16
|
+
private readonly rateLimiter;
|
|
17
|
+
private readonly routeMatcher;
|
|
18
|
+
constructor(options: CoreHandlerOptions);
|
|
19
|
+
/**
|
|
20
|
+
* Dispose of the handler and release resources.
|
|
21
|
+
* Call this when the handler is no longer needed to prevent memory leaks.
|
|
22
|
+
*/
|
|
23
|
+
dispose(): void;
|
|
24
|
+
/**
|
|
25
|
+
* Handle HTTP request
|
|
26
|
+
*/
|
|
27
|
+
handle(req: GenericRequest, res: GenericResponse): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Handle errors
|
|
30
|
+
*/
|
|
31
|
+
private handleError;
|
|
32
|
+
/**
|
|
33
|
+
* Check if origin is allowed
|
|
34
|
+
*/
|
|
35
|
+
private isOriginAllowed;
|
|
36
|
+
/**
|
|
37
|
+
* Get HTTP status code from error
|
|
38
|
+
*/
|
|
39
|
+
private getErrorStatusCode;
|
|
40
|
+
/**
|
|
41
|
+
* Format error response in provider-specific format
|
|
42
|
+
*/
|
|
43
|
+
private formatErrorResponse;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/handler.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EACf,kBAAkB,EAEnB,MAAM,YAAY,CAAC;AAMpB;;GAEG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAQtB;IACF,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAqB;IACjD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAsB;gBAEvC,OAAO,EAAE,kBAAkB;IAsCvC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAMf;;OAEG;IACG,MAAM,CAAC,GAAG,EAAE,cAAc,EAAE,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;IAmNtE;;OAEG;YACW,WAAW;IAuBzB;;OAEG;IACH,OAAO,CAAC,eAAe;IAuBvB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAgC1B;;OAEG;IACH,OAAO,CAAC,mBAAmB;CA+B5B"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Health Check Utilities
|
|
3
|
+
*
|
|
4
|
+
* Health check endpoint for monitoring and load balancers.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { GenericRequest, GenericResponse } from './types.js';
|
|
9
|
+
import type { Bridge, Router } from 'ai.matey.core';
|
|
10
|
+
/**
|
|
11
|
+
* Health status levels
|
|
12
|
+
*/
|
|
13
|
+
export type HealthStatus = 'healthy' | 'degraded' | 'unhealthy';
|
|
14
|
+
/**
|
|
15
|
+
* Health check result
|
|
16
|
+
*/
|
|
17
|
+
export interface HealthCheckResult {
|
|
18
|
+
/**
|
|
19
|
+
* Overall health status
|
|
20
|
+
*/
|
|
21
|
+
status: HealthStatus;
|
|
22
|
+
/**
|
|
23
|
+
* Timestamp of health check
|
|
24
|
+
*/
|
|
25
|
+
timestamp: string;
|
|
26
|
+
/**
|
|
27
|
+
* Service name/version
|
|
28
|
+
*/
|
|
29
|
+
service?: string;
|
|
30
|
+
version?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Uptime in milliseconds
|
|
33
|
+
*/
|
|
34
|
+
uptime?: number;
|
|
35
|
+
/**
|
|
36
|
+
* Component-specific checks
|
|
37
|
+
*/
|
|
38
|
+
checks?: Record<string, ComponentHealth>;
|
|
39
|
+
/**
|
|
40
|
+
* Additional metadata
|
|
41
|
+
*/
|
|
42
|
+
metadata?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Component health status
|
|
46
|
+
*/
|
|
47
|
+
export interface ComponentHealth {
|
|
48
|
+
status: HealthStatus;
|
|
49
|
+
message?: string;
|
|
50
|
+
latency?: number;
|
|
51
|
+
details?: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Health check configuration
|
|
55
|
+
*/
|
|
56
|
+
export interface HealthCheckConfig {
|
|
57
|
+
/**
|
|
58
|
+
* Service name
|
|
59
|
+
* @default 'ai.matey'
|
|
60
|
+
*/
|
|
61
|
+
serviceName?: string;
|
|
62
|
+
/**
|
|
63
|
+
* Service version
|
|
64
|
+
* @default '1.0.0'
|
|
65
|
+
*/
|
|
66
|
+
version?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Include uptime in response
|
|
69
|
+
* @default true
|
|
70
|
+
*/
|
|
71
|
+
includeUptime?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Include component checks
|
|
74
|
+
* @default true
|
|
75
|
+
*/
|
|
76
|
+
includeChecks?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Custom health checks
|
|
79
|
+
*/
|
|
80
|
+
customChecks?: Record<string, () => Promise<ComponentHealth>>;
|
|
81
|
+
/**
|
|
82
|
+
* Check backend health (for routers)
|
|
83
|
+
* @default true
|
|
84
|
+
*/
|
|
85
|
+
checkBackends?: boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Additional metadata to include
|
|
88
|
+
*/
|
|
89
|
+
metadata?: Record<string, unknown>;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Health check handler
|
|
93
|
+
*/
|
|
94
|
+
export declare class HealthCheck {
|
|
95
|
+
private config;
|
|
96
|
+
private customChecks;
|
|
97
|
+
private metadata;
|
|
98
|
+
private startTime;
|
|
99
|
+
private router?;
|
|
100
|
+
constructor(bridgeOrRouter: Bridge | Router, config?: HealthCheckConfig);
|
|
101
|
+
/**
|
|
102
|
+
* Perform health check
|
|
103
|
+
*/
|
|
104
|
+
check(): Promise<HealthCheckResult>;
|
|
105
|
+
/**
|
|
106
|
+
* Handle health check request
|
|
107
|
+
*/
|
|
108
|
+
handle(_req: GenericRequest, res: GenericResponse): Promise<void>;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Create health check handler
|
|
112
|
+
*
|
|
113
|
+
* @param bridgeOrRouter - Bridge or Router instance
|
|
114
|
+
* @param config - Health check configuration
|
|
115
|
+
* @returns Health check handler
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* import { createHealthCheck } from 'ai.matey.http';
|
|
120
|
+
*
|
|
121
|
+
* const healthCheck = createHealthCheck(bridge, {
|
|
122
|
+
* serviceName: 'my-ai-service',
|
|
123
|
+
* version: '1.0.0',
|
|
124
|
+
* });
|
|
125
|
+
*
|
|
126
|
+
* // In your HTTP server:
|
|
127
|
+
* if (req.url === '/health') {
|
|
128
|
+
* await healthCheck.handle(req, res);
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare function createHealthCheck(bridgeOrRouter: Bridge | Router, config?: HealthCheckConfig): HealthCheck;
|
|
133
|
+
/**
|
|
134
|
+
* Create simple health check middleware
|
|
135
|
+
*
|
|
136
|
+
* @param bridgeOrRouter - Bridge or Router instance
|
|
137
|
+
* @param path - Health check path (default: '/health')
|
|
138
|
+
* @param config - Health check configuration
|
|
139
|
+
* @returns Middleware function
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* import { createHealthCheckMiddleware } from 'ai.matey.http';
|
|
144
|
+
*
|
|
145
|
+
* const healthMiddleware = createHealthCheckMiddleware(bridge);
|
|
146
|
+
*
|
|
147
|
+
* // Use in HTTP server
|
|
148
|
+
* app.use(healthMiddleware);
|
|
149
|
+
* ```
|
|
150
|
+
*/
|
|
151
|
+
export declare function createHealthCheckMiddleware(bridgeOrRouter: Bridge | Router, path?: string, config?: HealthCheckConfig): (req: GenericRequest, res: GenericResponse) => Promise<boolean>;
|
|
152
|
+
/**
|
|
153
|
+
* Readiness check (for Kubernetes)
|
|
154
|
+
*
|
|
155
|
+
* Returns 200 when service is ready to accept traffic.
|
|
156
|
+
*/
|
|
157
|
+
export declare function createReadinessCheck(bridgeOrRouter: Bridge | Router): (req: GenericRequest, res: GenericResponse) => Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Liveness check (for Kubernetes)
|
|
160
|
+
*
|
|
161
|
+
* Returns 200 if service is alive (even if degraded).
|
|
162
|
+
*/
|
|
163
|
+
export declare function createLivenessCheck(): (req: GenericRequest, res: GenericResponse) => void;
|
|
164
|
+
//# sourceMappingURL=health.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../src/health.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,SAAS,GAAG,UAAU,GAAG,WAAW,CAAC;AAEhE;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,MAAM,EAAE,YAAY,CAAC;IAErB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAEzC;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,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AAG,MAAM,EAC/B,IAAI,GAAE,MAAkB,EACxB,MAAM,CAAC,EAAE,iBAAiB,GACzB,CAAC,GAAG,EAAE,cAAc,EAAE,GAAG,EAAE,eAAe,KAAK,OAAO,CAAC,OAAO,CAAC,CAUjE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,cAAc,EAAE,MAAM,GAAG,MAAM,GAC9B,CAAC,GAAG,EAAE,cAAc,EAAE,GAAG,EAAE,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,CAqB9D;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,IAAI,CAAC,GAAG,EAAE,cAAc,EAAE,GAAG,EAAE,eAAe,KAAK,IAAI,CAYzF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Core Module
|
|
3
|
+
*
|
|
4
|
+
* Framework-agnostic HTTP utilities for the Universal AI Adapter System.
|
|
5
|
+
* This package provides the core logic for HTTP request handling that can
|
|
6
|
+
* be adapted to any HTTP framework.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
export { CoreHTTPHandler } from './handler.js';
|
|
11
|
+
export type { HTTPRequestHandler, HTTPListenerOptions, CORSOptions, RateLimitOptions, RouteConfig, AuthValidator, ErrorHandler, RateLimitKeyGenerator, RateLimitHandler, ParsedRequest, RateLimitState, MatchedRoute, GenericRequest, GenericResponse, GenericAuthValidator, GenericErrorHandler, GenericRateLimitKeyGenerator, GenericRateLimitHandler, GenericRateLimitOptions, CoreHandlerOptions, ParsedGenericRequest, } from './types.js';
|
|
12
|
+
export { parseRequest, extractBearerToken, getClientIP } from './request-parser.js';
|
|
13
|
+
export { sendJSON, sendError, sendSSEHeaders, sendSSEChunk, sendSSEEvent, sendSSEDone, sendSSEError, sendText, sendNoContent, detectProviderFormat, } from './response-formatter.js';
|
|
14
|
+
export { normalizeCORSOptions, handleCORS, handlePreflight, isPreflight } from './cors.js';
|
|
15
|
+
export { defaultAuthValidator, createBearerTokenValidator, createAPIKeyValidator, createBasicAuthValidator, combineAuthValidators, requireAllAuth, skipAuthForPaths, } from './auth.js';
|
|
16
|
+
export { RateLimiter, userIDKeyGenerator, tokenKeyGenerator, combineKeyGenerators, } from './rate-limiter.js';
|
|
17
|
+
export { defaultErrorHandler, createLoggingErrorHandler, createReportingErrorHandler, wrapErrorHandler, isRetryableError, isClientError, isServerError, } from './error-handler.js';
|
|
18
|
+
export { handleStreamingRequest, SSEKeepAlive, onClientDisconnect, supportsStreaming, createAbortController, } from './streaming-handler.js';
|
|
19
|
+
export { RouteMatcher, createDefaultRoutes, normalizePath, applyPathPrefix, } from './route-matcher.js';
|
|
20
|
+
export { HealthCheck, createHealthCheck, createHealthCheckMiddleware, createReadinessCheck, createLivenessCheck, type HealthCheckConfig, type HealthCheckResult, type HealthStatus, type ComponentHealth, } from './health.js';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAG/C,YAAY,EACV,kBAAkB,EAClB,mBAAmB,EACnB,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,aAAa,EACb,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,YAAY,EACZ,cAAc,EACd,eAAe,EACf,oBAAoB,EACpB,mBAAmB,EACnB,4BAA4B,EAC5B,uBAAuB,EACvB,uBAAuB,EACvB,kBAAkB,EAClB,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAGpF,OAAO,EACL,QAAQ,EACR,SAAS,EACT,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,aAAa,EACb,oBAAoB,GACrB,MAAM,yBAAyB,CAAC;AAGjC,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAG3F,OAAO,EACL,oBAAoB,EACpB,0BAA0B,EAC1B,qBAAqB,EACrB,wBAAwB,EACxB,qBAAqB,EACrB,cAAc,EACd,gBAAgB,GACjB,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EACL,mBAAmB,EACnB,yBAAyB,EACzB,2BAA2B,EAC3B,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,aAAa,GACd,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,kBAAkB,EAClB,iBAAiB,EACjB,qBAAqB,GACtB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,aAAa,EACb,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,WAAW,EACX,iBAAiB,EACjB,2BAA2B,EAC3B,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,EACtB,KAAK,YAAY,EACjB,KAAK,eAAe,GACrB,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate Limiter
|
|
3
|
+
*
|
|
4
|
+
* Implements rate limiting for HTTP requests using a sliding window algorithm.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { IncomingMessage, ServerResponse } from 'http';
|
|
9
|
+
import type { RateLimitOptions, RateLimitState } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Rate limiter class
|
|
12
|
+
*/
|
|
13
|
+
export declare class RateLimiter {
|
|
14
|
+
private readonly options;
|
|
15
|
+
private readonly store;
|
|
16
|
+
private cleanupIntervalId;
|
|
17
|
+
constructor(options: RateLimitOptions);
|
|
18
|
+
/**
|
|
19
|
+
* Dispose of the rate limiter and clean up resources.
|
|
20
|
+
* Call this when the rate limiter is no longer needed to prevent memory leaks.
|
|
21
|
+
*/
|
|
22
|
+
dispose(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Check if request should be rate limited
|
|
25
|
+
*/
|
|
26
|
+
check(req: IncomingMessage, res: ServerResponse): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* Clean up expired entries
|
|
29
|
+
*/
|
|
30
|
+
private cleanup;
|
|
31
|
+
/**
|
|
32
|
+
* Reset rate limit for a specific key
|
|
33
|
+
*/
|
|
34
|
+
reset(key: string): void;
|
|
35
|
+
/**
|
|
36
|
+
* Reset all rate limits
|
|
37
|
+
*/
|
|
38
|
+
resetAll(): void;
|
|
39
|
+
/**
|
|
40
|
+
* Get current state for a key
|
|
41
|
+
*/
|
|
42
|
+
getState(key: string): RateLimitState | undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create key generator from user ID in request body
|
|
46
|
+
*/
|
|
47
|
+
export declare function userIDKeyGenerator(req: IncomingMessage): string;
|
|
48
|
+
/**
|
|
49
|
+
* Create key generator from authorization token
|
|
50
|
+
*/
|
|
51
|
+
export declare function tokenKeyGenerator(req: IncomingMessage): string;
|
|
52
|
+
/**
|
|
53
|
+
* Create key generator that combines multiple factors
|
|
54
|
+
*/
|
|
55
|
+
export declare function combineKeyGenerators(...generators: ((req: IncomingMessage) => string)[]): (req: IncomingMessage) => string;
|
|
56
|
+
//# sourceMappingURL=rate-limiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rate-limiter.d.ts","sourceRoot":"","sources":["../../src/rate-limiter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAInE;;GAEG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA6B;IACrD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA8B;IACpD,OAAO,CAAC,iBAAiB,CAA6C;gBAE1D,OAAO,EAAE,gBAAgB;IAgBrC;;;OAGG;IACH,OAAO,IAAI,IAAI;IAQf;;OAEG;IACG,KAAK,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC;IAgDxE;;OAEG;IACH,OAAO,CAAC,OAAO;IAUf;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAIxB;;OAEG;IACH,QAAQ,IAAI,IAAI;IAIhB;;OAEG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;CAGlD;AA8BD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,CAI/D;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,CAQ9D;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,UAAU,EAAE,CAAC,CAAC,GAAG,EAAE,eAAe,KAAK,MAAM,CAAC,EAAE,GAClD,CAAC,GAAG,EAAE,eAAe,KAAK,MAAM,CAKlC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Request Parser
|
|
3
|
+
*
|
|
4
|
+
* Parses incoming HTTP requests into a standardized format for processing.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { IncomingMessage } from 'http';
|
|
9
|
+
import type { ParsedRequest } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Parse incoming HTTP request
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseRequest(req: IncomingMessage, maxBodySize?: number): Promise<ParsedRequest>;
|
|
14
|
+
/**
|
|
15
|
+
* Extract bearer token from Authorization header
|
|
16
|
+
*/
|
|
17
|
+
export declare function extractBearerToken(req: IncomingMessage): string | null;
|
|
18
|
+
/**
|
|
19
|
+
* Get client IP address from request
|
|
20
|
+
*/
|
|
21
|
+
export declare function getClientIP(req: IncomingMessage): string;
|
|
22
|
+
//# sourceMappingURL=request-parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-parser.d.ts","sourceRoot":"","sources":["../../src/request-parser.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEhD;;GAEG;AACH,wBAAsB,YAAY,CAChC,GAAG,EAAE,eAAe,EACpB,WAAW,GAAE,MAAyB,GACrC,OAAO,CAAC,aAAa,CAAC,CA8DxB;AAgDD;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,GAAG,IAAI,CAStE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,eAAe,GAAG,MAAM,CAexD"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Response Formatter
|
|
3
|
+
*
|
|
4
|
+
* Formats responses for HTTP responses with proper headers and status codes.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { ServerResponse } from 'http';
|
|
9
|
+
/**
|
|
10
|
+
* Send JSON response
|
|
11
|
+
*/
|
|
12
|
+
export declare function sendJSON(res: ServerResponse, data: any, statusCode?: number, headers?: Record<string, string>): void;
|
|
13
|
+
/**
|
|
14
|
+
* Send error response
|
|
15
|
+
*/
|
|
16
|
+
export declare function sendError(res: ServerResponse, error: Error, statusCode?: number, format?: 'openai' | 'anthropic' | 'generic'): void;
|
|
17
|
+
/**
|
|
18
|
+
* Send Server-Sent Events (SSE) headers
|
|
19
|
+
*/
|
|
20
|
+
export declare function sendSSEHeaders(res: ServerResponse, headers?: Record<string, string>): void;
|
|
21
|
+
/**
|
|
22
|
+
* Send SSE data chunk
|
|
23
|
+
*/
|
|
24
|
+
export declare function sendSSEChunk(res: ServerResponse, data: any): void;
|
|
25
|
+
/**
|
|
26
|
+
* Send SSE event with custom event type
|
|
27
|
+
*/
|
|
28
|
+
export declare function sendSSEEvent(res: ServerResponse, event: string, data: any): void;
|
|
29
|
+
/**
|
|
30
|
+
* Send SSE done marker
|
|
31
|
+
*/
|
|
32
|
+
export declare function sendSSEDone(res: ServerResponse): void;
|
|
33
|
+
/**
|
|
34
|
+
* Send SSE error
|
|
35
|
+
*/
|
|
36
|
+
export declare function sendSSEError(res: ServerResponse, error: Error, format?: 'openai' | 'anthropic' | 'generic'): void;
|
|
37
|
+
/**
|
|
38
|
+
* Send plain text response
|
|
39
|
+
*/
|
|
40
|
+
export declare function sendText(res: ServerResponse, text: string, statusCode?: number, headers?: Record<string, string>): void;
|
|
41
|
+
/**
|
|
42
|
+
* Send 204 No Content response
|
|
43
|
+
*/
|
|
44
|
+
export declare function sendNoContent(res: ServerResponse): void;
|
|
45
|
+
/**
|
|
46
|
+
* Detect provider format from request path or headers
|
|
47
|
+
*/
|
|
48
|
+
export declare function detectProviderFormat(path: string): 'openai' | 'anthropic' | 'generic';
|
|
49
|
+
//# sourceMappingURL=response-formatter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-formatter.d.ts","sourceRoot":"","sources":["../../src/response-formatter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAE3C;;GAEG;AACH,wBAAgB,QAAQ,CACtB,GAAG,EAAE,c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
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route Matcher
|
|
3
|
+
*
|
|
4
|
+
* Matches incoming HTTP requests to configured routes.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { IncomingMessage } from 'http';
|
|
9
|
+
import type { RouteConfig, MatchedRoute } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Route matcher class
|
|
12
|
+
*/
|
|
13
|
+
export declare class RouteMatcher {
|
|
14
|
+
private routes;
|
|
15
|
+
constructor(routes: RouteConfig[]);
|
|
16
|
+
/**
|
|
17
|
+
* Match request to a route
|
|
18
|
+
*/
|
|
19
|
+
match(req: IncomingMessage): MatchedRoute | null;
|
|
20
|
+
/**
|
|
21
|
+
* Add a route
|
|
22
|
+
*/
|
|
23
|
+
addRoute(route: RouteConfig): void;
|
|
24
|
+
/**
|
|
25
|
+
* Remove a route
|
|
26
|
+
*/
|
|
27
|
+
removeRoute(path: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Get all routes
|
|
30
|
+
*/
|
|
31
|
+
getRoutes(): readonly RouteConfig[];
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create default routes for common provider endpoints
|
|
35
|
+
*/
|
|
36
|
+
export declare function createDefaultRoutes(frontendAdapters: Map<string, any>): RouteConfig[];
|
|
37
|
+
/**
|
|
38
|
+
* Normalize path by removing trailing slashes and adding leading slash
|
|
39
|
+
*/
|
|
40
|
+
export declare function normalizePath(path: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* Apply path prefix to route paths
|
|
43
|
+
*/
|
|
44
|
+
export declare function applyPathPrefix(routes: RouteConfig[], prefix: string): RouteConfig[];
|
|
45
|
+
//# sourceMappingURL=route-matcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-matcher.d.ts","sourceRoot":"","sources":["../../src/route-matcher.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,MAAM,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE5D;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAgB;gBAElB,MAAM,EAAE,WAAW,EAAE;IAIjC;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,eAAe,GAAG,YAAY,GAAG,IAAI;IAyBhD;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI;IAIlC;;OAEG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IASlC;;OAEG;IACH,SAAS,IAAI,SAAS,WAAW,EAAE;CAGpC;AAkFD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,WAAW,EAAE,CAsBrF;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAYlD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,WAAW,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,EAAE,CAOpF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Streaming Handler
|
|
3
|
+
*
|
|
4
|
+
* Handles Server-Sent Events (SSE) streaming for HTTP responses.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { ServerResponse } from 'http';
|
|
9
|
+
import type { Bridge } from 'ai.matey.core';
|
|
10
|
+
/**
|
|
11
|
+
* Handle streaming request
|
|
12
|
+
*/
|
|
13
|
+
export declare function handleStreamingRequest(bridge: Bridge, request: any, res: ServerResponse, format?: 'openai' | 'anthropic' | 'generic', headers?: Record<string, string>): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Create SSE keep-alive mechanism
|
|
16
|
+
*/
|
|
17
|
+
export declare class SSEKeepAlive {
|
|
18
|
+
private intervalId;
|
|
19
|
+
/**
|
|
20
|
+
* Start sending keep-alive pings
|
|
21
|
+
*/
|
|
22
|
+
start(res: ServerResponse, intervalMs?: number): void;
|
|
23
|
+
/**
|
|
24
|
+
* Stop sending keep-alive pings
|
|
25
|
+
*/
|
|
26
|
+
stop(): void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Handle client disconnect
|
|
30
|
+
*/
|
|
31
|
+
export declare function onClientDisconnect(res: ServerResponse, callback: () => void): void;
|
|
32
|
+
/**
|
|
33
|
+
* Check if response supports streaming
|
|
34
|
+
*/
|
|
35
|
+
export declare function supportsStreaming(res: ServerResponse): boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Create streaming abort controller from response
|
|
38
|
+
*/
|
|
39
|
+
export declare function createAbortController(res: ServerResponse): AbortController;
|
|
40
|
+
//# sourceMappingURL=streaming-handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"streaming-handler.d.ts","sourceRoot":"","sources":["../../src/streaming-handler.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAS5C;;GAEG;AACH,wBAAsB,sBAAsB,CAC1C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,GAAG,EACZ,GAAG,EAAE,cAAc,EACnB,MAAM,GAAE,QAAQ,GAAG,WAAW,GAAG,SAAqB,EACtD,OAAO,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAM,GACnC,OAAO,CAAC,IAAI,CAAC,CAoCf;AAcD;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,UAAU,CAA+B;IAEjD;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,cAAc,EAAE,UAAU,GAAE,MAAc,GAAG,IAAI;IAU5D;;OAEG;IACH,IAAI,IAAI,IAAI;CAMb;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI,CAIlF;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,cAAc,GAAG,OAAO,CAE9D;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,cAAc,GAAG,eAAe,CAQ1E"}
|