@asenajs/hono-adapter 1.7.0 → 2.0.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/README.md +8 -1
- package/dist/lib/HonoAdapter.d.ts +82 -48
- package/dist/lib/HonoAdapter.js +192 -198
- package/dist/lib/HonoAdapter.js.map +1 -1
- package/dist/lib/HonoContextWrapper.js +9 -2
- package/dist/lib/HonoContextWrapper.js.map +1 -1
- package/dist/lib/HonoWebsocketAdapter.js +5 -5
- package/dist/lib/HonoWebsocketAdapter.js.map +1 -1
- package/dist/lib/defaults/ConfigService.d.ts +35 -0
- package/dist/lib/defaults/ConfigService.js +22 -0
- package/dist/lib/defaults/ConfigService.js.map +1 -1
- package/dist/lib/errors.d.ts +36 -3
- package/dist/lib/errors.js +53 -1
- package/dist/lib/errors.js.map +1 -1
- package/dist/lib/middlewares/RateLimiterMiddleware.js.map +1 -1
- package/dist/lib/types/HonoAdapterOptions.d.ts +14 -0
- package/dist/lib/types/HonoHandler.d.ts +2 -1
- package/dist/lib/utils/createHonoAdapter.js +1 -4
- package/dist/lib/utils/createHonoAdapter.js.map +1 -1
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# Asena Hono Adapter
|
|
2
2
|
|
|
3
|
+
[](https://github.com/AsenaJs/hono-adapter)
|
|
4
|
+
[](https://bun.sh)
|
|
5
|
+
|
|
3
6
|
HTTP and WebSocket adapter implementation based on Hono web framework for Asena.js.
|
|
4
7
|
|
|
5
8
|
## Features
|
|
@@ -112,7 +115,11 @@ describe("YourController", () => {
|
|
|
112
115
|
let baseUrl;
|
|
113
116
|
|
|
114
117
|
beforeEach(async () => {
|
|
115
|
-
|
|
118
|
+
// 10000-31999: below the kernel's ephemeral floor (net.ipv4.ip_local_port_range,
|
|
119
|
+
// 32768-60999). A server port drawn from that range collides with the outbound
|
|
120
|
+
// sockets the suite itself holds open - TIME_WAIT included - and Bun.serve then
|
|
121
|
+
// fails with EADDRINUSE, randomly, in whichever test happened to draw it.
|
|
122
|
+
const port = 10000 + Math.floor(Math.random() * 22000);
|
|
116
123
|
const [adapter, logger] = createHonoAdapter(new DefaultLogger());
|
|
117
124
|
const app = new AsenaServer(adapter).logger(logger).port(port);
|
|
118
125
|
|
|
@@ -3,7 +3,7 @@ import type { Server } from 'bun';
|
|
|
3
3
|
import type { WebsocketRouteParams } from '@asenajs/asena/adapter';
|
|
4
4
|
import { AsenaAdapter, type AsenaServeOptions, type AsenaStartOptions, type BaseMiddleware, type RouteParams } from '@asenajs/asena/adapter';
|
|
5
5
|
import type { GlobalMiddlewareRouteConfig } from '@asenajs/asena/server/config';
|
|
6
|
-
import type { HonoAdapterOptions, HonoErrorHandler, StaticServeExtras } from './types';
|
|
6
|
+
import type { HonoAdapterOptions, HonoErrorHandler, HonoNotFoundHandler, StaticServeExtras } from './types';
|
|
7
7
|
import { type ServerLogger } from '@asenajs/asena/logger';
|
|
8
8
|
import type { ValidationSchema } from './defaults';
|
|
9
9
|
import type { Context as HonoAdapterContext } from './defaults/Context';
|
|
@@ -13,14 +13,17 @@ export declare class HonoAdapter extends AsenaAdapter<HonoAdapterContext, Valida
|
|
|
13
13
|
name: string;
|
|
14
14
|
app: Hono;
|
|
15
15
|
private readonly _strict;
|
|
16
|
+
private readonly logErrors;
|
|
16
17
|
private server;
|
|
17
18
|
private options;
|
|
18
19
|
/**
|
|
19
|
-
* Whether the application registered an error handler.
|
|
20
|
-
*
|
|
21
|
-
*
|
|
20
|
+
* Whether the *application* registered an error handler. Read only by `start()`, to decide
|
|
21
|
+
* whether the framework's own default needs registering - Hono keeps a single `onError`, so
|
|
22
|
+
* registering both would replace the application's.
|
|
22
23
|
*/
|
|
23
24
|
private hasErrorHandler;
|
|
25
|
+
/** Whether the application registered an onNotFound hook (see start()) */
|
|
26
|
+
private hasNotFoundHandler;
|
|
24
27
|
/**
|
|
25
28
|
* Normalizes a route path for Hono compatibility.
|
|
26
29
|
* Strips trailing slash (except root '/') because Hono's strict:false
|
|
@@ -100,31 +103,76 @@ export declare class HonoAdapter extends AsenaAdapter<HonoAdapterContext, Valida
|
|
|
100
103
|
registerWebsocketRoute(params: WebsocketRouteParams<HonoAdapterContext>): Promise<void> | void;
|
|
101
104
|
start(options?: AsenaStartOptions): Promise<Server<WebSocketData<any>>>;
|
|
102
105
|
/**
|
|
103
|
-
* Registers
|
|
106
|
+
* Registers the handler for requests that match no route.
|
|
104
107
|
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
* - If user handler returns a response, it's used
|
|
108
|
-
* - Otherwise, HTTPException's default response is returned
|
|
109
|
-
* - Other errors follow normal error handling flow
|
|
108
|
+
* Separate from {@link onError}: an unmatched route is a routing outcome, not a thrown error,
|
|
109
|
+
* so the application's error handler never sees one and never has to discriminate.
|
|
110
110
|
*
|
|
111
|
-
* @param
|
|
111
|
+
* @param notFoundHandler - Handler that produces the 404 response
|
|
112
|
+
*/
|
|
113
|
+
onNotFound(notFoundHandler: HonoNotFoundHandler): void;
|
|
114
|
+
/**
|
|
115
|
+
* The 404 both adapters answer when no `onNotFound` is registered, and the log line that goes
|
|
116
|
+
* with it.
|
|
117
|
+
*
|
|
118
|
+
* Hono's own default is `text/plain`, so the same application used to produce a different
|
|
119
|
+
* body depending on which adapter it ran under - the specific complaint that started this
|
|
120
|
+
* work. Registered unconditionally in `start()` so the default holds even for an app with
|
|
121
|
+
* no config at all.
|
|
122
|
+
*
|
|
123
|
+
* `info`, not `warn`: a scanner walking /wp-admin, /.env and /phpmyadmin must not be able to
|
|
124
|
+
* fill the warning stream. Not `debug` either - a 404 nobody can see is how a mistyped route
|
|
125
|
+
* survives to production. Only reached when the framework is the one answering, so an
|
|
126
|
+
* application that shaped its own 404 gets no line from here.
|
|
127
|
+
*/
|
|
128
|
+
private defaultNotFoundResponse;
|
|
129
|
+
/**
|
|
130
|
+
* Registers the application's global error handler.
|
|
131
|
+
*
|
|
132
|
+
* Every error the application throws is offered to it first, `HTTPException` included, so an
|
|
133
|
+
* application can reshape its own 401/403/429 envelopes rather than only the 500s. Returning
|
|
134
|
+
* nothing means "not mine, use the default" - the framework then answers, and logs.
|
|
135
|
+
*
|
|
136
|
+
* @param errorHandler - Handler that produces the response for a thrown error
|
|
112
137
|
*
|
|
113
138
|
* @example
|
|
114
139
|
* ```typescript
|
|
115
140
|
* adapter.onError((error, context) => {
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* // Add custom logging or return custom response
|
|
119
|
-
* return context.send({ custom: 'response' }, error.status);
|
|
141
|
+
* if (isHttpException(error)) {
|
|
142
|
+
* return context.send({ error: error.message }, error.status);
|
|
120
143
|
* }
|
|
121
144
|
*
|
|
122
|
-
* // Other errors
|
|
123
145
|
* return context.send({ error: 'Internal error' }, 500);
|
|
124
146
|
* });
|
|
125
147
|
* ```
|
|
126
148
|
*/
|
|
127
149
|
onError(errorHandler: HonoErrorHandler): void;
|
|
150
|
+
/**
|
|
151
|
+
* The response the framework itself answers with, and the log line that goes with it.
|
|
152
|
+
*
|
|
153
|
+
* Reached when the application declared no `onError`, when its handler declined by returning
|
|
154
|
+
* nothing, or when the handler threw. All three mean the framework is producing the response,
|
|
155
|
+
* so it is the framework that has to record what happened - otherwise an `onError` that
|
|
156
|
+
* returns `undefined` swallows a 500 with no trace anywhere.
|
|
157
|
+
*
|
|
158
|
+
* The body is the same one `@asenajs/ergenecore` answers. It used to be `text/plain` for an
|
|
159
|
+
* application with no config and `{error, message, timestamp}` for one whose handler declined,
|
|
160
|
+
* so the same failure produced three different envelopes across the two adapters.
|
|
161
|
+
*/
|
|
162
|
+
private defaultErrorResponse;
|
|
163
|
+
/**
|
|
164
|
+
* Logs an error that is about to be handed to the application's error handler.
|
|
165
|
+
*
|
|
166
|
+
* The level is derived from the status the client will actually see: a 4xx is the
|
|
167
|
+
* caller's mistake and the auth/validation layer doing its job, so it must not flood the
|
|
168
|
+
* ERROR stream with stack traces - an unauthenticated request would otherwise be an
|
|
169
|
+
* attacker-controlled log amplifier. Only 5xx carries a stack, because that is the class
|
|
170
|
+
* of failure nobody predicted.
|
|
171
|
+
*
|
|
172
|
+
* `ServerLogger.debug` is optional, so fall back to `info` when an implementation does
|
|
173
|
+
* not provide it.
|
|
174
|
+
*/
|
|
175
|
+
private logHandledError;
|
|
128
176
|
serveOptions(options: () => Promise<AsenaServeOptions> | AsenaServeOptions): Promise<void>;
|
|
129
177
|
setPort(port: number): void;
|
|
130
178
|
private prepareMiddlewares;
|
|
@@ -144,15 +192,16 @@ export declare class HonoAdapter extends AsenaAdapter<HonoAdapterContext, Valida
|
|
|
144
192
|
/**
|
|
145
193
|
* @description Answer a failed validation.
|
|
146
194
|
*
|
|
147
|
-
* Throws a `ValidationError` so the failure travels the same path as every other
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
195
|
+
* Throws a `ValidationError` so the failure travels the same path as every other error and
|
|
196
|
+
* reaches `ConfigService.onError`, which is what the documentation has always described.
|
|
197
|
+
*
|
|
198
|
+
* The adapter used to answer its own 400 here when no error handler was configured, which made
|
|
199
|
+
* a validation failure the one 4xx that reached neither `onError` nor the log. That envelope
|
|
200
|
+
* did not disappear with the branch - it moved onto `ValidationError.getResponse()`, which
|
|
201
|
+
* `defaultErrorResponse` falls back to.
|
|
152
202
|
* @param {ZodError} error - The Zod failure
|
|
153
203
|
* @param {string} target - Which part of the request failed
|
|
154
|
-
* @
|
|
155
|
-
* @returns {Response} The fallback response, when no error handler is configured
|
|
204
|
+
* @returns {never} Always throws
|
|
156
205
|
*/
|
|
157
206
|
private handleValidationFailure;
|
|
158
207
|
/**
|
|
@@ -190,27 +239,16 @@ export declare class HonoAdapter extends AsenaAdapter<HonoAdapterContext, Valida
|
|
|
190
239
|
*/
|
|
191
240
|
private getGlobalMiddlewaresForPath;
|
|
192
241
|
/**
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* -
|
|
197
|
-
*
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
*/
|
|
204
|
-
private groupRoutesByBasePath;
|
|
205
|
-
/**
|
|
206
|
-
* Finds common middlewares across all routes in a group
|
|
207
|
-
* Returns middlewares that appear in ALL routes
|
|
208
|
-
*/
|
|
209
|
-
private extractCommonMiddlewares;
|
|
210
|
-
/**
|
|
211
|
-
* Registers routes to Hono app with optimization
|
|
212
|
-
* Groups routes by base path and applies common middlewares at group level
|
|
213
|
-
* Also registers WebSocket routes
|
|
242
|
+
* Sorts the queued routes and registers each one with its own middlewares.
|
|
243
|
+
*
|
|
244
|
+
* There used to be a "common middleware" optimisation here: routes sharing a base path were
|
|
245
|
+
* mounted on a sub-app and the middlewares they had in common were hoisted to `group.use('*')`.
|
|
246
|
+
* It compared middlewares with `mw.constructor.name`, but by this point a middleware is the
|
|
247
|
+
* plain `{ handle, override }` literal PrepareMiddlewareService builds - so the name was
|
|
248
|
+
* `"Object"` for every one of them and the comparison was always true. Every route in a group
|
|
249
|
+
* got the *first* route's middlewares and had its own filtered away, which silently swapped
|
|
250
|
+
* authorisation guards between sibling routes. Measured at 100 routes it bought nothing, so
|
|
251
|
+
* it is gone rather than repaired.
|
|
214
252
|
*/
|
|
215
253
|
private optimizeAndRegisterRoutes;
|
|
216
254
|
/**
|
|
@@ -222,10 +260,6 @@ export declare class HonoAdapter extends AsenaAdapter<HonoAdapterContext, Valida
|
|
|
222
260
|
* Converts HTTP GET route to WebSocket upgrade endpoint
|
|
223
261
|
*/
|
|
224
262
|
private registerWebsocketRouteDirect;
|
|
225
|
-
/**
|
|
226
|
-
* Registers a group of routes with common middlewares at base path level
|
|
227
|
-
*/
|
|
228
|
-
private registerRouteGroup;
|
|
229
263
|
/**
|
|
230
264
|
* Registers a single route directly without grouping
|
|
231
265
|
*/
|