-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathhttp.js
More file actions
62 lines (51 loc) · 1.74 KB
/
http.js
File metadata and controls
62 lines (51 loc) · 1.74 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
// Import Node.js Dependencies
import fs from "node:fs";
import path from "node:path";
import { styleText } from "node:util";
// Import Third-party Dependencies
import * as SemVer from "semver";
import * as i18n from "@nodesecure/i18n";
// Import Internal Dependencies
import { buildServer } from "../http-server/index.js";
// CONSTANTS
const kRequiredScannerRange = ">=5.1.0";
export async function start(
payloadFileBasename = "nsecure-result.json",
options = {}
) {
const port = Number(options.port);
const fileExtension = path.extname(payloadFileBasename);
if (fileExtension !== ".json" && fileExtension !== "") {
throw new Error("You must provide a JSON file (scanner payload) to open");
}
const dataFilePath = path.join(
process.cwd(),
fileExtension === "" ? `${payloadFileBasename}.json` : payloadFileBasename
);
assertScannerVersion(dataFilePath);
const httpServer = buildServer(dataFilePath, {
port: Number.isNaN(port) ? 0 : port
});
for (const eventName of ["SIGINT", "SIGTERM"]) {
process.on(eventName, () => httpServer.server.close());
}
}
function assertScannerVersion(
dataFilePath
) {
const rawStr = fs.readFileSync(dataFilePath, "utf-8");
const { scannerVersion } = JSON.parse(rawStr);
if (!SemVer.satisfies(scannerVersion, kRequiredScannerRange)) {
const error = i18n.getTokenSync(
"cli.startHttp.invalidScannerVersion",
styleText("yellow", scannerVersion),
styleText("yellow", kRequiredScannerRange)
);
const regenerate = i18n.getTokenSync("cli.startHttp.regenerate");
console.log(" > " + path.basename(dataFilePath));
console.log(" > " + styleText(["red", "bold"], error));
console.log(` > ${regenerate}`);
console.log();
process.exit(0);
}
}