P2P Docs
ReferenceHelpers

compact-encoding

Small binary encoding toolkit for protocol messages and storage formats.

Documented against v3.7.0
stable

compact-encoding packages small binary codecs behind a shared encoder interface. It is commonly paired with Protomux message schemas and shows up in Hypercore and other Holepunch protocol surfaces whenever structured binary payloads matter. For the upstream package and implementation details, see the compact-encoding repository.

Module-level encode and decode helpers

const buffer = cenc.encode(enc, value)

Encodes a single value using enc into a freshly allocated Buffer and returns it. Equivalent to manually creating a state, calling enc.preencode and enc.encode, and slicing the buffer — useful when you just want bytes back in one step.

ParamTypeDescription
encobjectAny compact-encoding encoder (for example cenc.uint, a custom encoder).
value*The value to encode.
  • Returns: Buffer
const buf = cenc.encode(cenc.uint, 42)
// <Buffer 2a>

const value = cenc.decode(enc, buffer)

Decodes a single value from buffer using enc. Equivalent to constructing a state from the buffer and calling enc.decode.

ParamTypeDescription
encobjectAny compact-encoding encoder.
bufferBufferThe buffer to decode from.
  • Returns: The decoded value.
const val = cenc.decode(cenc.uint, buf) // 42

Install

npm i compact-encoding

The package ships TypeScript declarations and an exports map. Besides the main entry it exposes compact-encoding/raw (the same object as cenc.raw), compact-encoding/lexint (the same codec as cenc.lexint) and compact-encoding/endian (the host byte order flags LE and BE), each with its own .d.ts. Any other deep import is refused by the map.

Quickstart

import cenc from 'compact-encoding'

const state = cenc.state()

cenc.uint.preencode(state, 42)
cenc.string.preencode(state, 'hello')

state.buffer = Buffer.allocUnsafe(state.end)
state.start = 0

cenc.uint.encode(state, 42)
cenc.string.encode(state, 'hello')

state.start = 0

console.log(cenc.uint.decode(state))
console.log(cenc.string.decode(state))

API Reference

Encoder contract

Every bundled encoder follows the same three-method contract. The top-level helpers and exported factories below all return or consume objects with this shape.

enc.preencode(state, val)

Does a fast preencode dry-run that only sets state.end. Use this to figure out how big of a buffer you need.

preencode is not guaranteed to reject an invalid val; encode is where a value is checked. Most codecs refuse a bad value in encode only, so call encode (or cenc.encode) before trusting a value.

enc.encode(state, val)

Writes the encoded form of the value into state.buffer at state.start (the buffer must have been preallocated via preencode), advancing state.start past the written bytes.

ParameterTypeDescription
stateobjectA compact-encoding state with start, end, and buffer.
val*The value to encode.
  • Returns: undefined — the encoded bytes are written into state.buffer in place.
  • Throws: a TypeError or RangeError when val is not valid for the codec (see Value validation).
const cenc = require('compact-encoding')
const state = cenc.state()
cenc.uint.preencode(state, 42)
state.buffer = Buffer.allocUnsafe(state.end)
cenc.uint.encode(state, 42)
// state.buffer is a 1-byte Buffer containing the encoded uint

val = enc.decode(state)

Reads and returns a value from state.buffer at state.start, advancing state.start past the decoded value.

ParameterTypeDescription
stateobjectA compact-encoding state with start, end, and buffer.
  • Returns: * — The decoded value.
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.bool, true)
const state = cenc.state(0, buf.byteLength, buf)
const val = cenc.bool.decode(state) // true

State and convenience helpers

cenc.state(start = 0, end = 0, buffer = null)

src

A mutable state object { start, end, buffer } used by all encoders.

ParameterTypeDefaultDescription
startnumber0Initial read/write offset.
endnumber0Initial end offset (set by preencode to determine required buffer size).
bufferBuffer|Uint8Array|nullnullUnderlying buffer; allocate after preencoding.
  • Returns: State — A new state object { start, end, buffer }.
const cenc = require('compact-encoding')
const state = cenc.state()
cenc.uint.preencode(state, 42)
cenc.string.preencode(state, 'hi')
state.buffer = Buffer.allocUnsafe(state.end)
cenc.uint.encode(state, 42)
cenc.string.encode(state, 'hi')

