corestore 6.5.2 → 6.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +15 -26
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const { EventEmitter } = require('events')
|
|
2
1
|
const safetyCatch = require('safety-catch')
|
|
3
2
|
const crypto = require('hypercore-crypto')
|
|
4
3
|
const sodium = require('sodium-universal')
|
|
5
4
|
const Hypercore = require('hypercore')
|
|
6
5
|
const Xache = require('xache')
|
|
7
6
|
const b4a = require('b4a')
|
|
7
|
+
const ReadyResource = require('ready-resource')
|
|
8
8
|
|
|
9
9
|
const [NS] = crypto.namespace('corestore', 1)
|
|
10
10
|
const DEFAULT_NAMESPACE = b4a.alloc(32) // This is meant to be 32 0-bytes
|
|
@@ -15,13 +15,13 @@ const USERDATA_NAME_KEY = 'corestore/name'
|
|
|
15
15
|
const USERDATA_NAMESPACE_KEY = 'corestore/namespace'
|
|
16
16
|
const POOL_SIZE = 512 // how many open fds to aim for before cycling them
|
|
17
17
|
|
|
18
|
-
module.exports = class Corestore extends
|
|
18
|
+
module.exports = class Corestore extends ReadyResource {
|
|
19
19
|
constructor (storage, opts = {}) {
|
|
20
20
|
super()
|
|
21
21
|
|
|
22
22
|
const root = opts._root
|
|
23
23
|
|
|
24
|
-
this.storage = Hypercore.defaultStorage(storage, { lock: PRIMARY_KEY_FILE_NAME, poolSize: opts.poolSize || POOL_SIZE })
|
|
24
|
+
this.storage = Hypercore.defaultStorage(storage, { lock: PRIMARY_KEY_FILE_NAME, poolSize: opts.poolSize || POOL_SIZE, rmdir: true })
|
|
25
25
|
this.cores = root ? root.cores : new Map()
|
|
26
26
|
this.cache = !!opts.cache
|
|
27
27
|
this.primaryKey = opts.primaryKey || null
|
|
@@ -33,6 +33,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
33
33
|
this._root = root || this
|
|
34
34
|
this._replicationStreams = root ? root._replicationStreams : []
|
|
35
35
|
this._overwrite = opts.overwrite === true
|
|
36
|
+
this._readonly = opts.writable === false
|
|
36
37
|
|
|
37
38
|
this._sessions = new Set() // sessions for THIS namespace
|
|
38
39
|
this._rootStoreSessions = new Set()
|
|
@@ -41,14 +42,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
41
42
|
this._findingPeers = []
|
|
42
43
|
|
|
43
44
|
if (this._namespace.byteLength !== 32) throw new Error('Namespace must be a 32-byte Buffer or Uint8Array')
|
|
44
|
-
|
|
45
|
-
this._closing = null
|
|
46
|
-
this._opening = this._open()
|
|
47
|
-
this._opening.catch(safetyCatch)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
ready () {
|
|
51
|
-
return this._opening
|
|
45
|
+
this.ready().catch(safetyCatch)
|
|
52
46
|
}
|
|
53
47
|
|
|
54
48
|
findingPeers () {
|
|
@@ -97,7 +91,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
97
91
|
|
|
98
92
|
async _open () {
|
|
99
93
|
if (this._root !== this) {
|
|
100
|
-
await this._root.
|
|
94
|
+
await this._root.ready()
|
|
101
95
|
if (!this.primaryKey) this.primaryKey = this._root.primaryKey
|
|
102
96
|
if (this._bootstrap) await this._openNamespaceFromBootstrap()
|
|
103
97
|
return
|
|
@@ -179,7 +173,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
179
173
|
}
|
|
180
174
|
|
|
181
175
|
async _preload (opts) {
|
|
182
|
-
if (!this.primaryKey) await this.
|
|
176
|
+
if (!this.primaryKey) await this.ready()
|
|
183
177
|
|
|
184
178
|
const { discoveryKey, keyPair, auth } = await this._generateKeys(opts)
|
|
185
179
|
const id = b4a.toString(discoveryKey, 'hex')
|
|
@@ -219,7 +213,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
219
213
|
: null
|
|
220
214
|
})
|
|
221
215
|
|
|
222
|
-
if (this._root.
|
|
216
|
+
if (this._root.closing) throw new Error('The corestore is closed')
|
|
223
217
|
this.cores.set(id, core)
|
|
224
218
|
core.ready().then(() => {
|
|
225
219
|
if (core.closing) return // extra safety here as ready is a tick after open
|
|
@@ -242,7 +236,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
242
236
|
}
|
|
243
237
|
|
|
244
238
|
async createKeyPair (name, namespace = this._namespace) {
|
|
245
|
-
if (!this.primaryKey) await this.
|
|
239
|
+
if (!this.primaryKey) await this.ready()
|
|
246
240
|
|
|
247
241
|
const keyPair = {
|
|
248
242
|
publicKey: b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES),
|
|
@@ -262,7 +256,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
262
256
|
}
|
|
263
257
|
|
|
264
258
|
get (opts = {}) {
|
|
265
|
-
if (this._root.
|
|
259
|
+
if (this._root.closing) throw new Error('The corestore is closed')
|
|
266
260
|
opts = validateGetOptions(opts)
|
|
267
261
|
|
|
268
262
|
if (opts.cache !== false) {
|
|
@@ -270,6 +264,7 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
270
264
|
}
|
|
271
265
|
|
|
272
266
|
const core = new Hypercore(null, {
|
|
267
|
+
writable: !this._readonly,
|
|
273
268
|
...opts,
|
|
274
269
|
name: null,
|
|
275
270
|
preload: () => this._preload(opts)
|
|
@@ -318,17 +313,18 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
318
313
|
return stream
|
|
319
314
|
}
|
|
320
315
|
|
|
321
|
-
namespace (name) {
|
|
316
|
+
namespace (name, opts) {
|
|
322
317
|
if (name instanceof Hypercore) {
|
|
323
|
-
return this.session({ _bootstrap: name })
|
|
318
|
+
return this.session({ ...opts, _bootstrap: name })
|
|
324
319
|
}
|
|
325
|
-
return this.session({ namespace: generateNamespace(this._namespace, name) })
|
|
320
|
+
return this.session({ ...opts, namespace: generateNamespace(this._namespace, name) })
|
|
326
321
|
}
|
|
327
322
|
|
|
328
323
|
session (opts) {
|
|
329
324
|
const session = new Corestore(this.storage, {
|
|
330
325
|
namespace: this._namespace,
|
|
331
326
|
cache: this.cache,
|
|
327
|
+
writable: !this._readonly,
|
|
332
328
|
_root: this._root,
|
|
333
329
|
...opts
|
|
334
330
|
})
|
|
@@ -364,19 +360,12 @@ module.exports = class Corestore extends EventEmitter {
|
|
|
364
360
|
}
|
|
365
361
|
|
|
366
362
|
async _close () {
|
|
367
|
-
await this._opening
|
|
368
363
|
this._root._rootStoreSessions.delete(this)
|
|
369
364
|
await this._closeNamespace()
|
|
370
365
|
if (this._root === this) {
|
|
371
366
|
await this._closePrimaryNamespace()
|
|
372
367
|
}
|
|
373
368
|
}
|
|
374
|
-
|
|
375
|
-
close () {
|
|
376
|
-
if (this._closing) return this._closing
|
|
377
|
-
this._closing = this._close()
|
|
378
|
-
return this._closing
|
|
379
|
-
}
|
|
380
369
|
}
|
|
381
370
|
|
|
382
371
|
function sign (keyPair, message) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "corestore",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.7.0",
|
|
4
4
|
"description": "A Hypercore factory that simplifies managing collections of cores.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,8 +30,9 @@
|
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"b4a": "^1.3.1",
|
|
33
|
-
"hypercore": "^10.
|
|
33
|
+
"hypercore": "^10.12.0",
|
|
34
34
|
"hypercore-crypto": "^3.2.1",
|
|
35
|
+
"ready-resource": "^1.0.0",
|
|
35
36
|
"safety-catch": "^1.0.1",
|
|
36
37
|
"sodium-universal": "^4.0.0",
|
|
37
38
|
"xache": "^1.1.0"
|