UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

73 lines (71 loc) • 2.75 kB
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; //#region src/services/registry/cache.ts /** * The manifest cache — the stand-in layer, in one place. A cached resolution * stands in for a live fetch only while it is fresh (the #546 machinery): * pin-match required when the resolution is pinned, TTL-bounded always. The * cache exists for offline dev ONLY — never stale-pin grace: slot-first * resolution makes old pins resolvable forever from immutable slots, so a * cache can no longer be the reason a pin resolves. Without the freshness * bound a registry outage (or a cache written before the parent's latest * publish) silently serves a months-old release manifest — pins derive from * it and the mismatch can take an hour to diagnose. Past the TTL the cache * is refused: pin-less resolution degrades to "no manifest"; pinned * resolution fails loud with the slot key named. * * Storage: one record at `<configDir>/.bos/platform-manifest.json` (the * rename is ticket 08; this module only owns the semantics). The record's * wire shape (`kind`, fields) is unchanged from the pre-module layout so * existing on-disk caches keep reading. */ const MANIFEST_CACHE_TTL_MS = 864e5; function defaultCachePath(configDir) { return join(configDir, ".bos", "platform-manifest.json"); } function defaultRead(configDir) { try { if (!existsSync(defaultCachePath(configDir))) return null; const raw = JSON.parse(readFileSync(defaultCachePath(configDir), "utf-8")); return raw?.kind === "everything-dev/platform-manifest-cache" ? raw : null; } catch { return null; } } /** * The freshness predicate: `resolvedAt` inside the TTL, parseable (a cache * with an unparseable timestamp is stale — never trusted), and pin-matched * when a pin is in force. */ function readManifestCache(opts) { const record = opts.read ? opts.read() : defaultRead(opts.configDir); if (!record) return { state: "miss" }; const now = (opts.now ?? Date.now)(); const ttl = opts.ttlMs ?? 864e5; const resolvedAt = Date.parse(record.resolvedAt); if (Number.isNaN(resolvedAt) || now - resolvedAt > ttl) return { state: "stale", record }; if (opts.pin !== void 0 && record.manifest.frameworkVersion !== opts.pin) return { state: "stale", record }; return { state: "hit", record }; } function writeManifestCache(opts) { try { if (opts.write) { opts.write(opts.record); return; } mkdirSync(join(opts.configDir, ".bos"), { recursive: true }); writeFileSync(defaultCachePath(opts.configDir), `${JSON.stringify(opts.record, null, 2)}\n`); } catch {} } //#endregion export { MANIFEST_CACHE_TTL_MS, readManifestCache, writeManifestCache }; //# sourceMappingURL=cache.mjs.map