This repository has been archived by the owner on Jul 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
319 lines (266 loc) · 8.06 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const p = require('path')
const fs = require('fs-extra')
const level = require('level')
const sub = require('subleveldown')
const prefixer = require('sublevel-prefixer')
const hypercore = require('hypercore')
const crypto = require('hypercore/lib/crypto')
const datEncoding = require('dat-encoding')
const mkdirp = require('mkdirp')
const Replicator = require('./lib/replicator.js')
const messages = require('./lib/messages.js')
const KEY_PREFIX = 'key'
const DKEY_PREFIX = 'dkey'
const NAME_PREFIX = 'name'
const prefix = prefixer()
module.exports = Corestore
function Corestore (dir, opts = {}) {
if (!(this instanceof Corestore)) return new Corestore(dir, opts)
this._opts = opts
this.dir = dir
this._root = p.join(dir, 'cores')
this._opened = false
if (!(opts.network && opts.network.disable)) {
this._replicator = Replicator(this, opts.network)
} else {
this._noNetwork = true
}
// Set in ready.
this._metadata = null
this._metadataByDKey = null
this._metadataByName = null
this._metadataByKey = null
this.coresByKey = new Map()
this.coresByDKey = new Map()
this._ready = new Promise(async (resolve, reject) => {
mkdirp(dir, async err => {
if (err) return reject(err)
this._metadata = level(p.join(dir, 'metadata'), {
keyEncoding: 'utf-8',
valueEncoding: 'binary'
})
this._metadataByName = sub(this._metadata, NAME_PREFIX, { valueEncoding: 'binary' })
this._metadataByDKey = sub(this._metadata, DKEY_PREFIX, { valueEncoding: 'binary' })
this._metadataByKey = sub(this._metadata, KEY_PREFIX, { valueEncoding: 'binary' })
try {
if (!(opts.network && opts.network.disable)) {
await this._seedAllCores()
}
} catch (err) {
return reject(err)
}
this._opened = true
return resolve()
})
})
this.ready = cb => {
if (!cb) return this._ready
this._ready.then(() => {
return cb()
}).catch(err => {
return cb(err)
})
}
}
Corestore.prototype._path = function (key) {
return p.join(this._root, key)
}
Corestore.prototype._cacheCore = function (core) {
this.coresByKey.set(ensureString(core.key), core)
this.coresByDKey.set(ensureString(core.discoveryKey), core)
}
Corestore.prototype._removeCachedCore = function (core) {
this.coresByKey.delete(ensureString(core.key))
this.coresByDKey.delete(ensureString(core.discoveryKey))
}
Corestore.prototype._getCachedCore = function (key) {
let keyString = ensureString(key)
return this.coresByKey.get(keyString)
}
Corestore.prototype._seedAllCores = async function () {
return new Promise((resolve, reject) => {
let stream = this._metadataByKey.createReadStream()
stream.on('error', err => reject(err))
stream.on('end', () => resolve())
stream.on('data', ({ value }) => {
try {
let info = messages.Core.decode(value)
if (info.seed) {
this._replicator.add(info.discoveryKey)
}
} catch (err) {
return reject(err)
}
})
})
}
Corestore.prototype._create = function (key, opts = {}) {
let keyString = ensureString(key)
let core = hypercore(this._path(keyString), key, opts)
core.on('close', () => {
this._removeCachedCore(core)
this._unseed(core)
})
let ready = core.ready.bind(core)
core.ready = (cb) => {
return new Promise((resolve, reject) => {
ready(err => {
if (err) return reject(err)
let dKey = ensureString(core.discoveryKey)
let key = ensureString(core.key)
this._cacheCore(core)
opts.key = core.key
opts.discoveryKey = core.discoveryKey
let info = messages.Core.encode(opts)
let batch = [
{ type: 'put', key: prefix(KEY_PREFIX, key), value: info },
{ type: 'put', key: prefix(DKEY_PREFIX, dKey), value: key }
]
if (opts.name) batch.push({ type: 'put', key: prefix(NAME_PREFIX, opts.name), value: key })
this._metadata.batch(batch, err => {
if (err) return reject(err)
if (opts.seed) {
this._seed(core)
}
return resolve()
})
})
}).then(() => {
if (cb) return cb()
}).catch(err => {
if (cb) return cb(err)
})
}
return core
}
Corestore.prototype._seed = function (core) {
if (!this._noNetwork) {
this._replicator.add(core.discoveryKey)
core.isSwarming = true
}
}
Corestore.prototype._unseed = function (core) {
if (!this._noNetwork) {
this._replicator.remove(core)
core.isSwarming = false
}
}
Corestore.prototype._getSeedCore = async function (dKey, opts) {
let info = await this.info(ensureString(dKey), { dkey: true })
if (!info) return null
if (!info.seed) return null
let core = this._getCachedCore(info.key)
if (core) return core
core = this.get(info.key, opts)
await core.ready()
return core
}
Corestore.prototype.info = async function (key, opts = {}) {
try {
if (opts.name) {
key = await this._metadataByName.get(key)
} else if (opts.dkey) {
key = ensureString(key)
key = await this._metadataByDKey.get(key)
} else {
key = ensureString(key)
}
let value = await this._metadataByKey.get(key)
return messages.Core.decode(value)
} catch (err) {
if (err.notFound) return null
throw err
}
}
Corestore.prototype.get = function (key, opts) {
if (typeof key === 'object' && !(key instanceof Buffer)) {
opts = key
key = null
}
opts = opts || {}
opts.seed = opts.seed !== undefined ? opts.seed : true
opts.sparse = opts.sparse !== undefined ? opts.sparse : true
if (!key) opts.valueEncoding = opts.valueEncoding || 'binary'
if (key) {
let existing = this._getCachedCore(key)
if (existing) return existing
} else {
let { publicKey, secretKey } = opts.keyPair || crypto.keyPair()
opts.secretKey = secretKey
opts.writable = true
key = publicKey
}
let core = this._create(key, opts)
opts.writable = core.writable
return core
}
Corestore.prototype.getByName = async function (name, opts) {
let info = await this.info(name, { name: true })
if (!info) return null
let core = this.get(info.key, opts)
await core.ready()
return core
}
Corestore.prototype.update = async function (key, opts) {
let keyString = ensureString(key)
let info = await this.info(key)
if (!info) throw new Error('Cannot update a nonexistent core.')
let core = await this.get(key)
if (opts.seed !== undefined && !opts.seed) {
await this._unseed(core)
}
Object.assign(info, opts)
this._metadataByKey.put(keyString, messages.Core.encode(info))
}
Corestore.prototype.delete = async function (key) {
key = ensureString(key)
let info = await this.info(key)
if (!info) throw new Error('Cannot delete a nonexistent core')
let core = this.get(key)
if (info.seed && !this._noNetwork) {
await this._unseed(core)
}
return new Promise(async (resolve, reject) => {
core.close(async err => {
if (err) return reject(err)
try {
// TODO: Delete discovery key pointers too.
let batch = [
{ type: 'del', key: prefix(KEY_PREFIX, key) }
]
if (info.name) batch.push({ type: 'del', key: prefix(NAME_PREFIX, info.name) })
await this._metadata.batch(batch)
await fs.remove(this._path(key))
this._removeCachedCore(core)
return resolve()
} catch (err) {
return reject(err)
}
})
})
}
Corestore.prototype.list = async function () {
return new Promise((resolve, reject) => {
let result = new Map()
let stream = this._metadataByKey.createReadStream()
stream.on('data', ({ key, value }) => {
result.set(key, messages.Core.decode(value))
})
stream.on('end', () => {
return resolve(result)
})
stream.on('error', err => {
return reject(err)
})
})
}
Corestore.prototype.close = async function () {
let self = this
if (!this._opened) return
let tasks = [self._metadata.close()]
if (self._replicator) tasks.push(self._replicator.stop())
return Promise.all(tasks)
}
function ensureString (key) {
return datEncoding.toStr(key)
}