hypercore 11.28.1 → 11.29.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 CHANGED
@@ -34,7 +34,8 @@ const {
34
34
  SESSION_MOVED,
35
35
  SESSION_NOT_WRITABLE,
36
36
  SNAPSHOT_NOT_AVAILABLE,
37
- DECODING_ERROR
37
+ DECODING_ERROR,
38
+ REQUEST_CANCELLED
38
39
  } = require('hypercore-errors')
39
40
 
40
41
  // Hypercore actually does not have any notion of max/min block sizes
@@ -192,7 +193,12 @@ class Hypercore extends EventEmitter {
192
193
  }
193
194
 
194
195
  static clearRequests(session, err) {
195
- return Replicator.clearRequests(session, err)
196
+ Replicator.clearRequests(session, err)
197
+ }
198
+
199
+ static destroyRequests(session, err) {
200
+ Replicator.clearRequests(session, err)
201
+ session.push(null) // mark as dead
196
202
  }
197
203
 
198
204
  static async treeHashFromStorage(session, length = session.length) {
@@ -736,6 +742,8 @@ class Hypercore extends EventEmitter {
736
742
 
737
743
  if (!upgraded && remoteWait) {
738
744
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
745
+ if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED()
746
+
739
747
  const req = this.core.replicator.addUpgrade(activeRequests)
740
748
 
741
749
  try {
@@ -755,6 +763,7 @@ class Hypercore extends EventEmitter {
755
763
  if (!isValidIndex(bytes)) throw ASSERTION('seek is invalid', this.discoveryKey)
756
764
 
757
765
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
766
+ if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED()
758
767
 
759
768
  if (this.encryption && !this.core.manifest) {
760
769
  const req = this.replicator.addUpgrade(activeRequests)
@@ -777,6 +786,7 @@ class Hypercore extends EventEmitter {
777
786
 
778
787
  if (!this._shouldWait(opts, this.wait)) return null
779
788
 
789
+ if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED()
780
790
  const req = this.core.replicator.addSeek(activeRequests, s)
781
791
 
782
792
  const timeout = opts && opts.timeout !== undefined ? opts.timeout : this.timeout
@@ -916,6 +926,7 @@ class Hypercore extends EventEmitter {
916
926
  if (this.onwait) this.onwait(index, this)
917
927
 
918
928
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
929
+ if (isRequestsDestroyed(activeRequests)) throw REQUEST_CANCELLED()
919
930
 
920
931
  const force = opts ? opts.force === true : false
921
932
  const req = this.core.replicator.addBlock(activeRequests, index, force)
@@ -1393,3 +1404,8 @@ function createDiscoveryKeyHandler(fn) {
1393
1404
  return fn(id)
1394
1405
  }
1395
1406
  }
1407
+
1408
+ function isRequestsDestroyed(activeRequests) {
1409
+ // TODO: move this to an object instead so we can store a property in next major
1410
+ return activeRequests.length > 0 && activeRequests[0] === null
1411
+ }
package/lib/replicator.js CHANGED
@@ -2277,6 +2277,7 @@ module.exports = class Replicator {
2277
2277
 
2278
2278
  static clearRequests(session, err) {
2279
2279
  if (session.length === 0) return
2280
+ if (session.length === 1 && session[0] === null) return
2280
2281
 
2281
2282
  const updated = new Set()
2282
2283
 
@@ -408,8 +408,12 @@ class SessionState {
408
408
  const batch = this.createTreeBatch()
409
409
  await MerkleTree.truncate(this, length, batch, fork)
410
410
 
411
- if (!signature && keyPair && length > 0) signature = this.core.verifier.sign(batch, keyPair)
412
- if (signature) batch.signature = signature
411
+ if (signature) {
412
+ batch.signature = signature
413
+ this.core._verifyBatchUpgrade(batch)
414
+ } else if (keyPair) {
415
+ batch.signature = this.core.verifier.sign(batch, keyPair)
416
+ }
413
417
 
414
418
  const tx = this.createWriteBatch()
415
419
 
@@ -646,8 +650,12 @@ class SessionState {
646
650
  throw INVALID_OPERATION('Append is not consistent with prologue', this.core.discoveryKey)
647
651
  }
648
652
 
649
- if (!signature && keyPair) signature = this.core.verifier.sign(batch, keyPair)
650
- if (signature) batch.signature = signature
653
+ if (signature) {
654
+ batch.signature = signature
655
+ this.core._verifyBatchUpgrade(batch)
656
+ } else if (keyPair) {
657
+ batch.signature = this.core.verifier.sign(batch, keyPair)
658
+ }
651
659
 
652
660
  batch.commit(tx)
653
661
 
@@ -873,9 +881,11 @@ class SessionState {
873
881
 
874
882
  rx.tryFlush()
875
883
 
876
- const blocks = await Promise.all(blockPromises)
877
- const nodes = await Promise.all(treePromises)
878
- const roots = await Promise.all(rootPromises)
884
+ const [blocks, nodes, roots] = await Promise.all([
885
+ Promise.all(blockPromises),
886
+ Promise.all(treePromises),
887
+ Promise.all(rootPromises)
888
+ ])
879
889
 
880
890
  if (this.core.destroyed) throw new Error('Core destroyed')
881
891
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.28.1",
3
+ "version": "11.29.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {