hypercore 10.22.2 → 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.
Files changed (3) hide show
  1. package/index.js +4 -4
  2. package/lib/batch.js +18 -6
  3. package/package.json +1 -1
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.22.2",
3
+ "version": "10.22.3",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {