Skip to content

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

Initializer

init(stream:count:)

Creates a chunked, streaming HTTP Response body.
init(stream: @escaping @Sendable (borrowing any HTTPBodyWriter & ~Escapable) async throws -> Void, count: Int?) throws

Parameters

stream

The closure that writes the body chunks.

count

The number of bytes that will be written. The stream MUST produce exactly count bytes. nil means the length is not known in advance, and the response is chunked.

Discussion

The closure receives a HTTPBodyWriter and writes chunks to it with await. Writes are backpressured by the transport, so the closure naturally throttles to the speed of the client. Throwing from the closure fails the response.

Throws

Response.Body.NegativeCountError if count is negative.