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
21 changes: 4 additions & 17 deletions lib/hasher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const crypto = require('crypto');
const Stream = require('stream').Stream;
const parallel = require('async').parallel;
const _ = require('underscore');
const StreamSpeed = require('streamspeed');

const queue = require('./queue');
Expand Down Expand Up @@ -41,18 +40,6 @@ const round = (num, dec) => {
};


/**
* Allows arrays of objects with a `path` property to be
* sorted alphabetically
*
* @param {Object} obj
* @return {string}
*/
const sortByPath = (obj) => {
return obj.path.join();
};


module.exports = class Hasher extends Stream {
/**
* Calculates piece hashes of file list.
Expand Down Expand Up @@ -96,8 +83,8 @@ module.exports = class Hasher extends Stream {
}

const readTasks = (err) => {
// Sort files alphabetically.
this.files = _.sortBy(this.files, sortByPath);
// Create shallow copy of the files array.
this.files = Array.from(this.files);

if (err) return this.emit('error', err);

Expand Down Expand Up @@ -144,9 +131,9 @@ module.exports = class Hasher extends Stream {
this.filetasks.push({
length : task.length,
path : task.path,
// Piece this task will write to.
// Piece this task will read from.
piece,
// Piece buffer offset to start writing at.
// Piece buffer offset to start reading at.
offset,
});

Expand Down
7 changes: 3 additions & 4 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ const checkBuffer = (torrent, field) => {
*/
const checkAnnounce = (torrent) => {
let err;
if (!torrent.announce && !torrent['announce-list']) {
return '`announce` and `announce-list` fields not found';
if (torrent.announce || torrent['announce-list']) {
if ((err = checkBuffer(torrent, 'announce'))) return err;
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
}
if ((err = checkBuffer(torrent, 'announce'))) return err;
if (!util.isURL(torrent.announce)) return '`announce` is not a URL';
return null;
};

Expand Down
11 changes: 1 addition & 10 deletions package-lock.json

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

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"buffers": "~0.1.1",
"memorystream": "~0.3.1",
"ordered-emitter": "^1.0.0",
"streamspeed": "~2.0.0",
"underscore": "^1.10.2"
"streamspeed": "~2.0.0"
},
"devDependencies": {
"chai": "6.2.1",
Expand Down
39 changes: 36 additions & 3 deletions test/hashCheck-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,44 @@ const nt = require('..');
const { expect } = require('chai');
const path = require('path');

const file = path.join(__dirname, 'torrents', 'files.torrent');
const folder = path.join(__dirname, 'files');

describe('Hash Check', function() {
describe('Read a torrent and hash check it', function() {
const file = path.join(__dirname, 'torrents', 'files.torrent');
const folder = path.join(__dirname, 'files');

let percent;

before(function(done) {
nt.read(file, (err, torrent) => {
if (err) return done(err);

let hasher = torrent.hashCheck(folder);

hasher.on('matcherror', (i, file) => {
done(new Error('Could not match file ' + file));
});

hasher.on('match', (index, hash, percentMatched) => {
percent = percentMatched;
});

hasher.on('end', () => {
done();
});

hasher.on('error', done);
});
});

it('100% match', function() {
expect(percent).to.equal(100);
});
});

describe('Read a non-alphabetical torrent and hash check it', function() {
const file = path.join(__dirname, 'torrents', 'files-reverse.torrent');
const folder = path.join(__dirname, 'files');

let percent;

before(function(done) {
Expand Down
14 changes: 14 additions & 0 deletions test/make-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ describe('Make', function() {

it('Info hash matches torrent previously made by mktorrent', function() {
expect(torrent.infoHash()).to.equal('2fff646b166f37f4fd131778123b25a01639e0b3');
/*Torrent {
metadata: {
announce: 'http://faketracker.com',
'created by': 'nt 0.7.1',
'creation date': 1781518092,
info: {
length: 87582,
name: 'click.jpg',
'piece length': 262144,
pieces: <Buffer 23 16 e5 0e 24 8d 8e a5 df 98 80 49 d6 e1 43 0a 59 05 bc 89>,
private: 1
}
}
}*/
});
});

Expand Down
5 changes: 0 additions & 5 deletions test/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ describe('Schema', function() {
};
});

it('`announce` and `announce-list` fields not found', function() {
delete torrent.announce;
expect(schema.checkTorrent(torrent)).to.equal('`announce` and `announce-list` fields not found');
});

it('`announce` is not a buffer', function() {
torrent.announce = 43;
expect(schema.checkTorrent(torrent)).to.equal('`announce` is not a buffer');
Expand Down
1 change: 1 addition & 0 deletions test/torrents/files-reverse.torrent
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d4:infod5:filesld6:lengthi837e4:pathl9:heart.jpgeed6:lengthi87582e4:pathl9:click.jpgeee4:name5:files12:piece lengthi262144e6:pieces20:��-�:^�+!i\0�ק��Mee