fix: shorten managed npm generation paths (#97488)

This commit is contained in:
ooiuuii
2026-06-28 11:58:16 -07:00
committed by GitHub
parent 245c18da5a
commit 3c826ed5c9
2 changed files with 69 additions and 1 deletions
+60
View File
@@ -0,0 +1,60 @@
// Covers managed plugin install path generation.
import path from "node:path";
import { describe, expect, it } from "vitest";
import {
resolvePluginNpmGenerationProjectDir,
resolvePluginNpmGenerationProjectDirPrefix,
} from "./install-paths.js";
describe("managed npm plugin install paths", () => {
it("keeps generation project names compact for nested Windows runtime binaries", () => {
const packageName = "@openclaw/codex";
const generationKey = [
packageName,
"2026.6.10",
`${packageName}@2026.6.10`,
"sha512-test-integrity",
"codexshasum",
].join("\n");
const projectDir = resolvePluginNpmGenerationProjectDir({
npmDir: String.raw`C:\Users\Administrator\.openclaw\npm`,
packageName,
generationKey,
});
const projectName = path.basename(projectDir);
expect(projectName).toMatch(
/^openclaw-codex-[a-f0-9]{10}__openclaw-generation__g-[a-f0-9]{16}$/u,
);
expect(projectName.length).toBeLessThanOrEqual(66);
const nestedCodexBinaryPath = path.win32.join(
String.raw`C:\Users\Administrator\.openclaw\npm\projects`,
projectName,
"node_modules",
"@openclaw",
"codex",
"node_modules",
"@openai",
"codex-win32-x64",
"vendor",
"x86_64-pc-windows-msvc",
"bin",
"codex.exe",
);
expect(nestedCodexBinaryPath.length).toBeLessThan(260);
});
it("keeps generation project names under the recoverable package prefix", () => {
const packageName = "@openclaw/codex";
const projectDir = resolvePluginNpmGenerationProjectDir({
npmDir: "/tmp/openclaw/npm",
packageName,
generationKey: "codex-v2",
});
expect(path.basename(projectDir)).toMatch(
new RegExp(`^${resolvePluginNpmGenerationProjectDirPrefix(packageName)}`, "u"),
);
});
});
+9 -1
View File
@@ -1,4 +1,5 @@
// Resolves plugin install paths for local and package sources.
import { createHash } from "node:crypto";
import path from "node:path";
import {
resolveSafeInstallDir,
@@ -121,12 +122,19 @@ export function resolvePluginNpmProjectDir(params: {
}
const PLUGIN_NPM_GENERATION_PROJECT_SEPARATOR = "__openclaw-generation__";
const PLUGIN_NPM_GENERATION_KEY_HASH_CHARS = 16;
/** Resolves the managed npm artifact-generation project directory prefix for a package. */
export function resolvePluginNpmGenerationProjectDirPrefix(packageName: string): string {
return `${encodePluginNpmProjectDirName(packageName)}${PLUGIN_NPM_GENERATION_PROJECT_SEPARATOR}`;
}
/** Encodes a package generation fingerprint into a compact project directory suffix. */
export function encodePluginNpmGenerationKeyDirName(generationKey: string): string {
const digest = createHash("sha256").update(generationKey).digest("hex");
return `g-${digest.slice(0, PLUGIN_NPM_GENERATION_KEY_HASH_CHARS)}`;
}
/** Resolves an artifact-generation-specific managed npm project directory. */
export function resolvePluginNpmGenerationProjectDir(params: {
packageName: string;
@@ -135,7 +143,7 @@ export function resolvePluginNpmGenerationProjectDir(params: {
}): string {
return path.join(
resolvePluginNpmProjectsDir(params.npmDir),
`${resolvePluginNpmGenerationProjectDirPrefix(params.packageName)}${safePathSegmentHashed(
`${resolvePluginNpmGenerationProjectDirPrefix(params.packageName)}${encodePluginNpmGenerationKeyDirName(
params.generationKey,
)}`,
);