A full-featured download manager.
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import os from 'node:os'
import path from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import {
buildThirdPartyBundle,
collectRuntimePackages,
} from '../scripts/generate-third-party-notices.mjs'
const temporaryDirectories: string[] = []
async function createFixture(): Promise<string> {
const root = await mkdtemp(path.join(os.tmpdir(), 'motrix-licenses-'))
temporaryDirectories.push(root)
return root
}
async function writeJson(filePath: string, value: unknown): Promise<void> {
await mkdir(path.dirname(filePath), { recursive: true })
await writeFile(filePath, `${JSON.stringify(value, null, 2)}\n`)
}
async function writePackage(
root: string,
relativeDirectory: string,
manifest: Record<string, unknown>,
license = 'fixture license text\n'
): Promise<void> {
const directory = path.join(root, relativeDirectory)
await writeJson(path.join(directory, 'package.json'), manifest)
if (license) await writeFile(path.join(directory, 'LICENSE'), license)
}
afterEach(async () => {
await Promise.all(
temporaryDirectories
.splice(0)
.map((directory) => rm(directory, { recursive: true, force: true }))
)
})
describe('third-party notice generator', () => {
it('retains an omitted license declaration while applying a version-specific reviewed conclusion', async () => {
const root = await createFixture()
await writeJson(path.join(root, 'package.json'), {
name: 'fixture',
version: '1.0.0',
dependencies: { alpha: '1.0.0' },
})
await writePackage(root, 'node_modules/alpha', {
name: 'alpha',
version: '1.0.0',
})
await expect(collectRuntimePackages({ projectDir: root })).rejects.toThrow(
'no declared license'
)
const graph = await collectRuntimePackages({
projectDir: root,
packageOverrides: { '[email protected]': { licenseConcluded: 'MIT' } },
})
expect(graph.packages[0]).toMatchObject({
licenseDeclared: 'NOASSERTION',
licenseConcluded: 'MIT',
})
})
it('uses root runtime declarations despite an existing staged manifest', async () => {
const root = await createFixture()
await writeJson(path.join(root, 'package.json'), {
name: 'fixture-app',
version: '1.0.0',
dependencies: { alpha: '1.0.0' },
devDependencies: { buildOnly: '1.0.0' },
})
await writeJson(path.join(root, 'dist/electron-app/package.json'), {
name: 'staged-app',
version: '9.9.9',
dependencies: { buildOnly: '1.0.0' },
})
await writePackage(root, 'node_modules/alpha', {
name: 'alpha',
version: '1.0.0',
license: 'MIT',
dependencies: { beta: '2.0.0' },
optionalDependencies: { unavailable: '1.0.0' },
})
await writePackage(root, 'node_modules/alpha/node_modules/beta', {
name: 'beta',
version: '2.0.0',
license: 'ISC',
})
await writePackage(root, 'node_modules/buildOnly', {
name: 'buildOnly',
version: '1.0.0',
license: 'MIT',
})
await writePackage(root, 'unrelated/package', {
name: 'unrelated',
version: '9.9.9',
license: 'MIT',
})
const graph = await collectRuntimePackages({ projectDir: root })
expect(graph.root).toEqual({ name: 'fixture-app', version: '1.0.0' })
expect(graph.rootDependencies).toEqual(['[email protected]'])
expect(graph.packages.map((pkg) => pkg.key)).toEqual([
'[email protected]',
'[email protected]',
])
expect(graph.relationships).toEqual([
{ from: '[email protected]', to: '[email protected]' },
])
})
it('requires an explicit exception when a package omits its license file', async () => {
const root = await createFixture()
await writeJson(path.join(root, 'package.json'), {
name: 'fixture-app',
version: '1.0.0',
dependencies: { alpha: '1.0.0' },
})
await writePackage(
root,
'node_modules/alpha',
{ name: 'alpha', version: '1.0.0', license: 'MIT' },
''
)
await expect(collectRuntimePackages({ projectDir: root })).rejects.toThrow(
'add a reviewed packageOverrides entry'
)
await writeFile(path.join(root, 'MIT.txt'), 'MIT fixture text\n')
const graph = await collectRuntimePackages({
projectDir: root,
packageOverrides: {
'[email protected]': {
fallbackLicenseFiles: ['MIT.txt'],
attribution: 'Fixture Author',
},
},
})
expect(graph.packages[0]?.licenseFiles[0]?.text).toContain(
'Attribution: Fixture Author'
)
})
it('preserves a root edge when a direct dependency is also transitive', async () => {
const root = await createFixture()
await writeJson(path.join(root, 'package.json'), {
name: 'fixture-app',
version: '1.0.0',
dependencies: { alpha: '1.0.0', beta: '2.0.0' },
})
await writePackage(root, 'node_modules/alpha', {
name: 'alpha',
version: '1.0.0',
license: 'MIT',
dependencies: { beta: '2.0.0' },
})
await writePackage(root, 'node_modules/beta', {
name: 'beta',
version: '2.0.0',
license: 'ISC',
})
const graph = await collectRuntimePackages({ projectDir: root })
expect(graph.rootDependencies).toEqual(['[email protected]', '[email protected]'])
expect(graph.relationships).toEqual([
{ from: '[email protected]', to: '[email protected]' },
])
})
it('builds an SPDX SBOM and consolidated notices for the current release graph', async () => {
const bundle = await buildThirdPartyBundle()
const inventory = bundle.files['THIRD_PARTY_DEPENDENCIES.md']
const licenses = bundle.files['THIRD_PARTY_LICENSES.txt']
const sbom = JSON.parse(bundle.files['sbom.spdx.json']) as {
spdxVersion: string
packages: Array<{
SPDXID: string
name: string
versionInfo?: string
downloadLocation: string
}>
relationships: Array<{
spdxElementId: string
relationshipType: string
relatedSpdxElement: string
}>
}
expect(bundle.packageCount).toBeGreaterThan(100)
expect(inventory).toContain('| @xyflow/react | 13.0.1 |')
expect(inventory).toContain('| react-compiler-runtime | 1.0.0 |')
expect(inventory).toContain('| aria2 | 1.37.0-motrix.25 |')
expect(inventory).toContain('| motrix.filename-template | 1.1.1 |')
expect(inventory).toContain('Apple San Francisco tray font')
expect(licenses).toContain('GNU GENERAL PUBLIC LICENSE')
expect(licenses).toContain(
'Chromium software is made available as source code'
)
expect(sbom.spdxVersion).toBe('SPDX-2.3')
expect(sbom.packages).toContainEqual(
expect.objectContaining({ name: 'Electron', versionInfo: '45.3.3' })
)
expect(sbom.packages).toContainEqual(
expect.objectContaining({
name: 'type-fest',
versionInfo: '4.41.0',
licenseDeclared: '(MIT OR CC0-1.0)',
licenseConcluded: 'MIT',
})
)
expect(
sbom.packages.every(
(pkg) =>
pkg.downloadLocation === 'NOASSERTION' ||
/^(?:https?|git|ssh):/.test(pkg.downloadLocation)
)
).toBe(true)
const identifiers = new Set([
'SPDXRef-DOCUMENT',
...sbom.packages.map((pkg) => pkg.SPDXID),
])
expect(
sbom.relationships.every(
(relationship) =>
identifiers.has(relationship.spdxElementId) &&
identifiers.has(relationship.relatedSpdxElement)
)
).toBe(true)
const rootId = sbom.relationships.find(
(relationship) => relationship.relationshipType === 'DESCRIBES'
)?.relatedSpdxElement
const packageIds = new Map(
sbom.packages.map((pkg) => [`${pkg.name}@${pkg.versionInfo}`, pkg.SPDXID])
)
const rootDependencyIds = new Set(
sbom.relationships
.filter(
(relationship) =>
relationship.spdxElementId === rootId &&
relationship.relationshipType === 'DEPENDS_ON'
)
.map((relationship) => relationship.relatedSpdxElement)
)
for (const packageName of [
'ajv',
'clsx',
'pino',
'tailwind-merge',
'tailwindcss',
'tslib',
'vscode-jsonrpc',
'ws',
'zod',
]) {
// A transitive dependency can install another version of the same name.
// The root edge must point to the version actually resolved by the app.
const manifest = JSON.parse(
await readFile(
path.join(process.cwd(), 'node_modules', packageName, 'package.json'),
'utf8'
)
) as { version: string }
expect(rootDependencyIds).toContain(
packageIds.get(`${packageName}@${manifest.version}`)
)
}
const parsedAgain = JSON.parse(
await readFile(
path.join(process.cwd(), 'scripts/third-party-notices.config.json'),
'utf8'
)
)
expect(parsedAgain.schemaVersion).toBe(1)
}, 15_000)
it('omits desktop-only components from the server distribution', async () => {
const [desktop, server] = await Promise.all([
buildThirdPartyBundle({ distribution: 'desktop' }),
buildThirdPartyBundle({ distribution: 'server' }),
])
const desktopLicenses = desktop.files['THIRD_PARTY_LICENSES.txt']
const serverLicenses = server.files['THIRD_PARTY_LICENSES.txt']
// The Server runtime never ships Electron, so Chromium's license set --
// by far the largest block in the bundle -- must not ride along.
expect(desktopLicenses).toContain(
'Chromium software is made available as source code'
)
expect(serverLicenses).not.toContain(
'Chromium software is made available as source code'
)
expect(desktopLicenses).toMatch(/^Components:.*\bElectron@/m)
expect(serverLicenses).not.toMatch(/^Components:.*\bElectron@/m)
// Everything the Server does ship keeps its notice.
for (const licenses of [desktopLicenses, serverLicenses]) {
expect(licenses).toContain('GNU GENERAL PUBLIC LICENSE')
}
const serverInventory = server.files['THIRD_PARTY_DEPENDENCIES.md']
expect(serverInventory).toContain('| aria2 | 1.37.0-motrix.25 |')
expect(serverInventory).toContain('| motrix.filename-template | 1.1.1 |')
expect(serverInventory).not.toContain('| Electron |')
const serverSbom = JSON.parse(server.files['sbom.spdx.json']) as {
packages: Array<{ name: string }>
}
expect(serverSbom.packages.some((pkg) => pkg.name === 'Electron')).toBe(
false
)
expect(Buffer.byteLength(serverLicenses, 'utf8')).toBeLessThan(
Buffer.byteLength(desktopLicenses, 'utf8') / 2
)
}, 20_000)
})