hypercore 11.27.9 → 11.27.11

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
@@ -376,16 +376,12 @@ class Hypercore extends EventEmitter {
376
376
  const state = this.state
377
377
 
378
378
  if (opts.atom) {
379
- await parentState.mutex.lock()
380
379
  this.state = await parentState.createSession(null, false, opts.atom)
381
- parentState.mutex.unlock()
382
380
  if (state) state.unref()
383
381
  } else if (opts.name) {
384
382
  // todo: need to make named sessions safe before ready
385
383
  // atm we always copy the state in passCapabilities
386
- await parentState.mutex.lock()
387
384
  this.state = await parentState.createSession(opts.name, !!opts.overwrite, null)
388
- parentState.mutex.unlock()
389
385
  if (state) state.unref() // ref'ed above in setup session
390
386
  }
391
387
 
package/lib/core.js CHANGED
@@ -55,6 +55,7 @@ module.exports = class Core {
55
55
  this.closed = false
56
56
 
57
57
  this.hintsChanged = false
58
+ this.remoteContiguousLengthChanged = false
58
59
 
59
60
  this._bitfield = null
60
61
  this._verifies = null
@@ -86,6 +87,17 @@ module.exports = class Core {
86
87
  s._monitorIndex = -1
87
88
  }
88
89
 
90
+ emitRemoteContiguousLength() {
91
+ if (!this.remoteContiguousLengthChanged) return
92
+ this.remoteContiguousLengthChanged = false
93
+
94
+ const length = this.header.hints.remoteContiguousLength
95
+
96
+ for (let i = this.monitors.length - 1; i >= 0; i--) {
97
+ this.monitors[i].emit('remote-contiguous-length', length)
98
+ }
99
+ }
100
+
89
101
  emitManifest() {
90
102
  for (let i = this.monitors.length - 1; i >= 0; i--) {
91
103
  this.monitors[i].emit('manifest')
@@ -819,10 +831,7 @@ module.exports = class Core {
819
831
  updateRemoteContiguousLength(length) {
820
832
  this.header.hints.remoteContiguousLength = length
821
833
  this.hintsChanged = true
822
-
823
- for (let i = this.monitors.length - 1; i >= 0; i--) {
824
- this.monitors[i].emit('remote-contiguous-length', length)
825
- }
834
+ this.remoteContiguousLengthChanged = true
826
835
  }
827
836
 
828
837
  onappend(tree, bitfield) {
package/lib/replicator.js CHANGED
@@ -1434,6 +1434,8 @@ class Peer {
1434
1434
 
1435
1435
  if (this.core.hintsChanged) await this.core.flushHints()
1436
1436
  if (drop === false) this._update()
1437
+
1438
+ this.core.emitRemoteContiguousLength()
1437
1439
  }
1438
1440
 
1439
1441
  onreorghint() {
@@ -125,7 +125,7 @@ class SessionState {
125
125
 
126
126
  async _updateSnapshotStorage(storage) {
127
127
  if (!this.atomized || !this.atomized.flushing) return this.treeInfo()
128
- await this.atomized.flushed()
128
+ while (this.atomized.flushing) await this.atomized.flushed()
129
129
 
130
130
  let rx = storage.read()
131
131
  const headPromise = rx.getHead()
@@ -206,16 +206,10 @@ class SessionState {
206
206
  }
207
207
 
208
208
  async snapshot() {
209
- try {
210
- await this.mutex.lock()
211
-
212
- const storage = this.storage.snapshot()
213
- const treeInfo = await this._updateSnapshotStorage(storage)
209
+ const storage = this.storage.snapshot()
210
+ const treeInfo = await this._updateSnapshotStorage(storage)
214
211
 
215
- return new SessionState(this.core, null, storage, treeInfo, this.name)
216
- } finally {
217
- this._unlock()
218
- }
212
+ return new SessionState(this.core, null, storage, treeInfo, this.name)
219
213
  }
220
214
 
221
215
  updateDependency(tx, length) {
@@ -1137,56 +1131,62 @@ class SessionState {
1137
1131
  }
1138
1132
 
1139
1133
  async createSession(name, overwrite, atom) {
1140
- let storage = null
1141
- let treeInfo = null
1134
+ await this.mutex.lock()
1142
1135
 
1143
- if (!atom && !overwrite && this.storage) {
1144
- storage = await this.storage.resumeSession(name)
1136
+ try {
1137
+ let storage = null
1138
+ let treeInfo = null
1145
1139
 
1146
- if (storage !== null) treeInfo = (await getCoreHead(storage)) || getDefaultTree()
1147
- }
1140
+ if (!atom && !overwrite && this.storage) {
1141
+ storage = await this.storage.resumeSession(name)
1148
1142
 
1149
- const length = treeInfo ? treeInfo.length : this.length
1143
+ if (storage !== null) treeInfo = (await getCoreHead(storage)) || getDefaultTree()
1144
+ }
1150
1145
 
1151
- if (storage === null) {
1152
- treeInfo = await this._getTreeHeadAt(length)
1146
+ const length = treeInfo ? treeInfo.length : this.length
1153
1147
 
1154
- if (atom) {
1155
- storage = await this.storage.createAtomicSession(atom, treeInfo)
1156
- } else {
1157
- storage = await this.storage.createSession(name, treeInfo)
1148
+ if (storage === null) {
1149
+ treeInfo = await this._getTreeHeadAt(length)
1150
+
1151
+ if (atom) {
1152
+ storage = await this.storage.createAtomicSession(atom, treeInfo)
1153
+ } else {
1154
+ storage = await this.storage.createSession(name, treeInfo)
1155
+ }
1158
1156
  }
1159
- }
1160
1157
 
1161
- if (this.atomized && atom) {
1162
- throw new Error('Session already atomized')
1163
- }
1158
+ if (this.atomized && atom) {
1159
+ throw new Error('Session already atomized')
1160
+ }
1164
1161
 
1165
- const head = {
1166
- fork: this.fork,
1167
- roots:
1168
- length === this.length
1169
- ? this.roots.slice()
1170
- : await MerkleTree.getRootsFromStorage(storage, length),
1171
- length,
1172
- prologue: this.prologue,
1173
- signature: length === this.length ? this.signature : null
1174
- }
1162
+ const head = {
1163
+ fork: this.fork,
1164
+ roots:
1165
+ length === this.length
1166
+ ? this.roots.slice()
1167
+ : await MerkleTree.getRootsFromStorage(storage, length),
1168
+ length,
1169
+ prologue: this.prologue,
1170
+ signature: length === this.length ? this.signature : null
1171
+ }
1175
1172
 
1176
- const state = new SessionState(
1177
- this.core,
1178
- atom ? this : null,
1179
- storage,
1180
- head,
1181
- atom ? this.name : name
1182
- )
1173
+ const state = new SessionState(
1174
+ this.core,
1175
+ atom ? this : null,
1176
+ storage,
1177
+ head,
1178
+ atom ? this.name : name
1179
+ )
1183
1180
 
1184
- if (atom) {
1185
- this.atomized = atom
1186
- atom.onflush(state._commit.bind(state))
1187
- }
1181
+ if (atom) {
1182
+ this.atomized = atom
1183
+ atom.onflush(state._commit.bind(state))
1184
+ }
1188
1185
 
1189
- return state
1186
+ return state
1187
+ } finally {
1188
+ this._unlock()
1189
+ }
1190
1190
  }
1191
1191
  }
1192
1192
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.27.9",
3
+ "version": "11.27.11",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {