everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
74 lines (72 loc) • 2.9 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
let node_path = require("node:path");
node_path = require_runtime.__toESM(node_path, 1);
let node_crypto = require("node:crypto");
let node_fs_promises = require("node:fs/promises");
//#region src/bundle-cache.ts
/**
* Bundle disk cache (ADR 0011 amendment — foreign-namespace resilience):
* the child tier loads base workspaces from foreign origins; this cache
* keeps the last-known-good bytes so an origin outage degrades to stale
* serving instead of a hard failure. One cache root, two entrances: the
* host's `/bundles/*` proxy handler (browser traffic) and the CLI fetch
* adapter (boot-time outbound). Keyed exactly like the staged layout —
* `<account>/<gateway>/<rest>` — but a separate root from BOS_BUNDLE_DIR:
* cached bytes are a resilience artifact, never a deployment (plan 043 —
* never point a runtime's BOS_BUNDLE_DIR at another runtime's layout).
*/
function bundleCacheRoot(input) {
return node_path.default.resolve(input?.cacheDir ?? process.env.BOS_BUNDLE_CACHE_DIR ?? joinDefault());
}
function joinDefault() {
return node_path.default.join(process.cwd(), ".bos", "bundle-cache");
}
function bundleCachePath(url, input) {
let parsed;
try {
parsed = new URL(url);
} catch {
return null;
}
if (parsed.protocol !== "https:" && parsed.protocol !== "http:") return null;
const match = /^\/bundles\/([^/]+)\/([^/]+)\/(.+)$/.exec(parsed.pathname);
if (!match) return null;
const [, account, gateway, rest] = match;
if (!rest || rest.endsWith("/")) return null;
let decoded;
try {
decoded = decodeURIComponent(rest);
} catch {
return null;
}
const root = node_path.default.resolve(bundleCacheRoot(input));
const filePath = node_path.default.resolve(root, account, gateway, decoded);
if (!filePath.startsWith(root + node_path.default.sep)) return null;
return filePath;
}
async function readBundleCache(url, input) {
const filePath = bundleCachePath(url, input);
if (!filePath) return null;
try {
return new Uint8Array(await (0, node_fs_promises.readFile)(filePath));
} catch {
return null;
}
}
async function writeBundleCache(url, bytes, input) {
const filePath = bundleCachePath(url, input);
if (!filePath) return;
try {
await (0, node_fs_promises.mkdir)(node_path.default.dirname(filePath), { recursive: true });
const tmp = `${filePath}.${process.pid}.${(0, node_crypto.createHash)("sha256").update(bytes).digest("hex").slice(0, 8)}.tmp`;
await (0, node_fs_promises.writeFile)(tmp, bytes);
await (0, node_fs_promises.rename)(tmp, filePath);
} catch {}
}
//#endregion
exports.bundleCachePath = bundleCachePath;
exports.bundleCacheRoot = bundleCacheRoot;
exports.readBundleCache = readBundleCache;
exports.writeBundleCache = writeBundleCache;
//# sourceMappingURL=bundle-cache.cjs.map