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

116 lines
3.6 KiB
JavaScript

import { existsSync, readFileSync, readdirSync } 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: `notices` are shown in Settings > About, and
// `licensesText` is THIRD_PARTY_LICENSES.txt, the license files of the npm packages whose
// notices minification strips from the bundle.
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}`,
files: readdirSync(join(cwd, path))
.filter((file) => /^(licen[cs]e|copying|notice)/i.test(file))
.map((file) => read(join(path, file)).trim())
};
});
// 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'));
const licensesText = [
...new Map(
npm.map(({ name, version, license, url, files }) => [
`${name}@${version}`,
[
`${name} ${version}`,
`License: ${license}`,
...(files.length ? files : [`The package ships no license file; see ${url}`])
].join('\n\n')
])
).values()
].join(`\n\n${'-'.repeat(80)}\n\n`);
const notices = [
...new Map(
[...npm, ...pyodide, ...crates, ...python].map(({ files, ...notice }) => [
`${notice.url}@${notice.version}`,
notice
])
).values()
];
return { notices, licensesText };
}