@fireproof/core 0.3.2 → 0.3.3

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 (51) hide show
  1. package/README.md +1 -1
  2. package/dist/src/blockstore.d.ts +115 -0
  3. package/dist/src/blockstore.d.ts.map +1 -0
  4. package/dist/src/clock.d.ts +98 -0
  5. package/dist/src/clock.d.ts.map +1 -0
  6. package/dist/src/crypto.d.ts +18 -0
  7. package/dist/src/crypto.d.ts.map +1 -0
  8. package/dist/src/db-index.d.ts +116 -0
  9. package/dist/src/db-index.d.ts.map +1 -0
  10. package/dist/src/fireproof.d.ts +167 -0
  11. package/dist/src/fireproof.d.ts.map +1 -0
  12. package/dist/src/hydrator.d.ts +6 -0
  13. package/dist/src/hydrator.d.ts.map +1 -0
  14. package/dist/src/index.d.ts +6 -0
  15. package/dist/src/index.d.ts.map +1 -0
  16. package/dist/src/listener.d.ts +36 -0
  17. package/dist/src/listener.d.ts.map +1 -0
  18. package/dist/src/prolly.d.ts +83 -0
  19. package/dist/src/prolly.d.ts.map +1 -0
  20. package/dist/src/sha1.d.ts +9 -0
  21. package/dist/src/sha1.d.ts.map +1 -0
  22. package/dist/src/valet.d.ts +34 -0
  23. package/dist/src/valet.d.ts.map +1 -0
  24. package/dist/tsconfig.tsbuildinfo +1 -0
  25. package/package.json +33 -3
  26. package/src/blockstore.js +3 -2
  27. package/src/clock.js +4 -3
  28. package/src/crypto.js +1 -0
  29. package/src/db-index.js +10 -5
  30. package/src/fireproof.js +15 -10
  31. package/src/hydrator.js +3 -3
  32. package/src/index.js +6 -0
  33. package/src/listener.js +2 -1
  34. package/src/prolly.js +11 -24
  35. package/src/sha1.js +2 -1
  36. package/src/valet.js +7 -5
  37. package/hooks/use-fireproof.js +0 -135
  38. package/index.js +0 -6
  39. package/scripts/keygen.js +0 -3
  40. package/test/block.js +0 -65
  41. package/test/clock.test.js +0 -694
  42. package/test/db-index.test.js +0 -261
  43. package/test/fireproof.test.js +0 -493
  44. package/test/fulltext.test.js +0 -66
  45. package/test/helpers.js +0 -45
  46. package/test/hydrator.test.js +0 -81
  47. package/test/listener.test.js +0 -102
  48. package/test/prolly.test.js +0 -190
  49. package/test/proofs.test.js +0 -53
  50. package/test/reproduce-fixture-bug.test.js +0 -65
  51. package/test/valet.test.js +0 -59
package/src/valet.js CHANGED
@@ -1,3 +1,4 @@
1
+ // @ts-nocheck
1
2
  import { CarReader } from '@ipld/car'
2
3
  import { CID } from 'multiformats/cid'
3
4
  import { sha256 } from 'multiformats/hashes/sha2'
@@ -12,13 +13,13 @@ import { nocache as cache } from 'prolly-trees/cache'
12
13
  import { encrypt, decrypt } from './crypto.js'
13
14
  import { Buffer } from 'buffer'
14
15
  import * as codec from 'encrypted-block'
15
- import sha1sync from './sha1.js'
16
+ import { rawSha1 as sha1sync } from './sha1.js'
16
17
  const chunker = bf(3)
17
18
 
18
19
  const NO_ENCRYPT =
19
20
  typeof process !== 'undefined' ? process.env.NO_ENCRYPT : import.meta && import.meta.env.VITE_NO_ENCRYPT
20
21
 
