everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
1 lines • 5.17 kB
Source Map (JSON)
{"version":3,"file":"cache.mjs","names":[],"sources":["../../../src/services/registry/cache.ts"],"sourcesContent":["/**\n * The manifest cache — the stand-in layer, in one place. A cached resolution\n * stands in for a live fetch only while it is fresh (the #546 machinery):\n * pin-match required when the resolution is pinned, TTL-bounded always. The\n * cache exists for offline dev ONLY — never stale-pin grace: slot-first\n * resolution makes old pins resolvable forever from immutable slots, so a\n * cache can no longer be the reason a pin resolves. Without the freshness\n * bound a registry outage (or a cache written before the parent's latest\n * publish) silently serves a months-old release manifest — pins derive from\n * it and the mismatch can take an hour to diagnose. Past the TTL the cache\n * is refused: pin-less resolution degrades to \"no manifest\"; pinned\n * resolution fails loud with the slot key named.\n *\n * Storage: one record at `<configDir>/.bos/platform-manifest.json` (the\n * rename is ticket 08; this module only owns the semantics). The record's\n * wire shape (`kind`, fields) is unchanged from the pre-module layout so\n * existing on-disk caches keep reading.\n */\nimport { existsSync, mkdirSync, readFileSync, writeFileSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport type { PlatformReleaseManifest } from \"../platform/manifest-schema\";\n\nexport interface ManifestCacheRecord {\n schemaVersion: 1;\n kind: \"everything-dev/platform-manifest-cache\";\n resolvedAt: string;\n source: { account: string; gateway: string };\n manifest: PlatformReleaseManifest;\n provenance?: \"slot\" | \"pointer\";\n}\n\nexport const MANIFEST_CACHE_TTL_MS = 24 * 60 * 60_000;\n\nexport type ManifestCacheRead =\n | { state: \"miss\" }\n | { state: \"stale\"; record: ManifestCacheRecord }\n | { state: \"hit\"; record: ManifestCacheRecord };\n\nexport interface ReadManifestCacheOptions {\n configDir: string;\n /** When set, a fresh cache only hits if the record IS that release — the\n * pin-match half of the #546 predicate. */\n pin?: string;\n now?: () => number;\n ttlMs?: number;\n /** test seam — replaces the file read */\n read?: () => ManifestCacheRecord | null;\n}\n\nexport interface WriteManifestCacheOptions {\n configDir: string;\n record: ManifestCacheRecord;\n /** test seam — replaces the file write */\n write?: (record: ManifestCacheRecord) => void;\n}\n\nfunction defaultCachePath(configDir: string): string {\n return join(configDir, \".bos\", \"platform-manifest.json\");\n}\n\nfunction defaultRead(configDir: string): ManifestCacheRecord | null {\n try {\n if (!existsSync(defaultCachePath(configDir))) return null;\n const raw = JSON.parse(\n readFileSync(defaultCachePath(configDir), \"utf-8\"),\n ) as ManifestCacheRecord;\n return raw?.kind === \"everything-dev/platform-manifest-cache\" ? raw : null;\n } catch {\n return null;\n }\n}\n\n/**\n * The freshness predicate: `resolvedAt` inside the TTL, parseable (a cache\n * with an unparseable timestamp is stale — never trusted), and pin-matched\n * when a pin is in force.\n */\nexport function readManifestCache(opts: ReadManifestCacheOptions): ManifestCacheRead {\n const record = opts.read ? opts.read() : defaultRead(opts.configDir);\n if (!record) return { state: \"miss\" };\n const now = (opts.now ?? Date.now)();\n const ttl = opts.ttlMs ?? MANIFEST_CACHE_TTL_MS;\n const resolvedAt = Date.parse(record.resolvedAt);\n if (Number.isNaN(resolvedAt) || now - resolvedAt > ttl) return { state: \"stale\", record };\n if (opts.pin !== undefined && record.manifest.frameworkVersion !== opts.pin) {\n return { state: \"stale\", record };\n }\n return { state: \"hit\", record };\n}\n\nexport function writeManifestCache(opts: WriteManifestCacheOptions): void {\n try {\n if (opts.write) {\n opts.write(opts.record);\n return;\n }\n mkdirSync(join(opts.configDir, \".bos\"), { recursive: true });\n writeFileSync(defaultCachePath(opts.configDir), `${JSON.stringify(opts.record, null, 2)}\\n`);\n } catch {\n // cache writes never fail the resolution\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,wBAAwB;AAyBrC,SAAS,iBAAiB,WAA2B;CACnD,OAAO,KAAK,WAAW,QAAQ,wBAAwB;AACzD;AAEA,SAAS,YAAY,WAA+C;CAClE,IAAI;EACF,IAAI,CAAC,WAAW,iBAAiB,SAAS,CAAC,GAAG,OAAO;EACrD,MAAM,MAAM,KAAK,MACf,aAAa,iBAAiB,SAAS,GAAG,OAAO,CACnD;EACA,OAAO,KAAK,SAAS,2CAA2C,MAAM;CACxE,QAAQ;EACN,OAAO;CACT;AACF;;;;;;AAOA,SAAgB,kBAAkB,MAAmD;CACnF,MAAM,SAAS,KAAK,OAAO,KAAK,KAAK,IAAI,YAAY,KAAK,SAAS;CACnE,IAAI,CAAC,QAAQ,OAAO,EAAE,OAAO,OAAO;CACpC,MAAM,OAAO,KAAK,OAAO,KAAK,IAAG,CAAE;CACnC,MAAM,MAAM,KAAK;CACjB,MAAM,aAAa,KAAK,MAAM,OAAO,UAAU;CAC/C,IAAI,OAAO,MAAM,UAAU,KAAK,MAAM,aAAa,KAAK,OAAO;EAAE,OAAO;EAAS;CAAO;CACxF,IAAI,KAAK,QAAQ,UAAa,OAAO,SAAS,qBAAqB,KAAK,KACtE,OAAO;EAAE,OAAO;EAAS;CAAO;CAElC,OAAO;EAAE,OAAO;EAAO;CAAO;AAChC;AAEA,SAAgB,mBAAmB,MAAuC;CACxE,IAAI;EACF,IAAI,KAAK,OAAO;GACd,KAAK,MAAM,KAAK,MAAM;GACtB;EACF;EACA,UAAU,KAAK,KAAK,WAAW,MAAM,GAAG,EAAE,WAAW,KAAK,CAAC;EAC3D,cAAc,iBAAiB,KAAK,SAAS,GAAG,GAAG,KAAK,UAAU,KAAK,QAAQ,MAAM,CAAC,EAAE,GAAG;CAC7F,QAAQ,CAER;AACF"}