git ssb

0+

cel / %ccrX9uZ2173/xsDbtTM…



forked from Dominic / scuttlebot

Tree: 43ca596ccac83e143189759816e49e548b63038e

Files: 43ca596ccac83e143189759816e49e548b63038e / index.js

4344 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'), null, opts.keys)
46 var feed = ssb.createFeed(opts.keys)
47 var _close = api.close
48 var close = function (cb) {
49 // override to close the SSB database
50 ssb.close(function (err) {
51 if (err) throw err
52 _close(cb)
53 })
54 }
55 return {
56 id : feed.id,
57 keys : opts.keys,
58
59 usage : valid.sync(usage, 'string?|boolean?'),
60 close : valid.async(close),
61
62 publish : valid.async(feed.add, 'string|msgContent'),
63 add : valid.async(ssb.add, 'msg'),
64 get : valid.async(ssb.get, 'msgId'),
65
66 pre : ssb.pre,
67 post : ssb.post,
68
69 getPublicKey : ssb.getPublicKey,
70 latest : ssb.latest,
71 getLatest : valid.async(ssb.getLatest, 'feedId'),
72 latestSequence : valid.async(ssb.latestSequence, 'feedId'),
73 createFeed : ssb.createFeed,
74 whoami : function () { return { id: feed.id } },
75 relatedMessages : valid.async(ssb.relatedMessages, 'relatedMessagesOpts'),
76 query : ssb.query,
77 createFeedStream : valid.source(ssb.createFeedStream, 'readStreamOpts?'),
78 createHistoryStream : valid.source(ssb.createHistoryStream, ['createHistoryStreamOpts'], ['feedId', 'number?', 'boolean?']),
79 createLogStream : valid.source(ssb.createLogStream, 'readStreamOpts?'),
80 createUserStream : valid.source(ssb.createUserStream, 'createUserStreamOpts'),
81 links : valid.source(ssb.links, 'linksOpts'),
82 sublevel : ssb.sublevel,
83 messagesByType : valid.source(ssb.messagesByType, 'string|messagesByTypeOpts'),
84 createWriteStream : ssb.createWriteStream,
85// createLatestLookupStream : ssb.createLatestLookupStream,
86 }
87 }
88}
89
90// live help RPC method
91function usage (cmd) {
92 var path = (cmd||'').split('.')
93 if ((path[0] && apidocs[path[0]]) || (cmd && apidocs[cmd])) {
94 // return usage for the plugin
95 cmd = path.slice(1).join('.')
96 return mdm.usage(apidocs[path[0]], cmd, { prefix: path[0] })
97 }
98 if (!cmd) {
99 // return usage for all docs
100 return Object.keys(apidocs).map(function (name) {
101 if (name == '_')
102 return mdm.usage(apidocs[name], null, { nameWidth: 20 })
103
104 var text = mdm.usage(apidocs[name], null, { prefix: name, nameWidth: 20 })
105 return text.slice(text.indexOf('Commands:') + 10) // skip past the toplevel summary, straight to the cmd list
106 }).join('\n\n')
107 }
108 // toplevel cmd usage
109 cmd = cmdAliases[cmd] || cmd
110 return mdm.usage(apidocs._, cmd)
111}
112
113module.exports = SecretStack({
114 appKey: require('./lib/ssb-cap')
115})
116.use(SSB)
117
118

Built with git-ssb-web