@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.d.ts
CHANGED
@@ -13,7 +13,7 @@ import { CID } from 'multiformats';
|
|
13
13
|
* @param {object} [authCtx] - Optional authorization context object to use for any authentication checks.
|
14
14
|
*
|
15
15
|
*/
|
16
|
-
declare class Database
|
16
|
+
declare class Database {
|
17
17
|
constructor(blocks: any, clock: any, config?: {});
|
18
18
|
listeners: Set<any>;
|
19
19
|
indexes: Map<any, any>;
|
@@ -217,8 +217,8 @@ declare class DbIndex {
|
|
217
217
|
/**
|
218
218
|
* @param {Database} database
|
219
219
|
*/
|
220
|
-
constructor(database: Database
|
221
|
-
database: Database
|
220
|
+
constructor(database: Database, name: any, mapFn: any, clock?: any, opts?: {});
|
221
|
+
database: Database;
|
222
222
|
indexById: {
|
223
223
|
root: any;
|
224
224
|
cid: any;
|
@@ -331,10 +331,10 @@ declare class Listener {
|
|
331
331
|
* @param {import('./database.js').Database} database
|
332
332
|
* @param {(_: any, emit: any) => void} routingFn
|
333
333
|
*/
|
334
|
-
constructor(database: Database
|
334
|
+
constructor(database: Database, routingFn?: (_: any, emit: any) => void);
|
335
335
|
subcribers: Map<any, any>;
|
336
336
|
doStopListening: any;
|
337
|
-
database: Database
|
337
|
+
database: Database;
|
338
338
|
/**
|
339
339
|
* The map function to apply to each entry in the database.
|
340
340
|
* @type {Function}
|
@@ -368,15 +368,16 @@ declare class Sync {
|
|
368
368
|
* @param {import("./database.js").Database} database
|
369
369
|
* @param {string} key
|
370
370
|
*/
|
371
|
-
static makeCar(database: Database
|
371
|
+
static makeCar(database: Database, key: string, skip?: any[]): Promise<multiformats.BlockView<Uint8Array, 85, 18, 1>>;
|
372
|
+
constructor(database: any, PeerClass?: any);
|
372
373
|
/**
|
373
374
|
* @param {Database} database
|
374
375
|
* @param {typeof SimplePeer} [PeerClass]
|
375
376
|
* @memberof Sync
|
376
377
|
* @static
|
377
378
|
*/
|
378
|
-
|
379
|
-
database:
|
379
|
+
status: string;
|
380
|
+
database: any;
|
380
381
|
PeerClass: any;
|
381
382
|
pushBacklog: Promise<any>;
|
382
383
|
pushBacklogResolve: (value: any) => void;
|
@@ -388,10 +389,10 @@ declare class Sync {
|
|
388
389
|
peer: any;
|
389
390
|
backlog(): Promise<any>;
|
390
391
|
gotData(data: any): Promise<void>;
|
392
|
+
destroy(): void;
|
391
393
|
sendUpdate(blockstore: any): Promise<void>;
|
392
394
|
startSync(): Promise<void>;
|
393
395
|
}
|
394
|
-
type Database = Database$1;
|
395
396
|
|
396
397
|
declare class Fireproof {
|
397
398
|
/**
|
@@ -402,10 +403,10 @@ declare class Fireproof {
|
|
402
403
|
* @static
|
403
404
|
* @returns {Database} - a new Fireproof instance
|
404
405
|
*/
|
405
|
-
static storage: (name?: any, opts?: {}) => Database
|
406
|
+
static storage: (name?: any, opts?: {}) => Database;
|
406
407
|
static fromJSON(json: any, database: any): any;
|
407
408
|
static snapshot(database: any, clock: any): any;
|
408
409
|
static zoom(database: any, clock: any): Promise<any>;
|
409
410
|
}
|
410
411
|
|
411
|
-
export { Database
|
412
|
+
export { Database, Fireproof, DbIndex as Index, Listener, Sync };
|
package/dist/src/fireproof.js
CHANGED
@@ -37410,8 +37410,8 @@ class TransactionBlockstore {
|
|
37410
37410
|
if (doSync) {
|
37411
37411
|
// const all =
|
37412
37412
|
await Promise.all([...this.syncs].map(async sync => sync.sendUpdate(innerBlockstore).catch(e => {
|
37413
|
-
console.error('sync error', e);
|
37414
|
-
|
37413
|
+
console.error('sync error, cancelling', e);
|
37414
|
+
sync.destroy();
|
37415
37415
|
})));
|
37416
37416
|
}
|
37417
37417
|
}
|
@@ -41048,6 +41048,7 @@ class Sync {
|
|
41048
41048
|
* @memberof Sync
|
41049
41049
|
* @static
|
41050
41050
|
*/
|
41051
|
+
status = 'new'
|
41051
41052
|
constructor (database, PeerClass = simplePeer) {
|
41052
41053
|
this.database = database;
|
41053
41054
|
this.database.blocks.syncs.add(this); // should this happen during setup?
|
@@ -41063,6 +41064,7 @@ class Sync {
|
|
41063
41064
|
}
|
41064
41065
|
|
41065
41066
|
async offer () {
|
41067
|
+
this.status = 'offering';
|
41066
41068
|
return this.setupPeer(true)
|
41067
41069
|
}
|
41068
41070
|
|
@@ -41070,11 +41072,13 @@ class Sync {
|
|
41070
41072
|
const offer = JSON.parse(atob(base64offer));
|
41071
41073
|
const p = this.setupPeer(false);
|
41072
41074
|
this.peer.signal(offer);
|
41075
|
+
this.status = 'accepting';
|
41073
41076
|
return p
|
41074
41077
|
}
|
41075
41078
|
|
41076
41079
|
connect (base64accept) {
|
41077
41080
|
const accept = JSON.parse(atob(base64accept));
|
41081
|
+
this.status = 'connecting';
|
41078
41082
|
this.peer.signal(accept);
|
41079
41083
|
}
|
41080
41084
|
|
@@ -41105,39 +41109,46 @@ class Sync {
|
|
41105
41109
|
// console.log('not a car', data.toString())
|
41106
41110
|
}
|
41107
41111
|
if (reader) {
|
41112
|
+
console.log('got car');
|
41113
|
+
this.status = 'parking car';
|
41108
41114
|
const blz = new Set();
|
41109
41115
|
for await (const block of reader.blocks()) {
|
41110
41116
|
blz.add(block);
|
41111
41117
|
}
|
41112
41118
|
const roots = await reader.getRoots();
|
41113
|
-
|
41114
|
-
|
41115
|
-
|
41116
|
-
|
41117
|
-
|
41118
|
-
|
41119
|
-
|
41120
|
-
|
41121
|
-
|
41119
|
+
console.log(
|
41120
|
+
'got car',
|
41121
|
+
roots.map(c => c.toString()),
|
41122
|
+
this.database.clock.map(c => c.toString())
|
41123
|
+
);
|
41124
|
+
console.log(
|
41125
|
+
'got blocks',
|
41126
|
+
[...blz].map(({ cid }) => cid.toString())
|
41127
|
+
);
|
41122
41128
|
// @ts-ignore
|
41123
41129
|
reader.entries = reader.blocks;
|
41124
|
-
await this.database.blocks.commit(
|
41125
|
-
|
41126
|
-
|
41127
|
-
|
41128
|
-
|
41129
|
-
|
41130
|
+
await this.database.blocks.commit(
|
41131
|
+
{
|
41132
|
+
label: 'sync',
|
41133
|
+
entries: () => [...blz],
|
41134
|
+
get: async cid => await reader.get(cid),
|
41135
|
+
lastCid: [...blz][0].cid // doesn't matter
|
41136
|
+
},
|
41137
|
+
false
|
41138
|
+
);
|
41130
41139
|
// first arg could be the roots parents?
|
41131
41140
|
// get the roots parents
|
41132
|
-
const parents = await Promise.all(
|
41133
|
-
|
41134
|
-
|
41135
|
-
|
41136
|
-
|
41137
|
-
|
41138
|
-
|
41139
|
-
|
41140
|
-
|
41141
|
+
const parents = await Promise.all(
|
41142
|
+
roots.map(async cid => {
|
41143
|
+
const rbl = await reader.get(cid);
|
41144
|
+
if (!rbl) {
|
41145
|
+
console.log('missing root block', cid.toString(), reader);
|
41146
|
+
throw new Error('missing root block')
|
41147
|
+
}
|
41148
|
+
const block = await decodeEventBlock(rbl.bytes);
|
41149
|
+
return block.value.parents
|
41150
|
+
})
|
41151
|
+
);
|
41141
41152
|
this.database.applyClock(parents.flat(), roots);
|
41142
41153
|
this.database.notifyReset();
|
41143
41154
|
// console.log('after', this.database.clockToJSON())
|
@@ -41147,6 +41158,7 @@ class Sync {
|
|
41147
41158
|
const message = JSON.parse(data.toString());
|
41148
41159
|
// console.log('got message', message)
|
41149
41160
|
if (message.ok) {
|
41161
|
+
this.status = 'ok';
|
41150
41162
|
this.pushBacklogResolve({ ok: true });
|
41151
41163
|
} else if (message.clock) {
|
41152
41164
|
const reqCidDiff = message;
|
@@ -41154,12 +41166,14 @@ class Sync {
|
|
41154
41166
|
console.log('got diff', reqCidDiff);
|
41155
41167
|
const carBlock = await Sync.makeCar(this.database, null, reqCidDiff.cids);
|
41156
41168
|
if (!carBlock) {
|
41157
|
-
|
41158
|
-
|
41169
|
+
// we are full synced
|
41170
|
+
// console.log('we are full synced')
|
41171
|
+
this.status = 'full synced';
|
41159
41172
|
this.peer.send(JSON.stringify({ ok: true }));
|
41160
41173
|
// this.pushBacklogResolve({ ok: true })
|
41161
41174
|
} else {
|
41162
|
-
|
41175
|
+
console.log('do send', carBlock.bytes.length);
|
41176
|
+
this.status = 'sending diff car';
|
41163
41177
|
this.peer.send(carBlock.bytes);
|
41164
41178
|
// this.pushBacklogResolve({ ok: true })
|
41165
41179
|
}
|
@@ -41167,10 +41181,18 @@ class Sync {
|
|
41167
41181
|
}
|
41168
41182
|
}
|
41169
41183
|
|
41184
|
+
destroy () {
|
41185
|
+
this.database.blocks.syncs.delete(this);
|
41186
|
+
this.status = 'destroyed';
|
41187
|
+
this.peer.destroy();
|
41188
|
+
}
|
41189
|
+
|
41170
41190
|
async sendUpdate (blockstore) {
|
41191
|
+
if (!this.peer) return
|
41171
41192
|
// console.log('send update from', this.database.instanceId)
|
41172
41193
|
// todo should send updates since last sync
|
41173
41194
|
const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore);
|
41195
|
+
this.status = 'sending update car';
|
41174
41196
|
this.peer.send(newCar.bytes);
|
41175
41197
|
}
|
41176
41198
|
|
@@ -41183,6 +41205,7 @@ class Sync {
|
|
41183
41205
|
cids: allCIDs.map(cid => cid.toString())
|
41184
41206
|
};
|
41185
41207
|
// console.log('send diff', reqCidDiff)
|
41208
|
+
this.status = 'sending cid diff';
|
41186
41209
|
this.peer.send(JSON.stringify(reqCidDiff));
|
41187
41210
|
}
|
41188
41211
|
|