diff --git a/src/components/MediaWriterDownload/index.tsx b/src/components/MediaWriterDownload/index.tsx new file mode 100644 index 00000000..36befc95 --- /dev/null +++ b/src/components/MediaWriterDownload/index.tsx @@ -0,0 +1,129 @@ +import {useEffect, useState, type ReactNode} from 'react'; +import clsx from 'clsx'; +import Link from '@docusaurus/Link'; +import { + OS_LABEL, + detectPlatform, + formatSize, + parseMediaWriter, + type MediaWriterRelease, + type Platform, +} from '@site/src/data/media-writer'; +import page from '@site/src/css/page.module.css'; +import styles from './styles.module.css'; + +const DOWNLOADS_URL = 'https://iso.tunaos.org/downloads.json'; +const RELEASES_URL = 'https://github.com/tuna-os/iso-builder/releases/latest'; +const BROWSER_BUILDER_URL = 'https://iso.tunaos.org/'; + +const FILE_KIND = {windows: '.msi installer', macos: '.dmg disk image', linux: 'Flatpak bundle'}; + +// The top of /download: one button for the app that matches the visitor's +// computer. The server render has no user agent, so it shows the generic +// release link until the browser detects the platform. +export default function MediaWriterDownload(): ReactNode { + const [platform, setPlatform] = useState(null); + const [release, setRelease] = useState(null); + + useEffect(() => { + setPlatform( + detectPlatform({ + userAgent: navigator.userAgent, + platform: navigator.platform, + maxTouchPoints: navigator.maxTouchPoints, + }), + ); + + // If this fails (offline, blocked, or no CORS header), the buttons keep + // pointing at the GitHub release page. + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 10000); + fetch(DOWNLOADS_URL, {cache: 'no-cache', signal: controller.signal}) + .then((res) => (res.ok ? res.json() : null)) + .then((doc) => setRelease(parseMediaWriter(doc))) + .catch(() => setRelease(null)) + .finally(() => clearTimeout(timeout)); + return () => { + clearTimeout(timeout); + controller.abort(); + }; + }, []); + + if (platform?.kind === 'mobile') { + return ( +
+

+ Install TunaOS from a computer +

+

+ TunaOS installs on a desktop or laptop PC. Open{' '} + tunaos.org/download on the Windows, macOS or Linux computer you + want to use: the page then offers TunaOS Media Writer, which gets the ISO and + writes it to a USB stick. +

