Skip to content

Commit 23b124e

Browse files
committed
Make abortSignal a constant
1 parent 3ece9f5 commit 23b124e

File tree

12 files changed

+71
-38
lines changed

12 files changed

+71
-38
lines changed

bin/cli.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,28 @@
33

44
const constants = require('../dist/constants')
55

6-
const { DIST_TYPE, execPath } = constants
6+
const { DIST_TYPE } = constants
77

88
if (DIST_TYPE === 'require') {
9-
require(`../dist/require/cli.js`)
9+
require(`../dist/${DIST_TYPE}/cli.js`)
1010
} else {
1111
const path = require('node:path')
1212
const spawn = require('@npmcli/promise-spawn')
13-
const { onExit } = require('signal-exit')
1413

15-
const abortController = new AbortController()
16-
const { signal: abortSignal } = abortController
17-
18-
// Detect ^C, i.e. Ctrl + C.
19-
onExit(() => {
20-
abortController.abort()
21-
})
14+
const { abortSignal, execPath, rootDistPath } = constants
2215

16+
process.exitCode = 1
2317
const spawnPromise = spawn(
2418
execPath,
2519
[
2620
// Lazily access constants.nodeNoWarningsFlags.
2721
...constants.nodeNoWarningsFlags,
28-
path.join(constants.rootDistPath, DIST_TYPE, 'cli.js'),
22+
path.join(rootDistPath, DIST_TYPE, 'cli.js'),
2923
...process.argv.slice(2)
3024
],
3125
{
32-
stdio: 'inherit',
33-
signal: abortSignal
26+
signal: abortSignal,
27+
stdio: 'inherit'
3428
}
3529
)
3630
spawnPromise.process.on('exit', (code, signal) => {

src/commands/npm.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import constants from '../constants'
66

77
import type { CliSubcommand } from '../utils/meow-with-subcommands'
88

9-
const { execPath, rootBinPath } = constants
9+
const { abortSignal, execPath, rootBinPath } = constants
1010

1111
const description = 'npm wrapper functionality'
1212

@@ -23,7 +23,10 @@ export const npm: CliSubcommand = {
2323
wrapperPath,
2424
...argv
2525
],
26-
{ stdio: 'inherit' }
26+
{
27+
signal: abortSignal,
28+
stdio: 'inherit'
29+
}
2730
)
2831
spawnPromise.process.on('exit', (code, signal) => {
2932
if (signal) {

src/commands/npx.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import constants from '../constants'
66

77
import type { CliSubcommand } from '../utils/meow-with-subcommands'
88

9-
const { execPath, rootBinPath } = constants
9+
const { abortSignal, execPath, rootBinPath } = constants
1010

1111
const description = 'npx wrapper functionality'
1212

@@ -23,7 +23,10 @@ export const npx: CliSubcommand = {
2323
wrapperPath,
2424
...argv
2525
],
26-
{ stdio: 'inherit' }
26+
{
27+
abortSignal,
28+
stdio: 'inherit'
29+
}
2730
)
2831
spawnPromise.process.on('exit', (code, signal) => {
2932
if (signal) {

src/commands/optimize.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ import type { Spinner } from '@socketregistry/yocto-spinner'
4242

4343
type PackageJson = Awaited<ReturnType<typeof readPackageJson>>
4444

45-
const { UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE, execPath, rootBinPath } =
46-
constants
45+
const {
46+
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
47+
abortSignal,
48+
execPath,
49+
rootBinPath
50+
} = constants
4751

4852
const COMMAND_TITLE = 'Socket Optimize'
4953
const OVERRIDES_FIELD_NAME = 'overrides'
@@ -896,6 +900,7 @@ export const optimize: CliSubcommand = {
896900
if (isNpm) {
897901
const wrapperPath = path.join(rootBinPath, 'npm-cli.js')
898902
const npmSpawnOptions: Parameters<typeof spawn>[2] = {
903+
signal: abortSignal,
899904
stdio: 'ignore',
900905
env: {
901906
...process.env,
@@ -923,7 +928,10 @@ export const optimize: CliSubcommand = {
923928
)
924929
} else {
925930
// All package managers support the "install" command.
926-
await spawn(agentExecPath, ['install'], { stdio: 'ignore' })
931+
await spawn(agentExecPath, ['install'], {
932+
signal: abortSignal,
933+
stdio: 'ignore'
934+
})
927935
}
928936
spinner.stop()
929937
if (isNpm) {

src/commands/raw-npm.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import spawn from '@npmcli/promise-spawn'
22
import meow from 'meow'
33

4+
import constants from '../constants'
45
import { commonFlags, validationFlags } from '../flags'
56
import { printFlagList } from '../utils/formatting'
67

78
import type { CliSubcommand } from '../utils/meow-with-subcommands'
89

10+
const { abortSignal } = constants
11+
912
export const rawNpm: CliSubcommand = {
1013
description: 'Temporarily disable the Socket npm wrapper',
1114
async run(argv, importMeta, { parentName }) {
@@ -55,6 +58,7 @@ async function setupCommand(
5558
return
5659
}
5760
const spawnPromise = spawn('npm', <string[]>argv, {
61+
signal: abortSignal,
5862
stdio: 'inherit'
5963
})
6064
spawnPromise.process.on('exit', (code, signal) => {

src/commands/raw-npx.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import spawn from '@npmcli/promise-spawn'
22
import meow from 'meow'
33

4+
import constants from '../constants'
45
import { commonFlags, validationFlags } from '../flags'
56
import { printFlagList } from '../utils/formatting'
67

78
import type { CliSubcommand } from '../utils/meow-with-subcommands'
89

10+
const { abortSignal } = constants
11+
912
export const rawNpx: CliSubcommand = {
1013
description: 'Temporarily disable the Socket npm/npx wrapper',
1114
async run(argv, importMeta, { parentName }) {
@@ -55,6 +58,7 @@ async function setupCommand(
5558
return
5659
}
5760
const spawnPromise = spawn('npx', [argv.join(' ')], {
61+
signal: abortSignal,
5862
stdio: 'inherit'
5963
})
6064
spawnPromise.process.on('exit', (code, signal) => {

src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { realpathSync } from 'node:fs'
22
import path from 'node:path'
33

4+
import { onExit } from 'signal-exit'
5+
46
import { envAsBoolean } from '@socketsecurity/registry/lib/env'
57
import registryConstants from '@socketsecurity/registry/lib/constants'
68

@@ -43,6 +45,14 @@ const LAZY_DIST_TYPE = () =>
4345

4446
const lazyDistPath = () => path.join(rootDistPath, constants.DIST_TYPE)
4547

48+
const abortController = new AbortController()
49+
const { signal: abortSignal } = abortController
50+
51+
// Detect ^C, i.e. Ctrl + C.
52+
onExit(() => {
53+
abortController.abort()
54+
})
55+
4656
const constants = <
4757
{
4858
readonly API_V0_URL: 'https://api.socket.dev/v0'
@@ -51,6 +61,8 @@ const constants = <
5161
readonly NPM_REGISTRY_URL: 'https://registry.npmjs.org'
5262
readonly SOCKET_CLI_ISSUES_URL: 'https://github.com/SocketDev/socket-cli/issues'
5363
readonly UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: 'UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE'
64+
readonly abortController: typeof abortController
65+
readonly abortSignal: typeof abortSignal
5466
readonly cdxgenBinPath: string
5567
readonly distPath: string
5668
readonly nmBinPath: string
@@ -70,6 +82,8 @@ const constants = <
7082
NPM_REGISTRY_URL,
7183
SOCKET_CLI_ISSUES_URL,
7284
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
85+
abortController,
86+
abortSignal,
7387
cdxgenBinPath,
7488
distPath: undefined,
7589
nmBinPath,

src/shadow/arborist.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import yoctoSpinner from '@socketregistry/yocto-spinner'
1010
import isInteractive from 'is-interactive'
1111
import npa from 'npm-package-arg'
1212
import semver from 'semver'
13-
import { onExit } from 'signal-exit'
1413

1514
import config from '@socketsecurity/config'
1615
import { isObject } from '@socketsecurity/registry/lib/objects'
@@ -223,6 +222,7 @@ const {
223222
SOCKET_CLI_ISSUES_URL,
224223
SOCKET_PUBLIC_API_KEY,
225224
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
225+
abortSignal,
226226
rootPath
227227
} = constants
228228

@@ -300,14 +300,6 @@ const pacote = tryRequire(<'pacote'>path.join(npmNmPath, 'pacote'), 'pacote')!
300300
const { tarball } = pacote
301301
const translations = require(path.join(rootPath, 'translations.json'))
302302

303-
const abortController = new AbortController()
304-
const { signal: abortSignal } = abortController
305-
306-
// Detect ^C, i.e. Ctrl + C.
307-
onExit(() => {
308-
abortController.abort()
309-
})
310-
311303
const Arborist: ArboristClass = require(arboristClassPath)
312304
const depValid: (
313305
child: NodeClass,

src/shadow/link.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ export async function installLinks(
1313
binName: 'npm' | 'npx'
1414
): Promise<string> {
1515
// Find package manager being shadowed by this process.
16-
const bins = await which(binName, {
17-
all: true
18-
})
16+
const bins =
17+
(await which(binName, {
18+
all: true,
19+
nothrow: true
20+
})) ?? []
1921
let shadowIndex = -1
2022
const binPath = bins.find((binPath, i) => {
2123
// Skip our bin directory if it's in the front.

src/shadow/shadow-bin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import constants from '../constants'
77
import { installLinks } from './link'
88
import { findRoot } from '../utils/path-resolve'
99

10-
const { distPath, execPath, shadowBinPath } = constants
10+
const { abortSignal, distPath, execPath, shadowBinPath } = constants
1111

1212
const injectionPath = path.join(distPath, 'npm-injection.js')
1313

1414
export default async function shadow(binName: 'npm' | 'npx') {
1515
const binPath = await installLinks(shadowBinPath, binName)
16+
if (abortSignal.aborted) {
17+
return
18+
}
1619
// Adding the `--quiet` and `--no-progress` flags when the `proc-log` module
1720
// is found to fix a UX issue when running the command with recent versions of
1821
// npm (input swallowed by the standard npm spinner)
@@ -52,7 +55,10 @@ export default async function shadow(binName: 'npm' | 'npx') {
5255
binPath,
5356
...binArgs
5457
],
55-
{ stdio: 'inherit' }
58+
{
59+
signal: abortSignal,
60+
stdio: 'inherit'
61+
}
5662
)
5763
spawnPromise.process.on('exit', (code, signal) => {
5864
if (signal) {

0 commit comments

Comments
 (0)