@hadron-memory/urn-lib-js

中文文档
0.0.22发布于 2026/11/14MITTypeScriptES Module零依赖近 30 天下载 1.2万

Hadron URN compose/parse/normalize — fixture-corpus-driven, cross-language parity with urn-lib-go

原始使用文档
文档目录11 节
  1. Status
  2. Getting a loc: use the decomposers, not pathSegments
  3. Usage
  4. The conformance corpus
  5. Tickers and agent tasks (grammar v2)
  6. App events (grammar v2)
  7. Grants (grammar v2)
  8. Root registration policy
  9. Actions (grammar v2)
  10. AI service configs (grammar v2)
  11. License

@hadron-memory/urn-lib-js

Hadron URN compose / parse / normalize for JavaScript & TypeScript.

Paired with urn-lib-go. Both implementations run the same conformance corpus (fixtures/corpus.json) — the corpus is the contract, so the two languages cannot drift. This exists to replace the copy-pasted URN parsers that had already drifted across hadron-server / portal / docs / cli (hadron-server#239, #693).

Status

Increment 1 — v1-parity core. This release ports the pure, self-contained slice of hadron-server/src/lib/urn.ts verbatim, behind the shared corpus:

  • scheme — CANONICAL_SCHEME, LEGACY_SCHEME, hasSchemePrefix, normalizeScheme
  • registry — the locked type registry (URN_TYPES, ROLE_MARKERS, RESERVED_SLUGS, …)
  • normalize — normalizeUrnForLookup, legacyMemoryUrnToCanonical, agentSlugFromUrn
  • slug — validateAtomShape, validateUserSlug, validateOrgSlug (requires a dotted root, #692), validateUserHandle (dot-free, #692), deriveSlugFromName
  • legacy — parseUrnInput, formatUrn, validateUrnType (the pre-021 surface)
  • parser — parseUrn (the canonical-form parser, D11 cats 1 + 4), isParserCanonical, toParserCanonical
  • compose — formatCanonicalUrn, composeNodeUrn, composeEdgeUrn
  • display — parseDisplayUrn, DISPLAY_URN_TYPES (the tolerant spec-010 chip parser/registry — separate from the strict canonical parser; falls back to unknown for unregistered display kinds)
  • errors — UrnParseError + the machine-stable UrnParseErrorReason union

Grammar-v2 flat forms (hadron-server#694) are ported: parseUrnV2 / composeUrnV2 / isFlatV2 over hrn:<type>:<root>[:<segment>...] (single colon, no sigil), additive to the v1 surface.

Per-entity v2 shapes (hadron-server#696, decision D-2026-07-15-006) are ported too:

  • secret — two shapes, and the arity decides which: hrn:secret:<root>:<name> (org/user root + exactly one name atom) and the App secret hrn:secret:<root>:app:<app-slug>:<name> (the literal lowercase app marker, then the App's slug and the secret's name; urn-lib-js#40). hrn:secret:<root>:app is still the flat secret named app. Two parts, four or more parts, and any other marker (APP, memory, org, agent, user) are refused, and the App shape never sits under a reserved root (server, platform). The #679 :: spelling <root>::<app|memory>:<slug>::<name> is refused by parseUrn, never normalized. Build with composeAppSecretUrnV2; parseSecretUrnV2 says whose it is ({ownerKind: 'root', root, name} or {ownerKind: 'app', root, appSlug, name}), so consumers never re-derive the arity rule.
  • apprun — hrn:apprun:<root>:<app>:<run-id> (fixed arity).
  • noderev — hrn:noderev:<root>:<mem>:<loc...>:<rev>, end-anchored (last atom is the revision id); decompose with parseNodeRevUrnV2.
  • node / edge — hrn:<type>:<root>:<mem>:<loc...> (a memory + at least one loc atom); the loc is an opaque terminal (an edge loc is never re-split into source:target).
  • #data fragment — <node-or-apprun-urn>#data (node-data is a fragment of its parent, not a standalone type). composeDataFragmentV2 always emits a canonical hrn: URN, even from a legacy urn:-scheme parent.

#696 demoted the v1 node-part type words (data, condition) to fragments. That changed the spelling, not the meaning — so parseUrn maps a fragmented flat-v2 URN onto the fragment's v1 type word: hrn:node:<root>:<mem>:<loc>#data parses as type: 'data' over the parent's path, which is exactly the v1 hrn:data:<root>::<mem>::<loc> reading of the same resource. parserCanonical keeps the v2 form (fragment included), so it round-trips. A fragment on a v2-only parent (apprun, worker) has no v1 type equivalent and keeps its unknown-type error; reach for parseUrnV2 when you need the parent type and fragment as separate fields.

Typed helpers: composeSecretUrnV2, composeAppSecretUrnV2, parseSecretUrnV2, composeAppRunUrnV2, composeNodeRevUrnV2, composeDataFragmentV2, and parseNodeRevUrnV2.

Not yet ported (later increments, gated by the same corpus): legacy chain→flat normalization + the stored alias map (hadron-server#697) and the unified principal-pool enforcement (hadron-server#692).

Getting a loc: use the decomposers, not pathSegments

ParsedUrn.pathSegments is a raw split whose shape follows the input's grammar (#12). v1 input is split on ::, so a segment may carry an internal :; flat-v2 input is split on the single :, so every atom is its own element. The two spellings of one resource therefore differ:

parseUrn('hrn:node:acme.com::specs::cor:urn').pathSegments  // ['acme.com', 'specs', 'cor:urn']
parseUrn('hrn:node:acme.com:specs:cor:urn').pathSegments    // ['acme.com', 'specs', 'cor', 'urn']

This affects every type whose v1 form permits an internal : inside a segment — memory (valued role markers like app-user:<id>), node, and edge. org, user, agent, app, secret and asset are identical under both grammars.

splitNodeUrn / splitEdgeUrn normalize across grammars and report a #data fragment separately, so a caller never has to know which grammar it was handed:

splitNodeUrn('hrn:node:acme.com::specs::cor:urn');       // { memoryUrn: 'acme.com:specs', loc: 'cor:urn' }
splitNodeUrn('hrn:node:acme.com:specs:cor:urn');         // { memoryUrn: 'acme.com:specs', loc: 'cor:urn' }
splitNodeUrn('hrn:node:acme.com:specs:cor:urn#data');    // { …, loc: 'cor:urn', fragment: 'data' }

An edge loc is an opaque terminal — splitEdgeUrn never re-splits it into source:target. Use parseUrnV2 when you want the v2 root / segments / fragment as separate fields.

When a fragment is the only thing wrong with a URN, parseUrn says so with the fragment-unsupported reason rather than leaking the v1 parser's complaint about the # character:

parseUrn('hrn:node:acme.com:mem:loc#bogus');  // UrnParseError, reason 'fragment-unsupported'
parseUrn('hrn:worker:acme.com:eng:iris#data'); // 'unknown-type' — worker has no v1 form either way

The test is whether removing the fragment would make the input parse. If it wouldn't, the original reason stands, so fixing what the error names always moves you forward.

Both return NodeLikeUrnParts (node and edge share one shape). Under v1 the memory may be multi-segment, and the terminal :: segment is the loc:

splitNodeUrn('hrn:node:mm.org::coding-app::coding-agent::app-mem::a:b');
// { memoryUrn: 'mm.org:coding-app:coding-agent:app-mem', loc: 'a:b' }

Both are self-validating: they reject an unregistered fragment word and a fragment on an edge (only node/apprun may parent one), so they are never more permissive than parseUrn for the same input.

Known gap: there is no splitMemoryUrn yet. v2 leaves mem arity unconstrained, so hrn:mem:<root>:<a>:<b>:<c> cannot be split into containers vs leaf until the v2 spec pins it (hadron-server#698).

Usage

import { normalizeUrnForLookup, validateOrgSlug, UrnParseError } from '@hadron-memory/urn-lib-js';

normalizeUrnForLookup('acme.com::specs::cor:urn'); // 'acme.com:specs:cor:urn'

try {
  validateOrgSlug('Acme.com');
} catch (e) {
  if (e instanceof UrnParseError) console.log(e.reason); // 'slug-not-lowercase'
}

The conformance corpus

fixtures/corpus.json is the source of truth for behavior. Each case is { fn, in: [args], out?, throws? } — out is the expected return, throws is the expected UrnParseError.reason, and neither means a void call that must not throw. test/corpus.test.ts runs every case against this implementation; urn-lib-go runs the identical file. Add behavior by adding a corpus case, not by editing a test in one language.

npm is the package manager of record. package-lock.json is the only lockfile in the tree — don't add a second one.

npm install
npm test          # runs the corpus
npm run build

Tickers and agent tasks (grammar v2)

hrn:ticker:<root>:<app-slug>:<ticker-slug> and hrn:agenttask:<root>:<app-slug>:<task-id> each have exactly two segments after the root. Use composeTickerUrnV2 with the ticker's permanent derived slug and composeAgentTaskUrnV2 with the task's id. Both parse through parseUrnV2, render their own display badge, and normalize a legacy urn: prefix to hrn:. They have no v1 equivalent; parseUrn rejects them with unknown-type.

Task ids follow the shared URN atom rule (letters/digits, dots, underscores and hyphens, at most 64 characters). A 32-hex or cuid id is valid in the task-id leaf; root-name registration restrictions do not apply to that leaf.

App events (grammar v2)

hrn:appevent:<root>:<app-slug>:<event-id> addresses one event in an App's event stream (cor:urn:010:01). It has exactly two segments after the root, like hrn:agenttask. Use composeAppEventUrnV2 with the event's id. It parses through parseUrnV2, renders its own display badge, normalizes a legacy urn: prefix to hrn:, and refuses a #data fragment. It is v2-only: parseUrn rejects it with unknown-type. The event id is one URN atom (letters/digits, dots, underscores and hyphens, at most 64 characters).

Grants (grammar v2)

hrn:grant:<root>:<app-slug>:<grant-id> addresses one grant in an App (cor:urn:010:01, Grant row; ruling 0255 item 9). It has exactly two segments after the root, like hrn:appevent, with the App slug as the container and the grant's id as the leaf. Use composeGrantUrnV2 with the grant's id. It parses through parseUrnV2, renders its own display badge, normalizes a legacy urn: prefix to hrn:, and refuses a #data fragment. The grant id is one ordinary URN atom (letters/digits, dots, underscores and hyphens, at most 64 characters); there is no UUID-only rule. No other arity is defined, so an org-wide grant has no URN here. Today parseUrn (v1) rejects hrn:grant with unknown-type; that is current behaviour, not a ruled rejection (grant being greenfield v2-only is still Proposed in cor:urn:010:01).

Root registration policy

validateOrgSlug admits dotted and undotted organization names. Both it and validateUserHandle reject 32-hex/cuid-shaped roots (root-id-shaped) and the reserved root words hrn/urn (reserved-word-slug). New handles remain dot-free. Bare-name, lowercase, charset and length checks remain in place.

This is create/rename policy. Generic slug validation, URN parsing and composition remain lenient so existing stored URNs and ID-shaped task leaves still round-trip. The obsolete org-root-not-dotted reason is retired.

Actions (grammar v2)

hrn:action:<root>:<agent-slug>:<code> has exactly two segments after the root: the Team Agent's slug and the action's code (cor:urn:010:01). Use composeActionUrnV2. It parses through parseUrnV2, renders its own display badge, and normalizes a legacy urn: prefix to hrn:. It has no v1 equivalent; parseUrn rejects it with unknown-type. Both segments follow the shared URN atom rule (letters/digits, dots, underscores and hyphens, at most 64 characters).

AI service configs (grammar v2)

hrn:aiconf:<owner-root>:<owner-kind>[:<owner-slug>]:<name>: the owner kind (an exact, lowercase first segment) decides the shape (cor:urn:010:01). org takes org:<name> (hrn:aiconf:acme.com:org:default); app and agent take <kind>:<owner-slug>:<name> (hrn:aiconf:acme.com:app:mealplan:default). Any other kind is refused as invalid-segment-shape, server included: a HadronServer owner has no URN form, so the lib builds nothing for it. Under the reserved roots server and platform the config takes exactly one part, the name (hrn:aiconf:server:default); no owner kind follows. A name is one atom under the ordinary rule, so one ending in - or _ is refused. Build with composeUrnV2('aiconf', root, ...parts). The lib checks shape only: whether the kind matches the config's real owner is resolution (hadron-server#3857).

License

MIT © Baragaun, Inc.

版本与发布记录

@hadron-memory/urn-lib-js 共发布 17 个版本,首次发布于 2026/07/16;过去 12 个月发布了 17 个版本。

版本发布日期距今
0.0.22latest2026/11/143 天前
0.0.182026/11/0116 天前
0.0.172026/10/121 个月前
0.0.152026/08/252 个月前
0.0.132026/08/163 个月前
0.0.122026/08/163 个月前

关于 @hadron-memory/urn-lib-js 的常见问题

@hadron-memory/urn-lib-js 是什么?

Hadron URN compose/parse/normalize — fixture-corpus-driven, cross-language parity with urn-lib-go

如何安装 @hadron-memory/urn-lib-js?

在项目目录运行 npm install @hadron-memory/urn-lib-js,或使用 pnpm add / yarn add / bun add。

@hadron-memory/urn-lib-js 支持 TypeScript 吗?

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

@hadron-memory/urn-lib-js 支持 ESM 还是 CommonJS?

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

@hadron-memory/urn-lib-js 可以商用吗?

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

@hadron-memory/urn-lib-js 的下载量有多少?

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

@hadron-memory/urn-lib-js 还在维护吗?

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