/
protobom.js
66 lines (63 loc) · 1.68 KB
/
protobom.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
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { cdx_15, cdx_16 } from "@appthreat/cdx-proto";
/**
* Stringify the given bom json based on the type.
*
* @param {string | Object} bomJson string or object
* @returns {string} BOM json string
*/
const stringifyIfNeeded = (bomJson) => {
if (typeof bomJson === "string" || bomJson instanceof String) {
return bomJson;
}
return JSON.stringify(bomJson);
};
/**
* Method to convert the given bom json to proto binary
*
* @param {string | Object} bomJson BOM Json
* @param {string} binFile Binary file name
*/
export const writeBinary = (bomJson, binFile) => {
if (bomJson && binFile) {
let bomObject = undefined;
if (+bomJson.specVersion === 1.6) {
bomObject = new cdx_16.Bom();
} else {
bomObject = new cdx_15.Bom();
}
writeFileSync(
binFile,
bomObject
.fromJsonString(stringifyIfNeeded(bomJson), {
ignoreUnknownFields: true,
})
.toBinary({ writeUnknownFields: true }),
);
}
};
/**
* Method to read a serialized binary
*
* @param {string} binFile Binary file name
* @param {boolean} asJson Convert to JSON
* @param {number} specVersion Specification version. Defaults to 1.5
*/
export const readBinary = (binFile, asJson = true, specVersion = 1.5) => {
if (!existsSync(binFile)) {
return undefined;
}
let bomLib = undefined;
if (specVersion === 1.6) {
bomLib = new cdx_16.Bom();
} else {
bomLib = new cdx_15.Bom();
}
const bomObject = bomLib.fromBinary(readFileSync(binFile), {
readUnknownFields: true,
});
if (asJson) {
return bomObject.toJson({ emitDefaultValues: true });
}
return bomObject;
};