Write to a stream vs String
Wouldn't it be better for handlers to be writing to a stream instead of to a string? Could be better for large files and if you ever want to handle multiple handlers for one request.
Yes. I completely agree. Do you know any Swift protocol similar to Java's OutputStream ? I think it should allow to write raw bytes.
No, I was searching for it a bit but couldn't find it.
Will we end up with protocol JavaOutputStream :P ?
:) I asked this on stackoverflow: http://stackoverflow.com/questions/24184429/does-swift-have-a-protocol-for-writing-a-stream-of-bytes
@apsanz so we're having WWDC2015 right now and we still have Streamable protocol in the standard library. I would like to start a discussion about how we can use it to provide bytes "streaming" api. Right now, swifter's handler design requires to load the data to the memory and then write it the socket. For big files it's better to buffer the data in a loop with read/write operations.
Streamable Doc: https://developer.apple.com/library/prerelease/watchos/documentation/Swift/Reference/Swift_Streamable_Protocol/#//apple_ref/swift/intfm/Streamable/s:FPSs10Streamable7writeTou__Rq_S_qd__Ss16OutputStreamType_Fq_FRqd__T_
The protocol is supported by following native types: String, Character, UnicodeScalar. Let's have a look at the source code:
protocol OutputStreamType {
/// Append the given `string` to this stream.
mutating func write(string: String)
}
/// A source of text streaming operations. `Streamable` instances can
/// be written to any *output stream*.
///
/// For example: `String`, `Character`, `UnicodeScalar`.
protocol Streamable {
/// Write a textual representation of `self` into `target`
func writeTo<Target : OutputStreamType>(inout target: Target)
}
extension String : OutputStreamType {
/// Append `other` to this stream.
mutating func write(other: String)
}
extension String : Streamable {
/// Write a textual representation of `self` into `target`
func writeTo<Target : OutputStreamType>(inout target: Target)
}
Here is what we can do with this design:
- Extend native types and AnyObject ( for JSONing ) by ::writeTo(response) method. Examples:
server["/"] = { response, request in
"Hello World".writeTo(&response);
["posts" : [[ "id" : 1, "message" : "hello world"],[ "id" : 2, "message" : "sample message"]].writeTo(&response)
}
I see two problems here:
a) the response will not be provided by the "return" statement. We lose this enum based DSL for responses. It's more like in Go programming language when you got a request object and response writer.
b) It just looks weird.
We can iterate more on this and find GO language like approach:
server["/"] = { responseWriter, request in
responseWriter.writeHeader("Set-Cookie", "")
responseWriter.writeStatus(200)
responseWriter.writeBody("Hello World") // string
responseWriter.writeBody(AnyObject) // json
responseWriter.writeBody(..chunk of bytes...)
}
Here, the writer must be smart. The headers, staus code and the body needs to be flushed to the socket in a right order.
What do you think ?
Just a quick update here. We switched to [UInt8] as a medium for sharing the content of response.
hi @apsanz
first version of streaming API is ready ( https://github.com/glock45/swifter/commit/cec9a93c2fab3d1d59538116e8cd9a71cf546b90).
example: https://github.com/glock45/swifter/blob/master/Sources/DemoServer.swift#L106
best dk