cenc.from(enc)

src

Coerces enc — an existing compact encoder, a named raw string encoding such as 'utf8' or 'json', a codec with encode/decode, or an abstract encoding with encodingLength — into a compact-encoding-compatible encoder object.

ParameterTypeDescription
encstring|objectAn encoding name, compact encoder, abstract-encoding, or codec.
  • Returns: object — A compact-encoding encoder with preencode, encode, and decode.
const cenc = require('compact-encoding')
const enc = cenc.from('utf-8')
const buf = cenc.encode(enc, 'hello')
const val = cenc.decode(enc, buf) // 'hello'

Encoder factories

cenc.fixed(n)

src

An encoder for fixed-size buffers, where n is the exact byte length every encoded value must have.

ParameterTypeDescription
nnumberExact number of bytes to encode/decode.
  • Returns: object — An encoder that reads and writes exactly n bytes.
const cenc = require('compact-encoding')
const enc = cenc.fixed(4)
const buf = cenc.encode(enc, Buffer.alloc(4, 0xff))
const val = cenc.decode(enc, buf) // <Buffer ff ff ff ff>

cenc.array(enc)

src

An encoder that prefixes arrays with their length and encodes each item in order using enc.

ParameterTypeDescription
encobjectAn encoder with preencode, encode, and decode methods.
  • Returns: object — An encoder for arrays of values handled by enc.
const cenc = require('compact-encoding')
const enc = cenc.array(cenc.uint)
const buf = cenc.encode(enc, [1, 2, 3])
const val = cenc.decode(enc, buf) // [1, 2, 3]

cenc.frame(enc)

src

An encoder that prefixes one encoded payload with its byte length. Decoding throws Out of bounds when the byte-length prefix is longer than the bytes left in the buffer. Before 3.5.2 it decoded whatever bytes remained.

ParameterTypeDescription
encobjectAn encoder with preencode, encode, and decode methods.
  • Returns: object — An encoder that wraps each value in a byte-length frame.
const cenc = require('compact-encoding')
const enc = cenc.frame(cenc.string)
const buf = cenc.encode(enc, 'hello')
const val = cenc.decode(enc, buf) // 'hello'

cenc.record(keyEncoding, valueEncoding)

src

An encoder for plain object records, with keyEncoding for object keys and valueEncoding for object values. Keys are always strings, so keyEncoding has to be a string encoding. The record is prefixed with its number of pairs. The order the pairs are written in is undefined, so two encoders can write the same record as different bytes; use cenc.orderedRecord when the bytes must be stable.

ParameterTypeDescription
keyEncodingobjectEncoder used for each object key.
valueEncodingobjectEncoder used for each object value.
  • Returns: object — An encoder that reads/writes plain objects as key-value pairs.
const cenc = require('compact-encoding')
const enc = cenc.record(cenc.string, cenc.uint)
const buf = cenc.encode(enc, { a: 1, b: 2 })
const val = cenc.decode(enc, buf) // [Object: null prototype] { a: 1, b: 2 }

cenc.orderedRecord(keyEncoding, valueEncoding)

src

Same as cenc.record, but the keys are sorted before they are written, so a record encodes to the same bytes whatever order its keys were set in. Keys sort by their own UTF-8 bytes rather than by what keyEncoding writes, a comparison every language can reproduce. Keys that encode to the same bytes, two different unpaired surrogates for example, keep the order they were set in. Decoding is the same as cenc.record. Added in 3.6.0.

ParameterTypeDescription
keyEncodingobjectString encoder used for each object key.
valueEncodingobjectEncoder used for each object value.
  • Returns: object — An encoder that reads plain objects and writes them with sorted keys.
const cenc = require('compact-encoding')
const enc = cenc.orderedRecord(cenc.string, cenc.uint)
const a = cenc.encode(enc, { b: 2, a: 1 })
const b = cenc.encode(enc, { a: 1, b: 2 })
a.equals(b) // true

cenc.stringRecord

src

A prebuilt record(cenc.string, cenc.string) encoder for simple string maps. cenc.orderedStringRecord is the same with sorted keys, built on orderedRecord.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.stringRecord, { name: 'alice', role: 'admin' })
const val = cenc.decode(cenc.stringRecord, buf) // [Object: null prototype] { name: 'alice', role: 'admin' }

