@ailura/nestjs-hono-adapter

中文文档
2.1.0发布于 2026/10/20MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2866

A NestJS HTTP adapter that runs an application on Hono.

原始使用文档
文档目录8 节
  1. Install
  2. Companion packages
  3. Migrating to 2.x
  4. Use
  5. Options
  6. Documentation
  7. Requirements
  8. License

@ailura/nestjs-hono-adapter

CI npm version npm downloads bundle size Socket Badge Snyk security codecov GitHub license

An HTTP adapter that runs a NestJS application on Hono, with no Express or Fastify underneath. The core is runtime-neutral; a transport decides how it is served: bunServer() (Bun's own server, the default), nodeServer() for @hono/node-server, or fetchServer() for a host that serves a Web Request/Response.

Problem: Nest has no official Hono adapter. The two published alternatives target Nest 11 and answer incorrectly — one returns a success status to a handler that threw, the other writes every response twice.

Solution: This package implements the Nest 11/12 AbstractHttpAdapter contract directly on Hono: routes are registered on a Hono application, and Hono's Web Request/Response are translated to and from the objects Nest reads and writes.

Install

bun add @ailura/nestjs-hono-adapter hono

@nestjs/common, @nestjs/core, hono, rxjs and @hono/node-server are peers, so the application decides their versions. @hono/node-server is optional: only the ./node-server transport reaches it, and a Bun or fetch deployment never loads it. Use npm install or pnpm add in place of bun add as needed.

The optional capability lives in a package of its own, with a single entrypoint, so importing it loads none of the others:

PackageForInstalls
@ailura/nestjs-platform-hono-multipartuploadsbusboy

WebSockets are not a capability but a compatibility bridge: @ailura/nestjs-platform-hono-ws fits Nest's own @nestjs/platform-ws WsAdapter to Hono, so you install the package and write no import of it — app.useWebSocketAdapter(new WsAdapter(app)) is the whole wiring. Socket.IO is the same, through @ailura/nestjs-platform-hono-socket-io and the stock @nestjs/platform-socket.io IoAdapter. See docs/compatibility.md.

A socket.io gateway is served by the Bun transport's own server where it can bind an engine, and by the Node transport's http.Server otherwise; the fetch transport has neither and is refused by name.

Companion packages

PackageWhat it doesWhen to install
@ailura/nestjs-hono-adapterthe adapter itselfalways
@ailura/nestjs-platform-hono-multipartmultipart uploads via busboyupload routes
@ailura/nestjs-platform-hono-graphql@nestjs/apollo ApolloDriver fitted to HonoGraphQL drivers
@ailura/nestjs-platform-hono-ws@nestjs/platform-ws WsAdapter fitted to HonoWebSocket gateways
@ailura/nestjs-platform-hono-socket-io@nestjs/platform-socket.io IoAdapter fitted to HonoSocket.IO gateways
@ailura/nestjs-platform-hono-serve-staticserve-static fitted to Honostatic assets

And inside this repository:

Migrating to 2.x

Five things break a deployment that tracked @ailura/[email protected]: ./ws moved, the default transport is no longer the Node one, HonoWsAdapter is gone, security headers are off by default, and the package is ESM only.

./ws and the HonoWsAdapter it exported are gone. WebSockets are served by the stock @nestjs/platform-ws WsAdapter, fitted to Hono by @ailura/nestjs-platform-hono-ws; install that package, which the compatibility region discovers, so a deployment writes no import of it and names the stock class instead — and installs @nestjs/platform-ws in place of @hono/node-ws.

OldNew
@ailura/nestjs-hono-adapter/ws@ailura/nestjs-platform-hono-ws, installed alone
new HonoWsAdapter(app)new WsAdapter(app) from @nestjs/platform-ws

Only a gateway's path is honoured; the wire protocol is platform-ws's own.

The zero-configuration bootstrap now serves through bunServer() where it served through @hono/node-server; a deployment on Node names the transport explicitly (Use).

ServerAdapterOptions.overrideGlobalObjects is gone and nodeServer() carries it, defaulting to true, the fastest path and the behaviour the implicit Node transport had; the replacement of the global Request and Response is process-wide and not restored, so a deployment that wants the globals left alone asks by name: nodeServer({ overrideGlobalObjects: false }).

secureHeaders defaulted to true and now defaults to false; a deployment that relied on the default sets secureHeaders: true.

The Bun transport resolves hono/bun in a top-level await so the root entrypoint still imports on Node, which makes the package ESM only: require() throws, import works on Node and Bun.

./multipart and ./socket-io were added on main after 1.4.1 and never published, so nobody has to migrate for them; a deployment that tracked main and imported one installs the matching package above.

Use

import { NestFactory } from '@nestjs/core';
import { ServerAdapter } from '@ailura/nestjs-hono-adapter';
import { bunServer } from '@ailura/nestjs-hono-adapter/bun-server';

import { AppModule } from './app.module.ts';

const app = await NestFactory.create(
  AppModule,
  new ServerAdapter({
    bodyLimit: 2 * 1024 * 1024,
    transport: bunServer(),
    trustProxy: true,
  }),
);
app.enableCors({
  credentials: true,
  origin: ['https://app.example.com'],
});
await app.listen(3000);

new ServerAdapter() with no transport serves through Bun's own server. A deployment on Node names it:

import { nodeServer } from '@ailura/nestjs-hono-adapter/node-server';

new ServerAdapter({ transport: nodeServer() });

nodeServer() takes autoCleanupIncoming (default true), which drains a request whose body was never read, and overrideGlobalObjects (default true), which asks @hono/node-server to replace the process's Request and Response with its lighter classes — the fastest path, and process-wide; pass false to leave the globals alone. Both only reach @hono/node-server, so they change nothing for a deployment that serves through bunServer().

On a fetch host — Cloudflare Workers, Vercel Functions or Deno — the transport takes no port and the deployment exports the handler:

import { fetchHandler, fetchServer } from '@ailura/nestjs-hono-adapter/fetch';

const adapter = new ServerAdapter({ transport: fetchServer() });
const app = await NestFactory.create(AppModule, adapter);
await app.init();

export default { fetch: fetchHandler(adapter.getHono()) };

listen() on the fetch server is refused, passing an error to the callback or throwing when none is given. The fetch handler has no client address by default and ignores cf-connecting-ip; pass fetchHandler(app, { clientAddress }) with a resolver for a trusted, host-supplied address, and read a header there only if the host guarantees clients cannot forge it. x-forwarded-for still supplies ip when trustProxy trusts the proxy. See fetch hosts.

getType() answers hono, the value ecosystem packages branch on.

For in-process tests, call await app.init() before app.inject(). String targets accept relative paths and absolute URLs; query appends to the existing query. GET and HEAD ignore payload/body; other methods send them as the request body.

setOnRequestHook() continues the request when the hook calls done or its returned promise/thenable resolves, whichever happens first. setOnResponseHook() runs when the chain settles, including when the request hook throws or rejects; it waits up to one second for response-hook work before releasing the response, and later rejections are still logged. See instrumentation hooks.

Options

app.set('views', directory) accepts a string or a string array, searched in order; app.set('view engine', extension) accepts a string. View data combines setViewEngine({ defaultContext }), setLocal() values, and handler data, in that order, preserving locals including keys such as __proto__.

OptionTypeDefaultEffect
bodyLimitnumber1 MiBLargest non-multipart request body, in bytes; 0 removes the limit. Multipart parsing uses upload-route limits when the multipart package is enabled; declared lengths are still checked.
rawBodybooleanfalseKeep the bytes that were read in NestRequest.rawBody
secureHeadersboolean | objectfalseInstall hono/secure-headers, with its defaults or with the given options; off by default
transportTransport—The runtime: bunServer(), nodeServer() or fetchServer()
trustProxyboolean | number | string[]falseRead x-forwarded-proto, x-forwarded-for and x-forwarded-host
viewsobject—The engine a @Render() handler renders with, and where templates are read from

app.useBodyParser() accepts json, urlencoded, text and raw; unsupported names are rejected. Parsing follows the request's content type, and limit applies to every parser and route; omitting limit still enables parsing and applies the raw-body setting, leaving the existing size limit intact.

Documentation

Architecture, deployment, parity and the API reference live in the repository's docs/ directory.

Requirements

  • Bun 1.2+ (toolchain, and the runtime the default transport serves on), or Node 22.12+.
  • @hono/node-server, which nodeServer() serves through; it is an optional peer, and the default transport is Bun's own server.
  • Nest 11 or 12.
  • Hono 4.

License

MIT

版本与发布记录

@ailura/nestjs-hono-adapter 共发布 22 个版本,首次发布于 2026/09/21;过去 12 个月发布了 22 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.0latest2026/10/2018 天前
2.0.22026/10/1721 天前
2.0.12026/10/1622 天前
2.0.02026/10/1622 天前
3.0.02026/10/1622 天前
2.0.0-beta.12026/10/1622 天前

关于 @ailura/nestjs-hono-adapter 的常见问题

@ailura/nestjs-hono-adapter 是什么?

A NestJS HTTP adapter that runs an application on Hono.

如何安装 @ailura/nestjs-hono-adapter?

在项目目录运行 npm install @ailura/nestjs-hono-adapter,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:hono(^4.13.8)、rxjs(^7.8.2)、@nestjs/core(>=11 <13)、@nestjs/common(>=11 <13)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ailura/nestjs-hono-adapter 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@ailura/nestjs-hono-adapter 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@ailura/nestjs-hono-adapter 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@ailura/nestjs-hono-adapter 可以商用吗?

@ailura/nestjs-hono-adapter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ailura/nestjs-hono-adapter 的下载量有多少?

根据 npm 官方下载统计,@ailura/nestjs-hono-adapter 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,866 次,日均约 96 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ailura/nestjs-hono-adapter 还在维护吗?

最新版本 2.1.0 发布于 2026/10/20(18 天前);至今共发布 22 个版本,其中过去 12 个月发布了 22 个;首次发布于 2026/09/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。