@basaltkit/hono 1.3.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 +77 -3
- package/dist/index.d.ts +7 -0
- package/dist/index.js +4 -1
- 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):
|
|
@@ -224,11 +275,31 @@ In this mode errors are still standardized (each handler wraps `toErrorResponse`
|
|
|
224
275
|
| Option | Type | Required? | Default | Description |
|
|
225
276
|
|---|---|---|---|---|
|
|
226
277
|
| `routes` | `BasaltRoute[]` | No | `[]` | Routes (created with `route()` from `@basaltkit/http`) to mount. |
|
|
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. |
|
|
227
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. |
|
|
228
282
|
|
|
229
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.
|
|
230
284
|
|
|
231
|
-
> 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. |
|
|
232
303
|
|
|
233
304
|
### `HONO`
|
|
234
305
|
|
|
@@ -246,7 +317,7 @@ Dependency injection token (`Token<Hono>`): `app.container.get(HONO)` returns th
|
|
|
246
317
|
|
|
247
318
|
### What to import from where
|
|
248
319
|
|
|
249
|
-
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.)
|
|
250
321
|
|
|
251
322
|
## Common errors and solutions (FAQ)
|
|
252
323
|
|
|
@@ -271,3 +342,6 @@ This package only exports `honoPlugin`, `registerRoutes`, `HONO`, and `HonoPlugi
|
|
|
271
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.
|
|
272
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.
|
|
273
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/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,13 @@ export declare const DEFAULT_BODY_LIMIT = 1048576;
|
|
|
8
8
|
export declare function registerRoutes(app: Hono<any>, routes: BasaltRoute[], container?: Container, enrichers?: RequestEnricher[], guards?: RouteGuard[]): void;
|
|
9
9
|
export interface HonoPluginOptions {
|
|
10
10
|
routes?: BasaltRoute[];
|
|
11
|
+
/**
|
|
12
|
+
* Waives the boot-time check that every route declaring security meta
|
|
13
|
+
* (`auth`, `can`, `teamRole`) has a registered guard enforcing it. Pass
|
|
14
|
+
* `true` to waive everything (e.g. authentication handled at an outer
|
|
15
|
+
* edge/gateway), or an array of specific keys. Default: fail loud at boot.
|
|
16
|
+
*/
|
|
17
|
+
allowUnguardedMeta?: boolean | string[];
|
|
11
18
|
/** Bring your own Hono app; otherwise a fresh one is created. */
|
|
12
19
|
app?: Hono<any>;
|
|
13
20
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Container, createToken, definePlugin, ensureMetadata } from '@basaltkit/core';
|
|
2
|
-
import { NOT_FOUND_RESPONSE, HttpServerCollector, HTTP_SERVER, runRoute, toErrorResponse, isSseResponse, sseProducerOf, driveSse, SSE_HEADERS, } from '@basaltkit/http';
|
|
2
|
+
import { NOT_FOUND_RESPONSE, HttpServerCollector, HTTP_SERVER, runRoute, toErrorResponse, isSseResponse, sseProducerOf, driveSse, SSE_HEADERS, GUARDED_META_BUCKET, assertRoutesGuarded, } from '@basaltkit/http';
|
|
3
3
|
import { Hono } from 'hono';
|
|
4
4
|
export const HONO = createToken('hono');
|
|
5
5
|
/** Default maximum request body size (1 MiB) — override via honoPlugin({ bodyLimit }). */
|
|
@@ -156,6 +156,9 @@ export function honoPlugin(options = {}) {
|
|
|
156
156
|
const metadata = ensureMetadata(container);
|
|
157
157
|
const enrichers = metadata.get('http:enrichers');
|
|
158
158
|
const guards = metadata.get('http:guards');
|
|
159
|
+
// Fail loud BEFORE traffic if a route declares security meta (auth/can/
|
|
160
|
+
// teamRole) that no registered guard enforces — it would serve open.
|
|
161
|
+
assertRoutesGuarded(routes, new Set(metadata.get(GUARDED_META_BUCKET)), options.allowUnguardedMeta);
|
|
159
162
|
// Mount once edge plugins have registered their hooks/routes.
|
|
160
163
|
const bodyLimit = options.bodyLimit ?? DEFAULT_BODY_LIMIT;
|
|
161
164
|
hooks.on('app:booted', () => {
|
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/hono",
|
|
3
|
-
"version": "1.
|
|
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.1
|
|
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"
|