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 }
.
Keys | Description |
---|---|
| Byte offset to start encoding/decoding at. |
| Byte offset indicating the end of the buffer. |
| Either a Node.js Buffer or Uint8Array. |
| Used internally by codecs, starts as |
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.
Encodings | Description |
---|---|
| Pass through encodes a buffer, i.e., a basic copy. |
| Encodes a uint using compact-uint. |
| Encodes a fixed size uint8. |
| Encodes a fixed size uint16. Useful for things like ports. |
| Encodes a fixed size uint24. Useful for message framing. |
| Encodes a fixed size uint32. Useful for very large message framing. |
| Encodes a fixed size uint40. |
| Encodes a fixed size uint48. |
| Encodes a fixed size uint56. |
| Encodes a fixed size uint64. |
| Encodes an int using |
| Encodes a fixed size int8 using |
| Encodes a fixed size int16 using |
| Encodes a fixed size int24 using |
| Encodes a fixed size int32 using |
| Encodes a fixed size int40 using |
| Encodes a fixed size int48 using |
| Encodes a fixed size int56 using |
| Encodes a fixed size int64 using |
| Encodes an int using lexicographic-integer encoding so that encoded values are lexicographically sorted in ascending numerical order. |
| Encodes a fixed size float32. |
| Encodes a fixed size float64. |
| Encodes a buffer with its length uint prefixed. When decoding an empty buffer, |
| Encodes a buffer without a length prefixed. |
| Encodes a uint8array with its element length uint prefixed. |
| Encodes a uint8array without a length prefixed. |
| Encodes a uint16array with its element length uint prefixed. |
| Encodes a uint16array without a length prefixed. |
| Encodes a uint32array with its element length uint prefixed. |
| Encodes a uint32array without a length prefixed. |
| Encodes a int8array with its element length uint prefixed. |
| Encodes a int8array without a length prefixed. |
| Encodes a int16array with its element length uint prefixed. |
| Encodes a int16array without a length prefixed. |
| Encodes a int32array with its element length uint prefixed. |
| Encodes a int32array without a length prefixed. |
| Encodes a float32array with its element length uint prefixed. |
| Encodes a float32array without a length prefixed. |
| Encodes a float64array with its element length uint prefixed. |
| Encodes a float64array without a length prefixed. |
| Encodes a boolean as 1 or 0. |
| Encodes a utf-8 string, similar to buffer. |
| Encodes a utf-8 string without a length prefixed. |
| Encodes a fixed sized utf-8 string. |
| Encodes an ascii string. |
| Encodes an ascii string without a length prefixed. |
| Encodes a fixed size ascii string. |
| Encodes a hex string. |
| Encodes a hex string without a length prefixed. |
| Encodes a fixed size hex string. |
| Encodes a base64 string. |
| Encodes a base64 string without a length prefixed. |
| Encodes a fixed size base64 string. |
| Encodes a utf16le string. |
| Encodes a utf16le string without a length prefixed. |
| Encodes a fixed size utf16le string. |
| Encodes a fixed 32 byte buffer. |
| Encodes a fixed 64 byte buffer. |
| Makes a fixed sized encoder. |
| Makes an array encoder from another encoder. Arrays are uint prefixed with their length. |
| Makes an array encoder from another encoder, without a length prefixed. |
| Encodes a JSON value as utf-8. |
| Encodes a JSON value as utf-8 without a length prefixed. |
| Encodes a JSON value as newline delimited utf-8. |
| Encodes a JSON value as newline delimited utf-8 without a length prefixed. |
| Makes a compact encoder from a codec or abstract-encoding. |
Last updated