-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
66 lines (58 loc) · 1.5 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
'use strict'
var path = require('path')
var ssbKeys = require('ssb-keys')
var createConfig = require('ssb-config/inject')
var createClient = require('./client')
module.exports = function (keys, opts, cb) {
var config
if (typeof keys === 'function') {
cb = keys
keys = null
opts = null
} else if (typeof opts === 'function') {
cb = opts
opts = keys
keys = null
}
if (typeof opts === 'string' || opts == null || !keys) {
config = createConfig(
(typeof opts === 'string' ? opts : null) || process.env.ssb_appname
)
} else if (opts && typeof opts === 'object') {
config = opts
}
keys = keys || ssbKeys.loadOrCreateSync(path.join(config.path, 'secret'))
opts = opts || {}
var remote
if (opts.remote) { remote = opts.remote } else {
var host = opts.host || 'localhost'
var port = opts.port || config.port || 8008
var key = opts.key || keys.id
var protocol = 'net:'
if (host.endsWith('.onion')) {
protocol = 'onion:'
}
const trimmedKey = key.substring(1).replace('.ed25519', '')
remote = `${protocol}${host}:${port}~shs:${trimmedKey}`
}
var manifest = opts.manifest || null
const options = {
keys: keys,
manifest: manifest,
config: config,
remote: remote
}
if (typeof cb === 'function') {
createClient(options, cb)
} else {
return new Promise((resolve, reject) => {
createClient(options, (err, val) => {
if (err) {
reject(err)
} else {
resolve(val)
}
})
})
}
}