UNPKG

1.56 kBJavaScriptView Raw
1const debug = require('debug')('ssb-blobs')
2var pull = require('pull-stream')
3var crypto = require('crypto')
4
5var log = exports.log = function log (name) {
6 if(process.env.DEBUG)
7 return pull.through(function (e) {
8 debug(name, e)
9 })
10 else
11 return pull.through()
12}
13
14function bindAll(obj, context) {
15 var o = {}
16 for(var k in obj) {
17 if(obj[k])
18 o[k] = obj[k].bind(context)
19 }
20 return o
21}
22
23exports.peers = function (nameA, a, nameB, b, async) {
24 var na = nameA[0].toUpperCase(), nb = nameB[0].toUpperCase()
25 //this is just a hack to fake RPC. over rpc each method is called
26 //with the remote id in the current this context.
27 a._onConnect({id: nameB, blobs: bindAll(b, {id: nameA})}, nb+na)
28 b._onConnect({id: nameA, blobs: bindAll(a, {id: nameB})}, na+nb)
29}
30
31
32exports.hash = function (buf) {
33 buf = 'string' == typeof buf ? new Buffer(buf) : buf
34 return '&'+crypto.createHash('sha256')
35 .update(buf).digest('base64')+'.sha256'
36}
37
38exports.fake = function (string, length) {
39 var b = new Buffer(length)
40 var n = Buffer.byteLength(string)
41 for(var i = 0; i < length; i += n)
42 b.write(string, i)
43 return b
44}
45
46
47exports.sync = function noAsync (test, done) {
48 function async(fn) {
49 return fn
50 }
51 async.through = function () { return pull.through() }
52 async.done = done
53 test(async)
54}
55
56exports.tests = function (tests) {
57 tests(function (name, async) {
58 return require('../inject')(
59 require('./mock/blobs')(name, async),
60 require('./mock/set')(async),
61 name
62 )
63 }, exports.sync)
64}
65
66