Skip to content

Commit 21f5f8e

Browse files
committed
Fix check:tsc pass
1 parent 6aac060 commit 21f5f8e

File tree

1 file changed

+80
-80
lines changed
  • src/shadow/arborist/lib/arborist

1 file changed

+80
-80
lines changed

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

+80-80
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,90 @@ function findPackageNodes(tree: SafeNode, packageName: string): SafeNode[] {
106106
return matches
107107
}
108108

109+
let _translations: typeof import('../../../../../translations.json') | undefined
110+
function getTranslations() {
111+
if (_translations === undefined) {
112+
_translations = require(
113+
// Lazily access constants.rootPath.
114+
path.join(constants.rootPath, 'translations.json')
115+
)
116+
}
117+
return _translations!
118+
}
119+
120+
function updateNode(
121+
node: SafeNode,
122+
packument: Packument,
123+
vulnerableVersionRange?: string,
124+
firstPatchedVersionIdentifier?: string
125+
): boolean {
126+
const availableVersions = Object.keys(packument.versions)
127+
// Find the highest non-vulnerable version within the same major range
128+
const targetVersion = findBestPatchVersion(
129+
node,
130+
availableVersions,
131+
vulnerableVersionRange,
132+
firstPatchedVersionIdentifier
133+
)
134+
const targetPackument = targetVersion
135+
? packument.versions[targetVersion]
136+
: undefined
137+
// Check !targetVersion to make TypeScript happy.
138+
if (!targetVersion || !targetPackument) {
139+
// No suitable patch version found.
140+
return false
141+
}
142+
// Use Object.defineProperty to override the version.
143+
Object.defineProperty(node, 'version', {
144+
configurable: true,
145+
enumerable: true,
146+
get: () => targetVersion
147+
})
148+
node.package.version = targetVersion
149+
// Update resolved and clear integrity for the new version.
150+
const purlObj = PackageURL.fromString(`pkg:npm/${node.name}`)
151+
node.resolved = `${NPM_REGISTRY_URL}/${node.name}/-/${purlObj.name}-${targetVersion}.tgz`
152+
const { integrity } = targetPackument.dist
153+
if (integrity) {
154+
node.integrity = integrity
155+
} else {
156+
delete node.integrity
157+
}
158+
if ('deprecated' in targetPackument) {
159+
node.package['deprecated'] = <string>targetPackument.deprecated
160+
} else {
161+
delete node.package['deprecated']
162+
}
163+
const newDeps = { ...targetPackument.dependencies }
164+
const { dependencies: oldDeps } = node.package
165+
node.package.dependencies = newDeps
166+
if (oldDeps) {
167+
for (const oldDepName of Object.keys(oldDeps)) {
168+
if (!hasOwn(newDeps, oldDepName)) {
169+
node.edgesOut.get(oldDepName)?.detach()
170+
}
171+
}
172+
}
173+
for (const newDepName of Object.keys(newDeps)) {
174+
if (!hasOwn(oldDeps, newDepName)) {
175+
node.addEdgeOut((<unknown>new Edge({
176+
from: node,
177+
name: newDepName,
178+
spec: newDeps[newDepName],
179+
type: 'prod'
180+
})) as SafeEdge)
181+
}
182+
}
183+
return true
184+
}
185+
109186
type GetPackageAlertsOptions = {
110187
output?: Writable
111188
includeExisting?: boolean
112189
includeUnfixable?: boolean
113190
}
114191

115-
async function getPackagesAlerts(
192+
export async function getPackagesAlerts(
116193
arb: SafeArborist,
117194
options?: GetPackageAlertsOptions
118195
): Promise<SocketPackageAlert[]> {
@@ -240,18 +317,7 @@ async function getPackagesAlerts(
240317
return packageAlerts
241318
}
242319

243-
let _translations: typeof import('../../../../../translations.json') | undefined
244-
function getTranslations() {
245-
if (_translations === undefined) {
246-
_translations = require(
247-
// Lazily access constants.rootPath.
248-
path.join(constants.rootPath, 'translations.json')
249-
)
250-
}
251-
return _translations!
252-
}
253-
254-
async function updateAdvisoryNodes(
320+
export async function updateAdvisoryNodes(
255321
arb: SafeArborist,
256322
alerts: SocketPackageAlert[]
257323
) {
@@ -313,7 +379,7 @@ async function updateAdvisoryNodes(
313379
}
314380
}
315381

316-
async function updateSocketRegistryNodes(arb: SafeArborist) {
382+
export async function updateSocketRegistryNodes(arb: SafeArborist) {
317383
await arb.buildIdealTree()
318384
const tree = arb.idealTree!
319385
for (const { 1: data } of getManifestData(NPM)) {
@@ -330,72 +396,6 @@ async function updateSocketRegistryNodes(arb: SafeArborist) {
330396
}
331397
}
332398

333-
function updateNode(
334-
node: SafeNode,
335-
packument: Packument,
336-
vulnerableVersionRange?: string,
337-
firstPatchedVersionIdentifier?: string
338-
): boolean {
339-
const availableVersions = Object.keys(packument.versions)
340-
// Find the highest non-vulnerable version within the same major range
341-
const targetVersion = findBestPatchVersion(
342-
node,
343-
availableVersions,
344-
vulnerableVersionRange,
345-
firstPatchedVersionIdentifier
346-
)
347-
const targetPackument = targetVersion
348-
? packument.versions[targetVersion]
349-
: undefined
350-
// Check !targetVersion to make TypeScript happy.
351-
if (!targetVersion || !targetPackument) {
352-
// No suitable patch version found.
353-
return false
354-
}
355-
// Use Object.defineProperty to override the version.
356-
Object.defineProperty(node, 'version', {
357-
configurable: true,
358-
enumerable: true,
359-
get: () => targetVersion
360-
})
361-
node.package.version = targetVersion
362-
// Update resolved and clear integrity for the new version.
363-
const purlObj = PackageURL.fromString(`pkg:npm/${node.name}`)
364-
node.resolved = `${NPM_REGISTRY_URL}/${node.name}/-/${purlObj.name}-${targetVersion}.tgz`
365-
const { integrity } = targetPackument.dist
366-
if (integrity) {
367-
node.integrity = integrity
368-
} else {
369-
delete node.integrity
370-
}
371-
if ('deprecated' in targetPackument) {
372-
node.package['deprecated'] = <string>targetPackument.deprecated
373-
} else {
374-
delete node.package['deprecated']
375-
}
376-
const newDeps = { ...targetPackument.dependencies }
377-
const { dependencies: oldDeps } = node.package
378-
node.package.dependencies = newDeps
379-
if (oldDeps) {
380-
for (const oldDepName of Object.keys(oldDeps)) {
381-
if (!hasOwn(newDeps, oldDepName)) {
382-
node.edgesOut.get(oldDepName)?.detach()
383-
}
384-
}
385-
}
386-
for (const newDepName of Object.keys(newDeps)) {
387-
if (!hasOwn(oldDeps, newDepName)) {
388-
node.addEdgeOut((<unknown>new Edge({
389-
from: node,
390-
name: newDepName,
391-
spec: newDeps[newDepName],
392-
type: 'prod'
393-
})) as SafeEdge)
394-
}
395-
}
396-
return true
397-
}
398-
399399
export const kRiskyReify = Symbol('riskyReify')
400400

401401
type SafeArborist = ArboristClass & {

0 commit comments

Comments
 (0)