protobuf-codec
Version:
Minimal Protocol Buffers wire encoding/decoding
28 lines (22 loc) • 689 B
JavaScript
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)
}