Skip to content

Commit

Permalink
Make abortSignal a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Dec 16, 2024
1 parent 3ece9f5 commit 23b124e
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 38 deletions.
20 changes: 7 additions & 13 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,28 @@

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

const { DIST_TYPE, execPath } = constants
const { DIST_TYPE } = constants

if (DIST_TYPE === 'require') {
require(`../dist/require/cli.js`)
require(`../dist/${DIST_TYPE}/cli.js`)
} else {
const path = require('node:path')
const spawn = require('@npmcli/promise-spawn')
const { onExit } = require('signal-exit')

const abortController = new AbortController()
const { signal: abortSignal } = abortController

// Detect ^C, i.e. Ctrl + C.
onExit(() => {
abortController.abort()
})
const { abortSignal, execPath, rootDistPath } = constants

process.exitCode = 1
const spawnPromise = spawn(
execPath,
[
// Lazily access constants.nodeNoWarningsFlags.
...constants.nodeNoWarningsFlags,
path.join(constants.rootDistPath, DIST_TYPE, 'cli.js'),
path.join(rootDistPath, DIST_TYPE, 'cli.js'),
...process.argv.slice(2)
],
{
stdio: 'inherit',
signal: abortSignal
signal: abortSignal,
stdio: 'inherit'
}
)
spawnPromise.process.on('exit', (code, signal) => {
Expand Down
7 changes: 5 additions & 2 deletions src/commands/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import constants from '../constants'

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

const { execPath, rootBinPath } = constants
const { abortSignal, execPath, rootBinPath } = constants

const description = 'npm wrapper functionality'

Expand All @@ -23,7 +23,10 @@ export const npm: CliSubcommand = {
wrapperPath,
...argv
],
{ stdio: 'inherit' }
{
signal: abortSignal,
stdio: 'inherit'
}
)
spawnPromise.process.on('exit', (code, signal) => {
if (signal) {
Expand Down
7 changes: 5 additions & 2 deletions src/commands/npx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import constants from '../constants'

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

const { execPath, rootBinPath } = constants
const { abortSignal, execPath, rootBinPath } = constants

const description = 'npx wrapper functionality'

Expand All @@ -23,7 +23,10 @@ export const npx: CliSubcommand = {
wrapperPath,
...argv
],
{ stdio: 'inherit' }
{
abortSignal,
stdio: 'inherit'
}
)
spawnPromise.process.on('exit', (code, signal) => {
if (signal) {
Expand Down
14 changes: 11 additions & 3 deletions src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ import type { Spinner } from '@socketregistry/yocto-spinner'

type PackageJson = Awaited<ReturnType<typeof readPackageJson>>

const { UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE, execPath, rootBinPath } =
constants
const {
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
abortSignal,
execPath,
rootBinPath
} = constants

const COMMAND_TITLE = 'Socket Optimize'
const OVERRIDES_FIELD_NAME = 'overrides'
Expand Down Expand Up @@ -896,6 +900,7 @@ export const optimize: CliSubcommand = {
if (isNpm) {
const wrapperPath = path.join(rootBinPath, 'npm-cli.js')
const npmSpawnOptions: Parameters<typeof spawn>[2] = {
signal: abortSignal,
stdio: 'ignore',
env: {
...process.env,
Expand Down Expand Up @@ -923,7 +928,10 @@ export const optimize: CliSubcommand = {
)
} else {
// All package managers support the "install" command.
await spawn(agentExecPath, ['install'], { stdio: 'ignore' })
await spawn(agentExecPath, ['install'], {
signal: abortSignal,
stdio: 'ignore'
})
}
spinner.stop()
if (isNpm) {
Expand Down
4 changes: 4 additions & 0 deletions src/commands/raw-npm.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import spawn from '@npmcli/promise-spawn'
import meow from 'meow'

import constants from '../constants'
import { commonFlags, validationFlags } from '../flags'
import { printFlagList } from '../utils/formatting'

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

const { abortSignal } = constants

export const rawNpm: CliSubcommand = {
description: 'Temporarily disable the Socket npm wrapper',
async run(argv, importMeta, { parentName }) {
Expand Down Expand Up @@ -55,6 +58,7 @@ async function setupCommand(
return
}
const spawnPromise = spawn('npm', <string[]>argv, {
signal: abortSignal,
stdio: 'inherit'
})
spawnPromise.process.on('exit', (code, signal) => {
Expand Down
4 changes: 4 additions & 0 deletions src/commands/raw-npx.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import spawn from '@npmcli/promise-spawn'
import meow from 'meow'

import constants from '../constants'
import { commonFlags, validationFlags } from '../flags'
import { printFlagList } from '../utils/formatting'

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

const { abortSignal } = constants

export const rawNpx: CliSubcommand = {
description: 'Temporarily disable the Socket npm/npx wrapper',
async run(argv, importMeta, { parentName }) {
Expand Down Expand Up @@ -55,6 +58,7 @@ async function setupCommand(
return
}
const spawnPromise = spawn('npx', [argv.join(' ')], {
signal: abortSignal,
stdio: 'inherit'
})
spawnPromise.process.on('exit', (code, signal) => {
Expand Down
14 changes: 14 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { realpathSync } from 'node:fs'
import path from 'node:path'

import { onExit } from 'signal-exit'

import { envAsBoolean } from '@socketsecurity/registry/lib/env'
import registryConstants from '@socketsecurity/registry/lib/constants'

Expand Down Expand Up @@ -43,6 +45,14 @@ const LAZY_DIST_TYPE = () =>

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

const abortController = new AbortController()
const { signal: abortSignal } = abortController

// Detect ^C, i.e. Ctrl + C.
onExit(() => {
abortController.abort()
})

const constants = <
{
readonly API_V0_URL: 'https://api.socket.dev/v0'
Expand All @@ -51,6 +61,8 @@ const constants = <
readonly NPM_REGISTRY_URL: 'https://registry.npmjs.org'
readonly SOCKET_CLI_ISSUES_URL: 'https://github.com/SocketDev/socket-cli/issues'
readonly UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE: 'UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE'
readonly abortController: typeof abortController
readonly abortSignal: typeof abortSignal
readonly cdxgenBinPath: string
readonly distPath: string
readonly nmBinPath: string
Expand All @@ -70,6 +82,8 @@ const constants = <
NPM_REGISTRY_URL,
SOCKET_CLI_ISSUES_URL,
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
abortController,
abortSignal,
cdxgenBinPath,
distPath: undefined,
nmBinPath,
Expand Down
10 changes: 1 addition & 9 deletions src/shadow/arborist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import yoctoSpinner from '@socketregistry/yocto-spinner'
import isInteractive from 'is-interactive'
import npa from 'npm-package-arg'
import semver from 'semver'
import { onExit } from 'signal-exit'

import config from '@socketsecurity/config'
import { isObject } from '@socketsecurity/registry/lib/objects'
Expand Down Expand Up @@ -223,6 +222,7 @@ const {
SOCKET_CLI_ISSUES_URL,
SOCKET_PUBLIC_API_KEY,
UPDATE_SOCKET_OVERRIDES_IN_PACKAGE_LOCK_FILE,
abortSignal,
rootPath
} = constants

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

const abortController = new AbortController()
const { signal: abortSignal } = abortController

// Detect ^C, i.e. Ctrl + C.
onExit(() => {
abortController.abort()
})

const Arborist: ArboristClass = require(arboristClassPath)
const depValid: (
child: NodeClass,
Expand Down
8 changes: 5 additions & 3 deletions src/shadow/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export async function installLinks(
binName: 'npm' | 'npx'
): Promise<string> {
// Find package manager being shadowed by this process.
const bins = await which(binName, {
all: true
})
const bins =
(await which(binName, {
all: true,
nothrow: true
})) ?? []
let shadowIndex = -1
const binPath = bins.find((binPath, i) => {
// Skip our bin directory if it's in the front.
Expand Down
10 changes: 8 additions & 2 deletions src/shadow/shadow-bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import constants from '../constants'
import { installLinks } from './link'
import { findRoot } from '../utils/path-resolve'

const { distPath, execPath, shadowBinPath } = constants
const { abortSignal, distPath, execPath, shadowBinPath } = constants

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

export default async function shadow(binName: 'npm' | 'npx') {
const binPath = await installLinks(shadowBinPath, binName)
if (abortSignal.aborted) {
return
}
// Adding the `--quiet` and `--no-progress` flags when the `proc-log` module
// is found to fix a UX issue when running the command with recent versions of
// npm (input swallowed by the standard npm spinner)
Expand Down Expand Up @@ -52,7 +55,10 @@ export default async function shadow(binName: 'npm' | 'npx') {
binPath,
...binArgs
],
{ stdio: 'inherit' }
{
signal: abortSignal,
stdio: 'inherit'
}
)
spawnPromise.process.on('exit', (code, signal) => {
if (signal) {
Expand Down
5 changes: 3 additions & 2 deletions test/socket-cdxgen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ type PromiseSpawnOptions = Exclude<Parameters<typeof spawn>[2], undefined> & {
encoding?: BufferEncoding | undefined
}

const { execPath, rootBinPath } = constants
const { abortSignal, execPath, rootBinPath } = constants

const entryPath = path.join(rootBinPath, 'cli.js')
const testPath = __dirname
const npmFixturesPath = path.join(testPath, 'socket-npm-fixtures')

const spawnOpts: PromiseSpawnOptions = {
cwd: npmFixturesPath,
encoding: 'utf8'
encoding: 'utf8',
signal: abortSignal
}

describe('Socket cdxgen command', async () => {
Expand Down
6 changes: 4 additions & 2 deletions test/socket-npm.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { describe, it } = require('node:test')
const spawn = require('@npmcli/promise-spawn')

const constants = require('../dist/constants.js')
const { execPath, rootBinPath } = constants
const { abortSignal, execPath, rootBinPath } = constants

const entryPath = path.join(rootBinPath, 'cli.js')
const testPath = __dirname
Expand All @@ -21,6 +21,7 @@ for (const npm of ['npm8', 'npm10']) {

spawnSync('npm', ['install', '--silent'], {
cwd: npmPath,
signal: abortSignal,
stdio: 'ignore'
})

Expand All @@ -35,7 +36,8 @@ for (const npm of ['npm8', 'npm10']) {
// Make sure we don't borrow TTY from parent.
SOCKET_SECURITY_TTY_IPC: undefined,
PATH: `${npmBinPath}:${process.env.PATH}`
}
},
signal: abortSignal
}),
e => e?.stderr.includes('Unable to prompt')
)
Expand Down

0 comments on commit 23b124e

Please sign in to comment.