Skip to content

Commit 3a25e34

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

File tree

3 files changed

+78
-68
lines changed

3 files changed

+78
-68
lines changed

workspaces/vis-network/src/dataset.js

Lines changed: 73 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,95 @@ 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.rootDependency.name);
102+
const rootDependency = dependencies.find(([name]) => name === data.rootDependency.name);
103103
const rootContributors = [
104104
rootDependency[1].metadata.author,
105105
...rootDependency[1].metadata.maintainers,
106106
...rootDependency[1].metadata.publishers
107107
];
108-
for (const [packageName, descriptor] of dataEntries) {
109-
const contributors = [descriptor.metadata.author, ...descriptor.metadata.maintainers, ...descriptor.metadata.publishers];
108+
109+
const extractor = new Extractors.Payload(data, [
110+
new Extractors.Probes.Licenses(),
111+
new Extractors.Probes.Extensions()]);
112+
113+
const { extensions, licenses } = extractor.extractAndMerge();
114+
115+
this.extensions = extensions;
116+
this.licenses = licenses;
117+
118+
for (const [packageName, descriptor] of dependencies) {
119+
const contributors = [
120+
descriptor.metadata.author, ...descriptor.metadata.maintainers, ...descriptor.metadata.publishers
121+
];
110122
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));
123+
const {
124+
id,
125+
usedBy,
126+
flags,
127+
size,
128+
author,
129+
warnings,
130+
links
131+
} = opt;
132+
const filteredWarnings = warnings.filter(
133+
(row) => !this.warningsToIgnore.has(row.kind)
134+
);
114135
const hasWarnings = filteredWarnings.length > 0;
115136

116137
opt.name = packageName;
117138
opt.version = currVersion;
118139
opt.hidden = false;
119140
opt.hasWarnings = hasWarnings;
120141

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

125148
if (flags.includes("hasIndirectDependencies")) {
126149
this.indirectDependencies++;
127150
}
128-
this.size += size;
151+
this.size = size;
129152

130153
const flagStr = utils.getFlagsEmojisInlined(
131154
flags,
132-
hasWarnings ? this.flagsToIgnore : new Set([...this.flagsToIgnore, "hasWarnings"])
155+
hasWarnings
156+
? this.flagsToIgnore
157+
: new Set([...this.flagsToIgnore, "hasWarnings"])
133158
);
134-
const isFriendly = window.settings.config.showFriendlyDependencies & rootContributors.some(
135-
(rootContributor) => contributors.some((contributor) => {
159+
const isFriendly =
160+
window.settings.config.showFriendlyDependencies &
161+
rootContributors.some((rootContributor) => contributors.some((contributor) => {
136162
if (contributor === null || rootContributor === null) {
137163
return false;
138164
}
139-
else if (contributor.email && contributor.email === rootContributor.email) {
165+
else if (
166+
contributor.email &&
167+
contributor.email === rootContributor.email
168+
) {
140169
return true;
141170
}
142-
else if (contributor.name && contributor.name === rootContributor.name) {
171+
else if (
172+
contributor.name &&
173+
contributor.name === rootContributor.name
174+
) {
143175
return true;
144176
}
145177

146178
return false;
147179
})
148-
);
180+
);
149181
opt.isFriendly = isFriendly;
150182
this.packages.push({
151183
id,
@@ -170,7 +202,10 @@ export default class NodeSecureDataSet extends EventTarget {
170202
this.rawNodesData.push(Object.assign({ id, label }, color));
171203

172204
for (const [name, version] of Object.entries(usedBy)) {
173-
this.rawEdgesData.push({ from: id, to: data.dependencies[name].versions[version].id });
205+
this.rawEdgesData.push({
206+
from: id,
207+
to: data.dependencies[name].versions[version].id
208+
});
174209
}
175210
}
176211
}
@@ -187,25 +222,13 @@ export default class NodeSecureDataSet extends EventTarget {
187222
return null;
188223
}
189224

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-
204225
computeAuthor(author, spec, contributors = []) {
205226
if (author === null) {
206227
return;
207228
}
208-
const contributor = contributors.find((contributor) => contributor.email === author.email && contributor.npmAvatar !== null);
229+
const contributor = contributors.find(
230+
(contributor) => contributor.email === author.email && contributor.npmAvatar !== null
231+
);
209232

210233
if (this.authors.has(author.name)) {
211234
this.authors.get(author.name).packages.add(spec);
@@ -229,7 +252,10 @@ export default class NodeSecureDataSet extends EventTarget {
229252
}
230253

231254
isHighlighted(contact) {
232-
return this.#highligthedContacts.names.has(contact.name) || this.#highligthedContacts.emails.has(contact.email);
255+
return (
256+
this.#highligthedContacts.names.has(contact.name) ||
257+
this.#highligthedContacts.emails.has(contact.email)
258+
);
233259
}
234260

235261
findPackagesByName(name) {

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: 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: [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)