hypercore 11.18.4 → 11.19.0

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
@@ -104,8 +104,9 @@ class Hypercore extends EventEmitter {
104
104
  static DefaultEncryption = DefaultEncryption
105
105
 
106
106
  static key(manifest, { compat, version, namespace } = {}) {
107
- if (b4a.isBuffer(manifest))
107
+ if (b4a.isBuffer(manifest)) {
108
108
  manifest = { version, signers: [{ publicKey: manifest, namespace }] }
109
+ }
109
110
  return compat ? manifest.signers[0].publicKey : manifestHash(createManifest(manifest))
110
111
  }
111
112
 
@@ -353,13 +354,15 @@ class Hypercore extends EventEmitter {
353
354
  }
354
355
 
355
356
  if (this.state && checkout !== -1) {
356
- if (!opts.name && !opts.atom)
357
+ if (!opts.name && !opts.atom) {
357
358
  throw ASSERTION('Checkouts must be named or atomized', this.discoveryKey)
358
- if (checkout > this.state.length)
359
+ }
360
+ if (checkout > this.state.length) {
359
361
  throw ASSERTION(
360
362
  `Invalid checkout ${checkout} for ${opts.name}, length is ${this.state.length}`,
361
363
  this.discoveryKey
362
364
  )
365
+ }
363
366
  if (this.state.prologue && checkout < this.state.prologue.length) {
364
367
  throw ASSERTION(
365
368
  `Invalid checkout ${checkout} for ${opts.name}, prologue length is ${this.state.prologue.length}`,
@@ -704,8 +707,9 @@ class Hypercore extends EventEmitter {
704
707
  const offset = await s.update()
705
708
  if (offset) return offset
706
709
 
707
- if (this.closing !== null)
710
+ if (this.closing !== null) {
708
711
  throw SESSION_CLOSED('cannot seek on a closed session', this.discoveryKey)
712
+ }
709
713
 
710
714
  if (!this._shouldWait(opts, this.wait)) return null
711
715
 
@@ -724,8 +728,9 @@ class Hypercore extends EventEmitter {
724
728
 
725
729
  async has(start, end = start + 1) {
726
730
  if (this.opened === false) await this.opening
727
- if (!isValidIndex(start) || !isValidIndex(end))
731
+ if (!isValidIndex(start) || !isValidIndex(end)) {
728
732
  throw ASSERTION('has range is invalid', this.discoveryKey)
733
+ }
729
734
 
730
735
  if (this.state.isDefault()) {
731
736
  if (end === start + 1) return this.core.bitfield.get(start)
@@ -757,8 +762,9 @@ class Hypercore extends EventEmitter {
757
762
  if (this.opened === false) await this.opening
758
763
  if (!isValidIndex(index)) throw ASSERTION('block index is invalid', this.discoveryKey)
759
764
 
760
- if (this.closing !== null)
765
+ if (this.closing !== null) {
761
766
  throw SESSION_CLOSED('cannot get on a closed session', this.discoveryKey)
767
+ }
762
768
 
763
769
  const encoding =
764
770
  (opts && opts.valueEncoding && c.from(opts.valueEncoding)) || this.valueEncoding
@@ -784,16 +790,18 @@ class Hypercore extends EventEmitter {
784
790
 
785
791
  async clear(start, end = start + 1, opts) {
786
792
  if (this.opened === false) await this.opening
787
- if (this.closing !== null)
793
+ if (this.closing !== null) {
788
794
  throw SESSION_CLOSED('cannot clear on a closed session', this.discoveryKey)
795
+ }
789
796
 
790
797
  if (typeof end === 'object') {
791
798
  opts = end
792
799
  end = start + 1
793
800
  }
794
801
 
795
- if (!isValidIndex(start) || !isValidIndex(end))
802
+ if (!isValidIndex(start) || !isValidIndex(end)) {
796
803
  throw ASSERTION('clear range is invalid', this.discoveryKey)
804
+ }
797
805
 
798
806
  const cleared = opts && opts.diff ? { blocks: 0 } : null
799
807
 
@@ -815,8 +823,9 @@ class Hypercore extends EventEmitter {
815
823
 
816
824
  if (block !== null) return block
817
825
 
818
- if (this.closing !== null)
826
+ if (this.closing !== null) {
819
827
  throw SESSION_CLOSED('cannot get on a closed session', this.discoveryKey)
828
+ }
820
829
 
821
830
  // snapshot should check if core has block
822
831
  if (this._snapshot !== null) {
@@ -906,8 +915,9 @@ class Hypercore extends EventEmitter {
906
915
 
907
916
  const isDefault = this.state === this.core.state
908
917
  const writable = !this._readonly && !!(signature || (keyPair && keyPair.secretKey))
909
- if (isDefault && writable === false && (newLength > 0 || fork !== this.state.fork))
918
+ if (isDefault && writable === false && (newLength > 0 || fork !== this.state.fork)) {
910
919
  throw SESSION_NOT_WRITABLE('cannot append to a non-writable core', this.discoveryKey)
920
+ }
911
921
 
912
922
  await this.state.truncate(newLength, fork, { keyPair, signature })
913
923
 
@@ -925,8 +935,9 @@ class Hypercore extends EventEmitter {
925
935
  const writable =
926
936
  !isDefault || !!signature || !!(keyPair && keyPair.secretKey) || opts.writable === true
927
937
 
928
- if (this._readonly || writable === false)
938
+ if (this._readonly || writable === false) {
929
939
  throw SESSION_NOT_WRITABLE('cannot append to a readonly core', this.discoveryKey)
940
+ }
930
941
 
931
942
  blocks = Array.isArray(blocks) ? blocks : [blocks]
932
943
 
@@ -1108,11 +1119,12 @@ function maybeUnslab(block) {
1108
1119
  }
1109
1120
 
1110
1121
  function checkSnapshot(snapshot, index) {
1111
- if (index >= snapshot.state.snapshotCompatLength)
1122
+ if (index >= snapshot.state.snapshotCompatLength) {
1112
1123
  throw SNAPSHOT_NOT_AVAILABLE(
1113
1124
  `snapshot at index ${index} not available (max compat length ${snapshot.state.snapshotCompatLength})`,
1114
1125
  snapshot.discoveryKey
1115
1126
  )
1127
+ }
1116
1128
  }
1117
1129
 
1118
1130
  function readBlock(rx, index) {
package/lib/core.js CHANGED
@@ -140,7 +140,7 @@ module.exports = class Core {
140
140
  async _tryOpen(opts) {
141
141
  if (opts.preopen) await opts.preopen // just a hook to allow exclusive access here...
142
142
 
143
- let storage = await this.db.resume(this.discoveryKey)
143
+ let storage = await this.db.resumeCore(this.discoveryKey)
144
144
 
145
145
  let overwrite = opts.overwrite === true
146
146
 
@@ -203,7 +203,7 @@ module.exports = class Core {
203
203
 
204
204
  const discoveryKey = opts.discoveryKey || crypto.discoveryKey(header.key)
205
205
 
206
- storage = await this.db.create({
206
+ storage = await this.db.createCore({
207
207
  key: header.key,
208
208
  manifest,
209
209
  keyPair,
@@ -330,8 +330,9 @@ module.exports = class Core {
330
330
 
331
331
  try {
332
332
  if (manifest && this.header.manifest === null) {
333
- if (!Verifier.isValidManifest(this.header.key, manifest))
333
+ if (!Verifier.isValidManifest(this.header.key, manifest)) {
334
334
  throw INVALID_CHECKSUM('Manifest hash does not match', this.discoveryKey)
335
+ }
335
336
 
336
337
  const tx = this.state.createWriteBatch()
337
338
  this._setManifest(tx, Verifier.createManifest(manifest), null)
@@ -343,8 +344,9 @@ module.exports = class Core {
343
344
  }
344
345
 
345
346
  _setManifest(tx, manifest, keyPair) {
346
- if (!manifest && b4a.equals(keyPair.publicKey, this.header.key))
347
+ if (!manifest && b4a.equals(keyPair.publicKey, this.header.key)) {
347
348
  manifest = Verifier.defaultSignerManifest(this.header.key)
349
+ }
348
350
  if (!manifest) return
349
351
 
350
352
  const verifier = new Verifier(this.header.key, manifest, { legacy: this._legacy })
@@ -499,8 +501,9 @@ module.exports = class Core {
499
501
 
500
502
  // if we got a manifest AND its strictly a non compat one, lets store it
501
503
  if (manifest && this.header.manifest === null) {
502
- if (!Verifier.isValidManifest(this.header.key, manifest))
504
+ if (!Verifier.isValidManifest(this.header.key, manifest)) {
503
505
  throw INVALID_CHECKSUM('Manifest hash does not match', this.discoveryKey)
506
+ }
504
507
  this._setManifest(tx, manifest, null)
505
508
  }
506
509
 
@@ -25,9 +25,11 @@ module.exports = class DefaultEncryption {
25
25
  const blindingKey = subKeys.subarray(sodium.crypto_stream_KEYBYTES)
26
26
 
27
27
  if (!block) {
28
- if (compat) sodium.crypto_generichash_batch(blockKey, [encryptionKey], hypercoreKey)
29
- else
28
+ if (compat) {
29
+ sodium.crypto_generichash_batch(blockKey, [encryptionKey], hypercoreKey)
30
+ } else {
30
31
  sodium.crypto_generichash_batch(blockKey, [DEFAULT_ENCRYPTION, hypercoreKey, encryptionKey])
32
+ }
31
33
  }
32
34
 
33
35
  sodium.crypto_generichash(blindingKey, blockKey)
@@ -39,7 +39,7 @@ async function verify(storage, buffer, { referrer = null } = {}) {
39
39
  block: null
40
40
  }
41
41
 
42
- const core = await storage.resume(discoveryKey)
42
+ const core = await storage.resumeCore(discoveryKey)
43
43
  if (core === null) return null
44
44
 
45
45
  let rx = core.read()
@@ -231,8 +231,9 @@ class MerkleTreeBatch {
231
231
 
232
232
  commit(tx) {
233
233
  if (tx === undefined) throw INVALID_OPERATION('No database batch was passed')
234
- if (!this.commitable())
234
+ if (!this.commitable()) {
235
235
  throw INVALID_OPERATION('Tree was modified during batch, refusing to commit')
236
+ }
236
237
 
237
238
  if (this.upgraded) this._commitUpgrade(tx)
238
239
 
@@ -696,8 +697,9 @@ class MerkleTree {
696
697
  // TODO: we could prop use a read batch here and do this in blocks of X for perf
697
698
  while (!ite.contains(head) && !(await hasTreeNode(session.storage, ite.index))) {
698
699
  cnt++
699
- if (cnt >= 1024)
700
+ if (cnt >= 1024) {
700
701
  throw ASSERTION('Bad arguments to missingNodes index=' + index + ' at length=' + length)
702
+ }
701
703
  ite.parent()
702
704
  }
703
705
 
@@ -1023,8 +1025,9 @@ function blockAndSeekProof(session, rx, node, seek, seekRoot, root, p) {
1023
1025
  if (!node.value) p.node.push(getTreeNodeOrError(rx, ite.index))
1024
1026
 
1025
1027
  while (ite.index !== root) {
1026
- if (++cnt >= 1024)
1028
+ if (++cnt >= 1024) {
1027
1029
  throw ASSERTION('Bad blockAndSeekProof seekRoot=' + seekRoot + ', root=' + root)
1030
+ }
1028
1031
  ite.sibling()
1029
1032
 
1030
1033
  if (seek && ite.contains(seekRoot) && ite.index !== seekRoot) {
@@ -1105,10 +1108,11 @@ function additionalUpgradeProof(session, rx, from, to, p) {
1105
1108
  ite.seek(target)
1106
1109
 
1107
1110
  while (ite.index !== root) {
1108
- if (++cnt >= 1024)
1111
+ if (++cnt >= 1024) {
1109
1112
  throw ASSERTION(
1110
1113
  'Bad arguments to additionalUpgradeProof root=' + root + ' target=' + target
1111
1114
  )
1115
+ }
1112
1116
  ite.sibling()
1113
1117
  if (ite.index > target) {
1114
1118
  p.additionalUpgrade.push(getTreeNodeOrError(rx, ite.index))
@@ -1171,22 +1175,30 @@ function log2(n) {
1171
1175
  }
1172
1176
 
1173
1177
  function normalizeIndexed(block, hash) {
1174
- if (block)
1175
- return { value: true, index: block.index * 2, nodes: block.nodes, lastIndex: block.index }
1176
- if (hash)
1178
+ if (block) {
1179
+ return {
1180
+ value: true,
1181
+ index: block.index * 2,
1182
+ nodes: block.nodes,
1183
+ lastIndex: block.index
1184
+ }
1185
+ }
1186
+ if (hash) {
1177
1187
  return {
1178
1188
  value: false,
1179
1189
  index: hash.index,
1180
1190
  nodes: hash.nodes,
1181
1191
  lastIndex: flat.rightSpan(hash.index) / 2
1182
1192
  }
1193
+ }
1183
1194
  return null
1184
1195
  }
1185
1196
 
1186
1197
  async function getTreeNodeOrError(rx, index) {
1187
1198
  const node = await rx.getTreeNode(index)
1188
- if (node === null)
1199
+ if (node === null) {
1189
1200
  throw INVALID_OPERATION('Expected tree node ' + index + ' from storage, got (nil)')
1201
+ }
1190
1202
  return node
1191
1203
  }
1192
1204
 
package/lib/multisig.js CHANGED
@@ -31,8 +31,9 @@ async function partialSignature(
31
31
  if (from > core.state.length) return null
32
32
  const nodes = to <= from ? null : await upgradeNodes(core, from, to)
33
33
 
34
- if (signature.byteLength !== 64)
34
+ if (signature.byteLength !== 64) {
35
35
  signature = c.decode(multiSignature, signature).proofs[0].signature
36
+ }
36
37
 
37
38
  return {
38
39
  signer,
package/lib/mutex.js CHANGED
@@ -14,8 +14,9 @@ module.exports = class Mutex {
14
14
  }
15
15
 
16
16
  lock() {
17
- if (this.destroyed)
17
+ if (this.destroyed) {
18
18
  return Promise.reject(this._destroyError || new Error('Mutex has been destroyed'))
19
+ }
19
20
  if (this.locked) return new Promise(this._enqueue)
20
21
  this.locked = true
21
22
  return Promise.resolve()
@@ -30,8 +31,9 @@ module.exports = class Mutex {
30
31
  }
31
32
 
32
33
  destroy(err) {
33
- if (!this._destroying)
34
+ if (!this._destroying) {
34
35
  this._destroying = this.locked ? this.lock().catch(() => {}) : Promise.resolve()
36
+ }
35
37
 
36
38
  this.destroyed = true
37
39
  if (err) this._destroyError = err
package/lib/replicator.js CHANGED
@@ -496,8 +496,9 @@ class Peer {
496
496
  }
497
497
 
498
498
  signalUpgrade() {
499
- if (this.fullyDownloadedSignaled && !this.replicator.fullyDownloaded())
499
+ if (this.fullyDownloadedSignaled && !this.replicator.fullyDownloaded()) {
500
500
  this.fullyDownloadedSignaled = false
501
+ }
501
502
  if (this._shouldUpdateCanUpgrade() === true) this._updateCanUpgradeAndSync()
502
503
  else this.sendSync()
503
504
  }
@@ -520,8 +521,9 @@ class Peer {
520
521
  !this.remoteSegmentsWanted.has(i) &&
521
522
  !drop &&
522
523
  !contig
523
- )
524
+ ) {
524
525
  return
526
+ }
525
527
 
526
528
  let force = false
527
529
  if (contig && !drop) {
@@ -566,8 +568,9 @@ class Peer {
566
568
  const name = message.name || this.lastExtensionRecv
567
569
  this.lastExtensionRecv = name
568
570
  const ext = this.extensions.get(name)
569
- if (ext)
571
+ if (ext) {
570
572
  ext._onmessage({ start: 0, end: message.message.byteLength, buffer: message.message }, this)
573
+ }
571
574
  }
572
575
 
573
576
  sendSync() {
@@ -1319,10 +1322,19 @@ class Peer {
1319
1322
 
1320
1323
  _canRequest(index) {
1321
1324
  if (!(index >= 0)) throw ASSERTION('bad index to _canRequest: ' + index)
1322
- if (this.remoteLength >= this.core.state.length) return true
1323
- if (index < this.lastUpgradableLength && this.lastUpgradableFork === this.core.state.fork)
1325
+
1326
+ if (this.remoteLength >= this.core.state.length) {
1324
1327
  return true
1325
- if (this.canUpgrade && this.syncsProcessing === 0) return true
1328
+ }
1329
+
1330
+ if (index < this.lastUpgradableLength && this.lastUpgradableFork === this.core.state.fork) {
1331
+ return true
1332
+ }
1333
+
1334
+ if (this.canUpgrade && this.syncsProcessing === 0) {
1335
+ return true
1336
+ }
1337
+
1326
1338
  return false
1327
1339
  }
1328
1340
 
@@ -1888,13 +1900,16 @@ module.exports = class Replicator {
1888
1900
 
1889
1901
  if (this.core.state.length === 0 && peer.remoteLength > 0) return
1890
1902
 
1891
- if (peer.remoteLength <= this._upgrade.length || peer.remoteFork !== this._upgrade.fork)
1903
+ if (peer.remoteLength <= this._upgrade.length || peer.remoteFork !== this._upgrade.fork) {
1892
1904
  continue
1905
+ }
1893
1906
 
1894
1907
  if (peer.syncsProcessing > 0) return
1895
1908
 
1896
- if (peer.lengthAcked !== this.core.state.length && peer.remoteFork === this.core.state.fork)
1909
+ if (peer.lengthAcked !== this.core.state.length && peer.remoteFork === this.core.state.fork) {
1897
1910
  return
1911
+ }
1912
+
1898
1913
  if (peer.remoteCanUpgrade === true) return
1899
1914
  }
1900
1915
 
@@ -2080,8 +2095,9 @@ module.exports = class Replicator {
2080
2095
  if (
2081
2096
  this.core.state.length === this._upgrade.length &&
2082
2097
  this.core.state.fork === this._upgrade.fork
2083
- )
2098
+ ) {
2084
2099
  return false
2100
+ }
2085
2101
 
2086
2102
  const u = this._upgrade
2087
2103
  this._upgrade = null
@@ -2285,8 +2301,9 @@ module.exports = class Replicator {
2285
2301
  }
2286
2302
 
2287
2303
  _unmarkInflight(index) {
2288
- if (this.core.skipBitfield !== null)
2304
+ if (this.core.skipBitfield !== null) {
2289
2305
  this.core.skipBitfield.set(index, this.core.bitfield.get(index))
2306
+ }
2290
2307
  for (const peer of this.peers) peer._resetMissingBlock(index)
2291
2308
  }
2292
2309
 
@@ -2322,8 +2339,10 @@ module.exports = class Replicator {
2322
2339
  this._manifestPeer = null
2323
2340
  }
2324
2341
 
2325
- if (this._seeks.length > 0 || this._ranges.length > 0)
2342
+ if (this._seeks.length > 0 || this._ranges.length > 0) {
2326
2343
  this._updateNonPrimary(this._seeks.length > 0)
2344
+ }
2345
+
2327
2346
  this.updatePeer(peer)
2328
2347
  }
2329
2348
 
@@ -2643,8 +2662,9 @@ module.exports = class Replicator {
2643
2662
 
2644
2663
  _makePeer(protomux) {
2645
2664
  const replicator = this
2646
- if (protomux.opened({ protocol: 'hypercore/alpha', id: this.core.discoveryKey }))
2665
+ if (protomux.opened({ protocol: 'hypercore/alpha', id: this.core.discoveryKey })) {
2647
2666
  return onnochannel()
2667
+ }
2648
2668
 
2649
2669
  const channel = protomux.createChannel({
2650
2670
  userData: null,
@@ -2742,8 +2762,9 @@ module.exports = class Replicator {
2742
2762
  }
2743
2763
 
2744
2764
  function matchingRequest(req, data) {
2745
- if (data.block !== null && (req.block === null || req.block.index !== data.block.index))
2765
+ if (data.block !== null && (req.block === null || req.block.index !== data.block.index)) {
2746
2766
  return false
2767
+ }
2747
2768
  if (data.hash !== null && (req.hash === null || req.hash.index !== data.hash.index)) return false
2748
2769
  if (data.seek !== null && (req.seek === null || req.seek.bytes !== data.seek.bytes)) return false
2749
2770
  if (data.upgrade !== null && req.upgrade === null) return false
@@ -709,11 +709,12 @@ module.exports = class SessionState {
709
709
  const truncating = sharedLength < origLength
710
710
 
711
711
  for (const node of roots) {
712
- if (node === null)
712
+ if (node === null) {
713
713
  throw INVALID_OPERATION(
714
714
  'Invalid catchup length, tree nodes not available',
715
715
  this.core.discoveryKey
716
716
  )
717
+ }
717
718
  }
718
719
 
719
720
  const fork = truncating ? this.fork + 1 : this.fork
package/lib/verifier.js CHANGED
@@ -336,8 +336,9 @@ function parseSigner(signer) {
336
336
 
337
337
  function validateSigner(signer) {
338
338
  if (!signer || !signer.publicKey) throw BAD_ARGUMENT('Signer missing public key')
339
- if (signer.signature && signer.signature !== 'ed25519')
339
+ if (signer.signature && signer.signature !== 'ed25519') {
340
340
  throw BAD_ARGUMENT('Only Ed25519 signatures are supported')
341
+ }
341
342
  }
342
343
 
343
344
  function manifestHash(manifest) {
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.18.4",
3
+ "version": "11.19.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "format": "prettier --write .",
8
- "test": "prettier --check . && brittle test/all.js",
8
+ "lint": "prettier --check .",
9
+ "test": "brittle test/all.js",
9
10
  "test:bare": "bare test/all.js",
10
11
  "test:generate": "brittle -r test/all.js test/*.js"
11
12
  },
@@ -53,7 +54,7 @@
53
54
  "hypercore-crypto": "^3.2.1",
54
55
  "hypercore-errors": "^1.5.0",
55
56
  "hypercore-id-encoding": "^1.2.0",
56
- "hypercore-storage": "^1.0.0",
57
+ "hypercore-storage": "^2.0.0",
57
58
  "is-options": "^1.0.1",
58
59
  "nanoassert": "^2.0.0",
59
60
  "protomux": "^3.5.0",
@@ -69,6 +70,7 @@
69
70
  "brittle": "^3.0.0",
70
71
  "debugging-stream": "^3.1.0",
71
72
  "hyperswarm": "^4.3.6",
73
+ "lunte": "^1.2.0",
72
74
  "prettier": "^3.6.2",
73
75
  "prettier-config-holepunch": "^2.0.0",
74
76
  "rache": "^1.0.0",
package/lib/read-batch.js DELETED
@@ -1,10 +0,0 @@
1
- class HypercoreReadBatch {
2
- constructor(core) {
3
- this.core = core
4
- this.gets = []
5
- }
6
-
7
- get(index) {}
8
-
9
- tryFlush() {}
10
- }