diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-01-05 09:08:58 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-01-05 09:08:58 +0300 |
commit | f5d24efd1cc6ab419dc50de091949bcd7b58be3f (patch) | |
tree | 49f4fcdf35aa6b1cb813030b1041fdaa985b034a | |
parent | ffcd84f07bd161ac424e682a683b09d376eff331 (diff) |
upgrade simple-sha1 to v2; use async sha1
-rw-r--r-- | lib/storage.js | 35 | ||||
-rw-r--r-- | package.json | 2 |
2 files changed, 23 insertions, 14 deletions
diff --git a/lib/storage.js b/lib/storage.js index 2964f2f..cfaa825 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -132,12 +132,23 @@ Piece.prototype.verify = function (buffer) { return } - self.verified = self.noVerify || (sha1(buffer) === self.hash) - if (self.verified) { - self.emit('done') + if (self.noVerify) { + self.verified = true + onResult() } else { - self.emit('warning', new Error('piece ' + self.index + ' failed verification; ' + sha1(buffer) + ' expected ' + self.hash)) - self._reset() + sha1(buffer, function (hash) { + self.verified = (hash === self.hash) + onResult() + }) + } + + function onResult () { + if (self.verified) { + self.emit('done') + } else { + self.emit('warning', new Error('piece ' + self.index + ' failed verification; ' + sha1(buffer) + ' expected ' + self.hash)) + self._reset() + } } } @@ -359,8 +370,11 @@ Storage.BLOCK_LENGTH = BLOCK_LENGTH Storage.prototype.load = function (streams, cb) { var self = this if (!Array.isArray(streams)) streams = [ streams ] - if (!cb) cb = function () {} - cb = once(cb) + cb = once(cb || function () {}) + + self.once('done', function () { + cb(null) + }) var pieceIndex = 0 ;(new MultiStream(streams)) @@ -379,12 +393,7 @@ Storage.prototype.load = function (streams, cb) { }) s.end(piece) }) - .on('end', function () { - cb(null) - }) - .on('error', function (err) { - cb(err) - }) + .on('error', cb) } Object.defineProperty(Storage.prototype, 'downloaded', { diff --git a/package.json b/package.json index e2cbd02..5361ad9 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "rimraf": "^2.2.5", "run-parallel": "^1.0.0", "simple-get": "^1.0.0", - "simple-sha1": "^1.0.2", + "simple-sha1": "^2.0.0", "speedometer": "^0.1.2", "thunky": "^0.1.0", "torrent-discovery": "^2.0.1", |