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

Create multiple DataChannels #694

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
interior tests with twintap
  • Loading branch information
t-mullen committed Dec 15, 2019
commit 75e5518f0c9eb73f0f9362834d63fe75d7fb9660
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ addons:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
sauce_connect: true
sauce_connect:
no_ssl_bump_domains: airtap.local
hosts:
- airtap.local
env:
Expand Down
10 changes: 10 additions & 0 deletions .twintap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sauce_connect: true
loopback: airtap.local
browsers:
- name: firefox
version: latest
- name: chrome
version: latest
- name: safari
version: latest

11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Peer extends stream.Duplex {
}

signal (data) {
if (this.destroyed) throw makeError('cannot signal after peer is destroyed', 'ERR_SIGNALING')
if (this.destroyed) throw makeError('cannot signal after peer is destroyed', 'ERR_DESTROYED')
if (typeof data === 'string') {
try {
data = JSON.parse(data)
Expand Down Expand Up @@ -249,6 +249,7 @@ class Peer extends stream.Duplex {
*/
addTransceiver (kind, init) {
this._debug('addTransceiver()')
if (this.destroyed) throw makeError('cannot add transceiver after peer is destroyed', 'ERR_DESTROYED')

if (this.initiator) {
try {
Expand All @@ -270,6 +271,7 @@ class Peer extends stream.Duplex {
*/
addStream (stream) {
this._debug('addStream()')
if (this.destroyed) throw makeError('cannot add stream after peer is destroyed', 'ERR_DESTROYED')

stream.getTracks().forEach(track => {
this.addTrack(track, stream)
Expand All @@ -283,6 +285,7 @@ class Peer extends stream.Duplex {
*/
addTrack (track, stream) {
this._debug('addTrack()')
if (this.destroyed) throw makeError('cannot add track after peer is destroyed', 'ERR_DESTROYED')

var submap = this._senderMap.get(track) || new Map() // nested Maps map [track, stream] to sender
var sender = submap.get(stream)
Expand All @@ -306,6 +309,7 @@ class Peer extends stream.Duplex {
*/
replaceTrack (oldTrack, newTrack, stream) {
this._debug('replaceTrack()')
if (this.destroyed) throw makeError('cannot replace track after peer is destroyed', 'ERR_DESTROYED')

var submap = this._senderMap.get(oldTrack)
var sender = submap ? submap.get(stream) : null
Expand All @@ -328,6 +332,7 @@ class Peer extends stream.Duplex {
*/
removeTrack (track, stream) {
this._debug('removeSender()')
if (this.destroyed) throw makeError('cannot replace track after peer is destroyed', 'ERR_DESTROYED')

var submap = this._senderMap.get(track)
var sender = submap ? submap.get(stream) : null
Expand Down Expand Up @@ -507,7 +512,7 @@ class Peer extends stream.Duplex {
_read () {}

_write (chunk, encoding, cb) {
if (this.destroyed) return cb(makeError('cannot write after peer is destroyed', 'ERR_DATA_CHANNEL'))
if (this.destroyed) return cb(makeError('cannot write after peer is destroyed', 'ERR_DESTROYED'))

if (this._connected) {
try {
Expand Down Expand Up @@ -681,6 +686,8 @@ class Peer extends stream.Duplex {
}

getStats (cb) {
if (this.destroyed) throw makeError('cannot get stats after peer is destroyed', 'ERR_DESTROYED')

// statreports can come with a value array instead of properties
const flattenValues = report => {
if (Object.prototype.toString.call(report.values) === '[object Array]') {
Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"string-to-stream": "^3.0.0",
"tape": "^4.0.0",
"thunky": "^1.0.1",
"twintap": "^1.3.1",
"wrtc": "^0.4.1",
"ws": "^7.0.0"
},
Expand Down Expand Up @@ -57,10 +58,13 @@
"scripts": {
"build": "browserify -s SimplePeer -r . | minify > simplepeer.min.js",
"size": "npm run build && cat simplepeer.min.js | gzip | wc -c",
"test": "standard && npm run test-node && npm run test-browser",
"test-browser": "airtap --coverage -- test/*.js",
"test-browser-local": "airtap --coverage --local -- test/*.js",
"test-coverage": "nyc report --reporter=html",
"test-node": "WRTC=wrtc tape test/*.js"
"test": "standard && npm run test-node && npm run test-browser && npm run test-interop && coverage",
"test-browser": "airtap --coverage -- test/unit/*.js",
"test-browser-local": "airtap --coverage --local -- test/unit/*.js",
"test-node": "WRTC=wrtc tape test/unit/*.js",
"test-interop": "npm run test-interop-node && npm run test-interop-browser",
"test-interop-browser": "twintap -- test/interop/*.js",
"test-interop-browser-local": "twintap --local -- test/interop/*.js",
"coverage": "nyc report --reporter=html"
}
}
5 changes: 5 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ var get = require('simple-get')
var thunky = require('thunky')
var bowser = require('bowser')

let cachedConfig = null
exports.getConfig = thunky(function (cb) {
// Includes TURN -- needed for tests to pass on Sauce Labs
// https://github.com/feross/simple-peer/issues/41
// WARNING: This is *NOT* a public endpoint. Do not depend on it in your app.
if (cachedConfig) {
return cb(null, cachedConfig)
}
get.concat('https://instant.io/__rtcConfig__', function (err, res, data) {
if (err) return cb(err)
data = data.toString()
Expand All @@ -15,6 +19,7 @@ exports.getConfig = thunky(function (cb) {
cb(err)
return
}
cachedConfig = data
cb(null, data)
})
})
Expand Down
45 changes: 45 additions & 0 deletions test/interop/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var common = require('../common')
var Peer = require('../..')
var test = require('twintap/tape')

var config
test('get config', function (t) {
common.getConfig(function (err, _config) {
if (err) return t.fail(err)
config = _config
t.end()
})
})

test('data send/receive text', function (t) {
t.plan(5)

var peer = new Peer({ config: config, initiator: !!t.instance, wrtc: common.wrtc })

var numSignal = 0
peer.on('signal', function (data) {
numSignal += 1
t.send('signal', data)
})
t.receive('signal', (data) => {
peer.signal(data)
})
peer.on('connect', tryTest)

async function tryTest () {
await t.barrier('connected')

t.ok(numSignal >= 1)
t.equal(peer.initiator, !!t.instance, 'peer.initiator is correct')

peer.send('sup peer' + ((t.instance + 1) % 2))
peer.on('data', async function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.equal(data.toString(), 'sup peer' + (t.instance), 'got correct message')

await t.barrier('waitingToClose')
peer.on('close', function () { t.pass('peer destroyed') })
peer.destroy()
})
}
})
97 changes: 97 additions & 0 deletions test/interop/binary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
var common = require('../common')
var Peer = require('../..')
var test = require('twintap/tape')

var config
test('get config', function (t) {
common.getConfig(function (err, _config) {
if (err) return t.fail(err)
config = _config
t.end()
})
})

test('data send/receive Buffer', function (t) {
t.plan(3)

var peer = new Peer({ config: config, initiator: !!t.instance, wrtc: common.wrtc })
peer.on('signal', function (data) {
t.send('signal', data)
})
t.receive('signal', function (data) {
peer.signal(data)
})
peer.on('connect', tryTest)

async function tryTest () {
await t.barrier('connected')

peer.send(Buffer.from([0, 1, 2]))
peer.on('data', async function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')

await t.barrier('readyToClose')
peer.on('close', function () { t.pass('peer destroyed') })
peer.destroy()
})
}
})

test('data send/receive Uint8Array', function (t) {
t.plan(3)

var peer = new Peer({ config: config, initiator: !!t.instance, wrtc: common.wrtc })
peer.on('signal', function (data) {
t.send('signal', data)
})
t.receive('signal', function (data) {
peer.signal(data)
})
peer.on('connect', tryTest)

async function tryTest () {
await t.barrier('connected')

peer.send(new Uint8Array([0, 1, 2]))
peer.on('data', async function (data) {
// binary types always get converted to Buffer
// See: https://github.com/feross/simple-peer/issues/138#issuecomment-278240571
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')

await t.barrier('readyToClose')
peer.on('close', function () { t.pass('peer destroyed') })
peer.destroy()
})
}
})

test('data send/receive ArrayBuffer', function (t) {
t.plan(3)

var peer = new Peer({ config: config, initiator: !!t.instance, wrtc: common.wrtc })
peer.on('signal', function (data) {
t.send('signal', data)
})
t.receive('signal', function (data) {
peer.signal(data)
})
peer.on('connect', tryTest)

async function tryTest () {
await t.barrier('connected')

peer.send(new Uint8Array([0, 1, 2]).buffer)
peer.on('data', async function (data) {
// binary types always get converted to Buffer
// See: https://github.com/feross/simple-peer/issues/138#issuecomment-278240571
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')

await t.barrier('readyToClose')
peer.on('close', function () { t.pass('peer destroyed') })
peer.destroy()
})
}
})
Loading