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,14 +25,16 @@ 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
3232 } , opts )
3333
3434 super ( opts )
3535
36+ this . __objectMode = ! ! opts . objectMode // streamx is objectMode by default, so implement readable's fuctionality
37+
3638 this . _id = randombytes ( 4 ) . toString ( 'hex' ) . slice ( 0 , 7 )
3739 this . _debug ( 'new peer %o' , opts )
3840
@@ -52,8 +54,8 @@ class Peer extends stream.Duplex {
5254 this . allowHalfTrickle = opts . allowHalfTrickle !== undefined ? opts . allowHalfTrickle : false
5355 this . iceCompleteTimeout = opts . iceCompleteTimeout || ICECOMPLETE_TIMEOUT
5456
55- this . destroyed = false
56- this . destroying = false
57+ this . _destroying = false
58+
5759 this . _connected = false
5860
5961 this . remoteAddress = undefined
@@ -180,7 +182,7 @@ class Peer extends stream.Duplex {
180182 }
181183
182184 signal ( data ) {
183- if ( this . destroying ) return
185+ if ( this . _destroying ) return
184186 if ( this . destroyed ) throw errCode ( new Error ( 'cannot signal after peer is destroyed' ) , 'ERR_DESTROYED' )
185187 if ( typeof data === 'string' ) {
186188 try {
@@ -244,7 +246,7 @@ class Peer extends stream.Duplex {
244246 * @param {ArrayBufferView|ArrayBuffer|Buffer|string|Blob } chunk
245247 */
246248 send ( chunk ) {
247- if ( this . destroying ) return
249+ if ( this . _destroying ) return
248250 if ( this . destroyed ) throw errCode ( new Error ( 'cannot send after peer is destroyed' ) , 'ERR_DESTROYED' )
249251 this . _channel . send ( chunk )
250252 }
@@ -255,7 +257,7 @@ class Peer extends stream.Duplex {
255257 * @param {Object } init
256258 */
257259 addTransceiver ( kind , init ) {
258- if ( this . destroying ) return
260+ if ( this . _destroying ) return
259261 if ( this . destroyed ) throw errCode ( new Error ( 'cannot addTransceiver after peer is destroyed' ) , 'ERR_DESTROYED' )
260262 this . _debug ( 'addTransceiver()' )
261263
@@ -279,7 +281,7 @@ class Peer extends stream.Duplex {
279281 * @param {MediaStream } stream
280282 */
281283 addStream ( stream ) {
282- if ( this . destroying ) return
284+ if ( this . _destroying ) return
283285 if ( this . destroyed ) throw errCode ( new Error ( 'cannot addStream after peer is destroyed' ) , 'ERR_DESTROYED' )
284286 this . _debug ( 'addStream()' )
285287
@@ -294,7 +296,7 @@ class Peer extends stream.Duplex {
294296 * @param {MediaStream } stream
295297 */
296298 addTrack ( track , stream ) {
297- if ( this . destroying ) return
299+ if ( this . _destroying ) return
298300 if ( this . destroyed ) throw errCode ( new Error ( 'cannot addTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
299301 this . _debug ( 'addTrack()' )
300302
@@ -319,7 +321,7 @@ class Peer extends stream.Duplex {
319321 * @param {MediaStream } stream
320322 */
321323 replaceTrack ( oldTrack , newTrack , stream ) {
322- if ( this . destroying ) return
324+ if ( this . _destroying ) return
323325 if ( this . destroyed ) throw errCode ( new Error ( 'cannot replaceTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
324326 this . _debug ( 'replaceTrack()' )
325327
@@ -343,7 +345,7 @@ class Peer extends stream.Duplex {
343345 * @param {MediaStream } stream
344346 */
345347 removeTrack ( track , stream ) {
346- if ( this . destroying ) return
348+ if ( this . _destroying ) return
347349 if ( this . destroyed ) throw errCode ( new Error ( 'cannot removeTrack after peer is destroyed' ) , 'ERR_DESTROYED' )
348350 this . _debug ( 'removeSender()' )
349351
@@ -370,7 +372,7 @@ class Peer extends stream.Duplex {
370372 * @param {MediaStream } stream
371373 */
372374 removeStream ( stream ) {
373- if ( this . destroying ) return
375+ if ( this . _destroying ) return
374376 if ( this . destroyed ) throw errCode ( new Error ( 'cannot removeStream after peer is destroyed' ) , 'ERR_DESTROYED' )
375377 this . _debug ( 'removeSenders()' )
376378
@@ -396,7 +398,7 @@ class Peer extends stream.Duplex {
396398 }
397399
398400 negotiate ( ) {
399- if ( this . destroying ) return
401+ if ( this . _destroying ) return
400402 if ( this . destroyed ) throw errCode ( new Error ( 'cannot negotiate after peer is destroyed' ) , 'ERR_DESTROYED' )
401403
402404 if ( this . initiator ) {
@@ -424,29 +426,19 @@ class Peer extends stream.Duplex {
424426 this . _isNegotiating = true
425427 }
426428
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 ( )
432432 }
433433
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
437437
438- this . _debug ( 'destroying (error: %s)' , err && ( err . message || err ) )
438+ if ( ! this . _writableState . ended ) this . end ( )
439439
440440 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
450442
451443 this . _connected = false
452444 this . _pcReady = false
@@ -493,8 +485,6 @@ class Peer extends stream.Duplex {
493485 this . _pc = null
494486 this . _channel = null
495487
496- if ( err ) this . emit ( 'error' , err )
497- this . emit ( 'close' )
498488 cb ( )
499489 } )
500490 }
@@ -548,9 +538,7 @@ class Peer extends stream.Duplex {
548538 } , CHANNEL_CLOSING_TIMEOUT )
549539 }
550540
551- _read ( ) { }
552-
553- _write ( chunk , encoding , cb ) {
541+ _write ( chunk , cb ) {
554542 if ( this . destroyed ) return cb ( errCode ( new Error ( 'cannot write after peer is destroyed' ) , 'ERR_DATA_CHANNEL' ) )
555543
556544 if ( this . _connected ) {
@@ -972,7 +960,7 @@ class Peer extends stream.Duplex {
972960 _onChannelMessage ( event ) {
973961 if ( this . destroyed ) return
974962 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 )
976964 this . push ( data )
977965 }
978966
0 commit comments