Numeric encodings

cenc.uint64

src

Unsigned integer encoders (cenc.uint, cenc.uint8, cenc.uint16, cenc.uint24, cenc.uint32, cenc.uint40, cenc.uint48, cenc.uint56, cenc.uint64). cenc.uint chooses a compact size automatically; the fixed-width variants always use the named byte width.

Big-endian variants (cenc.uintbe, cenc.uint16be, cenc.uint32be, cenc.uint64be) write each multi-byte body most-significant byte first, so they are not interchangeable with the little-endian codecs on the wire. cenc.uintbe keeps the same one-byte size prefix as cenc.uint. cenc.uintbe and cenc.uint16be are new in 3.7.0; cenc.uint32be and cenc.uint64be predate it.

JavaScript numbers are exact only up to Number.MAX_SAFE_INTEGER, so cenc.uint64 and the other number codecs stop there. For the full 64-bit range use cenc.biguint64, which writes the same bytes.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.uint64, 2 ** 32)
const val = cenc.decode(cenc.uint64, buf) // 4294967296

cenc.int64

src

Signed integer encoders (cenc.int, cenc.int8 … cenc.int64) built on top of the unsigned variants with ZigZag encoding.

ZigZag doubles a value's magnitude before writing it as an unsigned integer, so a signed integer only reaches half as far as an unsigned one of the same width. Since 3.3.1 the signed codecs validate that range explicitly and reject anything outside -(2**52) to 2**52 - 1; use cenc.bigint for larger values. Earlier releases silently lost precision instead.

Big-endian variants (cenc.intbe, cenc.int16be, cenc.int32be, cenc.int64be) are the matching unsigned big-endian codec with ZigZag encoding. They are new in 3.7.0. Each fixed-width signed codec also inherits the width of its unsigned codec, so cenc.int8 only accepts -128 to 127.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.int64, -4294967296)
const val = cenc.decode(cenc.int64, buf) // -4294967296

cenc.bigint

src

BigInt-aware integer encoders (cenc.biguint64, cenc.bigint64, cenc.biguint, cenc.bigint) for fixed-width or variable-width large integer values. cenc.biguint64 and cenc.bigint64 write the same bytes as cenc.uint64 and cenc.int64 and carry the whole 64-bit range, which those cannot. Since 3.6.0 cenc.biguint refuses a negative value and the 64-bit codecs refuse a value outside their width, both with a RangeError.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.bigint, -(2n ** 128n))
const val = cenc.decode(cenc.bigint, buf) // -340282366920938463463374607431768211456n

cenc.float64

src

Floating-point encoders (cenc.float32, cenc.float64) using IEEE-754 little-endian layouts. Since 3.7.0 encode throws a TypeError for anything that is not a number; NaN and the infinities are still accepted.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.float64, Math.PI)
const val = cenc.decode(cenc.float64, buf) // 3.141592653589793

cenc.lexint

src

The lexicographic integer encoder exported from the ./lexint module. Encoded values sort byte-wise in the same order as the numbers. Since 3.6.0 it throws a RangeError for a negative number, NaN, or an infinite value, in both preencode and encode.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.lexint, 1000)
const val = cenc.decode(cenc.lexint, buf) // 1000

Binary, buffer, and typed-array encodings

cenc.binary

src

Buffer-oriented encoders. buffer length-prefixes a byte sequence and decodes a zero length as an empty buffer. optionalBuffer is like buffer but also accepts null; an empty buffer and null both encode as length 0, and that length decodes as null. binary accepts either a string or buffer-like input.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.binary, 'hello')
const val = cenc.decode(cenc.binary, buf) // <Buffer 68 65 6c 6c 6f>

cenc.arraybuffer

src

An encoder for ArrayBuffer instances. Decoding throws Out of bounds when the length prefix is longer than the bytes left in the buffer. Before 3.5.2 it returned a zero-padded ArrayBuffer.

  • Returns: object
const cenc = require('compact-encoding')
const ab = new ArrayBuffer(4)
const buf = cenc.encode(cenc.arraybuffer, ab)
const val = cenc.decode(cenc.arraybuffer, buf) // ArrayBuffer { byteLength: 4 }

cenc.float64array

src

