Skip to content

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

Structure

StreamingMultipartWriterAsyncSequence

An async sequence that converts a sequence of MultipartSection values into serialized multipart data chunks.
struct StreamingMultipartWriterAsyncSequence<OutboundBody, BackingSequence, BackingBody> where OutboundBody : RangeReplaceableCollection, OutboundBody : Sendable, BackingSequence : AsyncSequence, BackingBody : RangeReplaceableCollection, BackingBody : Sendable, OutboundBody.Element == UInt8, BackingSequence.Element == MultipartSection<BackingBody>, BackingBody.Element == UInt8

Overview

This streaming writer processes multipart sections on-demand, making it memory-efficient for large multipart messages. It’s particularly useful when working with file uploads or when you need to stream multipart data without buffering the entire message in memory.

It is the mirror image of StreamingMultipartParserAsyncSequence, and serializes exactly the sections it is given: the boundaries separating the parts, and the one ending the message, have to appear in the backing sequence. A part is a .boundary(end: false), then its .headerFields, then one or more .bodyChunks.

let sections: [MultipartSection<ArraySlice<UInt8>>] = [
    .boundary(end: false),
    .headerFields([.contentType: "text/plain"]),
    .bodyChunk(ArraySlice("Hello, world!".utf8)),
    .boundary(end: true),
]

let writer = StreamingMultipartWriterAsyncSequence(
    backingSequence: sections.async,  // any AsyncSequence of sections
    boundary: "boundary123",
    outboundBody: ArraySlice<UInt8>.self
)

for try await chunk in writer {
    // Process each serialized chunk
}

Topics

Structures

Initializers

Instance Methods

Relationships

Conforms To

  • _Concurrency.AsyncSequence