hyperdrive
Advanced tools
Comparing version 3.2.1 to 3.3.0
60
index.js
@@ -60,2 +60,17 @@ var constants = require('constants') | ||
function Progress () { | ||
this.bytesInitial = 0 | ||
this.bytesRead = 0 | ||
this.bytesTotal = 0 | ||
events.EventEmitter.call(this) | ||
} | ||
util.inherits(Progress, events.EventEmitter) | ||
Progress.prototype.end = function (err) { | ||
if (!err) return this.emit('end') | ||
if (this.listeners('error').length) this.emit('error', err) | ||
return | ||
} | ||
function Archive (drive, folder, id) { | ||
@@ -133,2 +148,4 @@ events.EventEmitter.call(this) | ||
var self = this | ||
var stats = new Progress() | ||
var offset = 0 | ||
@@ -138,2 +155,4 @@ if (typeof i === 'number') this.entry(i, onentry) | ||
return stats | ||
function onentry (err, entry) { | ||
@@ -147,5 +166,25 @@ if (err) return cb(err) | ||
feed.open(kick) | ||
stats.bytesTotal = entry.size | ||
function kick () { | ||
if (feed._storage) onstorage(feed._storage) | ||
else feed.on('_storage', onstorage) | ||
function onstorage (storage) { | ||
if (!storage._bytesWritten) return | ||
storage._bytesWritten(function (_, bytes) { | ||
if (bytes) { | ||
stats.bytesInitial = bytes - offset | ||
stats.bytesRead = bytes | ||
stats.emit('ready') | ||
} | ||
}) | ||
} | ||
function kick (block, data) { | ||
if (!feed.blocks) return | ||
if (data && this === feed && entry.link && block < feed.blocks - entry.link.index.length) { | ||
stats.bytesRead += data.length | ||
offset += data.length | ||
} | ||
for (; ptr < feed.blocks; ptr++) { | ||
@@ -172,5 +211,6 @@ if (!feed.has(ptr)) return | ||
function done (err) { | ||
if (err) return cb(err) | ||
self.stats.filesDownloaded++ | ||
cb(null) | ||
if (!err) self.stats.filesDownloaded++ | ||
stats.bytesRead = stats.bytesTotal | ||
stats.end(err) | ||
cb(err) | ||
} | ||
@@ -385,6 +425,6 @@ } | ||
var self = this | ||
var stats = {bytesRead: 0, bytesTotal: 0} | ||
var stats = new Progress() | ||
fs.lstat(filename, function (err, st) { | ||
if (err) return cb(err) | ||
if (err) return done(err) | ||
@@ -397,5 +437,6 @@ var opts = {filename: filename, stats: stats} | ||
link: null | ||
}, opts, cb) | ||
}, opts, done) | ||
stats.bytesTotal = st.size | ||
stats.emit('ready') | ||
@@ -409,2 +450,7 @@ if (ws) { | ||
return stats | ||
function done (err) { | ||
stats.end(err) | ||
cb(err) | ||
} | ||
} | ||
@@ -411,0 +457,0 @@ |
@@ -65,2 +65,3 @@ var fs = require('fs') | ||
if (err) return cb(err) | ||
self._feed.emit('_storage', self) | ||
cb(null, !st.size) | ||
@@ -225,2 +226,3 @@ }) | ||
ptr += bytes | ||
if (ptr === buf.length) return cb(null, buf) | ||
@@ -318,2 +320,32 @@ fs.read(self.fd, buf, ptr, buf.length - ptr, offset + ptr, read) | ||
ChunkStore.prototype._bytesWritten = function (cb) { | ||
var self = this | ||
var cnt = this._index.length | ||
var i = 0 | ||
loop(null) | ||
function loop (err) { | ||
if (err) return cb(err) | ||
if (i >= cnt) { | ||
var written = 0 | ||
for (var j = 0; j < self.contentBlocks; j++) { | ||
if (!self._feed.has(j)) continue | ||
var rem = j & MASK | ||
var buf = self._offsets[(j - rem) / SIZE] | ||
var end = buf.readUInt32BE(4 * rem) | ||
var start = rem && buf.readUInt32BE(4 * rem - 4) | ||
written += (end - start) | ||
} | ||
return cb(null, written) | ||
} | ||
self._load(i++, loop) | ||
} | ||
} | ||
ChunkStore.prototype._flushAndWrite = function (offset, buf, cb) { | ||
@@ -320,0 +352,0 @@ var self = this |
{ | ||
"name": "hyperdrive", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "A file sharing network based on rabin file chunking and append only feeds of data verified by merkle trees.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58334
775