Length-prefixed typed-array encoders (cenc.uint8array, cenc.uint16array, cenc.uint32array, cenc.int8array, cenc.int16array, cenc.int32array, cenc.biguint64array, cenc.bigint64array, cenc.float32array, cenc.float64array) that preserve the underlying element type.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.float64array, new Float64Array([Math.PI]))
const val = cenc.decode(cenc.float64array, buf) // Float64Array [3.141592653589793]

cenc.fixed64

src

Prebuilt fixed-size buffer encoders — cenc.fixed8, cenc.fixed16, cenc.fixed24, cenc.fixed32, and cenc.fixed64 — for 8, 16, 24, 32, and 64-byte values.

  • Returns: object
const cenc = require('compact-encoding')
const sig = Buffer.alloc(64)
const buf = cenc.encode(cenc.fixed64, sig)
const val = cenc.decode(cenc.fixed64, buf) // <Buffer 00 00 ... (64 bytes)>

Text encodings

cenc.ucs2

src

String encoders for the named text encoding (cenc.string, cenc.utf8, cenc.ascii, cenc.hex, cenc.base64, cenc.utf16le, cenc.ucs2). Each one also exposes .fixed(length) for fixed-size strings.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.ucs2, 'hi')
const val = cenc.decode(cenc.ucs2, buf) // 'hi'

Boolean, date, and structured-value encodings

cenc.bool

src

A one-byte boolean encoder.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.bool, true)
const val = cenc.decode(cenc.bool, buf) // true

cenc.bitarray

src

A length-prefixed encoder for an array of booleans, packed one bit per value.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.bitarray, [true, false, true])
const val = cenc.decode(cenc.bitarray, buf) // [true, false, true]

cenc.date

src

A Date encoder backed by the signed integer timestamp value from date.getTime().

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.date, new Date('2024-01-01'))
const val = cenc.decode(cenc.date, buf) // Date object for 2024-01-01

cenc.ndjson

src

UTF-8 JSON encoders (cenc.json, cenc.ndjson). ndjson appends a trailing newline before encoding.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.ndjson, { a: 1 })
const val = cenc.decode(cenc.ndjson, buf) // { a: 1 }

cenc.none

src

A sentinel encoder that always decodes to null and writes no payload bytes.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.none, null)
const val = cenc.decode(cenc.none, buf) // null

cenc.any

src

A schemaless tagged-value encoder for JSON-like values, arrays, objects, dates, strings, buffers, booleans, integers, and floats.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.any, { n: 42, flag: true })
const val = cenc.decode(cenc.any, buf) // { n: 42, flag: true }

Network encodings

cenc.port

src

The same encoder as cenc.uint16, provided for protocol readability when encoding network ports.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.port, 8080)
const val = cenc.decode(cenc.port, buf) // 8080

cenc.ip

src

Encoders for IPv4 addresses, IPv6 addresses, or either family with an embedded family tag (cenc.ipv4, cenc.ipv6, cenc.ip). Since 3.6.0 encode throws a RangeError for an address that does not have 4 parts (IPv4) or 8 parts (IPv6, where :: stands for the missing zero parts), or whose part is larger than 255 (IPv4) or 0xffff (IPv6). Earlier releases wrote whatever bytes the parser produced.

  • Returns: object
const cenc = require('compact-encoding')
const buf4 = cenc.encode(cenc.ip, '1.2.3.4')
cenc.decode(cenc.ip, buf4) // '1.2.3.4'
const buf6 = cenc.encode(cenc.ip, '::1')
cenc.decode(cenc.ip, buf6) // '0:0:0:0:0:0:0:1'

cenc.ipAddress

src

Address-object encoders (cenc.ipv4Address, cenc.ipv6Address, cenc.ipAddress) for { host, family?, port } shapes.

  • Returns: object
const cenc = require('compact-encoding')
const buf = cenc.encode(cenc.ipAddress, { host: '1.2.3.4', port: 8080 })
const val = cenc.decode(cenc.ipAddress, buf) // { host: '1.2.3.4', family: 4, port: 8080 }

Raw variants

cenc.raw

src

