Alexander
11/13/2018, 10:35 AMktor
http client with nodejs
and found out that Js
engine only works within browser environment. So I took code of js engine and try to adopt it to work with http
module of nodejs. But it uses streams to read data (an example of how to read data: https://nodejs.org/api/http.html#http_http_request_url_options_callback). And I can't figure out how to do it better. Now I pass all data via channel:
class ResponseReader(response: IncommingMessage) {
private val chunksChannel = Channel<ByteArray>(UNLIMITED)
init {
response.on("data") { chunk: Uint8Array ->
chunksChannel.offer(chunk.asByteArray())
}
response.on("end") { ->
chunksChannel.close()
}
}
suspend fun readChunk(): ByteArray? {
return try {
chunksChannel.receive()
} catch (exc: ClosedReceiveChannelException) {
null
}
}
}
With this solution I cannot cancel reading data. Also as I understand it misses a backpressure propagation thus if data arrives faster then we can process it may lead to a huge memory consumption.gildor
11/13/2018, 10:45 AMAlexander
11/13/2018, 10:49 AMsuspendCancellableCoroutine
because callback passed to response.on("data", callback)
is called multiple times.gildor
11/13/2018, 12:11 PMAlexander
11/13/2018, 1:15 PM