UNPKG

protobuf-codec

Version:
28 lines (22 loc) 689 B
const assert = require('nanoassert') const { decoders, tag } = require('./wire-types.js') module.exports = function * reader ( buf, byteOffset = 0, byteLength = buf.byteLength ) { let o = byteOffset const end = byteOffset + byteLength assert(end <= buf.byteLength) for (; o < end;) { const { wireType, fieldNumber } = tag(buf, o) const tagByteOffset = o const tagByteLength = tag.bytes o += tagByteLength const data = decoders[wireType](buf, o) const dataByteLength = decoders[wireType].bytes o += dataByteLength yield [fieldNumber, { tagByteOffset, tagByteLength, fieldNumber, wireType, dataByteLength, data }] } assert(o === end) }