Open History to see the last commit
1import { mFallback, msg } from "gt-i18n";
2import type { M } from "gt-i18n";
3
4export interface ThirdPartyLicenseEntry {
5 readonly bundles: ReadonlyArray<string>;
6 readonly kind: "custom" | "package";
7 readonly license: string;
8 readonly name: string;
9 readonly noticeText: string;
10 readonly sourceUrl: string | null;
11 readonly version: string | null;
12}
13
14export interface ThirdPartyLicenseManifest {
15 readonly schemaVersion: 1;
16 readonly entries: ReadonlyArray<ThirdPartyLicenseEntry>;
17}
18
19function isRecord(value: unknown): value is Record<string, unknown> {
20 return typeof value === "object" && value !== null && !Array.isArray(value);
21}
22
23function isStringArray(value: unknown): value is ReadonlyArray<string> {
24 return Array.isArray(value) && value.every((entry) => typeof entry === "string");
25}
26
27function isHttpUrl(value: unknown): value is string {
28 if (typeof value !== "string") return false;
29 try {
30 const protocol = new URL(value).protocol;
31 return protocol === "http:" || protocol === "https:";
32 } catch {
33 return false;
34 }
35}
36
37function decodeEntry(value: unknown, index: number, resolve: M): ThirdPartyLicenseEntry {
38 if (!isRecord(value)) {
39 throw new Error(resolve(msg`License entry ${{ number: String(index + 1) }} is not an object.`));
40 }
41 if (
42 !isStringArray(value.bundles) ||
43 value.bundles.length === 0 ||
44 (value.kind !== "custom" && value.kind !== "package") ||
45 typeof value.license !== "string" ||
46 typeof value.name !== "string" ||
47 typeof value.noticeText !== "string" ||
48 (value.sourceUrl !== null && !isHttpUrl(value.sourceUrl)) ||
49 (value.version !== null && typeof value.version !== "string")
50 ) {
51 throw new Error(
52 resolve(msg`License entry ${{ number: String(index + 1) }} has an invalid shape.`),
53 );
54 }
55 return {
56 bundles: value.bundles,
57 kind: value.kind,
58 license: value.license,
59 name: value.name,
60 noticeText: value.noticeText,
61 sourceUrl: value.sourceUrl,
62 version: value.version,
63 };
64}
65
66export function decodeThirdPartyLicenseManifest(
67 value: unknown,
68 resolve: M = mFallback,
69): ThirdPartyLicenseManifest {
70 if (!isRecord(value) || value.schemaVersion !== 1 || !Array.isArray(value.entries)) {
71 throw new Error(resolve(msg("The open-source license manifest has an unsupported format.")));
72 }
73 const entries = value.entries.map((entry, index) => decodeEntry(entry, index, resolve));
74 const entryKeys = new Set<string>();
75 for (const entry of entries) {
76 const key = thirdPartyLicenseEntryKey(entry);
77 if (entryKeys.has(key)) {
78 throw new Error(
79 resolve(msg`The open-source license manifest contains a duplicate entry: ${{ key }}`),
80 );
81 }
82 entryKeys.add(key);
83 }
84 return {
85 schemaVersion: 1,
86 entries,
87 };
88}
89
90export function filterThirdPartyLicenseEntries(
91 entries: ReadonlyArray<ThirdPartyLicenseEntry>,
92 query: string,
93 resolve: M = mFallback,
94): ReadonlyArray<ThirdPartyLicenseEntry> {
95 const terms = query
96 .trim()
97 .toLowerCase()
98 .split(/\s+/)
99 .filter((term) => term.length > 0);
100 if (terms.length === 0) return entries;
101 return entries.filter((entry) => {
102 const searchable = [
103 entry.name,
104 entry.version,
105 entry.license,
106 ...entry.bundles,
107 formatLicenseBundles(entry.bundles, resolve),
108 ]
109 .filter((value): value is string => value !== null)
110 .join(" ")
111 .toLowerCase();
112 return terms.every((term) => searchable.includes(term));
113 });
114}
115
116const BUNDLE_LABELS: Readonly<Record<string, string>> = {
117 android: "Android",
118 assets: msg("Assets"),
119 desktop: msg("Desktop"),
120 "device-tools": msg("Device tools"),
121 ios: "iOS",
122 mobile: msg("Mobile"),
123 server: msg("Server"),
124 web: msg("Web"),
125};
126
127export function formatLicenseBundles(
128 bundles: ReadonlyArray<string>,
129 resolve: M = mFallback,
130): string {
131 return bundles
132 .map((bundle) =>
133 Object.prototype.hasOwnProperty.call(BUNDLE_LABELS, bundle)
134 ? resolve(BUNDLE_LABELS[bundle]!)
135 : bundle,
136 )
137 .join(", ");
138}
139
140export function thirdPartyLicenseEntryKey(entry: ThirdPartyLicenseEntry): string {
141 return encodeURIComponent(JSON.stringify([entry.kind, entry.name, entry.version]));
142}
143
144export function findThirdPartyLicenseEntry(
145 entries: ReadonlyArray<ThirdPartyLicenseEntry>,
146 key: string,
147): ThirdPartyLicenseEntry | undefined {
148 return entries.find((entry) => thirdPartyLicenseEntryKey(entry) === key);
149}