@stacksjs/bun-router 0.0.2
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.md +21 -0
- package/README.md +397 -0
- package/bin/cli.ts +9 -0
- package/dist/auth.d.ts +77 -0
- package/dist/cache/lru-cache.d.ts +88 -0
- package/dist/cache/middleware-memoization.d.ts +101 -0
- package/dist/cache/route-cache-warmer.d.ts +125 -0
- package/dist/cache/sqlite-cache.d.ts +114 -0
- package/dist/cache/streaming-cache.d.ts +86 -0
- package/dist/chunk-r15a48wc.js +25448 -0
- package/dist/cli/colors.d.ts +12 -0
- package/dist/cli/index.d.ts +10 -0
- package/dist/cli/middleware.d.ts +31 -0
- package/dist/cli/openapi.d.ts +17 -0
- package/dist/cli/router.d.ts +15 -0
- package/dist/cli/routes.d.ts +30 -0
- package/dist/cli/utils.d.ts +68 -0
- package/dist/cli.d.ts +0 -0
- package/dist/cli.js +3195 -0
- package/dist/config.d.ts +274 -0
- package/dist/container/container.d.ts +141 -0
- package/dist/container/contextual-binding.d.ts +112 -0
- package/dist/container/decorators.d.ts +114 -0
- package/dist/container/service-provider.d.ts +221 -0
- package/dist/development/hot-reload.d.ts +129 -0
- package/dist/development/index.d.ts +144 -0
- package/dist/development/performance-profiler.d.ts +152 -0
- package/dist/development/route-debugger.d.ts +102 -0
- package/dist/development/route-inspector.d.ts +142 -0
- package/dist/development/typescript-utilities.d.ts +116 -0
- package/dist/docs.d.ts +10 -0
- package/dist/errors/circuit-breaker.d.ts +138 -0
- package/dist/errors/error-handler.d.ts +57 -0
- package/dist/errors/error-reporting.d.ts +135 -0
- package/dist/errors/exceptions.d.ts +178 -0
- package/dist/errors/graceful-degradation.d.ts +130 -0
- package/dist/errors/index.d.ts +6 -0
- package/dist/errors/router-errors.d.ts +97 -0
- package/dist/file-serving/static-files.d.ts +98 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +13417 -0
- package/dist/middleware/auth.d.ts +34 -0
- package/dist/middleware/content_security_policy.d.ts +54 -0
- package/dist/middleware/cors.d.ts +5 -0
- package/dist/middleware/csrf.d.ts +7 -0
- package/dist/middleware/ddos_protection.d.ts +47 -0
- package/dist/middleware/file_security.d.ts +23 -0
- package/dist/middleware/file_upload.d.ts +41 -0
- package/dist/middleware/helmet.d.ts +54 -0
- package/dist/middleware/index.d.ts +55 -0
- package/dist/middleware/input_validation.d.ts +46 -0
- package/dist/middleware/json_body.d.ts +4 -0
- package/dist/middleware/performance_alerting.d.ts +88 -0
- package/dist/middleware/performance_dashboard.d.ts +97 -0
- package/dist/middleware/performance_monitor.d.ts +236 -0
- package/dist/middleware/pipeline.d.ts +90 -0
- package/dist/middleware/rate_limit.d.ts +33 -0
- package/dist/middleware/request_id.d.ts +4 -0
- package/dist/middleware/request_signing.d.ts +96 -0
- package/dist/middleware/request_tracer.d.ts +76 -0
- package/dist/middleware/response_cache.d.ts +90 -0
- package/dist/middleware/security.d.ts +70 -0
- package/dist/middleware/security_suite.d.ts +46 -0
- package/dist/middleware/session.d.ts +7 -0
- package/dist/model-binding/index.d.ts +17 -0
- package/dist/model-binding/model-middleware.d.ts +80 -0
- package/dist/model-binding/model-registry.d.ts +125 -0
- package/dist/model-binding.d.ts +123 -0
- package/dist/model-resolver-factory.d.ts +31 -0
- package/dist/observability/correlation.d.ts +131 -0
- package/dist/observability/health-checks.d.ts +129 -0
- package/dist/observability/index.d.ts +92 -0
- package/dist/observability/integration.d.ts +112 -0
- package/dist/observability/metrics.d.ts +135 -0
- package/dist/observability/tracing.d.ts +112 -0
- package/dist/optimization/bun-utilities.d.ts +106 -0
- package/dist/query-builder-integration.d.ts +24 -0
- package/dist/request/enhanced-request.d.ts +143 -0
- package/dist/request/macros.d.ts +267 -0
- package/dist/response/macros.d.ts +207 -0
- package/dist/response/response-factory.d.ts +93 -0
- package/dist/router/file-based-routing.d.ts +40 -0
- package/dist/router/file-streaming.d.ts +6 -0
- package/dist/router/fluent-router.d.ts +179 -0
- package/dist/router/fluent-routing.d.ts +198 -0
- package/dist/router/group-organization.d.ts +5 -0
- package/dist/router/handler-resolver.d.ts +13 -0
- package/dist/router/http-methods.d.ts +10 -0
- package/dist/router/index.d.ts +114 -0
- package/dist/router/middleware-integration.d.ts +86 -0
- package/dist/router/middleware.d.ts +5 -0
- package/dist/router/model-binding.d.ts +7 -0
- package/dist/router/optimized-route-matching.d.ts +6 -0
- package/dist/router/route-building.d.ts +5 -0
- package/dist/router/route-compiler.d.ts +67 -0
- package/dist/router/route-matching.d.ts +5 -0
- package/dist/router/route-trie.d.ts +69 -0
- package/dist/router/router.d.ts +133 -0
- package/dist/router/server.d.ts +5 -0
- package/dist/router/validation-integration.d.ts +116 -0
- package/dist/router/view-rendering.d.ts +5 -0
- package/dist/router/websocket.d.ts +5 -0
- package/dist/routing/route-caching.d.ts +105 -0
- package/dist/routing/route-throttling.d.ts +112 -0
- package/dist/routing/subdomain-routing.d.ts +159 -0
- package/dist/streaming/index.d.ts +15 -0
- package/dist/streaming/sse-handler.d.ts +81 -0
- package/dist/streaming/stream-handler.d.ts +81 -0
- package/dist/testing/auth-testing.d.ts +107 -0
- package/dist/testing/file-upload-testing.d.ts +139 -0
- package/dist/testing/index.d.ts +10 -0
- package/dist/testing/middleware-testing.d.ts +99 -0
- package/dist/testing/model-binding-testing.d.ts +132 -0
- package/dist/testing/performance-testing.d.ts +202 -0
- package/dist/testing/test-client.d.ts +58 -0
- package/dist/testing/test-request.d.ts +43 -0
- package/dist/testing/test-response.d.ts +45 -0
- package/dist/testing/types.d.ts +206 -0
- package/dist/testing/websocket-testing.d.ts +170 -0
- package/dist/types/controller-types.d.ts +303 -0
- package/dist/types/core.d.ts +526 -0
- package/dist/types/middleware-types.d.ts +315 -0
- package/dist/types/request-response-augmentation.d.ts +373 -0
- package/dist/types/route-inference.d.ts +287 -0
- package/dist/types.d.ts +1620 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/query-preservation.d.ts +31 -0
- package/dist/utils.d.ts +69 -0
- package/dist/validation/validator.d.ts +169 -0
- package/dist/websocket/clustering.d.ts +114 -0
- package/package.json +81 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import type { MiddlewareHandler, RouteHandler } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Create subdomain routing middleware
|
|
4
|
+
*/
|
|
5
|
+
export declare function createSubdomainMiddleware(subdomainRouter: SubdomainRouter): void;
|
|
6
|
+
/**
|
|
7
|
+
* Utility functions for subdomain routing
|
|
8
|
+
*/
|
|
9
|
+
export declare const SubdomainUtils: {
|
|
10
|
+
/**
|
|
11
|
+
* Extract subdomain from request
|
|
12
|
+
*/
|
|
13
|
+
getSubdomain: (req: EnhancedRequest) => any;
|
|
14
|
+
/**
|
|
15
|
+
* Get domain parameters from request
|
|
16
|
+
*/
|
|
17
|
+
getDomainParams: (req: EnhancedRequest) => unknown;
|
|
18
|
+
/**
|
|
19
|
+
* Check if request matches domain pattern
|
|
20
|
+
*/
|
|
21
|
+
matchesDomain: (req: EnhancedRequest, pattern: string) => any;
|
|
22
|
+
/**
|
|
23
|
+
* Get full subdomain info from request
|
|
24
|
+
*/
|
|
25
|
+
getSubdomainInfo: (req: EnhancedRequest) => any
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Factory functions for common subdomain patterns
|
|
29
|
+
*/
|
|
30
|
+
export declare const SubdomainPatterns: {
|
|
31
|
+
/**
|
|
32
|
+
* Single subdomain pattern (e.g., api.example.com)
|
|
33
|
+
*/
|
|
34
|
+
single: (subdomain: string, domain: string) => any;
|
|
35
|
+
/**
|
|
36
|
+
* Wildcard subdomain pattern (e.g., {account}.example.com)
|
|
37
|
+
*/
|
|
38
|
+
wildcard: (domain: string, paramName: string?) => any;
|
|
39
|
+
/**
|
|
40
|
+
* Multi-level subdomain (e.g., {service}.{env}.example.com)
|
|
41
|
+
*/
|
|
42
|
+
multiLevel: (domain: string, ...paramNames: string[]) => any;
|
|
43
|
+
/**
|
|
44
|
+
* API versioning pattern (e.g., v1.api.example.com)
|
|
45
|
+
*/
|
|
46
|
+
apiVersion: (domain: string) => any;
|
|
47
|
+
/**
|
|
48
|
+
* Tenant pattern (e.g., {tenant}.app.example.com)
|
|
49
|
+
*/
|
|
50
|
+
tenant: (domain: string) => any;
|
|
51
|
+
/**
|
|
52
|
+
* Environment pattern (e.g., {env}.example.com)
|
|
53
|
+
*/
|
|
54
|
+
environment: (domain: string) => any
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Domain pattern configuration
|
|
58
|
+
*/
|
|
59
|
+
export declare interface DomainPattern {
|
|
60
|
+
pattern: string
|
|
61
|
+
parameters: string[]
|
|
62
|
+
regex: RegExp
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Domain group configuration
|
|
66
|
+
*/
|
|
67
|
+
export declare interface DomainGroupConfig {
|
|
68
|
+
domain: string
|
|
69
|
+
middleware?: MiddlewareHandler[]
|
|
70
|
+
prefix?: string
|
|
71
|
+
name?: string
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Subdomain route information
|
|
75
|
+
*/
|
|
76
|
+
export declare interface SubdomainRouteInfo {
|
|
77
|
+
domain: string
|
|
78
|
+
parameters: Record<string, string>
|
|
79
|
+
subdomain?: string
|
|
80
|
+
rootDomain?: string
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Domain pattern parser and matcher
|
|
84
|
+
*/
|
|
85
|
+
export declare class DomainMatcher {
|
|
86
|
+
static parseDomainPattern(pattern: string): DomainPattern;
|
|
87
|
+
static matchDomain(domain: string, pattern: DomainPattern): Record<string, string> | null;
|
|
88
|
+
static extractSubdomainInfo(domain: string): SubdomainRouteInfo;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Domain group for organizing routes by domain patterns
|
|
92
|
+
*/
|
|
93
|
+
export declare class DomainGroup {
|
|
94
|
+
private routes: Array<{
|
|
95
|
+
method: string
|
|
96
|
+
path: string
|
|
97
|
+
handler: RouteHandler
|
|
98
|
+
middleware: MiddlewareHandler[]
|
|
99
|
+
name?: string
|
|
100
|
+
}>;
|
|
101
|
+
public pattern: DomainPattern;
|
|
102
|
+
public config: DomainGroupConfig;
|
|
103
|
+
constructor(pattern: DomainPattern, config: DomainGroupConfig);
|
|
104
|
+
addRoute(method: string, path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
105
|
+
getRoutes(): Array<{ method: string, path: string, handler: RouteHandler, middleware: MiddlewareHandler[] }>;
|
|
106
|
+
matchesDomain(domain: string): Record<string, string> | null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Subdomain router for managing domain-based routing
|
|
110
|
+
*/
|
|
111
|
+
export declare class SubdomainRouter {
|
|
112
|
+
private domainGroups: DomainGroup[];
|
|
113
|
+
private wildcardGroup?: DomainGroup;
|
|
114
|
+
domain(pattern: string): DomainGroupBuilder;
|
|
115
|
+
addDomainGroup(group: DomainGroup): void;
|
|
116
|
+
findDomainGroup(domain: string): { group: DomainGroup, parameters: Record<string, string> } | null;
|
|
117
|
+
getAllRoutes(): Array<{
|
|
118
|
+
domainPattern: string
|
|
119
|
+
domainParameters: Record<string, string>
|
|
120
|
+
method: string
|
|
121
|
+
path: string
|
|
122
|
+
handler: RouteHandler
|
|
123
|
+
middleware: MiddlewareHandler[]
|
|
124
|
+
name?: string
|
|
125
|
+
}>;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Domain group builder for fluent API
|
|
129
|
+
*/
|
|
130
|
+
export declare class DomainGroupBuilder {
|
|
131
|
+
private group: DomainGroup;
|
|
132
|
+
private router: SubdomainRouter;
|
|
133
|
+
private pattern: DomainPattern;
|
|
134
|
+
constructor(router: SubdomainRouter, pattern: DomainPattern);
|
|
135
|
+
middleware(middleware: MiddlewareHandler[]): this;
|
|
136
|
+
prefix(prefix: string): this;
|
|
137
|
+
name(name: string): this;
|
|
138
|
+
routes(callback: (builder: RouteGroupBuilder) => void): void;
|
|
139
|
+
get(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[]): void;
|
|
140
|
+
post(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[]): void;
|
|
141
|
+
put(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[]): void;
|
|
142
|
+
delete(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[]): void;
|
|
143
|
+
patch(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[]): void;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Route group builder for defining routes within domain groups
|
|
147
|
+
*/
|
|
148
|
+
export declare class RouteGroupBuilder {
|
|
149
|
+
private group: DomainGroup;
|
|
150
|
+
constructor(group: DomainGroup);
|
|
151
|
+
get(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
152
|
+
post(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
153
|
+
put(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
154
|
+
delete(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
155
|
+
patch(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
156
|
+
options(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
157
|
+
head(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
158
|
+
any(path: string, handler: RouteHandler, middleware?: MiddlewareHandler[], name?: string): void;
|
|
159
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type { SSEConfig, SSEEvent } from '../types';
|
|
2
|
+
export {
|
|
3
|
+
createSSEMiddleware,
|
|
4
|
+
SSEConnectionManager,
|
|
5
|
+
SSEHandler,
|
|
6
|
+
sseManager,
|
|
7
|
+
SSEUtils,
|
|
8
|
+
} from './sse-handler';
|
|
9
|
+
export {
|
|
10
|
+
type BaseStreamConfig,
|
|
11
|
+
FileStreamHandler,
|
|
12
|
+
StreamHandler,
|
|
13
|
+
StreamResponse,
|
|
14
|
+
StreamUtils,
|
|
15
|
+
} from './stream-handler';
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { SSEConfig, SSEEvent, } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* SSE middleware for route handlers
|
|
4
|
+
*/
|
|
5
|
+
export declare function createSSEMiddleware(config?: SSEConfig): void;
|
|
6
|
+
/**
|
|
7
|
+
* Global SSE connection manager instance
|
|
8
|
+
*/
|
|
9
|
+
export declare const sseManager: SSEConnectionManager;
|
|
10
|
+
/**
|
|
11
|
+
* SSE utility functions
|
|
12
|
+
*/
|
|
13
|
+
export declare const SSEUtils: {
|
|
14
|
+
/**
|
|
15
|
+
* Create a simple SSE route handler
|
|
16
|
+
*/
|
|
17
|
+
createHandler<TPath extends string>(
|
|
18
|
+
handler: SSERouteHandler<TPath>,
|
|
19
|
+
): (req: EnhancedRequest & { params: ExtractRouteParams<TPath> }) => unknown;
|
|
20
|
+
/**
|
|
21
|
+
* Create SSE event from object
|
|
22
|
+
*/
|
|
23
|
+
createEvent(data: any, options: Partial<SSEEvent> = {}): unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Validate SSE event
|
|
26
|
+
*/
|
|
27
|
+
validateEvent(event: SSEEvent): unknown;
|
|
28
|
+
/**
|
|
29
|
+
* Create periodic SSE sender
|
|
30
|
+
*/
|
|
31
|
+
createPeriodicSender(
|
|
32
|
+
sse: SSEHandler,
|
|
33
|
+
dataFn: () => any | Promise<any>,
|
|
34
|
+
intervalMs: number,
|
|
35
|
+
): () => unknown;
|
|
36
|
+
/**
|
|
37
|
+
* Create SSE from async iterable
|
|
38
|
+
*/
|
|
39
|
+
async fromAsyncIterable<T>(
|
|
40
|
+
sse: SSEHandler,
|
|
41
|
+
iterable: AsyncIterable<T>,
|
|
42
|
+
transform?: (item: T) => SSEEvent,
|
|
43
|
+
): unknown
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Server-Sent Events handler
|
|
47
|
+
*/
|
|
48
|
+
export declare class SSEHandler {
|
|
49
|
+
private config: Required<SSEConfig>;
|
|
50
|
+
private controller?: ReadableStreamDefaultController<Uint8Array>;
|
|
51
|
+
private encoder: any;
|
|
52
|
+
private isConnected: any;
|
|
53
|
+
private heartbeatInterval?: Timer;
|
|
54
|
+
constructor(config?: SSEConfig);
|
|
55
|
+
createStream(): Response;
|
|
56
|
+
send(event: SSEEvent): boolean;
|
|
57
|
+
sendBatch(events: SSEEvent[]): number;
|
|
58
|
+
message(data: any, id?: string): boolean;
|
|
59
|
+
event(type: string, data: any, id?: string): boolean;
|
|
60
|
+
comment(text: string): boolean;
|
|
61
|
+
close(): void;
|
|
62
|
+
private formatEvent(event: SSEEvent): string;
|
|
63
|
+
private setupHeartbeat(): void;
|
|
64
|
+
private clearHeartbeat(): void;
|
|
65
|
+
private sendRetryDelay(): void;
|
|
66
|
+
private disconnect(): void;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* SSE connection manager
|
|
70
|
+
*/
|
|
71
|
+
export declare class SSEConnectionManager {
|
|
72
|
+
private connections: any;
|
|
73
|
+
private connectionCounts: any;
|
|
74
|
+
createConnection(connectionId: string, config?: SSEConfig): SSEHandler;
|
|
75
|
+
getConnection(connectionId: string): SSEHandler | undefined;
|
|
76
|
+
closeConnection(connectionId: string): boolean;
|
|
77
|
+
broadcast(event: SSEEvent): number;
|
|
78
|
+
broadcastToConnections(connectionIds: string[], event: SSEEvent): number;
|
|
79
|
+
getStats(): { totalConnections: number, connectionsByUser: Record<string, number>, activeConnections: string[] };
|
|
80
|
+
cleanup(): void;
|
|
81
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { BunFile } from 'bun';
|
|
2
|
+
import type { BufferedStreamConfig, ContentType, DirectStreamConfig, EnhancedRequest, ResponseStatus, StreamingCompression, StreamingFormat, } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* Stream utilities
|
|
5
|
+
*/
|
|
6
|
+
export declare const StreamUtils: {
|
|
7
|
+
U>(
|
|
8
|
+
transformFn: (chunk: T) => U | Promise<U>,
|
|
9
|
+
): unknown;
|
|
10
|
+
/**
|
|
11
|
+
* Create a filter stream
|
|
12
|
+
*/
|
|
13
|
+
filter<T>(
|
|
14
|
+
filterFn: (chunk: T) => boolean | Promise<boolean>,
|
|
15
|
+
): unknown;
|
|
16
|
+
/**
|
|
17
|
+
* Create a batch stream
|
|
18
|
+
*/
|
|
19
|
+
batch<T>(batchSize: number): unknown;
|
|
20
|
+
/**
|
|
21
|
+
* Create a delay stream
|
|
22
|
+
*/
|
|
23
|
+
delay<T>(ms: number): unknown;
|
|
24
|
+
/**
|
|
25
|
+
* Create a rate limit stream
|
|
26
|
+
*/
|
|
27
|
+
rateLimit<T>(itemsPerSecond: number): unknown
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Base streaming configuration
|
|
31
|
+
*/
|
|
32
|
+
export declare interface BaseStreamConfig {
|
|
33
|
+
contentType?: ContentType
|
|
34
|
+
headers?: Record<string, string>
|
|
35
|
+
status?: ResponseStatus
|
|
36
|
+
bufferSize?: number
|
|
37
|
+
enableCompression?: boolean
|
|
38
|
+
compressionType?: StreamingCompression
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Stream response wrapper
|
|
42
|
+
*/
|
|
43
|
+
export declare class StreamResponse {
|
|
44
|
+
private stream: ReadableStream;
|
|
45
|
+
private headers: Headers;
|
|
46
|
+
private status: ResponseStatus;
|
|
47
|
+
constructor(stream: ReadableStream, options?: {
|
|
48
|
+
headers?: Headers | Record<string, string>
|
|
49
|
+
status?: ResponseStatus
|
|
50
|
+
contentType?: ContentType
|
|
51
|
+
});
|
|
52
|
+
toResponse(): Response;
|
|
53
|
+
addHeader(name: string, value: string): this;
|
|
54
|
+
setContentType(contentType: ContentType): this;
|
|
55
|
+
setStatus(status: ResponseStatus): this;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Generic streaming handler
|
|
59
|
+
*/
|
|
60
|
+
export declare class StreamHandler {
|
|
61
|
+
static direct<T>(data: T[] | AsyncIterable<T>, config?: DirectStreamConfig): StreamResponse;
|
|
62
|
+
static buffered<T>(data: T[] | AsyncIterable<T>, config?: BufferedStreamConfig): StreamResponse;
|
|
63
|
+
static json<T>(data: T[] | AsyncIterable<T>, config?: BaseStreamConfig): StreamResponse;
|
|
64
|
+
static text(data: string[] | AsyncIterable<string>, config?: BaseStreamConfig): StreamResponse;
|
|
65
|
+
static csv<T extends Record<string, any>>(data: T[] | AsyncIterable<T>, config?: BaseStreamConfig & { csvHeaders?: string[] }): StreamResponse;
|
|
66
|
+
static private formatData<T>(data: T, format: StreamingFormat): string;
|
|
67
|
+
static private objectToCsvRow(obj: Record<string, any>, headers?: string[]): string;
|
|
68
|
+
static private getContentTypeForFormat(format: StreamingFormat): ContentType;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* File streaming utilities
|
|
72
|
+
*/
|
|
73
|
+
export declare class FileStreamHandler {
|
|
74
|
+
static streamFile(filePath: string, request: EnhancedRequest, options?: {
|
|
75
|
+
contentType?: ContentType
|
|
76
|
+
enableRanges?: boolean
|
|
77
|
+
chunkSize?: number
|
|
78
|
+
}): Promise<Response>;
|
|
79
|
+
static private handleRangeRequest(file: BunFile, rangeHeader: string, contentType?: ContentType): Promise<Response>;
|
|
80
|
+
static private parseRangeHeader(rangeHeader: string, fileSize: number): [number, number][] | null;
|
|
81
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { EnhancedRequest } from '../types';
|
|
2
|
+
import type { JWTTestOptions, SessionTestOptions, TestUser } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Factory functions
|
|
5
|
+
*/
|
|
6
|
+
export declare function createAuthTester(request?: EnhancedRequest): AuthTester;
|
|
7
|
+
export declare function createJWTTester(options?: JWTTestOptions): JWTTester;
|
|
8
|
+
export declare function createSessionTester(options?: SessionTestOptions): SessionTester;
|
|
9
|
+
/**
|
|
10
|
+
* Authentication mock factories
|
|
11
|
+
*/
|
|
12
|
+
export declare const authMocks: {
|
|
13
|
+
/**
|
|
14
|
+
* Create a mock user
|
|
15
|
+
*/
|
|
16
|
+
user: (overrides: Partial<TestUser> = {}) => any;
|
|
17
|
+
/**
|
|
18
|
+
* Create an admin user
|
|
19
|
+
*/
|
|
20
|
+
adminUser: (overrides: Partial<TestUser> = {}) => any;
|
|
21
|
+
/**
|
|
22
|
+
* Create a guest user
|
|
23
|
+
*/
|
|
24
|
+
guestUser: () => any;
|
|
25
|
+
/**
|
|
26
|
+
* Mock authentication middleware
|
|
27
|
+
*/
|
|
28
|
+
authMiddleware: (user?: TestUser) => any;
|
|
29
|
+
/**
|
|
30
|
+
* Mock role-based middleware
|
|
31
|
+
*/
|
|
32
|
+
roleMiddleware: (requiredRoles: string[]) => any;
|
|
33
|
+
/**
|
|
34
|
+
* Mock permission-based middleware
|
|
35
|
+
*/
|
|
36
|
+
permissionMiddleware: (requiredPermissions: string[]) => any
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Helper functions
|
|
40
|
+
*/
|
|
41
|
+
export declare const authHelpers: {
|
|
42
|
+
/**
|
|
43
|
+
* Create an authenticated request
|
|
44
|
+
*/
|
|
45
|
+
authenticatedRequest: (user: TestUser, request?: EnhancedRequest) => any;
|
|
46
|
+
/**
|
|
47
|
+
* Create a request with JWT
|
|
48
|
+
*/
|
|
49
|
+
jwtRequest: (payload: Record<string, any> = {}, request?: EnhancedRequest) => any;
|
|
50
|
+
/**
|
|
51
|
+
* Create a request with session
|
|
52
|
+
*/
|
|
53
|
+
sessionRequest: (sessionData: Record<string, any> = {}, request?: EnhancedRequest) => any;
|
|
54
|
+
/**
|
|
55
|
+
* Verify JWT token structure
|
|
56
|
+
*/
|
|
57
|
+
verifyJWTStructure: (token: string) => any;
|
|
58
|
+
/**
|
|
59
|
+
* Extract payload from JWT (for testing)
|
|
60
|
+
*/
|
|
61
|
+
extractJWTPayload: (token: string) => any
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Authentication testing utilities
|
|
65
|
+
*/
|
|
66
|
+
export declare class AuthTester {
|
|
67
|
+
private request: EnhancedRequest;
|
|
68
|
+
constructor(request?: EnhancedRequest);
|
|
69
|
+
as(user: TestUser): AuthTester;
|
|
70
|
+
withJWT(token: string, type?: 'Bearer' | 'Basic'): AuthTester;
|
|
71
|
+
withSession(sessionData: any): AuthTester;
|
|
72
|
+
withApiKey(apiKey: string, headerName?: string): AuthTester;
|
|
73
|
+
withBasicAuth(username: string, password: string): AuthTester;
|
|
74
|
+
withAuthCookies(cookies: Record<string, string>): AuthTester;
|
|
75
|
+
unauthenticated(): AuthTester;
|
|
76
|
+
getRequest(): EnhancedRequest;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* JWT testing utilities
|
|
80
|
+
*/
|
|
81
|
+
export declare class JWTTester {
|
|
82
|
+
private options: JWTTestOptions;
|
|
83
|
+
constructor(options?: JWTTestOptions);
|
|
84
|
+
generateToken(payload?: Record<string, any>): string;
|
|
85
|
+
generateExpiredToken(payload?: Record<string, any>): string;
|
|
86
|
+
generateInvalidToken(): string;
|
|
87
|
+
decodeToken(token: string): { header: any, payload: any, signature: string };
|
|
88
|
+
private parseExpiresIn(expiresIn: string): number;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Session testing utilities
|
|
92
|
+
*/
|
|
93
|
+
export declare class SessionTester {
|
|
94
|
+
private options: SessionTestOptions;
|
|
95
|
+
constructor(options?: SessionTestOptions);
|
|
96
|
+
createSession(data?: Record<string, any>): {
|
|
97
|
+
sessionId: string
|
|
98
|
+
data: Record<string, any>
|
|
99
|
+
cookie: string
|
|
100
|
+
};
|
|
101
|
+
generateSessionCookie(name?: string): string;
|
|
102
|
+
createExpiredSession(): {
|
|
103
|
+
sessionId: string
|
|
104
|
+
data: Record<string, any>
|
|
105
|
+
cookie: string
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { EnhancedRequest, UploadedFile } from '../types';
|
|
2
|
+
import type { TestFile } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Factory functions
|
|
5
|
+
*/
|
|
6
|
+
export declare function createFileUploadTester(request?: EnhancedRequest): FileUploadTester;
|
|
7
|
+
/**
|
|
8
|
+
* File upload mock factories
|
|
9
|
+
*/
|
|
10
|
+
export declare const fileUploadMocks: {
|
|
11
|
+
validationMiddleware: (options?: ValidationMiddlewareOptions) => MiddlewareFn
|
|
12
|
+
imageUploadMiddleware: (options?: ImageUploadMiddlewareOptions) => MiddlewareFn
|
|
13
|
+
storageService: FileStorageService
|
|
14
|
+
virusScanner: VirusScanner
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* File upload test scenarios
|
|
18
|
+
*/
|
|
19
|
+
export declare const fileUploadScenarios: {
|
|
20
|
+
/**
|
|
21
|
+
* Test successful single file upload
|
|
22
|
+
*/
|
|
23
|
+
singleFileUpload: () => any;
|
|
24
|
+
/**
|
|
25
|
+
* Test multiple file upload
|
|
26
|
+
*/
|
|
27
|
+
multipleFileUpload: () => any;
|
|
28
|
+
/**
|
|
29
|
+
* Test file size limit exceeded
|
|
30
|
+
*/
|
|
31
|
+
fileSizeExceeded: () => any;
|
|
32
|
+
/**
|
|
33
|
+
* Test invalid file type
|
|
34
|
+
*/
|
|
35
|
+
invalidFileType: () => any;
|
|
36
|
+
/**
|
|
37
|
+
* Test empty file upload
|
|
38
|
+
*/
|
|
39
|
+
emptyFileUpload: () => any;
|
|
40
|
+
/**
|
|
41
|
+
* Test file with malicious filename
|
|
42
|
+
*/
|
|
43
|
+
maliciousFilename: () => any
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* File upload assertion helpers
|
|
47
|
+
*/
|
|
48
|
+
export declare const fileUploadAssertions: {
|
|
49
|
+
/**
|
|
50
|
+
* Assert that request has files
|
|
51
|
+
*/
|
|
52
|
+
hasFiles: (request: EnhancedRequest, count?: number) => any;
|
|
53
|
+
/**
|
|
54
|
+
* Assert that request has file with specific field name
|
|
55
|
+
*/
|
|
56
|
+
hasFileWithField: (request: EnhancedRequest, fieldName: string) => any;
|
|
57
|
+
/**
|
|
58
|
+
* Assert file properties
|
|
59
|
+
*/
|
|
60
|
+
fileMatches: (file: UploadedFile, expected: Partial<UploadedFile>) => any;
|
|
61
|
+
/**
|
|
62
|
+
* Assert file size is within limits
|
|
63
|
+
*/
|
|
64
|
+
fileSizeWithinLimit: (file: UploadedFile, maxSize: number) => any;
|
|
65
|
+
/**
|
|
66
|
+
* Assert file type is allowed
|
|
67
|
+
*/
|
|
68
|
+
fileTypeAllowed: (file: UploadedFile, allowedTypes: string[]) => any
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* Helper to create test files
|
|
72
|
+
*/
|
|
73
|
+
export declare const testFiles: {
|
|
74
|
+
image: (fieldName: string?, filename: string?) => any;
|
|
75
|
+
document: (fieldName: string?, filename: string?) => any;
|
|
76
|
+
text: (fieldName: string?, filename: string?, content: string?) => any;
|
|
77
|
+
large: (fieldName: string?, sizeInMB: number?) => any;
|
|
78
|
+
malicious: (fieldName: string?) => any
|
|
79
|
+
};
|
|
80
|
+
/** File storage service interface */
|
|
81
|
+
export declare interface FileStorageService {
|
|
82
|
+
store: (file: UploadedFile, path?: string) => Promise<string>
|
|
83
|
+
delete: (path: string) => Promise<void>
|
|
84
|
+
exists: (path: string) => Promise<boolean>
|
|
85
|
+
getUrl: (path: string) => string
|
|
86
|
+
}
|
|
87
|
+
/** Virus scanner interface */
|
|
88
|
+
export declare interface VirusScanResult {
|
|
89
|
+
clean: boolean
|
|
90
|
+
threat?: string
|
|
91
|
+
}
|
|
92
|
+
export declare interface VirusScanner {
|
|
93
|
+
scan: (file: UploadedFile) => Promise<VirusScanResult>
|
|
94
|
+
}
|
|
95
|
+
/** Validation middleware options */
|
|
96
|
+
export declare interface ValidationMiddlewareOptions {
|
|
97
|
+
maxSize?: number
|
|
98
|
+
allowedTypes?: string[]
|
|
99
|
+
maxFiles?: number
|
|
100
|
+
}
|
|
101
|
+
/** Image upload middleware options */
|
|
102
|
+
export declare interface ImageUploadMiddlewareOptions {
|
|
103
|
+
maxSize?: number
|
|
104
|
+
maxWidth?: number
|
|
105
|
+
maxHeight?: number
|
|
106
|
+
}
|
|
107
|
+
/** Middleware function type */
|
|
108
|
+
export type MiddlewareFn = (req: EnhancedRequest, next: () => Promise<Response>) => Promise<Response>
|
|
109
|
+
/**
|
|
110
|
+
* File upload testing utilities
|
|
111
|
+
*/
|
|
112
|
+
export declare class FileUploadTester {
|
|
113
|
+
private request: EnhancedRequest;
|
|
114
|
+
private files: UploadedFile[];
|
|
115
|
+
constructor(request?: EnhancedRequest);
|
|
116
|
+
addFile(testFile: TestFile): FileUploadTester;
|
|
117
|
+
addFiles(testFiles: TestFile[]): FileUploadTester;
|
|
118
|
+
addImage(fieldName: string, filename?: string, size?: number): FileUploadTester;
|
|
119
|
+
addDocument(fieldName: string, filename?: string, content?: string): FileUploadTester;
|
|
120
|
+
addTextFile(fieldName: string, filename?: string, content?: string): FileUploadTester;
|
|
121
|
+
addLargeFile(fieldName: string, sizeInMB?: number): FileUploadTester;
|
|
122
|
+
addInvalidFile(fieldName: string, filename?: string): FileUploadTester;
|
|
123
|
+
withFormData(data: Record<string, any>): FileUploadTester;
|
|
124
|
+
getRequest(): EnhancedRequest;
|
|
125
|
+
getFiles(): UploadedFile[];
|
|
126
|
+
clearFiles(): FileUploadTester;
|
|
127
|
+
private detectMimeType(filename: string): string;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* File validation testing utilities
|
|
131
|
+
*/
|
|
132
|
+
export declare class FileValidationTester {
|
|
133
|
+
static testFileSize(file: UploadedFile, maxSize: number): boolean;
|
|
134
|
+
static testFileType(file: UploadedFile, allowedTypes: string[]): boolean;
|
|
135
|
+
static testFileExtension(file: UploadedFile, allowedExtensions: string[]): boolean;
|
|
136
|
+
static testFilename(file: UploadedFile): boolean;
|
|
137
|
+
static testImageFile(file: UploadedFile): boolean;
|
|
138
|
+
static testDocumentFile(file: UploadedFile): boolean;
|
|
139
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './auth-testing';
|
|
2
|
+
export * from './file-upload-testing';
|
|
3
|
+
export * from './middleware-testing';
|
|
4
|
+
export * from './model-binding-testing';
|
|
5
|
+
export * from './performance-testing';
|
|
6
|
+
export * from './test-client';
|
|
7
|
+
export * from './test-request';
|
|
8
|
+
export * from './test-response';
|
|
9
|
+
export * from './types';
|
|
10
|
+
export * from './websocket-testing';
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { mock, spyOn } from 'bun:test';
|
|
2
|
+
import type { EnhancedRequest, MiddlewareHandler, NextFunction } from '../types';
|
|
3
|
+
import type { MiddlewareTestContext } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Create a middleware tester
|
|
6
|
+
*/
|
|
7
|
+
export declare function testMiddleware(middleware: MiddlewareHandler): MiddlewareTester;
|
|
8
|
+
/**
|
|
9
|
+
* Create a middleware chain tester
|
|
10
|
+
*/
|
|
11
|
+
export declare function testMiddlewareChain(middlewares: MiddlewareHandler[]): MiddlewareChainTester;
|
|
12
|
+
/**
|
|
13
|
+
* Spy on middleware execution
|
|
14
|
+
*/
|
|
15
|
+
export declare function spyOnMiddleware(middleware: MiddlewareHandler): ReturnType<typeof spyOn>;
|
|
16
|
+
/**
|
|
17
|
+
* Create a middleware spy that tracks calls
|
|
18
|
+
*/
|
|
19
|
+
export declare function createMiddlewareSpy(): {
|
|
20
|
+
middleware: MiddlewareHandler
|
|
21
|
+
spy: ReturnType<typeof mock>
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Test middleware performance
|
|
25
|
+
*/
|
|
26
|
+
export declare function benchmarkMiddleware(middleware: MiddlewareHandler, iterations?: number): Promise<{
|
|
27
|
+
averageTime: number
|
|
28
|
+
minTime: number
|
|
29
|
+
maxTime: number
|
|
30
|
+
totalTime: number
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* Mock middleware factory
|
|
34
|
+
*/
|
|
35
|
+
export declare const mockMiddleware: {
|
|
36
|
+
/**
|
|
37
|
+
* Create a middleware that always calls next
|
|
38
|
+
*/
|
|
39
|
+
passthrough: () => any;
|
|
40
|
+
/**
|
|
41
|
+
* Create a middleware that returns a response without calling next
|
|
42
|
+
*/
|
|
43
|
+
response: (status: number?, body?: any) => any;
|
|
44
|
+
/**
|
|
45
|
+
* Create a middleware that throws an error
|
|
46
|
+
*/
|
|
47
|
+
error: (message: string?) => any;
|
|
48
|
+
/**
|
|
49
|
+
* Create a middleware that modifies the request
|
|
50
|
+
*/
|
|
51
|
+
modifyRequest: (modifier: (req: EnhancedRequest) => void) => any;
|
|
52
|
+
/**
|
|
53
|
+
* Create a middleware that adds context
|
|
54
|
+
*/
|
|
55
|
+
addContext: (key: string, value: any) => any;
|
|
56
|
+
/**
|
|
57
|
+
* Create an authentication middleware mock
|
|
58
|
+
*/
|
|
59
|
+
auth: (user?: any) => any;
|
|
60
|
+
/**
|
|
61
|
+
* Create a rate limiting middleware mock
|
|
62
|
+
*/
|
|
63
|
+
rateLimit: (shouldLimit: boolean?) => any;
|
|
64
|
+
/**
|
|
65
|
+
* Create a CORS middleware mock
|
|
66
|
+
*/
|
|
67
|
+
cors: (origin: string?) => any
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Middleware testing utilities
|
|
71
|
+
*/
|
|
72
|
+
export declare class MiddlewareTester {
|
|
73
|
+
private middleware: MiddlewareHandler;
|
|
74
|
+
private context: MiddlewareTestContext;
|
|
75
|
+
constructor(middleware: MiddlewareHandler);
|
|
76
|
+
private createContext(): MiddlewareTestContext;
|
|
77
|
+
withRequest(request: EnhancedRequest): MiddlewareTester;
|
|
78
|
+
withNext(nextFn: NextFunction): MiddlewareTester;
|
|
79
|
+
withError(error: Error): MiddlewareTester;
|
|
80
|
+
execute(): Promise<Response | null>;
|
|
81
|
+
expectNextCalled(times?: number): MiddlewareTester;
|
|
82
|
+
expectNextNotCalled(): MiddlewareTester;
|
|
83
|
+
expectError(errorMessage?: string): MiddlewareTester;
|
|
84
|
+
getContext(): MiddlewareTestContext;
|
|
85
|
+
reset(): MiddlewareTester;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Middleware chain tester
|
|
89
|
+
*/
|
|
90
|
+
export declare class MiddlewareChainTester {
|
|
91
|
+
private middlewares: MiddlewareHandler[];
|
|
92
|
+
private request: EnhancedRequest;
|
|
93
|
+
private finalHandler: () => Promise<Response>;
|
|
94
|
+
constructor(middlewares: MiddlewareHandler[]);
|
|
95
|
+
withRequest(request: EnhancedRequest): MiddlewareChainTester;
|
|
96
|
+
withFinalHandler(handler: () => Promise<Response>): MiddlewareChainTester;
|
|
97
|
+
execute(): Promise<Response | null>;
|
|
98
|
+
getExecutionOrder(): number[];
|
|
99
|
+
}
|