Skip to content

Commit 33c58a1

Browse files
author
Raulo Erwan
committed
feat(vis-network): use scanner extractors
1 parent b18f6bc commit 33c58a1

File tree

3 files changed

+80
-68
lines changed

3 files changed

+80
-68
lines changed

workspaces/vis-network/src/dataset.js

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

@@ -50,10 +51,7 @@ export default class NodeSecureDataSet extends EventTarget {
5051
this.indirectDependencies = 0;
5152
}
5253

53-
async init(
54-
initialPayload = null,
55-
initialFlags = {}
56-
) {
54+
async init(initialPayload = null, initialFlags = {}) {
5755
console.log("[NodeSecureDataSet] Initialization started...");
5856
let FLAGS;
5957
/** @type {import("@nodesecure/scanner").Payload | null} */
@@ -65,9 +63,10 @@ export default class NodeSecureDataSet extends EventTarget {
6563
FLAGS = initialFlags;
6664
}
6765
else {
68-
([data, FLAGS] = await Promise.all([
69-
utils.getJSON("/data"), utils.getJSON("/flags")
70-
]));
66+
[data, FLAGS] = await Promise.all([
67+
utils.getJSON("/data"),
68+
utils.getJSON("/flags")
69+
]);
7170
}
7271

7372
this.FLAGS = FLAGS;
@@ -77,12 +76,11 @@ export default class NodeSecureDataSet extends EventTarget {
7776
return;
7877
}
7978

80-
this.warnings = data.warnings.map(
81-
(warning) => (typeof warning === "string" ? warning : warning.message)
79+
this.warnings = data.warnings.map((warning) => (typeof warning === "string" ? warning : warning.message)
8280
);
8381

84-
this.#highligthedContacts = data.highlighted.contacts
85-
.reduce((acc, { name, email }) => {
82+
this.#highligthedContacts = data.highlighted.contacts.reduce(
83+
(acc, { name, email }) => {
8684
if (name) {
8785
acc.names.add(name);
8886
}
@@ -91,61 +89,97 @@ export default class NodeSecureDataSet extends EventTarget {
9189
}
9290

9391
return acc;
94-
}, { names: new Set(), emails: new Set() });
92+
},
93+
{ names: new Set(), emails: new Set() }
94+
);
9595

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

9999
this.rawEdgesData = [];
100100
this.rawNodesData = [];
101101

102-
const rootDependency = dataEntries.find(([name]) => name === data.rootDependencyName);
102+
const rootDependency = dependencies.find(
103+
([name]) => name === data.rootDependencyName
104+
);
103105
const rootContributors = [
104106
rootDependency[1].metadata.author,
105107
...rootDependency[1].metadata.maintainers,
106108
...rootDependency[1].metadata.publishers
107109
];
108-
for (const [packageName, descriptor] of dataEntries) {
109-
const contributors = [descriptor.metadata.author, ...descriptor.metadata.maintainers, ...descriptor.metadata.publishers];
110+
111+
const extractor = new Extractors.Payload(data, [
112+
new Extractors.Probes.Licenses(),
113+
new Extractors.Probes.Extensions()]);
114+
115+
const { extensions, licenses } = extractor.extractAndMerge();
116+
117+
this.extensions = extensions;
118+
this.licenses = licenses;
119+
120+
for (const [packageName, descriptor] of dependencies) {
121+
const contributors = [
122+
descriptor.metadata.author, ...descriptor.metadata.maintainers, ...descriptor.metadata.publishers
123+
];
110124
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));
125+
const {
126+
id,
127+
usedBy,
128+
flags,
129+
size,
130+
author,
131+
warnings,
132+
links
133+
} = opt;
134+
const filteredWarnings = warnings.filter(
135+
(row) => !this.warningsToIgnore.has(row.kind)
136+
);
114137
const hasWarnings = filteredWarnings.length > 0;
115138

116139
opt.name = packageName;
117140
opt.version = currVersion;
118141
opt.hidden = false;
119142
opt.hasWarnings = hasWarnings;
120143

121-
this.computeExtension(composition.extensions);
122-
this.computeLicense(uniqueLicenseIds);
123-
this.computeAuthor(author, `${packageName}@${currVersion}`, contributors);
144+
this.computeAuthor(
145+
author,
146+
`${packageName}@${currVersion}`,
147+
contributors
148+
);
124149

125150
if (flags.includes("hasIndirectDependencies")) {
126151
this.indirectDependencies++;
127152
}
128-
this.size += size;
153+
this.size = size;
129154

130155
const flagStr = utils.getFlagsEmojisInlined(
131156
flags,
132-
hasWarnings ? this.flagsToIgnore : new Set([...this.flagsToIgnore, "hasWarnings"])
157+
hasWarnings
158+
? this.flagsToIgnore
159+
: new Set([...this.flagsToIgnore, "hasWarnings"])
133160
);
134-
const isFriendly = window.settings.config.showFriendlyDependencies & rootContributors.some(
135-
(rootContributor) => contributors.some((contributor) => {
161+
const isFriendly =
162+
window.settings.config.showFriendlyDependencies &
163+
rootContributors.some((rootContributor) => contributors.some((contributor) => {
136164
if (contributor === null || rootContributor === null) {
137165
return false;
138166
}
139-
else if (contributor.email && contributor.email === rootContributor.email) {
167+
else if (
168+
contributor.email &&
169+
contributor.email === rootContributor.email
170+
) {
140171
return true;
141172
}
142-
else if (contributor.name && contributor.name === rootContributor.name) {
173+
else if (
174+
contributor.name &&
175+
contributor.name === rootContributor.name
176+
) {
143177
return true;
144178
}
145179

146180
return false;
147181
})
148-
);
182+
);
149183
opt.isFriendly = isFriendly;
150184
this.packages.push({
151185
id,
@@ -170,7 +204,10 @@ export default class NodeSecureDataSet extends EventTarget {
170204
this.rawNodesData.push(Object.assign({ id, label }, color));
171205

172206
for (const [name, version] of Object.entries(usedBy)) {
173-
this.rawEdgesData.push({ from: id, to: data.dependencies[name].versions[version].id });
207+
this.rawEdgesData.push({
208+
from: id,
209+
to: data.dependencies[name].versions[version].id
210+
});
174211
}
175212
}
176213
}
@@ -187,25 +224,13 @@ export default class NodeSecureDataSet extends EventTarget {
187224
return null;
188225
}
189226

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-
204227
computeAuthor(author, spec, contributors = []) {
205228
if (author === null) {
206229
return;
207230
}
208-
const contributor = contributors.find((contributor) => contributor.email === author.email && contributor.npmAvatar !== null);
231+
const contributor = contributors.find(
232+
(contributor) => contributor.email === author.email && contributor.npmAvatar !== null
233+
);
209234

210235
if (this.authors.has(author.name)) {
211236
this.authors.get(author.name).packages.add(spec);
@@ -229,7 +254,10 @@ export default class NodeSecureDataSet extends EventTarget {
229254
}
230255

231256
isHighlighted(contact) {
232-
return this.#highligthedContacts.names.has(contact.name) || this.#highligthedContacts.emails.has(contact.email);
257+
return (
258+
this.#highligthedContacts.names.has(contact.name) ||
259+
this.#highligthedContacts.emails.has(contact.email)
260+
);
233261
}
234262

235263
findPackagesByName(name) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
},
6161
"licenses": [],
6262
"uniqueLicenseIds": [
63-
"Unlicense"
63+
"MIT"
6464
],
6565
"name": "pkg2",
6666
"version": "1.0.3"
@@ -83,7 +83,7 @@
8383
},
8484
"licenses": [],
8585
"uniqueLicenseIds": [
86-
"Unlicense"
86+
"MIT"
8787
],
8888
"name": "pkg2",
8989
"version": "1.0.4"
@@ -123,7 +123,7 @@
123123
},
124124
"licenses": [],
125125
"uniqueLicenseIds": [
126-
"Licence1"
126+
"RND"
127127
]
128128
}
129129
}
@@ -145,7 +145,8 @@
145145
],
146146
"size": 200,
147147
"author": {
148-
"name": "john doe"
148+
"name": "john doe",
149+
"email": "dummy@email.com"
149150
},
150151
"composition": {
151152
"extensions": [

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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: gentilhomme.thomas@gmail.com 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" }, "pkg@1.1");

0 commit comments

Comments
 (0)