22const debug = require ( 'debug' ) ( 'simple-peer' )
33const getBrowserRTC = require ( 'get-browser-rtc' )
44const randombytes = require ( 'randombytes' )
5- const stream = require ( 'readable-stream ' )
5+ const { Duplex } = require ( 'streamx ' )
66const queueMicrotask = require ( 'queue-microtask' ) // TODO: remove when Node 10 is not supported
77const errCode = require ( 'err-code' )
88const { Buffer } = require ( 'buffer' )
@@ -25,7 +25,7 @@ function warn (message) {
2525 * Duplex stream.
2626 * @param {Object } opts
2727 */
28- class Peer extends stream . Duplex {
28+ class Peer extends Duplex {
2929 constructor ( opts ) {
3030 opts = Object . assign ( {
3131 allowHalfOpen : false
@@ -52,8 +52,8 @@ class Peer extends stream.Duplex {
5252 this . allowHalfTrickle = opts . allowHalfTrickle !== undefined ? opts . allowHalfTrickle : false
5353 this . iceCompleteTimeout = opts . iceCompleteTimeout || ICECOMPLETE_TIMEOUT
5454
55- this . destroyed = false
56- this . destroying = false
55+ this . _destroyed = false
56+ this . _destroying = false
5757 this . _connected = false
5858
5959 this . remoteAddress = undefined
@@ -180,8 +180,8 @@ class Peer extends stream.Duplex {
180180 }
181181
182182 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' )
185185 if ( typeof data === 'string' ) {
186186 try {
187187 data = JSON . parse ( data )
@@ -209,7 +209,7 @@ class Peer extends stream.Duplex {
209209 if ( data . sdp ) {
210210 this . _pc . setRemoteDescription ( new ( this . _wrtc . RTCSessionDescription ) ( data ) )
211211 . then ( ( ) => {
212- if ( this . destroyed ) return
212+ if ( this . _destroyed ) return
213213
214214 this . _pendingCandidates . forEach ( candidate => {
215215 this . _addIceCandidate ( candidate )
@@ -244,8 +244,8 @@ class Peer extends stream.Duplex {
244244 * @param {ArrayBufferView|ArrayBuffer|Buffer|string|Blob } chunk
245245 */
246246 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' )
249249 this . _channel . send ( chunk )
250250 }
251251
@@ -255,8 +255,8 @@ class Peer extends stream.Duplex {
255255 * @param {Object } init
256256 */
257257 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' )
260260 this . _debug ( 'addTransceiver()' )
261261
262262 if ( this . initiator ) {
@@ -279,8 +279,8 @@ class Peer extends stream.Duplex {
279279 * @param {MediaStream } stream
280280 */
281281 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' )
284284 this . _debug ( 'addStream()' )
285285
286286 stream . getTracks ( ) . forEach ( track => {
@@ -294,8 +294,8 @@ class Peer extends stream.Duplex {
294294 * @param {MediaStream } stream
295295 */
296296 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' )
299299 this . _debug ( 'addTrack()' )
300300
301301 const submap = this . _senderMap . get ( track ) || new Map ( ) // nested Maps map [track, stream] to sender
@@ -319,8 +319,8 @@ class Peer extends stream.Duplex {
319319 * @param {MediaStream } stream
320320 */
321321 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' )
324324 this . _debug ( 'replaceTrack()' )
325325
326326 const submap = this . _senderMap . get ( oldTrack )
@@ -343,8 +343,8 @@ class Peer extends stream.Duplex {
343343 * @param {MediaStream } stream
344344 */
345345 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' )
348348 this . _debug ( 'removeSender()' )
349349
350350 const submap = this . _senderMap . get ( track )
@@ -370,8 +370,8 @@ class Peer extends stream.Duplex {
370370 * @param {MediaStream } stream
371371 */
372372 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' )
375375 this . _debug ( 'removeSenders()' )
376376
377377 stream . getTracks ( ) . forEach ( track => {
@@ -396,8 +396,8 @@ class Peer extends stream.Duplex {
396396 }
397397
398398 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' )
401401
402402 if ( this . initiator ) {
403403 if ( this . _isNegotiating ) {
@@ -424,29 +424,16 @@ class Peer extends stream.Duplex {
424424 this . _isNegotiating = true
425425 }
426426
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
439430
440431 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
447434
448435 if ( ! this . _readableState . ended ) this . push ( null )
449- if ( ! this . _writableState . finished ) this . end ( )
436+ if ( ! this . _writableState . ended ) this . end ( )
450437
451438 this . _connected = false
452439 this . _pcReady = false
@@ -492,10 +479,6 @@ class Peer extends stream.Duplex {
492479 }
493480 this . _pc = null
494481 this . _channel = null
495-
496- if ( err ) this . emit ( 'error' , err )
497- this . emit ( 'close' )
498- cb ( )
499482 } )
500483 }
501484
@@ -548,10 +531,8 @@ class Peer extends stream.Duplex {
548531 } , CHANNEL_CLOSING_TIMEOUT )
549532 }
550533
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' ) )
555536
556537 if ( this . _connected ) {
557538 try {
@@ -575,7 +556,7 @@ class Peer extends stream.Duplex {
575556 // When stream finishes writing, close socket. Half open connections are not
576557 // supported.
577558 _onFinish ( ) {
578- if ( this . destroyed ) return
559+ if ( this . _destroyed ) return
579560
580561 // Wait a bit before destroying so the socket flushes.
581562 // TODO: is there a more reliable way to accomplish this?
@@ -591,7 +572,7 @@ class Peer extends stream.Duplex {
591572 }
592573
593574 _startIceCompleteTimeout ( ) {
594- if ( this . destroyed ) return
575+ if ( this . _destroyed ) return
595576 if ( this . _iceCompleteTimer ) return
596577 this . _debug ( 'started iceComplete timeout' )
597578 this . _iceCompleteTimer = setTimeout ( ( ) => {
@@ -605,16 +586,16 @@ class Peer extends stream.Duplex {
605586 }
606587
607588 _createOffer ( ) {
608- if ( this . destroyed ) return
589+ if ( this . _destroyed ) return
609590
610591 this . _pc . createOffer ( this . offerOptions )
611592 . then ( offer => {
612- if ( this . destroyed ) return
593+ if ( this . _destroyed ) return
613594 if ( ! this . trickle && ! this . allowHalfTrickle ) offer . sdp = filterTrickle ( offer . sdp )
614595 offer . sdp = this . sdpTransform ( offer . sdp )
615596
616597 const sendOffer = ( ) => {
617- if ( this . destroyed ) return
598+ if ( this . _destroyed ) return
618599 const signal = this . _pc . localDescription || offer
619600 this . _debug ( 'signal' )
620601 this . emit ( 'signal' , {
@@ -625,7 +606,7 @@ class Peer extends stream.Duplex {
625606
626607 const onSuccess = ( ) => {
627608 this . _debug ( 'createOffer success' )
628- if ( this . destroyed ) return
609+ if ( this . _destroyed ) return
629610 if ( this . trickle || this . _iceComplete ) sendOffer ( )
630611 else this . once ( '_iceComplete' , sendOffer ) // wait for candidates
631612 }
@@ -655,16 +636,16 @@ class Peer extends stream.Duplex {
655636 }
656637
657638 _createAnswer ( ) {
658- if ( this . destroyed ) return
639+ if ( this . _destroyed ) return
659640
660641 this . _pc . createAnswer ( this . answerOptions )
661642 . then ( answer => {
662- if ( this . destroyed ) return
643+ if ( this . _destroyed ) return
663644 if ( ! this . trickle && ! this . allowHalfTrickle ) answer . sdp = filterTrickle ( answer . sdp )
664645 answer . sdp = this . sdpTransform ( answer . sdp )
665646
666647 const sendAnswer = ( ) => {
667- if ( this . destroyed ) return
648+ if ( this . _destroyed ) return
668649 const signal = this . _pc . localDescription || answer
669650 this . _debug ( 'signal' )
670651 this . emit ( 'signal' , {
@@ -675,7 +656,7 @@ class Peer extends stream.Duplex {
675656 }
676657
677658 const onSuccess = ( ) => {
678- if ( this . destroyed ) return
659+ if ( this . _destroyed ) return
679660 if ( this . trickle || this . _iceComplete ) sendAnswer ( )
680661 else this . once ( '_iceComplete' , sendAnswer )
681662 }
@@ -694,14 +675,14 @@ class Peer extends stream.Duplex {
694675 }
695676
696677 _onConnectionStateChange ( ) {
697- if ( this . destroyed ) return
678+ if ( this . _destroyed ) return
698679 if ( this . _pc . connectionState === 'failed' ) {
699680 this . destroy ( errCode ( new Error ( 'Connection failed.' ) , 'ERR_CONNECTION_FAILURE' ) )
700681 }
701682 }
702683
703684 _onIceStateChange ( ) {
704- if ( this . destroyed ) return
685+ if ( this . _destroyed ) return
705686 const iceConnectionState = this . _pc . iceConnectionState
706687 const iceGatheringState = this . _pc . iceGatheringState
707688
@@ -750,7 +731,7 @@ class Peer extends stream.Duplex {
750731 } else if ( this . _pc . getStats . length > 0 ) {
751732 this . _pc . getStats ( res => {
752733 // 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
754735
755736 const reports = [ ]
756737 res . result ( ) . forEach ( result => {
@@ -781,10 +762,10 @@ class Peer extends stream.Duplex {
781762
782763 // HACK: We can't rely on order here, for details see https://github.com/js-platform/node-webrtc/issues/339
783764 const findCandidatePair = ( ) => {
784- if ( this . destroyed ) return
765+ if ( this . _destroyed ) return
785766
786767 this . getStats ( ( err , items ) => {
787- if ( this . destroyed ) return
768+ if ( this . _destroyed ) return
788769
789770 // Treat getStats error as non-fatal. It's not essential.
790771 if ( err ) items = [ ]
@@ -921,7 +902,7 @@ class Peer extends stream.Duplex {
921902 }
922903
923904 _onSignalingStateChange ( ) {
924- if ( this . destroyed ) return
905+ if ( this . _destroyed ) return
925906
926907 if ( this . _pc . signalingState === 'stable' ) {
927908 this . _isNegotiating = false
@@ -949,7 +930,7 @@ class Peer extends stream.Duplex {
949930 }
950931
951932 _onIceCandidate ( event ) {
952- if ( this . destroyed ) return
933+ if ( this . _destroyed ) return
953934 if ( event . candidate && this . trickle ) {
954935 this . emit ( 'signal' , {
955936 type : 'candidate' ,
@@ -970,35 +951,35 @@ class Peer extends stream.Duplex {
970951 }
971952
972953 _onChannelMessage ( event ) {
973- if ( this . destroyed ) return
954+ if ( this . _destroyed ) return
974955 let data = event . data
975956 if ( data instanceof ArrayBuffer ) data = Buffer . from ( data )
976957 this . push ( data )
977958 }
978959
979960 _onChannelBufferedAmountLow ( ) {
980- if ( this . destroyed || ! this . _cb ) return
961+ if ( this . _destroyed || ! this . _cb ) return
981962 this . _debug ( 'ending backpressure: bufferedAmount %d' , this . _channel . bufferedAmount )
982963 const cb = this . _cb
983964 this . _cb = null
984965 cb ( null )
985966 }
986967
987968 _onChannelOpen ( ) {
988- if ( this . _connected || this . destroyed ) return
969+ if ( this . _connected || this . _destroyed ) return
989970 this . _debug ( 'on channel open' )
990971 this . _channelReady = true
991972 this . _maybeReady ( )
992973 }
993974
994975 _onChannelClose ( ) {
995- if ( this . destroyed ) return
976+ if ( this . _destroyed ) return
996977 this . _debug ( 'on channel close' )
997978 this . destroy ( )
998979 }
999980
1000981 _onTrack ( event ) {
1001- if ( this . destroyed ) return
982+ if ( this . _destroyed ) return
1002983
1003984 event . streams . forEach ( eventStream => {
1004985 this . _debug ( 'on track' )
0 commit comments