Grounds
    Preparing search index...

    Cursor-based binary decoder with validation for Relish wire format.

    Reads bytes sequentially using an internal cursor, validating structure and constraints (field order, map key uniqueness, enum length, bit 7 rules).

    Decoding with error handling:

    import { Decoder } from '@grounds/core';

    const bytes = new Uint8Array([0x02, 0x2a]); // U8(42)
    const decoder = new Decoder(bytes);

    decoder.decodeValue().match(
    (value) => console.log('Decoded:', value), // 42
    (error) => console.error('Decode failed:', error.code, error.message)
    );

    The Decoder validates all wire format constraints:

    • Struct fields must be in ascending field ID order
    • Map keys must be unique (checked via JSON serialization)
    • Enum content length must match declared length
    • Field IDs and variant IDs must have bit 7 clear (0-127 range)

    Decoding produces DecodedValue (raw JavaScript values), not wrapped RelishValue objects. This asymmetry simplifies consumption of decoded data.

    decode for one-shot decoding convenience function

    Index

    Constructors

    • Creates a decoder for the given byte buffer.

      Parameters

      • buffer: Uint8Array

        Binary data to decode

      Returns Decoder

    Accessors

    • get position(): number

      Current read position in the buffer (bytes consumed).

      Returns number

    • get remaining(): number

      Bytes remaining to be read from current position.

      Returns number

    Methods

    • Decodes a tagged variable-length integer (varint) from the buffer.

      The Relish wire format uses tagged varints for length fields. The encoding uses bit 0 as a flag: if 0, the upper 7 bits contain the length (short form); if 1, the next 4 bytes contain a 32-bit little-endian length (long form).

      Short form handles lengths 0-127 in a single byte. Long form handles lengths up to 2^31-1 in 4 bytes, supporting large payloads.

      Returns Result<number, DecodeError>

      A Result containing the decoded length, or DecodeError if not enough data

      Advances the cursor past the varint bytes consumed (1 byte for short form, 4 bytes for long form). Returns UNEXPECTED_EOF if insufficient bytes remain.

      This is a public method since decoders in other packages need direct byte tracking during streaming operations.

      decode for full message decoding

    • Decodes the next value from the buffer at the current cursor position.

      Advances the cursor past the decoded value. Returns raw JavaScript values (number, bigint, boolean, null, string, DateTime, Array, Map, object) rather than wrapped RelishValue objects.

      Returns Result<DecodedValue, DecodeError>

      Result containing decoded value, or DecodeError on validation failure

      import { Decoder } from '@grounds/core';

      const bytes = new Uint8Array([0x0e, 0x0a, 0x48, 0x65, 0x6c, 0x6c, 0x6f]); // String("Hello")
      const decoder = new Decoder(bytes);

      decoder.decodeValue().match(
      (value) => {
      console.log(value);
      },
      (error) => console.error(error.code)
      );