@fireproof/core 0.3.22 → 0.4.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 (55) hide show
  1. package/dist/blockstore.js +242 -0
  2. package/dist/clock.js +355 -0
  3. package/dist/crypto.js +59 -0
  4. package/dist/database.js +308 -0
  5. package/dist/db-index.js +314 -0
  6. package/dist/fireproof.js +83 -0
  7. package/dist/hooks/use-fireproof.js +100 -0
  8. package/dist/listener.js +110 -0
  9. package/dist/prolly.js +316 -0
  10. package/dist/sha1.js +74 -0
  11. package/dist/src/blockstore.js +242 -0
  12. package/dist/src/clock.js +355 -0
  13. package/dist/src/crypto.js +59 -0
  14. package/dist/src/database.js +312 -0
  15. package/dist/src/db-index.js +314 -0
  16. package/dist/src/fireproof.d.ts +319 -0
  17. package/dist/src/fireproof.js +38976 -0
  18. package/dist/src/fireproof.js.map +1 -0
  19. package/dist/src/fireproof.mjs +38972 -0
  20. package/dist/src/fireproof.mjs.map +1 -0
  21. package/dist/src/index.d.ts +1 -1
  22. package/dist/src/index.js +19 -14
  23. package/dist/src/index.js.map +1 -1
  24. package/dist/src/index.mjs +19 -14
  25. package/dist/src/index.mjs.map +1 -1
  26. package/dist/src/listener.js +108 -0
  27. package/dist/src/prolly.js +319 -0
  28. package/dist/src/sha1.js +74 -0
  29. package/dist/src/utils.js +16 -0
  30. package/dist/src/valet.js +262 -0
  31. package/dist/test/block.js +57 -0
  32. package/dist/test/clock.test.js +556 -0
  33. package/dist/test/db-index.test.js +231 -0
  34. package/dist/test/fireproof.test.js +444 -0
  35. package/dist/test/fulltext.test.js +61 -0
  36. package/dist/test/helpers.js +39 -0
  37. package/dist/test/hydrator.test.js +142 -0
  38. package/dist/test/listener.test.js +103 -0
  39. package/dist/test/prolly.test.js +162 -0
  40. package/dist/test/proofs.test.js +45 -0
  41. package/dist/test/reproduce-fixture-bug.test.js +57 -0
  42. package/dist/test/valet.test.js +56 -0
  43. package/dist/utils.js +16 -0
  44. package/dist/valet.js +262 -0
  45. package/hooks/use-fireproof.js +38 -63
  46. package/package.json +13 -14
  47. package/src/blockstore.js +8 -4
  48. package/src/database.js +338 -0
  49. package/src/db-index.js +3 -3
  50. package/src/fireproof.js +65 -322
  51. package/src/listener.js +10 -8
  52. package/src/prolly.js +10 -6
  53. package/src/utils.js +16 -0
  54. package/src/hydrator.js +0 -54
  55. package/src/index.js +0 -6
