I have a custom brotli encoder that relies on the ...
# ktor
z
I have a custom brotli encoder that relies on the Google brotli java library. Now I'm updating to ktor 3.0.0-beta-2 and the way custom encoders are created has changed a lot. I can't figure out how to adapt the encoder for the newer version. I tried looking at how the built in ones work but it's not really clear to me
Copy code
internal object BrotliEncoder : ContentEncoder {
    override val name = "br"

    override fun CoroutineScope.encode(source: ByteReadChannel): ByteReadChannel {
        error("BrotliOutputStream not available (<https://github.com/google/brotli/issues/715>)")
    }

    override fun CoroutineScope.decode(source: ByteReadChannel): ByteReadChannel {
        return BrotliInputStream(source.toInputStream()).toByteReadChannel()
    }
}

fun ContentEncoding.Config.brotli(quality: Float? = null) {
    customEncoder(BrotliEncoder, quality)
}
a
Can you share the original implementation of the encoder?
z
The code there is the original implementation, for 2.x
a
The following code compiles with Ktor 3.0.0-beta-2:
Copy code
object BrotliEncoder : ContentEncoder, Encoder by BrotliEncoder {
    override val name = "br"
    override fun decode(source: ByteReadChannel, coroutineContext: CoroutineContext): ByteReadChannel {
        return BrotliInputStream(source.toInputStream()).toByteReadChannel()
    }

    override fun encode(source: ByteReadChannel, coroutineContext: CoroutineContext): ByteReadChannel {
        error("BrotliOutputStream not available (<https://github.com/google/brotli/issues/715>)")
    }

    override fun encode(source: ByteWriteChannel, coroutineContext: CoroutineContext): ByteWriteChannel {
        error("BrotliOutputStream not available (<https://github.com/google/brotli/issues/715>)")
    }
}

fun ContentEncodingConfig.brotli(quality: Float? = null) {
    customEncoder(BrotliEncoder, quality)
}
Can you elaborate on the problem you experience?
z
The thing confusing me was the coroutine context parameter. Wasn't sure how to use it for this case. I saw how the Deflate encoder uses GlobalScope to launch a coroutine which confused me