@fireproof/core 0.4.0 → 0.5.0

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.
Files changed (53) hide show
  1. package/README.md +20 -22
  2. package/dist/src/fireproof.d.ts +45 -15
  3. package/dist/src/fireproof.js +97 -27
  4. package/dist/src/fireproof.js.map +1 -1
  5. package/dist/src/fireproof.mjs +97 -28
  6. package/dist/src/fireproof.mjs.map +1 -1
  7. package/package.json +2 -2
  8. package/src/database.js +31 -13
  9. package/src/db-index.js +19 -9
  10. package/src/fireproof.js +45 -5
  11. package/src/prolly.js +2 -1
  12. package/src/valet.js +3 -2
  13. package/dist/blockstore.js +0 -242
  14. package/dist/clock.js +0 -355
  15. package/dist/crypto.js +0 -59
  16. package/dist/database.js +0 -308
  17. package/dist/db-index.js +0 -314
  18. package/dist/fireproof.js +0 -83
  19. package/dist/hooks/use-fireproof.js +0 -100
  20. package/dist/listener.js +0 -110
  21. package/dist/main.js +0 -2
  22. package/dist/main.js.LICENSE.txt +0 -17
  23. package/dist/prolly.js +0 -316
  24. package/dist/sha1.js +0 -74
  25. package/dist/src/blockstore.js +0 -242
  26. package/dist/src/clock.js +0 -355
  27. package/dist/src/crypto.js +0 -59
  28. package/dist/src/database.js +0 -312
  29. package/dist/src/db-index.js +0 -314
  30. package/dist/src/index.d.ts +0 -321
  31. package/dist/src/index.js +0 -38936
  32. package/dist/src/index.js.map +0 -1
  33. package/dist/src/index.mjs +0 -38931
  34. package/dist/src/index.mjs.map +0 -1
  35. package/dist/src/listener.js +0 -108
  36. package/dist/src/prolly.js +0 -319
  37. package/dist/src/sha1.js +0 -74
  38. package/dist/src/utils.js +0 -16
  39. package/dist/src/valet.js +0 -262
  40. package/dist/test/block.js +0 -57
  41. package/dist/test/clock.test.js +0 -556
  42. package/dist/test/db-index.test.js +0 -231
  43. package/dist/test/fireproof.test.js +0 -444
  44. package/dist/test/fulltext.test.js +0 -61
  45. package/dist/test/helpers.js +0 -39
  46. package/dist/test/hydrator.test.js +0 -142
  47. package/dist/test/listener.test.js +0 -103
  48. package/dist/test/prolly.test.js +0 -162
  49. package/dist/test/proofs.test.js +0 -45
  50. package/dist/test/reproduce-fixture-bug.test.js +0 -57
  51. package/dist/test/valet.test.js +0 -56
  52. package/dist/utils.js +0 -16
  53. package/dist/valet.js +0 -262
package/dist/src/clock.js DELETED
@@ -1,355 +0,0 @@
1
- import { Block, encode, decode } from 'multiformats/block';
2
- import { sha256 } from 'multiformats/hashes/sha2';
3
- import * as cbor from '@ipld/dag-cbor';
4
- // @ts-ignore
5
- import { CIDCounter } from 'prolly-trees/utils';
6
- /**
7
- * @template T
8
- * @typedef {{ parents: EventLink<T>[], data: T }} EventView
9
- */
10
- /**
11
- * @template T
12
- * @typedef {import('multiformats').BlockView<EventView<T>>} EventBlockView
13
- */
14
- /**
15
- * @template T
16
- * @typedef {import('multiformats').Link<EventView<T>>} EventLink
17
- */
18
- /**
19
- * @typedef {{
20
- * type: 'put'|'del'
21
- * key: string
22
- * value: import('./link').AnyLink
23
- * root: import('./link').AnyLink
24
- * }} EventData
25
- * @typedef {{
26
- * root: import('./link').AnyLink
27
- * head: import('./clock').EventLink<EventData>[]
28
- * event: import('./clock').EventBlockView<EventData>
29
- * }} Result
30
- */
31
- /**
32
- * Advance the clock by adding an event.
33
- *
34
- * @template T
35
- * @param {import('./blockstore').TransactionBlockstore} blocks Block storage.
36
- * @param {EventLink<T>[]} head The head of the clock.
37
- * @param {EventLink<T>} event The event to add.
38
- * @returns {Promise<{head:EventLink<T>[], cids:any[]}>} The new head of the clock.
39
- */
40
- export async function advance(blocks, head, event) {
41
- /** @type {EventFetcher<T>} */
42
- const events = new EventFetcher(blocks);
43
- const headmap = new Map(head.map((cid) => [cid.toString(), cid]));
44
- // Check if the headmap already includes the event, return head if it does
45
- if (headmap.has(event.toString()))
46
- return { head, cids: await events.all() };
47
- // Does event contain the clock?
48
- let changed = false;
49
- for (const cid of head) {
50
- if (await contains(events, event, cid)) {
51
- headmap.delete(cid.toString());
52
- headmap.set(event.toString(), event);
53
- changed = true;
54
- }
55
- }
56
- // If the headmap has been changed, return the new headmap values
57
- if (changed) {
58
- return { head: [...headmap.values()], cids: await events.all() };
59
- }
60
- // Does clock contain the event?
61
- for (const p of head) {
62
- if (await contains(events, p, event)) {
63
- return { head, cids: await events.all() };
64
- }
65
- }
66
- // Return the head concatenated with the new event if it passes both checks
67
- return { head: head.concat(event), cids: await events.all() };
68
- }
69
- /**
70
- * @template T
71
- * @implements {EventBlockView<T>}
72
- */
73
- export class EventBlock extends Block {
74
- /**
75
- * @param {object} config
76
- * @param {EventLink<T>} config.cid
77
- * @param {Event} config.value
78
- * @param {Uint8Array} config.bytes
79
- */
80
- constructor({ cid, value, bytes }) {
81
- // @ts-expect-error
82
- super({ cid, value, bytes });
83
- }
84
- /**
85
- * @template T
86
- * @param {T} data
87
- * @param {EventLink<T>[]} [parents]
88
- */
89
- static create(data, parents) {
90
- return encodeEventBlock({ data, parents: parents ?? [] });
91
- }
92
- }
93
- /** @template T */
94
- export class EventFetcher {
95
- /** @param {import('./blockstore').TransactionBlockstore} blocks */
96
- constructor(blocks) {
97
- /** @private */
98
- this._blocks = blocks;
99
- this._cids = new CIDCounter();
100
- this._cache = new Map();
101
- }
102
- /**
103
- * @param {EventLink<T>} link
104
- * @returns {Promise<EventBlockView<T>>}
105
- */
106
- async get(link) {
107
- const slink = link.toString();
108
- // console.log('get', link.toString())
109
- if (this._cache.has(slink))
110
- return this._cache.get(slink);
111
- const block = await this._blocks.get(link);
112
- this._cids.add({ address: link });
113
- if (!block)
114
- throw new Error(`missing block: ${link}`);
115
- const got = decodeEventBlock(block.bytes);
116
- this._cache.set(slink, got);
117
- return got;
118
- }
119
- async all() {
120
- // await Promise.all([...this._cids])
121
- return this._cids.all();
122
- }
123
- }
124
- /**
125
- * @template T
126
- * @param {EventView<T>} value
127
- * @returns {Promise<EventBlockView<T>>}
128
- */
129
- export async function encodeEventBlock(value) {
130
- // TODO: sort parents
131
- const { cid, bytes } = await encode({ value, codec: cbor, hasher: sha256 });
132
- // @ts-expect-error
133
- return new Block({ cid, value, bytes });
134
- }
135
- /**
136
- * @template T
137
- * @param {Uint8Array} bytes
138
- * @returns {Promise<EventBlockView<T>>}
139
- */
140
- export async function decodeEventBlock(bytes) {
141
- const { cid, value } = await decode({ bytes, codec: cbor, hasher: sha256 });
142
- // @ts-expect-error
143
- return new Block({ cid, value, bytes });
144
- }
145
- /**
146
- * Returns true if event "a" contains event "b". Breadth first search.
147
- * @template T
148
- * @param {EventFetcher} events
149
- * @param {EventLink<T>} a
150
- * @param {EventLink<T>} b
151
- */
152
- async function contains(events, a, b) {
153
- if (a.toString() === b.toString())
154
- return true;
155
- const [{ value: aevent }, { value: bevent }] = await Promise.all([events.get(a), events.get(b)]);
156
- const links = [...aevent.parents];
157
- while (links.length) {
158
- const link = links.shift();
159
- if (!link)
160
- break;
161
- if (link.toString() === b.toString())
162
- return true;
163
- // if any of b's parents are this link, then b cannot exist in any of the
164
- // tree below, since that would create a cycle.
165
- if (bevent.parents.some((p) => link.toString() === p.toString()))
166
- continue;
167
- const { value: event } = await events.get(link);
168
- links.push(...event.parents);
169
- }
170
- return false;
171
- }
172
- /**
173
- * @template T
174
- * @param {import('./blockstore').TransactionBlockstore} blocks Block storage.
175
- * @param {EventLink<T>[]} head
176
- * @param {object} [options]
177
- * @param {(b: EventBlockView<T>) => string} [options.renderNodeLabel]
178
- */
179
- export async function* vis(blocks, head, options = {}) {
180
- // @ts-ignore
181
- const renderNodeLabel = options.renderNodeLabel ?? ((b) => b.value.data.value);
182
- const events = new EventFetcher(blocks);
183
- yield 'digraph clock {';
184
- yield ' node [shape=point fontname="Courier"]; head;';
185
- const hevents = await Promise.all(head.map((link) => events.get(link)));
186
- const links = [];
187
- const nodes = new Set();
188
- for (const e of hevents) {
189
- nodes.add(e.cid.toString());
190
- yield ` node [shape=oval fontname="Courier"]; ${e.cid} [label="${renderNodeLabel(e)}"];`;
191
- yield ` head -> ${e.cid};`;
192
- for (const p of e.value.parents) {
193
- yield ` ${e.cid} -> ${p};`;
194
- }
195
- links.push(...e.value.parents);
196
- }
197
- while (links.length) {
198
- const link = links.shift();
199
- if (!link)
200
- break;
201
- if (nodes.has(link.toString()))
202
- continue;
203
- nodes.add(link.toString());
204
- const block = await events.get(link);
205
- yield ` node [shape=oval]; ${link} [label="${renderNodeLabel(block)}" fontname="Courier"];`;
206
- for (const p of block.value.parents) {
207
- yield ` ${link} -> ${p};`;
208
- }
209
- links.push(...block.value.parents);
210
- }
211
- yield '}';
212
- }
213
- export async function findEventsToSync(blocks, head) {
214
- // const callTag = Math.random().toString(36).substring(7)
215
- const events = new EventFetcher(blocks);
216
- // console.time(callTag + '.findCommonAncestorWithSortedEvents')
217
- const { ancestor, sorted } = await findCommonAncestorWithSortedEvents(events, head);
218
- // console.timeEnd(callTag + '.findCommonAncestorWithSortedEvents')
219
- // console.log('sorted', sorted.length)
220
- // console.time(callTag + '.contains')
221
- const toSync = await asyncFilter(sorted, async (uks) => !(await contains(events, ancestor, uks.cid)));
222
- // console.timeEnd(callTag + '.contains')
223
- return { cids: events.all(), events: toSync };
224
- }
225
- const asyncFilter = async (arr, predicate) => Promise.all(arr.map(predicate)).then((results) => arr.filter((_v, index) => results[index]));
226
- export async function findCommonAncestorWithSortedEvents(events, children) {
227
- // const callTag = Math.random().toString(36).substring(7)
228
- // console.time(callTag + '.findCommonAncestor')
229
- const ancestor = await findCommonAncestor(events, children);
230
- // console.timeEnd(callTag + '.findCommonAncestor')
231
- if (!ancestor) {
232
- throw new Error('failed to find common ancestor event');
233
- }
234
- // console.time(callTag + '.findSortedEvents')
235
- const sorted = await findSortedEvents(events, children, ancestor);
236
- // console.timeEnd(callTag + '.findSortedEvents')
237
- return { ancestor, sorted };
238
- }
239
- /**
240
- * Find the common ancestor event of the passed children. A common ancestor is
241
- * the first single event in the DAG that _all_ paths from children lead to.
242
- *
243
- * @param {import('./clock').EventFetcher} events
244
- * @param {import('./clock').EventLink<EventData>[]} children
245
- */
246
- async function findCommonAncestor(events, children) {
247
- if (!children.length)
248
- return;
249
- const candidates = children.map((c) => [c]);
250
- while (true) {
251
- let changed = false;
252
- for (const c of candidates) {
253
- const candidate = await findAncestorCandidate(events, c[c.length - 1]);
254
- if (!candidate)
255
- continue;
256
- changed = true;
257
- c.push(candidate);
258
- const ancestor = findCommonString(candidates);
259
- if (ancestor)
260
- return ancestor;
261
- }
262
- if (!changed)
263
- return;
264
- }
265
- }
266
- /**
267
- * @param {import('./clock').EventFetcher} events
268
- * @param {import('./clock').EventLink<EventData>} root
269
- */
270
- async function findAncestorCandidate(events, root) {
271
- const { value: event } = await events.get(root);
272
- if (!event.parents.length)
273
- return root;
274
- return event.parents.length === 1 ? event.parents[0] : findCommonAncestor(events, event.parents);
275
- }
276
- /**
277
- * @template {{ toString: () => string }} T
278
- * @param {Array<T[]>} arrays
279
- */
280
- function findCommonString(arrays) {
281
- arrays = arrays.map((a) => [...a]);
282
- for (const arr of arrays) {
283
- for (const item of arr) {
284
- let matched = true;
285
- for (const other of arrays) {
286
- if (arr === other)
287
- continue;
288
- matched = other.some((i) => String(i) === String(item));
289
- if (!matched)
290
- break;
291
- }
292
- if (matched)
293
- return item;
294
- }
295
- }
296
- }
297
- /**
298
- * Find and sort events between the head(s) and the tail.
299
- * @param {import('./clock').EventFetcher} events
300
- * @param {import('./clock').EventLink<EventData>[]} head
301
- * @param {import('./clock').EventLink<EventData>} tail
302
- */
303
- async function findSortedEvents(events, head, tail) {
304
- // const callTag = Math.random().toString(36).substring(7)
305
- // get weighted events - heavier events happened first
306
- /** @type {Map<string, { event: import('./clock').EventBlockView<EventData>, weight: number }>} */
307
- const weights = new Map();
308
- const all = await Promise.all(head.map((h) => findEvents(events, h, tail)));
309
- for (const arr of all) {
310
- for (const { event, depth } of arr) {
311
- // console.log('event value', event.value.data.value)
312
- const info = weights.get(event.cid.toString());
313
- if (info) {
314
- info.weight += depth;
315
- }
316
- else {
317
- weights.set(event.cid.toString(), { event, weight: depth });
318
- }
319
- }
320
- }
321
- // group events into buckets by weight
322
- /** @type {Map<number, import('./clock').EventBlockView<EventData>[]>} */
323
- const buckets = new Map();
324
- for (const { event, weight } of weights.values()) {
325
- const bucket = buckets.get(weight);
326
- if (bucket) {
327
- bucket.push(event);
328
- }
329
- else {
330
- buckets.set(weight, [event]);
331
- }
332
- }
333
- // sort by weight, and by CID within weight
334
- const sorted = Array.from(buckets)
335
- .sort((a, b) => b[0] - a[0])
336
- .flatMap(([, es]) => es.sort((a, b) => (String(a.cid) < String(b.cid) ? -1 : 1)));
337
- // console.log('sorted', sorted.map(s => s.value.data.value))
338
- return sorted;
339
- }
340
- /**
341
- * @param {EventFetcher} events
342
- * @param {EventLink<EventData>} start
343
- * @param {EventLink<EventData>} end
344
- * @returns {Promise<Array<{ event: EventBlockView<EventData>, depth: number }>>}
345
- */
346
- async function findEvents(events, start, end, depth = 0) {
347
- // console.log('findEvents', start)
348
- const event = await events.get(start);
349
- const acc = [{ event, depth }];
350
- const { parents } = event.value;
351
- if (parents.length === 1 && String(parents[0]) === String(end))
352
- return acc;
353
- const rest = await Promise.all(parents.map((p) => findEvents(events, p, end, depth + 1)));
354
- return acc.concat(...rest);
355
- }
@@ -1,59 +0,0 @@
1
- // @ts-nocheck
2
- import * as codec from 'encrypted-block';
3
- import { create, load } from 'prolly-trees/cid-set';
4
- import { CID } from 'multiformats';
5
- import { encode, decode, create as mfCreate } from 'multiformats/block';
6
- import * as dagcbor from '@ipld/dag-cbor';
7
- import { sha256 as hasher } from 'multiformats/hashes/sha2';
8
- const createBlock = (bytes, cid) => mfCreate({ cid, bytes, hasher, codec });
9
- const encrypt = async function* ({ get, cids, hasher, key, cache, chunker, root }) {
10
- const set = new Set();
11
- let eroot;
12
- for (const string of cids) {
13
- const cid = CID.parse(string);
14
- const unencrypted = await get(cid);
15
- const block = await encode({ ...await codec.encrypt({ ...unencrypted, key }), codec, hasher });
16
- // console.log(`encrypting ${string} as ${block.cid}`)
17
- yield block;
18
- set.add(block.cid.toString());
19
- if (unencrypted.cid.equals(root))
20
- eroot = block.cid;
21
- }
22
- if (!eroot)
23
- throw new Error('cids does not include root');
24
- const list = [...set].map(s => CID.parse(s));
25
- let last;
26
- for await (const node of create({ list, get, cache, chunker, hasher, codec: dagcbor })) {
27
- const block = await node.block;
28
- yield block;
29
- last = block;
30
- }
31
- const head = [eroot, last.cid];
32
- const block = await encode({ value: head, codec: dagcbor, hasher });
33
- yield block;
34
- };
35
- const decrypt = async function* ({ root, get, key, cache, chunker, hasher }) {
36
- const o = { ...await get(root), codec: dagcbor, hasher };
37
- const decodedRoot = await decode(o);
38
- // console.log('decodedRoot', decodedRoot)
39
- const { value: [eroot, tree] } = decodedRoot;
40
- const rootBlock = await get(eroot); // should I decrypt?
41
- const cidset = await load({ cid: tree, get, cache, chunker, codec, hasher });
42
- const { result: nodes } = await cidset.getAllEntries();
43
- const unwrap = async (eblock) => {
44
- const { bytes, cid } = await codec.decrypt({ ...eblock, key }).catch(e => {
45
- console.log('ekey', e);
46
- throw new Error('bad key: ' + key.toString('hex'));
47
- });
48
- const block = await createBlock(bytes, cid);
49
- return block;
50
- };
51
- const promises = [];
52
- for (const { cid } of nodes) {
53
- if (!rootBlock.cid.equals(cid))
54
- promises.push(get(cid).then(unwrap));
55
- }
56
- yield* promises;
57
- yield unwrap(rootBlock);
58
- };
59
- export { encrypt, decrypt };