Skip to content

Commit df67b07

Browse files
clemgbldfraxken
andauthored
feat(depWalker): highlight packages (#584)
Update workspaces/scanner/test/depWalker.spec.ts Update workspaces/scanner/src/types.ts Co-authored-by: Thomas.G <gentilhomme.thomas@gmail.com>
1 parent b9a9e45 commit df67b07

File tree

5 files changed

+66
-1
lines changed

5 files changed

+66
-1
lines changed

.changeset/upset-jokes-listen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/scanner": minor
3+
---
4+
5+
feat(deepWalker): highlight packages

workspaces/scanner/src/depWalker.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type { ManifestVersion, PackageJSON, WorkspacesPackageJSON } from "@nodes
1616
import { getNpmRegistryURL } from "@nodesecure/npm-registry-sdk";
1717
import type Config from "@npmcli/config";
1818
import { fromData } from "ssri";
19+
import semver from "semver";
1920

2021
// Import Internal Dependencies
2122
import {
@@ -136,6 +137,7 @@ export async function depWalker(
136137
};
137138

138139
const dependencies: Map<string, Dependency> = new Map();
140+
const highlightedPackages: Set<string> = new Set();
139141
const npmTreeWalker = new npm.TreeWalker({
140142
registry
141143
});
@@ -300,6 +302,10 @@ export async function depWalker(
300302
}
301303
for (const version of Object.entries(dependency.versions)) {
302304
const [verStr, verDescriptor] = version as [string, DependencyVersion];
305+
const range = options.highlight?.packages?.[packageName];
306+
if (range && semver.satisfies(verStr, range)) {
307+
highlightedPackages.add(`${packageName}@${verStr}`);
308+
}
303309
verDescriptor.flags.push(
304310
...addMissingVersionFlags(new Set(verDescriptor.flags), dependency)
305311
);
@@ -338,7 +344,8 @@ export async function depWalker(
338344
);
339345
payload.warnings = globalWarnings.concat(dependencyConfusionWarnings as GlobalWarning[]).concat(warnings);
340346
payload.highlighted = {
341-
contacts: illuminated
347+
contacts: illuminated,
348+
packages: [...highlightedPackages]
342349
};
343350
payload.dependencies = Object.fromEntries(dependencies);
344351
payload.metadata.executionTime = Date.now() - startedAt;

workspaces/scanner/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export interface Payload {
198198
warnings: GlobalWarning[];
199199
highlighted: {
200200
contacts: IlluminatedContact[];
201+
packages: string[];
201202
};
202203
/** All the dependencies of the package (flattened) */
203204
dependencies: Dependencies;
@@ -218,6 +219,10 @@ export interface Payload {
218219
};
219220
}
220221

222+
export type SemverRange = string | "*";
223+
224+
export type HighlightPackages = Record<string, SemverRange>;
225+
221226
export interface Options {
222227
/**
223228
* Maximum tree depth
@@ -253,6 +258,7 @@ export interface Options {
253258

254259
highlight?: {
255260
contacts: Contact[];
261+
packages?: HighlightPackages;
256262
};
257263

258264
/**

workspaces/scanner/test/depWalker.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ const pkgTypoSquatting = JSON.parse(readFileSync(
4747
"utf8"
4848
));
4949

50+
const pkgHighlightedPackages = JSON.parse(readFileSync(
51+
path.join(kFixturePath, "highlighted-packages.json"),
52+
"utf8"
53+
));
54+
5055
function cleanupPayload(payload: Payload) {
5156
for (const pkg of Object.values(payload)) {
5257
const versions = Object.values(
@@ -221,6 +226,36 @@ test("execute depWalker on typo-squatting (with no location)", async(test) => {
221226
]);
222227
});
223228

229+
test("should highlight the given packages", async() => {
230+
const { logger } = errorLogger();
231+
test.after(() => logger.removeAllListeners());
232+
233+
const hightlightPackages = {
234+
"zen-observable": "0.8.14 || 0.8.15",
235+
nanoid: "*"
236+
};
237+
238+
const result = await depWalker(
239+
pkgHighlightedPackages,
240+
structuredClone({
241+
...kDefaultWalkerOptions,
242+
highlight: {
243+
packages: hightlightPackages,
244+
contacts: []
245+
}
246+
}),
247+
logger
248+
);
249+
250+
assert.deepStrictEqual(
251+
result.highlighted.packages.sort(),
252+
[
253+
"nanoid@5.1.6",
254+
"zen-observable@0.8.15"
255+
]
256+
);
257+
});
258+
224259
test("fetch payload of pacote on the npm registry", async() => {
225260
const result = await from(
226261
"pacote",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "highlighted-packages",
3+
"version": "0.1.0",
4+
"description": "Mock package with dependency versions",
5+
"main": "index.js",
6+
"homepage": "https://github.com/username/highlighted-packages#readme",
7+
"dependencies": {
8+
"zen-observable": "0.8.15",
9+
"nanoid": "5.1.6",
10+
"nanoevents": "9.1.0"
11+
}
12+
}

0 commit comments

Comments
 (0)