Skeg. RESP
(skeg v1.0.0)
Copy Markdown
RESP2 and RESP3 wire codec.
The codec is pure: it never touches a socket. decode/2 reads one frame
from the front of the bytes it is given, continuing from a decoder state.
When the frame is not complete it returns the state holding its progress;
pass that state 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 Skeg.RESP.decode(bytes, state) do
{:ok, value, rest, state} -> # use value; rest starts the next frame
{:incomplete, state} -> # read more bytes and decode them with state
{:error, reason} -> # the stream is corrupt: close the connection
endValues decode to:
- simple and bulk strings: binaries
- nulls (
_,$-1,*-1):nil - integers and big numbers: integers
- booleans:
trueandfalse - doubles: floats, or
:infinity,:neg_infinity,:nan - arrays: lists
- maps:
{:map, [{key, value}]}, pairs in the order the server sent them - sets:
{:set, list}; pushes:{:push, list} - verbatim strings:
{:verbatim, format, data} - errors (
-and!):%Skeg.Error{kind: :server}. They are values, not decode failures: a batch reply can hold an error next to results.
No atom is ever created from remote input.
The decoder enforces the same limits as the skeg server's own parser, so a
misbehaving peer cannot make the client buffer or nest without bound.
Streamed aggregates (*?), streamed strings ($?) and attributes (|)
are not supported; skeg never sends them.
Summary
Functions
Appends bytes to what state has buffered and decodes one frame.
Encodes a command as an array of bulk strings, the only request form the
server accepts. Every argument must be a binary and is sent byte for byte;
anything else raises ArgumentError rather than being stringified.
Bytes the decoder has examined since it was created. Every byte is examined at most once, however the input is fragmented.
Largest number of elements in an aggregate, or of pairs in a map.
Largest bulk string, blob error or verbatim payload, in bytes.
Deepest aggregate nesting. A top-level scalar is at depth 0.
Longest length, count or integer line, without its CRLF.
A decoder that has not read anything yet.
Types
@type reason() :: {:unknown_marker, byte()} | :invalid_utf8 | {:invalid_integer, String.t()} | {:invalid_length, integer()} | :header_too_long | {:bulk_too_large, integer()} | {:aggregate_too_large, integer()} | :nesting_too_deep | :missing_terminator | :invalid_boolean | {:invalid_double, String.t()} | :invalid_verbatim | :unexpected_payload
Why a byte stream cannot be RESP. The connection must be closed.
@opaque t()
Functions
@spec decode(binary(), t()) :: {:ok, value(), binary(), t()} | {:incomplete, t()} | {:error, reason()}
Appends bytes to what state has buffered and decodes one frame.
{:incomplete, state} is returned only while the bytes 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. The state
returned with a complete frame is a fresh decoder.
Encodes a command as an array of bulk strings, the only request form the
server accepts. Every argument must be a binary and is sent byte for byte;
anything else raises ArgumentError rather than being stringified.
@spec examined_bytes(t()) :: non_neg_integer()
Bytes the decoder has examined since it was created. Every byte is examined at most once, however the input is fragmented.
Largest number of elements in an aggregate, or of pairs in a map.
Largest bulk string, blob error or verbatim payload, in bytes.
Deepest aggregate nesting. A top-level scalar is at depth 0.
Longest length, count or integer line, without its CRLF.
@spec new() :: t()
A decoder that has not read anything yet.