@fungi.computer/cairn

中文文档
0.1.0发布于 2026/10/23MITTypeScriptES Module近 30 天下载 1.3万

Small, opaque identity and candidate-word primitives for Fungi packages

原始使用文档
文档目录7 节
  1. Install or set up
  2. Quick start
  3. Core concepts
  4. API reference
  5. Examples and checks
  6. Limits and errors
  7. Contribute and license

Cairn (@fungi.computer/cairn)

Cairn gives Fungi packages typed, opaque identifiers, temporary command and correlation keys, and candidate word names. Use it so you do not have to hand-roll your own ID format and parser. The package provides these values and their parsing rules. The calling domain owns persistence, uniqueness, authorization, and lifecycle policy.

Install or set up

npm install @fungi.computer/cairn

The package ships ESM only ("type": "module") and installs nanoid, Zod, and uuid as runtime dependencies. Import the root entrypoint for IDs and ephemeral keys together, or import @fungi.computer/cairn/id and @fungi.computer/cairn/words separately.

Quick start

This illustrative example creates one caller-owned ID family, generates an ID, and checks a decoded string without changing its spelling:

import { defineIdFamily } from "@fungi.computer/cairn/id";

const jobIds = defineIdFamily({
  family: "jobs",
  version: 1,
  kinds: { job: { prefix: "job" } },
});

const jobId = jobIds.job.generate();
const result = jobIds.job.parse(jobId);
if (!result.ok) throw new Error(`Invalid job ID: ${result.error.reason}`);

The result is a canonical job_ identifier. The runnable ID tests exercise family-scoped generation and parsing in src/id.test.ts.

Core concepts

defineIdFamily validates a trusted family declaration and returns one codec for each declared kind. Each codec has a branded TypeScript ID type, a Zod schema for an untrusted string boundary, and parse and is methods for a string that has already been decoded. Version 1 IDs have the form <prefix>_<21 lowercase base36 characters>. Parsing does not trim, decode, case-fold, or normalize the input. A rejected parse returns bounded metadata and a reason. It does not return the rejected value.

IDs do not declare database uniqueness, relationships, permissions, or entity lifecycle. The domain that declares a family owns those rules and any bounded collision retry.

The root also exports replayKey, deterministicReplayKey, correlationKey, and createCorrelationSequence. replayKey() mints a UUIDv7 for one command; retain it across an ambiguous retry. await deterministicReplayKey(domain, parts) derives the same replay-key brand using Web Crypto SHA-256 and length-prefixed tuple encoding in Node, browsers and Workers. Supply a stable host-owned domain and the complete authority scope and stored command identity so a restart derives the same key. A correlation key is a UUIDv4 for transport or provider correlation. A correlation sequence starts at 1 for one connection and throws when it reaches safe-integer exhaustion. None of these keys is a durable domain identity.

words() chooses a candidate name from Cairn's exported adjective and noun lists. It does not keep a registry or promise uniqueness. Callers choose a word count that fits their collision requirements.

API reference

See the generated API reference. The public entrypoints are @fungi.computer/cairn, @fungi.computer/cairn/id, and @fungi.computer/cairn/words.

Examples and checks

The test suite covers identifier grammar, ephemeral keys, and candidate words. After building, run the unit tests and the public ./id bundle-isolation proof:

pnpm --filter @fungi.computer/cairn test

test/runtime/id-only-proof.mjs verifies that the public ./id entrypoint does not include the word corpus. The focused tests are in src/id.test.ts, src/ephemeral-key.test.ts, and src/index.test.ts.

Limits and errors

ID generation needs secure randomness. If that source is unavailable, Cairn throws CairnGenerationError with the validated family and kind. Invalid trusted family declarations throw CairnDefinitionError. The caller must use the codec's schema at an untrusted wire boundary before passing data to parse.

The root nanoid(length) export is retained for callers that need a raw random string. Its finite length coercion follows the selected upstream runtime implementation. Use a bounded nonnegative integer for predictable results. LONG is a conventional size, not a maximum. Likewise, words({ count }) keeps legacy count coercion and does not impose a maximum. CPU and output grow with the requested size, so validate and bound untrusted sizing values before calling either helper. Use defineIdFamily for durable domain IDs.

Contribute and license

Cairn's license is MIT. See LICENSE. Its direct runtime dependencies are nanoid, Zod, and uuid. Consult their package distributions for their license terms.

版本与发布记录

@fungi.computer/cairn 共发布 3 个版本,首次发布于 2026/10/15;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.1.0latest2026/10/2323 天前
0.0.12026/10/151 个月前
0.0.0-stage2026/10/151 个月前

关于 @fungi.computer/cairn 的常见问题

@fungi.computer/cairn 是什么?

Small, opaque identity and candidate-word primitives for Fungi packages

如何安装 @fungi.computer/cairn?

在项目目录运行 npm install @fungi.computer/cairn,或使用 pnpm add / yarn add / bun add。

@fungi.computer/cairn 支持 TypeScript 吗?

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

@fungi.computer/cairn 支持 ESM 还是 CommonJS?

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

@fungi.computer/cairn 可以商用吗?

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

@fungi.computer/cairn 的下载量有多少?

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

@fungi.computer/cairn 还在维护吗?

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