@fireproof/core 0.8.0 → 0.10.1-dev

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 (57) hide show
  1. package/README.md +5 -184
  2. package/dist/fireproof.browser.js +18879 -0
  3. package/dist/fireproof.browser.js.map +7 -0
  4. package/dist/fireproof.cjs.js +9305 -0
  5. package/dist/fireproof.cjs.js.map +7 -0
  6. package/dist/fireproof.esm.js +9295 -0
  7. package/dist/fireproof.esm.js.map +7 -0
  8. package/package.json +57 -105
  9. package/dist/blockstore.js +0 -268
  10. package/dist/clock.js +0 -459
  11. package/dist/crypto.js +0 -63
  12. package/dist/database.js +0 -434
  13. package/dist/db-index.js +0 -403
  14. package/dist/encrypted-block.js +0 -48
  15. package/dist/fireproof.js +0 -84
  16. package/dist/import.js +0 -29
  17. package/dist/listener.js +0 -111
  18. package/dist/loader.js +0 -13
  19. package/dist/prolly.js +0 -405
  20. package/dist/remote.js +0 -102
  21. package/dist/sha1.js +0 -74
  22. package/dist/src/fireproof.d.ts +0 -472
  23. package/dist/src/fireproof.js +0 -81191
  24. package/dist/src/fireproof.js.map +0 -1
  25. package/dist/src/fireproof.mjs +0 -81186
  26. package/dist/src/fireproof.mjs.map +0 -1
  27. package/dist/storage/base.js +0 -426
  28. package/dist/storage/blocksToEncryptedCarBlock.js +0 -144
  29. package/dist/storage/browser.js +0 -62
  30. package/dist/storage/filesystem.js +0 -67
  31. package/dist/storage/rest.js +0 -57
  32. package/dist/storage/ucan.js +0 -0
  33. package/dist/storage/utils.js +0 -144
  34. package/dist/sync.js +0 -218
  35. package/dist/utils.js +0 -16
  36. package/dist/valet.js +0 -102
  37. package/src/blockstore.js +0 -283
  38. package/src/clock.js +0 -486
  39. package/src/crypto.js +0 -70
  40. package/src/database.js +0 -469
  41. package/src/db-index.js +0 -426
  42. package/src/encrypted-block.js +0 -57
  43. package/src/fireproof.js +0 -98
  44. package/src/import.js +0 -34
  45. package/src/link.d.ts +0 -3
  46. package/src/loader.js +0 -16
  47. package/src/prolly.js +0 -445
  48. package/src/remote.js +0 -113
  49. package/src/sha1.js +0 -83
  50. package/src/storage/base.js +0 -463
  51. package/src/storage/browser.js +0 -67
  52. package/src/storage/filesystem.js +0 -73
  53. package/src/storage/rest.js +0 -59
  54. package/src/storage/ucan.js +0 -0
  55. package/src/storage/utils.js +0 -152
  56. package/src/sync.js +0 -237
  57. package/src/valet.js +0 -105
