Skip to content

Commit 39b09a4

Browse files
pfrazeemafintosh
authored andcommitted
Add debug output (hypercore-protocol#6)
* add debugMode option * rewrite debug output to use the debug module
1 parent c51a85c commit 39b09a4

File tree

3 files changed

+98
-34
lines changed

3 files changed

+98
-34
lines changed

index.js

+70-9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var equals = require('buffer-equals')
1111
var varint = require('varint')
1212
var xtend = require('xtend')
1313
var pe = require('passthrough-encoding')
14+
var debug = require('debug')('hypercore-protocol')
1415
var messages = require('./messages')
1516

1617
var KEEP_ALIVE = Buffer([0])
@@ -118,6 +119,7 @@ function use (extensions) {
118119
}
119120

120121
Channel.prototype._onhandshake = function (handshake) {
122+
niceDebug('handshaked', { channel: this })
121123
this.protocol._onhandshake(handshake)
122124
}
123125

@@ -194,6 +196,7 @@ function use (extensions) {
194196

195197
var keys = Object.keys(self.channels)
196198
for (var i = 0; i < keys.length; i++) {
199+
niceDebug('closed', { channel: self.channels[keys[i]] })
197200
self._close(self.channels[keys[i]])
198201
}
199202
}
@@ -256,7 +259,10 @@ function use (extensions) {
256259
}
257260

258261
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+
}
260266
if (!opts) opts = {}
261267

262268
var d = opts.discoveryKey || discoveryKey(key)
@@ -277,6 +283,7 @@ function use (extensions) {
277283
ch.local = this._local.indexOf(null)
278284
if (ch.local === -1) ch.local = this._local.push(null) - 1
279285
this._local[ch.local] = ch
286+
niceDebug('open()', { channel: ch })
280287

281288
var open = messages.Open.encode({
282289
feed: ch.discoveryKey,
@@ -302,7 +309,11 @@ function use (extensions) {
302309
}
303310

304311
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 })
306317

307318
var enc = types[type]
308319
var len = enc ? enc.encodingLength(message) : 0
@@ -335,10 +346,12 @@ function use (extensions) {
335346
}
336347

337348
Protocol.prototype._pause = function () {
349+
debug('pause()')
338350
if (!this._paused++) this._decode.pause()
339351
}
340352

341353
Protocol.prototype._resume = function () {
354+
debug('resume()')
342355
if (!--this._paused) this._decode.resume()
343356
}
344357

@@ -362,7 +375,11 @@ function use (extensions) {
362375
Protocol.prototype._parse = function (data) {
363376
this._remoteKeepAlive = 0
364377

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+
}
366383

367384
var remote = varint.decode(data, 0)
368385
var offset = varint.decode.bytes
@@ -410,7 +427,11 @@ function use (extensions) {
410427
this.channels[keyHex] = ch
411428
}
412429

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 })
414435

415436
ch.remote = remote
416437
ch._remoteNonce = open.nonce
@@ -424,19 +445,34 @@ function use (extensions) {
424445
var channel = this._remote[remote]
425446

426447
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+
}
428452
channel.buffer.push(data)
429453
return
430454
}
431455

432456
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+
}
434461

435462
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+
}
437467

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+
}
440476

441477
var enc = types[type]
442478

@@ -446,11 +482,13 @@ function use (extensions) {
446482
return this.destroy(err)
447483
}
448484

485+
niceDebug('recv()', { channel: channel, type: type, message: message })
449486
channel.emit(eventNames[type], message)
450487
}
451488

