# `Alternera.Plugin.Body`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/body.ex#L1)

A body a pipeline holds, as the codecs hand it to the steps: in memory
(`{:inline, binary}`) or spooled to a file in the body storage directory
(`{:file, path, size}`, `Proxy.BodySpool`).

The plugin callbacks keep taking and returning binaries: the runner reads
a file-backed body inside the step's own process (`Pipelines.Runner`), so
the exchange's process never holds it. A body no step changed goes on as
the handle it came as.

Whoever spooled a body releases it (`release/1`) once the exchange no
longer needs it.

# `t`

```elixir
@type t() :: {:inline, binary()} | {:file, Path.t(), non_neg_integer()}
```

# `file?`

```elixir
@spec file?(term()) :: boolean()
```

Whether `body` is spooled to a file.

# `from_tee`

```elixir
@spec from_tee(
  {:inline, binary(), non_neg_integer(), boolean()}
  | {:file, String.t(), non_neg_integer()}
  | {:file_truncated, String.t(), non_neg_integer()}
  | {:discarded, non_neg_integer()}
  | {:capture_failed, non_neg_integer()}
) :: {:ok, t()} | {:error, :spool_failed | :incomplete}
```

The handle for a `Proxy.BodyTee.finalize/1` result. A failed file write
is `{:error, :spool_failed}`; a capture that kept only part of the body
(truncated or discarded) is `{:error, :incomplete}`; a capped file is
removed with it.

# `handle?`

```elixir
@spec handle?(term()) :: boolean()
```

Whether `term` is a body handle (not a bare binary).

# `is_body`
*macro* 

A binary or a body handle: what `wrap/1` takes.

# `keep_fresh`

```elixir
@spec keep_fresh(Path.t(), integer() | nil) :: integer()
```

Refreshes the modification time of a held file a reader is still
sending, at most once a minute: `TempFileCleanup` takes a held file by
its age, so a body written long ago and sent slowly (a client reading a
large response at its own pace) must not look orphaned. `touched_at` is
the monotonic time (ms) this returned last, nil before the first read,
which touches at once. Returns the time of the last touch.

# `link`

```elixir
@spec link(t()) :: {:ok, t()} | {:error, File.posix()}
```

A second name for a file body (a hard link beside it, same filesystem, no
copy), so a reader that outlives the body's owner keeps the bytes after
the owner's `release/1`: the recording of a rewritten body
(`Streaming.Recording`). An inline body is returned as it is.

# `read`

```elixir
@spec read(t()) :: binary()
```

The whole body as a binary. Raises `File.Error` when a file body cannot
be read: a spooled body that is gone is a bug in its owner's release. The
caller bounds the read by checking `size/1` against its cap first.

# `read_prefix`

```elixir
@spec read_prefix(t(), non_neg_integer()) :: binary()
```

At most `max` bytes from the start of the body.

# `release`

```elixir
@spec release(t() | nil) :: :ok
```

Removes a file body's file; a no-op for an inline body, nil and a file
already gone. Any other failure is logged and `:ok` all the same: the
`TempFileCleanup` sweep is the safety net.

# `size`

```elixir
@spec size(t()) :: non_neg_integer()
```

The body's size in bytes.

# `stream`

```elixir
@spec stream(t(), pos_integer()) :: Enumerable.t()
```

The body in chunks of `chunk_bytes` (64 KiB by default), the last one
shorter; nothing for an empty body. A file is read as the stream is
consumed, and kept fresh while it is (`keep_fresh/2`).

# `wrap`

```elixir
@spec wrap(binary() | t()) :: t()
```

`body` as a handle: a binary becomes inline; a handle stays as it is.

---

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