Skip to content
Merged
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
17 changes: 2 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Peer extends stream.Duplex {
this.allowHalfTrickle = opts.allowHalfTrickle !== undefined ? opts.allowHalfTrickle : false
this.iceCompleteTimeout = opts.iceCompleteTimeout || ICECOMPLETE_TIMEOUT

this.destroyed = false
this._connected = false

this.remoteAddress = undefined
Expand Down Expand Up @@ -391,24 +390,14 @@ class Peer extends stream.Duplex {
this._isNegotiating = true
}

// TODO: Delete this method once readable-stream is updated to contain a default
// implementation of destroy() that automatically calls _destroy()
// See: https://github.com/nodejs/readable-stream/issues/283
destroy (err) {
this._destroy(err, () => {})
}

_destroy (err, cb) {
if (this.destroyed) return

this._debug('destroy (error: %s)', err && (err.message || err))

this.readable = this.writable = false

if (!this._readableState.ended) this.push(null)
if (!this._writableState.finished) this.end()

this.destroyed = true
this._connected = false
this._pcReady = false
this._channelReady = false
Expand Down Expand Up @@ -452,9 +441,7 @@ class Peer extends stream.Duplex {
this._pc = null
this._channel = null

if (err) this.emit('error', err)
this.emit('close')
cb()
cb(err)
}

_setupData (event) {
Expand Down Expand Up @@ -924,7 +911,7 @@ class Peer extends stream.Duplex {
_onChannelMessage (event) {
if (this.destroyed) return
var data = event.data
if (data instanceof ArrayBuffer) data = Buffer.from(data)
if (data instanceof ArrayBuffer) data = new Uint8Array(data)
this.push(data)
}

Expand Down
28 changes: 14 additions & 14 deletions test/object-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ test('data send/receive Buffer {objectMode: true}', function (t) {
function tryTest () {
if (!peer1.connected || !peer2.connected) return

peer1.send(Buffer.from('this is a Buffer'))
peer1.send(Uint8Array.from('this is a Buffer'))
peer2.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is a Buffer')
t.deepEqual(data, Buffer.from('this is a Buffer'), 'got correct message')
t.ok(data instanceof Uint8Array, 'data is a Buffer')
t.deepEqual(data, Uint8Array.from('this is a Buffer'), 'got correct message')

peer2.send(Buffer.from('this is another Buffer'))
peer2.send(Uint8Array.from('this is another Uint8Array'))
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is a Buffer')
t.deepEqual(data, Buffer.from('this is another Buffer'), 'got correct message')
t.ok(data instanceof Uint8Array, 'data is a Uint8Array')
t.deepEqual(data, Uint8Array.from('this is another Uint8Array'), 'got correct message')

peer1.on('close', function () { t.pass('peer1 destroyed') })
peer1.destroy()
Expand Down Expand Up @@ -104,13 +104,13 @@ test('data send/receive Uint8Array {objectMode: true}', function (t) {
peer2.on('data', 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 a Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')
t.ok(data instanceof Uint8Array, 'data is a Uint8Array')
t.deepEqual(data, Uint8Array.from([0, 1, 2]), 'got correct message')

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

peer1.on('close', function () { t.pass('peer1 destroyed') })
peer1.destroy()
Expand Down Expand Up @@ -140,13 +140,13 @@ test('data send/receive ArrayBuffer {objectMode: true}', function (t) {

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

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

peer1.on('close', function () { t.pass('peer1 destroyed') })
peer1.destroy()
Expand Down