UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

36 lines (35 loc) • 1.75 kB
import path from "node:path"; import fs from "node:fs/promises"; import { assertCanonicalPathWithinBase, resolveSafeInstallDir, safeDirName, safePathSegmentHashed } from "@openclaw/fs-safe/advanced"; //#region src/infra/install-safe-path.ts /** Returns the package basename for scoped npm names while preserving plain ids. */ function unscopedPackageName(e) { const t = e.trim(); return t.slice(t.lastIndexOf("/") + 1); } /** Matches a requested install id against either the full package name or unscoped basename. */ function packageNameMatchesId(e, t) { const n = t.trim(); if (!n) return false; const r = e.trim(); if (!r) return false; return n === r || n === unscopedPackageName(r); } const INSTALL_BASE_CHANGED_ERROR_MESSAGE = "install base directory changed during install"; async function assertInstallBaseStable(e) { if (!(await fs.stat(e.installBaseDir)).isDirectory()) throw new Error(INSTALL_BASE_CHANGED_ERROR_MESSAGE); if (await fs.realpath(e.installBaseDir) !== e.expectedRealPath) throw new Error(INSTALL_BASE_CHANGED_ERROR_MESSAGE); } async function resolveInstallPublishTarget(e) { const t = path.resolve(e.installBaseDir); const n = path.resolve(e.targetDir); const r = path.relative(t, n); if (!r || r === ".." || r.startsWith(`..${path.sep}`)) throw new Error("invalid install target path"); const i = await fs.realpath(e.installBaseDir); return { installBaseRealPath: i, canonicalTargetDir: path.join(i, r) }; } //#endregion export { resolveInstallPublishTarget as a, safePathSegmentHashed as c, packageNameMatchesId as i, unscopedPackageName as l, assertCanonicalPathWithinBase as n, resolveSafeInstallDir as o, assertInstallBaseStable as r, safeDirName as s, INSTALL_BASE_CHANGED_ERROR_MESSAGE as t };