-
Notifications
You must be signed in to change notification settings - Fork 39
/
dataset.js
198 lines (169 loc) · 5.73 KB
/
dataset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Import Third-party Dependencies
import prettyBytes from "pretty-bytes";
import { DataSet } from "vis-data";
// Import Internal Dependencies
import * as utils from "./utils.js";
export default class NodeSecureDataSet extends EventTarget {
/**
*
* @param {object} [options]
* @param {string[]} [options.flagsToIgnore=[]]
* @param {string[]} [options.warningsToIgnore=[]]
*/
constructor(options = {}) {
super();
const { flagsToIgnore = [], warningsToIgnore = [] } = options;
this.flagsToIgnore = new Set(flagsToIgnore);
this.warningsToIgnore = new Set(warningsToIgnore);
this.reset();
}
get prettySize() {
return prettyBytes(this.size);
}
reset() {
this.warnings = [];
this.packages = [];
this.linker = new Map();
this.authors = new Map();
this.extensions = Object.create(null);
this.licenses = { Unknown: 0 };
this.rawNodesData = [];
this.rawEdgesData = [];
this.data = {};
this.dependenciesCount = 0;
this.size = 0;
this.indirectDependencies = 0;
}
async init(
initialPayload = null,
initialFlags = {}
) {
console.log("[NodeSecureDataSet] Initialization started...");
let FLAGS;
let data;
this.reset();
if (initialPayload) {
data = initialPayload;
FLAGS = initialFlags;
}
else {
([data, FLAGS] = await Promise.all([
utils.getJSON("/data"), utils.getJSON("/flags")
]));
}
this.FLAGS = FLAGS;
this.warnings = data.warnings;
this.data = data;
const dataEntries = Object.entries(data.dependencies);
this.dependenciesCount = dataEntries.length;
this.rawEdgesData = [];
this.rawNodesData = [];
const rootDependency = dataEntries.find(([name]) => name === data.rootDependencyName);
const rootContributors = [
rootDependency[1].metadata.author,
...rootDependency[1].metadata.maintainers,
...rootDependency[1].metadata.publishers
];
for (const [packageName, descriptor] of dataEntries) {
const contributors = [descriptor.metadata.author, ...descriptor.metadata.maintainers, ...descriptor.metadata.publishers];
for (const [currVersion, opt] of Object.entries(descriptor.versions)) {
const { id, usedBy, flags, size, uniqueLicenseIds, author, composition, warnings, links } = opt;
const filteredWarnings = warnings
.filter((row) => !this.warningsToIgnore.has(row.kind));
const hasWarnings = filteredWarnings.length > 0;
opt.name = packageName;
opt.version = currVersion;
opt.hidden = false;
opt.hasWarnings = hasWarnings;
this.computeExtension(composition.extensions);
this.computeLicense(uniqueLicenseIds);
this.computeAuthor(author, `${packageName}@${currVersion}`, contributors);
if (flags.includes("hasIndirectDependencies")) {
this.indirectDependencies++;
}
this.size += size;
const flagStr = utils.getFlagsEmojisInlined(
flags,
hasWarnings ? this.flagsToIgnore : new Set([...this.flagsToIgnore, "hasWarnings"])
);
const isFriendly = window.settings.config.showFriendlyDependencies & rootContributors.some(
(rootContributor) => contributors.some((contributor) => {
if (contributor.email && contributor.email === rootContributor.email) {
return true;
}
else if (contributor.name && contributor.name === rootContributor.name) {
return true;
}
return false;
})
);
opt.isFriendly = isFriendly;
this.packages.push({
id,
name: packageName,
version: currVersion,
hasWarnings,
flags: flagStr.replace(/\s/g, ""),
links,
isFriendly
});
const label = `<b>${packageName}@${currVersion}</b>${flagStr}\n<b>[${prettyBytes(size)}]</b>`;
const color = utils.getNodeColor({
id,
hasWarnings,
isFriendly
});
color.font.multi = "html";
this.linker.set(Number(id), opt);
this.rawNodesData.push(Object.assign({ id, label }, color));
for (const [name, version] of Object.entries(usedBy)) {
this.rawEdgesData.push({ from: id, to: data.dependencies[name].versions[version].id });
}
}
}
console.log("[NodeSecureDataSet] Initialization done!");
}
getAuthorByEmail(emailToMatch) {
for (const [name, data] of this.authors.entries()) {
if (data.email === emailToMatch) {
return [name, data];
}
}
return null;
}
computeExtension(extensions) {
for (const extName of extensions) {
if (extName !== "") {
this.extensions[extName] = Reflect.has(this.extensions, extName) ? ++this.extensions[extName] : 1;
}
}
}
computeLicense(uniqueLicenseIds) {
for (const licenseName of uniqueLicenseIds) {
this.licenses[licenseName] = Reflect.has(this.licenses, licenseName) ? ++this.licenses[licenseName] : 1;
}
}
computeAuthor(author, spec, contributors = []) {
if (author === null) {
return;
}
const contributor = contributors.find((contributor) => contributor.email === author.email && contributor.npmAvatar !== null);
if (this.authors.has(author.name)) {
this.authors.get(author.name).packages.add(spec);
}
else {
this.authors.set(
author.name,
Object.assign({}, author, { packages: new Set([spec]) })
);
}
if (contributor && contributor.npmAvatar) {
this.authors.get(author.name).npmAvatar = contributor.npmAvatar;
}
}
build() {
const nodes = new DataSet(this.rawNodesData);
const edges = new DataSet(this.rawEdgesData);
return { nodes, edges };
}
}