UNPKG

everything-dev

Version:

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

88 lines (86 loc) 3.13 kB
const require_runtime = require('./_virtual/_rolldown/runtime.cjs'); let node_fs = require("node:fs"); let node_path = require("node:path"); let effect = require("effect"); let node_fs_promises = require("node:fs/promises"); let node_os = require("node:os"); //#region src/process-registry.ts function getRegistryDir() { return (0, node_path.join)((0, node_os.homedir)(), ".cache", "everything-dev"); } function ensureRegistryDir() { const dir = (0, node_path.join)(getRegistryPath(), ".."); if (!(0, node_fs.existsSync)(dir)) (0, node_fs.mkdirSync)(dir, { recursive: true }); } function readRegistry() { const path = getRegistryPath(); if (!(0, node_fs.existsSync)(path)) return []; try { const raw = JSON.parse((0, node_fs.readFileSync)(path, "utf-8")); if (!Array.isArray(raw)) return []; return raw.filter((entry) => entry && typeof entry === "object" && typeof entry.pid === "number" && typeof entry.configDir === "string" && typeof entry.role === "string"); } catch { return []; } } function isPidAlive(pid) { try { process.kill(pid, 0); return true; } catch (err) { return err.code !== "ESRCH"; } } function pruneDead(entries) { return entries.filter((entry) => entry.pid > 1 && (0, node_fs.existsSync)(entry.configDir) && isPidAlive(entry.pid)); } function pruneDeadEffect(entries) { return effect.Effect.forEach(entries, (entry) => { if (entry.pid <= 1) return effect.Effect.succeed(null); return effect.Effect.gen(function* () { if (!(yield* effect.Effect.promise(() => (0, node_fs_promises.access)(entry.configDir).then(() => true).catch(() => false)))) return null; if (!isPidAlive(entry.pid)) return null; return entry; }); }, { concurrency: "unbounded" }).pipe(effect.Effect.map((results) => results.filter((e) => e !== null))); } function writeRegistry(entries) { ensureRegistryDir(); const path = getRegistryPath(); const tmpPath = `${path}.tmp`; (0, node_fs.writeFileSync)(tmpPath, `${JSON.stringify(entries, null, 2)}\n`); (0, node_fs.renameSync)(tmpPath, path); } function registerStandalone(entry) { const live = pruneDead(readRegistry()); const full = { ...entry, role: "standalone" }; const withoutSelf = live.filter((existing) => existing.pid !== entry.pid); withoutSelf.push(full); writeRegistry(withoutSelf); return full; } function unregisterPid(pid) { const live = pruneDead(readRegistry()); const next = live.filter((entry) => entry.pid !== pid); if (next.length === live.length) return; writeRegistry(next); } function getRegistryPath() { return process.env.BO_PID_REGISTRY_PATH ?? (0, node_path.join)(getRegistryDir(), "pids.json"); } function claimedPorts() { const live = pruneDead(readRegistry()); const out = /* @__PURE__ */ new Set(); for (const entry of live) for (const port of Object.values(entry.ports)) if (typeof port === "number") out.add(port); return out; } //#endregion exports.claimedPorts = claimedPorts; exports.pruneDeadEffect = pruneDeadEffect; exports.readRegistry = readRegistry; exports.registerStandalone = registerStandalone; exports.unregisterPid = unregisterPid; //# sourceMappingURL=process-registry.cjs.map