@@ -1,472 +0,0 @@
1
- import * as multiformats from 'multiformats';
2
- import { Link, CID } from 'multiformats';
3
-
4
- type AnyLink = Link<unknown, number, number, 1|0>
5
-
6
- declare class Valet {
7
- constructor(name?: string, config?: {});
8
- idb: any;
9
- name: any;
10
- uploadQueue: any;
11
- alreadyEnqueued: Set<any>;
12
- instanceId: string;
13
- primary: any;
14
- secondary: any;
15
- ready: Promise<any[]>;
16
- saveHeader(header: any): Promise<any>;
17
- compact(clock: any): Promise<void>;
18
- /**
19
- * Group the blocks into a car and write it to the valet.
20
- * @param {import('./blockstore.js').InnerBlockstore} innerBlockstore
21
- * @param {Set<string>} cids
22
- * @returns {Promise<void>}
23
- * @memberof Valet
24
- */
25
- writeTransaction(innerBlockstore: InnerBlockstore, cids: Set<string>): Promise<void>;
26
- /**
27
- * Iterate over all blocks in the store.
28
- *
29
- * @yields {{cid: string, value: Uint8Array}}
30
- * @returns {AsyncGenerator<any, any, any>}
31
- */
32
- cids(): AsyncGenerator<any, any, any>;
33
- remoteBlockFunction: any;
34
- getValetBlock(dataCID: any): Promise<any>;
35
- }
36
-
37
- /**
38
- * @typedef {{ get: (link: import('../src/link').AnyLink) => Promise<AnyBlock | undefined> }} BlockFetcher
39
- */
40
- /**
41
- * @typedef {Object} AnyBlock
42
- * @property {import('./link').AnyLink} cid - The CID of the block
43
- * @property {Uint8Array} bytes - The block's data
44
- *
45
- * @typedef {Object} Blockstore
46
- * @property {function(import('./link').AnyLink): Promise<AnyBlock|undefined>} get - A function to retrieve a block by CID
47
- * @property {function(import('./link').AnyLink, Uint8Array): Promise<void>} put - A function to store a block's data and CID
48
- *
49
- * A blockstore that caches writes to a transaction and only persists them when committed.
50
- */
51
- declare class TransactionBlockstore {
52
- constructor(name: any, config: any);
53
- /** @type {Map<string, Uint8Array>} */
54
- committedBlocks: Map<string, Uint8Array>;
55
- /** @type {Valet} */
56
- valet: Valet;
57
- instanceId: string;
58
- inflightTransactions: Set<any>;
59
- syncs: Set<any>;
60
- ready: Promise<void> | Promise<any[]>;
61
- remoteBlockFunction: any;
62
- /**
63
- * Get a block from the store.
64
- *
65
- * @param {import('./link').AnyLink} cid
66
- * @returns {Promise<AnyBlock | undefined>}
67
- */
68
- get(cid: AnyLink): Promise<AnyBlock | undefined>;
69
- transactionsGet(key: any): Promise<any>;
70
- committedGet(key: any): Promise<any>;
71
- clearCommittedCache(): Promise<void>;
72
- networkGet(key: any): Promise<any>;
73
- /**
74
- * Add a block to the store. Usually bound to a transaction by a closure.
75
- * It sets the lastCid property to the CID of the block that was put.
76
- * This is used by the transaction as the head of the car when written to the valet.
77
- * We don't have to worry about which transaction we are when we are here because
78
- * we are the transactionBlockstore.
79
- *
80
- * @param {import('./link').AnyLink} cid
81
- * @param {Uint8Array} bytes
82
- */
83
- put(cid: AnyLink, bytes: Uint8Array): void;
84
- /**
85
- * Iterate over all blocks in the store.
86
- *
87
- * @yields {{cid: string, bytes: Uint8Array}}
88
- * @returns {AsyncGenerator<any, any, any>}
89
- */
90
- entries(): AsyncGenerator<any, any, any>;
91
- /**
92
- * Begin a transaction. Ensures the uncommited blocks are empty at the begining.
93
- * Returns the blocks to read and write during the transaction.
94
- * @returns {InnerBlockstore}
95
- * @memberof TransactionBlockstore
96
- */
97
- begin(label?: string): InnerBlockstore;
98
- /**
99
- * Commit the transaction. Writes the blocks to the store.
100
- * @returns {Promise<void>}
101
- * @memberof TransactionBlockstore
102
- */
103
- commit(innerBlockstore: any, doSync?: boolean): Promise<void>;
104
- doCommit: (innerBlockstore: any) => Promise<void>;
105
- /**
106
- * Retire the transaction. Clears the uncommited blocks.
107
- * @returns {void}
108
- * @memberof TransactionBlockstore
109
- */
110
- retire(innerBlockstore: any): void;
111
- }
112
- declare class InnerBlockstore {
113
- constructor(label: any, parentBlockstore: any);
114
- /** @type {Map<string, Uint8Array>} */
115
- blocks: Map<string, Uint8Array>;
116
- head: any[];
117
- lastCid: any;
118
- label: string;
119
- parentBlockstore: any;
120
- /**
121
- * @param {import('./link').AnyLink} cid
122
- * @returns {Promise<AnyBlock | undefined>}
123
- */
124
- get(cid: AnyLink): Promise<AnyBlock | undefined>;
125
- /**
126
- * @param {import('./link').AnyLink} cid
127
- * @param {Uint8Array} bytes
128
- */
129
- put(cid: AnyLink, bytes: Uint8Array): Promise<void>;
130
- entries(): Generator<{
131
- cid: multiformats.Link<unknown, number, number, multiformats.Version>;
132
- bytes: Uint8Array;
133
- }, void, unknown>;
134
- }
135
- type AnyBlock = {
136
- /**
137
- * - The CID of the block
138
- */
139
- cid: AnyLink;
140
- /**
141
- * - The block's data
142
- */
143
- bytes: Uint8Array;
144
- };
145
-
146
- /**
147
- * @class Fireproof
148
- * @classdesc Fireproof stores data in IndexedDB and provides a Merkle clock.
149
- * This is the main class for saving and loading JSON and other documents with the database. You can find additional examples and
150
- * usage guides in the repository README.
151
- *
152
- * @param {CID[]} clock - The Merkle clock head to use for the Fireproof instance.
153
- * @param {object} [config] - Optional configuration options for the Fireproof instance.
154
- * @param {object} [authCtx] - Optional authorization context object to use for any authentication checks.
155
- *
156
- */
157
- declare class Database {
158
- constructor(name: any, config?: {});
159
- listeners: Set<any>;
160
- indexes: Map<any, any>;
161
- rootCache: any;
162
- eventsCache: Map<any, any>;
163
- remote: any;
164
- name: string;
165
- clock: any[];
166
- instanceId: string;
167
- blocks: TransactionBlockstore;
168
- indexBlocks: TransactionBlockstore;
169
- config: {};
170
- ready: Promise<void>;
171
- /**
172
- * Renders the Fireproof instance as a JSON object.
173
- * @returns {Object} - The JSON representation of the Fireproof instance. Includes clock heads for the database and its indexes.
174
- * @memberof Fireproof
175
- * @instance
176
- */
177
- toJSON(): any;
178
- toHeader(): {
179
- name: string;
180
- index: {
181
- key: any;
182
- car: any;
183
- };
184
- indexes: any[];
185
- };
186
- /**
187
- * Returns the Merkle clock heads for the Fireproof instance.
188
- * @returns {string[]} - The Merkle clock heads for the Fireproof instance.
189
- * @memberof Fireproof
190
- * @instance
191
- */
192
- clockToJSON(clock?: any): string[];
193
- maybeSaveClock(): Promise<void>;
194
- index(name: any): any;
195
- /**
196
- * Triggers a notification to all listeners
197
- * of the Fireproof instance so they can repaint UI, etc.
198
- * @returns {Promise<void>}
199
- * @memberof Fireproof
200
- * @instance
201
- */
202
- notifyReset(): Promise<void>;
203
- compact(): Promise<void>;
204
- /**
205
- * Returns the changes made to the Fireproof instance since the specified event.
206
- * @function changesSince
207
- * @param {CID[]} [event] - The clock head to retrieve changes since. If null or undefined, retrieves all changes.
208
- * @returns {Promise<{rows : Object[], clock: CID[], proof: {}}>} An object containing the rows and the head of the instance's clock.
209
- * @memberof Fireproof
210
- * @instance
211
- */
212
- changesSince(aClock: any): Promise<{
213
- rows: any[];
214
- clock: CID[];
215
- proof: {};
216
- }>;
217
- allDocuments(): Promise<{
218
- rows: {
219
- key: any;
220
- value: any;
221
- }[];
222
- clock: string[];
223
- proof: any[];
224
- }>;
225
- allCIDs(): Promise<any[]>;
226
- allStoredCIDs(): Promise<any[]>;
227
- /**
228
- * Retrieves the document with the specified ID from the database
229
- *
230
- * @param {string} key - the ID of the document to retrieve
231
- * @param {Object} [opts] - options
232
- * @returns {Promise<{_id: string}>} - the document with the specified ID
233
- * @memberof Fireproof
234
- * @instance
235
- */
236
- get(key: string, opts?: any): Promise<{
237
- _id: string;
238
- }>;
239
- /**
240
- * @typedef {any} Document
241
- * @property {string} _id - The ID of the document (required)
242
- * @property {string} [_proof] - The proof of the document (optional)
243
- * @property {string} [_clock] - The clock of the document (optional)
244
- * @property {Object.<string, any>} [unknown: string] - Any other unknown properties (optional)
245
- */
246
- /**
247
- * Adds a new document to the database, or updates an existing document. Returns the ID of the document and the new clock head.
248
- *
249
- * @param {Document} doc - the document to be added
250
- * @returns {Promise<{ id: string, clock: CID[] }>} - The result of adding the document to the database
251
- * @memberof Fireproof
252
- * @instance
253
- */
254
- put({ _id, _proof, _clock, ...doc }: any): Promise<{
255
- id: string;
256
- clock: CID[];
257
- }>;
258
- /**
259
- * Deletes a document from the database
260
- * @param {string | any} docOrId - the document ID
261
- * @returns {Promise<{ id: string, clock: CID[] }>} - The result of deleting the document from the database
262
- * @memberof Fireproof
263
- * @instance
264
- */
265
- del(docOrId: string | any): Promise<{
266
- id: string;
267
- clock: CID[];
268
- }>;
269
- /**
270
- * Runs validation on the specified document using the Fireproof instance's configuration. Throws an error if the document is invalid.
271
- *
272
- * @param {Object} doc - The document to validate.
273
- * @returns {Promise<void>}
274
- * @throws {Error} - Throws an error if the document is invalid.
275
- * @memberof Fireproof
276
- * @instance
277
- */
278
- runValidation(doc: any): Promise<void>;
279
- /**
280
- * Updates the underlying storage with the specified event.
281
- * @private
282
- * @param {{del?: true, key : string, value?: any}} decodedEvent - the event to add
283
- * @returns {Promise<{ proof:{}, id: string, clock: CID[] }>} - The result of adding the event to storage
284
- */
285
- private putToProllyTree;
286
- applyClock(prevClock: any, newClock: any): void;
287
- vis(): AsyncGenerator<any, {
288
- cids: any;
289
- result: any;
290
- vis?: undefined;
291
- } | {
292
- vis: string;
293
- cids: any;
294
- result?: undefined;
295
- }, unknown>;
296
- visTree(): Promise<{
297
- vis: string;
298
- cids: any;
299
- }>;
300
- visClock(): Promise<{
301
- vis: string;
302
- }>;
303
- /**
304
- * Registers a Listener to be called when the Fireproof instance's clock is updated.
305
- * Recieves live changes from the database after they are committed.
306
- * @param {Function} listener - The listener to be called when the clock is updated.
307
- * @returns {Function} - A function that can be called to unregister the listener.
308
- * @memberof Fireproof
309
- */
310
- subscribe(listener: Function): Function;
311
- /**
312
- * @deprecated 0.7.0 - renamed subscribe(listener)
313
- * @param {Function} listener - The listener to be called when the clock is updated.
314
- * @returns {Function} - A function that can be called to unregister the listener.
315
- * @memberof Fireproof
316
- */
317
- registerListener(listener: Function): Function;
318
- notifyListeners(changes: any): Promise<void>;
319
- setRemoteBlockReader(remoteBlockReaderFn: any): void;
320
- }
321
-
322
- /**
323
- * Represents an DbIndex for a Fireproof database.
324
- *
325
- * @class DbIndex
326
- * @classdesc An DbIndex can be used to order and filter the documents in a Fireproof database.
327
- *
328
- * @param {Database} database - The Fireproof database instance to DbIndex.
329
- * @param {Function} mapFn - The map function to apply to each entry in the database.
330
- *
331
- */
332
- declare class DbIndex {
333
- static registerWithDatabase(inIndex: any, database: any): void;
334
- static fromJSON(database: any, { code, clock, name }: {
335
- code: any;
336
- clock: any;
337
- name: any;
338
- }): DbIndex;
339
- /**
340
- * @param {Database} database
341
- */
342
- constructor(database: Database, name: any, mapFn: any, clock?: any, opts?: {});
343
- database: Database;
344
- indexById: {
345
- root: any;
346
- cid: any;
347
- };
348
- indexByKey: {
349
- root: any;
350
- cid: any;
351
- };
352
- dbHead: any;
353
- instanceId: string;
354
- updateIndexPromise: Promise<any>;
355
- applyMapFn(mapFn: any, name: any): void;
356
- mapFnString: any;
357
- mapFn: any;
358
- includeDocsDefault: any;
359
- name: any;
360
- makeName(): any;
361
- toJSON(): {
362
- name: any;
363
- code: any;
364
- clock: {
365
- db: any;
366
- byId: any;
367
- byKey: any;
368
- };
369
- };
370
- visKeyTree(): Promise<{
371
- vis: string;
372
- cids: any;
373
- }>;
374
- visIdTree(): Promise<{
375
- vis: string;
376
- cids: any;
377
- }>;
378
- /**
379
- * JSDoc for Query type.
380
- * @typedef {Object} DbQuery
381
- * @property {string[]} [range] - The range to query.
382
- * @memberof DbIndex
383
- */
384
- /**
385
- * Query object can have {range}
386
- * @param {DbQuery} query - the query range to use
387
- * @returns {Promise<{proof: {}, rows: Array<{id: string, key: string, value: any, doc?: any}>}>}
388
- * @memberof DbIndex
389
- * @instance
390
- */
391
- query(query?: {
392
- /**
393
- * - The range to query.
394
- */
395
- range?: string[];
396
- }, update?: boolean): Promise<{
397
- proof: {};
398
- rows: Array<{
399
- id: string;
400
- key: string;
401
- value: any;
402
- doc?: any;
403
- }>;
404
- }>;
405
- /**
406
- *
407
- * @param {any} resp
408
- * @param {any} query
409
- * @returns
410
- */
411
- applyQuery(resp: any, query: any): Promise<any>;
412
- doIndexQuery(query?: {}): Promise<any>;
413
- /**
414
- * Update the DbIndex with the latest changes
415
- * @private
416
- * @returns {Promise<void>}
417
- */
418
- private updateIndex;
419
- innerUpdateIndex(inBlocks: any): Promise<any>;
420
- }
421
-
422
- /**
423
- * @typedef {import('./database.js').Database} Database
424
- */
425
- declare class Sync {
426
- /**
427
- * @param {import("./database.js").Database} database
428
- * @param {string} key
429
- */
430
- static makeCar(database: Database, key: string, skip?: any[]): Promise<multiformats.BlockView<Uint8Array, 85, 18, 1>>;
431
- constructor(database: any, PeerClass?: any);
432
- /**
433
- * @param {Database} database
434
- * @param {typeof SimplePeer} [PeerClass]
435
- * @memberof Sync
436
- * @static
437
- */
438
- status: string;
439
- database: any;
440
- PeerClass: any;
441
- pushBacklog: Promise<any>;
442
- pushBacklogResolve: (value: any) => void;
443
- pushBacklogReject: (reason?: any) => void;
444
- isReady: boolean;
445
- offer(): Promise<string>;
446
- accept(base64offer: any): Promise<string>;
447
- connect(base64accept: any): void;
448
- setupPeer(initiator?: boolean): Promise<string>;
449
- peer: any;
450
- backlog(): Promise<any>;
451
- gotData(data: any): Promise<void>;
452
- destroy(): void;
453
- sendUpdate(blockstore: any): Promise<void>;
454
- startSync(): Promise<void>;
455
- }
456
-
457
- declare class Fireproof {
458
- /**
459
- * @function storage
460
- * @memberof Fireproof
461
- * Creates a new Fireproof instance with default storage settings
462
- * Most apps should use this and not worry about the details.
463
- * @static
464
- * @returns {Database} - a new Fireproof instance
465
- */
466
- static storage: (name?: any, opts?: {}) => Database;
467
- static fromJSON(primary: any, secondary: any, database: any): any;
468
- static snapshot(database: any, clock: any): any;
469
- static zoom(database: any, clock: any): Promise<any>;
470
- }
471
-
472
- export { Database, Fireproof, DbIndex as Index, Sync };