@fireproof/core 0.8.0 → 0.10.1-dev

Sign up to get free protection for your applications and to get access to all the features.
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
package/README.md CHANGED
@@ -1,188 +1,9 @@
1
- <p align="center" >
2
- <a href="https://fireproof.storage/">
3
- <img src="https://fireproof.storage/static/img/logo-animated-black.svg" alt="Fireproof logo" width="200">
4
- </a>
5
- </p>
6
- <h3 align="center">
7
- Cloudless database for React apps
8
- </h3>
1
+ # Fireproof
9
2
 
10
- <p align="center">
11
- <a href="https://github.com/fireproof-storage/fireproof/actions/workflows/test.yml">
12
- <img src="https://github.com/fireproof-storage/fireproof/actions/workflows/test.yml/badge.svg" alt="Test" style="max-width: 100%;">
13
- </a>
14
- <a href="https://standardjs.com" rel="nofollow">
15
- <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="JavaScript Style Guide" style="max-width: 100%;">
16
- </a>
17
- <a href="https://github.com/fireproof-storage/fireproof/blob/main/packages/react/README.md">
18
- <img src="https://shields.io/badge/react-black?logo=react&style=for-the-badge%22" alt="React" style="max-width: 100%;">
19
- </a>
20
- <a href="https://bundlephobia.com/package/@fireproof/core" rel="nofollow">
21
- <img src="https://deno.bundlejs.com/?q=@fireproof/core&treeshake=[*+as+fireproofCore]&badge" alt="Bundle Size" style="max-width: 100%;">
22
- </a>
23
- </p>
3
+ ## Build and Lint Process
24
4
 
25
- Fireproof uses immutable data and distributed protocols to offer a new kind of database that:
26
- - can be embedded in any page or app, with a flexible data ownership model
27
- - can be hosted on any cloud
28
- - uses cryptographically verifiable protocols (what plants crave)
5
+ The project uses `esbuild` and `esbuild-plugin-tsc` for compiling TypeScript to JavaScript, the results of which are bundled into different module formats for different environments. The `build.js` script in the `scripts` directory orchestrates this process. You can trigger the build process by running `npm run build` in your terminal.
29
6
 
