> In the one place I use it I buffer the whole ...
# squarelibraries
a
In the one place I use it I buffer the whole body before wrapping
I think the rough plan for OkHttp multiplatform JS support would be. omit the streaming data accessors, and only have one providing completed data. String, bytes.
Buffering the entire contents of a large response in memory isn't going to work for large/streaming responses. I'd imagine what I'll have to end up doing is creating an async version of Pipe and then maybe wrapping it in sum type we actually expose like:
Copy code
sealed class ByteStream {
     data class Source(val source: okio.Source): ByteStream(), okio.Source by source
     
     data class Streaming(val source: AsyncStream): ByteStream()
}

// convenience functions for most common operations that deal with the variant differences 
suspend fun ByteStream.writeToFile(file: File): Unit
suspend fun ByteStream.toByteArray(): ByteArray
suspend fun ByteStream.toBuffer(): okio.Buffer
For JVM/Native it will be relatively simple, just expose the underlying contents of the response as a
Source
and have the convenience suspend functions block on
<http://Dispatchers.IO|Dispatchers.IO>
. On JS expose an async abstraction that introduces one layer of buffering between the consumer and producer.