-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
204 lines (176 loc) · 5.1 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
198
199
200
201
202
203
204
const debug = require('debug')('db')
const pretty = require('pretty-hash')
const thunky = require('thunky')
const Group = require('./group')
const { uuid, sink, noop } = require('./lib/util')
const createKvView = require('./views/kv')
const createRecordsView = require('./views/records')
const createIndexView = require('./views/indexes')
const Record = require('./lib/record')
const Schema = require('./lib/schema')
const FEED_TYPE = 'kappa-records'
const SCHEMA_SOURCE = 'core/source'
const bind = (obj, fn) => fn.bind(obj)
module.exports = defaultDatabase
module.exports.Group = Group
function defaultDatabase (opts = {}) {
opts.swarmMode = opts.swarmMode || 'rootfeed'
// opts.swarmMode = 'multifeed'
const group = new Group(opts)
const db = new Database(group, opts)
group.put = bind(db, db.put)
group.del = bind(db, db.del)
group.putSchema = bind(db, db.putSchema)
group.getSchema = bind(db, db.getSchema)
group.getSchemas = bind(db, db.getSchemas)
group.putSource = bind(db, db.putSource)
group.db = db
if (opts.swarmMode === 'rootfeed') {
const sources = new Sources(group, {
onput: bind(db, db.put),
onsource: bind(group, group.addFeed)
})
group.putSource = bind(sources, sources.put)
sources.open(noop)
} else {
group.putSource = function (key, info = {}, cb) {
if (typeof info === 'function') {
cb = info
info = {}
}
group.addFeed({ key, ...info }, cb)
}
}
return group
}
class Database {
constructor (group, opts) {
this.group = group
this.opts = opts
this.schemas = new Schema()
this.group.registerFeedType(FEED_TYPE, {
onopen: this._onopen.bind(this),
onload: this._onload.bind(this),
onappend: this._onappend.bind(this)
})
const viewOpts = { schemas: this.schemas }
this.group.use('kv', createKvView)
this.group.use('records', createRecordsView, viewOpts)
this.group.use('index', createIndexView, viewOpts)
}
_onopen (cb) {
this.schemas.open(this.group, cb)
}
_onload (message, opts, cb) {
const { key, seq, lseq, value, feedType } = message
const record = Record.decode(value, { key, seq, lseq, feedType })
cb(null, record)
}
_onappend (record, opts, cb) {
if (!record.schema) return cb(new Error('schema is required'))
if (record.op === undefined) record.op = Record.PUT
if (record.op === 'put') record.op = Record.PUT
if (record.op === 'del') record.op = Record.DEL
if (!record.id) record.id = uuid()
record.schema = this.schemas.resolveName(record.schema)
if (record.op === Record.PUT) {
let validate = false
if (this.opts.validate) validate = true
if (typeof opts.validate !== 'undefined') validate = !!opts.validate
if (validate) {
if (!this.schemas.validate(record)) return cb(this.schemas.error)
}
}
record.timestamp = Date.now()
this.group.view.kv.getLinks(record, (err, links) => {
if (err && err.status !== 404) return cb(err)
record.links = links || []
const buf = Record.encode(record)
cb(null, buf, record.id)
})
}
put (record, opts, cb) {
record.op = Record.PUT
this.group.append(record, opts, cb)
}
del ({ id, schema }, opts, cb) {
const record = {
id,
schema,
op: Record.DEL
}
this.group.append(record, opts, cb)
}
putSchema (name, schema, cb) {
this.group.ready(() => {
const value = this.schemas.parseSchema(name, schema)
if (!value) return cb(this.schemas.error)
const record = {
schema: 'core/schema',
value
}
this.schemas.put(value)
this.group.put(record, cb)
})
}
getSchema (name) {
return this.schemas.get(name)
}
getSchemas () {
return this.schemas.list()
}
putSource (key, info = {}, cb) {
// opts should/can include: { alias }
if (typeof info === 'function') {
cb = info
info = {}
}
if (Buffer.isBuffer(key)) key = key.toString('hex')
this.put({
schema: SCHEMA_SOURCE,
id: key,
value: {
type: FEED_TYPE,
key,
...info
}
}, cb)
}
}
class Sources {
constructor (group, handlers) {
this.group = group
this.handlers = handlers
}
open (cb) {
const qs = this.group.createQueryStream('records', { schema: 'core/source' }, { live: true })
qs.once('sync', cb)
qs.pipe(sink((record, next) => {
const { alias, key, type, ...info } = record.value
if (type !== FEED_TYPE) return next()
debug('[%s] source:add key %s alias %s type %s', this.group._name, pretty(key), alias, type)
const opts = { alias, key, type, info }
this.handlers.onsource(opts)
next()
}))
}
put (key, opts = {}, cb) {
// opts should/can include: { alias }
if (typeof opts === 'function') {
cb = opts
opts = {}
}
if (Buffer.isBuffer(key)) key = key.toString('hex')
const record = {
schema: 'core/source',
id: key,
value: {
type: FEED_TYPE,
key,
...opts
}
}
this.handlers.onput(record, cb)
}
}
module.exports.Database = Database