@@ -11,6 +11,7 @@ var equals = require('buffer-equals')
11
11
var varint = require ( 'varint' )
12
12
var xtend = require ( 'xtend' )
13
13
var pe = require ( 'passthrough-encoding' )
14
+ var debug = require ( 'debug' ) ( 'hypercore-protocol' )
14
15
var messages = require ( './messages' )
15
16
16
17
var KEEP_ALIVE = Buffer ( [ 0 ] )
@@ -118,6 +119,7 @@ function use (extensions) {
118
119
}
119
120
120
121
Channel . prototype . _onhandshake = function ( handshake ) {
122
+ niceDebug ( 'handshaked' , { channel : this } )
121
123
this . protocol . _onhandshake ( handshake )
122
124
}
123
125
@@ -194,6 +196,7 @@ function use (extensions) {
194
196
195
197
var keys = Object . keys ( self . channels )
196
198
for ( var i = 0 ; i < keys . length ; i ++ ) {
199
+ niceDebug ( 'closed' , { channel : self . channels [ keys [ i ] ] } )
197
200
self . _close ( self . channels [ keys [ i ] ] )
198
201
}
199
202
}
@@ -256,7 +259,10 @@ function use (extensions) {
256
259
}
257
260
258
261
Protocol . prototype . open = function ( key , opts ) {
259
- if ( this . destroyed ) return null // already finalized
262
+ if ( this . destroyed ) {
263
+ niceDebug ( 'Open() called after finalized, aborting' , { key : key } )
264
+ return null // already finalized
265
+ }
260
266
if ( ! opts ) opts = { }
261
267
262
268
var d = opts . discoveryKey || discoveryKey ( key )
@@ -277,6 +283,7 @@ function use (extensions) {
277
283
ch . local = this . _local . indexOf ( null )
278
284
if ( ch . local === - 1 ) ch . local = this . _local . push ( null ) - 1
279
285
this . _local [ ch . local ] = ch
286
+ niceDebug ( 'open()' , { channel : ch } )
280
287
281
288
var open = messages . Open . encode ( {
282
289
feed : ch . discoveryKey ,
@@ -302,7 +309,11 @@ function use (extensions) {
302
309
}
303
310
304
311
Protocol . prototype . _send = function ( channel , type , message ) {
305
- if ( channel . closed ) return false
312
+ if ( channel . closed ) {
313
+ niceDebug ( 'Send called after close, discarding' , { channel : channel , type : type , message : message } )
314
+ return false
315
+ }
316
+ niceDebug ( 'send()' , { channel : channel , type : type , message : message } )
306
317
307
318
var enc = types [ type ]
308
319
var len = enc ? enc . encodingLength ( message ) : 0
@@ -335,10 +346,12 @@ function use (extensions) {
335
346
}
336
347
337
348
Protocol . prototype . _pause = function ( ) {
349
+ debug ( 'pause()' )
338
350
if ( ! this . _paused ++ ) this . _decode . pause ( )
339
351
}
340
352
341
353
Protocol . prototype . _resume = function ( ) {
354
+ debug ( 'resume()' )
342
355
if ( ! -- this . _paused ) this . _decode . resume ( )
343
356
}
344
357
@@ -362,7 +375,11 @@ function use (extensions) {
362
375
Protocol . prototype . _parse = function ( data ) {
363
376
this . _remoteKeepAlive = 0
364
377
365
- if ( ! data . length || this . destroyed ) return
378
+ if ( ! data . length ) return
379
+ if ( this . destroyed ) {
380
+ debug ( 'Received message after destroy(), discarding' )
381
+ return
382
+ }
366
383
367
384
var remote = varint . decode ( data , 0 )
368
385
var offset = varint . decode . bytes
@@ -410,7 +427,11 @@ function use (extensions) {
410
427
this . channels [ keyHex ] = ch
411
428
}
412
429
413
- if ( ch . remote > - 1 ) return this . destroy ( new Error ( 'Double open for same channel' ) )
430
+ if ( ch . remote > - 1 ) {
431
+ debug ( 'Double open error, closing channel' , { channel : ch , message : open } )
432
+ return this . destroy ( new Error ( 'Double open for same channel' ) )
433
+ }
434
+ niceDebug ( 'opened' , { channel : ch , message : open } )
414
435
415
436
ch . remote = remote
416
437
ch . _remoteNonce = open . nonce
@@ -424,19 +445,34 @@ function use (extensions) {
424
445
var channel = this . _remote [ remote ]
425
446
426
447
if ( ! channel . key || channel . buffer . length || ! channel . _ready ) {
427
- if ( channel . buffer . length === 16 ) return this . destroy ( new Error ( 'Buffer overflow' ) )
448
+ if ( channel . buffer . length === 16 ) {
449
+ niceDebug ( 'Buffer overflow in received message, closing channel' , { channel : channel } )
450
+ return this . destroy ( new Error ( 'Buffer overflow' ) )
451
+ }
428
452
channel . buffer . push ( data )
429
453
return
430
454
}
431
455
432
456
var box = this . _decrypt ( channel , data . slice ( offset ) )
433
- if ( ! box || ! box . length ) return this . destroy ( new Error ( 'Invalid message' ) )
457
+ if ( ! box || ! box . length ) {
458
+ niceDebug ( 'Received invalid message, closing channel' , { channel : channel } )
459
+ return this . destroy ( new Error ( 'Invalid message' ) )
460
+ }
434
461
435
462
var type = this . _parseType ( box [ 0 ] )
436
- if ( type < 0 ) return
463
+ if ( type < 0 ) {
464
+ niceDebug ( 'Received invalid message type, discarding' , { channel : channel , type : type } )
465
+ return
466
+ }
437
467
438
- if ( type && ! this . remoteId ) return this . destroy ( new Error ( 'Did not receive handshake' ) )
439
- if ( type >= types . length ) return
468
+ if ( type && ! this . remoteId ) {
469
+ niceDebug ( 'Received message without handshake, destroying channel' , { channel : channel } )
470
+ return this . destroy ( new Error ( 'Did not receive handshake' ) )
471
+ }
472
+ if ( type >= types . length ) {
473
+ niceDebug ( 'Received invalid message type, discarding' , { channel : channel , type : type } )
474
+ return
475
+ }
440
476
441
477
var enc = types [ type ]
442
478
@@ -446,11 +482,13 @@ function use (extensions) {
446
482
return this . destroy ( err )
447
483
}
448
484
485
+ niceDebug ( 'recv()' , { channel : channel , type : type , message : message } )
449
486
channel . emit ( eventNames [ type ] , message )
450
487
}
451
488
452
489
Protocol . prototype . _onclose = function ( remote ) {
453
490
var channel = this . _remote [ remote ]
491
+ niceDebug ( 'closed by remote' , { channel : channel } )
454
492
455
493
this . _remote [ remote ] = null
456
494
channel . remote = - 1
@@ -535,5 +573,28 @@ function use (extensions) {
535
573
536
574
function noop ( ) { }
537
575
576
+ function niceDebug ( label , data ) {
577
+ if ( ! debug . enabled ) return
578
+ if ( data ) {
579
+ var parts = [ ]
580
+ if ( data . channel ) parts . push ( 'chan=' + shortHex ( data . channel . discoveryKey ) )
581
+ parts . push ( label )
582
+ if ( 'type' in data ) {
583
+ var type = ( types [ data . type ] && types [ data . type ] . name ) ? types [ data . type ] . name : data . type
584
+ if ( type === 6 ) type = 'Pause'
585
+ if ( type === 7 ) type = 'Resume'
586
+ parts . push ( 'type=' + type )
587
+ }
588
+ if ( data . key ) parts . push ( 'key=' + shortHex ( data . key ) )
589
+ debug ( parts . join ( ' ' ) )
590
+ } else {
591
+ debug ( label )
592
+ }
593
+ }
594
+ function shortHex ( buf ) {
595
+ buf = buf . toString ( 'hex' )
596
+ return buf . slice ( 0 , 6 ) + '..' + buf . slice ( - 2 )
597
+ }
598
+
538
599
return Protocol
539
600
}
0 commit comments