Compare commits

..

9 Commits

Author SHA1 Message Date
Anselm
5dfd74d7f5 Publish
- jazz-example-todo@0.0.5
 - jazz-react@0.0.11
 - jazz-react-auth-local@0.0.8
2023-08-17 14:02:50 +01:00
Anselm
5fbd598cfe Fix missing dependency 2023-08-17 14:01:22 +01:00
Anselm
d54b53737d Try removing awareness of workspace 2023-08-17 13:59:35 +01:00
Anselm
0f0cd08c36 Try removing local yarn lock 2023-08-17 13:51:11 +01:00
Anselm
4bb3834333 Try newer node version 2023-08-17 13:45:31 +01:00
Anselm
b9007dd1b5 Publish
- jazz-example-todo@0.0.4
 - cojson@0.0.18
 - jazz-react@0.0.10
 - jazz-react-auth-local@0.0.7
 - jazz-storage-indexeddb@0.0.5
2023-08-17 13:43:46 +01:00
Anselm
ee1e5b06e4 Fix bundling of packages 2023-08-17 13:43:30 +01:00
Anselm
fae290c4cf Add exports to cojson 2023-08-17 12:36:58 +01:00
Anselm
381d68019f Try deploying jazz-example 2023-08-17 12:33:28 +01:00
84 changed files with 1482 additions and 11940 deletions

View File

@@ -71,7 +71,11 @@ jobs:
export DOCKER_PASSWORD=${{ secrets.DOCKER_PULL_PAT }};
export DOCKER_TAG=${{ env.DOCKER_TAG }};
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;
for region in ${{ vars.DEPLOY_REGIONS }}
do
export REGION=$region;
envsubst '${DOCKER_USER} ${DOCKER_PASSWORD} ${DOCKER_TAG} ${BRANCH_SUFFIX} ${BRANCH_SUBDOMAIN} ${REGION}' < job-template.nomad > job-instance.nomad;
cat job-instance.nomad;
NOMAD_ADDR='${{ secrets.NOMAD_ADDR }}' nomad job run job-instance.nomad;
done
working-directory: ./examples/todo

View File

@@ -1,3 +0,0 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

339
README.md
View File

@@ -1,338 +1,9 @@
# Jazz - instant sync
Homepage: [jazz.tools](https://jazz.tools) &mdash; [Discord](https://discord.gg/utDMjHYg42)
Jazz is an open-source toolkit for telepathic data.
Jazz is an open-source toolkit for *secure telepathic data.*
Ship faster and simplify frontend, backend & devops by building with Telepathic Data.
Get real-time multiplayer and cross-device sync for free.
- Ship faster & simplify your frontend and backend
- Get cross-device sync, real-time collaboration & offline support for free
[Jazz Global Mesh](https://jazz.tools/mesh) is serverless sync & storage for Jazz apps. (currently free!)
## What is Secure Telepathic Data?
**Telepathic** means:
- **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.)
**Secure** means:
- **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.
## How to build an app with Jazz?
### Building a new app, completely with Jazz
It's still a bit early, but these are the rough steps:
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).
The best example is currently the [Todo List app](#example-app-todo-list).
### Gradually adding Jazz to an existing app
Coming soon: Jazz will support gradual adoption by integrating with your existing UI, auth and database.
## Example App: Todo List
The best example of Jazz is currently the Todo List app.
- Live version: https://example-todo.jazz.tools
- Source code: [`./examples/todo`](./examples/todo). See the README there for a walk-through and running instructions.
# API Reference
Note: Since it's early days, this is the only source of documentation so far.
If you want to build something with Jazz, [join the Jazz Discord](https://discord.gg/utDMjHYg42) for encouragement and help!
## Overview: Main Packages
**`cojson`**
A library implementing abstractions and protocols for "Collaborative JSON". This will soon be standardized and forms the basis of secure telepathic data.
**`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.
### Supporting packages
<small>
**`cojson-simple-sync`**
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`**
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>
## `CoJSON`
CoJSON is the core implementation of secure telepathic data. It provides abstractions for Collaborative JSON values ("`CoValues`"), groups for permission management and a protocol for syncing between nodes. Our goal is to standardise CoJSON soon and port it to other languages and platforms.
---
### `LocalNode`
A `LocalNode` represents a local view of a set of loaded `CoValue`s, from the perspective of a particular account (or primitive cryptographic agent).
A `LocalNode` can have peers that it syncs to, for example some form of local persistence, or a sync server, such as `sync.jazz.tools` (Jazz Global Mesh).
You typically get hold of a `LocalNode` using `jazz-react`'s `useJazz()`:
```typescript
const { localNode } = useJazz();
```
#### `LocalNode.load(id)`
```typescript
load<T extends ContentType>(id: CoID<T>): Promise<T>
```
Loads a CoValue's content, syncing from peers as necessary and resolving the returned promise once a first version has been loaded. See `ContentType.subscribe()` and `useTelepathicData` for listening to subsequent updates to the CoValue.
#### `LocalNode.loadProfile(id)`
```typescript
loadProfile(accountID: AccountID): Promise<Profile>
```
Loads a profile associated with an account. `Profile` is at least a `CoMap<{string: name}>`, but might contain other, app-specific properties.
#### `LocalNode.acceptInvite(valueOrGroup, inviteSecret)`
```typescript
acceptInvite<T extends ContentType>(
valueOrGroup: CoID<T>,
inviteSecret: InviteSecret
): Promise<void>
```
Accepts an invite for a group, or infers the group if given the `CoID` of a value owned by that group. Resolves upon successful joining of that group, at which point you should be able to `LocalNode.load` the value.
Invites can be created with `Group.createInvite(role)`.
#### `LocalNode.createGroup()`
```typescript
createGroup(): Group
```
Creates a new group (with the current account as the group's first admin).
---
### `Group`
A CoJSON group manages permissions of its members. A `Group` object exposes those capabilities and allows you to create new CoValues owned by that group.
(Internally, a `Group` is also just a `CoMap`, mapping member accounts to roles and containing some state management for making cryptographic keys available to current members)
#### `Group.id`
Returns the `CoID` of the `Group`.
#### `Group.roleOf(accountID)`
```typescript
roleOf(accountID: AccountID): "reader" | "writer" | "admin" | undefined
```
Returns the current role of a given account.
#### `Group.myRole()`
```typescript
myRole(accountID: AccountID): "reader" | "writer" | "admin" | undefined
```
Returns the role of the current account in the group.
#### `Group.addMember(accountID, role)`
```typescript
addMember(
accountID: AccountIDOrAgentID,
role: "reader" | "writer" | "admin"
)
```
Directly grants a new member a role in the group. The current account must be an admin to be able to do so. Throws otherwise.
#### `Group.createInvite(role)`
```typescript
createInvite(role: "reader" | "writer" | "admin"): InviteSecret
```
Creates an invite for new members to indirectly join the group, allowing them to grant themselves the specified role with the InviteSecret (a string starting with "inviteSecret_") - use `LocalNode.acceptInvite()` for this purpose.
#### `Group.removeMember(accountID)`
```typescript
removeMember(accountID: AccountID)
```
Strips the specified member of all roles (preventing future writes) and rotates the read encryption key for that group (preventing reads of new content, including in covalues owned by this group)
#### `Group.createMap(meta?)`
```typescript
createMap<
M extends { [key: string]: JsonValue },
Meta extends JsonObject | null = null
>(meta?: Meta): CoMap<M, Meta>
```
Creates a new `CoMap` within this group, with the specified inner content type `M` and optional static metadata.
#### `Group.createList(meta?)` (coming soon)
#### `Group.createStream(meta?)` (coming soon)
#### `Group.createStatic(meta)` (coming soon)
---
### `CoValue` ContentType: `CoMap`
```typescript
class CoMap<
M extends { [key: string]: JsonValue; },
Meta extends JsonObject | null = null,
>
```
#### `CoMap.id`
```typescript
id: CoID<CoMap<M, Meta>>
```
Returns the CoMap's (precisely typed) `CoID`
#### `CoMap.keys()`
```typescript
keys(): (keyof M & string)[]
```
#### `CoMap.get(key)`
```typescript
get<K extends keyof M>(key: K): M[K] | undefined
```
Returns the current value for the given key.
#### `CoMap.getLastEditor(key)`
```typescript
getLastEditor<K extends keyof M>(key: K): AccountID | undefined
```
Returns the accountID of the last account to modify the value for the given key.
#### `CoMap.toJSON()`
```typescript
toJSON(): JsonObject
```
Returns a JSON representation of the state of the CoMap.
#### `CoMap.subscribe(listener)`
```typescript
subscribe(
listener: (coMap: CoMap<M, Meta>) => void
): () => void
```
Lets you subscribe to future updates to this CoMap (whether made locally or by other users). Takes a listener function that will be called with the current state for each update. Returns an unsubscribe function.
Used internally by `useTelepathicData()` for reactive updates on changes to a `CoMap`.
#### `CoMap.edit(editable => {...})`
```typescript
edit(changer: (editable: WriteableCoMap<M, Meta>) => void): CoMap<M, Meta>
```
Lets you apply edits to a `CoMap`, inside the changer callback, which receives a `WriteableCoMap`. A `WritableCoMap` has all the same methods as a `CoMap`, but all edits made to it with `set` or `delete` are reflected in it immediately - so it behaves mutably, whereas a `CoMap` is always immutable (you need to use `subscribe` to receive new versions of it).
```typescript
export class WriteableCoMap<
M extends { [key: string]: JsonValue; },
Meta extends JsonObject | null = null,
> extends CoMap<M, Meta>
```
#### `WritableCoMap.set(key, value)`
```typescript
set<K extends keyof M>(
key: K,
value: M[K],
privacy: "private" | "trusting" = "private"
): void
```
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.
#### `WritableCoMap.delete(key)`
```typescript
delete<K extends keyof M>(
key: K,
privacy: "private" | "trusting" = "private"
): void
```
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.
---
### `CoValue` ContentType: `CoList` (not yet implemented)
---
### `CoValue` ContentType: `CoStram` (not yet implemented)
---
### `CoValue` ContentType: `Static` (not yet implemented)
---
## `jazz-react`
---
### `<WithJazz>`
Not yet documented, see [`examples/todo`](./examples/todo/) for now
---
### `useJazz()`
Not yet documented, see [`examples/todo`](./examples/todo/) for now
---
### `useTelepathicData(coID)`
Not yet documented, see [`examples/todo`](./examples/todo/) for now
---
### `useProfile(accountID)`
Not yet documented, see [`examples/todo`](./examples/todo/) for now
## What is Telepathic Data?
...

View File

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

View File

@@ -1,344 +1,27 @@
# Jazz Todo List Example
# React + TypeScript + Vite
Live version: https://example-todo.jazz.tools
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
More comprehensive guide coming soon, but these are the most important bits, with explanations:
Currently, two official plugins are available:
From `./src/main.tsx`
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
```typescript
// ...
## Expanding the ESLint configuration
import { WithJazz } from "jazz-react";
import { LocalAuth } from "jazz-react-auth-local";
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:
// ...
ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ThemeProvider>
<div className="flex items-center gap-2 justify-center mt-5">
<img src="jazz-logo.png" className="h-5" /> Jazz Todo List
Example
</div>
<WithJazz
auth={LocalAuth({
appName: "Jazz Todo List Example",
Component: PrettyAuthComponent,
})}
>
<App />
</WithJazz>
</ThemeProvider>
</React.StrictMode>
);
- Configure the top-level `parserOptions` property like this:
```js
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json'],
tsconfigRootDir: __dirname,
},
```
This shows how to use the top-level component `<WithJazz/>`, which provides the rest of the app with a `LocalNode` (used through `useJazz` later), based on `LocalAuth` that uses Passkeys to store a user's account secret - no backend needed.
Let's move on to the main app code.
---
From `./src/App.tsx`
```typescript
// ...
import { CoMap, CoID, AccountID } from "cojson";
import {
consumeInviteLinkFromWindowLocation,
useJazz,
useProfile,
useTelepathicState,
createInviteLink
} from "jazz-react";
// ...
type TaskContent = { done: boolean; text: string };
type Task = CoMap<TaskContent>;
type TodoListContent = {
title: string;
// other keys form a set of task IDs
[taskId: CoID<Task>]: true;
};
type TodoList = CoMap<TodoListContent>;
// ...
```
First, we define our main data model of tasks and todo lists, using CoJSON's collaborative map type, `CoMap`. We reference CoMaps of individual tasks by using them as keys inside the `TodoList` CoMap - as a makeshift solution until `CoList` is implemented.
---
```typescript
// ...
export default function App() {
const [listId, setListId] = useState<CoID<TodoList>>();
const { localNode, logOut } = useJazz();
useEffect(() => {
const listener = async () => {
const acceptedInvitation =
await consumeInviteLinkFromWindowLocation(localNode);
if (acceptedInvitation) {
setListId(acceptedInvitation.valueID as CoID<TodoList>);
window.location.hash = acceptedInvitation.valueID;
return;
}
setListId(window.location.hash.slice(1) as CoID<TodoList>);
};
window.addEventListener("hashchange", listener);
listener();
return () => {
window.removeEventListener("hashchange", listener);
};
}, [localNode]);
const createList = useCallback(
(title: string) => {
const listGroup = localNode.createGroup();
const list = listGroup.createMap<TodoListContent>();
list.edit((list) => {
list.set("title", title);
});
window.location.hash = list.id;
},
[localNode]
);
return (
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
{listId ? (
<TodoListComponent listId={listId} />
) : (
<SubmittableInput
onSubmit={createList}
label="Create New List"
placeholder="New list title"
/>
)}
<Button
onClick={() => {
window.location.hash = "";
logOut();
}}
variant="outline"
>
Log Out
</Button>
</div>
);
}
```
`<App>` is the main app component, handling client-side routing based on the CoValue ID (`CoID`) of our `TodoList`, stored in the URL hash - which can also contain invite links, which we intercept and use with `consumeInviteLinkFromWindowLocation`.
`createList` is the first time we see CoJSON in action: using our `localNode` (which we got from `useJazz`), we first create a group for a new todo list (which allows us to set permissions later). Then, within that group, we create a new `CoMap<TodoListContent>` with `listGroup.createMap()`.
We immediately start editing the created `list`. Within the edit callback, we can use the `set` function, to collaboratively set the key `title` to the initial title provided to `createList`.
If we have a current `listId` set, we render `<TodoListComponent>` with it, which we'll see next.
If we have no `listId` set, the user can use the displayed creation input to create (and open) their first list.
---
```typescript
export function TodoListComponent({ listId }: { listId: CoID<TodoList> }) {
const list = useTelepathicState(listId);
const createTask = (text: string) => {
if (!list) return;
const task = list.coValue.getGroup().createMap<TaskContent>();
task.edit((task) => {
task.set("text", text);
task.set("done", false);
});
list.edit((list) => {
list.set(task.id, true);
});
};
return (
<div className="max-w-full w-4xl">
<div className="flex justify-between items-center gap-4 mb-4">
<h1>
{list?.get("title") ? (
<>
{list.get("title")}{" "}
<span className="text-sm">({list.id})</span>
</>
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)}
</h1>
{list && <InviteButton list={list} />}
</div>
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[40px]">Done</TableHead>
<TableHead>Task</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{list &&
list
.keys()
.filter((key): key is CoID<Task> =>
key.startsWith("co_")
)
.map((taskId) => (
<TaskRow key={taskId} taskId={taskId} />
))}
<TableRow key="new">
<TableCell>
<Checkbox className="mt-1" disabled />
</TableCell>
<TableCell>
<SubmittableInput
onSubmit={(taskText) => createTask(taskText)}
label="Add"
placeholder="New task"
disabled={!list}
/>
</TableCell>
</TableRow>
</TableBody>
</Table>
</div>
);
}
```
Here in `<TodoListComponent>`, we use `useTelepathicData()` for the first time, in this case to load the CoValue for our `TodoList` and to reactively subscribe to updates to its content - whether we create edits locally, load persisted data, or receive sync updates from other devices or participants!
`createTask` is similar to `createList` we saw earlier, creating a new CoMap for a new task, and then adding it as a key to our `TodoList`.
As you can see, we iterate over the keys of `TodoList` and for those that look like `CoID`s (they always start with `co_`), we render a `<TaskRow>`.
Below all tasks, we render a simple input for adding a task.
---
```typescript
function TaskRow({ taskId }: { taskId: CoID<Task> }) {
const task = useTelepathicState(taskId);
return (
<TableRow>
<TableCell>
<Checkbox
className="mt-1"
checked={task?.get("done")}
onCheckedChange={(checked) => {
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>
<NameBadge accountID={task?.getLastEditor("text")} />
</div>
</TableCell>
</TableRow>
);
}
```
`<TaskRow>` uses `useTelepathicState()` as well, to granularly load and subscribe to changes for that particular task (the only thing we let the user change is the "done" status).
We also use a `<NameBadge>` helper component to render the name of the author of the task, which we get by using the collaboration feature `getLastEditor(key)` on our `Task` CoMap, which returns the accountID of the last account that changed a given key in the CoMap.
---
```typescript
function NameBadge({ accountID }: { accountID?: AccountID }) {
const profile = useProfile(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 (
profile?.get("name") && <span
className="rounded-full py-0.5 px-2 text-xs"
style={{
color: theme == "light" ? darkColor : brightColor,
background: theme == "light" ? brightColor : darkColor,
}}
>
{profile.get("name")}
</span>
);
}
```
`<NameBadge>` uses `useProfile(accountID)`, which is a shorthand for loading an account's profile (which is always a `CoMap<{name: string}>`, but might have app-specific additional properties).
In our case, we just display the profile name (which, by the way, is set by the `LocalAuth` provider when we first create an account).
---
```typescript
function InviteButton({ list }: { list: TodoList }) {
const [existingInviteLink, setExistingInviteLink] = useState<string>();
const { toast } = useToast();
return (
list.coValue.getGroup().myRole() === "admin" && (
<Button
size="sm"
className="py-0"
disabled={!list}
variant="outline"
onClick={() => {
let inviteLink = existingInviteLink;
if (list && !inviteLink) {
inviteLink = createInviteLink(list, "writer");
setExistingInviteLink(inviteLink);
}
if (inviteLink) {
navigator.clipboard.writeText(inviteLink).then(() =>
toast({
description: "Copied invite link to clipboard!",
})
);
}
}}
>
Invite
</Button>
)
);
}
```
Last, we have a look at the `<InviteButton>` component, which we use inside `<TodoListComponent>`. It only becomes visible when the current user is an admin in the `TodoList`'s group. You can see how we can create an invite link using `createInviteLink(coValue, role)` that allows anyone who has it to join the group as a specified role (here, as a writer).
---
This is the whole Todo List app!
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.
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list

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;
}
}

View File

@@ -2,9 +2,9 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/png" href="/jazz-logo.png" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Jazz Todo List Example</title>
<title>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>

View File

@@ -1,9 +1,9 @@
job "example-todo$BRANCH_SUFFIX" {
region = "global"
datacenters = ["*"]
region = "$REGION"
datacenters = ["$REGION"]
group "static" {
count = 8
// count = 3
network {
port "http" {
@@ -14,17 +14,13 @@ job "example-todo$BRANCH_SUFFIX" {
constraint {
attribute = "${node.class}"
operator = "="
value = "mesh"
value = "edge"
}
spread {
attribute = "${node.datacenter}"
weight = 100
}
constraint {
distinct_hosts = true
}
// spread {
// attribute = "${node.datacenter}"
// weight = 100
// }
task "server" {
driver = "docker"
@@ -41,7 +37,9 @@ job "example-todo$BRANCH_SUFFIX" {
service {
tags = ["public"]
name = "example-todo$BRANCH_SUFFIX"
meta {
public_name = "${BRANCH_SUBDOMAIN}example-todo"
}
port = "http"
provider = "consul"
}

View File

@@ -1,7 +1,7 @@
{
"name": "jazz-example-todo",
"private": true,
"version": "0.0.21",
"version": "0.0.5",
"type": "module",
"scripts": {
"dev": "vite",
@@ -12,19 +12,15 @@
"dependencies": {
"@radix-ui/react-checkbox": "^1.0.4",
"@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",
"jazz-react": "^0.1.7",
"jazz-react-auth-local": "^0.1.7",
"jazz-react": "^0.0.11",
"jazz-react-auth-local": "^0.0.8",
"lucide-react": "^0.265.0",
"qrcode": "^1.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwind-merge": "^1.14.0",
"tailwindcss-animate": "^1.0.6",
"uniqolor": "^1.1.0"
"tailwindcss-animate": "^1.0.6"
},
"devDependencies": {
"@types/react": "^18.2.15",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.3 KiB

View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -1,17 +1,3 @@
import { useCallback, useEffect, useState } from "react";
import { CoMap, CoID, AccountID } from "cojson";
import {
consumeInviteLinkFromWindowLocation,
useJazz,
useProfile,
useTelepathicState,
createInviteLink,
} from "jazz-react";
import { SubmittableInput } from "./components/SubmittableInput";
import { useToast } from "./components/ui/use-toast";
import { Skeleton } from "./components/ui/skeleton";
import {
Table,
TableBody,
@@ -20,121 +6,80 @@ import {
TableHeader,
TableRow,
} from "@/components/ui/table";
import { Input } from "@/components/ui/input";
import { Checkbox } from "@/components/ui/checkbox";
import { Button } from "@/components/ui/button";
import uniqolor from "uniqolor";
import QRCode from "qrcode";
import { CoList } from "cojson/dist/contentTypes/coList";
import { useEffect, useState } from "react";
import { CoMap, CoID } from "cojson";
import { useJazz, useTelepathicState } from "jazz-react";
type Task = CoMap<{ done: boolean; text: string }>;
type TaskContent = { done: boolean; text: string };
type Task = CoMap<TaskContent>;
type ListOfTasks = CoList<CoID<Task>>;
type TodoListContent = { title: string; [taskId: CoID<Task>]: true };
type TodoList = CoMap<TodoListContent>;
type TodoList = CoMap<{
title: string;
tasks: CoID<ListOfTasks>;
}>;
function App() {
const [listId, setListId] = useState<CoID<TodoList>>(window.location.hash.slice(1) as CoID<TodoList>);
export default function App() {
const [listId, setListId] = useState<CoID<TodoList>>();
const { localNode } = useJazz();
const { localNode, logOut } = useJazz();
const createList = () => {
const listTeam = localNode.createTeam();
const list = listTeam.createMap<TodoListContent>();
list.edit((list) => {
list.set("title", "My Todo List");
});
window.location.hash = list.id;
};
useEffect(() => {
const listener = async () => {
const acceptedInvitation =
await consumeInviteLinkFromWindowLocation(localNode);
if (acceptedInvitation) {
setListId(acceptedInvitation.valueID as CoID<TodoList>);
window.location.hash = acceptedInvitation.valueID;
return;
}
const listener = () => {
setListId(window.location.hash.slice(1) as CoID<TodoList>);
};
}
window.addEventListener("hashchange", listener);
listener();
return () => {
window.removeEventListener("hashchange", listener);
};
}, [localNode]);
const createList = useCallback(
(title: string) => {
if (!title) return;
const listGroup = localNode.createGroup();
const list = listGroup.createMap<TodoList>();
const tasks = listGroup.createList<ListOfTasks>();
list.edit((list) => {
list.set("title", title);
list.set("tasks", tasks.id);
});
window.location.hash = list.id;
},
[localNode]
);
}
}, [])
return (
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 pb-10 px-5">
{listId ? (
<TodoListComponent listId={listId} />
) : (
<SubmittableInput
onSubmit={createList}
label="Create New List"
placeholder="New list title"
/>
)}
<Button
onClick={() => {
window.location.hash = "";
logOut();
}}
variant="outline"
>
Log Out
</Button>
<div className="flex flex-col h-full items-center justify-start gap-10 pt-10 md:pt-[30vh] pb-10">
{listId && <TodoList listId={listId} />}
<Button onClick={createList}>Create New List</Button>
</div>
);
}
export function TodoListComponent({ listId }: { listId: CoID<TodoList> }) {
export function TodoList({ listId }: { listId: CoID<TodoList> }) {
const list = useTelepathicState(listId);
const tasks = useTelepathicState(list?.get("tasks"));
const createTask = (text: string) => {
if (!tasks || !text) return;
const task = tasks.coValue.getGroup().createMap<Task>();
const createTodo = (text: string) => {
if (!list) return;
let task = list.coValue.getTeam().createMap<TaskContent>();
task.edit((task) => {
task = task.edit((task) => {
task.set("text", text);
task.set("done", false);
});
tasks.edit((tasks) => {
tasks.push(task.id);
console.log("Created task", task.id, task.toJSON());
const listAfter = list.edit((list) => {
list.set(task.id, true);
});
console.log("Updated list", listAfter.toJSON());
};
return (
<div className="max-w-full w-4xl">
<div className="flex justify-between items-center gap-4 mb-4">
<h1>
{list?.get("title") ? (
<>
{list.get("title")}{" "}
<span className="text-sm">({list.id})</span>
</>
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)}
</h1>
{list && <InviteButton list={list} />}
</div>
<h1>
{list?.get("title")} ({list?.id})
</h1>
<Table>
<TableHeader>
<TableRow>
@@ -143,23 +88,42 @@ export function TodoListComponent({ listId }: { listId: CoID<TodoList> }) {
</TableRow>
</TableHeader>
<TableBody>
{tasks &&
tasks
.asArray()
{list &&
list
.keys()
.filter((key): key is CoID<Task> =>
key.startsWith("co_")
)
.map((taskId) => (
<TaskRow key={taskId} taskId={taskId} />
<TodoRow key={taskId} taskId={taskId} />
))}
<TableRow key="new">
<TableCell>
<Checkbox className="mt-1" disabled />
</TableCell>
<TableCell>
<SubmittableInput
onSubmit={(taskText) => createTask(taskText)}
label="Add"
placeholder="New task"
disabled={!list}
/>
<form
className="flex flex-row items-center gap-5"
onSubmit={(e) => {
e.preventDefault();
const textEl =
e.currentTarget.elements.namedItem(
"text"
) as HTMLInputElement;
createTodo(textEl.value);
textEl.value = "";
}}
>
<Input
className="-ml-3 -my-2"
name="text"
placeholder="Add todo"
autoComplete="off"
/>
<Button asChild type="submit">
<Input type="submit" value="Add" />
</Button>
</form>
</TableCell>
</TableRow>
</TableBody>
@@ -168,7 +132,7 @@ export function TodoListComponent({ listId }: { listId: CoID<TodoList> }) {
);
}
function TaskRow({ taskId }: { taskId: CoID<Task> }) {
function TodoRow({ taskId }: { taskId: CoID<Task> }) {
const task = useTelepathicState(taskId);
return (
@@ -184,81 +148,11 @@ function TaskRow({ taskId }: { taskId: CoID<Task> }) {
}}
/>
</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>
<NameBadge accountID={task?.getLastEditor("text")} />
</div>
<TableCell className={task?.get("done") ? "line-through" : ""}>
{task?.get("text")}
</TableCell>
</TableRow>
);
}
function NameBadge({ accountID }: { accountID?: AccountID }) {
const profile = useProfile(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 (
profile?.get("name") ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={{
color: theme == "light" ? darkColor : brightColor,
background: theme == "light" ? brightColor : darkColor,
}}
>
{profile.get("name")}
</span>
) : (
<Skeleton className="mt-1 w-[50px] h-[1em] rounded-full" />
)
);
}
function InviteButton({ list }: { list: TodoList }) {
const [existingInviteLink, setExistingInviteLink] = useState<string>();
const { toast } = useToast();
return (
list.coValue.getGroup().myRole() === "admin" && (
<Button
size="sm"
className="py-0"
disabled={!list}
variant="outline"
onClick={async () => {
let inviteLink = existingInviteLink;
if (list && !inviteLink) {
inviteLink = createInviteLink(list, "writer");
setExistingInviteLink(inviteLink);
}
if (inviteLink) {
const qr = await QRCode.toDataURL(inviteLink, {
errorCorrectionLevel: "L",
});
navigator.clipboard.writeText(inviteLink).then(() =>
toast({
title: "Copied invite link to clipboard!",
description: (
<img src={qr} className="w-20 h-20" />
),
})
);
}
}}
>
Invite
</Button>
)
);
}
export default App;

View File

@@ -0,0 +1,30 @@
import { Input } from "./components/ui/input.tsx";
import { Button } from "./components/ui/button.tsx";
import { AuthComponent } from "jazz-react";
import { useLocalAuth } from "jazz-react-auth-local";
export const LocalAuth: AuthComponent = ({ onCredential }) => {
const { displayName, setDisplayName, signIn, signUp } = useLocalAuth(onCredential);
return (<div className="w-full h-full flex items-center justify-center">
<div className="w-72 flex flex-col gap-4">
<form
className="w-72 flex flex-col gap-2"
onSubmit={(e) => {
e.preventDefault();
signUp();
}}
>
<Input
placeholder="Display name"
value={displayName}
onChange={(e) => setDisplayName(e.target.value)}
autoComplete="webauthn" />
<Button asChild>
<Input type="submit" value="Sign Up as new account" />
</Button>
</form>
<Button onClick={signIn}>Log In with existing account</Button>
</div>
</div>);
};

View File

@@ -1,39 +0,0 @@
import { Input } from "@/components/ui/input";
import { Button } from "@/components/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

@@ -1,47 +0,0 @@
import { LocalAuthComponent } from "jazz-react-auth-local";
import { useState } from "react";
import { Input } from "./ui/input";
import { Button } from "./ui/button";
export const PrettyAuthComponent: 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

@@ -1,72 +0,0 @@
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

@@ -1,15 +0,0 @@
import { cn } from "@/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

@@ -1,127 +0,0 @@
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 "@/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

@@ -1,33 +0,0 @@
import {
Toast,
ToastClose,
ToastDescription,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/components/ui/toast"
import { useToast } from "@/components/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

@@ -1,192 +0,0 @@
// Inspired by react-hot-toast library
import * as React from "react"
import type {
ToastActionElement,
ToastProps,
} from "@/components/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,35 +1,14 @@
import React from "react";
import ReactDOM from "react-dom/client";
import { WithJazz } from "jazz-react";
import { LocalAuth } from "jazz-react-auth-local";
import { PrettyAuthComponent } from "./components/prettyAuth.tsx";
import { ThemeProvider } from "./components/themeProvider.tsx";
import { Toaster } from "./components/ui/toaster.tsx";
import App from "./App.tsx";
import "./index.css";
import { WithJazz } from "jazz-react";
import { LocalAuth } from "./LocalAuth.tsx";
ReactDOM.createRoot(document.getElementById("root")!).render(
// <React.StrictMode>
<ThemeProvider>
<div className="flex items-center gap-2 justify-center mt-5">
<img src="jazz-logo.png" className="h-5" /> Jazz Todo List
Example
</div>
<WithJazz
auth={LocalAuth({
appName: "Jazz Todo List Example",
Component: PrettyAuthComponent,
})}
syncAddress={
new URLSearchParams(window.location.search).get("sync") ||
undefined
}
>
<App />
<Toaster />
</WithJazz>
</ThemeProvider>
// </React.StrictMode>
<React.StrictMode>
<WithJazz auth={LocalAuth} syncAddress={new URLSearchParams(window.location.search).get("sync") || undefined}>
<App />
</WithJazz>
</React.StrictMode>
);

View File

@@ -9,8 +9,5 @@ export default defineConfig({
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
build: {
minify: false
}
})

View File

@@ -1,6 +0,0 @@
/** @type {import('jest').Config} */
const config = {
projects: ['<rootDir>/packages/cojson'],
};
module.exports = config;

View File

@@ -1,18 +0,0 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
project: './tsconfig.json',
},
root: true,
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
"@typescript-eslint/no-floating-promises": "error",
},
};

View File

@@ -1,174 +0,0 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
.DS_Store
out
sync.db*

View File

@@ -1,35 +0,0 @@
{
"name": "cojson-simple-sync",
"module": "dist/index.js",
"types": "src/index.ts",
"type": "module",
"license": "MIT",
"version": "0.1.7",
"devDependencies": {
"@types/jest": "^29.5.3",
"@types/ws": "^8.5.5",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.2.1",
"eslint": "^8.46.0",
"jest": "^29.6.2",
"ts-jest": "^29.1.1",
"typescript": "5.0.2"
},
"dependencies": {
"cojson": "^0.1.6",
"cojson-storage-sqlite": "^0.1.4",
"ws": "^8.13.0"
},
"scripts": {
"build": "rm -rf ./dist && tsc --sourceMap --outDir dist && npm run add-shebang && chmod +x ./dist/index.js",
"add-shebang": "echo \"#!/usr/bin/env node\" | cat - ./dist/index.js > /tmp/out && mv /tmp/out ./dist/index.js",
"start": "node dist/index.js",
"test": "jest",
"prepublishOnly": "npm run build"
},
"bin": "./dist/index.js",
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
}
}

View File

@@ -1,54 +0,0 @@
import { AnonymousControlledAccount, LocalNode, cojsonInternals } from "cojson";
import { WebSocketServer } from "ws";
import { SQLiteStorage } from "cojson-storage-sqlite";
import { websocketReadableStream, websocketWritableStream } from "./websocketStreams.js";
const wss = new WebSocketServer({ port: 4200 });
console.log("COJSON sync server listening on port " + wss.options.port);
const agentSecret = cojsonInternals.newRandomAgentSecret();
const agentID = cojsonInternals.getAgentID(agentSecret);
const localNode = new LocalNode(
new AnonymousControlledAccount(agentSecret),
cojsonInternals.newRandomSessionID(agentID)
);
SQLiteStorage.asPeer({ filename: "./sync.db" })
.then((storage) => localNode.sync.addPeer(storage))
.catch((e) => console.error(e));
wss.on("connection", function connection(ws, req) {
const pinging = setInterval(() => {
ws.send(
JSON.stringify({
type: "ping",
time: Date.now(),
dc: "cojson-simple-sync",
})
);
}, 2000);
ws.on("close", () => {
clearInterval(pinging);
});
const clientAddress =
(req.headers["x-forwarded-for"] as string | undefined)
?.split(",")[0]
?.trim() || req.socket.remoteAddress;
const clientId = clientAddress + "@" + new Date().toISOString();
localNode.sync.addPeer({
id: clientId,
role: "client",
incoming: websocketReadableStream(ws),
outgoing: websocketWritableStream(ws),
});
ws.on("error", (e) => console.error(`Error on connection ${clientId}:`, e));
});

View File

@@ -1,86 +0,0 @@
import { WebSocket } from "ws";
import { WritableStream, ReadableStream } from "isomorphic-streams";
export function websocketReadableStream<T>(ws: WebSocket) {
ws.binaryType = "arraybuffer";
return new ReadableStream<T>({
start(controller) {
ws.addEventListener("message", (event) => {
if (typeof event.data !== "string")
return console.warn(
"Got non-string message from client",
event.data
);
const msg = JSON.parse(event.data);
if (msg.type === "ping") {
// console.debug(
// "Got ping from",
// msg.dc,
// "latency",
// Date.now() - msg.time,
// "ms"
// );
return;
}
controller.enqueue(msg);
});
ws.addEventListener("close", () => controller.close());
ws.addEventListener("error", () =>
controller.error(new Error("The WebSocket errored!"))
);
},
cancel() {
ws.close();
},
});
}
export function websocketWritableStream<T>(ws: WebSocket) {
return new WritableStream<T>({
start(controller) {
ws.addEventListener("close", () =>
controller.error(
new Error("The WebSocket closed unexpectedly!")
)
);
ws.addEventListener("error", () =>
controller.error(new Error("The WebSocket errored!"))
);
if (ws.readyState === WebSocket.OPEN) {
return;
}
return new Promise((resolve) => ws.once("open", resolve));
},
write(chunk) {
ws.send(JSON.stringify(chunk));
// Return immediately, since the web socket gives us no easy way to tell
// when the write completes.
},
close() {
return closeWS(1000);
},
abort(reason) {
return closeWS(4000, reason && reason.message);
},
});
function closeWS(code: number, reasonString?: string) {
return new Promise<void>((resolve, reject) => {
ws.onclose = (e) => {
if (e.wasClean) {
resolve();
} else {
reject(new Error("The connection was not closed cleanly"));
}
};
ws.close(code, reasonString);
});
}
}

View File

@@ -1,19 +0,0 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "preserve",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
},
"include": ["./src/**/*"],
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,17 +0,0 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
project: "./tsconfig.json",
},
root: true,
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
// "@typescript-eslint/no-floating-promises": "error",
},
};

View File

@@ -1,171 +0,0 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
.DS_Store

View File

@@ -1,2 +0,0 @@
coverage
node_modules

View File

@@ -1,21 +0,0 @@
{
"name": "cojson-storage-sqlite",
"type": "module",
"version": "0.1.4",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"better-sqlite3": "^8.5.2",
"cojson": "^0.1.6",
"typescript": "^5.1.6"
},
"scripts": {
"lint": "eslint src/**/*.ts",
"build": "npm run lint && rm -rf ./dist && tsc --declaration --sourceMap --outDir dist",
"prepublishOnly": "npm run build"
},
"devDependencies": {
"@types/better-sqlite3": "^7.6.4"
}
}

View File

@@ -1,386 +0,0 @@
import {
cojsonInternals,
SyncMessage,
Peer,
CojsonInternalTypes,
SessionID,
// CojsonInternalTypes,
// SessionID,
} from "cojson";
import {
ReadableStream,
WritableStream,
ReadableStreamDefaultReader,
WritableStreamDefaultWriter,
} from "isomorphic-streams";
import Database, { Database as DatabaseT } from "better-sqlite3";
import { RawCoID } from "cojson/dist/ids";
type CoValueRow = {
id: CojsonInternalTypes.RawCoID;
header: string;
};
type StoredCoValueRow = CoValueRow & { rowID: number };
type SessionRow = {
coValue: number;
sessionID: SessionID;
lastIdx: number;
lastSignature: CojsonInternalTypes.Signature;
};
type StoredSessionRow = SessionRow & { rowID: number };
type TransactionRow = {
ses: number;
idx: number;
tx: string;
};
export class SQLiteStorage {
fromLocalNode!: ReadableStreamDefaultReader<SyncMessage>;
toLocalNode: WritableStreamDefaultWriter<SyncMessage>;
db: DatabaseT;
constructor(
db: DatabaseT,
fromLocalNode: ReadableStream<SyncMessage>,
toLocalNode: WritableStream<SyncMessage>
) {
this.db = db;
this.fromLocalNode = fromLocalNode.getReader();
this.toLocalNode = toLocalNode.getWriter();
(async () => {
let done = false;
while (!done) {
const result = await this.fromLocalNode.read();
done = result.done;
if (result.value) {
this.handleSyncMessage(result.value);
}
}
})();
}
static async asPeer({
filename,
trace,
localNodeName = "local",
}: {
filename: string;
trace?: boolean;
localNodeName?: string;
}): Promise<Peer> {
const [localNodeAsPeer, storageAsPeer] = cojsonInternals.connectedPeers(
localNodeName,
"storage",
{ peer1role: "client", peer2role: "server", trace }
);
await SQLiteStorage.open(
filename,
localNodeAsPeer.incoming,
localNodeAsPeer.outgoing
);
return storageAsPeer;
}
static async open(
filename: string,
fromLocalNode: ReadableStream<SyncMessage>,
toLocalNode: WritableStream<SyncMessage>
) {
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();
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 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();
return new SQLiteStorage(db, fromLocalNode, toLocalNode);
}
async handleSyncMessage(msg: SyncMessage) {
switch (msg.action) {
case "load":
await this.handleLoad(msg);
break;
case "content":
await this.handleContent(msg);
break;
case "known":
await this.handleKnown(msg);
break;
case "done":
await this.handleDone(msg);
break;
}
}
async sendNewContentAfter(
theirKnown: CojsonInternalTypes.CoValueKnownState,
asDependencyOf?: CojsonInternalTypes.RawCoID
) {
const coValueRow = (await this.db
.prepare(`SELECT * FROM coValues WHERE id = ?`)
.get(theirKnown.id)) as StoredCoValueRow | undefined;
const allOurSessions = coValueRow
? (this.db
.prepare<number>(`SELECT * FROM sessions WHERE coValue = ?`)
.all(coValueRow.rowID) as StoredSessionRow[])
: [];
const ourKnown: CojsonInternalTypes.CoValueKnownState = {
id: theirKnown.id,
header: !!coValueRow,
sessions: {},
};
const parsedHeader = (coValueRow?.header &&
JSON.parse(coValueRow.header)) as
| CojsonInternalTypes.CoValueHeader
| undefined;
const newContent: CojsonInternalTypes.NewContentMessage = {
action: "content",
id: theirKnown.id,
header: theirKnown.header ? undefined : parsedHeader,
new: {},
};
for (const sessionRow of allOurSessions) {
ourKnown.sessions[sessionRow.sessionID] = sessionRow.lastIdx;
if (
sessionRow.lastIdx >
(theirKnown.sessions[sessionRow.sessionID] || 0)
) {
const firstNewTxIdx =
theirKnown.sessions[sessionRow.sessionID] || 0;
const newTxInSession = this.db
.prepare<[number, number]>(
`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)
),
};
}
}
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_")
);
})
)
: parsedHeader?.ruleset.type === "ownedByGroup"
? [parsedHeader?.ruleset.group]
: [];
for (const dependedOnCoValue of dependedOnCoValues) {
await this.sendNewContentAfter(
{ id: dependedOnCoValue, header: false, sessions: {} },
asDependencyOf || theirKnown.id
);
}
await this.toLocalNode.write({
action: "known",
...ourKnown,
asDependencyOf,
});
if (newContent.header || Object.keys(newContent.new).length > 0) {
await this.toLocalNode.write(newContent);
}
}
handleLoad(msg: CojsonInternalTypes.LoadMessage) {
return this.sendNewContentAfter(msg);
}
async handleContent(msg: CojsonInternalTypes.NewContentMessage) {
let storedCoValueRowID = (
this.db
.prepare<RawCoID>(`SELECT rowID FROM coValues WHERE id = ?`)
.get(msg.id) as StoredCoValueRow | undefined
)?.rowID;
if (storedCoValueRowID === undefined) {
const header = msg.header;
if (!header) {
console.error("Expected to be sent header first");
await this.toLocalNode.write({
action: "known",
id: msg.id,
header: false,
sessions: {},
isCorrection: true,
});
return;
}
storedCoValueRowID = this.db
.prepare<[RawCoID, string]>(
`INSERT INTO coValues (id, header) VALUES (?, ?)`
)
.run(msg.id, JSON.stringify(header)).lastInsertRowid as number;
}
const ourKnown: CojsonInternalTypes.CoValueKnownState = {
id: msg.id,
header: true,
sessions: {},
};
let invalidAssumptions = false;
this.db.transaction(() => {
const allOurSessions = (
this.db
.prepare<number>(`SELECT * FROM sessions WHERE coValue = ?`)
.all(storedCoValueRowID!) as StoredSessionRow[]
).reduce((acc, row) => {
acc[row.sessionID] = row;
return acc;
}, {} as { [sessionID: string]: StoredSessionRow });
for (const sessionID of Object.keys(msg.new) as SessionID[]) {
const sessionRow = allOurSessions[sessionID];
if (sessionRow) {
ourKnown.sessions[sessionRow.sessionID] =
sessionRow.lastIdx;
}
if (
(sessionRow?.lastIdx || 0) <
(msg.new[sessionID]?.after || 0)
) {
invalidAssumptions = true;
} else {
const newTransactions =
msg.new[sessionID]?.newTransactions || [];
const actuallyNewOffset =
(sessionRow?.lastIdx || 0) -
(msg.new[sessionID]?.after || 0);
const actuallyNewTransactions =
newTransactions.slice(actuallyNewOffset);
let nextIdx = sessionRow?.lastIdx || 0;
const sessionUpdate = {
coValue: storedCoValueRowID!,
sessionID: sessionID,
lastIdx:
(sessionRow?.lastIdx || 0) +
actuallyNewTransactions.length,
lastSignature: msg.new[sessionID]!.lastSignature,
};
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
RETURNING rowID`
)
.get(
sessionUpdate.coValue,
sessionUpdate.sessionID,
sessionUpdate.lastIdx,
sessionUpdate.lastSignature
) as {rowID: number});
const sessionRowID = upsertedSession.rowID;
for (const newTransaction of actuallyNewTransactions) {
nextIdx++;
this.db
.prepare<[number, number, string]>(
`INSERT INTO transactions (ses, idx, tx) VALUES (?, ?, ?)`
)
.run(
sessionRowID,
nextIdx,
JSON.stringify(newTransaction)
);
}
}
}
})();
if (invalidAssumptions) {
await this.toLocalNode.write({
action: "known",
...ourKnown,
isCorrection: invalidAssumptions,
});
}
}
handleKnown(msg: CojsonInternalTypes.KnownStateMessage) {
return this.sendNewContentAfter(msg);
}
handleDone(_msg: CojsonInternalTypes.DoneMessage) {}
}

View File

@@ -1,15 +0,0 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "ES2020",
"moduleResolution": "bundler",
"moduleDetection": "force",
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
},
"include": ["./src/**/*"],
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,53 @@
# CoJSON
[See the top-level README](../../README.md#cojson)
CoJSON ("Collaborative JSON") will be a minimal protocol and implementation for collaborative values (CRDTs + public-key cryptography).
CoJSON is developed by [Garden Computing](https://gcmp.io) as the underpinnings of [Jazz](https://jazz.tools), a framework for building apps with telepathic data.
The protocol and implementation will cover:
- how to represent collaborative values internally
- the APIs collaborative values expose
- how to sync and query for collaborative values between peers
- how to enforce access rights within collaborative values locally and at sync boundaries
THIS IS WORK IN PROGRESS
## Core Value Types
### `Immutable` Values (JSON)
- null
- boolean
- number
- string
- stringly-encoded CoJSON identifiers & data (`CoID`, `AgentID`, `SessionID`, `SignerID`, `SignerSecret`, `Signature`, `SealerID`, `SealerSecret`, `Sealed`, `Hash`, `ShortHash`, `KeySecret`, `KeyID`, `Encrypted`, `Role`)
- array
- object
### `Collaborative` Values
- CoMap (`string``Immutable`, last-writer-wins per key)
- Team (`AgentID``Role`)
- CoList (`Immutable[]`, addressable positions, insertAfter semantics)
- Agent (`{signerID, sealerID}[]`)
- CoStream (independent per-session streams of `Immutable`s)
- Static (single addressable `Immutable`)
## Implementation Abstractions
- CoValue
- Session Logs
- Transactions
- Private (encrypted) transactions
- Trusting (unencrypted) transactions
- Rulesets
- CoValue Content Types
- LocalNode
- Peers
- AgentCredentials
- Peer
## Extensions & higher-level protocols
### More complex datastructures
- CoText: a clean way to collaboratively mark up rich text with CoJSON
- CoJSON Tree: a clean way to represent collaborative tree structures with CoJSON

View File

@@ -2,10 +2,10 @@
"name": "cojson",
"module": "dist/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"types": "src/index.ts",
"type": "module",
"license": "MIT",
"version": "0.1.6",
"version": "0.0.18",
"devDependencies": {
"@types/jest": "^29.5.3",
"@typescript-eslint/eslint-plugin": "^6.2.1",

View File

@@ -19,14 +19,14 @@ test("Can create a node while creating a new account with profile", async () =>
);
});
test("A node with an account can create groups and and objects within them", async () => {
test("A node with an account can create teams and and objects within them", async () => {
const { node, accountID } =
LocalNode.withNewlyCreatedAccount("Hermes Puggington");
const group = await node.createGroup();
expect(group).not.toBeNull();
const team = await node.createTeam();
expect(team).not.toBeNull();
let map = group.createMap();
let map = team.createMap();
map = map.edit((edit) => {
edit.set("foo", "bar", "private");
expect(edit.get("foo")).toEqual("bar");
@@ -41,10 +41,10 @@ test("Can create account with one node, and then load it on another", async () =
const { node, accountID, accountSecret } =
LocalNode.withNewlyCreatedAccount("Hermes Puggington");
const group = await node.createGroup();
expect(group).not.toBeNull();
const team = await node.createTeam();
expect(team).not.toBeNull();
let map = group.createMap();
let map = team.createMap();
map = map.edit((edit) => {
edit.set("foo", "bar", "private");
expect(edit.get("foo")).toEqual("bar");

View File

@@ -14,7 +14,7 @@ import {
} from "./crypto.js";
import { AgentID } from "./ids.js";
import { CoMap, LocalNode } from "./index.js";
import { Group, GroupContent } from "./group.js";
import { Team, TeamContent } from "./permissions.js";
export function accountHeaderForInitialAgentSecret(
agentSecret: AgentSecret
@@ -22,7 +22,7 @@ export function accountHeaderForInitialAgentSecret(
const agent = getAgentID(agentSecret);
return {
type: "comap",
ruleset: { type: "group", initialAdmin: agent },
ruleset: { type: "team", initialAdmin: agent },
meta: {
type: "account",
},
@@ -31,13 +31,13 @@ export function accountHeaderForInitialAgentSecret(
};
}
export class Account extends Group {
export class Account extends Team {
get id(): AccountID {
return this.groupMap.id as AccountID;
return this.teamMap.id as AccountID;
}
getCurrentAgentID(): AgentID {
const agents = this.groupMap
const agents = this.teamMap
.keys()
.filter((k): k is AgentID => k.startsWith("sealer_"));
@@ -70,10 +70,10 @@ export class ControlledAccount
constructor(
agentSecret: AgentSecret,
groupMap: CoMap<AccountContent, AccountMeta>,
teamMap: CoMap<AccountContent, AccountMeta>,
node: LocalNode
) {
super(groupMap, node);
super(teamMap, node);
this.agentSecret = agentSecret;
}
@@ -133,10 +133,9 @@ export class AnonymousControlledAccount
}
}
export type AccountContent = GroupContent & { profile: CoID<Profile> };
export type AccountContent = TeamContent & { profile: CoID<Profile> };
export type AccountMeta = { type: "account" };
export type AccountMap = CoMap<AccountContent, AccountMeta>;
export type AccountID = CoID<AccountMap>;
export type AccountID = CoID<CoMap<AccountContent, AccountMeta>>;
export type AccountIDOrAgentID = AgentID | AccountID;
export type AccountOrAgentID = AgentID | Account;

View File

@@ -2,9 +2,6 @@ import { Transaction } from "./coValue.js";
import { LocalNode } from "./node.js";
import { createdNowUnique, getAgentSignerSecret, newRandomAgentSecret, sign } from "./crypto.js";
import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
import { CoMap, MapOpPayload } from "./contentTypes/coMap.js";
import { AccountID } from "./index.js";
import { Role } from "./permissions.js";
test("Can create coValue with new agent credentials and add transaction to it", () => {
const [account, sessionID] = randomAnonymousAccountAndSessionID();
@@ -124,58 +121,3 @@ test("transactions with correctly signed, but wrong hash are rejected", () => {
)
).toBe(false);
});
test("New transactions in a group correctly update owned values, including subscriptions", async () => {
const [account, sessionID] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(account, sessionID);
const group = node.createGroup();
const timeBeforeEdit = Date.now();
await new Promise((resolve) => setTimeout(resolve, 10));
let map = group.createMap();
let mapAfterEdit = map.edit((map) => {
map.set("hello", "world");
});
const listener = jest.fn().mockImplementation();
map.subscribe(listener);
expect(listener.mock.calls[0][0].get("hello")).toBe("world");
const resignationThatWeJustLearnedAbout = {
privacy: "trusting",
madeAt: timeBeforeEdit,
changes: [
{
op: "set",
key: account.id,
value: "revoked"
} satisfies MapOpPayload<typeof account.id, Role>
]
} satisfies Transaction;
const { expectedNewHash } = group.groupMap.coValue.expectedNewHashAfter(sessionID, [
resignationThatWeJustLearnedAbout,
]);
const signature = sign(
node.account.currentSignerSecret(),
expectedNewHash
);
expect(map.coValue.getValidSortedTransactions().length).toBe(1);
const manuallyAdddedTxSuccess = group.groupMap.coValue.tryAddTransactions(node.ownSessionID, [resignationThatWeJustLearnedAbout], expectedNewHash, signature);
expect(manuallyAdddedTxSuccess).toBe(true);
expect(listener.mock.calls.length).toBe(2);
expect(listener.mock.calls[1][0].get("hello")).toBe(undefined);
expect(map.coValue.getValidSortedTransactions().length).toBe(0);
});

View File

@@ -24,10 +24,11 @@ import { JsonObject, JsonValue } from "./jsonValue.js";
import { base58 } from "@scure/base";
import {
PermissionsDef as RulesetDef,
Team,
determineValidTransactions,
expectTeamContent,
isKeyForKeyField,
} from "./permissions.js";
import { Group, expectGroupContent } from "./group.js";
import { LocalNode } from "./node.js";
import { CoValueKnownState, NewContentMessage } from "./sync.js";
import { RawCoID, SessionID, TransactionID } from "./ids.js";
@@ -92,41 +93,19 @@ export type DecryptedTransaction = {
madeAt: number;
};
const readKeyCache = new WeakMap<CoValue, { [id: KeyID]: KeySecret }>();
export class CoValue {
id: RawCoID;
node: LocalNode;
header: CoValueHeader;
_sessions: { [key: SessionID]: SessionLog };
_cachedContent?: ContentType;
sessions: { [key: SessionID]: SessionLog };
content?: ContentType;
listeners: Set<(content?: ContentType) => void> = new Set();
constructor(
header: CoValueHeader,
node: LocalNode,
internalInitSessions: { [key: SessionID]: SessionLog } = {}
) {
constructor(header: CoValueHeader, node: LocalNode) {
this.id = idforHeader(header);
this.header = header;
this._sessions = internalInitSessions;
this.sessions = {};
this.node = node;
if (header.ruleset.type == "ownedByGroup") {
this.node
.expectCoValueLoaded(header.ruleset.group)
.subscribe((_groupUpdate) => {
this._cachedContent = undefined;
const newContent = this.getCurrentContent();
for (const listener of this.listeners) {
listener(newContent);
}
});
}
}
get sessions(): Readonly<{ [key: SessionID]: SessionLog }> {
return this._sessions;
}
testWithDifferentAccount(
@@ -193,10 +172,7 @@ export class CoValue {
);
if (givenExpectedNewHash && givenExpectedNewHash !== expectedNewHash) {
console.warn("Invalid hash", {
expectedNewHash,
givenExpectedNewHash,
});
console.warn("Invalid hash", { expectedNewHash, givenExpectedNewHash });
return false;
}
@@ -214,14 +190,14 @@ export class CoValue {
transactions.push(...newTransactions);
this._sessions[sessionID] = {
this.sessions[sessionID] = {
transactions,
lastHash: expectedNewHash,
streamingHash: newStreamingHash,
lastSignature: newSignature,
};
this._cachedContent = undefined;
this.content = undefined;
const content = this.getCurrentContent();
@@ -320,23 +296,23 @@ export class CoValue {
}
getCurrentContent(): ContentType {
if (this._cachedContent) {
return this._cachedContent;
if (this.content) {
return this.content;
}
if (this.header.type === "comap") {
this._cachedContent = new CoMap(this);
this.content = new CoMap(this);
} else if (this.header.type === "colist") {
this._cachedContent = new CoList(this);
this.content = new CoList(this);
} else if (this.header.type === "costream") {
this._cachedContent = new CoStream(this);
this.content = new CoStream(this);
} else if (this.header.type === "static") {
this._cachedContent = new Static(this);
this.content = new Static(this);
} else {
throw new Error(`Unknown coValue type ${this.header.type}`);
}
return this._cachedContent;
return this.content;
}
getValidSortedTransactions(): DecryptedTransaction[] {
@@ -391,8 +367,8 @@ export class CoValue {
}
getCurrentReadKey(): { secret: KeySecret | undefined; id: KeyID } {
if (this.header.ruleset.type === "group") {
const content = expectGroupContent(this.getCurrentContent());
if (this.header.ruleset.type === "team") {
const content = expectTeamContent(this.getCurrentContent());
const currentKeyId = content.get("readKey");
@@ -406,29 +382,24 @@ export class CoValue {
secret: secret,
id: currentKeyId,
};
} else if (this.header.ruleset.type === "ownedByGroup") {
} else if (this.header.ruleset.type === "ownedByTeam") {
return this.node
.expectCoValueLoaded(this.header.ruleset.group)
.expectCoValueLoaded(this.header.ruleset.team)
.getCurrentReadKey();
} else {
throw new Error(
"Only groups or values owned by groups have read secrets"
"Only teams or values owned by teams have read secrets"
);
}
}
getReadKey(keyID: KeyID): KeySecret | undefined {
if (readKeyCache.get(this)?.[keyID]) {
return readKeyCache.get(this)?.[keyID];
}
if (this.header.ruleset.type === "group") {
const content = expectGroupContent(this.getCurrentContent());
if (this.header.ruleset.type === "team") {
const content = expectTeamContent(this.getCurrentContent());
// Try to find key revelation for us
const readKeyEntry = content.getLastEntry(
`${keyID}_for_${this.node.account.id}`
);
const readKeyEntry = content.getLastEntry(`${keyID}_for_${this.node.account.id}`);
if (readKeyEntry) {
const revealer = accountOrAgentIDfromSessionID(
@@ -449,16 +420,7 @@ export class CoValue {
}
);
if (secret) {
let cache = readKeyCache.get(this);
if (!cache) {
cache = {};
readKeyCache.set(this, cache);
}
cache[keyID] = secret;
return secret as KeySecret;
}
if (secret) return secret as KeySecret;
}
// Try to find indirect revelation through previousKeys
@@ -466,8 +428,7 @@ export class CoValue {
for (const field of content.keys()) {
if (isKeyForKeyField(field) && field.startsWith(keyID)) {
const encryptingKeyID = field.split("_for_")[1] as KeyID;
const encryptingKeySecret =
this.getReadKey(encryptingKeyID);
const encryptingKeySecret = this.getReadKey(encryptingKeyID);
if (!encryptingKeySecret) {
continue;
@@ -485,43 +446,37 @@ export class CoValue {
);
if (secret) {
let cache = readKeyCache.get(this);
if (!cache) {
cache = {};
readKeyCache.set(this, cache);
}
cache[keyID] = secret;
return secret as KeySecret;
return secret;
} else {
console.error(
`Encrypting ${encryptingKeyID} key didn't decrypt ${keyID}`
);
}
}
}
return undefined;
} else if (this.header.ruleset.type === "ownedByGroup") {
} else if (this.header.ruleset.type === "ownedByTeam") {
return this.node
.expectCoValueLoaded(this.header.ruleset.group)
.expectCoValueLoaded(this.header.ruleset.team)
.getReadKey(keyID);
} else {
throw new Error(
"Only groups or values owned by groups have read secrets"
"Only teams or values owned by teams have read secrets"
);
}
}
getGroup(): Group {
if (this.header.ruleset.type !== "ownedByGroup") {
throw new Error("Only values owned by groups have groups");
getTeam(): Team {
if (this.header.ruleset.type !== "ownedByTeam") {
throw new Error("Only values owned by teams have teams");
}
return new Group(
expectGroupContent(
return new Team(
expectTeamContent(
this.node
.expectCoValueLoaded(this.header.ruleset.group)
.expectCoValueLoaded(this.header.ruleset.team)
.getCurrentContent()
),
this.node
@@ -570,7 +525,10 @@ export class CoValue {
),
};
if (!newContent.header && Object.keys(newContent.new).length === 0) {
if (
!newContent.header &&
Object.keys(newContent.new).length === 0
) {
return undefined;
}
@@ -578,12 +536,12 @@ export class CoValue {
}
getDependedOnCoValues(): RawCoID[] {
return this.header.ruleset.type === "group"
? expectGroupContent(this.getCurrentContent())
return this.header.ruleset.type === "team"
? expectTeamContent(this.getCurrentContent())
.keys()
.filter((k): k is AccountID => k.startsWith("co_"))
: this.header.ruleset.type === "ownedByGroup"
? [this.header.ruleset.group]
: this.header.ruleset.type === "ownedByTeam"
? [this.header.ruleset.team]
: [];
}
}
}

View File

@@ -3,7 +3,7 @@ import { createdNowUnique } from "./crypto.js";
import { LocalNode } from "./node.js";
import { randomAnonymousAccountAndSessionID } from "./testUtils.js";
test("Empty CoMap works", () => {
test("Empty COJSON Map works", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
@@ -24,7 +24,7 @@ test("Empty CoMap works", () => {
expect(content.toJSON()).toEqual({});
});
test("Can insert and delete CoMap entries in edit()", () => {
test("Can insert and delete Map entries in edit()", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
@@ -53,7 +53,7 @@ test("Can insert and delete CoMap entries in edit()", () => {
});
});
test("Can get CoMap entry values at different points in time", () => {
test("Can get map entry values at different points in time", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
@@ -89,7 +89,7 @@ test("Can get CoMap entry values at different points in time", () => {
});
});
test("Can get all historic values of key in CoMap", () => {
test("Can get all historic values of key", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
@@ -141,7 +141,7 @@ test("Can get all historic values of key in CoMap", () => {
});
});
test("Can get last tx ID for a key in CoMap", () => {
test("Can get last tx ID for a key", () => {
const node = new LocalNode(...randomAnonymousAccountAndSessionID());
const coValue = node.createCoValue({
@@ -173,112 +173,3 @@ test("Can get last tx ID for a key in CoMap", () => {
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"]);
});
})

View File

@@ -1,262 +1,19 @@
import { JsonObject, JsonValue } from "../jsonValue.js";
import { CoID } from "../contentType.js";
import { CoValue, accountOrAgentIDfromSessionID } from "../coValue.js";
import { SessionID, TransactionID } from "../ids.js";
import { AccountID } from "../index.js";
import { isAccountID } from "../account.js";
import { JsonObject, JsonValue } from '../jsonValue.js';
import { CoID } from '../contentType.js';
import { CoValue } from '../coValue.js';
type OpID = TransactionID & { changeIdx: number };
type InsertionOpPayload<T extends JsonValue> =
| {
op: "pre";
value: T;
before: OpID | "end";
}
| {
op: "app";
value: T;
after: OpID | "start";
};
type DeletionOpPayload = {
op: "del";
insertion: OpID;
};
export type ListOpPayload<T extends JsonValue> =
| InsertionOpPayload<T>
| DeletionOpPayload;
type InsertionEntry<T extends JsonValue> = {
madeAt: number;
predecessors: OpID[];
successors: OpID[];
} & InsertionOpPayload<T>;
type DeletionEntry = {
madeAt: number;
deletionID: OpID;
} & DeletionOpPayload;
export class CoList<
T extends JsonValue,
Meta extends JsonObject | null = null
> {
export class CoList<T extends JsonValue, Meta extends JsonObject | null = null> {
id: CoID<CoList<T, Meta>>;
type = "colist" as const;
coValue: CoValue;
afterStart: OpID[];
beforeEnd: OpID[];
insertions: {
[sessionID: SessionID]: {
[txIdx: number]: {
[changeIdx: number]: InsertionEntry<T>;
};
};
};
deletionsByInsertion: {
[deletedSessionID: SessionID]: {
[deletedTxIdx: number]: {
[deletedChangeIdx: number]: DeletionEntry[];
};
};
};
constructor(coValue: CoValue) {
this.id = coValue.id as CoID<CoList<T, Meta>>;
this.coValue = coValue;
this.afterStart = [];
this.beforeEnd = [];
this.insertions = {};
this.deletionsByInsertion = {};
this.fillOpsFromCoValue();
}
get meta(): Meta {
return this.coValue.header.meta as Meta;
}
protected fillOpsFromCoValue() {
this.insertions = {};
this.deletionsByInsertion = {};
this.afterStart = [];
this.beforeEnd = [];
for (const {
txID,
changes,
madeAt,
} of this.coValue.getValidSortedTransactions()) {
for (const [changeIdx, changeUntyped] of changes.entries()) {
const change = changeUntyped as ListOpPayload<T>;
if (change.op === "pre" || change.op === "app") {
let sessionEntry = this.insertions[txID.sessionID];
if (!sessionEntry) {
sessionEntry = {};
this.insertions[txID.sessionID] = sessionEntry;
}
let txEntry = sessionEntry[txID.txIndex];
if (!txEntry) {
txEntry = {};
sessionEntry[txID.txIndex] = txEntry;
}
txEntry[changeIdx] = {
madeAt,
predecessors: [],
successors: [],
...change,
};
if (change.op === "pre") {
if (change.before === "end") {
this.beforeEnd.push({
...txID,
changeIdx,
});
} else {
const beforeEntry =
this.insertions[change.before.sessionID]?.[
change.before.txIndex
]?.[change.before.changeIdx];
if (!beforeEntry) {
throw new Error(
"Not yet implemented: insertion before missing op " +
change.before
);
}
beforeEntry.predecessors.splice(0, 0, {
...txID,
changeIdx,
});
}
} else {
if (change.after === "start") {
this.afterStart.push({
...txID,
changeIdx,
});
} else {
const afterEntry =
this.insertions[change.after.sessionID]?.[
change.after.txIndex
]?.[change.after.changeIdx];
if (!afterEntry) {
throw new Error(
"Not yet implemented: insertion after missing op " +
change.after
);
}
afterEntry.successors.push({
...txID,
changeIdx,
});
}
}
} else if (change.op === "del") {
let sessionEntry =
this.deletionsByInsertion[change.insertion.sessionID];
if (!sessionEntry) {
sessionEntry = {};
this.deletionsByInsertion[change.insertion.sessionID] =
sessionEntry;
}
let txEntry = sessionEntry[change.insertion.txIndex];
if (!txEntry) {
txEntry = {};
sessionEntry[change.insertion.txIndex] = txEntry;
}
let changeEntry = txEntry[change.insertion.changeIdx];
if (!changeEntry) {
changeEntry = [];
txEntry[change.insertion.changeIdx] = changeEntry;
}
changeEntry.push({
madeAt,
deletionID: {
...txID,
changeIdx,
},
...change,
});
} else {
throw new Error(
"Unknown list operation " +
(change as { op: unknown }).op
);
}
}
}
}
entries(): { value: T; madeAt: number; opID: OpID }[] {
const arr: { value: T; madeAt: number; opID: OpID }[] = [];
for (const opID of this.afterStart) {
this.fillArrayFromOpID(opID, arr);
}
for (const opID of this.beforeEnd) {
this.fillArrayFromOpID(opID, arr);
}
return arr;
}
private fillArrayFromOpID(
opID: OpID,
arr: { value: T; madeAt: number; opID: OpID }[]
) {
const entry =
this.insertions[opID.sessionID]?.[opID.txIndex]?.[opID.changeIdx];
if (!entry) {
throw new Error("Missing op " + opID);
}
for (const predecessor of entry.predecessors) {
this.fillArrayFromOpID(predecessor, arr);
}
const deleted =
(this.deletionsByInsertion[opID.sessionID]?.[opID.txIndex]?.[
opID.changeIdx
]?.length || 0) > 0;
if (!deleted) {
arr.push({
value: entry.value,
madeAt: entry.madeAt,
opID,
});
}
for (const successor of entry.successors) {
this.fillArrayFromOpID(successor, arr);
}
}
getLastEditor(idx: number): AccountID | undefined {
const entry = this.entries()[idx];
if (!entry) {
return undefined;
}
const accountID = accountOrAgentIDfromSessionID(entry.opID.sessionID);
if (isAccountID(accountID)) {
return accountID;
} else {
return undefined;
}
}
toJSON(): T[] {
return this.asArray();
}
asArray(): T[] {
return this.entries().map((entry) => entry.value);
}
edit(
changer: (editable: WriteableCoList<T, Meta>) => void
): CoList<T, Meta> {
const editable = new WriteableCoList<T, Meta>(this.coValue);
changer(editable);
return new CoList(this.coValue);
toJSON(): JsonObject {
throw new Error("Method not implemented.");
}
subscribe(listener: (coMap: CoList<T, Meta>) => void): () => void {
@@ -265,106 +22,3 @@ export class CoList<
});
}
}
export class WriteableCoList<
T extends JsonValue,
Meta extends JsonObject | null = null
> extends CoList<T, Meta> {
append(
after: number,
value: T,
privacy: "private" | "trusting" = "private"
): void {
const entries = this.entries();
let opIDBefore;
if (entries.length > 0) {
const entryBefore = entries[after];
if (!entryBefore) {
throw new Error("Invalid index " + after);
}
opIDBefore = entryBefore.opID;
} else {
if (after !== 0) {
throw new Error("Invalid index " + after);
}
opIDBefore = "start";
}
this.coValue.makeTransaction(
[
{
op: "app",
value,
after: opIDBefore,
},
],
privacy
);
this.fillOpsFromCoValue();
}
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);
}
prepend(
before: number,
value: T,
privacy: "private" | "trusting" = "private"
): void {
const entries = this.entries();
let opIDAfter;
if (entries.length > 0) {
const entryAfter = entries[before];
if (entryAfter) {
opIDAfter = entryAfter.opID;
} else {
if (before !== entries.length) {
throw new Error("Invalid index " + before);
}
opIDAfter = "end";
}
} else {
if (before !== 0) {
throw new Error("Invalid index " + before);
}
opIDAfter = "end";
}
this.coValue.makeTransaction(
[
{
op: "pre",
value,
before: opIDAfter,
},
],
privacy
);
this.fillOpsFromCoValue();
}
delete(
at: number,
privacy: "private" | "trusting" = "private"
): void {
const entries = this.entries();
const entry = entries[at];
if (!entry) {
throw new Error("Invalid index " + at);
}
this.coValue.makeTransaction(
[
{
op: "del",
insertion: entry.opID,
},
],
privacy
);
this.fillOpsFromCoValue();
}
}

View File

@@ -12,12 +12,12 @@ type MapOp<K extends string, V extends JsonValue> = {
// TODO: add after TransactionID[] for conflicts/ordering
export type MapOpPayload<K extends string, V extends JsonValue> = {
op: "set";
op: "insert";
key: K;
value: V;
} |
{
op: "del";
op: "delete";
key: K;
};
@@ -46,10 +46,6 @@ export class CoMap<
this.fillOpsFromCoValue();
}
get meta(): Meta {
return this.coValue.header.meta as Meta;
}
protected fillOpsFromCoValue() {
this.ops = {};
@@ -85,7 +81,7 @@ export class CoMap<
const lastEntry = ops[ops.length - 1]!;
if (lastEntry.op === "del") {
if (lastEntry.op === "delete") {
return undefined;
} else {
return lastEntry.value;
@@ -104,7 +100,7 @@ export class CoMap<
return undefined;
}
if (lastOpBeforeOrAtTime.op === "del") {
if (lastOpBeforeOrAtTime.op === "delete") {
return undefined;
} else {
return lastOpBeforeOrAtTime.value;
@@ -143,7 +139,7 @@ export class CoMap<
const lastEntry = ops[ops.length - 1]!;
if (lastEntry.op === "del") {
if (lastEntry.op === "delete") {
return undefined;
} else {
return { at: lastEntry.madeAt, txID: lastEntry.txID, value: lastEntry.value };
@@ -159,7 +155,7 @@ export class CoMap<
const history: { at: number; txID: TransactionID; value: M[K] | undefined; }[] = [];
for (const op of ops) {
if (op.op === "del") {
if (op.op === "delete") {
history.push({ at: op.madeAt, txID: op.txID, value: undefined });
} else {
history.push({ at: op.madeAt, txID: op.txID, value: op.value });
@@ -203,7 +199,7 @@ export class WriteableCoMap<
set<K extends MapK<M>>(key: K, value: M[K], privacy: "private" | "trusting" = "private"): void {
this.coValue.makeTransaction([
{
op: "set",
op: "insert",
key,
value,
},
@@ -215,7 +211,7 @@ export class WriteableCoMap<
delete(key: MapK<M>, privacy: "private" | "trusting" = "private"): void {
this.coValue.makeTransaction([
{
op: "del",
op: "delete",
key,
},
], privacy);

View File

@@ -20,7 +20,6 @@ import { x25519 } from "@noble/curves/ed25519";
import { xsalsa20_poly1305 } from "@noble/ciphers/salsa";
import { blake3 } from "@noble/hashes/blake3";
import stableStringify from "fast-json-stable-stringify";
import { SessionID } from './ids.js';
test("Signatures round-trip and use stable stringify", () => {
const data = { b: "world", a: "hello" };
@@ -50,7 +49,7 @@ test("encrypting round-trips, but invalid receiver can't unseal", () => {
const nOnceMaterial = {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 0 },
} as const;
const sealed = seal(
@@ -102,22 +101,22 @@ test("Encryption for transactions round-trips", () => {
const encrypted1 = encryptForTransaction({ a: "hello" }, secret, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 0 },
});
const encrypted2 = encryptForTransaction({ b: "world" }, secret, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 1 },
});
const decrypted1 = decryptForTransaction(encrypted1, secret, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 0 },
});
const decrypted2 = decryptForTransaction(encrypted2, secret, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 1 },
});
expect([decrypted1, decrypted2]).toEqual([{ a: "hello" }, { b: "world" }]);
@@ -129,22 +128,22 @@ test("Encryption for transactions doesn't decrypt with a wrong key", () => {
const encrypted1 = encryptForTransaction({ a: "hello" }, secret, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 0 },
});
const encrypted2 = encryptForTransaction({ b: "world" }, secret, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 1 },
});
const decrypted1 = decryptForTransaction(encrypted1, secret2, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 0 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 0 },
});
const decrypted2 = decryptForTransaction(encrypted2, secret2, {
in: "co_zTEST",
tx: { sessionID: "co_zTEST_session_zTEST" as SessionID, txIndex: 1 },
tx: { sessionID: "co_zTEST_session_zTEST", txIndex: 1 },
});
expect([decrypted1, decrypted2]).toEqual([undefined, undefined]);

View File

@@ -1,11 +1,11 @@
import { ed25519, x25519 } from "@noble/curves/ed25519";
import { xsalsa20_poly1305, xsalsa20 } from "@noble/ciphers/salsa";
import { JsonValue } from "./jsonValue.js";
import { JsonValue } from './jsonValue.js';
import { base58, base64url } from "@scure/base";
import stableStringify from "fast-json-stable-stringify";
import { blake3 } from "@noble/hashes/blake3";
import { randomBytes } from "@noble/ciphers/webcrypto/utils";
import { AgentID, RawCoID, TransactionID } from "./ids.js";
import { AgentID, RawCoID, TransactionID } from './ids.js';
export type SignerSecret = `signerSecret_z${string}`;
export type SignerID = `signer_z${string}`;
@@ -21,7 +21,9 @@ const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
export function newRandomSigner(): SignerSecret {
return `signerSecret_z${base58.encode(ed25519.utils.randomPrivateKey())}`;
return `signerSecret_z${base58.encode(
ed25519.utils.randomPrivateKey()
)}`;
}
export function signerSecretToBytes(secret: SignerSecret): Uint8Array {
@@ -93,16 +95,20 @@ export function agentSecretToBytes(secret: AgentSecret): Uint8Array {
}
export function agentSecretFromBytes(bytes: Uint8Array): AgentSecret {
const sealerSecret = sealerSecretFromBytes(bytes.slice(0, 32));
const signerSecret = signerSecretFromBytes(bytes.slice(32));
const sealerSecret = sealerSecretFromBytes(
bytes.slice(0, 32)
);
const signerSecret = signerSecretFromBytes(
bytes.slice(32)
);
return `${sealerSecret}/${signerSecret}`;
}
export function getAgentID(secret: AgentSecret): AgentID {
const [sealerSecret, signerSecret] = secret.split("/");
return `${getSealerID(sealerSecret as SealerSecret)}/${getSignerID(
signerSecret as SignerSecret
)}`;
return `${getSealerID(
sealerSecret as SealerSecret
)}/${getSignerID(signerSecret as SignerSecret)}`;
}
export function getAgentSignerID(agentId: AgentID): SignerID {
@@ -133,17 +139,24 @@ export function seal<T extends JsonValue>(
const sealerPub = base58.decode(to.substring("sealer_z".length));
const senderPriv = base58.decode(from.substring("sealerSecret_z".length));
const senderPriv = base58.decode(
from.substring("sealerSecret_z".length)
);
const plaintext = textEncoder.encode(stableStringify(message));
const sharedSecret = x25519.getSharedSecret(senderPriv, sealerPub);
const sharedSecret = x25519.getSharedSecret(
senderPriv,
sealerPub
);
const sealedBytes = xsalsa20_poly1305(sharedSecret, nOnce).encrypt(
plaintext
);
return `sealed_U${base64url.encode(sealedBytes)}` as Sealed<T>;
return `sealed_U${base64url.encode(
sealedBytes
)}` as Sealed<T>
}
export function unseal<T extends JsonValue>(
@@ -156,7 +169,9 @@ export function unseal<T extends JsonValue>(
textEncoder.encode(stableStringify(nOnceMaterial))
).slice(0, 24);
const sealerPriv = base58.decode(sealer.substring("sealerSecret_z".length));
const sealerPriv = base58.decode(
sealer.substring("sealerSecret_z".length)
);
const senderPub = base58.decode(from.substring("sealer_z".length));
@@ -206,11 +221,10 @@ export class StreamingHash {
}
export type ShortHash = `shortHash_z${string}`;
export const shortHashLength = 19;
export function shortHash(value: JsonValue): ShortHash {
return `shortHash_z${base58.encode(
blake3(textEncoder.encode(stableStringify(value))).slice(0, shortHashLength)
blake3(textEncoder.encode(stableStringify(value))).slice(0, 19)
)}`;
}
@@ -260,10 +274,7 @@ export function encryptKeySecret(keys: {
}): {
encryptedID: KeyID;
encryptingID: KeyID;
encrypted: Encrypted<
KeySecret,
{ encryptedID: KeyID; encryptingID: KeyID }
>;
encrypted: Encrypted<KeySecret, { encryptedID: KeyID; encryptingID: KeyID }>;
} {
const nOnceMaterial = {
encryptedID: keys.toEncrypt.id,
@@ -317,10 +328,7 @@ export function decryptKeySecret(
encryptedInfo: {
encryptedID: KeyID;
encryptingID: KeyID;
encrypted: Encrypted<
KeySecret,
{ encryptedID: KeyID; encryptingID: KeyID }
>;
encrypted: Encrypted<KeySecret, { encryptedID: KeyID; encryptingID: KeyID }>;
},
sealingSecret: KeySecret
): KeySecret | undefined {
@@ -336,35 +344,10 @@ export function uniquenessForHeader(): `z${string}` {
return `z${base58.encode(randomBytes(12))}`;
}
export function createdNowUnique(): {
createdAt: `2${string}`;
uniqueness: `z${string}`;
} {
const createdAt = new Date().toISOString() as `2${string}`;
export function createdNowUnique(): {createdAt: `2${string}`, uniqueness: `z${string}`} {
const createdAt = (new Date()).toISOString() as `2${string}`;
return {
createdAt,
uniqueness: uniquenessForHeader(),
};
}
export const secretSeedLength = 32;
export function newRandomSecretSeed(): Uint8Array {
return randomBytes(secretSeedLength);
}
export function agentSecretFromSecretSeed(secretSeed: Uint8Array): AgentSecret {
if (secretSeed.length !== secretSeedLength) {
throw new Error(`Secret seed needs to be ${secretSeedLength} bytes long`);
}
return `sealerSecret_z${base58.encode(
blake3(secretSeed, {
context: textEncoder.encode("seal"),
})
)}/signerSecret_z${base58.encode(
blake3(secretSeed, {
context: textEncoder.encode("sign"),
})
)}`;
}
}

View File

@@ -1,249 +0,0 @@
import { CoID, ContentType } from "./contentType.js";
import { CoMap } from "./contentTypes/coMap.js";
import { JsonObject, JsonValue } from "./jsonValue.js";
import {
Encrypted,
KeyID,
KeySecret,
createdNowUnique,
newRandomKeySecret,
seal,
encryptKeySecret,
getAgentSealerID,
Sealed,
newRandomSecretSeed,
agentSecretFromSecretSeed,
getAgentID,
} from "./crypto.js";
import { LocalNode } from "./node.js";
import { SessionID, isAgentID } from "./ids.js";
import {
AccountIDOrAgentID,
GeneralizedControlledAccount,
Profile,
} from "./account.js";
import { Role } from "./permissions.js";
import { base58 } from "@scure/base";
import { CoList } from "./contentTypes/coList.js";
export type GroupContent = {
profile: CoID<Profile> | null;
[key: AccountIDOrAgentID]: Role;
readKey: KeyID;
[revelationFor: `${KeyID}_for_${AccountIDOrAgentID}`]: Sealed<KeySecret>;
[oldKeyForNewKey: `${KeyID}_for_${KeyID}`]: Encrypted<
KeySecret,
{ encryptedID: KeyID; encryptingID: KeyID }
>;
};
export function expectGroupContent(
content: ContentType
): CoMap<GroupContent, JsonObject | null> {
if (content.type !== "comap") {
throw new Error("Expected map");
}
return content as CoMap<GroupContent, JsonObject | null>;
}
export class Group {
groupMap: CoMap<GroupContent, JsonObject | null>;
node: LocalNode;
constructor(
groupMap: CoMap<GroupContent, JsonObject | null>,
node: LocalNode
) {
this.groupMap = groupMap;
this.node = node;
}
get id(): CoID<CoMap<GroupContent, JsonObject | null>> {
return this.groupMap.id;
}
roleOf(accountID: AccountIDOrAgentID): Role | undefined {
return this.groupMap.get(accountID);
}
myRole(): Role | undefined {
return this.roleOf(this.node.account.id);
}
addMember(accountID: AccountIDOrAgentID, role: Role) {
this.groupMap = this.groupMap.edit((map) => {
const currentReadKey = this.groupMap.coValue.getCurrentReadKey();
if (!currentReadKey.secret) {
throw new Error("Can't add member without read key secret");
}
const agent = this.node.resolveAccountAgent(
accountID,
"Expected to know agent to add them to group"
);
map.set(accountID, role, "trusting");
if (map.get(accountID) !== role) {
throw new Error("Failed to set role");
}
map.set(
`${currentReadKey.id}_for_${accountID}`,
seal(
currentReadKey.secret,
this.groupMap.coValue.node.account.currentSealerSecret(),
getAgentSealerID(agent),
{
in: this.groupMap.coValue.id,
tx: this.groupMap.coValue.nextTransactionID(),
}
),
"trusting"
);
});
}
createInvite(role: "reader" | "writer" | "admin"): InviteSecret {
const secretSeed = newRandomSecretSeed();
const inviteSecret = agentSecretFromSecretSeed(secretSeed);
const inviteID = getAgentID(inviteSecret);
this.addMember(inviteID, `${role}Invite` as Role);
return inviteSecretFromSecretSeed(secretSeed);
}
rotateReadKey() {
const currentlyPermittedReaders = this.groupMap.keys().filter((key) => {
if (key.startsWith("co_") || isAgentID(key)) {
const role = this.groupMap.get(key);
return (
role === "admin" || role === "writer" || role === "reader"
);
} else {
return false;
}
}) as AccountIDOrAgentID[];
const maybeCurrentReadKey = this.groupMap.coValue.getCurrentReadKey();
if (!maybeCurrentReadKey.secret) {
throw new Error(
"Can't rotate read key secret we don't have access to"
);
}
const currentReadKey = {
id: maybeCurrentReadKey.id,
secret: maybeCurrentReadKey.secret,
};
const newReadKey = newRandomKeySecret();
this.groupMap = this.groupMap.edit((map) => {
for (const readerID of currentlyPermittedReaders) {
const reader = this.node.resolveAccountAgent(
readerID,
"Expected to know currently permitted reader"
);
map.set(
`${newReadKey.id}_for_${readerID}`,
seal(
newReadKey.secret,
this.groupMap.coValue.node.account.currentSealerSecret(),
getAgentSealerID(reader),
{
in: this.groupMap.coValue.id,
tx: this.groupMap.coValue.nextTransactionID(),
}
),
"trusting"
);
}
map.set(
`${currentReadKey.id}_for_${newReadKey.id}`,
encryptKeySecret({
encrypting: newReadKey,
toEncrypt: currentReadKey,
}).encrypted,
"trusting"
);
map.set("readKey", newReadKey.id, "trusting");
});
}
removeMember(accountID: AccountIDOrAgentID) {
this.groupMap = this.groupMap.edit((map) => {
map.set(accountID, "revoked", "trusting");
});
this.rotateReadKey();
}
createMap<M extends CoMap<{ [key: string]: JsonValue }, JsonObject | null>>(
meta?: M["meta"]
): M {
return this.node
.createCoValue({
type: "comap",
ruleset: {
type: "ownedByGroup",
group: this.groupMap.id,
},
meta: meta || null,
...createdNowUnique(),
})
.getCurrentContent() as M;
}
createList<L extends CoList<JsonValue, JsonObject | null>>(
meta?: L["meta"]
): L {
return this.node
.createCoValue({
type: "colist",
ruleset: {
type: "ownedByGroup",
group: this.groupMap.id,
},
meta: meta || null,
...createdNowUnique(),
})
.getCurrentContent() as L;
}
testWithDifferentAccount(
account: GeneralizedControlledAccount,
sessionId: SessionID
): Group {
return new Group(
expectGroupContent(
this.groupMap.coValue
.testWithDifferentAccount(account, sessionId)
.getCurrentContent()
),
this.node
);
}
}
export type InviteSecret = `inviteSecret_z${string}`;
function inviteSecretFromSecretSeed(secretSeed: Uint8Array): InviteSecret {
return `inviteSecret_z${base58.encode(secretSeed)}`;
}
export function secretSeedFromInviteSecret(inviteSecret: InviteSecret) {
if (!inviteSecret.startsWith("inviteSecret_z")) {
throw new Error("Invalid invite secret");
}
return base58.decode(inviteSecret.slice("inviteSecret_z".length));
}

View File

@@ -1,20 +1,7 @@
import { AccountIDOrAgentID } from './account.js';
import { base58 } from "@scure/base";
import { shortHashLength } from './crypto.js';
export type RawCoID = `co_z${string}`;
export function rawCoIDtoBytes(id: RawCoID): Uint8Array {
return base58.decode(
id.substring("co_z".length)
)
}
export function rawCoIDfromBytes(bytes: Uint8Array): RawCoID {
return `co_z${base58.encode(bytes.slice(0, shortHashLength))}` as RawCoID;
}
export type TransactionID = { sessionID: SessionID; txIndex: number };
export type AgentID = `sealer_z${string}/signer_z${string}`;

View File

@@ -6,29 +6,15 @@ import {
agentSecretToBytes,
getAgentID,
newRandomAgentSecret,
newRandomSecretSeed,
agentSecretFromSecretSeed,
secretSeedLength,
shortHashLength,
} from "./crypto.js";
import { connectedPeers } from "./streamUtils.js";
import { AnonymousControlledAccount, ControlledAccount } from "./account.js";
import { rawCoIDtoBytes, rawCoIDfromBytes } from "./ids.js";
import { Group, expectGroupContent } from "./group.js"
import type { SessionID, AgentID } from "./ids.js";
import type { CoID, ContentType } from "./contentType.js";
import type { JsonValue } from "./jsonValue.js";
import type { SyncMessage, Peer } from "./sync.js";
import type { SyncMessage } from "./sync.js";
import type { AgentSecret } from "./crypto.js";
import type {
AccountID,
AccountContent,
ProfileContent,
ProfileMeta,
Profile,
} from "./account.js";
import type { InviteSecret } from "./group.js";
type Value = JsonValue | ContentType;
@@ -39,22 +25,15 @@ export const cojsonInternals = {
newRandomAgentSecret,
connectedPeers,
getAgentID,
rawCoIDtoBytes,
rawCoIDfromBytes,
newRandomSecretSeed,
agentSecretFromSecretSeed,
secretSeedLength,
shortHashLength,
expectGroupContent
};
export {
LocalNode,
CoValue,
CoMap,
cojsonInternals as internals,
AnonymousControlledAccount,
ControlledAccount,
Group
};
export type {
@@ -66,16 +45,8 @@ export type {
SessionID,
SyncMessage,
AgentID,
AccountID,
Peer,
AccountContent,
Profile,
ProfileContent,
ProfileMeta,
InviteSecret
};
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace CojsonInternalTypes {
export type CoValueKnownState = import("./sync.js").CoValueKnownState;
export type DoneMessage = import("./sync.js").DoneMessage;
@@ -86,5 +57,4 @@ export namespace CojsonInternalTypes {
export type Transaction = import("./coValue.js").Transaction;
export type Signature = import("./crypto.js").Signature;
export type RawCoID = import("./ids.js").RawCoID;
export type AccountIDOrAgentID = import("./account.js").AccountIDOrAgentID;
}

View File

@@ -1,6 +1,6 @@
import { RawCoID } from './ids.js';
import { CoID, ContentType } from './contentType.js';
export type JsonAtom = string | number | boolean | null;
export type JsonValue = JsonAtom | JsonArray | JsonObject | RawCoID;
export type JsonValue = JsonAtom | JsonArray | JsonObject | CoID<ContentType>;
export type JsonArray = JsonValue[];
export type JsonObject = { [key: string]: JsonValue; };

View File

@@ -1,6 +1,5 @@
import {
AgentSecret,
agentSecretFromSecretSeed,
createdNowUnique,
getAgentID,
getAgentSealerID,
@@ -10,13 +9,7 @@ import {
seal,
} from "./crypto.js";
import { CoValue, CoValueHeader, newRandomSessionID } from "./coValue.js";
import {
InviteSecret,
Group,
GroupContent,
expectGroupContent,
secretSeedFromInviteSecret,
} from "./group.js";
import { Team, TeamContent, expectTeamContent } from "./permissions.js";
import { Peer, SyncManager } from "./sync.js";
import { AgentID, RawCoID, SessionID, isAgentID } from "./ids.js";
import { CoID, ContentType } from "./contentType.js";
@@ -31,7 +24,8 @@ import {
AccountID,
Profile,
AccountContent,
AccountMap,
ProfileContent,
ProfileMeta,
} from "./account.js";
import { CoMap } from "./index.js";
@@ -49,10 +43,7 @@ export class LocalNode {
this.ownSessionID = ownSessionID;
}
static withNewlyCreatedAccount(
name: string,
initialAgentSecret = newRandomAgentSecret()
): {
static withNewlyCreatedAccount(name: string): {
node: LocalNode;
accountID: AccountID;
accountSecret: AgentSecret;
@@ -64,7 +55,7 @@ export class LocalNode {
newRandomSessionID(getAgentID(throwawayAgent))
);
const account = setupNode.createAccount(name, initialAgentSecret);
const account = setupNode.createAccount(name);
const nodeWithAccount = account.node.testWithDifferentAccount(
account,
@@ -79,16 +70,8 @@ export class LocalNode {
};
}
static async withLoadedAccount(
accountID: AccountID,
accountSecret: AgentSecret,
sessionID: SessionID,
peersToLoadFrom: Peer[]
): Promise<LocalNode> {
const loadingNode = new LocalNode(
new AnonymousControlledAccount(accountSecret),
newRandomSessionID(accountID)
);
static async withLoadedAccount(accountID: AccountID, accountSecret: AgentSecret, sessionID: SessionID, peersToLoadFrom: Peer[]): Promise<LocalNode> {
const loadingNode = new LocalNode(new AnonymousControlledAccount(accountSecret), newRandomSessionID(accountID));
const accountPromise = loadingNode.load(accountID);
@@ -99,10 +82,7 @@ export class LocalNode {
const account = await accountPromise;
// since this is all synchronous, we can just swap out nodes for the SyncManager
const node = loadingNode.testWithDifferentAccount(
new ControlledAccount(accountSecret, account, loadingNode),
sessionID
);
const node = loadingNode.testWithDifferentAccount(new ControlledAccount(accountSecret, account, loadingNode), sessionID);
node.sync = loadingNode.sync;
node.sync.local = node;
@@ -138,93 +118,13 @@ export class LocalNode {
}
async loadProfile(id: AccountID): Promise<Profile> {
const account = await this.load<AccountMap>(id);
const account = await this.load<CoMap<AccountContent>>(id);
const profileID = account.get("profile");
if (!profileID) {
throw new Error(`Account ${id} has no profile`);
}
return (
await this.loadCoValue(profileID)
).getCurrentContent() as Profile;
}
async acceptInvite<T extends ContentType>(
groupOrOwnedValueID: CoID<T>,
inviteSecret: InviteSecret
): Promise<void> {
const groupOrOwnedValue = await this.load(groupOrOwnedValueID);
if (groupOrOwnedValue.coValue.header.ruleset.type === "ownedByGroup") {
return this.acceptInvite(
groupOrOwnedValue.coValue.header.ruleset.group as CoID<
CoMap<GroupContent>
>,
inviteSecret
);
} else if (groupOrOwnedValue.coValue.header.ruleset.type !== "group") {
throw new Error("Can only accept invites to groups");
}
const group = new Group(expectGroupContent(groupOrOwnedValue), this);
const inviteAgentSecret = agentSecretFromSecretSeed(
secretSeedFromInviteSecret(inviteSecret)
);
const inviteAgentID = getAgentID(inviteAgentSecret);
const inviteRole = await new Promise((resolve, reject) => {
group.groupMap.subscribe((groupMap) => {
const role = groupMap.get(inviteAgentID);
if (role) {
resolve(role);
}
});
setTimeout(
() =>
reject(
new Error("Couldn't find invite before timeout")
),
1000
);
});
if (!inviteRole) {
throw new Error("No invite found");
}
const existingRole = group.groupMap.get(this.account.id);
if (
existingRole === "admin" ||
(existingRole === "writer" && inviteRole === "writerInvite") ||
(existingRole === "writer" && inviteRole === "reader") ||
(existingRole === "reader" && inviteRole === "readerInvite")
) {
console.debug("Not accepting invite that would replace or downgrade role");
return;
}
const groupAsInvite = group.testWithDifferentAccount(
new AnonymousControlledAccount(inviteAgentSecret),
newRandomSessionID(inviteAgentID)
);
groupAsInvite.addMember(
this.account.id,
inviteRole === "adminInvite"
? "admin"
: inviteRole === "writerInvite"
? "writer"
: "reader"
);
group.groupMap.coValue._sessions = groupAsInvite.groupMap.coValue.sessions;
group.groupMap.coValue._cachedContent = undefined;
for (const groupListener of group.groupMap.coValue.listeners) {
groupListener(group.groupMap.coValue.getCurrentContent());
}
return (await this.loadCoValue(profileID)).getCurrentContent() as Profile;
}
expectCoValueLoaded(id: RawCoID, expectation?: string): CoValue {
@@ -246,9 +146,7 @@ export class LocalNode {
expectProfileLoaded(id: AccountID, expectation?: string): Profile {
const account = this.expectCoValueLoaded(id, expectation);
const profileID = expectGroupContent(account.getCurrentContent()).get(
"profile"
);
const profileID = expectTeamContent(account.getCurrentContent()).get("profile");
if (!profileID) {
throw new Error(
`${
@@ -256,16 +154,12 @@ export class LocalNode {
}Account ${id} has no profile`
);
}
return this.expectCoValueLoaded(
profileID,
expectation
).getCurrentContent() as Profile;
return this.expectCoValueLoaded(profileID, expectation).getCurrentContent() as Profile;
}
createAccount(
name: string,
agentSecret = newRandomAgentSecret()
): ControlledAccount {
createAccount(name: string): ControlledAccount {
const agentSecret = newRandomAgentSecret();
const account = this.createCoValue(
accountHeaderForInitialAgentSecret(agentSecret)
).testWithDifferentAccount(
@@ -273,12 +167,9 @@ export class LocalNode {
newRandomSessionID(getAgentID(agentSecret))
);
const accountAsGroup = new Group(
expectGroupContent(account.getCurrentContent()),
account.node
);
const accountAsTeam = new Team(expectTeamContent(account.getCurrentContent()), account.node);
accountAsGroup.groupMap.edit((editable) => {
accountAsTeam.teamMap.edit((editable) => {
editable.set(getAgentID(agentSecret), "admin", "trusting");
const readKey = newRandomKeySecret();
@@ -306,23 +197,16 @@ export class LocalNode {
account.node
);
const profile = accountAsGroup.createMap<Profile>({
type: "profile",
});
const profile = accountAsTeam.createMap<ProfileContent, ProfileMeta>({ type: "profile" });
profile.edit((editable) => {
editable.set("name", name, "trusting");
});
accountAsGroup.groupMap.edit((editable) => {
accountAsTeam.teamMap.edit((editable) => {
editable.set("profile", profile.id, "trusting");
});
const accountOnThisNode = this.expectCoValueLoaded(account.id);
accountOnThisNode._sessions = {...accountAsGroup.groupMap.coValue.sessions};
accountOnThisNode._cachedContent = undefined;
return controlledAccount;
}
@@ -335,7 +219,7 @@ export class LocalNode {
if (
coValue.header.type !== "comap" ||
coValue.header.ruleset.type !== "group" ||
coValue.header.ruleset.type !== "team" ||
!coValue.header.meta ||
!("type" in coValue.header.meta) ||
coValue.header.meta.type !== "account"
@@ -348,22 +232,22 @@ export class LocalNode {
}
return new Account(
coValue.getCurrentContent() as CoMap<GroupContent, AccountMeta>,
coValue.getCurrentContent() as CoMap<TeamContent, AccountMeta>,
this
).getCurrentAgentID();
}
createGroup(): Group {
const groupCoValue = this.createCoValue({
createTeam(): Team {
const teamCoValue = this.createCoValue({
type: "comap",
ruleset: { type: "group", initialAdmin: this.account.id },
ruleset: { type: "team", initialAdmin: this.account.id },
meta: null,
...createdNowUnique(),
});
let groupContent = expectGroupContent(groupCoValue.getCurrentContent());
let teamContent = expectTeamContent(teamCoValue.getCurrentContent());
groupContent = groupContent.edit((editable) => {
teamContent = teamContent.edit((editable) => {
editable.set(this.account.id, "admin", "trusting");
const readKey = newRandomKeySecret();
@@ -375,8 +259,8 @@ export class LocalNode {
this.account.currentSealerSecret(),
this.account.currentSealerID(),
{
in: groupCoValue.id,
tx: groupCoValue.nextTransactionID(),
in: teamCoValue.id,
tx: teamCoValue.nextTransactionID(),
}
),
"trusting"
@@ -385,7 +269,7 @@ export class LocalNode {
editable.set("readKey", readKey.id, "trusting");
});
return new Group(groupContent, this);
return new Team(teamContent, this);
}
testWithDifferentAccount(
@@ -394,36 +278,24 @@ export class LocalNode {
): LocalNode {
const newNode = new LocalNode(account, ownSessionID);
const coValuesToCopy = Object.entries(this.coValues);
newNode.coValues = Object.fromEntries(
Object.entries(this.coValues)
.map(([id, entry]) => {
if (entry.state === "loading") {
return undefined;
}
while (coValuesToCopy.length > 0) {
const [coValueID, entry] =
coValuesToCopy[coValuesToCopy.length - 1]!;
const newCoValue = new CoValue(
entry.coValue.header,
newNode
);
if (entry.state === "loading") {
coValuesToCopy.pop();
continue;
} else {
const allDepsCopied = entry.coValue
.getDependedOnCoValues()
.every((dep) => newNode.coValues[dep]?.state === "loaded");
newCoValue.sessions = entry.coValue.sessions;
if (!allDepsCopied) {
// move to end of queue
coValuesToCopy.unshift(coValuesToCopy.pop()!);
continue;
}
const newCoValue = new CoValue(entry.coValue.header, newNode, {...entry.coValue.sessions});
newNode.coValues[coValueID as RawCoID] = {
state: "loaded",
coValue: newCoValue,
};
coValuesToCopy.pop();
}
}
return [id, { state: "loaded", coValue: newCoValue }];
})
.filter((x): x is Exclude<typeof x, undefined> => !!x)
);
return newNode;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,16 @@
import { CoID } from "./contentType.js";
import { MapOpPayload } from "./contentTypes/coMap.js";
import { JsonValue } from "./jsonValue.js";
import { CoID, ContentType } from "./contentType.js";
import { CoMap, MapOpPayload } from "./contentTypes/coMap.js";
import { JsonObject, JsonValue } from "./jsonValue.js";
import {
Encrypted,
KeyID,
KeySecret,
createdNowUnique,
newRandomKeySecret,
seal,
encryptKeySecret,
getAgentSealerID,
Sealed,
} from "./crypto.js";
import {
CoValue,
@@ -10,30 +18,21 @@ import {
TrustingTransaction,
accountOrAgentIDfromSessionID,
} from "./coValue.js";
import { RawCoID, SessionID, TransactionID } from "./ids.js";
import {
AccountIDOrAgentID,
Profile,
} from "./account.js";
import { LocalNode } from "./node.js";
import { RawCoID, SessionID, TransactionID, isAgentID } from "./ids.js";
import { AccountIDOrAgentID, GeneralizedControlledAccount, Profile } from "./account.js";
export type PermissionsDef =
| { type: "group"; initialAdmin: AccountIDOrAgentID }
| { type: "ownedByGroup"; group: RawCoID }
| { type: "team"; initialAdmin: AccountIDOrAgentID }
| { type: "ownedByTeam"; team: RawCoID }
| { type: "unsafeAllowAll" };
export type Role =
| "reader"
| "writer"
| "admin"
| "revoked"
| "adminInvite"
| "writerInvite"
| "readerInvite";
export type Role = "reader" | "writer" | "admin" | "revoked";
export function determineValidTransactions(
coValue: CoValue
): { txID: TransactionID; tx: Transaction }[] {
if (coValue.header.ruleset.type === "group") {
if (coValue.header.ruleset.type === "team") {
const allTrustingTransactionsSorted = Object.entries(
coValue.sessions
).flatMap(([sessionID, sessionLog]) => {
@@ -43,7 +42,7 @@ export function determineValidTransactions(
if (tx.privacy === "trusting") {
return true;
} else {
console.warn("Unexpected private transaction in Group");
console.warn("Unexpected private transaction in Team");
return false;
}
}) as {
@@ -60,7 +59,7 @@ export function determineValidTransactions(
const initialAdmin = coValue.header.ruleset.initialAdmin;
if (!initialAdmin) {
throw new Error("Group must have initialAdmin");
throw new Error("Team must have initialAdmin");
}
const memberState: { [agent: AccountIDOrAgentID]: Role } = {};
@@ -81,12 +80,12 @@ export function determineValidTransactions(
| MapOpPayload<"readKey", JsonValue>
| MapOpPayload<"profile", CoID<Profile>>;
if (tx.changes.length !== 1) {
console.warn("Group transaction must have exactly one change");
console.warn("Team transaction must have exactly one change");
continue;
}
if (change.op !== "set") {
console.warn("Group transaction must set a role or readKey");
if (change.op !== "insert") {
console.warn("Team transaction must set a role or readKey");
continue;
}
@@ -98,7 +97,7 @@ export function determineValidTransactions(
validTransactions.push({ txID: { sessionID, txIndex }, tx });
continue;
} else if (change.key === "profile") {
} else if (change.key === 'profile') {
if (memberState[transactor] !== "admin") {
console.warn("Only admins can set profile");
continue;
@@ -106,16 +105,8 @@ export function determineValidTransactions(
validTransactions.push({ txID: { sessionID, txIndex }, tx });
continue;
} else if (
isKeyForKeyField(change.key) ||
isKeyForAccountField(change.key)
) {
if (
memberState[transactor] !== "admin" &&
memberState[transactor] !== "adminInvite" &&
memberState[transactor] !== "writerInvite" &&
memberState[transactor] !== "readerInvite"
) {
} else if (isKeyForKeyField(change.key) || isKeyForAccountField(change.key)) {
if (memberState[transactor] !== "admin") {
console.warn("Only admins can reveal keys");
continue;
}
@@ -133,53 +124,35 @@ export function determineValidTransactions(
change.value !== "admin" &&
change.value !== "writer" &&
change.value !== "reader" &&
change.value !== "revoked" &&
change.value !== "adminInvite" &&
change.value !== "writerInvite" &&
change.value !== "readerInvite"
change.value !== "revoked"
) {
console.warn("Group transaction must set a valid role");
console.warn("Team transaction must set a valid role");
continue;
}
const isFirstSelfAppointment =
!memberState[transactor] &&
transactor === initialAdmin &&
change.op === "set" &&
change.op === "insert" &&
change.key === transactor &&
change.value === "admin";
if (!isFirstSelfAppointment) {
if (memberState[transactor] === "admin") {
if (
memberState[affectedMember] === "admin" &&
affectedMember !== transactor &&
assignedRole !== "admin"
) {
console.warn("Admins can only demote themselves.");
continue;
}
} else if (memberState[transactor] === "adminInvite") {
if (change.value !== "admin") {
console.warn("AdminInvites can only create admins.");
continue;
}
} else if (memberState[transactor] === "writerInvite") {
if (change.value !== "writer") {
console.warn("WriterInvites can only create writers.");
continue;
}
} else if (memberState[transactor] === "readerInvite") {
if (change.value !== "reader") {
console.warn("ReaderInvites can only create reader.");
continue;
}
} else {
if (memberState[transactor] !== "admin") {
console.warn(
"Group transaction must be made by current admin or invite"
"Team transaction must be made by current admin"
);
continue;
}
if (
memberState[affectedMember] === "admin" &&
affectedMember !== transactor &&
assignedRole !== "admin"
) {
console.warn("Admins can only demote themselves.");
continue;
}
}
memberState[affectedMember] = change.value;
@@ -189,16 +162,16 @@ export function determineValidTransactions(
}
return validTransactions;
} else if (coValue.header.ruleset.type === "ownedByGroup") {
const groupContent = coValue.node
} else if (coValue.header.ruleset.type === "ownedByTeam") {
const teamContent = coValue.node
.expectCoValueLoaded(
coValue.header.ruleset.group,
"Determining valid transaction in owned object but its group wasn't loaded"
coValue.header.ruleset.team,
"Determining valid transaction in owned object but its team wasn't loaded"
)
.getCurrentContent();
if (groupContent.type !== "comap") {
throw new Error("Group must be a map");
if (teamContent.type !== "comap") {
throw new Error("Team must be a map");
}
return Object.entries(coValue.sessions).flatMap(
@@ -208,7 +181,7 @@ export function determineValidTransactions(
);
return sessionLog.transactions
.filter((tx) => {
const transactorRoleAtTxTime = groupContent.getAtTime(
const transactorRoleAtTxTime = teamContent.getAtTime(
transactor,
tx.madeAt
);
@@ -240,17 +213,180 @@ export function determineValidTransactions(
}
}
export function isKeyForKeyField(
field: string
): field is `${KeyID}_for_${KeyID}` {
export type TeamContent = {
profile: CoID<Profile> | null;
[key: AccountIDOrAgentID]: Role;
readKey: KeyID;
[revelationFor: `${KeyID}_for_${AccountIDOrAgentID}`]: Sealed<KeySecret>;
[oldKeyForNewKey: `${KeyID}_for_${KeyID}`]: Encrypted<
KeySecret,
{ encryptedID: KeyID; encryptingID: KeyID }
>;
};
export function expectTeamContent(
content: ContentType
): CoMap<TeamContent, JsonObject | null> {
if (content.type !== "comap") {
throw new Error("Expected map");
}
return content as CoMap<TeamContent, JsonObject | null>;
}
export class Team {
teamMap: CoMap<TeamContent, JsonObject | null>;
node: LocalNode;
constructor(teamMap: CoMap<TeamContent, JsonObject | null>, node: LocalNode) {
this.teamMap = teamMap;
this.node = node;
}
get id(): CoID<CoMap<TeamContent, JsonObject | null>> {
return this.teamMap.id;
}
addMember(accountID: AccountIDOrAgentID, role: Role) {
this.teamMap = this.teamMap.edit((map) => {
const currentReadKey = this.teamMap.coValue.getCurrentReadKey();
if (!currentReadKey.secret) {
throw new Error("Can't add member without read key secret");
}
const agent = this.node.resolveAccountAgent(
accountID,
"Expected to know agent to add them to team"
);
map.set(accountID, role, "trusting");
if (map.get(accountID) !== role) {
throw new Error("Failed to set role");
}
map.set(
`${currentReadKey.id}_for_${accountID}`,
seal(
currentReadKey.secret,
this.teamMap.coValue.node.account.currentSealerSecret(),
getAgentSealerID(agent),
{
in: this.teamMap.coValue.id,
tx: this.teamMap.coValue.nextTransactionID(),
}
),
"trusting"
);
});
}
rotateReadKey() {
const currentlyPermittedReaders = this.teamMap.keys().filter((key) => {
if (key.startsWith("co_") || isAgentID(key)) {
const role = this.teamMap.get(key);
return (
role === "admin" || role === "writer" || role === "reader"
);
} else {
return false;
}
}) as AccountIDOrAgentID[];
const maybeCurrentReadKey = this.teamMap.coValue.getCurrentReadKey();
if (!maybeCurrentReadKey.secret) {
throw new Error(
"Can't rotate read key secret we don't have access to"
);
}
const currentReadKey = {
id: maybeCurrentReadKey.id,
secret: maybeCurrentReadKey.secret,
};
const newReadKey = newRandomKeySecret();
this.teamMap = this.teamMap.edit((map) => {
for (const readerID of currentlyPermittedReaders) {
const reader = this.node.resolveAccountAgent(
readerID,
"Expected to know currently permitted reader"
);
map.set(
`${newReadKey.id}_for_${readerID}`,
seal(
newReadKey.secret,
this.teamMap.coValue.node.account.currentSealerSecret(),
getAgentSealerID(reader),
{
in: this.teamMap.coValue.id,
tx: this.teamMap.coValue.nextTransactionID(),
}
),
"trusting"
);
}
map.set(
`${currentReadKey.id}_for_${newReadKey.id}`,
encryptKeySecret({
encrypting: newReadKey,
toEncrypt: currentReadKey,
}).encrypted,
"trusting"
);
map.set("readKey", newReadKey.id, "trusting");
});
}
removeMember(accountID: AccountIDOrAgentID) {
this.teamMap = this.teamMap.edit((map) => {
map.set(accountID, "revoked", "trusting");
});
this.rotateReadKey();
}
createMap<M extends { [key: string]: JsonValue }, Meta extends JsonObject | null = null>(
meta?: Meta
): CoMap<M, Meta> {
return this.node
.createCoValue({
type: "comap",
ruleset: {
type: "ownedByTeam",
team: this.teamMap.id,
},
meta: meta || null,
...createdNowUnique(),
})
.getCurrentContent() as CoMap<M, Meta>;
}
testWithDifferentAccount(
account: GeneralizedControlledAccount,
sessionId: SessionID
): Team {
return new Team(
expectTeamContent(
this.teamMap.coValue
.testWithDifferentAccount(account, sessionId)
.getCurrentContent()
),
this.node
);
}
}
export function isKeyForKeyField(field: string): field is `${KeyID}_for_${KeyID}` {
return field.startsWith("key_") && field.includes("_for_key");
}
export function isKeyForAccountField(
field: string
): field is `${KeyID}_for_${AccountIDOrAgentID}` {
return (
field.startsWith("key_") &&
(field.includes("_for_sealer") || field.includes("_for_co"))
);
}
export function isKeyForAccountField(field: string): field is `${KeyID}_for_${AccountIDOrAgentID}` {
return field.startsWith("key_") && (field.includes("_for_sealer") || field.includes("_for_co"));
}

View File

@@ -1,17 +1,12 @@
import {
ReadableStream,
TransformStream,
WritableStream,
} from "isomorphic-streams";
import { ReadableStream, TransformStream, WritableStream } from "isomorphic-streams";
import { Peer, PeerID, SyncMessage } from "./sync.js";
export function connectedPeers(
peer1id: PeerID,
peer2id: PeerID,
{
trace = false,
peer1role = "peer",
peer2role = "peer",
trace = false, peer1role = "peer", peer2role = "peer",
}: {
trace?: boolean;
peer1role?: Peer["role"];
@@ -29,13 +24,9 @@ export function connectedPeers(
new TransformStream({
transform(
chunk: SyncMessage,
controller: { enqueue: (msg: SyncMessage) => void }
controller: { enqueue: (msg: SyncMessage) => void; }
) {
trace &&
console.debug(
`${peer2id} -> ${peer1id}`,
JSON.stringify(chunk, null, 2)
);
trace && console.debug(`${peer2id} -> ${peer1id}`, JSON.stringify(chunk, null, 2));
controller.enqueue(chunk);
},
})
@@ -47,13 +38,9 @@ export function connectedPeers(
new TransformStream({
transform(
chunk: SyncMessage,
controller: { enqueue: (msg: SyncMessage) => void }
controller: { enqueue: (msg: SyncMessage) => void; }
) {
trace &&
console.debug(
`${peer1id} -> ${peer2id}`,
JSON.stringify(chunk, null, 2)
);
trace && console.debug(`${peer1id} -> ${peer2id}`, JSON.stringify(chunk, null, 2));
controller.enqueue(chunk);
},
})
@@ -78,22 +65,39 @@ export function connectedPeers(
}
export function newStreamPair<T>(): [ReadableStream<T>, WritableStream<T>] {
const queue: T[] = [];
let resolveNextItemReady: () => void = () => { };
let nextItemReady: Promise<void> = new Promise((resolve) => {
resolveNextItemReady = resolve;
});
let writerClosed = false;
let readerClosed = false;
let resolveEnqueue: (enqueue: (item: T) => void) => void;
const enqueuePromise = new Promise<(item: T) => void>((resolve) => {
resolveEnqueue = resolve;
});
let resolveClose: (close: () => void) => void;
const closePromise = new Promise<() => void>((resolve) => {
resolveClose = resolve;
});
const readable = new ReadableStream<T>({
async start(controller) {
resolveEnqueue(controller.enqueue.bind(controller));
resolveClose(controller.close.bind(controller));
async pull(controller) {
let retriesLeft = 3;
while (retriesLeft > 0) {
if (writerClosed) {
controller.close();
return;
}
retriesLeft--;
if (queue.length > 0) {
controller.enqueue(queue.shift()!);
if (queue.length === 0) {
nextItemReady = new Promise((resolve) => {
resolveNextItemReady = resolve;
});
}
return;
} else {
await nextItemReady;
}
}
throw new Error(
"Should only use one retry to get next item in queue."
);
},
cancel(_reason) {
@@ -103,21 +107,22 @@ export function newStreamPair<T>(): [ReadableStream<T>, WritableStream<T>] {
});
const writable = new WritableStream<T>({
async write(chunk) {
const enqueue = await enqueuePromise;
write(chunk) {
if (readerClosed) {
throw new Error("Reader closed");
} else {
// make sure write resolves before corresponding read
setTimeout(() => {
enqueue(chunk);
})
console.log("Reader closed, not writing chunk", chunk);
throw new Error("Reader closed, not writing chunk");
}
queue.push(chunk);
if (queue.length === 1) {
// make sure that await write resolves before corresponding read
setTimeout(() => resolveNextItemReady());
}
},
async abort(reason) {
console.debug("Manually closing writer", reason);
const close = await closePromise;
close();
abort(_reason) {
console.log("Manually closing writer");
writerClosed = true;
resolveNextItemReady();
return Promise.resolve();
},
});

View File

@@ -3,7 +3,7 @@ import { LocalNode } from "./node.js";
import { Peer, PeerID, SyncMessage } from "./sync.js";
import { expectMap } from "./contentType.js";
import { MapOpPayload } from "./contentTypes/coMap.js";
import { Group } from "./group.js";
import { Team } from "./permissions.js";
import {
ReadableStream,
WritableStream,
@@ -23,9 +23,9 @@ test("Node replies with initial tx and header to empty subscribe", async () => {
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "trusting");
@@ -53,7 +53,7 @@ test("Node replies with initial tx and header to empty subscribe", async () => {
const reader = outRx.getReader();
// expect((await reader.read()).value).toMatchObject(admStateEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupStateEx(group));
expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const mapTellKnownStateMsg = await reader.read();
expect(mapTellKnownStateMsg.value).toEqual({
@@ -62,7 +62,7 @@ test("Node replies with initial tx and header to empty subscribe", async () => {
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const newContentMsg = await reader.read();
@@ -71,7 +71,7 @@ test("Node replies with initial tx and header to empty subscribe", async () => {
id: map.coValue.id,
header: {
type: "comap",
ruleset: { type: "ownedByGroup", group: group.id },
ruleset: { type: "ownedByTeam", team: team.id },
meta: null,
createdAt: map.coValue.header.createdAt,
uniqueness: map.coValue.header.uniqueness,
@@ -86,7 +86,7 @@ test("Node replies with initial tx and header to empty subscribe", async () => {
.transactions[0]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "hello",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -104,9 +104,9 @@ test("Node replies with only new tx to subscribe with some known state", async (
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "trusting");
@@ -137,7 +137,7 @@ test("Node replies with only new tx to subscribe with some known state", async (
const reader = outRx.getReader();
// expect((await reader.read()).value).toMatchObject(admStateEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupStateEx(group));
expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const mapTellKnownStateMsg = await reader.read();
expect(mapTellKnownStateMsg.value).toEqual({
@@ -146,7 +146,7 @@ test("Node replies with only new tx to subscribe with some known state", async (
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapNewContentMsg = await reader.read();
@@ -164,7 +164,7 @@ test("Node replies with only new tx to subscribe with some known state", async (
.transactions[1]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "goodbye",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -186,9 +186,9 @@ test("After subscribing, node sends own known state and new txs to peer", async
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
const [inRx, inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -214,7 +214,7 @@ test("After subscribing, node sends own known state and new txs to peer", async
const reader = outRx.getReader();
// expect((await reader.read()).value).toMatchObject(admStateEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupStateEx(group));
expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const mapTellKnownStateMsg = await reader.read();
expect(mapTellKnownStateMsg.value).toEqual({
@@ -223,7 +223,7 @@ test("After subscribing, node sends own known state and new txs to peer", async
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapNewContentHeaderOnlyMsg = await reader.read();
@@ -253,7 +253,7 @@ test("After subscribing, node sends own known state and new txs to peer", async
.transactions[0]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "hello",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -285,7 +285,7 @@ test("After subscribing, node sends own known state and new txs to peer", async
.transactions[1]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "goodbye",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -303,9 +303,9 @@ test("Client replies with known new content to tellKnownState from server", asyn
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "trusting");
@@ -323,7 +323,7 @@ test("Client replies with known new content to tellKnownState from server", asyn
const reader = outRx.getReader();
// expect((await reader.read()).value).toMatchObject(groupStateEx(group));
// expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const writer = inTx.getWriter();
@@ -337,7 +337,7 @@ test("Client replies with known new content to tellKnownState from server", asyn
});
// expect((await reader.read()).value).toMatchObject(admStateEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupStateEx(group));
expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const mapTellKnownStateMsg = await reader.read();
expect(mapTellKnownStateMsg.value).toEqual({
@@ -346,7 +346,7 @@ test("Client replies with known new content to tellKnownState from server", asyn
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapNewContentMsg = await reader.read();
@@ -364,7 +364,7 @@ test("Client replies with known new content to tellKnownState from server", asyn
.transactions[0]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "hello",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -382,9 +382,9 @@ test("No matter the optimistic known state, node respects invalid known state me
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
const [inRx, inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -410,7 +410,7 @@ test("No matter the optimistic known state, node respects invalid known state me
const reader = outRx.getReader();
// expect((await reader.read()).value).toMatchObject(admStateEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupStateEx(group));
expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const mapTellKnownStateMsg = await reader.read();
expect(mapTellKnownStateMsg.value).toEqual({
@@ -419,7 +419,7 @@ test("No matter the optimistic known state, node respects invalid known state me
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapNewContentHeaderOnlyMsg = await reader.read();
@@ -467,7 +467,7 @@ test("No matter the optimistic known state, node respects invalid known state me
.transactions[1]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "goodbye",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -485,9 +485,9 @@ test("If we add a peer, but it never subscribes to a coValue, it won't get any m
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
const [inRx, _inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -514,9 +514,9 @@ test("If we add a server peer, all updates to all coValues are sent to it, even
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
const [inRx, _inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -535,7 +535,7 @@ test("If we add a server peer, all updates to all coValues are sent to it, even
// });
expect((await reader.read()).value).toMatchObject({
action: "load",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
});
const mapSubscribeMsg = await reader.read();
@@ -552,7 +552,7 @@ test("If we add a server peer, all updates to all coValues are sent to it, even
});
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapNewContentMsg = await reader.read();
@@ -570,7 +570,7 @@ test("If we add a server peer, all updates to all coValues are sent to it, even
.transactions[0]!.madeAt,
changes: [
{
op: "set",
op: "insert",
key: "hello",
value: "world",
} satisfies MapOpPayload<string, string>,
@@ -588,7 +588,7 @@ test("If we add a server peer, newly created coValues are auto-subscribed to", a
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const [inRx, _inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -607,10 +607,10 @@ test("If we add a server peer, newly created coValues are auto-subscribed to", a
// });
expect((await reader.read()).value).toMatchObject({
action: "load",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
});
const map = group.createMap();
const map = team.createMap();
const mapSubscribeMsg = await reader.read();
@@ -620,7 +620,7 @@ test("If we add a server peer, newly created coValues are auto-subscribed to", a
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(adminID));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapContentMsg = await reader.read();
@@ -640,9 +640,9 @@ test("When we connect a new server peer, we try to sync all existing coValues to
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
const [inRx, _inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -657,11 +657,11 @@ test("When we connect a new server peer, we try to sync all existing coValues to
const reader = outRx.getReader();
// const _adminSubscribeMessage = await reader.read();
const groupSubscribeMessage = await reader.read();
const teamSubscribeMessage = await reader.read();
expect(groupSubscribeMessage.value).toEqual({
expect(teamSubscribeMessage.value).toEqual({
action: "load",
...group.groupMap.coValue.knownState(),
...team.teamMap.coValue.knownState(),
} satisfies SyncMessage);
const secondMessage = await reader.read();
@@ -676,9 +676,9 @@ test("When receiving a subscribe with a known state that is ahead of our own, pe
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const map = group.createMap();
const map = team.createMap();
const [inRx, inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -704,7 +704,7 @@ test("When receiving a subscribe with a known state that is ahead of our own, pe
const reader = outRx.getReader();
// expect((await reader.read()).value).toMatchObject(admStateEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupStateEx(group));
expect((await reader.read()).value).toMatchObject(teamStateEx(team));
const mapTellKnownState = await reader.read();
expect(mapTellKnownState.value).toEqual({
@@ -719,7 +719,7 @@ test.skip("When replaying creation and transactions of a coValue as new content,
const node1 = new LocalNode(admin, session);
const group = node1.createGroup();
const team = node1.createTeam();
const [inRx1, inTx1] = newStreamPair<SyncMessage>();
const [outRx1, outTx1] = newStreamPair<SyncMessage>();
@@ -754,40 +754,40 @@ test.skip("When replaying creation and transactions of a coValue as new content,
action: "load",
id: admin.id,
});
const groupSubscribeMsg = await from1.read();
expect(groupSubscribeMsg.value).toMatchObject({
const teamSubscribeMsg = await from1.read();
expect(teamSubscribeMsg.value).toMatchObject({
action: "load",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
});
await to2.write(adminSubscribeMessage.value!);
await to2.write(groupSubscribeMsg.value!);
await to2.write(teamSubscribeMsg.value!);
// const adminTellKnownStateMsg = await from2.read();
// expect(adminTellKnownStateMsg.value).toMatchObject(admStateEx(admin.id));
const groupTellKnownStateMsg = await from2.read();
expect(groupTellKnownStateMsg.value).toMatchObject(groupStateEx(group));
const teamTellKnownStateMsg = await from2.read();
expect(teamTellKnownStateMsg.value).toMatchObject(teamStateEx(team));
expect(
node2.sync.peers["test1"]!.optimisticKnownStates[
group.groupMap.coValue.id
team.teamMap.coValue.id
]
).toBeDefined();
// await to1.write(adminTellKnownStateMsg.value!);
await to1.write(groupTellKnownStateMsg.value!);
await to1.write(teamTellKnownStateMsg.value!);
// const adminContentMsg = await from1.read();
// expect(adminContentMsg.value).toMatchObject(admContEx(admin.id));
const groupContentMsg = await from1.read();
expect(groupContentMsg.value).toMatchObject(groupContentEx(group));
const teamContentMsg = await from1.read();
expect(teamContentMsg.value).toMatchObject(teamContentEx(team));
// await to2.write(adminContentMsg.value!);
await to2.write(groupContentMsg.value!);
await to2.write(teamContentMsg.value!);
const map = group.createMap();
const map = team.createMap();
const mapSubscriptionMsg = await from1.read();
expect(mapSubscriptionMsg.value).toMatchObject({
@@ -840,9 +840,9 @@ test.skip("When loading a coValue on one node, the server node it is requested f
const node1 = new LocalNode(admin, session);
const group = node1.createGroup();
const team = node1.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "trusting");
});
@@ -868,9 +868,9 @@ test("Can sync a coValue through a server to another client", async () => {
const client1 = new LocalNode(admin, session);
const group = client1.createGroup();
const team = client1.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "trusting");
});
@@ -910,9 +910,9 @@ test("Can sync a coValue with private transactions through a server to another c
const client1 = new LocalNode(admin, session);
const group = client1.createGroup();
const team = client1.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "private");
});
@@ -952,7 +952,7 @@ test("When a peer's incoming/readable stream closes, we remove the peer", async
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const [inRx, inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -971,10 +971,10 @@ test("When a peer's incoming/readable stream closes, we remove the peer", async
// });
expect((await reader.read()).value).toMatchObject({
action: "load",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
});
const map = group.createMap();
const map = team.createMap();
const mapSubscribeMsg = await reader.read();
@@ -984,7 +984,7 @@ test("When a peer's incoming/readable stream closes, we remove the peer", async
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapContentMsg = await reader.read();
@@ -1006,7 +1006,7 @@ test("When a peer's outgoing/writable stream closes, we remove the peer", async
const [admin, session] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, session);
const group = node.createGroup();
const team = node.createTeam();
const [inRx, inTx] = newStreamPair<SyncMessage>();
const [outRx, outTx] = newStreamPair<SyncMessage>();
@@ -1025,10 +1025,10 @@ test("When a peer's outgoing/writable stream closes, we remove the peer", async
// });
expect((await reader.read()).value).toMatchObject({
action: "load",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
});
const map = group.createMap();
const map = team.createMap();
const mapSubscribeMsg = await reader.read();
@@ -1038,7 +1038,7 @@ test("When a peer's outgoing/writable stream closes, we remove the peer", async
} satisfies SyncMessage);
// expect((await reader.read()).value).toMatchObject(admContEx(admin.id));
expect((await reader.read()).value).toMatchObject(groupContentEx(group));
expect((await reader.read()).value).toMatchObject(teamContentEx(team));
const mapContentMsg = await reader.read();
@@ -1066,9 +1066,9 @@ test("If we start loading a coValue before connecting to a peer that has it, it
const node1 = new LocalNode(admin, session);
const group = node1.createGroup();
const team = node1.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((editable) => {
editable.set("hello", "world", "trusting");
});
@@ -1096,10 +1096,10 @@ test("If we start loading a coValue before connecting to a peer that has it, it
);
});
function groupContentEx(group: Group) {
function teamContentEx(team: Team) {
return {
action: "content",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
};
}
@@ -1110,10 +1110,10 @@ function admContEx(adminID: AccountID) {
};
}
function groupStateEx(group: Group) {
function teamStateEx(team: Team) {
return {
action: "known",
id: group.groupMap.coValue.id,
id: team.teamMap.coValue.id,
};
}

View File

@@ -149,11 +149,16 @@ export class SyncManager {
}
}
async subscribeToIncludingDependencies(id: RawCoID, peer: PeerState) {
async subscribeToIncludingDependencies(
id: RawCoID,
peer: PeerState
) {
const entry = this.local.coValues[id];
if (!entry) {
throw new Error("Expected coValue entry on subscribe");
throw new Error(
"Expected coValue entry on subscribe"
);
}
if (entry.state === "loading") {
@@ -207,7 +212,10 @@ export class SyncManager {
}
}
async sendNewContentIncludingDependencies(id: RawCoID, peer: PeerState) {
async sendNewContentIncludingDependencies(
id: RawCoID,
peer: PeerState
) {
const coValue = this.local.expectCoValueLoaded(id);
for (const id of coValue.getDependedOnCoValues()) {
@@ -221,7 +229,8 @@ export class SyncManager {
if (newContent) {
await this.trySendToPeer(peer, newContent);
peer.optimisticKnownStates[id] = combinedKnownStates(
peer.optimisticKnownStates[id] || emptyKnownState(id),
peer.optimisticKnownStates[id] ||
emptyKnownState(id),
coValue.knownState()
);
}
@@ -256,23 +265,17 @@ export class SyncManager {
}
const readIncoming = async () => {
try {
for await (const msg of peerState.incoming) {
try {
await this.handleSyncMessage(msg, peerState);
} catch (e) {
console.error(
`Error reading from peer ${peer.id}, handling msg`,
JSON.stringify(msg),
e
);
}
for await (const msg of peerState.incoming) {
try {
await this.handleSyncMessage(msg, peerState);
} catch (e) {
console.error(
`Error reading from peer ${peer.id}`,
JSON.stringify(msg),
e
);
}
console.log("DONE!!!");
} catch (e) {
console.error(`Error reading from peer ${peer.id}`, e);
}
console.log("Peer disconnected:", peer.id);
delete this.peers[peer.id];
};
@@ -281,32 +284,9 @@ export class SyncManager {
}
trySendToPeer(peer: PeerState, msg: SyncMessage) {
return new Promise<void>((resolve) => {
const timeout = setTimeout(() => {
console.error(
new Error(
`Writing to peer ${peer.id} took >1s - this should never happen as write should resolve quickly or error`
)
);
resolve();
}, 1000);
peer.outgoing
.write(msg)
.then(() => {
clearTimeout(timeout);
resolve();
})
.catch((e) => {
console.error(
new Error(
`Error writing to peer ${peer.id}, disconnecting`,
{
cause: e,
}
)
);
delete this.peers[peer.id];
});
return peer.outgoing.write(msg).catch((e) => {
console.error(new Error(`Error writing to peer ${peer.id}, disconnecting`, {cause: e}));
delete this.peers[peer.id];
});
}
@@ -315,19 +295,7 @@ export class SyncManager {
if (!entry || entry.state === "loading") {
if (!entry) {
await new Promise<void>((resolve) => {
this.local
.loadCoValue(msg.id)
.then(() => resolve())
.catch((e) => {
console.error(
"Error loading coValue in handleLoad",
e
);
resolve();
});
setTimeout(resolve, 1000);
});
this.local.coValues[msg.id] = newLoadingState();
}
peer.optimisticKnownStates[msg.id] = knownStateIn(msg);
@@ -345,7 +313,10 @@ export class SyncManager {
peer.optimisticKnownStates[msg.id] = knownStateIn(msg);
await this.tellUntoldKnownStateIncludingDependencies(msg.id, peer);
await this.tellUntoldKnownStateIncludingDependencies(
msg.id,
peer
);
await this.sendNewContentIncludingDependencies(msg.id, peer);
}
@@ -354,7 +325,8 @@ export class SyncManager {
let entry = this.local.coValues[msg.id];
peer.optimisticKnownStates[msg.id] = combinedKnownStates(
peer.optimisticKnownStates[msg.id] || emptyKnownState(msg.id),
peer.optimisticKnownStates[msg.id] ||
emptyKnownState(msg.id),
knownStateIn(msg)
);
@@ -380,7 +352,10 @@ export class SyncManager {
return [];
}
await this.tellUntoldKnownStateIncludingDependencies(msg.id, peer);
await this.tellUntoldKnownStateIncludingDependencies(
msg.id,
peer
);
await this.sendNewContentIncludingDependencies(msg.id, peer);
}
@@ -395,7 +370,8 @@ export class SyncManager {
let resolveAfterDone: ((coValue: CoValue) => void) | undefined;
const peerOptimisticKnownState = peer.optimisticKnownStates[msg.id];
const peerOptimisticKnownState =
peer.optimisticKnownStates[msg.id];
if (!peerOptimisticKnownState) {
throw new Error(
@@ -477,7 +453,10 @@ export class SyncManager {
}
}
async handleCorrection(msg: KnownStateMessage, peer: PeerState) {
async handleCorrection(
msg: KnownStateMessage,
peer: PeerState
) {
const coValue = this.local.expectCoValueLoaded(msg.id);
peer.optimisticKnownStates[msg.id] = combinedKnownStates(
@@ -520,7 +499,11 @@ export class SyncManager {
}
}
function knownStateIn(msg: LoadMessage | KnownStateMessage) {
function knownStateIn(
msg:
| LoadMessage
| KnownStateMessage
) {
return {
id: msg.id,
header: msg.header,

View File

@@ -1,7 +1,7 @@
import { AgentSecret, createdNowUnique, getAgentID, newRandomAgentSecret } from "./crypto.js";
import { newRandomSessionID } from "./coValue.js";
import { LocalNode } from "./node.js";
import { expectGroupContent } from "./group.js";
import { expectTeamContent } from "./permissions.js";
import { AnonymousControlledAccount } from "./account.js";
import { SessionID } from "./ids.js";
@@ -13,69 +13,69 @@ export function randomAnonymousAccountAndSessionID(): [AnonymousControlledAccoun
return [new AnonymousControlledAccount(agentSecret), sessionID];
}
export function newGroup() {
export function newTeam() {
const [admin, sessionID] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, sessionID);
const group = node.createCoValue({
const team = node.createCoValue({
type: "comap",
ruleset: { type: "group", initialAdmin: admin.id },
ruleset: { type: "team", initialAdmin: admin.id },
meta: null,
...createdNowUnique(),
});
const groupContent = expectGroupContent(group.getCurrentContent());
const teamContent = expectTeamContent(team.getCurrentContent());
groupContent.edit((editable) => {
teamContent.edit((editable) => {
editable.set(admin.id, "admin", "trusting");
expect(editable.get(admin.id)).toEqual("admin");
});
return { node, group, admin };
return { node, team, admin };
}
export function groupWithTwoAdmins() {
const { group, admin, node } = newGroup();
export function teamWithTwoAdmins() {
const { team, admin, node } = newTeam();
const otherAdmin = node.createAccount("otherAdmin");
let content = expectGroupContent(group.getCurrentContent());
let content = expectTeamContent(team.getCurrentContent());
content.edit((editable) => {
editable.set(otherAdmin.id, "admin", "trusting");
expect(editable.get(otherAdmin.id)).toEqual("admin");
});
content = expectGroupContent(group.getCurrentContent());
content = expectTeamContent(team.getCurrentContent());
if (content.type !== "comap") {
throw new Error("Expected map");
}
expect(content.get(otherAdmin.id)).toEqual("admin");
return { group, admin, otherAdmin, node };
return { team, admin, otherAdmin, node };
}
export function newGroupHighLevel() {
export function newTeamHighLevel() {
const [admin, sessionID] = randomAnonymousAccountAndSessionID();
const node = new LocalNode(admin, sessionID);
const group = node.createGroup();
const team = node.createTeam();
return { admin, node, group };
return { admin, node, team };
}
export function groupWithTwoAdminsHighLevel() {
const { admin, node, group } = newGroupHighLevel();
export function teamWithTwoAdminsHighLevel() {
const { admin, node, team } = newTeamHighLevel();
const otherAdmin = node.createAccount("otherAdmin");
group.addMember(otherAdmin.id, "admin");
team.addMember(otherAdmin.id, "admin");
return { admin, node, group, otherAdmin };
return { admin, node, team, otherAdmin };
}
export function shouldNotResolve<T>(

View File

@@ -12,5 +12,4 @@
"esModuleInterop": true,
},
"include": ["./src/**/*"],
"exclude": ["./src/**/*.test.*"],
}

View File

@@ -1,21 +0,0 @@
module.exports = {
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:require-extensions/recommended",
],
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint", "require-extensions"],
parserOptions: {
project: "./tsconfig.json",
},
root: true,
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
],
"@typescript-eslint/no-floating-promises": "error",
},
};

View File

@@ -1,171 +0,0 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
.DS_Store

View File

@@ -1,2 +0,0 @@
coverage
node_modules

View File

@@ -1,16 +0,0 @@
{
"name": "jazz-browser-auth-local",
"version": "0.1.6",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"jazz-browser": "^0.1.6",
"typescript": "^5.1.6"
},
"scripts": {
"lint": "eslint src/**/*.ts",
"build": "npm run lint && rm -rf ./dist && tsc --declaration --sourceMap --outDir dist",
"prepublishOnly": "npm run build"
}
}

View File

@@ -1,212 +0,0 @@
import {
AccountID,
AgentSecret,
cojsonInternals,
LocalNode,
Peer,
} from "cojson";
import { agentSecretFromSecretSeed } from "cojson/src/crypto";
import { AuthProvider, SessionProvider } from "jazz-browser";
type SessionStorageData = {
accountID: AccountID;
accountSecret: AgentSecret;
};
const sessionStorageKey = "jazz-logged-in-secret";
export interface BrowserLocalAuthDriver {
onReady: (next: {
signUp: (username: string) => Promise<void>;
logIn: () => Promise<void>;
}) => void;
onSignedIn: (next: { logOut: () => void }) => void;
}
export class BrowserLocalAuth implements AuthProvider {
driver: BrowserLocalAuthDriver;
appName: string;
appHostname: string;
constructor(
driver: BrowserLocalAuthDriver,
appName: string,
// TODO: is this a safe default?
appHostname: string = window.location.hostname
) {
this.driver = driver;
this.appName = appName;
this.appHostname = appHostname;
}
async createNode(
getSessionFor: SessionProvider,
initialPeers: Peer[]
): Promise<LocalNode> {
if (sessionStorage[sessionStorageKey]) {
const sessionStorageData = JSON.parse(
sessionStorage[sessionStorageKey]
) as SessionStorageData;
const sessionID = await getSessionFor(sessionStorageData.accountID);
const node = await LocalNode.withLoadedAccount(
sessionStorageData.accountID,
sessionStorageData.accountSecret,
sessionID,
initialPeers
);
this.driver.onSignedIn({ logOut });
return Promise.resolve(node);
} else {
const node = await new Promise<LocalNode>(
(doneSigningUpOrLoggingIn) => {
this.driver.onReady({
signUp: async (username) => {
const node = await signUp(
username,
getSessionFor,
this.appName,
this.appHostname
);
for (const peer of initialPeers) {
node.sync.addPeer(peer);
}
doneSigningUpOrLoggingIn(node);
this.driver.onSignedIn({ logOut });
},
logIn: async () => {
const node = await logIn(
getSessionFor,
this.appHostname,
initialPeers
);
doneSigningUpOrLoggingIn(node);
this.driver.onSignedIn({ logOut });
},
});
}
);
return node;
}
}
}
async function signUp(
username: string,
getSessionFor: SessionProvider,
appName: string,
appHostname: string
): Promise<LocalNode> {
const secretSeed = cojsonInternals.newRandomSecretSeed();
const { node, accountID, accountSecret } =
LocalNode.withNewlyCreatedAccount(
username,
agentSecretFromSecretSeed(secretSeed)
);
const webAuthNCredentialPayload = new Uint8Array(
cojsonInternals.secretSeedLength + cojsonInternals.shortHashLength
);
webAuthNCredentialPayload.set(secretSeed);
webAuthNCredentialPayload.set(
cojsonInternals.rawCoIDtoBytes(accountID),
cojsonInternals.secretSeedLength
);
const webAuthNCredential = await navigator.credentials.create({
publicKey: {
challenge: Uint8Array.from([0, 1, 2]),
rp: {
name: appName,
id: appHostname,
},
user: {
id: webAuthNCredentialPayload,
name: username + `(${new Date().toLocaleString()})`,
displayName: username,
},
pubKeyCredParams: [{ alg: -7, type: "public-key" }],
authenticatorSelection: {
authenticatorAttachment: "platform",
},
timeout: 60000,
attestation: "direct",
},
});
console.log(webAuthNCredential, accountID);
sessionStorage[sessionStorageKey] = JSON.stringify({
accountID,
accountSecret,
} satisfies SessionStorageData);
node.ownSessionID = await getSessionFor(accountID);
return node;
}
async function logIn(
getSessionFor: SessionProvider,
appHostname: string,
initialPeers: Peer[]
): Promise<LocalNode> {
const webAuthNCredential = (await navigator.credentials.get({
publicKey: {
challenge: Uint8Array.from([0, 1, 2]),
rpId: appHostname,
allowCredentials: [],
timeout: 60000,
},
})) as unknown as {
response: { userHandle: ArrayBuffer };
};
if (!webAuthNCredential) {
throw new Error("Couldn't log in");
}
const webAuthNCredentialPayload = new Uint8Array(
webAuthNCredential.response.userHandle
);
const accountSecretSeed = webAuthNCredentialPayload.slice(
0,
cojsonInternals.secretSeedLength
);
const accountID = cojsonInternals.rawCoIDfromBytes(
webAuthNCredentialPayload.slice(
cojsonInternals.secretSeedLength,
cojsonInternals.secretSeedLength + cojsonInternals.shortHashLength
)
) as AccountID;
const accountSecret = agentSecretFromSecretSeed(accountSecretSeed);
if (!accountSecret) {
throw new Error("Invalid credential");
}
sessionStorage[sessionStorageKey] = JSON.stringify({
accountID,
accountSecret,
} satisfies SessionStorageData);
const node = await LocalNode.withLoadedAccount(
accountID,
accountSecret,
await getSessionFor(accountID),
initialPeers
);
return node;
}
function logOut() {
delete sessionStorage[sessionStorageKey];
}

View File

@@ -1,16 +0,0 @@
{
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"module": "esnext",
"target": "ES2020",
"moduleResolution": "bundler",
"moduleDetection": "force",
"strict": true,
"jsx": "react",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
},
"include": ["./src/**/*"],
}

View File

@@ -1,83 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@noble/ciphers@^0.1.3":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.1.4.tgz#96327dca147829ed9eee0d96cfdf7c57915765f0"
integrity sha512-d3ZR8vGSpy3v/nllS+bD/OMN5UZqusWiQqkyj7AwzTnhXFH72pF5oB4Ach6DQ50g5kXxC28LdaYBEpsyv9KOUQ==
"@noble/curves@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"
"@noble/hashes@1.3.1", "@noble/hashes@^1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==
"@scure/base@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==
"@types/prop-types@*":
version "15.7.5"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==
"@types/react@^18.2.19":
version "18.2.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.19.tgz#f77cb2c8307368e624d464a25b9675fa35f95a8b"
integrity sha512-e2S8wmY1ePfM517PqCG80CcE48Xs5k0pwJzuDZsfE8IZRRBfOMCF+XqnFxu6mWtyivum1MQm4aco+WIt6Coimw==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"
"@types/scheduler@*":
version "0.16.3"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.3.tgz#cef09e3ec9af1d63d2a6cc5b383a737e24e6dcf5"
integrity sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==
cojson@^0.0.14:
version "0.0.14"
resolved "https://registry.yarnpkg.com/cojson/-/cojson-0.0.14.tgz#e7b190ade1efc20d6f0fa12411d7208cdc0f19f7"
integrity sha512-TFenIGswEEhnZlCmq+B1NZPztjovZ72AjK1YkkZca54ZFbB1lAHdPt2hqqu/QBO24C9+6DtuoS2ixm6gbSBWCg==
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 "^2.1.0"
isomorphic-streams "https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae"
csstype@^3.0.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b"
integrity sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==
fast-json-stable-stringify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
"isomorphic-streams@git+https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae":
version "1.0.3"
resolved "git+https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae"
jazz-react@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/jazz-react/-/jazz-react-0.0.6.tgz#53b0245720b10ec31ac8deba45fd8a052b313b06"
integrity sha512-JlYTKUVPpuK3T7cLfk2YwHh3yH+2BPVSuWIQui35U52/gce+HmTMGolqFYGghWMVQtwclaZ0IoEbtuycPiADOQ==
dependencies:
cojson "^0.0.14"
typescript "^5.1.6"
typescript@^5.1.6:
version "5.1.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274"
integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==

View File

@@ -1,17 +0,0 @@
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
parserOptions: {
project: "./tsconfig.json",
},
root: true,
rules: {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }],
"@typescript-eslint/no-floating-promises": "error",
},
};

View File

@@ -1,171 +0,0 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
.DS_Store

View File

@@ -1,2 +0,0 @@
coverage
node_modules

View File

@@ -1,17 +0,0 @@
{
"name": "jazz-browser",
"version": "0.1.6",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"cojson": "^0.1.6",
"jazz-storage-indexeddb": "^0.1.6",
"typescript": "^5.1.6"
},
"scripts": {
"lint": "eslint src/**/*.ts",
"build": "npm run lint && rm -rf ./dist && tsc --declaration --sourceMap --outDir dist",
"prepublishOnly": "npm run build"
}
}

View File

@@ -1,350 +0,0 @@
import { InviteSecret } from "cojson";
import {
LocalNode,
cojsonInternals,
CojsonInternalTypes,
SessionID,
SyncMessage,
Peer,
ContentType,
Group,
CoID,
} from "cojson";
import { ReadableStream, WritableStream } from "isomorphic-streams";
import { IDBStorage } from "jazz-storage-indexeddb";
export type BrowserNodeHandle = {
node: LocalNode;
// TODO: Symbol.dispose?
done: () => void;
};
export async function createBrowserNode({
auth,
syncAddress = "wss://sync.jazz.tools",
reconnectionTimeout = 300,
}: {
auth: AuthProvider;
syncAddress?: string;
reconnectionTimeout?: number;
}): Promise<BrowserNodeHandle> {
let sessionDone: () => void;
const firstWsPeer = createWebSocketPeer(syncAddress);
let shouldTryToReconnect = true;
const node = await auth.createNode(
(accountID) => {
const sessionHandle = getSessionHandleFor(accountID);
sessionDone = sessionHandle.done;
return sessionHandle.session;
},
[await IDBStorage.asPeer({ trace: true }), firstWsPeer]
);
async function websocketReconnectLoop() {
while (shouldTryToReconnect) {
if (
Object.keys(node.sync.peers).some((peerId) =>
peerId.includes(syncAddress)
)
) {
await new Promise((resolve) =>
setTimeout(resolve, reconnectionTimeout)
);
} else {
console.log("Websocket disconnected, trying to reconnect");
node.sync.addPeer(createWebSocketPeer(syncAddress));
await new Promise((resolve) =>
setTimeout(resolve, reconnectionTimeout)
);
}
}
}
void websocketReconnectLoop();
return {
node,
done: () => {
shouldTryToReconnect = false;
sessionDone?.();
},
};
}
export interface AuthProvider {
createNode(
getSessionFor: SessionProvider,
initialPeers: Peer[]
): Promise<LocalNode>;
}
export type SessionProvider = (
accountID: CojsonInternalTypes.AccountIDOrAgentID
) => Promise<SessionID>;
export type SessionHandle = {
session: Promise<SessionID>;
done: () => void;
};
function getSessionHandleFor(
accountID: CojsonInternalTypes.AccountIDOrAgentID
): SessionHandle {
let done!: () => void;
const donePromise = new Promise<void>((resolve) => {
done = resolve;
});
let resolveSession: (sessionID: SessionID) => void;
const sessionPromise = new Promise<SessionID>((resolve) => {
resolveSession = resolve;
});
void (async function () {
for (let idx = 0; idx < 100; idx++) {
// To work better around StrictMode
for (let retry = 0; retry < 2; retry++) {
console.debug("Trying to get lock", accountID + "_" + idx);
const sessionFinishedOrNoLock = await navigator.locks.request(
accountID + "_" + idx,
{ ifAvailable: true },
async (lock) => {
if (!lock) return "noLock";
const sessionID =
localStorage[accountID + "_" + idx] ||
cojsonInternals.newRandomSessionID(accountID);
localStorage[accountID + "_" + idx] = sessionID;
console.debug(
"Got lock",
accountID + "_" + idx,
sessionID
);
resolveSession(sessionID);
await donePromise;
console.log(
"Done with lock",
accountID + "_" + idx,
sessionID
);
return "sessionFinished";
}
);
if (sessionFinishedOrNoLock === "sessionFinished") {
return;
}
}
}
throw new Error("Couldn't get lock on session after 100x2 tries");
})();
return {
session: sessionPromise,
done,
};
}
function websocketReadableStream<T>(ws: WebSocket) {
ws.binaryType = "arraybuffer";
return new ReadableStream<T>({
start(controller) {
let pingTimeout: ReturnType<typeof setTimeout> | undefined;
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "ping") {
console.debug(
"Got ping from",
msg.dc,
"latency",
Date.now() - msg.time,
"ms"
);
if (pingTimeout) {
clearTimeout(pingTimeout);
}
pingTimeout = setTimeout(() => {
console.debug("Ping timeout");
controller.close();
ws.close();
}, 2500);
return;
}
controller.enqueue(msg);
};
const closeListener = () => controller.close();
ws.addEventListener("close", closeListener);
ws.addEventListener("error", () => {
controller.error(new Error("The WebSocket errored!"));
ws.removeEventListener("close", closeListener);
});
},
cancel() {
ws.close();
},
});
}
function createWebSocketPeer(syncAddress: string): Peer {
const ws = new WebSocket(syncAddress);
const incoming = websocketReadableStream<SyncMessage>(ws);
const outgoing = websocketWritableStream<SyncMessage>(ws);
return {
id: syncAddress + "@" + new Date().toISOString(),
incoming,
outgoing,
role: "server",
};
}
function websocketWritableStream<T>(ws: WebSocket) {
const initialQueue = [] as T[];
let isOpen = false;
return new WritableStream<T>({
start(controller) {
ws.addEventListener("error", (event) => {
controller.error(
new Error("The WebSocket errored!" + JSON.stringify(event))
);
});
ws.addEventListener("close", () => {
controller.error(
new Error("The server closed the connection unexpectedly!")
);
});
ws.addEventListener("open", () => {
for (const item of initialQueue) {
ws.send(JSON.stringify(item));
}
isOpen = true;
});
},
async write(chunk) {
if (isOpen) {
ws.send(JSON.stringify(chunk));
// Return immediately, since the web socket gives us no easy way to tell
// when the write completes.
} else {
initialQueue.push(chunk);
}
},
close() {
return closeWS(1000);
},
abort(reason) {
return closeWS(4000, reason && reason.message);
},
});
function closeWS(code: number, reasonString?: string) {
return new Promise<void>((resolve, reject) => {
ws.addEventListener(
"close",
(e) => {
if (e.wasClean) {
resolve();
} else {
reject(
new Error("The connection was not closed cleanly")
);
}
},
{ once: true }
);
ws.close(code, reasonString);
});
}
}
export function createInviteLink(
value: ContentType,
role: "reader" | "writer" | "admin",
// default to same address as window.location, but without hash
{
baseURL = window.location.href.replace(/#.*$/, ""),
}: { baseURL?: string } = {}
): string {
const coValue = value.coValue;
const node = coValue.node;
let currentCoValue = coValue;
while (currentCoValue.header.ruleset.type === "ownedByGroup") {
currentCoValue = node.expectCoValueLoaded(
currentCoValue.header.ruleset.group
);
}
if (currentCoValue.header.ruleset.type !== "group") {
throw new Error("Can't create invite link for object without group");
}
const group = new Group(
cojsonInternals.expectGroupContent(currentCoValue.getCurrentContent()),
node
);
const inviteSecret = group.createInvite(role);
return `${baseURL}#invitedTo=${value.id}&${inviteSecret}`;
}
export function parseInviteLink(inviteURL: string):
| {
valueID: CoID<ContentType>;
inviteSecret: InviteSecret;
}
| undefined {
const url = new URL(inviteURL);
const valueID = url.hash
.split("&")[0]
?.replace(/^#invitedTo=/, "") as CoID<ContentType>;
const inviteSecret = url.hash.split("&")[1] as InviteSecret;
if (!valueID || !inviteSecret) {
return undefined;
}
return { valueID, inviteSecret };
}
export function consumeInviteLinkFromWindowLocation(node: LocalNode): Promise<
| {
valueID: string;
inviteSecret: string;
}
| undefined
> {
return new Promise((resolve, reject) => {
const result = parseInviteLink(window.location.href);
if (result) {
node.acceptInvite(result.valueID, result.inviteSecret)
.then(() => {
resolve(result);
window.history.replaceState(
{},
"",
window.location.href.replace(/#.*$/, "")
);
})
.catch(reject);
} else {
resolve(undefined);
}
});
}

View File

@@ -1,16 +0,0 @@
{
"compilerOptions": {
"lib": ["ESNext", "DOM"],
"module": "esnext",
"target": "ES2020",
"moduleResolution": "bundler",
"moduleDetection": "force",
"strict": true,
"skipLibCheck": true,
"jsx": "react",
"forceConsistentCasingInFileNames": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
},
"include": ["./src/**/*"],
}

View File

@@ -1,75 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@noble/ciphers@^0.1.3":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-0.1.4.tgz#96327dca147829ed9eee0d96cfdf7c57915765f0"
integrity sha512-d3ZR8vGSpy3v/nllS+bD/OMN5UZqusWiQqkyj7AwzTnhXFH72pF5oB4Ach6DQ50g5kXxC28LdaYBEpsyv9KOUQ==
"@noble/curves@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@noble/curves/-/curves-1.1.0.tgz#f13fc667c89184bc04cccb9b11e8e7bae27d8c3d"
integrity sha512-091oBExgENk/kGj3AZmtBDMpxQPDtxQABR2B9lb1JbVTs6ytdzZNwvhxQ4MWasRNEzlbEH8jCWFCwhF/Obj5AA==
dependencies:
"@noble/hashes" "1.3.1"
"@noble/hashes@1.3.1", "@noble/hashes@^1.3.1":
version "1.3.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9"
integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA==
"@scure/base@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@scure/base/-/base-1.1.1.tgz#ebb651ee52ff84f420097055f4bf46cfba403938"
integrity sha512-ZxOhsSyxYwLJj3pLZCefNitxsj093tb2vq90mp2txoYeBqbcjDjqFhyM8eUjq/uFm6zJ+mUuqxlS2FkuSY1MTA==
"@types/prop-types@*":
version "15.7.5"
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==
"@types/react@^18.2.19":
version "18.2.19"
resolved "https://registry.yarnpkg.com/@types/react/-/react-18.2.19.tgz#f77cb2c8307368e624d464a25b9675fa35f95a8b"
integrity sha512-e2S8wmY1ePfM517PqCG80CcE48Xs5k0pwJzuDZsfE8IZRRBfOMCF+XqnFxu6mWtyivum1MQm4aco+WIt6Coimw==
dependencies:
"@types/prop-types" "*"
"@types/scheduler" "*"
csstype "^3.0.2"
"@types/scheduler@*":
version "0.16.3"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.3.tgz#cef09e3ec9af1d63d2a6cc5b383a737e24e6dcf5"
integrity sha512-5cJ8CB4yAx7BH1oMvdU0Jh9lrEXyPkar6F9G/ERswkCuvP4KQZfZkSjcMbAICCpQTN4OuZn8tz0HiKv9TGZgrQ==
cojson@^0.0.14:
version "0.0.14"
resolved "https://registry.yarnpkg.com/cojson/-/cojson-0.0.14.tgz#e7b190ade1efc20d6f0fa12411d7208cdc0f19f7"
integrity sha512-TFenIGswEEhnZlCmq+B1NZPztjovZ72AjK1YkkZca54ZFbB1lAHdPt2hqqu/QBO24C9+6DtuoS2ixm6gbSBWCg==
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 "^2.1.0"
isomorphic-streams "https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae"
csstype@^3.0.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b"
integrity sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==
fast-json-stable-stringify@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
"isomorphic-streams@git+https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae":
version "1.0.3"
resolved "git+https://github.com/sgwilym/isomorphic-streams.git#aa9394781bfc92f8d7c981be7daf8af4b4cd4fae"
typescript@^5.1.6:
version "5.1.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274"
integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==

View File

@@ -1,12 +1,11 @@
{
"name": "jazz-react-auth-local",
"version": "0.1.7",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"version": "0.0.8",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "MIT",
"dependencies": {
"jazz-browser-auth-local": "^0.1.6",
"jazz-react": "^0.1.7",
"jazz-react": "^0.0.11",
"typescript": "^5.1.6"
},
"devDependencies": {
@@ -16,7 +15,7 @@
"react": "^17.0.2"
},
"scripts": {
"lint": "eslint src/**/*.tsx",
"lint": "eslint src/**/*.ts",
"build": "npm run lint && rm -rf ./dist && tsc --declaration --sourceMap --outDir dist",
"prepublishOnly": "npm run build"
}

View File

@@ -0,0 +1,89 @@
import { cojsonInternals, AgentSecret } from "cojson";
import { useCallback, useEffect, useState } from "react";
export function useLocalAuth(onCredential: (credentials: AgentSecret) => void) {
const [displayName, setDisplayName] = useState<string>("");
useEffect(() => {
if (sessionStorage.credential) {
const credential = JSON.parse(sessionStorage.credential);
onCredential(credential);
}
}, [onCredential]);
const signUp = useCallback(() => {
void (async function () {
const credential = cojsonInternals.newRandomAgentSecret();
console.log(credential);
const webAuthNCredential = await navigator.credentials.create({
publicKey: {
challenge: Uint8Array.from([0, 1, 2]),
rp: {
name: "TodoApp",
// TODO: something safer as default?
id: window.location.hostname,
},
user: {
id: cojsonInternals.agentSecretToBytes(credential),
name: displayName,
displayName: displayName,
},
pubKeyCredParams: [{ alg: -7, type: "public-key" }],
authenticatorSelection: {
authenticatorAttachment: "platform",
},
timeout: 60000,
attestation: "direct",
},
});
console.log(
webAuthNCredential,
credential,
cojsonInternals.agentSecretToBytes(credential)
);
sessionStorage.credential = JSON.stringify(credential);
onCredential(credential);
})();
}, [displayName]);
const signIn = useCallback(() => {
void (async function () {
const webAuthNCredential = await navigator.credentials.get({
publicKey: {
challenge: Uint8Array.from([0, 1, 2]),
// TODO: something safer as default?
rpId: window.location.hostname,
allowCredentials: [],
timeout: 60000,
},
});
if (!webAuthNCredential) {
throw new Error("Couldn't log in");
}
const userIdBytes = new Uint8Array(
(
webAuthNCredential as unknown as {
response: { userHandle: ArrayBuffer };
}
).response.userHandle
);
const credential =
cojsonInternals.agentSecretFromBytes(userIdBytes);
if (!credential) {
throw new Error("Invalid credential");
}
sessionStorage.credential = JSON.stringify(credential);
onCredential(credential);
})();
}, []);
return { displayName, setDisplayName, signUp, signIn };
}

View File

@@ -1,149 +0,0 @@
import React from "react";
import { useMemo, useState, ReactNode } from "react";
import { BrowserLocalAuth } from "jazz-browser-auth-local";
import { ReactAuthHook } from "jazz-react";
export type LocalAuthComponent = (props: {
loading: boolean;
logIn: () => void;
signUp: (username: string) => void;
}) => ReactNode;
export function LocalAuth({
appName,
appHostname,
Component = LocalAuthBasicUI,
}: {
appName: string;
appHostname?: string;
Component?: LocalAuthComponent;
}): ReactAuthHook {
return function useLocalAuth() {
const [authState, setAuthState] = useState<
| { state: "loading" }
| {
state: "ready";
logIn: () => void;
signUp: (username: string) => void;
}
| { state: "signedIn"; logOut: () => void }
>({ state: "loading" });
const [logOutCounter, setLogOutCounter] = useState(0);
const auth = useMemo(() => {
return new BrowserLocalAuth(
{
onReady(next) {
setAuthState({
state: "ready",
logIn: next.logIn,
signUp: next.signUp,
});
},
onSignedIn(next) {
setAuthState({
state: "signedIn",
logOut: () => {
next.logOut();
setAuthState({ state: "loading" });
setLogOutCounter((c) => c + 1);
},
});
},
},
appName,
appHostname
);
}, [appName, appHostname, logOutCounter]);
const AuthUI =
authState.state === "ready"
? Component({
loading: false,
logIn: authState.logIn,
signUp: authState.signUp,
})
: Component({
loading: false,
logIn: () => {},
signUp: (_) => {},
});
return {
auth,
AuthUI,
logOut:
authState.state === "signedIn" ? authState.logOut : undefined,
};
};
}
export const LocalAuthBasicUI = ({
logIn,
signUp,
}: {
logIn: () => void;
signUp: (username: string) => void;
}) => {
const [username, setUsername] = useState<string>("");
return (
<div
style={{
width: "100%",
height: "100%",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<div
style={{
width: "18rem",
display: "flex",
flexDirection: "column",
gap: "2rem",
}}
>
<form
style={{
width: "18rem",
display: "flex",
flexDirection: "column",
gap: "0.5rem",
}}
onSubmit={(e) => {
e.preventDefault();
signUp(username);
}}
>
<input
placeholder="Display name"
value={username}
onChange={(e) => setUsername(e.target.value)}
autoComplete="webauthn"
style={{
border: "1px solid #333",
padding: "10px 5px",
}}
/>
<input
type="submit"
value="Sign Up as new account"
style={{
background: "#aaa",
padding: "10px 5px",
}}
/>
</form>
<button
onClick={logIn}
style={{ background: "#aaa", padding: "10px 5px" }}
>
Log In with existing account
</button>
</div>
</div>
);
};

View File

@@ -6,7 +6,6 @@
"moduleResolution": "bundler",
"moduleDetection": "force",
"strict": true,
"jsx": "react",
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noUncheckedIndexedAccess": true,

View File

@@ -1,12 +1,12 @@
{
"name": "jazz-react",
"version": "0.1.7",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"version": "0.0.11",
"main": "src/index.tsx",
"types": "src/index.tsx",
"license": "MIT",
"dependencies": {
"cojson": "^0.1.6",
"jazz-browser": "^0.1.6",
"cojson": "^0.0.18",
"jazz-storage-indexeddb": "^0.0.5",
"typescript": "^5.1.6"
},
"devDependencies": {

View File

@@ -1,82 +1,182 @@
import {
LocalNode,
cojsonInternals,
SessionID,
ContentType,
SyncMessage,
AgentSecret,
CoID,
ProfileContent,
ProfileMeta,
CoMap,
AccountID,
JsonValue,
AnonymousControlledAccount,
AgentID
} from "cojson";
import React, { useEffect, useState } from "react";
import { AuthProvider, createBrowserNode } from "jazz-browser";
export {
createInviteLink,
parseInviteLink,
consumeInviteLinkFromWindowLocation,
} from "jazz-browser";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { ReadableStream, WritableStream } from "isomorphic-streams";
import { IDBStorage } from "jazz-storage-indexeddb";
type JazzContext = {
localNode: LocalNode;
logOut: () => void;
};
const JazzContext = React.createContext<JazzContext | undefined>(undefined);
export type ReactAuthHook = () => {
auth: AuthProvider;
AuthUI: React.ReactNode;
logOut?: () => void;
};
export type AuthComponent = (props: {
onCredential: (credentials: AgentSecret) => void;
}) => React.ReactElement;
export function WithJazz({
children,
auth: authHook,
syncAddress,
auth: Auth,
syncAddress = "wss://sync.jazz.tools",
}: {
children: React.ReactNode;
auth: ReactAuthHook;
auth: AuthComponent;
syncAddress?: string;
}) {
const [node, setNode] = useState<LocalNode | undefined>();
const sessionDone = useRef<() => void>();
const { auth, AuthUI, logOut } = authHook();
const onCredential = useCallback((credential: AgentSecret) => {
const agentID = cojsonInternals.getAgentID(credential);
const sessionHandle = getSessionFor(agentID);
void sessionHandle.session.then((sessionID) =>
setNode(
new LocalNode(
new AnonymousControlledAccount(credential),
sessionID
)
)
);
sessionDone.current = sessionHandle.done;
}, []);
useEffect(() => {
let done: (() => void) | undefined = undefined;
(async () => {
const nodeHandle = await createBrowserNode({
auth: auth,
syncAddress,
});
setNode(nodeHandle.node);
done = nodeHandle.done;
})().catch((e) => {
console.log("Failed to create browser node", e);
});
return () => {
done && done();
sessionDone.current && sessionDone.current();
};
}, [auth, syncAddress]);
}, []);
return (
<>
{node && logOut ? (
<JazzContext.Provider value={{ localNode: node, logOut }}>
<>{children}</>
</JazzContext.Provider>
) : (
AuthUI
)}
</>
useEffect(() => {
if (node) {
void IDBStorage.connectTo(node, { trace: true });
let shouldTryToReconnect = true;
let ws: WebSocket | undefined;
void (async function websocketReconnectLoop() {
while (shouldTryToReconnect) {
ws = new WebSocket(syncAddress);
const timeToReconnect = new Promise<void>((resolve) => {
if (
!ws ||
ws.readyState === WebSocket.CLOSING ||
ws.readyState === WebSocket.CLOSED
)
resolve();
ws?.addEventListener(
"close",
() => {
console.log(
"Connection closed, reconnecting in 5s"
);
setTimeout(resolve, 5000);
},
{ once: true }
);
});
const incoming = websocketReadableStream<SyncMessage>(ws);
const outgoing = websocketWritableStream<SyncMessage>(ws);
node.sync.addPeer({
id: syncAddress + "@" + new Date().toISOString(),
incoming,
outgoing,
role: "server",
});
await timeToReconnect;
}
})();
return () => {
shouldTryToReconnect = false;
ws?.close();
};
}
}, [node, syncAddress]);
return node ? (
<JazzContext.Provider value={{ localNode: node }}>
<>{children}</>
</JazzContext.Provider>
) : (
<Auth onCredential={onCredential} />
);
}
type SessionHandle = {
session: Promise<SessionID>;
done: () => void;
};
function getSessionFor(agentID: AgentID): SessionHandle {
let done!: () => void;
const donePromise = new Promise<void>((resolve) => {
done = resolve;
});
let resolveSession: (sessionID: SessionID) => void;
const sessionPromise = new Promise<SessionID>((resolve) => {
resolveSession = resolve;
});
void (async function () {
for (let idx = 0; idx < 100; idx++) {
// To work better around StrictMode
for (let retry = 0; retry < 2; retry++) {
console.log("Trying to get lock", agentID + "_" + idx);
const sessionFinishedOrNoLock = await navigator.locks.request(
agentID + "_" + idx,
{ ifAvailable: true },
async (lock) => {
if (!lock) return "noLock";
const sessionID =
localStorage[agentID + "_" + idx] ||
cojsonInternals.newRandomSessionID(agentID);
localStorage[agentID + "_" + idx] = sessionID;
console.log("Got lock", agentID + "_" + idx, sessionID);
resolveSession(sessionID);
await donePromise;
console.log(
"Done with lock",
agentID + "_" + idx,
sessionID
);
return "sessionFinished";
}
);
if (sessionFinishedOrNoLock === "sessionFinished") {
return;
}
}
}
throw new Error("Couldn't get lock on session after 100x2 tries");
})();
return {
session: sessionPromise,
done,
};
}
export function useJazz() {
const context = React.useContext(JazzContext);
@@ -87,33 +187,30 @@ export function useJazz() {
return context;
}
export function useTelepathicState<T extends ContentType>(id?: CoID<T>) {
export function useTelepathicState<T extends ContentType>(id: CoID<T>) {
const [state, setState] = useState<T>();
const { localNode } = useJazz();
useEffect(() => {
if (!id) return;
let unsubscribe: (() => void) | undefined = undefined;
let done = false;
localNode
.load(id)
.then((state) => {
if (done) return;
unsubscribe = state.subscribe((newState) => {
// console.log(
// "Got update",
// id,
// newState.toJSON(),
// );
setState(newState as T);
});
})
.catch((e) => {
console.log("Failed to load", id, e);
localNode.load(id).then((state) => {
if (done) return;
unsubscribe = state.subscribe((newState) => {
console.log(
"Got update",
id,
newState.toJSON(),
newState.coValue.sessions
);
setState(newState as T);
});
}).catch((e) => {
console.log("Failed to load", id, e);
});
return () => {
done = true;
@@ -124,20 +221,75 @@ export function useTelepathicState<T extends ContentType>(id?: CoID<T>) {
return state;
}
export function useProfile<
P extends { [key: string]: JsonValue } & ProfileContent = ProfileContent
>(accountID?: AccountID): CoMap<P, ProfileMeta> | undefined {
const [profileID, setProfileID] = useState<CoID<CoMap<P, ProfileMeta>>>();
function websocketReadableStream<T>(ws: WebSocket) {
ws.binaryType = "arraybuffer";
const { localNode } = useJazz();
return new ReadableStream<T>({
start(controller) {
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === "ping") {
console.debug(
"Got ping from",
msg.dc,
"latency",
Date.now() - msg.time,
"ms"
);
return;
}
controller.enqueue(msg);
};
ws.onclose = () => controller.close();
ws.onerror = () =>
controller.error(new Error("The WebSocket errored!"));
},
useEffect(() => {
accountID &&
localNode
.loadProfile(accountID)
.then((profile) => setProfileID(profile.id as typeof profileID))
.catch((e) => console.log("Failed to load profile", e));
}, [localNode, accountID]);
return useTelepathicState(profileID);
cancel() {
ws.close();
},
});
}
function websocketWritableStream<T>(ws: WebSocket) {
return new WritableStream<T>({
start(controller) {
ws.onerror = () => {
controller.error(new Error("The WebSocket errored!"));
ws.onclose = null;
};
ws.onclose = () =>
controller.error(
new Error("The server closed the connection unexpectedly!")
);
return new Promise((resolve) => (ws.onopen = resolve));
},
write(chunk) {
ws.send(JSON.stringify(chunk));
// Return immediately, since the web socket gives us no easy way to tell
// when the write completes.
},
close() {
return closeWS(1000);
},
abort(reason) {
return closeWS(4000, reason && reason.message);
},
});
function closeWS(code: number, reasonString?: string) {
return new Promise<void>((resolve, reject) => {
ws.onclose = (e) => {
if (e.wasClean) {
resolve();
} else {
reject(new Error("The connection was not closed cleanly"));
}
};
ws.close(code, reasonString);
});
}
}

View File

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

View File

@@ -1,22 +1,23 @@
import { expect, test } from "vitest";
import { AnonymousControlledAccount, LocalNode, cojsonInternals } from "cojson";
import { LocalNode } from "cojson";
import { getAgentID, newRandomAgentSecret } from "cojson/src/crypto";
import { newRandomSessionID } from "cojson/src/coValue";
import { AnonymousControlledAccount } from "cojson/src/account";
import { IDBStorage } from ".";
test.skip("Should be able to initialize and load from empty DB", async () => {
const agentSecret = cojsonInternals.newRandomAgentSecret();
const agentSecret = newRandomAgentSecret();
const node = new LocalNode(
new AnonymousControlledAccount(agentSecret),
cojsonInternals.newRandomSessionID(
cojsonInternals.getAgentID(agentSecret)
)
newRandomSessionID(getAgentID(agentSecret))
);
node.sync.addPeer(await IDBStorage.asPeer({ trace: true }));
await IDBStorage.connectTo(node, { trace: true });
console.log("yay!");
const _group = node.createGroup();
const _team = node.createTeam();
await new Promise((resolve) => setTimeout(resolve, 200));
@@ -24,24 +25,20 @@ test.skip("Should be able to initialize and load from empty DB", async () => {
});
test("Should be able to sync data to database and then load that from a new node", async () => {
const agentSecret = cojsonInternals.newRandomAgentSecret();
const agentSecret = newRandomAgentSecret();
const node1 = new LocalNode(
new AnonymousControlledAccount(agentSecret),
cojsonInternals.newRandomSessionID(
cojsonInternals.getAgentID(agentSecret)
)
newRandomSessionID(getAgentID(agentSecret))
);
node1.sync.addPeer(
await IDBStorage.asPeer({ trace: true, localNodeName: "node1" })
);
await IDBStorage.connectTo(node1, { trace: true, localNodeName: "node1" });
console.log("yay!");
const group = node1.createGroup();
const team = node1.createTeam();
const map = group.createMap();
const map = team.createMap();
map.edit((m) => {
m.set("hello", "world");
@@ -51,14 +48,10 @@ test("Should be able to sync data to database and then load that from a new node
const node2 = new LocalNode(
new AnonymousControlledAccount(agentSecret),
cojsonInternals.newRandomSessionID(
cojsonInternals.getAgentID(agentSecret)
)
newRandomSessionID(getAgentID(agentSecret))
);
node2.sync.addPeer(
await IDBStorage.asPeer({ trace: true, localNodeName: "node2" })
);
await IDBStorage.connectTo(node2, { trace: true, localNodeName: "node2" });
const map2 = await node2.load(map.id);

View File

@@ -1,4 +1,5 @@
import { cojsonInternals, SessionID, SyncMessage, Peer, CojsonInternalTypes } from "cojson";
import { LocalNode, cojsonInternals, SessionID, SyncMessage } from "cojson";
import { CojsonInternalTypes } from "cojson";
import {
ReadableStream,
WritableStream,
@@ -55,14 +56,15 @@ export class IDBStorage {
})();
}
static async asPeer(
static async connectTo(
localNode: LocalNode,
{
trace,
localNodeName = "local",
}: { trace?: boolean; localNodeName?: string } | undefined = {
localNodeName: "local",
}
): Promise<Peer> {
) {
const [localNodeAsPeer, storageAsPeer] = cojsonInternals.connectedPeers(
localNodeName,
"storage",
@@ -74,7 +76,7 @@ export class IDBStorage {
localNodeAsPeer.outgoing
);
return storageAsPeer;
localNode.sync.addPeer(storageAsPeer);
}
static async open(
@@ -205,7 +207,7 @@ export class IDBStorage {
}
const dependedOnCoValues =
coValueRow?.header.ruleset.type === "group"
coValueRow?.header.ruleset.type === "team"
? Object.values(newContent.new).flatMap((sessionEntry) =>
sessionEntry.newTransactions.flatMap((tx) => {
if (tx.privacy !== "trusting") return [];
@@ -226,8 +228,8 @@ export class IDBStorage {
);
})
)
: coValueRow?.header.ruleset.type === "ownedByGroup"
? [coValueRow?.header.ruleset.group]
: coValueRow?.header.ruleset.type === "ownedByTeam"
? [coValueRow?.header.ruleset.team]
: [];
for (const dependedOnCoValue of dependedOnCoValues) {
@@ -389,7 +391,7 @@ export class IDBStorage {
);
tx.onerror = (event) => {
const target = event.target as unknown as {
const target = event.target as {
error: DOMException;
source?: { name: string };
} | null;

310
yarn.lock
View File

@@ -1175,17 +1175,6 @@
"@radix-ui/react-use-previous" "1.0.1"
"@radix-ui/react-use-size" "1.0.1"
"@radix-ui/react-collection@1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@radix-ui/react-collection/-/react-collection-1.0.3.tgz#9595a66e09026187524a36c6e7e9c7d286469159"
integrity sha512-3SzW+0PW7yBBoQlT8wNcGtaxaD0XSu0uLUFgrtHY08Acx05TaHaOmVLR73c0j/cqpDy53KBMO7s0dx2wmOIDIA==
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/react-compose-refs" "1.0.1"
"@radix-ui/react-context" "1.0.1"
"@radix-ui/react-primitive" "1.0.3"
"@radix-ui/react-slot" "1.0.2"
"@radix-ui/react-compose-refs@1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@radix-ui/react-compose-refs/-/react-compose-refs-1.0.1.tgz#7ed868b66946aa6030e580b1ffca386dd4d21989"
@@ -1200,26 +1189,6 @@
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/react-dismissable-layer@1.0.4":
version "1.0.4"
resolved "https://registry.yarnpkg.com/@radix-ui/react-dismissable-layer/-/react-dismissable-layer-1.0.4.tgz#883a48f5f938fa679427aa17fcba70c5494c6978"
integrity sha512-7UpBa/RKMoHJYjie1gkF1DlK8l1fdU/VKDpoS3rCCo8YBJR294GwcEHyxHw72yvphJ7ld0AXEcSLAzY2F/WyCg==
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/primitive" "1.0.1"
"@radix-ui/react-compose-refs" "1.0.1"
"@radix-ui/react-primitive" "1.0.3"
"@radix-ui/react-use-callback-ref" "1.0.1"
"@radix-ui/react-use-escape-keydown" "1.0.3"
"@radix-ui/react-portal@1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@radix-ui/react-portal/-/react-portal-1.0.3.tgz#ffb961244c8ed1b46f039e6c215a6c4d9989bda1"
integrity sha512-xLYZeHrWoPmA5mEKEfZZevoVRK/Q43GfzRXkWV6qawIWWK8t6ifIiLQdd7rmQ4Vk1bmI21XhqF9BN3jWf+phpA==
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/react-primitive" "1.0.3"
"@radix-ui/react-presence@1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@radix-ui/react-presence/-/react-presence-1.0.1.tgz#491990ba913b8e2a5db1b06b203cb24b5cdef9ba"
@@ -1245,25 +1214,6 @@
"@babel/runtime" "^7.13.10"
"@radix-ui/react-compose-refs" "1.0.1"
"@radix-ui/react-toast@^1.1.4":
version "1.1.4"
resolved "https://registry.yarnpkg.com/@radix-ui/react-toast/-/react-toast-1.1.4.tgz#9a7fc2d71700886f3292f7699c905f1e01be59e1"
integrity sha512-wf+fc8DOywrpRK3jlPlWVe+ELYGHdKDaaARJZNuUTWyWYq7+ANCFLp4rTjZ/mcGkJJQ/vZ949Zis9xxEpfq9OA==
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/primitive" "1.0.1"
"@radix-ui/react-collection" "1.0.3"
"@radix-ui/react-compose-refs" "1.0.1"
"@radix-ui/react-context" "1.0.1"
"@radix-ui/react-dismissable-layer" "1.0.4"
"@radix-ui/react-portal" "1.0.3"
"@radix-ui/react-presence" "1.0.1"
"@radix-ui/react-primitive" "1.0.3"
"@radix-ui/react-use-callback-ref" "1.0.1"
"@radix-ui/react-use-controllable-state" "1.0.1"
"@radix-ui/react-use-layout-effect" "1.0.1"
"@radix-ui/react-visually-hidden" "1.0.3"
"@radix-ui/react-use-callback-ref@1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.0.1.tgz#f4bb1f27f2023c984e6534317ebc411fc181107a"
@@ -1279,14 +1229,6 @@
"@babel/runtime" "^7.13.10"
"@radix-ui/react-use-callback-ref" "1.0.1"
"@radix-ui/react-use-escape-keydown@1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@radix-ui/react-use-escape-keydown/-/react-use-escape-keydown-1.0.3.tgz#217b840c250541609c66f67ed7bab2b733620755"
integrity sha512-vyL82j40hcFicA+M4Ex7hVkB9vHgSse1ZWomAqV2Je3RleKGO5iM8KMOEtfoSB0PnIelMd2lATjTGMYqN5ylTg==
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/react-use-callback-ref" "1.0.1"
"@radix-ui/react-use-layout-effect@1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@radix-ui/react-use-layout-effect/-/react-use-layout-effect-1.0.1.tgz#be8c7bc809b0c8934acf6657b577daf948a75399"
@@ -1309,14 +1251,6 @@
"@babel/runtime" "^7.13.10"
"@radix-ui/react-use-layout-effect" "1.0.1"
"@radix-ui/react-visually-hidden@1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@radix-ui/react-visually-hidden/-/react-visually-hidden-1.0.3.tgz#51aed9dd0fe5abcad7dee2a234ad36106a6984ac"
integrity sha512-D4w41yN5YRKtu464TLnByKzMDG/JlMPHtfZgQAu9v6mNakUqGUI9vUrfQKz8NK41VMm/xbZbh76NUTVtIYqOMA==
dependencies:
"@babel/runtime" "^7.13.10"
"@radix-ui/react-primitive" "1.0.3"
"@rollup/pluginutils@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.1.0.tgz#706b4524ee6dc8b103b3c995533e5ad680c02b9b"
@@ -1513,13 +1447,6 @@
dependencies:
"@babel/types" "^7.20.7"
"@types/better-sqlite3@^7.6.4":
version "7.6.4"
resolved "https://registry.yarnpkg.com/@types/better-sqlite3/-/better-sqlite3-7.6.4.tgz#102462611e67aadf950d3ccca10292de91e6f35b"
integrity sha512-dzrRZCYPXIXfSR1/surNbJ/grU3scTaygS0OMzjlGf71i9sc2fGyHPXXiXmEvNIoE0cGwsanEFMVJxPXmco9Eg==
dependencies:
"@types/node" "*"
"@types/chai-subset@^1.3.3":
version "1.3.3"
resolved "https://registry.yarnpkg.com/@types/chai-subset/-/chai-subset-1.3.3.tgz#97893814e92abd2c534de422cb377e0e0bdaac94"
@@ -1611,13 +1538,6 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf"
integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==
"@types/qrcode@^1.5.1":
version "1.5.1"
resolved "https://registry.yarnpkg.com/@types/qrcode/-/qrcode-1.5.1.tgz#027c2dbfbc8505e1fe2f4033daba920dbd182b44"
integrity sha512-HpSN675K0PmxIDRpjMI3Mc2GiKo3dNu+X/F5SoItiaDS1lVfgC6Wac1c5lQDfKWbTJUSHWiHKzpJpBZG7k9gaA==
dependencies:
"@types/node" "*"
"@types/react-dom@^18.2.7":
version "18.2.7"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.2.7.tgz#67222a08c0a6ae0a0da33c3532348277c70abb63"
@@ -1654,7 +1574,7 @@
resolved "https://registry.yarnpkg.com/@types/which/-/which-2.0.2.tgz#54541d02d6b1daee5ec01ac0d1b37cecf37db1ae"
integrity sha512-113D3mDkZDjo+EeUEHCFy0qniNc1ZpecGiAU7WSo7YDoSzolZIQKpYFHrPpjkB2nuyahcKfrmLXeQlh7gqJYdw==
"@types/ws@^8.5.3", "@types/ws@^8.5.5":
"@types/ws@^8.5.3":
version "8.5.5"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.5.tgz#af587964aa06682702ee6dcbc7be41a80e4b28eb"
integrity sha512-lwhs8hktwxSjf9UaZ9tG5M03PGogvFaH8gUgLNbN9HKIg0dvv6q+gkSuJ8HN4/VbyxkuLzCjlN7GquQ0gUJfIg==
@@ -2257,14 +2177,6 @@ before-after-hook@^2.2.0:
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==
better-sqlite3@^8.5.2:
version "8.5.2"
resolved "https://registry.yarnpkg.com/better-sqlite3/-/better-sqlite3-8.5.2.tgz#a1c13e4361125255e39302e8b569a6568c3291e3"
integrity sha512-w/EZ/jwuZF+/47mAVC2+rhR2X/gwkZ+fd1pbX7Y90D5NRaRzDQcxrHY10t6ijGiYIonCVsBSF5v1cay07bP5sg==
dependencies:
bindings "^1.5.0"
prebuild-install "^7.1.0"
big-integer@^1.6.17:
version "1.6.51"
resolved "https://registry.yarnpkg.com/big-integer/-/big-integer-1.6.51.tgz#0df92a5d9880560d3ff2d5fd20245c889d130686"
@@ -2283,13 +2195,6 @@ binary@~0.3.0:
buffers "~0.1.1"
chainsaw "~0.1.0"
bindings@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"
bl@^4.0.3, bl@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/bl/-/bl-4.1.0.tgz#451535264182bec2fbbc83a62ab98cf11d9f7b3a"
@@ -2462,7 +2367,7 @@ camelcase-keys@^6.2.2:
map-obj "^4.0.0"
quick-lru "^4.0.1"
camelcase@^5.0.0, camelcase@^5.3.1:
camelcase@^5.3.1:
version "5.3.1"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-5.3.1.tgz#e3c9b31569e106811df242f715725a1f4c494320"
integrity sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==
@@ -2557,11 +2462,6 @@ chokidar@^3.5.3:
optionalDependencies:
fsevents "~2.3.2"
chownr@^1.1.1:
version "1.1.4"
resolved "https://registry.yarnpkg.com/chownr/-/chownr-1.1.4.tgz#6fc9d7b42d32a583596337666e7d08084da2cc6b"
integrity sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==
chownr@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/chownr/-/chownr-2.0.0.tgz#15bfbe53d2eab4cf70f18a8cd68ebe5b3cb1dece"
@@ -2628,15 +2528,6 @@ cli-width@^3.0.0:
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-3.0.0.tgz#a2f48437a2caa9a22436e794bf071ec9e61cedf6"
integrity sha512-FxqpkPPwu1HjuN93Omfm4h8uIanXofW0RxVEW3k5RKx+mJJYSthzNhp32Kzxxy3YAEZ/Dc/EWN1vZRY0+kOhbw==
cliui@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-6.0.0.tgz#511d702c0c4e41ca156d7d0e96021f23e13225b1"
integrity sha512-t6wbgtoCXvAzst7QgXxJYqPt0usEfbgQdftEPbLL/cvv6HPE5VgvqCuAIDR0NgU52ds6rFwqrgakNLrHEjCbrQ==
dependencies:
string-width "^4.2.0"
strip-ansi "^6.0.0"
wrap-ansi "^6.2.0"
cliui@^7.0.2:
version "7.0.4"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.4.tgz#a0265ee655476fc807aea9df3df8df7783808b4f"
@@ -2970,7 +2861,7 @@ decamelize-keys@^1.1.0:
decamelize "^1.1.0"
map-obj "^1.0.0"
decamelize@^1.1.0, decamelize@^1.2.0:
decamelize@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
integrity sha512-z2S+W9X73hAUUki+N+9Za2lBlun89zigOyGrsax+KUQ6wKW4ZoWpEYBkGhQjwAjjDCkWxhY0VKEhk8wzY7F5cA==
@@ -3004,11 +2895,6 @@ deep-eql@^4.1.2:
dependencies:
type-detect "^4.0.0"
deep-extend@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"
integrity sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==
deep-is@^0.1.3:
version "0.1.4"
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.4.tgz#a6f2dce612fadd2ef1f519b73551f17e85199831"
@@ -3075,11 +2961,6 @@ detect-indent@^5.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
integrity sha512-rlpvsxUtM0PQvy9iZe640/IWwWYyBsTApREbA1pHOpmOUIl9MkP/U4z7vTtg4Oaojvqhxt7sdufnT0EzGaR31g==
detect-libc@^2.0.0:
version "2.0.2"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-2.0.2.tgz#8ccf2ba9315350e1241b88d0ac3b0e1fbd99605d"
integrity sha512-UX6sGumvvqSaXgdKGUsgZWqcUyIXZ/vZTrlRT/iobiKhGL0zL4d3osHj3uqllWJK+i+sixDS/3COVEOFbupFyw==
detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
@@ -3105,11 +2986,6 @@ diff-sequences@^29.4.3:
resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-29.4.3.tgz#9314bc1fabe09267ffeca9cbafc457d8499a13f2"
integrity sha512-ofrBgwpPhCD85kMKtE9RYFFq6OC1A89oW2vvgWZNCwxrUpRUILopY7lsYyMDSjc8g6U6aiO0Qubg6r4Wgt5ZnA==
dijkstrajs@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/dijkstrajs/-/dijkstrajs-1.0.3.tgz#4c8dbdea1f0f6478bff94d9c49c784d623e4fc23"
integrity sha512-qiSlmBq9+BCdCA/L46dw8Uy93mloxsPSbwnm5yrKn2vMPiy8KyAskTF6zuV/j5BMsmOGZDPs7KjU+mjb670kfA==
dir-glob@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"
@@ -3206,11 +3082,6 @@ emoji-regex@^9.2.2:
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-9.2.2.tgz#840c8803b0d8047f4ff0cf963176b32d4ef3ed72"
integrity sha512-L18DaJsXSUk2+42pv8mLs5jJT2hqFkFE4j21wOmgbUqsZ2hL72NsUU785g9RXgo3s0ZNgVl42TiHp3ZtOv/Vyg==
encode-utf8@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/encode-utf8/-/encode-utf8-1.0.3.tgz#f30fdd31da07fb596f281beb2f6b027851994cda"
integrity sha512-ucAnuBEhUK4boH2HjVYG5Q2mQyPorvv0u/ocS+zhdw0S8AlHYY+GOFhP1Gio5z4icpP2ivFSvhtFjQi8+T9ppw==
encoding@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9"
@@ -3606,11 +3477,6 @@ exit@^0.1.2:
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
integrity sha512-Zk/eNKV2zbjpKzrsQ+n1G6poVbErQxJ0LBOJXaKZ1EViLzH+hrLu9cdXI4zw9dBQJslwBEpbQ2P1oS7nDxs6jQ==
expand-template@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/expand-template/-/expand-template-2.0.3.tgz#6e14b3fcee0f3a6340ecb57d2e8918692052a47c"
integrity sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==
expect@^29.0.0, expect@^29.6.2:
version "29.6.2"
resolved "https://registry.yarnpkg.com/expect/-/expect-29.6.2.tgz#7b08e83eba18ddc4a2cf62b5f2d1918f5cd84521"
@@ -3742,11 +3608,6 @@ file-entry-cache@^6.0.1:
dependencies:
flat-cache "^3.0.4"
file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
filelist@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/filelist/-/filelist-1.0.4.tgz#f78978a1e944775ff9e62e744424f215e58352b5"
@@ -3944,7 +3805,7 @@ gensync@^1.0.0-beta.2:
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"
integrity sha512-3hN7NaskYvMDLQY55gnW3NQ+mesEAepTqlg+VEbj7zzqEMBVNhzcGYYeqFo/TlYz6eQiFcp1HcsCZO+nGgS8zg==
get-caller-file@^2.0.1, get-caller-file@^2.0.5:
get-caller-file@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
@@ -4053,11 +3914,6 @@ gitconfiglocal@^1.0.0:
dependencies:
ini "^1.3.2"
github-from-package@0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/github-from-package/-/github-from-package-0.0.0.tgz#97fb5d96bfde8973313f20e8288ef9a167fa64ce"
integrity sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==
glob-parent@5.1.2, glob-parent@^5.1.2, glob-parent@~5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4"
@@ -4437,7 +4293,7 @@ inherits@2, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.0, inherits@~2.0.3:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
ini@^1.3.2, ini@^1.3.8, ini@~1.3.0:
ini@^1.3.2, ini@^1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==
@@ -5692,7 +5548,7 @@ minimist-options@4.1.0:
is-plain-obj "^1.1.0"
kind-of "^6.0.3"
minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5, minimist@^1.2.6:
minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
@@ -5779,7 +5635,7 @@ mitt@3.0.0:
resolved "https://registry.yarnpkg.com/mitt/-/mitt-3.0.0.tgz#69ef9bd5c80ff6f57473e8d89326d01c414be0bd"
integrity sha512-7dX2/10ITVyqh4aOSVI9gdape+t9l2/8QxHrFmUXu4EEUpdlxl6RudZUPZoc+zuY2hk1j7XxVroIVIan/pD/SQ==
mkdirp-classic@^0.5.2, mkdirp-classic@^0.5.3:
mkdirp-classic@^0.5.2:
version "0.5.3"
resolved "https://registry.yarnpkg.com/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz#fa10c9115cc6d8865be221ba47ee9bed78601113"
integrity sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==
@@ -5876,11 +5732,6 @@ nanoid@^3.3.6:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==
napi-build-utils@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/napi-build-utils/-/napi-build-utils-1.0.2.tgz#b1fddc0b2c46e380a0b7a76f984dd47c41a13806"
integrity sha512-ONmRUqK7zj7DWX0D9ADe03wbwOBZxNAfF20PlGfCWQcD3+/MakShIHrMqx9YwPTfxDdF1zLeL+RGZiR9kGMLdg==
natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
@@ -5909,13 +5760,6 @@ nice-napi@^1.0.2:
node-addon-api "^3.0.0"
node-gyp-build "^4.2.2"
node-abi@^3.3.0:
version "3.47.0"
resolved "https://registry.yarnpkg.com/node-abi/-/node-abi-3.47.0.tgz#6cbfa2916805ae25c2b7156ca640131632eb05e8"
integrity sha512-2s6B2CWZM//kPgwnuI0KrYwNjfdByE25zvAaEpq9IH4zcNsarH8Ihu/UuX6XMPEogDAxkuUFeZn60pXNHAqn3A==
dependencies:
semver "^7.3.5"
node-addon-api@^3.0.0, node-addon-api@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161"
@@ -6611,11 +6455,6 @@ pkg-types@^1.0.3:
mlly "^1.2.0"
pathe "^1.1.0"
pngjs@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/pngjs/-/pngjs-5.0.0.tgz#e79dd2b215767fd9c04561c01236df960bce7fbb"
integrity sha512-40QW5YalBNfQo5yRYmiw7Yz6TKKVr3h6970B2YE+3fQpsWcrbj1PzJgxeJ19DRQjhMbKPIuMY8rFaXc8moolVw==
postcss-import@^15.1.0:
version "15.1.0"
resolved "https://registry.yarnpkg.com/postcss-import/-/postcss-import-15.1.0.tgz#41c64ed8cc0e23735a9698b3249ffdbf704adc70"
@@ -6669,24 +6508,6 @@ postcss@^8.4.23, postcss@^8.4.27:
picocolors "^1.0.0"
source-map-js "^1.0.2"
prebuild-install@^7.1.0:
version "7.1.1"
resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-7.1.1.tgz#de97d5b34a70a0c81334fd24641f2a1702352e45"
integrity sha512-jAXscXWMcCK8GgCoHOfIr0ODh5ai8mj63L2nWrjuAgXE6tDyYGnx4/8o/rCgU+B4JSyZBKbeZqzhtwtC3ovxjw==
dependencies:
detect-libc "^2.0.0"
expand-template "^2.0.3"
github-from-package "0.0.0"
minimist "^1.2.3"
mkdirp-classic "^0.5.3"
napi-build-utils "^1.0.1"
node-abi "^3.3.0"
pump "^3.0.0"
rc "^1.2.7"
simple-get "^4.0.0"
tar-fs "^2.0.0"
tunnel-agent "^0.6.0"
prelude-ls@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
@@ -6798,16 +6619,6 @@ pure-rand@^6.0.0:
resolved "https://registry.yarnpkg.com/pure-rand/-/pure-rand-6.0.2.tgz#a9c2ddcae9b68d736a8163036f088a2781c8b306"
integrity sha512-6Yg0ekpKICSjPswYOuC5sku/TSWaRYlA0qsXqJgM/d/4pLPHPuTxK7Nbf7jFKzAeedUhR8C7K9Uv63FBsSo8xQ==
qrcode@^1.5.3:
version "1.5.3"
resolved "https://registry.yarnpkg.com/qrcode/-/qrcode-1.5.3.tgz#03afa80912c0dccf12bc93f615a535aad1066170"
integrity sha512-puyri6ApkEHYiVl4CFzo1tDkAZ+ATcnbJrJ6RiBM1Fhctdn/ix9MTE3hRph33omisEbC/2fcfemsseiKgBPKZg==
dependencies:
dijkstrajs "^1.0.1"
encode-utf8 "^1.0.3"
pngjs "^5.0.0"
yargs "^15.3.1"
query-selector-shadow-dom@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/query-selector-shadow-dom/-/query-selector-shadow-dom-1.0.1.tgz#1c7b0058eff4881ac44f45d8f84ede32e9a2f349"
@@ -6833,16 +6644,6 @@ quick-lru@^5.1.1:
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932"
integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA==
rc@^1.2.7:
version "1.2.8"
resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.8.tgz#cd924bf5200a075b83c188cd6b9e211b7fc0d3ed"
integrity sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==
dependencies:
deep-extend "^0.6.0"
ini "~1.3.0"
minimist "^1.2.0"
strip-json-comments "~2.0.1"
react-dom@^18.2.0:
version "18.2.0"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.2.0.tgz#22aaf38708db2674ed9ada224ca4aa708d821e3d"
@@ -7009,11 +6810,6 @@ require-directory@^2.1.1:
resolved "https://registry.yarnpkg.com/require-directory/-/require-directory-2.1.1.tgz#8c64ad5fd30dab1c976e2344ffe7f792a6a6df42"
integrity sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==
require-main-filename@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b"
integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg==
resolve-alpn@^1.2.0:
version "1.2.1"
resolved "https://registry.yarnpkg.com/resolve-alpn/-/resolve-alpn-1.2.1.tgz#b7adbdac3546aaaec20b45e7d8265927072726f9"
@@ -7139,16 +6935,16 @@ safaridriver@^0.1.0:
resolved "https://registry.yarnpkg.com/safaridriver/-/safaridriver-0.1.0.tgz#8ff901e847b003c6a52b534028f57cddc82d6b14"
integrity sha512-azzzIP3gR1TB9bVPv7QO4Zjw0rR1BWEU/s2aFdUMN48gxDjxEB13grAEuXDmkKPgE74cObymDxmAmZnL3clj4w==
safe-buffer@^5.0.1, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
"safer-buffer@>= 2.1.2 < 3", "safer-buffer@>= 2.1.2 < 3.0.0":
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
@@ -7247,20 +7043,6 @@ sigstore@^1.3.0, sigstore@^1.4.0:
"@sigstore/tuf" "^1.0.3"
make-fetch-happen "^11.0.1"
simple-concat@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/simple-concat/-/simple-concat-1.0.1.tgz#f46976082ba35c2263f1c8ab5edfe26c41c9552f"
integrity sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==
simple-get@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/simple-get/-/simple-get-4.0.1.tgz#4a39db549287c979d352112fa03fd99fd6bc3543"
integrity sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==
dependencies:
decompress-response "^6.0.0"
once "^1.3.1"
simple-concat "^1.0.0"
sirv@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/sirv/-/sirv-2.0.3.tgz#ca5868b87205a74bef62a469ed0296abceccd446"
@@ -7501,11 +7283,6 @@ strip-json-comments@^3.1.1:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
strip-json-comments@~2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
integrity sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==
strip-literal@^1.0.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/strip-literal/-/strip-literal-1.3.0.tgz#db3942c2ec1699e6836ad230090b84bb458e3a07"
@@ -7608,17 +7385,7 @@ tar-fs@3.0.4, tar-fs@^3.0.4:
pump "^3.0.0"
tar-stream "^3.1.5"
tar-fs@^2.0.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/tar-fs/-/tar-fs-2.1.1.tgz#489a15ab85f1f0befabb370b7de4f9eb5cbe8784"
integrity sha512-V0r2Y9scmbDRLCNex/+hYzvp/zyYjvFbHPNgVTKfQvVrb6guiE/fxP+XblDNR011utopbkex2nM4dHNV6GDsng==
dependencies:
chownr "^1.1.1"
mkdirp-classic "^0.5.2"
pump "^3.0.0"
tar-stream "^2.1.4"
tar-stream@^2.1.4, tar-stream@^2.2.0, tar-stream@~2.2.0:
tar-stream@^2.2.0, tar-stream@~2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-2.2.0.tgz#acad84c284136b060dc3faa64474aa9aebd77287"
integrity sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==
@@ -7826,13 +7593,6 @@ tuf-js@^1.1.7:
debug "^4.3.4"
make-fetch-happen "^11.1.1"
tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"
integrity sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==
dependencies:
safe-buffer "^5.0.1"
type-check@^0.4.0, type-check@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.4.0.tgz#07b8203bfa7056c0657050e3ccd2c37730bab8f1"
@@ -7918,11 +7678,6 @@ unbzip2-stream@1.4.3:
buffer "^5.2.1"
through "^2.3.8"
uniqolor@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/uniqolor/-/uniqolor-1.1.0.tgz#7519f81133cd54a1f4a59c33c81dbe04a3ad155d"
integrity sha512-j2XyokF24fsj+L5u6fbu4rM3RQc6VWJuAngYM2k0ZdG3yiVxt0smLkps2GmQIYqK8VkELGdM9vFU/HfOkK/zoQ==
unique-filename@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/unique-filename/-/unique-filename-3.0.0.tgz#48ba7a5a16849f5080d26c760c86cf5cf05770ea"
@@ -8175,11 +7930,6 @@ whatwg-url@^5.0.0:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"
which-module@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.1.tgz#776b1fe35d90aebe99e8ac15eb24093389a4a409"
integrity sha512-iBdZ57RDvnOR9AGBhML2vFZf7h8vmBjhoaZqODJBFWHVtKkDmKuHai3cx5PgVMrX5YDNp27AofYbAwctSS+vhQ==
which@^2.0.1, which@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
@@ -8224,7 +7974,7 @@ wordwrap@^1.0.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"
wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
wrap-ansi@^6.0.1:
version "6.2.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
integrity sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==
@@ -8293,7 +8043,7 @@ write-pkg@4.0.0:
type-fest "^0.4.1"
write-json-file "^3.2.0"
ws@8.13.0, ws@^8.13.0, ws@^8.8.0:
ws@8.13.0, ws@^8.8.0:
version "8.13.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0"
integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA==
@@ -8303,11 +8053,6 @@ xtend@~4.0.1:
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==
y18n@^4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.3.tgz#b5f259c82cd6e336921efd7bfd8bf560de9eeedf"
integrity sha512-JKhqTOwSrqNA1NY5lSztJ1GrBiUodLMmIZuLiDaMRJ+itFd+ABVE8XBjOvIWL+rSqNDC74LCSFmlb/U4UZ4hJQ==
y18n@^5.0.5:
version "5.0.8"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55"
@@ -8338,14 +8083,6 @@ yargs-parser@21.1.1, yargs-parser@^21.0.1, yargs-parser@^21.1.1:
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==
yargs-parser@^18.1.2:
version "18.1.3"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.3.tgz#be68c4975c6b2abf469236b0c870362fab09a7b0"
integrity sha512-o50j0JeToy/4K6OZcaQmW6lyXXKhq7csREXcDwk2omFPJEwUNOVtJKvmDr9EI1fAJZUyZcRF7kxGBWmRXudrCQ==
dependencies:
camelcase "^5.0.0"
decamelize "^1.2.0"
yargs-parser@^20.2.2, yargs-parser@^20.2.3:
version "20.2.9"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
@@ -8377,23 +8114,6 @@ yargs@17.7.1:
y18n "^5.0.5"
yargs-parser "^21.1.1"
yargs@^15.3.1:
version "15.4.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8"
integrity sha512-aePbxDmcYW++PaqBsJ+HYUFwCdv4LVvdnhBy78E57PIor8/OVvhMrADFFEDh8DHDFRv/O9i3lPhsENjO7QX0+A==
dependencies:
cliui "^6.0.0"
decamelize "^1.2.0"
find-up "^4.1.0"
get-caller-file "^2.0.1"
require-directory "^2.1.1"
require-main-filename "^2.0.0"
set-blocking "^2.0.0"
string-width "^4.2.0"
which-module "^2.0.0"
y18n "^4.0.0"
yargs-parser "^18.1.2"
yargs@^17.3.1, yargs@^17.6.2:
version "17.7.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269"