@fireproof/core 0.4.0 → 0.4.1

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 (48) hide show
  1. package/dist/src/fireproof.d.ts +27 -2
  2. package/dist/src/fireproof.js +2 -1
  3. package/dist/src/fireproof.js.map +1 -1
  4. package/dist/src/fireproof.mjs +2 -1
  5. package/dist/src/fireproof.mjs.map +1 -1
  6. package/package.json +1 -1
  7. package/src/database.js +2 -1
  8. package/dist/blockstore.js +0 -242
  9. package/dist/clock.js +0 -355
  10. package/dist/crypto.js +0 -59
  11. package/dist/database.js +0 -308
  12. package/dist/db-index.js +0 -314
  13. package/dist/fireproof.js +0 -83
  14. package/dist/hooks/use-fireproof.js +0 -100
  15. package/dist/listener.js +0 -110
  16. package/dist/main.js +0 -2
  17. package/dist/main.js.LICENSE.txt +0 -17
  18. package/dist/prolly.js +0 -316
  19. package/dist/sha1.js +0 -74
  20. package/dist/src/blockstore.js +0 -242
  21. package/dist/src/clock.js +0 -355
  22. package/dist/src/crypto.js +0 -59
  23. package/dist/src/database.js +0 -312
  24. package/dist/src/db-index.js +0 -314
  25. package/dist/src/index.d.ts +0 -321
  26. package/dist/src/index.js +0 -38936
  27. package/dist/src/index.js.map +0 -1
  28. package/dist/src/index.mjs +0 -38931
  29. package/dist/src/index.mjs.map +0 -1
  30. package/dist/src/listener.js +0 -108
  31. package/dist/src/prolly.js +0 -319
  32. package/dist/src/sha1.js +0 -74
  33. package/dist/src/utils.js +0 -16
  34. package/dist/src/valet.js +0 -262
  35. package/dist/test/block.js +0 -57
  36. package/dist/test/clock.test.js +0 -556
  37. package/dist/test/db-index.test.js +0 -231
  38. package/dist/test/fireproof.test.js +0 -444
  39. package/dist/test/fulltext.test.js +0 -61
  40. package/dist/test/helpers.js +0 -39
  41. package/dist/test/hydrator.test.js +0 -142
  42. package/dist/test/listener.test.js +0 -103
  43. package/dist/test/prolly.test.js +0 -162
  44. package/dist/test/proofs.test.js +0 -45
  45. package/dist/test/reproduce-fixture-bug.test.js +0 -57
  46. package/dist/test/valet.test.js +0 -56
  47. package/dist/utils.js +0 -16
  48. package/dist/valet.js +0 -262
