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