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: 18 additions & 3 deletions workspaces/scanner/src/depWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,24 @@ export async function depWalker(
}
}

async function trackedExtract(
spec: string,
dest: string,
opts: pacote.Options
): Promise<void> {
await statsCollector.track(
`pacote.extract[${spec}]`,
() => pacote.extract(spec, dest, opts)
);
}

const scanDirOptions = {
ref: dependency.versions[version] as any,
location,
isRootNode: scanRootNode && name === manifest.name,
registry,
statsCollector
statsCollector,
extractFn: trackedExtract
};
operationsQueue.push(
scanDirOrArchiveEx(name, version, locker, tempDir, logger, scanDirOptions)
Expand Down Expand Up @@ -394,6 +406,7 @@ async function scanDirOrArchiveEx(
location: string | undefined;
ref: any;
statsCollector: StatsCollector;
extractFn?: (spec: string, dest: string, opts: pacote.Options) => Promise<void>;
}
) {
using _ = await locker.acquire();
Expand All @@ -406,14 +419,16 @@ async function scanDirOrArchiveEx(
location = process.cwd(),
isRootNode,
ref,
statsCollector
statsCollector,
extractFn
} = options;

const mama = await (isRootNode ?
ManifestManager.fromPackageJSON(location) :
extractAndResolve(tempDir.location, {
spec,
registry
registry,
extractFn
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would just inline the operation and rename the extractFn to pacoteProvider:

{
pacoteProvider:(spec: string, dest: string, opts: pacote.Options) => statsCollector.track(
          `pacote.extract ${spec}`,
          () => pacote.extract(spec, dest, opts)
        )
}

})
);

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

type ExtractFunction = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i would rename it PacoteProvider and use an interface with the method extract

spec: string,
destination: string,
options: pacote.Options
) => Promise<void>;

export interface TarballResolutionOptions {
spec: string;
registry?: string;
extractFn?: ExtractFunction;
}

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

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