Packages
Typed distributed messaging for Gleam on the BEAM.
Retired package: Deprecated - The project needs to be redesigned around a much smaller and clearer core.
Current section
Files
Jump to
Current section
Files
src/distribute@handshake.erl
-module(distribute@handshake).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/distribute/handshake.gleam").
-export([peek_envelope_tag/1, wrap_envelope/3, unwrap_envelope/1, capability_encoder/0, capability_sized_decoder/0, hello_encoder/0, hello_sized_decoder/0, capabilities_encoder/0, capabilities_sized_decoder/0, accept_encoder/0, accept_sized_decoder/0, reject_encoder/0, reject_sized_decoder/0, keyexchange_encoder/0, keyexchange_sized_decoder/0, hello_schema/0, capabilities_schema/0, accept_schema/0, reject_schema/0, keyexchange_schema/0, encode_hello/1, decode_hello/1, encode_capabilities/1, decode_capabilities/1, encode_accept/1, decode_accept/1, encode_reject/1, decode_reject/1, encode_keyexchange/1, decode_keyexchange/1]).
-export_type([capability/0, hello/0, capabilities_msg/0, accept/0, reject/0, key_exchange_msg/0, envelope/0, member_metadata/0, handshake_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type capability() :: {capability,
binary(),
integer(),
integer(),
list({binary(), binary()})}.
-type hello() :: {hello,
binary(),
list({binary(), binary()}),
list(capability())}.
-type capabilities_msg() :: {capabilities_msg, list(capability())}.
-type accept() :: {accept, binary(), integer(), list({binary(), binary()})}.
-type reject() :: {reject, binary()}.
-type key_exchange_msg() :: {key_exchange_msg, bitstring()}.
-type envelope() :: {envelope, binary(), integer(), bitstring()}.
-type member_metadata() :: {member_metadata,
list({binary(), integer()}),
gleam@option:option(distribute@crypto@provider:secure_context())}.
-type handshake_error() :: {invalid_envelope, binary()} |
{unsupported_protocol, binary()} |
{timeout, binary()} |
{crypto_error, binary()}.
-file("src/distribute/handshake.gleam", 76).
?DOC(
" Peeks at the envelope tag and version without consuming the data.\n"
"\n"
" Returns the tag string and version number if the envelope is valid.\n"
).
-spec peek_envelope_tag(bitstring()) -> {ok, {binary(), integer()}} |
{error, handshake_error()}.
peek_envelope_tag(Data) ->
case distribute@codec:peek_envelope(Data) of
{ok, T} ->
{ok, T};
{error, _} ->
{error, {invalid_envelope, <<"invalid envelope"/utf8>>}}
end.
-file("src/distribute/handshake.gleam", 88).
?DOC(
" Wraps a payload in an envelope with the given tag and version.\n"
"\n"
" Returns the complete envelope as a BitArray ready for transmission.\n"
).
-spec wrap_envelope(binary(), integer(), bitstring()) -> bitstring().
wrap_envelope(Tag, Version, Payload) ->
distribute@codec:wrap_envelope(Tag, Version, Payload).
-file("src/distribute/handshake.gleam", 95).
?DOC(
" Unwraps an envelope, extracting the tag, version, and payload.\n"
"\n"
" Returns a tuple of (tag, version, payload) or an error if invalid.\n"
).
-spec unwrap_envelope(bitstring()) -> {ok, {binary(), integer(), bitstring()}} |
{error, handshake_error()}.
unwrap_envelope(Data) ->
case distribute@codec:unwrap_envelope(Data) of
{ok, R} ->
{ok, R};
{error, _} ->
{error, {invalid_envelope, <<"invalid envelope"/utf8>>}}
end.
-file("src/distribute/handshake.gleam", 109).
-spec pair_encoder() -> fun(({binary(), binary()}) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
pair_encoder() ->
fun(Pair) ->
{A, B} = Pair,
gleam@result:'try'(
distribute@codec:encode(distribute@codec:string_encoder(), A),
fun(A_bytes) ->
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:string_encoder(),
B
),
fun(B_bytes) ->
{ok, gleam@bit_array:append(A_bytes, B_bytes)}
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 118).
-spec pair_sized_decoder() -> fun((bitstring()) -> {ok,
{{binary(), binary()}, bitstring()}} |
{error, distribute@codec:decode_error()}).
pair_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:string_sized_decoder())(Data),
fun(_use0) ->
{A, Rest} = _use0,
gleam@result:'try'(
(distribute@codec:string_sized_decoder())(Rest),
fun(_use0@1) ->
{B, Rest2} = _use0@1,
{ok, {{A, B}, Rest2}}
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 127).
?DOC(" Creates an encoder for serializing Capability values to binary format.\n").
-spec capability_encoder() -> fun((capability()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
capability_encoder() ->
fun(C) ->
Meta_enc = distribute@codec:list_encoder(pair_encoder()),
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:string_encoder(),
erlang:element(2, C)
),
fun(Proto) ->
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:int_encoder(),
erlang:element(3, C)
),
fun(Min_b) ->
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:int_encoder(),
erlang:element(4, C)
),
fun(Max_b) ->
gleam@result:'try'(
distribute@codec:encode(
Meta_enc,
erlang:element(5, C)
),
fun(Meta_b) ->
{ok,
gleam@bit_array:append(
Proto,
gleam@bit_array:append(
Min_b,
gleam@bit_array:append(
Max_b,
Meta_b
)
)
)}
end
)
end
)
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 142).
?DOC(" Creates a sized decoder for deserializing Capability values from binary format.\n").
-spec capability_sized_decoder() -> fun((bitstring()) -> {ok,
{capability(), bitstring()}} |
{error, distribute@codec:decode_error()}).
capability_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:string_sized_decoder())(Data),
fun(_use0) ->
{Protocol, Rest1} = _use0,
gleam@result:'try'(
(distribute@codec:int_sized_decoder())(Rest1),
fun(_use0@1) ->
{Min, Rest2} = _use0@1,
gleam@result:'try'(
(distribute@codec:int_sized_decoder())(Rest2),
fun(_use0@2) ->
{Max, Rest3} = _use0@2,
gleam@result:'try'(
(distribute@codec:list_sized_decoder(
pair_sized_decoder()
))(Rest3),
fun(_use0@3) ->
{Meta, Rest4} = _use0@3,
{ok,
{{capability,
Protocol,
Min,
Max,
Meta},
Rest4}}
end
)
end
)
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 155).
?DOC(" Creates an encoder for serializing Hello messages to binary format.\n").
-spec hello_encoder() -> fun((hello()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
hello_encoder() ->
fun(H) ->
Info_enc = distribute@codec:list_encoder(pair_encoder()),
Cap_enc = distribute@codec:list_encoder(capability_encoder()),
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:string_encoder(),
erlang:element(2, H)
),
fun(Id_b) ->
gleam@result:'try'(
distribute@codec:encode(Info_enc, erlang:element(3, H)),
fun(Info_b) ->
gleam@result:'try'(
distribute@codec:encode(
Cap_enc,
erlang:element(4, H)
),
fun(Caps_b) ->
{ok,
gleam@bit_array:append(
Id_b,
gleam@bit_array:append(Info_b, Caps_b)
)}
end
)
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 167).
?DOC(" Creates a sized decoder for deserializing Hello messages from binary format.\n").
-spec hello_sized_decoder() -> fun((bitstring()) -> {ok, {hello(), bitstring()}} |
{error, distribute@codec:decode_error()}).
hello_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:string_sized_decoder())(Data),
fun(_use0) ->
{Node_id, Rest1} = _use0,
gleam@result:'try'(
(distribute@codec:list_sized_decoder(pair_sized_decoder()))(
Rest1
),
fun(_use0@1) ->
{Node_info, Rest2} = _use0@1,
gleam@result:'try'(
(distribute@codec:list_sized_decoder(
capability_sized_decoder()
))(Rest2),
fun(_use0@2) ->
{Capabilities, Rest3} = _use0@2,
{ok,
{{hello, Node_id, Node_info, Capabilities},
Rest3}}
end
)
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 181).
?DOC(" Creates an encoder for serializing CapabilitiesMsg to binary format.\n").
-spec capabilities_encoder() -> fun((capabilities_msg()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
capabilities_encoder() ->
fun(M) ->
Enc = distribute@codec:list_encoder(capability_encoder()),
distribute@codec:encode(Enc, erlang:element(2, M))
end.
-file("src/distribute/handshake.gleam", 189).
?DOC(" Creates a sized decoder for deserializing CapabilitiesMsg from binary format.\n").
-spec capabilities_sized_decoder() -> fun((bitstring()) -> {ok,
{capabilities_msg(), bitstring()}} |
{error, distribute@codec:decode_error()}).
capabilities_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:list_sized_decoder(capability_sized_decoder()))(
Data
),
fun(_use0) ->
{Caps, Rest} = _use0,
{ok, {{capabilities_msg, Caps}, Rest}}
end
)
end.
-file("src/distribute/handshake.gleam", 199).
?DOC(" Creates an encoder for serializing Accept messages to binary format.\n").
-spec accept_encoder() -> fun((accept()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
accept_encoder() ->
fun(A) ->
Params_enc = distribute@codec:list_encoder(pair_encoder()),
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:string_encoder(),
erlang:element(2, A)
),
fun(P) ->
gleam@result:'try'(
distribute@codec:encode(
distribute@codec:int_encoder(),
erlang:element(3, A)
),
fun(Vb) ->
gleam@result:'try'(
distribute@codec:encode(
Params_enc,
erlang:element(4, A)
),
fun(Pb) ->
{ok,
gleam@bit_array:append(
P,
gleam@bit_array:append(Vb, Pb)
)}
end
)
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 210).
?DOC(" Creates a sized decoder for deserializing Accept messages from binary format.\n").
-spec accept_sized_decoder() -> fun((bitstring()) -> {ok,
{accept(), bitstring()}} |
{error, distribute@codec:decode_error()}).
accept_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:string_sized_decoder())(Data),
fun(_use0) ->
{Protocol, Rest1} = _use0,
gleam@result:'try'(
(distribute@codec:int_sized_decoder())(Rest1),
fun(_use0@1) ->
{Version, Rest2} = _use0@1,
gleam@result:'try'(
(distribute@codec:list_sized_decoder(
pair_sized_decoder()
))(Rest2),
fun(_use0@2) ->
{Params, Rest3} = _use0@2,
{ok,
{{accept, Protocol, Version, Params}, Rest3}}
end
)
end
)
end
)
end.
-file("src/distribute/handshake.gleam", 222).
?DOC(" Creates an encoder for serializing Reject messages to binary format.\n").
-spec reject_encoder() -> fun((reject()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
reject_encoder() ->
fun(R) ->
distribute@codec:encode(
distribute@codec:string_encoder(),
erlang:element(2, R)
)
end.
-file("src/distribute/handshake.gleam", 227).
?DOC(" Creates a sized decoder for deserializing Reject messages from binary format.\n").
-spec reject_sized_decoder() -> fun((bitstring()) -> {ok,
{reject(), bitstring()}} |
{error, distribute@codec:decode_error()}).
reject_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:string_sized_decoder())(Data),
fun(_use0) ->
{Reason, Rest} = _use0,
{ok, {{reject, Reason}, Rest}}
end
)
end.
-file("src/distribute/handshake.gleam", 235).
?DOC(" Creates an encoder for serializing KeyExchangeMsg to binary format.\n").
-spec keyexchange_encoder() -> fun((key_exchange_msg()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}).
keyexchange_encoder() ->
fun(K) ->
distribute@codec:encode(
distribute@codec:bitarray_encoder(),
erlang:element(2, K)
)
end.
-file("src/distribute/handshake.gleam", 240).
?DOC(" Creates a sized decoder for deserializing KeyExchangeMsg from binary format.\n").
-spec keyexchange_sized_decoder() -> fun((bitstring()) -> {ok,
{key_exchange_msg(), bitstring()}} |
{error, distribute@codec:decode_error()}).
keyexchange_sized_decoder() ->
fun(Data) ->
gleam@result:'try'(
(distribute@codec:bitarray_sized_decoder())(Data),
fun(_use0) ->
{Payload, Rest} = _use0,
{ok, {{key_exchange_msg, Payload}, Rest}}
end
)
end.
-file("src/distribute/handshake.gleam", 248).
?DOC(" Returns the codec schema for Hello messages with envelope support.\n").
-spec hello_schema() -> distribute@codec:schema(hello()).
hello_schema() ->
distribute@codec:new_schema(
<<"distribute:hello"/utf8>>,
1,
hello_encoder(),
distribute@codec:to_decoder(hello_sized_decoder())
).
-file("src/distribute/handshake.gleam", 258).
?DOC(" Returns the codec schema for CapabilitiesMsg with envelope support.\n").
-spec capabilities_schema() -> distribute@codec:schema(capabilities_msg()).
capabilities_schema() ->
distribute@codec:new_schema(
<<"distribute:capabilities"/utf8>>,
1,
capabilities_encoder(),
distribute@codec:to_decoder(capabilities_sized_decoder())
).
-file("src/distribute/handshake.gleam", 268).
?DOC(" Returns the codec schema for Accept messages with envelope support.\n").
-spec accept_schema() -> distribute@codec:schema(accept()).
accept_schema() ->
distribute@codec:new_schema(
<<"distribute:accept"/utf8>>,
1,
accept_encoder(),
distribute@codec:to_decoder(accept_sized_decoder())
).
-file("src/distribute/handshake.gleam", 278).
?DOC(" Returns the codec schema for Reject messages with envelope support.\n").
-spec reject_schema() -> distribute@codec:schema(reject()).
reject_schema() ->
distribute@codec:new_schema(
<<"distribute:reject"/utf8>>,
1,
reject_encoder(),
distribute@codec:to_decoder(reject_sized_decoder())
).
-file("src/distribute/handshake.gleam", 288).
?DOC(" Returns the codec schema for KeyExchangeMsg with envelope support.\n").
-spec keyexchange_schema() -> distribute@codec:schema(key_exchange_msg()).
keyexchange_schema() ->
distribute@codec:new_schema(
<<"distribute:keyexchange"/utf8>>,
1,
keyexchange_encoder(),
distribute@codec:to_decoder(keyexchange_sized_decoder())
).
-file("src/distribute/handshake.gleam", 298).
?DOC(" Encodes a Hello message to binary format with envelope wrapper.\n").
-spec encode_hello(hello()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}.
encode_hello(H) ->
distribute@codec:schema_encode(hello_schema(), H).
-file("src/distribute/handshake.gleam", 303).
?DOC(" Decodes a Hello message from binary format, unwrapping the envelope.\n").
-spec decode_hello(bitstring()) -> {ok, hello()} |
{error, distribute@codec:decode_error()}.
decode_hello(Data) ->
distribute@codec:schema_decode(hello_schema(), Data).
-file("src/distribute/handshake.gleam", 308).
?DOC(" Encodes a CapabilitiesMsg to binary format with envelope wrapper.\n").
-spec encode_capabilities(capabilities_msg()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}.
encode_capabilities(M) ->
distribute@codec:schema_encode(capabilities_schema(), M).
-file("src/distribute/handshake.gleam", 315).
?DOC(" Decodes a CapabilitiesMsg from binary format, unwrapping the envelope.\n").
-spec decode_capabilities(bitstring()) -> {ok, capabilities_msg()} |
{error, distribute@codec:decode_error()}.
decode_capabilities(Data) ->
distribute@codec:schema_decode(capabilities_schema(), Data).
-file("src/distribute/handshake.gleam", 322).
?DOC(" Encodes an Accept message to binary format with envelope wrapper.\n").
-spec encode_accept(accept()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}.
encode_accept(A) ->
distribute@codec:schema_encode(accept_schema(), A).
-file("src/distribute/handshake.gleam", 327).
?DOC(" Decodes an Accept message from binary format, unwrapping the envelope.\n").
-spec decode_accept(bitstring()) -> {ok, accept()} |
{error, distribute@codec:decode_error()}.
decode_accept(Data) ->
distribute@codec:schema_decode(accept_schema(), Data).
-file("src/distribute/handshake.gleam", 332).
?DOC(" Encodes a Reject message to binary format with envelope wrapper.\n").
-spec encode_reject(reject()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}.
encode_reject(R) ->
distribute@codec:schema_encode(reject_schema(), R).
-file("src/distribute/handshake.gleam", 337).
?DOC(" Decodes a Reject message from binary format, unwrapping the envelope.\n").
-spec decode_reject(bitstring()) -> {ok, reject()} |
{error, distribute@codec:decode_error()}.
decode_reject(Data) ->
distribute@codec:schema_decode(reject_schema(), Data).
-file("src/distribute/handshake.gleam", 342).
?DOC(" Encodes a KeyExchangeMsg to binary format with envelope wrapper.\n").
-spec encode_keyexchange(key_exchange_msg()) -> {ok, bitstring()} |
{error, distribute@codec:encode_error()}.
encode_keyexchange(K) ->
distribute@codec:schema_encode(keyexchange_schema(), K).
-file("src/distribute/handshake.gleam", 349).
?DOC(" Decodes a KeyExchangeMsg from binary format, unwrapping the envelope.\n").
-spec decode_keyexchange(bitstring()) -> {ok, key_exchange_msg()} |
{error, distribute@codec:decode_error()}.
decode_keyexchange(Data) ->
distribute@codec:schema_decode(keyexchange_schema(), Data).