{"version":3,"file":"keys.mjs","names":[],"sources":["../../../src/services/registry/keys.ts"],"sourcesContent":["/**\n * The registry's typed key model — the one derivation every reader and\n * writer shares. A key is a closed union; its wire form (`keyPathOf`) is the\n * exact FastKV key string (`apps/<account>/<gateway>/…`). Consumers never\n * hand-build registry paths: the golden tests pin the wire forms, and the\n * invariant they encode is the one this module exists to make provable —\n * the account segment is the signing namespace (`dev.everything.near`), the\n * gateway segment is the runtime's domain (`everything.dev`), and nothing\n * that reads or writes may derive one from the other.\n *\n * Vocabulary (the sprint's decision ledger, .scratch/registry-primitive):\n * - `RuntimeRef` — `bos://<account>/<gateway>`: a runtime's identity.\n * - `RuntimeConfig` — the runtime's composition (bos.config.json).\n * - `Manifest` — the moving release tag (manifest.json, thin: version + digest).\n * - `Version` — the immutable per-release record (versions/<v>.json).\n * - `DeployRecord` — the per-deploy audit trail (deploys/<ts>.json).\n */\n\nexport type RuntimeRef = `bos://${string}/${string}`;\n\nexport interface RuntimeRefParts {\n  accountId: string;\n  gatewayId: string;\n}\n\nexport type RegistryKey =\n  | { readonly kind: \"runtimeConfig\"; readonly ref: RuntimeRef; readonly path?: readonly string[] }\n  | { readonly kind: \"manifest\"; readonly ref: RuntimeRef }\n  | { readonly kind: \"version\"; readonly ref: RuntimeRef; readonly version: string }\n  | { readonly kind: \"deployRecord\"; readonly ref: RuntimeRef; readonly ts: string };\n\nexport class RegistryRefError extends Error {\n  constructor(message: string) {\n    super(message);\n    this.name = \"RegistryRefError\";\n  }\n}\n\n/** `runtimeRefOf({ accountId, gatewayId })` — the runtime identity. */\nexport function runtimeRefOf(parts: RuntimeRefParts): RuntimeRef {\n  if (!parts.accountId || !parts.gatewayId) {\n    throw new RegistryRefError(\n      `Invalid runtime ref: account \"${parts.accountId}\", gateway \"${parts.gatewayId}\" — both segments are required`,\n    );\n  }\n  return `bos://${parts.accountId}/${parts.gatewayId}`;\n}\n\n/**\n * Parse a runtime ref into its parts. Extra path segments beyond\n * `<gateway>` are the config sub-path (`bos://a/g/team/app` →\n * `path: [\"team\", \"app\"]`); a `#fragment` is stripped. Throws\n * `RegistryRefError` on any malformed ref.\n */\nexport function parseRuntimeRef(ref: RuntimeRef | string): RuntimeRefParts & { path: string[] } {\n  const stripped = ref.split(\"#\")[0]!;\n  const match = stripped.match(/^bos:\\/\\/([^/]+)\\/(.+)$/);\n  if (!match?.[1] || !match[2]) {\n    throw new RegistryRefError(`Invalid runtime ref: ${ref}`);\n  }\n  const segments = match[2]\n    .split(\"/\")\n    .filter(Boolean)\n    .map((segment) => decodeURIComponent(segment));\n  const [gatewayId, ...path] = segments;\n  if (!gatewayId) {\n    throw new RegistryRefError(`Invalid runtime ref: ${ref}`);\n  }\n  return { accountId: match[1]!, gatewayId, path };\n}\n\nconst singleSegment = (value: string, label: string): string => {\n  if (!value || value.includes(\"/\")) {\n    throw new RegistryRefError(\n      `Invalid registry key: ${label} \"${value}\" must be one path segment`,\n    );\n  }\n  return value;\n};\n\n/** The exact wire key for a registry key — raw (unencoded); the client\n * encodes it as a single path segment per the FastKV read API. */\nexport function keyPathOf(key: RegistryKey): string {\n  const { accountId, gatewayId } = parseRuntimeRef(key.ref);\n  switch (key.kind) {\n    case \"runtimeConfig\": {\n      const suffix = (key.path ?? []).map((segment) => singleSegment(segment, \"config path\"));\n      return [\"apps\", accountId, gatewayId, ...suffix, \"bos.config.json\"].join(\"/\");\n    }\n    case \"manifest\":\n      return [\"apps\", accountId, gatewayId, \"manifest.json\"].join(\"/\");\n    case \"version\":\n      return [\n        \"apps\",\n        accountId,\n        gatewayId,\n        \"versions\",\n        `${singleSegment(key.version, \"version\")}.json`,\n      ].join(\"/\");\n    case \"deployRecord\":\n      return [\n        \"apps\",\n        accountId,\n        gatewayId,\n        \"deploys\",\n        `${singleSegment(key.ts, \"deploy timestamp\")}.json`,\n      ].join(\"/\");\n  }\n}\n"],"mappings":";AA+BA,IAAa,mBAAb,cAAsC,MAAM;CAC1C,YAAY,SAAiB;EAC3B,MAAM,OAAO;EACb,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