git ssb

4+

Dominic / scuttlebot



Tree: 98073738664294ded7f111ec6d6a62529686182e

Files: 98073738664294ded7f111ec6d6a62529686182e / index.js

6092 bytesRaw
1var SecretStack = require('secret-stack')
2var create = require('ssb-db/create')
3var ssbKeys = require('ssb-keys')
4var path = require('path')
5var osenv = require('osenv')
6var mkdirp = require('mkdirp')
7var rimraf = require('rimraf')
8var mdm = require('mdmanifest')
9var cmdAliases = require('./lib/cli-cmd-aliases')
10var valid = require('./lib/validators')
11var apidocs = require('./lib/apidocs.js')
12var pkg = require('./package.json')
13
14function isString(s) { return 'string' === typeof s }
15function isObject(o) { return 'object' === typeof o }
16function isFunction (f) { return 'function' === typeof f }
17// create SecretStack definition
18var manifest = mdm.manifest(apidocs._)
19manifest.seq = 'async'
20manifest.usage = 'sync'
21manifest.clock = 'async'
22manifest.version = 'sync'
23
24var SSB = {
25 manifest: manifest,
26 permissions: {
27 master: {allow: null, deny: null},
28 anonymous: {allow: ['createHistoryStream'], deny: null}
29 },
30 init: function (api, opts) {
31
32 // .temp: use a /tmp data directory
33 // (useful for testing)
34 if(opts.temp) {
35 var name = isString(opts.temp) ? opts.temp : ''+Date.now()
36 opts.path = path.join(osenv.tmpdir(), name)
37 rimraf.sync(opts.path)
38 }
39
40 // load/create secure scuttlebutt data directory
41 var dbPath = path.join(opts.path, 'db')
42 mkdirp.sync(dbPath)
43
44 if(!opts.keys)
45 opts.keys = ssbKeys.generate('ed25519', opts.seed && Buffer.from(opts.seed, 'base64'))
46
47 if(!opts.path)
48 throw new Error('opts.path *must* be provided, or use opts.temp=name to create a test instance')
49
50 // main interface
51 var ssb = create(path.join(opts.path, 'db'), opts, opts.keys)
52 //treat the main feed as remote, because it's likely handled like that by others.
53 var feed = ssb.createFeed(opts.keys, {remote: true})
54 var _close = api.close
55 var close = function (arg, cb) {
56 if('function' === typeof arg) cb = arg
57 // override to close the SSB database
58 ssb.close(function (err) {
59 if (err) throw err
60 _close()
61 cb && cb() //multiserver doesn't take a callback on close.
62 })
63 }
64
65 function since () {
66 var plugs = {}
67 var sync = true
68 for(var k in ssb) {
69 if(ssb[k] && isObject(ssb[k]) && isFunction(ssb[k].since)) {
70 plugs[k] = ssb[k].since.value
71 sync = sync && (plugs[k] === ssb.since.value)
72 }
73 }
74 return {
75 since: ssb.since.value,
76 plugins: plugs,
77 sync: sync,
78 }
79 }
80 var self
81 return self = {
82 id : feed.id,
83 keys : opts.keys,
84
85 ready : function () {
86 return ssb.ready.value
87 },
88
89 progress : function () {
90 return ssb.progress
91 },
92
93 status : function () {
94 return {progress: self.progress(), db: ssb.status, sync: since() }
95 },
96
97 version : function () {
98 return pkg.version
99 },
100
101 //temporary!
102 _flumeUse :
103 function (name, flumeview) {
104 ssb.use(name, flumeview)
105 return ssb[name]
106 },
107
108 usage : valid.sync(usage, 'string?|boolean?'),
109 close : valid.async(close),
110
111 publish : valid.async(feed.add, 'string|msgContent'),
112 add : valid.async(ssb.add, 'msg'),
113 queue : valid.async(ssb.queue, 'msg'),
114 get : valid.async(ssb.get, 'msgLink|number|object'),
115
116 post : ssb.post,
117 addMap : ssb.addMap,
118
119 since : since,
120
121 getPublicKey : ssb.getPublicKey,
122 latest : ssb.latest,
123 getLatest : valid.async(ssb.getLatest, 'feedId'),
124 latestSequence : valid.async(ssb.latestSequence, 'feedId'),
125 createFeed : ssb.createFeed,
126 whoami : function () { return { id: feed.id } },
127 query : ssb.query,
128 createFeedStream : valid.source(ssb.createFeedStream, 'readStreamOpts?'),
129 createHistoryStream : valid.source(ssb.createHistoryStream, ['createHistoryStreamOpts'], ['feedId', 'number?', 'boolean?']),
130 createLogStream : valid.source(ssb.createLogStream, 'readStreamOpts?'),
131 createUserStream : valid.source(ssb.createUserStream, 'createUserStreamOpts'),
132 links : valid.source(ssb.links, 'linksOpts'),
133 sublevel : ssb.sublevel,
134 messagesByType : valid.source(ssb.messagesByType, 'string|messagesByTypeOpts'),
135 createWriteStream : ssb.createWriteStream,
136 getVectorClock : ssb.getVectorClock,
137 getAtSequence : ssb.getAtSequence,
138 addUnboxer : ssb.addUnboxer,
139 }
140 }
141}
142
143// live help RPC method
144function usage (cmd) {
145 var path = (cmd||'').split('.')
146 if ((path[0] && apidocs[path[0]]) || (cmd && apidocs[cmd])) {
147 // return usage for the plugin
148 cmd = path.slice(1).join('.')
149 return mdm.usage(apidocs[path[0]], cmd, { prefix: path[0] })
150 }
151 if (!cmd) {
152 // return usage for all docs
153 return Object.keys(apidocs).map(function (name) {
154 if (name == '_')
155 return mdm.usage(apidocs[name], null, { nameWidth: 20 })
156
157 var text = mdm.usage(apidocs[name], null, { prefix: name, nameWidth: 20 })
158 return text.slice(text.indexOf('Commands:') + 10) // skip past the toplevel summary, straight to the cmd list
159 }).join('\n\n')
160 }
161 // toplevel cmd usage
162 cmd = cmdAliases[cmd] || cmd
163 return mdm.usage(apidocs._, cmd)
164}
165
166function createSbot() {
167 return SecretStack({
168 //this is just the default app key.
169 //it can be overridden by passing a appKey as option
170 //when creating a Sbot instance.
171 appKey: require('./lib/ssb-cap')
172 })
173 .use(SSB)
174}
175module.exports = createSbot()
176module.exports.createSbot = createSbot
177
178
179
180
181
182

Built with git-ssb-web