multipartkit/stream

Types

One streamed multipart part.

body is single-pass; once consumed it cannot be replayed. Errors that arise while reading the body (PartTooLarge, UnexpectedEndOfInput, etc.) are surfaced inline as Error(_) items rather than swallowed.

In v0.1.0 the input chunks yielder is consumed lazily, but each StreamPart body is materialised as a single buffered chunk before the part is yielded. The body yielder therefore always emits at most one Ok(BitArray) (or one Error(_)). Per-part memory is bounded by max_part_bytes. True chunk-by-chunk body streaming is on the roadmap but is not part of v0.1.0.

Note: the spec lists body as iterator.Iterator(_). v0.1.0 uses gleam/yielder.Yielder(_) because gleam_stdlib 1.0.0 dropped the iterator module. The streaming surface is experimental and the type may change before v1.0.0.

pub type StreamPart {
  StreamPart(
    headers: List(#(String, String)),
    name: option.Option(String),
    filename: option.Option(String),
    content_type: option.Option(String),
    body: yielder.Yielder(Result(BitArray, error.MultipartError)),
  )
}

Constructors

Values

pub fn drain_body(
  source: yielder.Yielder(Result(BitArray, error.MultipartError)),
) -> Result(BitArray, error.MultipartError)

Consume a StreamPart’s body yielder and return the concatenated bytes.

Stops at the first Error(_) and returns it. Because StreamPart.body in v0.1.0 always emits a single buffered chunk, this is a constant-time pull over a one-element yielder; future releases that switch to true chunked body streaming will still let this helper drain the whole body.

pub fn from_datastream(
  source: yielder.Yielder(BitArray),
) -> yielder.Yielder(BitArray)

Adapter that returns the source unchanged. Exists so callers can write source |> from_datastream in a pipeline.

pub fn from_part(the_part: part.Part) -> StreamPart

Build a StreamPart from a fully buffered Part.

Useful when feeding parts into encode_stream or when adapting a buffered parse result into the streaming API surface.

pub fn parse_stream(
  chunks: yielder.Yielder(BitArray),
  content_type: String,
) -> Result(
  yielder.Yielder(Result(StreamPart, error.MultipartError)),
  error.MultipartError,
)

Parse a stream of input chunks using default_limits().

The outer Result reports errors decidable from content_type alone. Each yielded item is a Result because once body consumption begins, failures can arise later from message structure, limits, or truncation. After the first Error(_) is yielded the iterator is exhausted.

pub fn parse_stream_with_limits(
  chunks: yielder.Yielder(BitArray),
  content_type: String,
  limits: limit.Limits,
) -> Result(
  yielder.Yielder(Result(StreamPart, error.MultipartError)),
  error.MultipartError,
)

Parse a stream of input chunks with caller-supplied limits.

Chunks are pulled lazily: bytes are consumed from chunks only as needed to deliver the next part’s headers and body. max_body_bytes is enforced incrementally as chunks arrive, so an oversized stream is rejected before it is fully buffered. Per-part memory is bounded by max_part_bytes.

pub fn to_datastream(
  source: yielder.Yielder(BitArray),
) -> yielder.Yielder(BitArray)

Adapter that returns the source unchanged. Mirror of from_datastream.

Search Document