websocket-driver 0.7.7 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f871879972966e5137b9e1d791df17db7e6186e35bbf326e156682c7cc092410
4
- data.tar.gz: a96b99173f63da1fe2769af05e78a40a51137f7e48cac2ec01d88bb883724c0f
3
+ metadata.gz: c1f212eb47e19ec359d90624302280cfc94d6fbc9d4bf9a419574fa8531c7d38
4
+ data.tar.gz: a35ae8ecae5b94d297141eb9bc0f29647614034cc961bc8c4e26a7876aa40a4b
5
5
  SHA512:
6
- metadata.gz: 41df0469a3e829b6a13ddbbacdd39f81d80f433a14248f0748dc8831848de7c5c4db65539fdc5905d91fdd7fd2bcfaae08048f1ee6652603e79ced492230bab6
7
- data.tar.gz: 1920877851a371d6d5493c94bd413fff3ce7f918166506aaa0a3b1a6fcae3961e1da06c315d0fd1bfc94a6c2bd0798f77f6e42429b86ccd9e25c6ad8dcedfa80
6
+ metadata.gz: 0dd79a8fd32a6380ef832549c1139b1f425f0fe49b33adb32338ce39d5ad9d1d558ce1f33f89c3fefa8464b65a0b9d9664e509e200c55979add09b0cd872e55d
7
+ data.tar.gz: 87684980b91fb5d6cf1ae8866ab51cd60809fbbba4d90032585c8596797333e8e0e995f1b98cb7269e917de2c501ee524d0c2ff72917457092fa292e51e7e0a9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### 0.8.0 / 2025-05-25
2
+
3
+ - Emit binary message as a string with `Encoding::BINARY` instead of an array
4
+ - Add the option `:binary_data_format` to force the previous behaviour
5
+
1
6
  ### 0.7.7 / 2025-01-04
2
7
 
3
8
  - Add `base64` gem to the dependencies to support Ruby 3.4
data/README.md CHANGED
@@ -275,6 +275,10 @@ keys:
275
275
  - `:protocols` - an array of strings representing acceptable subprotocols for
276
276
  use over the socket. The driver will negotiate one of these to use via the
277
277
  `Sec-WebSocket-Protocol` header if supported by the other peer.
278
+ - `:binary_data_format` - in older versions of this library, binary messages
279
+ were represented as arrays of bytes, whereas they're now represented as
280
+ strings with `Encoding::BINARY` for performance reasons. Set this option to
281
+ `:array` to restore the old behaviour.
278
282
 
279
283
  All drivers respond to the following API methods, but some of them are no-ops
280
284
  depending on whether the client supports the behaviour.
@@ -290,8 +294,8 @@ Adds a callback block to execute when the socket becomes open.
290
294
  #### `driver.on :message, -> (event) {}`
291
295
 
292
296
  Adds a callback block to execute when a message is received. `event` will have a
293
- `data` attribute containing either a string in the case of a text message or an
294
- array of integers in the case of a binary message.
297
+ `data` attribute whose value is a string with the encoding `Encoding::UTF_8` for
298
+ text message, and `Encoding::BINARY` for binary message.
295
299
 
296
300
  #### `driver.on :error, -> (event) {}`
297
301
 
@@ -346,11 +350,12 @@ Sends a text message over the socket. If the socket handshake is not yet
346
350
  complete, the message will be queued until it is. Returns `true` if the message
347
351
  was sent or queued, and `false` if the socket can no longer send messages.
348
352
 
349
- #### `driver.binary(array)`
353
+ #### `driver.binary(buffer)`
350
354
 
351
- Takes an array of byte-sized integers and sends them as a binary message. Will
352
- queue and return `true` or `false` the same way as the `text` method. It will
353
- also return `false` if the driver does not support binary messages.
355
+ Takes either a string with encoding `Encoding::BINARY`, or an array of
356
+ byte-sized integers, and sends it as a binary message. Will queue and return
357
+ `true` or `false` the same way as the `text` method. It will also return `false`
358
+ if the driver does not support binary messages.
354
359
 
355
360
  #### `driver.ping(string = '', &callback)`
356
361
 
@@ -163,11 +163,13 @@ module WebSocket
163
163
  message = Message.new
164
164
  frame = Frame.new
165
165
 
