@fireproof/core 0.5.7 → 0.5.9
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 +109 -104
- package/dist/src/fireproof.js +309 -235
- package/dist/src/fireproof.js.map +1 -1
- package/dist/src/fireproof.mjs +309 -235
- package/dist/src/fireproof.mjs.map +1 -1
- package/package.json +1 -1
- package/src/blockstore.js +4 -1
- package/src/clock.js +41 -9
- package/src/database.js +33 -12
- package/src/db-index.js +14 -3
- package/src/fireproof.js +2 -2
- package/src/prolly.js +82 -48
- package/src/sync.js +23 -12
- package/src/valet.js +1 -1
package/dist/src/fireproof.mjs
CHANGED
@@ -32,17 +32,9 @@ function getAugmentedNamespace(n) {
|
|
32
32
|
return a;
|
33
33
|
}
|
34
34
|
|
35
|
-
var
|
36
|
-
var browser$d = {
|
37
|
-
get exports(){ return browserExports$1; },
|
38
|
-
set exports(v){ browserExports$1 = v; },
|
39
|
-
};
|
35
|
+
var browser$d = {exports: {}};
|
40
36
|
|
41
|
-
var
|
42
|
-
var safeBuffer$1 = {
|
43
|
-
get exports(){ return safeBufferExports; },
|
44
|
-
set exports(v){ safeBufferExports = v; },
|
45
|
-
};
|
37
|
+
var safeBuffer$1 = {exports: {}};
|
46
38
|
|
47
39
|
var global$1 = (typeof global !== "undefined" ? global :
|
48
40
|
typeof self !== "undefined" ? self :
|
@@ -2107,8 +2099,10 @@ var require$$6$1 = /*@__PURE__*/getAugmentedNamespace(_polyfillNode_buffer);
|
|
2107
2099
|
throw new TypeError('Argument must be a number')
|
2108
2100
|
}
|
2109
2101
|
return buffer.SlowBuffer(size)
|
2110
|
-
};
|
2111
|
-
} (safeBuffer$1,
|
2102
|
+
};
|
2103
|
+
} (safeBuffer$1, safeBuffer$1.exports));
|
2104
|
+
|
2105
|
+
var safeBufferExports = safeBuffer$1.exports;
|
2112
2106
|
|
2113
2107
|
// limit of Crypto.getRandomValues()
|
2114
2108
|
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
@@ -2159,6 +2153,9 @@ function randomBytes$2 (size, cb) {
|
|
2159
2153
|
return bytes
|
2160
2154
|
}
|
2161
2155
|
|
2156
|
+
var browserExports$1 = browser$d.exports;
|
2157
|
+
var randomBytes$3 = /*@__PURE__*/getDefaultExportFromCjs(browserExports$1);
|
2158
|
+
|
2162
2159
|
var encode_1$2 = encode$9;
|
2163
2160
|
|
2164
2161
|
var MSB$4 = 0x80
|
@@ -2241,13 +2238,13 @@ var length$2 = function (value) {
|
|
2241
2238
|
)
|
2242
2239
|
};
|
2243
2240
|
|
2244
|
-
var varint$
|
2241
|
+
var varint$3 = {
|
2245
2242
|
encode: encode_1$2
|
2246
2243
|
, decode: decode$d
|
2247
2244
|
, encodingLength: length$2
|
2248
2245
|
};
|
2249
2246
|
|
2250
|
-
var _brrp_varint$1 = varint$
|
2247
|
+
var _brrp_varint$1 = varint$3;
|
2251
2248
|
|
2252
2249
|
/**
|
2253
2250
|
* @param {Uint8Array} data
|
@@ -5666,7 +5663,11 @@ async function contains (events, a, b) {
|
|
5666
5663
|
*/
|
5667
5664
|
async function * vis$1 (blocks, head, options = {}) {
|
5668
5665
|
// @ts-ignore
|
5669
|
-
const renderNodeLabel = options.renderNodeLabel ?? ((b) =>
|
5666
|
+
const renderNodeLabel = options.renderNodeLabel ?? ((b) => {
|
5667
|
+
// @ts-ignore
|
5668
|
+
const { key, root, type } = b.value.data;
|
5669
|
+
return b.cid.toString() + '\n' + JSON.stringify({ key, root: root.cid.toString(), type }, null, 2).replace(/"/g, '\'')
|
5670
|
+
});
|
5670
5671
|
const events = new EventFetcher(blocks);
|
5671
5672
|
yield 'digraph clock {';
|
5672
5673
|
yield ' node [shape=point fontname="Courier"]; head;';
|
@@ -5707,6 +5708,7 @@ async function findEventsToSync (blocks, head) {
|
|
5707
5708
|
// console.time(callTag + '.contains')
|
5708
5709
|
const toSync = await asyncFilter(sorted, async (uks) => !(await contains(events, ancestor, uks.cid)));
|
5709
5710
|
// console.timeEnd(callTag + '.contains')
|
5711
|
+
// console.log('toSync.contains', toSync.length)
|
5710
5712
|
|
5711
5713
|
return { cids: events, events: toSync }
|
5712
5714
|
}
|
@@ -5714,17 +5716,21 @@ async function findEventsToSync (blocks, head) {
|
|
5714
5716
|
const asyncFilter = async (arr, predicate) =>
|
5715
5717
|
Promise.all(arr.map(predicate)).then((results) => arr.filter((_v, index) => results[index]));
|
5716
5718
|
|
5717
|
-
async function findCommonAncestorWithSortedEvents (events, children) {
|
5719
|
+
async function findCommonAncestorWithSortedEvents (events, children, doFull = false) {
|
5720
|
+
// console.trace('findCommonAncestorWithSortedEvents')
|
5718
5721
|
// const callTag = Math.random().toString(36).substring(7)
|
5722
|
+
// console.log(callTag + '.children', children.map((c) => c.toString()))
|
5719
5723
|
// console.time(callTag + '.findCommonAncestor')
|
5720
5724
|
const ancestor = await findCommonAncestor(events, children);
|
5721
5725
|
// console.timeEnd(callTag + '.findCommonAncestor')
|
5726
|
+
// console.log('ancestor', ancestor.toString())
|
5722
5727
|
if (!ancestor) {
|
5723
5728
|
throw new Error('failed to find common ancestor event')
|
5724
5729
|
}
|
5725
5730
|
// console.time(callTag + '.findSortedEvents')
|
5726
|
-
const sorted = await findSortedEvents(events, children, ancestor);
|
5731
|
+
const sorted = await findSortedEvents(events, children, ancestor, doFull);
|
5727
5732
|
// console.timeEnd(callTag + '.findSortedEvents')
|
5733
|
+
// console.log('sorted', sorted.length)
|
5728
5734
|
return { ancestor, sorted }
|
5729
5735
|
}
|
5730
5736
|
|
@@ -5737,6 +5743,7 @@ async function findCommonAncestorWithSortedEvents (events, children) {
|
|
5737
5743
|
*/
|
5738
5744
|
async function findCommonAncestor (events, children) {
|
5739
5745
|
if (!children.length) return
|
5746
|
+
if (children.length === 1) return children[0]
|
5740
5747
|
const candidates = children.map((c) => [c]);
|
5741
5748
|
while (true) {
|
5742
5749
|
let changed = false;
|
@@ -5757,7 +5764,7 @@ async function findCommonAncestor (events, children) {
|
|
5757
5764
|
* @param {import('./clock').EventLink<EventData>} root
|
5758
5765
|
*/
|
5759
5766
|
async function findAncestorCandidate (events, root) {
|
5760
|
-
const { value: event } = await events.get(root)
|
5767
|
+
const { value: event } = await events.get(root);// .catch(() => ({ value: { parents: [] } }))
|
5761
5768
|
if (!event.parents.length) return root
|
5762
5769
|
return event.parents.length === 1 ? event.parents[0] : findCommonAncestor(events, event.parents)
|
5763
5770
|
}
|
@@ -5767,6 +5774,7 @@ async function findAncestorCandidate (events, root) {
|
|
5767
5774
|
* @param {Array<T[]>} arrays
|
5768
5775
|
*/
|
5769
5776
|
function findCommonString (arrays) {
|
5777
|
+
// console.log('findCommonString', arrays.map((a) => a.map((i) => String(i))))
|
5770
5778
|
arrays = arrays.map((a) => [...a]);
|
5771
5779
|
for (const arr of arrays) {
|
5772
5780
|
for (const item of arr) {
|
@@ -5784,15 +5792,33 @@ function findCommonString (arrays) {
|
|
5784
5792
|
/**
|
5785
5793
|
* Find and sort events between the head(s) and the tail.
|
5786
5794
|
* @param {import('./clock').EventFetcher} events
|
5787
|
-
* @param {
|
5795
|
+
* @param {any[]} head
|
5788
5796
|
* @param {import('./clock').EventLink<EventData>} tail
|
5789
5797
|
*/
|
5790
|
-
async function findSortedEvents (events, head, tail) {
|
5798
|
+
async function findSortedEvents (events, head, tail, doFull) {
|
5791
5799
|
// const callTag = Math.random().toString(36).substring(7)
|
5792
5800
|
// get weighted events - heavier events happened first
|
5801
|
+
// const callTag = Math.random().toString(36).substring(7)
|
5802
|
+
|
5793
5803
|
/** @type {Map<string, { event: import('./clock').EventBlockView<EventData>, weight: number }>} */
|
5794
5804
|
const weights = new Map();
|
5805
|
+
head = [...new Set([...head.map((h) => h.toString())])];
|
5806
|
+
// console.log(callTag + '.head', head.length)
|
5807
|
+
|
5808
|
+
const allEvents = new Set([tail.toString(), ...head]);
|
5809
|
+
if (!doFull && allEvents.size === 1) {
|
5810
|
+
// console.log('head contains tail', tail.toString())
|
5811
|
+
return []
|
5812
|
+
// const event = await events.get(tail)
|
5813
|
+
// return [event]
|
5814
|
+
}
|
5815
|
+
|
5816
|
+
// console.log('finding events')
|
5817
|
+
// console.log(callTag + '.head', head.length, [...head.map((h) => h.toString())], tail.toString())
|
5818
|
+
|
5819
|
+
// console.time(callTag + '.findEvents')
|
5795
5820
|
const all = await Promise.all(head.map((h) => findEvents(events, h, tail)));
|
5821
|
+
// console.timeEnd(callTag + '.findEvents')
|
5796
5822
|
for (const arr of all) {
|
5797
5823
|
for (const { event, depth } of arr) {
|
5798
5824
|
// console.log('event value', event.value.data.value)
|
@@ -5821,7 +5847,7 @@ async function findSortedEvents (events, head, tail) {
|
|
5821
5847
|
const sorted = Array.from(buckets)
|
5822
5848
|
.sort((a, b) => b[0] - a[0])
|
5823
5849
|
.flatMap(([, es]) => es.sort((a, b) => (String(a.cid) < String(b.cid) ? -1 : 1)));
|
5824
|
-
// console.log('sorted', sorted.map(s => s.
|
5850
|
+
// console.log('sorted', sorted.map(s => s.cid))
|
5825
5851
|
|
5826
5852
|
return sorted
|
5827
5853
|
}
|
@@ -5833,11 +5859,14 @@ async function findSortedEvents (events, head, tail) {
|
|
5833
5859
|
* @returns {Promise<Array<{ event: EventBlockView<EventData>, depth: number }>>}
|
5834
5860
|
*/
|
5835
5861
|
async function findEvents (events, start, end, depth = 0) {
|
5836
|
-
// console.log('findEvents', start)
|
5862
|
+
// console.log('findEvents', start.toString(), end.toString(), depth)
|
5837
5863
|
const event = await events.get(start);
|
5864
|
+
const send = String(end);
|
5838
5865
|
const acc = [{ event, depth }];
|
5839
5866
|
const { parents } = event.value;
|
5840
|
-
if (parents.length === 1 && String(parents[0]) ===
|
5867
|
+
// if (parents.length === 1 && String(parents[0]) === send) return acc
|
5868
|
+
if (parents.findIndex((p) => String(p) === send) !== -1) return acc
|
5869
|
+
// if (parents.length === 1) return acc
|
5841
5870
|
const rest = await Promise.all(parents.map((p) => findEvents(events, p, end, depth + 1)));
|
5842
5871
|
return acc.concat(...rest)
|
5843
5872
|
}
|
@@ -7044,6 +7073,8 @@ var varint$1 = {
|
|
7044
7073
|
, encodingLength: length$1
|
7045
7074
|
};
|
7046
7075
|
|
7076
|
+
var varint$2 = /*@__PURE__*/getDefaultExportFromCjs(varint$1);
|
7077
|
+
|
7047
7078
|
const CIDV0_BYTES = {
|
7048
7079
|
SHA2_256: 0x12,
|
7049
7080
|
LENGTH: 0x20,
|
@@ -7068,8 +7099,8 @@ function decodeVarint (bytes, seeker) {
|
|
7068
7099
|
if (!bytes.length) {
|
7069
7100
|
throw new Error('Unexpected end of data')
|
7070
7101
|
}
|
7071
|
-
const i = varint$
|
7072
|
-
seeker.seek(/** @type {number} */(varint$
|
7102
|
+
const i = varint$2.decode(bytes);
|
7103
|
+
seeker.seek(/** @type {number} */(varint$2.decode.bytes));
|
7073
7104
|
return i
|
7074
7105
|
/* c8 ignore next 2 */
|
7075
7106
|
// Node.js 12 c8 bug
|
@@ -7120,10 +7151,10 @@ function getMultihashLength (bytes) {
|
|
7120
7151
|
// where both code and length are varints, so we have to decode
|
7121
7152
|
// them first before we can know total length
|
7122
7153
|
|
7123
|
-
varint$
|
7124
|
-
const codeLength = /** @type {number} */(varint$
|
7125
|
-
const length = varint$
|
7126
|
-
const lengthLength = /** @type {number} */(varint$
|
7154
|
+
varint$2.decode(bytes); // code
|
7155
|
+
const codeLength = /** @type {number} */(varint$2.decode.bytes);
|
7156
|
+
const length = varint$2.decode(bytes.subarray(varint$2.decode.bytes));
|
7157
|
+
const lengthLength = /** @type {number} */(varint$2.decode.bytes);
|
7127
7158
|
const mhLength = codeLength + lengthLength + length;
|
7128
7159
|
|
7129
7160
|
return mhLength
|
@@ -7831,7 +7862,7 @@ const addRoot = (writer, root, options = {}) => {
|
|
7831
7862
|
*/
|
7832
7863
|
const blockLength = ({ cid, bytes }) => {
|
7833
7864
|
const size = cid.bytes.byteLength + bytes.byteLength;
|
7834
|
-
return varint$
|
7865
|
+
return varint$2.encodingLength(size) + size
|
7835
7866
|
};
|
7836
7867
|
|
7837
7868
|
/**
|
@@ -7840,7 +7871,7 @@ const blockLength = ({ cid, bytes }) => {
|
|
7840
7871
|
*/
|
7841
7872
|
const addBlock = (writer, { cid, bytes }) => {
|
7842
7873
|
const byteLength = cid.bytes.byteLength + bytes.byteLength;
|
7843
|
-
const size = varint$
|
7874
|
+
const size = varint$2.encode(byteLength);
|
7844
7875
|
if (writer.byteOffset + size.length + byteLength > writer.bytes.byteLength) {
|
7845
7876
|
throw new RangeError('Buffer has no capacity for this block')
|
7846
7877
|
} else {
|
@@ -7860,7 +7891,7 @@ const close = (writer, options = {}) => {
|
|
7860
7891
|
const { roots, bytes, byteOffset, headerSize } = writer;
|
7861
7892
|
|
7862
7893
|
const headerBytes = encode$5({ version: 1, roots });
|
7863
|
-
const varintBytes = varint$
|
7894
|
+
const varintBytes = varint$2.encode(headerBytes.length);
|
7864
7895
|
|
7865
7896
|
const size = varintBytes.length + headerBytes.byteLength;
|
7866
7897
|
const offset = headerSize - size;
|
@@ -7937,7 +7968,7 @@ const calculateHeaderLength = (rootLengths) => {
|
|
7937
7968
|
tokens.push(new Token(Type.bytes, { length: rootLength + 1 }));
|
7938
7969
|
}
|
7939
7970
|
const length = tokensToLength(tokens); // no options needed here because we have simple tokens
|
7940
|
-
return varint$
|
7971
|
+
return varint$2.encodingLength(length) + length
|
7941
7972
|
};
|
7942
7973
|
|
7943
7974
|
/**
|
@@ -8280,23 +8311,11 @@ replaceTraps((oldTraps) => ({
|
|
8280
8311
|
has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
|
8281
8312
|
}));
|
8282
8313
|
|
8283
|
-
var
|
8284
|
-
var cargoQueue$1 = {
|
8285
|
-
get exports(){ return cargoQueueExports; },
|
8286
|
-
set exports(v){ cargoQueueExports = v; },
|
8287
|
-
};
|
8314
|
+
var cargoQueue$1 = {exports: {}};
|
8288
8315
|
|
8289
|
-
var
|
8290
|
-
var queue$1 = {
|
8291
|
-
get exports(){ return queueExports; },
|
8292
|
-
set exports(v){ queueExports = v; },
|
8293
|
-
};
|
8316
|
+
var queue$1 = {exports: {}};
|
8294
8317
|
|
8295
|
-
var
|
8296
|
-
var onlyOnce = {
|
8297
|
-
get exports(){ return onlyOnceExports; },
|
8298
|
-
set exports(v){ onlyOnceExports = v; },
|
8299
|
-
};
|
8318
|
+
var onlyOnce = {exports: {}};
|
8300
8319
|
|
8301
8320
|
(function (module, exports) {
|
8302
8321
|
|
@@ -8312,8 +8331,10 @@ var onlyOnce = {
|
|
8312
8331
|
callFn.apply(this, args);
|
8313
8332
|
};
|
8314
8333
|
}
|
8315
|
-
module.exports = exports["default"];
|
8316
|
-
} (onlyOnce,
|
8334
|
+
module.exports = exports["default"];
|
8335
|
+
} (onlyOnce, onlyOnce.exports));
|
8336
|
+
|
8337
|
+
var onlyOnceExports = onlyOnce.exports;
|
8317
8338
|
|
8318
8339
|
var setImmediate$1 = {};
|
8319
8340
|
|
@@ -8350,11 +8371,7 @@ if (hasQueueMicrotask) {
|
|
8350
8371
|
|
8351
8372
|
setImmediate$1.default = wrap(_defer);
|
8352
8373
|
|
8353
|
-
var
|
8354
|
-
var DoublyLinkedList = {
|
8355
|
-
get exports(){ return DoublyLinkedListExports; },
|
8356
|
-
set exports(v){ DoublyLinkedListExports = v; },
|
8357
|
-
};
|
8374
|
+
var DoublyLinkedList = {exports: {}};
|
8358
8375
|
|
8359
8376
|
(function (module, exports) {
|
8360
8377
|
|
@@ -8447,22 +8464,16 @@ var DoublyLinkedList = {
|
|
8447
8464
|
dll.length = 1;
|
8448
8465
|
dll.head = dll.tail = node;
|
8449
8466
|
}
|
8450
|
-
module.exports = exports["default"];
|
8451
|
-
} (DoublyLinkedList,
|
8467
|
+
module.exports = exports["default"];
|
8468
|
+
} (DoublyLinkedList, DoublyLinkedList.exports));
|
8469
|
+
|
8470
|
+
var DoublyLinkedListExports = DoublyLinkedList.exports;
|
8452
8471
|
|
8453
8472
|
var wrapAsync = {};
|
8454
8473
|
|
8455
|
-
var
|
8456
|
-
var asyncify = {
|
8457
|
-
get exports(){ return asyncifyExports; },
|
8458
|
-
set exports(v){ asyncifyExports = v; },
|
8459
|
-
};
|
8474
|
+
var asyncify = {exports: {}};
|
8460
8475
|
|
8461
|
-
var
|
8462
|
-
var initialParams = {
|
8463
|
-
get exports(){ return initialParamsExports; },
|
8464
|
-
set exports(v){ initialParamsExports = v; },
|
8465
|
-
};
|
8476
|
+
var initialParams = {exports: {}};
|
8466
8477
|
|
8467
8478
|
(function (module, exports) {
|
8468
8479
|
|
@@ -8477,13 +8488,15 @@ var initialParams = {
|
|
8477
8488
|
};
|
8478
8489
|
};
|
8479
8490
|
|
8480
|
-
module.exports = exports["default"];
|
8481
|
-
} (initialParams,
|
8491
|
+
module.exports = exports["default"];
|
8492
|
+
} (initialParams, initialParams.exports));
|
8493
|
+
|
8494
|
+
var initialParamsExports = initialParams.exports;
|
8482
8495
|
|
8483
8496
|
var hasRequiredAsyncify;
|
8484
8497
|
|
8485
8498
|
function requireAsyncify () {
|
8486
|
-
if (hasRequiredAsyncify) return
|
8499
|
+
if (hasRequiredAsyncify) return asyncify.exports;
|
8487
8500
|
hasRequiredAsyncify = 1;
|
8488
8501
|
(function (module, exports) {
|
8489
8502
|
|
@@ -8602,9 +8615,9 @@ function requireAsyncify () {
|
|
8602
8615
|
}, err);
|
8603
8616
|
}
|
8604
8617
|
}
|
8605
|
-
module.exports = exports['default'];
|
8606
|
-
} (asyncify,
|
8607
|
-
return
|
8618
|
+
module.exports = exports['default'];
|
8619
|
+
} (asyncify, asyncify.exports));
|
8620
|
+
return asyncify.exports;
|
8608
8621
|
}
|
8609
8622
|
|
8610
8623
|
var hasRequiredWrapAsync;
|
@@ -8941,8 +8954,10 @@ function requireWrapAsync () {
|
|
8941
8954
|
});
|
8942
8955
|
return q;
|
8943
8956
|
}
|
8944
|
-
module.exports = exports['default'];
|
8945
|
-
} (queue$1,
|
8957
|
+
module.exports = exports['default'];
|
8958
|
+
} (queue$1, queue$1.exports));
|
8959
|
+
|
8960
|
+
var queueExports = queue$1.exports;
|
8946
8961
|
|
8947
8962
|
(function (module, exports) {
|
8948
8963
|
|
@@ -9014,9 +9029,10 @@ function requireWrapAsync () {
|
|
9014
9029
|
function cargo(worker, concurrency, payload) {
|
9015
9030
|
return (0, _queue2.default)(worker, concurrency, payload);
|
9016
9031
|
}
|
9017
|
-
module.exports = exports['default'];
|
9018
|
-
} (cargoQueue$1,
|
9032
|
+
module.exports = exports['default'];
|
9033
|
+
} (cargoQueue$1, cargoQueue$1.exports));
|
9019
9034
|
|
9035
|
+
var cargoQueueExports = cargoQueue$1.exports;
|
9020
9036
|
var cargoQueue = /*@__PURE__*/getDefaultExportFromCjs(cargoQueueExports);
|
9021
9037
|
|
9022
9038
|
var cryptoBrowserify = {};
|
@@ -9053,11 +9069,7 @@ var _polyfillNode_inherits = /*#__PURE__*/Object.freeze({
|
|
9053
9069
|
|
9054
9070
|
var require$$3$1 = /*@__PURE__*/getAugmentedNamespace(_polyfillNode_inherits);
|
9055
9071
|
|
9056
|
-
var
|
9057
|
-
var readableBrowser = {
|
9058
|
-
get exports(){ return readableBrowserExports; },
|
9059
|
-
set exports(v){ readableBrowserExports = v; },
|
9060
|
-
};
|
9072
|
+
var readableBrowser = {exports: {}};
|
9061
9073
|
|
9062
9074
|
var domain;
|
9063
9075
|
|
@@ -13454,8 +13466,10 @@ var pipeline_1 = pipeline;
|
|
13454
13466
|
exports.Transform = _stream_transform;
|
13455
13467
|
exports.PassThrough = _stream_passthrough;
|
13456
13468
|
exports.finished = endOfStream;
|
13457
|
-
exports.pipeline = pipeline_1;
|
13458
|
-
} (readableBrowser,
|
13469
|
+
exports.pipeline = pipeline_1;
|
13470
|
+
} (readableBrowser, readableBrowser.exports));
|
13471
|
+
|
13472
|
+
var readableBrowserExports = readableBrowser.exports;
|
13459
13473
|
|
13460
13474
|
var Buffer$E = safeBufferExports.Buffer;
|
13461
13475
|
var Transform$6 = readableBrowserExports.Transform;
|
@@ -13861,11 +13875,7 @@ function fn5 (a, b, c, d, e, m, k, s) {
|
|
13861
13875
|
|
13862
13876
|
var ripemd160 = RIPEMD160$4;
|
13863
13877
|
|
13864
|
-
var
|
13865
|
-
var sha_js = {
|
13866
|
-
get exports(){ return sha_jsExports; },
|
13867
|
-
set exports(v){ sha_jsExports = v; },
|
13868
|
-
};
|
13878
|
+
var sha_js = {exports: {}};
|
13869
13879
|
|
13870
13880
|
var Buffer$B = safeBufferExports.Buffer;
|
13871
13881
|
|
@@ -14669,6 +14679,8 @@ exports.sha256 = sha256$1;
|
|
14669
14679
|
exports.sha384 = sha384$1;
|
14670
14680
|
exports.sha512 = sha512$1;
|
14671
14681
|
|
14682
|
+
var sha_jsExports = sha_js.exports;
|
14683
|
+
|
14672
14684
|
function BufferList() {
|
14673
14685
|
this.head = null;
|
14674
14686
|
this.tail = null;
|
@@ -16619,12 +16631,6 @@ var browser$9 = function createHmac (alg, key) {
|
|
16619
16631
|
return new Hmac$2(alg, key)
|
16620
16632
|
};
|
16621
16633
|
|
16622
|
-
var algosExports = {};
|
16623
|
-
var algos = {
|
16624
|
-
get exports(){ return algosExports; },
|
16625
|
-
set exports(v){ algosExports = v; },
|
16626
|
-
};
|
16627
|
-
|
16628
16634
|
var sha224WithRSAEncryption = {
|
16629
16635
|
sign: "rsa",
|
16630
16636
|
hash: "sha224",
|
@@ -16789,9 +16795,7 @@ var require$$6 = {
|
|
16789
16795
|
}
|
16790
16796
|
};
|
16791
16797
|
|
16792
|
-
|
16793
|
-
module.exports = require$$6;
|
16794
|
-
} (algos));
|
16798
|
+
var algos = require$$6;
|
16795
16799
|
|
16796
16800
|
var browser$8 = {};
|
16797
16801
|
|
@@ -19031,7 +19035,7 @@ var modes = {};
|
|
19031
19035
|
exports['des-ede'] = {
|
19032
19036
|
key: 16,
|
19033
19037
|
iv: 0
|
19034
|
-
};
|
19038
|
+
};
|
19035
19039
|
} (modes));
|
19036
19040
|
|
19037
19041
|
var DES = browserifyDes;
|
@@ -19104,11 +19108,9 @@ browser$7.listCiphers = browser$7.getCiphers = getCiphers;
|
|
19104
19108
|
|
19105
19109
|
var browser$5 = {};
|
19106
19110
|
|
19107
|
-
var
|
19108
|
-
|
19109
|
-
|
19110
|
-
set exports(v){ bnExports$1 = v; },
|
19111
|
-
};
|
19111
|
+
var bn$1 = {exports: {}};
|
19112
|
+
|
19113
|
+
bn$1.exports;
|
19112
19114
|
|
19113
19115
|
(function (module) {
|
19114
19116
|
(function (module, exports) {
|
@@ -22553,19 +22555,17 @@ var bn$1 = {
|
|
22553
22555
|
var res = this.imod(a._invmp(this.m).mul(this.r2));
|
22554
22556
|
return res._forceRed(this);
|
22555
22557
|
};
|
22556
|
-
})(module, commonjsGlobal);
|
22558
|
+
})(module, commonjsGlobal);
|
22557
22559
|
} (bn$1));
|
22558
22560
|
|
22559
|
-
var
|
22560
|
-
|
22561
|
-
|
22562
|
-
set exports(v){ brorandExports = v; },
|
22563
|
-
};
|
22561
|
+
var bnExports$1 = bn$1.exports;
|
22562
|
+
|
22563
|
+
var brorand = {exports: {}};
|
22564
22564
|
|
22565
22565
|
var hasRequiredBrorand;
|
22566
22566
|
|
22567
22567
|
function requireBrorand () {
|
22568
|
-
if (hasRequiredBrorand) return
|
22568
|
+
if (hasRequiredBrorand) return brorand.exports;
|
22569
22569
|
hasRequiredBrorand = 1;
|
22570
22570
|
var r;
|
22571
22571
|
|
@@ -22579,7 +22579,7 @@ function requireBrorand () {
|
|
22579
22579
|
function Rand(rand) {
|
22580
22580
|
this.rand = rand;
|
22581
22581
|
}
|
22582
|
-
|
22582
|
+
brorand.exports.Rand = Rand;
|
22583
22583
|
|
22584
22584
|
Rand.prototype.generate = function generate(len) {
|
22585
22585
|
return this._rand(len);
|
@@ -22632,7 +22632,7 @@ function requireBrorand () {
|
|
22632
22632
|
} catch (e) {
|
22633
22633
|
}
|
22634
22634
|
}
|
22635
|
-
return
|
22635
|
+
return brorand.exports;
|
22636
22636
|
}
|
22637
22637
|
|
22638
22638
|
var mr;
|
@@ -23139,17 +23139,11 @@ function requireBrowser$2 () {
|
|
23139
23139
|
return browser$5;
|
23140
23140
|
}
|
23141
23141
|
|
23142
|
-
var
|
23143
|
-
var sign = {
|
23144
|
-
get exports(){ return signExports; },
|
23145
|
-
set exports(v){ signExports = v; },
|
23146
|
-
};
|
23142
|
+
var sign = {exports: {}};
|
23147
23143
|
|
23148
|
-
var
|
23149
|
-
|
23150
|
-
|
23151
|
-
set exports(v){ bnExports = v; },
|
23152
|
-
};
|
23144
|
+
var bn = {exports: {}};
|
23145
|
+
|
23146
|
+
bn.exports;
|
23153
23147
|
|
23154
23148
|
(function (module) {
|
23155
23149
|
(function (module, exports) {
|
@@ -26499,9 +26493,11 @@ var bn = {
|
|
26499
26493
|
var res = this.imod(a._invmp(this.m).mul(this.r2));
|
26500
26494
|
return res._forceRed(this);
|
26501
26495
|
};
|
26502
|
-
})(module, commonjsGlobal);
|
26496
|
+
})(module, commonjsGlobal);
|
26503
26497
|
} (bn));
|
26504
26498
|
|
26499
|
+
var bnExports = bn.exports;
|
26500
|
+
|
26505
26501
|
var BN$a = bnExports;
|
26506
26502
|
var randomBytes$1 = browserExports$1;
|
26507
26503
|
|
@@ -26672,7 +26668,7 @@ var utils$k = {};
|
|
26672
26668
|
return toHex(arr);
|
26673
26669
|
else
|
26674
26670
|
return arr;
|
26675
|
-
};
|
26671
|
+
};
|
26676
26672
|
} (utils$k));
|
26677
26673
|
|
26678
26674
|
(function (exports) {
|
@@ -26792,7 +26788,7 @@ var utils$k = {};
|
|
26792
26788
|
function intFromLE(bytes) {
|
26793
26789
|
return new BN(bytes, 'hex', 'le');
|
26794
26790
|
}
|
26795
|
-
utils.intFromLE = intFromLE;
|
26791
|
+
utils.intFromLE = intFromLE;
|
26796
26792
|
} (utils$l));
|
26797
26793
|
|
26798
26794
|
var curve = {};
|
@@ -28731,7 +28727,7 @@ Point.prototype.mixedAdd = Point.prototype.add;
|
|
28731
28727
|
curve.base = base$2;
|
28732
28728
|
curve.short = short;
|
28733
28729
|
curve.mont = mont;
|
28734
|
-
curve.edwards = edwards;
|
28730
|
+
curve.edwards = edwards;
|
28735
28731
|
} (curve));
|
28736
28732
|
|
28737
28733
|
var curves$1 = {};
|
@@ -29944,7 +29940,7 @@ Hmac.prototype.digest = function digest(enc) {
|
|
29944
29940
|
hash.sha224 = hash.sha.sha224;
|
29945
29941
|
hash.sha384 = hash.sha.sha384;
|
29946
29942
|
hash.sha512 = hash.sha.sha512;
|
29947
|
-
hash.ripemd160 = hash.ripemd.ripemd160;
|
29943
|
+
hash.ripemd160 = hash.ripemd.ripemd160;
|
29948
29944
|
} (hash$2));
|
29949
29945
|
|
29950
29946
|
var secp256k1;
|
@@ -30941,7 +30937,7 @@ function requireSecp256k1 () {
|
|
30941
30937
|
'483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
30942
30938
|
pre,
|
30943
30939
|
],
|
30944
|
-
});
|
30940
|
+
});
|
30945
30941
|
} (curves$1));
|
30946
30942
|
|
30947
30943
|
var hash$1 = hash$2;
|
@@ -31884,8 +31880,8 @@ function requireElliptic () {
|
|
31884
31880
|
|
31885
31881
|
// Protocols
|
31886
31882
|
elliptic.ec = requireEc();
|
31887
|
-
elliptic.eddsa = eddsa;
|
31888
|
-
} (elliptic));
|
31883
|
+
elliptic.eddsa = eddsa;
|
31884
|
+
} (elliptic));
|
31889
31885
|
return elliptic;
|
31890
31886
|
}
|
31891
31887
|
|
@@ -32945,7 +32941,7 @@ var der$2 = {};
|
|
32945
32941
|
0x1d: 'charstr',
|
32946
32942
|
0x1e: 'bmpstr'
|
32947
32943
|
};
|
32948
|
-
exports.tagByName = reverse(exports.tag);
|
32944
|
+
exports.tagByName = reverse(exports.tag);
|
32949
32945
|
} (der$2));
|
32950
32946
|
|
32951
32947
|
const inherits$3 = require$$3$1;
|
@@ -33269,7 +33265,7 @@ PEMEncoder.prototype.encode = function encode(data, options) {
|
|
33269
33265
|
const encoders = exports;
|
33270
33266
|
|
33271
33267
|
encoders.der = der_1$1;
|
33272
|
-
encoders.pem = pem$1;
|
33268
|
+
encoders.pem = pem$1;
|
33273
33269
|
} (encoders));
|
33274
33270
|
|
33275
33271
|
var decoders = {};
|
@@ -33663,7 +33659,7 @@ PEMDecoder.prototype.decode = function decode(data, options) {
|
|
33663
33659
|
const decoders = exports;
|
33664
33660
|
|
33665
33661
|
decoders.der = der_1;
|
33666
|
-
decoders.pem = pem;
|
33662
|
+
decoders.pem = pem;
|
33667
33663
|
} (decoders));
|
33668
33664
|
|
33669
33665
|
(function (exports) {
|
@@ -33722,7 +33718,7 @@ PEMDecoder.prototype.decode = function decode(data, options) {
|
|
33722
33718
|
|
33723
33719
|
Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) {
|
33724
33720
|
return this._getEncoder(enc).encode(data, reporter);
|
33725
|
-
};
|
33721
|
+
};
|
33726
33722
|
} (api));
|
33727
33723
|
|
33728
33724
|
var base$1 = {};
|
@@ -33734,7 +33730,7 @@ var base$1 = {};
|
|
33734
33730
|
base.Reporter = reporter.Reporter;
|
33735
33731
|
base.DecoderBuffer = buffer.DecoderBuffer;
|
33736
33732
|
base.EncoderBuffer = buffer.EncoderBuffer;
|
33737
|
-
base.Node = node;
|
33733
|
+
base.Node = node;
|
33738
33734
|
} (base$1));
|
33739
33735
|
|
33740
33736
|
var constants = {};
|
@@ -33759,7 +33755,7 @@ var constants = {};
|
|
33759
33755
|
return res;
|
33760
33756
|
};
|
33761
33757
|
|
33762
|
-
constants.der = der$2;
|
33758
|
+
constants.der = der$2;
|
33763
33759
|
} (constants));
|
33764
33760
|
|
33765
33761
|
(function (exports) {
|
@@ -33772,7 +33768,7 @@ var constants = {};
|
|
33772
33768
|
asn1.base = base$1;
|
33773
33769
|
asn1.constants = constants;
|
33774
33770
|
asn1.decoders = decoders;
|
33775
|
-
asn1.encoders = encoders;
|
33771
|
+
asn1.encoders = encoders;
|
33776
33772
|
} (asn1$2));
|
33777
33773
|
|
33778
33774
|
var asn = asn1$2;
|
@@ -34146,7 +34142,7 @@ var require$$4 = {
|
|
34146
34142
|
var hasRequiredSign;
|
34147
34143
|
|
34148
34144
|
function requireSign () {
|
34149
|
-
if (hasRequiredSign) return
|
34145
|
+
if (hasRequiredSign) return sign.exports;
|
34150
34146
|
hasRequiredSign = 1;
|
34151
34147
|
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
|
34152
34148
|
var Buffer = safeBufferExports.Buffer;
|
@@ -34289,9 +34285,9 @@ function requireSign () {
|
|
34289
34285
|
}
|
34290
34286
|
|
34291
34287
|
sign.exports = sign$1;
|
34292
|
-
|
34293
|
-
|
34294
|
-
return
|
34288
|
+
sign.exports.getKey = getKey;
|
34289
|
+
sign.exports.makeKey = makeKey;
|
34290
|
+
return sign.exports;
|
34295
34291
|
}
|
34296
34292
|
|
34297
34293
|
var verify_1;
|
@@ -34870,7 +34866,7 @@ function compare$3 (a, b) {
|
|
34870
34866
|
|
34871
34867
|
exports.publicDecrypt = function publicDecrypt (key, buf) {
|
34872
34868
|
return exports.privateDecrypt(key, buf, true)
|
34873
|
-
};
|
34869
|
+
};
|
34874
34870
|
} (browser$2));
|
34875
34871
|
|
34876
34872
|
var browser$1 = {};
|
@@ -34992,8 +34988,8 @@ function requireCryptoBrowserify () {
|
|
34992
34988
|
cryptoBrowserify.createHash = cryptoBrowserify.Hash = browser$a;
|
34993
34989
|
cryptoBrowserify.createHmac = cryptoBrowserify.Hmac = browser$9;
|
34994
34990
|
|
34995
|
-
var algos =
|
34996
|
-
var algoKeys = Object.keys(algos);
|
34991
|
+
var algos$1 = algos;
|
34992
|
+
var algoKeys = Object.keys(algos$1);
|
34997
34993
|
var hashes = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512', 'md5', 'rmd160'].concat(algoKeys);
|
34998
34994
|
cryptoBrowserify.getHashes = function () {
|
34999
34995
|
return hashes
|
@@ -36926,7 +36922,7 @@ function rawSha1 (b) {
|
|
36926
36922
|
return new Uint8Array(H.buffer, H.byteOffset, H.byteLength)
|
36927
36923
|
}
|
36928
36924
|
|
36929
|
-
const chunker = bf(
|
36925
|
+
const chunker = bf(30);
|
36930
36926
|
|
36931
36927
|
const NO_ENCRYPT = typeof process !== 'undefined' && !!process.env?.NO_ENCRYPT;
|
36932
36928
|
// ? process.env.NO_ENCRYPT : import.meta && import.meta.env.VITE_NO_ENCRYPT
|
@@ -37375,7 +37371,10 @@ class TransactionBlockstore {
|
|
37375
37371
|
await this.doCommit(innerBlockstore);
|
37376
37372
|
if (doSync) {
|
37377
37373
|
// const all =
|
37378
|
-
await Promise.all([...this.syncs].map(async sync => sync.sendUpdate(innerBlockstore)
|
37374
|
+
await Promise.all([...this.syncs].map(async sync => sync.sendUpdate(innerBlockstore).catch(e => {
|
37375
|
+
console.error('sync error', e);
|
37376
|
+
this.syncs.delete(sync);
|
37377
|
+
})));
|
37379
37378
|
}
|
37380
37379
|
}
|
37381
37380
|
|
@@ -37482,23 +37481,24 @@ class InnerBlockstore {
|
|
37482
37481
|
}
|
37483
37482
|
}
|
37484
37483
|
|
37485
|
-
const blockOpts = { cache: nocache, chunker: bf(
|
37484
|
+
const blockOpts = { cache: nocache, chunker: bf(30), codec: codec$1, hasher: sha256$2, compare: simpleCompare };
|
37486
37485
|
|
37487
37486
|
/**
|
37488
37487
|
* @typedef {import('./blockstore.js').TransactionBlockstore} TransactionBlockstore
|
37489
37488
|
*/
|
37490
37489
|
|
37491
|
-
const withLog = async (label, fn) => {
|
37492
|
-
|
37493
|
-
|
37494
|
-
|
37495
|
-
}
|
37490
|
+
// const withLog = async (label, fn) => {
|
37491
|
+
// const resp = await fn()
|
37492
|
+
// // console.log('withLog', label, !!resp)
|
37493
|
+
// return resp
|
37494
|
+
// }
|
37496
37495
|
|
37497
37496
|
// should also return a CIDCounter
|
37498
|
-
const makeGetBlock =
|
37497
|
+
const makeGetBlock = blocks => {
|
37499
37498
|
// const cids = new CIDCounter() // this could be used for proofs of mutations
|
37500
|
-
const getBlockFn = async
|
37501
|
-
const { cid, bytes } = await withLog(address, () => blocks.get(address))
|
37499
|
+
const getBlockFn = async address => {
|
37500
|
+
// const { cid, bytes } = await withLog(address, () => blocks.get(address))
|
37501
|
+
const { cid, bytes } = await blocks.get(address);
|
37502
37502
|
// cids.add({ address: cid })
|
37503
37503
|
return create$5({ cid, bytes, hasher: sha256$2, codec: codec$1 })
|
37504
37504
|
};
|
@@ -37513,25 +37513,17 @@ const makeGetBlock = (blocks) => {
|
|
37513
37513
|
* @param {*} param0
|
37514
37514
|
* @returns
|
37515
37515
|
*/
|
37516
|
-
async function createAndSaveNewEvent ({
|
37517
|
-
inBlocks,
|
37518
|
-
bigPut,
|
37519
|
-
root,
|
37520
|
-
event: inEvent,
|
37521
|
-
head,
|
37522
|
-
additions,
|
37523
|
-
removals = []
|
37524
|
-
}) {
|
37516
|
+
async function createAndSaveNewEvent ({ inBlocks, bigPut, root, event: inEvent, head, additions, removals = [] }) {
|
37525
37517
|
let cids;
|
37526
37518
|
const { key, value, del } = inEvent;
|
37527
37519
|
const data = {
|
37528
|
-
root:
|
37520
|
+
root: root
|
37529
37521
|
? {
|
37530
37522
|
cid: root.cid,
|
37531
37523
|
bytes: root.bytes, // can we remove this?
|
37532
37524
|
value: root.value // can we remove this?
|
37533
37525
|
}
|
37534
|
-
: null
|
37526
|
+
: null,
|
37535
37527
|
key
|
37536
37528
|
};
|
37537
37529
|
// import('./clock').EventLink<import('./clock').EventData>
|
@@ -37559,7 +37551,7 @@ async function createAndSaveNewEvent ({
|
|
37559
37551
|
}
|
37560
37552
|
}
|
37561
37553
|
|
37562
|
-
const makeGetAndPutBlock =
|
37554
|
+
const makeGetAndPutBlock = inBlocks => {
|
37563
37555
|
// const mblocks = new MemoryBlockstore()
|
37564
37556
|
// const blocks = new MultiBlockFetcher(mblocks, inBlocks)
|
37565
37557
|
const { getBlock, cids } = makeGetBlock(inBlocks);
|
@@ -37618,20 +37610,24 @@ const prollyRootFromAncestor = async (events, ancestor, getBlock) => {
|
|
37618
37610
|
}
|
37619
37611
|
};
|
37620
37612
|
|
37621
|
-
const doProllyBulk = async (inBlocks, head, event) => {
|
37613
|
+
const doProllyBulk = async (inBlocks, head, event, doFull = false) => {
|
37622
37614
|
const { getBlock, blocks } = makeGetAndPutBlock(inBlocks);
|
37623
37615
|
let bulkSorted = [];
|
37624
37616
|
let prollyRootNode = null;
|
37625
37617
|
const events = new EventFetcher(blocks);
|
37626
37618
|
if (head.length) {
|
37627
|
-
|
37619
|
+
if (!doFull && head.length === 1) {
|
37620
|
+
prollyRootNode = await prollyRootFromAncestor(events, head[0], getBlock);
|
37621
|
+
} else {
|
37622
|
+
// Otherwise, we find the common ancestor and update the root and other blocks
|
37628
37623
|
// todo this is returning more events than necessary, lets define the desired semantics from the top down
|
37629
37624
|
// good semantics mean we can cache the results of this call
|
37630
|
-
|
37631
|
-
|
37632
|
-
|
37633
|
-
|
37625
|
+
const { ancestor, sorted } = await findCommonAncestorWithSortedEvents(events, head, doFull);
|
37626
|
+
bulkSorted = sorted;
|
37627
|
+
// console.log('sorted', JSON.stringify(sorted.map(({ value: { data: { key, value } } }) => ({ key, value }))))
|
37628
|
+
prollyRootNode = await prollyRootFromAncestor(events, ancestor, getBlock);
|
37634
37629
|
// console.log('event', event)
|
37630
|
+
}
|
37635
37631
|
}
|
37636
37632
|
|
37637
37633
|
const bulkOperations = bulkFromEvents(bulkSorted, event);
|
@@ -37641,7 +37637,7 @@ const doProllyBulk = async (inBlocks, head, event) => {
|
|
37641
37637
|
let root;
|
37642
37638
|
const newBlocks = [];
|
37643
37639
|
// if all operations are deletes, we can just return an empty root
|
37644
|
-
if (bulkOperations.every(
|
37640
|
+
if (bulkOperations.every(op => op.del)) {
|
37645
37641
|
return { root: null, blocks: [], clockCIDs: await events.all() }
|
37646
37642
|
}
|
37647
37643
|
for await (const node of create$3({ get: getBlock, list: bulkOperations, ...blockOpts })) {
|
@@ -37661,7 +37657,7 @@ const doProllyBulk = async (inBlocks, head, event) => {
|
|
37661
37657
|
*
|
37662
37658
|
* @param {import('./blockstore.js').Blockstore} inBlocks Bucket block storage.
|
37663
37659
|
* @param {import('./clock').EventLink<import('./clock').EventData>[]} head Merkle clock head.
|
37664
|
-
* @param {{key: string, value: import('./clock').EventLink<import('./clock').EventData>}} event The key of the value to put.
|
37660
|
+
* @param {{key: string, value: import('./clock').EventLink<import('./clock').EventData>}} event The key of the value to put.
|
37665
37661
|
* @param {object} [options]
|
37666
37662
|
* @returns {Promise<any>}
|
37667
37663
|
*/
|
@@ -37713,18 +37709,24 @@ async function put (inBlocks, head, event, options) {
|
|
37713
37709
|
* @param {TransactionBlockstore} inBlocks Bucket block storage.
|
37714
37710
|
* @param {import('./clock').EventLink<import('./clock').EventData>[]} head Merkle clock head.
|
37715
37711
|
*/
|
37716
|
-
async function root (inBlocks, head) {
|
37712
|
+
async function root (inBlocks, head, doFull = false) {
|
37717
37713
|
if (!head.length) {
|
37718
37714
|
throw new Error('no head')
|
37719
37715
|
}
|
37720
|
-
|
37716
|
+
// console.log('root', head.map(h => h.toString()))
|
37717
|
+
const { root: newProllyRootNode, blocks: newBlocks, clockCIDs } = await doProllyBulk(inBlocks, head, null, doFull);
|
37721
37718
|
// todo maybe these should go to a temp blockstore?
|
37722
|
-
await doTransaction(
|
37723
|
-
|
37724
|
-
|
37725
|
-
|
37726
|
-
|
37727
|
-
|
37719
|
+
await doTransaction(
|
37720
|
+
'root',
|
37721
|
+
inBlocks,
|
37722
|
+
async transactionBlocks => {
|
37723
|
+
const { bigPut } = makeGetAndPutBlock(transactionBlocks);
|
37724
|
+
for (const nb of newBlocks) {
|
37725
|
+
bigPut(nb);
|
37726
|
+
}
|
37727
|
+
},
|
37728
|
+
false
|
37729
|
+
);
|
37728
37730
|
return { clockCIDs, node: newProllyRootNode }
|
37729
37731
|
}
|
37730
37732
|
|
@@ -37751,22 +37753,37 @@ async function eventsSince (blocks, head, since) {
|
|
37751
37753
|
* @param {TransactionBlockstore} blocks Bucket block storage.
|
37752
37754
|
* @param {import('./clock').EventLink<import('./clock').EventData>[]} head Merkle clock head.
|
37753
37755
|
*
|
37754
|
-
* @returns {Promise<{cids: CIDCounter, clockCIDs: CIDCounter, result: import('./clock').EventData[]}>}
|
37756
|
+
* @returns {Promise<{root: any, cids: CIDCounter, clockCIDs: CIDCounter, result: import('./clock').EventData[]}>}
|
37755
37757
|
*
|
37756
37758
|
*/
|
37757
|
-
async function getAll (blocks, head) {
|
37759
|
+
async function getAll (blocks, head, rootCache = null, doFull = false) {
|
37758
37760
|
// todo use the root node left around from put, etc
|
37759
37761
|
// move load to a central place
|
37760
37762
|
if (!head.length) {
|
37761
|
-
return { clockCIDs: new CIDCounter(), cids: new CIDCounter(), result: [] }
|
37763
|
+
return { root: null, clockCIDs: new CIDCounter(), cids: new CIDCounter(), result: [] }
|
37762
37764
|
}
|
37763
|
-
const { node: prollyRootNode, clockCIDs } = await
|
37765
|
+
const { node: prollyRootNode, clockCIDs } = await rootOrCache(blocks, head, rootCache, doFull);
|
37764
37766
|
|
37765
37767
|
if (!prollyRootNode) {
|
37766
|
-
return { clockCIDs, cids: new CIDCounter(), result: [] }
|
37768
|
+
return { root: null, clockCIDs, cids: new CIDCounter(), result: [] }
|
37767
37769
|
}
|
37768
37770
|
const { result, cids } = await prollyRootNode.getAllEntries(); // todo params
|
37769
|
-
return { clockCIDs, cids, result: result.map(({ key, value }) => ({ key, value })) }
|
37771
|
+
return { root: prollyRootNode, clockCIDs, cids, result: result.map(({ key, value }) => ({ key, value })) }
|
37772
|
+
}
|
37773
|
+
|
37774
|
+
async function rootOrCache (blocks, head, rootCache, doFull = false) {
|
37775
|
+
let node;
|
37776
|
+
let clockCIDs;
|
37777
|
+
if (!doFull && rootCache && rootCache.root) {
|
37778
|
+
// console.log('get root from cache', rootCache)
|
37779
|
+
node = rootCache.root;
|
37780
|
+
clockCIDs = rootCache.clockCIDs;
|
37781
|
+
} else {
|
37782
|
+
({ node, clockCIDs } = await root(blocks, head, doFull));
|
37783
|
+
// console.timeEnd(callTag + '.root')
|
37784
|
+
// console.log('found root')
|
37785
|
+
}
|
37786
|
+
return { node, clockCIDs }
|
37770
37787
|
}
|
37771
37788
|
|
37772
37789
|
/**
|
@@ -37774,17 +37791,19 @@ async function getAll (blocks, head) {
|
|
37774
37791
|
* @param {import('./clock').EventLink<import('./clock').EventData>[]} head Merkle clock head.
|
37775
37792
|
* @param {string} key The key of the value to retrieve.
|
37776
37793
|
*/
|
37777
|
-
async function get (blocks, head, key) {
|
37794
|
+
async function get (blocks, head, key, rootCache = null) {
|
37778
37795
|
// instead pass root from db? and always update on change
|
37779
37796
|
if (!head.length) {
|
37780
37797
|
return { cids: new CIDCounter(), result: null }
|
37781
37798
|
}
|
37782
|
-
|
37799
|
+
|
37800
|
+
const { node: prollyRootNode, clockCIDs } = await rootOrCache(blocks, head, rootCache);
|
37801
|
+
|
37783
37802
|
if (!prollyRootNode) {
|
37784
37803
|
return { clockCIDs, cids: new CIDCounter(), result: null }
|
37785
37804
|
}
|
37786
37805
|
const { result, cids } = await prollyRootNode.get(key);
|
37787
|
-
return { result, cids, clockCIDs }
|
37806
|
+
return { result, cids, clockCIDs, root: prollyRootNode }
|
37788
37807
|
}
|
37789
37808
|
|
37790
37809
|
async function * vis (blocks, head) {
|
@@ -37801,8 +37820,19 @@ async function * vis (blocks, head) {
|
|
37801
37820
|
}
|
37802
37821
|
|
37803
37822
|
async function visMerkleTree (blocks, head) {
|
37804
|
-
if (!head
|
37805
|
-
|
37823
|
+
// if (!head) return
|
37824
|
+
if (head && !Array.isArray(head)) {
|
37825
|
+
const getBl = makeGetBlock(blocks);
|
37826
|
+
const prollyRootNode = await load$2({
|
37827
|
+
cid: head,
|
37828
|
+
get: getBl.getBlock,
|
37829
|
+
...blockOpts
|
37830
|
+
});
|
37831
|
+
const lines = [];
|
37832
|
+
for await (const line of prollyRootNode.vis()) {
|
37833
|
+
lines.push(line);
|
37834
|
+
}
|
37835
|
+
return { vis: lines.join('\n'), cids: new CIDCounter() }
|
37806
37836
|
}
|
37807
37837
|
const { node: prollyRootNode, cids } = await root(blocks, head);
|
37808
37838
|
const lines = [];
|
@@ -37821,7 +37851,7 @@ async function visMerkleClock (blocks, head) {
|
|
37821
37851
|
return { vis: lines.join('\n') }
|
37822
37852
|
}
|
37823
37853
|
|
37824
|
-
var charwise = {};
|
37854
|
+
var charwise$1 = {};
|
37825
37855
|
|
37826
37856
|
var number = {};
|
37827
37857
|
|
@@ -38049,8 +38079,10 @@ object.factory = function (codec) {
|
|
38049
38079
|
|
38050
38080
|
//for leveldb, request strings
|
38051
38081
|
exports.buffer = false;
|
38052
|
-
exports.type = 'charwise';
|
38053
|
-
} (charwise));
|
38082
|
+
exports.type = 'charwise';
|
38083
|
+
} (charwise$1));
|
38084
|
+
|
38085
|
+
var charwise = /*@__PURE__*/getDefaultExportFromCjs(charwise$1);
|
38054
38086
|
|
38055
38087
|
/* global localStorage */
|
38056
38088
|
let storageSupported = false;
|
@@ -38085,15 +38117,16 @@ const parseCID = cid => (typeof cid === 'string' ? CID$1.parse(cid) : cid);
|
|
38085
38117
|
*/
|
38086
38118
|
class Database {
|
38087
38119
|
listeners = new Set()
|
38120
|
+
indexes = new Map()
|
38121
|
+
rootCache = null
|
38122
|
+
eventsCache = new Map()
|
38088
38123
|
|
38089
|
-
// todo refactor this for the next version
|
38090
38124
|
constructor (blocks, clock, config = {}) {
|
38091
38125
|
this.name = config.name;
|
38092
38126
|
this.instanceId = `fp.${this.name}.${Math.random().toString(36).substring(2, 7)}`;
|
38093
38127
|
this.blocks = blocks;
|
38094
38128
|
this.clock = clock;
|
38095
38129
|
this.config = config;
|
38096
|
-
this.indexes = new Map();
|
38097
38130
|
}
|
38098
38131
|
|
38099
38132
|
/**
|
@@ -38160,11 +38193,22 @@ class Database {
|
|
38160
38193
|
* @instance
|
38161
38194
|
*/
|
38162
38195
|
async changesSince (event) {
|
38196
|
+
// console.log('events for', this.instanceId, event.constructor.name)
|
38163
38197
|
// console.log('changesSince', this.instanceId, event, this.clock)
|
38164
38198
|
let rows, dataCIDs, clockCIDs;
|
38165
38199
|
// if (!event) event = []
|
38166
38200
|
if (event) {
|
38167
|
-
|
38201
|
+
event = event.map((cid) => cid.toString());
|
38202
|
+
const eventKey = JSON.stringify([...event, ...this.clockToJSON()]);
|
38203
|
+
|
38204
|
+
let resp;
|
38205
|
+
if (this.eventsCache.has(eventKey)) {
|
38206
|
+
console.log('events from cache');
|
38207
|
+
resp = this.eventsCache.get(eventKey);
|
38208
|
+
} else {
|
38209
|
+
resp = await eventsSince(this.blocks, this.clock, event);
|
38210
|
+
this.eventsCache.set(eventKey, resp);
|
38211
|
+
}
|
38168
38212
|
const docsMap = new Map();
|
38169
38213
|
for (const { key, type, value } of resp.result.map(decodeEvent)) {
|
38170
38214
|
if (type === 'del') {
|
@@ -38177,7 +38221,9 @@ class Database {
|
|
38177
38221
|
clockCIDs = resp.clockCIDs;
|
38178
38222
|
// console.log('change rows', this.instanceId, rows)
|
38179
38223
|
} else {
|
38180
|
-
const allResp = await getAll(this.blocks, this.clock);
|
38224
|
+
const allResp = await getAll(this.blocks, this.clock, this.rootCache);
|
38225
|
+
this.rootCache = { root: allResp.root, clockCIDs: allResp.clockCIDs };
|
38226
|
+
|
38181
38227
|
rows = allResp.result.map(({ key, value }) => decodeEvent({ key, value }));
|
38182
38228
|
dataCIDs = allResp.cids;
|
38183
38229
|
// console.log('dbdoc rows', this.instanceId, rows)
|
@@ -38190,7 +38236,9 @@ class Database {
|
|
38190
38236
|
}
|
38191
38237
|
|
38192
38238
|
async allDocuments () {
|
38193
|
-
const allResp = await getAll(this.blocks, this.clock);
|
38239
|
+
const allResp = await getAll(this.blocks, this.clock, this.rootCache);
|
38240
|
+
this.rootCache = { root: allResp.root, clockCIDs: allResp.clockCIDs };
|
38241
|
+
|
38194
38242
|
const rows = allResp.result
|
38195
38243
|
.map(({ key, value }) => decodeEvent({ key, value }))
|
38196
38244
|
.map(({ key, value }) => ({ key, value: { _id: key, ...value } }));
|
@@ -38202,7 +38250,8 @@ class Database {
|
|
38202
38250
|
}
|
38203
38251
|
|
38204
38252
|
async allCIDs () {
|
38205
|
-
const allResp = await getAll(this.blocks, this.clock);
|
38253
|
+
const allResp = await getAll(this.blocks, this.clock, this.rootCache, true);
|
38254
|
+
this.rootCache = { root: allResp.root, clockCIDs: allResp.clockCIDs };
|
38206
38255
|
// console.log('allcids', allResp.cids, allResp.clockCIDs)
|
38207
38256
|
const cids = await cidsToProof(allResp.cids);
|
38208
38257
|
const clockCids = await cidsToProof(allResp.clockCIDs);
|
@@ -38248,13 +38297,13 @@ class Database {
|
|
38248
38297
|
*/
|
38249
38298
|
async get (key, opts = {}) {
|
38250
38299
|
const clock = opts.clock || this.clock;
|
38251
|
-
const resp = await get(this.blocks, clock, charwise.encode(key));
|
38252
|
-
|
38300
|
+
const resp = await get(this.blocks, clock, charwise.encode(key), this.rootCache);
|
38301
|
+
this.rootCache = { root: resp.root, clockCIDs: resp.clockCIDs };
|
38253
38302
|
// this tombstone is temporary until we can get the prolly tree to delete
|
38254
38303
|
if (!resp || resp.result === null) {
|
38255
38304
|
throw new Error('Not found')
|
38256
38305
|
}
|
38257
|
-
const doc = resp.result;
|
38306
|
+
const doc = { ...resp.result };
|
38258
38307
|
if (opts.mvcc === true) {
|
38259
38308
|
doc._clock = this.clockToJSON();
|
38260
38309
|
}
|
@@ -38348,7 +38397,9 @@ class Database {
|
|
38348
38397
|
}
|
38349
38398
|
|
38350
38399
|
applyClock (prevClock, newClock) {
|
38351
|
-
// console.log('
|
38400
|
+
// console.log('prevClock', prevClock.length, prevClock.map((cid) => cid.toString()))
|
38401
|
+
// console.log('newClock', newClock.length, newClock.map((cid) => cid.toString()))
|
38402
|
+
// console.log('this.clock', this.clock.length, this.clockToJSON())
|
38352
38403
|
const stPrev = prevClock.map(cid => cid.toString());
|
38353
38404
|
const keptPrevClock = this.clock.filter(cid => stPrev.indexOf(cid.toString()) === -1);
|
38354
38405
|
const merged = keptPrevClock.concat(newClock);
|
@@ -38356,8 +38407,10 @@ class Database {
|
|
38356
38407
|
for (const cid of merged) {
|
38357
38408
|
uniquebyCid.set(cid.toString(), cid);
|
38358
38409
|
}
|
38359
|
-
this.clock = Array.from(uniquebyCid.values());
|
38360
|
-
|
38410
|
+
this.clock = Array.from(uniquebyCid.values()).sort((a, b) => a.toString().localeCompare(b.toString()));
|
38411
|
+
this.rootCache = null;
|
38412
|
+
this.eventsCache.clear();
|
38413
|
+
// console.log('afterClock', this.clock.length, this.clockToJSON())
|
38361
38414
|
}
|
38362
38415
|
|
38363
38416
|
// /**
|
@@ -38693,8 +38746,8 @@ const refCompare = (aRef, bRef) => {
|
|
38693
38746
|
return simpleCompare(aRef, bRef)
|
38694
38747
|
};
|
38695
38748
|
|
38696
|
-
const dbIndexOpts = { cache: nocache, chunker: bf(
|
38697
|
-
const idIndexOpts = { cache: nocache, chunker: bf(
|
38749
|
+
const dbIndexOpts = { cache: nocache, chunker: bf(30), codec: codec$1, hasher: sha256$2, compare };
|
38750
|
+
const idIndexOpts = { cache: nocache, chunker: bf(30), codec: codec$1, hasher: sha256$2, compare: simpleCompare };
|
38698
38751
|
|
38699
38752
|
const makeDoc = ({ key, value }) => ({ _id: key, ...value });
|
38700
38753
|
|
@@ -38751,6 +38804,9 @@ const indexEntriesForChanges = (changes, mapFn) => {
|
|
38751
38804
|
*
|
38752
38805
|
*/
|
38753
38806
|
class DbIndex {
|
38807
|
+
/**
|
38808
|
+
* @param {Database} database
|
38809
|
+
*/
|
38754
38810
|
constructor (database, name, mapFn, clock = null, opts = {}) {
|
38755
38811
|
this.database = database;
|
38756
38812
|
if (!database.indexBlocks) {
|
@@ -38822,6 +38878,14 @@ class DbIndex {
|
|
38822
38878
|
return new DbIndex(database, name, code, clock)
|
38823
38879
|
}
|
38824
38880
|
|
38881
|
+
async visKeyTree () {
|
38882
|
+
return await visMerkleTree(this.database.indexBlocks, this.indexById.cid)
|
38883
|
+
}
|
38884
|
+
|
38885
|
+
async visIdTree () {
|
38886
|
+
return await visMerkleTree(this.database.indexBlocks, this.indexByKey.cid)
|
38887
|
+
}
|
38888
|
+
|
38825
38889
|
/**
|
38826
38890
|
* JSDoc for Query type.
|
38827
38891
|
* @typedef {Object} DbQuery
|
@@ -38991,11 +39055,7 @@ async function doIndexQuery (blocks, indexByKey, query = {}) {
|
|
38991
39055
|
}
|
38992
39056
|
}
|
38993
39057
|
|
38994
|
-
var
|
38995
|
-
var browser = {
|
38996
|
-
get exports(){ return browserExports; },
|
38997
|
-
set exports(v){ browserExports = v; },
|
38998
|
-
};
|
39058
|
+
var browser = {exports: {}};
|
38999
39059
|
|
39000
39060
|
/**
|
39001
39061
|
* Helpers.
|
@@ -39711,8 +39771,10 @@ var common = setup;
|
|
39711
39771
|
} catch (error) {
|
39712
39772
|
return '[UnexpectedJSONParseError]: ' + error.message;
|
39713
39773
|
}
|
39714
|
-
};
|
39715
|
-
} (browser,
|
39774
|
+
};
|
39775
|
+
} (browser, browser.exports));
|
39776
|
+
|
39777
|
+
var browserExports = browser.exports;
|
39716
39778
|
|
39717
39779
|
// originally pulled out of simple-peer
|
39718
39780
|
|
@@ -40863,6 +40925,8 @@ Peer.channelConfig = {};
|
|
40863
40925
|
|
40864
40926
|
var simplePeer = Peer;
|
40865
40927
|
|
40928
|
+
var SimplePeer = /*@__PURE__*/getDefaultExportFromCjs(simplePeer);
|
40929
|
+
|
40866
40930
|
/**
|
40867
40931
|
* @typedef {import('./database.js').Database} Database
|
40868
40932
|
*/
|
@@ -40873,9 +40937,9 @@ class Sync {
|
|
40873
40937
|
* @memberof Sync
|
40874
40938
|
* @static
|
40875
40939
|
*/
|
40876
|
-
constructor (database, PeerClass =
|
40940
|
+
constructor (database, PeerClass = SimplePeer) {
|
40877
40941
|
this.database = database;
|
40878
|
-
this.database.blocks.syncs.add(this);
|
40942
|
+
this.database.blocks.syncs.add(this); // should this happen during setup?
|
40879
40943
|
this.PeerClass = PeerClass;
|
40880
40944
|
this.pushBacklog = new Promise((resolve, reject) => {
|
40881
40945
|
this.pushBacklogResolve = resolve;
|
@@ -40923,8 +40987,13 @@ class Sync {
|
|
40923
40987
|
|
40924
40988
|
async gotData (data) {
|
40925
40989
|
// console.log('got data', data.toString())
|
40990
|
+
let reader = null;
|
40926
40991
|
try {
|
40927
|
-
|
40992
|
+
reader = await CarReader.fromBytes(data);
|
40993
|
+
} catch (e) {
|
40994
|
+
// console.log('not a car', data.toString())
|
40995
|
+
}
|
40996
|
+
if (reader) {
|
40928
40997
|
const blz = new Set();
|
40929
40998
|
for await (const block of reader.blocks()) {
|
40930
40999
|
blz.add(block);
|
@@ -40947,17 +41016,22 @@ class Sync {
|
|
40947
41016
|
get: async cid => await reader.get(cid),
|
40948
41017
|
lastCid: [...blz][0].cid // doesn't matter
|
40949
41018
|
}, false);
|
40950
|
-
|
41019
|
+
// first arg could be the roots parents?
|
41020
|
+
// get the roots parents
|
41021
|
+
const parents = await Promise.all(roots.map(async (cid) => {
|
41022
|
+
const rbl = await reader.get(cid);
|
41023
|
+
if (!rbl) {
|
41024
|
+
console.log('missing root block', cid.toString(), reader);
|
41025
|
+
throw new Error('missing root block')
|
41026
|
+
}
|
41027
|
+
const block = await decodeEventBlock(rbl.bytes);
|
41028
|
+
return block.value.parents
|
41029
|
+
}));
|
41030
|
+
this.database.applyClock(parents.flat(), roots);
|
40951
41031
|
this.database.notifyReset();
|
40952
41032
|
// console.log('after', this.database.clockToJSON())
|
40953
41033
|
this.pushBacklogResolve({ ok: true });
|
40954
|
-
}
|
40955
|
-
// console.error(e)
|
40956
|
-
// if e.message matche 'CBOR' we can ignore it
|
40957
|
-
if (!e.message.match(/CBOR|fromBytes/)) {
|
40958
|
-
throw e
|
40959
|
-
}
|
40960
|
-
|
41034
|
+
} else {
|
40961
41035
|
// data is a json string, parse it
|
40962
41036
|
const message = JSON.parse(data.toString());
|
40963
41037
|
// console.log('got message', message)
|
@@ -40966,7 +41040,7 @@ class Sync {
|
|
40966
41040
|
} else if (message.clock) {
|
40967
41041
|
const reqCidDiff = message;
|
40968
41042
|
// this might be a CID diff
|
40969
|
-
|
41043
|
+
console.log('got diff', reqCidDiff);
|
40970
41044
|
const carBlock = await Sync.makeCar(this.database, null, reqCidDiff.cids);
|
40971
41045
|
if (!carBlock) {
|
40972
41046
|
// we are full synced
|
@@ -40983,7 +41057,7 @@ class Sync {
|
|
40983
41057
|
}
|
40984
41058
|
|
40985
41059
|
async sendUpdate (blockstore) {
|
40986
|
-
console.log('send update from', this.database.instanceId)
|
41060
|
+
// console.log('send update from', this.database.instanceId)
|
40987
41061
|
// todo should send updates since last sync
|
40988
41062
|
const newCar = await blocksToCarBlock(blockstore.lastCid, blockstore);
|
40989
41063
|
this.peer.send(newCar.bytes);
|
@@ -41069,9 +41143,9 @@ class Fireproof {
|
|
41069
41143
|
if (existing) {
|
41070
41144
|
const existingConfig = JSON.parse(existing);
|
41071
41145
|
const fp = new Database(new TransactionBlockstore(name, existingConfig.key), [], opts);
|
41072
|
-
return
|
41146
|
+
return Fireproof.fromJSON(existingConfig, fp)
|
41073
41147
|
} else {
|
41074
|
-
const instanceKey =
|
41148
|
+
const instanceKey = randomBytes$3(32).toString('hex'); // pass null to disable encryption
|
41075
41149
|
return new Database(new TransactionBlockstore(name, instanceKey), [], opts)
|
41076
41150
|
}
|
41077
41151
|
} else {
|
@@ -41112,7 +41186,7 @@ class Fireproof {
|
|
41112
41186
|
index.clock.db = null;
|
41113
41187
|
});
|
41114
41188
|
}
|
41115
|
-
const snappedDb =
|
41189
|
+
const snappedDb = Fireproof.fromJSON(definition, withBlocks)
|
41116
41190
|
;[...database.indexes.values()].forEach(index => {
|
41117
41191
|
snappedDb.indexes.get(index.mapFnString).mapFn = index.mapFn;
|
41118
41192
|
});
|