Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/test.torrent
/test-2.torrent
/node_modules/
/ref
.project
app.js
8 changes: 5 additions & 3 deletions lib/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,11 @@ Peer.prototype.sendMessage = function(message) {
Peer.prototype.sendExtendedMessage = function(type, data) {

LOGGER.debug('Peer [%s] sending extended message of type %j', this.getIdentifier(), type);

var code = Message.EXTENDED_HANDSHAKE === type
? 0
: this._extensionData && this._extensionData.m[type];
: this._extensionData.m !== undefined
? this._extensionData && this._extensionData.m[type]
: undefined
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR! This project isn't really getting any love so nice to have contributions :).

This change could fail if this._extensionData is undefined - something more like this._extensionData && this._extensionData.m && this._extensionData.m[type] might work. Though if m can be undefined it will also be an issue in Peer.prototype.supportsExtension. It could be worth add a new function Peer.prototype.getExtension which returns they extension value for a key and encapsulates the undefined checking.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok, Im new in contributions. Im learning and liked for this repo. I love .torrents!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do when this._extensionData.m is undefined?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just set code to undefined. It'd be best to break this out so it's more readable. So the code could be -

var code = null;
if (Message.EXTENDED_HANDSHAKE === type) {
  code = 0;
} else if (this._extensionData && this._extensionData.m) {
  code = this._extensionData.m[type];
}


if (code !== undefined) {

Expand Down Expand Up @@ -480,7 +481,7 @@ function processData(self) {
break;

case Message.PIECE:
LOGGER.debug('Peer [%s] sent PIECE', self.getIdentifier());
LOGGER.debug('Peer [%s] sent PIECE', self.getIdentifier());

var index = message.payload.readInt32BE(0, true);
var begin = message.payload.readInt32BE(4, true);
Expand Down Expand Up @@ -515,6 +516,7 @@ function processData(self) {

case Message.EXTENDED:
LOGGER.debug('Received EXTENDED from ' + Peer.getIdentifier(self));
console.log(message.payload)

var extendedCode = message.payload[0]
, data = message.payload.slice(1)
Expand Down
6 changes: 3 additions & 3 deletions lib/torrent/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function Torrent(clientId, clientPort, downloadPath, dataUrl, extensions) {
util.inherits(Torrent, EventEmitter);

Torrent.prototype.start = function() {
LOGGER.debug('Starting torrent.');
LOGGER.debug('Starting torrent.');
var callback = this.addPeer.bind(this);
// TODO: treat as tracker
DHT.advertise(this.infoHash, callback);
Expand Down Expand Up @@ -254,9 +254,9 @@ Torrent.prototype._initialise = function() {
};

Torrent.prototype._pieceComplete = function(piece) {

LOGGER.debug('Piece complete, piece index = ' + piece.index);
this.stats.downloaded += piece.length;

this.stats.downloaded += piece.length;
this.emit(Torrent.PROGRESS, this.stats.downloaded / this.size);

if (this.isComplete()) {
Expand Down
240 changes: 240 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "node-torrent",
"description": "Bittorrent client for node.js.",
"version": "0.2.2",
"homepage" : "http://github.com/superafroman/node-torrent",
"homepage": "http://github.com/superafroman/node-torrent",
"author": "Max Stewart <max.stewart@superafroman.com>",
"main": "lib/client.js",
"repository" : {
"type" : "git",
"url" : "http://github.com/superafroman/node-torrent.git"
"repository": {
"type": "git",
"url": "http://github.com/superafroman/node-torrent.git"
},
"dependencies": {
"base32": ">= 0.0.5",
Expand All @@ -18,5 +18,7 @@
"directories": {
"lib": "./lib"
},
"engines": { "node": ">= 0.8.0" }
"engines": {
"node": ">= 0.8.0"
}
}