-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
70 lines (56 loc) · 1.58 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const ssbKeys = require('ssb-keys')
const crypto = require('crypto')
const { join } = require('path')
const rimraf = require('rimraf')
const os = require('os')
const replicate = require('./replicate')
const connect = require('./connect')
const colorLog = require('./color-log')
let plugins = []
function createTestBot (opts = {}) {
if (!opts.name) { opts.name = randomName() }
if (!opts.path) { opts.path = join(os.tmpdir(), opts.name) }
if (!opts.keys) { opts.keys = ssbKeys.generate() }
opts.conn = {
autostart: false,
...opts.conn
}
opts.db2 = {
// tune db2 for quick write then read (instead of bulk write)
addBatchThrottle: 50,
writeTimeout: 50,
...opts.db2
}
const caps = {
shs: crypto.randomBytes(32).toString('base64'),
...opts.caps
}
const createSbot = require('secret-stack')({ caps })
if (process.env.SSB_DB1 || opts.db1) {
createSbot.use(require('ssb-db'))
} else if (opts.noDefaultUse) {
// no use
} else {
createSbot.use(require('ssb-db2'))
}
plugins.forEach(plugin => createSbot.use(plugin))
plugins = []
if (
opts.startUnclean !== true &&
opts.rimraf !== false
) { rimraf.sync(opts.path) }
const sbot = createSbot(opts)
if (!sbot.name) sbot.name = opts.name
return sbot
}
createTestBot.use = function use (plugin) {
plugins.push(plugin)
return createTestBot
}
createTestBot.replicate = replicate
createTestBot.connect = connect
createTestBot.colorLog = colorLog
module.exports = createTestBot
function randomName () {
return `ssb-test-${Date.now()}-${Math.floor(Math.random() * 1000)}`
}