Merge P2.3: license gate and third-party notices
# Conflicts: # docs/oss/upstream-fixes.md
This commit is contained in:
287
scripts/check-licenses.mjs
Normal file
287
scripts/check-licenses.mjs
Normal file
@@ -0,0 +1,287 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* License gate for the npm packages that ship in the community edition.
|
||||
*
|
||||
* Reads the production dependency closures of the sidecar and the web UI from
|
||||
* package-lock.json (see scripts/license-inventory.mjs), so it needs no
|
||||
* `npm install`. Every package must declare a license expression that the
|
||||
* allowlist satisfies, or match a named exception below. GPL, AGPL, SSPL,
|
||||
* missing and unknown licenses fail.
|
||||
*
|
||||
* The Rust crates of the desktop shell are checked by cargo-deny with the same
|
||||
* allowlist (app/src-tauri/deny.toml); this script fails when the two lists
|
||||
* drift apart. Node.js and the npm CLI that bundle-node.mjs downloads are not
|
||||
* in package-lock.json and are not checked here (THIRD_PARTY_NOTICES.md lists them).
|
||||
*
|
||||
* Usage: node scripts/check-licenses.mjs [--verbose]
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { ROOT, npmArtifacts, npmClosure, readLockfilePackages, uniqueVersions } from './license-inventory.mjs';
|
||||
|
||||
/** SPDX identifiers that may ship in an MIT-licensed distribution. Keep in sync with app/src-tauri/deny.toml. */
|
||||
export const ALLOWED_LICENSES = [
|
||||
'0BSD',
|
||||
'Apache-2.0',
|
||||
'BSD-2-Clause',
|
||||
'BSD-3-Clause',
|
||||
'BlueOak-1.0.0',
|
||||
'CC0-1.0',
|
||||
'ISC',
|
||||
'MIT',
|
||||
'MPL-2.0',
|
||||
'Python-2.0',
|
||||
'Unicode-3.0',
|
||||
'Unlicense',
|
||||
'Zlib',
|
||||
];
|
||||
|
||||
/**
|
||||
* Named exceptions. An exception applies only while the package still declares
|
||||
* exactly `license` in package-lock.json (null: no license declared), so a
|
||||
* license change needs a new review. `pending` marks exceptions that wait for
|
||||
* the licensing review (pending decision Q6); the gate prints them on every run.
|
||||
*/
|
||||
export const EXCEPTIONS = [
|
||||
{
|
||||
packages: ['@img/sharp-libvips-*'],
|
||||
license: 'LGPL-3.0-or-later',
|
||||
pending: true,
|
||||
reason: 'Prebuilt libvips for sharp, required by @huggingface/transformers (embeddings). '
|
||||
+ 'Shipped unmodified as a separate shared library in resources/node_modules and loaded '
|
||||
+ 'at runtime; LGPL obligations are listed in THIRD_PARTY_NOTICES.md.',
|
||||
},
|
||||
{
|
||||
packages: ['@img/sharp-win32-x64'],
|
||||
license: 'Apache-2.0 AND LGPL-3.0-or-later',
|
||||
pending: true,
|
||||
reason: 'sharp binding for Windows; the package carries the libvips DLLs (LGPL-3.0-or-later). '
|
||||
+ 'Shipped unmodified in resources/node_modules, same terms as @img/sharp-libvips-*.',
|
||||
},
|
||||
{
|
||||
packages: ['node-webpmux'],
|
||||
license: 'LGPL-3.0-or-later',
|
||||
pending: true,
|
||||
reason: 'Required dependency of whatsapp-web.js (ADR-0005). Kept external to service.js and '
|
||||
+ 'shipped unmodified as a separate package in resources/node_modules.',
|
||||
},
|
||||
{
|
||||
packages: ['@fontsource-variable/hanken-grotesk', '@fontsource-variable/jetbrains-mono'],
|
||||
license: 'OFL-1.1',
|
||||
pending: true,
|
||||
reason: 'Fonts bundled into the web UI. OFL-1.1 allows bundling with software if the '
|
||||
+ 'license ships with the fonts and the fonts are not sold on their own.',
|
||||
},
|
||||
{
|
||||
packages: ['buffers'],
|
||||
license: null,
|
||||
pending: true,
|
||||
reason: 'buffers 0.1.1 (exceljs > unzipper > binary > buffers) has no license field, file or '
|
||||
+ 'README statement. Unlicensed code; needs a decision or a dependency change.',
|
||||
},
|
||||
{
|
||||
packages: ['posthog-js'],
|
||||
license: 'SEE LICENSE IN LICENSE',
|
||||
reason: 'The LICENSE file is Apache-2.0, with MIT for files taken from Sentry, Metro and Expo.',
|
||||
},
|
||||
{
|
||||
packages: ['duck'],
|
||||
license: 'BSD',
|
||||
reason: 'The LICENSE file is the BSD-2-Clause text.',
|
||||
},
|
||||
{
|
||||
packages: ['chainsaw', 'traverse'],
|
||||
license: 'MIT/X11',
|
||||
reason: '"MIT/X11" is the MIT license (traverse ships the MIT text; chainsaw has no license file).',
|
||||
},
|
||||
{
|
||||
packages: ['png-js'],
|
||||
license: null,
|
||||
reason: 'No license field; the LICENSE file is the MIT text.',
|
||||
},
|
||||
{
|
||||
packages: ['async'],
|
||||
license: null,
|
||||
reason: 'async 0.2.10 (whatsapp-web.js > fluent-ffmpeg > async) declares MIT in the legacy '
|
||||
+ '"licenses" field and ships the MIT text.',
|
||||
},
|
||||
];
|
||||
|
||||
const COPYLEFT = /^(?:A?GPL|LGPL|SSPL|EUPL|OSL|CC-BY-SA|CPAL)/i;
|
||||
const LICENSE_ID = /^(?:[A-Za-z0-9][A-Za-z0-9.+-]*|LicenseRef-[A-Za-z0-9.-]+)$/;
|
||||
|
||||
/** Parse an SPDX license expression. Throws on anything that is not one. */
|
||||
export function parseLicenseExpression(expression) {
|
||||
const tokens = expression.replace(/[()]/g, ' $& ').trim().split(/\s+/).filter(Boolean);
|
||||
let position = 0;
|
||||
const peek = () => tokens[position];
|
||||
const isOperator = (token, operator) => token !== undefined && token.toUpperCase() === operator;
|
||||
|
||||
function parseAtom() {
|
||||
const token = tokens[position++];
|
||||
if (token === '(') {
|
||||
const inner = parseOr();
|
||||
if (tokens[position++] !== ')') throw new Error(`missing ")" in "${expression}"`);
|
||||
return inner;
|
||||
}
|
||||
if (token === undefined || !LICENSE_ID.test(token) || ['AND', 'OR', 'WITH'].includes(token.toUpperCase())) {
|
||||
throw new Error(`"${expression}" is not an SPDX expression`);
|
||||
}
|
||||
if (isOperator(peek(), 'WITH')) {
|
||||
position++;
|
||||
const exception = tokens[position++];
|
||||
if (!exception || !LICENSE_ID.test(exception)) throw new Error(`bad WITH clause in "${expression}"`);
|
||||
return { license: token, exception };
|
||||
}
|
||||
return { license: token };
|
||||
}
|
||||
function parseAnd() {
|
||||
let left = parseAtom();
|
||||
while (isOperator(peek(), 'AND')) {
|
||||
position++;
|
||||
left = { and: [left, parseAtom()] };
|
||||
}
|
||||
return left;
|
||||
}
|
||||
function parseOr() {
|
||||
let left = parseAnd();
|
||||
while (isOperator(peek(), 'OR')) {
|
||||
position++;
|
||||
left = { or: [left, parseAnd()] };
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
const tree = parseOr();
|
||||
if (position !== tokens.length) throw new Error(`"${expression}" is not an SPDX expression`);
|
||||
return tree;
|
||||
}
|
||||
|
||||
function satisfied(node, allowed) {
|
||||
if (node.or) return node.or.some((child) => satisfied(child, allowed));
|
||||
if (node.and) return node.and.every((child) => satisfied(child, allowed));
|
||||
// An exception only adds permissions, so an allowed license stays allowed with it.
|
||||
return allowed.has(node.license) || allowed.has(`${node.license} WITH ${node.exception}`);
|
||||
}
|
||||
|
||||
function leaves(node) {
|
||||
if (node.or) return node.or.flatMap(leaves);
|
||||
if (node.and) return node.and.flatMap(leaves);
|
||||
return [node.license];
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify one declared license.
|
||||
* Returns { status: 'allowed' | 'missing' | 'invalid' | 'denied', copyleft, alternatives }.
|
||||
*/
|
||||
export function evaluateLicense(expression, allowed = new Set(ALLOWED_LICENSES)) {
|
||||
if (expression === null || expression === undefined || expression.trim() === '') {
|
||||
return { status: 'missing', copyleft: false, alternatives: [] };
|
||||
}
|
||||
let tree;
|
||||
try {
|
||||
tree = parseLicenseExpression(expression);
|
||||
} catch {
|
||||
return { status: 'invalid', copyleft: false, alternatives: [] };
|
||||
}
|
||||
const ids = leaves(tree);
|
||||
const copyleft = ids.some((id) => COPYLEFT.test(id));
|
||||
if (satisfied(tree, allowed)) {
|
||||
// Dual-licensed packages pass on their permissive option; report the rest.
|
||||
return { status: 'allowed', copyleft, alternatives: ids.filter((id) => !allowed.has(id)) };
|
||||
}
|
||||
return { status: 'denied', copyleft, alternatives: [] };
|
||||
}
|
||||
|
||||
function matchesPattern(name, pattern) {
|
||||
return pattern.endsWith('*') ? name.startsWith(pattern.slice(0, -1)) : name === pattern;
|
||||
}
|
||||
|
||||
export function findException(pkg, exceptions = EXCEPTIONS) {
|
||||
return exceptions.find(
|
||||
(exception) => exception.license === pkg.license
|
||||
&& exception.packages.some((pattern) => matchesPattern(pkg.name, pattern)),
|
||||
);
|
||||
}
|
||||
|
||||
/** The `allow` list of the [licenses] table in a cargo-deny config. */
|
||||
export function readDenyAllowList(denyToml) {
|
||||
const section = /^\[licenses\]\s*$([\s\S]*?)(?=^\[)/m.exec(`${denyToml}\n[`);
|
||||
const allow = section && /^allow\s*=\s*\[([\s\S]*?)\]/m.exec(section[1]);
|
||||
if (!allow) throw new Error('app/src-tauri/deny.toml has no [licenses] allow list');
|
||||
return [...allow[1].replace(/#.*$/gm, '').matchAll(/"([^"]+)"/g)].map((match) => match[1]);
|
||||
}
|
||||
|
||||
function main() {
|
||||
const verbose = process.argv.includes('--verbose');
|
||||
const packages = readLockfilePackages();
|
||||
const failures = [];
|
||||
const usedExceptions = new Map();
|
||||
const dualLicensed = new Map();
|
||||
|
||||
for (const artifact of npmArtifacts()) {
|
||||
const { packages: closure, unresolved } = npmClosure(packages, artifact);
|
||||
for (const missing of unresolved) {
|
||||
failures.push(`${artifact.title}: required dependency is not in package-lock.json: ${missing}`);
|
||||
}
|
||||
const shipped = uniqueVersions(closure.values());
|
||||
for (const pkg of shipped) {
|
||||
const result = evaluateLicense(pkg.license);
|
||||
const id = `${pkg.name}@${pkg.version}`;
|
||||
if (result.status === 'allowed') {
|
||||
if (result.alternatives.length > 0) dualLicensed.set(id, pkg.license);
|
||||
continue;
|
||||
}
|
||||
const exception = findException(pkg);
|
||||
if (exception) {
|
||||
if (!usedExceptions.has(exception)) usedExceptions.set(exception, new Set());
|
||||
usedExceptions.get(exception).add(`${id} (${artifact.title})`);
|
||||
continue;
|
||||
}
|
||||
const label = {
|
||||
missing: 'no license declared',
|
||||
invalid: `not an SPDX expression: "${pkg.license}"`,
|
||||
denied: `${pkg.license}${result.copyleft ? ' (copyleft)' : ''} is not on the allowlist`,
|
||||
}[result.status];
|
||||
failures.push(`${artifact.title}: ${id}: ${label}\n path: ${pkg.via.join(' > ')}`);
|
||||
}
|
||||
if (verbose) console.log(`[check-licenses] ${artifact.title}: ${shipped.length} third-party packages`);
|
||||
}
|
||||
|
||||
const denyPath = path.join(ROOT, 'app', 'src-tauri', 'deny.toml');
|
||||
const denyAllow = readDenyAllowList(fs.readFileSync(denyPath, 'utf8'));
|
||||
const missingInDeny = ALLOWED_LICENSES.filter((id) => !denyAllow.includes(id));
|
||||
const extraInDeny = denyAllow.filter((id) => !ALLOWED_LICENSES.includes(id));
|
||||
if (missingInDeny.length > 0 || extraInDeny.length > 0) {
|
||||
failures.push(
|
||||
'app/src-tauri/deny.toml allow list differs from ALLOWED_LICENSES'
|
||||
+ `${missingInDeny.length ? `; missing: ${missingInDeny.join(', ')}` : ''}`
|
||||
+ `${extraInDeny.length ? `; extra: ${extraInDeny.join(', ')}` : ''}`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const [exception, hits] of usedExceptions) {
|
||||
if (!exception.pending && !verbose) continue;
|
||||
const tag = exception.pending ? 'pending licensing review' : 'exception';
|
||||
console.warn(`[check-licenses] ${tag}: ${exception.license ?? 'no license'}: ${[...hits].sort().join(', ')}`);
|
||||
console.warn(` ${exception.reason}`);
|
||||
}
|
||||
for (const exception of EXCEPTIONS.filter((entry) => !usedExceptions.has(entry))) {
|
||||
console.warn(`[check-licenses] unused exception for ${exception.packages.join(', ')}; remove it`);
|
||||
}
|
||||
if (verbose) {
|
||||
for (const [id, license] of [...dualLicensed].sort()) {
|
||||
console.log(`[check-licenses] dual-licensed, used under the allowed option: ${id} (${license})`);
|
||||
}
|
||||
}
|
||||
|
||||
if (failures.length > 0) {
|
||||
console.error(`[check-licenses] FAILED: ${failures.length} problem(s)`);
|
||||
for (const failure of failures) console.error(` - ${failure}`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log('[check-licenses] OK: every shipped npm package is allowed or a named exception');
|
||||
}
|
||||
|
||||
if (import.meta.url === pathToFileURL(process.argv[1] ?? '').href) main();
|
||||
58
scripts/check-licenses.test.ts
Normal file
58
scripts/check-licenses.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
ALLOWED_LICENSES,
|
||||
evaluateLicense,
|
||||
findException,
|
||||
readDenyAllowList,
|
||||
} from './check-licenses.mjs';
|
||||
|
||||
describe('evaluateLicense', () => {
|
||||
it('allows listed licenses and expressions the allowlist satisfies', () => {
|
||||
expect(evaluateLicense('MIT').status).toBe('allowed');
|
||||
expect(evaluateLicense('(MIT AND Zlib)').status).toBe('allowed');
|
||||
expect(evaluateLicense('(MIT OR Apache-2.0) AND Unicode-3.0').status).toBe('allowed');
|
||||
expect(evaluateLicense('Apache-2.0 WITH LLVM-exception').status).toBe('allowed');
|
||||
});
|
||||
|
||||
it('passes dual-licensed packages on their permissive option and reports the other one', () => {
|
||||
const result = evaluateLicense('(MIT OR GPL-3.0-or-later)');
|
||||
expect(result.status).toBe('allowed');
|
||||
expect(result.alternatives).toEqual(['GPL-3.0-or-later']);
|
||||
});
|
||||
|
||||
it('denies copyleft licenses, alone or in a conjunction', () => {
|
||||
for (const expression of ['GPL-3.0-only', 'AGPL-3.0-or-later', 'SSPL-1.0', 'MIT AND LGPL-3.0-or-later']) {
|
||||
const result = evaluateLicense(expression);
|
||||
expect(result.status, expression).toBe('denied');
|
||||
expect(result.copyleft, expression).toBe(true);
|
||||
}
|
||||
expect(evaluateLicense('GPL-2.0-only WITH Classpath-exception-2.0').status).toBe('denied');
|
||||
});
|
||||
|
||||
it('flags missing and non-SPDX licenses', () => {
|
||||
expect(evaluateLicense(null).status).toBe('missing');
|
||||
expect(evaluateLicense(' ').status).toBe('missing');
|
||||
for (const expression of ['SEE LICENSE IN LICENSE', 'MIT/X11', 'Apache 2.0', '(MIT', 'MIT OR']) {
|
||||
expect(evaluateLicense(expression).status, expression).toBe('invalid');
|
||||
}
|
||||
expect(evaluateLicense('BSD').status).toBe('denied');
|
||||
});
|
||||
});
|
||||
|
||||
describe('findException', () => {
|
||||
it('matches by name pattern and the exact declared license', () => {
|
||||
expect(findException({ name: '@img/sharp-libvips-linux-x64', license: 'LGPL-3.0-or-later' })).toBeDefined();
|
||||
expect(findException({ name: '@img/sharp-libvips-linux-x64', license: 'GPL-3.0-only' })).toBeUndefined();
|
||||
expect(findException({ name: 'async', license: null })).toBeDefined();
|
||||
expect(findException({ name: 'async', license: 'GPL-3.0-only' })).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('deny.toml', () => {
|
||||
it('uses the same allowlist as the npm check', () => {
|
||||
const denyToml = fs.readFileSync(path.join(__dirname, '..', 'app', 'src-tauri', 'deny.toml'), 'utf8');
|
||||
expect([...readDenyAllowList(denyToml)].sort()).toEqual([...ALLOWED_LICENSES].sort());
|
||||
});
|
||||
});
|
||||
435
scripts/generate-third-party-notices.mjs
Normal file
435
scripts/generate-third-party-notices.mjs
Normal file
@@ -0,0 +1,435 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Write THIRD_PARTY_NOTICES.md: the third-party components that ship in the
|
||||
* community edition, their licenses and the license texts they carry.
|
||||
*
|
||||
* Offline. npm packages come from package-lock.json (closures in
|
||||
* scripts/license-inventory.mjs) and their texts from the installed packages;
|
||||
* Rust crates come from `cargo metadata --offline` and their texts from the
|
||||
* cargo registry cache. Targets whose crates are not cached are reported in the
|
||||
* file and on stderr. Platform packages (npm installs them on some of the v1
|
||||
* platforms only) are listed without their texts, because a machine has only
|
||||
* its own platform's variants installed; that keeps the output the same on
|
||||
* every platform.
|
||||
*
|
||||
* Usage: node scripts/generate-third-party-notices.mjs
|
||||
*/
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
ROOT,
|
||||
RUST_TARGETS,
|
||||
bundledNodeVersion,
|
||||
manifestLicense,
|
||||
npmArtifacts,
|
||||
npmClosure,
|
||||
readInstalledManifest,
|
||||
readLicenseFiles,
|
||||
readLockfilePackages,
|
||||
rustCrates,
|
||||
uniqueVersions,
|
||||
} from './license-inventory.mjs';
|
||||
import { ALLOWED_LICENSES, EXCEPTIONS, evaluateLicense, findException } from './check-licenses.mjs';
|
||||
|
||||
const OUTPUT = path.join(ROOT, 'THIRD_PARTY_NOTICES.md');
|
||||
const TARGET_LABELS = {
|
||||
'x86_64-pc-windows-msvc': 'Windows x64',
|
||||
'aarch64-apple-darwin': 'macOS arm64',
|
||||
'x86_64-apple-darwin': 'macOS x64',
|
||||
'x86_64-unknown-linux-gnu': 'Linux x64',
|
||||
};
|
||||
// Licenses that impose no conditions beyond keeping the notice. MPL-2.0 is on
|
||||
// the allowlist but is file-level copyleft, so it is reported with LGPL.
|
||||
const PERMISSIVE = new Set(ALLOWED_LICENSES.filter((id) => id !== 'MPL-2.0'));
|
||||
const COPYLEFT_LEAF = /^(?:A?GPL|LGPL|MPL|EPL|EUPL|CDDL|OFL)/;
|
||||
|
||||
// ── License texts, deduplicated by content ─────────────────────────────────
|
||||
const texts = [];
|
||||
const textIndex = new Map();
|
||||
|
||||
function registerTexts(files, owner) {
|
||||
return files.map(({ file, text }) => {
|
||||
const key = text.replace(/\s+/g, ' ');
|
||||
if (!textIndex.has(key)) {
|
||||
texts.push({ number: texts.length + 1, text, source: `${file} of ${owner}`, users: [] });
|
||||
textIndex.set(key, texts.length - 1);
|
||||
}
|
||||
const entry = texts[textIndex.get(key)];
|
||||
entry.users.push(owner);
|
||||
return entry.number;
|
||||
});
|
||||
}
|
||||
|
||||
function textLinks(numbers) {
|
||||
return numbers.length > 0 ? numbers.map((n) => `[${n}](#license-text-${n})`).join(', ') : '';
|
||||
}
|
||||
|
||||
function fence(text) {
|
||||
const longest = Math.max(0, ...[...text.matchAll(/`+/g)].map((match) => match[0].length));
|
||||
return '`'.repeat(Math.max(3, longest + 1));
|
||||
}
|
||||
|
||||
function cell(value) {
|
||||
return String(value ?? '').replace(/\|/g, '\\|').replace(/\n/g, ' ');
|
||||
}
|
||||
|
||||
function byNameAndVersion(a, b) {
|
||||
return a.name.localeCompare(b.name) || a.version.localeCompare(b.version, undefined, { numeric: true });
|
||||
}
|
||||
|
||||
function findText(pattern) {
|
||||
return texts.find((entry) => pattern.test(entry.text.slice(0, 400)));
|
||||
}
|
||||
|
||||
// ── Classification for the copyleft section ────────────────────────────────
|
||||
function leafIds(expression) {
|
||||
return expression.replace(/[()]/g, ' ').split(/\s+/).filter((token) => token && !/^(?:AND|OR|WITH)$/i.test(token));
|
||||
}
|
||||
|
||||
/** Cargo still accepts "MIT/Apache-2.0"; read the slash as OR. */
|
||||
function normalizeCargoLicense(license) {
|
||||
return license ? license.replace(/\s*\/\s*/g, ' OR ') : license;
|
||||
}
|
||||
|
||||
function classify(component) {
|
||||
const declared = component.license;
|
||||
const result = evaluateLicense(declared, PERMISSIVE);
|
||||
if (result.status === 'allowed') {
|
||||
const copyleftOptions = leafIds(declared).filter((id) => COPYLEFT_LEAF.test(id));
|
||||
return copyleftOptions.length > 0 ? 'dual' : null;
|
||||
}
|
||||
if (result.status === 'denied') {
|
||||
const ids = leafIds(declared);
|
||||
if (ids.some((id) => /^LGPL/.test(id))) return 'lgpl';
|
||||
if (ids.some((id) => /^MPL/.test(id))) return 'mpl';
|
||||
if (ids.some((id) => /^OFL/.test(id))) return 'ofl';
|
||||
}
|
||||
const exception = component.ecosystem === 'npm'
|
||||
? findException({ name: component.name, license: component.lockLicense })
|
||||
: undefined;
|
||||
if (exception && !exception.pending) return null;
|
||||
if (!declared) return 'none';
|
||||
return 'other';
|
||||
}
|
||||
|
||||
function permissiveChoice(expression) {
|
||||
const options = expression.replace(/^\((.*)\)$/, '$1').split(/\s+OR\s+/i);
|
||||
return options.find((option) => evaluateLicense(option, PERMISSIVE).status === 'allowed') ?? '';
|
||||
}
|
||||
|
||||
// ── Inventory ──────────────────────────────────────────────────────────────
|
||||
const lockPackages = readLockfilePackages();
|
||||
const artifacts = npmArtifacts().map((artifact) => {
|
||||
const { packages, unresolved } = npmClosure(lockPackages, artifact);
|
||||
for (const missing of unresolved) console.warn(`[notices] not in package-lock.json: ${missing}`);
|
||||
const rows = uniqueVersions(packages.values()).map((pkg) => {
|
||||
const manifest = pkg.platformSpecific ? null : readInstalledManifest(ROOT, pkg.key);
|
||||
const license = pkg.license ?? manifestLicense(manifest);
|
||||
const files = pkg.platformSpecific ? [] : readLicenseFiles(path.join(ROOT, pkg.key));
|
||||
if (!pkg.platformSpecific && !manifest) {
|
||||
console.warn(`[notices] ${pkg.name}@${pkg.version} is not installed; run npm ci first`);
|
||||
}
|
||||
return {
|
||||
...pkg,
|
||||
ecosystem: 'npm',
|
||||
artifact: artifact.title,
|
||||
license,
|
||||
lockLicense: pkg.license,
|
||||
textNumbers: registerTexts(files, `${pkg.name} ${pkg.version}`),
|
||||
installed: Boolean(manifest),
|
||||
};
|
||||
});
|
||||
return { ...artifact, rows };
|
||||
});
|
||||
|
||||
const { crates, unresolvedTargets } = rustCrates();
|
||||
const resolvedTargets = RUST_TARGETS.filter((target) => !unresolvedTargets.some((entry) => entry.target === target));
|
||||
for (const { target, reason } of unresolvedTargets) {
|
||||
console.warn(`[notices] Rust target ${target} not resolved offline: ${reason}`);
|
||||
}
|
||||
const crateRows = [...crates.values()].sort(byNameAndVersion).map((crate) => ({
|
||||
...crate,
|
||||
ecosystem: 'cargo',
|
||||
artifact: 'Desktop shell',
|
||||
license: normalizeCargoLicense(crate.license),
|
||||
declaredLicense: crate.license ?? (crate.licenseFile ? `see ${crate.licenseFile}` : null),
|
||||
textNumbers: registerTexts(
|
||||
readLicenseFiles(crate.dir, crate.licenseFile ? [crate.licenseFile] : []),
|
||||
`${crate.name} ${crate.version}`,
|
||||
),
|
||||
}));
|
||||
|
||||
// ── Rendering ──────────────────────────────────────────────────────────────
|
||||
const nodeVersion = bundledNodeVersion();
|
||||
const out = [];
|
||||
const line = (text = '') => out.push(text);
|
||||
|
||||
function npmSource(row) {
|
||||
if (row.resolved) return row.resolved;
|
||||
return row.key.startsWith('node_modules/') || row.key.includes('/node_modules/')
|
||||
? ''
|
||||
: `vendored at \`${row.key}\``;
|
||||
}
|
||||
|
||||
function platformLabel(row) {
|
||||
if (!row.platformSpecific) return 'all';
|
||||
return [row.os?.join('/'), row.cpu?.join('/')].filter(Boolean).join(' ');
|
||||
}
|
||||
|
||||
line('# Third-party notices');
|
||||
line();
|
||||
line('The Waggle community edition is MIT-licensed and ships the third-party components listed');
|
||||
line('below. Each entry names its license; the license and notice texts the components carry are');
|
||||
line('reproduced at the end of this file.');
|
||||
line();
|
||||
line('Generated by `node scripts/generate-third-party-notices.mjs` from `package-lock.json`, the');
|
||||
line('installed npm packages and `cargo metadata --offline` for `app/src-tauri`. Do not edit by hand;');
|
||||
line('regenerate after changing dependencies. `npm run check:licenses` checks the same npm packages');
|
||||
line('against the license policy, and cargo-deny checks the crates (`app/src-tauri/deny.toml`).');
|
||||
line();
|
||||
line('## Contents');
|
||||
line();
|
||||
line('- [What ships](#what-ships)');
|
||||
line('- [LGPL, MPL and other licenses with conditions](#lgpl-mpl-and-other-licenses-with-conditions)');
|
||||
line('- [Node.js runtime and npm](#nodejs-runtime-and-npm)');
|
||||
for (const artifact of artifacts) {
|
||||
const anchor = `${artifact.title} npm packages`.toLowerCase().replace(/[^a-z0-9 -]/g, '').replace(/ /g, '-');
|
||||
line(`- [${artifact.title} npm packages](#${anchor})`);
|
||||
}
|
||||
line('- [Rust crates](#rust-crates)');
|
||||
line('- [License texts](#license-texts)');
|
||||
line();
|
||||
|
||||
line('## What ships');
|
||||
line();
|
||||
line('| Artifact | Contents | Third-party components |');
|
||||
line('|---|---|---|');
|
||||
for (const artifact of artifacts) {
|
||||
line(`| ${artifact.title} | ${artifact.ships} | ${artifact.rows.length} npm packages |`);
|
||||
}
|
||||
line(`| Node.js runtime | Node.js ${nodeVersion} and npm, staged by \`scripts/bundle-node.mjs\` for the desktop sidecar | 2 components with their own notices |`);
|
||||
line(`| Desktop shell | the Tauri executable built from \`app/src-tauri\` | ${crateRows.length} Rust crates |`);
|
||||
line();
|
||||
line('The npm lists are the production dependency closures of `packages/server` and `apps/web`');
|
||||
line('from `package-lock.json`, restricted to the v1 platforms (Windows x64, macOS arm64 and x64,');
|
||||
line('Linux x64). They include everything the bundlers could pull in, so a few packages that');
|
||||
line('tree-shaking drops may be listed. The sidecar list leaves out the packages that');
|
||||
line('`scripts/stage-sidecar-deps.mjs` never ships (its `SKIP` set).');
|
||||
line();
|
||||
|
||||
// Copyleft section
|
||||
const components = [...artifacts.flatMap((artifact) => artifact.rows), ...crateRows];
|
||||
const groups = { lgpl: [], mpl: [], ofl: [], none: [], other: [], dual: [] };
|
||||
for (const component of components) {
|
||||
const group = classify(component);
|
||||
if (group) groups[group].push(component);
|
||||
}
|
||||
const lgplText = findText(/GNU LESSER GENERAL PUBLIC LICENSE\s+Version 3/);
|
||||
const gplText = texts.find((entry) => /\bGNU GENERAL PUBLIC LICENSE\s+Version 3, 29 June 2007/.test(entry.text));
|
||||
const textRef = (entry, name) => (entry ? `[license text ${entry.number}](#license-text-${entry.number})` : `the ${name} text (not found in any shipped package; add it before distribution)`);
|
||||
|
||||
function componentTable(rows) {
|
||||
line('| Component | Version | Artifact | License | Source |');
|
||||
line('|---|---|---|---|---|');
|
||||
for (const row of rows) {
|
||||
const source = row.ecosystem === 'cargo'
|
||||
? `https://crates.io/crates/${row.name}/${row.version}${row.repository ? ` (repository: ${row.repository})` : ''}`
|
||||
: npmSource(row);
|
||||
line(`| ${cell(row.name)} | ${cell(row.version)} | ${row.artifact} | ${cell(row.declaredLicense ?? row.license ?? 'none declared')} | ${cell(source)} |`);
|
||||
}
|
||||
line();
|
||||
}
|
||||
|
||||
line('## LGPL, MPL and other licenses with conditions');
|
||||
line();
|
||||
line('Most components are under permissive licenses that only ask for their notice to be kept,');
|
||||
line('which this file does. The components below carry further conditions. The summaries say what');
|
||||
line('the licenses ask of whoever distributes Waggle; they are engineering notes, not legal advice,');
|
||||
line('and the licensing review of the project owner (pending decision Q6) decides whether the');
|
||||
line('measures are sufficient.');
|
||||
line();
|
||||
|
||||
line('### LGPL-3.0');
|
||||
line();
|
||||
if (groups.lgpl.length === 0) {
|
||||
line('No shipped component is under the LGPL.');
|
||||
line();
|
||||
} else {
|
||||
componentTable(groups.lgpl.sort(byNameAndVersion));
|
||||
line('How they ship: `build-sidecar.mjs` keeps `@huggingface/transformers`, `sharp` and');
|
||||
line('`whatsapp-web.js` external to `service.js`, and `stage-sidecar-deps.mjs` copies these');
|
||||
line('packages unmodified into `resources/node_modules` of the desktop app. The libvips packages');
|
||||
line('(and, on Windows, `@img/sharp-win32-x64`) hold prebuilt shared libraries that sharp loads');
|
||||
line('at runtime; node-webpmux is a JavaScript package that whatsapp-web.js loads at startup.');
|
||||
line('None of them is compiled into Waggle code.');
|
||||
line();
|
||||
line('What LGPL-3.0 asks when they are distributed with Waggle:');
|
||||
line();
|
||||
line('1. Keep each library a separate file that the user can replace with a modified version');
|
||||
line(' (section 4(d)). The packaged layout above does this; a signed macOS or Windows bundle');
|
||||
line(' loses its signature when a file inside it is replaced, which the licensing review should');
|
||||
line(' consider.');
|
||||
line('2. Say prominently that the library is used and is covered by the LGPL, and ship the');
|
||||
line(` LGPL-3.0 and GPL-3.0 texts with it (sections 4(a) and 4(b)). LGPL-3.0: ${textRef(lgplText, 'LGPL-3.0')}.`);
|
||||
line(` GPL-3.0: ${textRef(gplText, 'GPL-3.0')}. The libvips packages contain no license file of their own.`);
|
||||
line('3. Make the source of the exact library version available, or point to where it is');
|
||||
line(' published (section 4(d)). The Source column links the npm tarballs; for the libvips');
|
||||
line(' packages these contain only binaries, and the build scripts and the upstream versions');
|
||||
line(' (`versions.json` in each package) are in the sharp-libvips repository of the same release.');
|
||||
line('4. Do not forbid users to modify the combined work or to reverse engineer it to debug such');
|
||||
line(' modifications (section 4). MIT does not.');
|
||||
line();
|
||||
const libvipsDir = ['darwin-arm64', 'darwin-x64', 'linux-x64', 'linuxmusl-x64', 'win32-x64']
|
||||
.map((suffix) => path.join(ROOT, 'node_modules', '@img', `sharp-libvips-${suffix}`))
|
||||
.find((dir) => fs.existsSync(path.join(dir, 'README.md')));
|
||||
if (libvipsDir) {
|
||||
const readme = fs.readFileSync(path.join(libvipsDir, 'README.md'), 'utf8');
|
||||
const table = /## Licensing[\s\S]*?(\|[\s\S]*?\n)(?:\n[^|]|$)/.exec(readme);
|
||||
if (table) {
|
||||
line('The libvips packages bundle further libraries. Their README lists them as follows (for');
|
||||
line('cairo under MPL-2.0, see below):');
|
||||
line();
|
||||
for (const row of table[1].trim().split('\n')) line(row);
|
||||
line();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
line('### MPL-2.0');
|
||||
line();
|
||||
if (groups.mpl.length === 0) {
|
||||
line('No shipped component is under the MPL alone.');
|
||||
line();
|
||||
} else {
|
||||
componentTable(groups.mpl.sort(byNameAndVersion));
|
||||
}
|
||||
const mplText = findText(/Mozilla Public License,? Version 2\.0/);
|
||||
line(`MPL-2.0 is a per-file copyleft; its text is ${textRef(mplText, 'MPL-2.0')}. What it asks:`);
|
||||
line();
|
||||
line('1. MPL-covered files stay under MPL-2.0, and a modified MPL file must be published under');
|
||||
line(' MPL-2.0. Waggle uses these components unmodified.');
|
||||
line('2. With the executable, tell recipients where the source of the MPL-covered files is');
|
||||
line(' available (section 3.2(a)). The Source column links the exact published versions; cairo');
|
||||
line(' inside libvips is published by the sharp-libvips release named above.');
|
||||
line('3. Keep the MPL notices intact. The executable may be distributed under other terms (MIT)');
|
||||
line(' as long as they do not restrict access to that source (section 3.2(b)).');
|
||||
line();
|
||||
|
||||
if (groups.ofl.length > 0) {
|
||||
line('### OFL-1.1 (fonts)');
|
||||
line();
|
||||
componentTable(groups.ofl.sort(byNameAndVersion));
|
||||
line('The SIL Open Font License allows bundling the fonts with software. The license text and');
|
||||
line('copyright notice must ship with the fonts (they are reproduced in this file), the fonts');
|
||||
line('may not be sold on their own, and modified fonts may not use the reserved font names.');
|
||||
line();
|
||||
}
|
||||
|
||||
if (groups.none.length > 0 || groups.other.length > 0) {
|
||||
line('### No license or an unrecognised license');
|
||||
line();
|
||||
componentTable([...groups.none, ...groups.other].sort(byNameAndVersion));
|
||||
line('Without a license grant the code cannot be redistributed. These need a decision of the');
|
||||
line('licensing review or a dependency change before release.');
|
||||
line();
|
||||
}
|
||||
|
||||
if (groups.dual.length > 0) {
|
||||
line('### Dual-licensed components used under the permissive option');
|
||||
line();
|
||||
line('| Component | Version | Artifact | License | Used under |');
|
||||
line('|---|---|---|---|---|');
|
||||
for (const row of groups.dual.sort(byNameAndVersion)) {
|
||||
line(`| ${cell(row.name)} | ${cell(row.version)} | ${row.artifact} | ${cell(row.declaredLicense ?? row.license)} | ${cell(permissiveChoice(row.license))} |`);
|
||||
}
|
||||
line();
|
||||
}
|
||||
|
||||
// Runtime
|
||||
const lockEntry = (key) => lockPackages[key] ?? {};
|
||||
const braceExpansion = lockEntry('node_modules/archiver-utils/node_modules/brace-expansion');
|
||||
const ipAddress = lockEntry('node_modules/ip-address');
|
||||
line('## Node.js runtime and npm');
|
||||
line();
|
||||
line(`The desktop sidecar runs on Node.js ${nodeVersion}, which \`scripts/bundle-node.mjs\` downloads from`);
|
||||
line('nodejs.org and verifies against the official SHA-256 sums. It is not an npm dependency of');
|
||||
line('Waggle and is not part of the npm lists below.');
|
||||
line();
|
||||
line('| Component | Version | License | Where its notices ship |');
|
||||
line('|---|---|---|---|');
|
||||
line(`| Node.js | ${nodeVersion} | MIT, plus the licenses of the libraries Node.js includes (OpenSSL, ICU, libuv, V8 and others) | Node's LICENSE file, which reproduces all of them, as \`resources/node_modules/waggle-node-runtime/NODE-LICENSE\` |`);
|
||||
line(`| npm | the release bundled with Node.js ${nodeVersion} | Artistic-2.0; its bundled dependencies carry their own licenses | with its LICENSE and its bundled \`node_modules\` in \`resources/node_modules/waggle-node-runtime/node_modules/npm\`; two bundled dependencies are replaced (next row) |`);
|
||||
line(`| brace-expansion, ip-address | ${braceExpansion.version ?? '?'}, ${ipAddress.version ?? '?'} | ${braceExpansion.license ?? '?'}, ${ipAddress.license ?? '?'} | copied from \`package-lock.json\` over npm's bundled copies by \`bundle-node.mjs\` |`);
|
||||
line();
|
||||
|
||||
// npm tables
|
||||
for (const artifact of artifacts) {
|
||||
line(`## ${artifact.title} npm packages`);
|
||||
line();
|
||||
line(`${artifact.rows.length} packages from the production dependencies of \`${artifact.workspace}\`; they ship in ${artifact.ships}.`);
|
||||
line();
|
||||
line('| Package | Version | License | Platforms | License texts |');
|
||||
line('|---|---|---|---|---|');
|
||||
for (const row of artifact.rows) {
|
||||
let note = textLinks(row.textNumbers);
|
||||
if (row.platformSpecific) note = 'platform package; ships its own files';
|
||||
else if (!row.installed) note = 'not installed when generated';
|
||||
else if (!note) note = 'no license file in the package';
|
||||
const vendored = npmSource(row).startsWith('vendored') ? ` (${npmSource(row)}, modified)` : '';
|
||||
line(`| ${cell(row.name)}${vendored} | ${cell(row.version)} | ${cell(row.license ?? 'none declared')} | ${platformLabel(row)} | ${note} |`);
|
||||
}
|
||||
line();
|
||||
}
|
||||
|
||||
// Rust
|
||||
line('## Rust crates');
|
||||
line();
|
||||
line(`${crateRows.length} crates that the desktop shell (\`app/src-tauri\`) links, following normal dependencies`);
|
||||
line('(build and dev dependencies do not ship).');
|
||||
line();
|
||||
line(`Resolved offline for: ${resolvedTargets.map((target) => TARGET_LABELS[target]).join(', ') || 'none'}.`);
|
||||
if (unresolvedTargets.length > 0) {
|
||||
line();
|
||||
const labels = unresolvedTargets.map(({ target }) => TARGET_LABELS[target]);
|
||||
const named = labels.length > 1 ? `${labels.slice(0, -1).join(', ')} and ${labels.at(-1)}` : labels[0];
|
||||
line(`**Incomplete:** the crates for ${named} were`);
|
||||
line('not in the local cargo cache when this file was generated, so crates used only on those');
|
||||
line('platforms are missing below. Fetch them (`cargo fetch --locked --manifest-path');
|
||||
line('app/src-tauri/Cargo.toml`, which needs network access) and regenerate before a release.');
|
||||
}
|
||||
line();
|
||||
const targetSets = new Set(crateRows.map((row) => [...row.targets].sort().join(',')));
|
||||
const showTargets = targetSets.size > 1;
|
||||
line(`| Crate | Version | License |${showTargets ? ' Targets |' : ''} License texts |`);
|
||||
line(`|---|---|---|${showTargets ? '---|' : ''}---|`);
|
||||
for (const row of crateRows) {
|
||||
const targets = showTargets ? ` ${[...row.targets].map((target) => TARGET_LABELS[target]).join(', ')} |` : '';
|
||||
const note = textLinks(row.textNumbers) || 'no license file in the crate';
|
||||
line(`| ${cell(row.name)} | ${cell(row.version)} | ${cell(row.declaredLicense ?? 'none declared')} |${targets} ${note} |`);
|
||||
}
|
||||
line();
|
||||
|
||||
// Texts
|
||||
line('## License texts');
|
||||
line();
|
||||
line('Each text appears once, followed by the components that carry it.');
|
||||
line();
|
||||
for (const entry of texts) {
|
||||
const marker = fence(entry.text);
|
||||
line(`### License text ${entry.number}`);
|
||||
line();
|
||||
line(`From ${entry.source}. Carried by: ${entry.users.join(', ')}.`);
|
||||
line();
|
||||
line(`${marker}text`);
|
||||
line(entry.text);
|
||||
line(marker);
|
||||
line();
|
||||
}
|
||||
|
||||
fs.writeFileSync(OUTPUT, `${out.join('\n').trimEnd()}\n`, 'utf8');
|
||||
const pendingExceptions = EXCEPTIONS.filter((exception) => exception.pending).length;
|
||||
console.log(
|
||||
`[notices] Wrote ${path.relative(ROOT, OUTPUT)}: ${artifacts.map((a) => `${a.rows.length} ${a.id}`).join(', ')} npm packages, `
|
||||
+ `${crateRows.length} crates, ${texts.length} distinct license texts`
|
||||
+ ` (${pendingExceptions} npm exceptions pending review)`,
|
||||
);
|
||||
304
scripts/license-inventory.mjs
Normal file
304
scripts/license-inventory.mjs
Normal file
@@ -0,0 +1,304 @@
|
||||
/**
|
||||
* 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];
|
||||
}
|
||||
Reference in New Issue
Block a user