@fireproof/core 0.5.11 → 0.5.13
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/dist/src/fireproof.d.ts +12 -11
- package/dist/src/fireproof.js +52 -29
- package/dist/src/fireproof.js.map +1 -1
- package/dist/src/fireproof.mjs +52 -29
- package/dist/src/fireproof.mjs.map +1 -1
- package/package.json +1 -1
- package/src/blockstore.js +2 -2
- package/src/sync.js +50 -27
package/dist/src/fireproof.mjs
CHANGED
@@ -37408,8 +37408,8 @@ class TransactionBlockstore {
|
|
37408
37408
|
if (doSync) {
|
37409
37409
|
// const all =
|
37410
37410
|
await Promise.all([...this.syncs].map(async sync => sync.sendUpdate(innerBlockstore).catch(e => {
|
37411
|
-
console.error('sync error', e);
|
37412
|
-
|
37411
|
+
console.error('sync error, cancelling', e);
|
37412
|
+
sync.destroy();
|
37413
37413
|
})));
|
37414
37414
|
}
|
37415
37415
|
}
|
@@ -41046,6 +41046,7 @@ class Sync {
|
|
41046
41046
|
* @memberof Sync
|
41047
41047
|
* @static
|
41048
41048
|
*/
|
41049
|
+
status = 'new'
|
41049
41050
|
constructor (database, PeerClass = simplePeer) {
|
41050
41051
|
this.database = database;
|
41051
41052
|
this.database.blocks.syncs.add(this); // should this happen during setup?
|
@@ -41061,6 +41062,7 @@ class Sync {
|
|
41061
41062
|
}
|
41062
41063
|
|
41063
41064
|
async offer () {
|
41065
|
+
this.status = 'offering';
|
41064
41066
|
return this.setupPeer(true)
|
41065
41067
|
}
|
41066
41068
|
|
@@ -41068,11 +41070,13 @@ class Sync {
|
|
41068
41070
|
const offer = JSON.parse(atob(base64offer));
|
41069
41071
|
const p = this.setupPeer(false);
|
41070
41072
|
this.peer.signal(offer);
|
41073
|
+
this.status = 'accepting';
|
41071
41074
|
return p
|
41072
41075
|
}
|
41073
41076
|
|
41074
41077
|
connect (base64accept) {
|
41075
41078
|
const accept = JSON.parse(atob(base64accept));
|
41079
|
+
this.status = 'connecting';
|
41076
41080
|
this.peer.signal(accept);
|
41077
41081
|
}
|
41078
41082
|
|
@@ -41103,39 +41107,46 @@ class Sync {
|
|
41103
41107
|
// console.log('not a car', data.toString())
|
41104
41108
|
}
|
41105
41109
|
if (reader) {
|
41110
|
+
console.log('got car');
|
41111
|
+
this.status = 'parking car';
|
41106
41112
|
const blz = new Set();
|
41107
41113
|
for await (const block of reader.blocks()) {
|
41108
41114
|
blz.add(block);
|
41109
41115
|
}
|
41110
41116
|
const roots = await reader.getRoots();
|
41111
|
-
|
41112
|
-
|
41113
|
-
|
41114
|
-
|
41115
|
-
|
41116
|
-
|
41117
|
-
|
41118
|
-
|
41119
|
-
|
41117
|
+
console.log(
|
41118
|
+
'got car',
|
41119
|
+
roots.map(c => c.toString()),
|
41120
|
+
this.database.clock.map(c => c.toString())
|
41121
|
+
);
|
41122
|
+
console.log(
|
41123
|
+
'got blocks',
|
41124
|
+
[...blz].map(({ cid }) => cid.toString())
|
41125
|
+
);
|
41120
41126
|
// @ts-ignore
|
41121
41127
|
reader.entries = reader.blocks;
|
41122
|
-
await this.database.blocks.commit(
|
41123
|
-
|
41124
|
-
|
41125
|
-
|
41126
|
-
|
41127
|
-
|
41128
|
+
await this.database.blocks.commit(
|
41129
|
+
{
|
41130
|
+
label: 'sync',
|
41131
|
+
entries: () => [...blz],
|
41132
|
+
get: async cid => await reader.get(cid),
|
41133
|
+
lastCid: [...blz][0].cid // doesn't matter
|
41134
|
+
},
|
41135
|
+
false
|
41136
|
+
);
|
41128
41137
|
// first arg could be the roots parents?
|
41129
41138
|
// get the roots parents
|
41130
|
-
const parents = await Promise.all(
|
41131
|
-
|
41132
|
-
|
41133
|
-
|
41134
|
-
|
41135
|
-
|
41136
|
-
|
41137
|
-
|
41138
|
-
|
41139
|
+
const parents = await Promise.all(
|
41140
|
+
roots.map(async cid => {
|
41141
|
+
const rbl = await reader.get(cid);
|
41142
|
+
if (!rbl) {
|
41143
|
+
console.log('missing root block', cid.toString(), reader);
|
41144
|
+
throw new Error('missing root block')
|
41145
|
+
}
|
41146
|
+
const block = await decodeEventBlock(rbl.bytes);
|
41147
|
+
return block.value.parents
|
41148
|
+
})
|
41149
|
+
);
|
41139
41150
|
this.database.applyClock(parents.flat(), roots);
|
41140
41151
|
this.database.notifyReset();
|
41141
41152
|
// console.log('after', this.database.clockToJSON())
|
@@ -41145,6 +41156,7 @@ class Sync {
|
|
41145
41156
|
const message = JSON.parse(data.toString());
|
41146
41157
|
// console.log('got message', message)
|
41147
41158
|
if (message.ok) {
|
41159
|
+
this.status = 'ok';
|
41148
41160
|
this.pushBacklogResolve({ ok: true });
|
41149
41161
|
} else if (message.clock) {
|
41150
41162
|
const reqCidDiff = message;
|
@@ -41152,12 +41164,14 @@ class Sync {
|
|
41152
41164
|
console.log('got diff', reqCidDiff);
|
41153
41165
|
const carBlock = await Sync.makeCar(this.database, null, reqCidDiff.cids);
|
41154
41166
|
if (!carBlock) {
|
41155
|
-
|
41156
|
-
|
41167
|
+
// we are full synced
|
41168
|
+
// console.log('we are full synced')
|
41169
|
+
this.status = 'full synced';
|
41157
41170
|
this.peer.send(JSON.stringify({ ok: true }));
|
41158
41171
|
// this.pushBacklogResolve({ ok: true })
|
41159
41172
|
} else {
|
41160
|
-
|
41173
|
+
console.log('do send', carBlock.bytes.length);
|
41174
|
+
this.status = 'sending diff car';
|
41161
41175
|
this.peer.send(carBlock.bytes);
|
41162
41176
|
// this.pushBacklogResolve({ ok: true })
|
41163
41177
|
}
|
@@ -41165,10 +41179,18 @@ class Sync {
|
|
41165
41179
|
}
|
41166
41180
|
}
|
41167
41181
|
|
41182
|
+
destroy () {
|
41183
|
+
this.database.blocks.syncs.delete(this);
|
41184
|
+
this.status = 'destroyed';
|
41185
|
+
this.peer.destroy();
|
41186
|
+
}
|
41187
|
+
|
41168
41188
|
async sendUpdate (blockstore) {
|
41189
|
+
if (!this.peer) return
|
41169
41190
|
// console.log('send update from', this.database.instanceId)
|
41170
41191
|
// todo should send updates since last sync
|
41171
41192
|
const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore);
|
41193
|
+
this.status = 'sending update car';
|
41172
41194
|
this.peer.send(newCar.bytes);
|
41173
41195
|
}
|
41174
41196
|
|
@@ -41181,6 +41203,7 @@ class Sync {
|
|
41181
41203
|
cids: allCIDs.map(cid => cid.toString())
|
41182
41204
|
};
|
41183
41205
|
// console.log('send diff', reqCidDiff)
|
41206
|
+
this.status = 'sending cid diff';
|
41184
41207
|
this.peer.send(JSON.stringify(reqCidDiff));
|
41185
41208
|
}
|
41186
41209
|
|