@asenajs/hono-adapter 1.1.0 → 1.3.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/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/HonoAdapter.d.ts +211 -5
- package/dist/lib/HonoAdapter.js +622 -56
- package/dist/lib/HonoAdapter.js.map +1 -1
- package/dist/lib/HonoContextWrapper.d.ts +1 -2
- package/dist/lib/HonoWebsocketAdapter.d.ts +42 -4
- package/dist/lib/HonoWebsocketAdapter.js +172 -12
- package/dist/lib/HonoWebsocketAdapter.js.map +1 -1
- package/dist/lib/defaults/index.d.ts +2 -0
- package/dist/lib/defaults/index.js +2 -0
- package/dist/lib/defaults/index.js.map +1 -1
- package/dist/lib/middlewares/CorsMiddleware.d.ts +148 -0
- package/dist/lib/middlewares/CorsMiddleware.js +169 -0
- package/dist/lib/middlewares/CorsMiddleware.js.map +1 -0
- package/dist/lib/middlewares/RateLimiterMiddleware.d.ts +329 -0
- package/dist/lib/middlewares/RateLimiterMiddleware.js +301 -0
- package/dist/lib/middlewares/RateLimiterMiddleware.js.map +1 -0
- package/dist/lib/types/HonoHandler.d.ts +8 -0
- package/dist/lib/types/StaticServeExtras.d.ts +2 -0
- package/dist/lib/utils/middlewareParser.d.ts +6 -0
- package/dist/lib/utils/middlewareParser.js +28 -2
- package/dist/lib/utils/middlewareParser.js.map +1 -1
- package/package.json +13 -10
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,4 +3,6 @@ export * from './lib/HonoWebsocketAdapter';
|
|
|
3
3
|
export * from './lib/types';
|
|
4
4
|
export * from './lib/defaults';
|
|
5
5
|
export * from './lib/utils/createHonoAdapter';
|
|
6
|
+
// Re-export Hono's HTTPException for user convenience in middlewares
|
|
7
|
+
export { HTTPException } from 'hono/http-exception';
|
|
6
8
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,+BAA+B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,+BAA+B,CAAC;AAE9C,qEAAqE;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -2,27 +2,233 @@ import { Hono } from 'hono';
|
|
|
2
2
|
import type { Server } from 'bun';
|
|
3
3
|
import type { AsenaWebsocketAdapter, WebsocketRouteParams } from '@asenajs/asena/adapter';
|
|
4
4
|
import { AsenaAdapter, type AsenaServeOptions, type BaseMiddleware, type RouteParams } from '@asenajs/asena/adapter';
|
|
5
|
+
import type { GlobalMiddlewareRouteConfig } from '@asenajs/asena/server/config';
|
|
5
6
|
import type { HonoErrorHandler, StaticServeExtras } from './types';
|
|
6
7
|
import { type ServerLogger } from '@asenajs/asena/logger';
|
|
7
8
|
import type { ValidationSchema } from './defaults';
|
|
8
9
|
import type { Context as HonoAdapterContext } from './defaults/Context';
|
|
10
|
+
import type { WebSocketData } from '@asenajs/asena/web-socket';
|
|
9
11
|
export declare class HonoAdapter extends AsenaAdapter<HonoAdapterContext, ValidationSchema> {
|
|
10
12
|
name: string;
|
|
11
13
|
app: Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
|
|
12
14
|
private server;
|
|
13
15
|
private options;
|
|
16
|
+
private routeQueue;
|
|
17
|
+
/**
|
|
18
|
+
* WebSocket route queue for deferred registration
|
|
19
|
+
* WebSocket routes are queued during initialization and registered when server starts
|
|
20
|
+
*/
|
|
21
|
+
private wsRouteQueue;
|
|
22
|
+
/**
|
|
23
|
+
* Global middlewares with optional path filtering configuration
|
|
24
|
+
* Middlewares are stored with their pattern config and applied during route registration
|
|
25
|
+
*/
|
|
26
|
+
private globalMiddlewares;
|
|
27
|
+
private routesRegistered;
|
|
14
28
|
private readonly methodMap;
|
|
15
29
|
constructor(logger: ServerLogger, websocketAdapter?: AsenaWebsocketAdapter);
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Registers a global middleware with optional path filtering
|
|
32
|
+
*
|
|
33
|
+
* Middlewares are stored and applied during route registration based on include/exclude patterns.
|
|
34
|
+
* This enables zero-runtime-overhead pattern matching.
|
|
35
|
+
*
|
|
36
|
+
* @param middleware - The middleware to register globally
|
|
37
|
+
* @param config - Optional path filtering configuration with include/exclude patterns
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* // Apply to all routes
|
|
42
|
+
* adapter.use(authMiddleware);
|
|
43
|
+
*
|
|
44
|
+
* // Apply only to specific patterns
|
|
45
|
+
* adapter.use(authMiddleware, {
|
|
46
|
+
* include: ['/api/*', '/admin/*'],
|
|
47
|
+
* exclude: ['/api/health']
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Apply to all except specific patterns
|
|
51
|
+
* adapter.use(rateLimitMiddleware, {
|
|
52
|
+
* exclude: ['/health', '/metrics']
|
|
53
|
+
* });
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
use(middleware: BaseMiddleware<HonoAdapterContext>, config?: GlobalMiddlewareRouteConfig): void;
|
|
57
|
+
registerRoute(params: RouteParams<HonoAdapterContext, ValidationSchema, StaticServeExtras>): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Registers a WebSocket route (deferred registration)
|
|
60
|
+
*
|
|
61
|
+
* Routes are queued and will be registered when the server starts.
|
|
62
|
+
* This allows for controller-based logging and optimization.
|
|
63
|
+
*
|
|
64
|
+
* @param params - WebSocket route parameters
|
|
65
|
+
*/
|
|
66
|
+
registerWebsocketRoute(params: WebsocketRouteParams<HonoAdapterContext>): Promise<void> | void;
|
|
67
|
+
start(): Promise<Server<WebSocketData<any>>>;
|
|
68
|
+
/**
|
|
69
|
+
* Registers global error handler with enhanced error handling capabilities
|
|
70
|
+
*
|
|
71
|
+
* Supports Hono's HTTPException for proper status code handling:
|
|
72
|
+
* - HTTPException instances are passed to user handler for custom handling
|
|
73
|
+
* - If user handler returns a response, it's used
|
|
74
|
+
* - Otherwise, HTTPException's default response is returned
|
|
75
|
+
* - Other errors follow normal error handling flow
|
|
76
|
+
*
|
|
77
|
+
* @param errorHandler - Custom error handler function
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* adapter.onError((error, context) => {
|
|
82
|
+
* // HTTPException handling (optional custom behavior)
|
|
83
|
+
* if (error instanceof HTTPException) {
|
|
84
|
+
* // Add custom logging or return custom response
|
|
85
|
+
* return context.send({ custom: 'response' }, error.status);
|
|
86
|
+
* }
|
|
87
|
+
*
|
|
88
|
+
* // Other errors
|
|
89
|
+
* return context.send({ error: 'Internal error' }, 500);
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
20
93
|
onError(errorHandler: HonoErrorHandler): void;
|
|
21
94
|
serveOptions(options: () => Promise<AsenaServeOptions> | AsenaServeOptions): Promise<void>;
|
|
22
95
|
setPort(port: number): void;
|
|
23
96
|
private prepareMiddlewares;
|
|
24
97
|
private prepareHandler;
|
|
98
|
+
/**
|
|
99
|
+
* Prepares static serve options with enhanced features
|
|
100
|
+
* @param staticServe - Base static serve parameters
|
|
101
|
+
* @returns Configured static serve options for Hono
|
|
102
|
+
*/
|
|
25
103
|
private prepareStaticServeOptions;
|
|
104
|
+
/**
|
|
105
|
+
* Prepares validation middleware with enhanced error handling
|
|
106
|
+
* @param baseValidator - The validator to prepare
|
|
107
|
+
* @returns Array of Hono middleware handlers for validation
|
|
108
|
+
*/
|
|
26
109
|
private prepareValidator;
|
|
27
|
-
|
|
110
|
+
/**
|
|
111
|
+
* Gets global middlewares that should be applied to a specific path
|
|
112
|
+
*
|
|
113
|
+
* Filters global middlewares based on their include/exclude patterns.
|
|
114
|
+
* Pattern matching happens once during route registration (zero runtime overhead).
|
|
115
|
+
*
|
|
116
|
+
* @param path - The route path to check against middleware patterns
|
|
117
|
+
* @returns Array of middlewares that should be applied to this path
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* // If global middlewares are:
|
|
122
|
+
* // - authMiddleware with { include: ['/api/*'], exclude: ['/api/health'] }
|
|
123
|
+
* // - rateLimitMiddleware with no config
|
|
124
|
+
*
|
|
125
|
+
* getGlobalMiddlewaresForPath('/api/users') // => [authMiddleware, rateLimitMiddleware]
|
|
126
|
+
* getGlobalMiddlewaresForPath('/api/health') // => [rateLimitMiddleware]
|
|
127
|
+
* getGlobalMiddlewaresForPath('/public/file') // => [rateLimitMiddleware]
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
private getGlobalMiddlewaresForPath;
|
|
131
|
+
/**
|
|
132
|
+
* Extracts base path from a route path
|
|
133
|
+
* Examples:
|
|
134
|
+
* - "/api/users/:id" -> "/api/users"
|
|
135
|
+
* - "/users" -> "/users"
|
|
136
|
+
* - "/" -> "/"
|
|
137
|
+
*/
|
|
138
|
+
private extractBasePath;
|
|
139
|
+
/**
|
|
140
|
+
* Groups routes by their base path
|
|
141
|
+
* Returns a map of base path -> routes with that base path
|
|
142
|
+
*/
|
|
143
|
+
private groupRoutesByBasePath;
|
|
144
|
+
/**
|
|
145
|
+
* Finds common middlewares across all routes in a group
|
|
146
|
+
* Returns middlewares that appear in ALL routes
|
|
147
|
+
*/
|
|
148
|
+
private extractCommonMiddlewares;
|
|
149
|
+
/**
|
|
150
|
+
* Registers routes to Hono app with optimization
|
|
151
|
+
* Groups routes by base path and applies common middlewares at group level
|
|
152
|
+
* Also registers WebSocket routes
|
|
153
|
+
*/
|
|
154
|
+
private optimizeAndRegisterRoutes;
|
|
155
|
+
/**
|
|
156
|
+
* Logs a summary of registered controllers
|
|
157
|
+
*/
|
|
158
|
+
private logControllerSummary;
|
|
159
|
+
/**
|
|
160
|
+
* Registers a WebSocket route directly
|
|
161
|
+
* Converts HTTP GET route to WebSocket upgrade endpoint
|
|
162
|
+
*/
|
|
163
|
+
private registerWebsocketRouteDirect;
|
|
164
|
+
/**
|
|
165
|
+
* Registers a group of routes with common middlewares at base path level
|
|
166
|
+
*/
|
|
167
|
+
private registerRouteGroup;
|
|
168
|
+
/**
|
|
169
|
+
* Registers a single route directly without grouping
|
|
170
|
+
*/
|
|
171
|
+
private registerRouteDirect;
|
|
172
|
+
/**
|
|
173
|
+
* Groups HTTP routes by controller name
|
|
174
|
+
*
|
|
175
|
+
* Creates a map of controller names to their routes for organized logging.
|
|
176
|
+
* Each group contains the controller's base path and all its routes.
|
|
177
|
+
*
|
|
178
|
+
* @returns Map of controller names to route groups
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```typescript
|
|
182
|
+
* groupRoutesByController()
|
|
183
|
+
* // => Map {
|
|
184
|
+
* // 'UserController' => {
|
|
185
|
+
* // basePath: '/users',
|
|
186
|
+
* // routes: [{ method: 'GET', path: '/users' }, { method: 'POST', path: '/users' }]
|
|
187
|
+
* // }
|
|
188
|
+
* // }
|
|
189
|
+
* ```
|
|
190
|
+
*/
|
|
191
|
+
private groupRoutesByController;
|
|
192
|
+
/**
|
|
193
|
+
* Groups WebSocket routes by controller name
|
|
194
|
+
*
|
|
195
|
+
* Creates a map of controller names to their WebSocket routes for organized logging.
|
|
196
|
+
*
|
|
197
|
+
* @returns Map of controller names to WebSocket route groups
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* groupWebSocketRoutesByController()
|
|
202
|
+
* // => Map {
|
|
203
|
+
* // 'ChatController' => {
|
|
204
|
+
* // basePath: '/chat',
|
|
205
|
+
* // routes: [{ path: '/chat' }]
|
|
206
|
+
* // }
|
|
207
|
+
* // }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
private groupWebSocketRoutesByController;
|
|
211
|
+
/**
|
|
212
|
+
* Builds controller-based log output
|
|
213
|
+
*
|
|
214
|
+
* Creates a formatted string showing all routes grouped by controller,
|
|
215
|
+
* with HTTP and WebSocket routes clearly organized.
|
|
216
|
+
*
|
|
217
|
+
* @returns Formatted log string
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* Output format:
|
|
221
|
+
* ```
|
|
222
|
+
* Registered routes:
|
|
223
|
+
*
|
|
224
|
+
* UserController (/users):
|
|
225
|
+
* GET /users
|
|
226
|
+
* GET /users/:id
|
|
227
|
+
* POST /users
|
|
228
|
+
*
|
|
229
|
+
* ChatController (/chat):
|
|
230
|
+
* WS /chat
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
private buildControllerBasedLog;
|
|
28
234
|
}
|