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
@@ -2,17 +2,16 @@
|
||||
/**
|
||||
* License-compliance gate.
|
||||
*
|
||||
* Enumerates production `dependencies` declared in the root + workspace
|
||||
* package.json manifests, locates each dep's installed package.json under
|
||||
* `node_modules`, and classifies its SPDX license expression against:
|
||||
* Enumerates the complete installed production dependency graph reported by
|
||||
* pnpm and classifies every package/version SPDX expression against:
|
||||
*
|
||||
* - ALLOWED_LICENSES permissive / Apache-style licenses we accept.
|
||||
* - BLOCKED_LICENSES copyleft / source-available families we refuse.
|
||||
* - scripts/license-baseline.json reviewed exceptions
|
||||
* (specific `name@version` pairs).
|
||||
*
|
||||
* Unknown SPDX strings are surfaced as warnings (manual review required)
|
||||
* but do not block. Blocked-list matches always fail with exit code 1.
|
||||
* Unknown SPDX strings and blocked-list matches fail closed unless an exact
|
||||
* package version has a documented reviewed exception.
|
||||
*
|
||||
* Output (single line on success):
|
||||
* PASS scanned=N allowed=M reviewed=K unknown=L
|
||||
@@ -24,6 +23,7 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import url from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
const __filename = url.fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
@@ -39,10 +39,15 @@ export const ALLOWED_LICENSES = new Set([
|
||||
"0BSD",
|
||||
"Unlicense",
|
||||
"CC0-1.0",
|
||||
"CC-BY-4.0",
|
||||
"(MIT OR Apache-2.0)",
|
||||
"(MIT OR CC0-1.0)",
|
||||
"BlueOak-1.0.0",
|
||||
"MIT-0",
|
||||
"MPL-2.0",
|
||||
"Python-2.0",
|
||||
"WTFPL",
|
||||
"Zlib",
|
||||
]);
|
||||
|
||||
export const BLOCKED_LICENSE_PATTERNS = [
|
||||
@@ -183,9 +188,8 @@ export function splitSpdxExpression(expr) {
|
||||
*
|
||||
* Returns one of: "allowed" | "blocked" | "unknown".
|
||||
*
|
||||
* A composite OR expression is "allowed" as long as at least one of its
|
||||
* atoms is on the allowlist. A blocked atom still wins, because we never
|
||||
* want to import a project where the redistributor could pick GPL.
|
||||
* SPDX OR grants the distributor a choice, so one allowed branch is enough.
|
||||
* SPDX AND requires every branch. Unknown branches remain review-required.
|
||||
*/
|
||||
export function classifyLicense(
|
||||
license,
|
||||
@@ -195,18 +199,70 @@ export function classifyLicense(
|
||||
} = {},
|
||||
) {
|
||||
if (license == null) return "unknown";
|
||||
if (allowed.has(license)) return "allowed";
|
||||
for (const pat of blockedPatterns) {
|
||||
if (pat.test(license)) return "blocked";
|
||||
}
|
||||
const atoms = splitSpdxExpression(license);
|
||||
if (atoms.length > 1) {
|
||||
for (const atom of atoms) {
|
||||
for (const pat of blockedPatterns) {
|
||||
if (pat.test(atom)) return "blocked";
|
||||
return classifySpdxExpression(license.trim(), allowed, blockedPatterns);
|
||||
}
|
||||
|
||||
function unwrapSpdxParentheses(expression) {
|
||||
let value = expression.trim();
|
||||
while (value.startsWith("(") && value.endsWith(")")) {
|
||||
let depth = 0;
|
||||
let wrapsWholeExpression = true;
|
||||
for (let index = 0; index < value.length; index += 1) {
|
||||
if (value[index] === "(") depth += 1;
|
||||
if (value[index] === ")") depth -= 1;
|
||||
if (depth === 0 && index < value.length - 1) {
|
||||
wrapsWholeExpression = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (atoms.some((atom) => allowed.has(atom))) return "allowed";
|
||||
if (!wrapsWholeExpression) break;
|
||||
value = value.slice(1, -1).trim();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function splitTopLevelSpdx(expression, operator) {
|
||||
const parts = [];
|
||||
let depth = 0;
|
||||
let start = 0;
|
||||
const token = ` ${operator} `;
|
||||
const upper = expression.toUpperCase();
|
||||
for (let index = 0; index < expression.length; index += 1) {
|
||||
if (expression[index] === "(") depth += 1;
|
||||
else if (expression[index] === ")") depth -= 1;
|
||||
else if (depth === 0 && upper.startsWith(token, index)) {
|
||||
parts.push(expression.slice(start, index).trim());
|
||||
index += token.length - 1;
|
||||
start = index + 1;
|
||||
}
|
||||
}
|
||||
if (parts.length === 0) return [expression.trim()];
|
||||
parts.push(expression.slice(start).trim());
|
||||
return parts.filter(Boolean);
|
||||
}
|
||||
|
||||
function classifySpdxExpression(expression, allowed, blockedPatterns) {
|
||||
const value = unwrapSpdxParentheses(expression);
|
||||
if (allowed.has(expression) || allowed.has(value)) return "allowed";
|
||||
|
||||
const alternatives = splitTopLevelSpdx(value, "OR");
|
||||
if (alternatives.length > 1) {
|
||||
const verdicts = alternatives.map((branch) => classifySpdxExpression(branch, allowed, blockedPatterns));
|
||||
if (verdicts.includes("allowed")) return "allowed";
|
||||
if (verdicts.every((verdict) => verdict === "blocked")) return "blocked";
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
const requirements = splitTopLevelSpdx(value, "AND");
|
||||
if (requirements.length > 1) {
|
||||
const verdicts = requirements.map((branch) => classifySpdxExpression(branch, allowed, blockedPatterns));
|
||||
if (verdicts.includes("blocked")) return "blocked";
|
||||
if (verdicts.every((verdict) => verdict === "allowed")) return "allowed";
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
for (const pattern of blockedPatterns) {
|
||||
if (pattern.test(value)) return "blocked";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
@@ -227,6 +283,51 @@ export function isReviewed(baseline, name, version) {
|
||||
return Object.prototype.hasOwnProperty.call(baseline, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flatten pnpm's license-grouped JSON into unique package/version records.
|
||||
* A dependency can occur through many workspace paths; compliance needs one
|
||||
* verdict per shipped version, not one verdict per path.
|
||||
*/
|
||||
export function parsePnpmLicenseReport(report) {
|
||||
if (!report || typeof report !== "object" || Array.isArray(report)) {
|
||||
throw new Error("pnpm license report must be an object grouped by license");
|
||||
}
|
||||
|
||||
const packages = new Map();
|
||||
for (const [groupLicense, entries] of Object.entries(report)) {
|
||||
if (!Array.isArray(entries)) continue;
|
||||
for (const entry of entries) {
|
||||
if (!entry || typeof entry !== "object" || typeof entry.name !== "string") continue;
|
||||
const versions = Array.isArray(entry.versions) ? entry.versions : [];
|
||||
const license = typeof entry.license === "string" && entry.license.trim() ? entry.license.trim() : groupLicense;
|
||||
for (const version of versions) {
|
||||
if (typeof version !== "string" || version.length === 0) continue;
|
||||
const key = `${entry.name}@${version}`;
|
||||
packages.set(key, { name: entry.name, version, license });
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...packages.values()].sort((left, right) => `${left.name}@${left.version}`.localeCompare(`${right.name}@${right.version}`));
|
||||
}
|
||||
|
||||
export function collectInstalledProductionPackages(repoRoot = REPO_ROOT) {
|
||||
const result = spawnSync("pnpm", ["licenses", "list", "--prod", "--json"], {
|
||||
cwd: repoRoot,
|
||||
encoding: "utf8",
|
||||
maxBuffer: 50 * 1024 * 1024,
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
const detail = result.stderr?.trim() || result.stdout?.trim() || `exit ${result.status ?? "unknown"}`;
|
||||
throw new Error(`pnpm production license inventory failed: ${detail}`);
|
||||
}
|
||||
try {
|
||||
return parsePnpmLicenseReport(JSON.parse(result.stdout));
|
||||
} catch (error) {
|
||||
const detail = error instanceof Error ? error.message : String(error);
|
||||
throw new Error(`pnpm production license inventory was invalid: ${detail}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the audit. Returns a result object — does not exit or print.
|
||||
* The CLI shell at the bottom of this file handles I/O.
|
||||
@@ -236,34 +337,16 @@ export function auditLicenses({
|
||||
baseline = loadBaseline(),
|
||||
allowed = ALLOWED_LICENSES,
|
||||
blockedPatterns = BLOCKED_LICENSE_PATTERNS,
|
||||
installedPackages,
|
||||
} = {}) {
|
||||
const manifests = discoverWorkspaceManifests(repoRoot);
|
||||
const deps = collectProductionDeps(manifests);
|
||||
const packages = installedPackages ?? collectInstalledProductionPackages(repoRoot);
|
||||
|
||||
const counts = { scanned: 0, allowed: 0, reviewed: 0, unknown: 0, blocked: 0 };
|
||||
const blocked = [];
|
||||
const unknown = [];
|
||||
const missing = [];
|
||||
|
||||
for (const name of deps) {
|
||||
for (const { name, version, license } of packages) {
|
||||
counts.scanned += 1;
|
||||
const manifestPath = resolveInstalledManifest(name, repoRoot);
|
||||
if (!manifestPath) {
|
||||
missing.push(name);
|
||||
counts.unknown += 1;
|
||||
unknown.push({ name, version: "(not installed)", license: null });
|
||||
continue;
|
||||
}
|
||||
let pkg;
|
||||
try {
|
||||
pkg = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
||||
} catch {
|
||||
counts.unknown += 1;
|
||||
unknown.push({ name, version: "(unreadable)", license: null });
|
||||
continue;
|
||||
}
|
||||
const version = typeof pkg.version === "string" ? pkg.version : "0.0.0";
|
||||
const license = parseLicenseField(pkg);
|
||||
if (isReviewed(baseline, name, version)) {
|
||||
counts.reviewed += 1;
|
||||
continue;
|
||||
@@ -279,7 +362,11 @@ export function auditLicenses({
|
||||
}
|
||||
}
|
||||
|
||||
return { counts, blocked, unknown, missing };
|
||||
return { counts, blocked, unknown, missing: [] };
|
||||
}
|
||||
|
||||
export function hasLicenseViolations(result) {
|
||||
return (result?.blocked?.length ?? 0) > 0 || (result?.unknown?.length ?? 0) > 0;
|
||||
}
|
||||
|
||||
function formatLine(prefix, entry) {
|
||||
@@ -287,15 +374,21 @@ function formatLine(prefix, entry) {
|
||||
}
|
||||
|
||||
function main() {
|
||||
const result = auditLicenses();
|
||||
let result;
|
||||
try {
|
||||
result = auditLicenses();
|
||||
} catch (error) {
|
||||
console.error(`FAIL ${error instanceof Error ? error.message : String(error)}`);
|
||||
process.exit(1);
|
||||
}
|
||||
const { counts, blocked, unknown } = result;
|
||||
for (const entry of blocked) {
|
||||
console.error(formatLine("FAIL", entry));
|
||||
}
|
||||
for (const entry of unknown) {
|
||||
console.warn(formatLine("WARN", entry));
|
||||
console.error(formatLine("FAIL", entry));
|
||||
}
|
||||
if (blocked.length > 0) {
|
||||
if (hasLicenseViolations(result)) {
|
||||
console.error(
|
||||
`FAIL scanned=${counts.scanned} allowed=${counts.allowed} reviewed=${counts.reviewed} unknown=${counts.unknown} blocked=${counts.blocked}`,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user