CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-server-compression-jvm

A Ktor server plugin that provides HTTP response compression and request decompression capabilities with support for gzip, deflate, and identity encoding algorithms.

Pending
Overview
Eval results
Files

encoding.mddocs/

Content Encoding

The Compression plugin supports multiple HTTP compression algorithms through the ContentEncoder interface, with built-in implementations for standard algorithms.

Built-in Encoders

GZip Encoding

fun CompressionConfig.gzip(block: CompressionEncoderBuilder.() -> Unit = {})

Configures gzip compression (RFC 1952) with default priority 1.0:

install(Compression) {
    gzip {
        priority = 0.9
        minimumSize(1024)
        matchContentType(ContentType.Text.Plain, ContentType.Application.Json)
    }
}

Deflate Encoding

fun CompressionConfig.deflate(block: CompressionEncoderBuilder.() -> Unit = {})

Configures deflate compression (RFC 1951) with default priority 0.9:

install(Compression) {
    deflate {
        priority = 0.8
        excludeContentType(ContentType.Image.Any)
    }
}

Identity Encoding

fun CompressionConfig.identity(block: CompressionEncoderBuilder.() -> Unit = {})

Configures identity encoding (no compression) as a fallback option:

install(Compression) {
    identity {
        // Identity encoding typically used as fallback
        priority = 0.1
    }
}

Encoder Configuration

class CompressionEncoderBuilder internal constructor(
    val encoder: ContentEncoder
) : ConditionsHolderBuilder {
    override val conditions: ArrayList<ApplicationCall.(OutgoingContent) -> Boolean> = arrayListOf()
    var priority: Double = 1.0
    
    internal fun buildConfig(): CompressionEncoderConfig
}

Priority System

Encoders are selected based on client Accept-Encoding headers and configured priorities. Higher priority encoders are preferred when multiple options are available:

install(Compression) {
    gzip {
        priority = 1.0  // Highest priority
    }
    deflate {
        priority = 0.9  // Second choice
    }
    identity {
        priority = 0.1  // Fallback
    }
}

Per-Encoder Conditions

Each encoder can have specific conditions that must be met for it to be used:

install(Compression) {
    gzip {
        // Only compress JSON responses larger than 500 bytes
        minimumSize(500)
        matchContentType(ContentType.Application.Json)
    }
    
    deflate {
        // Compress text content but exclude certain types
        matchContentType(ContentType.Text.Any)
        excludeContentType(ContentType.Text.EventStream)
        condition { _, content ->
            content.contentLength?.let { it > 100 } ?: true
        }
    }
}

ContentEncoder Interface

The underlying encoder implementations from io.ktor.util:

interface Encoder {
    fun encode(source: ByteReadChannel, coroutineContext: CoroutineContext = EmptyCoroutineContext): ByteReadChannel
    fun encode(source: ByteWriteChannel, coroutineContext: CoroutineContext = EmptyCoroutineContext): ByteWriteChannel
    fun decode(source: ByteReadChannel, coroutineContext: CoroutineContext = EmptyCoroutineContext): ByteReadChannel
}

interface ContentEncoder : Encoder {
    val name: String
    fun predictCompressedLength(contentLength: Long): Long?
}

object GZipEncoder : ContentEncoder {
    override val name: String = "gzip"
}

object DeflateEncoder : ContentEncoder {
    override val name: String = "deflate"
}

object IdentityEncoder : ContentEncoder {
    override val name: String = "identity"
    override fun predictCompressedLength(contentLength: Long): Long = contentLength
}

Content Negotiation

The plugin automatically handles HTTP content negotiation:

  1. Reads client Accept-Encoding header
  2. Matches available encoders with client preferences
  3. Considers encoder priorities and quality values
  4. Selects the best encoder that meets all conditions
  5. Sets appropriate Content-Encoding response header

Example Client Negotiation

Client sends: Accept-Encoding: gzip, deflate;q=0.8, *;q=0.1
Server response: Content-Encoding: gzip

The server will prefer gzip (quality 1.0) over deflate (quality 0.8) when both are available and meet the configured conditions.

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-server-compression-jvm

docs

conditions.md

configuration.md

control.md

decompression.md

encoding.md

index.md

tile.json