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