A namespace of non-length-prefixed variants for many buffer, string, array, JSON, and typed-array encodings such as cenc.raw.buffer, cenc.raw.utf8, cenc.raw.array(enc), and cenc.raw.json. A raw decoder consumes everything up to state.end. Since 3.6.0 it stops at state.end rather than the end of state.buffer, so a raw codec nested in cenc.frame reads only its own frame.

  • Returns: object
const cenc = require('compact-encoding')
// copy raw bytes into state
const buf = Buffer.from([1, 2, 3])
const state = cenc.state()
cenc.raw.preencode(state, buf)
state.buffer = Buffer.allocUnsafe(state.end)
cenc.raw.encode(state, buf)

Complete codec reference

The sections above detail the contract and the most common encoders. Below is the full catalog of bundled codecs shipped by compact-encoding v3.7.0. Every entry is an object implementing the encoder contract (preencode / encode / decode), so any of them can be composed with cenc.array(enc), cenc.frame(enc), or used directly with cenc.encode / cenc.decode. Fixed-size integers are little-endian unless the name ends in be.

Unsigned integers

CodecBytesNotes
cenc.uint1–9Smallest fixed-size encoding with a prefix signalling which; best for values spanning a wide range.
cenc.uintbe1–9Like cenc.uint with big-endian bodies.
cenc.uint81Fixed uint8.
cenc.uint162Fixed uint16; useful for ports.
cenc.uint16be2Big-endian uint16.
cenc.uint243Fixed uint24; useful for message framing.
cenc.uint324Fixed uint32; useful for large message framing.
cenc.uint32be4Big-endian uint32.
cenc.uint405Fixed uint40.
cenc.uint486Fixed uint48.
cenc.uint567Fixed uint56.
cenc.uint648Fixed uint64.
cenc.uint64be8Big-endian uint64.

Signed integers — each is the matching unsigned codec with ZigZag encoding, so small-magnitude negatives stay compact.

CodecBytes
cenc.int1–9
cenc.intbe1–9
cenc.int81
cenc.int162
cenc.int16be2
cenc.int243
cenc.int324
cenc.int32be4
cenc.int405
cenc.int486
cenc.int567
cenc.int648
cenc.int64be8

Big integers

CodecBytesNotes
cenc.biguint648Fixed biguint64; same bytes as cenc.uint64 with the full 64-bit range.
cenc.bigint648biguint64 with ZigZag encoding; same bytes as cenc.int64.
cenc.biguintvariableWord count uint-prefixed.
cenc.bigintvariablebiguint with ZigZag encoding.

Lexicographic integer

CodecNotes
cenc.lexintVariable-size number whose bytes sort in numeric order.

Floating point

CodecBytes
cenc.float324
cenc.float648

Buffers

CodecNotes
cenc.bufferBuffer with a uint length prefix.
cenc.optionalBufferLike cenc.buffer but also accepts null; empty and null both decode as null.
cenc.binaryBuffer or string, length-prefixed; decodes to a buffer.
cenc.arraybufferArrayBuffer with a uint length prefix.

Typed arrays — each is prefixed with its element count as a uint.

CodecElement type
cenc.uint8arrayUint8Array
cenc.uint16arrayUint16Array
cenc.uint32arrayUint32Array
cenc.int8arrayInt8Array
cenc.int16arrayInt16Array
cenc.int32arrayInt32Array
cenc.biguint64arrayBigUint64Array
cenc.bigint64arrayBigInt64Array
cenc.float32arrayFloat32Array
cenc.float64arrayFloat64Array

Strings — each is length-prefixed and also exposes a .fixed(n) factory for a fixed-length variant (for example cenc.hex.fixed(32)).

CodecEncoding
cenc.string, cenc.utf8UTF-8
cenc.asciiASCII
cenc.hexHex
cenc.base64Base64
cenc.utf16le, cenc.ucs2UTF-16LE

Fixed-size buffers

CodecNotes
cenc.fixed(n)Factory for an n-byte fixed buffer (no length prefix).
cenc.fixed8Fixed 8-byte buffer.
cenc.fixed16Fixed 16-byte buffer.
cenc.fixed24Fixed 24-byte buffer.
cenc.fixed32Fixed 32-byte buffer.
cenc.fixed64Fixed 64-byte buffer.

Structured & special values

