scripts/check-licenses.mjs (npm run check:licenses) reads the production dependency closures of the sidecar (@waggle/server minus the packages stage-sidecar-deps.mjs never ships) and the web UI (apps/web) from package-lock.json and fails on GPL, AGPL, SSPL, missing and unknown licenses. It allows permissive licenses plus MPL-2.0 and lists named exceptions with reasons; LGPL libvips and node-webpmux, the OFL fonts and the unlicensed buffers package are marked as pending the licensing review. app/src-tauri/deny.toml applies the same allowlist to the Tauri crates with cargo-deny; the npm check fails when the two lists drift apart. A new CI job runs both, with cargo-deny 0.18.3 installed and cached in CI. scripts/license-inventory.mjs holds the shared closure logic.
305 lines
12 KiB
JavaScript
305 lines
12 KiB
JavaScript
/**
|
|
* Inventory of the third-party code that ships in the community edition.
|
|
*
|
|
* Shared by scripts/generate-third-party-notices.mjs and scripts/check-licenses.mjs.
|
|
* Works offline: npm packages come from package-lock.json, Rust crates from
|
|
* `cargo metadata --offline`.
|
|
*
|
|
* What ships:
|
|
* - Sidecar: build-sidecar.mjs bundles @waggle/server into
|
|
* app/src-tauri/resources/service.js, and stage-sidecar-deps.mjs copies the
|
|
* packages it keeps external into resources/node_modules. Both follow the
|
|
* production dependencies of @waggle/server, so the sidecar is modelled as
|
|
* that closure minus the packages stage-sidecar-deps.mjs never ships (SKIP).
|
|
* - Web UI: Vite bundles apps/web and its production dependencies.
|
|
* - Desktop shell: the Rust crates of app/src-tauri, following normal
|
|
* dependencies (proc-macro crates included); build and dev dependencies do
|
|
* not end up in the binary and are left out.
|
|
*
|
|
* The npm closures follow `dependencies`, `optionalDependencies`, required
|
|
* `peerDependencies` and the vendored `file:` devDependencies of workspace
|
|
* packages, and keep platform packages for the v1 platforms only.
|
|
* They are a superset of what the bundlers keep: modules that tree-shaking
|
|
* drops are still listed.
|
|
*/
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { execFileSync } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
export const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
|
|
/** v1 platforms (docs/oss/adr/README.md): Windows x64, macOS arm64 and x64, Linux x64. */
|
|
export const NPM_TARGETS = [
|
|
{ os: 'win32', cpu: 'x64' },
|
|
{ os: 'darwin', cpu: 'arm64' },
|
|
{ os: 'darwin', cpu: 'x64' },
|
|
{ os: 'linux', cpu: 'x64' },
|
|
];
|
|
export const RUST_TARGETS = [
|
|
'x86_64-pc-windows-msvc',
|
|
'aarch64-apple-darwin',
|
|
'x86_64-apple-darwin',
|
|
'x86_64-unknown-linux-gnu',
|
|
];
|
|
|
|
const FIRST_PARTY_KEY = /^(?:apps|packages)\/[^/]+$/;
|
|
const LICENSE_FILE = /^(?:(?:mit|apache|bsd)[-_.])?(?:licen[cs]e|copying|notice|copyright|unlicense)(?:$|[-_.])/i;
|
|
|
|
/** Packages that stage-sidecar-deps.mjs never ships, read from its SKIP set. */
|
|
export function sidecarSkippedPackages(root = ROOT) {
|
|
const source = fs.readFileSync(path.join(root, 'scripts', 'stage-sidecar-deps.mjs'), 'utf8');
|
|
const match = /const SKIP = new Set\(\[([^\]]*)\]\)/.exec(source);
|
|
if (!match) throw new Error('Could not find the SKIP set in scripts/stage-sidecar-deps.mjs');
|
|
return new Set([...match[1].replace(/\/\/.*$/gm, '').matchAll(/'([^']+)'/g)].map((m) => m[1]));
|
|
}
|
|
|
|
/** The npm-based artifacts, each rooted at a workspace package. */
|
|
export function npmArtifacts(root = ROOT) {
|
|
return [
|
|
{
|
|
id: 'sidecar',
|
|
title: 'Sidecar',
|
|
workspace: 'packages/server',
|
|
ships: 'app/src-tauri/resources/service.js and resources/node_modules',
|
|
skip: sidecarSkippedPackages(root),
|
|
},
|
|
{
|
|
id: 'web',
|
|
title: 'Web UI',
|
|
workspace: 'apps/web',
|
|
ships: 'the apps/web bundle that the sidecar and the desktop shell serve',
|
|
skip: new Set(),
|
|
},
|
|
];
|
|
}
|
|
|
|
export function readLockfilePackages(root = ROOT) {
|
|
const lock = JSON.parse(fs.readFileSync(path.join(root, 'package-lock.json'), 'utf8'));
|
|
if (!lock.packages || lock.lockfileVersion < 2) {
|
|
throw new Error('package-lock.json must be lockfileVersion 2 or later');
|
|
}
|
|
return lock.packages;
|
|
}
|
|
|
|
function matchesPlatformList(list, value) {
|
|
if (!Array.isArray(list) || list.length === 0) return true;
|
|
if (list.includes(`!${value}`)) return false;
|
|
const positive = list.filter((entry) => !entry.startsWith('!'));
|
|
return positive.length === 0 || positive.includes(value);
|
|
}
|
|
|
|
function installsOn(entry, target) {
|
|
return matchesPlatformList(entry.os, target.os) && matchesPlatformList(entry.cpu, target.cpu);
|
|
}
|
|
|
|
/** True when an npm package installs on at least one v1 platform. */
|
|
export function installsOnSupportedPlatform(entry) {
|
|
return NPM_TARGETS.some((target) => installsOn(entry, target));
|
|
}
|
|
|
|
function nameFromKey(key) {
|
|
const index = key.lastIndexOf('node_modules/');
|
|
return index === -1 ? null : key.slice(index + 'node_modules/'.length);
|
|
}
|
|
|
|
/** Resolve `name` from the package at `fromKey` the way Node walks node_modules. */
|
|
function resolveKey(packages, fromKey, name) {
|
|
let dir = fromKey;
|
|
for (;;) {
|
|
const candidate = dir ? `${dir}/node_modules/${name}` : `node_modules/${name}`;
|
|
if (packages[candidate]) return candidate;
|
|
if (!dir) return null;
|
|
const parent = path.posix.dirname(dir);
|
|
dir = parent === '.' ? '' : parent;
|
|
}
|
|
}
|
|
|
|
function runtimeDependencies(entry, firstParty) {
|
|
const deps = new Map();
|
|
for (const name of Object.keys(entry.dependencies ?? {})) deps.set(name, false);
|
|
// Vendored packages (`file:` specs, e.g. vendor/pptxgenjs in @waggle/agent) are
|
|
// devDependencies so the npm package can stage its own copy, but the bundlers
|
|
// inline them from source.
|
|
if (firstParty) {
|
|
for (const [name, spec] of Object.entries(entry.devDependencies ?? {})) {
|
|
if (String(spec).startsWith('file:')) deps.set(name, false);
|
|
}
|
|
}
|
|
for (const name of Object.keys(entry.optionalDependencies ?? {})) deps.set(name, true);
|
|
const peerMeta = entry.peerDependenciesMeta ?? {};
|
|
for (const name of Object.keys(entry.peerDependencies ?? {})) {
|
|
if (!peerMeta[name]?.optional && !deps.has(name)) deps.set(name, false);
|
|
}
|
|
return deps;
|
|
}
|
|
|
|
/**
|
|
* Production dependency closure of one artifact.
|
|
* Returns third-party packages keyed by lockfile path, each with the shortest
|
|
* dependency path from the artifact's workspace package.
|
|
*/
|
|
export function npmClosure(packages, artifact) {
|
|
const rootEntry = packages[artifact.workspace];
|
|
if (!rootEntry) throw new Error(`${artifact.workspace} is not in package-lock.json`);
|
|
const rootName = rootEntry.name ?? artifact.workspace;
|
|
const result = new Map();
|
|
const unresolved = [];
|
|
const visited = new Set([artifact.workspace]);
|
|
const queue = [{ key: artifact.workspace, via: [rootName] }];
|
|
|
|
while (queue.length > 0) {
|
|
const { key, via } = queue.shift();
|
|
for (const [depName, optional] of runtimeDependencies(packages[key], FIRST_PARTY_KEY.test(key))) {
|
|
if (artifact.skip.has(depName)) continue;
|
|
const found = resolveKey(packages, key, depName);
|
|
const target = found && packages[found].link ? packages[found].resolved : found;
|
|
const entry = target ? packages[target] : undefined;
|
|
if (!entry) {
|
|
if (!optional) unresolved.push(`${via.join(' > ')} > ${depName}`);
|
|
continue;
|
|
}
|
|
if (visited.has(target) || !installsOnSupportedPlatform(entry)) continue;
|
|
visited.add(target);
|
|
const name = entry.name ?? nameFromKey(target) ?? depName;
|
|
const chain = [...via, name];
|
|
if (!FIRST_PARTY_KEY.test(target)) {
|
|
result.set(target, {
|
|
key: target,
|
|
name,
|
|
version: entry.version ?? '',
|
|
license: typeof entry.license === 'string' ? entry.license.trim() : null,
|
|
resolved: entry.resolved ?? null,
|
|
platformSpecific: !NPM_TARGETS.every((platform) => installsOn(entry, platform)),
|
|
os: entry.os ?? null,
|
|
cpu: entry.cpu ?? null,
|
|
via: chain,
|
|
});
|
|
}
|
|
queue.push({ key: target, via: chain });
|
|
}
|
|
}
|
|
return { packages: result, unresolved };
|
|
}
|
|
|
|
/** npm installs several copies of one version at different paths; keep each version once, sorted. */
|
|
export function uniqueVersions(packages) {
|
|
const seen = new Map();
|
|
for (const pkg of packages) {
|
|
const id = `${pkg.name}@${pkg.version}`;
|
|
if (!seen.has(id) || pkg.via.length < seen.get(id).via.length) seen.set(id, pkg);
|
|
}
|
|
return [...seen.values()].sort(
|
|
(a, b) => a.name.localeCompare(b.name) || a.version.localeCompare(b.version, undefined, { numeric: true }),
|
|
);
|
|
}
|
|
|
|
/** The package.json of an installed package, or null when it is not installed here. */
|
|
export function readInstalledManifest(root, key) {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(path.join(root, key, 'package.json'), 'utf8'));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** License declared by an installed manifest, including the legacy object and array forms. */
|
|
export function manifestLicense(manifest) {
|
|
if (!manifest) return null;
|
|
const { license, licenses } = manifest;
|
|
if (typeof license === 'string' && license.trim()) return license.trim();
|
|
if (license && typeof license.type === 'string') return license.type;
|
|
if (Array.isArray(licenses) && licenses.length > 0) {
|
|
const types = licenses.map((entry) => (typeof entry === 'string' ? entry : entry?.type)).filter(Boolean);
|
|
if (types.length === 1) return types[0];
|
|
if (types.length > 1) return `(${types.join(' OR ')})`;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** License and notice files at the top of a package or crate directory. */
|
|
export function readLicenseFiles(dir, extraFiles = []) {
|
|
if (!dir || !fs.existsSync(dir)) return [];
|
|
const names = fs.readdirSync(dir, { withFileTypes: true })
|
|
.filter((entry) => entry.isFile() && LICENSE_FILE.test(entry.name))
|
|
.map((entry) => entry.name);
|
|
for (const extra of extraFiles) {
|
|
const relative = path.relative(dir, path.resolve(dir, extra));
|
|
if (!names.includes(relative) && fs.existsSync(path.join(dir, relative))) names.push(relative);
|
|
}
|
|
return names.sort().map((file) => ({
|
|
file,
|
|
text: fs.readFileSync(path.join(dir, file), 'utf8').replace(/\r\n?/g, '\n').trim(),
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Rust crates linked into the desktop shell, per supported target.
|
|
* Targets whose crates are not in the local cargo cache cannot be resolved
|
|
* offline; they are returned in `unresolvedTargets` instead of failing.
|
|
*/
|
|
export function rustCrates(root = ROOT) {
|
|
const manifestPath = path.join(root, 'app', 'src-tauri', 'Cargo.toml');
|
|
const crates = new Map();
|
|
const unresolvedTargets = [];
|
|
for (const target of RUST_TARGETS) {
|
|
let metadata;
|
|
try {
|
|
metadata = JSON.parse(execFileSync(
|
|
'cargo',
|
|
[
|
|
'metadata', '--format-version', '1', '--offline', '--locked',
|
|
'--filter-platform', target, '--manifest-path', manifestPath,
|
|
],
|
|
{ encoding: 'utf8', maxBuffer: 512 * 1024 * 1024, stdio: ['ignore', 'pipe', 'pipe'] },
|
|
));
|
|
} catch (error) {
|
|
const detail = String(error.stderr || error.message).trim().split('\n')[0];
|
|
unresolvedTargets.push({ target, reason: detail });
|
|
continue;
|
|
}
|
|
const byId = new Map(metadata.packages.map((pkg) => [pkg.id, pkg]));
|
|
const nodes = new Map(metadata.resolve.nodes.map((node) => [node.id, node]));
|
|
const rootId = metadata.resolve.root;
|
|
const seen = new Set([rootId]);
|
|
const stack = [rootId];
|
|
while (stack.length > 0) {
|
|
for (const dep of nodes.get(stack.pop()).deps) {
|
|
// Build and dev dependencies do not end up in the shipped binary.
|
|
if (!dep.dep_kinds.some((kind) => kind.kind === null)) continue;
|
|
if (seen.has(dep.pkg)) continue;
|
|
seen.add(dep.pkg);
|
|
stack.push(dep.pkg);
|
|
}
|
|
}
|
|
seen.delete(rootId);
|
|
for (const id of seen) {
|
|
const pkg = byId.get(id);
|
|
const key = `${pkg.name}@${pkg.version}`;
|
|
if (!crates.has(key)) {
|
|
crates.set(key, {
|
|
key,
|
|
name: pkg.name,
|
|
version: pkg.version,
|
|
license: pkg.license ?? null,
|
|
licenseFile: pkg.license_file ?? null,
|
|
repository: pkg.repository ?? null,
|
|
source: pkg.source ?? null,
|
|
dir: path.dirname(pkg.manifest_path),
|
|
targets: new Set(),
|
|
});
|
|
}
|
|
crates.get(key).targets.add(target);
|
|
}
|
|
}
|
|
return { crates, unresolvedTargets };
|
|
}
|
|
|
|
/** The Node.js version that scripts/bundle-node.mjs ships with the desktop sidecar. */
|
|
export function bundledNodeVersion(root = ROOT) {
|
|
const source = fs.readFileSync(path.join(root, 'scripts', 'bundle-node.mjs'), 'utf8');
|
|
const match = /const DESKTOP_NODE_VERSION = '([^']+)'/.exec(source);
|
|
if (!match) throw new Error('Could not find DESKTOP_NODE_VERSION in scripts/bundle-node.mjs');
|
|
return match[1];
|
|
}
|