git ssb

4+

Dominic / scuttlebot



Tree: 0cd58652563ce86bd6be1809c271805692470a58

Files: 0cd58652563ce86bd6be1809c271805692470a58 / index.js

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

Built with git-ssb-web