git ssb

3+

Dominic / ssb-blobs



Tree: bde3bf7b399b05e5d5c6475ee35ef2745c6788de

Files: bde3bf7b399b05e5d5c6475ee35ef2745c6788de / test / util.js

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

Built with git-ssb-web