-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
type-erased HTMLResponse + headers (#4)
- Loading branch information
Showing
4 changed files
with
92 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Elementary | ||
import Vapor | ||
|
||
struct HTMLResponseBodyStreamWriter: HTMLStreamWriter { | ||
let allocator: ByteBufferAllocator = .init() | ||
var writer: any AsyncBodyStreamWriter | ||
|
||
mutating func write(_ bytes: ArraySlice<UInt8>) async throws { | ||
try await self.writer.writeBuffer(self.allocator.buffer(bytes: bytes)) | ||
} | ||
} | ||
|
||
public extension AsyncBodyStreamWriter { | ||
/// Writes HTML by rendering chuncks of bytes to the response body | ||
/// | ||
/// - Parameters: | ||
/// - html: The HTML content to render in the response | ||
/// - chunkSize: The number of bytes to write to the response body at a time (default is 1024 bytes) | ||
func writeHTML(_ html: consuming some HTML, chunkSize: Int = 1204) async throws { | ||
try await html.render( | ||
into: HTMLResponseBodyStreamWriter(writer: self), | ||
chunkSize: chunkSize | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters