Skip to content

Commit ef1e7e9

Browse files
committed
Fix how shadowBin results are returned to avoid exiting process
1 parent 5d6f1c1 commit ef1e7e9

File tree

13 files changed

+220
-92
lines changed

13 files changed

+220
-92
lines changed

bin/cli.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
11
#!/usr/bin/env node
22
'use strict'
3+
;(async () => {
4+
const Module = require('node:module')
5+
const path = require('node:path')
6+
const rootPath = path.join(__dirname, '..')
7+
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
38

4-
const Module = require('node:module')
5-
const path = require('node:path')
6-
const rootPath = path.join(__dirname, '..')
7-
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
8-
const process = require('node:process')
9+
const constants = require(path.join(rootPath, 'dist/constants.js'))
10+
const { spawn } = require(
11+
path.join(rootPath, 'external/@socketsecurity/registry/lib/spawn.js'),
12+
)
913

10-
const constants = require(path.join(rootPath, 'dist/constants.js'))
11-
const { spawn } = require(
12-
path.join(rootPath, 'external/@socketsecurity/registry/lib/spawn.js'),
13-
)
14+
process.exitCode = 1
1415

15-
process.exitCode = 1
16-
17-
spawn(
18-
constants.execPath,
19-
[
20-
...constants.nodeNoWarningsFlags,
21-
...constants.nodeHardenFlags,
22-
...constants.nodeMemoryFlags,
23-
...(constants.ENV.INLINED_SOCKET_CLI_SENTRY_BUILD
24-
? ['--require', constants.instrumentWithSentryPath]
25-
: []),
26-
constants.distCliPath,
27-
...process.argv.slice(2),
28-
],
29-
{
30-
env: {
31-
...process.env,
32-
...constants.processEnv,
16+
const spawnPromise = spawn(
17+
constants.execPath,
18+
[
19+
...constants.nodeNoWarningsFlags,
20+
...constants.nodeHardenFlags,
21+
...constants.nodeMemoryFlags,
22+
...(constants.ENV.INLINED_SOCKET_CLI_SENTRY_BUILD
23+
? ['--require', constants.instrumentWithSentryPath]
24+
: []),
25+
constants.distCliPath,
26+
...process.argv.slice(2),
27+
],
28+
{
29+
env: {
30+
...process.env,
31+
...constants.processEnv,
32+
},
33+
stdio: 'inherit',
3334
},
34-
stdio: 'inherit',
35-
},
36-
)
37-
// See https://nodejs.org/api/all.html#all_child_process_event-exit.
38-
.process.on('exit', (code, signalName) => {
35+
)
36+
37+
// See https://nodejs.org/api/child_process.html#event-exit.
38+
spawnPromise.process.on('exit', (code, signalName) => {
3939
if (signalName) {
4040
process.kill(process.pid, signalName)
41-
} else if (code) {
41+
} else if (typeof code === 'number') {
4242
// eslint-disable-next-line n/no-process-exit
4343
process.exit(code)
4444
}
4545
})
46+
47+
await spawnPromise
48+
})()

bin/npm-cli.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
#!/usr/bin/env node
22
'use strict'
3+
;(async () => {
4+
const Module = require('node:module')
5+
const path = require('node:path')
6+
const rootPath = path.join(__dirname, '..')
7+
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
38

4-
const Module = require('node:module')
5-
const path = require('node:path')
6-
const rootPath = path.join(__dirname, '..')
7-
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
9+
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
810

9-
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
10-
shadowBin('npm', process.argv.slice(2), { stdio: 'inherit' })
11+
process.exitCode = 1
12+
13+
const { spawnPromise } = await shadowBin('npm', process.argv.slice(2), {
14+
stdio: 'inherit',
15+
})
16+
17+
// See https://nodejs.org/api/child_process.html#event-exit.
18+
spawnPromise.process.on('exit', (code, signalName) => {
19+
if (signalName) {
20+
process.kill(process.pid, signalName)
21+
} else if (typeof code === 'number') {
22+
// eslint-disable-next-line n/no-process-exit
23+
process.exit(code)
24+
}
25+
})
26+
27+
await spawnPromise
28+
})()

bin/npx-cli.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
#!/usr/bin/env node
22
'use strict'
3+
;(async () => {
4+
const Module = require('node:module')
5+
const path = require('node:path')
6+
const rootPath = path.join(__dirname, '..')
7+
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
38

4-
const Module = require('node:module')
5-
const path = require('node:path')
6-
const rootPath = path.join(__dirname, '..')
7-
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
9+
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
810

9-
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
10-
shadowBin('npx', process.argv.slice(2), { stdio: 'inherit' })
11+
process.exitCode = 1
12+
13+
const { spawnPromise } = await shadowBin('npx', process.argv.slice(2), {
14+
stdio: 'inherit',
15+
})
16+
17+
// See https://nodejs.org/api/child_process.html#event-exit.
18+
spawnPromise.process.on('exit', (code, signalName) => {
19+
if (signalName) {
20+
process.kill(process.pid, signalName)
21+
} else if (typeof code === 'number') {
22+
// eslint-disable-next-line n/no-process-exit
23+
process.exit(code)
24+
}
25+
})
26+
27+
await spawnPromise
28+
})()

shadow-bin/npm

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const Module = require('node:module')
5-
const path = require('node:path')
6-
const rootPath = path.join(__dirname, '..')
7-
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
4+
;(async () => {
5+
const Module = require('node:module')
6+
const path = require('node:path')
7+
const rootPath = path.join(__dirname, '..')
8+
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
89

9-
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
10-
shadowBin('npm', process.argv.slice(2), { stdio: 'inherit' })
10+
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
11+
12+
process.exitCode = 1
13+
14+
const { spawnPromise } = await shadowBin('npm', process.argv.slice(2), { stdio: 'inherit' })
15+
16+
// See https://nodejs.org/api/child_process.html#event-exit.
17+
spawnPromise.process.on('exit', (code, signalName) => {
18+
if (signalName) {
19+
process.kill(process.pid, signalName)
20+
} else if (typeof code === 'number') {
21+
// eslint-disable-next-line n/no-process-exit
22+
process.exit(code)
23+
}
24+
})
25+
26+
await spawnPromise
27+
})()

shadow-bin/npx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
const Module = require('node:module')
5-
const path = require('node:path')
6-
const rootPath = path.join(__dirname, '..')
7-
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
4+
;(async () => {
5+
const Module = require('node:module')
6+
const path = require('node:path')
7+
const rootPath = path.join(__dirname, '..')
8+
Module.enableCompileCache?.(path.join(rootPath, '.cache'))
89

9-
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
10-
shadowBin('npx', process.argv.slice(2), { stdio: 'inherit' })
10+
const shadowBin = require(path.join(rootPath, 'dist/shadow-npm-bin.js'))
11+
12+
process.exitCode = 1
13+
14+
const { spawnPromise } = await shadowBin('npx', process.argv.slice(2), { stdio: 'inherit' })
15+
16+
// See https://nodejs.org/api/child_process.html#event-exit.
17+
spawnPromise.process.on('exit', (code, signalName) => {
18+
if (signalName) {
19+
process.kill(process.pid, signalName)
20+
} else if (typeof code === 'number') {
21+
// eslint-disable-next-line n/no-process-exit
22+
process.exit(code)
23+
}
24+
})
25+
26+
await spawnPromise
27+
})()

src/commands/manifest/cmd-manifest-cdxgen.mts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,19 @@ async function run(
298298
}
299299
}
300300

301-
await runCdxgen(yargv)
301+
process.exitCode = 1
302+
303+
const { spawnPromise } = await runCdxgen(yargv)
304+
305+
// See https://nodejs.org/api/child_process.html#event-exit.
306+
spawnPromise.process.on('exit', (code, signalName) => {
307+
if (signalName) {
308+
process.kill(process.pid, signalName)
309+
} else if (typeof code === 'number') {
310+
// eslint-disable-next-line n/no-process-exit
311+
process.exit(code)
312+
}
313+
})
314+
315+
await spawnPromise
302316
}

src/commands/manifest/run-cdxgen.mts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, promises as fs } from 'node:fs'
1+
import { existsSync, rmSync } from 'node:fs'
22
import path from 'node:path'
33

