Files: 8e8434f441e3e179672053fbde29570a4fcf6981 / index.js
4111 bytesRaw
1 | var create = require('./create') |
2 | var ssbKeys = require('ssb-keys') |
3 | var path = require('path') |
4 | var osenv = require('osenv') |
5 | var mkdirp = require('mkdirp') |
6 | var rimraf = require('rimraf') |
7 | var valid = require('./lib/validators') |
8 | var pkg = require('./package.json') |
9 | var manifest = require('./manifest.json') |
10 | |
11 | function isString(s) { return 'string' === typeof s } |
12 | function isObject(o) { return 'object' === typeof o } |
13 | function isFunction (f) { return 'function' === typeof f } |
14 | // create SecretStack definition |
15 | var fs = require('fs') |
16 | |
17 | manifest.seq = 'async' |
18 | manifest.usage = 'sync' |
19 | manifest.clock = 'async' |
20 | manifest.version = 'sync' |
21 | |
22 | module.exports = { |
23 | manifest: manifest, |
24 | permissions: { |
25 | master: {allow: null, deny: null}, |
26 | anonymous: {allow: ['createHistoryStream'], deny: null} |
27 | }, |
28 | init: function (api, opts) { |
29 | |
30 | // .temp: use a /tmp data directory |
31 | // (useful for testing) |
32 | if(opts.temp) { |
33 | var name = isString(opts.temp) ? opts.temp : ''+Date.now() |
34 | opts.path = path.join(osenv.tmpdir(), name) |
35 | rimraf.sync(opts.path) |
36 | } |
37 | |
38 | // load/create secure scuttlebutt data directory |
39 | mkdirp.sync(opts.path) |
40 | |
41 | if(!opts.keys) |
42 | opts.keys = ssbKeys.generate('ed25519', opts.seed && Buffer.from(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(opts.path, 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 | ssb.flush(function (err) { |
55 | if(err) return cb(err) |
56 | // override to close the SSB database |
57 | ssb.close(function (err) { |
58 | if (err) return cb(err) |
59 | console.log("fallback to close") |
60 | _close(cb) //multiserver doesn't take a callback on close. |
61 | }) |
62 | }) |
63 | } |
64 | |
65 | //calculate status |
66 | function since () { |
67 | var plugs = {} |
68 | var sync = true |
69 | for(var k in ssb) { |
70 | if(ssb[k] && isObject(ssb[k]) && isFunction(ssb[k].since)) { |
71 | plugs[k] = ssb[k].since.value |
72 | sync = sync && (plugs[k] === ssb.since.value) |
73 | } |
74 | } |
75 | return { |
76 | since: ssb.since.value, |
77 | plugins: plugs, |
78 | sync: sync, |
79 | } |
80 | } |
81 | var self |
82 | return self = { |
83 | id : opts.keys.id, |
84 | keys : opts.keys, |
85 | |
86 | ready : function () { |
87 | return ssb.ready.value |
88 | }, |
89 | |
90 | progress : function () { |
91 | return ssb.progress |
92 | }, |
93 | |
94 | status : function () { |
95 | //this ought to be more consistently organized by the name of the thing. |
96 | return {progress: self.progress(), db: ssb.status, sync: since() } |
97 | }, |
98 | |
99 | version : function () { |
100 | return pkg.version |
101 | }, |
102 | |
103 | //temporary!... but became permanent |
104 | _flumeUse : |
105 | function (name, flumeview) { |
106 | ssb.use(name, flumeview) |
107 | return ssb[name] |
108 | }, |
109 | |
110 | close : close, |
111 | |
112 | publish : valid.async(ssb.publish, 'string|msgContent'), |
113 | add : valid.async(ssb.add, 'msg'), |
114 | queue : valid.async(ssb.queue, 'msg'), |
115 | get : valid.async(ssb.get, 'msgLink|number|object'), |
116 | |
117 | post : ssb.post, |
118 | addMap : ssb.addMap, |
119 | |
120 | since : since, |
121 | |
122 | whoami : function () { return { id: opts.keys.id } }, |
123 | createRawLogStream : ssb.createRawLogStream, |
124 | createLogStream : ssb.createRawLogStream, |
125 | getVectorClock : ssb.getVectorClock, |
126 | getAtSequence : ssb.getAtSequence, |
127 | addUnboxer : ssb.addUnboxer, |
128 | box : ssb.box, |
129 | } |
130 | } |
131 | } |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | |
141 | |
142 |
Built with git-ssb-web