Skip to content

Commit c689415

Browse files
committed
refactor: convert Promise.all to Promise.allSettled for parallel operations
Converts Promise.all to Promise.allSettled in build scripts, command utilities, audit checks, and test setup to prevent memory leaks from short-circuited rejected promises. Changes: - scripts/utils/run-command.mjs: runParallel() uses Promise.allSettled - scripts/build.mjs: parallel builds use Promise.allSettled - scripts/claude.mjs: executeParallel() and audit checks use Promise.allSettled - test/purl-spec.test.mts: test file loading uses Promise.allSettled
1 parent 93d756c commit c689415

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

scripts/build.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ async function main() {
351351
}
352352

353353
// Run source and types builds in parallel
354-
const [srcResult, typesExitCode] = await Promise.all([
354+
const results = await Promise.allSettled([
355355
buildSource({
356356
quiet,
357357
verbose,
@@ -361,6 +361,13 @@ async function main() {
361361
buildTypes({ quiet, verbose, skipClean: true }),
362362
])
363363

364+
const srcResult =
365+
results[0].status === 'fulfilled'
366+
? results[0].value
367+
: { exitCode: 1, buildTime: 0, result: null }
368+
const typesExitCode =
369+
results[1].status === 'fulfilled' ? results[1].value : 1
370+
364371
// Log completion messages in order
365372
if (!quiet) {
366373
if (srcResult.exitCode === 0) {

scripts/claude.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,8 @@ async function executeParallel(tasks, workers = 3) {
19141914
}
19151915
}
19161916

1917-
return Promise.all(results)
1917+
const settled = await Promise.allSettled(results)
1918+
return settled.map(r => (r.status === 'fulfilled' ? r.value : undefined))
19181919
}
19191920

19201921
/**
@@ -3420,12 +3421,19 @@ async function runAudit(claudeCmd, options = {}) {
34203421
log.step('Gathering project information')
34213422

34223423
// Run various checks.
3423-
const [npmAudit, depCheck, licenseCheck] = await Promise.all([
3424+
const results = await Promise.allSettled([
34243425
runCommandWithOutput('npm', ['audit', '--json']),
34253426
runCommandWithOutput('pnpm', ['licenses', 'list', '--json']),
34263427
fs.readFile(path.join(rootPath, 'package.json'), 'utf8'),
34273428
])
34283429

3430+
const npmAudit =
3431+
results[0].status === 'fulfilled' ? results[0].value : { stdout: '' }
3432+
const depCheck =
3433+
results[1].status === 'fulfilled' ? results[1].value : { stdout: '' }
3434+
const licenseCheck =
3435+
results[2].status === 'fulfilled' ? results[2].value : '{}'
3436+
34293437
const packageJson = JSON.parse(licenseCheck)
34303438

34313439
const prompt = `Perform a comprehensive audit of the project:

scripts/utils/run-command.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ export async function runParallel(commands) {
8282
const promises = commands.map(({ args = [], command, options = {} }) =>
8383
runCommand(command, args, options),
8484
)
85-
return Promise.all(promises)
85+
const results = await Promise.allSettled(promises)
86+
return results.map(r => (r.status === 'fulfilled' ? r.value : 1))
8687
}
8788

8889
/**

test/purl-spec.test.mts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,18 @@ function toUrlSearchParams(search: string) {
4646

4747
describe('PackageURL purl-spec test suite', async () => {
4848
// Tests from the official purl-spec test suite (data/*.json files)
49-
const TEST_FILES = (
50-
await Promise.all(
51-
(
52-
await glob(['**/**.json'], {
53-
absolute: true,
54-
cwd: path.join(__dirname, 'data'),
55-
})
56-
).map(p => readJson(p)),
57-
)
49+
const settled = await Promise.allSettled(
50+
(
51+
await glob(['**/**.json'], {
52+
absolute: true,
53+
cwd: path.join(__dirname, 'data'),
54+
})
55+
).map(p => readJson(p)),
5856
)
57+
58+
const TEST_FILES = settled
59+
.filter(r => r.status === 'fulfilled')
60+
.map(r => r.value)
5961
.filter(Boolean)
6062
.flatMap((o: any) => o.tests ?? [])
6163

0 commit comments

Comments
 (0)