corestore 6.5.2 → 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 +9 -23
  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
@@ -41,14 +41,7 @@ module.exports = class Corestore extends EventEmitter {
41
41
  this._findingPeers = []
42
42
 
43
43
  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
44
+ this.ready().catch(safetyCatch)
52
45
  }
53
46
 
54
47
  findingPeers () {
@@ -97,7 +90,7 @@ module.exports = class Corestore extends EventEmitter {
97
90
 
98
91
  async _open () {
99
92
  if (this._root !== this) {
100
- await this._root._opening
93
+ await this._root.ready()
101
94
  if (!this.primaryKey) this.primaryKey = this._root.primaryKey
102
95
  if (this._bootstrap) await this._openNamespaceFromBootstrap()
103
96
  return
@@ -179,7 +172,7 @@ module.exports = class Corestore extends EventEmitter {
179
172
  }
180
173
 
181
174
  async _preload (opts) {
182
- if (!this.primaryKey) await this._opening
175
+ if (!this.primaryKey) await this.ready()
183
176
 
184
177
  const { discoveryKey, keyPair, auth } = await this._generateKeys(opts)
185
178
  const id = b4a.toString(discoveryKey, 'hex')
@@ -219,7 +212,7 @@ module.exports = class Corestore extends EventEmitter {
219
212
  : null
220
213
  })
221
214
 
222
- if (this._root._closing) throw new Error('The corestore is closed')
215
+ if (this._root.closing) throw new Error('The corestore is closed')
223
216
  this.cores.set(id, core)
224
217
  core.ready().then(() => {
225
218
  if (core.closing) return // extra safety here as ready is a tick after open
@@ -242,7 +235,7 @@ module.exports = class Corestore extends EventEmitter {
242
235
  }
243
236
 
244
237
  async createKeyPair (name, namespace = this._namespace) {
245
- if (!this.primaryKey) await this._opening
238
+ if (!this.primaryKey) await this.ready()
246
239
 
247
240
  const keyPair = {
248
241
  publicKey: b4a.allocUnsafe(sodium.crypto_sign_PUBLICKEYBYTES),
@@ -262,7 +255,7 @@ module.exports = class Corestore extends EventEmitter {
262
255
  }
263
256
 
264
257
  get (opts = {}) {
265
- if (this._root._closing) throw new Error('The corestore is closed')
258
+ if (this._root.closing) throw new Error('The corestore is closed')
266
259
  opts = validateGetOptions(opts)
267
260
 
268
261
  if (opts.cache !== false) {
@@ -364,19 +357,12 @@ module.exports = class Corestore extends EventEmitter {
364
357
  }
365
358
 
366
359
  async _close () {
367
- await this._opening
368
360
  this._root._rootStoreSessions.delete(this)
369
361
  await this._closeNamespace()
370
362
  if (this._root === this) {
371
363
  await this._closePrimaryNamespace()
372
364
  }
373
365
  }
374
-
375
- close () {
376
- if (this._closing) return this._closing
377
- this._closing = this._close()
378
- return this._closing
379
- }
380
366
  }
381
367
 
382
368
  function sign (keyPair, message) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.5.2",
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"