# `Alternera.PluginHosts.Frame`
[🔗](https://github.com/10e-ab/alternera-camel/blob/v0.9.0/alternera_plugin_hosts/lib/alternera/plugin_hosts/frame.ex#L1)

The frames between the proxy and a plugin host: a 4-byte big-endian length
of what follows, a 1-byte type, an 8-byte call id and the payload. The
hello is JSON; a call and its reply are in the host's codec, JSON or, for
a `:msgpack` host, MessagePack (`pack/2`, `unpack/2`). A `reload` (proxy
to host) and the `catalogue` that answers it, or that a host sends of id
0 after reloading on its own, are JSON. A `define` (proxy to host) carries
a written document (`PluginHosts.Definition`) in the host's codec, and the
host answers it with a `reply` of the same id; so does a `check`, which
asks the host to check a document without defining it, and a
`vocabulary`, which asks what the editor completes in one, and a
`describe`, which asks for the tables, columns or procedures of a
database a written route may use. A `variables` (proxy to host, JSON,
`{"values": {...}}`, id 0) comes before the hello of a host whose entry
lists tenant variables, and is not answered: the values its routes on
disk read. A `source` (proxy to host, the host's codec) is a call to one
of the host's message sources, answered with a `reply` of its id; a
`messages` (host to proxy, JSON, id 0, not answered) is what a pushing
source sends to the sink its open named (`PluginHosts.Host.subscribe/3`).

The id sits outside the payload so `PluginHosts.Host` can route a
reply to its caller without decoding it; the caller decodes. The port runs
in stream mode rather than `{:packet, 4}`, so the length is read here and
checked against a limit before any of the frame is buffered: a host that
announces a frame of gigabytes is refused at its header.

# `type`

```elixir
@type type() ::
  :hello
  | :call
  | :reply
  | :cancel
  | :ping
  | :pong
  | :reload
  | :catalogue
  | :define
  | :check
  | :vocabulary
  | :describe
  | :variables
  | :source
  | :messages
```

# `encode`

```elixir
@spec encode(type(), non_neg_integer(), iodata()) :: iodata()
```

The frame as iodata.

# `header_size`

The bytes a frame adds to its payload.

# `pack`

```elixir
@spec pack(term(), :json | :msgpack) :: binary()
```

`term` as a call's or reply's payload. In MessagePack a binary that is not
UTF-8 travels as bytes, which JSON cannot carry. Raises on a term the
codec cannot write.

# `parse`

```elixir
@spec parse(binary(), pos_integer()) ::
  {:ok, type(), non_neg_integer(), binary(), binary()}
  | :more
  | {:error, term()}
```

The first whole frame in `buffer`: `{:ok, type, id, payload, rest}`,
`:more` when the frame is not all there yet, or `{:error, reason}` when
the bytes cannot be a frame (a length under the header or over
`max_bytes`, an unknown type).

# `unpack`

```elixir
@spec unpack(binary(), :json | :msgpack) :: {:ok, term()} | {:error, term()}
```

The term in a payload: `{:ok, term}` or `{:error, reason}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
