Answers in protobuf from generated structs, and the page reads them (#470)
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>
This commit is contained in:
@@ -29,13 +29,34 @@ const WEB_VERSION = JSON.parse(fs.readFileSync(path.join(here, '..', 'package.js
|
||||
// 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', 'theme', 'feeds', 'feedpage', 'items', 'player', 'dialogs', 'signals', 'gestures', 'offline', 'events', 'native'] },
|
||||
'admin.html': { script: 'admin.js', src: ['util', 'theme', 'admin'] },
|
||||
'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,
|
||||
@@ -78,9 +99,11 @@ export function buildStyle({ minify = !coverageMap } = {}) {
|
||||
return out;
|
||||
}
|
||||
|
||||
/// A script served on its own, not one of the pages' (#392): TypeScript made JavaScript.
|
||||
function compile(file: string, { minify = true } = {}): string {
|
||||
const src = fs.readFileSync(path.resolve(here, file), 'utf8');
|
||||
/// 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 },
|
||||
@@ -90,7 +113,7 @@ function compile(file: string, { minify = true } = {}): string {
|
||||
}
|
||||
|
||||
/// The offline service worker (#389), at the top of the site.
|
||||
export const buildWorker = (o?: { minify?: boolean }) => compile('sw/sw.ts', o);
|
||||
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 = () =>
|
||||
@@ -108,7 +131,8 @@ export function buildPage(name: string, { minify = !coverageMap } = {}) {
|
||||
const src = parts.map(p => p.text).join('\n')
|
||||
.replaceAll('__HLS_JS__', HLS_URL)
|
||||
.replaceAll('__ICONS__', ICONS_URL)
|
||||
.replaceAll('__WEB_VERSION__', WEB_VERSION);
|
||||
.replaceAll('__WEB_VERSION__', WEB_VERSION)
|
||||
.replaceAll('/*__WIRE__*/{}', WIRE);
|
||||
const js = swc.transformSync(src, {
|
||||
filename: name + '.ts',
|
||||
jsc: {
|
||||
|
||||
Reference in New Issue
Block a user