Compression class final

Compresses responses a client said it can decompress.

final app = Router()
  ..layer(const Compression())
  ..merge(routes);

Only gzip: it is the encoding every client has supported for two decades, and brotli would mean a dependency for a few percent. A client that asks for nothing, or asks only for something else, gets the body uncompressed.

A response is left alone when any of these is true, because compressing it would cost more than it saves or would break something:

  • the client did not offer gzip;
  • the body is already encoded — re-encoding is how a double-gzipped response happens, which no client will unwrap twice;
  • the type is not in types, so it is probably compressed already;
  • a known length is below minimumBytes, where the gzip header alone can make the answer bigger.

Vary: Accept-Encoding is always added, compressed or not: without it a shared cache can hand a gzipped body to a client that never asked for one.

Implemented types

Constructors

Compression({int minimumBytes = 1024, Set<String> types = defaultCompressibleTypes, int level = 6})
Compresses text-ish responses over minimumBytes.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
level int
The gzip level, 1 (fastest) to 9 (smallest).
final
minimumBytes int
Below this many bytes, compression is not worth the header.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
types Set<String>
Which content types are worth compressing.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toMiddleware() Middleware
Builds the shelf middleware this value configures.
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited