2
2
const debug = require ( 'debug' ) ( 'simple-peer' )
3
3
const getBrowserRTC = require ( 'get-browser-rtc' )
4
4
const randombytes = require ( 'randombytes' )
5
- const stream = require ( 'readable-stream ' )
5
+ const { Duplex } = require ( 'streamx ' )
6
6
const queueMicrotask = require ( 'queue-microtask' ) // TODO: remove when Node 10 is not supported
7
7
const errCode = require ( 'err-code' )
8
8
const { Buffer } = require ( 'buffer' )
@@ -25,14 +25,16 @@ function warn (message) {
25
25
* Duplex stream.
26
26
* @param {Object } opts
27
27
*/
28
- class Peer extends stream . Duplex {
28
+ class Peer extends Duplex {
29
29
constructor ( opts ) {
30
30
opts = Object . assign ( {
31
31
allowHalfOpen : false
32
32
} , opts )
33
33
34
34
super ( opts )
35
35
36
+ this . __objectMode = ! ! opts . objectMode // streamx is objectMode by default, so implement readable's fuctionality
37
+
36
38
this . _id = randombytes ( 4 ) . toString ( 'hex' ) . slice ( 0 , 7 )
37
39
this . _debug ( 'new peer %o' , opts )
38
40
@@ -52,8 +54,8 @@ class Peer extends stream.Duplex {
52
54
this . allowHalfTrickle = opts . allowHalfTrickle !== undefined ? opts . allowHalfTrickle : false
53
55
this . iceCompleteTimeout = opts . iceCompleteTimeout || ICECOMPLETE_TIMEOUT
54
56
55
- this . destroyed = false
56
- this . destroying = false
57
+ this . _destroying = false
58
+
57
59
this . _connected = false
58
60
59
61
this . remoteAddress = undefined
@@ -180,7 +182,7 @@ class Peer extends stream.Duplex {
180
182
}
181
183
182
184
signal ( data ) {
183
- if ( this . destroying ) return
185
+ if ( this . _destroying ) return
184
186
if ( this . destroyed ) throw errCode ( new Error ( 'cannot signal after peer is destroyed' ) , 'ERR_DESTROYED' )
185
187
if ( typeof data === 'string' ) {
186
188
try {
@@ -244,7 +246,7 @@ class Peer extends stream.Duplex {
244
246
* @param {ArrayBufferView|ArrayBuffer|Buffer|string|Blob } chunk
245
247
*/
246
248
send ( chunk ) {
247
- if ( this . destroying ) return
249
+ if ( this . _destroying ) return
248
250
if ( this . destroyed ) throw errCode ( new Error ( 'cannot send after peer is destroyed' ) , 'ERR_DESTROYED' )
249
251
this . _channel . send ( chunk )
250
252
}
@@ -255,7 +257,7 @@ class Peer extends stream.Duplex {
255
257
* @param {Object } init
256
258
*/
257
259
addTransceiver ( kind , init ) {
258
- if ( this . destroying ) return
260
+ if ( this . _destroying ) return
259
261
if ( this . destroyed ) throw errCode ( new Error ( 'cannot addTransceiver after peer is destroyed' ) , 'ERR_DESTROYED' )
260
262
this . _debug ( 'addTransceiver()' )
261
263
@@ -279,7 +281,7 @@ class Peer extends stream.Duplex {
279
281
* @param {MediaStream } stream
280
282
*/
281
283
addStream ( stream ) {
282
- if ( this . destroying ) return
284
+ if ( this . _destroying ) return
283
285
if ( this . destroyed ) throw errCode ( new Error ( 'cannot addStream after peer is destroyed' ) , 'ERR_DESTROYED' )
284
286
this . _debug ( 'addStream()' )
285
287
@@ -294,7 +296,7 @@ class Peer extends stream.Duplex {
294
296
* @param {MediaStream } stream
295
297
*/
296
298
addTrack ( track , stream ) {
297
- if ( this . destroying ) return
299
+ if ( this . _destroying ) return
298
300
if ( this . destroyed ) throw errCode ( new Error ( 'cannot addTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
299
301
this . _debug ( 'addTrack()' )
300
302
@@ -319,7 +321,7 @@ class Peer extends stream.Duplex {
319
321
* @param {MediaStream } stream
320
322
*/
321
323
replaceTrack ( oldTrack , newTrack , stream ) {
322
- if ( this . destroying ) return
324
+ if ( this . _destroying ) return
323
325
if ( this . destroyed ) throw errCode ( new Error ( 'cannot replaceTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
324
326
this . _debug ( 'replaceTrack()' )
325
327
@@ -343,7 +345,7 @@ class Peer extends stream.Duplex {
343
345
* @param {MediaStream } stream
344
346
*/
345
347
removeTrack ( track , stream ) {
346
- if ( this . destroying ) return
348
+ if ( this . _destroying ) return
347
349
if ( this . destroyed ) throw errCode ( new Error ( 'cannot removeTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
348
350
this . _debug ( 'removeSender()' )
349
351
@@ -370,7 +372,7 @@ class Peer extends stream.Duplex {
370
372
* @param {MediaStream } stream
371
373
*/
372
374
removeStream ( stream ) {
373
- if ( this . destroying ) return
375
+ if ( this . _destroying ) return
374
376
if ( this . destroyed ) throw errCode ( new Error ( 'cannot removeStream after peer is destroyed' ) , 'ERR_DESTROYED' )
375
377
this . _debug ( 'removeSenders()' )
376
378
@@ -396,7 +398,7 @@ class Peer extends stream.Duplex {
396
398
}
397
399
398
400
negotiate ( ) {
399
- if ( this . destroying ) return
401
+ if ( this . _destroying ) return
400
402
if ( this . destroyed ) throw errCode ( new Error ( 'cannot negotiate after peer is destroyed' ) , 'ERR_DESTROYED' )
401
403
402
404
if ( this . initiator ) {
@@ -424,29 +426,19 @@ class Peer extends stream.Duplex {
424
426
this . _isNegotiating = true
425
427
}
426
428
427
- // TODO: Delete this method once readable-stream is updated to contain a default
428
- // implementation of destroy() that automatically calls _destroy()
429
- // See: https://github.com/nodejs/readable-stream/issues/283
430
- destroy ( err ) {
431
- this . _destroy ( err , ( ) => { } )
429
+ _final ( cb ) {
430
+ if ( ! this . _readableState . ended ) this . push ( null )
431
+ cb ( )
432
432
}
433
433
434
- _destroy ( err , cb ) {
435
- if ( this . destroyed || this . destroying ) return
436
- this . destroying = true
434
+ _destroy ( cb ) {
435
+ if ( this . destroyed || this . _destroying ) return
436
+ this . _destroying = true
437
437
438
- this . _debug ( 'destroying (error: %s)' , err && ( err . message || err ) )
438
+ if ( ! this . _writableState . ended ) this . end ( )
439
439
440
440
queueMicrotask ( ( ) => { // allow events concurrent with the call to _destroy() to fire (see #692)
441
- this . destroyed = true
442
- this . destroying = false
443
-
444
- this . _debug ( 'destroy (error: %s)' , err && ( err . message || err ) )
445
-
446
- this . readable = this . writable = false
447
-
448
- if ( ! this . _readableState . ended ) this . push ( null )
449
- if ( ! this . _writableState . finished ) this . end ( )
441
+ this . _destroying = false
450
442
451
443
this . _connected = false
452
444
this . _pcReady = false
@@ -493,8 +485,6 @@ class Peer extends stream.Duplex {
493
485
this . _pc = null
494
486
this . _channel = null
495
487
496
- if ( err ) this . emit ( 'error' , err )
497
- this . emit ( 'close' )
498
488
cb ( )
499
489
} )
500
490
}
@@ -548,9 +538,7 @@ class Peer extends stream.Duplex {
548
538
} , CHANNEL_CLOSING_TIMEOUT )
549
539
}
550
540
551
- _read ( ) { }
552
-
553
- _write ( chunk , encoding , cb ) {
541
+ _write ( chunk , cb ) {
554
542
if ( this . destroyed ) return cb ( errCode ( new Error ( 'cannot write after peer is destroyed' ) , 'ERR_DATA_CHANNEL' ) )
555
543
556
544
if ( this . _connected ) {
@@ -972,7 +960,7 @@ class Peer extends stream.Duplex {
972
960
_onChannelMessage ( event ) {
973
961
if ( this . destroyed ) return
974
962
let data = event . data
975
- if ( data instanceof ArrayBuffer ) data = Buffer . from ( data )
963
+ if ( data instanceof ArrayBuffer || this . __objectMode === false ) data = Buffer . from ( data )
976
964
this . push ( data )
977
965
}
978
966
0 commit comments