44
import colors from 'yoctocolors-cjs'
@@ -8,6 +8,8 @@ import { logger } from '@socketsecurity/registry/lib/logger'
88
import constants from '../../constants.mts'
99
import shadowBin from '../../shadow/npm/bin.mts'
1010

11+
import type { ShadowBinResult } from '../../shadow/npm/bin.mts'
12+
1113
const { PACKAGE_LOCK_JSON, YARN, YARN_LOCK } = constants
1214

1315
const nodejsPlatformTypes = new Set([
@@ -57,7 +59,7 @@ function argvToArray(argvObj: ArgvObject): string[] {
5759
return result
5860
}
5961

60-
export async function runCdxgen(argvObj: ArgvObject) {
62+
export async function runCdxgen(argvObj: ArgvObject): Promise<ShadowBinResult> {
6163
let cleanupPackageLock = false
6264
const argvMutable = { __proto__: null, ...argvObj } as ArgvObject
6365
if (
@@ -71,7 +73,7 @@ export async function runCdxgen(argvObj: ArgvObject) {
7173
// Use synp to create a package-lock.json from the yarn.lock,
7274
// based on the node_modules folder, for a more accurate SBOM.
7375
try {
74-
await shadowBin(
76+
const { spawnPromise: synpPromise } = await shadowBin(
7577
'npx',
7678
[
7779
'--yes',
@@ -84,12 +86,14 @@ export async function runCdxgen(argvObj: ArgvObject) {
8486
stdio: 'inherit',
8587
},
8688
)
89+
await synpPromise
8790
argvMutable['type'] = 'npm'
8891
cleanupPackageLock = true
8992
} catch {}
9093
}
9194
}
92-
await shadowBin(
95+
96+
const shadowResult = await shadowBin(
9397
'npx',
9498
[
9599
'--yes',
@@ -102,17 +106,21 @@ export async function runCdxgen(argvObj: ArgvObject) {
102106
},
103107
)
104108

105-
if (cleanupPackageLock) {
106-
try {
107-
await fs.rm(`./${PACKAGE_LOCK_JSON}`)
108-
} catch {}
109-
}
109+
shadowResult.spawnPromise.process.on('exit', () => {
110+
if (cleanupPackageLock) {
111+
try {
112+
rmSync(`./${PACKAGE_LOCK_JSON}`)
113+
} catch {}
114+
}
110115

111-
const outputPath = argvMutable['output'] as string
112-
if (outputPath) {
113-
const fullOutputPath = path.join(process.cwd(), outputPath)
114-
if (existsSync(fullOutputPath)) {
115-
logger.log(colors.cyanBright(`${outputPath} created!`))
116+
const outputPath = argvMutable['output'] as string
117+
if (outputPath) {
118+
const fullOutputPath = path.join(process.cwd(), outputPath)
119+
if (existsSync(fullOutputPath)) {
120+
logger.log(colors.cyanBright(`${outputPath} created!`))
121+
}
116122
}
117-
}
123+
})
124+
125+
return shadowResult
118126
}

src/commands/npm/cmd-npm.mts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,23 @@ async function run(
6868
}
6969

7070
const shadowBin = /*@__PURE__*/ require(constants.shadowNpmBinPath)
71-
await shadowBin('npm', argv, { stdio: 'inherit' })
71+
72+
process.exitCode = 1
73+
74+
const { spawnPromise } = await shadowBin('npm', argv, { stdio: 'inherit' })
75+
76+
// See https://nodejs.org/api/child_process.html#event-exit.
77+
spawnPromise.process.on(
78+
'exit',
79+
(code: string | null, signalName: NodeJS.Signals | null) => {
80+
if (signalName) {
81+
process.kill(process.pid, signalName)
82+
} else if (typeof code === 'number') {
83+
// eslint-disable-next-line n/no-process-exit
84+
process.exit(code)
85+
}
86+
},
87+
)
88+
89+
await spawnPromise
7290
}

src/commands/npx/cmd-npx.mts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,23 @@ async function run(
6767
}
6868

6969
const shadowBin = /*@__PURE__*/ require(constants.shadowNpmBinPath)
70-
await shadowBin('npx', argv, { stdio: 'inherit' })
70+
71+
process.exitCode = 1
72+
73+
const { spawnPromise } = await shadowBin('npx', argv, { stdio: 'inherit' })
74+
75+
// See https://nodejs.org/api/child_process.html#event-exit.
76+
spawnPromise.process.on(
77+
'exit',
78+
(code: string | null, signalName: NodeJS.Signals | null) => {
79+
if (signalName) {
80+
process.kill(process.pid, signalName)
81+
} else if (typeof code === 'number') {
82+
// eslint-disable-next-line n/no-process-exit
83+
process.exit(code)
84+
}
85+
},
86+
)
87+
88+
await spawnPromise
7189
}

src/commands/raw-npm/run-raw-npm.mts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { getNpmBinPath } from '../../utils/npm-paths.mts'
66
export async function runRawNpm(
77
argv: string[] | readonly string[],
88
): Promise<void> {
9+
process.exitCode = 1
10+
911
const spawnPromise = spawn(getNpmBinPath(), argv as string[], {
1012
shell: constants.WIN32,
1113
stdio: 'inherit',
@@ -15,7 +17,7 @@ export async function runRawNpm(
1517
spawnPromise.process.on('exit', (code, signalName) => {
1618
if (signalName) {
1719
process.kill(process.pid, signalName)
18-
} else if (code) {
20+
} else if (typeof code === 'number') {
1921
// eslint-disable-next-line n/no-process-exit
2022
process.exit(code)
2123
}

0 commit comments

Comments
 (0)