forked from ssbc/ssb-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blobs.js
43 lines (39 loc) · 1.24 KB
/
blobs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
var pull = require('pull-stream')
var ssbHash = require('pull-hash/ext/ssb')
var multicb = require('multicb')
function isFunction(f) {
return 'function' === typeof f
}
// sbot.blobs.add function decorator
// that returns a function that complies with the spec at
// - http://scuttlebot.io/apis/scuttlebot/blobs.html#add-sink
// - http://scuttlebot.io/docs/advanced/publish-a-file.html
//
// Temporary solution until muxrpc supports sinks that can callback
// with arguments.
// See ssb thread for details:
// https://viewer.scuttlebot.io/%252YFBVzniDPuuyLnLk%2FsYSbIJzjhS7ctEIOv5frt9n9Q%3D.sha256
module.exports = function fixAddBlob(add) {
return function (hash, cb) {
if (typeof hash === 'function') cb = hash, hash = null
var done = multicb({ pluck: 1, spread: true })
var sink = pull(
ssbHash(done()),
pull.collect(done())
)
done(function(err, actualHash, buffers) {
if (hash && hash !== actualHash) return cb(new Error('Invalid blob hash value. expected: ' + hash + ', actual: ' + actualHash))
pull(
pull.values(buffers),
add(hash, function(err) {
if(isFunction(cb))
{
cb(err, actualHash)
}
})
)
})
return sink
}
}