Skip to content

Commit 2b0e399

Browse files
committed
fix(release): bound npm verifier commands
1 parent b234aa0 commit 2b0e399

4 files changed

Lines changed: 119 additions & 26 deletions

File tree

‎scripts/lib/npm-verify-exec.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { execFileSync } from "node:child_process";
2+
3+
export type NpmVerifyCommandInvocation = {
4+
command: string;
5+
args: string[];
6+
windowsVerbatimArguments?: boolean;
7+
};
8+
9+
const DEFAULT_NPM_VERIFY_COMMAND_TIMEOUT_MS = 5 * 60 * 1000;
10+
const DEFAULT_NPM_VERIFY_COMMAND_MAX_BUFFER_BYTES = 16 * 1024 * 1024;
11+
12+
function positiveEnvInt(name: string, fallback: number): number {
13+
const raw = process.env[name];
14+
if (raw === undefined || raw === "") {
15+
return fallback;
16+
}
17+
const value = Number.parseInt(raw, 10);
18+
return Number.isSafeInteger(value) && value > 0 ? value : fallback;
19+
}
20+
21+
export function runNpmVerifyCommand(
22+
invocation: NpmVerifyCommandInvocation,
23+
cwd: string,
24+
options: { maxBufferBytes?: number; timeoutMs?: number } = {},
25+
): string {
26+
const timeoutMs =
27+
options.timeoutMs ??
28+
positiveEnvInt("OPENCLAW_NPM_VERIFY_COMMAND_TIMEOUT_MS", DEFAULT_NPM_VERIFY_COMMAND_TIMEOUT_MS);
29+
const maxBuffer =
30+
options.maxBufferBytes ??
31+
positiveEnvInt(
32+
"OPENCLAW_NPM_VERIFY_COMMAND_MAX_BUFFER_BYTES",
33+
DEFAULT_NPM_VERIFY_COMMAND_MAX_BUFFER_BYTES,
34+
);
35+
36+
return execFileSync(invocation.command, invocation.args, {
37+
cwd,
38+
encoding: "utf8",
39+
killSignal: "SIGKILL",
40+
maxBuffer,
41+
stdio: ["ignore", "pipe", "pipe"],
42+
timeout: timeoutMs,
43+
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
44+
}).trim();
45+
}

‎scripts/openclaw-npm-postpublish-verify.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env -S node --import tsx
22

3-
import { execFileSync } from "node:child_process";
43
import {
54
existsSync,
65
lstatSync,
@@ -25,6 +24,7 @@ import { pathToFileURL } from "node:url";
2524
import { formatErrorMessage } from "../src/infra/errors.ts";
2625
import { BUNDLED_RUNTIME_SIDECAR_PATHS } from "../src/plugins/runtime-sidecar-paths.ts";
2726
import { listBundledPluginPackArtifacts } from "./lib/bundled-plugin-build-entries.mjs";
27+
import { runNpmVerifyCommand } from "./lib/npm-verify-exec.ts";
2828
import {
2929
collectRuntimeDependencySpecs,
3030
packageNameFromSpecifier,
@@ -616,12 +616,7 @@ function npmExec(args: string[], cwd: string): string {
616616
platform: process.platform,
617617
});
618618

619-
return execFileSync(invocation.command, invocation.args, {
620-
cwd,
621-
encoding: "utf8",
622-
stdio: ["ignore", "pipe", "pipe"],
623-
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
624-
}).trim();
619+
return runNpmVerifyCommand(invocation, cwd);
625620
}
626621

