Skip to content

Commit dde30e3

Browse files
author
Raulo Erwan
committed
feat(vis-network): use scanner extractors
1 parent 2e60c97 commit dde30e3

File tree

5 files changed

+89
-108
lines changed

5 files changed

+89
-108
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"@nodesecure/ossf-scorecard-sdk": "^3.2.1",
100100
"@nodesecure/rc": "^5.0.0",
101101
"@nodesecure/report": "4.1.0",
102-
"@nodesecure/scanner": "8.1.0",
102+
"@nodesecure/scanner": "8.2.0",
103103
"@nodesecure/server": "1.0.0",
104104
"@nodesecure/utils": "^2.2.0",
105105
"@nodesecure/vulnera": "^2.0.1",

workspaces/vis-network/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
},
3131
"devDependencies": {
3232
"@nodesecure/flags": "^3.0.3",
33-
"@nodesecure/scanner": "8.1.0"
33+
"@nodesecure/scanner": "8.2.0"
3434
}
3535
}

workspaces/vis-network/src/dataset.js

Lines changed: 81 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Import Third-party Dependencies
2+
import { Extractors } from "@nodesecure/scanner/extractors";
23
import prettyBytes from "pretty-bytes";
34
import { DataSet } from "vis-data";
45

@@ -93,87 +94,97 @@ export default class NodeSecureDataSet extends EventTarget {
9394
return acc;
9495
}, { names: new Set(), emails: new Set() });
9596

96-
const dataEntries = Object.entries(data.dependencies);
97-
this.dependenciesCount = dataEntries.length;
97+
const dependencies = Object.entries(data.dependencies);
98+
this.dependenciesCount = dependencies.length;
9899

99100
this.rawEdgesData = [];
100101
this.rawNodesData = [];
101102

