@fireproof/core 0.3.21 → 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.
- package/dist/blockstore.js +242 -0
- package/dist/clock.js +355 -0
- package/dist/crypto.js +59 -0
- package/dist/database.js +308 -0
- package/dist/db-index.js +314 -0
- package/dist/fireproof.js +83 -0
- package/dist/hooks/use-fireproof.js +100 -0
- package/dist/listener.js +110 -0
- package/dist/prolly.js +316 -0
- package/dist/sha1.js +74 -0
- package/dist/src/blockstore.js +242 -0
- package/dist/src/clock.js +355 -0
- package/dist/src/crypto.js +59 -0
- package/dist/src/database.js +312 -0
- package/dist/src/db-index.js +314 -0
- package/dist/src/fireproof.d.ts +319 -0
- package/dist/src/fireproof.js +38976 -0
- package/dist/src/fireproof.js.map +1 -0
- package/dist/src/fireproof.mjs +38972 -0
- package/dist/src/fireproof.mjs.map +1 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +21 -16
- package/dist/src/index.js.map +1 -1
- package/dist/src/index.mjs +21 -16
- package/dist/src/index.mjs.map +1 -1
- package/dist/src/listener.js +108 -0
- package/dist/src/prolly.js +319 -0
- package/dist/src/sha1.js +74 -0
- package/dist/src/utils.js +16 -0
- package/dist/src/valet.js +262 -0
- package/dist/test/block.js +57 -0
- package/dist/test/clock.test.js +556 -0
- package/dist/test/db-index.test.js +231 -0
- package/dist/test/fireproof.test.js +444 -0
- package/dist/test/fulltext.test.js +61 -0
- package/dist/test/helpers.js +39 -0
- package/dist/test/hydrator.test.js +142 -0
- package/dist/test/listener.test.js +103 -0
- package/dist/test/prolly.test.js +162 -0
- package/dist/test/proofs.test.js +45 -0
- package/dist/test/reproduce-fixture-bug.test.js +57 -0
- package/dist/test/valet.test.js +56 -0
- package/dist/utils.js +16 -0
- package/dist/valet.js +262 -0
- package/hooks/use-fireproof.js +38 -63
- package/package.json +13 -14
- package/src/blockstore.js +8 -4
- package/src/database.js +338 -0
- package/src/db-index.js +3 -3
- package/src/fireproof.js +65 -322
- package/src/listener.js +10 -8
- package/src/prolly.js +10 -6
- package/src/utils.js +16 -0
- package/src/valet.js +2 -2
- package/src/hydrator.js +0 -54
- package/src/index.js +0 -6
package/src/database.js
ADDED
@@ -0,0 +1,338 @@
|
|
1
|
+
// @ts-nocheck
|
2
|
+
import { visMerkleClock, visMerkleTree, vis, put, get, getAll, eventsSince } from './prolly.js'
|
3
|
+
import { doTransaction } from './blockstore.js'
|
4
|
+
import charwise from 'charwise'
|
5
|
+
import { localSet } from './utils.js'
|
6
|
+
|
7
|
+
// TypeScript Types
|
8
|
+
// eslint-disable-next-line no-unused-vars
|
9
|
+
// import { CID } from 'multiformats/dist/types/src/cid.js'
|
10
|
+
// eslint-disable-next-line no-unused-vars
|
11
|
+
class Proof {}
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @class Fireproof
|
15
|
+
* @classdesc Fireproof stores data in IndexedDB and provides a Merkle clock.
|
16
|
+
* This is the main class for saving and loading JSON and other documents with the database. You can find additional examples and
|
17
|
+
* usage guides in the repository README.
|
18
|
+
*
|
19
|
+
* @param {import('./blockstore.js').TransactionBlockstore} blocks - The block storage instance to use documents and indexes
|
20
|
+
* @param {CID[]} clock - The Merkle clock head to use for the Fireproof instance.
|
21
|
+
* @param {object} [config] - Optional configuration options for the Fireproof instance.
|
22
|
+
* @param {object} [authCtx] - Optional authorization context object to use for any authentication checks.
|
23
|
+
*
|
24
|
+
*/
|
25
|
+
export class Database {
|
26
|
+
listeners = new Set()
|
27
|
+
|
28
|
+
// todo refactor this for the next version
|
29
|
+
constructor (blocks, clock, config = {}) {
|
30
|
+
this.name = config.name
|
31
|
+
this.instanceId = `fp.${this.name}.${Math.random().toString(36).substring(2, 7)}`
|
32
|
+
this.blocks = blocks
|
33
|
+
this.clock = clock
|
34
|
+
this.config = config
|
35
|
+
this.indexes = new Map()
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Renders the Fireproof instance as a JSON object.
|
40
|
+
* @returns {Object} - The JSON representation of the Fireproof instance. Includes clock heads for the database and its indexes.
|
41
|
+
* @memberof Fireproof
|
42
|
+
* @instance
|
43
|
+
*/
|
44
|
+
toJSON () {
|
45
|
+
// todo this also needs to return the index roots...
|
46
|
+
return {
|
47
|
+
clock: this.clockToJSON(),
|
48
|
+
name: this.name,
|
49
|
+
key: this.blocks.valet?.getKeyMaterial(),
|
50
|
+
indexes: [...this.indexes.values()].map(index => index.toJSON())
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Returns the Merkle clock heads for the Fireproof instance.
|
56
|
+
* @returns {string[]} - The Merkle clock heads for the Fireproof instance.
|
57
|
+
* @memberof Fireproof
|
58
|
+
* @instance
|
59
|
+
*/
|
60
|
+
clockToJSON () {
|
61
|
+
return this.clock.map(cid => cid.toString())
|
62
|
+
}
|
63
|
+
|
64
|
+
hydrate ({ clock, name, key }) {
|
65
|
+
this.name = name
|
66
|
+
this.clock = clock
|
67
|
+
this.blocks.valet?.setKeyMaterial(key)
|
68
|
+
this.indexBlocks = null
|
69
|
+
}
|
70
|
+
|
71
|
+
maybeSaveClock () {
|
72
|
+
if (this.name && this.blocks.valet) {
|
73
|
+
localSet('fp.' + this.name, JSON.stringify(this))
|
74
|
+
}
|
75
|
+
}
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Triggers a notification to all listeners
|
79
|
+
* of the Fireproof instance so they can repaint UI, etc.
|
80
|
+
* @returns {Promise<void>}
|
81
|
+
* @memberof Fireproof
|
82
|
+
* @instance
|
83
|
+
*/
|
84
|
+
async notifyReset () {
|
85
|
+
await this.notifyListeners({ _reset: true, _clock: this.clockToJSON() })
|
86
|
+
}
|
87
|
+
|
88
|
+
// used be indexes etc to notify database listeners of new availability
|
89
|
+
async notifyExternal (source = 'unknown') {
|
90
|
+
await this.notifyListeners({ _external: source, _clock: this.clockToJSON() })
|
91
|
+
}
|
92
|
+
|
93
|
+
/**
|
94
|
+
* Returns the changes made to the Fireproof instance since the specified event.
|
95
|
+
* @function changesSince
|
96
|
+
* @param {CID[]} [event] - The clock head to retrieve changes since. If null or undefined, retrieves all changes.
|
97
|
+
* @returns {Promise<{rows : Object[], clock: CID[], proof: {}}>} An object containing the rows and the head of the instance's clock.
|
98
|
+
* @memberof Fireproof
|
99
|
+
* @instance
|
100
|
+
*/
|
101
|
+
async changesSince (event) {
|
102
|
+
// console.log('changesSince', this.instanceId, event, this.clock)
|
103
|
+
let rows, dataCIDs, clockCIDs
|
104
|
+
// if (!event) event = []
|
105
|
+
if (event) {
|
106
|
+
const resp = await eventsSince(this.blocks, this.clock, event)
|
107
|
+
const docsMap = new Map()
|
108
|
+
for (const { key, type, value } of resp.result.map(decodeEvent)) {
|
109
|
+
if (type === 'del') {
|
110
|
+
docsMap.set(key, { key, del: true })
|
111
|
+
} else {
|
112
|
+
docsMap.set(key, { key, value })
|
113
|
+
}
|
114
|
+
}
|
115
|
+
rows = Array.from(docsMap.values())
|
116
|
+
clockCIDs = resp.clockCIDs
|
117
|
+
// console.log('change rows', this.instanceId, rows)
|
118
|
+
} else {
|
119
|
+
const allResp = await getAll(this.blocks, this.clock)
|
120
|
+
rows = allResp.result.map(({ key, value }) => (decodeEvent({ key, value })))
|
121
|
+
dataCIDs = allResp.cids
|
122
|
+
// console.log('dbdoc rows', this.instanceId, rows)
|
123
|
+
}
|
124
|
+
return {
|
125
|
+
rows,
|
126
|
+
clock: this.clockToJSON(),
|
127
|
+
proof: { data: await cidsToProof(dataCIDs), clock: await cidsToProof(clockCIDs) }
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
async allDocuments () {
|
132
|
+
const allResp = await getAll(this.blocks, this.clock)
|
133
|
+
const rows = allResp.result.map(({ key, value }) => (decodeEvent({ key, value }))).map(({ key, value }) => ({ key, value: { _id: key, ...value } }))
|
134
|
+
return {
|
135
|
+
rows,
|
136
|
+
clock: this.clockToJSON(),
|
137
|
+
proof: await cidsToProof(allResp.cids)
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
/**
|
142
|
+
* Runs validation on the specified document using the Fireproof instance's configuration. Throws an error if the document is invalid.
|
143
|
+
*
|
144
|
+
* @param {Object} doc - The document to validate.
|
145
|
+
* @returns {Promise<void>}
|
146
|
+
* @throws {Error} - Throws an error if the document is invalid.
|
147
|
+
* @memberof Fireproof
|
148
|
+
* @instance
|
149
|
+
*/
|
150
|
+
async runValidation (doc) {
|
151
|
+
if (this.config && this.config.validateChange) {
|
152
|
+
const oldDoc = await this.get(doc._id)
|
153
|
+
.then((doc) => doc)
|
154
|
+
.catch(() => ({}))
|
155
|
+
this.config.validateChange(doc, oldDoc, this.authCtx)
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
/**
|
160
|
+
* Retrieves the document with the specified ID from the database
|
161
|
+
*
|
162
|
+
* @param {string} key - the ID of the document to retrieve
|
163
|
+
* @param {Object} [opts] - options
|
164
|
+
* @returns {Promise<{_id: string}>} - the document with the specified ID
|
165
|
+
* @memberof Fireproof
|
166
|
+
* @instance
|
167
|
+
*/
|
168
|
+
async get (key, opts = {}) {
|
169
|
+
const clock = opts.clock || this.clock
|
170
|
+
const resp = await get(this.blocks, clock, charwise.encode(key))
|
171
|
+
|
172
|
+
// this tombstone is temporary until we can get the prolly tree to delete
|
173
|
+
if (!resp || resp.result === null) {
|
174
|
+
throw new Error('Not found')
|
175
|
+
}
|
176
|
+
const doc = resp.result
|
177
|
+
if (opts.mvcc === true) {
|
178
|
+
doc._clock = this.clockToJSON()
|
179
|
+
}
|
180
|
+
doc._proof = {
|
181
|
+
data: await cidsToProof(resp.cids),
|
182
|
+
clock: this.clockToJSON()
|
183
|
+
}
|
184
|
+
doc._id = key
|
185
|
+
return doc
|
186
|
+
}
|
187
|
+
/**
|
188
|
+
* @typedef {any} Document
|
189
|
+
* @property {string} _id - The ID of the document (required)
|
190
|
+
* @property {string} [_proof] - The proof of the document (optional)
|
191
|
+
* @property {string} [_clock] - The clock of the document (optional)
|
192
|
+
* @property {any} [key: string] - Index signature notation to allow any other unknown fields
|
193
|
+
*/
|
194
|
+
|
195
|
+
/**
|
196
|
+
* Adds a new document to the database, or updates an existing document. Returns the ID of the document and the new clock head.
|
197
|
+
*
|
198
|
+
* @param {Document} doc - the document to be added
|
199
|
+
* @returns {Promise<{ id: string, clock: CID[] }>} - The result of adding the document to the database
|
200
|
+
* @memberof Fireproof
|
201
|
+
* @instance
|
202
|
+
*/
|
203
|
+
async put ({ _id, _proof, ...doc }) {
|
204
|
+
const id = _id || 'f' + Math.random().toString(36).slice(2)
|
205
|
+
await this.runValidation({ _id: id, ...doc })
|
206
|
+
return await this.putToProllyTree({ key: id, value: doc }, doc._clock)
|
207
|
+
}
|
208
|
+
|
209
|
+
/**
|
210
|
+
* Deletes a document from the database
|
211
|
+
* @param {string | any} docOrId - the document ID
|
212
|
+
* @returns {Promise<{ id: string, clock: CID[] }>} - The result of deleting the document from the database
|
213
|
+
* @memberof Fireproof
|
214
|
+
* @instance
|
215
|
+
*/
|
216
|
+
async del (docOrId) {
|
217
|
+
let id
|
218
|
+
let clock = null
|
219
|
+
if (docOrId._id) {
|
220
|
+
id = docOrId._id
|
221
|
+
clock = docOrId._clock
|
222
|
+
} else {
|
223
|
+
id = docOrId
|
224
|
+
}
|
225
|
+
await this.runValidation({ _id: id, _deleted: true })
|
226
|
+
return await this.putToProllyTree({ key: id, del: true }, clock) // not working at prolly tree layer?
|
227
|
+
// this tombstone is temporary until we can get the prolly tree to delete
|
228
|
+
// return await this.putToProllyTree({ key: id, value: null }, clock)
|
229
|
+
}
|
230
|
+
|
231
|
+
/**
|
232
|
+
* Updates the underlying storage with the specified event.
|
233
|
+
* @private
|
234
|
+
* @param {{del?: true, key : string, value?: any}} decodedEvent - the event to add
|
235
|
+
* @returns {Promise<{ proof:{}, id: string, clock: CID[] }>} - The result of adding the event to storage
|
236
|
+
*/
|
237
|
+
async putToProllyTree (decodedEvent, clock = null) {
|
238
|
+
const event = encodeEvent(decodedEvent)
|
239
|
+
if (clock && JSON.stringify(clock) !== JSON.stringify(this.clockToJSON())) {
|
240
|
+
// we need to check and see what version of the document exists at the clock specified
|
241
|
+
// if it is the same as the one we are trying to put, then we can proceed
|
242
|
+
const resp = await eventsSince(this.blocks, this.clock, event.value._clock)
|
243
|
+
const missedChange = resp.result.find(({ key }) => key === event.key)
|
244
|
+
if (missedChange) {
|
245
|
+
throw new Error('MVCC conflict, document is changed, please reload the document and try again.')
|
246
|
+
}
|
247
|
+
}
|
248
|
+
const result = await doTransaction(
|
249
|
+
'putToProllyTree',
|
250
|
+
this.blocks,
|
251
|
+
async (blocks) => await put(blocks, this.clock, event)
|
252
|
+
)
|
253
|
+
if (!result) {
|
254
|
+
console.error('failed', event)
|
255
|
+
throw new Error('failed to put at storage layer')
|
256
|
+
}
|
257
|
+
// console.log('new clock head', this.instanceId, result.head.toString())
|
258
|
+
this.clock = result.head // do we want to do this as a finally block
|
259
|
+
await this.notifyListeners([decodedEvent]) // this type is odd
|
260
|
+
return {
|
261
|
+
id: decodedEvent.key,
|
262
|
+
clock: this.clockToJSON(),
|
263
|
+
proof: { data: await cidsToProof(result.cids), clock: await cidsToProof(result.clockCIDs) }
|
264
|
+
}
|
265
|
+
// todo should include additions (or split clock)
|
266
|
+
}
|
267
|
+
|
268
|
+
// /**
|
269
|
+
// * Advances the clock to the specified event and updates the root CID
|
270
|
+
// * Will be used by replication
|
271
|
+
// */
|
272
|
+
// async advance (event) {
|
273
|
+
// this.clock = await advance(this.blocks, this.clock, event)
|
274
|
+
// this.rootCid = await root(this.blocks, this.clock)
|
275
|
+
// return this.clock
|
276
|
+
// }
|
277
|
+
|
278
|
+
async * vis () {
|
279
|
+
return yield * vis(this.blocks, this.clock)
|
280
|
+
}
|
281
|
+
|
282
|
+
async visTree () {
|
283
|
+
return await visMerkleTree(this.blocks, this.clock)
|
284
|
+
}
|
285
|
+
|
286
|
+
async visClock () {
|
287
|
+
return await visMerkleClock(this.blocks, this.clock)
|
288
|
+
}
|
289
|
+
|
290
|
+
/**
|
291
|
+
* Registers a Listener to be called when the Fireproof instance's clock is updated.
|
292
|
+
* Recieves live changes from the database after they are committed.
|
293
|
+
* @param {Function} listener - The listener to be called when the clock is updated.
|
294
|
+
* @returns {Function} - A function that can be called to unregister the listener.
|
295
|
+
* @memberof Fireproof
|
296
|
+
*/
|
297
|
+
registerListener (listener) {
|
298
|
+
this.listeners.add(listener)
|
299
|
+
return () => {
|
300
|
+
this.listeners.delete(listener)
|
301
|
+
}
|
302
|
+
}
|
303
|
+
|
304
|
+
async notifyListeners (changes) {
|
305
|
+
// await sleep(10)
|
306
|
+
await this.maybeSaveClock()
|
307
|
+
for (const listener of this.listeners) {
|
308
|
+
await listener(changes)
|
309
|
+
}
|
310
|
+
}
|
311
|
+
|
312
|
+
setCarUploader (carUploaderFn) {
|
313
|
+
// console.log('registering car uploader')
|
314
|
+
// https://en.wikipedia.org/wiki/Law_of_Demeter - this is a violation of the law of demeter
|
315
|
+
this.blocks.valet.uploadFunction = carUploaderFn
|
316
|
+
}
|
317
|
+
|
318
|
+
setRemoteBlockReader (remoteBlockReaderFn) {
|
319
|
+
this.blocks.remoteBlockFunction = remoteBlockReaderFn
|
320
|
+
}
|
321
|
+
}
|
322
|
+
|
323
|
+
export async function cidsToProof (cids) {
|
324
|
+
if (!cids || !cids.all) return []
|
325
|
+
const all = await cids.all()
|
326
|
+
return [...all].map((cid) => cid.toString())
|
327
|
+
}
|
328
|
+
|
329
|
+
function decodeEvent (event) {
|
330
|
+
const decodedKey = charwise.decode(event.key)
|
331
|
+
return { ...event, key: decodedKey }
|
332
|
+
}
|
333
|
+
|
334
|
+
function encodeEvent (event) {
|
335
|
+
if (!(event && event.key)) return
|
336
|
+
const encodedKey = charwise.encode(event.key)
|
337
|
+
return { ...event, key: encodedKey }
|
338
|
+
}
|
package/src/db-index.js
CHANGED
@@ -9,7 +9,7 @@ import { nocache as cache } from 'prolly-trees/cache'
|
|
9
9
|
import { bf, simpleCompare } from 'prolly-trees/utils'
|
10
10
|
import { makeGetBlock } from './prolly.js'
|
11
11
|
// eslint-disable-next-line no-unused-vars
|
12
|
-
import {
|
12
|
+
import { Database, cidsToProof } from './database.js'
|
13
13
|
|
14
14
|
import * as codec from '@ipld/dag-cbor'
|
15
15
|
// import { create as createBlock } from 'multiformats/block'
|
@@ -88,7 +88,7 @@ const indexEntriesForChanges = (changes, mapFn) => {
|
|
88
88
|
* @class DbIndex
|
89
89
|
* @classdesc An DbIndex can be used to order and filter the documents in a Fireproof database.
|
90
90
|
*
|
91
|
-
* @param {
|
91
|
+
* @param {Database} database - The Fireproof database instance to DbIndex.
|
92
92
|
* @param {Function} mapFn - The map function to apply to each entry in the database.
|
93
93
|
*
|
94
94
|
*/
|
@@ -96,7 +96,7 @@ export class DbIndex {
|
|
96
96
|
constructor (database, mapFn, clock, opts = {}) {
|
97
97
|
this.database = database
|
98
98
|
if (!database.indexBlocks) {
|
99
|
-
database.indexBlocks = new TransactionBlockstore(database
|
99
|
+
database.indexBlocks = new TransactionBlockstore(database?.name + '.indexes', database.blocks.valet?.getKeyMaterial())
|
100
100
|
}
|
101
101
|
/**
|
102
102
|
* The map function to apply to each entry in the database.
|