Skip to content

You're viewing documentation for a pre-release version. View the latest stable version

Structure

StreamingMultipartParserAsyncSequence

A sequence that parses a stream of multipart data into sections asynchronously.
struct StreamingMultipartParserAsyncSequence<BackingSequence> where BackingSequence : AsyncSequence, BackingSequence.Element : RangeReplaceableCollection, BackingSequence.Element : Sendable, BackingSequence.Element.Element == UInt8

Overview

This sequence is designed to be used with AsyncStream to parse a stream of data asynchronously. The sequence will yield MultipartSection values as they are parsed from the stream.

Each body chunk is yielded as soon as it is parsed, so the body of a part is never held in memory in its entirety. This makes the sequence suited to large messages such as file uploads. When parts are small enough to collect whole, MultipartParserAsyncSequence is easier to use: it yields each part’s body as a single section.

let sequence = StreamingMultipartParserAsyncSequence(boundary: "boundary123", buffer: stream)

for try await section in sequence {
    switch section {
    case .headerFields(let fields): print(fields)
    case .bodyChunk(let chunk): try await file.write(contentsOf: chunk)
    case .boundary(let end): print(end ? "message finished" : "part finished")
    }
}

Note

The sequence is single-pass. Iterating it more than once is not supported.

Topics

Structures

Initializers

Instance Methods

Relationships

Conforms To

  • _Concurrency.AsyncSequence