hypercore 10.32.3 → 10.32.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.
package/index.js CHANGED
@@ -813,8 +813,8 @@ module.exports = class Hypercore extends EventEmitter {
813
813
  return true
814
814
  }
815
815
 
816
- batch ({ checkout = -1, autoClose = true, session = true, restore = false } = {}) {
817
- return new Batch(session ? this.session() : this, checkout, autoClose, restore)
816
+ batch ({ checkout = -1, autoClose = true, session = true, restore = false, clear = false } = {}) {
817
+ return new Batch(session ? this.session() : this, checkout, autoClose, restore, clear)
818
818
  }
819
819
 
820
820
  async seek (bytes, opts) {
package/lib/batch.js CHANGED
@@ -4,7 +4,7 @@ const c = require('compact-encoding')
4
4
  const b4a = require('b4a')
5
5
 
6
6
  module.exports = class HypercoreBatch extends EventEmitter {
7
- constructor (session, checkoutLength, autoClose, restore) {
7
+ constructor (session, checkoutLength, autoClose, restore, clear) {
8
8
  super()
9
9
 
10
10
  this.session = session
@@ -25,8 +25,10 @@ module.exports = class HypercoreBatch extends EventEmitter {
25
25
  this._sessionByteLength = 0
26
26
  this._sessionBatch = null
27
27
  this._flushing = null
28
+ this._clear = clear
28
29
 
29
- this.opening = this.ready().catch(noop)
30
+ this.opening = this._open()
31
+ this.opening.catch(noop)
30
32
  }
31
33
 
32
34
  get id () {
@@ -69,23 +71,27 @@ module.exports = class HypercoreBatch extends EventEmitter {
69
71
  return this.session.manifest
70
72
  }
71
73
 
72
- async ready () {
74
+ ready () {
75
+ return this.opening
76
+ }
77
+
78
+ async _open () {
73
79
  await this.session.ready()
74
- if (this.opened) return
80
+
81
+ if (this._clear) this._checkoutLength = this.core.tree.length
75
82
 
76
83
  if (this._checkoutLength !== -1) {
77
84
  const batch = await this.session.core.tree.restoreBatch(this._checkoutLength)
78
85
  batch.treeLength = this._checkoutLength
79
- if (this.opened) return
80
86
  this._sessionLength = batch.length
81
87
  this._sessionByteLength = batch.byteLength
82
88
  this._sessionBatch = batch
89
+ if (this._clear) await this.core.clearBatch()
83
90
  } else {
84
91
  const last = this.restore ? this.session.core.bitfield.findFirst(false, this.session.length) : 0
85
92
 
86
93
  if (last > this.session.length) {
87
94
  const batch = await this.session.core.tree.restoreBatch(last)
88
- if (this.opened) return
89
95
  this._sessionLength = batch.length
90
96
  this._sessionByteLength = batch.byteLength - this.session.padding * batch.length
91
97
  this._sessionBatch = batch
@@ -1,4 +1,5 @@
1
1
  const b4a = require('b4a')
2
+ const { WRITE_FAILED } = require('hypercore-errors')
2
3
 
3
4
  module.exports = class BlockStore {
4
5
  constructor (storage, tree) {
@@ -54,7 +55,7 @@ module.exports = class BlockStore {
54
55
  _write (offset, data) {
55
56
  return new Promise((resolve, reject) => {
56
57
  this.storage.write(offset, data, (err) => {
57
- if (err) reject(err)
58
+ if (err) reject(WRITE_FAILED(err.message))
58
59
  else resolve(offset + data.byteLength)
59
60
  })
60
61
  })
package/lib/core.js CHANGED
@@ -440,6 +440,40 @@ module.exports = class Core {
440
440
  }
441
441
  }
442
442
 
443
+ async clearBatch () {
444
+ await this._mutex.lock()
445
+
446
+ try {
447
+ const len = this.bitfield.findFirst(false, this.tree.length)
448
+ if (len <= this.tree.length) return
449
+
450
+ const batch = await this.tree.truncate(this.tree.length, this.tree.fork)
451
+
452
+ batch.signature = this.tree.signature // same sig
453
+
454
+ const entry = {
455
+ userData: null,
456
+ treeNodes: batch.nodes,
457
+ treeUpgrade: batch,
458
+ bitfield: {
459
+ drop: true,
460
+ start: batch.ancestors,
461
+ length: len - batch.ancestors
462
+ }
463
+ }
464
+
465
+ await this.oplog.append([entry], false)
466
+
467
+ this.bitfield.setRange(batch.ancestors, len - batch.ancestors, false)
468
+ batch.commit()
469
+
470
+ // TODO: (see below todo)
471
+ await this._flushOplog()
472
+ } finally {
473
+ this._mutex.unlock()
474
+ }
475
+ }
476
+
443
477
  async clear (start, end, cleared) {
444
478
  await this._mutex.lock()
445
479
 
package/lib/messages.js CHANGED
@@ -114,7 +114,7 @@ const manifestv0 = {
114
114
  signer.encode(state, m.signers[0])
115
115
  } else {
116
116
  c.uint.encode(state, 2)
117
- c.uint.encode(state, m.allowPatched ? 1 : 0)
117
+ c.uint.encode(state, m.allowPatch ? 1 : 0)
118
118
  c.uint.encode(state, m.quorum)
119
119
  signerArray.encode(state, m.signers)
120
120
  }
package/lib/oplog.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const cenc = require('compact-encoding')
2
2
  const b4a = require('b4a')
3
3
  const { crc32 } = require('crc-universal')
4
- const { OPLOG_CORRUPT, OPLOG_HEADER_OVERFLOW } = require('hypercore-errors')
4
+ const { OPLOG_CORRUPT, OPLOG_HEADER_OVERFLOW, WRITE_FAILED } = require('hypercore-errors')
5
5
 
6
6
  module.exports = class Oplog {
7
7
  constructor (storage, { pageSize = 4096, headerEncoding = cenc.raw, entryEncoding = cenc.raw, readonly = false } = {}) {
@@ -216,7 +216,7 @@ module.exports = class Oplog {
216
216
  _append (buf, count) {
217
217
  return new Promise((resolve, reject) => {
218
218
  this.storage.write(this._entryOffset + this.byteLength, buf, err => {
219
- if (err) return reject(err)
219
+ if (err) return reject(WRITE_FAILED(err.message))
220
220
 
221
221
  this.byteLength += buf.byteLength
222
222
  this.length += count
package/lib/replicator.js CHANGED
@@ -706,6 +706,14 @@ class Peer {
706
706
  }
707
707
  } catch (err) {
708
708
  safetyCatch(err)
709
+
710
+ if (err.code === 'WRITE_FAILED') {
711
+ // For example, we don't want to keep pulling data when storage is full
712
+ // TODO: notify the user somehow
713
+ this.paused = true
714
+ return
715
+ }
716
+
709
717
  if (this.core.closed && !isCriticalError(err)) return
710
718
 
711
719
  if (err.code !== 'INVALID_OPERATION') {
package/lib/verifier.js CHANGED
@@ -73,7 +73,7 @@ module.exports = class Verifier {
73
73
 
74
74
  return {
75
75
  proofs: proofs.map(proofToVersion1),
76
- patch: patch
76
+ patch
77
77
  }
78
78
  }
79
79
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.32.3",
3
+ "version": "10.32.5",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -44,7 +44,7 @@
44
44
  "fast-fifo": "^1.3.0",
45
45
  "flat-tree": "^1.9.0",
46
46
  "hypercore-crypto": "^3.2.1",
47
- "hypercore-errors": "^1.1.0",
47
+ "hypercore-errors": "^1.1.1",
48
48
  "hypercore-id-encoding": "^1.2.0",
49
49
  "hypertrace": "^1.2.1",
50
50
  "is-options": "^1.0.1",