Comparing version 0.0.2 to 0.0.3
@@ -32,4 +32,2 @@ /*jshint node: true */ | ||
} | ||
} else { | ||
@@ -36,0 +34,0 @@ this.id = BitfieldMessage.Id; |
@@ -24,3 +24,3 @@ /*jshint node: true */ | ||
}); | ||
if (bytes) { | ||
if (this._body.length > 0) { | ||
this.id = this._body.readUInt8(0); | ||
@@ -27,0 +27,0 @@ } |
@@ -14,3 +14,3 @@ /*jshint node: true */ | ||
events.EventEmitter.call(this); | ||
this.setMaxListeners(0); | ||
Object.defineProperties(this,{ | ||
@@ -35,2 +35,5 @@ infoHash: { | ||
writable: true | ||
}, | ||
lastMessageReceivedOn: { | ||
writable: true | ||
} | ||
@@ -58,2 +61,3 @@ }); | ||
PeerConnection.prototype.resetDeathTimer = function() { | ||
this.lastMessageReceivedOn = new Date(); | ||
if (!this._deathTimer) { | ||
@@ -71,2 +75,3 @@ clearTimeout(this._deathTimer); | ||
this.resetDeathTimer(); | ||
if (!this.handshakeReceived) { | ||
@@ -81,20 +86,30 @@ var handshake = new Messages.Handshake(data); | ||
var msgLen = data.readInt32BE(0); | ||
if (msgLen === 0) { | ||
// we already reset the Death Timer | ||
return; | ||
} | ||
var cursor = 0; | ||
var cursor = 0, msgLen, part; | ||
var msgs = []; | ||
do { | ||
// 4 for the len integer | ||
var msg = this.fireEvent(data.slice(cursor, cursor + msgLen + 4)); | ||
msgLen = msg.len; | ||
// read the length again | ||
msgLen = data.readInt32BE(cursor); | ||
// slice the data | ||
part = data.slice(cursor, cursor + msgLen + 4); | ||
// fire the event for the part | ||
msgs.push(this.fireEvent(part)); | ||
// advance the cursor | ||
cursor += msgLen + 4; | ||
} while (cursor < data.length); | ||
return msgs; | ||
}; | ||
PeerConnection.prototype.fireEvent = function(part) { | ||
var msg = new Messages.Peer(part); | ||
var msg = new Messages.KeepAlive(part); | ||
if (msg.len === 0) { | ||
this.emit('keep-alive', msg); | ||
return msg; | ||
} | ||
msg = new Messages.Peer(part); | ||
switch (msg.id) { | ||
@@ -123,3 +138,3 @@ case Messages.Choke.Id: | ||
case Messages.Piece.Id: | ||
this.emit('peice',new Messages.Piece(part)); | ||
this.emit('piece',new Messages.Piece(part)); | ||
break; | ||
@@ -132,2 +147,5 @@ case Messages.Cancel.Id: | ||
break; | ||
default: | ||
this.emit('unknown', msg); | ||
break; | ||
} | ||
@@ -134,0 +152,0 @@ return msg; |
{ | ||
"name": "peerface", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "bittorrent peer communication library", | ||
@@ -17,3 +17,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "node ./test/messages.test.js" | ||
}, | ||
@@ -20,0 +20,0 @@ "repository": { |
@@ -5,2 +5,4 @@ /*jshint node: true */ | ||
var events = require('events'); | ||
var crypto = require('crypto'); | ||
@@ -14,21 +16,28 @@ | ||
var infoHash = crypto.randomBytes(20); | ||
var peerId = crypto.randomBytes(10).toString('hex'); | ||
var pstrlen = new Buffer(1); | ||
pstrlen.writeUInt8(19,0); | ||
var handshakeMessage = Buffer.concat([ | ||
// pstrlen | ||
pstrlen, | ||
// pstr | ||
new Buffer('BitTorrent protocol'), | ||
// reserved | ||
new Buffer([0,0,0,0,0,0,0,0]), | ||
// info hash | ||
infoHash, | ||
// peer_id | ||
new Buffer(peerId) | ||
]); | ||
var messages = { | ||
keepAlive: new Buffer([0,0,0,0]), | ||
choke: new Buffer([0,0,0,1,0]), | ||
unchoke: new Buffer([0,0,0,1,1]), | ||
interested: new Buffer([0,0,0,1,2]) | ||
}; | ||
test('handshake dto test', function(t){ | ||
var infoHash, peerId; | ||
peerId = crypto.randomBytes(10).toString('hex'); | ||
var pstrlen = new Buffer(1); | ||
pstrlen.writeUInt8(19,0); | ||
var bytes = Buffer.concat([ | ||
// pstrlen | ||
pstrlen, | ||
// pstr | ||
new Buffer('BitTorrent protocol'), | ||
// reserved | ||
new Buffer([0,0,0,0,0,0,0,0]), | ||
// info hash | ||
(infoHash = crypto.randomBytes(20)), | ||
// peer_id | ||
new Buffer(peerId) | ||
]); | ||
console.log(new Buffer(peerId).length, bytes.length, 19+49); | ||
var bytes = handshakeMessage; | ||
var msg = new lib.Messages.Handshake(bytes); | ||
@@ -46,6 +55,7 @@ | ||
test('keep-alive dto test', function(t) { | ||
var bytes = new Buffer([0,0,0,0]); | ||
var bytes = messages.keepAlive; | ||
var msg = new lib.Messages.KeepAlive(bytes); | ||
t.equal(msg.len, 0, 'message len should be 0'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -56,3 +66,3 @@ t.end(); | ||
test('choke dto test', function(t) { | ||
var bytes = new Buffer([0,0,0,1,0]); | ||
var bytes = messages.choke; | ||
var msg = new lib.Messages.Choke(bytes); | ||
@@ -62,2 +72,3 @@ | ||
t.equal(msg.id, 0, 'message id should be 0'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -68,3 +79,3 @@ t.end(); | ||
test('unchoke dto test', function(t) { | ||
var bytes = new Buffer([0,0,0,1,1]); | ||
var bytes = messages.unchoke; | ||
var msg = new lib.Messages.Unchoke(bytes); | ||
@@ -74,2 +85,3 @@ | ||
t.equal(msg.id, 1, 'message id should be 1'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -80,3 +92,3 @@ t.end(); | ||
test('interested dto test', function(t) { | ||
var bytes = new Buffer([0,0,0,1,2]); | ||
var bytes = messages.interested; | ||
var msg = new lib.Messages.Interested(bytes); | ||
@@ -86,2 +98,3 @@ | ||
t.equal(msg.id, 2, 'message id should be 2'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -92,3 +105,3 @@ t.end(); | ||
test('not-interested dto test', function(t) { | ||
var bytes = new Buffer([0,0,0,1,3]); | ||
var bytes = messages.notInterested = new Buffer([0,0,0,1,3]); | ||
var msg = new lib.Messages.NotInterested(bytes); | ||
@@ -98,2 +111,3 @@ | ||
t.equal(msg.id, 3, 'message id should be 3'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -104,3 +118,3 @@ t.end(); | ||
test('have dto test', function(t) { | ||
var bytes = new Buffer([0,0,0,5,4,0,0,0,1]); | ||
var bytes = messages.have = new Buffer([0,0,0,5,4,0,0,0,1]); | ||
var msg = new lib.Messages.Have(bytes); | ||
@@ -111,2 +125,3 @@ | ||
t.equal(msg.pieceIndex, 1, 'message piece index should be 1'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -125,3 +140,3 @@ t.end(); | ||
var bytes = Buffer.concat([ | ||
var bytes = messages.bitfield = Buffer.concat([ | ||
len, | ||
@@ -137,2 +152,3 @@ new Buffer([5]), | ||
t.deepEquals(msg.bitfield, bitfield, 'bitfields should match'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -143,3 +159,3 @@ t.end(); | ||
test('request dto test', function(t) { | ||
var bytes = new Buffer([ | ||
var bytes = messages.request = new Buffer([ | ||
0,0,0,13, | ||
@@ -159,2 +175,3 @@ 6, | ||
t.equal(msg.length, 5, 'message index should be 5'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -167,3 +184,3 @@ t.end(); | ||
var bytes = Buffer.concat([ | ||
var bytes = messages.piece = Buffer.concat([ | ||
new Buffer([ | ||
@@ -185,2 +202,3 @@ 0,0,0,13, | ||
t.deepEqual(msg.block,block, 'message block should match'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -191,3 +209,3 @@ t.end(); | ||
test('cancel dto test', function(t){ | ||
var bytes = new Buffer([ | ||
var bytes = messages.cancel = new Buffer([ | ||
0,0,0,13, | ||
@@ -207,2 +225,3 @@ 8, | ||
t.equal(msg.length, 5, 'message index should be 5'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
@@ -213,3 +232,3 @@ t.end(); | ||
test('port dto test', function(t){ | ||
var bytes = new Buffer([ | ||
var bytes = messages.port = new Buffer([ | ||
0,0,0,3, | ||
@@ -225,4 +244,216 @@ 9, | ||
t.equal(msg.listenPort, 10, 'message listenPort should be 10'); | ||
t.deepEqual(msg.toBuffer(),bytes, "buffers should match"); | ||
t.end(); | ||
}); | ||
test('route data test', function(t) { | ||
var mockSocket = new events.EventEmitter(); | ||
var peer = new lib.PeerConnection(mockSocket); | ||
t.test("handshake", function(t) { | ||
peer.once('handshake', function(e) { | ||
t.deepEqual(handshakeMessage, e.toBuffer(), "handshake should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(handshakeMessage); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("keep alive", function(t) { | ||
peer.once('keep-alive', function(e) { | ||
t.deepEqual(messages.keepAlive, e.toBuffer(), "keep-alive should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.keepAlive); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("choke", function(t) { | ||
peer.once('choke', function(e) { | ||
t.deepEqual(messages.choke, e.toBuffer(), "choke should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.choke); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("unchoke", function(t) { | ||
peer.once('unchoke', function(e) { | ||
t.deepEqual(messages.unchoke, e.toBuffer(), "unchoke should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.unchoke); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("interested", function(t) { | ||
peer.once('interested', function(e) { | ||
t.deepEqual(messages.interested, e.toBuffer(), "interested should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.interested); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("not-interested", function(t) { | ||
peer.once('not-interested', function(e) { | ||
t.deepEqual(messages.notInterested, e.toBuffer(), "not-interested should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.notInterested); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("have", function(t) { | ||
peer.once('have', function(e) { | ||
t.deepEqual(messages.have, e.toBuffer(), "have should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.have); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("bitfield", function(t) { | ||
peer.once('bitfield', function(e) { | ||
t.deepEqual(messages.bitfield, e.toBuffer(), "bitfield should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.bitfield); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("request", function(t) { | ||
peer.once('request', function(e) { | ||
t.deepEqual(messages.request, e.toBuffer(), "request should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.request); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("piece", function(t) { | ||
peer.once('piece', function(e) { | ||
t.deepEqual(messages.piece, e.toBuffer(), "piece should be the same"); | ||
}); | ||
t.plan(2); | ||
t.equal(peer.routeData(messages.piece).length, 1, "should have routed 1 message"); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("cancel", function(t) { | ||
peer.once('cancel', function(e) { | ||
t.deepEqual(messages.cancel, e.toBuffer(), "cancel should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.cancel); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("port", function(t) { | ||
peer.once('port', function(e) { | ||
t.deepEqual(messages.port, e.toBuffer(), "port should be the same"); | ||
}); | ||
t.plan(1); | ||
peer.routeData(messages.port); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.test("all the messages", function(t) { | ||
peer.on('keep-alive', function(e) { | ||
t.deepEqual(messages.keepAlive, e.toBuffer(), "keep-alive should be the same"); | ||
}); | ||
peer.on('choke', function(e) { | ||
t.deepEqual(messages.choke, e.toBuffer(), "choke should be the same"); | ||
}); | ||
peer.on('unchoke', function(e) { | ||
t.deepEqual(messages.unchoke, e.toBuffer(), "unchoke should be the same"); | ||
}); | ||
peer.on('interested', function(e) { | ||
t.deepEqual(messages.interested, e.toBuffer(), "interested should be the same"); | ||
}); | ||
peer.on('not-interested', function(e) { | ||
t.deepEqual(messages.notInterested, e.toBuffer(), "not-interested should be the same"); | ||
}); | ||
peer.on('have', function(e) { | ||
t.deepEqual(messages.have, e.toBuffer(), "have should be the same"); | ||
}); | ||
peer.on('bitfield', function(e) { | ||
t.deepEqual(messages.bitfield, e.toBuffer(), "bitfield should be the same"); | ||
}); | ||
peer.on('request', function(e) { | ||
t.deepEqual(messages.request, e.toBuffer(), "request should be the same"); | ||
}); | ||
peer.on('piece', function(e) { | ||
t.deepEqual(messages.piece, e.toBuffer(), "piece should be the same"); | ||
}); | ||
peer.on('cancel', function(e) { | ||
t.deepEqual(messages.cancel, e.toBuffer(), "cancel should be the same"); | ||
}); | ||
peer.on('port', function(e) { | ||
t.deepEqual(messages.port, e.toBuffer(), "port should be the same"); | ||
}); | ||
var keys = Object.keys(messages); | ||
var bytes = Buffer.concat(keys.map(function(key){ return messages[key]; })); | ||
t.plan(keys.length+1); | ||
t.equal(peer.routeData(bytes).length, keys.length, "should have " + keys.length + " messages"); | ||
// clear the deathTimer since this seems to hang tape | ||
clearTimeout(peer._deathTimer); | ||
}); | ||
t.end(); | ||
}); |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
33526
1122
1