|
| 1 | +// Server-side loader for /licenses. Combines the build-time static manifest |
| 2 | +// (web + api + built-in integrations) with one group per installed community |
| 3 | +// integration, sourced from the integration's `manifest.json` plus the |
| 4 | +// optional `dist/licenses.json` shipped inside its artifact. Runs on every |
| 5 | +// request so newly-installed integrations show up without rebuilding web. |
| 6 | + |
| 7 | +import { type Dirent, existsSync, readdirSync, readFileSync } from "node:fs"; |
| 8 | +import { join } from "node:path"; |
| 9 | +import { type LicenseNotice, repoPaths } from "@openmapx/core/server"; |
| 10 | + |
| 11 | +export interface LicenseGroup { |
| 12 | + /** `core` for web/api/built-in deps, or the community integration id. */ |
| 13 | + scope: string; |
| 14 | + /** Human-readable label shown in the table heading. */ |
| 15 | + label: string; |
| 16 | + /** Sub-heading describing what this group's notices represent. */ |
| 17 | + description?: string; |
| 18 | + notices: LicenseNotice[]; |
| 19 | +} |
| 20 | + |
| 21 | +export interface LicensesPageData { |
| 22 | + groups: LicenseGroup[]; |
| 23 | + generatedAt: string | null; |
| 24 | + totalCount: number; |
| 25 | +} |
| 26 | + |
| 27 | +interface StaticPayload { |
| 28 | + generatedAt?: string; |
| 29 | + notices?: LicenseNotice[]; |
| 30 | +} |
| 31 | + |
| 32 | +interface CommunityPayload { |
| 33 | + integrationId?: string; |
| 34 | + notices?: LicenseNotice[]; |
| 35 | +} |
| 36 | + |
| 37 | +interface CommunityManifest { |
| 38 | + id?: string; |
| 39 | + name?: string; |
| 40 | + version?: string; |
| 41 | + license?: string | { type?: string; url?: string }; |
| 42 | + author?: string; |
| 43 | + documentation?: string; |
| 44 | + description?: string; |
| 45 | +} |
| 46 | + |
| 47 | +export async function loadLicenseGroups(): Promise<LicensesPageData> { |
| 48 | + const groups: LicenseGroup[] = []; |
| 49 | + let generatedAt: string | null = null; |
| 50 | + |
| 51 | + const staticPayload = await loadStaticManifest(); |
| 52 | + if (staticPayload?.notices?.length) { |
| 53 | + groups.push({ |
| 54 | + scope: "core", |
| 55 | + label: "OpenMapX (web app, API, built-in integrations)", |
| 56 | + notices: staticPayload.notices, |
| 57 | + }); |
| 58 | + generatedAt = staticPayload.generatedAt ?? null; |
| 59 | + } |
| 60 | + |
| 61 | + const seenInCore = new Set(staticPayload?.notices?.map((n) => `${n.name}@${n.version}`) ?? []); |
| 62 | + for (const group of loadCommunityGroups(seenInCore)) { |
| 63 | + groups.push(group); |
| 64 | + } |
| 65 | + |
| 66 | + const totalCount = groups.reduce((sum, g) => sum + g.notices.length, 0); |
| 67 | + return { groups, generatedAt, totalCount }; |
| 68 | +} |
| 69 | + |
| 70 | +async function loadStaticManifest(): Promise<StaticPayload | null> { |
| 71 | + // Static import keeps Next's tracing aware of the file dependency; falling |
| 72 | + // back to a stub if the generator hasn't run yet lets `next dev` boot |
| 73 | + // without a build step. |
| 74 | + try { |
| 75 | + const mod = (await import("@/generated/open-source-licenses.json")) as { |
| 76 | + default?: StaticPayload; |
| 77 | + }; |
| 78 | + return mod.default ?? null; |
| 79 | + } catch { |
| 80 | + return null; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function loadCommunityGroups(skipKeys: Set<string>): LicenseGroup[] { |
| 85 | + const groups: LicenseGroup[] = []; |
| 86 | + let customDir: string; |
| 87 | + try { |
| 88 | + customDir = repoPaths().customIntegrationsDir; |
| 89 | + } catch { |
| 90 | + return groups; |
| 91 | + } |
| 92 | + if (!existsSync(customDir)) return groups; |
| 93 | + |
| 94 | + let entries: Dirent[]; |
| 95 | + try { |
| 96 | + entries = readdirSync(customDir, { withFileTypes: true }); |
| 97 | + } catch { |
| 98 | + return groups; |
| 99 | + } |
| 100 | + |
| 101 | + for (const entry of entries) { |
| 102 | + if (!entry.isDirectory()) continue; |
| 103 | + if (entry.name.startsWith(".") || entry.name.startsWith("_")) continue; |
| 104 | + |
| 105 | + const integrationDir = join(customDir, entry.name); |
| 106 | + const manifest = readCommunityManifest(integrationDir); |
| 107 | + if (!manifest) continue; // Skip half-extracted/staging dirs. |
| 108 | + |
| 109 | + const integrationId = manifest.id ?? entry.name; |
| 110 | + const integrationNotice = integrationSelfNotice(manifest, integrationId); |
| 111 | + |
| 112 | + const bundledNotices = readBundledLicenses(integrationDir).filter( |
| 113 | + (n) => !skipKeys.has(`${n.name}@${n.version}`), |
| 114 | + ); |
| 115 | + |
| 116 | + groups.push({ |
| 117 | + scope: entry.name, |
| 118 | + label: `Community integration · ${manifest.name ?? integrationId}`, |
| 119 | + description: |
| 120 | + bundledNotices.length > 0 |
| 121 | + ? "Integration license and bundled runtime dependencies." |
| 122 | + : "Integration license. No bundled dependency manifest was shipped.", |
| 123 | + notices: [integrationNotice, ...bundledNotices], |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + return groups.sort((a, b) => a.scope.localeCompare(b.scope)); |
| 128 | +} |
| 129 | + |
| 130 | +function readCommunityManifest(dir: string): CommunityManifest | null { |
| 131 | + const path = join(dir, "manifest.json"); |
| 132 | + if (!existsSync(path)) return null; |
| 133 | + try { |
| 134 | + return JSON.parse(readFileSync(path, "utf8")) as CommunityManifest; |
| 135 | + } catch { |
| 136 | + return null; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +function readBundledLicenses(dir: string): LicenseNotice[] { |
| 141 | + const path = join(dir, "dist", "licenses.json"); |
| 142 | + if (!existsSync(path)) return []; |
| 143 | + let raw: string; |
| 144 | + try { |
| 145 | + raw = readFileSync(path, "utf8"); |
| 146 | + } catch { |
| 147 | + return []; |
| 148 | + } |
| 149 | + let payload: CommunityPayload; |
| 150 | + try { |
| 151 | + payload = JSON.parse(raw) as CommunityPayload; |
| 152 | + } catch { |
| 153 | + return []; |
| 154 | + } |
| 155 | + return Array.isArray(payload.notices) ? payload.notices : []; |
| 156 | +} |
| 157 | + |
| 158 | +function integrationSelfNotice(manifest: CommunityManifest, id: string): LicenseNotice { |
| 159 | + let license = "Not specified"; |
| 160 | + let licenseUrl: string | undefined; |
| 161 | + const raw = manifest.license; |
| 162 | + if (typeof raw === "string" && raw.trim()) { |
| 163 | + license = raw.trim(); |
| 164 | + } else if (raw && typeof raw === "object") { |
| 165 | + if (raw.type?.trim()) license = raw.type.trim(); |
| 166 | + if (raw.url) licenseUrl = raw.url; |
| 167 | + } |
| 168 | + return { |
| 169 | + name: id, |
| 170 | + version: manifest.version ?? "?", |
| 171 | + license, |
| 172 | + licenseUrl, |
| 173 | + projectUrl: manifest.documentation, |
| 174 | + }; |
| 175 | +} |
0 commit comments