• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NFIBrokerage / slipstream / b8022eaf88a86aef198f8bc23bbe50c0f8a62072

26 Sep 2024 01:57PM UTC coverage: 98.861% (-1.1%) from 100.0%
b8022eaf88a86aef198f8bc23bbe50c0f8a62072

push

github

the-mikedavis
Bump Elixir & Erlang versions in CI

434 of 439 relevant lines covered (98.86%)

37.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/lib/slipstream/serializer/phoenix_socket_v2_serializer.ex
1
defmodule Slipstream.Serializer.PhoenixSocketV2Serializer do
2
  @moduledoc """
3
  A client-side implementation that corresponds to the server-side Phoenix.Socket.V2.JSONSerializer.
4
  """
5

6
  @behaviour Slipstream.Serializer
7

8
  alias Slipstream.Message
9
  alias Slipstream.Serializer
10

11
  @push 0
12
  @reply 1
13
  @broadcast 2
14

15
  def encode!(%Message{payload: {:binary, data}} = message, _opts) do
16
    try do
5✔
17
      join_ref = to_string(message.join_ref)
5✔
18
      ref = to_string(message.ref)
5✔
19
      join_ref_size = byte_size!(join_ref, :join_ref, 255)
5✔
20
      ref_size = byte_size!(ref, :ref, 255)
5✔
21
      topic_size = byte_size!(message.topic, :topic, 255)
5✔
22
      event_size = byte_size!(message.event, :event, 255)
4✔
23

24
      <<
4✔
25
        @push::size(8),
26
        join_ref_size::size(8),
27
        ref_size::size(8),
28
        topic_size::size(8),
29
        event_size::size(8),
30
        join_ref::binary-size(join_ref_size),
31
        ref::binary-size(ref_size),
32
        message.topic::binary-size(topic_size),
4✔
33
        message.event::binary-size(event_size),
4✔
34
        data::binary
35
      >>
36
    rescue
37
      exception in [ArgumentError] ->
1✔
38
        reraise(
1✔
39
          Serializer.EncodeError,
40
          [message: exception.message],
1✔
41
          __STACKTRACE__
42
        )
43
    end
44
  end
45

46
  def encode!(%Message{} = message, [json_parser: json_parser] = _opts) do
47
    try do
48✔
48
      [
49
        message.join_ref,
48✔
50
        message.ref,
48✔
51
        message.topic,
48✔
52
        message.event,
48✔
53
        message.payload
48✔
54
      ]
55
      |> json_parser.encode!()
48✔
56
    rescue
57
      exception in [Protocol.UndefinedError] ->
1✔
58
        reraise(
1✔
59
          Serializer.EncodeError,
60
          [message: inspect(exception)],
61
          __STACKTRACE__
62
        )
63

64
      # coveralls-ignore-start
65
      maybe_json_parser_exception ->
66
        reraise(
67
          Serializer.EncodeError,
68
          [message: Exception.message(maybe_json_parser_exception)],
69
          __STACKTRACE__
70
        )
71

72
        # coveralls-ignore-stop
73
    end
74
  end
75

76
  def decode!(binary, [opcode: opcode, json_parser: json_parser] = _opts) do
77
    try do
55✔
78
      case opcode do
55✔
79
        :text -> decode_text!(binary, json_parser: json_parser)
49✔
80
        :binary -> decode_binary!(binary)
6✔
81
      end
82
    rescue
83
      # for binary doesn't match decode_binary!/1 function pattern match
84
      exception in [FunctionClauseError] ->
1✔
85
        reraise(
1✔
86
          Serializer.DecodeError,
87
          [message: FunctionClauseError.message(exception)],
88
          __STACKTRACE__
89
        )
90

91
      # coveralls-ignore-start
92
      maybe_json_parser_exception ->
93
        reraise(
94
          Serializer.DecodeError,
95
          [message: inspect(maybe_json_parser_exception)],
96
          __STACKTRACE__
97
        )
98

99
        # coveralls-ignore-stop
100
    end
101
  end
102

103
  defp decode_binary!(<<
104
         @push::size(8),
105
         join_ref_size::size(8),
106
         topic_size::size(8),
107
         event_size::size(8),
108
         join_ref::binary-size(join_ref_size),
109
         topic::binary-size(topic_size),
110
         event::binary-size(event_size),
111
         data::binary
112
       >>) do
113
    %Message{
2✔
114
      topic: topic,
115
      event: event,
116
      payload: {:binary, data},
117
      ref: nil,
118
      join_ref: join_ref
119
    }
120
  end
121

122
  defp decode_binary!(<<
123
         @reply::size(8),
124
         join_ref_size::size(8),
125
         ref_size::size(8),
126
         topic_size::size(8),
127
         status_size::size(8),
128
         join_ref::binary-size(join_ref_size),
129
         ref::binary-size(ref_size),
130
         topic::binary-size(topic_size),
131
         status::binary-size(status_size),
132
         data::binary
133
       >>) do
134
    %Message{
×
135
      topic: topic,
136
      event: "phx_reply",
137
      payload: %{"response" => {:binary, data}, "status" => status},
138
      ref: ref,
139
      join_ref: join_ref
140
    }
141
  end
142

143
  defp decode_binary!(<<
144
         @broadcast::size(8),
145
         topic_size::size(8),
146
         event_size::size(8),
147
         topic::binary-size(topic_size),
148
         event::binary-size(event_size),
149
         data::binary
150
       >>) do
151
    %Message{
×
152
      topic: topic,
153
      event: event,
154
      payload: {:binary, data},
155
      ref: nil,
156
      join_ref: nil
157
    }
158
  end
159

160
  defp decode_text!(binary, json_parser: json_parser) do
161
    case json_parser.decode!(binary) do
49✔
162
      [join_ref, ref, topic, event, payload | _] ->
163
        %Message{
49✔
164
          join_ref: join_ref,
165
          ref: ref,
166
          topic: topic,
167
          event: event,
168
          payload: payload
169
        }
170

171
      # coveralls-ignore-start
172
      # this may occur if the remote websocket server does not support the v2
173
      # transport packets
174
      decoded_json when is_map(decoded_json) ->
175
        Message.from_map!(decoded_json)
176
        # coveralls-ignore-stop
177
    end
178
  end
179

180
  defp byte_size!(bin, kind, max) do
181
    case byte_size(bin) do
19✔
182
      size when size <= max ->
183
        size
18✔
184

185
      oversized ->
186
        raise ArgumentError, """
1✔
187
        unable to convert #{kind} to binary.
1✔
188

189
            #{inspect(bin)}
190

191
        must be less than or equal to #{max} bytes, but is #{oversized} bytes.
1✔
192
        """
193
    end
194
  end
195
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc