Skip to content

Commit

Permalink
upgrade simple-sha1 to v2; use async sha1
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Jan 5, 2015
1 parent ffcd84f commit f5d24ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
35 changes: 22 additions & 13 deletions lib/storage.js
Expand Up @@ -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()
}
}
}

Expand Down Expand Up @@ -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))
Expand All @@ -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', {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down

0 comments on commit f5d24ef

Please sign in to comment.