Comparing version 1.8.2 to 1.9.1
56
index.js
@@ -5,3 +5,2 @@ var cont = require('cont') | ||
var path = require('path') | ||
var toPull = require('stream-to-pull-stream') | ||
var explain = require('explain-error') | ||
@@ -16,6 +15,10 @@ var mkdirp = require('mkdirp') | ||
var Write = require('pull-write-file') | ||
var Read = require('pull-file') | ||
var u = require('./util') | ||
var createHash = u.createHash, toPath = u.toPath, isHash = u.isHash | ||
var createHash = u.createHash | ||
function write (filename, cb) { | ||
return WriteFile(filename, cb) | ||
return toPull.sink(fs.createWriteStream(filename), cb) | ||
@@ -25,3 +28,4 @@ } | ||
function read (filename) { | ||
return toPull.source(fs.createReadStream(filename)) | ||
return ReadFile(filename) | ||
// return toPull.source(fs.createReadStream(filename)) | ||
} | ||
@@ -38,6 +42,22 @@ | ||
var encode = config.encode || u.encode | ||
var decode = config.decode || u.decode | ||
var isHash = config.isHash || u.isHash | ||
function toPath (dir, string) { | ||
var d = decode(string) | ||
var h = d.hash.toString('hex') | ||
return path.join(dir, d.alg, h.substring(0,2), h.substring(2)) | ||
} | ||
function toHash(filename) { | ||
var parts = path.relative(dir, filename).split(path.sep) | ||
var alg = parts.shift() | ||
return encode(new Buffer(parts.join(''), 'hex'), alg) | ||
} | ||
var newBlob = Notify() | ||
config = config || {} | ||
config.hash = config.hash || config.alg || 'blake2s' | ||
var alg = config.hash = config.hash || config.alg || 'blake2s' | ||
@@ -93,8 +113,2 @@ dir = config.dir | ||
function toHash(filename) { | ||
var parts = filename.replace(dir+'/', '').split('/') | ||
var alg = parts.shift() | ||
return new Buffer(parts.join(''), 'hex').toString('base64')+'.'+alg | ||
} | ||
var listeners = [] | ||
@@ -106,3 +120,3 @@ | ||
if(isHash(opts)) | ||
return read(toPath(dir, opts)) | ||
return Read(toPath(dir, opts)) | ||
@@ -130,3 +144,3 @@ var hash = opts.key || opts.hash | ||
else | ||
stream.resolve(read(toPath(dir, hash))) | ||
stream.resolve(Read(toPath(dir, hash))) | ||
}) | ||
@@ -151,3 +165,3 @@ | ||
var tmpfile = path.join(dir, 'tmp', Date.now() + '-' + n++) | ||
var hasher = createHash(config.hash) | ||
var hasher = createHash(alg) | ||
var size = 0 | ||
@@ -160,15 +174,17 @@ | ||
}), | ||
write(tmpfile, function (err) { | ||
Write(tmpfile, function (err) { | ||
if(err) return cb(explain(err, 'could not write to tmpfile')) | ||
if(hash && hash !== hasher.digest) | ||
return cb(new Error('actual hash:'+ hasher.digest | ||
+ ' did not match expected hash:'+hash), hasher.digest) | ||
var _hash = encode(hasher.digest, alg) | ||
var p = toPath(dir, hash || hasher.digest) | ||
if(hash && hash !== _hash) | ||
return cb(new Error('actual hash:'+ _hash | ||
+ ' did not match expected hash:'+hash), _hash) | ||
var p = toPath(dir, hash || _hash) | ||
mkdirp(path.dirname(p), function () { | ||
fs.rename(tmpfile, p, function (err) { | ||
if(err) cb(explain(err, 'could not move file')) | ||
else newBlob({id:toHash(p), size: size, ts: Date.now()}), cb(null, hasher.digest) | ||
else newBlob({id:toHash(p), size: size, ts: Date.now()}), cb(null, _hash) | ||
}) | ||
@@ -215,1 +231,3 @@ }) | ||
} | ||
{ | ||
"name": "multiblob", | ||
"description": "", | ||
"version": "1.8.2", | ||
"version": "1.9.1", | ||
"homepage": "https://github.com/dominictarr/multiblob", | ||
@@ -16,10 +16,11 @@ "repository": { | ||
"pull-cat": "^1.1.8", | ||
"pull-defer": "~0.1.1", | ||
"pull-glob": "~1.0.1", | ||
"pull-defer": "^0.2.2", | ||
"pull-file": "^0.5.0", | ||
"pull-glob": "^1.0.4", | ||
"pull-notify": "0.0.2", | ||
"pull-paramap": "~1.1.3", | ||
"pull-stream": "^3.0.1", | ||
"pull-paramap": "^1.1.4", | ||
"pull-stream": "^3.3.0", | ||
"pull-write-file": "^0.2.0", | ||
"rc": "~0.5.4", | ||
"rimraf": "~2.2.8", | ||
"stream-to-pull-stream": "~1.6.1" | ||
"rimraf": "~2.2.8" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
@@ -10,6 +10,6 @@ | ||
var rimraf = require('rimraf') | ||
var toPull = require('stream-to-pull-stream') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var osenv = require('osenv') | ||
var Read = require('pull-file') | ||
@@ -32,3 +32,3 @@ var dirname = path.join(osenv.tmpdir(), 'test-multiblob') | ||
pull(pull.values(ary), hasher, pull.drain()) | ||
return hasher.digest | ||
return util.encode(hasher.digest, alg) | ||
} | ||
@@ -134,7 +134,7 @@ | ||
pull( | ||
toPull.source(fs.createReadStream(filename)), | ||
Read(filename), | ||
hasher, | ||
pull.drain(null, function (err) { | ||
t.notOk(err) | ||
t.equal(hasher.digest, hash1) | ||
t.equal(util.encode(hasher.digest, alg), hash1) | ||
t.end() | ||
@@ -149,1 +149,4 @@ }) | ||
module.exports('blake2s') | ||
@@ -19,6 +19,8 @@ | ||
module.exports = function (alg) { | ||
function hasher (ary) { | ||
var hasher = util.createHash() | ||
pull(pull.values(ary), hasher, pull.drain()) | ||
return hasher.digest | ||
return util.encode(hasher.digest, alg) | ||
} | ||
@@ -49,3 +51,3 @@ | ||
t.equal(hasher.digest, hash1) | ||
t.equal(util.encode(hasher.digest, alg), hash1) | ||
t.equal(hasher.size, 1024*100) | ||
@@ -127,2 +129,4 @@ t.end() | ||
} | ||
if(!module.parent) module.exports('blake2s') |
24
util.js
@@ -9,10 +9,2 @@ var Blake2s = require('blake2s') | ||
exports.toPath = function (dir, hash) { | ||
var i = hash.indexOf('.') | ||
var alg = hash.substring(i+1) | ||
var h = new Buffer(hash.substring(0, i), 'base64').toString('hex') | ||
return path.join(dir, alg, h.substring(0,2), h.substring(2)) | ||
} | ||
var algs = { | ||
@@ -23,2 +15,12 @@ blake2s: function () { return new Blake2s() }, | ||
exports.encode = function (buf, alg) { | ||
return buf.toString('base64')+'.'+alg | ||
} | ||
exports.decode = function (str) { | ||
var i = str.indexOf('.') | ||
var alg = str.substring(i+1) | ||
return {hash: new Buffer(str.substring(0, i), 'base64'), alg: alg} | ||
} | ||
exports.createHash = function (alg) { | ||
@@ -33,4 +35,5 @@ alg = alg || 'blake2s' | ||
}, function () { | ||
var digest = hash.digest('base64') + '.' + alg | ||
hasher.digest = digest | ||
hasher.digest = hash.digest() | ||
// var digest = hash.digest('base64') + '.' + alg | ||
// hasher.digest = digest | ||
}) | ||
@@ -49,1 +52,2 @@ | ||
} | ||
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
20312
592
14
+ Addedpull-file@^0.5.0
+ Addedpull-write-file@^0.2.0
+ Addedlooper@4.0.0(transitive)
+ Addedpull-defer@0.2.3(transitive)
+ Addedpull-paramap@1.2.2(transitive)
- Removedstream-to-pull-stream@~1.6.1
- Removedlooper@3.0.0(transitive)
- Removedpull-core@1.1.0(transitive)
- Removedpull-defer@0.1.1(transitive)
- Removedpull-paramap@1.1.6(transitive)
- Removedpull-stream@2.26.1(transitive)
- Removedstream-to-pull-stream@1.6.10(transitive)
Updatedpull-defer@^0.2.2
Updatedpull-glob@^1.0.4
Updatedpull-paramap@^1.1.4
Updatedpull-stream@^3.3.0