Compact encoding
A series of binary encoders/decoders for building small and fast parsers and serializers.
Installation
Install with npm:
Encoder API
state()
state()
An object with the keys{ start, end, buffer, cache }
.
Users can also get a blank state object using
cenc.state()
.
enc.preencode(state, val)
enc.preencode(state, val)
Performs a fast preencode dry-run that only sets state.end
. Use this to figure out how big the buffer needs to be.
enc.encode(state, val)
enc.encode(state, val)
Encodes val
into state.buffer
at position state.start
and updates state.start
to point after the encoded value when done.
val = enc.decode(state)
val = enc.decode(state)
Decodes a value from state.buffer
as position state.start
and updates state.start
to point after the decoded value when done in the buffer.
Helpers
To encode to a buffer or decode from one, use the encode
and decode
helpers to reduce boilerplate.
Bundled encodings
The following encodings are bundled as they are primitives that can be used to build others on top.
Feel free to make a PR to add more encodings that are missing.
Last updated