11// Import Node.js Dependencies
22import path from "node:path" ;
3- import { readFileSync , promises as fs } from "node:fs" ;
3+ import { readFileSync } from "node:fs" ;
44import timers from "node:timers/promises" ;
5- import os from "node:os" ;
65
76// Import Third-party Dependencies
87import { Mutex , MutexRelease } from "@openally/mutex" ;
9- import { scanDirOrArchive , type scanDirOrArchiveOptions } from "@nodesecure/tarball" ;
8+ import {
9+ extractAndResolve ,
10+ scanDirOrArchive
11+ } from "@nodesecure/tarball" ;
1012import * as Vulnera from "@nodesecure/vulnera" ;
1113import { npm } from "@nodesecure/tree-walker" ;
14+ import { ManifestManager } from "@nodesecure/mama" ;
1215import type { ManifestVersion , PackageJSON } from "@nodesecure/npm-types" ;
1316
1417// Import Internal Dependencies
@@ -20,6 +23,7 @@ import {
2023 getManifestLinks
2124} from "./utils/index.js" ;
2225import { packageMetadata , manifestMetadata } from "./npmRegistry.js" ;
26+ import { TempDirectory } from "./class/TempDirectory.class.js" ;
2327import { Logger , ScannerLoggerEvents } from "./class/logger.class.js" ;
2428import type {
2529 Dependency ,
@@ -90,11 +94,10 @@ export async function depWalker(
9094 registry
9195 } = options ;
9296
93- // Create TMP directory
94- const tmpLocation = await fs . mkdtemp ( path . join ( os . tmpdir ( ) , "/" ) ) ;
97+ const tempDir = await TempDirectory . create ( ) ;
9598
9699 const payload : Partial < Payload > = {
97- id : tmpLocation . slice ( - 6 ) ,
100+ id : tempDir . id ,
98101 rootDependencyName : manifest . name ,
99102 scannerVersion : packageVersion ,
100103 vulnerabilityStrategy,
@@ -179,10 +182,12 @@ export async function depWalker(
179182 const scanDirOptions = {
180183 ref : dependency . versions [ version ] as any ,
181184 location,
182- tmpLocation : scanRootNode && name === manifest . name ? null : tmpLocation ,
185+ isRootNode : scanRootNode && name === manifest . name ,
183186 registry
184187 } ;
185- operationsQueue . push ( scanDirOrArchiveEx ( name , version , locker , scanDirOptions ) ) ;
188+ operationsQueue . push (
189+ scanDirOrArchiveEx ( name , version , locker , tempDir , scanDirOptions )
190+ ) ;
186191 }
187192
188193 logger . end ( ScannerLoggerEvents . analysis . tree ) ;
@@ -279,7 +284,7 @@ export async function depWalker(
279284 }
280285 finally {
281286 await timers . setImmediate ( ) ;
282- await fs . rm ( tmpLocation , { recursive : true , force : true } ) ;
287+ await tempDir . clear ( ) ;
283288
284289 logger . emit ( ScannerLoggerEvents . done ) ;
285290 }
@@ -290,12 +295,33 @@ async function scanDirOrArchiveEx(
290295 name : string ,
291296 version : string ,
292297 locker : Mutex ,
293- options : scanDirOrArchiveOptions
298+ tempDir : TempDirectory ,
299+ options : {
300+ registry ?: string ;
301+ isRootNode : boolean ;
302+ location : string | undefined ;
303+ ref : any ;
304+ }
294305) {
295306 const free = await locker . acquire ( ) ;
296307
297308 try {
298- await scanDirOrArchive ( name , version , options ) ;
309+ const {
310+ registry,
311+ location = process . cwd ( ) ,
312+ isRootNode,
313+ ref
314+ } = options ;
315+
316+ const mama = await ( isRootNode ?
317+ ManifestManager . fromPackageJSON ( location ! ) :
318+ extractAndResolve ( tempDir . location , {
319+ spec : `${ name } @${ version } ` ,
320+ registry
321+ } )
322+ ) ;
323+
324+ await scanDirOrArchive ( mama , ref ) ;
299325 }
300326 catch {
301327 // ignore
0 commit comments