Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/stats-tracking-pacote-extract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@nodesecure/tarball": minor
"@nodesecure/scanner": minor
---

feat: add stats tracking on pacote.extract through extractAndResolve

Add support for dependency injection of extractFn in extractAndResolve to enable tracking of pacote.extract calls using StatsCollector. This allows measuring extraction time for each package during scanning.
21 changes: 17 additions & 4 deletions workspaces/scanner/src/depWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import * as npmRegistrySDK from "@nodesecure/npm-registry-sdk";
import { Mutex, MutexRelease } from "@openally/mutex";
import {
extractAndResolve,
scanDirOrArchive
scanDirOrArchive,
type PacoteProvider
} from "@nodesecure/tarball";
import * as Vulnera from "@nodesecure/vulnera";
import { npm } from "@nodesecure/tree-walker";
Expand Down Expand Up @@ -116,6 +117,14 @@ export async function depWalker(
} = options;

const statsCollector = new StatsCollector();

const pacoteProvider: PacoteProvider = {
extract: (spec, dest, opts) => statsCollector.track(
`pacote.extract ${spec}`,
() => pacote.extract(spec, dest, opts)
)
};

const isRemoteScanning = typeof location === "undefined";
const tokenStore = new RegistryTokenStore(npmRcConfig, NPM_TOKEN.token);

Expand Down Expand Up @@ -265,7 +274,8 @@ export async function depWalker(
location,
isRootNode: scanRootNode && name === manifest.name,
registry,
statsCollector
statsCollector,
pacoteProvider
};
operationsQueue.push(
scanDirOrArchiveEx(name, version, locker, tempDir, logger, scanDirOptions)
Expand Down Expand Up @@ -394,6 +404,7 @@ async function scanDirOrArchiveEx(
location: string | undefined;
ref: any;
statsCollector: StatsCollector;
pacoteProvider?: PacoteProvider;
}
) {
using _ = await locker.acquire();
Expand All @@ -406,14 +417,16 @@ async function scanDirOrArchiveEx(
location = process.cwd(),
isRootNode,
ref,
statsCollector
statsCollector,
pacoteProvider
} = options;

const mama = await (isRootNode ?
ManifestManager.fromPackageJSON(location) :
extractAndResolve(tempDir.location, {
spec,
registry
registry,
pacoteProvider
})
);

Expand Down
13 changes: 11 additions & 2 deletions workspaces/tarball/src/tarball.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,28 @@ export async function scanPackage(
};
}

export interface PacoteProvider {
extract(
spec: string,
destination: string,
options: pacote.Options
): Promise<any>;
}

export interface TarballResolutionOptions {
spec: string;
registry?: string;
pacoteProvider?: PacoteProvider;
}

export async function extractAndResolve(
location: string,
options: TarballResolutionOptions
): Promise<ManifestManager> {
const { spec, registry } = options;
const { spec, registry, pacoteProvider = pacote } = options;

const tarballLocation = path.join(location, spec.replaceAll("/", "_"));
await pacote.extract(
await pacoteProvider.extract(
spec,
tarballLocation,
{
Expand Down