627622
function resolveGlobalRoot(prefixDir: string, cwd: string): string {
@@ -638,12 +633,7 @@ function installSpec(prefixDir: string, spec: string, cwd: string): void {
638633

639634
function readInstalledBinaryVersion(prefixDir: string, cwd: string): string {
640635
const invocation = resolveInstalledBinaryCommandInvocation(prefixDir, ["--version"]);
641-
return execFileSync(invocation.command, invocation.args, {
642-
cwd,
643-
encoding: "utf8",
644-
stdio: ["ignore", "pipe", "pipe"],
645-
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
646-
}).trim();
636+
return runNpmVerifyCommand(invocation, cwd);
647637
}
648638

649639
function verifyScenario(version: string, scenario: PublishedInstallScenario): void {

‎scripts/openclaw-npm-prepublish-verify.ts‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env -S node --import tsx
22

3-
import { execFileSync } from "node:child_process";
43
import { mkdtempSync, readFileSync, realpathSync, rmSync } from "node:fs";
54
import { tmpdir } from "node:os";
65
import { join } from "node:path";
76
import { pathToFileURL } from "node:url";
87
import { formatErrorMessage } from "../src/infra/errors.ts";
8+
import { runNpmVerifyCommand } from "./lib/npm-verify-exec.ts";
99
import { runInstalledWorkspaceBootstrapSmoke } from "./lib/workspace-bootstrap-smoke.mjs";
1010
import {
1111
collectInstalledPackageErrors,
@@ -26,12 +26,7 @@ function npmExec(args: string[], cwd: string): string {
2626
platform: process.platform,
2727
});
2828

29-
return execFileSync(invocation.command, invocation.args, {
30-
cwd,
31-
encoding: "utf8",
32-
stdio: ["ignore", "pipe", "pipe"],
33-
windowsVerbatimArguments: invocation.windowsVerbatimArguments,
34-
}).trim();
29+
return runNpmVerifyCommand(invocation, cwd);
3530
}
3631

3732
function main(): void {
@@ -70,12 +65,7 @@ function main(): void {
7065
packageRoot,
7166
});
7267
const binaryInvocation = resolveInstalledBinaryCommandInvocation(prefixDir, ["--version"]);
73-
const installedBinaryVersion = execFileSync(binaryInvocation.command, binaryInvocation.args, {
74-
cwd: workingDir,
75-
encoding: "utf8",
76-
stdio: ["ignore", "pipe", "pipe"],
77-
windowsVerbatimArguments: binaryInvocation.windowsVerbatimArguments,
78-
}).trim();
68+
const installedBinaryVersion = runNpmVerifyCommand(binaryInvocation, workingDir);
7969
if (normalizeInstalledBinaryVersion(installedBinaryVersion) !== resolvedExpectedVersion) {
8070
errors.push(
8171
`installed openclaw binary version mismatch: expected ${resolvedExpectedVersion}, found ${installedBinaryVersion || "<missing>"}.`,
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { mkdtempSync, rmSync } from "node:fs";
2+
import { tmpdir } from "node:os";
3+
import path from "node:path";
4+
import { afterEach, describe, expect, it } from "vitest";
5+
import { runNpmVerifyCommand } from "../../scripts/lib/npm-verify-exec.ts";
6+
7+
const tempDirs: string[] = [];
8+
9+
function makeTempRoot(): string {
10+
const root = mkdtempSync(path.join(tmpdir(), "openclaw-npm-verify-exec-"));
11+
tempDirs.push(root);
12+
return root;
13+
}
14+
15+
afterEach(() => {
16+
for (const dir of tempDirs.splice(0)) {
17+
rmSync(dir, { force: true, recursive: true });
18+
}
19+
});
20+
21+
describe("npm verifier command execution", () => {
22+
it("trims successful command output", () => {
23+
const root = makeTempRoot();
24+
25+
expect(
26+
runNpmVerifyCommand(
27+
{
28+
command: process.execPath,
29+
args: ["-e", "process.stdout.write(' ok\\n')"],
30+
},
31+
root,
32+
{ timeoutMs: 5_000 },
33+
),
34+
).toBe("ok");
35+
});
36+
37+
it("bounds hung commands even when they ignore SIGTERM", () => {
38+
const root = makeTempRoot();
39+
const startedAt = Date.now();
40+
41+
expect(() =>
42+
runNpmVerifyCommand(
43+
{
44+
command: process.execPath,
45+
args: ["-e", "process.on('SIGTERM', () => {}); setInterval(() => {}, 1000);"],
46+
},
47+
root,
48+
{ timeoutMs: 100 },
49+
),
50+
).toThrow(/ETIMEDOUT|timed out/u);
51+
expect(Date.now() - startedAt).toBeLessThan(2_500);
52+
});
53+
54+
it("bounds buffered command output", () => {
55+
const root = makeTempRoot();
56+
57+
expect(() =>
58+
runNpmVerifyCommand(
59+
{
60+
command: process.execPath,
61+
args: ["-e", "process.stdout.write('x'.repeat(2048));"],
62+
},
63+
root,
64+
{ maxBufferBytes: 1024, timeoutMs: 5_000 },
65+
),
66+
).toThrow(/ENOBUFS|maxBuffer/u);
67+
});
68+
});

0 commit comments

Comments
 (0)