git ssb

2+

ev / minsbot



Tree: d5c6f9521130fcae13928a86e3f39d88e2586342

Files: d5c6f9521130fcae13928a86e3f39d88e2586342 / index.js

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

Built with git-ssb-web