everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
75 lines (73 loc) • 2.87 kB
JavaScript
let node_fs = require("node:fs");
let node_path = require("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 (0, node_path.join)(configDir, ".bos", "platform-manifest.json");
}
function defaultRead(configDir) {
try {
if (!(0, node_fs.existsSync)(defaultCachePath(configDir))) return null;
const raw = JSON.parse((0, node_fs.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;
}
(0, node_fs.mkdirSync)((0, node_path.join)(opts.configDir, ".bos"), { recursive: true });
(0, node_fs.writeFileSync)(defaultCachePath(opts.configDir), `${JSON.stringify(opts.record, null, 2)}\n`);
} catch {}
}
//#endregion
exports.MANIFEST_CACHE_TTL_MS = MANIFEST_CACHE_TTL_MS;
exports.readManifestCache = readManifestCache;
exports.writeManifestCache = writeManifestCache;
//# sourceMappingURL=cache.cjs.map