hypercore 10.0.0-alpha.41 → 10.0.0-alpha.42

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 CHANGED
@@ -191,16 +191,10 @@ module.exports = class Hypercore extends EventEmitter {
191
191
 
192
192
  s._passCapabilities(this)
193
193
 
194
- if (opts.encryptionKey) {
195
- // Only override the block encryption if its either not already set or if
196
- // the caller provided a different key.
197
- if (
198
- !s.encryption ||
199
- !b4a.equals(s.encryption.key, opts.encryptionKey)
200
- ) {
201
- s.encryption = new BlockEncryption(opts.encryptionKey, s.key)
202
- }
203
- }
194
+ // Pass on the cache unless explicitly disabled.
195
+ if (opts.cache !== false) s.cache = this.cache
196
+
197
+ ensureEncryption(s, opts)
204
198
 
205
199
  this.sessions.push(s)
206
200
 
@@ -229,6 +223,8 @@ module.exports = class Hypercore extends EventEmitter {
229
223
  this.storage = from.storage
230
224
  this.replicator.findingPeers += this._findingPeers
231
225
 
226
+ ensureEncryption(this, opts)
227
+
232
228
  this.sessions.push(this)
233
229
  }
234
230
 
@@ -587,33 +583,57 @@ module.exports = class Hypercore extends EventEmitter {
587
583
  if (this.closing !== null) throw SESSION_CLOSED()
588
584
  if (this._snapshot !== null && index >= this._snapshot.compatLength) throw SNAPSHOT_NOT_AVAILABLE()
589
585
 
590
- const c = this.cache && this.cache.get(index)
591
- if (c) return c
592
- const fork = this.core.tree.fork
593
- const b = await this._get(index, opts)
594
- if (this.cache && fork === this.core.tree.fork && b) this.cache.set(index, b)
595
- return b
586
+ const encoding = (opts && opts.valueEncoding && c.from(codecs(opts.valueEncoding))) || this.valueEncoding
587
+
588
+ let req = this.cache && this.cache.get(index)
589
+ if (!req) req = this._get(index, opts)
590
+
591
+ let block = await req
592
+ if (!block) return null
593
+
594
+ if (this.encryption) {
595
+ // Copy the block as it might be shared with other sessions.
596
+ block = b4a.from(block)
597
+
598
+ this.encryption.decrypt(index, block)
599
+ }
600
+
601
+ return this._decode(encoding, block)
596
602
  }
597
603
 
598
604
  async _get (index, opts) {
599
- const encoding = (opts && opts.valueEncoding && c.from(codecs(opts.valueEncoding))) || this.valueEncoding
600
-
601
- let block
605
+ let req
602
606
 
603
607
  if (this.core.bitfield.get(index)) {
604
- block = await this.core.blocks.get(index)
608
+ req = this.core.blocks.get(index)
609
+
610
+ if (this.cache) this.cache.set(index, req)
605
611
  } else {
606
612
  if (opts && opts.wait === false) return null
607
613
  if (opts && opts.onwait) opts.onwait(index)
608
614
 
609
615
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
610
- const req = this.replicator.addBlock(activeRequests, index)
611
616
 
612
- block = await req.promise
617
+ req = this._cacheOnResolve(
618
+ index,
619
+ this.replicator
620
+ .addBlock(activeRequests, index)
621
+ .promise,
622
+ this.core.tree.fork
623
+ )
613
624
  }
614
625
 
615
- if (this.encryption) this.encryption.decrypt(index, block)
616
- return this._decode(encoding, block)
626
+ return req
627
+ }
628
+
629
+ async _cacheOnResolve (index, req, fork) {
630
+ const block = await req
631
+
632
+ if (this.cache && fork === this.core.tree.fork) {
633
+ this.cache.set(index, Promise.resolve(block))
634
+ }
635
+
636
+ return block
617
637
  }
618
638
 
619
639
  createReadStream (opts) {
@@ -765,7 +785,7 @@ module.exports = class Hypercore extends EventEmitter {
765
785
  }
766
786
 
767
787
  _decode (enc, block) {
768
- block = block.subarray(this.padding)
788
+ if (this.padding) block = block.subarray(this.padding)
769
789
  if (enc) return c.decode(enc, block)
770
790
  return block
771
791
  }
@@ -797,3 +817,11 @@ function preappend (blocks) {
797
817
  this.encryption.encrypt(offset + i, blocks[i], fork)
798
818
  }
799
819
  }
820
+
821
+ function ensureEncryption (core, opts) {
822
+ if (!opts.encryptionKey) return
823
+ // Only override the block encryption if its either not already set or if
824
+ // the caller provided a different key.
825
+ if (core.encryption && b4a.equals(core.encryption.key, opts.encryptionKey)) return
826
+ core.encryption = new BlockEncryption(opts.encryptionKey, core.key)
827
+ }
package/lib/replicator.js CHANGED
@@ -1054,7 +1054,7 @@ module.exports = class Replicator {
1054
1054
  }
1055
1055
 
1056
1056
  _queueBlock (b) {
1057
- if (b.queued === true) return
1057
+ if (b.inflight.length > 0 || b.queued === true) return
1058
1058
  b.queued = true
1059
1059
  this._queued.push(b)
1060
1060
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.41",
3
+ "version": "10.0.0-alpha.42",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {