Skip to main content

src/aws@internal@codec@compression.erl

-module(aws@internal@codec@compression).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/aws/internal/codec/compression.gleam").
-export([gzip/1, maybe_compress/3]).

-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.

?MODULEDOC(
    " Request-body compression for `@smithy.api#requestCompression`.\n"
    "\n"
    " Mirrors the Rust SDK's `RequestCompressionInterceptor`\n"
    " (vendor/aws-sdk-rust/sdk/aws-smithy-compression/src/) — gzip the\n"
    " body when it's at least `min_compression_size_bytes` long, drop\n"
    " the wrap entirely when it's smaller (compressing tiny bodies\n"
    " usually makes them larger). The default matches Rust:\n"
    " 10 240 bytes (10 KiB).\n"
    "\n"
    " Erlang's stdlib `zlib:gzip/1` does the work — no extra deps.\n"
    " Only gzip is supported today; the trait technically allows other\n"
    " algorithms (brotli, etc.) but no AWS service uses them.\n"
).

-file("src/aws/internal/codec/compression.gleam", 28).
?DOC(
    " Gzip a body via Erlang's `zlib:gzip/1`. Pure function; no\n"
    " streaming variant since `@requestCompression` only wraps\n"
    " buffered bodies (streaming bodies use the chunked transport\n"
    " which has its own per-chunk wrappers).\n"
).
-spec gzip(bitstring()) -> bitstring().
gzip(Body) ->
    zlib:gzip(Body).

-file("src/aws/internal/codec/compression.gleam", 37).
?DOC(
    " Apply a `@requestCompression` algorithm to `body` if the body is\n"
    " at least `min_size` bytes long. Returns `#(compressed_body,\n"
    " applied)` — `applied` is `True` iff the body was actually wrapped\n"
    " (the caller uses it to decide whether to set the\n"
    " `Content-Encoding` header). When `encoding` isn't `\"gzip\"` (the\n"
    " only algorithm we support today), the body passes through\n"
    " unchanged and `applied` is `False`.\n"
).
-spec maybe_compress(bitstring(), binary(), integer()) -> {bitstring(),
    boolean()}.
maybe_compress(Body, Encoding, Min_size) ->
    case {Encoding, erlang:byte_size(Body) >= Min_size} of
        {<<"gzip"/utf8>>, true} ->
            {zlib:gzip(Body), true};

        {_, _} ->
            {Body, false}
    end.