git ssb

1+

Dominic / secure-scuttlebutt



Tree: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab

Files: 6dde1c5e1d32a7427848bfad8a4641577bd0d1ab / test / util.js

2086 bytesRaw
1var level = require('level-test')()
2var sublevel = require('level-sublevel/bytewise')
3var pull = require('pull-stream')
4var tape = require('tape')
5var createFeed = require('ssb-feed')
6var ssbKeys = require('ssb-keys')
7
8var SSB = require('../')
9//var u = require('../util')
10var w = require('./util')
11var u = w
12
13
14function rand (n) {
15 var a = []
16 while(n--)
17 a.push(Math.random())
18 return a
19}
20
21
22module.exports = function (opts) {
23
24 var create = require('ssb-feed/util').create
25
26 function createDB(name) {
27 return SSB(sublevel(level(name, {
28 valueEncoding: require('../codec')
29 })), opts)
30 }
31
32 var MESSAGE = new Buffer('msg')
33
34 function load (ssb, keys, n, cb) {
35 var prev
36 ssb.getLatest(keys.public, function (err, prev) {
37 if(err) return cb(err)
38 pull(
39 pull.values(rand(n)),
40 pull.asyncMap(function (r, cb) {
41 ssb.add(prev =
42 create(keys, 'msg', ''+r, prev), cb)
43 }),
44 pull.drain(null, cb)
45 )
46 })
47 }
48
49// function init (ssb, n, cb) {
50// var keys = opts.keys.generate()
51// var prev
52//
53// ssb.add(prev = create(keys, 'init', keys.public), function () {
54// pull(
55// pull.values(rand(n)),
56// pull.asyncMap(function (r, cb) {
57// ssb.add(prev =
58// create(keys, 'msg', ''+r, prev), cb)
59// }),
60// pull.drain(null, cb)
61// )
62// })
63// return keys
64// }
65//
66 function init2 (ssb, n, cb) {
67 var feed = createFeed(ssb, ssbKeys.generate(), opts)
68 var prev
69
70 pull(
71 pull.values(rand(n)),
72 pull.asyncMap(function (r, cb) {
73 feed.add('msg', ''+r, cb)
74 }),
75 pull.drain(null, cb)
76 )
77
78 return feed
79 }
80
81
82
83 function compareDbs (a, b, cb) {
84
85 var cbs = u.groups(next)
86
87 pull(a.createFeedStream(), pull.collect(cbs()))
88 pull(b.createFeedStream(), pull.collect(cbs()))
89
90 function next(err, ary) {
91 cb(err, ary && ary[0], ary && ary[1])
92 }
93 }
94
95 return {
96 createDB: createDB, init2: init2,
97 compareDbs: compareDbs, load: load
98 }
99
100}
101

Built with git-ssb-web