Files
open-webui/scripts/third-party-notices.js
T
2026-12-04 05:48:10 +04:00

96 lines
2.9 KiB
JavaScript

import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import JSZip from 'jszip';
// npm packages that publish no license in their package.json, taken from their license files.
const UNDECLARED_NPM_LICENSES = {
khroma: 'MIT',
'svelte-toolbelt': 'MIT',
'xmlhttprequest-ssl': 'MIT'
};
// Third-party code shipped in Open WebUI, shown in Settings > About.
export async function resolveThirdPartyNotices({
cwd = fileURLToPath(new URL('../', import.meta.url))
} = {}) {
const read = (path) => readFileSync(join(cwd, path), 'utf8');
// Every production npm package in the resolved tree, including transitive ones.
const npm = Object.entries(JSON.parse(read('package-lock.json')).packages)
.filter(
([path, pkg]) =>
path.startsWith('node_modules/') &&
!pkg.dev &&
!pkg.devOptional &&
existsSync(join(cwd, path))
)
.map(([path, pkg]) => {
const name = pkg.name ?? path.split('node_modules/').pop();
return {
name,
version: pkg.version,
license: pkg.license ?? UNDECLARED_NPM_LICENSES[name] ?? '',
url: `https://www.npmjs.com/package/${name}`
};
});
// Python wheels bundled for in-browser code execution.
const pyodideLock = 'static/pyodide/pyodide-lock.json';
const pyodide = existsSync(join(cwd, pyodideLock))
? await Promise.all(
Object.values(JSON.parse(read(pyodideLock)).packages)
.filter(
({ file_name }) =>
file_name.endsWith('.whl') && existsSync(join(cwd, 'static/pyodide', file_name))
)
.map(async ({ name, version, file_name }) => {
const zip = await JSZip.loadAsync(readFileSync(join(cwd, 'static/pyodide', file_name)));
const headers = (await zip.file(/\.dist-info\/METADATA$/)[0].async('string')).split(
/\r?\n\r?\n/
)[0];
const header = (key) =>
[...headers.matchAll(new RegExp(`^${key}: (.*)$`, 'gm'))].map((match) => match[1]);
return {
name,
version,
license:
header('License-Expression')[0] ??
header('Classifier')
.find((classifier) => classifier.startsWith('License :: '))
?.split(' :: ')
.pop() ??
header('License')[0] ??
'',
url: `https://pypi.org/project/${name}/`
};
})
)
: [];
// Rust crates compiled into the encryption WebAssembly module.
const crates = [
...read('src/lib/encryption/vendor/THIRD_PARTY_LICENSES.txt').matchAll(
/^Package: (.+)\nVersion: (.+)\nLicense: (.+)$/gm
)
].map(([, name, version, license]) => ({
name,
version,
license,
url: `https://crates.io/crates/${name}`
}));
// Backend Python packages, generated from requirements.lock by backend/lock-licenses.py.
const python = JSON.parse(read('backend/requirements-licenses.json'));
return [
...new Map(
[...npm, ...pyodide, ...crates, ...python].map((notice) => [
`${notice.url}@${notice.version}`,
notice
])
).values()
];
}