diff --git a/index.js b/index.js index dd5bbde0..874c5b74 100644 --- a/index.js +++ b/index.js @@ -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 @@ -391,16 +390,7 @@ 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 @@ -408,7 +398,6 @@ class Peer extends stream.Duplex { 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 @@ -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) { @@ -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) } diff --git a/test/object-mode.js b/test/object-mode.js index c7b5e642..cabe5693 100644 --- a/test/object-mode.js +++ b/test/object-mode.js @@ -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() @@ -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() @@ -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()