Classes
Compressor
4
▼
Contract for content encoders.
Compressor
4
▼
Contract for content encoders.
Methods
encoding
0
str
▼
The Content-Encoding value for this compressor (e.g., 'gzip').
property
encoding
0
str
▼
def encoding(self) -> str
Returns
str
compress
1
bytes
▼
Compress a chunk of data.
compress
1
bytes
▼
def compress(self, data: bytes) -> bytes
Parameters
| Name | Type | Description |
|---|---|---|
data |
— |
Input bytes to compress. |
Returns
bytes
Compressed bytes (may be empty if buffering internally).
flush
0
bytes
▼
Flush any buffered compressed data and finalize the stream.
After calling flus…
flush
0
bytes
▼
def flush(self) -> bytes
Flush any buffered compressed data and finalize the stream.
After calling flush(), the compressor should not be used again.
Returns
bytes
Final compressed bytes.
sync_flush
0
bytes
▼
Force buffered data out without finalizing the stream.
Used for streaming resp…
sync_flush
0
bytes
▼
def sync_flush(self) -> bytes
Force buffered data out without finalizing the stream.
Used for streaming responses where each chunk must produce compressed output immediately. The compressor remains usable after this call.
Returns
bytes
Compressed bytes for any internally buffered data.
GzipCompressor
5
▼
Gzip compressor using stdlib zlib.
Creates a fresh zlib compressor per instance. Each request gets…
GzipCompressor
5
▼
Gzip compressor using stdlib zlib.
Creates a fresh zlib compressor per instance. Each request gets its own GzipCompressor — no shared state.
Methods
encoding
0
str
▼
property
encoding
0
str
▼
def encoding(self) -> str
Returns
str
compress
1
bytes
▼
compress
1
bytes
▼
def compress(self, data: bytes) -> bytes
Parameters
| Name | Type | Description |
|---|---|---|
data |
— |
Returns
bytes
flush
0
bytes
▼
flush
0
bytes
▼
def flush(self) -> bytes
Returns
bytes
sync_flush
0
bytes
▼
sync_flush
0
bytes
▼
def sync_flush(self) -> bytes
Returns
bytes
Internal Methods 1 ▼
__init__
1
▼
__init__
1
▼
def __init__(self, *, level: int = 6) -> None
Parameters
| Name | Type | Description |
|---|---|---|
level |
— |
Default:6
|
ZstdCompressor
5
▼
Zstd compressor using Python 3.14 stdlib compression.zstd.
Requires Python 3.14+ with compression.…
ZstdCompressor
5
▼
Zstd compressor using Python 3.14 stdlib compression.zstd.
Requires Python 3.14+ with compression.zstd available (PEP 784). Each request gets its own ZstdCompressor — no shared state.
Methods
encoding
0
str
▼
property
encoding
0
str
▼
def encoding(self) -> str
Returns
str
compress
1
bytes
▼
compress
1
bytes
▼
def compress(self, data: bytes) -> bytes
Parameters
| Name | Type | Description |
|---|---|---|
data |
— |
Returns
bytes
flush
0
bytes
▼
flush
0
bytes
▼
def flush(self) -> bytes
Returns
bytes
sync_flush
0
bytes
▼
sync_flush
0
bytes
▼
def sync_flush(self) -> bytes
Returns
bytes
Internal Methods 1 ▼
__init__
1
▼
__init__
1
▼
def __init__(self, *, level: int = 3) -> None
Parameters
| Name | Type | Description |
|---|---|---|
level |
— |
Default:3
|
DictZstdCompressor
5
▼
Zstd compressor pre-loaded with a shared dictionary (RFC 9842).
Uses a ``CompressionDictionary`` t…
DictZstdCompressor
5
▼
Zstd compressor pre-loaded with a shared dictionary (RFC 9842).
Uses aCompressionDictionaryto achieve dramatically better
compression ratios on repetitive payloads (e.g. API JSON responses).
TheContent-Encoding is dcz(dictionary-compressed zstd).
Each request gets its own DictZstdCompressor — the underlying
ZstdDictis immutable and safe to share across threads.
Methods
encoding
0
str
▼
property
encoding
0
str
▼
def encoding(self) -> str
Returns
str
compress
1
bytes
▼
compress
1
bytes
▼
def compress(self, data: bytes) -> bytes
Parameters
| Name | Type | Description |
|---|---|---|
data |
— |
Returns
bytes
flush
0
bytes
▼
flush
0
bytes
▼
def flush(self) -> bytes
Returns
bytes
sync_flush
0
bytes
▼
sync_flush
0
bytes
▼
def sync_flush(self) -> bytes
Returns
bytes
Internal Methods 1 ▼
__init__
2
▼
__init__
2
▼
def __init__(self, zstd_dict: _zstd_mod.ZstdDict, *, level: int = 3) -> None
Parameters
| Name | Type | Description |
|---|---|---|
zstd_dict |
— |
|
level |
— |
Default:3
|
CompressionDictionary
4
▼
A loaded zstd dictionary with its RFC 9842 identity.
Immutable after creation — safe to share acro…
CompressionDictionary
4
▼
A loaded zstd dictionary with its RFC 9842 identity.
Immutable after creation — safe to share across threads.
Attributes
| Name | Type | Description |
|---|---|---|
sf_hash |
str
|
SHA-256 hash of dict content as sf-binary (e.g. |
match |
str
|
URL pattern this dictionary applies to (e.g. |
zstd_dict |
_zstd_mod.ZstdDict
|
The stdlib |
Methods
Internal Methods 1 ▼
__init__
2
▼
__init__
2
▼
def __init__(self, dict_content: bytes, match: str) -> None
Parameters
| Name | Type | Description |
|---|---|---|
dict_content |
— |
|
match |
— |
Functions
_build_encoding_priority
0
tuple[str, ...]
▼
Build encoding priority based on available libraries.
_build_encoding_priority
0
tuple[str, ...]
▼
def _build_encoding_priority() -> tuple[str, ...]
Returns
tuple[str, ...]
load_dictionary
2
CompressionDictionary
▼
Load a zstd dictionary from disk.
load_dictionary
2
CompressionDictionary
▼
def load_dictionary(path: Path, match: str) -> CompressionDictionary
Parameters
| Name | Type | Description |
|---|---|---|
path |
Path |
Path to the dictionary file (created by |
match |
str |
URL pattern this dictionary applies to. |
Returns
CompressionDictionary
parse_sf_binary
1
bytes
▼
Parse an RFC 8941 structured field binary value.
Structured field binary is ba…
parse_sf_binary
1
bytes
▼
def parse_sf_binary(value: bytes | str) -> bytes
Parse an RFC 8941 structured field binary value.
Structured field binary is base64-encoded content between colons:
:base64content=:
Parameters
| Name | Type | Description |
|---|---|---|
value |
bytes | str |
The sf-binary value (with or without surrounding whitespace). |
Returns
bytes
negotiate_encoding
1
str | None
▼
Parse Accept-Encoding and return the best supported encoding.
Respects q-value…
negotiate_encoding
1
str | None
▼
def negotiate_encoding(accept_encoding: bytes | str) -> str | None
Parse Accept-Encoding and return the best supported encoding.
Respects q-values and our encoding priority (zstd > gzip). Returns None if no supported encoding matches or the client explicitly declines all encodings.
Parameters
| Name | Type | Description |
|---|---|---|
accept_encoding |
bytes | str |
The Accept-Encoding header value. |
Returns
str | None
create_compressor
2
Compressor
▼
Create a compressor instance for the given encoding.
create_compressor
2
Compressor
▼
def create_compressor(encoding: str, *, dictionary: CompressionDictionary | None = None) -> Compressor
Parameters
| Name | Type | Description |
|---|---|---|
encoding |
str |
Encoding name (e.g., "zstd", "gzip", "dcz"). |
dictionary |
CompressionDictionary | None |
Optional compression dictionary for None
|
Returns
Compressor
negotiate_dictionary
3
CompressionDictionary | …
▼
Match an ``Available-Dictionary`` header to a loaded dictionary.
negotiate_dictionary
3
CompressionDictionary | …
▼
def negotiate_dictionary(available_dictionary: bytes | str, dictionaries: tuple[CompressionDictionary, ...], request_target: str = '') -> CompressionDictionary | None
Parameters
| Name | Type | Description |
|---|---|---|
available_dictionary |
bytes | str |
The |
dictionaries |
tuple[CompressionDictionary, ...] |
Server-loaded dictionaries to match against. |
request_target |
str |
The request URL path — used to filter by ''
|
Returns
CompressionDictionary | None
_match_pattern
2
bool
▼
Simple glob-style match: ``/api/v1/*`` matches ``/api/v1/users``.
_match_pattern
2
bool
▼
def _match_pattern(pattern: str, target: str) -> bool
Parameters
| Name | Type | Description |
|---|---|---|
pattern |
str |
|
target |
str |
Returns
bool