@alpamayo-solutions/colca-client

中文文档
0.44.1发布于 2026/11/05SEE LICENSE IN LICENSE.mdTypeScriptES Module零依赖构建来源已验证近 30 天下载 4186

Client for a Colca node's door: fetch, ack, publish, kv, self.

原始使用文档
文档目录10 节
  1. A consumer
  2. A producer
  3. Browsing the tree
  4. Live values
  5. Commands
  6. Standing alarms
  7. Which door, which credential
  8. Things the node insists on
  9. Ids
  10. Development

@alpamayo-solutions/colca-client

A Colca node's door, in TypeScript: records over HTTP — fetch, ack, publish, kv, self — and live values over MQTT. No runtime dependencies for the door, no framework, no opinion about how your service is built.

npm install @alpamayo-solutions/colca-client

A consumer

import { Door, Doorbell, Stream } from "@alpamayo-solutions/colca-client";

const door = new Door({ baseUrl: "http://colca", service: "my-app" });
const panels = new Stream(door, "annotations", door.cursorName("panels"), {
  prefix: "wisewoods/line1",
});

// Ring the bell whenever the stream may have grown: an MQTT message on the
// topics this stream reads, a /watch hint, a reconnect.
const bell = new Doorbell();
for await (const record of panels.follow({ bell })) {
  // Acked page by page: a handler that throws sees its page again.
  console.log(record.topic, record.payload);
}

follow() also reads from its cursor when the bell stayed silent for idleDrainMs (five minutes by default), so a ring that was lost does not stall the consumer for good. That is a recovery read, not a polling cadence: keep it long. idleDrainMs: null waits for a ring alone.

A producer

await door.publishTo(
  { root: "steine", contract: "_Metric", node: "n-technikum", path: "wisewoods/line1/mas2/grit" },
  { signal_id: "01M2AB…", timestamp: Date.now() / 1000, value: 60 },
);

Browsing the tree

// One level below "wisewoods/": its records, plus the paths that expand.
const { entries, folders } = await door.kvLevel("wisewoods/");
// The same scan without folders, narrowed to one contract.
const signals = await door.kv("wisewoods/line1/", { depth: 2, contract: "_Signal" });

Live values

@alpamayo-solutions/colca-client/live keeps one MQTT connection to the node's WebSocket door for people and applications, and hands out values as they change.

npm install mqtt
import { Live } from "@alpamayo-solutions/colca-client/live";

const live = new Live({
  url: "wss://node:8885",
  // Asked before every connection, so hand back a token that is valid now.
  token: () => auth.freshToken(),
});

const stop = live.subscribe("steine/v1/_Metric/n-technikum/wisewoods/#", (value) => {
  show(value.topic, value.payload);
});
live.onState((state) => showOffline(state !== "online"));

Data and entity paths are retained at the node, so a subscription starts with the current values and continues with the changes. The exception is a node with children: it does not retain the _Metric records its children replicate up (bus.retain_child_metrics), so a view of a child's metrics reads the current values from /kv first and takes the changes from the subscription. Around that the client does what a page left open all day needs:

  • One connection for the whole page. subscribe returns the function that ends that subscription and no other; the node's subscription goes when the last listener on a filter does.
  • A second subscriber gets the value at once. The client keeps the last value of every topic, and latest() and values() read it.
  • A fresh token before the old one runs out. The node ends a session when its token expires. Shortly before, the client hands the node a new token on the open connection (MQTT 5 re-authentication), so nothing is subscribed again. A node that does not offer that gets a new connection with the new token and every subscription sent again. Either way the client stays online.
  • Waits that grow after a drop, jittered, each attempt with a fresh token.
  • A word when the subscriptions go out again. onResubscribe() fires once they have, on every new connection — where the node's retained delivery starts over, and where a view reconciles a retained set from.

These are values, not a log. A change during a reconnect is superseded by the retained value that follows it. Whatever must see every record reads a stream through the door.

Commands

A person's session sends commands, not values. command() sends one and waits for the executor's _Ack:

const ack = await live.command("steine/v1/_CmdParam/n-technikum/wisewoods/line1/mas2/sta1/aggos/setGrit", {
  params: { signal: "grit", value: 120 },
});
if (ack.result_code !== 200) showRefusal(ack.message);

It adds the correlation id and the expiry, subscribes to the acknowledgements before it sends, so an executor that answers at once is not missed, and matches the answer by its id wherever in the tree it arrives. The promise settles with the _Ack whatever its result code, and rejects with CommandTimeout when nobody answers in time (30 s by default, which is also the command's expiry). Offline, nothing is queued: a setpoint sent minutes late is a different setpoint.

publish() sends a single record without waiting, and newUlid() makes ids that sort by the time they were made, as the node's own do.

Standing alarms

_AlarmState is retained, one record per alarm and an empty payload when the alarm goes. What the node holds is therefore what stands: nothing to fetch first, no history to fold, and no normal records to read past.

import { Alarms } from "@alpamayo-solutions/colca-client/live";

const alarms = new Alarms({ live, node: "n-technikum", root: "steine" });

const stop = alarms.onChange((standing) => showBanner(standing), { minSeverity: "warning" });

// Throws when the node refuses it, and when nobody answers.
await alarms.acknowledge("wisewoods/line1/mas2/gritLow", { note: "Korn getauscht" });

standing() reads the set at any time — worst first, and the oldest first within a severity. onChange() is told what stands now and again on every change, and returns the function that ends that watch and no other. An alarm's name comes from the _SystemElement it hangs on, and its path is where a view jumps to.

When the connection comes back — after a drop, and after a token renewal that had to reconnect — the node starts its retained delivery over, and an alarm that went while the client was away leaves nothing behind to say so. Alarms therefore gives the set 750 ms to arrive again (resyncMs) and drops what did not come back: a view can be that much behind the node, but it never goes on showing an alarm that is over. It is a window, and a guess, because the node does not say where its retained delivery ends; resyncMs: 0 turns the reconciliation off.

acknowledge() sends a _CmdAcknowledge, and silence(path, { minutes: 30 }) and unsilence() send _CmdOperate, each on the alarm's own path and each waiting for its _Ack. Quitting an alarm therefore needs an acknowledge grant, silencing an operate grant (see security). The note and the deadline ride in the payload's command object, where the contract keeps a verb's arguments. The client sends no identity: who quit an alarm is the node's word on the record, and the caller adds the note. A refusal — 300 and up — throws AlarmRefused instead of resolving, because an acknowledgement that was swallowed is worse than one that was never sent.

Which door, which credential

DoorURLCredential
localhttp://colca (port 80, inside the deployment network)none — reachability is the credential; service names the caller
publishedhttps://node:443a person's bearer token, or a machine's pinned client certificate

A client certificate is a TLS matter, so it belongs to the runtime rather than to this package: hand in a fetch that carries it (options.fetch).

Things the node insists on

  • Cursors belong to their caller. They are named c/{service}/…, which is what door.cursorName() builds; the node refuses any other name.
  • Fetching never moves a cursor. Only ack does, and only forward. tail reads the end of a stream without touching it, which is what a view wants.
  • A gap is not an error. When records were pruned below the cursor, the page says so. Stream passes it to onGap and acks past it, so it is reported once rather than forever.
  • The node decides what you see. /fetch and /kv filter by the caller's read grants; a publish is judged against its zone and grants. A 403 carries the node's reason.

Ids

deriveAnnotationId() computes an annotation's id the way the node's own contracts package does. It is derived rather than drawn so that create, update and delete of one annotation are appends under the same id — a producer that runs twice overwrites its own record instead of doubling it.

The agreement is checked against clients/spec/vectors.json, generated from colca-data-contracts. If a rule changes there, the tests here fail.

Development

npm install
npm run lint
npm run typecheck
npm test
npm run build

The wire format itself is documented in HTTP API.

版本与发布记录

@alpamayo-solutions/colca-client 共发布 90 个版本,首次发布于 2026/09/17;过去 12 个月发布了 90 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.44.1latest2026/11/05今天
0.44.02026/11/042 天前
0.43.02026/10/316 天前
0.42.12026/10/289 天前
0.42.02026/10/289 天前
0.41.22026/10/279 天前

关于 @alpamayo-solutions/colca-client 的常见问题

@alpamayo-solutions/colca-client 是什么?

Client for a Colca node's door: fetch, ack, publish, kv, self.

如何安装 @alpamayo-solutions/colca-client?

在项目目录运行 npm install @alpamayo-solutions/colca-client,或使用 pnpm add / yarn add / bun add。

@alpamayo-solutions/colca-client 支持 TypeScript 吗?

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

@alpamayo-solutions/colca-client 支持 ESM 还是 CommonJS?

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

@alpamayo-solutions/colca-client 需要什么版本的 Node.js?

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

@alpamayo-solutions/colca-client 可以商用吗?

@alpamayo-solutions/colca-client 使用 SEE LICENSE IN LICENSE.md 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@alpamayo-solutions/colca-client 的下载量有多少?

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

@alpamayo-solutions/colca-client 还在维护吗?

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