Set binaryType="arraybuffer" for RTCDataChannel, just as with WebSocket.

The binaryType can be "arraybuffer" or "blob", and "blob" is the
default. The code is only aware of "arraybuffer": I discovered a problem
while running snowflake.html in debug mode; this code fails:
    if DEBUG
      # Go sends only raw bytes...
      if '[object ArrayBuffer]' == recv.toString()
        bytes = new Uint8Array recv
        line = String.fromCharCode.apply(null, bytes)
      line = line.trim()
      log 'WebRTC --> websocket data: ' + line
with the error:
	TypeError: line.trim is not a function[Learn More] snowflake.js:497:16
because recv is of type Blob, not ArrayBuffer.

Despite the unexpected type, the code seemed to work as expected when
not in debug mode. Though the two types provide different interfaces,
they are both valid to pass on to WebSocket.send. The only other thing
we did with it was try to read the .length member for rate-limiting
purposes:
        @rateLimit.update chunk.length
but .length is incorrect for either type: Blob uses .size and
ArrayBuffer uses .byteLength. It worked anyway, because
DummyRateLimit.update doesn't actually look at its argument.

We were already setting binaryType="arraybuffer" for WebSocket
connections.
This commit is contained in:
David Fifield 2018-12-04 15:28:19 -07:00
parent a554439370
commit aa668bdc92

View file

@ -88,6 +88,7 @@ class ProxyPair
# TODO: Change this for multiplexing. # TODO: Change this for multiplexing.
snowflake.reset() snowflake.reset()
channel.onerror = -> log 'Data channel error!' channel.onerror = -> log 'Data channel error!'
channel.binaryType = "arraybuffer"
channel.onmessage = @onClientToRelayMessage channel.onmessage = @onClientToRelayMessage
# Assumes WebRTC datachannel is connected. # Assumes WebRTC datachannel is connected.