forked from CycloneDX/cdxgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobom.js
36 lines (33 loc) · 920 Bytes
/
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
import { Bom } from "@appthreat/cdx-proto";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
const stringifyIfNeeded = (bomJson) => {
if (typeof bomJson === "string" || bomJson instanceof String) {
return bomJson;
}
return JSON.stringify(bomJson);
};
export const writeBinary = (bomJson, binFile) => {
if (bomJson && binFile) {
const bomObject = new Bom();
writeFileSync(
binFile,
bomObject
.fromJsonString(stringifyIfNeeded(bomJson), {
ignoreUnknownFields: true
})
.toBinary({ writeUnknownFields: true })
);
}
};
export const readBinary = (binFile, asJson = true) => {
if (!existsSync(binFile)) {
return undefined;
}
const bomObject = new Bom().fromBinary(readFileSync(binFile), {
readUnknownFields: true
});
if (asJson) {
return bomObject.toJson({ emitDefaultValues: true });
}
return bomObject;
};