hypercore 11.30.2 → 11.32.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
@@ -270,6 +270,11 @@ class Hypercore extends EventEmitter {
270
270
  this.encryption = encryption
271
271
  }
272
272
 
273
+ async setGroup(topic) {
274
+ if (!this.opened) await this.opening
275
+ return this.core.setGroup(topic)
276
+ }
277
+
273
278
  setKeyPair(keyPair) {
274
279
  this.keyPair = keyPair
275
280
  }
@@ -1363,6 +1368,7 @@ function initOnce(session, storage, key, opts) {
1363
1368
  keyPair: opts.keyPair,
1364
1369
  legacy: opts.legacy,
1365
1370
  manifest: opts.manifest,
1371
+ group: opts.group,
1366
1372
  globalCache: opts.globalCache || null // session is a temp option, not to be relied on unless you know what you are doing (no semver guarantees)
1367
1373
  })
1368
1374
  }
package/lib/core.js CHANGED
@@ -48,6 +48,7 @@ module.exports = class Core {
48
48
  this.globalCache = opts.globalCache || null
49
49
  this.autoClose = opts.autoClose !== false
50
50
  this.onidle = noop
51
+ this.ongroupupdate = noop
51
52
 
52
53
  this.state = null
53
54
  this.opened = false
@@ -214,8 +215,10 @@ module.exports = class Core {
214
215
  fork: 0,
215
216
  length: 0,
216
217
  rootHash: null,
217
- signature: null
218
+ signature: null,
219
+ timestamp: 0
218
220
  },
221
+ group: null,
219
222
  hints: {
220
223
  reorgs: [],
221
224
  contiguousLength: 0,
@@ -296,6 +299,19 @@ module.exports = class Core {
296
299
  this.replicator.setPushOnly(true)
297
300
  }
298
301
 
302
+ if (opts.group) {
303
+ if (!header.group) header.group = await this.db.createGroup(opts.group)
304
+ else if (!b4a.equals(header.group.key, opts.group)) {
305
+ throw STORAGE_CONFLICT(
306
+ 'Core is already register to group: ' + b4a.toString(header.group.key, 'hex')
307
+ )
308
+ }
309
+
310
+ const tx = storage.write()
311
+ tx.setGroup(header.group)
312
+ await tx.flush()
313
+ }
314
+
299
315
  if (overwrite) {
300
316
  const tx = storage.write()
301
317
  tx.deleteTreeNodeRange(0, -1)
@@ -354,6 +370,24 @@ module.exports = class Core {
354
370
  }
355
371
  }
356
372
 
373
+ async setGroup(group) {
374
+ await this.state.mutex.lock()
375
+ try {
376
+ if (!this.header.group) this.header.group = await this.db.createGroup(group)
377
+ else if (!b4a.equals(this.header.group.key, group)) {
378
+ throw STORAGE_CONFLICT(
379
+ 'Core is already register to group: ' + b4a.toString(this.header.group.key, 'hex')
380
+ )
381
+ }
382
+
383
+ const tx = this.storage.write()
384
+ tx.setGroup(this.header.group)
385
+ await tx.flush()
386
+ } finally {
387
+ this.state.mutex.unlock()
388
+ }
389
+ }
390
+
357
391
  async setManifest(manifest) {
358
392
  await this.state.mutex.lock()
359
393
 
@@ -965,7 +999,8 @@ function getDefaultTree() {
965
999
  fork: 0,
966
1000
  length: 0,
967
1001
  rootHash: null,
968
- signature: null
1002
+ signature: null,
1003
+ timestamp: 0
969
1004
  }
970
1005
  }
971
1006
 
@@ -978,6 +1013,7 @@ function parseHeader(info) {
978
1013
  external: null,
979
1014
  keyPair: info.keyPair,
980
1015
  tree: info.head || getDefaultTree(),
1016
+ group: info.group,
981
1017
  hints: {
982
1018
  reorgs: [],
983
1019
  contiguousLength: info.hints ? info.hints.contiguousLength : 0,
@@ -1005,13 +1041,15 @@ async function getCoreInfo(storage) {
1005
1041
  const auth = r.getAuth()
1006
1042
  const head = r.getHead()
1007
1043
  const hints = r.getHints()
1044
+ const group = r.getGroup()
1008
1045
 
1009
1046
  r.tryFlush()
1010
1047
 
1011
- const [authInfo, headInfo, hintsInfo] = await Promise.all([auth, head, hints])
1048
+ const [authInfo, headInfo, hintsInfo, groupInfo] = await Promise.all([auth, head, hints, group])
1012
1049
  return {
1013
1050
  ...authInfo,
1014
1051
  head: headInfo,
1015
- hints: hintsInfo
1052
+ hints: hintsInfo,
1053
+ group: groupInfo
1016
1054
  }
1017
1055
  }
package/lib/replicator.js CHANGED
@@ -2460,7 +2460,12 @@ module.exports = class Replicator {
2460
2460
  }
2461
2461
 
2462
2462
  this._onpeerupdate(false, peer)
2463
- if (inflight) this.updateAll()
2463
+ if (inflight) {
2464
+ this.updateAll()
2465
+ } else {
2466
+ this._checkUpgradeIfAvailable()
2467
+ this._maybeResolveIfAvailableRanges()
2468
+ }
2464
2469
  }
2465
2470
 
2466
2471
  _queueBlock(b) {
@@ -219,6 +219,14 @@ class SessionState {
219
219
  return dependency
220
220
  }
221
221
 
222
+ updateGroupIndex(tx, tree) {
223
+ const { pointer } = this.core.header.group
224
+
225
+ tx.deleteGroupUpdate(pointer, this.core.header.tree.timestamp)
226
+ tx.putGroupUpdate(pointer, tree.timestamp, this.core.header.key)
227
+ this.core.ongroupupdate(this.core.header.group.key)
228
+ }
229
+
222
230
  _clearActiveBatch() {
223
231
  this._activeTx = null
224
232
  }
@@ -294,7 +302,14 @@ class SessionState {
294
302
  fork: this.fork,
295
303
  length: this.length,
296
304
  rootHash: this.hash(),
297
- signature: this.signature
305
+ signature: this.signature,
306
+ timestamp: Date.now()
307
+ }
308
+
309
+ if (this.isDefault() && this.core.header.group) {
310
+ const tx = await this.createWriteBatch()
311
+ this.updateGroupIndex(tx, tree)
312
+ await tx.flush()
298
313
  }
299
314
 
300
315
  if (dependency) this.storage.setDependencyHead(dependency)
@@ -364,10 +379,17 @@ class SessionState {
364
379
  fork: batch.fork,
365
380
  length: batch.length,
366
381
  rootHash: batch.hash(),
367
- signature: batch.signature
382
+ signature: batch.signature,
383
+ timestamp: Date.now()
368
384
  }
369
385
 
370
- if (batch.upgraded) tx.setHead(head)
386
+ if (batch.upgraded) {
387
+ tx.setHead(head)
388
+
389
+ if (this.core.header.group) {
390
+ this.updateGroupIndex(tx, head)
391
+ }
392
+ }
371
393
 
372
394
  const flushed = await this.flush()
373
395
 
@@ -487,7 +509,12 @@ class SessionState {
487
509
  fork: batch.fork,
488
510
  length: batch.length,
489
511
  rootHash: batch.hash(),
490
- signature: batch.signature
512
+ signature: batch.signature,
513
+ timestamp: Date.now()
514
+ }
515
+
516
+ if (this.isDefault() && this.core.header.group) {
517
+ this.updateGroupIndex(storage, tree)
491
518
  }
492
519
 
493
520
  storage.setHead(tree)
@@ -663,7 +690,8 @@ class SessionState {
663
690
  fork: batch.fork,
664
691
  length: batch.length,
665
692
  rootHash: batch.hash(),
666
- signature: batch.signature
693
+ signature: batch.signature,
694
+ timestamp: Date.now()
667
695
  }
668
696
 
669
697
  tx.setHead(tree)
@@ -676,6 +704,10 @@ class SessionState {
676
704
  remoteContiguousLength: this.core.header.hints.remoteContiguousLength
677
705
  })
678
706
  }
707
+
708
+ if (this.core.header.group) {
709
+ this.updateGroupIndex(tx, tree)
710
+ }
679
711
  }
680
712
 
681
713
  for (let i = 0; i < values.length; i++) {
@@ -829,9 +861,11 @@ class SessionState {
829
861
  fork,
830
862
  length,
831
863
  rootHash: crypto.tree(roots),
832
- signature: null
864
+ signature: null,
865
+ timestamp: Date.now()
833
866
  }
834
867
 
868
+ // never default state so do not update group index
835
869
  tx.setHead(tree)
836
870
 
837
871
  // prop a better way to do this
@@ -957,7 +991,12 @@ class SessionState {
957
991
  fork,
958
992
  length,
959
993
  rootHash: crypto.tree(roots),
960
- signature
994
+ signature,
995
+ timestamp: Date.now()
996
+ }
997
+
998
+ if (this.isDefault() && this.core.header.group) {
999
+ this.updateGroupIndex(tx, tree)
961
1000
  }
962
1001
 
963
1002
  const upgraded = treeLength < this.length || this.length < length || tree.fork !== this.fork
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.30.2",
3
+ "version": "11.32.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -54,7 +54,7 @@
54
54
  "hypercore-crypto": "^3.2.1",
55
55
  "hypercore-errors": "^1.5.0",
56
56
  "hypercore-id-encoding": "^1.2.0",
57
- "hypercore-storage": "^2.8.0",
57
+ "hypercore-storage": "^3.0.0",
58
58
  "is-options": "^1.0.1",
59
59
  "nanoassert": "^2.0.0",
60
60
  "protomux": "^3.5.0",