corestore 6.5.1 → 6.6.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.
Files changed (2) hide show
  1. package/index.js +19 -24
  2. 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 EventEmitter {
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
@@ -35,19 +35,13 @@ module.exports = class Corestore extends EventEmitter {
35
35
  this._overwrite = opts.overwrite === true
36
36
 
37
37
  this._sessions = new Set() // sessions for THIS namespace
38
+ this._rootStoreSessions = new Set()
38
39
 
39
40
  this._findingPeersCount = 0
40
41
  this._findingPeers = []
41
42
 
42
43
  if (this._namespace.byteLength !== 32) throw new Error('Namespace must be a 32-byte Buffer or Uint8Array')
43
-
44
- this._closing = null
45
- this._opening = this._open()
46
- this._opening.catch(safetyCatch)
47
- }
48
-
49
- ready () {
50
- return this._opening
44
+ this.ready().catch(safetyCatch)
51
45
  }
52
46
 
53
47
  findingPeers () {
@@ -63,6 +57,11 @@ module.exports = class Corestore extends EventEmitter {
63
57
 
64
58
  _emitCore (name, core) {
65
59
  this.emit(name, core)
60
+ for (const session of this._root._rootStoreSessions) {
61
+ if (session !== this) {
62
+ session.emit(name, core)
63
+ }
64
+ }
66
65
  if (this !== this._root) this._root.emit(name, core)
67
66
  }
68
67
 
@@ -91,7 +90,7 @@ module.exports = class Corestore extends EventEmitter {
91
90
 
92
91
  async _open () {
93
92
  if (this._root !== this) {
94
- await this._root._opening
93
+ await this._root.ready()
95
94
  if (!this.primaryKey) this.primaryKey = this._root.primaryKey
96
95
  if (this._bootstrap) await this._openNamespaceFromBootstrap()
97
96
  return
@@ -173,7 +172,7 @@ module.exports = class Corestore extends EventEmitter {
173
172
  }
174
173
 
175
174
  async _preload (opts) {
176
- if (!this.primaryKey) await this._opening
175
+ if (!this.primaryKey) await this.ready()
177
176
 
178
177
  const { discoveryKey, keyPair, auth } = await this._generateKeys(opts)
179
178
  const id = b4a.toString(discoveryKey, 'hex')
@@ -213,7 +212,7 @@ module.exports = class Corestore extends EventEmitter {
213
212
  : null
214
213
  })
215
214
 
216
- if (this._root._closing) throw new Error('The corestore is closed')
215
+ if (this._root.closing) throw new Error('The corestore is closed')
217
216
  this.cores.set(id, core)
218
217
  core.ready().then(() => {
219
218
  if (core.closing) return // extra safety here as ready is a tick after open
@@ -236,7 +235,7 @@ module.exports = class Corestore extends EventEmitter {
236
235
  }
237
236
 
238
237
  async createKeyPair (name, namespace = this._namespace) {
239
- if (!this.primaryKey) await this._opening
238
+ if (!this.primaryKey) await this.ready()
240
239
 
241
240
  const keyPair = {
242
241
  publicKey: b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES),
@@ -256,7 +255,7 @@ module.exports = class Corestore extends EventEmitter {
256
255
  }
257
256
 
258
257
  get (opts = {}) {
259
- if (this._root._closing) throw new Error('The corestore is closed')
258
+ if (this._root.closing) throw new Error('The corestore is closed')
260
259
  opts = validateGetOptions(opts)
261
260
 
262
261
  if (opts.cache !== false) {
@@ -320,12 +319,14 @@ module.exports = class Corestore extends EventEmitter {
320
319
  }
321
320
 
322
321
  session (opts) {
323
- return new Corestore(this.storage, {
322
+ const session = new Corestore(this.storage, {
324
323
  namespace: this._namespace,
325
324
  cache: this.cache,
326
325
  _root: this._root,
327
326
  ...opts
328
327
  })
328
+ if (this === this._root) this._rootStoreSessions.add(session)
329
+ return session
329
330
  }
330
331
 
331
332
  _closeNamespace () {
@@ -356,18 +357,12 @@ module.exports = class Corestore extends EventEmitter {
356
357
  }
357
358
 
358
359
  async _close () {
359
- await this._opening
360
+ this._root._rootStoreSessions.delete(this)
360
361
  await this._closeNamespace()
361
362
  if (this._root === this) {
362
363
  await this._closePrimaryNamespace()
363
364
  }
364
365
  }
365
-
366
- close () {
367
- if (this._closing) return this._closing
368
- this._closing = this._close()
369
- return this._closing
370
- }
371
366
  }
372
367
 
373
368
  function sign (keyPair, message) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.5.1",
3
+ "version": "6.6.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.5.3",
33
+ "hypercore": "^10.10.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"