Skip to content

Commit

Permalink
Move Req.Utils.stream_gzip/1
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Mar 15, 2024
1 parent dcb6b40 commit f822213
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
30 changes: 1 addition & 29 deletions lib/req/steps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ defmodule Req.Steps do
:zlib.gzip(iodata)

enumerable ->
gzip_stream(enumerable)
Req.Utils.stream_gzip(enumerable)
end

request
Expand All @@ -631,34 +631,6 @@ defmodule Req.Steps do
end
end

defp gzip_stream(enumerable) do
eof = make_ref()

enumerable
|> Stream.concat([eof])
|> Stream.transform(
fn ->
z = :zlib.open()
# https://github.com/erlang/otp/blob/OTP-26.0/erts/preloaded/src/zlib.erl#L551
:ok = :zlib.deflateInit(z, :default, :deflated, 16 + 15, 8, :default)
z
end,
fn
^eof, z ->
buf = :zlib.deflate(z, [], :finish)
{buf, z}

data, z ->
buf = :zlib.deflate(z, data)
{buf, z}
end,
fn z ->
:ok = :zlib.deflateEnd(z)
:ok = :zlib.close(z)
end
)
end

@doc """
Runs the request using `Finch`.
Expand Down
37 changes: 37 additions & 0 deletions lib/req/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,41 @@ defmodule Req.Utils do
def format_http_datetime(datetime) do
Calendar.strftime(datetime, "%a, %d %b %Y %H:%M:%S GMT")
end

@doc """
Returns a stream where each element is gzipped.
## Examples
iex> gzipped = Req.Utils.stream_gzip(~w[foo bar baz]) |> Enum.to_list()
iex> :zlib.gunzip(gzipped)
"foobarbaz"
"""
def stream_gzip(enumerable) do
eof = make_ref()

enumerable
|> Stream.concat([eof])
|> Stream.transform(
fn ->
z = :zlib.open()
# https://github.com/erlang/otp/blob/OTP-26.0/erts/preloaded/src/zlib.erl#L551
:ok = :zlib.deflateInit(z, :default, :deflated, 16 + 15, 8, :default)
z
end,
fn
^eof, z ->
buf = :zlib.deflate(z, [], :finish)
{buf, z}

data, z ->
buf = :zlib.deflate(z, data)
{buf, z}
end,
fn z ->
:ok = :zlib.deflateEnd(z)
:ok = :zlib.close(z)
end
)
end
end

0 comments on commit f822213

Please sign in to comment.