git ssb

4+

Dominic / scuttlebot



Tree: 28f67af09327610095853aaf8df4b182185f8108

Files: 28f67af09327610095853aaf8df4b182185f8108 / index.js

5668 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
78 //remove this, but we'll need something to make a progress bar
79 //otherwise people will get confused about what is happening.
80
81
82
83 return {
84 id : feed.id,
85 keys : opts.keys,
86
87 progress : function () {
88 return ssb.progress
89 },
90
91 //temporary!
92 _flumeUse :
93 function (name, flumeview) {
94 ssb.use(name, flumeview)
95 return ssb[name]
96 },
97
98 usage : valid.sync(usage, 'string?|boolean?'),
99 close : valid.async(close),
100
101 publish : valid.async(feed.add, 'string|msgContent'),
102 add : valid.async(ssb.add, 'msg'),
103 get : valid.async(ssb.get, 'msgId|number'),
104
105 post : ssb.post,
106
107 since : since,
108
109 getPublicKey : ssb.getPublicKey,
110 latest : ssb.latest,
111 getLatest : valid.async(ssb.getLatest, 'feedId'),
112 latestSequence : valid.async(ssb.latestSequence, 'feedId'),
113 createFeed : ssb.createFeed,
114 whoami : function () { return { id: feed.id } },
115 relatedMessages : valid.async(ssb.relatedMessages, 'relatedMessagesOpts'),
116 query : ssb.query,
117 createFeedStream : valid.source(ssb.createFeedStream, 'readStreamOpts?'),
118 createHistoryStream : valid.source(ssb.createHistoryStream, ['createHistoryStreamOpts'], ['feedId', 'number?', 'boolean?']),
119 createLogStream : valid.source(ssb.createLogStream, 'readStreamOpts?'),
120 createUserStream : valid.source(ssb.createUserStream, 'createUserStreamOpts'),
121 links : valid.source(ssb.links, 'linksOpts'),
122 sublevel : ssb.sublevel,
123 messagesByType : valid.source(ssb.messagesByType, 'string|messagesByTypeOpts'),
124 createWriteStream : ssb.createWriteStream,
125 getVectorClock : ssb.getVectorClock,
126 getAtSequence : ssb.getAtSequence,
127 }
128 }
129}
130
131// live help RPC method
132function usage (cmd) {
133 var path = (cmd||'').split('.')
134 if ((path[0] && apidocs[path[0]]) || (cmd && apidocs[cmd])) {
135 // return usage for the plugin
136 cmd = path.slice(1).join('.')
137 return mdm.usage(apidocs[path[0]], cmd, { prefix: path[0] })
138 }
139 if (!cmd) {
140 // return usage for all docs
141 return Object.keys(apidocs).map(function (name) {
142 if (name == '_')
143 return mdm.usage(apidocs[name], null, { nameWidth: 20 })
144
145 var text = mdm.usage(apidocs[name], null, { prefix: name, nameWidth: 20 })
146 return text.slice(text.indexOf('Commands:') + 10) // skip past the toplevel summary, straight to the cmd list
147 }).join('\n\n')
148 }
149 // toplevel cmd usage
150 cmd = cmdAliases[cmd] || cmd
151 return mdm.usage(apidocs._, cmd)
152}
153
154module.exports = SecretStack({
155 //this is just the default app key.
156 //it can be overridden by passing a appKey as option
157 //when creating a Sbot instance.
158 appKey: require('./lib/ssb-cap')
159})
160.use(SSB)
161
162
163
164

Built with git-ssb-web