Skip to content

Commit 3188a6a

Browse files
committed
refactor(dlx): extract spawn-trivy.mts from spawn.mts
1 parent c8fcc75 commit 3188a6a

2 files changed

Lines changed: 86 additions & 64 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Spawn Trivy for image / IaC vulnerability scanning.
3+
*
4+
* - spawnTrivyDlx: download from GitHub releases, then exec.
5+
* - spawnTrivyVfs: extract from SEA bundle, then exec.
6+
* - spawnTrivy: auto-detect SEA vs npm-CLI mode and dispatch.
7+
*/
8+
9+
import { spawn } from '@socketsecurity/lib/spawn'
10+
11+
import {
12+
downloadGitHubReleaseBinary,
13+
spawnToolVfs,
14+
} from './spawn.mts'
15+
import { resolveTrivy } from './resolve-binary.mjs'
16+
import { areExternalToolsAvailable } from './vfs-extract.mjs'
17+
import { isSeaBinary } from '../sea/detect.mts'
18+
19+
import type { DlxOptions, DlxSpawnResult } from './spawn.mts'
20+
import type { StdioOptions } from 'node:child_process'
21+
import type { SpawnExtra } from '@socketsecurity/lib/spawn'
22+
23+
export async function spawnTrivyDlx(
24+
args: string[] | readonly string[],
25+
options?: DlxOptions | undefined,
26+
spawnExtra?: SpawnExtra | undefined,
27+
): Promise<DlxSpawnResult> {
28+
const resolution = resolveTrivy()
29+
30+
if (resolution.type !== 'github-release') {
31+
throw new Error(
32+
`internal: resolveTrivy returned resolution.type="${resolution.type}" (expected "github-release"); this is a resolver contract bug — re-run with --debug and report the output`,
33+
)
34+
}
35+
36+
const { env: spawnEnv, ...dlxOptions } = {
37+
__proto__: null,
38+
...options,
39+
} as DlxOptions
40+
41+
const binaryPath = await downloadGitHubReleaseBinary(resolution.details)
42+
43+
const spawnPromise = spawn(binaryPath, args, {
44+
...dlxOptions,
45+
env: {
46+
...process.env,
47+
...spawnEnv,
48+
},
49+
stdio: (spawnExtra?.['stdio'] as StdioOptions | undefined) ?? 'inherit',
50+
})
51+
52+
return {
53+
spawnPromise,
54+
}
55+
}
56+
57+
/**
58+
* Spawn Trivy from VFS (SEA mode).
59+
*/
60+
export async function spawnTrivyVfs(
61+
args: string[] | readonly string[],
62+
options?: DlxOptions | undefined,
63+
spawnExtra?: SpawnExtra | undefined,
64+
): Promise<DlxSpawnResult> {
65+
return await spawnToolVfs('trivy', args, options, spawnExtra)
66+
}
67+
68+
/**
69+
* Spawn Trivy.
70+
* Auto-detects SEA mode and uses appropriate spawn method.
71+
*/
72+
export async function spawnTrivy(
73+
args: string[] | readonly string[],
74+
options?: DlxOptions | undefined,
75+
spawnExtra?: SpawnExtra | undefined,
76+
): Promise<DlxSpawnResult> {
77+
if (isSeaBinary() && areExternalToolsAvailable()) {
78+
return await spawnTrivyVfs(args, options, spawnExtra)
79+
}
80+
return await spawnTrivyDlx(args, options, spawnExtra)
81+
}

packages/cli/src/utils/dlx/spawn.mts

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
resolvePyCli,
3838
resolveSfw,
3939
resolveSocketPatch,
40-
resolveTrivy,
4140
} from './resolve-binary.mjs'
4241

4342
import type { GitHubReleaseSpec } from './resolve-binary.mjs'
@@ -1658,69 +1657,11 @@ export async function spawnSocketPyCli(
16581657
* In SEA mode, they're extracted from VFS. In npm CLI mode, they're downloaded from GitHub.
16591658
*/
16601659

1661-
/**
1662-
* Spawn Trivy via GitHub download (npm CLI mode).
1663-
* Downloads from GitHub releases (aquasecurity/trivy).
1664-
*/
1665-
export async function spawnTrivyDlx(
1666-
args: string[] | readonly string[],
1667-
options?: DlxOptions | undefined,
1668-
spawnExtra?: SpawnExtra | undefined,
1669-
): Promise<DlxSpawnResult> {
1670-
const resolution = resolveTrivy()
1671-
1672-
if (resolution.type !== 'github-release') {
1673-
throw new Error(
1674-
`internal: resolveTrivy returned resolution.type="${resolution.type}" (expected "github-release"); this is a resolver contract bug — re-run with --debug and report the output`,
1675-
)
1676-
}
1677-
1678-
const { env: spawnEnv, ...dlxOptions } = {
1679-
__proto__: null,
1680-
...options,
1681-
} as DlxOptions
1682-
1683-
const binaryPath = await downloadGitHubReleaseBinary(resolution.details)
1684-
1685-
const spawnPromise = spawn(binaryPath, args, {
1686-
...dlxOptions,
1687-
env: {
1688-
...process.env,
1689-
...spawnEnv,
1690-
},
1691-
stdio: (spawnExtra?.['stdio'] as StdioOptions | undefined) ?? 'inherit',
1692-
})
1693-
1694-
return {
1695-
spawnPromise,
1696-
}
1697-
}
1698-
1699-
/**
1700-
* Spawn Trivy from VFS (SEA mode).
1701-
*/
1702-
export async function spawnTrivyVfs(
1703-
args: string[] | readonly string[],
1704-
options?: DlxOptions | undefined,
1705-
spawnExtra?: SpawnExtra | undefined,
1706-
): Promise<DlxSpawnResult> {
1707-
return await spawnToolVfs('trivy', args, options, spawnExtra)
1708-
}
1709-
1710-
/**
1711-
* Spawn Trivy.
1712-
* Auto-detects SEA mode and uses appropriate spawn method.
1713-
*/
1714-
export async function spawnTrivy(
1715-
args: string[] | readonly string[],
1716-
options?: DlxOptions | undefined,
1717-
spawnExtra?: SpawnExtra | undefined,
1718-
): Promise<DlxSpawnResult> {
1719-
if (isSeaBinary() && areExternalToolsAvailable()) {
1720-
return await spawnTrivyVfs(args, options, spawnExtra)
1721-
}
1722-
return await spawnTrivyDlx(args, options, spawnExtra)
1723-
}
1660+
export {
1661+
spawnTrivy,
1662+
spawnTrivyDlx,
1663+
spawnTrivyVfs,
1664+
} from './spawn-trivy.mts'
17241665

17251666
/**
17261667
* Spawn TruffleHog via GitHub download (npm CLI mode).

0 commit comments

Comments
 (0)