@enkaku/http-serve

中文文档
0.23.3发布于 2026/11/11MITTypeScriptES Module近 30 天下载 3333

HTTP transport for Enkaku RPC servers

原始使用文档
文档目录4 节
  1. Installation
  2. Per-message HTTP context
  3. SSE session ownership
  4. Written frame bytes

Enkaku HTTP server transport

Installation

npm install @enkaku/http-serve

Per-message HTTP context

ServerTransport.getMessageContext(message) and createServerBridge().getMessageContext(message) return HTTPMessageContext | undefined. The context contains the original request: Request, an optional sessionID and an optional session: HTTPSessionHandle. Stream and channel invocations expose the new or existing SSE session ID from the enkaku-session-id header. Messages associated with a known session share its session handle. Unary requests and messages without a session still expose their HTTP request.

Context lookup uses message object identity and a weak map. Unknown objects, cloned messages and non-object values return undefined. Context stays outside the serialised message. The server's onInvocation hook receives this context through ctx.transport, typed as unknown. A client disconnect or session drop aborts ctx.signal during a pending hook and prevents subsequent handler dispatch.

SSE session ownership

HTTPSessionHandle exposes read-only id and owner properties plus bindOwner(did: string): void. Sessions start unbound. Binding the same DID again succeeds. Binding another DID throws and preserves the original owner. Bind ownership from the verified caller in an authenticated invocation hook:

import type { HTTPMessageContext } from '@enkaku/http-serve'
import { ServerTransport } from '@enkaku/http-serve'
import { Server } from '@enkaku/server'

const transport = new ServerTransport<typeof protocol>()
const server = new Server({
  protocol,
  handlers,
  identity,
  accessRules,
  onInvocation(ctx) {
    const context = ctx.transport as HTTPMessageContext | undefined
    if (ctx.caller != null) {
      context?.session?.bindOwner(ctx.caller.iss)
    }
  },
})
server.handle(transport)

ServerTransportOptions and ServerBridgeOptions accept enforceSessionOwner, defaulting to true. Subsequent POSTs referencing a bound session must carry a payload.iss matching its normalised owner DID. Binding stores the normalised DID. Long and short did:peer:4 forms identify the same owner. A missing or different issuer produces HTTP 403 with { error: 'Session owner mismatch' } before enqueueing the message. This applies to request, event, stream, channel, send and abort messages. The bridge compares normalised DIDs. The RPC server still authenticates the message. Unbound sessions keep their existing behaviour. Setting enforceSessionOwner: false disables the check without clearing ownership.

Written frame bytes

Both ServerTransportOptions and ServerBridgeOptions accept a synchronous onFrameWritten(info: FrameWrittenInfo) observer.

FieldMeaning
kind'response', 'sse' or 'heartbeat'
bytesUTF-8 byte length of the serialised body or frame
ridRequest ID for response and SSE frames, absent for heartbeats
sessionIDSession ID for SSE frames and heartbeats
const transport = new ServerTransport<typeof protocol>({
  onFrameWritten(info) {
    console.log(info.kind, info.bytes, info.rid, info.sessionID)
  },
})

Response counts cover the JSON message body. SSE counts include the data: prefix and trailing \n\n. Initial and periodic heartbeat comments contain :\n\n and count as three bytes, with a session ID and no request ID. Every enqueued SSE frame produces a callback, including the frame triggering buffer overflow. Frames rejected before enqueueing produce no callback. Counts exclude HTTP headers and network overhead. Resolving a response or enqueueing a frame does not confirm delivery to the client. Synchronous callback exceptions are logged and swallowed so writes continue.

版本与发布记录

@enkaku/http-serve 共发布 19 个版本,首次发布于 2026/06/21;过去 12 个月发布了 19 个版本。

版本发布日期距今
0.23.3latest2026/11/112 天前
0.23.22026/11/112 天前
0.23.12026/11/112 天前
0.23.02026/11/058 天前
0.22.52026/11/049 天前
0.22.42026/11/049 天前

关于 @enkaku/http-serve 的常见问题

@enkaku/http-serve 是什么?

HTTP transport for Enkaku RPC servers

如何安装 @enkaku/http-serve?

在项目目录运行 npm install @enkaku/http-serve,或使用 pnpm add / yarn add / bun add。

@enkaku/http-serve 支持 TypeScript 吗?

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

@enkaku/http-serve 支持 ESM 还是 CommonJS?

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

@enkaku/http-serve 可以商用吗?

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

@enkaku/http-serve 的下载量有多少?

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

@enkaku/http-serve 还在维护吗?

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