hypercore 11.27.8 → 11.27.10

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() {
@@ -124,8 +124,7 @@ class SessionState {
124
124
  }
125
125
 
126
126
  async _updateSnapshotStorage(storage) {
127
- if (!this.atomized || !this.atomized.flushing) return this.treeInfo()
128
- await this.atomized.flushed()
127
+ while (this.atomized && this.atomized.flushing) await this.atomized.flushed()
129
128
 
130
129
  let rx = storage.read()
131
130
  const headPromise = rx.getHead()
@@ -206,16 +205,10 @@ class SessionState {
206
205
  }
207
206
 
208
207
  async snapshot() {
209
- try {
210
- this.mutex.lock()
211
-
212
- const storage = this.storage.snapshot()
213
- const treeInfo = await this._updateSnapshotStorage(storage)
208
+ const storage = this.storage.snapshot()
209
+ const treeInfo = await this._updateSnapshotStorage(storage)
214
210
 
215
- return new SessionState(this.core, null, storage, treeInfo, this.name)
216
- } finally {
217
- this._unlock()
218
- }
211
+ return new SessionState(this.core, null, storage, treeInfo, this.name)
219
212
  }
220
213
 
221
214
  updateDependency(tx, length) {
@@ -1137,56 +1130,62 @@ class SessionState {
1137
1130
  }
1138
1131
 
1139
1132
  async createSession(name, overwrite, atom) {
1140
- let storage = null
1141
- let treeInfo = null
1133
+ await this.mutex.lock()
1142
1134
 
1143
- if (!atom && !overwrite && this.storage) {
1144
- storage = await this.storage.resumeSession(name)
1135
+ try {
1136
+ let storage = null
1137
+ let treeInfo = null
1145
1138
 
1146
- if (storage !== null) treeInfo = (await getCoreHead(storage)) || getDefaultTree()
1147
- }
1139
+ if (!atom && !overwrite && this.storage) {
1140
+ storage = await this.storage.resumeSession(name)
1148
1141
 
1149
- const length = treeInfo ? treeInfo.length : this.length
1142
+ if (storage !== null) treeInfo = (await getCoreHead(storage)) || getDefaultTree()
1143
+ }
1150
1144
 
1151
- if (storage === null) {
1152
- treeInfo = await this._getTreeHeadAt(length)
1145
+ const length = treeInfo ? treeInfo.length : this.length
1153
1146
 
1154
- if (atom) {
1155
- storage = await this.storage.createAtomicSession(atom, treeInfo)
1156
- } else {
1157
- storage = await this.storage.createSession(name, treeInfo)
1147
+ if (storage === null) {
1148
+ treeInfo = await this._getTreeHeadAt(length)
1149
+
1150
+ if (atom) {
1151
+ storage = await this.storage.createAtomicSession(atom, treeInfo)
1152
+ } else {
1153
+ storage = await this.storage.createSession(name, treeInfo)
1154
+ }
1158
1155
  }
1159
- }
1160
1156
 
1161
- if (this.atomized && atom) {
1162
- throw new Error('Session already atomized')
1163
- }
1157
+ if (this.atomized && atom) {
1158
+ throw new Error('Session already atomized')
1159
+ }
1164
1160
 
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
- }
1161
+ const head = {
1162
+ fork: this.fork,
1163
+ roots:
1164
+ length === this.length
1165
+ ? this.roots.slice()
1166
+ : await MerkleTree.getRootsFromStorage(storage, length),
1167
+ length,
1168
+ prologue: this.prologue,
1169
+ signature: length === this.length ? this.signature : null
1170
+ }
1175
1171
 
1176
- const state = new SessionState(
1177
- this.core,
1178
- atom ? this : null,
1179
- storage,
1180
- head,
1181
- atom ? this.name : name
1182
- )
1172
+ const state = new SessionState(
1173
+ this.core,
1174
+ atom ? this : null,
1175
+ storage,
1176
+ head,
1177
+ atom ? this.name : name
1178
+ )
1183
1179
 
1184
- if (atom) {
1185
- this.atomized = atom
1186
- atom.onflush(state._commit.bind(state))
1187
- }
1180
+ if (atom) {
1181
+ this.atomized = atom
1182
+ atom.onflush(state._commit.bind(state))
1183
+ }
1188
1184
 
1189
- return state
1185
+ return state
1186
+ } finally {
1187
+ this._unlock()
1188
+ }
1190
1189
  }
1191
1190
  }
1192
1191
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "11.27.8",
3
+ "version": "11.27.10",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {