Skip to content

Commit d4b2814

Browse files
committed
Avoid extra yml parsing and add onlyFixable option to batch purl processing
1 parent d4270e6 commit d4b2814

5 files changed

Lines changed: 43 additions & 25 deletions

File tree

src/commands/fix/shared.mts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ export function getFixAlertsMapOptions(
1414
__proto__: null,
1515
consolidate: true,
1616
nothrow: true,
17+
onlyFixable: true,
1718
...options,
1819
filter: toFilterConfig({
1920
existing: true,
20-
fixable: true,
21-
upgradable: false,
2221
...getOwn(options, 'filter'),
2322
}),
2423
} as Remap<

src/shadow/npm/arborist/lib/arborist/index.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export class SafeArborist extends Arborist {
129129
existing: true,
130130
}
131131
: {
132+
// actions: ['error', 'monitor', 'warn'],
132133
existing: isSafeNpx,
133134
},
134135
})

src/utils/alerts-map.mts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { logger } from '@socketsecurity/registry/lib/logger'
44
import { getOwn } from '@socketsecurity/registry/lib/objects'
55
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
66

7+
import { findSocketYmlSync } from './config.mts'
78
import { toFilterConfig } from './filter-config.mts'
89
import { extractPurlsFromPnpmLockfile } from './pnpm.mts'
910
import { getPublicApiToken, setupSdk } from './sdk.mts'
@@ -36,6 +37,7 @@ export async function getAlertsMapFromPnpmLockfile(
3637
export type GetAlertsMapFromPurlsOptions = {
3738
consolidate?: boolean | undefined
3839
filter?: AlertFilter | undefined
40+
onlyFixable?: boolean | undefined
3941
overrides?: { [key: string]: string } | undefined
4042
nothrow?: boolean | undefined
4143
spinner?: Spinner | undefined
@@ -45,14 +47,6 @@ export async function getAlertsMapFromPurls(
4547
purls: string[] | readonly string[],
4648
options?: GetAlertsMapFromPurlsOptions | undefined,
4749
): Promise<AlertsByPurl> {
48-
const opts = {
49-
__proto__: null,
50-
consolidate: false,
51-
nothrow: false,
52-
...options,
53-
filter: toFilterConfig(getOwn(options, 'filter')),
54-
} as GetAlertsMapFromPurlsOptions & { filter: AlertFilter }
55-
5650
const uniqPurls = arrayUnique(purls)
5751
debugDir('silly', { purls: uniqPurls })
5852

@@ -63,6 +57,18 @@ export async function getAlertsMapFromPurls(
6357
return alertsByPurl
6458
}
6559

60+
const opts = {
61+
__proto__: null,
62+
consolidate: false,
63+
nothrow: false,
64+
...options,
65+
filter: toFilterConfig(getOwn(options, 'filter')),
66+
} as GetAlertsMapFromPurlsOptions & { filter: AlertFilter }
67+
68+
if (opts.onlyFixable) {
69+
opts.filter.fixable = true
70+
}
71+
6672
const { spinner } = opts
6773
const getText = () => `Looking up data for ${remaining} packages`
6874

@@ -71,14 +77,16 @@ export async function getAlertsMapFromPurls(
7177
const sockSdkCResult = await setupSdk({ apiToken: getPublicApiToken() })
7278
if (!sockSdkCResult.ok) {
7379
spinner?.stop()
74-
throw new Error('Auth error: Try to run `socket login` first')
80+
throw new Error('Auth error: Run `socket login` first')
7581
}
7682
const sockSdk = sockSdkCResult.data
83+
const socketYml = findSocketYmlSync()?.parsed
7784

7885
const alertsMapOptions = {
7986
overrides: opts.overrides,
8087
consolidate: opts.consolidate,
8188
filter: opts.filter,
89+
socketYml,
8290
spinner,
8391
}
8492

@@ -90,18 +98,16 @@ export async function getAlertsMapFromPurls(
9098
queryParams: {
9199
alerts: 'true',
92100
compact: 'true',
101+
...(opts.onlyFixable ? { fixable: 'true ' } : {}),
93102
...(Array.isArray(opts.filter.actions)
94103
? { actions: opts.filter.actions.join(',') }
95104
: {}),
96105
},
97106
},
98107
)) {
99108
if (batchResult.success) {
100-
await addArtifactToAlertsMap(
101-
batchResult.data as CompactSocketArtifact,
102-
alertsByPurl,
103-
alertsMapOptions,
104-
)
109+
const artifact = batchResult.data as CompactSocketArtifact
110+
await addArtifactToAlertsMap(artifact, alertsByPurl, alertsMapOptions)
105111
} else if (!opts.nothrow) {
106112
spinner?.stop()
107113
if (isNonEmptyString(batchResult.error)) {

src/utils/config.mts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import { mkdirSync, writeFileSync } from 'node:fs'
22
import path from 'node:path'
33

44
import config from '@socketsecurity/config'
5-
import { debugFn } from '@socketsecurity/registry/lib/debug'
5+
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
66
import { safeReadFileSync } from '@socketsecurity/registry/lib/fs'
77
import { logger } from '@socketsecurity/registry/lib/logger'
88
import { naturalCompare } from '@socketsecurity/registry/lib/sorts'
99

1010
import constants from '../constants.mts'
1111

1212
import type { CResult } from '../types.mts'
13+
import type { SocketYml } from '@socketsecurity/config'
1314

1415
export interface LocalConfig {
1516
apiBaseUrl?: string | null | undefined
@@ -104,7 +105,14 @@ function normalizeConfigKey(
104105
return { ok: true, data: normalizedKey }
105106
}
106107

107-
export function findSocketYmlSync(dir = process.cwd()) {
108+
export type FoundSocketYml = {
109+
path: string
110+
parsed: SocketYml
111+
}
112+
113+
export function findSocketYmlSync(
114+
dir = process.cwd(),
115+
): FoundSocketYml | undefined {
108116
let prevDir = null
109117
while (dir !== prevDir) {
110118
let ymlPath = path.join(dir, 'socket.yml')
@@ -119,14 +127,15 @@ export function findSocketYmlSync(dir = process.cwd()) {
119127
path: ymlPath,
120128
parsed: config.parseSocketConfig(yml),
121129
}
122-
} catch {
130+
} catch (e) {
131+
debugDir('inspect', { error: e })
123132
throw new Error(`Found file but was unable to parse ${ymlPath}`)
124133
}
125134
}
126135
prevDir = dir
127136
dir = path.join(dir, '..')
128137
}
129-
return null
138+
return undefined
130139
}
131140

132141
export function getConfigValue<Key extends keyof LocalConfig>(

src/utils/socket-package-alert.mts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { isArtifactAlertCve } from './alert/artifact.mts'
1212
import { ALERT_FIX_TYPE } from './alert/fix.mts'
1313
import { ALERT_SEVERITY } from './alert/severity.mts'
1414
import { ColorOrMarkdown } from './color-or-markdown.mts'
15-
import { findSocketYmlSync } from './config.mts'
1615
import { toFilterConfig } from './filter-config.mts'
1716
import { createEnum } from './objects.mts'
1817
import { getPurlObject } from './purl.mts'
@@ -28,6 +27,7 @@ import type {
2827
CveProps,
2928
} from './alert/artifact.mts'
3029
import type { PURL_Type } from './ecosystem.mts'
30+
import type { SocketYml } from '@socketsecurity/config'
3131
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
3232

3333
export const ALERT_SEVERITY_COLOR = createEnum({
@@ -130,6 +130,7 @@ export type AddArtifactToAlertsMapOptions = {
130130
consolidate?: boolean | undefined
131131
filter?: AlertFilter | undefined
132132
overrides?: { [key: string]: string } | undefined
133+
socketYml?: SocketYml | undefined
133134
spinner?: Spinner | undefined
134135
}
135136

@@ -145,7 +146,11 @@ export async function addArtifactToAlertsMap<T extends AlertsByPurl>(
145146

146147
const { type: ecosystem, version } = artifact
147148

148-
const { consolidate = false, overrides } = {
149+
const {
150+
consolidate = false,
151+
overrides,
152+
socketYml,
153+
} = {
149154
__proto__: null,
150155
...options,
151156
} as AddArtifactToAlertsMapOptions
@@ -164,11 +169,9 @@ export async function addArtifactToAlertsMap<T extends AlertsByPurl>(
164169
...getOwn(options, 'filter'),
165170
}) as AlertFilter
166171

167-
const socketYml = findSocketYmlSync()
168-
169172
const enabledState = {
170173
__proto__: null,
171-
...socketYml?.parsed.issueRules,
174+
...socketYml?.issueRules,
172175
} as Partial<Record<ALERT_TYPE, boolean>>
173176

174177
let sockPkgAlerts: SocketPackageAlert[] = []

0 commit comments

Comments
 (0)