hypercore 10.22.1 → 10.22.3

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
@@ -930,13 +930,13 @@ module.exports = class Hypercore extends EventEmitter {
930
930
  // Do nothing for now
931
931
  }
932
932
 
933
- async truncate (newLength = 0, fork = -1) {
934
- if (this._batch) throw BATCH_UNFLUSHED()
935
-
933
+ async truncate (newLength = 0, opts = {}) {
936
934
  if (this.opened === false) await this.opening
937
935
  if (this.writable === false) throw SESSION_NOT_WRITABLE()
938
936
 
939
- if (fork === -1) fork = this.core.tree.fork + 1
937
+ const { fork = this.core.tree.fork + 1, force = false } = typeof opts === 'number' ? { fork: opts } : opts
938
+ if (this._batch && !force) throw BATCH_UNFLUSHED()
939
+
940
940
  await this.core.truncate(newLength, fork, this.auth)
941
941
 
942
942
  // TODO: Should propagate from an event triggered by the oplog
package/lib/batch.js CHANGED
@@ -12,10 +12,10 @@ module.exports = class HypercoreBatch extends EventEmitter {
12
12
  this.opening = null
13
13
  this.closing = null
14
14
  this.autoClose = autoClose
15
+ this.fork = 0
15
16
 
16
17
  this._appends = []
17
18
  this._byteLength = 0
18
- this._fork = 0
19
19
  this._sessionLength = 0
20
20
  this._sessionByteLength = 0
21
21
  this._flushing = null
@@ -68,6 +68,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
68
68
  if (this.opened) return
69
69
  this._sessionLength = this.session.length
70
70
  this._sessionByteLength = this.session.byteLength
71
+ this.fork = this.session.fork
71
72
  this.opened = true
72
73
  this.emit('ready')
73
74
  }
@@ -169,7 +170,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
169
170
  return b
170
171
  }
171
172
 
172
- async truncate (newLength) {
173
+ async truncate (newLength = 0, opts = {}) {
173
174
  if (this.opened === false) await this.opening
174
175
  if (this.writable === false) throw SESSION_NOT_WRITABLE()
175
176
  if (this.closing) throw SESSION_CLOSED()
@@ -177,11 +178,22 @@ module.exports = class HypercoreBatch extends EventEmitter {
177
178
  // wait for any pending flush... (prop needs a lock)
178
179
  await this._waitForFlush()
179
180
 
181
+ const { fork = this.fork + 1, force = false } = typeof opts === 'number' ? { fork: opts } : opts
182
+
180
183
  const length = this._sessionLength
181
- if (newLength < length) throw new Error('Cannot truncate committed blocks')
184
+ if (newLength < length) {
185
+ if (!force) throw new Error('Cannot truncate committed blocks')
186
+ this._appends.length = 0
187
+ this._byteLength = 0
188
+ await this.session.truncate(newLength, { fork, force: true })
189
+ this._sessionLength = this.session.length
190
+ this._sessionByteLength = this.session.byteLength
191
+ } else {
192
+ for (let i = newLength - length; i < this._appends.length; i++) this._byteLength -= this._appends[i].byteLength
193
+ this._appends.length = newLength - length
194
+ }
182
195
 
183
- this._appends.length = newLength - length
184
- this._fork++
196
+ this.fork = fork
185
197
 
186
198
  this.emit('truncate', newLength, this.fork)
187
199
  }
@@ -267,7 +279,7 @@ module.exports = class HypercoreBatch extends EventEmitter {
267
279
  _clearAppends () {
268
280
  this._appends = []
269
281
  this._byteLength = 0
270
- this._fork = 0
282
+ this.fork = 0
271
283
  }
272
284
 
273
285
  _clearBatch () {
package/lib/replicator.js CHANGED
@@ -295,6 +295,7 @@ class Peer {
295
295
  this.dataProcessing = 0
296
296
 
297
297
  this.canUpgrade = true
298
+ this.reopenMaybe = false
298
299
 
299
300
  this.needsSync = false
300
301
  this.syncsProcessing = 0
@@ -375,6 +376,7 @@ class Peer {
375
376
  }
376
377
 
377
378
  this.needsSync = false
379
+ if (this.replicator.downloading === false) this.reopenMaybe = true
378
380
 
379
381
  this.wireSync.send({
380
382
  fork: this.core.tree.fork,
@@ -417,7 +419,12 @@ class Peer {
417
419
  }
418
420
 
419
421
  onclose (isRemote) {
420
- if (this.session) this.replicator._closeSession(this.session)
422
+ // we might have signalled to the remote that we are done (ie not downloading) and the remote might agree on that
423
+ // if that happens, the channel might be closed by the remote. if so just renegotiate it.
424
+ const reopen = isRemote === true && this.remoteOpened === true && this.remoteDownloading === false &&
425
+ this.remoteUploading === true && this.replicator.downloading === true && this.reopenMaybe === true
426
+
427
+ if (this.session && !reopen) this.replicator._closeSession(this.session)
421
428
 
422
429
  if (this.remoteOpened === false) {
423
430
  this.replicator._ifAvailable--
@@ -427,6 +434,10 @@ class Peer {
427
434
 
428
435
  this.remoteOpened = false
429
436
  this.replicator._removePeer(this)
437
+
438
+ if (reopen) {
439
+ this.replicator._makePeer(this.protomux, this.session)
440
+ }
430
441
  }
431
442
 
432
443
  async onsync ({ fork, length, remoteLength, canUpgrade, uploading, downloading }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.22.1",
3
+ "version": "10.22.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {