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 +0 -4
- package/lib/core.js +13 -4
- package/lib/replicator.js +2 -0
- package/lib/session-state.js +49 -49
- package/package.json +1 -1
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
package/lib/session-state.js
CHANGED
|
@@ -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
|
-
|
|
210
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1141
|
-
let treeInfo = null
|
|
1134
|
+
await this.mutex.lock()
|
|
1142
1135
|
|
|
1143
|
-
|
|
1144
|
-
storage =
|
|
1136
|
+
try {
|
|
1137
|
+
let storage = null
|
|
1138
|
+
let treeInfo = null
|
|
1145
1139
|
|
|
1146
|
-
if (
|
|
1147
|
-
|
|
1140
|
+
if (!atom && !overwrite && this.storage) {
|
|
1141
|
+
storage = await this.storage.resumeSession(name)
|
|
1148
1142
|
|
|
1149
|
-
|
|
1143
|
+
if (storage !== null) treeInfo = (await getCoreHead(storage)) || getDefaultTree()
|
|
1144
|
+
}
|
|
1150
1145
|
|
|
1151
|
-
|
|
1152
|
-
treeInfo = await this._getTreeHeadAt(length)
|
|
1146
|
+
const length = treeInfo ? treeInfo.length : this.length
|
|
1153
1147
|
|
|
1154
|
-
if (
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
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
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1158
|
+
if (this.atomized && atom) {
|
|
1159
|
+
throw new Error('Session already atomized')
|
|
1160
|
+
}
|
|
1164
1161
|
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
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
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
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
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1181
|
+
if (atom) {
|
|
1182
|
+
this.atomized = atom
|
|
1183
|
+
atom.onflush(state._commit.bind(state))
|
|
1184
|
+
}
|
|
1188
1185
|
|
|
1189
|
-
|
|
1186
|
+
return state
|
|
1187
|
+
} finally {
|
|
1188
|
+
this._unlock()
|
|
1189
|
+
}
|
|
1190
1190
|
}
|
|
1191
1191
|
}
|
|
1192
1192
|
|