-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathreport.js
83 lines (76 loc) · 2 KB
/
report.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
// Import Node.js Dependencies
import fs from "node:fs";
// Import Third-party Dependencikes
import { report } from "@nodesecure/report";
import send from "@polka/send-type";
import * as coBody from "co-body";
// Import Internal Dependencies
import { context } from "../context.js";
// TODO: provide a non-file-based API on RC side ?
const kReportPayload = {
theme: "light",
includeTransitiveInternal: false,
reporters: [
"pdf"
],
charts: [
{
name: "Extensions",
display: true,
interpolation: "d3.interpolateRainbow",
type: "bar"
},
{
name: "Licenses",
display: true,
interpolation: "d3.interpolateCool",
type: "bar"
},
{
name: "Warnings",
display: true,
type: "horizontalBar",
interpolation: "d3.interpolateInferno"
},
{
name: "Flags",
display: true,
type: "horizontalBar",
interpolation: "d3.interpolateSinebow"
}
],
logoUrl: "https://avatars0.githubusercontent.com/u/29552883?s=200&v=4"
};
export async function post(req, res) {
const body = await coBody.json(req);
const { title, includesAllDeps } = body;
const { dataFilePath } = context.getStore();
const scannerPayload = JSON.parse(fs.readFileSync(dataFilePath, "utf-8"));
const reportPayload = kReportPayload;
const rootDependencyName = scannerPayload.rootDependencyName;
const [organizationPrefixOrRepo, repo] = rootDependencyName.split("/");
Object.assign(reportPayload, {
title,
npm: {
organizationPrefix: repo === undefined ? null : organizationPrefixOrRepo,
packages: [repo === undefined ? organizationPrefixOrRepo : repo]
}
});
try {
const data = await report(
reportPayload,
includesAllDeps ? scannerPayload.dependencies : { [rootDependencyName]: scannerPayload.dependencies[rootDependencyName] }
);
return send(res, 200, {
data
}, {
"Content-type": "application/pdf"
});
}
catch {
return send(
res,
500
);
}
}