UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

224 lines (223 loc) • 8.5 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { n as ignoreMissingPath } from "./errno-DFcoqIT9.mjs"; import { p as statIfExists } from "./path-existence-Co7-GyVr.mjs"; import { n as isNotFoundPathError, r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import { c as safePathSegmentHashed } from "./install-safe-path-DIFL0qYY.mjs"; import { m as validatePluginId, p as resolvePluginNpmProjectsDir, r as isPluginNpmProjectDir, s as resolveDefaultPluginNpmDir } from "./install-paths-Bwm0P5EG.mjs"; import { d as tryReadJsonSync } from "./json-files-Cb1h8pYs.mjs"; import fs from "node:fs"; import path from "node:path"; //#region src/plugins/managed-npm-retention-contract.ts /** Marker reason for packages preserved by an explicit `plugins uninstall --keep-files`. */ const RETAINED_MANAGED_NPM_KEEP_FILES_REASON = "removed-managed-npm-install-retained"; //#endregion //#region src/plugins/npm-project-roots.ts function sortPaths(e) { return e.toSorted((e, t) => e.localeCompare(t)); } /** Lists directory candidates; each consumer establishes its own package ownership. */ function listPluginNpmProjectCandidatesSync(e) { const t = resolvePluginNpmProjectsDir(e); try { return sortPaths(fs.readdirSync(t, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => path.join(t, e.name))); } catch (e) { if (isNotFoundPathError(e)) return []; throw e; } } /** Lists admitted projects with the manifest that established their managed ownership. */ function listManagedPluginNpmProjectsSync(e) { return listPluginNpmProjectCandidatesSync(e).flatMap((t) => { const n = tryReadJsonSync(path.join(t, "package.json")); if (!isRecord(n) || !isRecord(n.dependencies)) return []; return Object.keys(n.dependencies).some((n) => validatePluginId(n) === null && isPluginNpmProjectDir({ packageName: n, projectDir: t, npmDir: e })) ? [{ projectRoot: t, manifest: n }] : []; }); } /** Returns the root npm install plus all managed project npm roots. */ function listManagedPluginNpmRootsSync(e) { return [e, ...listManagedPluginNpmProjectsSync(e).map(({ projectRoot: e }) => e)]; } //#endregion //#region src/plugins/managed-npm-retention.ts const RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR = ".openclaw-retained-npm-installs"; /** A negative result avoids lease admission; markers still need the cleanup owner's checks. */ async function hasRetainedManagedNpmInstallCandidates(e) { const t = resolveDefaultPluginNpmDir(e); for (const e of [t, ...listPluginNpmProjectCandidatesSync(t)]) try { if ((await fs.promises.readdir(path.join(e, RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR))).length > 0) return true; } catch (e) { ignoreMissingPath(e); } return false; } function markerPreservesPackageFiles(e) { const t = JSON.parse(fs.readFileSync(e, "utf8")); return isRecord(t) && t.reason === "removed-managed-npm-install-retained"; } function resolveRetainedManagedNpmInstallPackageInfo(e) { const t = path.resolve(e); const n = path.basename(t); const r = path.dirname(t); const i = path.basename(r); const a = i.startsWith("@"); const o = a ? path.dirname(r) : r; if (path.basename(o) !== "node_modules") return null; const s = a ? `${i}/${n}` : n; if (!n || n === "." || !s.trim()) return null; const c = path.dirname(o); return { packageName: s, projectRoot: c, markerPath: path.join(c, RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR, `${safePathSegmentHashed(s)}.json`) }; } function resolveRetainedManagedNpmInstallMarkerPath(e) { const t = resolveRetainedManagedNpmInstallPackageInfo(e); if (!t) throw new Error("retained npm install marker requires a node_modules package directory"); return t.markerPath; } function hasRetainedManagedNpmInstallMarker(e) { const t = resolveRetainedManagedNpmInstallPackageInfo(e); return t ? fs.existsSync(t.markerPath) : false; } async function clearRetainedManagedNpmInstallMarker(e, t) { const n = resolveRetainedManagedNpmInstallPackageInfo(e); if (!n) return false; await t?.(); try { await fs.promises.rm(n.markerPath, { force: true }); } catch (e) { await t?.(); if (e.code === "ENOENT") return false; throw e; } await t?.(); try { await fs.promises.rmdir(path.dirname(n.markerPath)); } catch { await t?.(); } return true; } async function markRetainedManagedNpmInstall(e) { const t = resolveRetainedManagedNpmInstallPackageInfo(e.packageDir); if (!t) return false; if (!(await statIfExists(e.packageDir))?.isDirectory()) return false; await e.assertCurrent?.(); await fs.promises.mkdir(path.dirname(t.markerPath), { recursive: true }); await e.assertCurrent?.(); await fs.promises.writeFile(t.markerPath, `${JSON.stringify({ version: 1, pluginId: e.pluginId, retainedAt: (/* @__PURE__ */ new Date()).toISOString(), reason: e.reason }, null, 2)}\n`, "utf8"); return true; } /** Restore markers only while the same index transaction still owns compensation. */ async function restoreRetainedManagedNpmInstallMarkers(e) { for (const t of e.clearedMarkerSnapshots) { await e.assertCurrent(); await fs.promises.mkdir(path.dirname(t.markerPath), { recursive: true }); await e.assertCurrent(); await fs.promises.writeFile(t.markerPath, t.contents, "utf8"); } for (const t of e.createdMarkerPaths) { await e.assertCurrent(); await fs.promises.rm(t, { force: true }); } } function listManagedNpmPackageDirs(e) { const t = path.join(e, "node_modules"); let n; try { n = fs.readdirSync(t, { withFileTypes: true }); } catch (e) { if (e.code === "ENOENT") return []; throw e; } return n.flatMap((e) => { if (!e.isDirectory()) return []; if (!e.name.startsWith("@")) return [path.join(t, e.name)]; return fs.readdirSync(path.join(t, e.name), { withFileTypes: true }).filter((e) => e.isDirectory()).map((n) => path.join(t, e.name, n.name)); }); } function isOwnedManagedNpmProject(e) { return listManagedNpmPackageDirs(e.projectRoot).some((t) => { const n = resolveRetainedManagedNpmInstallPackageInfo(t); return Boolean(n && e.markerNames.has(path.basename(n.markerPath)) && isPluginNpmProjectDir({ npmDir: e.npmDir, packageName: n.packageName, projectDir: e.projectRoot })); }); } async function cleanupRetainedLegacyNpmPackages(e) { let t = 0; for (const n of listManagedNpmPackageDirs(e.npmRoot)) { if (!hasRetainedManagedNpmInstallMarker(n) || markerPreservesPackageFiles(resolveRetainedManagedNpmInstallMarkerPath(n)) || e.activeInstallPaths.some((e) => isPathInside(n, e))) continue; try { await e.assertCurrent?.(); await fs.promises.rm(n, { recursive: true, force: true }); await clearRetainedManagedNpmInstallMarker(n, e.assertCurrent); t += 1; } catch (t) { e.onError?.(t, n); } } return t; } async function cleanupRetainedManagedNpmInstallGenerations(e = {}) { const t = e.npmDir ?? resolveDefaultPluginNpmDir(e.env); const n = resolvePluginNpmProjectsDir(t); const r = Array.from(e.activeInstallPaths ?? [], (e) => path.resolve(e)); let i = 0; for (const a of [t, ...listPluginNpmProjectCandidatesSync(t)]) { if (path.resolve(a) === path.resolve(t)) { i += await cleanupRetainedLegacyNpmPackages({ npmRoot: a, activeInstallPaths: r, assertCurrent: e.assertCurrent, onError: e.onError }); continue; } const o = path.join(a, RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR); let s; try { s = fs.readdirSync(o, { withFileTypes: true }).filter((e) => e.isFile()); } catch (t) { if (t.code === "ENOENT") continue; e.onError?.(t, a); continue; } if (s.length === 0 || s.some((e) => markerPreservesPackageFiles(path.join(o, e.name))) || !isPathInside(n, a) || !isOwnedManagedNpmProject({ markerNames: new Set(s.map((e) => e.name)), npmDir: t, projectRoot: a }) || r.some((e) => isPathInside(a, e))) continue; try { await e.assertCurrent?.(); await fs.promises.rm(a, { recursive: true, force: true }); i += 1; } catch (t) { e.onError?.(t, a); } } return i; } //#endregion export { markRetainedManagedNpmInstall as a, restoreRetainedManagedNpmInstallMarkers as c, RETAINED_MANAGED_NPM_KEEP_FILES_REASON as d, hasRetainedManagedNpmInstallMarker as i, listManagedPluginNpmProjectsSync as l, clearRetainedManagedNpmInstallMarker as n, resolveRetainedManagedNpmInstallMarkerPath as o, hasRetainedManagedNpmInstallCandidates as r, resolveRetainedManagedNpmInstallPackageInfo as s, cleanupRetainedManagedNpmInstallGenerations as t, listManagedPluginNpmRootsSync as u };