hypercore 10.33.0 → 10.33.2

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 (2) hide show
  1. package/index.js +22 -3
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -20,6 +20,7 @@ const Batch = require('./lib/batch')
20
20
  const { manifestHash, defaultSignerManifest, createManifest, isCompat, sign } = require('./lib/verifier')
21
21
  const { ReadStream, WriteStream, ByteStream } = require('./lib/streams')
22
22
  const {
23
+ ASSERTION,
23
24
  BAD_ARGUMENT,
24
25
  SESSION_CLOSED,
25
26
  SESSION_NOT_WRITABLE,
@@ -29,6 +30,10 @@ const {
29
30
  const promises = Symbol.for('hypercore.promises')
30
31
  const inspect = Symbol.for('nodejs.util.inspect.custom')
31
32
 
33
+ // Hypercore actually does not have any notion of max/min block sizes
34
+ // but we enforce 15mb to ensure smooth replication (each block is transmitted atomically)
35
+ const MAX_SUGGESTED_BLOCK_SIZE = 15 * 1024 * 1024
36
+
32
37
  module.exports = class Hypercore extends EventEmitter {
33
38
  constructor (storage, key, opts) {
34
39
  super()
@@ -134,6 +139,8 @@ module.exports = class Hypercore extends EventEmitter {
134
139
  indent + ')'
135
140
  }
136
141
 
142
+ static MAX_SUGGESTED_BLOCK_SIZE = MAX_SUGGESTED_BLOCK_SIZE
143
+
137
144
  static key (manifest, { compat } = {}) {
138
145
  return compat ? manifest.signers[0].publicKey : manifestHash(createManifest(manifest))
139
146
  }
@@ -819,6 +826,7 @@ module.exports = class Hypercore extends EventEmitter {
819
826
 
820
827
  async seek (bytes, opts) {
821
828
  if (this.opened === false) await this.opening
829
+ if (!isValidIndex(bytes)) throw ASSERTION('seek is invalid')
822
830
 
823
831
  const tree = (opts && opts.tree) || this.core.tree
824
832
  const s = tree.seek(bytes, this.padding)
@@ -841,10 +849,9 @@ module.exports = class Hypercore extends EventEmitter {
841
849
 
842
850
  async has (start, end = start + 1) {
843
851
  if (this.opened === false) await this.opening
852
+ if (!isValidIndex(start) || !isValidIndex(end)) throw ASSERTION('has range is invalid')
844
853
 
845
- const length = end - start
846
- if (length <= 0) return false
847
- if (length === 1) return this.core.bitfield.get(start)
854
+ if (end === start + 1) return this.core.bitfield.get(start)
848
855
 
849
856
  const i = this.core.bitfield.firstUnset(start)
850
857
  return i === -1 || i >= end
@@ -852,6 +859,7 @@ module.exports = class Hypercore extends EventEmitter {
852
859
 
853
860
  async get (index, opts) {
854
861
  if (this.opened === false) await this.opening
862
+ if (!isValidIndex(index)) throw ASSERTION('block index is invalid')
855
863
 
856
864
  this.tracer.trace('get', { index })
857
865
 
@@ -885,6 +893,8 @@ module.exports = class Hypercore extends EventEmitter {
885
893
  end = start + 1
886
894
  }
887
895
 
896
+ if (!isValidIndex(start) || !isValidIndex(end)) throw ASSERTION('clear range is invalid')
897
+
888
898
  const cleared = (opts && opts.diff) ? { blocks: 0 } : null
889
899
 
890
900
  if (start >= end) return cleared
@@ -1024,6 +1034,11 @@ module.exports = class Hypercore extends EventEmitter {
1024
1034
  buffers[i] = this._encode(this.valueEncoding, blocks[i])
1025
1035
  }
1026
1036
  }
1037
+ for (const b of buffers) {
1038
+ if (b.byteLength > MAX_SUGGESTED_BLOCK_SIZE) {
1039
+ throw BAD_ARGUMENT('Appended block exceeds the maximum suggested block size')
1040
+ }
1041
+ }
1027
1042
 
1028
1043
  return this.core.append(buffers, { keyPair, signature, preappend })
1029
1044
  }
@@ -1145,3 +1160,7 @@ function ensureEncryption (core, opts) {
1145
1160
  function createCache (cache) {
1146
1161
  return cache === true ? new Xache({ maxSize: 65536, maxAge: 0 }) : (cache || null)
1147
1162
  }
1163
+
1164
+ function isValidIndex (index) {
1165
+ return index === 0 || index > 0
1166
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.33.0",
3
+ "version": "10.33.2",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -50,7 +50,7 @@
50
50
  "fast-fifo": "^1.3.0",
51
51
  "flat-tree": "^1.9.0",
52
52
  "hypercore-crypto": "^3.2.1",
53
- "hypercore-errors": "^1.1.1",
53
+ "hypercore-errors": "^1.2.0",
54
54
  "hypercore-id-encoding": "^1.2.0",
55
55
  "hypertrace": "^1.2.1",
56
56
  "is-options": "^1.0.1",