30
- Fireproof is optimized to make [building React apps](https://github.com/fireproof-storage/fireproof/blob/main/packages/react/README.md) fast and fun, with reliable results. Suitable for mission-critical data workloads like [LLM orchestration](https://fireproof.storage/posts/why-proofs-matter-for-ai/), supply-chain provenance, and field use of auditable data, [ Fireproof is also great](https://fireproof.storage/posts/great-opportunites-to-use-fireproof/) for social media, collaborative world-building, and rapidly implementing executive decision support tools that can stand up to blockchain levels of scrutiny.
7
+ For code quality assurance, we utilize ESLint with `@typescript-eslint/parser` and `@typescript-eslint/eslint-plugin`. ESLint checks both the JavaScript and TypeScript code in the project. The configuration for ESLint is stored in the `.eslintrc.js` file. For the test files located in the `test` directory, there are additional linting configurations to accommodate Mocha-specific code.
31
8
 
32
- With Fireproof, you **build first** and connect it to your cloud of choice when you are ready, so there's nothing holding you back from adding it to your existing apps, or [writing something new.](https://hackernoon.com/get-chatgpt-to-focus-on-coding-on-the-right-apis-with-gptdoc-strings)
33
-
34
-
35
- ## React Quick Start
36
- *One-line preview:* in the context of a React component render, `useLiveQuery` will automatically refresh (even on changes by remote collaborators):
37
-
38
- ```js
39
- const completedTodos = useLiveQuery((doc) => doc.completed, { key: true })
40
- ```
41
- This is the README for the core database technology. [The `useFireproof` hook documentation has features and a getting started guide](https://github.com/fireproof-storage/fireproof/blob/main/packages/react/README.md), like `useDocument` and database setup helpers. Don't forget to star 🌟 this repo on the way over.
42
-
43
- ## AI Assistant Quick Start
44
-
45
- Because Fireproof is designed around the mantra of build-first, it's ideal for AI-assisted app development as you can get an app up and running before even considering the cloud. If you are using GPT-3.5, GPT-4, or Bard, you can [easily enable the AI to write React apps using Fireproof](https://hackernoon.com/get-chatgpt-to-focus-on-coding-on-the-right-apis-with-gptdoc-strings). Paste this line of code into your prompt, along with a detailed description of the app to build:
46
-
47
- ```
48
- Fireproof/React/Usage: import { useFireproof } from '@fireproof/react'; function App() { const { useLiveQuery, useDocument, database } = useFireproof(); const result = useLiveQuery(doc => doc.word, { limit: 10 }); const [{ count }, saveCountDocument] = useDocument({_id: 'count', count: 0}); return (<><p>{count} changes</p><input type='text' onChange={() => saveCountDocument({count: count + 1})} onSubmit={e => database.put({word: e.target.value})} /><ul>{result.map(row => (<li key={row.id}>{row.key}</li>))}</ul></>)}
49
- ```
50
-
51
- Here are some examples that worked for us:
52
-
53
- * Create a React app using Fireproof for tracking party invites. It should have a text input that creates a new document with the guest name, and an Index that lists all guests in a &lt;ul&gt;. ([Running copy here.](https://codepen.io/jchrisa/pen/zYmogWO))
54
- * Build a photo grid app with drag-and-drop ordering that references photos by URL. Use tailwind and render all photos as squares. Keep grid arrangement in Fireproof with one document for each gallery, that is 4-16 photos arranged into a layout.
55
- * Build a React app that allows users to create a list of their favorite movies, and then share that list with friends. Use Fireproof to store the list of movies, and to store the list of friends. Use MagicLink to authenticate users. Use Tailwind to make it look good.
56
-
57
- You can enhance the AI's understanding by adding the core APIs. Use this if you aren't using React, or you are adding additional features to your app and you need to go deeper than the React hooks.
58
-
59
- ```
60
- Fireproof/API/Usage: import { Fireproof, Index } from '@fireproof/core'; const db = fireproof.storage('app-db-name'); const ok = await db.put({ any: 'json' }); const doc = await db.get(ok.id); await db.del(doc._id); const all = await db.allDocuments(); all.rows.map(({key, value}) => value); useEffect(()=> db.subscribe(updateReactStateFn), []); const index = new Index(db, (doc, map) => map(doc.any, {custom: Object.keys(doc)})); const result = await index.query({range : ['a', 'z']}); result.rows.map(({ key }) => key);
61
- ```
62
-
63
- Please share your successes with us here or on [Twitter.](https://twitter.com/FireproofStorge)
64
-
65
- # Database Features
66
-
67
- The core features of the database are available on any platform in a compact JavaScript package and a foundational cloud storage service.
68
-
69
- * **Local** - always encrypted, data automatically collocates with your users, for faster and more reliable apps.
70
- * **Immutable** - [cutting-edge git-like data structures](https://fireproof.storage/posts/from-mlops-to-point-of-sale:-merkle-proofs-and-data-locality/) allow Fireproof to combine cryptographic verification with append-only storage, automatically converging on a verified state.
71
- * **Distributed** - immutable data can be stored on the Fireproof service, your cloud, and the distributed IPFS network, so it is always available.
72
- * **Realtime** - use the Fireproof service or APIs like WebRTC, libp2p, PartyKit, or SocketSupply to push changes to connected peers. React hook APIs like `useLiveQuery` are designed for automatic UI refresh.
73
- * **Verifiable** - [cryptographic proofs make results verifiable](https://fireproof.storage/posts/why-proofs-matter-for-ai/), sync fast, and storage cheap.
74
- * **[Cloudless](https://www.oreilly.com/radar/the-paradigm-shift-to-cloudless-computing/)** - data can be hosted on any cloud, on the IPFS network, or both. UCAN integration allows for flexible data ownership models.
75
-
76
-
77
- Learn more about the [architecture](https://fireproof.storage/documentation/how-the-database-engine-works/) behind Fireproof, or see [examples on CodePen](https://codepen.io/jchrisa/pen/GRYJJEM).
78
-
79
- ## API Usage
80
-
81
- ### Encrypted Documents
82
-
83
- A simple put, get and delete interface for keeping track of all your JSON documents. Once your data is in Fireproof you can access it from any app or website. Fireproof document store uses MVCC versioning and Merkle clocks so you can always recover the version you are looking for.
84
-
85
- ```js
86
- const { id, ref } = await database.put({
87
- _id: 'three-thousand'
88
- name: 'André',
89
- age: 47
90
- });
91
-
92
- // mvcc is optional
93
- const doc = await database.get('three-thousand', { mvcc: true })
94
- // {
95
- // _id : 'three-thousand'
96
- // _clock : CID(bafy84...agfw7)
97
- // name : 'André',
98
- // age : 47
99
- // }
100
- ```
101
-
102
- As you can see in the return value above, the `_clock` allows you to query a stable snapshot of that version of the database. Fireproof uses immutable data structures under the hood, so you can always rollback to old data. Files can be embedded anywhere in your document using IPFS links like `{"/": "bafybeih3e3zdiehbqfpxzpppxrb6kaaw4xkbqzyr2f5pwr5refq2te2ape"}`, with API sugar coming soon.
103
-
104
- ### Live Query
105
-
106
- Fireproof provides a live query interface that allows you to subscribe to changes in your data. This means that your UI will automatically update whenever there is a change to your data. See the [useFireproof React hooks documentation](https://github.com/fireproof-storage/fireproof/blob/main/packages/react/README.md) for the easiest way to use this feature.
107
-
108
- Fireproof indexes are defined by custom JavaScript functions that you write, allowing you to easily index and search your data in the way that works best for your application. Easily handle data variety and schema drift by normalizing any data to the desired index. The index function defines the sort order. You can use the index to query for a range of values or to find exact matches. This baseline functionality is all you need to build many kinds of complex queries.
109
-
110
- ```js
111
- const index = new Index(database, "byAge", (doc) => doc.age)
112
- const { rows, proof } = await index.query({ range: [40, 52] })
113
- ```
114
-
115
- You can ignore the proof or use it to optimize hydration of your client side components. The `rows` are the results of the query. You can use `database.subscribe(myAppQueryFn)` to get notified of changes and re-issue your query. The React [useLiveQuery](https://fireproof.storage/documentation/usefireproof-hook-for-react/) hook does this for you automatically.
116
-
117
- If you need more control over the results, you can use the optional second argument to your map function to specify both keys and values for the index:
118
-
119
- ```js
120
- const index = new Index(database, "namesByAge", function (doc, map) {
121
- map(doc.age, doc.name)
122
- })
123
- const { rows, ref } = await index.query({ range: [40, 52] })
124
- // [ { key: 42, value: 'alice', id: 'a1s3b32a-3c3a-4b5e-9c1c-8c5c0c5c0c5c' },
125
- // { key: 47, value: 'André', id: 'three-thousand' } ]
126
- ```
127
-
128
- The same mechanism that powers the built-in indexes can all be used to connect secondary [vector indexers](https://github.com/tantaraio/voy) or fulltext indexes to Fireproof. [Follow this tutorial to connect a secondary index](https://fireproof.storage/documentation/external-indexers/).
129
-
130
- ### Realtime Updates
131
-
132
- Subscribe to query changes in your application, so your UI updates automatically. Use the supplied React hooks, or simple function calls to be notified of relevant changes.
133
-
134
- ```js
135
- const unsubscribe = database.subscribe(changes) => {
136
- changes.forEach(change => {
137
- console.log(change)
138
- })
139
- })
140
- ```
141
-
142
- Return the `unsubscribe` function from `useEffect` and React will handle it for you. (In the code below, we use the arrow function's implicit return to connect the unsubscribe function to the `useEffect` hook. This prevents extra subscriptions from building up on each render.)
143
-
144
- ```js
145
- useEffect(() => database.subscribe((changes) =>
146
- changes.forEach(change => console.log(change))), [])
147
- ```
148
-
149
- ### Cryptographic Proofs
150
-
151
- Fireproof's Merkle clocks and hash trees are immutable and self-validating, and all query results are offline-capable data slices. Fireproof makes cryptographic proofs available for all of its operations, accelerating replication and making trustless index sharing possible. If you are making a "DocuSign for _____", [proofs make Fireproof the ideal verifiable document database](https://fireproof.storage/posts/from-mlops-to-point-of-sale:-merkle-proofs-and-data-locality/) for smart contracts and other applications where unique, verifiable, and trustworthy data is required. [Proof chains provide performance benefits as well](https://purrfect-tracker-45c.notion.site/Data-Routing-23c37b269b4c4c3dacb60d0077113bcb), by allowing recipients to skip costly I/O operations and instead cryptographically verify that changes contain all of the required context.
152
-
153
- ### Automatic Replication
154
-
155
- Documents changes are persisted to [Filecoin](https://filecoin.io) via [web3.storage](https://web3.storage), and made available over IPFS and on a global content delivery network. All you need to do to sync state is send a link to the latest database head, and Fireproof will take care of the rest.
156
-
157
- ### Peer-to-peer Sync
158
-
159
- Application instances can be connected using WebRTC or any other stream API library, like [Socket Supply](https://socketsupply.co), [libp2p](https://libp2p.io), or [PartyKit](https://partykit.io). The [first sync demo uses pure WebRTC with no signaling server](https://game.fireproof.storage), which limits its usability. There are demos with other transports coming soon.
160
-
161
- ### Self-sovereign Identity
162
-
163
- Fireproof is so easy to integrate with any site or app because you can get started right away, and set up an account later. By default users write to their own database copy, so you can get pretty far before you even have to think about API keys. [Authorization is via non-extractable keypair](https://ucan.xyz), like TouchID / FaceID.
164
-
165
- ## Thanks 🙏
166
-
167
- Fireproof is a synthesis of work done by people in the web community over the years. I couldn't even begin to name all the folks who made pivotal contributions. Without npm, React, and VS Code all this would have taken so much longer. Thanks to everyone who supported me getting into database development via Apache CouchDB, one of the original document databases. The distinguishing work on immutable data-structures comes from the years of consideration [IPFS](https://ipfs.tech), [IPLD](https://ipld.io), and the [Filecoin APIs](https://docs.filecoin.io) have enjoyed.
168
-
169
- Thanks to Alan Shaw and Mikeal Rogers without whom this project would have never got started. The core Merkle hash-tree clock is based on [Alan's Pail](https://github.com/alanshaw/pail), and you can see the repository history goes all the way back to work begun as a branch of that repo. Mikeal wrote [the prolly trees implementation](https://github.com/mikeal/prolly-trees).
170
-
171
- # Contributing
172
-
173
- To contribute please follow these steps for local setup and installation of the project
174
-
175
- 1. Click on the "Fork" button in the top-right corner of the repository's page. This will create a copy of the repository in your account.
176
- 2. Clone the forked repository to your local machine using Git.
177
- 3. Now cd to the target directory, or load the directory in your IDE, and open up a terminal.
178
- 4. Write the command `pnpm install`. This will install all the dependencies that are listed in the `package.json` file.
179
- 5. Now change the directory to packages/fireproof using the command `cd packages/fireproof`.
180
- 6. See the `package.json` file to work with all the listed commands and try them out. You can also test your application locally using `npm test`.
181
- 7. Also change directory to `examples/todomvc` and run the command `npm run dev` to load up a simple application to understand the use of Fireproof as a real-time database.
182
- 8. Keep contributing :) See [projects](https://github.com/fireproof-storage/fireproof/projects?query=is%3Aopen) and [issues](https://github.com/fireproof-storage/fireproof/issues) for ideas where to get started.
183
-
184
- Feel free to join in. All welcome. [Open an issue](https://github.com/jchris/fireproof/issues)!
185
-
186
- # License
187
-
188
- Dual-licensed under [MIT or Apache 2.0](https://github.com/jchris/fireproof/blob/main/LICENSE.md)
9
+ Tests in this project are written with Mocha and can be run in both Node.js and a browser environment through the use of `polendina`. Test files are located in the `test` directory and are recognizable by their `.test.js` extension. You can run the tests by executing the command `npm test`, which also triggers the build process and checks for any linting errors.