hypercore 11.0.16 → 11.0.18

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
@@ -288,7 +288,6 @@ class Hypercore extends EventEmitter {
288
288
  try {
289
289
  await this._openSession(opts)
290
290
  } catch (err) {
291
- if (this.closing) return
292
291
  if (this.core.autoClose && this.core.hasSession() === false) await this.core.close()
293
292
 
294
293
  if (this.exclusive) this.core.unlockExclusive()
@@ -298,6 +297,7 @@ class Hypercore extends EventEmitter {
298
297
 
299
298
  if (this.state !== null) this.state.removeSession(this)
300
299
 
300
+ this.closed = true
301
301
  this.emit('close', this.core.hasSession() === false)
302
302
  throw err
303
303
  }
@@ -428,7 +428,14 @@ class Hypercore extends EventEmitter {
428
428
  }
429
429
 
430
430
  async _close (error) {
431
- if (this.opened === false) await this.opening
431
+ if (this.opened === false) {
432
+ try {
433
+ await this.opening
434
+ } catch (err) {
435
+ if (!this.closed) throw err
436
+ }
437
+ }
438
+
432
439
  if (this.closed === true) return
433
440
 
434
441
  this.core.removeMonitor(this)
@@ -850,14 +850,21 @@ module.exports = class SessionState {
850
850
  return head
851
851
  }
852
852
 
853
- _moveToCore (core) {
853
+ _moveToCore (core, truncated, appended) {
854
854
  const head = this.core.sessionStates.pop()
855
855
  if (head !== this) this.core.sessionStates[(head.index = this.index)] = head
856
856
 
857
857
  this.core = core
858
858
  this.index = this.core.sessionStates.push(this) - 1
859
859
 
860
- for (let i = this.sessions.length - 1; i >= 0; i--) this.sessions[i].transferSession(this.core)
860
+ for (let i = this.sessions.length - 1; i >= 0; i--) {
861
+ const s = this.sessions[i]
862
+ const manifest = s.manifest
863
+ s.transferSession(this.core)
864
+ if (!manifest && s.manifest) s.emit('manifest')
865
+ if (truncated) s.emit('truncate', truncated.to, truncated.fork)
866
+ if (appended) s.emit('append')
867
+ }
861
868
  }
862
869
 
863
870
  async moveTo (core, length) {
@@ -872,6 +879,9 @@ module.exports = class SessionState {
872
879
 
873
880
  const treeLength = this.length
874
881
 
882
+ let truncated = null
883
+ let appended = false
884
+
875
885
  if (!this.isSnapshot()) {
876
886
  if (this.lingers === null) this.lingers = []
877
887
  this.lingers.push(this.storage)
@@ -900,10 +910,14 @@ module.exports = class SessionState {
900
910
  this.roots = await this.tree.getRoots(length)
901
911
 
902
912
  if (truncation) {
903
- const { dependency, tree } = truncation
913
+ const { dependency } = truncation
904
914
 
905
915
  if (dependency) this.storage.updateDependencyLength(dependency.length, true)
906
- this.ontruncate(tree, tree.length, treeLength, true)
916
+ truncated = { to: treeLength, fork }
917
+ }
918
+
919
+ if (this.length > treeLength) {
920
+ appended = true
907
921
  }
908
922
  }
909
923
 
@@ -913,7 +927,7 @@ module.exports = class SessionState {
913
927
  if (state.name === this.name) state._moveToCore(core.core)
914
928
  }
915
929
 
916
- this._moveToCore(core.core)
930
+ this._moveToCore(core.core, truncated, appended)
917
931
  } finally {
918
932
  this.mutex.unlock()
919
933
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.0.16",
3
+ "version": "11.0.18",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {