-
Notifications
You must be signed in to change notification settings - Fork 75
/
index.js
197 lines (176 loc) · 6.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var create = require('./create')
var ssbKeys = require('ssb-keys')
var path = require('path')
var os = require('os')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var valid = require('./lib/validators')
var version = require('./package.json').version
var help = require('./help')
const pull = require('pull-stream')
const pullNotify = require('pull-notify')
const pullCat = require('pull-cat')
function isString(s) { return 'string' === typeof s }
function isObject(o) { return 'object' === typeof o }
function isFunction (f) { return 'function' === typeof f }
var manifest = {
add: 'async',
createFeedStream: 'source',
createHistoryStream: 'source',
createLogStream: 'source',
createRawLogStream: 'source',
createSequenceStream: 'source',
createUserStream: 'source',
createWriteStream: 'sink',
del: 'async',
get: 'async',
getLatest: 'async',
getVectorClock: 'async',
help: 'sync',
latest: 'source',
getFeedState: 'async',
latestSequence: 'async',
links: 'source',
messagesByType: 'source',
progress: 'sync',
publish: 'async',
rebuild: 'async',
status: 'sync',
version: 'sync',
whoami: 'sync',
}
module.exports = {
manifest: manifest,
permissions: {
master: {allow: null, deny: null},
anonymous: {allow: ['createHistoryStream'], deny: null}
},
init: function (api, opts) {
// .temp: use a /tmp data directory
// (useful for testing)
if(opts.temp) {
var name = isString(opts.temp) ? opts.temp : ''+Date.now()
opts.path = path.join(os.tmpdir(), name)
rimraf.sync(opts.path)
}
// load/create secure scuttlebutt data directory
mkdirp.sync(opts.path)
if(!opts.keys)
opts.keys = ssbKeys.generate('ed25519', opts.seed && Buffer.from(opts.seed, 'base64'))
if(!opts.path)
throw new Error('opts.path *must* be provided, or use opts.temp=name to create a test instance')
// main interface
var ssb = create(opts.path, opts, opts.keys)
//treat the main feed as remote, because it's likely handled like that by others.
var feed = ssb.createFeed(opts.keys, {remote: true})
var secretStackClose = api.close
function close (arg, cb) {
if('function' === typeof arg) cb = arg
ssb.flush(function (err) {
if(err) return cb(err)
// override to close the SSB database
continueClose()
})
function continueClose () {
if (ssb.rebuild.isActive) return setTimeout(continueClose, 100)
ssb.close(function (err) {
if (err) return cb(err)
//multiserver doesn't take a callback on close.
secretStackClose((err) => {
setImmediate(() => {
if (typeof cb === 'function') {
if (typeof err === 'object' && err !== null && err.code === 'ERR_SERVER_NOT_RUNNING') {
// https://github.com/ssbc/ssb-db/issues/300
err = null
}
cb(err)
}
})
})
})
}
}
function since () {
var plugs = {}
var sync = true
for (var name in ssb.views) {
plugs[name] = ssb.views[name].since.value
sync = sync && (plugs[name] === ssb.since.value)
}
return {
since: ssb.since.value,
plugins: plugs,
sync: sync
}
}
var self
// When `since` changes we want to send the new value to our instance of
// pull-notify so that the value can be streamed to any listeners (if they
// exist). Listeners are created by calling `createSequenceStream()` and are
// automatically removed when the stream closes.
const sequenceNotifier = pullNotify()
ssb.since(sequenceNotifier)
return self = {
keys : opts.keys,
id : feed.id,
whoami : () => {
return { id: feed.id }
},
version : () => version,
ready : () => ssb.ready.value,
progress : () => ssb.progress,
status : () => {
return {
progress: ssb.progress,
db: ssb.status,
sync: since()
}
},
//temporary!
_flumeUse : function (name, flumeview) {
ssb.use(name, flumeview)
return ssb[name]
},
close : close,
del : valid.async(ssb.del, 'msgLink|feedId'),
publish : valid.async(feed.add, 'string|msgContent'),
add : valid.async(ssb.add, 'msg'),
queue : valid.async(ssb.queue, 'msg'),
get : valid.async(ssb.get, 'msgLink|number|object'),
post : ssb.post,
addMap : ssb.addMap,
since : since,
latest : ssb.latest,
getFeedState : valid.async(ssb.getFeedState, 'feedId'),
getLatest : valid.async(ssb.getLatest, 'feedId'),
latestSequence : valid.async(ssb.latestSequence, 'feedId'),
createFeed : ssb.createFeed,
createFeedStream : valid.source(ssb.createFeedStream, 'readStreamOpts?'),
createHistoryStream : valid.source(ssb.createHistoryStream, ['createHistoryStreamOpts'], ['feedId', 'number?', 'boolean?']),
createLogStream : valid.source(ssb.createLogStream, 'readStreamOpts?'),
createRawLogStream : valid.source(ssb.createRawLogStream, 'readStreamOpts?'),
createUserStream : valid.source(ssb.createUserStream, 'createUserStreamOpts'),
createSequenceStream : () => {
// If the initial value is `undefined` we want it to be `-1`.
// This is because `-1` is a magic sequence number for an empty log.
const initialValue = ssb.since.value !== undefined
? ssb.since.value
: -1
return pullCat([
pull.values([initialValue]),
sequenceNotifier.listen()
])
},
links : valid.source(ssb.links, 'linksOpts'),
// sublevel : ssb.sublevel, // Disabled as does not appear to be used
messagesByType : valid.source(ssb.messagesByType, 'string|messagesByTypeOpts'),
createWriteStream : ssb.createWriteStream,
getVectorClock : ssb.getVectorClock,
getAtSequence : ssb.getAtSequence,
addBoxer : ssb.addBoxer,
addUnboxer : ssb.addUnboxer,
rebuild : ssb.rebuild,
help : () => help
}
}
}