Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
// Generates a lightweight SBOM-style dependency manifest by shelling out to
|
||||
// `pnpm list -r --json --depth=Infinity` and writing the result to
|
||||
// `sbom-pnpm.json` in the repo root.
|
||||
// Generates a lightweight SBOM-style dependency manifest. Docker builds point
|
||||
// it at the already-pruned runtime node_modules tree so the document describes
|
||||
// files that actually ship, rather than the full development workspace.
|
||||
//
|
||||
// Why pnpm-list instead of @cyclonedx/cyclonedx-npm:
|
||||
// - cyclonedx-npm is opinionated about lockfile shape and tends to mis-handle
|
||||
@@ -18,10 +18,14 @@ import { spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import process from "node:process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
const ROOT = path.resolve(HERE, "..");
|
||||
const DOCUMENT_ROOT = process.env.SBOM_ROOT_PATH ? path.resolve(process.env.SBOM_ROOT_PATH) : ROOT;
|
||||
const RUNTIME_NODE_MODULES_PATH = process.env.SBOM_NODE_MODULES_PATH
|
||||
? path.resolve(process.env.SBOM_NODE_MODULES_PATH)
|
||||
: undefined;
|
||||
const OUTPUT_PATH = process.env.SBOM_OUTPUT_PATH
|
||||
? path.resolve(process.env.SBOM_OUTPUT_PATH)
|
||||
: path.join(ROOT, "sbom-pnpm.json");
|
||||
@@ -53,31 +57,92 @@ function parseTree(raw) {
|
||||
}
|
||||
}
|
||||
|
||||
function repoVersion() {
|
||||
function repoVersion(root = DOCUMENT_ROOT) {
|
||||
try {
|
||||
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf8"));
|
||||
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf8"));
|
||||
return { name: pkg.name ?? null, version: pkg.version ?? null };
|
||||
} catch {
|
||||
return { name: null, version: null };
|
||||
}
|
||||
}
|
||||
|
||||
function packageDirectories(packageRoot) {
|
||||
if (!fs.existsSync(packageRoot)) return [];
|
||||
const directories = [];
|
||||
for (const entry of fs.readdirSync(packageRoot, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory() && !entry.isSymbolicLink()) continue;
|
||||
if (entry.name.startsWith("@")) {
|
||||
const scopeRoot = path.join(packageRoot, entry.name);
|
||||
if (!fs.existsSync(scopeRoot)) continue;
|
||||
for (const scopedEntry of fs.readdirSync(scopeRoot, { withFileTypes: true })) {
|
||||
if (scopedEntry.isDirectory() || scopedEntry.isSymbolicLink()) {
|
||||
directories.push(path.join(scopeRoot, scopedEntry.name));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
directories.push(path.join(packageRoot, entry.name));
|
||||
}
|
||||
}
|
||||
return directories;
|
||||
}
|
||||
|
||||
export function collectInstalledRuntimePackages(nodeModulesPath) {
|
||||
const virtualStore = path.join(nodeModulesPath, ".pnpm");
|
||||
if (!fs.existsSync(virtualStore)) {
|
||||
throw new Error(`runtime pnpm virtual store not found: ${virtualStore}`);
|
||||
}
|
||||
const packages = new Map();
|
||||
for (const entry of fs.readdirSync(virtualStore, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory() || entry.name === "node_modules") continue;
|
||||
const packageRoot = path.join(virtualStore, entry.name, "node_modules");
|
||||
for (const directory of packageDirectories(packageRoot)) {
|
||||
const manifestPath = path.join(directory, "package.json");
|
||||
if (!fs.existsSync(manifestPath)) continue;
|
||||
try {
|
||||
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
||||
if (typeof manifest.name !== "string" || typeof manifest.version !== "string") continue;
|
||||
const key = `${manifest.name}@${manifest.version}`;
|
||||
packages.set(key, {
|
||||
name: manifest.name,
|
||||
version: manifest.version,
|
||||
license: typeof manifest.license === "string" ? manifest.license : null,
|
||||
});
|
||||
} catch {
|
||||
// A malformed third-party manifest is omitted here and will remain
|
||||
// visible to the separate fail-closed license inventory gate.
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...packages.values()].sort((left, right) => `${left.name}@${left.version}`.localeCompare(`${right.name}@${right.version}`));
|
||||
}
|
||||
|
||||
function generatedAt() {
|
||||
const sourceDateEpoch = process.env.SOURCE_DATE_EPOCH;
|
||||
if (!sourceDateEpoch || !/^\d+$/.test(sourceDateEpoch)) return null;
|
||||
return new Date(Number(sourceDateEpoch) * 1000).toISOString();
|
||||
}
|
||||
|
||||
function main() {
|
||||
const raw = runPnpmList();
|
||||
const tree = parseTree(raw);
|
||||
const runtimePackages = RUNTIME_NODE_MODULES_PATH ? collectInstalledRuntimePackages(RUNTIME_NODE_MODULES_PATH) : undefined;
|
||||
const tree = runtimePackages ? undefined : parseTree(runPnpmList());
|
||||
const repo = repoVersion();
|
||||
const document = {
|
||||
schema: "hivegrid.sbom-pnpm/v1",
|
||||
generatedAt: new Date().toISOString(),
|
||||
schema: "hivegrid.sbom-pnpm/v2",
|
||||
generatedAt: generatedAt(),
|
||||
generator: "scripts/generate-sbom.mjs",
|
||||
source: "pnpm list -r --json --depth=Infinity",
|
||||
source: runtimePackages ? "installed pruned pnpm runtime tree" : "pnpm list -r --json --depth=Infinity",
|
||||
target: process.env.SBOM_TARGET ?? null,
|
||||
repo,
|
||||
workspaces: tree,
|
||||
...(runtimePackages ? { packageCount: runtimePackages.length, packages: runtimePackages } : { workspaces: tree }),
|
||||
};
|
||||
fs.mkdirSync(path.dirname(OUTPUT_PATH), { recursive: true });
|
||||
fs.writeFileSync(OUTPUT_PATH, JSON.stringify(document, null, 2));
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`SBOM written: ${OUTPUT_PATH} (${tree.length} workspace entries)`);
|
||||
console.log(
|
||||
`SBOM written: ${OUTPUT_PATH} (${runtimePackages ? `${runtimePackages.length} runtime packages` : `${tree.length} workspace entries`})`,
|
||||
);
|
||||
}
|
||||
|
||||
main();
|
||||
if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
|
||||
main();
|
||||
}
|
||||
Reference in new issue
Block a user