Compare commits

...

90 Commits

Author SHA1 Message Date
Anselm
27779ac792 Publish
- jazz-example-pets@0.0.16
 - jazz-example-todo@0.0.41
 - jazz-example-twit@0.0.3
 - cojson@0.4.1
 - cojson-simple-sync@0.4.1
 - cojson-storage-indexeddb@0.4.1
 - cojson-storage-sqlite@0.4.1
 - jazz-browser@0.4.1
 - jazz-browser-auth-local@0.4.1
 - jazz-browser-media-images@0.4.1
 - jazz-react@0.4.1
 - jazz-react-auth-local@0.4.1
2023-09-28 11:25:36 +01:00
Anselm
3f1bfa4629 Improve twit example 2023-09-28 11:25:09 +01:00
Anselm
15a693c3ed Simplify QueriedCoStream 2023-09-28 11:23:23 +01:00
Anselm
b1d620e145 Update docs 2023-09-28 11:23:06 +01:00
Anselm
478fbd0aa9 Bigger inputs on mobile 2023-09-27 22:21:19 +01:00
Anselm
ee906b7351 Add QR code to own profile 2023-09-27 22:13:55 +01:00
Anselm
dd15f21ccb Fix follow button 2023-09-27 21:51:20 +01:00
Anselm
d7cd5fda7c Actually deploy twit example 2023-09-27 21:43:07 +01:00
Anselm
174300b00f Deploy twit example 2023-09-27 21:39:30 +01:00
Anselm
b2c8d8c855 Publish
- jazz-example-pets@0.0.15
 - jazz-example-todo@0.0.40
 - jazz-example-twit@0.0.2
 - cojson@0.4.0
 - cojson-simple-sync@0.4.0
 - cojson-storage-indexeddb@0.4.0
 - cojson-storage-sqlite@0.4.0
 - jazz-browser@0.4.0
 - jazz-browser-auth-local@0.4.0
 - jazz-browser-media-images@0.4.0
 - jazz-react@0.4.0
 - jazz-react-auth-local@0.4.0
2023-09-27 21:37:56 +01:00
Anselm
2bad2b6bfe Update docs 2023-09-27 21:37:22 +01:00
Anselm
880d0ff855 Fix last lint issues 2023-09-27 21:37:04 +01:00
Anselm
e66cbee6cd Implement Twitter example 2023-09-27 21:27:49 +01:00
Anselm
03e470721e AAAAAAAAAA 2023-09-27 15:08:09 +01:00
Anselm
ecf73bcfa7 Basic account initialization, fixes #103 2023-09-26 18:07:14 +01:00
Anselm
2c3a500286 Add root to queried group 2023-09-26 17:47:31 +01:00
Anselm
8b83061cf4 Update docs 2023-09-26 17:42:48 +01:00
Anselm
e75c3207d6 Make Groups and Accounts behave like proper CoValues, fixes #101 2023-09-26 17:42:28 +01:00
Anselm
41d4b5ba0b Ability to add/remove the public as readers & writers #99 2023-09-26 11:19:39 +01:00
Anselm
21fa1b168b First sketch of twit example 2023-09-26 09:50:08 +01:00
Anselm
91e5e7f2ab v0.3.7 2023-09-24 20:25:23 +01:00
Anselm
e3f7e2f1bd Actually use delayOnerror 2023-09-24 20:24:58 +01:00
Anselm
084cf80c60 v0.3.6 2023-09-24 20:16:25 +01:00
Anselm
632e3bbb08 Add option for delay on error when handling peer messages 2023-09-24 20:15:10 +01:00
Anselm
17d17833b2 Publish
- jazz-example-pets@0.0.14
 - jazz-example-todo@0.0.39
 - cojson@0.3.5
 - cojson-simple-sync@0.3.7
 - cojson-storage-indexeddb@0.3.5
 - cojson-storage-sqlite@0.3.7
 - jazz-browser@0.3.5
 - jazz-browser-auth-local@0.3.5
 - jazz-browser-media-images@0.3.5
 - jazz-react@0.3.5
 - jazz-react-auth-local@0.3.5
 - jazz-react-media-images@0.3.5
2023-09-22 15:18:21 +01:00
Anselm
8e22bd9c1e Lint fix 2023-09-22 15:17:44 +01:00
Anselm
98213743f3 deploy bump 2023-09-22 15:15:09 +01:00
Anselm
bb855ed83d Publish
- jazz-example-pets@0.0.13
 - jazz-example-todo@0.0.38
 - cojson@0.3.4
 - cojson-simple-sync@0.3.6
 - cojson-storage-indexeddb@0.3.4
 - cojson-storage-sqlite@0.3.6
 - jazz-browser@0.3.4
 - jazz-browser-auth-local@0.3.4
 - jazz-browser-media-images@0.3.4
 - jazz-react@0.3.4
 - jazz-react-auth-local@0.3.4
 - jazz-react-media-images@0.3.4
2023-09-22 14:33:25 +01:00
Anselm
a8ef49e228 Small lint fixes 2023-09-22 14:32:41 +01:00
Anselm
e0ad32dbd2 Implement exponential falloff, fixes #69 2023-09-22 14:30:55 +01:00
Anselm
62bf769cad Publish
- cojson-simple-sync@0.3.5
 - cojson-storage-sqlite@0.3.5
2023-09-22 10:36:17 +01:00
Anselm
7488ff25b2 Missed one bit of JSON parsing to make more robust 2023-09-22 10:36:02 +01:00
Anselm
b69c9da983 Publish
- cojson-simple-sync@0.3.4
 - cojson-storage-sqlite@0.3.4
2023-09-22 10:25:25 +01:00
Anselm
d30fdef8aa More JSON.parse resiliency in cojson-storage-sqlite 2023-09-22 10:25:08 +01:00
Anselm
9c5a6b9833 Publish
- jazz-example-pets@0.0.12
 - jazz-example-todo@0.0.37
 - cojson@0.3.3
 - cojson-simple-sync@0.3.3
 - cojson-storage-indexeddb@0.3.3
 - cojson-storage-sqlite@0.3.3
 - jazz-browser@0.3.3
 - jazz-browser-auth-local@0.3.3
 - jazz-browser-media-images@0.3.3
 - jazz-react@0.3.3
 - jazz-react-auth-local@0.3.3
 - jazz-react-media-images@0.3.3
2023-09-22 10:09:04 +01:00
Anselm
d300d265c4 manually update cojson 2023-09-22 10:07:55 +01:00
Anselm
1d72ce587f Update version 2023-09-22 09:53:25 +01:00
Anselm
3fdb41dcb9 More resilience against invalid JSON 2023-09-22 09:51:07 +01:00
Anselm
f20de2f04a v0.3.1 2023-09-22 09:36:32 +01:00
Anselm
31b31f111b Shorter logs on failed transactions 2023-09-22 09:34:54 +01:00
Anselm Eickhoff
2ae9fb9778 Fix example comment 2023-09-21 18:00:28 +01:00
Anselm Eickhoff
cd0da0f6bf Merge pull request #94 from gardencmp/ergonomic-covalues
Implement queries
2023-09-21 17:31:31 +01:00
Anselm
cd9bfbb9fa Publish
- jazz-example-pets@0.0.11
 - jazz-example-todo@0.0.36
 - cojson@0.3.0
 - cojson-simple-sync@0.3.0
 - cojson-storage-indexeddb@0.3.0
 - cojson-storage-sqlite@0.3.0
 - jazz-browser@0.3.0
 - jazz-browser-auth-local@0.3.0
 - jazz-browser-media-images@0.3.0
 - jazz-react@0.3.0
 - jazz-react-auth-local@0.3.0
 - jazz-react-media-images@0.3.0
2023-09-21 17:29:23 +01:00
Anselm
ed0428bf97 Pre-release fixes 2023-09-21 17:12:10 +01:00
Anselm
c038a02051 Publish
- jazz-example-pets@0.0.10
 - jazz-example-todo@0.0.35
 - cojson@0.3.0-alpha.0
 - cojson-simple-sync@0.3.0-alpha.0
 - cojson-storage-indexeddb@0.3.0-alpha.0
 - cojson-storage-sqlite@0.3.0-alpha.0
 - jazz-browser@0.3.0-alpha.0
 - jazz-browser-auth-local@0.3.0-alpha.0
 - jazz-browser-media-images@0.3.0-alpha.0
 - jazz-react@0.3.0-alpha.0
 - jazz-react-auth-local@0.3.0-alpha.0
 - jazz-react-media-images@0.3.0-alpha.0
2023-09-21 17:07:06 +01:00
Anselm
31abcfeef4 Walkthrough and doc improvements 2023-09-21 17:02:34 +01:00
Anselm
5f32d9ccf5 Support external routers & more doc improvements 2023-09-21 16:35:13 +01:00
Anselm
0510600104 Lots of doc improvements, cleaner Queried's 2023-09-20 17:48:07 +01:00
Anselm
7f30fbf3c5 move stuff to "co" property in queries 2023-09-20 11:52:57 +01:00
Anselm
3d56260ca4 Lots more consistency and API improvements 2023-09-19 13:17:31 +01:00
Anselm
1137775da9 Publish
- jazz-example-pets@0.0.9
 - jazz-example-todo@0.0.34
 - cojson@0.2.3
 - cojson-simple-sync@0.2.6
 - cojson-storage-sqlite@0.2.6
 - jazz-browser@0.2.5
 - jazz-browser-auth-local@0.2.5
 - jazz-browser-media-images@0.2.5
 - jazz-react@0.2.5
 - jazz-react-auth-local@0.2.5
 - jazz-react-media-images@0.2.5
 - jazz-storage-indexeddb@0.2.5
2023-09-15 16:40:47 +01:00
Anselm
3951fdc938 Implement queries & use in examples 2023-09-15 16:36:48 +01:00
Anselm
5779e357dd Allow CoValues directly where ids would be expected 2023-09-13 17:48:04 +01:00
Anselm
2842d80f26 Improve docs for new packages 2023-09-12 16:55:58 +01:00
Anselm Eickhoff
96387d8023 Merge pull request #89 from gardencmp:stream-txs
Stream transactions
2023-09-12 16:26:09 +01:00
Anselm
6720c19233 Publish
- jazz-example-pets@0.0.8
 - jazz-example-todo@0.0.33
 - cojson-simple-sync@0.2.5
 - cojson-storage-sqlite@0.2.5
 - jazz-browser@0.2.4
 - jazz-browser-auth-local@0.2.4
 - jazz-browser-media-images@0.2.4
 - jazz-react@0.2.4
 - jazz-react-auth-local@0.2.4
 - jazz-react-media-images@0.2.4
 - jazz-storage-indexeddb@0.2.4
2023-09-12 16:17:09 +01:00
Anselm
ef732b4700 Implement saving signatures and streaming txs for SQLite 2023-09-12 16:16:40 +01:00
Anselm
ee7e3ee5a7 Publish
- jazz-example-pets@0.0.7
 - jazz-example-todo@0.0.32
 - cojson@0.2.2
 - cojson-simple-sync@0.2.4
 - cojson-storage-sqlite@0.2.4
 - jazz-browser@0.2.3
 - jazz-browser-auth-local@0.2.3
 - jazz-browser-media-images@0.2.3
 - jazz-react@0.2.3
 - jazz-react-auth-local@0.2.3
 - jazz-react-media-images@0.2.3
 - jazz-storage-indexeddb@0.2.3
2023-09-12 15:26:43 +01:00
Anselm
ceeed88fa5 Less verbose error output 2023-09-12 15:26:22 +01:00
Anselm
79353a1d97 Publish
- cojson-simple-sync@0.2.3
 - cojson-storage-sqlite@0.2.3
2023-09-12 15:22:01 +01:00
Anselm
7fdc42c62f Fix migration 2023-09-12 15:21:45 +01:00
Anselm
3a2e854a88 Publish
- cojson-simple-sync@0.2.2
 - cojson-storage-sqlite@0.2.2
2023-09-12 15:19:12 +01:00
Anselm
661a2d023a Fixes #90 for SQLite 2023-09-12 15:18:53 +01:00
Anselm
6ef5b6b2ab Publish
- jazz-example-pets@0.0.6
 - jazz-example-todo@0.0.31
 - jazz-browser@0.2.2
 - jazz-browser-auth-local@0.2.2
 - jazz-browser-media-images@0.2.2
 - jazz-react@0.2.2
 - jazz-react-auth-local@0.2.2
 - jazz-react-media-images@0.2.2
 - jazz-storage-indexeddb@0.2.2
2023-09-12 14:56:31 +01:00
Anselm
1384ebed84 Fix migration 2023-09-12 14:55:57 +01:00
Anselm
17e53f9998 Publish
- jazz-example-pets@0.0.5
 - jazz-example-todo@0.0.30
 - cojson@0.2.1
 - cojson-simple-sync@0.2.1
 - cojson-storage-sqlite@0.2.1
 - jazz-browser@0.2.1
 - jazz-browser-auth-local@0.2.1
 - jazz-browser-media-images@0.2.1
 - jazz-react@0.2.1
 - jazz-react-auth-local@0.2.1
 - jazz-react-media-images@0.2.1
 - jazz-storage-indexeddb@0.2.1
2023-09-12 14:47:50 +01:00
Anselm
cfb1f39efe update docs 2023-09-12 14:47:17 +01:00
Anselm
2234276dcf Implement extra signatures & fix #90 for IndexedDB 2023-09-12 14:42:47 +01:00
Anselm
bb0a6a0600 yield microtask between incoming messages 2023-09-12 11:22:44 +01:00
Anselm
0a6eb0c10a Lots of fixes around streaming 2023-09-12 11:13:19 +01:00
Anselm
88b67d89e0 First implementation of streaming transactions, also fixes #80 2023-09-11 19:29:52 +01:00
Anselm Eickhoff
1a65d826b2 Update pets README.md 2023-09-11 17:24:01 +01:00
Anselm Eickhoff
6c65ec2b46 Merge pull request #81 from gardencmp/publish-pet-example
Publish pet example
2023-09-11 17:21:16 +01:00
Anselm
5b578a832d Fix job name and missing amtrix 2023-09-11 17:13:16 +01:00
Anselm
042afc52d7 Fix interpolation 2023-09-11 17:10:12 +01:00
Anselm
1b83493964 Use matrix and add pets example 2023-09-11 17:09:14 +01:00
Anselm
3b50da1a74 Remove redundant yarn build step 2023-09-11 17:04:42 +01:00
Anselm
8e0fc74d9f Switch to buildx 2023-09-11 17:03:18 +01:00
Anselm Eickhoff
e28326f32c Merge pull request #79 from gardencmp/anselm-gar-155
Make payload of trusting transactions JSON string instead of immediately-parsed JSON
2023-09-11 16:32:30 +01:00
Anselm
d7e8b0b9da Publish
- jazz-example-pets@0.0.4
 - jazz-example-todo@0.0.29
 - cojson@0.2.0
 - cojson-simple-sync@0.2.0
 - cojson-storage-sqlite@0.2.0
 - jazz-browser@0.2.0
 - jazz-browser-auth-local@0.2.0
 - jazz-browser-media-images@0.2.0
 - jazz-react@0.2.0
 - jazz-react-auth-local@0.2.0
 - jazz-react-media-images@0.2.0
 - jazz-storage-indexeddb@0.2.0
2023-09-11 16:19:44 +01:00
Anselm
c46a1f6b0a Update docs 2023-09-11 16:18:39 +01:00
Anselm
7947918278 lint pet example 2023-09-11 16:11:26 +01:00
Anselm
50c36e7255 Make tx.changes stringified 2023-09-11 16:11:17 +01:00
Anselm
c39a7ed1b7 Implement jazz-browser-media-images 2023-09-11 11:44:55 +01:00
Anselm
83762dbb0f Fix getLastItemsPerAccount 2023-09-10 15:36:41 +01:00
Anselm
7c82e12508 Fix filenames in pets example 2023-09-10 15:20:12 +01:00
Anselm
6db149be36 Complete most of the pets example 2023-09-10 15:15:23 +01:00
Anselm
909a101f99 Publish
- jazz-example-pets@0.0.3
 - jazz-example-todo@0.0.28
 - cojson@0.1.12
 - cojson-simple-sync@0.1.13
 - cojson-storage-sqlite@0.1.10
 - jazz-browser@0.1.12
 - jazz-browser-auth-local@0.1.12
 - jazz-react@0.1.14
 - jazz-react-auth-local@0.1.14
 - jazz-storage-indexeddb@0.1.12
2023-09-08 17:29:07 +01:00
Anselm
df0b6fe138 Update docs 2023-09-08 17:28:53 +01:00
Anselm
0543756016 More optimizations and first support for streaming hashing 2023-09-08 17:28:33 +01:00
144 changed files with 19982 additions and 5365 deletions

View File

@@ -7,8 +7,11 @@ on:
branches: [ "main" ]
jobs:
build-and-deploy:
build:
runs-on: ubuntu-latest
strategy:
matrix:
example: ["todo", "pets", "twit"]
steps:
- uses: actions/checkout@v3
@@ -17,40 +20,50 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
node-version: 16
cache: 'yarn'
cache-dependency-path: yarn.lock
- name: Nuke Workspace
run: |
rm package.json yarn.lock;
- name: Yarn Build
run: |
yarn install --frozen-lockfile;
yarn build;
working-directory: ./examples/todo
- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true
with:
key: docker-layer-caching-${{ github.workflow }}-{hash}
restore-keys: |
docker-layer-caching-${{ github.workflow }}-
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
uses: docker/login-action@v2
with:
registry: ghcr.io
username: gardencmp
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker Build & Push
- name: Nuke Workspace
run: |
export DOCKER_TAG=ghcr.io/gardencmp/jazz-example-todo:${{github.head_ref || github.ref_name}}-${{github.sha}}-$(date +%s) ;
docker build . --file Dockerfile --tag $DOCKER_TAG;
docker push $DOCKER_TAG;
echo "DOCKER_TAG=$DOCKER_TAG" >> $GITHUB_ENV
working-directory: ./examples/todo
rm package.json yarn.lock;
- name: Yarn Build
run: |
yarn install --frozen-lockfile;
yarn build;
working-directory: ./examples/${{ matrix.example }}
- name: Docker Build & Push
uses: docker/build-push-action@v4
with:
context: ./examples/${{ matrix.example }}
push: true
tags: ghcr.io/gardencmp/${{github.event.repository.name}}-example-${{ matrix.example }}:${{github.head_ref || github.ref_name}}-${{github.sha}}-${{github.run_number}}-${{github.run_attempt}}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
runs-on: ubuntu-latest
needs: build
strategy:
matrix:
example: ["todo", "pets", "twit"]
steps:
- uses: actions/checkout@v3
with:
submodules: true
- uses: gacts/install-nomad@v1
- name: Tailscale
uses: tailscale/github-action@v1
@@ -69,9 +82,9 @@ jobs:
export DOCKER_USER=gardencmp;
export DOCKER_PASSWORD=${{ secrets.DOCKER_PULL_PAT }};
export DOCKER_TAG=${{ env.DOCKER_TAG }};
export DOCKER_TAG=ghcr.io/gardencmp/${{github.event.repository.name}}-example-${{ matrix.example }}:${{github.head_ref || github.ref_name}}-${{github.sha}}-${{github.run_number}}-${{github.run_attempt}};
envsubst '${DOCKER_USER} ${DOCKER_PASSWORD} ${DOCKER_TAG} ${BRANCH_SUFFIX} ${BRANCH_SUBDOMAIN}' < job-template.nomad > job-instance.nomad;
cat job-instance.nomad;
NOMAD_ADDR='http://control1v2-london:4646' nomad job run job-instance.nomad;
working-directory: ./examples/todo
working-directory: ./examples/${{ matrix.example }}

10903
DOCS.md

File diff suppressed because it is too large Load Diff

126
README.md
View File

@@ -1,82 +1,116 @@
# Jazz - instant sync
Homepage: [jazz.tools](https://jazz.tools) &mdash; [Discord](https://discord.gg/utDMjHYg42)
<sub>Homepage: [jazz.tools](https://jazz.tools) &mdash; Docs: [DOCS.md](./DOCS.md) &mdash; Community & support: [Discord](https://discord.gg/utDMjHYg42) &mdash; Updates: [Twitter](https://twitter.com/jazz_tools) & [Email](https://gcmp.io/news)</sub>
Jazz is an open-source toolkit for *secure telepathic data.*
**Jazz is an open-source toolkit for building apps with *secure sync.***
- Ship faster & simplify your frontend and backend
- Get cross-device sync, real-time collaboration & offline support for free
Quickly build and ship apps with:
[Jazz Global Mesh](https://jazz.tools/mesh) is serverless sync & storage for Jazz apps. (currently free!)
- **Cross-device sync**
- **Collaborative features** (incl. real-time multiplayer)
- **Instantly reacting UIs**
- Local-first storage & offline support
- File upload and real-time media streaming
# What is *secure sync*?
**Sync** means that, *instead of making API requests*, you:
- **Read and write data as if it was local** &mdash; from anywhere in your app.
- **Always have data synced to wherever it's needed, instantly:** to other devices of the same user, to other users, to your backend, to your local machine for debugging, etc.
**Secure** means that, *instead of relying on your API or DB for access control*, you:
- **Set fine-grained, role-based permissions in `Group`s** that are **synced along with your data**.
- **Permissions *verifiably enforced* everywhere,** using encryption & signatures under the hood.
- **Change roles dynamically** for evolving teams, expiring invite links and more.
# What's special about Jazz?
Compared to other libraries and frameworks for local-first, sync-based or real-time apps, these are some of the things that make Jazz unique:
- **Jazz is a *batteries-included,* vertically integrated toolkit,** offering everything you need to build an app, including auth, permissions, data model, sync, conflict resolution, blob storage, file uploads, real-time media streaming and more.
- **Jazz has a *small API surface* of only a few abstractions to learn,** which combine in powerful ways to implement a broad set of features.
- **Jazz *granularly* loads and caches *only the data that is needed*,** combining *local-first* instant UI reactivity and offline support with the on-demand data efficiency of conventional APIs
- **Jazz supports end-to-end encryption, but doesn't require it,** allowing you to either manage your user's secret keys for them (based on existing auth flows) or letting your users
- **Jazz is based on CoJSON, a soon-to-be *open standard,*** which means that there will be a whole ecosystem of compatible libraries and frameworks in a variety of environments &mdash; and it will be easy to achieve (secure) interop between Jazz/CoJSON-based apps and services.
# Jazz Global Mesh
Jazz is open source and you can run your own sync & storage server, but to really provide you with everything you need, we're also running
**[Jazz Global Mesh](https://jazz.tools/mesh)**, a globally distributed mesh of servers optimized for:
- **Ultra-low-latency sync** (with geo-aware edge caching and optimal routing)
- **Low-cost, reliable storage**
**Jazz Global Mesh is free for small volumes of data** and it's the **default syncing peer,** so you can **start building multi-user Jazz apps with persistent data in minutes,** using only frontend code!
## What is Secure Telepathic Data?
# Getting started
**Telepathic** means:
## Example App Walkthrough
- **Read and write data as if it was local,** from anywhere in your app.
- **Always have that data synced, instantly.** Across devices of the same user &mdash; or to other users (coming soon: to your backend, workers, etc.)
**For now the best tutorial is the walkthrough of the [Todo List Example App](#todo-list).**
**Secure** means:
## General Scenarios
- **Fine-grained, role-based permissions are *baked into* your data.**
- **Permissions are enforced everywhere, locally.** (using cryptography instead of through an API)
- Roles can be changed dynamically, supporting changing teams, invite links and more.
### Building a new, entirely sync-based React app
## How to build an app with Jazz?
1. Define your data model with [cojson Collaborative Values (CoValues)](./DOCS.md/#covalue).
2. Implement permission logic using [cojson Groups](./DOCS.md/#group).
3. Build a user interface with [jazz-react](./DOCS.md/#jazz-react)'s reactive [synced queries](./DOCS.md/#usesyncedqueryid).
### Building a new app, completely with Jazz
### Gradually adding sync to an existing React app
It's still a bit early, but these are the rough steps:
Gradually migrate app features to use sync:
1. Define your data model with [CoJSON Values](#cojson).
2. Implement permission logic using [CoJSON Groups](#group).
3. Hook up a user interface with [jazz-react](#jazz-react).
1. Define data model for small aspect of your app with [cojson Collaborative Values (CoValues)](./DOCS.md/#covalue).
- Schema adapters/importers for Prisma/Drizzle/PostgreSQL introspection coming soon.
2. Map existing permission logic with [cojson Groups](./DOCS.md/#group) & integrate existing auth.
- Auth integrations coming soon.
3. Replace some of the React state and API requests in your UI with [jazz-react](./DOCS.md/#jazz-react)'s reactive [synced queries](./DOCS.md/#usesyncedqueryid).
The best example is currently the [Todo List app](#example-app-todo-list).
# Example Apps
### Gradually adding Jazz to an existing app
## Todo List
Coming soon: Jazz will support gradual adoption by integrating with your existing UI, auth and database.
**A simple collaborative todo list app.**
## Example App: Todo List
Live version: https://example-todo.jazz.tools
The best example of Jazz is currently the Todo List app.
Source code & walkthrough: [`./examples/todo`](./examples/todo)
- Live version: https://example-todo.jazz.tools
- Source code: [`./examples/todo`](./examples/todo). See the README there for a walk-through and running instructions.
Demonstrates:
- Defining a data model with `CoMap`s and `CoList`s
- Creating data and setting permissions with `Group`s
- Fetching, rendering & editing data from nested `CoValue`s with reactive synced queries
# Documentation
Note: Since it's early days, this is the only source of documentation so far.
## Rate-My-Pet
If you want to build something with Jazz, [join the Jazz Discord](https://discord.gg/utDMjHYg42) for encouragement and help!
**A simple social polling app.**
## Overview: Main Packages
Live version: https://example-pets.jazz.tools
**`cojson`** → [DOCS](./DOCS.md#cojson)
Source code (walkthrough coming soon): [`./examples/pets`](./examples/pets)
A library implementing abstractions and protocols for "Collaborative JSON". This will soon be standardized and forms the basis of secure telepathic data.
Demonstrates:
- Implementing per-account data streams (reactions) with `CoStream`s
- Implementing image upload and progressive image streaming using helpers from `jazz-react-media-images` (on top of CoJSON's `BinaryCoStreams` & `ImageDefinition` convention)
**`jazz-react`** → [DOCS](./DOCS.md#jazz-react)
Provides you with everything you need to build react apps around CoJSON, including reactive hooks for telepathic data, local IndexedDB persistence, support for different auth providers and helpers for simple invite links for CoJSON groups.
# Documentation & API Reference
### Supporting packages
<small>
For now, docs are hosted in a single well-structured markdown file: [`./DOCS.md`](./DOCS.md).
**`cojson-simple-sync`**
- [Package Overview](./DOCS.md/#overview)
- [`jazz-react` API](./DOCS.md/#jazz-react)
- [`cojson` API](./DOCS.md/#cojson)
- [`jazz-react-media-images` API](./DOCS.md/#jazz-react-media-images)
A generic CoJSON sync server you can run locally if you don't want to use Jazz Global Mesh (the default sync backend, at `wss://sync.jazz.tools`)
**`jazz-browser`** → [DOCS](./DOCS.md#jazz-browser)
In the future we'll build a dedicated docs page on the Jazz homepage.
framework-agnostic primitives that allow you to use CoJSON in the browser. Used to implement `jazz-react`, will be used to implement bindings for other frameworks in the future.
----
**`jazz-react-auth-local`** (and `jazz-browser-auth-local`): A simple auth provider that stores cryptographic keys on user devices using WebAuthentication/Passkeys. Lets you build Jazz apps completely without a backend, with end-to-end encryption by default.
**`jazz-storage-indexeddb`**
Provides local, offline-capable persistence. Included and enabled in `jazz-react` by default.
</small>
Copyright 2023 &mdash; Garden Computing, Inc.

View File

@@ -1,14 +1,14 @@
# Jazz Todo List Example
# Jazz Rate-My-Pet List Example
Live version: https://example-todo.jazz.tools
Live version: https://example-pets.jazz.tools
## Installing & running the example locally
Start by checking out just the example app to a folder:
```bash
npx degit gardencmp/jazz/examples/todo jazz-example-todo
cd jazz-example-todo
npx degit gardencmp/jazz/examples/pets jazz-example-pets
cd jazz-example-pets
```
(This ensures that you have the example app without git history or our multi-package monorepo)
@@ -27,31 +27,17 @@ npm run dev
## Structure
- [`src/basicComponents`](./src/basicComponents) contains simple components to build the UI, unrelated to Jazz (powered by [shadcn/ui](https://ui.shadcn.com))
- [`src/components`](./src/components/) contains helper components that do contain Jazz-specific logic, but are not super relevant to understand the basics of Jazz and CoJSON
- [`src/0_main.tsx`](./src/0_main.tsx), [`src/1_types.ts`](./src/1_types.ts), [`src/2_App.tsx`](./src/2_App.tsx), [`src/3_TodoTable.tsx`](./src/3_TodoTable.tsx), [`src/router.ts`](./src/router.ts) - the main files for this example, see the walkthrough below
TODO
## Walkthrough
### Main parts
- The top-level provider `<WithJazz/>`: [`src/0_main.tsx`](./src/0_main.tsx)
- Defining the data model with CoJSON: [`src/1_types.ts`](./src/1_types.ts)
- Creating todo projects & routing in `<App/>`: [`src/2_App.tsx`](./src/2_App.tsx)
- Reactively rendering a todo project as a table, adding and editing tasks: [`src/3_TodoTable.tsx`](./src/3_TodoTable.tsx)
TODO
### Helpers
- Getting user profiles in `<NameBadge/>`: [`src/components/NameBadge.tsx`](./src/components/NameBadge.tsx)
- (not yet commented) Creating invite links/QR codes with `<InviteButton/>`: [`src/components/InviteButton.tsx`](./src/components/InviteButton.tsx)
- (not yet commented) `location.hash`-based routing and accepting invite links with `useSimpleHashRouterThatAcceptsInvites()` in [`src/router.ts`](./src/router.ts)
This is the whole Todo List app!
TODO
## Questions / problems / feedback

View File

@@ -8,6 +8,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/0_main.tsx"></script>
<script type="module" src="/src/2_main.tsx"></script>
</body>
</html>

View File

@@ -1,4 +1,4 @@
job "example-todo$BRANCH_SUFFIX" {
job "example-pets$BRANCH_SUFFIX" {
region = "global"
datacenters = ["*"]
@@ -41,7 +41,7 @@ job "example-todo$BRANCH_SUFFIX" {
service {
tags = ["public"]
name = "example-todo$BRANCH_SUFFIX"
name = "example-pets$BRANCH_SUFFIX"
port = "http"
provider = "consul"
}

View File

@@ -1,7 +1,7 @@
{
"name": "jazz-example-pets",
"private": true,
"version": "0.0.2",
"version": "0.0.16",
"type": "module",
"scripts": {
"dev": "vite",
@@ -16,16 +16,18 @@
"@types/qrcode": "^1.5.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"jazz-react": "^0.1.13",
"jazz-react-auth-local": "^0.1.13",
"jazz-browser-media-images": "^0.4.1",
"jazz-react": "^0.4.1",
"jazz-react-auth-local": "^0.4.1",
"lucide-react": "^0.274.0",
"qrcode": "^1.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router": "^6.16.0",
"react-router-dom": "^6.16.0",
"tailwind-merge": "^1.14.0",
"tailwindcss-animate": "^1.0.7",
"uniqolor": "^1.1.0",
"use-debounce": "^9.0.4"
"uniqolor": "^1.1.0"
},
"devDependencies": {
"@types/react": "^18.2.15",

View File

@@ -1,38 +0,0 @@
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { WithJazz } from "jazz-react";
import { LocalAuth } from "jazz-react-auth-local";
import { ThemeProvider, TitleAndLogo } from "./basicComponents/index.ts";
import { PrettyAuthUI } from "./components/Auth.tsx";
import App from "./2_App.tsx";
/** Walkthrough: The top-level provider `<WithJazz/>`
*
* This shows how to use the top-level provider `<WithJazz/>`,
* which provides the rest of the app with a `LocalNode` (used through `useJazz` later),
* based on `LocalAuth` that uses PassKeys (aka WebAuthn) to store a user's account secret
* - no backend needed. */
const appName = "Jazz Rate My Pet Example";
const auth = LocalAuth({
appName,
Component: PrettyAuthUI,
});
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ThemeProvider>
<TitleAndLogo name={appName} />
<WithJazz auth={auth}>
<App />
</WithJazz>
</ThemeProvider>
</React.StrictMode>
);
/** Walkthrough: Continue with ./1_types.ts */

View File

@@ -1,4 +1,11 @@
import { CoMap, CoID, BinaryCoStream, CoStream } from "cojson";
import {
AccountMigration,
CoList,
CoMap,
CoStream,
Media,
Profile,
} from "cojson";
/** Walkthrough: Defining the data model with CoJSON
*
@@ -9,19 +16,37 @@ import { CoMap, CoID, BinaryCoStream, CoStream } from "cojson";
export type PetPost = CoMap<{
name: string;
image: CoID<BinaryCoStream>;
reactions: CoID<PetReactions>;
image: Media.ImageDefinition["id"];
reactions: PetReactions["id"];
}>;
export type ReactionType =
| "aww"
| "love"
| "haha"
| "wow"
| "tiny"
| "chonkers"
| "good";
export const REACTION_TYPES = [
"aww",
"love",
"haha",
"wow",
"tiny",
"chonkers",
] as const;
export type ReactionType = (typeof REACTION_TYPES)[number];
export type PetReactions = CoStream<ReactionType>;
export type ListOfPosts = CoList<PetPost["id"]>;
export type PetAccountRoot = CoMap<{
posts: ListOfPosts["id"];
}>;
export const migration: AccountMigration<Profile, PetAccountRoot> = (account) => {
if (!account.get("root")) {
const root = account.createMap<PetAccountRoot>({
posts: account.createList<ListOfPosts>().id,
});
account.set("root", root.id);
console.log("Created root", root.id);
}
};
/** Walkthrough: Continue with ./2_App.tsx */

View File

@@ -1,50 +0,0 @@
import { useCallback } from "react";
import { useJazz } from "jazz-react";
import { PetPost } from "./1_types";
import { Button } from "./basicComponents";
import { useSimpleHashRouterThatAcceptsInvites } from "./router";
import { PetPostUI } from "./4_PetPostUI";
import { CreatePetPostForm } from "./4_CreatePetPostForm";
/** Walkthrough: Creating pet posts & routing in `<App/>`
*
* <App> is the main app component, handling client-side routing based
* on the CoValue ID (CoID) of our PetPost, stored in the URL hash
* - which can also contain invite links.
*/
export default function App() {
// A `LocalNode` represents a local view of loaded & created CoValues.
// It is associated with a current user account, which will determine
// access rights to CoValues. We get it from the top-level provider `<WithJazz/>`.
const { localNode, logOut } = useJazz();
// This sets up routing and accepting invites, skip for now
const [currentPetPostID, navigateToPetPostID] =
useSimpleHashRouterThatAcceptsInvites<PetPost>(localNode);
return (
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
{currentPetPostID ? (
<PetPostUI petPostID={currentPetPostID} />
) : (
<CreatePetPostForm onCreate={navigateToPetPostID} />
)}
<Button
onClick={() => {
navigateToPetPostID(undefined);
logOut();
}}
variant="outline"
>
Log Out
</Button>
</div>
);
}
/** Walkthrough: continue with ./3_TodoTable.tsx */

View File

@@ -0,0 +1,119 @@
import React from "react";
import ReactDOM from "react-dom/client";
import {
Link,
RouterProvider,
createHashRouter,
} from "react-router-dom";
import "./index.css";
import { WithJazz, useJazz, useAcceptInvite } from "jazz-react";
import { LocalAuth } from "jazz-react-auth-local";
import {
Button,
ThemeProvider,
TitleAndLogo,
} from "./basicComponents/index.ts";
import { PrettyAuthUI } from "./components/Auth.tsx";
import { NewPetPostForm } from "./3_NewPetPostForm.tsx";
import { RatePetPostUI } from "./4_RatePetPostUI.tsx";
import { PetAccountRoot, migration } from "./1_types.ts";
import { AccountMigration, Profile } from "cojson";
/** Walkthrough: The top-level provider `<WithJazz/>`
*
* This shows how to use the top-level provider `<WithJazz/>`,
* which provides the rest of the app with a `LocalNode` (used through `useJazz` later),
* based on `LocalAuth` that uses PassKeys (aka WebAuthn) to store a user's account secret
* - no backend needed. */
const appName = "Jazz Rate My Pet Example";
const auth = LocalAuth({
appName,
Component: PrettyAuthUI,
});
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ThemeProvider>
<TitleAndLogo name={appName} />
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
<WithJazz auth={auth} migration={migration as AccountMigration}>
<App />
</WithJazz>
</div>
</ThemeProvider>
</React.StrictMode>
);
/** Walkthrough: Creating pet posts & routing in `<App/>`
*
* <App> is the main app component, handling client-side routing based
* on the CoValue ID (CoID) of our PetPost, stored in the URL hash
* - which can also contain invite links.
*/
export default function App() {
const { logOut } = useJazz();
const router = createHashRouter([
{
path: "/",
element: <PostOverview />,
},
{
path: "/new",
element: <NewPetPostForm />,
},
{
path: "/pet/:petPostId",
element: <RatePetPostUI />,
},
{
path: "/invite/*",
element: <p>Accepting invite...</p>,
},
]);
useAcceptInvite((petPostID) => router.navigate("/pet/" + petPostID));
return (
<>
<RouterProvider router={router} />
<Button
onClick={() => router.navigate("/").then(logOut)}
variant="outline"
>
Log Out
</Button>
</>
);
}
export function PostOverview() {
const { me } = useJazz<Profile, PetAccountRoot>();
const myPosts = me.root?.posts;
return (
<>
root: {JSON.stringify(me.root?.coMap.asObject())}
posts: {JSON.stringify(me.root?.posts?.coList?.asArray())}
<h1>My posts</h1>
{myPosts?.length
? myPosts.map(
(post) =>
post && (
<Link key={post.id} to={"/pet/" + post.id}>
{post.name}
</Link>
)
)
: undefined}
<Link to="/new">New post</Link>
</>
);
}

View File

@@ -0,0 +1,107 @@
import { ChangeEvent, useCallback, useState } from "react";
import { useNavigate } from "react-router";
import { CoID, CoMap, Media, Profile } from "cojson";
import { useJazz, useSyncedQuery } from "jazz-react";
import { BrowserImage, createImage } from "jazz-browser-media-images";
import { PetAccountRoot, PetPost, PetReactions } from "./1_types";
import { Input, Button } from "./basicComponents";
/** Walkthrough: TODO
*/
type PartialPetPost = CoMap<{
name: string;
image?: Media.ImageDefinition["id"];
reactions: PetReactions["id"];
}>;
export function NewPetPostForm() {
const { me } = useJazz<Profile, PetAccountRoot>();
const navigate = useNavigate();
const [newPostId, setNewPostId] = useState<
CoID<PartialPetPost> | undefined
>(undefined);
const newPetPost = useSyncedQuery(newPostId);
const onChangeName = useCallback(
(name: string) => {
if (newPetPost) {
newPetPost.set({ name });
} else {
const petPostGroup = me.createGroup();
const petPost = petPostGroup.createMap<PartialPetPost>({
name,
reactions: petPostGroup.createStream<PetReactions>().id,
});
setNewPostId(petPost.id);
}
},
[me, newPetPost]
);
const onImageSelected = useCallback(
async (event: ChangeEvent<HTMLInputElement>) => {
if (!newPetPost || !event.target.files) return;
const image = await createImage(
event.target.files[0],
newPetPost.group
);
newPetPost.set({ image: image.id });
},
[newPetPost]
);
const onSubmit = useCallback(() => {
if (!newPetPost) return;
const myPosts = me.root?.posts;
if (!myPosts) {
throw new Error("No posts list found");
}
myPosts.append(newPetPost.id as PetPost["id"]);
navigate("/pet/" + newPetPost.id);
}, [me.root?.posts, newPetPost, navigate]);
return (
<div className="flex flex-col gap-10">
<p>Share your pet with friends!</p>
<Input
type="text"
placeholder="Pet Name"
className="text-3xl py-6"
onChange={(event) => onChangeName(event.target.value)}
value={newPetPost?.name || ""}
/>
{newPetPost?.image ? (
<img
className="w-80 max-w-full rounded"
src={
newPetPost?.image.as(BrowserImage)
?.highestResSrcOrPlaceholder
}
/>
) : (
<Input
type="file"
disabled={!newPetPost?.name}
onChange={onImageSelected}
/>
)}
{newPetPost?.name && newPetPost?.image && (
<Button onClick={onSubmit}>Submit Post</Button>
)}
</div>
);
}

View File

@@ -1,103 +0,0 @@
import { useCallback, useState } from "react";
import { BinaryCoStream, CoID } from "cojson";
import {
useBinaryStream,
useJazz,
useTelepathicState,
} from "jazz-react";
import { PetPost, PetReactions, ReactionType } from "./1_types";
import {
Input,
Button,
} from "./basicComponents";
import { InviteButton } from "./components/InviteButton";
import { NameBadge } from "./components/NameBadge";
import { useDebouncedCallback } from "use-debounce";
import { createBinaryStreamHandler } from "jazz-react";
/** Walkthrough: TODO
*/
export function CreatePetPostForm({
onCreate,
}: {
onCreate: (id: CoID<PetPost>) => void;
}) {
const { localNode } = useJazz();
const [creatingPostId, setCreatingPostId] = useState<
CoID<PetPost> | undefined
>(undefined);
const creatingPetPost = useTelepathicState(creatingPostId);
const onChangeName = useDebouncedCallback((name: string) => {
let petPost = creatingPetPost;
if (!petPost) {
const petPostGroup = localNode.createGroup();
petPost = petPostGroup.createMap<PetPost>();
const reactions = petPostGroup.createStream<PetReactions>();
petPost = petPost.edit((petPost) => {
petPost.set("reactions", reactions.id);
});
setCreatingPostId(petPost.id);
}
petPost.edit((petPost) => {
petPost.set("name", name);
});
}, 200);
const onImageCreated = useCallback(
(image: BinaryCoStream) => {
if (!creatingPetPost) throw new Error("Never get here");
creatingPetPost.edit((petPost) => {
petPost.set("image", image.id);
});
},
[creatingPetPost]
);
const image = useBinaryStream(creatingPetPost?.get("image"));
return (
<div>
<Input
type="text"
placeholder="Pet Name"
onChange={event => onChangeName(event.target.value)}
value={creatingPetPost?.get("name")}
/>
{image ? (
<img src={image.blobURL} />
) : (
creatingPetPost && (
<Input
type="file"
onChange={createBinaryStreamHandler(
onImageCreated,
creatingPetPost.group
)}
/>
)
)}
{creatingPetPost?.get("name") && creatingPetPost?.get("image") && (
<Button
onClick={() => {
onCreate(creatingPetPost.id);
}}
>
Submit Post
</Button>
)}
</div>
);
}

View File

@@ -1,18 +0,0 @@
import { useCallback } from "react";
import { CoID } from "cojson";
import { useTelepathicState } from "jazz-react";
import { PetPost } from "./1_types";
import { InviteButton } from "./components/InviteButton";
import { NameBadge } from "./components/NameBadge";
/** Walkthrough: TODO
*/
export function PetPostUI({ petPostID }: { petPostID: CoID<PetPost> }) {
return (<div>TODO</div>);
}

View File

@@ -0,0 +1,126 @@
import { useParams } from "react-router";
import { CoID, Queried } from "cojson";
import { useSyncedQuery } from "jazz-react";
import { PetPost, ReactionType, REACTION_TYPES, PetReactions } from "./1_types";
import { ShareButton } from "./components/ShareButton";
import { Button, Skeleton } from "./basicComponents";
import { BrowserImage } from "jazz-browser-media-images";
import uniqolor from "uniqolor";
/** Walkthrough: TODO
*/
const reactionEmojiMap: { [reaction in ReactionType]: string } = {
aww: "😍",
love: "❤️",
haha: "😂",
wow: "😮",
tiny: "🐥",
chonkers: "🐘",
};
export function RatePetPostUI() {
const petPostID = useParams<{ petPostId: CoID<PetPost> }>().petPostId;
const petPost = useSyncedQuery(petPostID);
return (
<div className="flex flex-col gap-8">
<div className="flex justify-between">
<h1 className="text-3xl font-bold">{petPost?.name}</h1>
<ShareButton petPost={petPost} />
</div>
{petPost?.image && (
<img
className="w-80 max-w-full rounded"
src={
petPost.image.as(BrowserImage)
?.highestResSrcOrPlaceholder
}
/>
)}
<div className="flex justify-between max-w-xs flex-wrap">
{REACTION_TYPES.map((reactionType) => (
<Button
key={reactionType}
variant={
petPost?.reactions?.me?.last === reactionType
? "default"
: "outline"
}
onClick={() => {
petPost?.reactions?.push(reactionType);
}}
title={`React with ${reactionType}`}
className="text-2xl px-2"
>
{reactionEmojiMap[reactionType]}
</Button>
))}
</div>
{petPost?.group.myRole() === "admin" && petPost.reactions && (
<ReactionOverview petReactions={petPost.reactions} />
)}
</div>
);
}
function ReactionOverview({
petReactions,
}: {
petReactions: Queried<PetReactions>;
}) {
return (
<div>
<h2>Reactions</h2>
<div className="flex flex-col gap-1">
{REACTION_TYPES.map((reactionType) => {
const reactionsOfThisType = petReactions.perAccount
.map(([, reaction]) => reaction)
.filter(({ last }) => last === reactionType);
if (reactionsOfThisType.length === 0) return null;
return (
<div
className="flex gap-2 items-center"
key={reactionType}
>
{reactionEmojiMap[reactionType]}{" "}
{reactionsOfThisType.map((reaction, idx) =>
reaction.by?.profile?.name ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={uniqueColoring(reaction.by.id)}
key={reaction.by.id}
>
{reaction.by.profile.name}
</span>
) : (
<Skeleton
className="mt-1 w-[50px] h-[1em] rounded-full"
key={idx}
/>
)
)}
</div>
);
})}
</div>
</div>
);
}
function uniqueColoring(seed: string) {
const darkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
return {
color: uniqolor(seed, { lightness: darkMode ? 80 : 20 }).color,
background: uniqolor(seed, { lightness: darkMode ? 20 : 80 }).color,
};
}

View File

@@ -1,7 +1,12 @@
export function TitleAndLogo({name}: {name: string}) {
return <>
<div className="flex items-center gap-2 justify-center mt-5">
import { Toaster } from ".";
export function TitleAndLogo({ name }: { name: string }) {
return (
<>
<div className="flex items-center gap-2 justify-center mt-5">
<img src="jazz-logo.png" className="h-5" /> {name}
</div>
</>
}
<Toaster />
</>
);
}

View File

@@ -1,4 +1,7 @@
export { Button } from "./ui/button";
export { Input } from "./ui/input";
export { Toaster } from "./ui/toaster";
export { useToast } from "./ui/use-toast";
export { Skeleton } from "./ui/skeleton";
export { TitleAndLogo } from "./TitleAndLogo";
export { ThemeProvider } from "./themeProvider";

View File

@@ -0,0 +1,15 @@
import { cn } from "@/basicComponents/lib/utils"
function Skeleton({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn("animate-pulse rounded-md bg-muted", className)}
{...props}
/>
)
}
export { Skeleton }

View File

@@ -0,0 +1,127 @@
import * as React from "react"
import * as ToastPrimitives from "@radix-ui/react-toast"
import { cva, type VariantProps } from "class-variance-authority"
import { X } from "lucide-react"
import { cn } from "@/basicComponents/lib/utils"
const ToastProvider = ToastPrimitives.Provider
const ToastViewport = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Viewport>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Viewport
ref={ref}
className={cn(
"fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]",
className
)}
{...props}
/>
))
ToastViewport.displayName = ToastPrimitives.Viewport.displayName
const toastVariants = cva(
"group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full",
{
variants: {
variant: {
default: "border bg-background text-foreground",
destructive:
"destructive group border-destructive bg-destructive text-destructive-foreground",
},
},
defaultVariants: {
variant: "default",
},
}
)
const Toast = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Root>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
VariantProps<typeof toastVariants>
>(({ className, variant, ...props }, ref) => {
return (
<ToastPrimitives.Root
ref={ref}
className={cn(toastVariants({ variant }), className)}
{...props}
/>
)
})
Toast.displayName = ToastPrimitives.Root.displayName
const ToastAction = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Action>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Action
ref={ref}
className={cn(
"inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive",
className
)}
{...props}
/>
))
ToastAction.displayName = ToastPrimitives.Action.displayName
const ToastClose = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Close>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Close
ref={ref}
className={cn(
"absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
className
)}
toast-close=""
{...props}
>
<X className="h-4 w-4" />
</ToastPrimitives.Close>
))
ToastClose.displayName = ToastPrimitives.Close.displayName
const ToastTitle = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Title>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Title
ref={ref}
className={cn("text-sm font-semibold", className)}
{...props}
/>
))
ToastTitle.displayName = ToastPrimitives.Title.displayName
const ToastDescription = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Description>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Description
ref={ref}
className={cn("text-sm opacity-90", className)}
{...props}
/>
))
ToastDescription.displayName = ToastPrimitives.Description.displayName
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
type ToastActionElement = React.ReactElement<typeof ToastAction>
export {
type ToastProps,
type ToastActionElement,
ToastProvider,
ToastViewport,
Toast,
ToastTitle,
ToastDescription,
ToastClose,
ToastAction,
}

View File

@@ -0,0 +1,33 @@
import {
Toast,
ToastClose,
ToastDescription,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/basicComponents/ui/toast"
import { useToast } from "@/basicComponents/ui/use-toast"
export function Toaster() {
const { toasts } = useToast()
return (
<ToastProvider>
{toasts.map(function ({ id, title, description, action, ...props }) {
return (
<Toast key={id} {...props}>
<div className="grid gap-1">
{title && <ToastTitle>{title}</ToastTitle>}
{description && (
<ToastDescription>{description}</ToastDescription>
)}
</div>
{action}
<ToastClose />
</Toast>
)
})}
<ToastViewport />
</ToastProvider>
)
}

View File

@@ -0,0 +1,192 @@
// Inspired by react-hot-toast library
import * as React from "react"
import type {
ToastActionElement,
ToastProps,
} from "@/basicComponents/ui/toast"
const TOAST_LIMIT = 1
const TOAST_REMOVE_DELAY = 1000000
type ToasterToast = ToastProps & {
id: string
title?: React.ReactNode
description?: React.ReactNode
action?: ToastActionElement
}
const actionTypes = {
ADD_TOAST: "ADD_TOAST",
UPDATE_TOAST: "UPDATE_TOAST",
DISMISS_TOAST: "DISMISS_TOAST",
REMOVE_TOAST: "REMOVE_TOAST",
} as const
let count = 0
function genId() {
count = (count + 1) % Number.MAX_VALUE
return count.toString()
}
type ActionType = typeof actionTypes
type Action =
| {
type: ActionType["ADD_TOAST"]
toast: ToasterToast
}
| {
type: ActionType["UPDATE_TOAST"]
toast: Partial<ToasterToast>
}
| {
type: ActionType["DISMISS_TOAST"]
toastId?: ToasterToast["id"]
}
| {
type: ActionType["REMOVE_TOAST"]
toastId?: ToasterToast["id"]
}
interface State {
toasts: ToasterToast[]
}
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
const addToRemoveQueue = (toastId: string) => {
if (toastTimeouts.has(toastId)) {
return
}
const timeout = setTimeout(() => {
toastTimeouts.delete(toastId)
dispatch({
type: "REMOVE_TOAST",
toastId: toastId,
})
}, TOAST_REMOVE_DELAY)
toastTimeouts.set(toastId, timeout)
}
export const reducer = (state: State, action: Action): State => {
switch (action.type) {
case "ADD_TOAST":
return {
...state,
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
}
case "UPDATE_TOAST":
return {
...state,
toasts: state.toasts.map((t) =>
t.id === action.toast.id ? { ...t, ...action.toast } : t
),
}
case "DISMISS_TOAST": {
const { toastId } = action
// ! Side effects ! - This could be extracted into a dismissToast() action,
// but I'll keep it here for simplicity
if (toastId) {
addToRemoveQueue(toastId)
} else {
state.toasts.forEach((toast) => {
addToRemoveQueue(toast.id)
})
}
return {
...state,
toasts: state.toasts.map((t) =>
t.id === toastId || toastId === undefined
? {
...t,
open: false,
}
: t
),
}
}
case "REMOVE_TOAST":
if (action.toastId === undefined) {
return {
...state,
toasts: [],
}
}
return {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
}
}
}
const listeners: Array<(state: State) => void> = []
let memoryState: State = { toasts: [] }
function dispatch(action: Action) {
memoryState = reducer(memoryState, action)
listeners.forEach((listener) => {
listener(memoryState)
})
}
type Toast = Omit<ToasterToast, "id">
function toast({ ...props }: Toast) {
const id = genId()
const update = (props: ToasterToast) =>
dispatch({
type: "UPDATE_TOAST",
toast: { ...props, id },
})
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
dispatch({
type: "ADD_TOAST",
toast: {
...props,
id,
open: true,
onOpenChange: (open) => {
if (!open) dismiss()
},
},
})
return {
id: id,
dismiss,
update,
}
}
function useToast() {
const [state, setState] = React.useState<State>(memoryState)
React.useEffect(() => {
listeners.push(setState)
return () => {
const index = listeners.indexOf(setState)
if (index > -1) {
listeners.splice(index, 1)
}
}
}, [state])
return {
...state,
toast,
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
}
}
export { useToast, toast }

View File

@@ -1,46 +0,0 @@
import { AccountID } from "cojson";
import { useProfile } from "jazz-react";
import { Skeleton } from "@/basicComponents";
import uniqolor from "uniqolor";
/** Walkthrough: Getting user profiles in `<NameBadge/>`
*
* `<NameBadge/>` uses `useProfile(accountID)`, which is a shorthand for
* useTelepathicState on an account's profile.
*
* Profiles are always a `CoMap<{name: string}>`, but they might have app-specific
* additional properties).
*
* In our case, we just display the profile name (which is set by the LocalAuth
* provider when we first create an account).
*/
export function NameBadge({ accountID }: { accountID?: AccountID }) {
const profile = useProfile(accountID);
return accountID && profile?.get("name") ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={randomUserColor(accountID)}
>
{profile.get("name")}
</span>
) : (
<Skeleton className="mt-1 w-[50px] h-[1em] rounded-full" />
);
}
function randomUserColor(accountID: AccountID) {
const theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
const brightColor = uniqolor(accountID || "", { lightness: 80 }).color;
const darkColor = uniqolor(accountID || "", { lightness: 20 }).color;
return {
color: theme == "light" ? darkColor : brightColor,
background: theme == "light" ? brightColor : darkColor,
};
}

View File

@@ -1,27 +1,28 @@
import { useState } from "react";
import { TodoProject } from "../1_types";
import { PetPost } from "../1_types";
import { createInviteLink } from "jazz-react";
import QRCode from "qrcode";
import { useToast, Button } from "../basicComponents";
import { Queried } from "cojson";
export function InviteButton({ list }: { list?: TodoProject }) {
export function ShareButton({ petPost }: { petPost?: Queried<PetPost> }) {
const [existingInviteLink, setExistingInviteLink] = useState<string>();
const { toast } = useToast();
return (
list?.group.myRole() === "admin" && (
petPost?.group.myRole() === "admin" && (
<Button
size="sm"
className="py-0"
disabled={!list}
disabled={!petPost}
variant="outline"
onClick={async () => {
let inviteLink = existingInviteLink;
if (list && !inviteLink) {
inviteLink = createInviteLink(list, "writer");
if (petPost && !inviteLink) {
inviteLink = createInviteLink(petPost, "writer");
setExistingInviteLink(inviteLink);
}
if (inviteLink) {
@@ -39,7 +40,7 @@ export function InviteButton({ list }: { list?: TodoProject }) {
}
}}
>
Invite
Share
</Button>
)
);

View File

@@ -1,37 +0,0 @@
import { useCallback, useEffect, useState } from "react";
import { CoID, LocalNode, CoValueImpl } from "cojson";
import { consumeInviteLinkFromWindowLocation } from "jazz-react";
export function useSimpleHashRouterThatAcceptsInvites<C extends CoValueImpl>(
localNode: LocalNode
) {
const [currentValueId, setCurrentValueId] = useState<CoID<C>>();
useEffect(() => {
const listener = async () => {
const acceptedInvitation = await consumeInviteLinkFromWindowLocation<C>(localNode);
if (acceptedInvitation) {
setCurrentValueId(acceptedInvitation.valueID);
window.location.hash = acceptedInvitation.valueID;
return;
}
setCurrentValueId(
(window.location.hash.slice(1) as CoID<C>) || undefined
);
};
window.addEventListener("hashchange", listener);
listener();
return () => {
window.removeEventListener("hashchange", listener);
};
}, [localNode]);
const navigateToValue = useCallback((id: CoID<C> | undefined) => {
window.location.hash = id || "";
}, []);
return [currentValueId, navigateToValue] as const;
}

View File

@@ -27,29 +27,28 @@ npm run dev
## Structure
- [`src/basicComponents`](./src/basicComponents) contains simple components to build the UI, unrelated to Jazz (powered by [shadcn/ui](https://ui.shadcn.com))
- [`src/components`](./src/components/) contains helper components that do contain Jazz-specific logic, but are not super relevant to understand the basics of Jazz and CoJSON
- [`src/0_main.tsx`](./src/0_main.tsx), [`src/1_types.ts`](./src/1_types.ts), [`src/2_App.tsx`](./src/2_App.tsx), [`src/3_TodoTable.tsx`](./src/3_TodoTable.tsx), [`src/router.ts`](./src/router.ts) - the main files for this example, see the walkthrough below
- [`src/basicComponents`](./src/basicComponents): simple components to build the UI, unrelated to Jazz (uses [shadcn/ui](https://ui.shadcn.com))
- [`src/components`](./src/components/): helper components that do contain Jazz-specific logic, but aren't very relevant to understand the basics of Jazz and CoJSON
- [`src/1_types.ts`](./src/1_types.ts),
[`src/2_main.tsx`](./src/2_main.tsx),
[`src/3_NewProjectForm.tsx`](./src/3_NewProjectForm.tsx),
[`src/4_ProjectTodoTable.tsx`](./src/4_ProjectTodoTable.tsx): the main files for this example, see the walkthrough below
## Walkthrough
### Main parts
- The top-level provider `<WithJazz/>`: [`src/0_main.tsx`](./src/0_main.tsx)
1. Defining the data model with CoJSON: [`src/1_types.ts`](./src/1_types.ts)
- Defining the data model with CoJSON: [`src/1_types.ts`](./src/1_types.ts)
2. The top-level provider `<WithJazz/>` and routing: [`src/2_main.tsx`](./src/2_main.tsx)
- Creating todo projects & routing in `<App/>`: [`src/2_App.tsx`](./src/2_App.tsx)
3. Creating a new todo project: [`src/3_NewProjectForm.tsx`](./src/3_NewProjectForm.tsx)
- Reactively rendering a todo project as a table, adding and editing tasks: [`src/3_TodoTable.tsx`](./src/3_TodoTable.tsx)
4. Reactively rendering a todo project as a table, adding and editing tasks: [`src/4_ProjectTodoTable.tsx`](./src/4_ProjectTodoTable.tsx)
### Helpers
- Getting user profiles in `<NameBadge/>`: [`src/components/NameBadge.tsx`](./src/components/NameBadge.tsx)
- (not yet commented) Creating invite links/QR codes with `<InviteButton/>`: [`src/components/InviteButton.tsx`](./src/components/InviteButton.tsx)
- (not yet commented) `location.hash`-based routing and accepting invite links with `useSimpleHashRouterThatAcceptsInvites()` in [`src/router.ts`](./src/router.ts)
- (not yet explained) Creating invite links/QR codes with `<InviteButton/>`: [`src/components/InviteButton.tsx`](./src/components/InviteButton.tsx)
This is the whole Todo List app!
@@ -62,4 +61,4 @@ If you have feedback, let us know on [Discord](https://discord.gg/utDMjHYg42) or
By default, the example app uses [Jazz Global Mesh](https://jazz.tools/mesh) (`wss://sync.jazz.tools`) - so cross-device use, invites and collaboration should just work.
You can also run a local sync server by running `npx cojson-simple-sync` and adding the query param `?sync=ws://localhost:4200` to the URL of the example app (for example: `http://localhost:5173/?sync=ws://localhost:4200`), or by setting the `sync` parameter of the `<WithJazz>` provider component in [./src/0_main.tsx](./src/0_main.tsx).
You can also run a local sync server by running `npx cojson-simple-sync` and adding the query param `?sync=ws://localhost:4200` to the URL of the example app (for example: `http://localhost:5173/?sync=ws://localhost:4200`), or by setting the `sync` parameter of the `<WithJazz>` provider component in [./src/2_main.tsx](./src/2_main.tsx).

View File

@@ -8,6 +8,6 @@
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/0_main.tsx"></script>
<script type="module" src="/src/2_main.tsx"></script>
</body>
</html>

View File

@@ -1,7 +1,7 @@
{
"name": "jazz-example-todo",
"private": true,
"version": "0.0.27",
"version": "0.0.41",
"type": "module",
"scripts": {
"dev": "vite",
@@ -16,12 +16,14 @@
"@types/qrcode": "^1.5.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"jazz-react": "^0.1.13",
"jazz-react-auth-local": "^0.1.13",
"jazz-react": "^0.4.1",
"jazz-react-auth-local": "^0.4.1",
"lucide-react": "^0.274.0",
"qrcode": "^1.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router": "^6.16.0",
"react-router-dom": "^6.16.0",
"tailwind-merge": "^1.14.0",
"tailwindcss-animate": "^1.0.7",
"uniqolor": "^1.1.0"

View File

@@ -1,38 +0,0 @@
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import { WithJazz } from "jazz-react";
import { LocalAuth } from "jazz-react-auth-local";
import { ThemeProvider, TitleAndLogo } from "./basicComponents/index.ts";
import { PrettyAuthUI } from "./components/Auth.tsx";
import App from "./2_App.tsx";
/** Walkthrough: The top-level provider `<WithJazz/>`
*
* This shows how to use the top-level provider `<WithJazz/>`,
* which provides the rest of the app with a `LocalNode` (used through `useJazz` later),
* based on `LocalAuth` that uses PassKeys (aka WebAuthn) to store a user's account secret
* - no backend needed. */
const appName = "Jazz Todo List Example";
const auth = LocalAuth({
appName,
Component: PrettyAuthUI,
});
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ThemeProvider>
<TitleAndLogo name={appName} />
<WithJazz auth={auth}>
<App />
</WithJazz>
</ThemeProvider>
</React.StrictMode>
);
/** Walkthrough: Continue with ./1_types.ts */

View File

@@ -1,28 +1,42 @@
import { CoMap, CoList, CoID } from "cojson";
import { CoMap, CoList, AccountMigration, Profile } from "cojson";
/** Walkthrough: Defining the data model with CoJSON
*
* Here, we define our main data model of tasks, lists of tasks and projects
* using CoJSON's collaborative map and list types, CoMap & CoList.
*
* CoMap values and CoLists items can be:
* CoMap values and CoLists items can contain:
* - arbitrary immutable JSON
* - references to other CoValues by their CoID
* - CoIDs are strings that look like `co_zXPuWmH1D1cKdMpDW6CMzWb3LpY`
* - In TypeScript, CoIDs take a generic parameter for the type of the
* referenced CoValue, e.g. `CoID<Task>` - to make the references precise
**/
/** An individual task which collaborators can tick or rename */
export type Task = CoMap<{ done: boolean; text: string; }>;
/** A collaborative, ordered list of task references */
export type ListOfTasks = CoList<CoID<Task>>;
export type ListOfTasks = CoList<Task["id"]>;
/** Our top level object: a project with a title, referencing a list of tasks */
export type TodoProject = CoMap<{
title: string;
tasks: CoID<ListOfTasks>;
/** A collaborative, ordered list of tasks */
tasks: ListOfTasks["id"];
}>;
/** Walkthrough: Continue with ./2_App.tsx */
export type ListOfProjects = CoList<TodoProject["id"]>;
export type TodoAccountRoot = CoMap<{
projects: ListOfProjects["id"];
}>;
export const migration: AccountMigration<Profile, TodoAccountRoot> = (account) => {
if (!account.get("root")) {
account.set(
"root",
account.createMap<TodoAccountRoot>({
projects: account.createList<ListOfProjects>().id,
}).id
);
}
}
/** Walkthrough: Continue with ./2_main.tsx */

View File

@@ -1,78 +0,0 @@
import { useCallback } from "react";
import { useJazz } from "jazz-react";
import { TodoProject, ListOfTasks } from "./1_types";
import { SubmittableInput, Button } from "./basicComponents";
import { useSimpleHashRouterThatAcceptsInvites } from "./router";
import { TodoTable } from "./3_TodoTable";
/** Walkthrough: Creating todo projects & routing in `<App/>`
*
* <App> is the main app component, handling client-side routing based
* on the CoValue ID (CoID) of our TodoProject, stored in the URL hash
* - which can also contain invite links.
*/
export default function App() {
// A `LocalNode` represents a local view of loaded & created CoValues.
// It is associated with a current user account, which will determine
// access rights to CoValues. We get it from the top-level provider `<WithJazz/>`.
const { localNode, logOut } = useJazz();
// This sets up routing and accepting invites, skip for now
const [currentProjectId, navigateToProjectId] =
useSimpleHashRouterThatAcceptsInvites<TodoProject>(localNode);
const createProject = useCallback(
(title: string) => {
if (!title) return;
// To create a new todo project, we first create a `Group`,
// which is a scope for defining access rights (reader/writer/admin)
// of its members, which will apply to all CoValues owned by that group.
const projectGroup = localNode.createGroup();
// Then we create an empty todo project and list of tasks within that group.
const project = projectGroup.createMap<TodoProject>();
const tasks = projectGroup.createList<ListOfTasks>();
// We edit the todo project to initialise it.
// Inside the `.edit` callback we can mutate a CoValue
project.edit((project) => {
project.set("title", title);
project.set("tasks", tasks.id);
});
navigateToProjectId(project.id);
},
[localNode, navigateToProjectId]
);
return (
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
{currentProjectId ? (
<TodoTable projectId={currentProjectId} />
) : (
<SubmittableInput
onSubmit={createProject}
label="Create New Project"
placeholder="New project title"
/>
)}
<Button
onClick={() => {
navigateToProjectId(undefined);
logOut();
}}
variant="outline"
>
Log Out
</Button>
</div>
);
}
/** Walkthrough: continue with ./3_TodoTable.tsx */

View File

@@ -0,0 +1,94 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { RouterProvider, createHashRouter } from "react-router-dom";
import "./index.css";
import { WithJazz, useJazz, useAcceptInvite } from "jazz-react";
import { LocalAuth } from "jazz-react-auth-local";
import {
Button,
ThemeProvider,
TitleAndLogo,
} from "./basicComponents/index.ts";
import { PrettyAuthUI } from "./components/Auth.tsx";
import { NewProjectForm } from "./3_NewProjectForm.tsx";
import { ProjectTodoTable } from "./4_ProjectTodoTable.tsx";
import { migration } from "./1_types.ts";
import { AccountMigration } from "cojson";
/**
* Walkthrough: The top-level provider `<WithJazz/>`
*
* This shows how to use the top-level provider `<WithJazz/>`,
* which provides the rest of the app with a `LocalNode` (used through `useJazz` later),
* based on `LocalAuth` that uses Passkeys (aka WebAuthn) to store a user's account secret
* - no backend needed.
*/
const appName = "Jazz Todo List Example";
const auth = LocalAuth({
appName,
Component: PrettyAuthUI,
});
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ThemeProvider>
<TitleAndLogo name={appName} />
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
<WithJazz auth={auth} migration={migration as AccountMigration}>
<App />
</WithJazz>
</div>
</ThemeProvider>
</React.StrictMode>
);
/**
* Routing in `<App/>`
*
* <App> is the main app component, handling client-side routing based
* on the CoValue ID (CoID) of our TodoProject, stored in the URL hash
* - which can also contain invite links.
*/
function App() {
// logOut logs out the AuthProvider passed to `<WithJazz/>` above.
const { logOut } = useJazz();
const router = createHashRouter([
{
path: "/",
element: <NewProjectForm />,
},
{
path: "/project/:projectId",
element: <ProjectTodoTable />,
},
{
path: "/invite/*",
element: <p>Accepting invite...</p>,
},
]);
// `useAcceptInvite()` is a hook that accepts an invite link from the URL hash,
// and on success calls our callback where we navigate to the project that we were just invited to.
useAcceptInvite((projectID) => router.navigate("/project/" + projectID));
return (
<>
<RouterProvider router={router} />
<Button
onClick={() => router.navigate("/").then(logOut)}
variant="outline"
>
Log Out
</Button>
</>
);
}
/** Walkthrough: Continue with ./3_NewProjectForm.tsx */

View File

@@ -0,0 +1,47 @@
import { useCallback } from "react";
import { useJazz } from "jazz-react";
import { ListOfTasks, TodoProject } from "./1_types";
import { SubmittableInput } from "./basicComponents";
import { useNavigate } from "react-router";
export function NewProjectForm() {
// A `LocalNode` represents a local view of loaded & created CoValues.
// It is associated with a current user account, which will determine
// access rights to CoValues. We get it from the top-level provider `<WithJazz/>`.
const { localNode } = useJazz();
const navigate = useNavigate();
const createProject = useCallback(
(title: string) => {
if (!title) return;
// To create a new todo project, we first create a `Group`,
// which is a scope for defining access rights (reader/writer/admin)
// of its members, which will apply to all CoValues owned by that group.
const projectGroup = localNode.createGroup();
// Then we create an empty todo project within that group
const project = projectGroup.createMap<TodoProject>({
title,
tasks: projectGroup.createList<ListOfTasks>().id,
});
navigate("/project/" + project.id);
},
[localNode, navigate]
);
return (
<SubmittableInput
onSubmit={createProject}
label="Create New Project"
placeholder="New project title"
/>
);
}
/** Walkthrough: continue with ./4_ProjectTodoTable.tsx */

View File

@@ -1,162 +0,0 @@
import { useCallback } from "react";
import { CoID } from "cojson";
import { useTelepathicState } from "jazz-react";
import { TodoProject, Task } from "./1_types";
import {
Checkbox,
SubmittableInput,
Skeleton,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "./basicComponents";
import { InviteButton } from "./components/InviteButton";
import { NameBadge } from "./components/NameBadge";
/** Walkthrough: Reactively rendering a todo project as a table,
* adding and editing tasks
*
* Here in `<TodoTable/>`, we use `useTelepathicData()` for the first time,
* in this case to load the CoValue for our `TodoProject` as well as
* the `ListOfTasks` referenced in it.
*/
export function TodoTable({ projectId }: { projectId: CoID<TodoProject> }) {
// `useTelepathicData()` reactively subscribes to updates to a CoValue's
// content - whether we create edits locally, load persisted data, or receive
// sync updates from other devices or participants!
const project = useTelepathicState(projectId);
const projectTasks = useTelepathicState(project?.get("tasks"));
// `createTask` is similar to `createProject` we saw earlier, creating a new CoMap
// for a new task (in the same group as the list of tasks/the project), and then
// adding it as an item to the project's list of tasks.
const createTask = useCallback(
(text: string) => {
if (!projectTasks || !text) return;
const task = projectTasks.group.createMap<Task>();
task.edit((task) => {
task.set("text", text);
task.set("done", false);
});
projectTasks.edit((projectTasks) => {
projectTasks.push(task.id);
});
},
[projectTasks]
);
return (
<div className="max-w-full w-4xl">
<div className="flex justify-between items-center gap-4 mb-4">
<h1>
{
// This is how we can access properties from the project,
// accounting for the fact that it might not be loaded yet
project?.get("title") ? (
<>
{project.get("title")}{" "}
<span className="text-sm">({project.id})</span>
</>
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)
}
</h1>
<InviteButton list={project} />
</div>
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[40px]">Done</TableHead>
<TableHead>Task</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{
// Here, we iterate over the items of our `ListOfTasks`
// and render a `<TaskRow>` for each.
projectTasks?.map((taskId: CoID<Task>) => (
<TaskRow key={taskId} taskId={taskId} />
))
}
<NewTaskInputRow
createTask={createTask}
disabled={!project}
/>
</TableBody>
</Table>
</div>
);
}
export function TaskRow({ taskId }: { taskId: CoID<Task> }) {
// `<TaskRow/>` uses `useTelepathicState()` as well, to granularly load and
// subscribe to changes for that particular task.
const task = useTelepathicState(taskId);
return (
<TableRow>
<TableCell>
<Checkbox
className="mt-1"
checked={task?.get("done")}
onCheckedChange={(checked) => {
// (the only thing we let the user change is the "done" status)
task?.edit((task) => {
task.set("done", !!checked);
});
}}
/>
</TableCell>
<TableCell>
<div className="flex flex-row justify-between items-center gap-2">
<span className={task?.get("done") ? "line-through" : ""}>
{task?.get("text") || (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)}
</span>
{/* We also use a `<NameBadge/>` helper component to render the name
of the author of the task. We get the author by using the collaboration
feature `whoEdited(key)` on our `Task` CoMap, which returns the accountID
of the last account that changed a given key in the CoMap. */}
<NameBadge accountID={task?.whoEdited("text")} />
</div>
</TableCell>
</TableRow>
);
}
function NewTaskInputRow({
createTask,
disabled,
}: {
createTask: (text: string) => void;
disabled: boolean;
}) {
return (
<TableRow>
<TableCell>
<Checkbox className="mt-1" disabled />
</TableCell>
<TableCell>
<SubmittableInput
onSubmit={(taskText) => createTask(taskText)}
label="Add"
placeholder="New task"
disabled={disabled}
/>
</TableCell>
</TableRow>
);
}

View File

@@ -0,0 +1,177 @@
import { useCallback } from "react";
import { CoID, Queried } from "cojson";
import { useSyncedQuery } from "jazz-react";
import { TodoProject, Task } from "./1_types";
import {
Checkbox,
SubmittableInput,
Skeleton,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "./basicComponents";
import { InviteButton } from "./components/InviteButton";
import uniqolor from "uniqolor";
import { useParams } from "react-router";
/** Walkthrough: Reactively rendering a todo project as a table,
* adding and editing tasks
*
* Here in `<TodoTable/>`, we use `useSyncedQuery()` for the first time,
* in this case to load the CoValue for our `TodoProject` as well as
* the `ListOfTasks` referenced in it.
*/
export function ProjectTodoTable() {
const projectId = useParams<{ projectId: CoID<TodoProject> }>().projectId;
// `useSyncedQuery()` reactively subscribes to updates to a CoValue's
// content - whether we create edits locally, load persisted data, or receive
// sync updates from other devices or participants!
// It also recursively resolves and subsribes to all referenced CoValues.
const project = useSyncedQuery(projectId);
// `createTask` is similar to `createProject` we saw earlier, creating a new CoMap
// for a new task (in the same group as the project), and then
// adding that as an item to the project's list of tasks.
const createTask = useCallback(
(text: string) => {
if (!project?.tasks || !text) return;
const task = project.group.createMap<Task>({
done: false,
text,
});
// project.tasks is immutable, but `append` will create an edit
// that will cause useSyncedQuery to rerender this component
// - here and on other devices!
project.tasks.append(task.id);
},
[project?.tasks, project?.group]
);
return (
<div className="max-w-full w-4xl">
<div className="flex justify-between items-center gap-4 mb-4">
<h1>
{
// This is how we can access properties from the project query,
// accounting for the fact that note everything might be loaded yet
project?.title ? (
<>
{project.title}{" "}
<span className="text-sm">({project.id})</span>
</>
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)
}
</h1>
<InviteButton value={project} />
</div>
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[40px]">Done</TableHead>
<TableHead>Task</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{project?.tasks?.map(
(task) => task && <TaskRow key={task.id} task={task} />
)}
<NewTaskInputRow
createTask={createTask}
disabled={!project}
/>
</TableBody>
</Table>
</div>
);
}
export function TaskRow({ task }: { task: Queried<Task> | undefined }) {
return (
<TableRow>
<TableCell>
<Checkbox
className="mt-1"
checked={task?.done}
onCheckedChange={(checked) => {
// Tick or untick the task
// Task is also immutable, but this will update all queries
// that include this task as a reference
task?.set({ done: !!checked });
}}
/>
</TableCell>
<TableCell>
<div className="flex flex-row justify-between items-center gap-2">
{task?.text ? (
<span className={task?.done ? "line-through" : ""}>
{task.text}
</span>
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)}
{
// Here we see for the first time how we can access edit history
// for a CoValue, and use it to display who created the task.
task?.edits.text?.by?.profile?.name ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={uniqueColoring(task.edits.text.by.id)}
>
{task.edits.text.by.profile.name}
</span>
) : (
<Skeleton className="mt-1 w-[50px] h-[1em] rounded-full" />
)
}
</div>
</TableCell>
</TableRow>
);
}
/** Walkthrough: This is the end of the walkthrough so far! */
function NewTaskInputRow({
createTask,
disabled,
}: {
createTask: (text: string) => void;
disabled: boolean;
}) {
return (
<TableRow>
<TableCell>
<Checkbox className="mt-1" disabled />
</TableCell>
<TableCell>
<SubmittableInput
onSubmit={(taskText) => createTask(taskText)}
label="Add"
placeholder="New task"
disabled={disabled}
/>
</TableCell>
</TableRow>
);
}
function uniqueColoring(seed: string) {
const darkMode = window.matchMedia("(prefers-color-scheme: dark)").matches;
return {
color: uniqolor(seed, { lightness: darkMode ? 80 : 20 }).color,
background: uniqolor(seed, { lightness: darkMode ? 20 : 80 }).color,
};
}

View File

@@ -1,27 +1,26 @@
import { useState } from "react";
import { TodoProject } from "../1_types";
import { createInviteLink } from "jazz-react";
import QRCode from "qrcode";
import { useToast, Button } from "../basicComponents";
import { CoValue, Queried } from "cojson";
export function InviteButton({ list }: { list?: TodoProject }) {
export function InviteButton<T extends CoValue>({ value }: { value: T | Queried<T> | undefined }) {
const [existingInviteLink, setExistingInviteLink] = useState<string>();
const { toast } = useToast();
return (
list?.group.myRole() === "admin" && (
value?.group?.myRole() === "admin" && (
<Button
size="sm"
className="py-0"
disabled={!list}
disabled={!value.group || !value.id}
variant="outline"
onClick={async () => {
let inviteLink = existingInviteLink;
if (list && !inviteLink) {
inviteLink = createInviteLink(list, "writer");
if (value.group && value.id && !inviteLink) {
inviteLink = createInviteLink(value, "writer");
setExistingInviteLink(inviteLink);
}
if (inviteLink) {

View File

@@ -1,46 +0,0 @@
import { AccountID } from "cojson";
import { useProfile } from "jazz-react";
import { Skeleton } from "@/basicComponents";
import uniqolor from "uniqolor";
/** Walkthrough: Getting user profiles in `<NameBadge/>`
*
* `<NameBadge/>` uses `useProfile(accountID)`, which is a shorthand for
* useTelepathicState on an account's profile.
*
* Profiles are always a `CoMap<{name: string}>`, but they might have app-specific
* additional properties).
*
* In our case, we just display the profile name (which is set by the LocalAuth
* provider when we first create an account).
*/
export function NameBadge({ accountID }: { accountID?: AccountID }) {
const profile = useProfile(accountID);
return accountID && profile?.get("name") ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={randomUserColor(accountID)}
>
{profile.get("name")}
</span>
) : (
<Skeleton className="mt-1 w-[50px] h-[1em] rounded-full" />
);
}
function randomUserColor(accountID: AccountID) {
const theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
const brightColor = uniqolor(accountID || "", { lightness: 80 }).color;
const darkColor = uniqolor(accountID || "", { lightness: 20 }).color;
return {
color: theme == "light" ? darkColor : brightColor,
background: theme == "light" ? brightColor : darkColor,
};
}

View File

@@ -1,37 +0,0 @@
import { useCallback, useEffect, useState } from "react";
import { CoID, LocalNode, CoValueImpl } from "cojson";
import { consumeInviteLinkFromWindowLocation } from "jazz-react";
export function useSimpleHashRouterThatAcceptsInvites<C extends CoValueImpl>(
localNode: LocalNode
) {
const [currentValueId, setCurrentValueId] = useState<CoID<C>>();
useEffect(() => {
const listener = async () => {
const acceptedInvitation = await consumeInviteLinkFromWindowLocation<C>(localNode);
if (acceptedInvitation) {
setCurrentValueId(acceptedInvitation.valueID);
window.location.hash = acceptedInvitation.valueID;
return;
}
setCurrentValueId(
(window.location.hash.slice(1) as CoID<C>) || undefined
);
};
window.addEventListener("hashchange", listener);
listener();
return () => {
window.removeEventListener("hashchange", listener);
};
}, [localNode]);
const navigateToValue = useCallback((id: CoID<C> | undefined) => {
window.location.hash = id || "";
}, []);
return [currentValueId, navigateToValue] as const;
}

View File

@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}

24
examples/twit/.gitignore vendored Normal file
View File

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

11
examples/twit/.prettierrc Normal file
View File

@@ -0,0 +1,11 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "avoid"
}

4
examples/twit/Dockerfile Normal file
View File

@@ -0,0 +1,4 @@
FROM caddy:2.7.3-alpine
LABEL org.opencontainers.image.source="https://github.com/gardencmp/jazz"
COPY ./dist /usr/share/caddy/

64
examples/twit/README.md Normal file
View File

@@ -0,0 +1,64 @@
# Jazz Todo List Example
Live version: https://example-todo.jazz.tools
## Installing & running the example locally
Start by checking out just the example app to a folder:
```bash
npx degit gardencmp/jazz/examples/todo jazz-example-todo
cd jazz-example-todo
```
(This ensures that you have the example app without git history or our multi-package monorepo)
Install dependencies:
```bash
npm install
```
Start the dev server:
```bash
npm run dev
```
## Structure
- [`src/basicComponents`](./src/basicComponents): simple components to build the UI, unrelated to Jazz (uses [shadcn/ui](https://ui.shadcn.com))
- [`src/components`](./src/components/): helper components that do contain Jazz-specific logic, but aren't very relevant to understand the basics of Jazz and CoJSON
- [`src/1_types.ts`](./src/1_types.ts),
[`src/2_main.tsx`](./src/2_main.tsx),
[`src/3_NewProjectForm.tsx`](./src/3_NewProjectForm.tsx),
[`src/4_ProjectTodoTable.tsx`](./src/4_ProjectTodoTable.tsx): the main files for this example, see the walkthrough below
## Walkthrough
### Main parts
1. Defining the data model with CoJSON: [`src/1_types.ts`](./src/1_types.ts)
2. The top-level provider `<WithJazz/>` and routing: [`src/2_main.tsx`](./src/2_main.tsx)
3. Creating a new todo project: [`src/3_NewProjectForm.tsx`](./src/3_NewProjectForm.tsx)
4. Reactively rendering a todo project as a table, adding and editing tasks: [`src/4_ProjectTodoTable.tsx`](./src/4_ProjectTodoTable.tsx)
### Helpers
- (not yet explained) Creating invite links/QR codes with `<InviteButton/>`: [`src/components/InviteButton.tsx`](./src/components/InviteButton.tsx)
This is the whole Todo List app!
## Questions / problems / feedback
If you have feedback, let us know on [Discord](https://discord.gg/utDMjHYg42) or open an issue or PR to fix something that seems wrong.
## Configuration: sync server
By default, the example app uses [Jazz Global Mesh](https://jazz.tools/mesh) (`wss://sync.jazz.tools`) - so cross-device use, invites and collaboration should just work.
You can also run a local sync server by running `npx cojson-simple-sync` and adding the query param `?sync=ws://localhost:4200` to the URL of the example app (for example: `http://localhost:5173/?sync=ws://localhost:4200`), or by setting the `sync` parameter of the `<WithJazz>` provider component in [./src/2_main.tsx](./src/2_main.tsx).

View File

@@ -0,0 +1,16 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "default",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.js",
"css": "src/index.css",
"baseColor": "stone",
"cssVariables": true
},
"aliases": {
"components": "@/basicComponents",
"utils": "@/basicComponents/lib/utils"
}
}

13
examples/twit/index.html Normal file
View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/jazz-logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Jazz Todo List Example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/2_main.tsx"></script>
</body>
</html>

View File

@@ -0,0 +1,56 @@
job "twit$BRANCH_SUFFIX" {
region = "global"
datacenters = ["*"]
group "static" {
count = 8
network {
port "http" {
to = 80
}
}
constraint {
attribute = "${node.class}"
operator = "="
value = "mesh"
}
spread {
attribute = "${node.datacenter}"
weight = 100
}
constraint {
distinct_hosts = true
}
task "server" {
driver = "docker"
config {
image = "$DOCKER_TAG"
ports = ["http"]
auth = {
username = "$DOCKER_USER"
password = "$DOCKER_PASSWORD"
}
}
service {
tags = ["public"]
name = "twit$BRANCH_SUFFIX"
port = "http"
provider = "consul"
}
resources {
cpu = 50 # MHz
memory = 50 # MB
}
}
}
}
# deploy bump 4

View File

@@ -0,0 +1,50 @@
{
"name": "jazz-example-twit",
"private": true,
"version": "0.0.3",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-popover": "^1.0.7",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.4",
"@types/qrcode": "^1.5.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.0.0",
"javascript-time-ago": "^2.5.9",
"jazz-browser-media-images": "^0.4.1",
"jazz-react": "^0.4.1",
"jazz-react-auth-local": "^0.4.1",
"lucide-react": "^0.274.0",
"qrcode": "^1.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router": "^6.16.0",
"react-router-dom": "^6.16.0",
"react-time-ago": "^7.2.1",
"tailwind-merge": "^1.14.0",
"tailwindcss-animate": "^1.0.7",
"uniqolor": "^1.1.0"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"@vitejs/plugin-react-swc": "^3.3.2",
"autoprefixer": "^10.4.14",
"eslint": "^8.45.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"postcss": "^8.4.27",
"tailwindcss": "^3.3.3",
"typescript": "^5.0.2",
"vite": "^4.4.5"
}
}

View File

@@ -0,0 +1,6 @@
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

View File

@@ -0,0 +1,64 @@
import { CoMap, CoList, Media, CoStream, Group, AccountMigration, EVERYONE, Profile } from 'cojson';
export type Twit = CoMap<{
text?: string;
images?: ListOfImages['id'];
likes: LikeStream['id'];
replies: ReplyStream['id'];
isReplyTo?: Twit['id'];
}>;
export type ListOfImages = CoList<Media.ImageDefinition['id']>;
export type LikeStream = CoStream<'❤️' | null>;
export type ReplyStream = CoStream<Twit['id']>;
export type ListOfTwits = CoList<Twit['id']>;
export type ListOfProfiles = CoList<TwitProfile['id']>;
export type StreamOfFollowers = CoStream<TwitProfile['id'] | null>;
export type TwitProfile = Profile<
{
name: string;
bio: string;
avatar?: Media.ImageDefinition['id'];
twits: ListOfTwits['id'];
following: ListOfProfiles['id'];
followers: StreamOfFollowers['id'];
twitStyle?: { fontFamily: string; color: string };
}
>;
export type TwitAccountRoot = CoMap<{
peopleWhoCanSeeMyTwits: Group['id'];
peopleWhoCanSeeMyFollows: Group['id'];
peopleWhoCanFollowMe: Group['id'];
peopleWhoCanInteractWithMe: Group['id'];
}>;
export const migration: AccountMigration<TwitProfile, TwitAccountRoot> = (account, profile) => {
if (!account.get('root')) {
const peopleWhoCanSeeMyTwits = account.createGroup();
const peopleWhoCanSeeMyFollows = account.createGroup();
const peopleWhoCanFollowMe = account.createGroup();
const peopleWhoCanInteractWithMe = account.createGroup();
peopleWhoCanFollowMe?.addMember(EVERYONE, 'writer');
peopleWhoCanSeeMyTwits?.addMember(EVERYONE, 'reader');
peopleWhoCanSeeMyFollows?.addMember(EVERYONE, 'reader');
peopleWhoCanInteractWithMe?.addMember(EVERYONE, 'writer');
const root = account.createMap<TwitAccountRoot>({
peopleWhoCanSeeMyTwits: peopleWhoCanSeeMyTwits.id,
peopleWhoCanSeeMyFollows: peopleWhoCanSeeMyFollows.id,
peopleWhoCanFollowMe: peopleWhoCanFollowMe.id,
peopleWhoCanInteractWithMe: peopleWhoCanInteractWithMe.id
});
account.set('root', root.id);
profile.set('twits', peopleWhoCanSeeMyTwits.createList<ListOfTwits>().id, 'trusting');
profile.set('following', peopleWhoCanSeeMyFollows.createList<ListOfProfiles>().id, 'trusting');
profile.set('followers', peopleWhoCanFollowMe.createStream<StreamOfFollowers>().id, 'trusting');
console.log('MIGRATION SUCCESSFUL!');
}
};

View File

@@ -0,0 +1,71 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { RouterProvider, createHashRouter } from 'react-router-dom';
import './index.css';
import { AccountMigration } from 'cojson';
import { WithJazz, useJazz } from 'jazz-react';
import { LocalAuth } from 'jazz-react-auth-local';
import { Button, ThemeProvider, TitleAndLogo } from './basicComponents/index.tsx';
import { PrettyAuthUI } from './components/Auth.tsx';
import { migration } from './1_dataModel.ts';
import { ChronoFeed } from './3_ChronoFeed.tsx';
import { ProfilePage } from './5_ProfilePage.tsx';
const appName = 'Jazz Twit Example';
const auth = LocalAuth({
appName,
Component: PrettyAuthUI
});
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ThemeProvider>
<TitleAndLogo name={appName} />
<div className="flex flex-col h-full items-stretch justify-start gap-10 pt-10 pb-10 px-5 w-full max-w-xl mx-auto">
<WithJazz auth={auth} migration={migration as AccountMigration}>
<App />
</WithJazz>
</div>
</ThemeProvider>
</React.StrictMode>
);
function App() {
const { me, logOut } = useJazz();
const router = createHashRouter([
{
path: '/',
element: <ChronoFeed />
},
{
path: '/:profileId',
element: <ProfilePage />
},
{
path: '/me',
loader: () => router.navigate('/' + me.profile?.id)
}
]);
return (
<>
<div className="flex gap-2">
<Button onClick={() => router.navigate('/')} variant="link" className="-ml-3">
Home
</Button>
<Button onClick={() => router.navigate('/me')} variant="link" className="ml-auto">
My Profile
</Button>
<Button onClick={() => router.navigate('/').then(logOut)} variant="outline">
Log Out
</Button>
</div>
<RouterProvider router={router} />
</>
);
}

View File

@@ -0,0 +1,35 @@
import { useMemo } from 'react';
import { useJazz } from 'jazz-react';
import { TwitAccountRoot, TwitProfile } from './1_dataModel.ts';
import { CreateTwitForm } from './6_CreateTwitForm.tsx';
import { TwitComponent } from './4_TwitComponent.tsx';
import { MainH1 } from './basicComponents/index.tsx';
export function ChronoFeed() {
const { me } = useJazz<TwitProfile, TwitAccountRoot>();
const myTwits = me.profile?.twits;
const twitsFromFollows = useMemo(
() => me.profile?.following?.flatMap(follow => follow?.twits || []) || [],
[me.profile?.following]
);
const allTwitsSorted = useMemo(
() =>
[...(myTwits || []), ...twitsFromFollows]
.flatMap(tw => (tw ? (tw.isReplyTo ? [] : tw) : []))
.sort((a, b) => (b.edits.text?.at?.getTime() || 0) - (a.edits.text?.at?.getTime() || 0)),
[myTwits, twitsFromFollows]
);
return (
<div className="flex flex-col items-stretch">
<CreateTwitForm className="mb-10" />
<MainH1>From people you follow</MainH1>
{allTwitsSorted?.map(twit => (
<TwitComponent twit={twit} key={twit.id} />
))}
</div>
);
}

View File

@@ -0,0 +1,101 @@
import React from 'react';
import { Link } from 'react-router-dom';
import {
ButtonWithCount,
ProfilePicImg,
ReactionsContainer,
RepliesContainer,
SubtleRelativeTimeAgo,
TwitContainer,
TwitWithRepliesContainer,
TwitImg,
TwitImgGallery,
TwitHeader,
TwitBody,
TwitText,
} from './basicComponents/index.tsx';
import { Twit, TwitProfile } from './1_dataModel.ts';
import { Queried } from 'cojson';
import { BrowserImage } from 'jazz-browser-media-images';
import { HeartIcon, MessagesSquareIcon } from 'lucide-react';
import { CreateTwitForm } from './6_CreateTwitForm.tsx';
export function TwitComponent({
twit,
alreadyInReplies: alreadyInReplies
}: {
twit?: Queried<Twit>;
alreadyInReplies?: boolean;
}) {
const [showReplyForm, setShowReplyForm] = React.useState(false);
const posterProfile = twit?.edits.text?.by?.profile as Queried<TwitProfile> | undefined;
const isTopLevel = !twit?.isReplyTo || alreadyInReplies;
return (
<TwitWithRepliesContainer isTopLevel={isTopLevel}>
<TwitContainer>
<ProfilePicImg
src={posterProfile?.avatar?.as(BrowserImage)?.highestResSrcOrPlaceholder}
linkTo={'/' + posterProfile?.id}
initial={posterProfile?.name[0]}
size={twit?.isReplyTo && "sm"}
/>
<TwitBody>
<TwitHeader>
<Link to={'/' + posterProfile?.id} className="font-bold hover:underline">
{posterProfile?.name}
</Link>
<SubtleRelativeTimeAgo dateTime={twit?.edits.text?.at} />
</TwitHeader>
<TwitText style={posterProfile?.twitStyle}>
{/* This is where the tweet text goes */}
{twit?.text}
</TwitText>
{twit?.images && (
<TwitImgGallery>
{twit.images.map(image => (
<TwitImg src={image?.as(BrowserImage)?.highestResSrcOrPlaceholder} key={image?.id} />
))}
</TwitImgGallery>
)}
<ReactionsContainer>
<ButtonWithCount
active={twit?.likes?.me?.last === '❤️'}
onClick={() => twit?.likes?.push(twit?.likes?.me?.last ? null : '❤️')}
count={twit?.likes?.perAccount.filter(([, liked]) => liked.last === '❤️').length || 0}
icon={<HeartIcon size="18" />}
activeIcon={<HeartIcon color="red" size="18" fill="red" />}
/>
<ButtonWithCount
onClick={() => setShowReplyForm(s => !s)}
count={twit?.replies?.perAccount.flatMap(([, byAccount]) => byAccount.all).length || 0}
icon={<MessagesSquareIcon size="18" />}
/>
</ReactionsContainer>
</TwitBody>
</TwitContainer>
<RepliesContainer>
{showReplyForm && (
<CreateTwitForm
inReplyTo={twit}
onSubmit={() => setShowReplyForm(false)}
className={'mt-5 ' + (isTopLevel ? 'ml-14' : 'ml-12')}
/>
)}
{twit?.replies?.perAccount
.flatMap(([, byAccount]) => byAccount.all)
.sort((a, b) => b.at.getTime() - a.at.getTime())
.map(replyEntry => (
<TwitComponent twit={replyEntry.value} key={replyEntry.value?.id} alreadyInReplies={!!twit?.isReplyTo} />
))}
</RepliesContainer>
</TwitWithRepliesContainer>
);
}

View File

@@ -0,0 +1,127 @@
import { useEffect, useMemo, useState } from 'react';
import { useParams } from 'react-router-dom';
import { useJazz, useSyncedQuery } from 'jazz-react';
import QRCode from 'qrcode';
import {
BioInput,
ChooseProfilePicInput,
FollowerStatsContainer,
Popover,
ProfileName,
ProfilePicImg,
ProfileTitleContainer,
SmallInlineButton
} from './basicComponents/index.tsx';
import { TwitAccountRoot, TwitProfile } from './1_dataModel.ts';
import { CoID } from 'cojson';
import { BrowserImage, createImage } from 'jazz-browser-media-images';
import { FollowButton, FollowerList, FollowingList } from './7_FollowStuff.tsx';
import { CreateTwitForm } from './6_CreateTwitForm.tsx';
import { TwitComponent } from './4_TwitComponent.tsx';
import { PopoverContent, PopoverTrigger } from '@radix-ui/react-popover';
export function ProfilePage() {
const { profileId } = useParams<{ profileId: CoID<TwitProfile> }>();
const { me } = useJazz<TwitProfile, TwitAccountRoot>();
const profile = useSyncedQuery(profileId);
const isMe = profile?.id == me.profile?.id;
const profileTwitsAndRepliedToTwits = useMemo(() => {
return profile?.twits?.map((twit, _, allTwits) =>
twit?.isReplyTo
? allTwits.some(
tw =>
tw?.id === twit?.isReplyTo?.id ||
tw?.id === twit?.isReplyTo?.isReplyTo?.id ||
tw?.id === twit?.isReplyTo?.isReplyTo?.isReplyTo?.id
)
? null
: twit?.isReplyTo
: twit
);
}, [profile?.twits]);
const [qr, setQr] = useState<string>('');
useEffect(() => {
QRCode.toDataURL(
window.location.protocol + '//' + window.location.host + window.location.pathname + '#/' + profile?.id,
{
errorCorrectionLevel: 'L'
}
).then(setQr);
}, [profile?.id]);
return (
<div>
<div className="py-2 mb-5 flex gap-4">
<div className="flex flex-col items-stretch">
<ProfilePicImg
src={profile?.avatar?.as(BrowserImage)?.highestResSrcOrPlaceholder}
initial={profile?.name[0]}
size="xxl"
/>
{isMe && (
<ChooseProfilePicInput
onChange={(file: File) =>
me.root?.peopleWhoCanSeeMyTwits &&
createImage(file, me.root.peopleWhoCanSeeMyTwits.group, 256).then(image => {
me.profile?.set({ avatar: image.id }, 'trusting');
})
}
/>
)}
</div>
<div className="grow">
<ProfileTitleContainer>
<ProfileName>{profile?.name}</ProfileName>
{!isMe && <FollowButton profile={profile} />}
</ProfileTitleContainer>
<div>
{isMe ? (
<BioInput
value={profile?.bio}
onChange={newBio => {
profile?.set({ bio: newBio }, 'trusting');
// prettier-ignore
if (newBio.startsWith('{')) { profile?.set('twitStyle', JSON.parse(newBio), 'trusting'); } else { profile?.set('twitStyle', undefined, 'trusting'); }
}}
/>
) : (
profile?.bio || '(No bio)'
)}
</div>
<FollowerStatsContainer>
<Popover>
<PopoverTrigger>
<SmallInlineButton>
{profile?.followers?.perAccount?.filter(([, status]) => status.last).length} Followers
</SmallInlineButton>
</PopoverTrigger>
<PopoverContent>
<FollowerList profile={profile} />
</PopoverContent>
</Popover>
<span className="hidden md:block">&mdash;</span> <br className="md:hidden" />
<Popover>
<PopoverTrigger>
<SmallInlineButton>{new Set(profile?.following || []).size} Following</SmallInlineButton>
</PopoverTrigger>
<PopoverContent>
<FollowingList profile={profile} />
</PopoverContent>
</Popover>
</FollowerStatsContainer>
</div>
{isMe && <img src={qr} className="rounded w-28 h-28 -mr-3 dark:invert max-sm:w-16 max-sm:h-16" />}
</div>
{isMe && <CreateTwitForm className="mb-4" />}
{profileTwitsAndRepliedToTwits?.map(twit => twit && <TwitComponent twit={twit} key={twit?.id} />)}
</div>
);
}

View File

@@ -0,0 +1,74 @@
import React, { useCallback, useEffect } from 'react';
import { useJazz } from 'jazz-react';
import { AddTwitPicsInput, TwitImg, TwitTextInput } from './basicComponents/index.tsx';
import { LikeStream, ListOfImages, ReplyStream, Twit, TwitAccountRoot, TwitProfile } from './1_dataModel.ts';
import { Queried } from 'cojson';
import { createImage } from 'jazz-browser-media-images';
export function CreateTwitForm(
props: {
inReplyTo?: Queried<Twit>;
onSubmit?: () => void;
className?: string;
} = {}
) {
const { me } = useJazz<TwitProfile, TwitAccountRoot>();
const [pics, setPics] = React.useState<File[]>([]);
const onSubmit = useCallback(
(twitText: string) => {
const audience = me.root?.peopleWhoCanSeeMyTwits;
const interactors = me.root?.peopleWhoCanInteractWithMe;
if (!audience || !interactors) return;
const twit = audience.createMap<Twit>({
text: twitText,
likes: interactors.createStream<LikeStream>().id,
replies: interactors.createStream<ReplyStream>().id
});
me.profile?.twits?.prepend(twit?.id as Twit['id']);
if (props.inReplyTo) {
props.inReplyTo.replies?.push(twit.id);
twit.set({ isReplyTo: props.inReplyTo.id });
}
Promise.all(pics.map(pic => createImage(pic, twit.group, 1024))).then(createdPics => {
twit.set({ images: audience.createList<ListOfImages>(createdPics.map(pic => pic.id)).id });
});
setPics([]);
props.onSubmit?.();
},
[me.profile?.twits, me.root?.peopleWhoCanSeeMyTwits, me.root?.peopleWhoCanInteractWithMe, props, pics]
);
const [picPreviews, setPicPreviews] = React.useState<string[]>([]);
useEffect(() => {
const previews = pics.map(pic => URL.createObjectURL(pic));
setPicPreviews(previews);
return () => previews.forEach(preview => URL.revokeObjectURL(preview));
}, [pics]);
return (
<div className={props.className}>
<TwitTextInput onSubmit={onSubmit} submitButtonLabel={props.inReplyTo ? 'Reply!' : 'Twit!'} />
{picPreviews.length ? (
<div className="flex gap-2 mt-2">
{picPreviews.map(preview => (
<TwitImg src={preview} />
))}
</div>
) : (
<AddTwitPicsInput
onChange={(newPics: File[]) => {
setPics(newPics);
}}
/>
)}
</div>
);
}

View File

@@ -0,0 +1,81 @@
import { useCallback } from 'react';
import { useJazz } from 'jazz-react';
import { Button, ProfilePicImg } from './basicComponents/index.tsx';
import { TwitAccountRoot, TwitProfile } from './1_dataModel.ts';
import { Queried } from 'cojson';
import { Link } from 'react-router-dom';
import { BrowserImage } from 'jazz-browser-media-images';
export function FollowButton({ profile }: { profile?: Queried<TwitProfile> }) {
const { me } = useJazz<TwitProfile, TwitAccountRoot>();
const alreadyFollowing = profile?.followers?.perAccount?.some(([acc, status]) => acc === me.id && !!status.last);
const theyFollowMe = profile?.following?.some(f => f?.id === me.profile?.id);
const followOrUnfollow = useCallback(() => {
if (!profile?.followers || !me.profile?.following) return;
if (alreadyFollowing) {
me.profile.following.delete(me.profile.following.findIndex(f => f?.id === profile.id));
profile.followers.push(null);
} else {
me.profile.following.append(profile.id);
profile.followers.push(me.profile.id);
}
}, [alreadyFollowing, me.profile, profile]);
return profile?.id === me.profile?.id ? (
<div className="ml-auto text-neutral-500">That's you!</div>
) : (
<Button onClick={followOrUnfollow} className="ml-auto" variant={alreadyFollowing ? 'ghost' : 'default'}>
{alreadyFollowing ? 'Unfollow' : theyFollowMe ? 'Follow Back' : 'Follow'}
</Button>
);
}
export function FollowerList({ profile }: { profile?: Queried<TwitProfile> }) {
return (
<div className="flex flex-col gap-4 p-4 bg-background rounded-lg border shadow-lg w-96 max-w-full m-2">
{profile?.followers?.perAccount.map(([, followEntry]) => {
const followerProfile = followEntry.last;
// not following anymore?
if (!followerProfile) return null;
return (
<div key={followerProfile.id} className="flex items-center">
<ProfilePicImg
src={followerProfile?.avatar?.as(BrowserImage)?.highestResSrcOrPlaceholder}
linkTo={'/' + followerProfile?.id}
initial={followerProfile?.name[0]}
/>
<Link to={'/' + followerProfile?.id} className="font-bold hover:underline">
{followerProfile?.name}
</Link>
<FollowButton profile={followerProfile} />
</div>
);
})}
</div>
);
}
export function FollowingList({ profile }: { profile?: Queried<TwitProfile> }) {
return (
<div className="flex flex-col gap-4 p-4 bg-background rounded-lg border shadow-lg w-96 max-w-full m-2">
{[...new Set(profile?.following || [])].map(followingProfile => {
return (
<div key={followingProfile?.id} className="flex items-center">
<ProfilePicImg
src={followingProfile?.avatar?.as(BrowserImage)?.highestResSrcOrPlaceholder}
linkTo={'/' + followingProfile?.id}
initial={followingProfile?.name[0]}
/>
<Link to={'/' + followingProfile?.id} className="font-bold hover:underline">
{followingProfile?.name}
</Link>
<FollowButton profile={followingProfile} />
</div>
);
})}
</div>
);
}

View File

@@ -0,0 +1,39 @@
import { Input } from "@/basicComponents/ui/input";
import { Button } from "@/basicComponents/ui/button";
export function SubmittableInput({
onSubmit,
label,
placeholder,
disabled,
}: {
onSubmit: (text: string) => void;
label: string;
placeholder: string;
disabled?: boolean;
}) {
return (
<form
className="flex flex-row items-center gap-3"
onSubmit={(e) => {
e.preventDefault();
const textEl = e.currentTarget.elements.namedItem(
"text"
) as HTMLInputElement;
onSubmit(textEl.value);
textEl.value = "";
}}
>
<Input
className="-ml-3 -my-2 flex-grow flex-3 text-base"
name="text"
placeholder={placeholder}
autoComplete="off"
disabled={disabled}
/>
<Button asChild type="submit" className="flex-shrink flex-1 cursor-pointer">
<Input type="submit" value={label} disabled={disabled} />
</Button>
</form>
);
}

View File

@@ -0,0 +1,10 @@
import { Toaster } from ".";
export function TitleAndLogo({name}: {name: string}) {
return <>
<div className="flex items-center gap-2 justify-center mt-5">
<img src="jazz-logo.png" className="h-5" /> {name}
</div>
<Toaster />
</>
}

View File

@@ -0,0 +1,228 @@
import ReactTimeAgo from 'react-time-ago';
import { Button, ButtonProps } from './ui/button';
export { Button } from './ui/button';
export { Checkbox } from './ui/checkbox';
import { Input } from './ui/input';
import { Link } from 'react-router-dom';
export { Input } from './ui/input';
export { Skeleton } from './ui/skeleton';
export { Toaster } from './ui/toaster';
export { useToast } from './ui/use-toast';
export { SubmittableInput } from './SubmittableInput';
export { TitleAndLogo } from './TitleAndLogo';
export { ThemeProvider } from './themeProvider';
export { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from './ui/table';
export { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from './ui/card';
export { Popover, PopoverContent, PopoverTrigger } from './ui/popover';
import TimeAgo from 'javascript-time-ago';
import en from 'javascript-time-ago/locale/en.json';
TimeAgo.addDefaultLocale(en);
export function BioInput(props: { value?: string; onChange: (value: string) => void }) {
return (
<Input
type="text"
value={props.value}
autoComplete="off"
onChange={e => {
props.onChange(e.target.value);
}}
placeholder="Add a bio..."
className="w-full p-2 border rounded max-md:text-base"
/>
);
}
export function ProfileTitleContainer(props: { children: React.ReactNode }) {
return <div className="flex items-baseline">{props.children}</div>;
}
export function ProfileName(props: { children: React.ReactNode }) {
return <h1 className="text-2xl">{props.children}</h1>;
}
export function FollowerStatsContainer(props: { children: React.ReactNode }) {
return <div className="flex gap-2 mt-2 text-neutral-500">{props.children}</div>;
}
export function ChooseProfilePicInput(props: { onChange: (file: File) => void }) {
return (
<Button asChild className="mt-2" size="sm" variant="secondary">
<label className="cursor-pointer text-xs">
Choose Pic
<Input
type="file"
accept="image/*"
onChange={e => {
e.target.files?.[0] && props.onChange(e.target.files[0]);
e.target.value = '';
}}
className="hidden"
/>
</label>
</Button>
);
}
export function ProfilePicImg(props: { src?: string; size?: 'sm' | 'xxl'; linkTo?: string; initial?: string }) {
return (
<Link to={props.linkTo || ''}>
{props.src ? (
<img
src={props.src}
className={
'bg-neutral-200 rounded-full mr-2 object-cover shrink-0' +
(props.size === 'sm' ? ' w-8 h-8' : props.size === 'xxl' ? ' w-20 h-20' : ' w-10 h-10')
}
/>
) : (
<div
className={
'bg-neutral-200 rounded-full mr-2 object-cover shrink-0 flex items-center justify-center text-neutral-700 ' +
(props.size === 'sm'
? ' w-8 h-8 text-[1.5rem]'
: props.size === 'xxl'
? ' w-20 h-20 text-[3.75rem]'
: ' w-10 h-10 text-[1.875rem]')
}
>
<div className="-mt-[8%]">{props.initial}</div>
</div>
)}
</Link>
);
}
export function SubtleRelativeTimeAgo(props: { dateTime?: Date }) {
return (
<div className="ml-auto text-neutral-300 text-xs whitespace-nowrap">
<ReactTimeAgo date={props.dateTime || 0} />
</div>
);
}
export function TwitImg(props: { src?: string }) {
return <img src={props.src} className="h-40 rounded object-cover" />;
}
export function ReactionsContainer(props: { children: React.ReactNode }) {
return <div className="flex gap-4 mt-2">{props.children}</div>;
}
export function RepliesContainer(props: { children: React.ReactNode }) {
return <div className="flex flex-col items-stretch gap-2 mt-2">{props.children}</div>;
}
export function ButtonWithCount(props: {
count: number;
onClick: () => void;
active?: boolean;
icon: React.ReactNode;
activeIcon?: React.ReactNode;
}) {
return (
<div className="flex items-center">
<Button
className="w-10 h-7 p-1 mr-1"
variant={props.active ? 'secondary' : 'outline'}
onClick={props.onClick}
size="icon"
>
{props.active ? props.activeIcon : props.icon}
</Button>{' '}
<span className="tabular-nums">{props.count}</span>
</div>
);
}
export function TwitTextInput(props: { onSubmit: (text: string) => void; submitButtonLabel: string }) {
return (
<form
onSubmit={event => {
event.preventDefault();
const form = event.target as HTMLFormElement;
const text = form.twitText.value;
text && props.onSubmit(text);
form.twitText.value = '';
}}
className="flex gap-2 items-end"
>
<Input
type="text"
name="twitText"
placeholder="What's happenin'"
autoComplete="off"
className="p-2 border rounded grow max-md:text-base"
/>
<Button asChild>
<input type="submit" value={props.submitButtonLabel} />
</Button>
</form>
);
}
export function AddTwitPicsInput(props: { onChange: (files: File[]) => void }) {
return (
<Button asChild className="mt-2" size="sm" variant="secondary">
<label className="cursor-pointer text-xs">
Add Pics
<Input
type="file"
onChange={e => {
props.onChange(Array.from(e.target.files || []));
}}
className="hidden"
accept="image/*"
multiple
/>
</label>
</Button>
);
}
export function TwitWithRepliesContainer(props: { children: React.ReactNode; isTopLevel?: boolean }) {
return (
<div className={'py-2 flex flex-col items-stretch' + (props.isTopLevel ? ' border-t' : ' ml-14')}>
{props.children}
</div>
);
}
export function TwitContainer(props: { children: React.ReactNode }) {
return <div className="flex gap-2">{props.children}</div>;
}
export function TwitBody(props: { children: React.ReactNode }) {
return <div className="grow flex flex-col items-stretch">{props.children}</div>;
}
export function TwitHeader(props: { children: React.ReactNode }) {
return <div className="flex items-baseline">{props.children}</div>;
}
export function TwitImgGallery(props: { children: React.ReactNode }) {
return <div className="flex gap-2 mt-2 max-w-full overflow-auto">{props.children}</div>;
}
export function TwitText(props: { children: React.ReactNode; style?: React.CSSProperties }) {
return <div style={props.style}>{props.children}</div>;
}
export function QuoteContainer(props: { children: React.ReactNode }) {
return <div className="border rounded">{props.children}</div>;
}
export function MainH1(props: { children: React.ReactNode }) {
return <h1 className="text-2xl mb-4">{props.children}</h1>;
}
export function SmallInlineButton(props: { children: React.ReactNode } & ButtonProps) {
const {children, ...rest} = props
return (
<Button variant={'ghost'} className="h-6 px-1 -mx-1" {...rest}>
{children}
</Button>
);
}

View File

@@ -0,0 +1,6 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}

View File

@@ -0,0 +1,72 @@
import { createContext, useContext, useEffect, useState } from "react";
type ThemeProviderProps = {
children: React.ReactNode;
defaultTheme?: string;
storageKey?: string;
};
type ThemeProviderState = {
theme: string;
setTheme: (theme: string) => void;
};
const initialState = {
theme: "system",
setTheme: () => null,
};
const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
export function ThemeProvider({
children,
defaultTheme = "system",
storageKey = "vite-ui-theme",
...props
}: ThemeProviderProps) {
const [theme, setTheme] = useState(
() => localStorage.getItem(storageKey) || defaultTheme
);
useEffect(() => {
const root = window.document.documentElement;
root.classList.remove("light", "dark");
if (theme === "system") {
const systemTheme = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches
? "dark"
: "light";
root.classList.add(systemTheme);
return;
}
root.classList.add(theme);
}, [theme]);
const value = {
theme,
setTheme: (theme: string) => {
localStorage.setItem(storageKey, theme);
setTheme(theme);
},
};
return (
<ThemeProviderContext.Provider {...props} value={value}>
{children}
</ThemeProviderContext.Provider>
);
}
export const useTheme = () => {
const context = useContext(ThemeProviderContext);
if (context === undefined)
throw new Error("useTheme must be used within a ThemeProvider");
return context;
};

View File

@@ -0,0 +1,56 @@
import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/basicComponents/lib/utils"
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground hover:bg-destructive/90",
outline:
"border border-input bg-background hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
lg: "h-11 rounded-md px-8",
icon: "h-10 w-10",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button"
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
)
}
)
Button.displayName = "Button"
export { Button, buttonVariants }

View File

@@ -0,0 +1,79 @@
import * as React from "react"
import { cn } from "@/basicComponents/lib/utils"
const Card = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
"rounded-lg border bg-card text-card-foreground shadow-sm",
className
)}
{...props}
/>
))
Card.displayName = "Card"
const CardHeader = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex flex-col space-y-1.5 p-6", className)}
{...props}
/>
))
CardHeader.displayName = "CardHeader"
const CardTitle = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h3
ref={ref}
className={cn(
"text-2xl font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
CardTitle.displayName = "CardTitle"
const CardDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<p
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
CardDescription.displayName = "CardDescription"
const CardContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("p-6 pt-0", className)} {...props} />
))
CardContent.displayName = "CardContent"
const CardFooter = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("flex items-center p-6 pt-0", className)}
{...props}
/>
))
CardFooter.displayName = "CardFooter"
export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }

View File

@@ -0,0 +1,28 @@
import * as React from "react"
import * as CheckboxPrimitive from "@radix-ui/react-checkbox"
import { Check } from "lucide-react"
import { cn } from "@/basicComponents/lib/utils"
const Checkbox = React.forwardRef<
React.ElementRef<typeof CheckboxPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
>(({ className, ...props }, ref) => (
<CheckboxPrimitive.Root
ref={ref}
className={cn(
"peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground",
className
)}
{...props}
>
<CheckboxPrimitive.Indicator
className={cn("flex items-center justify-center text-current")}
>
<Check className="h-4 w-4" />
</CheckboxPrimitive.Indicator>
</CheckboxPrimitive.Root>
))
Checkbox.displayName = CheckboxPrimitive.Root.displayName
export { Checkbox }

View File

@@ -0,0 +1,25 @@
import * as React from "react"
import { cn } from "@/basicComponents/lib/utils"
export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {}
const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
)
}
)
Input.displayName = "Input"
export { Input }

View File

@@ -0,0 +1,29 @@
import * as React from "react"
import * as PopoverPrimitive from "@radix-ui/react-popover"
import { cn } from "@/basicComponents/lib/utils"
const Popover = PopoverPrimitive.Root
const PopoverTrigger = PopoverPrimitive.Trigger
const PopoverContent = React.forwardRef<
React.ElementRef<typeof PopoverPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof PopoverPrimitive.Content>
>(({ className, align = "center", sideOffset = 4, ...props }, ref) => (
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
ref={ref}
align={align}
sideOffset={sideOffset}
className={cn(
"z-50 w-72 rounded-md border bg-popover p-4 text-popover-foreground shadow-md outline-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",
className
)}
{...props}
/>
</PopoverPrimitive.Portal>
))
PopoverContent.displayName = PopoverPrimitive.Content.displayName
export { Popover, PopoverTrigger, PopoverContent }

View File

@@ -0,0 +1,15 @@
import { cn } from "@/basicComponents/lib/utils"
function Skeleton({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) {
return (
<div
className={cn("animate-pulse rounded-md bg-muted", className)}
{...props}
/>
)
}
export { Skeleton }

View File

@@ -0,0 +1,114 @@
import * as React from "react"
import { cn } from "@/basicComponents/lib/utils"
const Table = React.forwardRef<
HTMLTableElement,
React.HTMLAttributes<HTMLTableElement>
>(({ className, ...props }, ref) => (
<div className="w-full overflow-auto">
<table
ref={ref}
className={cn("w-full caption-bottom text-sm", className)}
{...props}
/>
</div>
))
Table.displayName = "Table"
const TableHeader = React.forwardRef<
HTMLTableSectionElement,
React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
<thead ref={ref} className={cn("[&_tr]:border-b", className)} {...props} />
))
TableHeader.displayName = "TableHeader"
const TableBody = React.forwardRef<
HTMLTableSectionElement,
React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
<tbody
ref={ref}
className={cn("[&_tr:last-child]:border-0", className)}
{...props}
/>
))
TableBody.displayName = "TableBody"
const TableFooter = React.forwardRef<
HTMLTableSectionElement,
React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
<tfoot
ref={ref}
className={cn("bg-primary font-medium text-primary-foreground", className)}
{...props}
/>
))
TableFooter.displayName = "TableFooter"
const TableRow = React.forwardRef<
HTMLTableRowElement,
React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => (
<tr
ref={ref}
className={cn(
"border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
className
)}
{...props}
/>
))
TableRow.displayName = "TableRow"
const TableHead = React.forwardRef<
HTMLTableCellElement,
React.ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => (
<th
ref={ref}
className={cn(
"h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0",
className
)}
{...props}
/>
))
TableHead.displayName = "TableHead"
const TableCell = React.forwardRef<
HTMLTableCellElement,
React.TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => (
<td
ref={ref}
className={cn("p-4 align-middle [&:has([role=checkbox])]:pr-0", className)}
{...props}
/>
))
TableCell.displayName = "TableCell"
const TableCaption = React.forwardRef<
HTMLTableCaptionElement,
React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, ref) => (
<caption
ref={ref}
className={cn("mt-4 text-sm text-muted-foreground", className)}
{...props}
/>
))
TableCaption.displayName = "TableCaption"
export {
Table,
TableHeader,
TableBody,
TableFooter,
TableHead,
TableRow,
TableCell,
TableCaption,
}

View File

@@ -0,0 +1,127 @@
import * as React from "react"
import * as ToastPrimitives from "@radix-ui/react-toast"
import { cva, type VariantProps } from "class-variance-authority"
import { X } from "lucide-react"
import { cn } from "@/basicComponents/lib/utils"
const ToastProvider = ToastPrimitives.Provider
const ToastViewport = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Viewport>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Viewport
ref={ref}
className={cn(
"fixed top-0 z-[100] flex max-h-screen w-full flex-col-reverse p-4 sm:bottom-0 sm:right-0 sm:top-auto sm:flex-col md:max-w-[420px]",
className
)}
{...props}
/>
))
ToastViewport.displayName = ToastPrimitives.Viewport.displayName
const toastVariants = cva(
"group pointer-events-auto relative flex w-full items-center justify-between space-x-4 overflow-hidden rounded-md border p-6 pr-8 shadow-lg transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full data-[state=open]:sm:slide-in-from-bottom-full",
{
variants: {
variant: {
default: "border bg-background text-foreground",
destructive:
"destructive group border-destructive bg-destructive text-destructive-foreground",
},
},
defaultVariants: {
variant: "default",
},
}
)
const Toast = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Root>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
VariantProps<typeof toastVariants>
>(({ className, variant, ...props }, ref) => {
return (
<ToastPrimitives.Root
ref={ref}
className={cn(toastVariants({ variant }), className)}
{...props}
/>
)
})
Toast.displayName = ToastPrimitives.Root.displayName
const ToastAction = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Action>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Action
ref={ref}
className={cn(
"inline-flex h-8 shrink-0 items-center justify-center rounded-md border bg-transparent px-3 text-sm font-medium ring-offset-background transition-colors hover:bg-secondary focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 group-[.destructive]:border-muted/40 group-[.destructive]:hover:border-destructive/30 group-[.destructive]:hover:bg-destructive group-[.destructive]:hover:text-destructive-foreground group-[.destructive]:focus:ring-destructive",
className
)}
{...props}
/>
))
ToastAction.displayName = ToastPrimitives.Action.displayName
const ToastClose = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Close>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Close
ref={ref}
className={cn(
"absolute right-2 top-2 rounded-md p-1 text-foreground/50 opacity-0 transition-opacity hover:text-foreground focus:opacity-100 focus:outline-none focus:ring-2 group-hover:opacity-100 group-[.destructive]:text-red-300 group-[.destructive]:hover:text-red-50 group-[.destructive]:focus:ring-red-400 group-[.destructive]:focus:ring-offset-red-600",
className
)}
toast-close=""
{...props}
>
<X className="h-4 w-4" />
</ToastPrimitives.Close>
))
ToastClose.displayName = ToastPrimitives.Close.displayName
const ToastTitle = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Title>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Title
ref={ref}
className={cn("text-sm font-semibold", className)}
{...props}
/>
))
ToastTitle.displayName = ToastPrimitives.Title.displayName
const ToastDescription = React.forwardRef<
React.ElementRef<typeof ToastPrimitives.Description>,
React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
>(({ className, ...props }, ref) => (
<ToastPrimitives.Description
ref={ref}
className={cn("text-sm opacity-90", className)}
{...props}
/>
))
ToastDescription.displayName = ToastPrimitives.Description.displayName
type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>
type ToastActionElement = React.ReactElement<typeof ToastAction>
export {
type ToastProps,
type ToastActionElement,
ToastProvider,
ToastViewport,
Toast,
ToastTitle,
ToastDescription,
ToastClose,
ToastAction,
}

View File

@@ -0,0 +1,33 @@
import {
Toast,
ToastClose,
ToastDescription,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/basicComponents/ui/toast"
import { useToast } from "@/basicComponents/ui/use-toast"
export function Toaster() {
const { toasts } = useToast()
return (
<ToastProvider>
{toasts.map(function ({ id, title, description, action, ...props }) {
return (
<Toast key={id} {...props}>
<div className="grid gap-1">
{title && <ToastTitle>{title}</ToastTitle>}
{description && (
<ToastDescription>{description}</ToastDescription>
)}
</div>
{action}
<ToastClose />
</Toast>
)
})}
<ToastViewport />
</ToastProvider>
)
}

View File

@@ -0,0 +1,192 @@
// Inspired by react-hot-toast library
import * as React from "react"
import type {
ToastActionElement,
ToastProps,
} from "@/basicComponents/ui/toast"
const TOAST_LIMIT = 1
const TOAST_REMOVE_DELAY = 1000000
type ToasterToast = ToastProps & {
id: string
title?: React.ReactNode
description?: React.ReactNode
action?: ToastActionElement
}
const actionTypes = {
ADD_TOAST: "ADD_TOAST",
UPDATE_TOAST: "UPDATE_TOAST",
DISMISS_TOAST: "DISMISS_TOAST",
REMOVE_TOAST: "REMOVE_TOAST",
} as const
let count = 0
function genId() {
count = (count + 1) % Number.MAX_VALUE
return count.toString()
}
type ActionType = typeof actionTypes
type Action =
| {
type: ActionType["ADD_TOAST"]
toast: ToasterToast
}
| {
type: ActionType["UPDATE_TOAST"]
toast: Partial<ToasterToast>
}
| {
type: ActionType["DISMISS_TOAST"]
toastId?: ToasterToast["id"]
}
| {
type: ActionType["REMOVE_TOAST"]
toastId?: ToasterToast["id"]
}
interface State {
toasts: ToasterToast[]
}
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
const addToRemoveQueue = (toastId: string) => {
if (toastTimeouts.has(toastId)) {
return
}
const timeout = setTimeout(() => {
toastTimeouts.delete(toastId)
dispatch({
type: "REMOVE_TOAST",
toastId: toastId,
})
}, TOAST_REMOVE_DELAY)
toastTimeouts.set(toastId, timeout)
}
export const reducer = (state: State, action: Action): State => {
switch (action.type) {
case "ADD_TOAST":
return {
...state,
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
}
case "UPDATE_TOAST":
return {
...state,
toasts: state.toasts.map((t) =>
t.id === action.toast.id ? { ...t, ...action.toast } : t
),
}
case "DISMISS_TOAST": {
const { toastId } = action
// ! Side effects ! - This could be extracted into a dismissToast() action,
// but I'll keep it here for simplicity
if (toastId) {
addToRemoveQueue(toastId)
} else {
state.toasts.forEach((toast) => {
addToRemoveQueue(toast.id)
})
}
return {
...state,
toasts: state.toasts.map((t) =>
t.id === toastId || toastId === undefined
? {
...t,
open: false,
}
: t
),
}
}
case "REMOVE_TOAST":
if (action.toastId === undefined) {
return {
...state,
toasts: [],
}
}
return {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
}
}
}
const listeners: Array<(state: State) => void> = []
let memoryState: State = { toasts: [] }
function dispatch(action: Action) {
memoryState = reducer(memoryState, action)
listeners.forEach((listener) => {
listener(memoryState)
})
}
type Toast = Omit<ToasterToast, "id">
function toast({ ...props }: Toast) {
const id = genId()
const update = (props: ToasterToast) =>
dispatch({
type: "UPDATE_TOAST",
toast: { ...props, id },
})
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
dispatch({
type: "ADD_TOAST",
toast: {
...props,
id,
open: true,
onOpenChange: (open) => {
if (!open) dismiss()
},
},
})
return {
id: id,
dismiss,
update,
}
}
function useToast() {
const [state, setState] = React.useState<State>(memoryState)
React.useEffect(() => {
listeners.push(setState)
return () => {
const index = listeners.indexOf(setState)
if (index > -1) {
listeners.splice(index, 1)
}
}
}, [state])
return {
...state,
toast,
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
}
}
export { useToast, toast }

View File

@@ -0,0 +1,48 @@
import { useState } from "react";
import { LocalAuthComponent } from "jazz-react-auth-local";
import { Input, Button } from "../basicComponents";
export const PrettyAuthUI: LocalAuthComponent = ({
loading,
logIn,
signUp,
}) => {
const [username, setUsername] = useState<string>("");
return (
<div className="w-full h-full flex items-center justify-center p-5">
{loading ? (
<div>Loading...</div>
) : (
<div className="w-72 flex flex-col gap-4">
<form
className="w-72 flex flex-col gap-2"
onSubmit={(e) => {
e.preventDefault();
signUp(username);
}}
>
<Input
placeholder="Display name"
value={username}
onChange={(e) => setUsername(e.target.value)}
autoComplete="webauthn"
className="text-base"
/>
<Button asChild>
<Input
type="submit"
value="Sign Up as new account"
/>
</Button>
</form>
<Button onClick={logIn}>
Log In with existing account
</Button>
</div>
)}
</div>
);
};

View File

@@ -0,0 +1,76 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 20 14.3% 4.1%;
--card: 0 0% 100%;
--card-foreground: 20 14.3% 4.1%;
--popover: 0 0% 100%;
--popover-foreground: 20 14.3% 4.1%;
--primary: 24 9.8% 10%;
--primary-foreground: 60 9.1% 97.8%;
--secondary: 60 4.8% 95.9%;
--secondary-foreground: 24 9.8% 10%;
--muted: 60 4.8% 95.9%;
--muted-foreground: 25 5.3% 44.7%;
--accent: 60 4.8% 95.9%;
--accent-foreground: 24 9.8% 10%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 60 9.1% 97.8%;
--border: 20 5.9% 90%;
--input: 20 5.9% 90%;
--ring: 20 14.3% 4.1%;
--radius: 0.5rem;
}
.dark {
--background: 20 14.3% 4.1%;
--foreground: 60 9.1% 97.8%;
--card: 20 14.3% 4.1%;
--card-foreground: 60 9.1% 97.8%;
--popover: 20 14.3% 4.1%;
--popover-foreground: 60 9.1% 97.8%;
--primary: 60 9.1% 97.8%;
--primary-foreground: 24 9.8% 10%;
--secondary: 12 6.5% 15.1%;
--secondary-foreground: 60 9.1% 97.8%;
--muted: 12 6.5% 15.1%;
--muted-foreground: 24 5.4% 63.9%;
--accent: 12 6.5% 15.1%;
--accent-foreground: 60 9.1% 97.8%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 60 9.1% 97.8%;
--border: 12 6.5% 15.1%;
--input: 12 6.5% 15.1%;
--ring: 24 5.7% 82.9%;
}
}
@layer base {
* {
@apply border-border;
}
body {
@apply bg-background text-foreground;
}
}

1
examples/twit/src/vite-env.d.ts vendored Normal file
View File

@@ -0,0 +1 @@
/// <reference types="vite/client" />

View File

@@ -0,0 +1,76 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
darkMode: ["class"],
content: [
'./pages/**/*.{ts,tsx}',
'./components/**/*.{ts,tsx}',
'./app/**/*.{ts,tsx}',
'./src/**/*.{ts,tsx}',
],
theme: {
container: {
center: true,
padding: "2rem",
screens: {
"2xl": "1400px",
},
},
extend: {
colors: {
border: "hsl(var(--border))",
input: "hsl(var(--input))",
ring: "hsl(var(--ring))",
background: "hsl(var(--background))",
foreground: "hsl(var(--foreground))",
primary: {
DEFAULT: "hsl(var(--primary))",
foreground: "hsl(var(--primary-foreground))",
},
secondary: {
DEFAULT: "hsl(var(--secondary))",
foreground: "hsl(var(--secondary-foreground))",
},
destructive: {
DEFAULT: "hsl(var(--destructive))",
foreground: "hsl(var(--destructive-foreground))",
},
muted: {
DEFAULT: "hsl(var(--muted))",
foreground: "hsl(var(--muted-foreground))",
},
accent: {
DEFAULT: "hsl(var(--accent))",
foreground: "hsl(var(--accent-foreground))",
},
popover: {
DEFAULT: "hsl(var(--popover))",
foreground: "hsl(var(--popover-foreground))",
},
card: {
DEFAULT: "hsl(var(--card))",
foreground: "hsl(var(--card-foreground))",
},
},
borderRadius: {
lg: "var(--radius)",
md: "calc(var(--radius) - 2px)",
sm: "calc(var(--radius) - 4px)",
},
keyframes: {
"accordion-down": {
from: { height: 0 },
to: { height: "var(--radix-accordion-content-height)" },
},
"accordion-up": {
from: { height: "var(--radix-accordion-content-height)" },
to: { height: 0 },
},
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
"accordion-up": "accordion-up 0.2s ease-out",
},
},
},
plugins: [require("tailwindcss-animate")],
}

View File

@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2023", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}

View File

@@ -0,0 +1,10 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}

View File

@@ -0,0 +1,16 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import path from "path";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
build: {
minify: false
}
})

View File

@@ -1,5 +1,5 @@
import { readFile, writeFile } from "fs/promises";
import { Application, JSONOutput } from "typedoc";
import { Application, JSONOutput, ReflectionKind } from "typedoc";
const manuallyIgnore = new Set(["CojsonInternalTypes"]);
@@ -8,14 +8,17 @@ async function main() {
// Also accepts an array of option readers if you want to disable
// TypeDoc's tsconfig.json/package.json/typedoc.json option readers
const packageDocs = Object.entries({
cojson: "index.ts",
"jazz-react": "index.tsx",
cojson: "index.ts",
"jazz-browser": "index.ts",
"jazz-browser-media-images": "index.ts",
}).map(async ([packageName, entryPoint]) => {
const app = await Application.bootstrapWithPlugins({
entryPoints: [`packages/${packageName}/src/${entryPoint}`],
tsconfig: `packages/${packageName}/tsconfig.json`,
sort: ["required-first"],
groupOrder: ["Functions", "Classes", "TypeAliases", "Namespaces"],
categorizeByGroup: false
});
const project = await app.convert();
@@ -35,33 +38,67 @@ async function main() {
docs
.groups!.map((group) => {
return group.children
?.map((childId) => {
?.flatMap((childId) => {
const child = docs.children!.find(
(child) => child.id === childId
)!;
if (manuallyIgnore.has(child.name)) {
return "";
if (
manuallyIgnore.has(child.name) ||
child.comment?.blockTags?.some(
(tag) =>
tag.tag === "@deprecated" ||
tag.tag === "@internal" ||
tag.tag === "@ignore"
) ||
child.signatures?.every((signature) =>
signature.comment?.blockTags?.some(
(tag) =>
tag.tag === "@deprecated" ||
tag.tag === "@internal" ||
tag.tag === "@ignore"
)
)
) {
return [];
}
return (
`## \`${renderChildName(child)}\` (${group.title
`## \`${renderChildName(
child
)}\`\n\n<sup>(${group.title
.toLowerCase()
.replace("bles", "ble")
.replace("ces", "ce")
.replace(/es$/, "")
.replace(
"ns",
"n"
)} in \`${packageName}\`)\n\n` +
)} in \`${packageName}\`)</sup>\n\n` +
renderChildType(child) +
renderComment(child.comment) +
(child.kind === 128 || child.kind === 256
? child.groups
?.map((group) =>
renderChildGroup(child, group)
(child.kind === ReflectionKind.Class ||
child.kind === ReflectionKind.Interface ||
child.kind === ReflectionKind.Namespace
? renderSummary(child.comment) +
renderExamples(child.comment) +
(child.categories || child.groups)
?.map((category) =>
renderChildCategory(child, category)
)
.join("\n\n")
: "TODO: doc generator not implemented yet")
.join("<br/>\n\n")
: child.kind === ReflectionKind.Function
? renderSummary(
child.signatures?.[0].comment
) +
renderParamComments(
child.signatures?.[0].parameters || []
) +
renderExamples(
child.signatures?.[0].comment
) +
"\n\n"
: "TODO: doc generator not implemented yet " +
child.kind)
);
})
.join("\n\n----\n\n");
@@ -69,7 +106,7 @@ async function main() {
.join("\n\n----\n\n")
);
function renderComment(comment?: JSONOutput.Comment): string {
function renderSummary(comment?: JSONOutput.Comment): string {
if (comment) {
return (
comment.summary
@@ -80,11 +117,50 @@ async function main() {
)
.join("") +
"\n\n" +
(comment.blockTags || [])
.map((blockTag) =>
blockTag.tag === "@example"
? "##### Example:\n\n" +
blockTag.content
"\n\n"
);
} else {
return "TODO: document\n\n";
}
}
function renderExamples(comment?: JSONOutput.Comment): string {
return (comment?.blockTags || [])
.map((blockTag) =>
blockTag.tag === "@example"
? "##### Example:\n\n" +
blockTag.content
.map((token) =>
token.kind === "text" || token.kind === "code"
? token.text
: ""
)
.join("") +
"\n\n"
: ""
)
.join("");
}
function renderParamComments(params: JSONOutput.ParameterReflection[]) {
const paramDocs = params.flatMap((param) => {
if (param.type?.type === "reflection") {
return param.type.declaration.children?.flatMap((child) => {
if (
child.name === "children" &&
child.type?.type === "reference" &&
child.type?.name === "ReactNode"
) {
return [];
}
return (
`| \`${param.name}.${child.name}${
child.flags.isOptional || child.defaultValue
? "?"
: ""
}\` | ` +
(child.comment
? child.comment.summary
.map((token) =>
token.kind === "text" ||
token.kind === "code"
@@ -92,13 +168,37 @@ async function main() {
: ""
)
.join("")
: "TODO: document") +
" |"
);
});
} else {
const comment = param.comment;
return [
`| \`${param.name}${
param.flags.isOptional || param.defaultValue
? "?"
: ""
)
.join("\n\n") +
"\n\n"
);
} else {
return "TODO: document\n\n";
}\` | ` +
(comment
? comment.summary
.map((token) =>
token.kind === "text" ||
token.kind === "code"
? token.text
: ""
)
.join("")
: "TODO: document ") +
" |",
];
}
});
if (paramDocs.length) {
return `### Parameters:\n\n| name | description |\n| ----: | ---- |\n${paramDocs.join(
"\n"
)}\n\n`;
}
}
@@ -126,26 +226,30 @@ async function main() {
function renderChildType(
child: JSONOutput.DeclarationReflection
): string {
const isClass = child.kind === 128;
const isTypeDef = child.kind === 2097152;
const isInterface = child.kind === 256;
const isClass = child.kind === ReflectionKind.Class;
const isTypeAlias = child.kind === ReflectionKind.TypeAlias;
const isInterface = child.kind === ReflectionKind.Interface;
const isNamespace = child.kind === ReflectionKind.Namespace;
const isFunction = !!child.signatures;
const kind = isClass
? "class"
: isTypeAlias
? "type"
: isFunction
? "function"
: isInterface
? "interface"
: isNamespace
? "namespace"
: "";
return (
"```typescript\n" +
`export ${
isClass
? "class"
: isTypeDef
? "type"
: isFunction
? "function"
: isInterface
? "interface"
: ""
} ${child.name}` +
(child.typeParameters
`export ${kind} ${child.name}` +
((child.typeParameters || child.signatures?.[0].typeParameter)
? "<" +
child.typeParameters.map(renderTypeParam).join(", ") +
(child.typeParameters || child.signatures?.[0].typeParameter || []).map(renderTypeParam).join(", ") +
">"
: "") +
(child.extendedTypes
@@ -156,9 +260,9 @@ async function main() {
? " implements " +
child.implementedTypes.map(renderType).join(", ")
: "") +
(isClass || isInterface
(isClass || isInterface || isNamespace
? " {...}"
: isTypeDef
: isTypeAlias
? ` = ${renderType(child.type)}`
: child.signatures
? `(${(child.signatures[0].parameters || [])
@@ -171,33 +275,69 @@ async function main() {
);
}
function renderChildGroup(
function renderChildCategory(
child: JSONOutput.DeclarationReflection,
group: JSONOutput.ReflectionGroup
category: JSONOutput.ReflectionGroup
): string {
return (
`### ${group.title}\n\n` +
group.children
`### \`${child.name}\`: ${category.title.replace(/[^d]+\./, "")}\n\n` +
category.children
?.map((memberId) => {
const member = child.children!.find(
(member) => member.id === memberId
)!;
if (member.kind === 2048 || member.kind === 512) {
if (member.signatures?.every(sig => sig.comment?.modifierTags?.includes("@internal"))) {
return ""
if (
member.signatures?.every(
(sig) =>
sig.comment?.modifierTags?.includes(
"@internal"
) ||
sig.comment?.modifierTags?.includes(
"@deprecated"
)
)
) {
return "";
} else {
return documentConstructorOrMethod(member, child);
return documentConstructorOrMethod(
member,
child
);
}
} else if (
member.kind === 1024 ||
member.kind === 262144
) {
if (member.comment?.modifierTags?.includes("@internal")) {
return ""
if (
member.comment?.modifierTags?.includes(
"@internal"
) ||
member.comment?.modifierTags?.includes(
"@deprecated"
)
) {
return "";
} else {
return documentProperty(member, child);
}
} else if (member.kind === 2097152) {
if (
member.comment?.modifierTags?.includes(
"@internal"
) ||
member.comment?.modifierTags?.includes(
"@deprecated"
)
) {
return "";
} else {
return documentProperty(
{ ...member, flags: { isStatic: true } },
child
);
}
} else {
return "Unknown member kind " + member.kind;
}
@@ -220,9 +360,39 @@ async function main() {
} else if (t.type === "literal") {
return JSON.stringify(t.value);
} else if (t.type === "union") {
return [...new Set(t.types.map(renderType))].join(" | ");
const seen = new Set<string>();
return t.types
.flatMap((t) => {
const rendered =
t.type === "intersection" || t.type === "union"
? `(${renderType(t)})`
: renderType(t);
if (seen.has(rendered)) {
return [];
} else {
seen.add(rendered);
return [rendered];
}
})
.join(" | ");
} else if (t.type === "intersection") {
return [...new Set(t.types.map(renderType))].join(" & ");
const seen = new Set<string>();
return t.types
.flatMap((t) => {
const rendered =
t.type === "intersection" || t.type === "union"
? `(${renderType(t)})`
: renderType(t);
if (seen.has(rendered)) {
return [];
} else {
seen.add(rendered);
return [rendered];
}
})
.join(" & ");
} else if (t.type === "indexedAccess") {
return (
renderType(t.objectType) +
@@ -233,40 +403,84 @@ async function main() {
} else if (t.type === "reflection") {
if (t.declaration.indexSignature) {
return (
"{ [" +
`{${
t.declaration.children
? t.declaration.children
.map(
(child) =>
` ${child.name}${
child.flags.isOptional
? "?"
: ""
}: ${indentEnd(
renderType(child.type)
)},`
)
.join("\n")
: ""
}\n [` +
t.declaration.indexSignature?.parameters?.[0].name +
": " +
renderType(
t.declaration.indexSignature?.parameters?.[0].type
) +
"]: " +
renderType(t.declaration.indexSignature?.type) +
indentEnd(
renderType(t.declaration.indexSignature?.type)
) +
" }"
);
} else if (t.declaration.children) {
return `{${t.declaration.children
.map(
(child) =>
`${child.name}${
child.flags.isOptional ? "?" : ""
}: ${renderType(child.type)}`
return `{\n${t.declaration.children
.map((child) =>
child.signatures
? child.signatures
.map(
(signature) =>
` ${child.name}(${
signature.parameters
? "\n " +
indent(
signature.parameters
.map((p) =>
indentEnd(
renderParam(
p
)
)
)
.join(",\n ")
) +
"\n )"
: "()"
}: ${indentEnd(
renderType(signature.type)
)}`
)
.join("\n") + ",\n"
: ` ${child.name}${
child.flags.isOptional ? "?" : ""
}: ${indentEnd(renderType(child.type))},\n`
)
.join(", ")}}`;
.join("")}}`;
} else if (t.declaration.signatures) {
if (t.declaration.signatures.length > 1) {
return "COMPLEX_TYPE_MULTIPLE_INLINE_SIGNATURES";
} else {
return `(${(
t.declaration.signatures[0].parameters || []
).map(renderParam)}) => ${renderType(
t.declaration.signatures[0].type
)}`;
}
return t.declaration.signatures
.map(
(signature) =>
`(${(signature.parameters || [])
.map(renderParam)
.join(", ")}) => ${renderType(
signature.type
)}`
)
.join("\n");
} else {
return "COMPLEX_TYPE_REFLECTION";
}
} else if (t.type === "array") {
return renderType(t.elementType) + "[]";
} else if (t.type === "tuple") {
return `[${t.elements?.map(renderType).join(", ")}]`;
} else if (t.type === "templateLiteral") {
const matchingNamedType = docs.children?.find(
(child) =>
@@ -296,9 +510,55 @@ async function main() {
return "AgentID";
}
} else {
return "TEMPLATE_LITERAL";
return (
"`" +
t.head +
t.tail
.map(
(bit) =>
"${" + renderType(bit[0]) + "}" + bit[1]
)
.join("") +
"`"
);
}
}
} else if (t.type === "conditional") {
const trueRendered = renderType(t.trueType);
const falseRendered = renderType(t.falseType);
if (
trueRendered.includes("\n") ||
falseRendered.includes("\n")
) {
return (
renderType(t.checkType) +
" extends " +
renderType(t.extendsType) +
"\n ? " +
indentEnd(renderType(t.trueType)) +
"\n : " +
indentEnd(renderType(t.falseType))
);
} else {
return (
renderType(t.checkType) +
" extends " +
renderType(t.extendsType) +
" ? " +
renderType(t.trueType) +
" : " +
renderType(t.falseType)
);
}
} else if (t.type === "inferred") {
return "infer " + t.name;
} else if (t.type === "typeOperator") {
return t.operator + " " + renderType(t.target);
} else if (t.type === "mapped") {
return `{\n [${t.parameter} in ${renderType(
t.parameterType
)}]: ${indentEnd(renderType(t.templateType))}\n}`;
} else {
return "COMPLEX_TYPE_" + t.type;
}
@@ -341,101 +601,186 @@ async function main() {
(child) =>
child.name + (child.flags.isOptional ? "?" : "")
)
.join(", ")}}${param.defaultValue ? "?" : ""}`
: param.name + (param.defaultValue ? "?" : "");
.join(", ")}}${
param.flags.isOptional || param.defaultValue ? "?" : ""
}`
: param.name +
(param.flags.isOptional || param.defaultValue ? "?" : "");
}
function documentConstructorOrMethod(
member: JSONOutput.DeclarationReflection,
child: JSONOutput.DeclarationReflection
) {
const isInClass = child.kind === 128;
const isInTypeDef = child.kind === 2097152;
const isInInterface = child.kind === 256;
const isInNamespace = child.kind === 4;
const isInFunction = !!child.signatures;
const inKind = isInClass
? "class"
: isInTypeDef
? "type"
: isInFunction
? "function"
: isInInterface
? "interface"
: isInNamespace
? "namespace"
: "";
const stem =
member.name === "constructor"
? "new " + child.name
: (member.flags.isStatic
? child.name
: child.name[0].toLowerCase() + child.name.slice(1)) +
? "new " + child.name + "</code></b>"
: (member.flags.isStatic ? child.name : "") +
"." +
member.name;
member.name +
"";
return (
`<details>\n<summary><code>${stem}(${(
member.signatures?.[0]?.parameters?.map(
renderParamSimple
) || []
).join(", ")})</code> ${
member.inheritedFrom
? "(from <code>" +
member.inheritedFrom.name.split(".")[0] +
"</code>) "
: ""
} ${
member.signatures?.[0]?.comment ? "" : "(undocumented)"
}</summary>\n\n` +
member.signatures?.map((signature) => {
return member.signatures
?.map((signature) => {
return (
"```typescript\n" +
`${stem}${
signature.typeParameter
? `<${signature.typeParameter
.map(renderTypeParam)
.join(", ")}>`
`<details>\n<summary><b><code>${stem}(${(
signature?.parameters?.map(renderParamSimple) || []
).join(", ")})</code></b> ${
member.inheritedFrom
? "<sub><sup>from <code>" +
member.inheritedFrom.name.split(".")[0] +
"</code></sup></sub> "
: ""
}(${
(
signature.parameters?.map(
(param) =>
`\n ${param.name}${
param.defaultValue ? "?" : ""
}: ${renderType(param.type)}${
param.defaultValue
? ` = ${param.defaultValue}`
: ""
}`
) || []
).join(",") +
(signature.parameters?.length ? "\n" : "")
}): ${renderType(signature.type)}\n` +
"```\n" +
renderComment(signature.comment)
} ${
signature?.comment
? ""
: "<sub><sup>(undocumented)</sup></sub>"
}</summary>\n\n` +
("```typescript\n" +
`${inKind} ${child.name}${
child.typeParameters
? `<${child.typeParameters
.map((t) => t.name)
.join(", ")}>`
: ""
} {\n\n${indent(
`${member.name}${
signature.typeParameter
? `<${signature.typeParameter
.map(renderTypeParam)
.join(", ")}>`
: ""
}(${
(
signature.parameters?.map(
(param) =>
`\n ${param.name}${
param.flags.isOptional ||
param.defaultValue
? "?"
: ""
}: ${indentEnd(
renderType(param.type)
)}${
param.defaultValue
? ` = ${param.defaultValue}`
: ""
}`
) || []
).join(",") +
(signature.parameters?.length ? "\n" : "")
}): ${renderType(signature.type)} {...}`
)}\n\n}\n` +
"```\n" +
renderSummary(signature.comment)) +
renderParamComments(signature.parameters || []) +
renderExamples(signature.comment) +
"</details>\n\n"
);
}) +
"</details>\n\n"
);
})
.join("\n\n");
}
function documentProperty(
member: JSONOutput.DeclarationReflection,
child: JSONOutput.DeclarationReflection
) {
const stem = member.flags.isStatic
? child.name
: child.name[0].toLowerCase() + child.name.slice(1);
const isInClass = child.kind === 128;
const isInTypeDef = child.kind === 2097152;
const isInInterface = child.kind === 256;
const isInNamespace = child.kind === 4;
const isInFunction = !!child.signatures;
const inKind = isInClass
? "class"
: isInTypeDef
? "type"
: isInFunction
? "function"
: isInInterface
? "interface"
: isInNamespace
? "namespace"
: "";
const stem = member.flags.isStatic ? child.name : "";
return (
`<details>\n<summary><code>${stem}.${member.name}</code> ${
`<details>\n<summary><b><code>${stem}.${
member.name
}</code></b> ${
member.inheritedFrom
? "(from <code>" +
? "<sub><sup>from <code>" +
member.inheritedFrom.name.split(".")[0] +
"</code>) "
"</code></sup></sub> "
: ""
} ${
member.comment ? "" : "(undocumented)"
member.comment ? "" : "<sub><sup>(undocumented)</sup></sub>"
}</summary>\n\n` +
"```typescript\n" +
`${member.getSignature ? "get " : ""}${stem}.${member.name}${
member.getSignature ? "()" : ""
}: ${renderType(member.type || member.getSignature?.type)}\n` +
"```\n" +
renderComment(member.comment) +
`${inKind} ${child.name}${
child.typeParameters
? `<${child.typeParameters
.map((t) => t.name)
.join(", ")}>`
: ""
} {\n\n${indent(
`${member.getSignature ? "get " : ""}${member.name}${
member.getSignature ? "()" : ""
}: ${renderType(member.type || member.getSignature?.type)}${
member.getSignature ? " {...}" : ""
}`
)}` +
"\n\n}\n```\n" +
renderSummary(member.comment) +
renderExamples(member.comment) +
"</details>\n\n"
);
}
});
const docsContent = await readFile("./DOCS.md", "utf8");
await writeFile(
"./DOCS.md",
(await Promise.all(packageDocs)).join("\n\n\n")
docsContent.slice(
0,
docsContent.indexOf("<!-- AUTOGENERATED DOCS AFTER THIS POINT -->")
) +
"<!-- AUTOGENERATED DOCS AFTER THIS POINT -->\n" +
(await Promise.all(packageDocs)).join("\n\n\n")
);
}
function indent(text: string): string {
return text
.split("\n")
.map((line) => " " + line)
.join("\n");
}
function indentEnd(text: string): string {
return text
.split("\n")
.map((line, i) => (i === 0 ? line : " " + line))
.join("\n");
}
main().catch(console.error);

View File

@@ -4,7 +4,7 @@
"types": "src/index.ts",
"type": "module",
"license": "MIT",
"version": "0.1.12",
"version": "0.4.1",
"devDependencies": {
"@types/jest": "^29.5.3",
"@types/ws": "^8.5.5",
@@ -16,8 +16,8 @@
"typescript": "5.0.2"
},
"dependencies": {
"cojson": "^0.1.11",
"cojson-storage-sqlite": "^0.1.9",
"cojson": "^0.4.1",
"cojson-storage-sqlite": "^0.4.1",
"ws": "^8.13.0"
},
"scripts": {

View File

@@ -16,7 +16,7 @@ const localNode = new LocalNode(
);
SQLiteStorage.asPeer({ filename: "./sync.db" })
.then((storage) => localNode.sync.addPeer(storage))
.then((storage) => localNode.syncManager.addPeer(storage))
.catch((e) => console.error(e));
wss.on("connection", function connection(ws, req) {
@@ -34,8 +34,6 @@ wss.on("connection", function connection(ws, req) {
clearInterval(pinging);
});
const clientAddress =
(req.headers["x-forwarded-for"] as string | undefined)
?.split(",")[0]
@@ -43,7 +41,7 @@ wss.on("connection", function connection(ws, req) {
const clientId = clientAddress + "@" + new Date().toISOString();
localNode.sync.addPeer({
localNode.syncManager.addPeer({
id: clientId,
role: "client",
incoming: websocketReadableStream(ws),

View File

@@ -1,11 +1,11 @@
{
"name": "jazz-storage-indexeddb",
"version": "0.1.11",
"name": "cojson-storage-indexeddb",
"version": "0.4.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"cojson": "^0.1.11",
"cojson": "^0.4.1",
"typescript": "^5.1.6"
},
"devDependencies": {

View File

@@ -12,7 +12,7 @@ test.skip("Should be able to initialize and load from empty DB", async () => {
)
);
node.sync.addPeer(await IDBStorage.asPeer({ trace: true }));
node.syncManager.addPeer(await IDBStorage.asPeer({ trace: true }));
console.log("yay!");
@@ -20,7 +20,7 @@ test.skip("Should be able to initialize and load from empty DB", async () => {
await new Promise((resolve) => setTimeout(resolve, 200));
expect(node.sync.peers["storage"]).toBeDefined();
expect(node.syncManager.peers["storage"]).toBeDefined();
});
test("Should be able to sync data to database and then load that from a new node", async () => {
@@ -33,7 +33,7 @@ test("Should be able to sync data to database and then load that from a new node
)
);
node1.sync.addPeer(
node1.syncManager.addPeer(
await IDBStorage.asPeer({ trace: true, localNodeName: "node1" })
);
@@ -56,7 +56,7 @@ test("Should be able to sync data to database and then load that from a new node
)
);
node2.sync.addPeer(
node2.syncManager.addPeer(
await IDBStorage.asPeer({ trace: true, localNodeName: "node2" })
);

View File

@@ -1,4 +1,12 @@
import { cojsonInternals, SessionID, SyncMessage, Peer, CojsonInternalTypes } from "cojson";
import {
cojsonInternals,
SessionID,
SyncMessage,
Peer,
CojsonInternalTypes,
MAX_RECOMMENDED_TX_SIZE,
AccountID,
} from "cojson";
import {
ReadableStream,
WritableStream,
@@ -18,6 +26,7 @@ type SessionRow = {
sessionID: SessionID;
lastIdx: number;
lastSignature: CojsonInternalTypes.Signature;
bytesSinceLastSignature?: number;
};
type StoredSessionRow = SessionRow & { rowID: number };
@@ -28,6 +37,12 @@ type TransactionRow = {
tx: CojsonInternalTypes.Transaction;
};
type SignatureAfterRow = {
ses: number;
idx: number;
signature: CojsonInternalTypes.Signature;
};
export class IDBStorage {
db: IDBDatabase;
fromLocalNode!: ReadableStreamDefaultReader<SyncMessage>;
@@ -49,7 +64,7 @@ export class IDBStorage {
done = result.done;
if (result.value) {
this.handleSyncMessage(result.value);
await this.handleSyncMessage(result.value);
}
}
})();
@@ -82,42 +97,65 @@ export class IDBStorage {
toLocalNode: WritableStream<SyncMessage>
) {
const dbPromise = new Promise<IDBDatabase>((resolve, reject) => {
const request = indexedDB.open("jazz-storage", 1);
const request = indexedDB.open("jazz-storage", 4);
request.onerror = () => {
reject(request.error);
};
request.onsuccess = () => {
resolve(request.result);
};
request.onupgradeneeded = () => {
request.onupgradeneeded = async (ev) => {
const db = request.result;
if (ev.oldVersion === 0) {
const coValues = db.createObjectStore("coValues", {
autoIncrement: true,
keyPath: "rowID",
});
const coValues = db.createObjectStore("coValues", {
autoIncrement: true,
keyPath: "rowID",
});
coValues.createIndex("coValuesById", "id", {
unique: true,
});
const sessions = db.createObjectStore("sessions", {
autoIncrement: true,
keyPath: "rowID",
});
sessions.createIndex("sessionsByCoValue", "coValue");
sessions.createIndex(
"uniqueSessions",
["coValue", "sessionID"],
{
coValues.createIndex("coValuesById", "id", {
unique: true,
}
);
});
db.createObjectStore("transactions", {
keyPath: ["ses", "idx"],
});
const sessions = db.createObjectStore("sessions", {
autoIncrement: true,
keyPath: "rowID",
});
sessions.createIndex("sessionsByCoValue", "coValue");
sessions.createIndex(
"uniqueSessions",
["coValue", "sessionID"],
{
unique: true,
}
);
db.createObjectStore("transactions", {
keyPath: ["ses", "idx"],
});
}
if (ev.oldVersion <= 1) {
db.createObjectStore("signatureAfter", {
keyPath: ["ses", "idx"],
});
}
if (ev.oldVersion !== 0 && ev.oldVersion <= 3) {
// fix embarrassing off-by-one error for transaction indices
console.log("Migration: fixing off-by-one error");
const transaction = (
ev.target as unknown as { transaction: IDBTransaction }
).transaction;
const txsStore = transaction.objectStore("transactions");
const txs = await promised(txsStore.getAll());
for (const tx of txs) {
await promised(txsStore.delete([tx.ses, tx.idx]));
tx.idx -= 1;
await promised(txsStore.add(tx));
}
console.log("Migration: fixing off-by-one error - done");
}
};
});
@@ -147,10 +185,12 @@ export class IDBStorage {
coValues,
sessions,
transactions,
signatureAfter,
}: {
coValues: IDBObjectStore;
sessions: IDBObjectStore;
transactions: IDBObjectStore;
signatureAfter: IDBObjectStore;
},
asDependencyOf?: CojsonInternalTypes.RawCoID
) {
@@ -170,12 +210,14 @@ export class IDBStorage {
sessions: {},
};
const newContent: CojsonInternalTypes.NewContentMessage = {
action: "content",
id: theirKnown.id,
header: theirKnown.header ? undefined : coValueRow?.header,
new: {},
};
const newContentPieces: CojsonInternalTypes.NewContentMessage[] = [
{
action: "content",
id: theirKnown.id,
header: theirKnown.header ? undefined : coValueRow?.header,
new: {},
},
];
for (const sessionRow of allOurSessions) {
ourKnown.sessions[sessionRow.sessionID] = sessionRow.lastIdx;
@@ -187,6 +229,21 @@ export class IDBStorage {
const firstNewTxIdx =
theirKnown.sessions[sessionRow.sessionID] || 0;
const signaturesAndIdxs = await promised<SignatureAfterRow[]>(
signatureAfter.getAll(
IDBKeyRange.bound(
[sessionRow.rowID, firstNewTxIdx],
[sessionRow.rowID, Infinity]
)
)
);
// console.log(
// theirKnown.id,
// "signaturesAndIdxs",
// JSON.stringify(signaturesAndIdxs)
// );
const newTxInSession = await promised<TransactionRow[]>(
transactions.getAll(
IDBKeyRange.bound(
@@ -196,44 +253,110 @@ export class IDBStorage {
)
);
newContent.new[sessionRow.sessionID] = {
after: firstNewTxIdx,
lastSignature: sessionRow.lastSignature,
newTransactions: newTxInSession.map((row) => row.tx),
};
let idx = firstNewTxIdx;
// console.log(
// theirKnown.id,
// "newTxInSession",
// newTxInSession.length
// );
for (const tx of newTxInSession) {
let sessionEntry =
newContentPieces[newContentPieces.length - 1]!.new[
sessionRow.sessionID
];
if (!sessionEntry) {
sessionEntry = {
after: idx,
lastSignature:
"WILL_BE_REPLACED" as CojsonInternalTypes.Signature,
newTransactions: [],
};
newContentPieces[newContentPieces.length - 1]!.new[
sessionRow.sessionID
] = sessionEntry;
}
sessionEntry.newTransactions.push(tx.tx);
if (
signaturesAndIdxs[0] &&
idx === signaturesAndIdxs[0].idx
) {
sessionEntry.lastSignature =
signaturesAndIdxs[0].signature;
signaturesAndIdxs.shift();
newContentPieces.push({
action: "content",
id: theirKnown.id,
new: {},
});
} else if (
idx ===
firstNewTxIdx + newTxInSession.length - 1
) {
sessionEntry.lastSignature = sessionRow.lastSignature;
}
idx += 1;
}
}
}
const dependedOnCoValues =
coValueRow?.header.ruleset.type === "group"
? Object.values(newContent.new).flatMap((sessionEntry) =>
sessionEntry.newTransactions.flatMap((tx) => {
if (tx.privacy !== "trusting") return [];
return tx.changes
.map(
(change) =>
change &&
typeof change === "object" &&
"op" in change &&
change.op === "set" &&
"key" in change &&
change.key
)
.filter(
(key): key is CojsonInternalTypes.RawCoID =>
typeof key === "string" &&
key.startsWith("co_")
);
})
)
? newContentPieces
.flatMap((piece) => Object.values(piece.new))
.flatMap((sessionEntry) =>
sessionEntry.newTransactions.flatMap((tx) => {
if (tx.privacy !== "trusting") return [];
// TODO: avoid parse here?
return cojsonInternals
.parseJSON(tx.changes)
.map(
(change) =>
change &&
typeof change === "object" &&
"op" in change &&
change.op === "set" &&
"key" in change &&
change.key
)
.filter(
(
key
): key is CojsonInternalTypes.RawCoID =>
typeof key === "string" &&
key.startsWith("co_")
);
})
)
: coValueRow?.header.ruleset.type === "ownedByGroup"
? [coValueRow?.header.ruleset.group]
? [
coValueRow?.header.ruleset.group,
...new Set(
newContentPieces.flatMap((piece) =>
Object.keys(piece)
.map((sessionID) =>
cojsonInternals.accountOrAgentIDfromSessionID(
sessionID as SessionID
)
)
.filter(
(accountID): accountID is AccountID =>
cojsonInternals.isAccountID(
accountID
) && accountID !== theirKnown.id
)
)
),
]
: [];
for (const dependedOnCoValue of dependedOnCoValues) {
await this.sendNewContentAfter(
{ id: dependedOnCoValue, header: false, sessions: {} },
{ coValues, sessions, transactions },
{ coValues, sessions, transactions, signatureAfter },
asDependencyOf || theirKnown.id
);
}
@@ -244,8 +367,15 @@ export class IDBStorage {
asDependencyOf,
});
if (newContent.header || Object.keys(newContent.new).length > 0) {
await this.toLocalNode.write(newContent);
const nonEmptyNewContentPieces = newContentPieces.filter(
(piece) => piece.header || Object.keys(piece.new).length > 0
);
// console.log(theirKnown.id, nonEmptyNewContentPieces);
for (const piece of nonEmptyNewContentPieces) {
await this.toLocalNode.write(piece);
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
@@ -254,7 +384,7 @@ export class IDBStorage {
}
async handleContent(msg: CojsonInternalTypes.NewContentMessage) {
const { coValues, sessions, transactions } =
const { coValues, sessions, transactions, signatureAfter } =
this.inTransaction("readwrite");
let storedCoValueRowID = (
@@ -325,18 +455,39 @@ export class IDBStorage {
const actuallyNewOffset =
(sessionRow?.lastIdx || 0) -
(msg.new[sessionID]?.after || 0);
const actuallyNewTransactions =
newTransactions.slice(actuallyNewOffset);
let newBytesSinceLastSignature =
(sessionRow?.bytesSinceLastSignature || 0) +
actuallyNewTransactions.reduce(
(sum, tx) =>
sum +
(tx.privacy === "private"
? tx.encryptedChanges.length
: tx.changes.length),
0
);
const newLastIdx =
(sessionRow?.lastIdx || 0) + actuallyNewTransactions.length;
let shouldWriteSignature = false;
if (newBytesSinceLastSignature > MAX_RECOMMENDED_TX_SIZE) {
shouldWriteSignature = true;
newBytesSinceLastSignature = 0;
}
let nextIdx = sessionRow?.lastIdx || 0;
const sessionUpdate = {
coValue: storedCoValueRowID,
sessionID: sessionID,
lastIdx:
(sessionRow?.lastIdx || 0) +
actuallyNewTransactions.length,
lastIdx: newLastIdx,
lastSignature: msg.new[sessionID]!.lastSignature,
bytesSinceLastSignature: newBytesSinceLastSignature,
};
const sessionRowID = (await promised(
@@ -350,8 +501,18 @@ export class IDBStorage {
)
)) as number;
if (shouldWriteSignature) {
await promised(
signatureAfter.put({
ses: sessionRowID,
// TODO: newLastIdx is a misnomer, it's actually more like nextIdx or length
idx: newLastIdx - 1,
signature: msg.new[sessionID]!.lastSignature,
} satisfies SignatureAfterRow)
);
}
for (const newTransaction of actuallyNewTransactions) {
nextIdx++;
await promised(
transactions.add({
ses: sessionRowID,
@@ -359,6 +520,7 @@ export class IDBStorage {
tx: newTransaction,
} satisfies TransactionRow)
);
nextIdx++;
}
}
}
@@ -382,9 +544,10 @@ export class IDBStorage {
coValues: IDBObjectStore;
sessions: IDBObjectStore;
transactions: IDBObjectStore;
signatureAfter: IDBObjectStore;
} {
const tx = this.db.transaction(
["coValues", "sessions", "transactions"],
["coValues", "sessions", "transactions", "signatureAfter"],
mode
);
@@ -401,8 +564,9 @@ export class IDBStorage {
const coValues = tx.objectStore("coValues");
const sessions = tx.objectStore("sessions");
const transactions = tx.objectStore("transactions");
const signatureAfter = tx.objectStore("signatureAfter");
return { coValues, sessions, transactions };
return { coValues, sessions, transactions, signatureAfter };
}
}

View File

@@ -1,13 +1,13 @@
{
"name": "cojson-storage-sqlite",
"type": "module",
"version": "0.1.9",
"version": "0.4.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"better-sqlite3": "^8.5.2",
"cojson": "^0.1.11",
"cojson": "^0.4.1",
"typescript": "^5.1.6"
},
"scripts": {

View File

@@ -4,8 +4,8 @@ import {
Peer,
CojsonInternalTypes,
SessionID,
// CojsonInternalTypes,
// SessionID,
MAX_RECOMMENDED_TX_SIZE,
AccountID
} from "cojson";
import {
ReadableStream,
@@ -15,7 +15,6 @@ import {
} from "isomorphic-streams";
import Database, { Database as DatabaseT } from "better-sqlite3";
import { RawCoID } from "cojson/dist/ids";
type CoValueRow = {
id: CojsonInternalTypes.RawCoID;
@@ -29,6 +28,7 @@ type SessionRow = {
sessionID: SessionID;
lastIdx: number;
lastSignature: CojsonInternalTypes.Signature;
bytesSinceLastSignature?: number;
};
type StoredSessionRow = SessionRow & { rowID: number };
@@ -39,6 +39,12 @@ type TransactionRow = {
tx: string;
};
type SignatureAfterRow = {
ses: number;
idx: number;
signature: CojsonInternalTypes.Signature;
};
export class SQLiteStorage {
fromLocalNode!: ReadableStreamDefaultReader<SyncMessage>;
toLocalNode: WritableStreamDefaultWriter<SyncMessage>;
@@ -60,7 +66,7 @@ export class SQLiteStorage {
done = result.done;
if (result.value) {
this.handleSyncMessage(result.value);
await this.handleSyncMessage(result.value);
}
}
})();
@@ -98,41 +104,99 @@ export class SQLiteStorage {
const db = Database(filename);
db.pragma("journal_mode = WAL");
db.prepare(
`CREATE TABLE IF NOT EXISTS transactions (
ses INTEGER,
idx INTEGER,
tx TEXT NOT NULL ,
PRIMARY KEY (ses, idx)
) WITHOUT ROWID;`
).run();
const oldVersion = (
db.pragma("user_version") as [{ user_version: number }]
)[0].user_version as number;
db.prepare(
`CREATE TABLE IF NOT EXISTS sessions (
rowID INTEGER PRIMARY KEY,
coValue INTEGER NOT NULL,
sessionID TEXT NOT NULL,
lastIdx INTEGER,
lastSignature TEXT,
UNIQUE (sessionID, coValue)
);`
).run();
console.log("DB version", oldVersion);
db.prepare(
`CREATE INDEX IF NOT EXISTS sessionsByCoValue ON sessions (coValue);`
).run();
if (oldVersion === 0) {
console.log("Migration 0 -> 1: Basic schema");
db.prepare(
`CREATE TABLE IF NOT EXISTS transactions (
ses INTEGER,
idx INTEGER,
tx TEXT NOT NULL,
PRIMARY KEY (ses, idx)
) WITHOUT ROWID;`
).run();
db.prepare(
`CREATE TABLE IF NOT EXISTS coValues (
rowID INTEGER PRIMARY KEY,
id TEXT NOT NULL UNIQUE,
header TEXT NOT NULL UNIQUE
);`
).run();
db.prepare(
`CREATE TABLE IF NOT EXISTS sessions (
rowID INTEGER PRIMARY KEY,
coValue INTEGER NOT NULL,
sessionID TEXT NOT NULL,
lastIdx INTEGER,
lastSignature TEXT,
UNIQUE (sessionID, coValue)
);`
).run();
db.prepare(
`CREATE INDEX IF NOT EXISTS coValuesByID ON coValues (id);`
).run();
db.prepare(
`CREATE INDEX IF NOT EXISTS sessionsByCoValue ON sessions (coValue);`
).run();
db.prepare(
`CREATE TABLE IF NOT EXISTS coValues (
rowID INTEGER PRIMARY KEY,
id TEXT NOT NULL UNIQUE,
header TEXT NOT NULL UNIQUE
);`
).run();
db.prepare(
`CREATE INDEX IF NOT EXISTS coValuesByID ON coValues (id);`
).run();
db.pragma("user_version = 1");
console.log("Migration 0 -> 1: Basic schema - done");
}
if (oldVersion <= 1) {
// fix embarrassing off-by-one error for transaction indices
console.log(
"Migration 1 -> 2: Fix off-by-one error for transaction indices"
);
const txs = db
.prepare(`SELECT * FROM transactions`)
.all() as TransactionRow[];
for (const tx of txs) {
db.prepare(
`DELETE FROM transactions WHERE ses = ? AND idx = ?`
).run(tx.ses, tx.idx);
tx.idx -= 1;
db.prepare(
`INSERT INTO transactions (ses, idx, tx) VALUES (?, ?, ?)`
).run(tx.ses, tx.idx, tx.tx);
}
db.pragma("user_version = 2");
console.log(
"Migration 1 -> 2: Fix off-by-one error for transaction indices - done"
);
}
if (oldVersion <= 2) {
console.log("Migration 2 -> 3: Add signatureAfter");
db.prepare(
`CREATE TABLE IF NOT EXISTS signatureAfter (
ses INTEGER,
idx INTEGER,
signature TEXT NOT NULL,
PRIMARY KEY (ses, idx)
) WITHOUT ROWID;`
).run();
db.prepare(
`ALTER TABLE sessions ADD COLUMN bytesSinceLastSignature INTEGER;`
).run();
db.pragma("user_version = 3");
console.log("Migration 2 -> 3: Add signatureAfter - done");
}
return new SQLiteStorage(db, fromLocalNode, toLocalNode);
}
@@ -174,17 +238,31 @@ export class SQLiteStorage {
sessions: {},
};
const parsedHeader = (coValueRow?.header &&
JSON.parse(coValueRow.header)) as
| CojsonInternalTypes.CoValueHeader
| undefined;
let parsedHeader;
const newContent: CojsonInternalTypes.NewContentMessage = {
action: "content",
id: theirKnown.id,
header: theirKnown.header ? undefined : parsedHeader,
new: {},
};
try {
parsedHeader = (coValueRow?.header &&
JSON.parse(coValueRow.header)) as
| CojsonInternalTypes.CoValueHeader
| undefined;
} catch (e) {
console.warn(
theirKnown.id,
"Invalid JSON in header",
e,
coValueRow?.header
);
return;
}
const newContentPieces: CojsonInternalTypes.NewContentMessage[] = [
{
action: "content",
id: theirKnown.id,
header: theirKnown.header ? undefined : parsedHeader,
new: {},
},
];
for (const sessionRow of allOurSessions) {
ourKnown.sessions[sessionRow.sessionID] = sessionRow.lastIdx;
@@ -196,46 +274,153 @@ export class SQLiteStorage {
const firstNewTxIdx =
theirKnown.sessions[sessionRow.sessionID] || 0;
const signaturesAndIdxs = this.db
.prepare<[number, number]>(
`SELECT * FROM signatureAfter WHERE ses = ? AND idx >= ?`
)
.all(
sessionRow.rowID,
firstNewTxIdx
) as SignatureAfterRow[];
// console.log(
// theirKnown.id,
// "signaturesAndIdxs",
// JSON.stringify(signaturesAndIdxs)
// );
const newTxInSession = this.db
.prepare<[number, number]>(
`SELECT * FROM transactions WHERE ses = ? AND idx > ?`
`SELECT * FROM transactions WHERE ses = ? AND idx >= ?`
)
.all(sessionRow.rowID, firstNewTxIdx) as TransactionRow[];
newContent.new[sessionRow.sessionID] = {
after: firstNewTxIdx,
lastSignature: sessionRow.lastSignature,
newTransactions: newTxInSession.map((row) =>
JSON.parse(row.tx)
),
};
let idx = firstNewTxIdx;
// console.log(
// theirKnown.id,
// "newTxInSession",
// newTxInSession.length
// );
for (const tx of newTxInSession) {
let sessionEntry =
newContentPieces[newContentPieces.length - 1]!.new[
sessionRow.sessionID
];
if (!sessionEntry) {
sessionEntry = {
after: idx,
lastSignature:
"WILL_BE_REPLACED" as CojsonInternalTypes.Signature,
newTransactions: [],
};
newContentPieces[newContentPieces.length - 1]!.new[
sessionRow.sessionID
] = sessionEntry;
}
let parsedTx;
try {
parsedTx = JSON.parse(tx.tx);
} catch (e) {
console.warn(
theirKnown.id,
"Invalid JSON in transaction",
e,
tx.tx
);
break;
}
sessionEntry.newTransactions.push(parsedTx);
if (
signaturesAndIdxs[0] &&
idx === signaturesAndIdxs[0].idx
) {
sessionEntry.lastSignature =
signaturesAndIdxs[0].signature;
signaturesAndIdxs.shift();
newContentPieces.push({
action: "content",
id: theirKnown.id,
new: {},
});
} else if (
idx ===
firstNewTxIdx + newTxInSession.length - 1
) {
sessionEntry.lastSignature = sessionRow.lastSignature;
}
idx += 1;
}
}
}
const dependedOnCoValues =
parsedHeader?.ruleset.type === "group"
? Object.values(newContent.new).flatMap((sessionEntry) =>
sessionEntry.newTransactions.flatMap((tx) => {
if (tx.privacy !== "trusting") return [];
return tx.changes
.map(
(change) =>
change &&
typeof change === "object" &&
"op" in change &&
change.op === "set" &&
"key" in change &&
change.key
)
.filter(
(key): key is CojsonInternalTypes.RawCoID =>
typeof key === "string" &&
key.startsWith("co_")
);
})
)
? newContentPieces
.flatMap((piece) => Object.values(piece.new))
.flatMap((sessionEntry) =>
sessionEntry.newTransactions.flatMap((tx) => {
if (tx.privacy !== "trusting") return [];
// TODO: avoid parsing here?
let parsedChanges;
try {
parsedChanges = cojsonInternals.parseJSON(
tx.changes
);
} catch (e) {
console.warn(
theirKnown.id,
"Invalid JSON in transaction",
e,
tx.changes
);
return [];
}
return parsedChanges
.map(
(change) =>
change &&
typeof change === "object" &&
"op" in change &&
change.op === "set" &&
"key" in change &&
change.key
)
.filter(
(
key
): key is CojsonInternalTypes.RawCoID =>
typeof key === "string" &&
key.startsWith("co_")
);
})
)
: parsedHeader?.ruleset.type === "ownedByGroup"
? [parsedHeader?.ruleset.group]
? [
parsedHeader?.ruleset.group,
...new Set(
newContentPieces.flatMap((piece) =>
Object.keys(piece)
.map((sessionID) =>
cojsonInternals.accountOrAgentIDfromSessionID(
sessionID as SessionID
)
)
.filter(
(accountID): accountID is AccountID =>
cojsonInternals.isAccountID(accountID) &&
accountID !== theirKnown.id
)
)
),
]
: [];
for (const dependedOnCoValue of dependedOnCoValues) {
@@ -251,8 +436,15 @@ export class SQLiteStorage {
asDependencyOf,
});
if (newContent.header || Object.keys(newContent.new).length > 0) {
await this.toLocalNode.write(newContent);
const nonEmptyNewContentPieces = newContentPieces.filter(
(piece) => piece.header || Object.keys(piece.new).length > 0
);
// console.log(theirKnown.id, nonEmptyNewContentPieces);
for (const piece of nonEmptyNewContentPieces) {
await this.toLocalNode.write(piece);
await new Promise((resolve) => setTimeout(resolve, 0));
}
}
@@ -263,7 +455,9 @@ export class SQLiteStorage {
async handleContent(msg: CojsonInternalTypes.NewContentMessage) {
let storedCoValueRowID = (
this.db
.prepare<RawCoID>(`SELECT rowID FROM coValues WHERE id = ?`)
.prepare<CojsonInternalTypes.RawCoID>(
`SELECT rowID FROM coValues WHERE id = ?`
)
.get(msg.id) as StoredCoValueRow | undefined
)?.rowID;
@@ -282,7 +476,7 @@ export class SQLiteStorage {
}
storedCoValueRowID = this.db
.prepare<[RawCoID, string]>(
.prepare<[CojsonInternalTypes.RawCoID, string]>(
`INSERT INTO coValues (id, header) VALUES (?, ?)`
)
.run(msg.id, JSON.stringify(header)).lastInsertRowid as number;
@@ -324,37 +518,72 @@ export class SQLiteStorage {
const actuallyNewOffset =
(sessionRow?.lastIdx || 0) -
(msg.new[sessionID]?.after || 0);
const actuallyNewTransactions =
newTransactions.slice(actuallyNewOffset);
let newBytesSinceLastSignature =
(sessionRow?.bytesSinceLastSignature || 0) +
actuallyNewTransactions.reduce(
(sum, tx) =>
sum +
(tx.privacy === "private"
? tx.encryptedChanges.length
: tx.changes.length),
0
);
const newLastIdx =
(sessionRow?.lastIdx || 0) +
actuallyNewTransactions.length;
let shouldWriteSignature = false;
if (newBytesSinceLastSignature > MAX_RECOMMENDED_TX_SIZE) {
shouldWriteSignature = true;
newBytesSinceLastSignature = 0;
}
let nextIdx = sessionRow?.lastIdx || 0;
const sessionUpdate = {
coValue: storedCoValueRowID!,
sessionID: sessionID,
lastIdx:
(sessionRow?.lastIdx || 0) +
actuallyNewTransactions.length,
lastIdx: newLastIdx,
lastSignature: msg.new[sessionID]!.lastSignature,
bytesSinceLastSignature: newBytesSinceLastSignature,
};
const upsertedSession = (this.db
.prepare<[number, string, number, string]>(
`INSERT INTO sessions (coValue, sessionID, lastIdx, lastSignature) VALUES (?, ?, ?, ?)
ON CONFLICT(coValue, sessionID) DO UPDATE SET lastIdx=excluded.lastIdx, lastSignature=excluded.lastSignature
const upsertedSession = this.db
.prepare<[number, string, number, string, number]>(
`INSERT INTO sessions (coValue, sessionID, lastIdx, lastSignature, bytesSinceLastSignature) VALUES (?, ?, ?, ?, ?)
ON CONFLICT(coValue, sessionID) DO UPDATE SET lastIdx=excluded.lastIdx, lastSignature=excluded.lastSignature, bytesSinceLastSignature=excluded.bytesSinceLastSignature
RETURNING rowID`
)
.get(
sessionUpdate.coValue,
sessionUpdate.sessionID,
sessionUpdate.lastIdx,
sessionUpdate.lastSignature
) as {rowID: number});
sessionUpdate.lastSignature,
sessionUpdate.bytesSinceLastSignature
) as { rowID: number };
const sessionRowID = upsertedSession.rowID;
if (shouldWriteSignature) {
this.db
.prepare<[number, number, string]>(
`INSERT INTO signatureAfter (ses, idx, signature) VALUES (?, ?, ?)`
)
.run(
sessionRowID,
// TODO: newLastIdx is a misnomer, it's actually more like nextIdx or length
newLastIdx - 1,
msg.new[sessionID]!.lastSignature
);
}
for (const newTransaction of actuallyNewTransactions) {
nextIdx++;
this.db
.prepare<[number, number, string]>(
`INSERT INTO transactions (ses, idx, tx) VALUES (?, ?, ?)`
@@ -364,6 +593,7 @@ export class SQLiteStorage {
nextIdx,
JSON.stringify(newTransaction)
);
nextIdx++;
}
}
}

View File

@@ -9,6 +9,7 @@ module.exports = {
parserOptions: {
project: "./tsconfig.json",
},
ignorePatterns: [".eslint.cjs", "**/tests/*"],
root: true,
rules: {
"no-unused-vars": "off",

View File

@@ -5,7 +5,7 @@
"types": "dist/index.d.ts",
"type": "module",
"license": "MIT",
"version": "0.1.11",
"version": "0.4.1",
"devDependencies": {
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.2.1",
@@ -19,9 +19,8 @@
"dependencies": {
"@noble/ciphers": "^0.1.3",
"@noble/curves": "^1.1.0",
"@noble/hashes": "^1.3.1",
"@scure/base": "^1.1.1",
"fast-json-stable-stringify": "https://github.com/tirithen/fast-json-stable-stringify#7a3dcf2",
"hash-wasm": "^4.9.0",
"isomorphic-streams": "https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae"
},
"scripts": {

View File

@@ -25,6 +25,7 @@ export function base64URLtoBytes(base64: string) {
}
export function bytesToBase64url(bytes: Uint8Array) {
// const before = performance.now();
const m = bytes.length;
const k = m % 3;
const n = Math.floor(m / 3) * 4 + (k && k + 1);
@@ -43,16 +44,25 @@ export function bytesToBase64url(bytes: Uint8Array) {
let base64 = decoder.decode(new Uint8Array(encoded.buffer, 0, n));
if (k === 1) base64 += "==";
if (k === 2) base64 += "=";
// const after = performance.now();
// console.log(
// "bytesToBase64url bandwidth in MB/s for length",
// (1000 * bytes.length / (after - before)) / (1024 * 1024),
// bytes.length
// );
return base64;
}
const alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
const lookup = Object.fromEntries(
Array.from(alphabet).map((a, i) => [a.charCodeAt(0), i])
);
const lookup = new Uint8Array(128);
for (const [i, a] of Array.from(alphabet).entries()) {
lookup[a.charCodeAt(0)] = i;
}
lookup["=".charCodeAt(0)] = 0;
const encodeLookup = Object.fromEntries(
Array.from(alphabet).map((a, i) => [i, a.charCodeAt(0)])
);
const encodeLookup = new Uint8Array(64);
for (const [i, a] of Array.from(alphabet).entries()) {
encodeLookup[i] = a.charCodeAt(0);
}

View File

@@ -1,387 +0,0 @@
import { accountOrAgentIDfromSessionID } from "./coValueCore.js";
import { BinaryCoStream } from "./coValues/coStream.js";
import { createdNowUnique } from "./crypto.js";
import { LocalNode } from "./node.js";
import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
test("Empty CoMap works", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "comap",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "comap") {
throw new Error("Expected map");
}
expect(content.type).toEqual("comap");
expect([...content.keys()]).toEqual([]);
expect(content.toJSON()).toEqual({});
});
test("Can insert and delete CoMap entries in edit()", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "comap",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "comap") {
throw new Error("Expected map");
}
expect(content.type).toEqual("comap");
content.edit((editable) => {
editable.set("hello", "world", "trusting");
expect(editable.get("hello")).toEqual("world");
editable.set("foo", "bar", "trusting");
expect(editable.get("foo")).toEqual("bar");
expect([...editable.keys()]).toEqual(["hello", "foo"]);
editable.delete("foo", "trusting");
expect(editable.get("foo")).toEqual(undefined);
});
});
test("Can get CoMap entry values at different points in time", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "comap",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "comap") {
throw new Error("Expected map");
}
expect(content.type).toEqual("comap");
content.edit((editable) => {
const beforeA = Date.now();
while (Date.now() < beforeA + 10) {}
editable.set("hello", "A", "trusting");
const beforeB = Date.now();
while (Date.now() < beforeB + 10) {}
editable.set("hello", "B", "trusting");
const beforeC = Date.now();
while (Date.now() < beforeC + 10) {}
editable.set("hello", "C", "trusting");
expect(editable.get("hello")).toEqual("C");
expect(editable.getAtTime("hello", Date.now())).toEqual("C");
expect(editable.getAtTime("hello", beforeA)).toEqual(undefined);
expect(editable.getAtTime("hello", beforeB)).toEqual("A");
expect(editable.getAtTime("hello", beforeC)).toEqual("B");
});
});
test("Can get all historic values of key in CoMap", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "comap",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "comap") {
throw new Error("Expected map");
}
expect(content.type).toEqual("comap");
content.edit((editable) => {
editable.set("hello", "A", "trusting");
const txA = editable.getLastTxID("hello");
editable.set("hello", "B", "trusting");
const txB = editable.getLastTxID("hello");
editable.delete("hello", "trusting");
const txDel = editable.getLastTxID("hello");
editable.set("hello", "C", "trusting");
const txC = editable.getLastTxID("hello");
expect(editable.getHistory("hello")).toEqual([
{
txID: txA,
value: "A",
at: txA && coValue.getTx(txA)?.madeAt,
},
{
txID: txB,
value: "B",
at: txB && coValue.getTx(txB)?.madeAt,
},
{
txID: txDel,
value: undefined,
at: txDel && coValue.getTx(txDel)?.madeAt,
},
{
txID: txC,
value: "C",
at: txC && coValue.getTx(txC)?.madeAt,
},
]);
});
});
test("Can get last tx ID for a key in CoMap", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "comap",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "comap") {
throw new Error("Expected map");
}
expect(content.type).toEqual("comap");
content.edit((editable) => {
expect(editable.getLastTxID("hello")).toEqual(undefined);
editable.set("hello", "A", "trusting");
const sessionID = editable.getLastTxID("hello")?.sessionID;
expect(sessionID && accountOrAgentIDfromSessionID(sessionID)).toEqual(
node.account.id
);
expect(editable.getLastTxID("hello")?.txIndex).toEqual(0);
editable.set("hello", "B", "trusting");
expect(editable.getLastTxID("hello")?.txIndex).toEqual(1);
editable.set("hello", "C", "trusting");
expect(editable.getLastTxID("hello")?.txIndex).toEqual(2);
});
});
test("Empty CoList works", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "colist",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "colist") {
throw new Error("Expected list");
}
expect(content.type).toEqual("colist");
expect(content.toJSON()).toEqual([]);
});
test("Can append, prepend and delete items to CoList", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "colist",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "colist") {
throw new Error("Expected list");
}
expect(content.type).toEqual("colist");
content.edit((editable) => {
editable.append(0, "hello", "trusting");
expect(editable.toJSON()).toEqual(["hello"]);
editable.append(0, "world", "trusting");
expect(editable.toJSON()).toEqual(["hello", "world"]);
editable.prepend(1, "beautiful", "trusting");
expect(editable.toJSON()).toEqual(["hello", "beautiful", "world"]);
editable.prepend(3, "hooray", "trusting");
expect(editable.toJSON()).toEqual([
"hello",
"beautiful",
"world",
"hooray",
]);
editable.delete(2, "trusting");
expect(editable.toJSON()).toEqual(["hello", "beautiful", "hooray"]);
});
});
test("Push is equivalent to append after last item", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "colist",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "colist") {
throw new Error("Expected list");
}
expect(content.type).toEqual("colist");
content.edit((editable) => {
editable.append(0, "hello", "trusting");
expect(editable.toJSON()).toEqual(["hello"]);
editable.push("world", "trusting");
expect(editable.toJSON()).toEqual(["hello", "world"]);
editable.push("hooray", "trusting");
expect(editable.toJSON()).toEqual(["hello", "world", "hooray"]);
});
});
test("Can push into empty list", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "colist",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "colist") {
throw new Error("Expected list");
}
expect(content.type).toEqual("colist");
content.edit((editable) => {
editable.push("hello", "trusting");
expect(editable.toJSON()).toEqual(["hello"]);
});
});
test("Empty CoStream works", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "costream",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "costream") {
throw new Error("Expected stream");
}
expect(content.type).toEqual("costream");
expect(content.toJSON()).toEqual({});
expect(content.getSingleStream()).toEqual(undefined);
});
test("Can push into CoStream", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "costream",
ruleset: { type: "unsafeAllowAll" },
meta: null,
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "costream") {
throw new Error("Expected stream");
}
content.edit((editable) => {
editable.push({ hello: "world" }, "trusting");
expect(editable.toJSON()).toEqual({
[node.currentSessionID]: [{ hello: "world" }],
});
editable.push({ foo: "bar" }, "trusting");
expect(editable.toJSON()).toEqual({
[node.currentSessionID]: [{ hello: "world" }, { foo: "bar" }],
});
expect(editable.getSingleStream()).toEqual([
{ hello: "world" },
{ foo: "bar" },
]);
});
});
test("Empty BinaryCoStream works", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "costream",
ruleset: { type: "unsafeAllowAll" },
meta: { type: "binary" },
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "costream" || content.meta?.type !== "binary" || !(content instanceof BinaryCoStream)) {
throw new Error("Expected binary stream");
}
expect(content.type).toEqual("costream");
expect(content.meta.type).toEqual("binary");
expect(content.toJSON()).toEqual({});
expect(content.getBinaryChunks()).toEqual(undefined);
});
test("Can push into BinaryCoStream", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
type: "costream",
ruleset: { type: "unsafeAllowAll" },
meta: { type: "binary" },
...createdNowUnique(),
});
const content = coValue.getCurrentContent();
if (content.type !== "costream" || content.meta?.type !== "binary" || !(content instanceof BinaryCoStream)) {
throw new Error("Expected binary stream");
}
content.edit((editable) => {
editable.startBinaryStream({mimeType: "text/plain", fileName: "test.txt"}, "trusting");
expect(editable.getBinaryChunks()).toEqual({
mimeType: "text/plain",
fileName: "test.txt",
chunks: [],
finished: false,
});
});
});

View File

@@ -1,17 +1,33 @@
import { JsonObject, JsonValue } from "./jsonValue.js";
import { RawCoID } from "./ids.js";
import { CoMap } from "./coValues/coMap.js";
import { BinaryCoStream, BinaryCoStreamMeta, CoStream } from "./coValues/coStream.js";
import { Static } from "./coValues/static.js";
import {
BinaryCoStream,
BinaryCoStreamMeta,
CoStream,
} from "./coValues/coStream.js";
import { CoList } from "./coValues/coList.js";
import { CoValueCore } from "./coValueCore.js";
import { Group } from "./group.js";
import { Group } from "./coValues/group.js";
import { Account, Profile } from "./index.js";
export type CoID<T extends CoValueImpl> = RawCoID & {
export type CoID<T extends CoValue> = RawCoID & {
readonly __type: T;
};
export interface ReadableCoValue extends CoValue {
export interface CoValue {
/** The `CoValue`'s (precisely typed) `CoID` */
id: CoID<this>;
core: CoValueCore;
/** Specifies which kind of `CoValue` this is */
type: string;
/** The `CoValue`'s (precisely typed) static metadata */
meta: JsonObject | null;
/** The `Group` this `CoValue` belongs to (determining permissions) */
group: Group;
/** Returns an immutable JSON presentation of this `CoValue` */
toJSON(): JsonValue;
atTime(time: number): this;
/** Lets you subscribe to future updates to this CoValue (whether made locally or by other users).
*
* Takes a listener function that will be called with the current state for each update.
@@ -19,46 +35,42 @@ export interface ReadableCoValue extends CoValue {
* Returns an unsubscribe function.
*
* Used internally by `useTelepathicData()` for reactive updates on changes to a `CoValue`. */
subscribe(listener: (coValue: CoValueImpl) => void): () => void;
/** Lets you apply edits to a `CoValue`, inside the changer callback, which receives a `WriteableCoValue`.
*
* A `WritableCoValue` has all the same methods as a `CoValue`, but all edits made to it (with its additional mutator methods)
* are reflected in it immediately - so it behaves mutably, whereas a `CoValue` is always immutable
* (you need to use `subscribe` to receive new versions of it). */
edit?:
| ((changer: (editable: WriteableCoValue) => void) => CoValueImpl)
| undefined;
subscribe(listener: (coValue: this) => void): () => void;
}
export interface CoValue {
/** The `CoValue`'s (precisely typed) `CoID` */
id: CoID<CoValueImpl>;
core: CoValueCore;
/** Specifies which kind of `CoValue` this is */
type: CoValueImpl["type"];
/** The `CoValue`'s (precisely typed) static metadata */
meta: JsonObject | null;
/** The `Group` this `CoValue` belongs to (determining permissions) */
group: Group;
/** Returns an immutable JSON presentation of this `CoValue` */
toJSON(): JsonValue;
}
export type AnyCoValue = CoMap | Group | Account | Profile | CoList | CoStream | BinaryCoStream;
export interface WriteableCoValue extends CoValue {}
export type CoValueImpl =
| CoMap<{ [key: string]: JsonValue }, JsonObject | null>
| CoList<JsonValue, JsonObject | null>
| CoStream<JsonValue, JsonObject | null>
| BinaryCoStream<BinaryCoStreamMeta>
| Static<JsonObject>;
export function expectMap(
content: CoValueImpl
): CoMap<{ [key: string]: string }, JsonObject | null> {
export function expectMap(content: CoValue): CoMap {
if (content.type !== "comap") {
throw new Error("Expected map");
}
return content as CoMap<{ [key: string]: string }, JsonObject | null>;
return content as CoMap;
}
export function expectList(content: CoValue): CoList {
if (content.type !== "colist") {
throw new Error("Expected list");
}
return content as CoList;
}
export function expectStream(content: CoValue): CoStream {
if (content.type !== "costream") {
throw new Error("Expected stream");
}
return content as CoStream;
}
export function isCoValue(
value: JsonValue | CoValue | undefined
): value is CoValue {
return (
value instanceof CoMap ||
value instanceof CoList ||
value instanceof CoStream ||
value instanceof BinaryCoStream
);
}

View File

@@ -1,6 +1,5 @@
import { randomBytes } from "@noble/hashes/utils";
import { CoValueImpl } from "./coValue.js";
import { Static } from "./coValues/static.js";
import { AnyCoValue, CoValue } from "./coValue.js";
import { BinaryCoStream, CoStream } from "./coValues/coStream.js";
import { CoMap } from "./coValues/coMap.js";
import {
@@ -14,11 +13,11 @@ import {
sign,
verify,
encryptForTransaction,
decryptForTransaction,
KeyID,
decryptKeySecret,
getAgentSignerID,
getAgentSealerID,
decryptRawForTransaction,
} from "./crypto.js";
import { JsonObject, JsonValue } from "./jsonValue.js";
import { base58 } from "@scure/base";
@@ -27,18 +26,23 @@ import {
determineValidTransactions,
isKeyForKeyField,
} from "./permissions.js";
import { Group, expectGroupContent } from "./group.js";
import { LocalNode } from "./node.js";
import { Group, expectGroup } from "./coValues/group.js";
import { LocalNode } from "./localNode.js";
import { CoValueKnownState, NewContentMessage } from "./sync.js";
import { AgentID, RawCoID, SessionID, TransactionID } from "./ids.js";
import { CoList } from "./coValues/coList.js";
import {
Account,
AccountID,
GeneralizedControlledAccount,
} from "./account.js";
isAccountID,
} from "./coValues/account.js";
import { Stringified, stableStringify } from "./jsonStringify.js";
export const MAX_RECOMMENDED_TX_SIZE = 100 * 1024;
export type CoValueHeader = {
type: CoValueImpl["type"];
type: AnyCoValue["type"];
ruleset: RulesetDef;
meta: JsonObject | null;
createdAt: `2${string}` | null;
@@ -64,6 +68,7 @@ type SessionLog = {
transactions: Transaction[];
lastHash?: Hash;
streamingHash: StreamingHash;
signatureAfter: { [txIdx: number]: Signature | undefined };
lastSignature: Signature;
};
@@ -80,14 +85,14 @@ export type PrivateTransaction = {
export type TrustingTransaction = {
privacy: "trusting";
madeAt: number;
changes: JsonValue[];
changes: Stringified<JsonValue[]>;
};
export type Transaction = PrivateTransaction | TrustingTransaction;
export type DecryptedTransaction = {
txID: TransactionID;
changes: JsonValue[];
changes: Stringified<JsonValue[]>;
madeAt: number;
};
@@ -98,9 +103,13 @@ export class CoValueCore {
node: LocalNode;
header: CoValueHeader;
_sessions: { [key: SessionID]: SessionLog };
_cachedContent?: CoValueImpl;
listeners: Set<(content?: CoValueImpl) => void> = new Set();
_decryptionCache: {[key: Encrypted<JsonValue[], JsonValue>]: JsonValue[] | undefined} = {}
_cachedContent?: CoValue;
listeners: Set<(content?: CoValue) => void> = new Set();
_decryptionCache: {
[key: Encrypted<JsonValue[], JsonValue>]:
| Stringified<JsonValue[]>
| undefined;
} = {};
constructor(
header: CoValueHeader,
@@ -159,7 +168,15 @@ export class CoValueCore {
}
nextTransactionID(): TransactionID {
const sessionID = this.node.currentSessionID;
// This is an ugly hack to get a unique but stable session ID for editing the current account
const sessionID =
this.header.meta?.type === "account"
? (this.node.currentSessionID.replace(
this.node.account.id,
this.node.account.currentAgentID()
) as SessionID)
: this.node.currentSessionID;
return {
sessionID,
txIndex: this.sessions[sessionID]?.transactions.length || 0,
@@ -187,10 +204,16 @@ export class CoValueCore {
return false;
}
// const beforeHash = performance.now();
const { expectedNewHash, newStreamingHash } = this.expectedNewHashAfter(
sessionID,
newTransactions
);
// const afterHash = performance.now();
// console.log(
// "Hashing took",
// afterHash - beforeHash
// );
if (givenExpectedNewHash && givenExpectedNewHash !== expectedNewHash) {
console.warn("Invalid hash", {
@@ -200,25 +223,159 @@ export class CoValueCore {
return false;
}
// const beforeVerify = performance.now();
if (!verify(newSignature, expectedNewHash, signerID)) {
console.warn(
"Invalid signature",
"Invalid signature in",
this.id,
newSignature,
expectedNewHash,
signerID
);
return false;
}
// const afterVerify = performance.now();
// console.log(
// "Verify took",
// afterVerify - beforeVerify
// );
this.doAddTransactions(
sessionID,
newTransactions,
newSignature,
expectedNewHash,
newStreamingHash
);
return true;
}
async tryAddTransactionsAsync(
sessionID: SessionID,
newTransactions: Transaction[],
givenExpectedNewHash: Hash | undefined,
newSignature: Signature
): Promise<boolean> {
const signerID = getAgentSignerID(
this.node.resolveAccountAgent(
accountOrAgentIDfromSessionID(sessionID),
"Expected to know signer of transaction"
)
);
if (!signerID) {
console.warn(
"Unknown agent",
accountOrAgentIDfromSessionID(sessionID)
);
return false;
}
const nTxBefore = this.sessions[sessionID]?.transactions.length ?? 0;
// const beforeHash = performance.now();
const { expectedNewHash, newStreamingHash } =
await this.expectedNewHashAfterAsync(sessionID, newTransactions);
// const afterHash = performance.now();
// console.log(
// "Hashing took",
// afterHash - beforeHash
// );
const nTxAfter = this.sessions[sessionID]?.transactions.length ?? 0;
if (nTxAfter !== nTxBefore) {
const newTransactionLengthBefore = newTransactions.length;
newTransactions = newTransactions.slice(nTxAfter - nTxBefore);
console.warn("Transactions changed while async hashing", {
nTxBefore,
nTxAfter,
newTransactionLengthBefore,
remainingNewTransactions: newTransactions.length,
});
}
if (givenExpectedNewHash && givenExpectedNewHash !== expectedNewHash) {
console.warn("Invalid hash", {
expectedNewHash,
givenExpectedNewHash,
});
return false;
}
// const beforeVerify = performance.now();
if (!verify(newSignature, expectedNewHash, signerID)) {
console.warn(
"Invalid signature in",
this.id,
newSignature,
expectedNewHash,
signerID
);
return false;
}
// const afterVerify = performance.now();
// console.log(
// "Verify took",
// afterVerify - beforeVerify
// );
this.doAddTransactions(
sessionID,
newTransactions,
newSignature,
expectedNewHash,
newStreamingHash
);
return true;
}
private doAddTransactions(
sessionID: SessionID,
newTransactions: Transaction[],
newSignature: Signature,
expectedNewHash: Hash,
newStreamingHash: StreamingHash
) {
const transactions = this.sessions[sessionID]?.transactions ?? [];
transactions.push(...newTransactions);
const signatureAfter = this.sessions[sessionID]?.signatureAfter ?? {};
const lastInbetweenSignatureIdx = Object.keys(signatureAfter).reduce(
(max, idx) => (parseInt(idx) > max ? parseInt(idx) : max),
-1
);
const sizeOfTxsSinceLastInbetweenSignature = transactions
.slice(lastInbetweenSignatureIdx + 1)
.reduce(
(sum, tx) =>
sum +
(tx.privacy === "private"
? tx.encryptedChanges.length
: tx.changes.length),
0
);
if (sizeOfTxsSinceLastInbetweenSignature > 100 * 1024) {
// console.log(
// "Saving inbetween signature for tx ",
// sessionID,
// transactions.length - 1,
// sizeOfTxsSinceLastInbetweenSignature
// );
signatureAfter[transactions.length - 1] = newSignature;
}
this._sessions[sessionID] = {
transactions,
lastHash: expectedNewHash,
streamingHash: newStreamingHash,
lastSignature: newSignature,
signatureAfter: signatureAfter,
};
this._cachedContent = undefined;
@@ -229,11 +386,9 @@ export class CoValueCore {
listener(content);
}
}
return true;
}
subscribe(listener: (content?: CoValueImpl) => void): () => void {
subscribe(listener: (content?: CoValue) => void): () => void {
this.listeners.add(listener);
listener(this.getCurrentContent());
@@ -261,6 +416,32 @@ export class CoValueCore {
};
}
async expectedNewHashAfterAsync(
sessionID: SessionID,
newTransactions: Transaction[]
): Promise<{ expectedNewHash: Hash; newStreamingHash: StreamingHash }> {
const streamingHash =
this.sessions[sessionID]?.streamingHash.clone() ??
new StreamingHash();
let before = performance.now();
for (const transaction of newTransactions) {
streamingHash.update(transaction);
const after = performance.now();
if (after - before > 1) {
// console.log("Hashing blocked for", after - before);
await new Promise((resolve) => setTimeout(resolve, 0));
before = performance.now();
}
}
const newStreamingHash = streamingHash.clone();
return {
expectedNewHash: streamingHash.digest(),
newStreamingHash,
};
}
makeTransaction(
changes: JsonValue[],
privacy: "private" | "trusting"
@@ -278,24 +459,35 @@ export class CoValueCore {
);
}
const encrypted = encryptForTransaction(changes, keySecret, {
in: this.id,
tx: this.nextTransactionID(),
});
this._decryptionCache[encrypted] = stableStringify(changes);
transaction = {
privacy: "private",
madeAt,
keyUsed: keyID,
encryptedChanges: encryptForTransaction(changes, keySecret, {
in: this.id,
tx: this.nextTransactionID(),
}),
encryptedChanges: encrypted,
};
} else {
transaction = {
privacy: "trusting",
madeAt,
changes,
changes: stableStringify(changes),
};
}
const sessionID = this.node.currentSessionID;
// This is an ugly hack to get a unique but stable session ID for editing the current account
const sessionID =
this.header.meta?.type === "account"
? (this.node.currentSessionID.replace(
this.node.account.id,
this.node.account.currentAgentID()
) as SessionID)
: this.node.currentSessionID;
const { expectedNewHash } = this.expectedNewHashAfter(sessionID, [
transaction,
@@ -314,41 +506,57 @@ export class CoValueCore {
);
if (success) {
void this.node.sync.syncCoValue(this);
void this.node.syncManager.syncCoValue(this);
}
return success;
}
getCurrentContent(): CoValueImpl {
if (this._cachedContent) {
getCurrentContent(options?: { ignorePrivateTransactions: true }): CoValue {
if (!options?.ignorePrivateTransactions && this._cachedContent) {
return this._cachedContent;
}
let newContent;
if (this.header.type === "comap") {
this._cachedContent = new CoMap(this);
if (this.header.ruleset.type === "group") {
if (
this.header.meta?.type === "account" &&
!options?.ignorePrivateTransactions
) {
newContent = new Account(this);
} else {
newContent = new Group(this, options);
}
} else {
newContent = new CoMap(this);
}
} else if (this.header.type === "colist") {
this._cachedContent = new CoList(this);
newContent = new CoList(this);
} else if (this.header.type === "costream") {
if (this.header.meta && this.header.meta.type === "binary") {
this._cachedContent = new BinaryCoStream(this);
newContent = new BinaryCoStream(this);
} else {
this._cachedContent = new CoStream(this);
newContent = new CoStream(this);
}
} else if (this.header.type === "static") {
this._cachedContent = new Static(this);
} else {
throw new Error(`Unknown coValue type ${this.header.type}`);
}
return this._cachedContent;
if (!options?.ignorePrivateTransactions) {
this._cachedContent = newContent;
}
return newContent;
}
getValidSortedTransactions(): DecryptedTransaction[] {
getValidSortedTransactions(options?: {
ignorePrivateTransactions: true;
}): DecryptedTransaction[] {
const validTransactions = determineValidTransactions(this);
const allTransactions: DecryptedTransaction[] = validTransactions
.map(({ txID, tx }) => {
.flatMap(({ txID, tx }) => {
if (tx.privacy === "trusting") {
return {
txID,
@@ -356,15 +564,19 @@ export class CoValueCore {
changes: tx.changes,
};
} else {
if (options?.ignorePrivateTransactions) {
return undefined;
}
const readKey = this.getReadKey(tx.keyUsed);
if (!readKey) {
return undefined;
} else {
let decrytedChanges = this._decryptionCache[tx.encryptedChanges];
let decrytedChanges =
this._decryptionCache[tx.encryptedChanges];
if (!decrytedChanges) {
decrytedChanges = decryptForTransaction(
decrytedChanges = decryptRawForTransaction(
tx.encryptedChanges,
readKey,
{
@@ -372,7 +584,8 @@ export class CoValueCore {
tx: txID,
}
);
this._decryptionCache[tx.encryptedChanges] = decrytedChanges;
this._decryptionCache[tx.encryptedChanges] =
decrytedChanges;
}
if (!decrytedChanges) {
@@ -402,7 +615,7 @@ export class CoValueCore {
getCurrentReadKey(): { secret: KeySecret | undefined; id: KeyID } {
if (this.header.ruleset.type === "group") {
const content = expectGroupContent(this.getCurrentContent());
const content = expectGroup(this.getCurrentContent());
const currentKeyId = content.get("readKey");
@@ -428,45 +641,58 @@ export class CoValueCore {
}
getReadKey(keyID: KeyID): KeySecret | undefined {
if (readKeyCache.get(this)?.[keyID]) {
return readKeyCache.get(this)?.[keyID];
let key = readKeyCache.get(this)?.[keyID];
if (!key) {
key = this.getUncachedReadKey(keyID);
if (key) {
let cache = readKeyCache.get(this);
if (!cache) {
cache = {};
readKeyCache.set(this, cache);
}
cache[keyID] = key;
}
}
return key;
}
getUncachedReadKey(keyID: KeyID): KeySecret | undefined {
if (this.header.ruleset.type === "group") {
const content = expectGroupContent(this.getCurrentContent());
// Try to find key revelation for us
const readKeyEntry = content.getLastEntry(
`${keyID}_for_${this.node.account.id}`
const content = expectGroup(
this.getCurrentContent({ ignorePrivateTransactions: true })
);
if (readKeyEntry) {
const revealer = accountOrAgentIDfromSessionID(
readKeyEntry.txID.sessionID
);
const keyForEveryone = content.get(`${keyID}_for_everyone`);
if (keyForEveryone) return keyForEveryone;
// Try to find key revelation for us
const lookupAccountOrAgentID =
this.header.meta?.type === "account"
? this.node.account.currentAgentID()
: this.node.account.id;
const lastReadyKeyEdit = content.lastEditAt(
`${keyID}_for_${lookupAccountOrAgentID}`
);
if (lastReadyKeyEdit?.value) {
const revealer = lastReadyKeyEdit.by;
const revealerAgent = this.node.resolveAccountAgent(
revealer,
"Expected to know revealer"
);
const secret = unseal(
readKeyEntry.value,
lastReadyKeyEdit.value,
this.node.account.currentSealerSecret(),
getAgentSealerID(revealerAgent),
{
in: this.id,
tx: readKeyEntry.txID,
tx: lastReadyKeyEdit.tx,
}
);
if (secret) {
let cache = readKeyCache.get(this);
if (!cache) {
cache = {};
readKeyCache.set(this, cache);
}
cache[keyID] = secret;
return secret as KeySecret;
}
}
@@ -495,13 +721,6 @@ export class CoValueCore {
);
if (secret) {
let cache = readKeyCache.get(this);
if (!cache) {
cache = {};
readKeyCache.set(this, cache);
}
cache[keyID] = secret;
return secret as KeySecret;
} else {
console.error(
@@ -528,13 +747,10 @@ export class CoValueCore {
throw new Error("Only values owned by groups have groups");
}
return new Group(
expectGroupContent(
this.node
.expectCoValueLoaded(this.header.ruleset.group)
.getCurrentContent()
),
return expectGroup(
this.node
.expectCoValueLoaded(this.header.ruleset.group)
.getCurrentContent()
);
}
@@ -544,56 +760,119 @@ export class CoValueCore {
newContentSince(
knownState: CoValueKnownState | undefined
): NewContentMessage | undefined {
const newContent: NewContentMessage = {
): NewContentMessage[] | undefined {
let currentPiece: NewContentMessage = {
action: "content",
id: this.id,
header: knownState?.header ? undefined : this.header,
new: Object.fromEntries(
Object.entries(this.sessions)
.map(([sessionID, log]) => {
const newTransactions = log.transactions.slice(
knownState?.sessions[sessionID as SessionID] || 0
);
if (
newTransactions.length === 0 ||
!log.lastHash ||
!log.lastSignature
) {
return undefined;
}
return [
sessionID,
{
after:
knownState?.sessions[
sessionID as SessionID
] || 0,
newTransactions,
lastSignature: log.lastSignature,
},
];
})
.filter((x): x is Exclude<typeof x, undefined> => !!x)
),
new: {},
};
if (!newContent.header && Object.keys(newContent.new).length === 0) {
const pieces = [currentPiece];
const sentState: CoValueKnownState["sessions"] = {
...knownState?.sessions,
};
let newTxsWereAdded = true;
let pieceSize = 0;
while (newTxsWereAdded) {
newTxsWereAdded = false;
for (const [sessionID, log] of Object.entries(this.sessions) as [
SessionID,
SessionLog
][]) {
const nextKnownSignatureIdx = Object.keys(log.signatureAfter)
.map(Number)
.sort((a, b) => a - b)
.find((idx) => idx >= (sentState[sessionID] ?? -1));
const txsToAdd = log.transactions.slice(
sentState[sessionID] ?? 0,
nextKnownSignatureIdx === undefined
? undefined
: nextKnownSignatureIdx + 1
);
if (txsToAdd.length === 0) continue;
newTxsWereAdded = true;
const oldPieceSize = pieceSize;
pieceSize += txsToAdd.reduce(
(sum, tx) =>
sum +
(tx.privacy === "private"
? tx.encryptedChanges.length
: tx.changes.length),
0
);
if (pieceSize >= MAX_RECOMMENDED_TX_SIZE) {
currentPiece = {
action: "content",
id: this.id,
header: undefined,
new: {},
};
pieces.push(currentPiece);
pieceSize = pieceSize - oldPieceSize;
}
let sessionEntry = currentPiece.new[sessionID];
if (!sessionEntry) {
sessionEntry = {
after: sentState[sessionID] ?? 0,
newTransactions: [],
lastSignature: "WILL_BE_REPLACED" as Signature,
};
currentPiece.new[sessionID] = sessionEntry;
}
sessionEntry.newTransactions.push(...txsToAdd);
sessionEntry.lastSignature =
nextKnownSignatureIdx === undefined
? log.lastSignature!
: log.signatureAfter[nextKnownSignatureIdx]!;
sentState[sessionID] =
(sentState[sessionID] || 0) + txsToAdd.length;
}
}
const piecesWithContent = pieces.filter(
(piece) => Object.keys(piece.new).length > 0 || piece.header
);
if (piecesWithContent.length === 0) {
return undefined;
}
return newContent;
return piecesWithContent;
}
getDependedOnCoValues(): RawCoID[] {
return this.header.ruleset.type === "group"
? expectGroupContent(this.getCurrentContent())
? expectGroup(this.getCurrentContent())
.keys()
.filter((k): k is AccountID => k.startsWith("co_"))
: this.header.ruleset.type === "ownedByGroup"
? [this.header.ruleset.group]
? [
this.header.ruleset.group,
...new Set(
Object.keys(this._sessions)
.map((sessionID) =>
accountOrAgentIDfromSessionID(
sessionID as SessionID
)
)
.filter(
(session): session is AccountID =>
isAccountID(session) && session !== this.id
)
),
]
: [];
}
}

View File

@@ -1,5 +1,5 @@
import { CoValueHeader } from "./coValueCore.js";
import { CoID } from "./coValue.js";
import { CoValueCore, CoValueHeader } from "../coValueCore.js";
import { CoID, CoValue } from "../coValue.js";
import {
AgentSecret,
SealerID,
@@ -11,11 +11,10 @@ import {
getAgentSealerSecret,
getAgentSignerID,
getAgentSignerSecret,
} from "./crypto.js";
import { AgentID } from "./ids.js";
import { CoMap } from "./coValues/coMap.js";
import { LocalNode } from "./node.js";
import { Group, GroupContent } from "./group.js";
} from "../crypto.js";
import { AgentID } from "../ids.js";
import { CoMap } from "./coMap.js";
import { Group, InviteSecret } from "./group.js";
export function accountHeaderForInitialAgentSecret(
agentSecret: AgentSecret
@@ -32,15 +31,15 @@ export function accountHeaderForInitialAgentSecret(
};
}
export class Account extends Group {
get id(): AccountID {
return this.underlyingMap.id as AccountID;
}
export class Account<
P extends Profile = Profile,
R extends CoMap = CoMap,
Meta extends AccountMeta = AccountMeta
> extends Group<P, R, Meta> {
getCurrentAgentID(): AgentID {
const agents = this.underlyingMap
.keys()
.filter((k): k is AgentID => k.startsWith("sealer_"));
const agents = this.keys().filter((k): k is AgentID =>
k.startsWith("sealer_")
);
if (agents.length !== 1) {
throw new Error(
@@ -64,22 +63,37 @@ export interface GeneralizedControlledAccount {
}
/** @hidden */
export class ControlledAccount
extends Account
export class ControlledAccount<
P extends Profile = Profile,
R extends CoMap = CoMap,
Meta extends AccountMeta = AccountMeta
>
extends Account<P, R, Meta>
implements GeneralizedControlledAccount
{
agentSecret: AgentSecret;
constructor(
agentSecret: AgentSecret,
groupMap: CoMap<AccountContent, AccountMeta>,
node: LocalNode
) {
super(groupMap, node);
constructor(core: CoValueCore, agentSecret: AgentSecret) {
super(core);
this.agentSecret = agentSecret;
}
/**
* Creates a new group (with the current account as the group's first admin).
* @category 1. High-level
*/
createGroup() {
return this.core.node.createGroup();
}
async acceptInvite<T extends CoValue>(
groupOrOwnedValueID: CoID<T>,
inviteSecret: InviteSecret
): Promise<void> {
return this.core.node.acceptInvite(groupOrOwnedValueID, inviteSecret);
}
currentAgentID(): AgentID {
return getAgentID(this.agentSecret);
}
@@ -136,17 +150,22 @@ export class AnonymousControlledAccount
}
}
export type AccountContent = GroupContent & { profile: CoID<Profile> };
export type AccountMeta = { type: "account" };
export type AccountMap = CoMap<AccountContent, AccountMeta>;
export type AccountID = CoID<AccountMap>;
export type AccountID = CoID<Account>;
export function isAccountID(id: AccountID | AgentID): id is AccountID {
return id.startsWith("co_");
}
export type ProfileContent = {
export type ProfileShape = {
name: string;
};
export type ProfileMeta = { type: "profile" };
export type Profile = CoMap<ProfileContent, ProfileMeta>;
export class Profile<Shape extends ProfileShape = ProfileShape, Meta extends ProfileMeta = ProfileMeta> extends CoMap<Shape, Meta> {
}
export type AccountMigration< P extends Profile = Profile,
R extends CoMap = CoMap,
Meta extends AccountMeta = AccountMeta> = (account: ControlledAccount<P, R, Meta>, profile: P) => void;

View File

@@ -1,9 +1,10 @@
import { JsonObject, JsonValue } from "../jsonValue.js";
import { CoID, ReadableCoValue, WriteableCoValue } from "../coValue.js";
import { CoID, CoValue, isCoValue } from "../coValue.js";
import { CoValueCore, accountOrAgentIDfromSessionID } from "../coValueCore.js";
import { SessionID, TransactionID } from "../ids.js";
import { Group } from "../group.js";
import { AccountID, isAccountID } from "../account.js";
import { AgentID, SessionID, TransactionID } from "../ids.js";
import { AccountID } from "./account.js";
import { parseJSON } from "../jsonStringify.js";
import { Group } from "./group.js";
type OpID = TransactionID & { changeIdx: number };
@@ -39,11 +40,16 @@ type DeletionEntry = {
deletionID: OpID;
} & DeletionOpPayload;
export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
implements ReadableCoValue
export class CoListView<
Item extends JsonValue = JsonValue,
Meta extends JsonObject | null = null
> implements CoValue
{
id: CoID<CoList<T, Meta>>;
/** @category 6. Meta */
id: CoID<this>;
/** @category 6. Meta */
type = "colist" as const;
/** @category 6. Meta */
core: CoValueCore;
/** @internal */
afterStart: OpID[];
@@ -53,7 +59,7 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
insertions: {
[sessionID: SessionID]: {
[txIdx: number]: {
[changeIdx: number]: InsertionEntry<T>;
[changeIdx: number]: InsertionEntry<Item>;
};
};
};
@@ -65,29 +71,18 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
};
};
};
/** @category 6. Meta */
readonly _item!: Item;
/** @internal */
constructor(core: CoValueCore) {
this.id = core.id as CoID<CoList<T, Meta>>;
this.id = core.id as CoID<this>;
this.core = core;
this.afterStart = [];
this.beforeEnd = [];
this.insertions = {};
this.deletionsByInsertion = {};
this.fillOpsFromCoValue();
}
get meta(): Meta {
return this.core.header.meta as Meta;
}
get group(): Group {
return this.core.getGroup();
}
/** @internal */
protected fillOpsFromCoValue() {
this.insertions = {};
this.deletionsByInsertion = {};
this.afterStart = [];
@@ -98,8 +93,10 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
changes,
madeAt,
} of this.core.getValidSortedTransactions()) {
for (const [changeIdx, changeUntyped] of changes.entries()) {
const change = changeUntyped as ListOpPayload<T>;
for (const [changeIdx, changeUntyped] of parseJSON(
changes
).entries()) {
const change = changeUntyped as ListOpPayload<Item>;
if (change.op === "pre" || change.op === "app") {
let sessionEntry = this.insertions[txID.sessionID];
@@ -199,8 +196,31 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
}
}
/** Get the item currently at `idx`. */
get(idx: number): T | undefined {
/** @category 6. Meta */
get meta(): Meta {
return this.core.header.meta as Meta;
}
/** @category 6. Meta */
get group(): Group {
return this.core.getGroup();
}
/**
* Not yet implemented
*
* @category 4. Time travel
*/
atTime(_time: number): this {
throw new Error("Not yet implemented");
}
/**
* Get the item currently at `idx`.
*
* @category 1. Reading
*/
get(idx: number): Item | undefined {
const entry = this.entries()[idx];
if (!entry) {
return undefined;
@@ -208,13 +228,26 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
return entry.value;
}
/** Returns the current items in the CoList as an array. */
asArray(): T[] {
/**
* Returns the current items in the CoList as an array.
*
* @category 1. Reading
**/
asArray(): Item[] {
return this.entries().map((entry) => entry.value);
}
entries(): { value: T; madeAt: number; opID: OpID }[] {
const arr: { value: T; madeAt: number; opID: OpID }[] = [];
/** @internal */
entries(): {
value: Item;
madeAt: number;
opID: OpID;
}[] {
const arr: {
value: Item;
madeAt: number;
opID: OpID;
}[] = [];
for (const opID of this.afterStart) {
this.fillArrayFromOpID(opID, arr);
}
@@ -227,7 +260,11 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
/** @internal */
private fillArrayFromOpID(
opID: OpID,
arr: { value: T; madeAt: number; opID: OpID }[]
arr: {
value: Item;
madeAt: number;
opID: OpID;
}[]
) {
const entry =
this.insertions[opID.sessionID]?.[opID.txIndex]?.[opID.changeIdx];
@@ -253,86 +290,115 @@ export class CoList<T extends JsonValue, Meta extends JsonObject | null = null>
}
}
/** Returns the accountID of the account that inserted value at the given index. */
whoInserted(idx: number): AccountID | undefined {
/**
* Returns the current items in the CoList as an array. (alias of `asArray`)
*
* @category 1. Reading
*/
toJSON(): Item[] {
return this.asArray();
}
/** @category 5. Edit history */
editAt(idx: number):
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value: Item;
}
| undefined {
const entry = this.entries()[idx];
if (!entry) {
return undefined;
}
const accountID = accountOrAgentIDfromSessionID(entry.opID.sessionID);
if (isAccountID(accountID)) {
return accountID;
} else {
return undefined;
const madeAt = new Date(entry.madeAt);
const by = accountOrAgentIDfromSessionID(entry.opID.sessionID);
const value = entry.value;
return {
by,
tx: {
sessionID: entry.opID.sessionID,
txIndex: entry.opID.txIndex,
},
at: madeAt,
value,
};
}
/** @category 5. Edit history */
deletionEdits(): {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
// TODO: add indices that are now before and after the deleted item
}[] {
const edits: {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
}[] = [];
for (const sessionID in this.deletionsByInsertion) {
const sessionEntry =
this.deletionsByInsertion[sessionID as SessionID];
for (const txIdx in sessionEntry) {
const txEntry = sessionEntry[Number(txIdx)];
for (const changeIdx in txEntry) {
const changeEntry = txEntry[Number(changeIdx)];
for (const deletion of changeEntry || []) {
const madeAt = new Date(deletion.madeAt);
const by = accountOrAgentIDfromSessionID(
deletion.deletionID.sessionID
);
edits.push({
by,
tx: deletion.deletionID,
at: madeAt,
});
}
}
}
}
return edits;
}
/** Returns the current items in the CoList as an array. (alias of `asArray`) */
toJSON(): T[] {
return this.asArray();
}
map<U>(mapper: (value: T, idx: number) => U): U[] {
return this.entries().map((entry, idx) => mapper(entry.value, idx));
}
filter<U extends T>(predicate: (value: T, idx: number) => value is U): U[];
filter(predicate: (value: T, idx: number) => boolean): T[] {
return this.entries()
.filter((entry, idx) => predicate(entry.value, idx))
.map((entry) => entry.value);
}
reduce<U>(
reducer: (accumulator: U, value: T, idx: number) => U,
initialValue: U
): U {
return this.entries().reduce(
(accumulator, entry, idx) => reducer(accumulator, entry.value, idx),
initialValue
);
}
subscribe(listener: (coMap: CoList<T, Meta>) => void): () => void {
/** @category 3. Subscription */
subscribe(listener: (coList: this) => void): () => void {
return this.core.subscribe((content) => {
listener(content as CoList<T, Meta>);
listener(content as this);
});
}
edit(
changer: (editable: WriteableCoList<T, Meta>) => void
): CoList<T, Meta> {
const editable = new WriteableCoList<T, Meta>(this.core);
changer(editable);
return new CoList(this.core);
}
}
export class WriteableCoList<
T extends JsonValue,
Meta extends JsonObject | null = null
export class CoList<
Item extends JsonValue = JsonValue,
Meta extends JsonObject | null = JsonObject | null
>
extends CoList<T, Meta>
implements WriteableCoValue
extends CoListView<Item, Meta>
implements CoValue
{
/** @internal */
edit(
_changer: (editable: WriteableCoList<T, Meta>) => void
): CoList<T, Meta> {
throw new Error("Already editing.");
}
/** Appends a new item after index `after`.
/** Returns a new version of this CoList with `item` appended after the item currently at index `after`.
*
* If `privacy` is `"private"` **(default)**, both `value` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
* If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, both `value` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers. */
* If `privacy` is `"trusting"`, `item` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Editing
**/
append(
after: number,
value: T,
item: Item,
after?: number,
privacy: "private" | "trusting" = "private"
): void {
): this {
const entries = this.entries();
after =
after === undefined
? entries.length > 0
? entries.length - 1
: 0
: 0;
let opIDBefore;
if (entries.length > 0) {
const entryBefore = entries[after];
@@ -350,44 +416,32 @@ export class WriteableCoList<
[
{
op: "app",
value,
value: isCoValue(item) ? item.id : item,
after: opIDBefore,
},
],
privacy
);
this.fillOpsFromCoValue();
}
/** Pushes a new item to the end of the list.
*
* If `privacy` is `"private"` **(default)**, both `value` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, both `value` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers. */
push(value: T, privacy: "private" | "trusting" = "private"): void {
// TODO: optimize
const entries = this.entries();
this.append(
entries.length > 0 ? entries.length - 1 : 0,
value,
privacy
);
return new CoList(this.core) as this;
}
/**
* Prepends a new item before index `before`.
* Returns a new version of this CoList with `item` prepended before the item currently at index `before`.
*
* If `privacy` is `"private"` **(default)**, both `value` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
* If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, both `value` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
* If `privacy` is `"trusting"`, `item` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Editing
*/
prepend(
before: number,
value: T,
item: Item,
before?: number,
privacy: "private" | "trusting" = "private"
): void {
): this {
const entries = this.entries();
before = before === undefined ? 0 : before;
let opIDAfter;
if (entries.length > 0) {
const entryAfter = entries[before];
@@ -409,22 +463,25 @@ export class WriteableCoList<
[
{
op: "pre",
value,
value: isCoValue(item) ? item.id : item,
before: opIDAfter,
},
],
privacy
);
this.fillOpsFromCoValue();
return new CoList(this.core) as this;
}
/** Deletes the item at index `at` from the list.
/** Returns a new version of this CoList with the item at index `at` deleted from the list.
*
* If `privacy` is `"private"` **(default)**, the fact of this deletion is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, the fact of this deletion is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers. */
delete(at: number, privacy: "private" | "trusting" = "private"): void {
* If `privacy` is `"trusting"`, the fact of this deletion is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Editing
**/
delete(at: number, privacy: "private" | "trusting" = "private"): this {
const entries = this.entries();
const entry = entries[at];
if (!entry) {
@@ -440,6 +497,96 @@ export class WriteableCoList<
privacy
);
this.fillOpsFromCoValue();
return new CoList(this.core) as this;
}
/** @category 2. Editing */
mutate(mutator: (mutable: MutableCoList<Item, Meta>) => void): this {
const mutable = new MutableCoList<Item, Meta>(this.core);
mutator(mutable);
return new CoList(this.core) as this;
}
/** @deprecated Use `mutate` instead. */
edit(mutator: (mutable: MutableCoList<Item, Meta>) => void): this {
return this.mutate(mutator);
}
}
export class MutableCoList<
Item extends JsonValue = JsonValue,
Meta extends JsonObject | null = JsonObject | null
>
extends CoListView<Item, Meta>
implements CoValue
{
/** Appends `item` after the item currently at index `after`.
*
* If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, `item` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Mutating
**/
append(
item: Item,
after?: number,
privacy: "private" | "trusting" = "private"
): void {
const listAfter = CoList.prototype.append.call(
this,
item,
after,
privacy
) as CoList<Item, Meta>;
this.afterStart = listAfter.afterStart;
this.beforeEnd = listAfter.beforeEnd;
this.insertions = listAfter.insertions;
this.deletionsByInsertion = listAfter.deletionsByInsertion;
}
/** Prepends `item` before the item currently at index `before`.
*
* If `privacy` is `"private"` **(default)**, `item` is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, `item` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* * @category 2. Mutating
**/
prepend(
item: Item,
before?: number,
privacy: "private" | "trusting" = "private"
): void {
const listAfter = CoList.prototype.prepend.call(
this,
item,
before,
privacy
) as CoList<Item, Meta>;
this.afterStart = listAfter.afterStart;
this.beforeEnd = listAfter.beforeEnd;
this.insertions = listAfter.insertions;
this.deletionsByInsertion = listAfter.deletionsByInsertion;
}
/** Deletes the item at index `at` from the list.
*
* If `privacy` is `"private"` **(default)**, the fact of this deletion is encrypted in the transaction, only readable by other members of the group this `CoList` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, the fact of this deletion is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* * @category 2. Mutating
**/
delete(at: number, privacy: "private" | "trusting" = "private"): void {
const listAfter = CoList.prototype.delete.call(
this,
at,
privacy
) as CoList<Item, Meta>;
this.afterStart = listAfter.afterStart;
this.beforeEnd = listAfter.beforeEnd;
this.insertions = listAfter.insertions;
this.deletionsByInsertion = listAfter.deletionsByInsertion;
}
}

View File

@@ -1,72 +1,70 @@
import { JsonObject, JsonValue } from '../jsonValue.js';
import { TransactionID } from '../ids.js';
import { CoID, ReadableCoValue, WriteableCoValue } from '../coValue.js';
import { CoValueCore, accountOrAgentIDfromSessionID } from '../coValueCore.js';
import { AccountID, isAccountID } from '../account.js';
import { Group } from '../group.js';
import { JsonObject, JsonValue } from "../jsonValue.js";
import { AgentID, TransactionID } from "../ids.js";
import { CoID, CoValue, isCoValue } from "../coValue.js";
import { CoValueCore, accountOrAgentIDfromSessionID } from "../coValueCore.js";
import { AccountID } from "./account.js";
import { parseJSON } from "../jsonStringify.js";
import { Group } from "./group.js";
type MapOp<K extends string, V extends JsonValue> = {
type MapOp<K extends string, V extends JsonValue | undefined> = {
txID: TransactionID;
madeAt: number;
changeIdx: number;
} & MapOpPayload<K, V>;
// TODO: add after TransactionID[] for conflicts/ordering
export type MapOpPayload<K extends string, V extends JsonValue> = {
op: "set";
key: K;
value: V;
} |
export type MapOpPayload<K extends string, V extends JsonValue | undefined> =
| {
op: "set";
key: K;
value: V;
}
| {
op: "del";
key: K;
};
export class CoMapView<
Shape extends { [key: string]: JsonValue | undefined } = {
[key: string]: JsonValue | undefined;
},
Meta extends JsonObject | null = JsonObject | null
> implements CoValue
{
op: "del";
key: K;
};
export type MapK<M extends { [key: string]: JsonValue; }> = keyof M & string;
export type MapV<M extends { [key: string]: JsonValue; }> = M[MapK<M>];
export type MapM<M extends { [key: string]: JsonValue; }> = {
[KK in MapK<M>]: M[KK];
}
/** A collaborative map with precise shape `M` and optional static metadata `Meta` */
export class CoMap<
M extends { [key: string]: JsonValue; },
Meta extends JsonObject | null = null,
> implements ReadableCoValue {
id: CoID<CoMap<MapM<M>, Meta>>;
/** @category 6. Meta */
id: CoID<this>;
/** @category 6. Meta */
type = "comap" as const;
/** @category 6. Meta */
core: CoValueCore;
/** @internal */
ops: {
[KK in MapK<M>]?: MapOp<KK, M[KK]>[];
[Key in keyof Shape & string]?: MapOp<Key, Shape[Key]>[];
};
/** @internal */
atTimeFilter?: number = undefined;
/** @category 6. Meta */
readonly _shape!: Shape;
/** @internal */
constructor(core: CoValueCore) {
this.id = core.id as CoID<CoMap<MapM<M>, Meta>>;
constructor(
core: CoValueCore,
options?: { ignorePrivateTransactions: true }
) {
this.id = core.id as CoID<this>;
this.core = core;
this.ops = {};
this.fillOpsFromCoValue();
}
get meta(): Meta {
return this.core.header.meta as Meta;
}
get group(): Group {
return this.core.getGroup();
}
/** @internal */
protected fillOpsFromCoValue() {
this.ops = {};
for (const { txID, changes, madeAt } of this.core.getValidSortedTransactions()) {
for (const [changeIdx, changeUntyped] of (
for (const { txID, changes, madeAt } of core.getValidSortedTransactions(
options
)) {
for (const [changeIdx, changeUntyped] of parseJSON(
changes
).entries()) {
const change = changeUntyped as MapOpPayload<MapK<M>, MapV<M>>;
const change = changeUntyped as MapOpPayload<
keyof Shape & string,
Shape[keyof Shape & string]
>;
let entries = this.ops[change.key];
if (!entries) {
entries = [];
@@ -76,175 +74,347 @@ export class CoMap<
txID,
madeAt,
changeIdx,
...(change as MapOpPayload<MapK<M>, MapV<M>>),
...(change as MapOpPayload<
keyof Shape & string,
Shape[keyof Shape & string]
>),
});
}
}
}
keys(): MapK<M>[] {
return Object.keys(this.ops) as MapK<M>[];
/** @category 6. Meta */
get meta(): Meta {
return this.core.header.meta as Meta;
}
/** Returns the current value for the given key. */
get<K extends MapK<M>>(key: K): M[K] | undefined {
const ops = this.ops[key];
/** @category 6. Meta */
get group(): Group {
return this.core.getGroup();
}
/** @category 4. Time travel */
atTime(time: number): this {
const clone = Object.create(this) as this;
clone.id = this.id;
clone.type = this.type;
clone.core = this.core;
clone.ops = this.ops;
clone.atTimeFilter = time;
return clone;
}
/** @internal */
timeFilteredOps<K extends keyof Shape & string>(
key: K
): MapOp<K, Shape[K]>[] | undefined {
if (this.atTimeFilter) {
return this.ops[key]?.filter(
(op) => op.madeAt <= this.atTimeFilter!
);
} else {
return this.ops[key];
}
}
/**
* Get all keys currently in the map.
*
* @category 1. Reading */
keys<K extends (keyof Shape & string) = (keyof Shape & string)>(): K[] {
const keys = Object.keys(this.ops) as K[];
if (this.atTimeFilter) {
return keys.filter((key) => this.timeFilteredOps(key)?.length);
} else {
return keys;
}
}
/**
* Returns the current value for the given key.
*
* @category 1. Reading
**/
get<K extends keyof Shape & string>(key: K): Shape[K] | undefined {
const ops = this.timeFilteredOps(key);
if (!ops) {
return undefined;
}
const lastEntry = ops[ops.length - 1]!;
const includeUntil = this.atTimeFilter;
const lastEntry = includeUntil
? ops.findLast((entry) => entry.madeAt <= includeUntil)
: ops[ops.length - 1]!;
if (lastEntry.op === "del") {
if (lastEntry?.op === "del") {
return undefined;
} else {
return lastEntry.value;
return lastEntry?.value;
}
}
getAtTime<K extends MapK<M>>(key: K, time: number): M[K] | undefined {
const ops = this.ops[key];
if (!ops) {
return undefined;
}
const lastOpBeforeOrAtTime = ops.findLast((op) => op.madeAt <= time);
if (!lastOpBeforeOrAtTime) {
return undefined;
}
if (lastOpBeforeOrAtTime.op === "del") {
return undefined;
} else {
return lastOpBeforeOrAtTime.value;
}
}
/** Returns the accountID of the last account to modify the value for the given key. */
whoEdited<K extends MapK<M>>(key: K): AccountID | undefined {
const tx = this.getLastTxID(key);
if (!tx) {
return undefined;
}
const accountID = accountOrAgentIDfromSessionID(tx.sessionID);
if (isAccountID(accountID)) {
return accountID;
} else {
return undefined;
}
}
getLastTxID<K extends MapK<M>>(key: K): TransactionID | undefined {
const ops = this.ops[key];
if (!ops) {
return undefined;
}
const lastEntry = ops[ops.length - 1]!;
return lastEntry.txID;
}
getLastEntry<K extends MapK<M>>(key: K): { at: number; txID: TransactionID; value: M[K]; } | undefined {
const ops = this.ops[key];
if (!ops) {
return undefined;
}
const lastEntry = ops[ops.length - 1]!;
if (lastEntry.op === "del") {
return undefined;
} else {
return { at: lastEntry.madeAt, txID: lastEntry.txID, value: lastEntry.value };
}
}
getHistory<K extends MapK<M>>(key: K): { at: number; txID: TransactionID; value: M[K] | undefined; }[] {
const ops = this.ops[key];
if (!ops) {
return [];
}
const history: { at: number; txID: TransactionID; value: M[K] | undefined; }[] = [];
for (const op of ops) {
if (op.op === "del") {
history.push({ at: op.madeAt, txID: op.txID, value: undefined });
} else {
history.push({ at: op.madeAt, txID: op.txID, value: op.value });
}
}
return history;
}
toJSON(): JsonObject {
const json: JsonObject = {};
/** @category 1. Reading */
asObject(): {
[K in keyof Shape & string]: Shape[K];
} {
const object: Partial<{
[K in keyof Shape & string]: Shape[K];
}> = {};
for (const key of this.keys()) {
const value = this.get(key);
if (value !== undefined) {
json[key] = value;
object[key] = value;
}
}
return json;
return object as {
[K in keyof Shape & string]: Shape[K];
};
}
subscribe(listener: (coMap: CoMap<M, Meta>) => void): () => void {
/** @category 1. Reading */
toJSON(): {
[K in keyof Shape & string]: Shape[K];
} {
return this.asObject();
}
/** @category 5. Edit history */
nthEditAt<K extends keyof Shape & string>(
key: K,
n: number
):
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value?: Shape[K];
}
| undefined {
const ops = this.timeFilteredOps(key);
if (!ops || ops.length <= n) {
return undefined;
}
const entry = ops[n]!;
if (this.atTimeFilter && entry.madeAt > this.atTimeFilter) {
return undefined;
}
return {
by: accountOrAgentIDfromSessionID(entry.txID.sessionID),
tx: entry.txID,
at: new Date(entry.madeAt),
value: entry.op === "del" ? undefined : entry.value,
};
}
/** @category 5. Edit history */
lastEditAt<K extends keyof Shape & string>(
key: K
):
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value?: Shape[K];
}
| undefined {
const ops = this.timeFilteredOps(key);
if (!ops || ops.length === 0) {
return undefined;
}
return this.nthEditAt(key, ops.length - 1);
}
/** @category 5. Edit history */
*editsAt<K extends keyof Shape & string>(key: K) {
const ops = this.timeFilteredOps(key);
if (!ops) {
return;
}
for (let i = 0; i < ops.length; i++) {
yield this.nthEditAt(key, i)!;
}
}
/** @category 3. Subscription */
subscribe(listener: (coMap: this) => void): () => void {
return this.core.subscribe((content) => {
listener(content as CoMap<M, Meta>);
listener(content as this);
});
}
edit(changer: (editable: WriteableCoMap<M, Meta>) => void): CoMap<M, Meta> {
const editable = new WriteableCoMap<M, Meta>(this.core);
changer(editable);
return new CoMap(this.core);
}
}
export class WriteableCoMap<
M extends { [key: string]: JsonValue; },
Meta extends JsonObject | null = null,
> extends CoMap<M, Meta> implements WriteableCoValue {
/** @internal */
edit(_changer: (editable: WriteableCoMap<M, Meta>) => void): CoMap<M, Meta> {
throw new Error("Already editing.");
/** A collaborative map with precise shape `Shape` and optional static metadata `Meta` */
export class CoMap<
Shape extends { [key: string]: JsonValue | undefined } = {
[key: string]: JsonValue | undefined;
},
Meta extends JsonObject | null = JsonObject | null
>
extends CoMapView<Shape, Meta>
implements CoValue
{
/** Returns a new version of this CoMap with a new value for the given key.
*
* If `privacy` is `"private"` **(default)**, both `key` and `value` are encrypted in the transaction, only readable by other members of the group this `CoMap` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, both `key` and `value` are stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Editing
**/
set<K extends keyof Shape & string>(
key: K,
value: Shape[K],
privacy?: "private" | "trusting"
): this;
set(
kv: {
[K in keyof Shape & string]?: Shape[K];
},
privacy?: "private" | "trusting"
): this;
set<K extends keyof Shape & string>(
...args:
| [
{
[K in keyof Shape & string]?: Shape[K];
},
("private" | "trusting")?
]
| [K, Shape[K], ("private" | "trusting")?]
): this {
if (typeof args[0] === "string") {
const [key, value, privacy = "private"] = args;
this.core.makeTransaction(
[
{
op: "set",
key,
value: isCoValue(value) ? value.id : value,
},
],
privacy
);
} else {
const [kv, privacy = "private"] = args as [
{
[K in keyof Shape & string]: Shape[K] extends CoValue
? Shape[K] | CoID<Shape[K]>
: Shape[K];
},
"private" | "trusting" | undefined
];
for (const [key, value] of Object.entries(kv)) {
this.core.makeTransaction(
[
{
op: "set",
key,
value: isCoValue(value) ? value.id : value,
},
],
privacy
);
}
}
return new CoMap(this.core) as this;
}
/** Returns a new version of this CoMap with the given key deleted (setting it to undefined).
*
* If `privacy` is `"private"` **(default)**, `key` is encrypted in the transaction, only readable by other members of the group this `CoMap` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, `key` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Editing
**/
delete(
key: keyof Shape & string,
privacy: "private" | "trusting" = "private"
): this {
this.core.makeTransaction(
[
{
op: "del",
key,
},
],
privacy
);
return new CoMap(this.core) as this;
}
/** @category 2. Editing */
mutate(mutator: (mutable: MutableCoMap<Shape, Meta>) => void): this {
const mutable = new MutableCoMap<Shape, Meta>(this.core);
mutator(mutable);
return new (this.constructor as new (core: CoValueCore) => this)(
this.core
);
}
/** @deprecated Use `mutate` instead. */
edit(mutator: (mutable: MutableCoMap<Shape, Meta>) => void): this {
return this.mutate(mutator);
}
}
export class MutableCoMap<
Shape extends { [key: string]: JsonValue | undefined } = {
[key: string]: JsonValue | undefined;
},
Meta extends JsonObject | null = JsonObject | null
>
extends CoMapView<Shape, Meta>
implements CoValue
{
/** Sets a new value for the given key.
*
* If `privacy` is `"private"` **(default)**, both `key` and `value` are encrypted in the transaction, only readable by other members of the group this `CoMap` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, both `key` and `value` are stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers. */
set<K extends MapK<M>>(key: K, value: M[K], privacy: "private" | "trusting" = "private"): void {
this.core.makeTransaction([
{
op: "set",
key,
value,
},
], privacy);
this.fillOpsFromCoValue();
* If `privacy` is `"trusting"`, both `key` and `value` are stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
*
* @category 2. Mutation
*/
set<K extends keyof Shape & string>(
key: K,
value: Shape[K],
privacy: "private" | "trusting" = "private"
): void {
// eslint-disable-next-line @typescript-eslint/ban-types
const after = (CoMap.prototype.set as Function).call(
this,
key,
value,
privacy
) as CoMap<Shape, Meta>;
this.ops = after.ops;
}
/** Deletes the value for the given key (setting it to undefined).
*
* If `privacy` is `"private"` **(default)**, `key` is encrypted in the transaction, only readable by other members of the group this `CoMap` belongs to. Not even sync servers can see the content in plaintext.
*
* If `privacy` is `"trusting"`, `key` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers. */
delete(key: MapK<M>, privacy: "private" | "trusting" = "private"): void {
this.core.makeTransaction([
{
op: "del",
key,
},
], privacy);
this.fillOpsFromCoValue();
* If `privacy` is `"trusting"`, `key` is stored in plaintext in the transaction, visible to everyone who gets a hold of it, including sync servers.
* @category 2. Mutation
*/
delete(
key: keyof Shape & string,
privacy: "private" | "trusting" = "private"
): void {
const after = CoMap.prototype.delete.call(this, key, privacy) as CoMap<
Shape,
Meta
>;
this.ops = after.ops;
}
}

View File

@@ -1,11 +1,13 @@
import { JsonObject, JsonValue } from "../jsonValue.js";
import { CoID, ReadableCoValue, WriteableCoValue } from "../coValue.js";
import { CoValueCore } from "../coValueCore.js";
import { Group } from "../group.js";
import { SessionID } from "../ids.js";
import { CoValue, CoID, isCoValue } from "../coValue.js";
import { CoValueCore, accountOrAgentIDfromSessionID } from "../coValueCore.js";
import { Group } from "./group.js";
import { AgentID, SessionID, TransactionID } from "../ids.js";
import { base64URLtoBytes, bytesToBase64url } from "../base64url.js";
import { AccountID, isAccountID } from "./account.js";
import { parseJSON } from "../jsonStringify.js";
export type BinaryChunkInfo = {
export type BinaryStreamInfo = {
mimeType: string;
fileName?: string;
totalSizeBytes?: number;
@@ -13,11 +15,11 @@ export type BinaryChunkInfo = {
export type BinaryStreamStart = {
type: "start";
} & BinaryChunkInfo;
} & BinaryStreamInfo;
export type BinaryStreamChunk = {
type: "chunk";
chunk: `U${string}`;
chunk: `binary_U${string}`;
};
export type BinaryStreamEnd = {
@@ -31,20 +33,27 @@ export type BinaryStreamItem =
| BinaryStreamChunk
| BinaryStreamEnd;
export class CoStream<
T extends JsonValue,
Meta extends JsonObject | null = null
> implements ReadableCoValue
export type CoStreamItem<Item extends JsonValue> = {
value: Item;
tx: TransactionID;
madeAt: number;
};
export class CoStreamView<
Item extends JsonValue = JsonValue,
Meta extends JsonObject | null = JsonObject | null
> implements CoValue
{
id: CoID<CoStream<T, Meta>>;
id: CoID<this>;
type = "costream" as const;
core: CoValueCore;
items: {
[key: SessionID]: T[];
[key: SessionID]: CoStreamItem<Item>[];
};
readonly _item!: Item;
constructor(core: CoValueCore) {
this.id = core.id as CoID<CoStream<T, Meta>>;
this.id = core.id as CoID<this>;
this.core = core;
this.items = {};
this.fillFromCoValue();
@@ -58,27 +67,35 @@ export class CoStream<
return this.core.getGroup();
}
/** Not yet implemented */
atTime(_time: number): this {
throw new Error("Not yet implemented");
}
/** @internal */
protected fillFromCoValue() {
this.items = {};
for (const {
txID,
madeAt,
changes,
} of this.core.getValidSortedTransactions()) {
for (const changeUntyped of changes) {
const change = changeUntyped as T;
for (const changeUntyped of parseJSON(changes)) {
const change = changeUntyped as Item;
let entries = this.items[txID.sessionID];
if (!entries) {
entries = [];
this.items[txID.sessionID] = entries;
}
entries.push(change);
entries.push({ value: change, madeAt, tx: txID });
}
}
}
getSingleStream(): T[] | undefined {
getSingleStream():
| (Item)[]
| undefined {
if (Object.keys(this.items).length === 0) {
return undefined;
} else if (Object.keys(this.items).length !== 1) {
@@ -87,41 +104,200 @@ export class CoStream<
);
}
return Object.values(this.items)[0];
return Object.values(this.items)[0]?.map((item) => item.value);
}
sessions(): SessionID[] {
return Object.keys(this.items) as SessionID[];
}
accounts(): Set<AccountID> {
return new Set(this.sessions().map(accountOrAgentIDfromSessionID).filter(isAccountID));
}
nthItemIn(
sessionID: SessionID,
n: number
):
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value: Item;
}
| undefined {
const items = this.items[sessionID];
if (!items) return;
const item = items[n];
if (!item) return;
return {
by: accountOrAgentIDfromSessionID(sessionID),
tx: item.tx,
at: new Date(item.madeAt),
value: item.value,
};
}
lastItemIn(sessionID: SessionID):
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value: Item;
}
| undefined {
const items = this.items[sessionID];
if (!items) return;
return this.nthItemIn(sessionID, items.length - 1);
}
*itemsIn(sessionID: SessionID) {
const items = this.items[sessionID];
if (!items) return;
for (const item of items) {
yield {
by: accountOrAgentIDfromSessionID(sessionID),
tx: item.tx,
at: new Date(item.madeAt),
value: item.value as Item,
};
}
}
lastItemBy(account: AccountID | AgentID):
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value: Item;
}
| undefined {
let latestItem:
| {
by: AccountID | AgentID;
tx: TransactionID;
at: Date;
value: Item;
}
| undefined;
for (const sessionID of Object.keys(this.items)) {
if (sessionID.startsWith(account)) {
const item = this.lastItemIn(sessionID as SessionID);
if (!item) continue;
if (!latestItem || item.at > latestItem.at) {
latestItem = {
by: item.by,
tx: item.tx,
at: item.at,
value: item.value,
};
}
}
}
return latestItem;
}
*itemsBy(account: AccountID | AgentID) {
// TODO: this can be made more lazy without a huge collect and sort
const items = [
...Object.keys(this.items).flatMap((sessionID) =>
sessionID.startsWith(account)
? [...this.itemsIn(sessionID as SessionID)].map((item) => ({
in: sessionID as SessionID,
...item,
}))
: []
),
];
items.sort((a, b) => a.at.getTime() - b.at.getTime());
for (const item of items) {
yield item;
}
}
toJSON(): {
[key: SessionID]: T[];
[key: SessionID]: (Item )[];
} {
return this.items;
return Object.fromEntries(
Object.entries(this.items).map(([sessionID, items]) => [
sessionID,
items.map((item) => item.value),
])
);
}
subscribe(listener: (coMap: CoStream<T, Meta>) => void): () => void {
subscribe(listener: (coStream: this) => void): () => void {
return this.core.subscribe((content) => {
listener(content as CoStream<T, Meta>);
listener(content as this);
});
}
edit(
changer: (editable: WriteableCoStream<T, Meta>) => void
): CoStream<T, Meta> {
const editable = new WriteableCoStream<T, Meta>(this.core);
changer(editable);
return new CoStream(this.core);
}
}
export class BinaryCoStream<
export class CoStream<
Item extends JsonValue = JsonValue,
Meta extends JsonObject | null = JsonObject | null
>
extends CoStreamView<Item, Meta>
implements CoValue
{
push(
item: Item,
privacy: "private" | "trusting" = "private"
): this {
this.core.makeTransaction([isCoValue(item) ? item.id : item], privacy);
return new CoStream(this.core) as this;
}
mutate(mutator: (mutable: MutableCoStream<Item, Meta>) => void): this {
const mutable = new MutableCoStream<Item, Meta>(this.core);
mutator(mutable);
return new CoStream(this.core) as this;
}
/** @deprecated Use `mutate` instead. */
edit(mutator: (mutable: MutableCoStream<Item, Meta>) => void): this {
return this.mutate(mutator);
}
}
export class MutableCoStream<
Item extends JsonValue,
Meta extends JsonObject | null = JsonObject | null
>
extends CoStreamView<Item, Meta>
implements CoValue
{
push(
item: Item,
privacy: "private" | "trusting" = "private"
) {
this.core.makeTransaction([isCoValue(item) ? item.id : item], privacy);
this.fillFromCoValue();
}
}
const binary_U_prefixLength = 8; // "binary_U".length;
export class BinaryCoStreamView<
Meta extends BinaryCoStreamMeta = { type: "binary" }
>
extends CoStream<BinaryStreamItem, Meta>
implements ReadableCoValue
extends CoStreamView<BinaryStreamItem, Meta>
implements CoValue
{
id!: CoID<BinaryCoStream<Meta>>;
id!: CoID<this>;
getBinaryChunks():
| (BinaryChunkInfo & { chunks: Uint8Array[]; finished: boolean })
getBinaryChunks(
allowUnfinished?: boolean
):
| (BinaryStreamInfo & { chunks: Uint8Array[]; finished: boolean })
| undefined {
// const before = performance.now();
const items = this.getSingleStream();
if (!items) return;
@@ -133,17 +309,19 @@ export class BinaryCoStream<
return;
}
const end = items[items.length - 1];
if (end?.type !== "end" && !allowUnfinished) return;
const chunks: Uint8Array[] = [];
let finished = false;
// let totalLength = 0;
for (const item of items.slice(1)) {
if (item.type === "end") {
return {
mimeType: start.mimeType,
fileName: start.fileName,
totalSizeBytes: start.totalSizeBytes,
chunks,
finished: true,
};
finished = true;
break;
}
if (item.type !== "chunk") {
@@ -151,70 +329,110 @@ export class BinaryCoStream<
return undefined;
}
chunks.push(base64URLtoBytes(item.chunk.slice(1)));
const chunk = base64URLtoBytes(
item.chunk.slice(binary_U_prefixLength)
);
// totalLength += chunk.length;
chunks.push(chunk);
}
// const after = performance.now();
// console.log(
// "getBinaryChunks bandwidth in MB/s",
// (1000 * totalLength) / (after - before) / (1024 * 1024)
// );
return {
mimeType: start.mimeType,
fileName: start.fileName,
totalSizeBytes: start.totalSizeBytes,
chunks,
finished: false,
finished,
};
}
edit(
changer: (editable: WriteableBinaryCoStream<Meta>) => void
): BinaryCoStream<Meta> {
const editable = new WriteableBinaryCoStream<Meta>(this.core);
changer(editable);
return new BinaryCoStream(this.core);
}
}
export class WriteableCoStream<
T extends JsonValue,
Meta extends JsonObject | null = null
>
extends CoStream<T, Meta>
implements WriteableCoValue
{
/** @internal */
edit(
_changer: (editable: WriteableCoStream<T, Meta>) => void
): CoStream<T, Meta> {
throw new Error("Already editing.");
}
push(item: T, privacy: "private" | "trusting" = "private") {
this.core.makeTransaction([item], privacy);
this.fillFromCoValue();
}
}
export class WriteableBinaryCoStream<
export class BinaryCoStream<
Meta extends BinaryCoStreamMeta = { type: "binary" }
>
extends BinaryCoStream<Meta>
implements WriteableCoValue
extends BinaryCoStreamView<Meta>
implements CoValue
{
/** @internal */
edit(
_changer: (editable: WriteableBinaryCoStream<Meta>) => void
): BinaryCoStream<Meta> {
throw new Error("Already editing.");
}
/** @internal */
push(
item: BinaryStreamItem,
privacy: "private" | "trusting" = "private"
) {
WriteableCoStream.prototype.push.call(this, item, privacy);
): this {
this.core.makeTransaction([item], privacy);
return new BinaryCoStream(this.core) as this;
}
startBinaryStream(
settings: BinaryChunkInfo,
settings: BinaryStreamInfo,
privacy: "private" | "trusting" = "private"
): this {
return this.push(
{
type: "start",
...settings,
} satisfies BinaryStreamStart,
privacy
);
}
pushBinaryStreamChunk(
chunk: Uint8Array,
privacy: "private" | "trusting" = "private"
): this {
// const before = performance.now();
return this.push(
{
type: "chunk",
chunk: `binary_U${bytesToBase64url(chunk)}`,
} satisfies BinaryStreamChunk,
privacy
);
// const after = performance.now();
// console.log(
// "pushBinaryStreamChunk bandwidth in MB/s",
// (1000 * chunk.length) / (after - before) / (1024 * 1024)
// );
}
endBinaryStream(privacy: "private" | "trusting" = "private"): this {
return this.push(
{
type: "end",
} satisfies BinaryStreamEnd,
privacy
);
}
mutate(mutator: (mutable: MutableBinaryCoStream<Meta>) => void): this {
const mutable = new MutableBinaryCoStream<Meta>(this.core);
mutator(mutable);
return new BinaryCoStream(this.core) as this;
}
/** @deprecated Use `mutate` instead. */
edit(mutator: (mutable: MutableBinaryCoStream<Meta>) => void): this {
return this.mutate(mutator);
}
}
export class MutableBinaryCoStream<
Meta extends BinaryCoStreamMeta = { type: "binary" }
>
extends BinaryCoStreamView<Meta>
implements CoValue
{
/** @internal */
push(item: BinaryStreamItem, privacy: "private" | "trusting" = "private") {
MutableCoStream.prototype.push.call(this, item, privacy);
}
startBinaryStream(
settings: BinaryStreamInfo,
privacy: "private" | "trusting" = "private"
) {
this.push(
@@ -230,13 +448,19 @@ export class WriteableBinaryCoStream<
chunk: Uint8Array,
privacy: "private" | "trusting" = "private"
) {
// const before = performance.now();
this.push(
{
type: "chunk",
chunk: `U${bytesToBase64url(chunk)}`,
chunk: `binary_U${bytesToBase64url(chunk)}`,
} satisfies BinaryStreamChunk,
privacy
);
// const after = performance.now();
// console.log(
// "pushBinaryStreamChunk bandwidth in MB/s",
// (1000 * chunk.length) / (after - before) / (1024 * 1024)
// );
}
endBinaryStream(privacy: "private" | "trusting" = "private") {

Some files were not shown because too many files have changed in this diff Show More