-
Notifications
You must be signed in to change notification settings - Fork 75
/
create.js
210 lines (181 loc) · 5.14 KB
/
create.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
'use strict'
var join = require('path').join
var EventEmitter = require('events')
var pull = require('pull-stream')
var ref = require('ssb-ref')
var ssbKeys = require('ssb-keys')
var createDB = require('./db')
var extras = require('./extras')
var u = require('./util')
function isString (s) {
return typeof s === 'string'
}
function errorCB (err) {
if (err) throw err
}
module.exports = function create (path, opts, keys) {
// _ was legacy db. removed that, but for backwards compatibilty reasons do not change interface
if (!path) throw new Error('path must be provided')
keys = keys || ssbKeys.generate()
var db = createDB(join(opts.path || path, 'flume'), keys, opts)
// UGLY HACK, but...
// fairly sure that something up the stack expects ssb to be an event emitter.
db.__proto__ = new EventEmitter() // eslint-disable-line
db.opts = opts
var _get = db.get
var _del = db.del
// pull in the features that are needed to pass the tests
// and that sbot, etc uses but are slow.
extras(db, opts, keys)
// - adds indexes: links, feed, time
// - adds methods:
// - db.createLogStream
// - db.createFeedStream
// - db.latest
// - db.latestSequence
// - db.getLatest
db.get = function (key, cb) {
let doUnbox = false
let readKey
let meta = false
if (typeof key === 'object') {
doUnbox = key.private === true
readKey = key.unbox
meta = key.meta
key = key.id
}
if (isMsgWithQuery(key)) {
var link = ref.parseLink(key)
return db.get({
id: link.link,
private: true,
unbox: link.query.unbox.replace(/\s/g, '+'),
meta: link.query.meta
}, cb)
}
if (ref.isMsg(key)) {
return db.keys.get(key, function (err, data, offset) {
if (err) return cb(err)
var value
if (!doUnbox) value = u.originalValue(data.value)
else {
if (isUnboxed(data)) value = data.value
else {
const unboxed = db._unbox(data, readKey)
if (unboxed) value = unboxed.value
}
}
if (!value) value = data.value // if db._unbox fails value will be undefined
if (meta) cb(null, { key, value, timestamp: data.timestamp }, offset)
else cb(null, value, offset)
})
}
else if (Number.isInteger(key)) _get(key, cb) // seq
else {
throw new Error('ssb-db.get: key *must* be a ssb message id or a flume offset')
}
}
db.add = function (msg, cb) {
db.queue(msg, function (err, data) {
if (err) cb(err)
else db.flush(function () { cb(null, data) })
})
}
db.createFeed = function (keys) {
if (!keys) keys = ssbKeys.generate()
function add (content, cb) {
// LEGACY: hacks to support add as a continuable
if (!cb) { return function (cb) { add(content, cb) } }
db.append({ content: content, keys: keys }, cb)
}
return {
add: add,
publish: add,
id: keys.id,
keys: keys
}
}
db.createRawLogStream = function (opts) {
return pull(
db.stream(opts),
pull.map(({ seq, value }) => {
return { seq, value: u.originalData(value) }
})
)
}
// writeStream - used in (legacy) replication.
db.createWriteStream = function (cb) {
cb = cb || errorCB
return pull(
pull.asyncMap(function (data, cb) {
db.queue(data, function (err, msg) {
if (err) {
db.emit('invalid', err, msg)
}
setImmediate(cb)
})
}),
pull.drain(null, function (err) {
if (err) return cb(err)
db.flush(cb)
})
)
}
/* via clock index */
db.createHistoryStream = db.clock.createHistoryStream
db.createUserStream = db.clock.createUserStream
// called with [id, seq] or "<id>:<seq>"
db.getAtSequence = function (seqid, cb) {
// will NOT expose private plaintext
db.clock.get(isString(seqid) ? seqid.split(':') : seqid, function (err, value) {
if (err) cb(err)
else cb(null, u.originalData(value))
})
}
/* via last index */
db.getVectorClock = function (_, cb) {
if (!cb) cb = _
db.last.get(function (err, h) {
if (err) return cb(err)
var clock = {}
for (var k in h) { clock[k] = h[k].sequence }
cb(null, clock)
})
}
/* via clock + keys */
const deleteFeed = (feed, cb) => {
pull(
db.createUserStream({ id: feed }),
pull.asyncMap((msg, cb) => {
const key = msg.key
deleteMessage(key, (err) => {
cb(err, key)
})
}),
pull.collect(cb)
)
}
const deleteMessage = (key, cb) => {
db.keys.get(key, (err, val, seq) => {
if (err) return cb(err)
if (seq == null) return cb(new Error('seq is null!'))
_del(seq, cb)
})
}
db.del = (target, cb) => {
if (ref.isMsg(target)) {
deleteMessage(target, cb)
} else if (ref.isFeed(target)) {
deleteFeed(target, cb)
} else {
cb(new Error('deletion target must be a message or feed'))
}
}
return db
}
function isMsgWithQuery (key) {
return key.length > 52 && ref.isMsgLink(key)
}
function isUnboxed (data) {
return typeof data.value.content === 'object'
}