multiblob
Advanced tools
Comparing version 1.13.3 to 1.13.4
90
index.js
@@ -11,8 +11,5 @@ var cont = require('cont') | ||
var paramap = require('pull-paramap') | ||
var cat = require('pull-cat') | ||
var Notify = require('pull-notify') | ||
var Live = require('pull-live') | ||
var Write = require('pull-write-file') | ||
var Read = require('pull-file') | ||
var Catch = require('pull-catch') | ||
@@ -22,63 +19,5 @@ var u = require('./util') | ||
function write (filename, cb) { | ||
return WriteFile(filename, cb) | ||
} | ||
module.exports = function Blobs (config) { | ||
if (!config) throw Error('multiblob expects config') | ||
/** | ||
* Wraps the `pull-file` function module with two changes: errors are redacted, | ||
* and any error except ENOENT (file not found) will be logged to the server. | ||
* | ||
* @param {...object} args - arguments to pass to `pull-file` | ||
* | ||
* @return {function} pull-stream source, to be consumed by a through or sink | ||
*/ | ||
function readFile (...args) { | ||
return pull( | ||
Read(...args), | ||
Catch(err => { | ||
if (err.code !== 'ENOENT') { | ||
console.error(new Error(err)) | ||
} | ||
err.message = 'could not get blob' | ||
return false // pass along error | ||
}) | ||
) | ||
}; | ||
function toArray (h) { | ||
return Array.isArray(h) ? h : [h] | ||
} | ||
function single (fn) { | ||
var waiting = {} | ||
function async (key, cb) { | ||
if(!waiting[key]) { | ||
waiting[key] = [cb] | ||
var cbs = waiting[key] | ||
fn(key, function done (err, result) { | ||
if(cbs.length) | ||
delete waiting[key] | ||
while(cbs.length) cbs.shift()(err, result) | ||
}) | ||
} | ||
else | ||
waiting[key].push(cb) | ||
} | ||
//dump all the things that have been done already, | ||
//when something has been added? | ||
async.done = function (key, err, value) { | ||
if(!waiting[key]) return | ||
var cbs = waiting[key] | ||
delete waiting[key] | ||
while(cbs.length) cbs.shift()(err, result) | ||
} | ||
return async | ||
} | ||
var Blobs = module.exports = function (config) { | ||
var dir | ||
@@ -88,2 +27,4 @@ if('string' === typeof config) | ||
dir = config.dir | ||
var alg = config.hash = config.hash || config.alg || 'blake2s' | ||
var encode = config.encode || u.encode | ||
@@ -108,6 +49,4 @@ var decode = config.decode || u.decode | ||
config = config || {} | ||
var alg = config.hash = config.hash || config.alg || 'blake2s' | ||
var empty = u.encode(u.algs[alg]().digest(), alg) | ||
// MIX: I think this should be encode NOT u.encode ?! | ||
@@ -118,6 +57,4 @@ function isEmptyHash(hash) { | ||
dir = config.dir | ||
var n = 0 | ||
var waiting = [], tmp = false, clean = false | ||
var waiting = [], tmp = false | ||
@@ -129,3 +66,3 @@ function init (cb) { | ||
var stat = single(fs.stat) | ||
var stat = u.single(fs.stat) | ||
@@ -185,3 +122,3 @@ var tmpdir = path.join(dir, 'tmp') | ||
cont.para(toArray(hashes).map(test)) (function (err, ary) { | ||
cont.para(u.toArray(hashes).map(test)) (function (err, ary) { | ||
//will give an error if any hash was invalid. | ||
@@ -200,4 +137,2 @@ if(err) cb(err) | ||
var listeners = [] | ||
function getSlice(opts) { | ||
@@ -224,3 +159,3 @@ if(isEmptyHash(opts.hash)) return pull.empty() | ||
else | ||
stream.resolve(readFile(toPath(dir, opts.hash), { | ||
stream.resolve(u.readFile(toPath(dir, opts.hash), { | ||
start: opts.start, | ||
@@ -239,3 +174,3 @@ end: opts.end | ||
if(isEmptyHash(hash)) return pull.empty() | ||
return readFile(toPath(dir, opts)) | ||
return u.readFile(toPath(dir, opts)) | ||
} | ||
@@ -341,4 +276,4 @@ var hash = opts.key || opts.hash | ||
return long | ||
? newBlob.listen() | ||
: pull(newBlob.listen(), pull.map(function (e) { return e.id })) | ||
? newBlob.listen() | ||
: pull(newBlob.listen(), pull.map(function (e) { return e.id })) | ||
}), | ||
@@ -359,1 +294,2 @@ | ||
{ | ||
"name": "multiblob", | ||
"description": "", | ||
"version": "1.13.3", | ||
"version": "1.13.4", | ||
"homepage": "https://github.com/dominictarr/multiblob", | ||
@@ -15,3 +15,2 @@ "repository": { | ||
"mkdirp": "^0.5.0", | ||
"pull-cat": "^1.1.8", | ||
"pull-catch": "^1.0.0", | ||
@@ -18,0 +17,0 @@ "pull-defer": "^0.2.2", |
@@ -9,24 +9,36 @@ # multiblob | ||
``` js | ||
var Blobs = require('multiblob') | ||
var blobs = Blobs({dir:dir, alg: 'sha256'}) //pass in the basedir & hash alg | ||
var blobs = Blobs({ dir: '/media/blobCollection' }) | ||
pull(source, blobs.add(function (err, hash) { | ||
console.log('added source to blobs:', hash) | ||
}) | ||
pull( | ||
source, // a buffer stream e.g. from pull-file | ||
blobs.add(function (err, hash) { | ||
console.log('added source to blobs:', hash) | ||
}) | ||
) | ||
``` | ||
## api: Blobs(config) | ||
## API | ||
### add (hash?, cb?) => Sink | ||
### Blobs(config) => blobs | ||
create a sink stream for writing a blob. | ||
where config is an Object with properties: | ||
- `dir` _String_ - the directory to store blobs in | ||
- `alg` _String_ (optional) - the algorithm for hashing. Valid options: `'blake2s'`, `'sha256'` (default: `'blake2s'`) | ||
- `encode` _Function_ (optional) - converts a buffer to a string (default: see `util.js#encode`) | ||
- `decode` _Function_ (optional) - recovers a string into an object `{ hash: Buffer, alg }` (default: see `util.js#decode`) | ||
- `isHash` _Function_ (optional) - tests a string to check if it's a valid hash (default: see `util.js#isHash`) | ||
### blobs.add (hash?, cb?) => Sink | ||
Create a sink stream for writing a blob. | ||
Expects to receive a buffer stream. | ||
If `hash` was given, then it will error if the file turned out to be different. | ||
If a `cb` is not given and there was an error, this function will throw. | ||
### get (hash || opts) => Source | ||
### blobs.get (hash || opts) => Source | ||
create a source stream that reads from a given blob. | ||
Takes the hash of blob already in the store and return a source buffer stream. | ||
If the file does not exist this stream will error. | ||
@@ -39,3 +51,3 @@ | ||
### getSlice (opts) => Source | ||
### blbos.getSlice(opts) => Source | ||
@@ -48,3 +60,3 @@ create a source stream that reads a slice of a given blob, | ||
### has(hash, cb) | ||
### blobs.has(hash, cb) | ||
@@ -55,3 +67,3 @@ check if the given hash is in the store. | ||
### size(hash, cb) | ||
### blobs.size(hash, cb) | ||
@@ -63,11 +75,11 @@ get the size of this blob. If `hash` is an array of hashes, | ||
### ls() => Source | ||
### blobs.ls() => Source | ||
source stream that reads the list of hashes available in the store. | ||
### rm(hash, cb) | ||
### blobs.rm(hash, cb) | ||
remove a hash from the store. | ||
### isEmptyHash(hash) | ||
### blobs.isEmptyHash(hash) | ||
@@ -77,3 +89,11 @@ Check if a given hash is actually the empty hash. If something has the empty hash, | ||
### blobs.meta | ||
??? | ||
### blobs.resolve | ||
??? | ||
## todo | ||
@@ -80,0 +100,0 @@ |
63
util.js
var Blake2s = require('blake2s') | ||
var createHash = require('crypto').createHash | ||
var hash = require('crypto') | ||
var path = require('path') | ||
var pull = require('pull-stream') | ||
var pull = require('pull-stream') | ||
var Read = require('pull-file') | ||
var Catch = require('pull-catch') | ||
@@ -51,1 +51,58 @@ var isBuffer = Buffer.isBuffer | ||
exports.algs = algs | ||
/** | ||
* Wraps the `pull-file` function module with two changes: errors are redacted, | ||
* and any error except ENOENT (file not found) will be logged to the server. | ||
* | ||
* @param {...object} args - arguments to pass to `pull-file` | ||
* | ||
* @return {function} pull-stream source, to be consumed by a through or sink | ||
*/ | ||
exports.readFile = function (...args) { | ||
return pull( | ||
Read(...args), | ||
Catch(err => { | ||
if (err.code !== 'ENOENT') { | ||
console.error(new Error(err)) | ||
} | ||
err.message = 'could not get blob' | ||
return false // pass along error | ||
}) | ||
) | ||
}; | ||
exports.toArray = function (h) { | ||
return Array.isArray(h) ? h : [h] | ||
} | ||
exports.single = function (fn) { | ||
var waiting = {} | ||
function async (key, cb) { | ||
if(!waiting[key]) { | ||
waiting[key] = [cb] | ||
var cbs = waiting[key] | ||
fn(key, function done (err, result) { | ||
if(cbs.length) | ||
delete waiting[key] | ||
while(cbs.length) cbs.shift()(err, result) | ||
}) | ||
} | ||
else | ||
waiting[key].push(cb) | ||
} | ||
//dump all the things that have been done already, | ||
//when something has been added? | ||
async.done = function (key, err, value) { | ||
if(!waiting[key]) return | ||
var cbs = waiting[key] | ||
delete waiting[key] | ||
while(cbs.length) cbs.shift()(err, result) | ||
} | ||
return async | ||
} |
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
31557
15
100
874
- Removedpull-cat@^1.1.8