Skip to content

Commit 35b6353

Browse files
committed
fix stream mapping
- collect response into binary before mapping - don't try to map error responses
1 parent d4c56f2 commit 35b6353

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

lib/phoenix/sync/electric.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ defmodule Phoenix.Sync.Electric do
600600
def map_response_body(body, mapper) when is_binary(body) and is_function(mapper, 1) do
601601
body
602602
|> @json.decode!()
603-
|> Phoenix.Sync.Electric.map_response_body(mapper)
603+
|> map_response_body(mapper)
604604
|> then(fn item -> [@json.encode_to_iodata!(item)] end)
605605
end
606606

lib/phoenix/sync/electric/api_adapter.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ if Code.ensure_loaded?(Electric.Shapes.Api) do
5555
end
5656

5757
defp apply_transform(stream, transform_fun) do
58-
Stream.flat_map(stream, fn chunk ->
59-
chunk
60-
|> IO.iodata_to_binary()
61-
|> Phoenix.Sync.Electric.map_response_body(transform_fun)
62-
end)
58+
stream
59+
|> Enum.to_list()
60+
|> IO.iodata_to_binary()
61+
|> Phoenix.Sync.Electric.map_response_body(transform_fun)
6362
end
6463
end
6564
end

lib/phoenix/sync/electric/client_adapter.ex

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ defmodule Phoenix.Sync.Electric.ClientAdapter do
5858
end
5959

6060
body =
61-
Phoenix.Sync.Electric.map_response_body(
62-
response.body,
63-
PredefinedShape.transform_fun(shape)
64-
)
61+
if response.status in 200..299 do
62+
Phoenix.Sync.Electric.map_response_body(
63+
response.body,
64+
PredefinedShape.transform_fun(shape)
65+
)
66+
else
67+
response.body
68+
end
6569

6670
conn
6771
|> put_headers(response.headers)

0 commit comments

Comments
 (0)