git ssb

1+

Dominic / secure-scuttlebutt



Tree: ce0193e6fd7e6dfc8a6db20584bc72fa4886e719

Files: ce0193e6fd7e6dfc8a6db20584bc72fa4886e719 / plugin.js

6266 bytesRaw
1var SecretStack = require('secret-stack')
2var create = require('./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')
9//var cmdAliases = require('./lib/cli-cmd-aliases')
10var valid = require('./lib/validators')
11//var apidocs = require('./lib/apidocs.js')
12var pkg = require('./package.json')
13
14function isString(s) { return 'string' === typeof s }
15function isObject(o) { return 'object' === typeof o }
16function isFunction (f) { return 'function' === typeof f }
17// create SecretStack definition
18var fs = require('fs')
19var manifest = mdm.manifest(fs.readFileSync(path.join(__dirname, 'api.md'), 'utf-8'))
20
21manifest.seq = 'async'
22manifest.usage = 'sync'
23manifest.clock = 'async'
24manifest.version = 'sync'
25
26console.log('manifest', manifest)
27
28module.exports = {
29 manifest: manifest,
30 permissions: {
31 master: {allow: null, deny: null},
32 anonymous: {allow: ['createHistoryStream'], deny: null}
33 },
34 init: function (api, opts) {
35
36 // .temp: use a /tmp data directory
37 // (useful for testing)
38 if(opts.temp) {
39 var name = isString(opts.temp) ? opts.temp : ''+Date.now()
40 opts.path = path.join(osenv.tmpdir(), name)
41 rimraf.sync(opts.path)
42 }
43
44 // load/create secure scuttlebutt data directory
45 mkdirp.sync(opts.path)
46
47 if(!opts.keys)
48 opts.keys = ssbKeys.generate('ed25519', opts.seed && Buffer.from(opts.seed, 'base64'))
49
50 if(!opts.path)
51 throw new Error('opts.path *must* be provided, or use opts.temp=name to create a test instance')
52
53 // main interface
54 var ssb = create(opts.path, opts, opts.keys)
55 //treat the main feed as remote, because it's likely handled like that by others.
56 var feed = ssb.createFeed(opts.keys, {remote: true})
57 var _close = api.close
58 var close = function (arg, cb) {
59 if('function' === typeof arg) cb = arg
60 // override to close the SSB database
61 ssb.close(function (err) {
62 if (err) throw err
63 console.log("fallback to close")
64 _close(cb) //multiserver doesn't take a callback on close.
65 })
66 }
67
68 function since () {
69 var plugs = {}
70 var sync = true
71 for(var k in ssb) {
72 if(ssb[k] && isObject(ssb[k]) && isFunction(ssb[k].since)) {
73 plugs[k] = ssb[k].since.value
74 sync = sync && (plugs[k] === ssb.since.value)
75 }
76 }
77 return {
78 since: ssb.since.value,
79 plugins: plugs,
80 sync: sync,
81 }
82 }
83 var self
84 return self = {
85 id : feed.id,
86 keys : opts.keys,
87
88 ready : function () {
89 return ssb.ready.value
90 },
91
92 progress : function () {
93 return ssb.progress
94 },
95
96 status : function () {
97 return {progress: self.progress(), db: ssb.status, sync: since() }
98 },
99
100 version : function () {
101 return pkg.version
102 },
103
104 //temporary!
105 _flumeUse :
106 function (name, flumeview) {
107 ssb.use(name, flumeview)
108 return ssb[name]
109 },
110
111 // usage : valid.sync(usage, 'string?|boolean?'),
112 close : valid.async(close),
113
114 publish : valid.async(feed.add, 'string|msgContent'),
115 add : valid.async(ssb.add, 'msg'),
116 queue : valid.async(ssb.queue, 'msg'),
117 get : valid.async(ssb.get, 'msgLink|number|object'),
118
119 post : ssb.post,
120 addMap : ssb.addMap,
121
122 since : since,
123
124 getPublicKey : ssb.getPublicKey,
125 latest : ssb.latest,
126 getLatest : valid.async(ssb.getLatest, 'feedId'),
127 latestSequence : valid.async(ssb.latestSequence, 'feedId'),
128 createFeed : ssb.createFeed,
129 whoami : function () { return { id: feed.id } },
130 query : ssb.query,
131 createFeedStream : valid.source(ssb.createFeedStream, 'readStreamOpts?'),
132 createHistoryStream : valid.source(ssb.createHistoryStream, ['createHistoryStreamOpts'], ['feedId', 'number?', 'boolean?']),
133 createLogStream : valid.source(ssb.createLogStream, 'readStreamOpts?'),
134 createUserStream : valid.source(ssb.createUserStream, 'createUserStreamOpts'),
135 links : valid.source(ssb.links, 'linksOpts'),
136 sublevel : ssb.sublevel,
137 messagesByType : valid.source(ssb.messagesByType, 'string|messagesByTypeOpts'),
138 createWriteStream : ssb.createWriteStream,
139 getVectorClock : ssb.getVectorClock,
140 getAtSequence : ssb.getAtSequence,
141 addUnboxer : ssb.addUnboxer,
142 box : ssb.box,
143 }
144 }
145}
146
147/*
148// live help RPC method
149function usage (cmd) {
150 var path = (cmd||'').split('.')
151 if ((path[0] && apidocs[path[0]]) || (cmd && apidocs[cmd])) {
152 // return usage for the plugin
153 cmd = path.slice(1).join('.')
154 return mdm.usage(apidocs[path[0]], cmd, { prefix: path[0] })
155 }
156 if (!cmd) {
157 // return usage for all docs
158 return Object.keys(apidocs).map(function (name) {
159 if (name == '_')
160 return mdm.usage(apidocs[name], null, { nameWidth: 20 })
161
162 var text = mdm.usage(apidocs[name], null, { prefix: name, nameWidth: 20 })
163 return text.slice(text.indexOf('Commands:') + 10) // skip past the toplevel summary, straight to the cmd list
164 }).join('\n\n')
165 }
166 // toplevel cmd usage
167 cmd = cmdAliases[cmd] || cmd
168 return mdm.usage(apidocs._, cmd)
169}
170*/
171
172//function createSsbServer() {
173// return SecretStack({
174// //this is just the default app key.
175// //it can be overridden by passing a appKey as option
176// //when creating a SsbServer instance.
177// appKey: require('./lib/ssb-cap')
178// })
179// .use(SSB)
180//}
181//module.exports = createSsbServer()
182//module.exports.createSsbServer = createSsbServer
183
184
185
186
187

Built with git-ssb-web