@@ -0,0 +1,242 @@
1
+ import { parse } from 'multiformats/link';
2
+ import { CID } from 'multiformats';
3
+ import { Valet } from './valet.js';
4
+ // const sleep = ms => new Promise(r => setTimeout(r, ms))
5
+ const husherMap = new Map();
6
+ const husher = (id, workFn) => {
7
+ if (!husherMap.has(id)) {
8
+ husherMap.set(id, workFn().finally(() => setTimeout(() => husherMap.delete(id), 100)));
9
+ }
10
+ return husherMap.get(id);
11
+ };
12
+ /**
13
+ * @typedef {{ get: (link: import('../src/link').AnyLink) => Promise<AnyBlock | undefined> }} BlockFetcher
14
+ */
15
+ /**
16
+ * @typedef {Object} AnyBlock
17
+ * @property {import('./link').AnyLink} cid - The CID of the block
18
+ * @property {Uint8Array} bytes - The block's data
19
+ *
20
+ * @typedef {Object} Blockstore
21
+ * @property {function(import('./link').AnyLink): Promise<AnyBlock|undefined>} get - A function to retrieve a block by CID
22
+ * @property {function(import('./link').AnyLink, Uint8Array): Promise<void>} put - A function to store a block's data and CID
23
+ *
24
+ * A blockstore that caches writes to a transaction and only persists them when committed.
25
+ */
26
+ export class TransactionBlockstore {
27
+ /** @type {Map<string, Uint8Array>} */
28
+ committedBlocks = new Map();
29
+ valet = null;
30
+ instanceId = 'blkz.' + Math.random().toString(36).substring(2, 4);
31
+ inflightTransactions = new Set();
32
+ constructor(name, encryptionKey) {
33
+ if (name) {
34
+ this.valet = new Valet(name, encryptionKey);
35
+ }
36
+ this.remoteBlockFunction = null;
37
+ }
38
+ /**
39
+ * Get a block from the store.
40
+ *
41
+ * @param {import('./link').AnyLink} cid
42
+ * @returns {Promise<AnyBlock | undefined>}
43
+ */
44
+ async get(cid) {
45
+ const key = cid.toString();
46
+ // it is safe to read from the in-flight transactions becauase they are immutable
47
+ const bytes = await Promise.any([this.transactionsGet(key), this.committedGet(key)]).catch(e => {
48
+ // console.log('networkGet', cid.toString(), e)
49
+ return this.networkGet(key);
50
+ });
51
+ if (!bytes)
52
+ throw new Error('Missing block: ' + key);
53
+ return { cid, bytes };
54
+ }
55
+ // this iterates over the in-flight transactions
56
+ // and returns the first matching block it finds
57
+ async transactionsGet(key) {
58
+ for (const transaction of this.inflightTransactions) {
59
+ const got = await transaction.get(key);
60
+ if (got && got.bytes)
61
+ return got.bytes;
62
+ }
63
+ throw new Error('Missing block: ' + key);
64
+ }
65
+ async committedGet(key) {
66
+ const old = this.committedBlocks.get(key);
67
+ if (old)
68
+ return old;
69
+ if (!this.valet)
70
+ throw new Error('Missing block: ' + key);
71
+ const got = await this.valet.getBlock(key);
72
+ // console.log('committedGet: ' + key)
73
+ this.committedBlocks.set(key, got);
74
+ return got;
75
+ }
76
+ async clearCommittedCache() {
77
+ this.committedBlocks.clear();
78
+ }
79
+ async networkGet(key) {
80
+ if (this.remoteBlockFunction) {
81
+ // todo why is this on valet?
82
+ const value = await husher(key, async () => await this.remoteBlockFunction(key));
83
+ if (value) {
84
+ // console.log('networkGot: ' + key, value.length)
85
+ doTransaction('networkGot: ' + key, this, async (innerBlockstore) => {
86
+ await innerBlockstore.put(CID.parse(key), value);
87
+ });
88
+ return value;
89
+ }
90
+ }
91
+ else {
92
+ return false;
93
+ }
94
+ }
95
+ /**
96
+ * Add a block to the store. Usually bound to a transaction by a closure.
97
+ * It sets the lastCid property to the CID of the block that was put.
98
+ * This is used by the transaction as the head of the car when written to the valet.
99
+ * We don't have to worry about which transaction we are when we are here because
100
+ * we are the transactionBlockstore.
101
+ *
102
+ * @param {import('./link').AnyLink} cid
103
+ * @param {Uint8Array} bytes
104
+ */
105
+ put(cid, bytes) {
106
+ throw new Error('use a transaction to put');
107
+ }
108
+ /**
109
+ * Iterate over all blocks in the store.
110
+ *
111
+ * @yields {AnyBlock}
112
+ * @returns {AsyncGenerator<AnyBlock>}
113
+ */
114
+ // * entries () {
115
+ // // needs transaction blocks?
116
+ // // for (const [str, bytes] of this.blocks) {
117
+ // // yield { cid: parse(str), bytes }
118
+ // // }
119
+ // for (const [str, bytes] of this.committedBlocks) {
120
+ // yield { cid: parse(str), bytes }
121
+ // }
122
+ // }
123
+ /**
124
+ * Begin a transaction. Ensures the uncommited blocks are empty at the begining.
125
+ * Returns the blocks to read and write during the transaction.
126
+ * @returns {InnerBlockstore}
127
+ * @memberof TransactionBlockstore
128
+ */
129
+ begin(label = '') {
130
+ const innerTransactionBlockstore = new InnerBlockstore(label, this);
131
+ this.inflightTransactions.add(innerTransactionBlockstore);
132
+ return innerTransactionBlockstore;
133
+ }
134
+ /**
135
+ * Commit the transaction. Writes the blocks to the store.
136
+ * @returns {Promise<void>}
137
+ * @memberof TransactionBlockstore
138
+ */
139
+ async commit(innerBlockstore) {
140
+ await this.doCommit(innerBlockstore);
141
+ }
142
+ // first get the transaction blockstore from the map of transaction blockstores
143
+ // then copy it to committedBlocks
144
+ // then write the transaction blockstore to a car
145
+ // then write the car to the valet
146
+ // then remove the transaction blockstore from the map of transaction blockstores
147
+ doCommit = async (innerBlockstore) => {
148
+ const cids = new Set();
149
+ for (const { cid, bytes } of innerBlockstore.entries()) {
150
+ const stringCid = cid.toString(); // unnecessary string conversion, can we fix upstream?
151
+ if (this.committedBlocks.has(stringCid)) {
152
+ // console.log('Duplicate block: ' + stringCid) // todo some of this can be avoided, cost is extra size on car files
153
+ }
154
+ else {
155
+ this.committedBlocks.set(stringCid, bytes);
156
+ cids.add(stringCid);
157
+ }
158
+ }
159
+ if (cids.size > 0 && this.valet) {
160
+ // console.log(innerBlockstore.label, 'committing', cids.size, 'blocks')
161
+ await this.valet.writeTransaction(innerBlockstore, cids);
162
+ }
163
+ };
164
+ /**
165
+ * Retire the transaction. Clears the uncommited blocks.
166
+ * @returns {void}
167
+ * @memberof TransactionBlockstore
168
+ */
169
+ retire(innerBlockstore) {
170
+ this.inflightTransactions.delete(innerBlockstore);
171
+ }
172
+ }
173
+ /**
174
+ * Runs a function on an inner blockstore, then persists the change to a car writer
175
+ * or other outer blockstore.
176
+ * @param {string} label
177
+ * @param {TransactionBlockstore} blockstore
178
+ * @param {(innerBlockstore: Blockstore) => Promise<any>} doFun
179
+ * @returns {Promise<any>}
180
+ * @memberof TransactionBlockstore
181
+ */
182
+ export const doTransaction = async (label, blockstore, doFun) => {
183
+ // @ts-ignore
184
+ if (!blockstore.commit)
185
+ return await doFun(blockstore);
186
+ // @ts-ignore
187
+ const innerBlockstore = blockstore.begin(label);
188
+ try {
189
+ const result = await doFun(innerBlockstore);
190
+ // @ts-ignore
191
+ await blockstore.commit(innerBlockstore);
192
+ return result;
193
+ }
194
+ catch (e) {
195
+ console.error(`Transaction ${label} failed`, e, e.stack);
196
+ throw e;
197
+ }
198
+ finally {
199
+ // @ts-ignore
200
+ blockstore.retire(innerBlockstore);
201
+ }
202
+ };
203
+ export class InnerBlockstore {
204
+ /** @type {Map<string, Uint8Array>} */
205
+ blocks = new Map();
206
+ lastCid = null;
207
+ label = '';
208
+ parentBlockstore = null;
209
+ constructor(label, parentBlockstore) {
210
+ this.label = label;
211
+ this.parentBlockstore = parentBlockstore;
212
+ }
213
+ /**
214
+ * @param {import('./link').AnyLink} cid
215
+ * @returns {Promise<AnyBlock | undefined>}
216
+ */
217
+ async get(cid) {
218
+ const key = cid.toString();
219
+ let bytes = this.blocks.get(key);
220
+ if (bytes) {
221
+ return { cid, bytes };
222
+ }
223
+ bytes = await this.parentBlockstore.committedGet(key);
224
+ if (bytes) {
225
+ return { cid, bytes };
226
+ }
227
+ }
228
+ /**
229
+ * @param {import('./link').AnyLink} cid
230
+ * @param {Uint8Array} bytes
231
+ */
232
+ async put(cid, bytes) {
233
+ // console.log('put', cid)
234
+ this.blocks.set(cid.toString(), bytes);
235
+ this.lastCid = cid;
236
+ }
237
+ *entries() {
238
+ for (const [str, bytes] of this.blocks) {
239
+ yield { cid: parse(str), bytes };
240
+ }
241
+ }
242
+ }
package/dist/clock.js ADDED
@@ -0,0 +1,355 @@
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
+ }
package/dist/crypto.js ADDED
@@ -0,0 +1,59 @@
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 };