summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--proxy/proxypair.coffee18
-rw-r--r--proxy/spec/proxypair.spec.coffee29
2 files changed, 32 insertions, 15 deletions
diff --git a/proxy/proxypair.coffee b/proxy/proxypair.coffee
index 2adbc23..d3340ed 100644
--- a/proxy/proxypair.coffee
+++ b/proxy/proxypair.coffee
@@ -88,6 +88,7 @@ class ProxyPair
# TODO: Change this for multiplexing.
snowflake.reset()
channel.onerror = -> log 'Data channel error!'
+ channel.binaryType = "arraybuffer"
channel.onmessage = @onClientToRelayMessage
# Assumes WebRTC datachannel is connected.
@@ -131,21 +132,16 @@ class ProxyPair
# WebRTC --> websocket
onClientToRelayMessage: (msg) =>
- line = recv = msg.data
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
- @c2rSchedule.push recv
+ log 'WebRTC --> websocket data: ' + msg.data.byteLength + ' bytes'
+ @c2rSchedule.push msg.data
@flush()
# websocket --> WebRTC
onRelayToClientMessage: (event) =>
+ if DEBUG
+ log 'websocket --> WebRTC data: ' + event.data.byteLength + ' bytes'
@r2cSchedule.push event.data
- # log 'websocket-->WebRTC data: ' + event.data
@flush()
onError: (event) =>
@@ -175,7 +171,7 @@ class ProxyPair
@relay.bufferedAmount < @MAX_BUFFER &&
@c2rSchedule.length > 0
chunk = @c2rSchedule.shift()
- @rateLimit.update chunk.length
+ @rateLimit.update chunk.byteLength
@relay.send chunk
busy = true
# websocket --> WebRTC
@@ -183,7 +179,7 @@ class ProxyPair
@client.bufferedAmount < @MAX_BUFFER &&
@r2cSchedule.length > 0
chunk = @r2cSchedule.shift()
- @rateLimit.update chunk.length
+ @rateLimit.update chunk.byteLength
@client.send chunk
busy = true
diff --git a/proxy/spec/proxypair.spec.coffee b/proxy/spec/proxypair.spec.coffee
index 4cbb38d..566c6f1 100644
--- a/proxy/spec/proxypair.spec.coffee
+++ b/proxy/spec/proxypair.spec.coffee
@@ -2,6 +2,25 @@
jasmine tests for Snowflake proxypair
###
+# Replacement for MessageEvent constructor.
+# https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/MessageEvent
+MessageEvent = (type, init) ->
+ init
+
+# Asymmetic matcher that checks that two arrays have the same contents.
+arrayMatching = (sample) -> {
+ asymmetricMatch: (other) ->
+ a = new Uint8Array(sample)
+ b = new Uint8Array(other)
+ if a.length != b.length
+ return false
+ for _, i in a
+ if a[i] != b[i]
+ return false
+ true
+ jasmineToString: ->
+ '<arrayMatchine(' + jasmine.pp(sample) + ')>'
+}
describe 'ProxyPair', ->
fakeRelay = Parse.address '0.0.0.0:12345'
@@ -76,17 +95,19 @@ describe 'ProxyPair', ->
}
spyOn pp.client, 'send'
spyOn pp.relay, 'send'
- pp.c2rSchedule.push 'foo'
+ msg = new MessageEvent("message", { data: Uint8Array.from([1, 2, 3]).buffer })
+ pp.onClientToRelayMessage(msg)
pp.flush()
expect(pp.client.send).not.toHaveBeenCalled()
- expect(pp.relay.send).toHaveBeenCalledWith 'foo'
+ expect(pp.relay.send).toHaveBeenCalledWith arrayMatching([1, 2, 3])
it 'proxies data from relay to client', ->
spyOn pp.client, 'send'
spyOn pp.relay, 'send'
- pp.r2cSchedule.push 'bar'
+ msg = new MessageEvent("message", { data: Uint8Array.from([4, 5, 6]).buffer })
+ pp.onRelayToClientMessage(msg)
pp.flush()
- expect(pp.client.send).toHaveBeenCalledWith 'bar'
+ expect(pp.client.send).toHaveBeenCalledWith arrayMatching([4, 5, 6])
expect(pp.relay.send).not.toHaveBeenCalled()
it 'sends nothing with nothing to flush', ->