102-
const rootDependency = dataEntries.find(([name]) => name === data.rootDependency.name);
103-
const rootContributors = [
103+
const rootDependency = dependencies.find(([name]) => name === data.rootDependency.name);
104+
this.rootContributors = [
104105
rootDependency[1].metadata.author,
105106
...rootDependency[1].metadata.maintainers,
106107
...rootDependency[1].metadata.publishers
107108
];
108-
for (const [packageName, descriptor] of dataEntries) {
109-
const contributors = [descriptor.metadata.author, ...descriptor.metadata.maintainers, ...descriptor.metadata.publishers];
110-
for (const [currVersion, opt] of Object.entries(descriptor.versions)) {
111-
const { id, usedBy, flags, size, uniqueLicenseIds, author, composition, warnings, links } = opt;
112-
const filteredWarnings = warnings
113-
.filter((row) => !this.warningsToIgnore.has(row.kind));
114-
const hasWarnings = filteredWarnings.length > 0;
115-
116-
opt.name = packageName;
117-
opt.version = currVersion;
118-
opt.hidden = false;
119-
opt.hasWarnings = hasWarnings;
120-
121-
this.computeExtension(composition.extensions);
122-
this.computeLicense(uniqueLicenseIds);
123-
this.computeAuthor(author, `${packageName}@${currVersion}`, contributors);
124-
125-
if (flags.includes("hasIndirectDependencies")) {
126-
this.indirectDependencies++;
127-
}
128-
this.size += size;
129-
130-
const flagStr = utils.getFlagsEmojisInlined(
131-
flags,
132-
hasWarnings ? this.flagsToIgnore : new Set([...this.flagsToIgnore, "hasWarnings"])
133-
);
134-
const isFriendly = window.settings.config.showFriendlyDependencies & rootContributors.some(
135-
(rootContributor) => contributors.some((contributor) => {
136-
if (contributor === null || rootContributor === null) {
137-
return false;
138-
}
139-
else if (contributor.email && contributor.email === rootContributor.email) {
140-
return true;
141-
}
142-
else if (contributor.name && contributor.name === rootContributor.name) {
143-
return true;
144-
}
145109

110+
const extractor = new Extractors.Payload(data, [
111+
new Extractors.Probes.Licenses(),
112+
new Extractors.Probes.Extensions()
113+
]);
114+
115+
extractor.on("manifest", (currVersion, opt, { name, dependency }) => {
116+
const contributors = [dependency.metadata.author, ...dependency.metadata.maintainers, ...dependency.metadata.publishers];
117+
const packageName = name;
118+
const { id, usedBy, flags, size, author, warnings, links } = opt;
119+
const filteredWarnings = warnings
120+
.filter((row) => !this.warningsToIgnore.has(row.kind));
121+
const hasWarnings = filteredWarnings.length > 0;
122+
123+
opt.name = packageName;
124+
opt.version = currVersion;
125+
opt.hidden = false;
126+
opt.hasWarnings = hasWarnings;
127+
128+
this.computeAuthor(author, `${packageName}@${currVersion}`, contributors);
129+
130+
if (flags.includes("hasIndirectDependencies")) {
131+
this.indirectDependencies++;
132+
}
133+
this.size += size;
134+
135+
const flagStr = utils.getFlagsEmojisInlined(
136+
flags,
137+
hasWarnings ? this.flagsToIgnore : new Set([...this.flagsToIgnore, "hasWarnings"])
138+
);
139+
const isFriendly = window.settings.config.showFriendlyDependencies & this.rootContributors.some(
140+
(rootContributor) => contributors.some((contributor) => {
141+
if (contributor === null || rootContributor === null) {
146142
return false;
147-
})
148-
);
149-
opt.isFriendly = isFriendly;
150-
this.packages.push({
151-
id,
152-
name: packageName,
153-
version: currVersion,
154-
hasWarnings,
155-
flags: flagStr.replace(/\s/g, ""),
156-
links,
157-
isFriendly
158-
});
159-
160-
const label = `<b>${packageName}@${currVersion}</b>${flagStr}\n<b>[${prettyBytes(size)}]</b>`;
161-
const color = utils.getNodeColor({
162-
id,
163-
hasWarnings,
164-
isFriendly,
165-
theme: this.theme.toUpperCase()
166-
});
167-
color.font.multi = "html";
168-
169-
this.linker.set(Number(id), opt);
170-
this.rawNodesData.push(Object.assign({ id, label }, color));
171-
172-
for (const [name, version] of Object.entries(usedBy)) {
173-
this.rawEdgesData.push({ from: id, to: data.dependencies[name].versions[version].id });
174-
}
143+
}
144+
else if (contributor.email && contributor.email === rootContributor.email) {
145+
return true;
146+
}
147+
else if (contributor.name && contributor.name === rootContributor.name) {
148+
return true;
149+
}
150+
151+
return false;
152+
})
153+
);
154+
opt.isFriendly = isFriendly;
155+
156+
this.packages.push({
157+
id,
158+
name: packageName,
159+
version: currVersion,
160+
hasWarnings,
161+
flags: flagStr.replace(/\s/g, ""),
162+
links,
163+
isFriendly
164+
});
165+
166+
const label = `<b>${packageName}@${currVersion}</b>${flagStr}\n<b>[${prettyBytes(size)}]</b>`;
167+
const color = utils.getNodeColor({
168+
id,
169+
hasWarnings,
170+
isFriendly,
171+
theme: this.theme.toUpperCase()
172+
});
173+
color.font.multi = "html";
174+
175+
this.linker.set(Number(id), opt);
176+
this.rawNodesData.push(Object.assign({ id, label }, color));
177+
178+
for (const [name, version] of Object.entries(usedBy)) {
179+
this.rawEdgesData.push({ from: id, to: this.data.dependencies[name].versions[version].id });
175180
}
176-
}
181+
});
182+
183+
const { extensions, licenses } = extractor.extractAndMerge();
184+
185+
this.extensions = extensions;
186+
this.licenses = licenses;
187+
177188
console.log("[NodeSecureDataSet] Initialization done!");
178189
}
179190

@@ -187,20 +198,6 @@ export default class NodeSecureDataSet extends EventTarget {
187198
return null;
188199
}
189200

190-
computeExtension(extensions) {
191-
for (const extName of extensions) {
192-
if (extName !== "") {
193-
this.extensions[extName] = Reflect.has(this.extensions, extName) ? ++this.extensions[extName] : 1;
194-
}
195-
}
196-
}
197-
198-
computeLicense(uniqueLicenseIds) {
199-
for (const licenseName of uniqueLicenseIds) {
200-
this.licenses[licenseName] = Reflect.has(this.licenses, licenseName) ? ++this.licenses[licenseName] : 1;
201-
}
202-
}
203-
204201
computeAuthor(author, spec, contributors = []) {
205202
if (author === null) {
206203
return;

workspaces/vis-network/test/dataset-payload.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
},
6565
"licenses": [],
6666
"uniqueLicenseIds": [
67-
"Unlicense"
67+
"MIT"
6868
],
6969
"name": "pkg2",
7070
"version": "1.0.3"
@@ -87,7 +87,7 @@
8787
},
8888
"licenses": [],
8989
"uniqueLicenseIds": [
90-
"Unlicense"
90+
"MIT"
9191
],
9292
"name": "pkg2",
9393
"version": "1.0.4"
@@ -127,7 +127,7 @@
127127
},
128128
"licenses": [],
129129
"uniqueLicenseIds": [
130-
"Licence1"
130+
"RND"
131131
]
132132
}
133133
}
@@ -149,7 +149,8 @@
149149
],
150150
"size": 200,
151151
"author": {
152-
"name": "john doe"
152+
"name": "john doe",
153+
"email": "[email protected]"
153154
},
154155
"composition": {
155156
"extensions": [

workspaces/vis-network/test/dataset.test.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Import Node.js Dependencies
2-
import { test } from "node:test";
32
import assert from "node:assert";
3+
import { test } from "node:test";
44

55
// Import Internal Dependencies
66
import NodeSecureDataSet from "../src/dataset.js";
@@ -38,16 +38,6 @@ test("NodeSecureDataSet.prettySize", () => {
3838
assert.equal(nsDataSet.prettySize, "1.34 kB", "should convert bytes to human readable string");
3939
});
4040

41-
test("NodeSecureDataSet.computeExtensions", () => {
42-
const nsDataSet = new NodeSecureDataSet();
43-
assert.equal(Object.keys(nsDataSet.extensions).length, 0, "should have 0 extensions");
44-
45-
nsDataSet.computeExtension([".js", ".js", ".json"]);
46-
47-
assert.equal(Object.keys(nsDataSet.extensions).length, 2, "should have 2 extension (js and json)");
48-
assert.equal(nsDataSet.extensions[".js"], 2, "should have 2 '.js' extensions'");
49-
});
50-
5141
test("NodeSecureDataSet.isHighlighted", async() => {
5242
const nsDataSet = new NodeSecureDataSet();
5343
await nsDataSet.init(dataSetPayload);
@@ -63,13 +53,6 @@ test("NodeSecureDataSet.isHighlighted", async() => {
6353
"email: [email protected] should be hightlighted");
6454
});
6555

66-
test("NodeSecureDataSet.computeLicenses", () => {
67-
const nsDataSet = new NodeSecureDataSet();
68-
nsDataSet.computeLicense(["MIT", "MIT", "RND"]);
69-
assert.equal(Object.keys(nsDataSet.licenses).length, 3, "should have 3 licenses (MIT, RND & 1 unknown)");
70-
assert.equal(nsDataSet.licenses.MIT, 2, "should have 2 MIT licenses");
71-
});
72-
7356
test("NodeSecureDataSet.computeAuthors", () => {
7457
const nsDataSet = new NodeSecureDataSet();
7558
nsDataSet.computeAuthor({ name: "John Doe" }, "[email protected]");

0 commit comments

Comments
 (0)