git ssb

4+

Dominic / scuttlebot



Tree: f45313ea6f6ff027628835c00091eee7d0f86251

Files: f45313ea6f6ff027628835c00091eee7d0f86251 / index.js

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

Built with git-ssb-web