Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with wrtc not handling Buffers correctly #61

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ function Peer (opts) {

Peer.WEBRTC_SUPPORT = !!getBrowserRTC()

try {
require('wrtc')
Peer.USING_WRTC = true
} catch (err) {
if (err.message !== 'Cannot find module \'wtc\'') throw err
Peer.USING_WRTC = false
}

/**
* Expose config, constraints, and data channel config for overriding all Peer
* instances. Otherwise, just set opts.config, opts.constraints, or opts.channelConfig
Expand Down Expand Up @@ -197,7 +205,7 @@ Peer.prototype.send = function (chunk) {
}

// `wrtc` module doesn't accept node.js buffer
if (Buffer.isBuffer(chunk) && !isTypedArray.strict(chunk)) {
if (Peer.USING_WRTC && Buffer.isBuffer(chunk)) {
chunk = new Uint8Array(chunk)
}

Expand Down
13 changes: 8 additions & 5 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ test('get config', function (t) {

test('detect WebRTC support', function (t) {
t.equal(Peer.WEBRTC_SUPPORT, typeof window !== 'undefined', 'builtin webrtc support')
if (Peer.WEBRTC_SUPPORT) {
t.not(Peer.USING_WRTC, 'not using wrtc')
}
t.end()
})

test('signal event gets emitted', function (t) {
var peer = new Peer({ config: config, initiator: true })
var peer = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
peer.once('signal', function () {
t.pass('got signal event')
peer.destroy()
Expand All @@ -26,8 +29,8 @@ test('signal event gets emitted', function (t) {
})

test('data send/receive text', function (t) {
var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })

var numSignal1 = 0
peer1.on('signal', function (data) {
Expand Down Expand Up @@ -86,8 +89,8 @@ test('data send/receive text', function (t) {
})

test('sdpTransform function is called', function (t) {
var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config, sdpTransform: sdpTransform })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, sdpTransform: sdpTransform, wrtc: common.wrtc })

function sdpTransform (sdp) {
t.equal(typeof sdp, 'string', 'got a string as SDP')
Expand Down
36 changes: 18 additions & 18 deletions test/binary.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test('get config', function (t) {
})

test('data send/receive Uint8Array', function (t) {
var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })
peer1.on('signal', function (data) {
peer2.signal(data)
})
Expand All @@ -26,15 +26,15 @@ test('data send/receive Uint8Array', function (t) {
function tryTest () {
if (!peer1.connected || !peer2.connected) return

peer1.send(new Uint8Array([1, 2, 3]))
peer1.send(new Uint8Array([0, 1, 2]))
peer2.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, new Buffer([1, 2, 3]), 'got correct message')
t.deepEqual(data, new Buffer([0, 1, 2]), 'got correct message')

peer2.send(new Uint8Array([2, 3, 4]))
peer2.send(new Uint8Array([0, 2, 4]))
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, new Buffer([2, 3, 4]), 'got correct message')
t.deepEqual(data, new Buffer([0, 2, 4]), 'got correct message')

peer1.destroy(tryDone)
peer2.destroy(tryDone)
Expand All @@ -51,8 +51,8 @@ test('data send/receive Uint8Array', function (t) {
})

test('data send/receive Buffer', function (t) {
var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })
peer1.on('signal', function (data) {
peer2.signal(data)
})
Expand All @@ -65,15 +65,15 @@ test('data send/receive Buffer', function (t) {
function tryTest () {
if (!peer1.connected || peer2.connected) return

peer1.send(new Buffer([1, 2, 3]))
peer1.send(new Buffer([0, 1, 2]))
peer2.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, new Buffer([1, 2, 3]), 'got correct message')
t.deepEqual(data, new Buffer([0, 1, 2]), 'got correct message')

peer2.send(new Buffer([2, 3, 4]))
peer2.send(new Buffer([0, 2, 4]))
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, new Buffer([2, 3, 4]), 'got correct message')
t.deepEqual(data, new Buffer([0, 2, 4]), 'got correct message')

peer1.destroy(tryDone)
peer2.destroy(tryDone)
Expand All @@ -90,8 +90,8 @@ test('data send/receive Buffer', function (t) {
})

test('data send/receive ArrayBuffer', function (t) {
var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })
peer1.on('signal', function (data) {
peer2.signal(data)
})
Expand All @@ -104,15 +104,15 @@ test('data send/receive ArrayBuffer', function (t) {
function tryTest () {
if (!peer1.connected || !peer2.connected) return

peer1.send(new Uint8Array([1, 2, 3]).buffer)
peer1.send(new Uint8Array([0, 1, 2]).buffer)
peer2.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, new Buffer([1, 2, 3]), 'got correct message')
t.deepEqual(data, new Buffer([0, 1, 2]), 'got correct message')

peer2.send(new Uint8Array([2, 3, 4]).buffer)
peer2.send(new Uint8Array([0, 2, 4]).buffer)
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, new Buffer([2, 3, 4]), 'got correct message')
t.deepEqual(data, new Buffer([0, 2, 4]), 'got correct message')

peer1.destroy(tryDone)
peer2.destroy(tryDone)
Expand Down
7 changes: 7 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var get = require('simple-get')
var thunky = require('thunky')
var Peer = require('../')

exports.getConfig = thunky(function (cb) {
// Includes TURN -- needed for tests to pass on Sauce Labs
Expand All @@ -16,3 +17,9 @@ exports.getConfig = thunky(function (cb) {
cb(null, data)
})
})

// For testing on node, you'll need a WebRTC implementation
// Feel free to substitute in another implementation
if (Peer.USING_WRTC) {
exports.wrtc = require('wrtc')
}
8 changes: 4 additions & 4 deletions test/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ test('get config', function (t) {
test('duplex stream: send data before "connect" event', function (t) {
t.plan(9)

var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })
peer1.on('signal', function (data) { if (!peer2.destroyed) peer2.signal(data) })
peer2.on('signal', function (data) { if (!peer1.destroyed) peer1.signal(data) })

Expand Down Expand Up @@ -50,8 +50,8 @@ test('duplex stream: send data before "connect" event', function (t) {
test('duplex stream: send data one-way', function (t) {
t.plan(9)

var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })
peer1.on('signal', function (data) { peer2.signal(data) })
peer2.on('signal', function (data) { peer1.signal(data) })
peer1.on('connect', tryTest)
Expand Down
12 changes: 6 additions & 6 deletions test/trickle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ test('get config', function (t) {
})

test('disable trickle', function (t) {
var peer1 = new Peer({ config: config, initiator: true, trickle: false })
var peer2 = new Peer({ config: config, trickle: false })
var peer1 = new Peer({ config: config, initiator: true, trickle: false, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, trickle: false, wrtc: common.wrtc })

var numSignal1 = 0
peer1.on('signal', function (data) {
Expand Down Expand Up @@ -61,8 +61,8 @@ test('disable trickle', function (t) {
})

test('disable trickle (only initiator)', function (t) {
var peer1 = new Peer({ config: config, initiator: true, trickle: false })
var peer2 = new Peer({ config: config })
var peer1 = new Peer({ config: config, initiator: true, trickle: false, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, wrtc: common.wrtc })

var numSignal1 = 0
peer1.on('signal', function (data) {
Expand Down Expand Up @@ -110,8 +110,8 @@ test('disable trickle (only initiator)', function (t) {
})

test('disable trickle (only receiver)', function (t) {
var peer1 = new Peer({ config: config, initiator: true })
var peer2 = new Peer({ config: config, trickle: false })
var peer1 = new Peer({ config: config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config: config, trickle: false, wrtc: common.wrtc })

var numSignal1 = 0
peer1.on('signal', function (data) {
Expand Down