| 1 | import data from "../data/desktop-published-release.json"; |
| 2 | |
| 3 | export interface MacosAssetFact { |
| 4 | name: string; |
| 5 | platform: "macos"; |
| 6 | size: number; |
| 7 | sha256: string; |
| 8 | signed: true; |
| 9 | notarized: true; |
| 10 | url: string; |
| 11 | } |
| 12 | |
| 13 | export interface AptSource { |
| 14 | repository: string; |
| 15 | keyUrl: string; |
| 16 | suite: string; |
| 17 | component: string; |
| 18 | architecture: AptArchitecture; |
| 19 | package: string; |
| 20 | keyFingerprint: string; |
| 21 | } |
| 22 | |
| 23 | export const APT_ARCHITECTURES = ["amd64", "arm64"] as const; |
| 24 | export type AptArchitecture = (typeof APT_ARCHITECTURES)[number]; |
| 25 | |
| 26 | export interface LinuxDebAssetFact { |
| 27 | name: string; |
| 28 | platform: "linux"; |
| 29 | kind: "deb"; |
| 30 | arch: "x64"; |
| 31 | size: number; |
| 32 | sha256: string; |
| 33 | url: string; |
| 34 | apt: AptSource; |
| 35 | } |
| 36 | |
| 37 | export interface LinuxAppImageAssetFact { |
| 38 | name: string; |
| 39 | platform: "linux"; |
| 40 | kind: "appimage"; |
| 41 | arch: "x64"; |
| 42 | size: number; |
| 43 | sha256: string; |
| 44 | url: string; |
| 45 | } |
| 46 | |
| 47 | export type LinuxAssetFact = LinuxDebAssetFact | LinuxAppImageAssetFact; |
| 48 | export type DesktopAssetFact = MacosAssetFact | LinuxAssetFact; |
| 49 | |
| 50 | export interface DesktopReleaseFact { |
| 51 | tag: string; |
| 52 | version: string; |
| 53 | publishedAt: string; |
| 54 | url: string; |
| 55 | readAt: string; |
| 56 | assets: DesktopAssetFact[]; |
| 57 | } |
| 58 | |
| 59 | export type DesktopArch = "arm64" | "x64"; |
| 60 | |
| 61 | export interface LinuxDownloads { |
| 62 | version: string; |
| 63 | apt: AptSource | null; |
| 64 | appImage: { name: string; size: number; sha256: string; url: string } | null; |
| 65 | } |
| 66 | |
| 67 | export interface DesktopDownload { |
| 68 | version: string; |
| 69 | arch: DesktopArch | null; |
| 70 | name: string; |
| 71 | size: number; |
| 72 | sha256: string; |
| 73 | url: string; |
| 74 | } |
| 75 | |
| 76 | const DOWNLOAD_ORIGINS = ["https://github.com/codewhale-hq/", "https://downloads.codewhale.net/"]; |
| 77 | |
| 78 | function isRecord(value: unknown): value is Record<string, unknown> { |
| 79 | return value !== null && typeof value === "object" && !Array.isArray(value); |
| 80 | } |
| 81 | |
| 82 | function isDate(value: unknown): value is string { |
| 83 | return typeof value === "string" && Number.isFinite(Date.parse(value)); |
| 84 | } |
| 85 | |
| 86 | function isDownloadUrl(value: unknown): value is string { |
| 87 | return typeof value === "string" && DOWNLOAD_ORIGINS.some((origin) => value.startsWith(origin)); |
| 88 | } |
| 89 | |
| 90 | const SAFE_URL = /^https:\/\/[A-Za-z0-9.-]+(?::\d+)?(?:\/[A-Za-z0-9._~%+\-/]*)?$/; |
| 91 | const APT_TOKEN = /^[a-z0-9][a-z0-9+.-]*$/; |
| 92 | const KEY_FILE_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/; |
| 93 | const KEY_FINGERPRINT = /^[0-9A-F]{40}$/; |
| 94 | const REPOSITORY_ORIGIN = "https://downloads.codewhale.net/"; |
| 95 | |
| 96 | function hasExactKeys(value: Record<string, unknown>, keys: readonly string[]): boolean { |
| 97 | const actual = Object.keys(value); |
| 98 | return actual.length === keys.length && keys.every((key) => actual.includes(key)); |
| 99 | } |
| 100 | |
| 101 | function hasFileFacts(value: Record<string, unknown>): boolean { |
| 102 | return ( |
| 103 | typeof value.name === "string" && |
| 104 | value.name.length > 0 && |
| 105 | typeof value.size === "number" && |
| 106 | Number.isSafeInteger(value.size) && |
| 107 | value.size > 0 && |
| 108 | typeof value.sha256 === "string" && |
| 109 | /^[0-9a-f]{64}$/.test(value.sha256) && |
| 110 | isDownloadUrl(value.url) |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | export function aptKeyFileName(keyUrl: string): string | null { |
| 115 | const name = keyUrl.slice(keyUrl.lastIndexOf("/") + 1); |
| 116 | return KEY_FILE_NAME.test(name) ? name : null; |
| 117 | } |
| 118 | |
| 119 | function isAptSource(value: unknown): value is AptSource { |
| 120 | return ( |
| 121 | isRecord(value) && |
| 122 | hasExactKeys(value, ["repository", "keyUrl", "suite", "component", "architecture", "package", "keyFingerprint"]) && |
| 123 | typeof value.repository === "string" && |
| 124 | value.repository.startsWith(REPOSITORY_ORIGIN) && |
| 125 | SAFE_URL.test(value.repository) && |
| 126 | typeof value.keyUrl === "string" && |
| 127 | SAFE_URL.test(value.keyUrl) && |
| 128 | aptKeyFileName(value.keyUrl) !== null && |
| 129 | typeof value.suite === "string" && |
| 130 | APT_TOKEN.test(value.suite) && |
| 131 | typeof value.component === "string" && |
| 132 | APT_TOKEN.test(value.component) && |
| 133 | typeof value.architecture === "string" && |
| 134 | (APT_ARCHITECTURES as readonly string[]).includes(value.architecture) && |
| 135 | typeof value.package === "string" && |
| 136 | APT_TOKEN.test(value.package) && |
| 137 | typeof value.keyFingerprint === "string" && |
| 138 | KEY_FINGERPRINT.test(value.keyFingerprint) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | function isMacosAsset(value: Record<string, unknown>): boolean { |
| 143 | return value.platform === "macos" && hasFileFacts(value) && value.signed === true && value.notarized === true; |
| 144 | } |
| 145 | |
| 146 | function isLinuxAsset(value: Record<string, unknown>): boolean { |
| 147 | if (value.platform !== "linux" || value.arch !== "x64" || !hasFileFacts(value)) return false; |
| 148 | const base = ["name", "platform", "kind", "arch", "size", "sha256", "url"]; |
| 149 | if (value.kind === "deb") return hasExactKeys(value, [...base, "apt"]) && isAptSource(value.apt); |
| 150 | if (value.kind === "appimage") return hasExactKeys(value, base); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | function isDesktopAsset(value: unknown): value is DesktopAssetFact { |
| 155 | return isRecord(value) && (isMacosAsset(value) || isLinuxAsset(value)); |
| 156 | } |
| 157 | |
| 158 | export function parseDesktopRelease(value: unknown): DesktopReleaseFact | null { |
| 159 | if (!isRecord(value)) return null; |
| 160 | if ( |
| 161 | typeof value.tag !== "string" || |
| 162 | !/^v\d+\.\d+\.\d+$/.test(value.tag) || |
| 163 | value.version !== value.tag.slice(1) || |
| 164 | !isDate(value.publishedAt) || |
| 165 | !isDate(value.readAt) || |
| 166 | !isDownloadUrl(value.url) || |
| 167 | !Array.isArray(value.assets) || |
| 168 | !value.assets.every(isDesktopAsset) |
| 169 | ) { |
| 170 | return null; |
| 171 | } |
| 172 | return value as unknown as DesktopReleaseFact; |
| 173 | } |
| 174 | |
| 175 | export const DESKTOP_RELEASE: DesktopReleaseFact | null = parseDesktopRelease(data); |
| 176 | |
| 177 | function assetArch(name: string): DesktopArch | null { |
| 178 | if (/[-_.]arm64[-_.]/.test(name)) return "arm64"; |
| 179 | if (/[-_.]x64[-_.]/.test(name)) return "x64"; |
| 180 | return null; |
| 181 | } |
| 182 | |
| 183 | export function desktopDownloads(release: DesktopReleaseFact | null = DESKTOP_RELEASE): DesktopDownload[] { |
| 184 | if (!release) return []; |
| 185 | const seen = new Set<DesktopArch | null>(); |
| 186 | const downloads: DesktopDownload[] = []; |
| 187 | for (const asset of release.assets) { |
| 188 | const arch = assetArch(asset.name); |
| 189 | if (asset.platform !== "macos" || seen.has(arch)) continue; |
| 190 | seen.add(arch); |
| 191 | downloads.push({ version: release.version, arch, name: asset.name, size: asset.size, sha256: asset.sha256, url: asset.url }); |
| 192 | } |
| 193 | return downloads.sort((a, b) => Number(a.arch === "x64") - Number(b.arch === "x64")); |
| 194 | } |
| 195 | |
| 196 | export function desktopDownload(release: DesktopReleaseFact | null = DESKTOP_RELEASE): DesktopDownload | null { |
| 197 | return desktopDownloads(release)[0] ?? null; |
| 198 | } |
| 199 | |
| 200 | export function linuxDownloads(release: DesktopReleaseFact | null = DESKTOP_RELEASE): LinuxDownloads | null { |
| 201 | if (!release) return null; |
| 202 | let apt: AptSource | null = null; |
| 203 | let appImage: LinuxDownloads["appImage"] = null; |
| 204 | for (const asset of release.assets) { |
| 205 | if (asset.platform !== "linux") continue; |
| 206 | if (asset.kind === "deb") apt ??= asset.apt; |
| 207 | else appImage ??= { name: asset.name, size: asset.size, sha256: asset.sha256, url: asset.url }; |
| 208 | } |
| 209 | return apt || appImage ? { version: release.version, apt, appImage } : null; |
| 210 | } |
| 211 |