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:
2026-11-17 00:21:41 +00:00
parent baa6661622
commit 236bd02d88
22 changed files with 497 additions and 68 deletions

67
web/src/wire.ts Normal file
View File

@@ -0,0 +1,67 @@
// ipx's answers in protobuf (#470): the page asks for them, and this reads one into the same object
// its JSON would have been, so nothing past api() knows the difference. The messages come from
// crates/wire/proto, which web/build.mts reads into WIRE when it builds the page and the offline
// worker, both of which take this file. No library: the encoding is a few rules, and a decoder
// library was larger than everything protobuf saves the page.
/// A field: its number, its name, its type (s string, b bool, i signed, u unsigned, d double, or a
/// message's full name), whether repeated, whether optional.
type WireField = readonly [number, string, string, 0 | 1, 0 | 1];
const WIRE = /*__WIRE__*/{} as Record<string, readonly WireField[]>;
/// Message `type` from `buf`, as its JSON would have read: an absent optional or message is null, a
/// list message (only `repeated items`) is its array, 64-bit numbers are numbers.
function wireDecode(type: string, buf: Uint8Array): any {
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
let pos = 0;
const text = new TextDecoder();
// Low and high 32 bits, as protobufjs reads them: a JS number's bit operations stop at 32.
// Each length is read before `pos` is used: `pos += varint()[0]` adds to the old `pos`.
const varint = (): [number, number] => {
let lo = 0, hi = 0, b = 0;
for (let i = 0; i < 4; i++) { b = buf[pos++]!; lo |= (b & 127) << (i * 7); if (b < 128) return [lo >>> 0, 0]; }
b = buf[pos++]!; lo |= (b & 127) << 28; hi |= (b & 127) >> 4; if (b < 128) return [lo >>> 0, hi >>> 0];
for (let i = 0; i < 5; i++) { b = buf[pos++]!; hi |= (b & 127) << (i * 7 + 3); if (b < 128) return [lo >>> 0, hi >>> 0]; }
throw new Error('a varint longer than ten bytes');
};
const num = ([lo, hi]: [number, number], signed: boolean) =>
signed && hi >= 0x80000000 ? -((~hi >>> 0) * 4294967296 + (~lo >>> 0) + 1) : hi * 4294967296 + lo;
const skip = (wt: number) => {
if (wt === 0) varint(); else if (wt === 1) pos += 8; else if (wt === 2) { const len = varint()[0]; pos += len; } else if (wt === 5) pos += 4;
else throw new Error(`wire type ${wt}`);
};
const scalar = (t: string): unknown => {
if (t === 'd') { const v = view.getFloat64(pos, true); pos += 8; return v; }
const v = varint();
return t === 'b' ? (v[0] | v[1]) !== 0 : num(v, t === 'i');
};
const message = (type: string, end: number): any => {
const fields = WIRE[type];
if (!fields) throw new Error(`no message ${type} in the page`);
const o: Record<string, unknown> = {};
for (const [, name, t, rep, opt] of fields) {
o[name] = rep ? [] : opt || t.length > 1 ? null : t === 's' ? '' : t === 'b' ? false : 0;
}
while (pos < end) {
const [tag] = varint();
const f = fields.find(x => x[0] === tag >>> 3), wt = tag & 7;
if (!f) { skip(wt); continue; }
const [, name, t, rep] = f;
let v: unknown;
if (t.length > 1) { const len = varint()[0]; v = WIRE[t] ? message(t, pos + len) : (pos += len, null); }
else if (t === 's') { const len = varint()[0]; v = text.decode(buf.subarray(pos, pos + len)); pos += len; }
// Packed: proto3 sends a repeated number as one run of them.
else if (rep && wt === 2) { const len = varint()[0], e = pos + len; while (pos < e) (o[name] as unknown[]).push(scalar(t)); continue; }
else v = scalar(t);
if (rep) (o[name] as unknown[]).push(v); else o[name] = v;
}
const only = fields.length === 1 ? fields[0] : undefined;
return only && only[1] === 'items' && only[3] ? o.items : o;
};
return message(type, buf.length);
}
/// The message an answer is in, from its Content-Type, or undefined for one that is not protobuf.
const wireType = (contentType: string | null) =>
/^application\/x-protobuf;\s*messageType=([\w.]+)/.exec(contentType || '')?.[1];