-
Notifications
You must be signed in to change notification settings - Fork 74
/
minimal.js
241 lines (207 loc) · 6.16 KB
/
minimal.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict'
var path = require('path')
var Flume = require('flumedb')
var OffsetLog = require('flumelog-offset')
var codec = require('./codec')
var AsyncWrite = require('async-write')
var V = require('ssb-validate')
var timestamp = require('monotonic-timestamp')
var Obv = require('obv')
var ssbKeys = require('ssb-keys')
var box = ssbKeys.box
var u = require('./util')
var isFeed = require('ssb-ref').isFeed
var isArray = Array.isArray
function isFunction (f) { return typeof f === 'function' }
function unbox (data, unboxers, key) {
var plaintext
if (data && isString(data.value.content)) {
for (var i = 0; i < unboxers.length; i++) {
var unboxer = unboxers[i]
if (isFunction(unboxer)) {
plaintext = unboxer(data.value.content, data.value)
} else {
if (!key) key = unboxer.key(data.value.content, data.value)
if (key) plaintext = unboxer.value(data.value.content, key, data.value)
}
if (plaintext) {
var msg = {}
for (var k in data.value) { msg[k] = data.value[k] }
// set `meta.original.content`
msg.meta = u.metaBackup(msg, 'content')
// modify content now that it's saved at `meta.original.content`
msg.content = plaintext
// set meta properties for private messages
msg.meta.private = true
if (key) { msg.meta.unbox = key.toString('base64') }
// backward-compatibility with previous property location
// this property location may be deprecated in favor of `msg.meta`
msg.cyphertext = msg.meta.original.content
msg.private = msg.meta.private
if (key) { msg.unbox = msg.meta.unbox }
return { key: data.key, value: msg, timestamp: data.timestamp }
}
}
}
return data
}
/*
## queue (msg, cb)
add a message to the log, buffering the write to make it as fast as
possible, cb when the message is queued.
## append (msg, cb)
write a message, callback once it's definitely written.
*/
function isString (s) {
return typeof s === 'string'
}
module.exports = function (dirname, keys, opts) {
var caps = opts && opts.caps || {}
var hmacKey = caps.sign
var mainUnboxer = {
key: function (content) { return ssbKeys.unboxKey(content, keys) },
value: function (content, key) { return ssbKeys.unboxBody(content, key) }
}
var unboxers = [ mainUnboxer ]
var log = OffsetLog(path.join(dirname, 'log.offset'), { blockSize: 1024 * 16, codec })
const unboxerMap = (msg, cb) => cb(null, db.unbox(msg))
const maps = [ unboxerMap ]
const chainMaps = (val, cb) => {
// assumes `maps.length >= 1`
if (maps.length === 1) {
maps[0](val, cb)
} else {
let idx = -1 // haven't entered the chain yet
const next = (err, val) => {
idx += 1
if (err || idx === maps.length) {
cb(err, val)
} else {
maps[idx](val, next)
}
}
next(null, val)
}
}
// NOTE: must use db.ready.set(true) at when migration is complete
// false says the database is not ready yet!
var db = Flume(log, true, chainMaps)
.use('last', require('./indexes/last')())
var state = V.initial()
var ready = false
var waiting = []
var flush = []
var append = db.rawAppend = db.append
db.post = Obv()
var queue = AsyncWrite(function (_, cb) {
var batch = state.queue
state.queue = []
append(batch, function (err, v) {
batch.forEach(function (data) {
db.post.set(u.originalData(data))
})
cb(err, v)
})
}, function reduce (_, msg) {
return V.append(state, hmacKey, msg)
}, function (_state) {
return state.queue.length > 1000
}, function isEmpty (_state) {
return !state.queue.length
}, 100)
queue.onDrain = function () {
if (state.queue.length === 0) {
var l = flush.length
for (var i = 0; i < l; ++i) { flush[i]() }
flush = flush.slice(l)
}
}
db.last.get(function (_, last) {
// copy to so we avoid weirdness, because this object
// tracks the state coming in to the database.
for (var k in last) {
state.feeds[k] = {
id: last[k].id,
timestamp: last[k].ts || last[k].timestamp,
sequence: last[k].sequence,
queue: []
}
}
ready = true
var l = waiting.length
for (var i = 0; i < l; ++i) { waiting[i]() }
waiting = waiting.slice(l)
})
function wait (fn) {
return function (value, cb) {
if (ready) fn(value, cb)
else {
waiting.push(function () {
fn(value, cb)
})
}
}
}
db.queue = wait(function (msg, cb) {
queue(msg, function (err) {
var data = state.queue[state.queue.length - 1]
if (err) cb(err)
else cb(null, data)
})
})
db.append = wait(function (opts, cb) {
try {
var msg = V.create(
state.feeds[opts.keys.id],
opts.keys, opts.hmacKey || hmacKey,
db.box(opts, state.feeds[opts.keys.id]),
timestamp()
)
} catch (err) {
cb(err)
return
}
queue(msg, function (err) {
if (err) return cb(err)
var data = state.queue[state.queue.length - 1]
flush.push(function () {
cb(null, data)
})
})
})
db.buffer = function () {
return queue.buffer
}
db.flush = function (cb) {
// maybe need to check if there is anything currently writing?
if (!queue.buffer || !queue.buffer.queue.length && !queue.writing) cb()
else flush.push(cb)
}
db.addUnboxer = function (unboxer) {
unboxers.push(unboxer)
}
db.unbox = function (data, key) {
return unbox(data, unboxers, key)
}
db.box = function (opts, state) {
var content = opts.content
var recps = opts.content.recps
if (recps) {
const isNonEmptyArrayOfFeeds = isArray(recps) && recps.every(isFeed) && recps.length > 0
if (isFeed(recps) || isNonEmptyArrayOfFeeds) {
recps = opts.content.recps = [].concat(recps) // force to array
return box(opts.content, recps)
} else {
throw new Error(
'private message recipients must be valid feeds, '+
'was:' + JSON.stringify(recps)
)
}
}
return opts.content
}
db.addMap = function (fn) {
maps.push(fn)
}
return db
}