ipx-wire converted every protobuf answer at run time through prost-reflect and serde_json::Value, so protobuf cost more than the JSON it replaced: decoding 1,488 feeds took 1.50 ms that way against 0.37 ms for serde_json and 0.09 ms for prost's own structs, and GET /api/feeds as protobuf answered about 4 ms after JSON. build.rs now generates those structs (prost-build from protox's descriptors, still no protoc). An answer's JSON goes straight into its struct with serde and out with prost, byte for byte what the runtime path wrote (a test says so); bodies holding a google.protobuf.Value keep the runtime path, as they are only ever read. ipx's calls to the directory and ipx-play build and read the structs directly, through From conversions beside the directory's own types. The page asks for protobuf too, to see what it does in practice. It reads it with web/src/wire.ts, about seventy lines and no library: protobufjs alone was larger gzipped than all protobuf saves the page. build.mts reads the .proto files into a table for it, and the answer names its message in its Content-Type, so the page keeps no table of routes. An answer of null stays JSON, since the empty message would be a feed row of defaults. The offline worker decodes a kept answer to mark what was read offline, and hands the page JSON. The decoder's test caught `pos += varint()[0]`, which adds the length to pos as it was before the varint was read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
212 lines
13 KiB
TypeScript
212 lines
13 KiB
TypeScript
// Builds the frontend (#203): each page's TypeScript from web/src, types stripped and minified
|
|
// by swc into a script of its own (app.js, login.js), the page minified, its <script data-src>
|
|
// pointing at that script, and the icons, logos and typeface beside them. The result, web/dist,
|
|
// is a static site: Caddy serves it (web/Caddyfile, Dockerfile.web) and passes the API to ipx.
|
|
//
|
|
// The page names its script with a hash of the script's contents, /app.js?v=<hash>, and the
|
|
// server lets a browser keep that for a year without asking again. A changed script is a new
|
|
// URL, and the page, which the browser checks on every visit, is what carries it. Caddy's file
|
|
// server ignores the query.
|
|
//
|
|
// node web/build.mts [out-dir] default out-dir: web/dist
|
|
import crypto from 'node:crypto';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import zlib from 'node:zlib';
|
|
import { fileURLToPath } from 'node:url';
|
|
import swc from '@swc/core';
|
|
import html from '@swc/html';
|
|
import { concatMap } from './concat-map.mts';
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
// A build for measuring what the browser tests run (#390), IPX_COVERAGE=1: the script not minified
|
|
// and with a source map back to each file of web/src, which concat-map.mts makes. Loaded only then:
|
|
// it is TypeScript, which the Node of Dockerfile.web does not run.
|
|
const coverageMap = process.env.IPX_COVERAGE ? concatMap : null;
|
|
// The page's own version (package.json), given to its script in place of __WEB_VERSION__.
|
|
const WEB_VERSION = JSON.parse(fs.readFileSync(path.join(here, '..', 'package.json'), 'utf8')).version;
|
|
|
|
// The files are one script, concatenated in this order, not modules: they share one top-level
|
|
// scope, as the single inline script did, and code that runs at load needs what came before it.
|
|
const PAGES: Record<string, { script: string, src: string[] }> = {
|
|
'index.html': { script: 'app.js', src: ['util', 'wire', 'theme', 'feeds', 'feedpage', 'items', 'player', 'dialogs', 'signals', 'gestures', 'offline', 'events', 'native'] },
|
|
'admin.html': { script: 'admin.js', src: ['util', 'wire', 'theme', 'admin'] },
|
|
'login.html': { script: 'login.js', src: ['login'] },
|
|
};
|
|
// The stylesheet the app and admin pages share, served and named by hash as the scripts are.
|
|
const STYLE = 'app.css';
|
|
|
|
/// The protobuf messages ipx answers in (#470), for web/src/wire.ts: { 'pkg.Name': [[number, name,
|
|
/// type, repeated, optional]] }, read from crates/wire/proto. Only what those files use: proto3,
|
|
/// one level of messages, scalar types and messages as field types.
|
|
export function wireMessages() {
|
|
const dir = path.join(here, '..', 'crates', 'wire', 'proto');
|
|
const short: Record<string, string> = { string: 's', bool: 'b', int64: 'i', int32: 'i', uint64: 'u', uint32: 'u', double: 'd' };
|
|
const out: Record<string, [number, string, string, 0 | 1, 0 | 1][]> = {};
|
|
for (const file of fs.readdirSync(dir).filter(f => f.endsWith('.proto')).sort()) {
|
|
const text = fs.readFileSync(path.join(dir, file), 'utf8').replace(/\/\/.*$/gm, '');
|
|
const pkg = /^\s*package\s+([\w.]+)\s*;/m.exec(text)?.[1];
|
|
if (!pkg) throw new Error(`${file}: no package`);
|
|
for (const [, name, body] of text.matchAll(/message\s+(\w+)\s*\{([^}]*)\}/g)) {
|
|
out[`${pkg}.${name}`] = [...body!.matchAll(/^\s*(optional\s+|repeated\s+)?([\w.]+)\s+(\w+)\s*=\s*(\d+)\s*;/gm)].map(
|
|
([, label, type, field, num]) => [Number(num), field!, short[type!] ?? (type!.includes('.') ? type! : `${pkg}.${type}`),
|
|
label?.trim() === 'repeated' ? 1 : 0, label?.trim() === 'optional' ? 1 : 0]);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
const WIRE = JSON.stringify(wireMessages());
|
|
|
|
const hash = (s: string | Buffer) => crypto.createHash('sha256').update(s).digest('hex').slice(0, 12);
|
|
|
|
// hls.js, for browsers that cannot play DeadAir's playlists themselves (#181). Served as it comes,
|
|
// by a name with its hash in it, which the page's script is given in place of __HLS_JS__.
|
|
const HLS = fs.readFileSync(path.join(here, '..', 'node_modules', 'hls.js', 'dist', 'hls.light.min.js'), 'utf8');
|
|
const HLS_URL = `/hls.js?v=${hash(HLS)}`;
|
|
|
|
// Font Awesome Free, the set the page's own icons come from, whole, for a Signal's icon (#261):
|
|
// each icon a file of its own, /icons/<style>/<name>.svg, drawn as a mask so it takes the theme's
|
|
// colour, and an index of names and search terms the picker fetches when it opens. The page's
|
|
// script is given the index's address, named by its hash, in place of __ICONS__.
|
|
const FA = path.join(here, '..', 'node_modules', '@fortawesome', 'fontawesome-free');
|
|
const FA_STYLES = ['solid', 'regular', 'brands'];
|
|
/// [`style/name`, label, search terms] for every icon, solid first.
|
|
export function iconIndex() {
|
|
const meta = JSON.parse(fs.readFileSync(path.join(FA, 'metadata', 'icon-families.json'), 'utf8'));
|
|
return FA_STYLES.flatMap(style => fs.readdirSync(path.join(FA, 'svgs', style)).sort().map(f => {
|
|
const name = f.slice(0, -4), m = meta[name] || {};
|
|
return [`${style}/${name}`, m.label || name, (m.search?.terms || []).join(' ')];
|
|
}));
|
|
}
|
|
const ICONS = JSON.stringify(iconIndex());
|
|
const ICONS_URL = `/icons/index.json?v=${hash(ICONS)}`;
|
|
|
|
/// The shared stylesheet, minified. @swc/html minifies CSS inside a page, so it goes through as
|
|
/// one; the doctype only keeps it from complaining that a fragment has none.
|
|
export function buildStyle({ minify = !coverageMap } = {}) {
|
|
const css = fs.readFileSync(path.join(here, STYLE), 'utf8');
|
|
if (!minify) return css;
|
|
const r = html.minifySync(`<!doctype html><style>${css}</style>`, { minifyCss: true, removeComments: true });
|
|
const bad = (r.errors || []).filter(e => e.level === 'error' || e.level === 'Error');
|
|
if (bad.length) throw new Error(`${STYLE}: ${bad.map(e => e.message).join('; ')}`);
|
|
const out = r.code.slice(r.code.indexOf('<style>') + 7, r.code.lastIndexOf('</style>'));
|
|
// The minifier drops the space between a calc() and the value after it in a shorthand --
|
|
// `padding:7px calc(12px + var(--safe-r))7px ...` -- and a browser throws the whole
|
|
// declaration away, so the element silently loses its padding. It reports no error and the
|
|
// page still loads, which is why this is checked rather than trusted. Longhands avoid it.
|
|
const run = out.match(/calc\([^()]*(?:\([^()]*\)[^()]*)*\)(?=[0-9a-zA-Z.])/);
|
|
if (run) throw new Error(`${STYLE}: minifying ran ${run[0]} into the value after it; use longhand properties`);
|
|
return out;
|
|
}
|
|
|
|
/// A script served on its own, not one of the pages' (#392): TypeScript made JavaScript. `before`
|
|
/// are files put ahead of it, sharing its scope as the pages' files do.
|
|
function compile(file: string, { minify = true } = {}, before: string[] = []): string {
|
|
const src = [...before, file].map(f => fs.readFileSync(path.resolve(here, f), 'utf8')).join('\n')
|
|
.replaceAll('/*__WIRE__*/{}', WIRE);
|
|
return swc.transformSync(src, {
|
|
filename: path.basename(file),
|
|
jsc: { parser: { syntax: 'typescript' }, target: 'es2022', minify: minify ? { compress: true, mangle: true } : undefined },
|
|
isModule: false,
|
|
minify,
|
|
}).code;
|
|
}
|
|
|
|
/// The offline service worker (#389), at the top of the site.
|
|
export const buildWorker = (o?: { minify?: boolean }) => compile('sw/sw.ts', o, ['src/wire.ts']);
|
|
/// The directory's one script (#392), as crates/directory/src/site.js holds it: readable, since it
|
|
/// is committed, and saying where it comes from.
|
|
export const buildDirectoryScript = () =>
|
|
'// Made from site.ts by `node web/build.mts --directory-script`: edit that, not this.\n' +
|
|
compile('../crates/directory/src/site.ts', { minify: false });
|
|
/// What runs in <head> before the first frame (boot.ts), named by a hash of what is served.
|
|
const BOOT = compile('boot.ts');
|
|
|
|
/// The page and its script, built: { html, js, script }, where script is the file's name.
|
|
export function buildPage(name: string, { minify = !coverageMap } = {}) {
|
|
const spec = PAGES[name];
|
|
if (!spec) throw new Error(`${name} is not one of the pages`);
|
|
const { script, src: files } = spec;
|
|
const parts = files.map(f => ({ name: `web/src/${f}.ts`, text: fs.readFileSync(path.join(here, 'src', f + '.ts'), 'utf8') }));
|
|
const src = parts.map(p => p.text).join('\n')
|
|
.replaceAll('__HLS_JS__', HLS_URL)
|
|
.replaceAll('__ICONS__', ICONS_URL)
|
|
.replaceAll('__WEB_VERSION__', WEB_VERSION)
|
|
.replaceAll('/*__WIRE__*/{}', WIRE);
|
|
const js = swc.transformSync(src, {
|
|
filename: name + '.ts',
|
|
jsc: {
|
|
parser: { syntax: 'typescript' },
|
|
target: 'es2022',
|
|
// Top-level names stay as they are: markup calls some of them by name (onclick="closeModal()")
|
|
// and the browser tests reach others (player, savePos) through page.evaluate.
|
|
minify: minify ? { compress: { toplevel: false }, mangle: { toplevel: false } } : undefined,
|
|
},
|
|
isModule: false,
|
|
minify,
|
|
...(coverageMap ? { sourceMaps: 'inline', inputSourceMap: JSON.stringify(coverageMap(parts)) } : {}),
|
|
}).code;
|
|
const page = fs.readFileSync(path.join(here, name), 'utf8');
|
|
const marker = /<script data-src="[^"]*"><\/script>/;
|
|
if (!marker.test(page)) throw new Error(`${name} has no <script data-src> to put its script in`);
|
|
// Where the inline script was, and a plain <script src>, so it still runs in the same place:
|
|
// after the markup it wires up, before anything else.
|
|
let out = page.replace(marker, `<script src="/${script}?v=${hash(js)}"></script>`);
|
|
out = out.replace(/<link rel="stylesheet" data-src="[^"]*">/,
|
|
() => `<link rel="stylesheet" href="/${STYLE}?v=${hash(buildStyle({ minify }))}">`);
|
|
// The icons are named by their contents too. They are kept a day under a fixed name, and a new
|
|
// logo went unseen for that day, in browsers and at Cloudflare's edge. The server ignores the
|
|
// query; /favicon.ico, which a browser asks for on its own, cannot carry one.
|
|
out = out.replace(/(href|src|srcset|data-light|data-dark)="\/(favicon\.png|favicon-dark\.png|apple-touch-icon\.png|logo\.svg|logo-dark\.svg|boot\.js)"/g,
|
|
(_, attr, file) => `${attr}="/${file}?v=${hash(file === 'boot.js' ? BOOT : fs.readFileSync(path.join(here, file)))}"`);
|
|
if (!minify) return { html: out, js, script };
|
|
const r = html.minifySync(out, { minifyJs: false, minifyCss: true, removeComments: true });
|
|
const bad = (r.errors || []).filter(e => e.level === 'Error');
|
|
if (bad.length) throw new Error(`${name}: ${bad.map(e => e.message).join('; ')}`);
|
|
return { html: r.code, js, script };
|
|
}
|
|
|
|
if (process.argv[1] === fileURLToPath(import.meta.url) && process.argv[2] === '--directory-script') {
|
|
fs.writeFileSync(path.join(here, '..', 'crates', 'directory', 'src', 'site.js'), buildDirectoryScript());
|
|
} else if (process.argv[1] === fileURLToPath(import.meta.url)) {
|
|
const out = process.argv[2] || path.join(here, 'dist');
|
|
fs.mkdirSync(out, { recursive: true });
|
|
fs.writeFileSync(path.join(out, STYLE), buildStyle());
|
|
fs.writeFileSync(path.join(out, 'sw.js'), buildWorker());
|
|
fs.writeFileSync(path.join(out, 'boot.js'), BOOT);
|
|
fs.writeFileSync(path.join(out, 'hls.js'), HLS);
|
|
// Served as they are, under the names the pages and the stylesheet use. /favicon.ico is the
|
|
// PNG, for a browser that asks for that on its own; iOS asks for the precomposed touch icon
|
|
// first when the site is added to a home screen (#109). /icon.png is the 2004 icon, which
|
|
// nothing here shows any more but the classic theme.
|
|
for (const [from, to] of [
|
|
['logo.svg', 'logo.svg'], ['logo-dark.svg', 'logo-dark.svg'],
|
|
...['signals', 'deadair-on', 'deadair-off'].flatMap(n => [`${n}.svg`, `${n}-dark.svg`]).map(f => [f, f]),
|
|
['favicon.png', 'favicon.png'], ['favicon.png', 'favicon.ico'], ['favicon-dark.png', 'favicon-dark.png'],
|
|
['apple-touch-icon.png', 'apple-touch-icon.png'], ['apple-touch-icon.png', 'apple-touch-icon-precomposed.png'],
|
|
['ipodderx-icon.png', 'icon.png'], ['InterVariable.woff2', 'inter.woff2'], ['Inter-LICENSE.txt', 'Inter-LICENSE.txt'],
|
|
]) fs.copyFileSync(path.join(here, from), path.join(out, to));
|
|
// The fonts Settings offers besides Inter (#359), with their licences.
|
|
fs.cpSync(path.join(here, 'fonts'), path.join(out, 'fonts'), { recursive: true });
|
|
for (const style of FA_STYLES) {
|
|
fs.mkdirSync(path.join(out, 'icons', style), { recursive: true });
|
|
for (const f of fs.readdirSync(path.join(FA, 'svgs', style))) fs.copyFileSync(path.join(FA, 'svgs', style, f), path.join(out, 'icons', style, f));
|
|
}
|
|
fs.writeFileSync(path.join(out, 'icons', 'index.json'), ICONS);
|
|
fs.copyFileSync(path.join(FA, 'LICENSE.txt'), path.join(out, 'icons', 'LICENSE.txt'));
|
|
for (const name of Object.keys(PAGES)) {
|
|
const { html, js, script } = buildPage(name);
|
|
fs.writeFileSync(path.join(out, name), html);
|
|
fs.writeFileSync(path.join(out, script), js);
|
|
}
|
|
// Each text file compressed once here, as hard as it goes, brotli and gzip beside it, for Caddy
|
|
// to send in its place (#253): smaller than compressing each answer as it goes, and no work then.
|
|
for (const f of fs.readdirSync(out).filter(f => /\.(html|js|css|svg|txt)$/.test(f))) {
|
|
const body = fs.readFileSync(path.join(out, f));
|
|
fs.writeFileSync(path.join(out, f + '.br'), zlib.brotliCompressSync(body, {
|
|
params: { [zlib.constants.BROTLI_PARAM_QUALITY]: zlib.constants.BROTLI_MAX_QUALITY, [zlib.constants.BROTLI_PARAM_SIZE_HINT]: body.length },
|
|
}));
|
|
fs.writeFileSync(path.join(out, f + '.gz'), zlib.gzipSync(body, { level: 9 }));
|
|
}
|
|
}
|