skeg_client/resp_codec

RESP2 and RESP3 wire codec.

The codec is pure: it never touches a socket. decode reads one frame from the front of a buffer. When the frame is not complete it returns a Decoder holding its progress; feed that decoder the next bytes from the socket and it resumes where it stopped, so a reply arriving in many fragments is examined once, not once per fragment:

case resp_codec.feed(decoder, bytes) {
  Complete(value, rest) -> // use `value`; `rest` starts the next frame
  Incomplete(decoder) -> // read more bytes and feed them to `decoder`
  Invalid(reason) -> // the stream is corrupt: close the connection
}

Errors sent by the server (-ERR ..., !<len>) are values, not decode failures: a batch reply can hold an error next to successful results.

The decoder enforces the same limits as the skeg server’s own parser, so a misbehaving peer cannot make the client buffer or recurse without bound. Streamed aggregates (*?), streamed strings ($?) and attributes (|) are not supported; skeg never sends them.

Types

Why a buffer cannot be a valid RESP stream. Every one of these means the connection is out of step with the server and must be closed.

pub type DecodeError {
  UnknownMarker(Int)
  InvalidUtf8
  InvalidInteger(String)
  InvalidLength(Int)
  HeaderTooLong
  BulkTooLarge(Int)
  AggregateTooLarge(Int)
  NestingTooDeep
  MissingTerminator
  InvalidBoolean
  InvalidDouble(String)
  InvalidVerbatim
  UnexpectedPayload
}

Constructors

  • UnknownMarker(Int)

    The first byte of a frame is not a supported type marker.

  • InvalidUtf8
  • InvalidInteger(String)

    A line that should hold an integer does not, or it is out of range.

  • InvalidLength(Int)
  • HeaderTooLong

    A length, count or integer line exceeds max_header_bytes.

  • BulkTooLarge(Int)
  • AggregateTooLarge(Int)
  • NestingTooDeep
  • MissingTerminator

    A length-prefixed payload is not followed by CRLF.

  • InvalidBoolean
  • InvalidDouble(String)
  • InvalidVerbatim
  • UnexpectedPayload

    _ is followed by bytes before its CRLF.

pub type Decoded {
  Complete(value: Value, rest: BitArray)
  Incomplete(Decoder)
  Invalid(DecodeError)
}

Constructors

  • Complete(value: Value, rest: BitArray)

    One whole frame, and the unread bytes that follow it.

  • Incomplete(Decoder)

    The bytes so far are a valid start of a frame. Pass the decoder more bytes with feed.

  • Invalid(DecodeError)

Progress through one frame: the bytes not yet consumed, the element being read, and the aggregates still open around it.

pub opaque type Decoder

A RESP3 double. Erlang floats cannot hold infinities or NaN, so those are separate constructors.

pub type Double {
  Finite(Float)
  Infinity
  NegativeInfinity
  NotANumber
}

Constructors

  • Finite(Float)
  • Infinity
  • NegativeInfinity
  • NotANumber
pub type Value {
  SimpleString(String)
  SimpleError(String)
  Integer(Int)
  BulkString(BitArray)
  Null
  Array(List(Value))
  Boolean(Bool)
  Double(Double)
  BigNumber(Int)
  BlobError(code: String, message: String)
  Verbatim(format: String, data: BitArray)
  Map(List(#(Value, Value)))
  Set(List(Value))
  Push(List(Value))
}

Constructors

  • SimpleString(String)

    +<text>

  • SimpleError(String)

    -<text>: the whole line, error code included.

  • Integer(Int)

    :<n>, a signed 64-bit integer.

  • BulkString(BitArray)

    $<len>: binary-safe bytes.

  • Null

    _, $-1 or *-1.

  • Array(List(Value))

    *<n>

  • Boolean(Bool)

    #t or #f

  • Double(Double)

    ,<double>

  • BigNumber(Int)

    (<digits>, held exactly.

  • BlobError(code: String, message: String)

    !<len>: the payload split at its first space.

  • Verbatim(format: String, data: BitArray)

    =<len>: a three-character format such as txt and the data after it.

  • Map(List(#(Value, Value)))

    %<n>: pairs in the order the server sent them.

  • Set(List(Value))

    ~<n>

  • Push(List(Value))

    ><n>: an out-of-band message pushed by the server.

Values

pub fn decode(buffer: BitArray) -> Decoded

Decodes the first frame in buffer.

Incomplete is returned only while the buffer could still become a valid frame: oversized lengths and malformed headers are rejected as soon as they are visible, without waiting for the bytes they announce.

pub fn encode_command(parts: List(BitArray)) -> BitArray

Encodes a command as an array of bulk strings, the only request form the server accepts. Arguments are sent byte for byte.

pub fn examined_bytes(decoder: Decoder) -> Int

How many bytes the decoder has examined since it was created. Every byte is examined at most once, however the input is fragmented, so this never exceeds the number of bytes fed.

pub fn feed(decoder: Decoder, bytes: BitArray) -> Decoded

Appends bytes to what the decoder has buffered and continues decoding.

pub const max_aggregate_count: Int

Largest number of elements in an array, set or push, and of pairs in a map.

pub const max_bulk_bytes: Int

Largest bulk string, blob error or verbatim string payload, in bytes.

pub const max_depth: Int

Deepest aggregate nesting. A top-level scalar is at depth 0.

pub const max_header_bytes: Int

Longest length, count or integer line, without its CRLF. A signed 64-bit integer needs at most 20 characters.

pub fn new_decoder() -> Decoder

A decoder that has not read anything yet.

✨ Search Document