166
- message.rsv1 = message.rsv2 = message.rsv3 = false
167
- message.opcode = OPCODES[type || (String === buffer ? :text : :binary)]
166
+ is_binary = (Array === buffer or buffer.encoding == Encoding::BINARY)
167
+ payload = Driver.encode(buffer, is_binary ? nil : Encoding::UTF_8)
168
+ payload = [code, payload].pack('S>a*') if code
169
+ type ||= is_binary ? :binary : :text
168
170
 
169
- payload = Driver.encode(buffer)
170
- payload = [code, payload].pack('S>a*') if code
171
+ message.rsv1 = message.rsv2 = message.rsv3 = false
172
+ message.opcode = OPCODES[type]
171
173
  message.data = payload
172
174
 
173
175
  if MESSAGE_OPCODES.include?(message.opcode)
@@ -403,7 +405,11 @@ module WebSocket
403
405
  payload = Driver.encode(payload, Encoding::UTF_8)
404
406
  payload = nil unless payload.valid_encoding?
405
407
  when OPCODES[:binary]
406
- payload = payload.bytes.to_a
408
+ if @binary_data_format == :array
409
+ payload = payload.bytes.to_a
410
+ else
411
+ payload = Driver.encode(payload, Encoding::BINARY)
412
+ end
407
413
  end
408
414
 
409
415
  if payload
@@ -71,7 +71,7 @@ module WebSocket
71
71
 
72
72
  def initialize(socket, options = {})
73
73
  super()
74
- Driver.validate_options(options, [:max_length, :masking, :require_masking, :protocols])
74
+ Driver.validate_options(options, [:max_length, :masking, :require_masking, :protocols, :binary_data_format])
75
75
 
76
76
  @socket = socket
77
77
  @reader = StreamReader.new
@@ -80,6 +80,8 @@ module WebSocket
80
80
  @headers = Headers.new
81
81
  @queue = []
82
82
  @ready_state = 0
83
+
84
+ @binary_data_format = options[:binary_data_format] || :string
83
85
  end
84
86
 
85
87
  def state
@@ -197,17 +199,18 @@ module WebSocket
197
199
 
198
200
  def self.encode(data, encoding = nil)
199
201
  if Array === data
202
+ data = data.pack('C*')
200
203
  encoding ||= Encoding::BINARY
201
- return data.pack('C*').force_encoding(encoding)
202
204
  end
203
205
 
204
- encoding ||= Encoding::UTF_8
205
-
206
- return data if data.encoding == encoding
207
- return data.encode(encoding) unless data.encoding == Encoding::BINARY
206
+ return data if encoding.nil? or data.encoding == encoding
208
207
 
209
- data = data.dup if data.frozen?
210
- data.force_encoding(encoding)
208
+ if data.encoding == Encoding::BINARY
209
+ data = data.dup if data.frozen?
210
+ data.force_encoding(encoding)
211
+ else
212
+ data.encode(encoding)
213
+ end
211
214
  end
212
215
 
213
216
  def self.host_header(uri)
@@ -224,6 +227,12 @@ module WebSocket
224
227
  raise ConfigurationError, "Unrecognized option: #{ key.inspect }"
225
228
  end
226
229
  end
230
+
231
+ if options[:binary_data_format]
232
+ unless [:array, :string].include?(options[:binary_data_format])
233
+ raise ConfigurationError, "Invalid :binary_data_format: #{options[:binary_data_format].inspect}"
234
+ end
235
+ end
227
236
  end
228
237
 
229
238
  def self.websocket?(env)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-driver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.7
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Coglan
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-04 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: base64
@@ -127,7 +127,8 @@ files:
127
127
  homepage: https://github.com/faye/websocket-driver-ruby
128
128
  licenses:
129
129
  - Apache-2.0
130
- metadata: {}
130
+ metadata:
131
+ changelog_uri: https://github.com/faye/websocket-driver-ruby/blob/main/CHANGELOG.md
131
132
  rdoc_options:
132
133
  - "--main"
133
134
  - README.md
@@ -146,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  - !ruby/object:Gem::Version
147
148
  version: '0'
148
149
  requirements: []
149
- rubygems_version: 3.6.2
150
+ rubygems_version: 3.6.7
150
151
  specification_version: 4
151
152
  summary: WebSocket protocol handler with pluggable I/O
152
153
  test_files: []