Skip to content

Commit 0ed16c2

Browse files
authored
feat: use Uint8Array instead of Buffer (#1)
fix: use build in readable-stream.destroy() readable-stream 3 added support for .destroy, this will now just use the built in destroy and avoid overrind .destroyed, as that is handled automatically BREAKING CHANGES: uses Uint8Array instead of Buffer
1 parent 9976301 commit 0ed16c2

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

index.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class Peer extends stream.Duplex {
5151
this.allowHalfTrickle = opts.allowHalfTrickle !== undefined ? opts.allowHalfTrickle : false
5252
this.iceCompleteTimeout = opts.iceCompleteTimeout || ICECOMPLETE_TIMEOUT
5353

54-
this.destroyed = false
5554
this._connected = false
5655

5756
this.remoteAddress = undefined
@@ -391,24 +390,14 @@ class Peer extends stream.Duplex {
391390
this._isNegotiating = true
392391
}
393392

394-
// TODO: Delete this method once readable-stream is updated to contain a default
395-
// implementation of destroy() that automatically calls _destroy()
396-
// See: https://github.com/nodejs/readable-stream/issues/283
397-
destroy (err) {
398-
this._destroy(err, () => {})
399-
}
400-
401393
_destroy (err, cb) {
402-
if (this.destroyed) return
403-
404394
this._debug('destroy (error: %s)', err && (err.message || err))
405395

406396
this.readable = this.writable = false
407397

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

411-
this.destroyed = true
412401
this._connected = false
413402
this._pcReady = false
414403
this._channelReady = false
@@ -452,9 +441,7 @@ class Peer extends stream.Duplex {
452441
this._pc = null
453442
this._channel = null
454443

455-
if (err) this.emit('error', err)
456-
this.emit('close')
457-
cb()
444+
cb(err)
458445
}
459446

460447
_setupData (event) {
@@ -924,7 +911,7 @@ class Peer extends stream.Duplex {
924911
_onChannelMessage (event) {
925912
if (this.destroyed) return
926913
var data = event.data
927-
if (data instanceof ArrayBuffer) data = Buffer.from(data)
914+
if (data instanceof ArrayBuffer) data = new Uint8Array(data)
928915
this.push(data)
929916
}
930917

test/object-mode.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ test('data send/receive Buffer {objectMode: true}', function (t) {
6464
function tryTest () {
6565
if (!peer1.connected || !peer2.connected) return
6666

67-
peer1.send(Buffer.from('this is a Buffer'))
67+
peer1.send(Uint8Array.from('this is a Buffer'))
6868
peer2.on('data', function (data) {
69-
t.ok(Buffer.isBuffer(data), 'data is a Buffer')
70-
t.deepEqual(data, Buffer.from('this is a Buffer'), 'got correct message')
69+
t.ok(data instanceof Uint8Array, 'data is a Buffer')
70+
t.deepEqual(data, Uint8Array.from('this is a Buffer'), 'got correct message')
7171

72-
peer2.send(Buffer.from('this is another Buffer'))
72+
peer2.send(Uint8Array.from('this is another Uint8Array'))
7373
peer1.on('data', function (data) {
74-
t.ok(Buffer.isBuffer(data), 'data is a Buffer')
75-
t.deepEqual(data, Buffer.from('this is another Buffer'), 'got correct message')
74+
t.ok(data instanceof Uint8Array, 'data is a Uint8Array')
75+
t.deepEqual(data, Uint8Array.from('this is another Uint8Array'), 'got correct message')
7676

7777
peer1.on('close', function () { t.pass('peer1 destroyed') })
7878
peer1.destroy()
@@ -104,13 +104,13 @@ test('data send/receive Uint8Array {objectMode: true}', function (t) {
104104
peer2.on('data', function (data) {
105105
// binary types always get converted to Buffer
106106
// See: https://github.com/feross/simple-peer/issues/138#issuecomment-278240571
107-
t.ok(Buffer.isBuffer(data), 'data is a Buffer')
108-
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')
107+
t.ok(data instanceof Uint8Array, 'data is a Uint8Array')
108+
t.deepEqual(data, Uint8Array.from([0, 1, 2]), 'got correct message')
109109

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

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

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

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

151151
peer1.on('close', function () { t.pass('peer1 destroyed') })
152152
peer1.destroy()

0 commit comments

Comments
 (0)