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