452489
Protocol.prototype._onclose = function (remote) {
453490
var channel = this._remote[remote]
491+
niceDebug('closed by remote', { channel: channel })
454492

455493
this._remote[remote] = null
456494
channel.remote = -1
@@ -535,5 +573,28 @@ function use (extensions) {
535573

536574
function noop () {}
537575

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+
538599
return Protocol
539600
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"brfs": "^1.4.3",
88
"buffer-equals": "^1.0.3",
99
"create-hmac": "^1.1.4",
10+
"debug": "^2.3.2",
1011
"duplexify": "^3.4.3",
1112
"increment-buffer": "^1.0.0",
1213
"inherits": "^2.0.1",

test.js

+27-25
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ var lpm = require('length-prefixed-message')
55
var key = Buffer('12345678123456781234567812345678')
66
var otherKey = Buffer('02345678123456781234567812345678')
77

8+
var DEBUG_MODE = process.env.DEBUG_MODE ? 'log' : false
9+
810
tape('parse discovery key', function (t) {
911
t.plan(1)
1012

11-
var stream = protocol()
13+
var stream = protocol({ debugMode: DEBUG_MODE })
1214
var channel = stream.open(key)
1315
lpm.read(stream, function (buf) {
1416
var parsed = protocol.parseDiscoveryKey(buf)
@@ -19,8 +21,8 @@ tape('parse discovery key', function (t) {
1921
tape('open channel', function (t) {
2022
t.plan(3)
2123

22-
var stream1 = protocol()
23-
var stream2 = protocol()
24+
var stream1 = protocol({ debugMode: DEBUG_MODE })
25+
var stream2 = protocol({ debugMode: DEBUG_MODE })
2426

2527
var channel1 = stream1.open(key)
2628
var channel2 = stream2.open(key)
@@ -45,8 +47,8 @@ tape('open channel', function (t) {
4547
tape('async open', function (t) {
4648
t.plan(3)
4749

48-
var stream1 = protocol()
49-
var stream2 = protocol(function () {
50+
var stream1 = protocol({ debugMode: DEBUG_MODE })
51+
var stream2 = protocol({ debugMode: DEBUG_MODE }, function () {
5052
setTimeout(function () {
5153
var channel2 = stream2.open(key)
5254
channel2.once('request', function (message) {
@@ -73,8 +75,8 @@ tape('async open', function (t) {
7375
tape('empty messages work', function (t) {
7476
t.plan(2)
7577

76-
var stream1 = protocol()
77-
var stream2 = protocol()
78+
var stream1 = protocol({ debugMode: DEBUG_MODE })
79+
var stream2 = protocol({ debugMode: DEBUG_MODE })
7880

7981
var channel1 = stream1.open(key)
8082
var channel2 = stream2.open(key)
@@ -94,8 +96,8 @@ tape('empty messages work', function (t) {
9496
})
9597

9698
tape('is encrypted', function (t) {
97-
var stream1 = protocol()
98-
var stream2 = protocol()
99+
var stream1 = protocol({ debugMode: DEBUG_MODE })
100+
var stream2 = protocol({ debugMode: DEBUG_MODE })
99101

100102
var channel1 = stream1.open(key)
101103
var channel2 = stream2.open(key)
@@ -116,8 +118,8 @@ tape('is encrypted', function (t) {
116118
})
117119

118120
tape('can disable encryption', function (t) {
119-
var stream1 = protocol({encrypt: false})
120-
var stream2 = protocol({encrypt: false})
121+
var stream1 = protocol({debugMode: DEBUG_MODE, encrypt: false})
122+
var stream2 = protocol({debugMode: DEBUG_MODE, encrypt: false})
121123

122124
var foundHello = false
123125
var channel1 = stream1.open(key)
@@ -142,8 +144,8 @@ tape('can disable encryption', function (t) {
142144
tape('end channel', function (t) {
143145
t.plan(3)
144146

145-
var stream1 = protocol()
146-
var stream2 = protocol()
147+
var stream1 = protocol({ debugMode: DEBUG_MODE })
148+
var stream2 = protocol({ debugMode: DEBUG_MODE })
147149

148150
var c1 = stream1.open(key)
149151
var c2 = stream2.open(key)
@@ -171,8 +173,8 @@ tape('end channel', function (t) {
171173
tape('destroy ends all channels', function (t) {
172174
t.plan(3)
173175

174-
var stream1 = protocol()
175-
var stream2 = protocol()
176+
var stream1 = protocol({ debugMode: DEBUG_MODE })
177+
var stream2 = protocol({ debugMode: DEBUG_MODE })
176178

177179
var c1 = stream1.open(key)
178180
var other = stream1.open(otherKey)
@@ -198,8 +200,8 @@ tape('destroy ends all channels', function (t) {
198200
})
199201

200202
tape('times out', function (t) {
201-
var p1 = protocol()
202-
var p2 = protocol()
203+
var p1 = protocol({ debugMode: DEBUG_MODE })
204+
var p2 = protocol({ debugMode: DEBUG_MODE })
203205

204206
// dummy timeout to keep event loop running
205207
var timeout = setTimeout(function () {}, 100000)
@@ -216,8 +218,8 @@ tape('times out', function (t) {
216218
})
217219

218220
tape('timeout is implicit keep alive', function (t) {
219-
var p1 = protocol()
220-
var p2 = protocol()
221+
var p1 = protocol({ debugMode: DEBUG_MODE })
222+
var p2 = protocol({ debugMode: DEBUG_MODE })
221223

222224
// dummy timeout to keep event loop running
223225
setTimeout(function () {
@@ -239,8 +241,8 @@ tape('timeout is implicit keep alive', function (t) {
239241
})
240242

241243
tape('different timeouts', function (t) {
242-
var p1 = protocol()
243-
var p2 = protocol()
244+
var p1 = protocol({ debugMode: DEBUG_MODE })
245+
var p2 = protocol({ debugMode: DEBUG_MODE })
244246

245247
// dummy timeout to keep event loop running
246248
var timeout = setTimeout(function () {}, 100000)
@@ -299,8 +301,8 @@ tape('different extensions', function (t) {
299301

300302
var protocol1 = protocol.use({test: 1, bar: 1})
301303
var protocol2 = protocol.use({foo: 1, test: 1})
302-
var p1 = protocol1()
303-
var p2 = protocol2()
304+
var p1 = protocol1({ debugMode: DEBUG_MODE })
305+
var p2 = protocol2({ debugMode: DEBUG_MODE })
304306

305307
var ch1 = p1.open(key)
306308
var ch2 = p2.open(key)
@@ -337,8 +339,8 @@ tape('ignore unsupported message', function (t) {
337339
t.plan(6)
338340

339341
var protocol2 = protocol.use({test: 1, bar: 1})
340-
var p1 = protocol()
341-
var p2 = protocol2()
342+
var p1 = protocol({ debugMode: DEBUG_MODE })
343+
var p2 = protocol2({ debugMode: DEBUG_MODE })
342344

343345
var ch1 = p1.open(key)
344346
var ch2 = p2.open(key)

0 commit comments

Comments
 (0)