Skip to content

Commit 1b911e8

Browse files
committed
Cleanup arborist types
1 parent 10f3b5c commit 1b911e8

File tree

4 files changed

+94
-69
lines changed

4 files changed

+94
-69
lines changed

src/shadow/arborist/lib/arborist/diff.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,7 @@
11
import constants from '../../../../constants'
22

3+
import type { Diff } from './types'
34
import type { SafeNode } from '../node'
4-
import type { Diff as BaseDiff } from '@npmcli/arborist'
5-
6-
export type SafeDiff = Omit<
7-
BaseDiff,
8-
| 'actual'
9-
| 'children'
10-
| 'filterSet'
11-
| 'ideal'
12-
| 'leaves'
13-
| 'removed'
14-
| 'shrinkwrapInflated'
15-
| 'unchanged'
16-
> & {
17-
actual: SafeNode
18-
children: SafeDiff[]
19-
filterSet: Set<SafeNode>
20-
ideal: SafeNode
21-
leaves: SafeNode[]
22-
parent: SafeDiff | null
23-
removed: SafeNode[]
24-
shrinkwrapInflated: Set<SafeNode>
25-
unchanged: SafeNode[]
26-
}
275

286
const { LOOP_SENTINEL, NPM_REGISTRY_URL, SOCKET_CLI_FIX_PACKAGE_LOCK_FILE } =
297
constants
@@ -47,7 +25,7 @@ type GetPackagesToQueryFromDiffOptions = {
4725
}
4826

