Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2015-01-05 09:08:58 +0300
committerFeross Aboukhadijeh <feross@feross.org>2015-01-05 09:08:58 +0300
commitf5d24efd1cc6ab419dc50de091949bcd7b58be3f (patch)
tree49f4fcdf35aa6b1cb813030b1041fdaa985b034a
parentffcd84f07bd161ac424e682a683b09d376eff331 (diff)
upgrade simple-sha1 to v2; use async sha1
-rw-r--r--lib/storage.js35
-rw-r--r--package.json2
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",