diff --git a/lib/hasher.js b/lib/hasher.js index 9a33d1e..e19c07d 100644 --- a/lib/hasher.js +++ b/lib/hasher.js @@ -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'); @@ -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. @@ -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); @@ -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, }); diff --git a/lib/schema.js b/lib/schema.js index 17091d1..fe0b8ee 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -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; }; diff --git a/package-lock.json b/package-lock.json index f4d9849..ad0db9c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,8 +14,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", @@ -51,7 +50,6 @@ "version": "7.28.5", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.27.1", "@babel/generator": "^7.28.5", @@ -612,7 +610,6 @@ } ], "license": "MIT", - "peer": true, "dependencies": { "baseline-browser-mapping": "^2.8.25", "caniuse-lite": "^1.0.30001754", @@ -2164,12 +2161,6 @@ "is-typedarray": "^1.0.0" } }, - "node_modules/underscore": { - "version": "1.13.7", - "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.13.7.tgz", - "integrity": "sha512-GMXzWtsc57XAtguZgaQViUOzs0KTkk8ojr3/xAxXLITqf/3EMwxC0inyETfDFjH/Krbhuep0HNbbjI9i/q3F3g==", - "license": "MIT" - }, "node_modules/update-browserslist-db": { "version": "1.1.4", "dev": true, diff --git a/package.json b/package.json index b2b2495..e0c46a0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/hashCheck-test.js b/test/hashCheck-test.js index 8759fb9..975e6ac 100644 --- a/test/hashCheck-test.js +++ b/test/hashCheck-test.js @@ -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) { diff --git a/test/make-test.js b/test/make-test.js index 193ce2e..b502fc1 100644 --- a/test/make-test.js +++ b/test/make-test.js @@ -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: , + private: 1 + } + } + }*/ }); }); diff --git a/test/schema-test.js b/test/schema-test.js index ca902cb..1362c1d 100644 --- a/test/schema-test.js +++ b/test/schema-test.js @@ -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'); diff --git a/test/torrents/files-reverse.torrent b/test/torrents/files-reverse.torrent new file mode 100644 index 0000000..f930c54 --- /dev/null +++ b/test/torrents/files-reverse.torrent @@ -0,0 +1 @@ +d4:infod5:filesld6:lengthi837e4:pathl9:heart.jpgeed6:lengthi87582e4:pathl9:click.jpgeee4:name5:files12:piece lengthi262144e6:pieces20:-:^+!i\0קMee \ No newline at end of file