Yet another declarative binary parser/packer, but built for modern browsers.
Specifically, built to support WebUSB.
Documentation is coming, check the tests for examples for now.
Test branch coverage is ~75%, which will also be improving along with the documentation.
let report = Binary_Map({little_endian: true})
.set('size', Bits(2))
.set('type', Bits(2, {decode: (value, context) => {
if (value === 3) {
throw new Error(`Reserved value ${value} for ${context._identifier}`)
}
return value;
}}))
.set('tag', Bits(4))
})
Bits(size: 1-7 \[, {encode?, decode?}])
Uint(size: 8 | 16 | 32 | 64 \[, {encode?, decode?, little_endian?}])
Int(size: 8 | 16 | 32 | 64 \[, {encode?, decode?, little_endian?}])
Repeat({count?: Numeric, bytes?: Numeric, encode?, decode?, little_endian?}, ...elements)
:
One of either count
or bytes
must be provided.
Repeat will repeatedly iterate through elements
:
- If
count
is specified, it will repeatcount
times. - If
bytes
is specified, it will repeat untilbytes
bytes have been consumed. An error will be thrown ifbytes
isn't a multiple of the size ofelements
.