-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathreport.ts
More file actions
101 lines (90 loc) · 2.39 KB
/
report.ts
File metadata and controls
101 lines (90 loc) · 2.39 KB
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
// Import Node.js Dependencies
import fs from "node:fs";
// Import Third-party Dependencies
import send from "@polka/send-type";
import { report } from "@nodesecure/report";
import { appCache } from "@nodesecure/cache";
import type { Request, Response } from "express-serve-static-core";
import type { RC } from "@nodesecure/rc";
// Import Internal Dependencies
import { context } from "../ALS.js";
import { bodyParser } from "../middlewares/bodyParser.js";
// TODO: provide a non-file-based API on RC side ?
const kReportPayload: Partial<RC["report"]> = {
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"
}
]
};
export async function post(req: Request, res: Response) {
const body = await bodyParser(req) as {
title: string;
includesAllDeps: boolean;
theme: "light" | "dark";
};
const { title, includesAllDeps, theme } = body;
const { dataFilePath } = context.getStore()!;
const scannerPayload = dataFilePath ?
JSON.parse(fs.readFileSync(dataFilePath, "utf-8")) :
appCache.getPayload((await appCache.payloadsList()).current);
const rootDependencyName = scannerPayload.rootDependencyName;
const [organizationPrefixOrRepo, repo] = rootDependencyName.split("/");
const reportPayload = structuredClone({
...kReportPayload,
title,
npm: {
organizationPrefix: repo === undefined ? null : organizationPrefixOrRepo,
packages: [repo === undefined ? organizationPrefixOrRepo : repo]
},
theme
});
try {
const dependencies = includesAllDeps ?
scannerPayload.dependencies :
{
[rootDependencyName]: scannerPayload.dependencies[rootDependencyName]
};
const data = await report(
dependencies,
reportPayload
);
return send(res, 200, {
data
}, {
"Content-type": "application/pdf"
});
}
catch (err) {
console.error(err);
return send(
res,
500
);
}
}