21
- export default class Valet {
22
+ export class Valet {
22
23
  idb = null
23
24
  name = null
24
25
  uploadQueue = null
@@ -234,7 +235,8 @@ const blocksToEncryptedCarBlock = async (innerBlockStoreClockRootCid, blocks, ke
234
235
  key: encryptionKey,
235
236
  hasher: sha256,
236
237
  chunker,
237
- codec: dagcbor, // should be crypto?
238
+ cache,
239
+ // codec: dagcbor, // should be crypto?
238
240
  root: innerBlockStoreClockRootCid
239
241
  })) {
240
242
  encryptedBlocks.push(block)
@@ -262,8 +264,8 @@ const blocksFromEncryptedCarBlock = async (cid, get, keyMaterial) => {
262
264
  key: decryptionKey,
263
265
  chunker,
264
266
  hasher: sha256,
265
- cache,
266
- codec: dagcbor
267
+ cache
268
+ // codec: dagcbor
267
269
  })) {
268
270
  decryptedBlocks.push(block)
269
271
  cids.add(block.cid.toString())
@@ -1,135 +0,0 @@
1
- /* global localStorage */
2
- // @ts-ignore
3
- import { useEffect, useState, createContext } from 'react'
4
- import { Fireproof, Listener, Hydrator } from '../index'
5
-
6
- // export interface FireproofCtxValue {
7
- // addSubscriber: (label: String, fn: Function) => void
8
- // database: Fireproof
9
- // ready: boolean
10
- // persist: () => void
11
- // }
12
- export const FireproofCtx = createContext({
13
- addSubscriber: () => {},
14
- database: null,
15
- ready: false
16
- })
17
-
18
- const inboundSubscriberQueue = new Map()
19
-
20
- let startedSetup = false
21
- let database
22
- let listener
23
- const initializeDatabase = name => {
24
- if (database) return
25
- database = Fireproof.storage(name)
26
- listener = new Listener(database)
27
- }
28
-
29
- /**
30
- * @function useFireproof
31
- * React hook to initialize a Fireproof database, automatically saving and loading the clock.
32
- * You might need to `import { nodePolyfills } from 'vite-plugin-node-polyfills'` in your vite.config.ts
33
- * @param [defineDatabaseFn] Synchronous function that defines the database, run this before any async calls
34
- * @param [setupDatabaseFn] Asynchronous function that sets up the database, run this to load fixture data etc
35
- * @returns {FireproofCtxValue} { addSubscriber, database, ready }
36
- */
37
- export function useFireproof (
38
- defineDatabaseFn = () => {},
39
- setupDatabaseFn = async () => {},
40
- name
41
- ) {
42
- const [ready, setReady] = useState(false)
43
- initializeDatabase(name || 'useFireproof')
44
- const localStorageKey = 'fp.' + database.name
45
-
46
- const addSubscriber = (label, fn) => {
47
- inboundSubscriberQueue.set(label, fn)
48
- }
49
-
50
- const listenerCallback = async event => {
51
- localSet(localStorageKey, JSON.stringify(database))
52
- if (event._external) return
53
- for (const [, fn] of inboundSubscriberQueue) fn()
54
- }
55
-
56
- useEffect(() => {
57
- const doSetup = async () => {
58
- if (ready) return
59
- if (startedSetup) return
60
- startedSetup = true
61
- defineDatabaseFn(database) // define indexes before querying them
62
- console.log('Initializing database', database.name)
63
- const fp = localGet(localStorageKey) // todo use db.name
64
- if (fp) {
65
- try {
66
- const serialized = JSON.parse(fp)
67
- // console.log('serialized', JSON.stringify(serialized.indexes.map(c => c.clock)))
68
- console.log(`Loading previous database clock. (localStorage.removeItem('${localStorageKey}') to reset)`)
69
- await Hydrator.fromJSON(serialized, database)
70
- const changes = await database.changesSince()
71
- if (changes.rows.length < 2) {
72
- // console.log('Resetting database')
73
- throw new Error('Resetting database')
74
- }
75
- } catch (e) {
76
- console.error(`Error loading previous database clock. ${fp} Resetting.`, e)
77
- await Hydrator.zoom(database, [])
78
- await setupDatabaseFn(database)
79
- localSet(localStorageKey, JSON.stringify(database))
80
- }
81
- } else {
82
- await setupDatabaseFn(database)
83
- localSet(localStorageKey, JSON.stringify(database))
84
- }
85
- setReady(true)
86
- listener.on('*', listenerCallback)// hushed('*', listenerCallback, 250))
87
- }
88
- doSetup()
89
- }, [ready])
90
-
91
- return {
92
- addSubscriber,
93
- database,
94
- ready,
95
- persist: () => {
96
- localSet(localStorageKey, JSON.stringify(database))
97
- }
98
- }
99
- }
100
-
101
- // const husherMap = new Map()
102
- // const husher = (id, workFn, ms) => {
103
- // if (!husherMap.has(id)) {
104
- // const start = Date.now()
105
- // husherMap.set(
106
- // id,
107
- // workFn().finally(() => setTimeout(() => husherMap.delete(id), ms - (Date.now() - start)))
108
- // )
109
- // }
110
- // return husherMap.get(id)
111
- // }
112
- // const hushed =
113
- // (id, workFn, ms) =>
114
- // (...args) =>
115
- // husher(id, () => workFn(...args), ms)
116
-
117
- let storageSupported = false
118
- try {
119
- storageSupported = window.localStorage && true
120
- } catch (e) {}
121
- export function localGet (key) {
122
- if (storageSupported) {
123
- return localStorage && localStorage.getItem(key)
124
- }
125
- }
126
- function localSet (key, value) {
127
- if (storageSupported) {
128
- return localStorage && localStorage.setItem(key, value)
129
- }
130
- }
131
- // function localRemove(key) {
132
- // if (storageSupported) {
133
- // return localStorage && localStorage.removeItem(key)
134
- // }
135
- // }
package/index.js DELETED
@@ -1,6 +0,0 @@
1
- import Fireproof from './src/fireproof'
2
- import Index from './src/db-index'
3
- import Listener from './src/listener'
4
- import Hydrator from './src/hydrator'
5
-
6
- export { Fireproof, Index, Listener, Hydrator }
package/scripts/keygen.js DELETED
@@ -1,3 +0,0 @@
1
- import { randomBytes } from 'crypto'
2
-
3
- console.log(randomBytes(32).toString('hex'))
package/test/block.js DELETED
@@ -1,65 +0,0 @@
1
- import { parse } from 'multiformats/link'
2
-
3
- /**
4
- * @typedef {{ cid: import('../src/link').AnyLink, bytes: Uint8Array }} AnyBlock
5
- * @typedef {{ get: (link: import('../src/link').AnyLink) => Promise<AnyBlock | undefined> }} BlockFetcher
6
- */
7
-
8
- /** @implements {BlockFetcher} */
9
- export class MemoryBlockstore {
10
- /** @type {Map<string, Uint8Array>} */
11
- blocks = new Map()
12
-
13
- /**
14
- * @param {import('../src/link').AnyLink} cid
15
- * @returns {Promise<AnyBlock | undefined>}
16
- */
17
- async get (cid) {
18
- const bytes = this.blocks.get(cid.toString())
19
- if (!bytes) return
20
- return { cid, bytes }
21
- }
22
-
23
- /**
24
- * @param {import('../src/link').AnyLink} cid
25
- * @param {Uint8Array} bytes
26
- */
27
- async put (cid, bytes) {
28
- // console.log('put', cid)
29
- this.blocks.set(cid.toString(), bytes)
30
- }
31
-
32
- /**
33
- * @param {import('../src/link').AnyLink} cid
34
- * @param {Uint8Array} bytes
35
- */
36
- putSync (cid, bytes) {
37
- this.blocks.set(cid.toString(), bytes)
38
- }
39
-
40
- * entries () {
41
- for (const [str, bytes] of this.blocks) {
42
- yield { cid: parse(str), bytes }
43
- }
44
- }
45
- }
46
-
47
- export class MultiBlockFetcher {
48
- /** @type {BlockFetcher[]} */
49
- fetchers
50
-
51
- /** @param {BlockFetcher[]} fetchers */
52
- constructor (...fetchers) {
53
- this.fetchers = fetchers
54
- }
55
-
56
- /** @param {import('../src/link').AnyLink} link */
57
- async get (link) {
58
- for (const f of this.fetchers) {
59
- const v = await f.get(link)
60
- if (v) {
61
- return v
62
- }
63
- }
64
- }
65
- }