@fireproof/core 0.6.4 → 0.7.0-alpha.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 (48) hide show
  1. package/README.md +2 -2
  2. package/dist/{src/blockstore.js → blockstore.js} +7 -3
  3. package/dist/{src/database.js → database.js} +74 -48
  4. package/dist/{src/db-index.js → db-index.js} +13 -2
  5. package/dist/fireproof.js +92 -0
  6. package/dist/import.js +29 -0
  7. package/dist/loader.js +23 -0
  8. package/dist/{src/prolly.js → prolly.js} +1 -0
  9. package/dist/src/fireproof.d.ts +138 -137
  10. package/dist/src/fireproof.js +18183 -11479
  11. package/dist/src/fireproof.js.map +1 -1
  12. package/dist/src/fireproof.mjs +18184 -11479
  13. package/dist/src/fireproof.mjs.map +1 -1
  14. package/dist/storage/base.js +348 -0
  15. package/dist/storage/browser.js +61 -0
  16. package/dist/storage/filesystem.js +65 -0
  17. package/dist/storage/rest.js +58 -0
  18. package/dist/storage/ucan.js +0 -0
  19. package/dist/{src/sync.js → sync.js} +1 -1
  20. package/dist/valet.js +200 -0
  21. package/package.json +4 -5
  22. package/src/blockstore.js +6 -3
  23. package/src/database.js +80 -52
  24. package/src/db-index.js +12 -2
  25. package/src/fireproof.js +41 -30
  26. package/src/import.js +34 -0
  27. package/src/loader.js +26 -0
  28. package/src/prolly.js +2 -0
  29. package/src/storage/base.js +371 -0
  30. package/src/storage/browser.js +67 -0
  31. package/src/storage/filesystem.js +70 -0
  32. package/src/storage/rest.js +60 -0
  33. package/src/storage/ucan.js +0 -0
  34. package/src/sync.js +1 -1
  35. package/src/valet.js +57 -359
  36. package/dist/hooks/use-fireproof.js +0 -150
  37. package/dist/src/crypto-poly.js +0 -4
  38. package/dist/src/link.js +0 -1
  39. package/dist/src/loader.js +0 -131
  40. package/dist/src/valet.js +0 -476
  41. package/hooks/use-fireproof.js +0 -173
  42. package/src/listener.js +0 -119
  43. package/src/utils.js +0 -16
  44. /package/dist/{src/clock.js → clock.js} +0 -0
  45. /package/dist/{src/crypto.js → crypto.js} +0 -0
  46. /package/dist/{src/listener.js → listener.js} +0 -0
  47. /package/dist/{src/sha1.js → sha1.js} +0 -0
  48. /package/dist/{src/utils.js → utils.js} +0 -0
