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,7 +25,7 @@ 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
@@ -52,8 +52,8 @@ class Peer extends stream.Duplex {
52
52
this . allowHalfTrickle = opts . allowHalfTrickle !== undefined ? opts . allowHalfTrickle : false
53
53
this . iceCompleteTimeout = opts . iceCompleteTimeout || ICECOMPLETE_TIMEOUT
54
54
55
- this . destroyed = false
56
- this . destroying = false
55
+ this . _destroyed = false
56
+ this . _destroying = false
57
57
this . _connected = false
58
58
59
59
this . remoteAddress = undefined
@@ -180,8 +180,8 @@ class Peer extends stream.Duplex {
180
180
}
181
181
182
182
signal ( data ) {
183
- if ( this . destroying ) return
184
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot signal after peer is destroyed' ) , 'ERR_DESTROYED' )
183
+ if ( this . _destroying ) return
184
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot signal after peer is destroyed' ) , 'ERR_DESTROYED' )
185
185
if ( typeof data === 'string' ) {
186
186
try {
187
187
data = JSON . parse ( data )
@@ -209,7 +209,7 @@ class Peer extends stream.Duplex {
209
209
if ( data . sdp ) {
210
210
this . _pc . setRemoteDescription ( new ( this . _wrtc . RTCSessionDescription ) ( data ) )
211
211
. then ( ( ) => {
212
- if ( this . destroyed ) return
212
+ if ( this . _destroyed ) return
213
213
214
214
this . _pendingCandidates . forEach ( candidate => {
215
215
this . _addIceCandidate ( candidate )
@@ -244,8 +244,8 @@ class Peer extends stream.Duplex {
244
244
* @param {ArrayBufferView|ArrayBuffer|Buffer|string|Blob } chunk
245
245
*/
246
246
send ( chunk ) {
247
- if ( this . destroying ) return
248
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot send after peer is destroyed' ) , 'ERR_DESTROYED' )
247
+ if ( this . _destroying ) return
248
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot send after peer is destroyed' ) , 'ERR_DESTROYED' )
249
249
this . _channel . send ( chunk )
250
250
}
251
251
@@ -255,8 +255,8 @@ class Peer extends stream.Duplex {
255
255
* @param {Object } init
256
256
*/
257
257
addTransceiver ( kind , init ) {
258
- if ( this . destroying ) return
259
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot addTransceiver after peer is destroyed' ) , 'ERR_DESTROYED' )
258
+ if ( this . _destroying ) return
259
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot addTransceiver after peer is destroyed' ) , 'ERR_DESTROYED' )
260
260
this . _debug ( 'addTransceiver()' )
261
261
262
262
if ( this . initiator ) {
@@ -279,8 +279,8 @@ class Peer extends stream.Duplex {
279
279
* @param {MediaStream } stream
280
280
*/
281
281
addStream ( stream ) {
282
- if ( this . destroying ) return
283
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot addStream after peer is destroyed' ) , 'ERR_DESTROYED' )
282
+ if ( this . _destroying ) return
283
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot addStream after peer is destroyed' ) , 'ERR_DESTROYED' )
284
284
this . _debug ( 'addStream()' )
285
285
286
286
stream . getTracks ( ) . forEach ( track => {
@@ -294,8 +294,8 @@ class Peer extends stream.Duplex {
294
294
* @param {MediaStream } stream
295
295
*/
296
296
addTrack ( track , stream ) {
297
- if ( this . destroying ) return
298
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot addTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
297
+ if ( this . _destroying ) return
298
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot addTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
299
299
this . _debug ( 'addTrack()' )
300
300
301
301
const submap = this . _senderMap . get ( track ) || new Map ( ) // nested Maps map [track, stream] to sender
@@ -319,8 +319,8 @@ class Peer extends stream.Duplex {
319
319
* @param {MediaStream } stream
320
320
*/
321
321
replaceTrack ( oldTrack , newTrack , stream ) {
322
- if ( this . destroying ) return
323
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot replaceTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
322
+ if ( this . _destroying ) return
323
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot replaceTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
324
324
this . _debug ( 'replaceTrack()' )
325
325
326
326
const submap = this . _senderMap . get ( oldTrack )
@@ -343,8 +343,8 @@ class Peer extends stream.Duplex {
343
343
* @param {MediaStream } stream
344
344
*/
345
345
removeTrack ( track , stream ) {
346
- if ( this . destroying ) return
347
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot removeTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
346
+ if ( this . _destroying ) return
347
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot removeTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
348
348
this . _debug ( 'removeSender()' )
349
349
350
350
const submap = this . _senderMap . get ( track )
@@ -370,8 +370,8 @@ class Peer extends stream.Duplex {
370
370
* @param {MediaStream } stream
371
371
*/
372
372
removeStream ( stream ) {
373
- if ( this . destroying ) return
374
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot removeStream after peer is destroyed' ) , 'ERR_DESTROYED' )
373
+ if ( this . _destroying ) return
374
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot removeStream after peer is destroyed' ) , 'ERR_DESTROYED' )
375
375
this . _debug ( 'removeSenders()' )
376
376
377
377
stream . getTracks ( ) . forEach ( track => {
@@ -396,8 +396,8 @@ class Peer extends stream.Duplex {
396
396
}
397
397
398
398
negotiate ( ) {
399
- if ( this . destroying ) return
400
- if ( this . destroyed ) throw errCode ( new Error ( 'cannot negotiate after peer is destroyed' ) , 'ERR_DESTROYED' )
399
+ if ( this . _destroying ) return
400
+ if ( this . _destroyed ) throw errCode ( new Error ( 'cannot negotiate after peer is destroyed' ) , 'ERR_DESTROYED' )
401
401
402
402
if ( this . initiator ) {
403
403
if ( this . _isNegotiating ) {
@@ -424,29 +424,16 @@ class Peer extends stream.Duplex {
424
424
this . _isNegotiating = true
425
425
}
426
426
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 , ( ) => { } )
432
- }
433
-
434
- _destroy ( err , cb ) {
435
- if ( this . destroyed || this . destroying ) return
436
- this . destroying = true
437
-
438
- this . _debug ( 'destroying (error: %s)' , err && ( err . message || err ) )
427
+ _predestroy ( ) {
428
+ if ( this . _destroyed || this . _destroying ) return
429
+ this . _destroying = true
439
430
440
431
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
432
+ this . _destroyed = true
433
+ this . _destroying = false
447
434
448
435
if ( ! this . _readableState . ended ) this . push ( null )
449
- if ( ! this . _writableState . finished ) this . end ( )
436
+ if ( ! this . _writableState . ended ) this . end ( )
450
437
451
438
this . _connected = false
452
439
this . _pcReady = false
@@ -492,10 +479,6 @@ class Peer extends stream.Duplex {
492
479
}
493
480
this . _pc = null
494
481
this . _channel = null
495
-
496
- if ( err ) this . emit ( 'error' , err )
497
- this . emit ( 'close' )
498
- cb ( )
499
482
} )
500
483
}
501
484
@@ -548,10 +531,8 @@ class Peer extends stream.Duplex {
548
531
} , CHANNEL_CLOSING_TIMEOUT )
549
532
}
550
533
551
- _read ( ) { }
552
-
553
- _write ( chunk , encoding , cb ) {
554
- if ( this . destroyed ) return cb ( errCode ( new Error ( 'cannot write after peer is destroyed' ) , 'ERR_DATA_CHANNEL' ) )
534
+ _write ( chunk , cb ) {
535
+ if ( this . _destroyed ) return cb ( errCode ( new Error ( 'cannot write after peer is destroyed' ) , 'ERR_DATA_CHANNEL' ) )
555
536
556
537
if ( this . _connected ) {
557
538
try {
@@ -575,7 +556,7 @@ class Peer extends stream.Duplex {
575
556
// When stream finishes writing, close socket. Half open connections are not
576
557
// supported.
577
558
_onFinish ( ) {
578
- if ( this . destroyed ) return
559
+ if ( this . _destroyed ) return
579
560
580
561
// Wait a bit before destroying so the socket flushes.
581
562
// TODO: is there a more reliable way to accomplish this?
@@ -591,7 +572,7 @@ class Peer extends stream.Duplex {
591
572
}
592
573
593
574
_startIceCompleteTimeout ( ) {
594
- if ( this . destroyed ) return
575
+ if ( this . _destroyed ) return
595
576
if ( this . _iceCompleteTimer ) return
596
577
this . _debug ( 'started iceComplete timeout' )
597
578
this . _iceCompleteTimer = setTimeout ( ( ) => {
@@ -605,16 +586,16 @@ class Peer extends stream.Duplex {
605
586
}
606
587
607
588
_createOffer ( ) {
608
- if ( this . destroyed ) return
589
+ if ( this . _destroyed ) return
609
590
610
591
this . _pc . createOffer ( this . offerOptions )
611
592
. then ( offer => {
612
- if ( this . destroyed ) return
593
+ if ( this . _destroyed ) return
613
594
if ( ! this . trickle && ! this . allowHalfTrickle ) offer . sdp = filterTrickle ( offer . sdp )
614
595
offer . sdp = this . sdpTransform ( offer . sdp )
615
596
616
597
const sendOffer = ( ) => {
617
- if ( this . destroyed ) return
598
+ if ( this . _destroyed ) return
618
599
const signal = this . _pc . localDescription || offer
619
600
this . _debug ( 'signal' )
620
601
this . emit ( 'signal' , {
@@ -625,7 +606,7 @@ class Peer extends stream.Duplex {
625
606
626
607
const onSuccess = ( ) => {
627
608
this . _debug ( 'createOffer success' )
628
- if ( this . destroyed ) return
609
+ if ( this . _destroyed ) return
629
610
if ( this . trickle || this . _iceComplete ) sendOffer ( )
630
611
else this . once ( '_iceComplete' , sendOffer ) // wait for candidates
631
612
}
@@ -655,16 +636,16 @@ class Peer extends stream.Duplex {
655
636
}
656
637
657
638
_createAnswer ( ) {
658
- if ( this . destroyed ) return
639
+ if ( this . _destroyed ) return
659
640
660
641
this . _pc . createAnswer ( this . answerOptions )
661
642
. then ( answer => {
662
- if ( this . destroyed ) return
643
+ if ( this . _destroyed ) return
663
644
if ( ! this . trickle && ! this . allowHalfTrickle ) answer . sdp = filterTrickle ( answer . sdp )
664
645
answer . sdp = this . sdpTransform ( answer . sdp )
665
646
666
647
const sendAnswer = ( ) => {
667
- if ( this . destroyed ) return
648
+ if ( this . _destroyed ) return
668
649
const signal = this . _pc . localDescription || answer
669
650
this . _debug ( 'signal' )
670
651
this . emit ( 'signal' , {
@@ -675,7 +656,7 @@ class Peer extends stream.Duplex {
675
656
}
676
657
677
658
const onSuccess = ( ) => {
678
- if ( this . destroyed ) return
659
+ if ( this . _destroyed ) return
679
660
if ( this . trickle || this . _iceComplete ) sendAnswer ( )
680
661
else this . once ( '_iceComplete' , sendAnswer )
681
662
}
@@ -694,14 +675,14 @@ class Peer extends stream.Duplex {
694
675
}
695
676
696
677
_onConnectionStateChange ( ) {
697
- if ( this . destroyed ) return
678
+ if ( this . _destroyed ) return
698
679
if ( this . _pc . connectionState === 'failed' ) {
699
680
this . destroy ( errCode ( new Error ( 'Connection failed.' ) , 'ERR_CONNECTION_FAILURE' ) )
700
681
}
701
682
}
702
683
703
684
_onIceStateChange ( ) {
704
- if ( this . destroyed ) return
685
+ if ( this . _destroyed ) return
705
686
const iceConnectionState = this . _pc . iceConnectionState
706
687
const iceGatheringState = this . _pc . iceGatheringState
707
688
@@ -750,7 +731,7 @@ class Peer extends stream.Duplex {
750
731
} else if ( this . _pc . getStats . length > 0 ) {
751
732
this . _pc . getStats ( res => {
752
733
// If we destroy connection in `connect` callback this code might happen to run when actual connection is already closed
753
- if ( this . destroyed ) return
734
+ if ( this . _destroyed ) return
754
735
755
736
const reports = [ ]
756
737
res . result ( ) . forEach ( result => {
@@ -781,10 +762,10 @@ class Peer extends stream.Duplex {
781
762
782
763
// HACK: We can't rely on order here, for details see https://github.com/js-platform/node-webrtc/issues/339
783
764
const findCandidatePair = ( ) => {
784
- if ( this . destroyed ) return
765
+ if ( this . _destroyed ) return
785
766
786
767
this . getStats ( ( err , items ) => {
787
- if ( this . destroyed ) return
768
+ if ( this . _destroyed ) return
788
769
789
770
// Treat getStats error as non-fatal. It's not essential.
790
771
if ( err ) items = [ ]
@@ -921,7 +902,7 @@ class Peer extends stream.Duplex {
921
902
}
922
903
923
904
_onSignalingStateChange ( ) {
924
- if ( this . destroyed ) return
905
+ if ( this . _destroyed ) return
925
906
926
907
if ( this . _pc . signalingState === 'stable' ) {
927
908
this . _isNegotiating = false
@@ -949,7 +930,7 @@ class Peer extends stream.Duplex {
949
930
}
950
931
951
932
_onIceCandidate ( event ) {
952
- if ( this . destroyed ) return
933
+ if ( this . _destroyed ) return
953
934
if ( event . candidate && this . trickle ) {
954
935
this . emit ( 'signal' , {
955
936
type : 'candidate' ,
@@ -970,35 +951,35 @@ class Peer extends stream.Duplex {
970
951
}
971
952
972
953
_onChannelMessage ( event ) {
973
- if ( this . destroyed ) return
954
+ if ( this . _destroyed ) return
974
955
let data = event . data
975
956
if ( data instanceof ArrayBuffer ) data = Buffer . from ( data )
976
957
this . push ( data )
977
958
}
978
959
979
960
_onChannelBufferedAmountLow ( ) {
980
- if ( this . destroyed || ! this . _cb ) return
961
+ if ( this . _destroyed || ! this . _cb ) return
981
962
this . _debug ( 'ending backpressure: bufferedAmount %d' , this . _channel . bufferedAmount )
982
963
const cb = this . _cb
983
964
this . _cb = null
984
965
cb ( null )
985
966
}
986
967
987
968
_onChannelOpen ( ) {
988
- if ( this . _connected || this . destroyed ) return
969
+ if ( this . _connected || this . _destroyed ) return
989
970
this . _debug ( 'on channel open' )
990
971
this . _channelReady = true
991
972
this . _maybeReady ( )
992
973
}
993
974
994
975
_onChannelClose ( ) {
995
- if ( this . destroyed ) return
976
+ if ( this . _destroyed ) return
996
977
this . _debug ( 'on channel close' )
997
978
this . destroy ( )
998
979
}
999
980
1000
981
_onTrack ( event ) {
1001
- if ( this . destroyed ) return
982
+ if ( this . _destroyed ) return
1002
983
1003
984
event . streams . forEach ( eventStream => {
1004
985
this . _debug ( 'on track' )
0 commit comments