package/dist/fireproof.js DELETED
@@ -1,83 +0,0 @@
1
- import randomBytes from 'randombytes';
2
- import { Database } from './database.js';
3
- import { Listener } from './listener.js';
4
- import { DbIndex as Index } from './db-index.js';
5
- import { CID } from 'multiformats';
6
- import { TransactionBlockstore } from './blockstore.js';
7
- import { localGet } from './utils.js';
8
- export { Index, Listener };
9
- const parseCID = cid => typeof cid === 'string' ? CID.parse(cid) : cid;
10
- class Fireproof {
11
- /**
12
- * @function storage
13
- * @memberof Fireproof
14
- * Creates a new Fireproof instance with default storage settings
15
- * Most apps should use this and not worry about the details.
16
- * @static
17
- * @returns {Fireproof} - a new Fireproof instance
18
- */
19
- static storage = (name = null, opts = {}) => {
20
- if (name) {
21
- opts.name = name;
22
- const existing = localGet('fp.' + name);
23
- if (existing) {
24
- const existingConfig = JSON.parse(existing);
25
- const fp = new Database(new TransactionBlockstore(name, existingConfig.key), [], opts);
26
- return this.fromJSON(existingConfig, fp);
27
- }
28
- else {
29
- const instanceKey = randomBytes(32).toString('hex'); // pass null to disable encryption
30
- return new Database(new TransactionBlockstore(name, instanceKey), [], opts);
31
- }
32
- }
33
- else {
34
- return new Database(new TransactionBlockstore(), [], opts);
35
- }
36
- };
37
- static fromJSON(json, database) {
38
- database.hydrate({ clock: json.clock.map(c => parseCID(c)), name: json.name, key: json.key });
39
- if (json.indexes) {
40
- for (const { name, code, clock: { byId, byKey, db } } of json.indexes) {
41
- Index.fromJSON(database, {
42
- clock: {
43
- byId: byId ? parseCID(byId) : null,
44
- byKey: byKey ? parseCID(byKey) : null,
45
- db: db ? db.map(c => parseCID(c)) : null
46
- },
47
- code,
48
- name
49
- });
50
- }
51
- }
52
- return database;
53
- }
54
- static snapshot(database, clock) {
55
- const definition = database.toJSON();
56
- const withBlocks = new Database(database.blocks);
57
- if (clock) {
58
- definition.clock = clock.map(c => parseCID(c));
59
- definition.indexes.forEach(index => {
60
- index.clock.byId = null;
61
- index.clock.byKey = null;
62
- index.clock.db = null;
63
- });
64
- }
65
- const snappedDb = this.fromJSON(definition, withBlocks);
66
- ([...database.indexes.values()]).forEach(index => {
67
- snappedDb.indexes.get(index.mapFnString).mapFn = index.mapFn;
68
- });
69
- return snappedDb;
70
- }
71
- static async zoom(database, clock) {
72
- ;
73
- ([...database.indexes.values()]).forEach(index => {
74
- index.indexById = { root: null, cid: null };
75
- index.indexByKey = { root: null, cid: null };
76
- index.dbHead = null;
77
- });
78
- database.clock = clock.map(c => parseCID(c));
79
- await database.notifyReset(); // hmm... indexes should listen to this? might be more complex than worth it. so far this is the only caller
80
- return database;
81
- }
82
- }
83
- export { Fireproof };
@@ -1,100 +0,0 @@
1
- // @ts-ignore
2
- import { useEffect, useState } from 'react';
3
- import { Fireproof, Listener } from '../src/fireproof.js';
4
- /**
5
- @typedef {Object} FireproofCtxValue
6
- @property {Function} addSubscriber - A function to add a subscriber with a label and function.
7
- @property {Fireproof} database - An instance of the Fireproof class.
8
- @property {boolean} ready - A boolean indicating whether the database is ready.
9
- @param {string} label - A label for the subscriber.
10
- @param {Function} fn - A function to be added as a subscriber.
11
- @returns {void}
12
- */
13
- const inboundSubscriberQueue = new Map();
14
- let startedSetup = false;
15
- let database;
16
- let listener;
17
- const initializeDatabase = name => {
18
- if (database)
19
- return;
20
- database = Fireproof.storage(name);
21
- listener = new Listener(database);
22
- };
23
- /**
24
-
25
- @function useFireproof
26
- React hook to initialize a Fireproof database, automatically saving and loading the clock.
27
- You might need to import { nodePolyfills } from 'vite-plugin-node-polyfills' in your vite.config.ts
28
- @param {string} name - The path to the database file
29
- @param {function(database): void} [defineDatabaseFn] - Synchronous function that defines the database, run this before any async calls
30
- @param {function(database): Promise<void>} [setupDatabaseFn] - Asynchronous function that sets up the database, run this to load fixture data etc
31
- @returns {FireproofCtxValue} { addSubscriber, database, ready }
32
- */
33
- export function useFireproof(name, defineDatabaseFn = () => { }, setupDatabaseFn = async () => { }) {
34
- const [ready, setReady] = useState(false);
35
- initializeDatabase(name || 'useFireproof');
36
- const addSubscriber = (label, fn) => {
37
- inboundSubscriberQueue.set(label, fn);
38
- };
39
- const listenerCallback = async (event) => {
40
- if (event._external)
41
- return;
42
- for (const [, fn] of inboundSubscriberQueue)
43
- fn();
44
- };
45
- useEffect(() => {
46
- const doSetup = async () => {
47
- if (ready)
48
- return;
49
- if (startedSetup)
50
- return;
51
- startedSetup = true;
52
- defineDatabaseFn(database); // define indexes before querying them
53
- if (database.clock.length === 0) {
54
- await setupDatabaseFn(database);
55
- }
56
- setReady(true);
57
- listener.on('*', listenerCallback); // hushed('*', listenerCallback, 250))
58
- };
59
- doSetup();
60
- }, [ready]);
61
- return {
62
- addSubscriber,
63
- database,
64
- ready
65
- };
66
- }
67
- // const husherMap = new Map()
68
- // const husher = (id, workFn, ms) => {
69
- // if (!husherMap.has(id)) {
70
- // const start = Date.now()
71
- // husherMap.set(
72
- // id,
73
- // workFn().finally(() => setTimeout(() => husherMap.delete(id), ms - (Date.now() - start)))
74
- // )
75
- // }
76
- // return husherMap.get(id)
77
- // }
78
- // const hushed =
79
- // (id, workFn, ms) =>
80
- // (...args) =>
81
- // husher(id, () => workFn(...args), ms)
82
- // let storageSupported = false
83
- // try {
84
- // storageSupported = window.localStorage && true
85
- // } catch (e) {}
86
- // export function localGet (key) {
87
- // if (storageSupported) {
88
- // return localStorage && localStorage.getItem(key)
89
- // }
90
- // }
91
- // function localSet (key, value) {
92
- // if (storageSupported) {
93
- // return localStorage && localStorage.setItem(key, value)
94
- // }
95
- // }
96
- // function localRemove(key) {
97
- // if (storageSupported) {
98
- // return localStorage && localStorage.removeItem(key)
99
- // }
100
- // }
package/dist/listener.js DELETED
@@ -1,110 +0,0 @@
1
- /**
2
- * A Fireproof database Listener allows you to react to events in the database.
3
- *
4
- * @class Listener
5
- * @classdesc An listener attaches to a Fireproof database and runs a routing function on each change, sending the results to subscribers.
6
- *
7
- * @param {import('./database.js').Database} database - The Database database instance to index.
8
- * @param {Function} routingFn - The routing function to apply to each entry in the database.
9
- */
10
- // import { ChangeEvent } from './db-index'
11
- export class Listener {
12
- subcribers = new Map();
13
- doStopListening = null;
14
- /**
15
- * @param {import('./database.js').Database} database
16
- * @param {(_: any, emit: any) => void} routingFn
17
- */
18
- constructor(database, routingFn) {
19
- this.database = database;
20
- this.doStopListening = database.registerListener((/** @type {any} */ changes) => this.onChanges(changes));
21
- /**
22
- * The map function to apply to each entry in the database.
23
- * @type {Function}
24
- */
25
- this.routingFn =
26
- routingFn ||
27
- function (/** @type {any} */ _, /** @type {(arg0: string) => void} */ emit) {
28
- emit('*');
29
- };
30
- this.dbHead = null;
31
- }
32
- /**
33
- * Subscribe to a topic emitted by the event function.
34
- * @param {string} topic - The topic to subscribe to.
35
- * @param {Function} subscriber - The function to call when the topic is emitted.
36
- * @returns {Function} A function to unsubscribe from the topic.
37
- * @memberof Listener
38
- * @instance
39
- * @param {any} since
40
- */
41
- on(topic, subscriber, since) {
42
- const listOfTopicSubscribers = getTopicList(this.subcribers, topic);
43
- listOfTopicSubscribers.push(subscriber);
44
- if (typeof since !== 'undefined') {
45
- this.database.changesSince(since).then(({ rows: changes }) => {
46
- const keys = topicsForChanges(changes, this.routingFn).get(topic);
47
- if (keys)
48
- keys.forEach((/** @type {any} */ key) => subscriber(key));
49
- });
50
- }
51
- return () => {
52
- const index = listOfTopicSubscribers.indexOf(subscriber);
53
- if (index > -1)
54
- listOfTopicSubscribers.splice(index, 1);
55
- };
56
- }
57
- /**
58
- * @typedef {import('./db-index').ChangeEvent} ChangeEvent
59
- */
60
- /**
61
- * @param {ChangeEvent[]} changes
62
- */
63
- onChanges(changes) {
64
- if (Array.isArray(changes)) {
65
- const seenTopics = topicsForChanges(changes, this.routingFn);
66
- for (const [topic, keys] of seenTopics) {
67
- const listOfTopicSubscribers = getTopicList(this.subcribers, topic);
68
- listOfTopicSubscribers.forEach((/** @type {(arg0: any) => any} */ subscriber) => keys.forEach((/** @type {any} */ key) => subscriber(key)));
69
- }
70
- }
71
- else {
72
- // non-arrays go to all subscribers
73
- for (const [, listOfTopicSubscribers] of this.subcribers) {
74
- listOfTopicSubscribers.forEach((/** @type {(arg0: any) => any} */ subscriber) => subscriber(changes));
75
- }
76
- }
77
- }
78
- }
79
- /**
80
- * @param {Map<any, any>} subscribersMap
81
- * @param {string} name
82
- */
83
- function getTopicList(subscribersMap, name) {
84
- let topicList = subscribersMap.get(name);
85
- if (!topicList) {
86
- topicList = [];
87
- subscribersMap.set(name, topicList);
88
- }
89
- return topicList;
90
- }
91
- /**
92
- * Transforms a set of changes to events using an emitter function.
93
- *
94
- * @param {ChangeEvent[]} changes
95
- * @param {Function} routingFn
96
- * @returns {Map<string,string[]>} The topics emmitted by the event function.
97
- * @private
98
- */
99
- const topicsForChanges = (changes, routingFn) => {
100
- const seenTopics = new Map();
101
- changes.forEach(({ key, value, del }) => {
102
- if (del || !value)
103
- value = { _deleted: true };
104
- routingFn({ _id: key, ...value }, (/** @type {any} */ t) => {
105
- const topicList = getTopicList(seenTopics, t);
106
- topicList.push(key);
107
- });
108
- });
109
- return seenTopics;
110
- };