@asenajs/hono-adapter

中文文档
5.0.2发布于 2026/10/31MITTypeScriptESM + CJS构建来源已验证

Hono server and websocket adapter for Asenajs

原始使用文档
文档目录14 节
  1. Features
  2. Requirements
  3. Installation
  4. Usage
  5. Server Setup (index.ts)
  6. Controller Example
  7. API Documentation
  8. createHonoAdapter(logger, options)
  9. Testing
  10. Running Tests
  11. Test Structure
  12. Writing Tests
  13. Development
  14. License

Asena Hono Adapter

Version Bun Version

HTTP and WebSocket adapter implementation based on Hono web framework for Asena.js.

Features

  • Fast and modern API endpoints with Hono web framework
  • High performance with Bun runtime
  • Full type support with TypeScript
  • HTTP and WebSocket adapter support
  • Easy integration and usage
  • Validation support with Zod
  • Decorators for controller-based routing

Request pipeline order: global middlewares → route middlewares → validator → handler.

Requirements

  • Bun v1.4 or higher
  • @asenajs/asena v0.12.0 or higher (peer dependency)
  • Hono v4.12.9 or higher (peer dependency)
  • Zod v4.3.6 or higher (peer dependency)
  • TypeScript v5.8.2 or above

Installation

bun add @asenajs/hono-adapter hono zod

hono and zod are peer dependencies - this adapter offers the wrapper, your project owns the libraries it wraps. That is what keeps your code and the adapter on a single copy of hono; two copies mean two HTTPException classes, and a deliberate 403 thrown from the wrong one is answered 500.

Package managerCommand
bun, npm 7+, pnpm 8+bun add hono zod — peers auto-install, but declare them anyway so they survive a clean install
yarn 1yarn add hono zod — required, yarn 1 does not install peers for you

Usage

Server Setup (index.ts)

import { AsenaServer } from '@asenajs/asena';
import { DefaultLogger } from "@asenajs/asena/logger";
import { createHonoAdapter } from '@asenajs/hono-adapter';

const [adapter, logger] = createHonoAdapter(new DefaultLogger());

await new AsenaServer(adapter)
  .logger(logger)
  .port(3000)
  .start(true);

Controller Example

import { Controller } from '@asenajs/asena/decorators';
import { Get } from '@asenajs/asena/decorators/http';
import type { Context } from '@asenajs/hono-adapter';

@Controller()
export class TestController {
  @Get("/")
  public async me(context: Context) {
    return context.send("Hello World!");
  }
}

API Documentation

createHonoAdapter(logger, options)

Helper function used to create a Hono adapter.

Parameters:

  • logger (Logger): Logger instance from Asena

Returns:

  • A tuple with [adapter, logger]

Testing

Asena Hono Adapter uses Bun's built-in test framework for unit and integration testing.

Running Tests

# Run all tests
bun test

# Run tests with watch mode
bun test:watch

# Run tests with coverage report
bun test:coverage

Test Structure

  • Unit Tests: Test individual components in isolation

    • HonoAdapter.test.ts - Tests for the HTTP adapter
    • HonoContextWrapper.test.ts - Tests for the context wrapper
    • HonoWebsocketAdapter.test.ts - Tests for the WebSocket adapter
  • Integration Tests: Test how components work together

    • integration.test.ts - End-to-end tests for the adapter

Writing Tests

Example test for a controller:

import { describe, expect, it } from "bun:test";
import { AsenaServer } from "@asenajs/asena";
import { createHonoAdapter } from "@asenajs/hono-adapter";
import { DefaultLogger } from "@asenajs/asena/logger";
import { YourController } from "../path/to/your/controller";

describe("YourController", () => {
  let server;
  let baseUrl;
  
  beforeEach(async () => {
    // 10000-31999: below the kernel's ephemeral floor (net.ipv4.ip_local_port_range,
    // 32768-60999). A server port drawn from that range collides with the outbound
    // sockets the suite itself holds open - TIME_WAIT included - and Bun.serve then
    // fails with EADDRINUSE, randomly, in whichever test happened to draw it.
    const port = 10000 + Math.floor(Math.random() * 22000);
    const [adapter, logger] = createHonoAdapter(new DefaultLogger());
    const app = new AsenaServer(adapter).logger(logger).port(port);
    
    app.register(YourController);
    
    server = await app.start();
    baseUrl = `http://localhost:${port}`;
  });
  
  afterEach(() => {
    server.stop();
  });
  
  it("should respond correctly", async () => {
    const response = await fetch(`${baseUrl}/your-endpoint`);
    const data = await response.json();
    
    expect(response.status).toBe(200);
    expect(data).toEqual({ /* expected response */ });
  });
});

Development

To run the project in development mode:

bun run build

License

This project is licensed under the MIT License. See the LICENSE file for details.

版本与发布记录

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

版本发布日期距今
5.0.2latest2026/10/313 天前
5.0.12026/10/313 天前
5.0.02026/10/313 天前
4.0.02026/08/262 个月前
3.2.02026/08/202 个月前
3.1.02026/08/172 个月前

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

@asenajs/hono-adapter 是什么?

Hono服务器和用于Asenajs的websocket适配器

如何安装 @asenajs/hono-adapter?

在项目目录运行 npm install @asenajs/hono-adapter,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:zod(^4.3.6)、hono(>=4.12.9)、typescript(^5.8.2)、@asenajs/asena(^0.12.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@asenajs/hono-adapter 支持 TypeScript 吗?

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

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

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@asenajs/hono-adapter 可以商用吗?

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

@asenajs/hono-adapter 还在维护吗?

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