Skip to main content

A middleware for the FastStream framework to support message compression.

Project description

faststream-compressors

A middleware for the FastStream framework to support message compression.

Example

from faststream.nats import NatsBroker

from faststream_compressors.compressors import GzipCompressor, GzipDecompressor
from faststream_compressors.compressors.lz4 import Lz4Decompressor  # pip install "faststream_compressors[lz4]"
from faststream_compressors.middlewares.nats import NatsCompressionMiddleware

compression_middleware = NatsCompressionMiddleware.make_middleware(
    # Compression methods used for decompressing messages.
    # The order does not matter here.
    decompressors=(GzipDecompressor(), Lz4Decompressor()),
    # Compression methods used for compressing messages.
    # The order in which compressors are specified matters.
    compressors=GzipCompressor(),
)

broker = NatsBroker(middlewares=(compression_middleware,))
Broker Is Supported? Middleware
NATS faststream_compressors.middlewares.nats.NatsMiddleware
Other

You can submit a pull request to add support for middleware for your broker. I expect that FastStream will update its middleware API soon, allowing us to create a universal middleware for each broker. For now, only NATS is supported.

Compression Method Is Supported? Compressor Extra Dependency
gzip faststream_compressors.compressors.GzipCompressor
faststream_compressors.compressors.GzipDecompressor
lz4 faststream_compressors.compressors.lz4.Lz4Compressor
faststream_compressors.compressors.lz4.Lz4Decompressor
faststream-compressors[lz4]
Other

You can submit a pull request to add support for your compression method or use your custom algorithm that adheres to the BaseCompressor interface.

from faststream import FastStream, Header
from faststream.nats import NatsBroker

from faststream_compressors.compressors import BaseCompressor

from faststream_compressors.middlewares.nats import NatsCompressionMiddleware


class MyCompressor(BaseCompressor):
    ENCODING = "xor1"

    def __call__(self, data: bytes) -> bytes:
        return bytes(byte ^ 1 for byte in data)


compression_middleware = NatsCompressionMiddleware.make_middleware(
    decompressors=MyCompressor(), compressors=MyCompressor()
)

broker = NatsBroker(middlewares=(compression_middleware,))
app = FastStream(broker)


@broker.subscriber("my-subject")
async def my_handler(data: str, encoding: str = Header("content-encoding")):
    print(data, encoding)


@app.after_startup
async def ping():
    await broker.publish("My secret message", "my-subject")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faststream_compressors-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

faststream_compressors-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file faststream_compressors-0.1.0.tar.gz.

File metadata

  • Download URL: faststream_compressors-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/24.0.0

File hashes

Hashes for faststream_compressors-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fc72b521101427d1f2e60d896b3bf379e0c35bcc2c1730a7d28daa2015f342a2
MD5 b4c2dbe44c62fca6fa8a6f2bafe0637c
BLAKE2b-256 1129ebac4d66a00e73f732317bec32b2a9f1e7249c5ca212ae135a06b42db206

See more details on using hashes here.

File details

Details for the file faststream_compressors-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for faststream_compressors-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f4443f674171a8ee2da3ed00a4dfc99377f3a954b9c3e7bdc0f1d3e86e0545c
MD5 ed7ebbbcf42165eb04dac2b23610b421
BLAKE2b-256 730d64b28976bf84bd8f423a47282153bef8c8eb50ce3dc6f2127a5d4b63834a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page