@basaltkit/hono 1.4.0 → 1.4.1
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 +76 -3
- package/package.json +7 -3
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ The Basalt adapter for [Hono](https://hono.dev): the same typed routes, enricher
|
|
|
14
14
|
|
|
15
15
|
This module connects Hono to Basalt. **Routes** (a path + HTTP method, e.g. `POST /echo`) are defined with the `route()` function from `@basaltkit/http`, with [Zod](https://zod.dev) schemas that validate the body, query, and URL parameters — and give you TypeScript types for free. The adapter converts each Hono request into Basalt's neutral format, runs the shared pipeline (validation, *enrichers* — functions that enrich the request context — and *guards* — functions that can reject it, e.g. authentication), and returns responses with errors in a stable format.
|
|
16
16
|
|
|
17
|
-
The main benefit is **portability**: a route written here runs unchanged on `@basaltkit/fastify` and `@basaltkit/express
|
|
17
|
+
The main benefit is **portability**: a route written here runs unchanged on `@basaltkit/fastify` and `@basaltkit/express` — **the three adapters are equals**. Routes, enrichers, guards, the per-request context, standardized errors, the neutral 404, ETags (`meta.etag`), SSE, per-route rate limits and the boot-time guarded-meta check all behave identically. The neutral edge plugins (security, health, metrics, tracing, OpenAPI) from `@basaltkit/http` work on Hono exactly the same way.
|
|
18
18
|
|
|
19
19
|
## Installation
|
|
20
20
|
|
|
@@ -111,6 +111,57 @@ Unexpected errors respond with `500` and `{ error: { code: 'INTERNAL_ERROR', ...
|
|
|
111
111
|
|
|
112
112
|
The adapter reads the body based on `Content-Type`: `application/json` → JSON object; forms (`form`) → Hono's `parseBody()`; other text → string; empty or invalid body → `undefined` (Zod validation handles the rest). `GET`/`HEAD` requests never have a body.
|
|
113
113
|
|
|
114
|
+
### Body-size limit — `bodyLimit`
|
|
115
|
+
|
|
116
|
+
Hono and edge runtimes impose **no** default cap on a request body, so an upload is
|
|
117
|
+
unbounded unless something stops it. The plugin installs a guard that rejects any request
|
|
118
|
+
whose `Content-Length` exceeds the limit with `413` and
|
|
119
|
+
`{ code: 'PAYLOAD_TOO_LARGE', message: … }` — **before the body is read**.
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
import { DEFAULT_BODY_LIMIT, honoPlugin } from '@basaltkit/hono'
|
|
123
|
+
|
|
124
|
+
honoPlugin({ routes, bodyLimit: 5 * 1024 * 1024 }) // 5 MiB
|
|
125
|
+
DEFAULT_BODY_LIMIT // 1_048_576 — 1 MiB, the default
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
> Note this checks the declared `Content-Length`; it is a cheap first line of defence, not
|
|
129
|
+
> a streaming byte counter.
|
|
130
|
+
|
|
131
|
+
### Guarded route meta — the boot check
|
|
132
|
+
|
|
133
|
+
If a route declares `meta.auth`, `meta.can` or `meta.teamRole` and **no registered plugin
|
|
134
|
+
enforces that key**, the route would serve unprotected. `honoPlugin` refuses to boot: it
|
|
135
|
+
calls `assertRoutesGuarded()` in its boot phase and throws `UnguardedRouteMetaError`
|
|
136
|
+
(code `HTTP_UNGUARDED_ROUTE_META`), naming every offending route and key, before a single
|
|
137
|
+
request is served.
|
|
138
|
+
|
|
139
|
+
Fix it by registering the enforcing plugin (`auth` → `authPlugin`, `can` →
|
|
140
|
+
`permissionsPlugin`, `teamRole` → `teamsPlugin`). If protection really does happen at an
|
|
141
|
+
outer edge (a Worker in front, a gateway), waive it explicitly:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
honoPlugin({ routes, allowUnguardedMeta: true }) // waive every key
|
|
145
|
+
honoPlugin({ routes, allowUnguardedMeta: ['auth'] }) // waive one key
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Fastify and Express run the identical check with the identical option.
|
|
149
|
+
|
|
150
|
+
### The neutral 404
|
|
151
|
+
|
|
152
|
+
Unmatched routes get the same JSON body on every adapter —
|
|
153
|
+
`{ "error": { "code": "NOT_FOUND", "message": "Route not found." } }` (`NOT_FOUND_RESPONSE`
|
|
154
|
+
from `@basaltkit/http`) — instead of Hono's plain-text default. An app that calls
|
|
155
|
+
`hono.notFound(…)` *later* still wins (Hono keeps the last handler); pass `notFound: false`
|
|
156
|
+
to opt out entirely.
|
|
157
|
+
|
|
158
|
+
### Streaming — SSE
|
|
159
|
+
|
|
160
|
+
A handler returning `sse(producer)` from `@basaltkit/http` becomes a `Response` backed by a
|
|
161
|
+
web `ReadableStream`, so it streams on Node, Bun, Deno and edge alike. Client aborts
|
|
162
|
+
(`request.signal`) are relayed to `stream.onClose()`. Same handler code as on Fastify and
|
|
163
|
+
Express.
|
|
164
|
+
|
|
114
165
|
### Enrichers and guards (authentication, tenancy, …)
|
|
115
166
|
|
|
116
167
|
Plugins register these functions in the container's metadata "buckets"; the adapter applies them to every route. Real example (from the package's tests):
|
|
@@ -226,10 +277,29 @@ In this mode errors are still standardized (each handler wraps `toErrorResponse`
|
|
|
226
277
|
| `routes` | `BasaltRoute[]` | No | `[]` | Routes (created with `route()` from `@basaltkit/http`) to mount. |
|
|
227
278
|
| `allowUnguardedMeta` | `boolean \| string[]` | No | fail loud at boot | Waives the boot check that every route declaring security meta (`auth`, `can`, `teamRole`) has a registered guard enforcing it (`UnguardedRouteMetaError` otherwise). `true` waives everything (edge/gateway auth); an array waives specific keys. |
|
|
228
279
|
| `app` | `Hono` | No | `new Hono()` | Bring your own Hono app; otherwise a new one is created. |
|
|
280
|
+
| `notFound` | `boolean` | No | `true` | Serve `NOT_FOUND_RESPONSE` (the neutral JSON 404) for unmatched routes. A later `hono.notFound(…)` of your own still wins; `false` opts out entirely. |
|
|
281
|
+
| `bodyLimit` | `number` | No | `DEFAULT_BODY_LIMIT` = `1_048_576` (1 MiB) | Maximum request body in bytes. A request whose `Content-Length` exceeds it is rejected `413 PAYLOAD_TOO_LARGE` before the body is read — Hono/edge has no default cap of its own. |
|
|
229
282
|
|
|
230
283
|
Behavior: registers the Hono app on the `HONO` token and an `HttpServerCollector` on the `HTTP_SERVER` token. On the `app:booted` event it mounts, in order: *after-hooks* middleware (metrics/tracing, measuring duration), *pre-hooks* middleware (security/CORS/rate limit; if one of these responds, the route doesn't run), the Basalt routes, and the extra edge plugin routes (`/livez`, `/metrics`, `/openapi.json`, …). Publishes the routes to the `'http:routes'` metadata bucket for OpenAPI/CLI/SDK.
|
|
231
284
|
|
|
232
|
-
> Note: this plugin has no `shutdown` step of its own — stopping the server (`serve` from `@hono/node-server`, etc.) is your responsibility.
|
|
285
|
+
> Note: this plugin has no `shutdown` step of its own — Basalt never starts a listener for you, so stopping the server (`serve` from `@hono/node-server`, etc.) is your responsibility.
|
|
286
|
+
|
|
287
|
+
### `DEFAULT_BODY_LIMIT`
|
|
288
|
+
|
|
289
|
+
`1_048_576` (1 MiB) — the default for `honoPlugin({ bodyLimit })`, exported so you can
|
|
290
|
+
reason about it or reuse it.
|
|
291
|
+
|
|
292
|
+
### Errors
|
|
293
|
+
|
|
294
|
+
| Error | Code | HTTP | When |
|
|
295
|
+
|---|---|---|---|
|
|
296
|
+
| `RequestValidationError` | `HTTP_VALIDATION` | 400 | `body`/`query`/`params` failed its Zod schema. Response carries `part` + `issues[]`. |
|
|
297
|
+
| `HttpError(status, code, message)` | *yours* | *yours* | Thrown deliberately from any layer. |
|
|
298
|
+
| `UnguardedRouteMetaError` | `HTTP_UNGUARDED_ROUTE_META` | — (boot) | A route declares a guarded key (`auth`/`can`/`teamRole`/`scopes`/`subscribed`/`feature`) with no guard enforcing it. Waive with `allowUnguardedMeta`. |
|
|
299
|
+
| — | `NOT_FOUND` | 404 | No route matched (unless `notFound: false`). |
|
|
300
|
+
| — | `PAYLOAD_TOO_LARGE` | 413 | Declared `Content-Length` exceeds `bodyLimit`. Body shape here is flat (`{ code, message }`), not the nested `{ error: … }` envelope. |
|
|
301
|
+
| — | `RATE_LIMITED` | 429 | `securityPlugin`'s limiter rejected the request. |
|
|
302
|
+
| — | `INTERNAL_ERROR` | 500 | Any other thrown error. The real message never reaches the client. |
|
|
233
303
|
|
|
234
304
|
### `HONO`
|
|
235
305
|
|
|
@@ -247,7 +317,7 @@ Dependency injection token (`Token<Hono>`): `app.container.get(HONO)` returns th
|
|
|
247
317
|
|
|
248
318
|
### What to import from where
|
|
249
319
|
|
|
250
|
-
This package
|
|
320
|
+
This package exports `honoPlugin`, `registerRoutes`, `HONO`, `DEFAULT_BODY_LIMIT`, and `HonoPluginOptions`. Everything else — `route`, `HttpError`, `RequestValidationError`, `NOT_FOUND_RESPONSE`, `sse`, `securityPlugin`, `RedisRateLimitStore`, `healthPlugin`, `metricsPlugin`, `tracingPlugin`, `openapiPlugin`, `escapeHtml`/`pageCsp`, types like `RequestEnricher`/`RouteGuard` — is imported from **`@basaltkit/http`**. (Unlike `@basaltkit/fastify`, this package re-exports nothing; that is a naming choice, not a capability gap.)
|
|
251
321
|
|
|
252
322
|
## Common errors and solutions (FAQ)
|
|
253
323
|
|
|
@@ -272,3 +342,6 @@ This package only exports `honoPlugin`, `registerRoutes`, `HONO`, and `HonoPlugi
|
|
|
272
342
|
- **`@basaltkit/fastify` / `@basaltkit/express`** — sibling adapters: the same routes, enrichers, guards, and edge plugins run on any of them without changes; switching frameworks (or runtime — Node → edge) is just switching the plugin.
|
|
273
343
|
- **`@basaltkit/auth` / `@basaltkit/tenancy` / `@basaltkit/permissions`** — register guards/enrichers on `'http:guards'`/`'http:enrichers'` and read the routes' `meta` (e.g. `meta: { auth: true }`); this adapter applies them automatically.
|
|
274
344
|
- **`@basaltkit/sdk` and the CLI** — consume the `'http:routes'` bucket (routes + Zod schemas) that this plugin publishes.
|
|
345
|
+
- **`@basaltkit/testing`** — `createTestApp({ adapter: 'hono' })` runs your suite against this adapter in-process (`hono.fetch(new Request(…))`, no socket).
|
|
346
|
+
|
|
347
|
+
Guides: [Adapters](/guide/adapters) · [Testing](/guide/testing) · [Security](/guide/security) · [Production](/guide/production)
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/hono",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.1",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22.5.0"
|
|
6
|
+
},
|
|
4
7
|
"description": "Hono adapter for Basalt: run the same typed routes, enrichers and guards on Hono (Node, Bun, Deno, edge) as on Fastify or Express.",
|
|
5
8
|
"license": "MIT",
|
|
6
9
|
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
7
11
|
"exports": {
|
|
8
12
|
".": {
|
|
9
13
|
"types": "./dist/index.d.ts",
|
|
@@ -14,8 +18,8 @@
|
|
|
14
18
|
"dist"
|
|
15
19
|
],
|
|
16
20
|
"dependencies": {
|
|
17
|
-
"@basaltkit/core": "^1.3.
|
|
18
|
-
"@basaltkit/http": "^1.
|
|
21
|
+
"@basaltkit/core": "^1.3.1",
|
|
22
|
+
"@basaltkit/http": "^1.14.0"
|
|
19
23
|
},
|
|
20
24
|
"peerDependencies": {
|
|
21
25
|
"hono": "^4.0.0"
|