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