CodecNotes
cenc.boolEncodes a boolean as 1 or 0.
cenc.bitarrayLength-prefixed array of booleans, packed one bit per value.
cenc.dateEncodes a Date.
cenc.jsonJSON value serialized as UTF-8.
cenc.ndjsonJSON value serialized as newline-delimited UTF-8.
cenc.anyAny JSON-representable value into a self-describing buffer (compact types); useful for schemaless codecs.
cenc.noneEncodes nothing—placeholder for empty fields.
cenc.array(enc)Length-prefixed array of enc values.
cenc.frame(enc)Wraps enc with a uint byte-length frame prefix.
cenc.record(keyEnc, valueEnc)Length-prefixed map of key/value pairs.
cenc.stringRecordrecord(utf8, utf8)—a string-to-string map.
cenc.orderedRecord(keyEnc, valueEnc)Like cenc.record, with keys sorted by their UTF-8 bytes so the output is deterministic.
cenc.orderedStringRecordorderedRecord(utf8, utf8).

Network addresses

CodecNotes
cenc.portPort number (alias of cenc.uint16).
cenc.ipv4IPv4 address.
cenc.ipv4AddressIPv4 address plus port.
cenc.ipv6IPv6 address.
cenc.ipv6AddressIPv6 address plus port.
cenc.ipDual IPv4/IPv6 address.
cenc.ipAddressDual IPv4/IPv6 address plus port.

Non-prefixed variants — cenc.raw exposes length-prefix-free versions of the buffer, string, typed-array, array, and JSON codecs: cenc.raw.buffer, cenc.raw.arraybuffer, cenc.raw.<typedarray>, cenc.raw.utf8 / cenc.raw.ascii / cenc.raw.hex / cenc.raw.base64 / cenc.raw.ucs2, cenc.raw.array(enc), cenc.raw.json, and cenc.raw.ndjson.

Value validation

Since 3.6.0 encode rejects values a codec cannot represent instead of writing wrong bytes. The thrown error is a TypeError for a wrong kind of value and a RangeError for a value outside the codec's range. These errors replace the plain Error that earlier releases threw for integer ranges.

CodecThrows
cenc.uint, cenc.uint56, cenc.uint64, and the be variants of themTypeError for a non-integer. RangeError outside 0 to Number.MAX_SAFE_INTEGER. Decoding a stored value above that limit also throws a RangeError.
cenc.uint8, cenc.uint16, cenc.uint24, cenc.uint32, cenc.uint40, cenc.uint48, cenc.uint16be, cenc.uint32beTypeError for a non-integer. RangeError outside 0 to the width's maximum, for example 255 for uint8.
cenc.int and the fixed-width signed codecsTypeError for a non-integer. RangeError outside -(2**52) to 2**52 - 1, or outside the unsigned width after ZigZag.
cenc.biguint, cenc.bigintRangeError for a negative biguint.
cenc.biguint64, cenc.bigint64RangeError outside the 64-bit range.
cenc.float32, cenc.float64TypeError for a non-number (3.7.0).
cenc.lexintRangeError for a negative, NaN or infinite number.
cenc.ipv4, cenc.ipv6, cenc.ipRangeError for a wrong part count or an out-of-range part.
cenc.fixed(n)Error('Incorrect buffer size') unless the value is exactly n bytes.

Since 3.7.0 the error for cenc.uint and cenc.uint64 names the wider codec to switch to, such as uint must be between 0 and 9007199254740991, use biguint; cenc.int and cenc.int64 do the same with bigint and bigint64. Decoding a truncated buffer still throws Error('Out of bounds').

Code that passed undefined, a float or an out-of-range number to an integer codec, a non-number to a float codec, or an IP string with the wrong number of parts or an oversized part to an IP codec, now throws at encode time instead of producing bytes. Check values before they reach the encoder.

Types

State

PropertyTypeDefaultDescription
startnumber—Byte offset to read/write from. Advances after each encode/decode call.
endnumber—Byte offset marking the end of valid data. Advances during preencode.
bufferBuffer|Uint8Array|null—The underlying byte buffer. Set to null until allocation.

See also

  • Protomux—the most common higher-level protocol surface built directly on compact-encoding schemas.
  • Hypercore—accepts compact encodings for structured values and message payloads.
  • Upstream compact-encoding repository—source, releases, and implementation details.

Last updated on

Was this helpful?

On this page