@@ -1,173 +0,0 @@
1
- // @ts-ignore
2
- import { useEffect, useState, useCallback, createContext } from 'react'
3
- import { Fireproof, Index } from '@fireproof/core'
4
-
5
- /**
6
- @typedef {Object} FireproofCtxValue
7
- @property {Function} addSubscriber - A function to add a subscriber with a label and function.
8
- @property {Fireproof} database - An instance of the Fireproof class.
9
- @property {Function} useLiveQuery - A hook to return a query result
10
- @property {Function} useLiveDocument - A hook to return a live document
11
- @property {boolean} ready - A boolean indicating whether the database is ready.
12
- @param {string} label - A label for the subscriber.
13
- @param {Function} fn - A function to be added as a subscriber.
14
- @returns {void}
15
- */
16
- export const FireproofCtx = createContext({
17
- addSubscriber: () => {},
18
- database: null,
19
- ready: false
20
- })
21
-
22
- // const inboundSubscriberQueue = new Map()
23
-
24
- let startedSetup = false
25
- let database
26
- const initializeDatabase = name => {
27
- if (database) return
28
- database = Fireproof.storage(name)
29
- }
30
-
31
- /**
32
- @function useFireproof
33
- React hook to initialize a Fireproof database, automatically saving and loading the clock.
34
- You might need to import { nodePolyfills } from 'vite-plugin-node-polyfills' in your vite.config.ts
35
- @deprecated - npm install @fireproof/react instead
36
- @param {string} name - The path to the database file
37
- @param {function(database): void} [defineDatabaseFn] - Synchronous function that defines the database, run this before any async calls
38
- @param {function(database): Promise<void>} [setupDatabaseFn] - Asynchronous function that sets up the database, run this to load fixture data etc
39
- @returns {FireproofCtxValue} { useLiveQuery, useLiveDocument, database, ready }
40
- */
41
- export function useFireproof (name = 'useFireproof', defineDatabaseFn = () => {}, setupDatabaseFn = async () => {}) {
42
- const [ready, setReady] = useState(false)
43
- initializeDatabase(name)
44
-
45
- /**
46
- * @deprecated - use database.subscribe instead
47
- */
48
- const addSubscriber = (label, fn) => {
49
- // todo test that the label is not needed
50
- return database.registerListener(fn)
51
- // inboundSubscriberQueue.set(label, fn)
52
- }
53
-
54
- useEffect(() => {
55
- const doSetup = async () => {
56
- if (ready) return
57
- if (startedSetup) return
58
- startedSetup = true
59
- defineDatabaseFn(database) // define indexes before querying them
60
- if (database.clock.length === 0) {
61
- await setupDatabaseFn(database)
62
- }
63
- setReady(true)
64
- }
65
- doSetup()
66
- }, [ready])
67
-
68
- function useLiveDocument (initialDoc) {
69
- const id = initialDoc._id
70
- const [doc, setDoc] = useState(initialDoc)
71
-
72
- const saveDoc = async newDoc => {
73
- await database.put({ _id: id, ...newDoc })
74
- }
75
- const refreshDoc = useCallback(async () => {
76
- // todo add option for mvcc checks
77
- const got = await database.get(id).catch(() => initialDoc)
78
- setDoc(got)
79
- }, [id, initialDoc])
80
-
81
- useEffect(
82
- () =>
83
- database.subscribe(change => {
84
- if (change.find(c => c.key === id)) {
85
- refreshDoc() // todo use change.value
86
- }
87
- }),
88
- [id, refreshDoc]
89
- )
90
-
91
- useEffect(() => {
92
- refreshDoc()
93
- }, [])
94
-
95
- return [doc, saveDoc]
96
- }
97
-
98
- function useLiveQuery (mapFn, query = null, initialRows = []) {
99
- const [rows, setRows] = useState({ rows: initialRows, proof: {} })
100
- const [index, setIndex] = useState(null)
101
-
102
- const refreshRows = useCallback(async () => {
103
- if (!index) return
104
- const got = await index.query(query || {})
105
- setRows(got)
106
- }, [index, JSON.stringify(query)])
107
-
108
- useEffect(
109
- () => {
110
- // todo listen to index changes
111
- return database.subscribe(() => {
112
- refreshRows()
113
- })
114
- },
115
- [refreshRows]
116
- )
117
-
118
- useEffect(() => {
119
- refreshRows()
120
- }, [index])
121
-
122
- useEffect(() => {
123
- const index = new Index(database, null, mapFn) // this should only be created once
124
- setIndex(index)
125
- }, [mapFn.toString()])
126
-
127
- return rows
128
- }
129
-
130
- return {
131
- addSubscriber,
132
- useLiveQuery,
133
- useLiveDocument,
134
- database,
135
- ready
136
- }
137
- }
138
-
139
- // const husherMap = new Map()
140
- // const husher = (id, workFn, ms) => {
141
- // if (!husherMap.has(id)) {
142
- // const start = Date.now()
143
- // husherMap.set(
144
- // id,
145
- // workFn().finally(() => setTimeout(() => husherMap.delete(id), ms - (Date.now() - start)))
146
- // )
147
- // }
148
- // return husherMap.get(id)
149
- // }
150
- // const hushed =
151
- // (id, workFn, ms) =>
152
- // (...args) =>
153
- // husher(id, () => workFn(...args), ms)
154
-
155
- // let storageSupported = false
156
- // try {
157
- // storageSupported = window.localStorage && true
158
- // } catch (e) {}
159
- // export function localGet (key) {
160
- // if (storageSupported) {
161
- // return localStorage && localStorage.getItem(key)
162
- // }
163
- // }
164
- // function localSet (key, value) {
165
- // if (storageSupported) {
166
- // return localStorage && localStorage.setItem(key, value)
167
- // }
168
- // }
169
- // function localRemove(key) {
170
- // if (storageSupported) {
171
- // return localStorage && localStorage.removeItem(key)
172
- // }
173
- // }
package/src/listener.js DELETED
@@ -1,119 +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
- /**
12
- * @deprecated since version 0.7.0
13
- */
14
- export class Listener {
15
- subcribers = new Map()
16
- doStopListening = null
17
-
18
- /**
19
- * @param {import('./database.js').Database} database
20
- * @param {(_: any, emit: any) => void} routingFn
21
- */
22
- constructor (
23
- database,
24
- routingFn = function (/** @type {any} */ _, /** @type {(arg0: string) => void} */ emit) {
25
- emit('*')
26
- }
27
- ) {
28
- this.database = database
29
- this.doStopListening = database.registerListener((/** @type {any} */ changes) => this.onChanges(changes))
30
- /**
31
- * The map function to apply to each entry in the database.
32
- * @type {Function}
33
- */
34
- this.routingFn = routingFn
35
-
36
- this.dbHead = null
37
- }
38
-
39
- /**
40
- * Subscribe to a topic emitted by the event function.
41
- * @param {string} topic - The topic to subscribe to.
42
- * @param {Function} subscriber - The function to call when the topic is emitted.
43
- * @returns {Function} A function to unsubscribe from the topic.
44
- * @memberof Listener
45
- * @instance
46
- * @param {any} [since] - clock to flush from on launch, pass null for all
47
- */
48
- on (topic, subscriber, since = undefined) {
49
- const listOfTopicSubscribers = getTopicList(this.subcribers, topic)
50
- listOfTopicSubscribers.push(subscriber)
51
- if (typeof since !== 'undefined') {
52
- this.database.changesSince(since).then(({ rows: changes }) => {
53
- const keys = topicsForChanges(changes, this.routingFn).get(topic)
54
- if (keys) keys.forEach((/** @type {any} */ key) => subscriber(key))
55
- })
56
- }
57
- return () => {
58
- const index = listOfTopicSubscribers.indexOf(subscriber)
59
- if (index > -1) listOfTopicSubscribers.splice(index, 1)
60
- }
61
- }
62
-
63
- /**
64
- * @typedef {import('./db-index').ChangeEvent} ChangeEvent
65
- */
66
-
67
- /**
68
- * @param {ChangeEvent[]} changes
69
- */
70
- onChanges (changes) {
71
- if (Array.isArray(changes)) {
72
- const seenTopics = topicsForChanges(changes, this.routingFn)
73
- for (const [topic, keys] of seenTopics) {
74
- const listOfTopicSubscribers = getTopicList(this.subcribers, topic)
75
- listOfTopicSubscribers.forEach((/** @type {(arg0: any) => any} */ subscriber) =>
76
- keys.forEach((/** @type {any} */ key) => subscriber(key))
77
- )
78
- }
79
- } else {
80
- // non-arrays go to all subscribers
81
- for (const [, listOfTopicSubscribers] of this.subcribers) {
82
- listOfTopicSubscribers.forEach((/** @type {(arg0: any) => any} */ subscriber) => subscriber(changes))
83
- }
84
- }
85
- }
86
- }
87
-
88
- /**
89
- * @param {Map<any, any>} subscribersMap
90
- * @param {string} name
91
- */
92
- function getTopicList (subscribersMap, name) {
93
- let topicList = subscribersMap.get(name)
94
- if (!topicList) {
95
- topicList = []
96
- subscribersMap.set(name, topicList)
97
- }
98
- return topicList
99
- }
100
-
101
- /**
102
- * Transforms a set of changes to events using an emitter function.
103
- *
104
- * @param {ChangeEvent[]} changes
105
- * @param {Function} routingFn
106
- * @returns {Map<string,string[]>} The topics emmitted by the event function.
107
- * @private
108
- */
109
- const topicsForChanges = (changes, routingFn) => {
110
- const seenTopics = new Map()
111
- changes.forEach(({ key, value, del }) => {
112
- if (del || !value) value = { _deleted: true }
113
- routingFn({ _id: key, ...value }, (/** @type {any} */ t) => {
114
- const topicList = getTopicList(seenTopics, t)
115
- topicList.push(key)
116
- })
117
- })
118
- return seenTopics
119
- }
package/src/utils.js DELETED
@@ -1,16 +0,0 @@
1
-
2
- /* global localStorage */
3
- let storageSupported = false
4
- try {
5
- storageSupported = window.localStorage && true
6
- } catch (e) {}
7
- export function localGet (key) {
8
- if (storageSupported) {
9
- return localStorage && localStorage.getItem(key)
10
- }
11
- }
12
- export function localSet (key, value) {
13
- if (storageSupported) {
14
- return localStorage && localStorage.setItem(key, value)
15
- }
16
- }
File without changes
File without changes
File without changes
File without changes
File without changes