or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

conditions.mdconfiguration.mdcontrol.mddecompression.mdencoding.mdindex.md

configuration.mddocs/

0

# Plugin Configuration

1

2

The Compression plugin provides flexible configuration options for controlling both response compression and request decompression behavior.

3

4

## Installation

5

6

```kotlin { .api }

7

val Compression: RouteScopedPlugin<CompressionConfig>

8

```

9

10

Install the plugin in your Ktor application:

11

12

```kotlin

13

fun Application.module() {

14

install(Compression) {

15

// Configuration goes here

16

}

17

}

18

```

19

20

## Configuration Class

21

22

```kotlin { .api }

23

@KtorDsl

24

class CompressionConfig : ConditionsHolderBuilder {

25

var mode: Mode = Mode.All

26

val encoders: MutableMap<String, CompressionEncoderBuilder> = hashMapOf()

27

override val conditions: MutableList<ApplicationCall.(OutgoingContent) -> Boolean> = arrayListOf()

28

29

fun encoder(encoder: ContentEncoder, block: CompressionEncoderBuilder.() -> Unit = {})

30

fun default()

31

internal fun buildOptions(): CompressionOptions

32

}

33

```

34

35

## Operation Modes

36

37

Control whether the plugin compresses responses, decompresses requests, or both:

38

39

```kotlin { .api }

40

enum class CompressionConfig.Mode(

41

internal val request: Boolean,

42

internal val response: Boolean

43

) {

44

CompressResponse(false, true),

45

DecompressRequest(true, false),

46

All(true, true)

47

}

48

```

49

50

### Usage Examples

51

52

```kotlin

53

install(Compression) {

54

// Only compress outgoing responses (default behavior for most use cases)

55

mode = CompressionConfig.Mode.CompressResponse

56

}

57

58

install(Compression) {

59

// Only decompress incoming requests

60

mode = CompressionConfig.Mode.DecompressRequest

61

}

62

63

install(Compression) {

64

// Both compress responses and decompress requests (default)

65

mode = CompressionConfig.Mode.All

66

}

67

```

68

69

## Default Configuration

70

71

The plugin provides sensible defaults when no explicit configuration is provided:

72

73

```kotlin { .api }

74

fun CompressionConfig.default()

75

```

76

77

This enables:

78

- **gzip** encoding with default priority (1.0)

79

- **deflate** encoding with priority 0.9

80

- **identity** encoding (no compression) with default priority (1.0)

81

- Default content exclusions (images, videos, audio, multipart, SSE)

82

- Minimum compression size of 200 bytes

83

84

```kotlin

85

install(Compression) {

86

// Equivalent to calling default() explicitly

87

default()

88

}

89

90

// Or simply:

91

install(Compression)

92

// Automatically applies default configuration if no encoders are specified

93

```

94

95

## Custom Encoder Registration

96

97

Register custom or additional encoders:

98

99

```kotlin { .api }

100

fun CompressionConfig.encoder(

101

encoder: ContentEncoder,

102

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

103

)

104

```

105

106

```kotlin

107

install(Compression) {

108

// Custom encoder with specific configuration

109

encoder(MyCustomEncoder) {

110

priority = 0.8

111

minimumSize(1024)

112

matchContentType(ContentType.Application.Json)

113

}

114

}

115

```

116

117

## Configuration Options

118

119

```kotlin { .api }

120

data class CompressionOptions(

121

val encoders: Map<String, CompressionEncoderConfig> = emptyMap(),

122

val conditions: List<ApplicationCall.(OutgoingContent) -> Boolean> = emptyList()

123

)

124

125

data class CompressionEncoderConfig(

126

val encoder: ContentEncoder,

127

val conditions: List<ApplicationCall.(OutgoingContent) -> Boolean>,

128

val priority: Double

129

)

130

```

131

132

The `CompressionConfig` builds these options internally to configure the compression behavior at runtime.