+
+ ); + } + + const desktop = platform?.kind === 'windows' || platform?.kind === 'macos' || platform?.kind === 'linux'; + const asset = desktop ? release?.assets[platform.asset] : undefined; + const label = desktop ? `Get TunaOS Media Writer for ${OS_LABEL[platform.kind]}` : 'Get TunaOS Media Writer'; + const meta = [ + release?.version && `Version ${release.version}`, + asset && desktop && FILE_KIND[platform.kind], + asset?.size && formatSize(asset.size), + ].filter(Boolean); + const otherArch = + platform?.kind === 'linux' + ? release?.assets[platform.asset === 'linux-x86_64' ? 'linux-aarch64' : 'linux-x86_64'] + : undefined; + + return ( +
+

+ The easy way: TunaOS Media Writer +

+

+ Pick an image in the app. It downloads or builds the ISO and, if you want, + writes it to a USB stick. +

+
+ + {label} + + + Or build an ISO in your browser + +
+ {meta.length > 0 &&

{meta.join(' · ')}

} + {asset?.signed === false && platform?.kind === 'windows' && ( +

+ The installer is not code-signed yet, so Windows SmartScreen can show a warning. +

+ )} + {asset?.signed === false && platform?.kind === 'macos' && ( +

+ The app is not notarized yet: if macOS blocks it, open System Settings › Privacy + & Security and choose Open Anyway. +

+ )} + +
+ ); +} diff --git a/src/components/MediaWriterDownload/styles.module.css b/src/components/MediaWriterDownload/styles.module.css new file mode 100644 index 00000000..9f0be4d8 --- /dev/null +++ b/src/components/MediaWriterDownload/styles.module.css @@ -0,0 +1,50 @@ +/* The OS-detected Media Writer card at the top of /download. Buttons come + from src/css/page.module.css. */ + +.card { + margin-bottom: 2.5rem; + padding: 1.75rem; + border-radius: 6px; + border-left: 2px solid var(--tu-accent); + background: var(--tu-panel); +} + +.title { + margin: 0; + font-size: 1.3rem; + font-weight: 700; + letter-spacing: -0.01em; + color: var(--tu-ink); +} + +.text { + max-width: 62ch; + margin: 0.5rem 0 0; + font-size: 0.95rem; + line-height: 1.6; + color: var(--tu-dim); +} + +.actions { + margin-top: 1.25rem; +} + +.meta { + margin: 0.75rem 0 0; + font-size: 0.82rem; + color: var(--tu-dim); +} + +.links { + display: flex; + flex-wrap: wrap; + gap: 0.4rem 1.5rem; + margin: 1rem 0 0; + padding: 0; + list-style: none; + font-size: 0.88rem; +} + +.links a { + color: var(--tu-accent-soft); +} diff --git a/src/data/media-writer.test.ts b/src/data/media-writer.test.ts new file mode 100644 index 00000000..d5337f64 --- /dev/null +++ b/src/data/media-writer.test.ts @@ -0,0 +1,65 @@ +import {describe, expect, it} from 'vitest'; +import {detectPlatform, parseMediaWriter} from './media-writer'; + +const UA = { + windows: + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', + macos: + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15', + linux: 'Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0', + linuxArm: 'Mozilla/5.0 (X11; Linux aarch64; rv:133.0) Gecko/20100101 Firefox/133.0', + android: + 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36', + iphone: + 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1', + chromeos: + 'Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', +}; + +describe('detectPlatform', () => { + it('maps desktop user agents to their Media Writer asset', () => { + expect(detectPlatform({userAgent: UA.windows})).toEqual({kind: 'windows', asset: 'windows-x64'}); + expect(detectPlatform({userAgent: UA.macos, platform: 'MacIntel'})).toEqual({kind: 'macos', asset: 'macos'}); + expect(detectPlatform({userAgent: UA.linux})).toEqual({kind: 'linux', asset: 'linux-x86_64'}); + expect(detectPlatform({userAgent: UA.linuxArm})).toEqual({kind: 'linux', asset: 'linux-aarch64'}); + }); + + it('treats phones and tablets as mobile, even when the user agent says Linux or Mac', () => { + expect(detectPlatform({userAgent: UA.android}).kind).toBe('mobile'); + expect(detectPlatform({userAgent: UA.iphone}).kind).toBe('mobile'); + // iPadOS sends the desktop Safari user agent. + expect(detectPlatform({userAgent: UA.macos, platform: 'MacIntel', maxTouchPoints: 5}).kind).toBe('mobile'); + }); + + it('does not offer the Flatpak to ChromeOS', () => { + expect(detectPlatform({userAgent: UA.chromeos}).kind).toBe('unknown'); + }); +}); + +describe('parseMediaWriter', () => { + it('keeps the known https assets and drops the rest', () => { + const parsed = parseMediaWriter({ + mediaWriter: { + version: '0.1.0', + assets: { + 'windows-x64': {url: 'https://example.org/a.msi', size: 81020092, signed: false}, + macos: {url: 'javascript:alert(1)'}, + 'linux-x86_64': {url: 'https://example.org/a.flatpak'}, + extra: {url: 'https://example.org/x'}, + }, + }, + }); + expect(parsed).toEqual({ + version: '0.1.0', + assets: { + 'windows-x64': {url: 'https://example.org/a.msi', size: 81020092, signed: false}, + 'linux-x86_64': {url: 'https://example.org/a.flatpak', size: undefined, signed: undefined}, + }, + }); + }); + + it('returns null without a mediaWriter block', () => { + expect(parseMediaWriter(null)).toBeNull(); + expect(parseMediaWriter({prebuilt: {}})).toBeNull(); + }); +}); diff --git a/src/data/media-writer.ts b/src/data/media-writer.ts new file mode 100644 index 00000000..8f5d3ecc --- /dev/null +++ b/src/data/media-writer.ts @@ -0,0 +1,85 @@ +// Picks the TunaOS Media Writer build for the visitor's computer. The release +// assets are listed in iso.tunaos.org/downloads.json under mediaWriter.assets, +// keyed by the ids in AssetKey (generated by tuna-os/iso-builder +// scripts/generate-downloads.py). + +export type AssetKey = 'windows-x64' | 'macos' | 'linux-x86_64' | 'linux-aarch64'; + +export type Platform = + | {kind: 'windows'; asset: 'windows-x64'} + | {kind: 'macos'; asset: 'macos'} + | {kind: 'linux'; asset: 'linux-x86_64' | 'linux-aarch64'} + | {kind: 'mobile'} + | {kind: 'unknown'}; + +export type MediaWriterAsset = {url: string; size?: number; signed?: boolean}; + +export type MediaWriterRelease = { + version?: string; + assets: Partial>; +}; + +export type ClientHints = { + userAgent: string; + // navigator.platform; iPadOS reports "MacIntel" with a desktop user agent. + platform?: string; + maxTouchPoints?: number; +}; + +export const OS_LABEL: Record<'windows' | 'macos' | 'linux', string> = { + windows: 'Windows', + macos: 'macOS', + linux: 'Linux', +}; + +export function detectPlatform({userAgent, platform = '', maxTouchPoints = 0}: ClientHints): Platform { + const ua = userAgent; + // Android and iOS user agents also contain "Linux" or "Mac OS X", so test + // for phones and tablets first. + if (/Android|iPhone|iPad|iPod|Mobile|Silk|Kindle/i.test(ua)) return {kind: 'mobile'}; + if (platform === 'MacIntel' && maxTouchPoints > 1) return {kind: 'mobile'}; + if (/Windows/i.test(ua)) return {kind: 'windows', asset: 'windows-x64'}; + if (/Macintosh|Mac OS X/i.test(ua)) return {kind: 'macos', asset: 'macos'}; + // ChromeOS cannot install a Flatpak outside its Linux container; send it to + // the browser builder rather than guess. + if (/CrOS/i.test(ua)) return {kind: 'unknown'}; + if (/Linux|X11/i.test(ua)) { + const arm = /aarch64|arm64|armv8/i.test(`${ua} ${platform}`); + return {kind: 'linux', asset: arm ? 'linux-aarch64' : 'linux-x86_64'}; + } + return {kind: 'unknown'}; +} + +const ASSET_KEYS: readonly AssetKey[] = ['windows-x64', 'macos', 'linux-x86_64', 'linux-aarch64']; + +// Reads one property of an untrusted JSON value; undefined unless it is an object with that key. +function field(value: unknown, key: string): unknown { + return value && typeof value === 'object' && key in value ? Reflect.get(value, key) : undefined; +} + +// Returns the mediaWriter block of downloads.json, or null when it does not +// have the expected shape. +export function parseMediaWriter(doc: unknown): MediaWriterRelease | null { + const mw = field(doc, 'mediaWriter'); + const assets = field(mw, 'assets'); + if (!assets || typeof assets !== 'object') return null; + const out: MediaWriterRelease['assets'] = {}; + for (const key of ASSET_KEYS) { + const entry = field(assets, key); + const url = field(entry, 'url'); + if (typeof url !== 'string' || !url.startsWith('https://')) continue; + const size = field(entry, 'size'); + const signed = field(entry, 'signed'); + out[key] = { + url, + size: typeof size === 'number' ? size : undefined, + signed: typeof signed === 'boolean' ? signed : undefined, + }; + } + const version = field(mw, 'version'); + return {version: typeof version === 'string' ? version : undefined, assets: out}; +} + +export function formatSize(bytes: number): string { + return `${Math.max(1, Math.round(bytes / 1e6))} MB`; +} diff --git a/src/pages/download.tsx b/src/pages/download.tsx index c5a7e299..68759a17 100644 --- a/src/pages/download.tsx +++ b/src/pages/download.tsx @@ -4,6 +4,7 @@ import Layout from '@theme/Layout'; import Heading from '@theme/Heading'; import Link from '@docusaurus/Link'; import IsoDownloads from '@site/src/components/IsoDownloads'; +import MediaWriterDownload from '@site/src/components/MediaWriterDownload'; import FactoryHealth from '@site/src/components/FactoryHealth'; import page from '@site/src/css/page.module.css'; import styles from './download.module.css'; @@ -57,6 +58,7 @@ export default function Download(): ReactNode { description="Download bootable live ISOs for all TunaOS images — Albacore, Yellowfin, Skipjack, Bonito, Dakota and more.">
+ @@ -71,8 +73,9 @@ export default function Download(): ReactNode {

Replace /dev/sdX with your USB device (double-check with{' '} - lsblk — this erases the target). On Windows or macOS,{' '} - balenaEtcher works too. After + lsblk — this erases the target). On Windows or macOS, TunaOS + Media Writer (above) or{' '} + balenaEtcher writes it for you. After booting, follow the installer guide.