Skip to content

Commit 7716c08

Browse files
committed
Cleanup getPackagesToQueryFromDiff
1 parent 8bc6cc9 commit 7716c08

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,35 @@ import constants from '../../../../constants'
33
import type { SafeNode } from '../node'
44
import type { Diff } from '@npmcli/arborist'
55

6-
const { LOOP_SENTINEL, SOCKET_CLI_FIX_PACKAGE_LOCK_FILE } = constants
6+
const { LOOP_SENTINEL, NPM_REGISTRY_URL, SOCKET_CLI_FIX_PACKAGE_LOCK_FILE } =
7+
constants
78

8-
function toRepoUrl(resolved: string): string {
9+
function getUrlOrigin(input: string): string {
910
try {
10-
return URL.parse(resolved)?.origin ?? ''
11+
return URL.parse(input)?.origin ?? ''
1112
} catch {}
1213
return ''
1314
}
1415

1516
export type PackageDetail = {
1617
pkgid: SafeNode['pkgid']
17-
repository_url: string
18+
origin: string
1819
existing?: SafeNode['pkgid'] | undefined
1920
}
2021

21-
type GetPackagesToQueryFromDiffOptions = { includeUnchanged?: boolean }
22+
type GetPackagesToQueryFromDiffOptions = {
23+
includeUnchanged?: boolean
24+
includeUnknownOrigin?: boolean
25+
}
2226

2327
export function getPackagesToQueryFromDiff(
2428
diff_: Diff | null,
2529
options?: GetPackagesToQueryFromDiffOptions
2630
): PackageDetail[] {
2731
const {
2832
// Lazily access constants.IPC.
29-
includeUnchanged = constants.IPC[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE]
33+
includeUnchanged = constants.IPC[SOCKET_CLI_FIX_PACKAGE_LOCK_FILE],
34+
includeUnknownOrigin = false
3035
} = <GetPackagesToQueryFromDiffOptions>{
3136
__proto__: null,
3237
...options
@@ -70,11 +75,14 @@ export function getPackagesToQueryFromDiff(
7075
keep = action !== 'REMOVE'
7176
}
7277
if (keep && pkgNode?.resolved && (!oldNode || oldNode.resolved)) {
73-
details.push({
74-
existing,
75-
pkgid: pkgNode.pkgid,
76-
repository_url: toRepoUrl(pkgNode.resolved)
77-
})
78+
const origin = getUrlOrigin(pkgNode.resolved)
79+
if (includeUnknownOrigin || origin === NPM_REGISTRY_URL) {
80+
details.push({
81+
pkgid: pkgNode.pkgid,
82+
origin,
83+
existing
84+
})
85+
}
7886
}
7987
}
8088
for (const child of diff.children) {
@@ -85,11 +93,15 @@ export function getPackagesToQueryFromDiff(
8593
const { unchanged } = diff_!
8694
for (let i = 0, { length } = unchanged; i < length; i += 1) {
8795
const pkgNode = unchanged[i]!
88-
details.push({
89-
existing: pkgNode.pkgid,
90-
pkgid: pkgNode.pkgid,
91-
repository_url: toRepoUrl(pkgNode.resolved!)
92-
})
96+
const origin = getUrlOrigin(pkgNode.resolved!)
97+
if (includeUnknownOrigin || origin === NPM_REGISTRY_URL) {
98+
const { pkgid } = pkgNode
99+
details.push({
100+
pkgid,
101+
origin,
102+
existing: pkgid
103+
})
104+
}
93105
}
94106
}
95107
return details

src/shadow/arborist/lib/arborist/reify.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import {
1212
import { confirm } from '@socketsecurity/registry/lib/prompts'
1313
import { Spinner } from '@socketsecurity/registry/lib/spinner'
1414

15-
import { kCtorArgs, kRiskyReify } from './index'
1615
import { getPackagesToQueryFromDiff } from './diff'
16+
import { kCtorArgs, kRiskyReify } from './index'
1717
import constants from '../../../../constants'
1818
import {
1919
batchScan,
@@ -27,8 +27,8 @@ import { getSocketDevPackageOverviewUrl } from '../../../../utils/socket-url'
2727
import { pacotePath } from '../../../npm-paths'
2828
import { Edge, SafeEdge } from '../edge'
2929

30+
import type { PackageDetail } from './diff'
3031
import type { ArboristClass, AuditAdvisory, SafeArborist } from './index'
31-
import type { InstallEffect } from './walk'
3232
import type { SocketArtifact } from '../../../../utils/alert/artifact'
3333
import type { SafeNode } from '../node'
3434
import type { Writable } from 'node:stream'
@@ -107,10 +107,10 @@ type GetPackageAlertsOptions = {
107107

108108
async function getPackagesAlerts(
109109
safeArb: SafeArborist,
110-
pkgs: InstallEffect[],
110+
details: PackageDetail[],
111111
options?: GetPackageAlertsOptions
112112
): Promise<SocketPackageAlert[]> {
113-
let { length: remaining } = pkgs
113+
let { length: remaining } = details
114114
const packageAlerts: SocketPackageAlert[] = []
115115
if (!remaining) {
116116
return packageAlerts
@@ -125,7 +125,7 @@ async function getPackagesAlerts(
125125
: () => ''
126126
spinner?.start(getText())
127127
try {
128-
for await (const artifact of batchScan(pkgs.map(p => p.pkgid))) {
128+
for await (const artifact of batchScan(details.map(d => d.pkgid))) {
129129
if (!artifact.name || !artifact.version || !artifact.alerts?.length) {
130130
continue
131131
}
@@ -371,10 +371,7 @@ export async function reify(
371371
...args: Parameters<InstanceType<ArboristClass>['reify']>
372372
): Promise<SafeNode> {
373373
const needInfoOn = getPackagesToQueryFromDiff(this.diff)
374-
if (
375-
!needInfoOn.length ||
376-
needInfoOn.findIndex(c => c.repository_url === NPM_REGISTRY_URL) === -1
377-
) {
374+
if (!needInfoOn.length) {
378375
// Nothing to check, hmmm already installed or all private?
379376
return await this[kRiskyReify](...args)
380377
}

0 commit comments

Comments
 (0)