4927
export function getPackagesToQueryFromDiff(
50-
diff_: SafeDiff | null,
28+
diff_: Diff | null,
5129
options?: GetPackagesToQueryFromDiffOptions
5230
): PackageDetail[] {
5331
const {
@@ -63,7 +41,7 @@ export function getPackagesToQueryFromDiff(
6341
if (!diff_) {
6442
return details
6543
}
66-
const queue: SafeDiff[] = [...diff_.children]
44+
const queue: Diff[] = [...diff_.children]
6745
let pos = 0
6846
let { length: queueLength } = queue
6947
while (pos < queueLength) {

src/shadow/arborist/lib/arborist/index.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,12 @@
1-
import { reify } from './reify'
1+
import { kRiskyReify, reify } from './reify'
22
import { arboristClassPath } from '../../../npm-paths'
33

4-
import type { SafeDiff } from './diff'
4+
import type { ArboristClass, ArboristReifyOptions } from './types'
55
import type { SafeNode } from '../node'
6-
import type {
7-
Options as ArboristOptions,
8-
Advisory as BaseAdvisory,
9-
Arborist as BaseArborist,
10-
AuditReport as BaseAuditReport,
11-
ReifyOptions
12-
} from '@npmcli/arborist'
13-
14-
export type ArboristClass = ArboristInstance & {
15-
new (...args: any): ArboristInstance
16-
}
17-
18-
export type ArboristInstance = Omit<
19-
typeof BaseArborist,
20-
'actualTree' | 'auditReport' | 'diff' | 'idealTree' | 'reify'
21-
> & {
22-
auditReport?: AuditReportInstance | null | undefined
23-
actualTree?: SafeNode | null | undefined
24-
diff: SafeDiff | null
25-
idealTree?: SafeNode | null | undefined
26-
reify(options?: ArboristReifyOptions): Promise<SafeNode>
27-
}
28-
29-
export type ArboristReifyOptions = ReifyOptions & ArboristOptions
30-
31-
export type AuditReportInstance = Omit<BaseAuditReport, 'report'> & {
32-
report: { [dependency: string]: AuditAdvisory[] }
33-
}
34-
35-
export type AuditAdvisory = Omit<BaseAdvisory, 'id'> & {
36-
id: number
37-
cwe: string[]
38-
cvss: {
39-
score: number
40-
vectorString: string
41-
}
42-
vulnerable_versions: string
43-
}
446

457
export const Arborist: ArboristClass = require(arboristClassPath)
8+
469
export const kCtorArgs = Symbol('ctorArgs')
47-
export const kRiskyReify = Symbol('riskyReify')
4810

4911
// Implementation code not related to our custom behavior is based on
5012
// https://github.com/npm/cli/blob/v11.0.0/workspaces/arborist/lib/arborist/index.js:
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { SafeNode } from '../node'
2+
import type {
3+
Options as ArboristOptions,
4+
Advisory as BaseAdvisory,
5+
Arborist as BaseArborist,
6+
AuditReport as BaseAuditReport,
7+
Diff as BaseDiff,
8+
ReifyOptions
9+
} from '@npmcli/arborist'
10+
11+
export type ArboristClass = ArboristInstance & {
12+
new (...args: any): ArboristInstance
13+
}
14+
15+
export type ArboristInstance = Omit<
16+
typeof BaseArborist,
17+
'actualTree' | 'auditReport' | 'diff' | 'idealTree' | 'reify'
18+
> & {
19+
auditReport?: AuditReportInstance | null | undefined
20+
actualTree?: SafeNode | null | undefined
21+
diff: Diff | null
22+
idealTree?: SafeNode | null | undefined
23+
reify(options?: ArboristReifyOptions): Promise<SafeNode>
24+
}
25+
26+
export type ArboristReifyOptions = ReifyOptions & ArboristOptions
27+
28+
export type AuditReportInstance = Omit<BaseAuditReport, 'report'> & {
29+
report: { [dependency: string]: AuditAdvisory[] }
30+
}
31+
32+
export type AuditAdvisory = Omit<BaseAdvisory, 'id'> & {
33+
id: number
34+
cwe: string[]
35+
cvss: {
36+
score: number
37+
vectorString: string
38+
}
39+
vulnerable_versions: string
40+
}
41+
42+
export type Diff = Omit<
43+
BaseDiff,
44+
| 'actual'
45+
| 'children'
46+
| 'filterSet'
47+
| 'ideal'
48+
| 'leaves'
49+
| 'removed'
50+
| 'shrinkwrapInflated'
51+
| 'unchanged'
52+
> & {
53+
actual: SafeNode
54+
children: Diff[]
55+
filterSet: Set<SafeNode>
56+
ideal: SafeNode
57+
leaves: SafeNode[]
58+
parent: Diff | null
59+
removed: SafeNode[]
60+
shrinkwrapInflated: Set<SafeNode>
61+
unchanged: SafeNode[]
62+
}

src/utils/alert/artifact.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import rl from 'node:readline'
55
import constants from '../../constants'
66
import { getPublicToken } from '../sdk'
77

8+
export type CveAlertType = 'criticalCVE' | 'cve' | 'mediumCVE' | 'mildCVE'
9+
10+
export type ArtifactAlertCveFixable = Omit<
11+
SocketArtifactAlert,
12+
'props' | 'title'
13+
> & {
14+
type: CveAlertType
15+
props: {
16+
firstPatchedVersionIdentifier: string
17+
vulnerableVersionRange: string
18+
[key: string]: any
19+
}
20+
}
21+
22+
export type ArtifactAlertFixable = ArtifactAlertCveFixable & {
23+
type: CveAlertType | 'socketUpgradeAvailable'
24+
}
25+
826
export type SocketArtifactAlert = {
927
key: string
1028
type: string
@@ -85,18 +103,23 @@ export async function* batchScan(
85103
}
86104
}
87105

88-
export function isArtifactAlertCveFixable(alert: SocketArtifactAlert): boolean {
106+
export function isArtifactAlertCveFixable(
107+
alert: SocketArtifactAlert
108+
): alert is ArtifactAlertCveFixable {
89109
const { type } = alert
90110
return (
91111
(type === 'cve' ||
92112
type === 'mediumCVE' ||
93113
type === 'mildCVE' ||
94114
type === 'criticalCVE') &&
95-
!!alert.props?.['firstPatchedVersionIdentifier']
115+
!!alert.props?.['firstPatchedVersionIdentifier'] &&
116+
!!alert.props?.['vulnerableVersionRange']
96117
)
97118
}
98119

99-
export function isArtifactAlertFixable(alert: SocketArtifactAlert): boolean {
120+
export function isArtifactAlertFixable(
121+
alert: SocketArtifactAlert
122+
): alert is ArtifactAlertFixable {
100123
return (
101124
alert.type === 'socketUpgradeAvailable' || isArtifactAlertCveFixable(alert)
102125
)

0 commit comments

Comments
 (0)