Skip to content

Commit 976b2b2

Browse files
committed
fix: replace buffered exec/execFile with spawn to eliminate maxBuffer limit
The previous implementation used child_process.exec/execFile which buffer all stdout in memory (default 1MB). pnpm i output can exceed this. Since no caller uses stdout, switch to spawn with stdio ignore for stdout and pipe for stderr (to preserve error diagnostics).
1 parent b4eea77 commit 976b2b2

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

source/operations/exec.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
import { exec as nodeExec, execFile as nodeExecFile } from 'node:child_process'
2-
import { promisify } from 'node:util'
1+
import { type SpawnOptions, spawn } from 'node:child_process'
32

4-
const execAsync = promisify(nodeExec)
5-
const execFileAsync = promisify(nodeExecFile)
3+
function run(command: string, args: string[], options: SpawnOptions): Promise<void> {
4+
return new Promise((resolve, reject) => {
5+
const child = spawn(command, args, {
6+
...options,
7+
stdio: ['ignore', 'ignore', 'pipe'],
8+
})
9+
10+
let stderr = ''
11+
child.stderr?.on('data', (data: Buffer) => {
12+
stderr += data.toString()
13+
})
614

7-
export async function exec(command: string, options: { cwd?: string } = {}): Promise<string> {
8-
try {
9-
const { stdout } = await execAsync(command, {
10-
cwd: options.cwd,
15+
child.on('close', (code) => {
16+
if (code === 0) {
17+
resolve()
18+
} else {
19+
const message = stderr.trim() || `Command failed with exit code ${code}`
20+
reject(new Error(message))
21+
}
1122
})
1223

13-
return stdout.trim()
14-
} catch (error: unknown) {
15-
const execError = error as { stderr?: string; message?: string }
16-
const message = execError.stderr?.trim() || execError.message || 'Unknown error'
17-
throw new Error(message)
18-
}
24+
child.on('error', reject)
25+
})
26+
}
27+
28+
export async function exec(command: string, options: { cwd?: string } = {}): Promise<void> {
29+
await run('/bin/sh', ['-c', command], { cwd: options.cwd })
1930
}
2031

2132
export async function execFile(
2233
file: string,
2334
args: string[],
2435
options: { cwd?: string } = {},
25-
): Promise<string> {
26-
try {
27-
const { stdout } = await execFileAsync(file, args, {
28-
cwd: options.cwd,
29-
})
30-
31-
return stdout.trim()
32-
} catch (error: unknown) {
33-
const execError = error as { stderr?: string; message?: string }
34-
const message = execError.stderr?.trim() || execError.message || 'Unknown error'
35-
throw new Error(message)
36-
}
36+
): Promise<void> {
37+
await run(file, args, { cwd: options.cwd })
3738
}

0 commit comments

Comments
 (0)