Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/components/MediaWriterDownload/index.tsx
Original file line number Diff line number Diff line change
@@ -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<Platform | null>(null);
const [release, setRelease] = useState<MediaWriterRelease | null>(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 (
<section className={styles.card} aria-labelledby="media-writer-title">
<h2 id="media-writer-title" className={styles.title}>
Install TunaOS from a computer
</h2>
<p className={styles.text}>
TunaOS installs on a desktop or laptop PC. Open{' '}
<strong>tunaos.org/download</strong> 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.
</p>
</section>
);
}

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 (
<section className={styles.card} aria-labelledby="media-writer-title">
<h2 id="media-writer-title" className={styles.title}>
The easy way: TunaOS Media Writer
</h2>
<p className={styles.text}>
Pick an image in the app. It downloads or builds the ISO and, if you want,
writes it to a USB stick.
</p>
<div className={clsx(page.btnRow, page.btnRowLeft, styles.actions)}>
<a className={clsx('button', page.btnPrimary)} href={asset?.url ?? RELEASES_URL}>
{label}
</a>
<a className={clsx('button', page.btnGhost)} href={BROWSER_BUILDER_URL}>
Or build an ISO in your browser
</a>
</div>
{meta.length > 0 && <p className={styles.meta}>{meta.join(' · ')}</p>}
{asset?.signed === false && platform?.kind === 'windows' && (
<p className={styles.meta}>
The installer is not code-signed yet, so Windows SmartScreen can show a warning.
</p>
)}
{asset?.signed === false && platform?.kind === 'macos' && (
<p className={styles.meta}>
The app is not notarized yet: if macOS blocks it, open System Settings › Privacy
&amp; Security and choose Open Anyway.
</p>
)}
<ul className={styles.links}>
{platform?.kind === 'windows' && (
<li>
<Link to="/wootc">Install from Windows without a USB stick (wootc)</Link>
</li>
)}
{otherArch && platform?.kind === 'linux' && (
<li>
<a href={otherArch.url}>
{platform.asset === 'linux-x86_64' ? 'Linux on ARM (aarch64)' : 'Linux on x86_64'}
</a>
</li>
)}
<li>
<a href={RELEASES_URL}>All Media Writer downloads</a>
</li>
</ul>
</section>
);
}
50 changes: 50 additions & 0 deletions src/components/MediaWriterDownload/styles.module.css
Original file line number Diff line number Diff line change
@@ -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);
}
65 changes: 65 additions & 0 deletions src/data/media-writer.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
85 changes: 85 additions & 0 deletions src/data/media-writer.ts
Original file line number Diff line number Diff line change
@@ -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<Record<AssetKey, MediaWriterAsset>>;
};

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`;
}
7 changes: 5 additions & 2 deletions src/pages/download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.">
<DownloadHeader />
<main className={clsx('container', styles.main)}>
<MediaWriterDownload />
<ChooseBanner />
<FactoryHealth />
<IsoDownloads />
Expand All @@ -71,8 +73,9 @@ export default function Download(): ReactNode {
</pre>
<p>
Replace <code>/dev/sdX</code> with your USB device (double-check with{' '}
<code>lsblk</code> — this erases the target). On Windows or macOS,{' '}
<a href="https://etcher.balena.io/">balenaEtcher</a> works too. After
<code>lsblk</code> — this erases the target). On Windows or macOS, TunaOS
Media Writer (above) or{' '}
<a href="https://etcher.balena.io/">balenaEtcher</a> writes it for you. After
booting, follow the <a href="/docs/installer">installer guide</a>.
</p>
</div>
Expand Down
Loading