@enkaku/server

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

Server logic for Enkaku RPC

原始使用文档
文档目录5 节
  1. Installation
  2. Invocation hook
  3. Outgoing stream backpressure
  4. Channel sends
  5. Payload redaction

Enkaku server

Installation

npm install @enkaku/server

Invocation hook

Server, serve() and server.handle(transport, options) accept an optional onInvocation: InvocationHook callback. The per-transport hook overrides the server hook. The server awaits the hook after validation, authentication, access control, encryption and replay checks, before starting the handler. It runs once for each request, stream, channel or event invocation, never for send or abort messages.

InvocationContext contains:

FieldMeaning
type'request', 'stream', 'channel' or 'event'
procedureProcedure name
signalAbortSignal for cancellation while the hook awaits dispatch
ridRequest ID, absent for events
callerInvocationCaller with verified iss and optional caller-asserted sub
verifyDelegation()Asynchronous delegation check for this procedure and server
transportTransport-specific context, typed as unknown
messageOriginal message

With requireAuth: false, caller is absent and verifyDelegation() returns false. sub becomes trustworthy only when verifyDelegation() returns true. Missing or invalid delegation returns false, as does a subject matching the issuer. Unexpected verification failures reject the promise. Transport cancellation aborts signal while the hook is pending. Hooks can use it to cancel asynchronous checks. After cancellation, resolving the hook never starts the handler. The check memoises its promise and shares it with access-control predicates. The access-check functions return an AccessDecision exposing the same verifyDelegation() method.

import { InvocationRejectedError, Server } from '@enkaku/server'

const server = new Server({
  protocol,
  handlers,
  identity,
  accessRules,
  async onInvocation(ctx) {
    if (ctx.caller?.sub != null && !(await ctx.verifyDelegation())) {
      throw new InvocationRejectedError({
        code: 'DELEGATION_REQUIRED',
        message: 'A valid delegation is required',
      })
    }
  },
})

Throwing InvocationRejectedError({ code, message, cause? }) skips the handler and preserves the supplied code and message in the error reply. Other exceptions produce code EK01 and message Invocation hook failed, without exposing the exception message. Events receive no reply. All hook failures emit handlerError with category 'invocation' and mark the invocation span as failed.

Outgoing stream backpressure

Server and serve() accept streamHighWaterMark, a positive finite safe integer defaulting to 1. It bounds the outgoing readable queue for each stream and channel handler, measured in values rather than bytes. The channel's incoming pipe keeps its existing behaviour.

const server = serve({
  requireAuth: false,
  protocol,
  handlers,
  transport,
  streamHighWaterMark: 4,
})

When transport sends stall, the queue fills and the handler's next writer.write() stays pending. The writer's desiredSize drops to 0 until downstream consumption resumes. Values already in transport buffers sit outside this queue limit. Handlers must await writes or respect writer.ready to avoid accumulating pending writes themselves. The default now applies backpressure where previous versions allowed unbounded outgoing queues.

Channel sends

The server delivers a send message only when its prc matches the procedure of the channel open on its rid. Any other send, including one addressed to a request or stream, is an invalid message. The server emits invalidMessage and replies with error code EK08 on that rid. As with other invalid sends, the client then treats the channel as failed, while the server handler may keep running. abort messages carry no procedure and are routed by rid alone.

Payload redaction

Server and serve() accept redactProcedures, an array of procedure names or a predicate (procedure: string) => boolean. A predicate that throws counts as a match. The server never logs payloads of a redacted procedure, at any level. This covers params, event data, sent and received channel or stream values, results, and handler or invocation hook errors. Invalid messages that name a redacted procedure are logged without their validation error. Channel send messages and invalid messages use the procedure of the live request for their rid, not the client-supplied prc. With the option set, an invalid message naming no known procedure is also logged without its validation error. So is an invalid send or abort that arrives while authentication of its request is still pending. Log records keep the procedure name, request ID, message type and error code. Error records also keep the error name, never its message or cause. Spans of a redacted procedure record the error code and name only. Their error message and status message read Error details redacted, as does the validation error of an invalid message. Messages on the wire are unchanged. In-process handlerError and invalidMessage events still carry the payload, and the error.cause of writeFailed events can too. Consumers must not log those payloads.

const server = serve({
  requireAuth: false,
  protocol,
  handlers,
  transport,
  redactProcedures: ['secrets/provision', 'mcp/callTool'],
})

版本与发布记录

@enkaku/server 共发布 60 个版本,首次发布于 2024/10/06;过去 12 个月发布了 37 个版本。

版本发布日期距今
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/server 的常见问题

@enkaku/server 是什么?

Server logic for Enkaku RPC

如何安装 @enkaku/server?

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

@enkaku/server 支持 TypeScript 吗?

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

@enkaku/server 支持 ESM 还是 CommonJS?

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

@enkaku/server 可以商用吗?

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

@enkaku/server 的下载量有多少?

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

@enkaku/server 还在维护吗?

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