History

feat(settings): add open source license notices

Julius Marminge committed 2 months ago

Commit 3ffdc2a1 parent 31c1c5953 files · +2895 −14Browse files

Showing the changes to scripts (2 of 53 files). Show all

Unified · Split

2 files
  1. scripts/lib/third-party-licenses.test.ts +245 −0
  2. scripts/lib/third-party-licenses.ts +710 −0
scripts/lib/third-party-licenses.test.ts+245−0
@@ -0,0 +1,245 @@
1+// @effect-diagnostics nodeBuiltinImport:off - Tests exercise the Node filesystem build boundary.
2+
3+import * as NodeFSP from "node:fs/promises";
4+import * as NodeOS from "node:os";
5+import * as NodePath from "node:path";
6+
7+import { afterEach, describe, expect, it } from "vite-plus/test";
8+
9+import { generateThirdPartyLicenseManifest } from "./third-party-licenses.js";
10+
11+const tempDirectories: string[] = [];
12+
13+async function writeJson(path: string, value: unknown): Promise<void> {
14+ await NodeFSP.mkdir(NodePath.dirname(path), { recursive: true });
15+ await NodeFSP.writeFile(path, `${JSON.stringify(value)}\n`, "utf8");
16+}
17+
18+async function createFixture(): Promise<{
19+ readonly appManifest: string;
20+ readonly configFile: string;
21+ readonly dependencyRoot: string;
22+ readonly root: string;
23+}> {
24+ const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3code-licenses-"));
25+ tempDirectories.push(root);
26+ const appManifest = NodePath.join(root, "package.json");
27+ const dependencyRoot = NodePath.join(root, "node_modules", "demo-dependency");
28+ const configFile = NodePath.join(root, "third-party-licenses.config.json");
29+
30+ await writeJson(appManifest, {
31+ name: "fixture-app",
32+ dependencies: { "demo-dependency": "1.2.3" },
33+ });
34+ await writeJson(NodePath.join(dependencyRoot, "package.json"), {
35+ name: "demo-dependency",
36+ version: "1.2.3",
37+ license: "MIT",
38+ main: "index.js",
39+ repository: "example/demo-dependency",
40+ });
41+ await NodeFSP.writeFile(NodePath.join(dependencyRoot, "index.js"), "export {};\n", "utf8");
42+ await NodeFSP.writeFile(
43+ NodePath.join(dependencyRoot, "LICENSE"),
44+ "Demo MIT license text\n",
45+ "utf8",
46+ );
47+ await NodeFSP.writeFile(NodePath.join(root, "asset-notice.txt"), "Asset notice text\n", "utf8");
48+ await writeJson(configFile, {
49+ customNotices: [
50+ {
51+ name: "demo-asset",
52+ license: "CC-BY-4.0",
53+ noticeFile: "asset-notice.txt",
54+ bundles: ["assets", "web"],
55+ },
56+ ],
57+ packageOverrides: [],
58+ });
59+ return { appManifest, configFile, dependencyRoot, root };
60+}
61+
62+afterEach(async () => {
63+ await Promise.all(
64+ tempDirectories
65+ .splice(0)
66+ .map((directory) => NodeFSP.rm(directory, { force: true, recursive: true })),
67+ );
68+});
69+
70+describe("third-party license generation", () => {
71+ it("collects production packages and custom asset notices", async () => {
72+ const fixture = await createFixture();
73+ const manifest = await generateThirdPartyLicenseManifest({
74+ configFile: fixture.configFile,
75+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
76+ });
77+
78+ expect(manifest).toEqual({
79+ schemaVersion: 1,
80+ entries: [
81+ {
82+ bundles: ["assets", "web"],
83+ kind: "custom",
84+ license: "CC-BY-4.0",
85+ name: "demo-asset",
86+ noticeText: "Asset notice text",
87+ sourceUrl: null,
88+ version: null,
89+ },
90+ {
91+ bundles: ["web"],
92+ kind: "package",
93+ license: "MIT",
94+ name: "demo-dependency",
95+ noticeText: "Demo MIT license text",
96+ sourceUrl: "https://github.com/example/demo-dependency",
97+ version: "1.2.3",
98+ },
99+ ],
100+ });
101+ });
102+
103+ it("fails when a production package has no distributable notice text", async () => {
104+ const fixture = await createFixture();
105+ await NodeFSP.rm(NodePath.join(fixture.dependencyRoot, "LICENSE"));
106+
107+ await expect(
108+ generateThirdPartyLicenseManifest({
109+ configFile: fixture.configFile,
110+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
111+ }),
112+ ).rejects.toThrow("does not include a license or notice file");
113+ });
114+
115+ it("collects notices nested inside a published package", async () => {
116+ const fixture = await createFixture();
117+ await NodeFSP.rm(NodePath.join(fixture.dependencyRoot, "LICENSE"));
118+ await NodeFSP.mkdir(NodePath.join(fixture.dependencyRoot, "dist", "third-party"), {
119+ recursive: true,
120+ });
121+ await NodeFSP.writeFile(
122+ NodePath.join(fixture.dependencyRoot, "dist", "third-party", "NOTICE.txt"),
123+ "Nested notice\n",
124+ "utf8",
125+ );
126+
127+ const manifest = await generateThirdPartyLicenseManifest({
128+ configFile: fixture.configFile,
129+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
130+ });
131+
132+ expect(manifest.entries.find((entry) => entry.name === "demo-dependency")?.noticeText).toBe(
133+ "Nested notice",
134+ );
135+ });
136+
137+ it("uses package overrides for notices published outside the npm archive", async () => {
138+ const fixture = await createFixture();
139+ await NodeFSP.rm(NodePath.join(fixture.dependencyRoot, "LICENSE"));
140+ await NodeFSP.writeFile(NodePath.join(fixture.root, "override.txt"), "Override text\n", "utf8");
141+ await writeJson(fixture.configFile, {
142+ customNotices: [],
143+ packageOverrides: [
144+ {
145+ name: "demo-dependency",
146+ noticeFile: "override.txt",
147+ },
148+ ],
149+ });
150+
151+ const manifest = await generateThirdPartyLicenseManifest({
152+ configFile: fixture.configFile,
153+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
154+ });
155+
156+ expect(manifest.entries[0]?.noticeText).toBe("Override text");
157+ });
158+
159+ it("applies repository overrides across monorepo packages", async () => {
160+ const fixture = await createFixture();
161+ await NodeFSP.rm(NodePath.join(fixture.dependencyRoot, "LICENSE"));
162+ await NodeFSP.writeFile(
163+ NodePath.join(fixture.root, "override.txt"),
164+ "Repository text\n",
165+ "utf8",
166+ );
167+ await writeJson(fixture.configFile, {
168+ customNotices: [],
169+ packageOverrides: [
170+ {
171+ repositoryUrl: "https://github.com/example/demo-dependency",
172+ noticeFile: "override.txt",
173+ },
174+ ],
175+ });
176+
177+ const manifest = await generateThirdPartyLicenseManifest({
178+ configFile: fixture.configFile,
179+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
180+ });
181+
182+ expect(manifest.entries[0]?.noticeText).toBe("Repository text");
183+ });
184+
185+ it("reuses a repository license for packages from the same monorepo", async () => {
186+ const fixture = await createFixture();
187+ const siblingRoot = NodePath.join(fixture.root, "node_modules", "demo-sibling");
188+ await writeJson(fixture.appManifest, {
189+ name: "fixture-app",
190+ dependencies: { "demo-dependency": "1.2.3", "demo-sibling": "2.0.0" },
191+ });
192+ await writeJson(NodePath.join(siblingRoot, "package.json"), {
193+ name: "demo-sibling",
194+ version: "2.0.0",
195+ license: "MIT",
196+ main: "index.js",
197+ repository: "example/demo-dependency",
198+ });
199+ await NodeFSP.writeFile(NodePath.join(siblingRoot, "index.js"), "export {};\n", "utf8");
200+
201+ const manifest = await generateThirdPartyLicenseManifest({
202+ configFile: fixture.configFile,
203+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
204+ });
205+
206+ expect(manifest.entries.find((entry) => entry.name === "demo-sibling")?.noticeText).toBe(
207+ "Demo MIT license text",
208+ );
209+ });
210+
211+ it("omits custom notices for other bundles", async () => {
212+ const fixture = await createFixture();
213+ await writeJson(fixture.configFile, {
214+ customNotices: [
215+ {
216+ name: "web-only-asset",
217+ license: "MIT",
218+ noticeFile: "asset-notice.txt",
219+ bundles: ["assets", "web"],
220+ },
221+ ],
222+ packageOverrides: [],
223+ });
224+
225+ const manifest = await generateThirdPartyLicenseManifest({
226+ configFile: fixture.configFile,
227+ packageManifests: [{ bundle: "mobile", path: fixture.appManifest }],
228+ });
229+
230+ expect(manifest.entries.some((entry) => entry.name === "web-only-asset")).toBe(false);
231+ });
232+
233+ it("fails when a custom notice file is empty", async () => {
234+ const fixture = await createFixture();
235+ await NodeFSP.writeFile(NodePath.join(fixture.root, "asset-notice.txt"), "\n", "utf8");
236+
237+ await expect(
238+ generateThirdPartyLicenseManifest({
239+ configFile: fixture.configFile,
240+ packageManifests: [{ bundle: "web", path: fixture.appManifest }],
241+ }),
242+ ).rejects.toThrow('Custom third-party notice "demo-asset" is empty');
243+ });
244+});
245+// @effect-diagnostics nodeBuiltinImport:off - Tests exercise the Node filesystem build boundary.
scripts/lib/third-party-licenses.ts+710−0
@@ -0,0 +1,710 @@
1+// @effect-diagnostics nodeBuiltinImport:off - Vite's build plugin runs before an Effect runtime exists.
2+
3+import * as NodeFSP from "node:fs/promises";
4+import * as NodePath from "node:path";
5+import * as NodeURL from "node:url";
6+import * as NodeModule from "node:module";
7+
8+import type { Plugin } from "vite-plus";
9+
10+export const THIRD_PARTY_LICENSES_FILE_NAME = "third-party-licenses.json";
11+
12+export interface ThirdPartyLicenseEntry {
13+ readonly bundles: ReadonlyArray<string>;
14+ readonly kind: "custom" | "package";
15+ readonly license: string;
16+ readonly name: string;
17+ readonly noticeText: string;
18+ readonly sourceUrl: string | null;
19+ readonly version: string | null;
20+}
21+
22+export interface ThirdPartyLicenseManifest {
23+ readonly schemaVersion: 1;
24+ readonly entries: ReadonlyArray<ThirdPartyLicenseEntry>;
25+}
26+
27+export interface ThirdPartyLicensePackageManifest {
28+ readonly bundle: string;
29+ readonly path: string | URL;
30+}
31+
32+export interface ThirdPartyLicensesPluginOptions {
33+ readonly configFile?: string | URL;
34+ readonly packageManifests: ReadonlyArray<ThirdPartyLicensePackageManifest>;
35+ readonly bundleName: string;
36+}
37+
38+interface PackageJson {
39+ readonly dependencies?: Readonly<Record<string, string>>;
40+ readonly homepage?: unknown;
41+ readonly license?: unknown;
42+ readonly licenses?: unknown;
43+ readonly name?: unknown;
44+ readonly optionalDependencies?: Readonly<Record<string, string>>;
45+ readonly repository?: unknown;
46+ readonly version?: unknown;
47+}
48+
49+interface CustomNoticeConfigEntry {
50+ readonly bundles?: ReadonlyArray<string>;
51+ readonly license: string;
52+ readonly name: string;
53+ readonly noticeFile: string;
54+ readonly sourceUrl?: string;
55+ readonly version?: string;
56+}
57+
58+interface PackageNoticeOverrideConfigEntry {
59+ readonly license?: string;
60+ readonly name?: string;
61+ readonly noticeFile?: string;
62+ readonly repositoryUrl?: string;
63+ readonly sourceUrl?: string;
64+ readonly version?: string;
65+}
66+
67+interface ThirdPartyLicensesConfig {
68+ readonly customNotices: ReadonlyArray<CustomNoticeConfigEntry>;
69+ readonly packageOverrides: ReadonlyArray<PackageNoticeOverrideConfigEntry>;
70+}
71+
72+interface CollectedPackage {
73+ readonly bundles: Set<string>;
74+ readonly packageJson: PackageJson;
75+ readonly packageRoot: string;
76+}
77+
78+interface PackageCollection {
79+ readonly byIdentity: Map<string, CollectedPackage>;
80+}
81+
82+const EMPTY_CONFIG: ThirdPartyLicensesConfig = {
83+ customNotices: [],
84+ packageOverrides: [],
85+};
86+
87+const NOTICE_FILE_PATTERN = /^(?:licen[cs]e|copying|notice)(?:[._-].*)?$/i;
88+const FIRST_PARTY_PACKAGE_PREFIX = "@t3tools/";
89+
90+function asPath(value: string | URL): string {
91+ return value instanceof URL ? NodeURL.fileURLToPath(value) : NodePath.resolve(value);
92+}
93+
94+function isRecord(value: unknown): value is Record<string, unknown> {
95+ return typeof value === "object" && value !== null && !Array.isArray(value);
96+}
97+
98+function readRequiredString(value: Record<string, unknown>, key: string, context: string): string {
99+ const field = value[key];
100+ if (typeof field !== "string" || field.trim().length === 0) {
101+ throw new Error(`${context} must define a non-empty "${key}" string.`);
102+ }
103+ return field.trim();
104+}
105+
106+function readOptionalString(
107+ value: Record<string, unknown>,
108+ key: string,
109+ context: string,
110+): string | undefined {
111+ const field = value[key];
112+ if (field === undefined) return undefined;
113+ if (typeof field !== "string" || field.trim().length === 0) {
114+ throw new Error(`${context} must define "${key}" as a non-empty string when present.`);
115+ }
116+ return field.trim();
117+}
118+
119+function readOptionalStringArray(
120+ value: Record<string, unknown>,
121+ key: string,
122+ context: string,
123+): ReadonlyArray<string> | undefined {
124+ const field = value[key];
125+ if (field === undefined) return undefined;
126+ if (
127+ !Array.isArray(field) ||
128+ field.length === 0 ||
129+ field.some((entry) => typeof entry !== "string" || entry.trim().length === 0)
130+ ) {
131+ throw new Error(`${context} must define "${key}" as a non-empty string array.`);
132+ }
133+ return field.map((entry) => (entry as string).trim());
134+}
135+
136+function decodeCustomNotices(value: unknown): ReadonlyArray<CustomNoticeConfigEntry> {
137+ if (value === undefined) return [];
138+ if (!Array.isArray(value)) {
139+ throw new Error('Third-party license config field "customNotices" must be an array.');
140+ }
141+ return value.map((entry, index) => {
142+ const context = `Third-party custom notice at index ${String(index)}`;
143+ if (!isRecord(entry)) throw new Error(`${context} must be an object.`);
144+ const version = readOptionalString(entry, "version", context);
145+ const sourceUrl = readOptionalString(entry, "sourceUrl", context);
146+ const bundles = readOptionalStringArray(entry, "bundles", context);
147+ return {
148+ name: readRequiredString(entry, "name", context),
149+ license: readRequiredString(entry, "license", context),
150+ noticeFile: readRequiredString(entry, "noticeFile", context),
151+ ...(version !== undefined ? { version } : {}),
152+ ...(sourceUrl !== undefined ? { sourceUrl } : {}),
153+ ...(bundles !== undefined ? { bundles } : {}),
154+ };
155+ });
156+}
157+
158+function decodePackageOverrides(value: unknown): ReadonlyArray<PackageNoticeOverrideConfigEntry> {
159+ if (value === undefined) return [];
160+ if (!Array.isArray(value)) {
161+ throw new Error('Third-party license config field "packageOverrides" must be an array.');
162+ }
163+ return value.map((entry, index) => {
164+ const context = `Third-party package override at index ${String(index)}`;
165+ if (!isRecord(entry)) throw new Error(`${context} must be an object.`);
166+ const name = readOptionalString(entry, "name", context);
167+ const repositoryUrl = readOptionalString(entry, "repositoryUrl", context);
168+ if ((name === undefined) === (repositoryUrl === undefined)) {
169+ throw new Error(`${context} must define exactly one of "name" or "repositoryUrl".`);
170+ }
171+ const version = readOptionalString(entry, "version", context);
172+ const license = readOptionalString(entry, "license", context);
173+ const noticeFile = readOptionalString(entry, "noticeFile", context);
174+ const sourceUrl = readOptionalString(entry, "sourceUrl", context);
175+ return {
176+ ...(name !== undefined ? { name } : {}),
177+ ...(repositoryUrl !== undefined ? { repositoryUrl } : {}),
178+ ...(version !== undefined ? { version } : {}),
179+ ...(license !== undefined ? { license } : {}),
180+ ...(noticeFile !== undefined ? { noticeFile } : {}),
181+ ...(sourceUrl !== undefined ? { sourceUrl } : {}),
182+ };
183+ });
184+}
185+
186+async function readConfig(configFile: string | URL | undefined): Promise<{
187+ readonly config: ThirdPartyLicensesConfig;
188+ readonly directory: string;
189+}> {
190+ if (configFile === undefined) {
191+ return { config: EMPTY_CONFIG, directory: NodePath.resolve(".") };
192+ }
193+ const configPath = asPath(configFile);
194+ const source = await NodeFSP.readFile(configPath, "utf8");
195+ const decoded = JSON.parse(source) as unknown;
196+ if (!isRecord(decoded)) throw new Error("Third-party license config must contain an object.");
197+ return {
198+ config: {
199+ customNotices: decodeCustomNotices(decoded.customNotices),
200+ packageOverrides: decodePackageOverrides(decoded.packageOverrides),
201+ },
202+ directory: NodePath.dirname(configPath),
203+ };
204+}
205+
206+async function readPackageJson(packageJsonPath: string): Promise<PackageJson> {
207+ const source = await NodeFSP.readFile(packageJsonPath, "utf8");
208+ const value = JSON.parse(source) as unknown;
209+ if (!isRecord(value)) throw new Error(`Package manifest is not an object: ${packageJsonPath}`);
210+ return value as PackageJson;
211+}
212+
213+function packageIdentity(packageJson: PackageJson, packageRoot: string): string {
214+ const name =
215+ typeof packageJson.name === "string" ? packageJson.name : NodePath.basename(packageRoot);
216+ const version = typeof packageJson.version === "string" ? packageJson.version : "unknown";
217+ return `${name}@${version}`;
218+}
219+
220+async function findPackageRoot(
221+ resolvedPath: string,
222+ expectedName?: string,
223+): Promise<{ readonly packageJson: PackageJson; readonly packageRoot: string } | null> {
224+ let current = NodePath.dirname(resolvedPath);
225+ const root = NodePath.parse(current).root;
226+
227+ while (current !== root) {
228+ const packageJsonPath = NodePath.join(current, "package.json");
229+ try {
230+ const packageJson = await readPackageJson(packageJsonPath);
231+ const matchesExpectedPackage =
232+ expectedName !== undefined
233+ ? packageJson.name === expectedName
234+ : typeof packageJson.name === "string" && typeof packageJson.version === "string";
235+ if (matchesExpectedPackage) {
236+ return { packageJson, packageRoot: await NodeFSP.realpath(current) };
237+ }
238+ } catch (error) {
239+ const code = isRecord(error) && typeof error.code === "string" ? error.code : null;
240+ if (code !== "ENOENT" && code !== "ENOTDIR") throw error;
241+ }
242+ current = NodePath.dirname(current);
243+ }
244+ return null;
245+}
246+
247+async function resolveDependencyPackage(
248+ dependencyName: string,
249+ fromPackageJsonPath: string,
250+): Promise<{ readonly packageJson: PackageJson; readonly packageRoot: string } | null> {
251+ const requireFromPackage = NodeModule.createRequire(fromPackageJsonPath);
252+ const candidates = [`${dependencyName}/package.json`, dependencyName];
253+ for (const candidate of candidates) {
254+ try {
255+ const resolved = requireFromPackage.resolve(candidate);
256+ const found = await findPackageRoot(resolved, dependencyName);
257+ if (found) return found;
258+ } catch (error) {
259+ const code = isRecord(error) && typeof error.code === "string" ? error.code : null;
260+ if (code !== "MODULE_NOT_FOUND" && code !== "ERR_PACKAGE_PATH_NOT_EXPORTED") throw error;
261+ }
262+ }
263+ return null;
264+}
265+
266+function dependencyNames(packageJson: PackageJson): ReadonlyArray<string> {
267+ return [
268+ ...new Set([
269+ ...Object.keys(packageJson.dependencies ?? {}),
270+ ...Object.keys(packageJson.optionalDependencies ?? {}),
271+ ]),
272+ ].sort((left, right) => left.localeCompare(right));
273+}
274+
275+async function collectProductionDependencyPackages(
276+ packageManifests: ReadonlyArray<ThirdPartyLicensePackageManifest>,
277+): Promise<PackageCollection> {
278+ const collection: PackageCollection = {
279+ byIdentity: new Map(),
280+ };
281+ const visited = new Set<string>();
282+
283+ const visitManifest = async (packageJsonPath: string, bundle: string): Promise<void> => {
284+ const packageJson = await readPackageJson(packageJsonPath);
285+ for (const dependencyName of dependencyNames(packageJson)) {
286+ const resolved = await resolveDependencyPackage(dependencyName, packageJsonPath);
287+ if (!resolved) continue;
288+ const visitKey = `${bundle}:${resolved.packageRoot}`;
289+ if (visited.has(visitKey)) continue;
290+ visited.add(visitKey);
291+
292+ const dependencyPackageJsonPath = NodePath.join(resolved.packageRoot, "package.json");
293+ const name =
294+ typeof resolved.packageJson.name === "string" ? resolved.packageJson.name : dependencyName;
295+ if (!name.startsWith(FIRST_PARTY_PACKAGE_PREFIX)) {
296+ const identity = packageIdentity(resolved.packageJson, resolved.packageRoot);
297+ const existing = collection.byIdentity.get(identity);
298+ if (existing) {
299+ existing.bundles.add(bundle);
300+ } else {
301+ collection.byIdentity.set(identity, {
302+ bundles: new Set([bundle]),
303+ packageJson: resolved.packageJson,
304+ packageRoot: resolved.packageRoot,
305+ });
306+ }
307+ }
308+
309+ await visitManifest(dependencyPackageJsonPath, bundle);
310+ }
311+ };
312+
313+ for (const manifest of packageManifests) {
314+ await visitManifest(asPath(manifest.path), manifest.bundle);
315+ }
316+ return collection;
317+}
318+
319+function moduleFilePath(moduleId: string): string | null {
320+ if (moduleId.startsWith("\0") || moduleId.includes("\0")) return null;
321+ const withoutQuery = moduleId.split(/[?#]/, 1)[0] ?? moduleId;
322+ const filePath = withoutQuery.startsWith("file:")
323+ ? NodeURL.fileURLToPath(withoutQuery)
324+ : withoutQuery.replace(/^\/@fs\//, "/");
325+ const normalized = filePath.replaceAll("\\", "/");
326+ return normalized.includes("/node_modules/") ? filePath : null;
327+}
328+
329+async function addBundledModulePackages(
330+ collection: PackageCollection,
331+ moduleIds: ReadonlyArray<string>,
332+ bundle: string,
333+): Promise<void> {
334+ for (const moduleId of moduleIds) {
335+ const filePath = moduleFilePath(moduleId);
336+ if (!filePath) continue;
337+ let found: Awaited<ReturnType<typeof findPackageRoot>>;
338+ try {
339+ found = await findPackageRoot(await NodeFSP.realpath(filePath));
340+ } catch (error) {
341+ const code = isRecord(error) && typeof error.code === "string" ? error.code : null;
342+ if (code === "ENOENT" || code === "ENOTDIR") continue;
343+ throw error;
344+ }
345+ if (!found || typeof found.packageJson.name !== "string") continue;
346+ if (found.packageJson.name.startsWith(FIRST_PARTY_PACKAGE_PREFIX)) continue;
347+ const identity = packageIdentity(found.packageJson, found.packageRoot);
348+ const existing = collection.byIdentity.get(identity);
349+ if (existing) {
350+ existing.bundles.add(bundle);
351+ } else {
352+ collection.byIdentity.set(identity, {
353+ bundles: new Set([bundle]),
354+ packageJson: found.packageJson,
355+ packageRoot: found.packageRoot,
356+ });
357+ }
358+ }
359+}
360+
361+function normalizeLicense(packageJson: PackageJson): string | null {
362+ if (typeof packageJson.license === "string" && packageJson.license.trim().length > 0) {
363+ return packageJson.license.trim();
364+ }
365+ if (isRecord(packageJson.license) && typeof packageJson.license.type === "string") {
366+ return packageJson.license.type.trim() || null;
367+ }
368+ if (Array.isArray(packageJson.licenses)) {
369+ const licenses = packageJson.licenses
370+ .map((entry) => {
371+ if (typeof entry === "string") return entry.trim();
372+ if (isRecord(entry) && typeof entry.type === "string") return entry.type.trim();
373+ return "";
374+ })
375+ .filter((entry) => entry.length > 0);
376+ if (licenses.length > 0) return licenses.join(" OR ");
377+ }
378+ return null;
379+}
380+
381+function normalizeRepositoryUrl(value: unknown): string | null {
382+ const raw =
383+ typeof value === "string"
384+ ? value
385+ : isRecord(value) && typeof value.url === "string"
386+ ? value.url
387+ : null;
388+ if (!raw) return null;
389+ const trimmed = raw.trim();
390+ if (trimmed.startsWith("github:")) return `https://github.com/${trimmed.slice(7)}`;
391+ const normalized = trimmed
392+ .replace(/^git\+ssh:\/\/git@github\.com\//, "https://github.com/")
393+ .replace(/^git\+/, "")
394+ .replace(/^git@github\.com:/, "https://github.com/")
395+ .replace(/^ssh:\/\/(?:git@)?github\.com\//, "https://github.com/")
396+ .replace(/^git:\/\/github\.com\//, "https://github.com/")
397+ .replace(/\.git$/, "");
398+ if (/^[\w.-]+\/[\w.-]+$/.test(normalized)) return `https://github.com/${normalized}`;
399+ return normalized;
400+}
401+
402+function packageSourceUrl(packageJson: PackageJson): string | null {
403+ if (typeof packageJson.homepage === "string" && packageJson.homepage.trim().length > 0) {
404+ return packageJson.homepage.trim();
405+ }
406+ return normalizeRepositoryUrl(packageJson.repository);
407+}
408+
409+async function readPackageNoticeText(packageRoot: string): Promise<string | null> {
410+ const noticeFiles: string[] = [];
411+ const rootEntries = await NodeFSP.readdir(packageRoot, { withFileTypes: true });
412+ noticeFiles.push(
413+ ...rootEntries
414+ .filter((entry) => entry.isFile() && NOTICE_FILE_PATTERN.test(entry.name))
415+ .map((entry) => entry.name),
416+ );
417+
418+ const collectNestedNoticeFiles = async (directory: string, depth: number): Promise<void> => {
419+ const directoryEntries = await NodeFSP.readdir(NodePath.join(packageRoot, directory), {
420+ withFileTypes: true,
421+ });
422+ await Promise.all(
423+ directoryEntries.map(async (entry) => {
424+ const relativePath = NodePath.join(directory, entry.name);
425+ if (entry.isFile() && NOTICE_FILE_PATTERN.test(entry.name)) {
426+ noticeFiles.push(relativePath);
427+ return;
428+ }
429+ if (
430+ depth > 0 &&
431+ entry.isDirectory() &&
432+ entry.name !== "node_modules" &&
433+ entry.name !== ".git"
434+ ) {
435+ await collectNestedNoticeFiles(relativePath, depth - 1);
436+ }
437+ }),
438+ );
439+ };
440+ if (noticeFiles.length === 0) {
441+ await Promise.all(
442+ rootEntries
443+ .filter(
444+ (entry) => entry.isDirectory() && entry.name !== "node_modules" && entry.name !== ".git",
445+ )
446+ .map((entry) => collectNestedNoticeFiles(entry.name, 2)),
447+ );
448+ }
449+ noticeFiles.sort((left, right) => left.localeCompare(right));
450+ if (noticeFiles.length === 0) return null;
451+
452+ const sections: string[] = [];
453+ for (const fileName of noticeFiles) {
454+ const contents = (await NodeFSP.readFile(NodePath.join(packageRoot, fileName), "utf8")).trim();
455+ if (contents.length === 0) continue;
456+ sections.push(noticeFiles.length === 1 ? contents : `${fileName}\n\n${contents}`);
457+ }
458+ return sections.length > 0 ? sections.join("\n\n---\n\n") : null;
459+}
460+
461+function repositoryNoticeKey(packageJson: PackageJson, license: string): string | null {
462+ const repositoryUrl = normalizeRepositoryUrl(packageJson.repository);
463+ return repositoryUrl
464+ ? `${repositoryUrl.toLocaleLowerCase()}\n${license.toLocaleLowerCase()}`
465+ : null;
466+}
467+
468+async function collectRepositoryNotices(
469+ collection: PackageCollection,
470+ packageNotices: Map<string, Promise<string | null>>,
471+): Promise<ReadonlyMap<string, string>> {
472+ const notices = new Map<string, string>();
473+ await Promise.all(
474+ [...collection.byIdentity.values()].map(async (collected) => {
475+ const license = normalizeLicense(collected.packageJson);
476+ if (!license) return;
477+ const key = repositoryNoticeKey(collected.packageJson, license);
478+ if (!key || notices.has(key)) return;
479+ const noticeText = await packageNoticeText(collected.packageRoot, packageNotices);
480+ if (noticeText) notices.set(key, noticeText);
481+ }),
482+ );
483+ return notices;
484+}
485+
486+function packageNoticeText(
487+ packageRoot: string,
488+ cache: Map<string, Promise<string | null>>,
489+): Promise<string | null> {
490+ const existing = cache.get(packageRoot);
491+ if (existing) return existing;
492+ const notice = readPackageNoticeText(packageRoot);
493+ cache.set(packageRoot, notice);
494+ return notice;
495+}
496+
497+function findPackageOverride(
498+ overrides: ReadonlyArray<PackageNoticeOverrideConfigEntry>,
499+ name: string,
500+ version: string,
501+ packageJson: PackageJson,
502+): PackageNoticeOverrideConfigEntry | undefined {
503+ const matchingVersion = (override: PackageNoticeOverrideConfigEntry) =>
504+ override.version === undefined || override.version === version;
505+ const repositoryUrl = normalizeRepositoryUrl(packageJson.repository)?.toLocaleLowerCase();
506+ return (
507+ overrides.find((override) => override.name === name && override.version === version) ??
508+ overrides.find((override) => override.name === name && override.version === undefined) ??
509+ overrides.find(
510+ (override) =>
511+ repositoryUrl !== undefined &&
512+ override.repositoryUrl !== undefined &&
513+ normalizeRepositoryUrl(override.repositoryUrl)?.toLocaleLowerCase() === repositoryUrl &&
514+ matchingVersion(override),
515+ )
516+ );
517+}
518+
519+async function packageEntry(
520+ collected: CollectedPackage,
521+ config: ThirdPartyLicensesConfig,
522+ configDirectory: string,
523+ packageNotices: Map<string, Promise<string | null>>,
524+ repositoryNotices: ReadonlyMap<string, string>,
525+): Promise<ThirdPartyLicenseEntry> {
526+ const name =
527+ typeof collected.packageJson.name === "string"
528+ ? collected.packageJson.name
529+ : NodePath.basename(collected.packageRoot);
530+ const version =
531+ typeof collected.packageJson.version === "string" ? collected.packageJson.version : "unknown";
532+ const override = findPackageOverride(
533+ config.packageOverrides,
534+ name,
535+ version,
536+ collected.packageJson,
537+ );
538+ const license = override?.license ?? normalizeLicense(collected.packageJson);
539+ if (!license || /^(?:unlicensed|proprietary)$/i.test(license)) {
540+ throw new Error(
541+ `${name}@${version} does not declare a distributable license. Add a package override in the third-party license config if the package publishes its notice elsewhere.`,
542+ );
543+ }
544+
545+ const repositoryKey = repositoryNoticeKey(collected.packageJson, license);
546+ const noticeText = override?.noticeFile
547+ ? (
548+ await NodeFSP.readFile(NodePath.resolve(configDirectory, override.noticeFile), "utf8")
549+ ).trim()
550+ : ((await packageNoticeText(collected.packageRoot, packageNotices)) ??
551+ (repositoryKey ? repositoryNotices.get(repositoryKey) : undefined));
552+ if (!noticeText) {
553+ throw new Error(
554+ `${name}@${version} does not include a license or notice file. Add a package override with "noticeFile" in the third-party license config.`,
555+ );
556+ }
557+
558+ return {
559+ bundles: [...collected.bundles].sort((left, right) => left.localeCompare(right)),
560+ kind: "package",
561+ license,
562+ name,
563+ noticeText,
564+ sourceUrl: override?.sourceUrl ?? packageSourceUrl(collected.packageJson),
565+ version,
566+ };
567+}
568+
569+async function customEntries(
570+ config: ThirdPartyLicensesConfig,
571+ configDirectory: string,
572+ includedBundles: ReadonlySet<string>,
573+): Promise<ReadonlyArray<ThirdPartyLicenseEntry>> {
574+ return Promise.all(
575+ config.customNotices
576+ .filter(
577+ (notice) =>
578+ notice.bundles === undefined ||
579+ notice.bundles.some((bundle) => includedBundles.has(bundle)),
580+ )
581+ .map(async (notice) => {
582+ const noticeText = (
583+ await NodeFSP.readFile(NodePath.resolve(configDirectory, notice.noticeFile), "utf8")
584+ ).trim();
585+ if (noticeText.length === 0) {
586+ throw new Error(`Custom third-party notice "${notice.name}" is empty.`);
587+ }
588+ return {
589+ bundles: [...(notice.bundles ?? ["assets"])].sort((left, right) =>
590+ left.localeCompare(right),
591+ ),
592+ kind: "custom" as const,
593+ license: notice.license,
594+ name: notice.name,
595+ noticeText,
596+ sourceUrl: notice.sourceUrl ?? null,
597+ version: notice.version ?? null,
598+ };
599+ }),
600+ );
601+}
602+
603+function entrySort(left: ThirdPartyLicenseEntry, right: ThirdPartyLicenseEntry): number {
604+ return (
605+ left.name.localeCompare(right.name) ||
606+ (left.version ?? "").localeCompare(right.version ?? "") ||
607+ left.kind.localeCompare(right.kind)
608+ );
609+}
610+
611+export async function generateThirdPartyLicenseManifest(input: {
612+ readonly configFile?: string | URL;
613+ readonly packageManifests: ReadonlyArray<ThirdPartyLicensePackageManifest>;
614+ readonly bundledModuleIds?: ReadonlyArray<string>;
615+ readonly bundleName?: string;
616+}): Promise<ThirdPartyLicenseManifest> {
617+ const [{ config, directory }, collection] = await Promise.all([
618+ readConfig(input.configFile),
619+ collectProductionDependencyPackages(input.packageManifests),
620+ ]);
621+ if (input.bundledModuleIds && input.bundleName) {
622+ await addBundledModulePackages(collection, input.bundledModuleIds, input.bundleName);
623+ }
624+
625+ const packageNotices = new Map<string, Promise<string | null>>();
626+ const repositoryNotices = await collectRepositoryNotices(collection, packageNotices);
627+
628+ const packageEntryResults = await Promise.allSettled(
629+ [...collection.byIdentity.values()].map((collected) =>
630+ packageEntry(collected, config, directory, packageNotices, repositoryNotices),
631+ ),
632+ );
633+ const failures = packageEntryResults.flatMap((result) =>
634+ result.status === "rejected"
635+ ? [result.reason instanceof Error ? result.reason.message : String(result.reason)]
636+ : [],
637+ );
638+ if (failures.length > 0) {
639+ throw new Error(
640+ `Third-party license generation found ${String(failures.length)} invalid package notice${failures.length === 1 ? "" : "s"}:\n${failures.map((failure) => `- ${failure}`).join("\n")}`,
641+ );
642+ }
643+ const packageEntries = packageEntryResults.flatMap((result) =>
644+ result.status === "fulfilled" ? [result.value] : [],
645+ );
646+ const includedBundles = new Set(input.packageManifests.map((manifest) => manifest.bundle));
647+ if (input.bundleName) includedBundles.add(input.bundleName);
648+ const manualEntries = await customEntries(config, directory, includedBundles);
649+ return {
650+ schemaVersion: 1,
651+ entries: [...packageEntries, ...manualEntries].sort(entrySort),
652+ };
653+}
654+
655+function moduleIdsFromBundle(bundle: unknown): ReadonlyArray<string> {
656+ const ids = new Set<string>();
657+ if (!isRecord(bundle)) return [];
658+ for (const output of Object.values(bundle)) {
659+ if (!isRecord(output) || output.type !== "chunk" || !isRecord(output.modules)) continue;
660+ for (const id of Object.keys(output.modules)) ids.add(id);
661+ }
662+ return [...ids];
663+}
664+
665+function serializeManifest(manifest: ThirdPartyLicenseManifest): string {
666+ return `${JSON.stringify(manifest)}\n`;
667+}
668+
669+export function thirdPartyLicensesPlugin(options: ThirdPartyLicensesPluginOptions): Plugin {
670+ return {
671+ name: "t3code:third-party-licenses",
672+ configureServer(server) {
673+ let manifestPromise: Promise<ThirdPartyLicenseManifest> | null = null;
674+ server.middlewares.use((request, response, next) => {
675+ if (request.url?.split("?", 1)[0] !== `/${THIRD_PARTY_LICENSES_FILE_NAME}`) {
676+ next();
677+ return;
678+ }
679+ manifestPromise ??= generateThirdPartyLicenseManifest({
680+ packageManifests: options.packageManifests,
681+ ...(options.configFile !== undefined ? { configFile: options.configFile } : {}),
682+ });
683+ void manifestPromise.then(
684+ (manifest) => {
685+ response.statusCode = 200;
686+ response.setHeader("Content-Type", "application/json; charset=utf-8");
687+ response.setHeader("Cache-Control", "no-store");
688+ response.end(serializeManifest(manifest));
689+ },
690+ (error: unknown) => {
691+ next(error instanceof Error ? error : new Error(String(error)));
692+ },
693+ );
694+ });
695+ },
696+ async generateBundle(_outputOptions, bundle) {
697+ const manifest = await generateThirdPartyLicenseManifest({
698+ packageManifests: options.packageManifests,
699+ bundledModuleIds: moduleIdsFromBundle(bundle),
700+ bundleName: options.bundleName,
701+ ...(options.configFile !== undefined ? { configFile: options.configFile } : {}),
702+ });
703+ this.emitFile({
704+ type: "asset",
705+ fileName: THIRD_PARTY_LICENSES_FILE_NAME,
706+ source: serializeManifest(manifest),
707+ });
708+ },
709+ };
710+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t