import 'dart:convert'; /// Minimal reader retained only for endpoints that do not have an /// authoritative generated protobuf model yet. class ProtobufWireMessage { ProtobufWireMessage(this.bytes); final List bytes; Map>? _cache; Map> get _fields => _cache ??= _read(); int integer(int tag) => (_fields[tag]?.lastOrNull as int?) ?? 0; String string(int tag) { final value = _fields[tag]?.lastOrNull; return value is List ? utf8.decode(value, allowMalformed: true) : ''; } Map> _read() { final result = >{}; var index = 0; int varint() { var value = 0; var shift = 0; while (index < bytes.length) { final byte = bytes[index++]; value |= (byte & 127) << shift; if (byte < 128) return value; shift += 7; } throw const FormatException('Invalid protobuf'); } while (index < bytes.length) { final key = varint(); final tag = key >> 3; final Object field; if ((key & 7) == 0) { field = varint(); } else if ((key & 7) == 2) { final length = varint(); final end = index + length; if (end > bytes.length) throw const FormatException('Invalid protobuf'); field = bytes.sublist(index, end); index = end; } else { throw const FormatException('Unsupported protobuf'); } (result[tag] ??= []).add(field); } return result; } } extension on List { Object? get lastOrNull => isEmpty ? null : last; }