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 /lib | |
parent | ffcd84f07bd161ac424e682a683b09d376eff331 (diff) |
upgrade simple-sha1 to v2; use async sha1
Diffstat (limited to 'lib')
-rw-r--r-- | lib/storage.js | 35 |
1 files changed, 22 insertions, 13 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', { |