// 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; /// 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 = {}; 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];