corestore 6.15.3 → 6.15.5

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 +16 -6
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -294,9 +294,16 @@ module.exports = class Corestore extends ReadyResource {
294
294
  if (core.closing) return // extra safety here as ready is a tick after open
295
295
  if (hasKeyPair) core.setKeyPair(keyPair)
296
296
  this._emitCore('core-open', core)
297
- for (const { stream } of this._replicationStreams) {
298
- core.replicate(stream, { session: true })
297
+
298
+ const ondownloading = () => {
299
+ for (const { stream } of this._replicationStreams) {
300
+ core.replicate(stream, { session: true })
301
+ }
299
302
  }
303
+ // when the replicator says we are downloading, answer the call
304
+ core.replicator.ondownloading = ondownloading
305
+ // trigger once if the condition is already true
306
+ if (core.replicator.downloading) ondownloading()
300
307
  }, () => {
301
308
  this.cores.delete(id)
302
309
  })
@@ -343,6 +350,7 @@ module.exports = class Corestore extends ReadyResource {
343
350
  ...opts,
344
351
  name: null,
345
352
  preload: async () => {
353
+ if (opts.preload) opts = { ...opts, ...(await opts.preload()) }
346
354
  if (!this.opened) await this.ready()
347
355
 
348
356
  const keys = await this._generateKeys(opts)
@@ -382,7 +390,7 @@ module.exports = class Corestore extends ReadyResource {
382
390
  const stream = Hypercore.createProtocolStream(isInitiator, {
383
391
  ...opts,
384
392
  ondiscoverykey: async discoveryKey => {
385
- const core = this.get({ _discoveryKey: discoveryKey })
393
+ const core = this.get({ _discoveryKey: discoveryKey, active: false })
386
394
 
387
395
  try {
388
396
  await core.ready()
@@ -390,14 +398,16 @@ module.exports = class Corestore extends ReadyResource {
390
398
  return
391
399
  }
392
400
 
393
- if (this.passive && !core.closing) core.replicate(stream, { session: true })
401
+ // remote is asking for the core so we HAVE to answer even if not downloading
402
+ if (!core.closing) core.replicate(stream, { session: true })
394
403
  await core.close()
395
404
  }
396
405
  })
397
406
 
398
407
  if (!this.passive) {
399
408
  for (const core of this.cores.values()) {
400
- if (!core.opened || core.closing) continue // If the core is not opened, it will be replicated in preload.
409
+ // If the core is not opened, it will be replicated in preload.
410
+ if (!core.opened || core.closing || !core.replicator.downloading) continue
401
411
  core.replicate(stream, { session: true })
402
412
  }
403
413
  }
@@ -488,7 +498,7 @@ function validateGetOptions (opts) {
488
498
  if (opts.name && opts.secretKey) throw new Error('Cannot provide both a name and a secret key')
489
499
  if (opts.publicKey && !b4a.isBuffer(opts.publicKey)) throw new Error('publicKey option must be a Buffer or Uint8Array')
490
500
  if (opts.secretKey && !b4a.isBuffer(opts.secretKey)) throw new Error('secretKey option must be a Buffer or Uint8Array')
491
- if (!opts._discoveryKey && (!opts.name && !opts.publicKey && !opts.manifest && !opts.key)) throw new Error('Must provide either a name or a publicKey')
501
+ if (!opts._discoveryKey && (!opts.name && !opts.publicKey && !opts.manifest && !opts.key && !opts.preload)) throw new Error('Must provide either a name or a publicKey')
492
502
  return opts
493
503
  }
494
504
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "corestore",
3
- "version": "6.15.3",
3
+ "version": "6.15.5",
4
4
  "description": "A Hypercore factory that simplifies managing collections of cores.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "b4a": "^1.6.4",
34
- "hypercore": "^10.24.10",
34
+ "hypercore": "^10.25.0",
35
35
  "hypercore-crypto": "^3.4.0",
36
36
  "hypercore-id-encoding": "^1.2.0",
37
37
  "read-write-mutexify": "^2.1.0",