hypercore 10.32.2 → 10.32.4

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.
@@ -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/messages.js CHANGED
@@ -87,6 +87,11 @@ const manifestv0 = {
87
87
  hashes.preencode(state, m.hash)
88
88
  state.end++ // type
89
89
 
90
+ if (m.prologue && m.signers.length === 0) {
91
+ c.fixed32.preencode(state, m.prologue.hash)
92
+ return
93
+ }
94
+
90
95
  if (m.quorum === 1 && m.signers.length === 1 && !m.allowPatch) {
91
96
  signer.preencode(state, m.signers[0])
92
97
  } else {
@@ -98,12 +103,18 @@ const manifestv0 = {
98
103
  encode (state, m) {
99
104
  hashes.encode(state, m.hash)
100
105
 
106
+ if (m.prologue && m.signers.length === 0) {
107
+ c.uint.encode(state, 0)
108
+ c.fixed32.encode(state, m.prologue.hash)
109
+ return
110
+ }
111
+
101
112
  if (m.quorum === 1 && m.signers.length === 1 && !m.allowPatch) {
102
113
  c.uint.encode(state, 1)
103
114
  signer.encode(state, m.signers[0])
104
115
  } else {
105
116
  c.uint.encode(state, 2)
106
- c.uint.encode(state, m.allowPatched ? 1 : 0)
117
+ c.uint.encode(state, m.allowPatch ? 1 : 0)
107
118
  c.uint.encode(state, m.quorum)
108
119
  signerArray.encode(state, m.signers)
109
120
  }
@@ -112,9 +123,22 @@ const manifestv0 = {
112
123
  const hash = hashes.decode(state)
113
124
  const type = c.uint.decode(state)
114
125
 
115
- if (type === 0) throw new Error('Type 0 is deprecated')
116
126
  if (type > 2) throw new Error('Unknown type: ' + type)
117
127
 
128
+ if (type === 0) {
129
+ return {
130
+ version: 0,
131
+ hash,
132
+ allowPatch: false,
133
+ quorum: 0,
134
+ signers: [],
135
+ prologue: {
136
+ hash: c.fixed32.decode(state),
137
+ length: 0
138
+ }
139
+ }
140
+ }
141
+
118
142
  if (type === 1) {
119
143
  return {
120
144
  version: 0,
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
 
@@ -208,8 +208,9 @@ function toMap (nodes) {
208
208
  }
209
209
 
210
210
  function defaultQuorum (man) {
211
- if (!man.signers) return 0
212
- return typeof man.quorum === 'number' ? man.quorum : (man.signers.length >> 1) + 1
211
+ if (typeof man.quorum === 'number') return man.quorum
212
+ if (!man.signers || !man.signers.length) return 0
213
+ return (man.signers.length >> 1) + 1
213
214
  }
214
215
 
215
216
  function generateUpgrade (patch, start, length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.32.2",
3
+ "version": "10.32.4",
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",