@blueshed/joined 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/skills/joined/SKILL.md +9 -1
- package/.claude/skills/joined/reference.md +267 -53
- package/.claude/skills/joined-door/SKILL.md +57 -0
- package/README.md +19 -50
- package/docs/design.md +76 -10
- package/docs/introduction.md +18 -18
- package/docs/light-and-shadow.svg +1 -1
- package/examples/README.md +20 -0
- package/examples/account/account.json +1 -0
- package/examples/account/derive.ts +41 -0
- package/examples/account/generated/elements.ts +52 -0
- package/examples/account/generated/rows.ts +22 -0
- package/examples/account/generated/rules.ts +32 -0
- package/examples/account/routes.ts +5 -0
- package/examples/app.css +2 -1
- package/examples/cafe/books.html +2 -2
- package/examples/cafe/cafe.css +9 -16
- package/examples/cafe/generated/books.rows.ts +22 -0
- package/examples/cafe/generated/books.rules.ts +3 -2
- package/examples/cafe/generated/kitchen.rows.ts +27 -0
- package/examples/cafe/generated/kitchen.rules.ts +11 -1
- package/examples/cafe/generated/menu.rows.ts +22 -0
- package/examples/cafe/generated/menu.rules.ts +3 -2
- package/examples/cafe/generated/order.rows.ts +27 -0
- package/examples/cafe/generated/order.rules.ts +11 -1
- package/examples/cafe/generated/staff.rows.ts +22 -0
- package/examples/cafe/generated/staff.rules.ts +6 -1
- package/examples/cafe/generated/supplies.queries.ts +4 -2
- package/examples/cafe/generated/supplies.rows.ts +22 -0
- package/examples/cafe/generated/supplies.rules.ts +12 -1
- package/examples/cafe/generated/till.queries.ts +2 -0
- package/examples/cafe/generated/till.rows.ts +27 -0
- package/examples/cafe/generated/till.rules.ts +11 -1
- package/examples/cafe/index.html +14 -15
- package/examples/cafe/kitchen.ts +1 -1
- package/examples/cafe/look.ts +30 -0
- package/examples/cafe/menu.html +10 -26
- package/examples/cafe/menu.ts +8 -80
- package/examples/cafe/order.html +6 -6
- package/examples/cafe/order.ts +5 -1
- package/examples/cafe/routes.ts +0 -1
- package/examples/cafe/stories.jsonl +3 -1
- package/examples/cafe/supplies.html +34 -34
- package/examples/cafe/supplies.ts +14 -11
- package/examples/cafe/till.ts +5 -1
- package/examples/chart/arches.ts +148 -0
- package/examples/chart/chart.css +6 -0
- package/examples/chart/chart.json +87 -0
- package/examples/chart/generated/elements.ts +105 -0
- package/examples/chart/generated/queries.ts +36 -0
- package/examples/chart/generated/rows.ts +27 -0
- package/examples/chart/generated/rules.ts +111 -0
- package/examples/chart/grid.ts +118 -0
- package/examples/chart/index.html +78 -0
- package/examples/chart/migrations/001-chart.ts +6 -0
- package/examples/chart/migrations/002-create-conditions.ts +6 -0
- package/examples/chart/page.ts +20 -0
- package/examples/chart/routes.ts +7 -0
- package/examples/chart/stories.jsonl +14 -0
- package/examples/chart/teeth.ts +57 -0
- package/examples/desk/generated/rows.ts +22 -0
- package/examples/desk/generated/rules.ts +34 -1
- package/examples/desk/index.html +9 -2
- package/examples/desk/routes.ts +0 -2
- package/examples/desk/stories.jsonl +3 -2
- package/examples/desk/thumbs.ts +2 -2
- package/examples/hello/generated/elements.ts +2 -2
- package/examples/hello/generated/rules.ts +3 -2
- package/examples/hello/hello.css +2 -4
- package/examples/hello/index.html +10 -3
- package/examples/hello/page.ts +4 -4
- package/examples/index.html +4 -5
- package/examples/notes/generated/elements.ts +112 -0
- package/examples/notes/generated/queries.ts +36 -0
- package/examples/notes/generated/rows.ts +27 -0
- package/examples/notes/generated/rules.ts +133 -0
- package/examples/notes/index.html +32 -0
- package/examples/notes/look.ts +41 -0
- package/examples/notes/migrations/001-notes.ts +7 -0
- package/examples/notes/notes.json +78 -0
- package/examples/notes/page.ts +21 -0
- package/examples/notes/routes.ts +6 -0
- package/examples/notes/stories.jsonl +21 -0
- package/examples/people/generated/rows.ts +22 -0
- package/examples/people/generated/rules.ts +11 -1
- package/examples/people/links.ts +2 -1
- package/examples/people/routes.ts +1 -3
- package/examples/people/stories.jsonl +1 -0
- package/examples/people/view.ts +5 -2
- package/examples/trip/generated/elements.ts +4 -2
- package/examples/trip/generated/queries.ts +12 -1
- package/examples/trip/generated/rows.ts +27 -0
- package/examples/trip/generated/rules.ts +56 -2
- package/examples/trip/index.html +58 -12
- package/examples/trip/line.css +186 -0
- package/examples/trip/line.ts +51 -40
- package/examples/trip/page.ts +65 -71
- package/examples/trip/routes.ts +9 -5
- package/examples/trip/stays.css +12 -12
- package/examples/trip/stays.ts +15 -10
- package/examples/trip/stories.jsonl +2 -0
- package/examples/trip/told.ts +3 -26
- package/examples/trip/trip.css +16 -0
- package/examples/trips/generated/rows.ts +22 -0
- package/examples/trips/generated/rules.ts +9 -1
- package/examples/trips/index.html +10 -10
- package/examples/trips/map.ts +3 -14
- package/examples/trips/new-trip.ts +14 -16
- package/examples/weeks/generated/elements.ts +1 -1
- package/examples/weeks/generated/queries.ts +11 -0
- package/examples/weeks/generated/rows.ts +22 -0
- package/examples/weeks/generated/rules.ts +11 -5
- package/examples/weeks/routes.ts +3 -2
- package/examples/weeks/stories.jsonl +1 -0
- package/examples/welcome.css +7 -5
- package/package.json +8 -2
- package/src/awesome.ts +103 -0
- package/src/board.css +75 -0
- package/src/board.ts +173 -0
- package/src/cli.ts +15 -2
- package/src/compile.ts +569 -35
- package/src/definitions.ts +53 -15
- package/src/draft.ts +38 -23
- package/src/drawer.ts +222 -0
- package/src/engine.ts +240 -166
- package/src/fields.ts +268 -0
- package/src/files.ts +6 -4
- package/src/ground.ts +110 -0
- package/src/icon-names.ts +15 -2
- package/src/kit.ts +51 -0
- package/src/link.ts +3 -3
- package/src/map.css +133 -0
- package/src/map.ts +200 -0
- package/src/mcp.ts +106 -22
- package/src/members/demo.ts +13 -12
- package/src/members/generated/rows.ts +22 -0
- package/src/members/generated/rules.ts +3 -2
- package/src/members/group.ts +65 -39
- package/src/members/keys.ts +93 -77
- package/src/members/panel.css +23 -17
- package/src/members/panel.ts +49 -35
- package/src/migrate.ts +120 -104
- package/src/month.css +125 -0
- package/src/month.ts +179 -0
- package/src/page.css +8 -0
- package/src/page.ts +7 -4
- package/src/parts.css +462 -170
- package/src/parts.ts +209 -26
- package/src/plane.css +56 -0
- package/src/plane.ts +151 -0
- package/src/postgres-server.ts +60 -0
- package/src/reading.css +77 -0
- package/src/reading.ts +238 -0
- package/src/rows.css +308 -0
- package/src/rows.ts +780 -0
- package/src/search.css +80 -0
- package/src/search.ts +130 -0
- package/src/server.ts +205 -114
- package/src/settings.ts +9 -4
- package/src/sheet.ts +114 -0
- package/src/skill.ts +40 -0
- package/src/slots.css +94 -0
- package/src/slots.ts +213 -0
- package/src/specs.ts +38 -0
- package/src/starter.ts +5 -2
- package/src/store.ts +184 -143
- package/src/stories.ts +43 -11
- package/src/surface.ts +159 -0
- package/src/timeline.css +111 -0
- package/src/timeline.ts +226 -0
- package/src/toast.ts +21 -48
- package/src/together.ts +0 -7
- package/src/tokens.css +78 -1
- package/src/tokens.json +249 -0
- package/src/tool.ts +211 -0
- package/src/upkeep.ts +18 -20
- package/src/views.css +44 -0
- package/src/views.ts +94 -0
- package/src/week.css +151 -0
- package/src/week.ts +250 -0
- package/src/write.ts +202 -69
- package/starter/CLAUDE.md +3 -3
- package/starter/bunfig +4 -0
- package/starter/prettierignore +4 -0
- package/starter/tests/hello.test.ts +2 -0
- package/docs/learning.md +0 -76
- package/examples/cafe/trust.ts +0 -20
- package/examples/desk/trust.ts +0 -24
- package/examples/people/trust.ts +0 -14
- package/examples/trip/answer-rows.ts +0 -70
- package/examples/trip/bar.ts +0 -89
- package/examples/trip/calendar-map.css +0 -711
- package/examples/trip/calendar-map.ts +0 -249
- package/examples/trip/drags.ts +0 -143
- package/examples/trip/drawer.ts +0 -250
- package/examples/trip/event-form.ts +0 -200
- package/examples/trip/holding.ts +0 -61
- package/examples/trip/map.ts +0 -234
- package/examples/trip/month.ts +0 -87
- package/examples/trip/other-weeks.ts +0 -67
- package/examples/trip/peek.ts +0 -71
- package/examples/trip/people-chips.ts +0 -74
- package/examples/trip/shell.ts +0 -39
- package/examples/trip/trust.ts +0 -51
- package/examples/trip/week.ts +0 -237
- package/examples/trip/writes.ts +0 -91
- package/examples/trip/zones.ts +0 -16
- package/examples/weeks/derive.ts +0 -23
- package/src/scrim.ts +0 -81
package/src/members/group.ts
CHANGED
|
@@ -24,14 +24,22 @@ export type Who = { member: number; token: string; agent?: Agent };
|
|
|
24
24
|
/** A page's socket, as the server keeps it: the documents it subscribes to, who it is, and the site it came from. */
|
|
25
25
|
/** A page's socket's own: the documents it holds, who it is, where it's from, and the changes a stranger's page made
|
|
26
26
|
* (`<doc> <version>`), which only it may undo. */
|
|
27
|
-
export type Data = {
|
|
27
|
+
export type Data = {
|
|
28
|
+
docs: Set<string>;
|
|
29
|
+
who: Who | null;
|
|
30
|
+
origin: string;
|
|
31
|
+
made?: Set<string>;
|
|
32
|
+
/** Its messages, answered in turn. */
|
|
33
|
+
queue?: Promise<unknown>;
|
|
34
|
+
};
|
|
28
35
|
type Socket = Bun.ServerWebSocket<Data>;
|
|
29
36
|
/** A write the group lets go: its ops, with what they bring, and what follows them. */
|
|
30
37
|
/** A write the group lets go: its ops, with what they bring; what must land with them, in the write's own
|
|
31
38
|
* transaction (`within`: the keys of a member removed, a notice); and what follows once it has (`after`:
|
|
32
39
|
* pages told, or closed). */
|
|
33
40
|
type Write =
|
|
34
|
-
{ refused: string }
|
|
41
|
+
| { refused: string }
|
|
42
|
+
| { ops: Op[]; within?: (applied: Op[]) => Promise<void>; after: (applied: Op[]) => Promise<void> };
|
|
35
43
|
|
|
36
44
|
const COOKIE = "joined";
|
|
37
45
|
/** A year, in seconds: how long a link signs a browser in. */
|
|
@@ -61,7 +69,7 @@ export async function groupOf({
|
|
|
61
69
|
holds,
|
|
62
70
|
}: {
|
|
63
71
|
/** The documents a member may hold, told to their pages with who they are: a link to a page they can't is hidden. */
|
|
64
|
-
holds?: (member: number) => string[]
|
|
72
|
+
holds?: (member: number) => Promise<string[]>;
|
|
65
73
|
/** A demo on this machine (never live): anyone here may be any member, without their link (/as); whether a
|
|
66
74
|
* request is from this machine. */
|
|
67
75
|
demo?: (req: Request) => boolean;
|
|
@@ -72,18 +80,18 @@ export async function groupOf({
|
|
|
72
80
|
keysFile?: string;
|
|
73
81
|
/** Whether the database lasts (a file, not memory): only then is the keys file renamed once brought in. */
|
|
74
82
|
lasts?: boolean;
|
|
75
|
-
read: (doc: string) => Record<string, unknown
|
|
83
|
+
read: (doc: string) => Promise<Record<string, unknown>>;
|
|
76
84
|
}) {
|
|
77
|
-
const members = () => read(OWN).members as Member[];
|
|
85
|
+
const members = async () => (await read(OWN)).members as Member[];
|
|
78
86
|
const open = {
|
|
79
87
|
routes: {},
|
|
80
88
|
at: async (_req: Request): Promise<Who | null | Response> => null,
|
|
81
89
|
edge: (answer: Handler) => answer,
|
|
82
|
-
admit: (_ws: Socket) => {},
|
|
90
|
+
admit: async (_ws: Socket) => {},
|
|
83
91
|
opened: async (_ws: Socket, _doc: string) => {},
|
|
84
92
|
left: (_ws: Socket) => {},
|
|
85
|
-
write: (doc: string, ops: Op[], _who: Who | null, _why?: string): Write => ({
|
|
86
|
-
ops: doc === OWN ? withMovesUp(ops, members()) : ops,
|
|
93
|
+
write: async (doc: string, ops: Op[], _who: Who | null, _why?: string): Promise<Write> => ({
|
|
94
|
+
ops: doc === OWN ? withMovesUp(ops, await members()) : ops,
|
|
87
95
|
after: async () => {},
|
|
88
96
|
}),
|
|
89
97
|
asked: async (_ws: Socket, m: Asked & { id: number }): Promise<object> => ({
|
|
@@ -92,21 +100,22 @@ export async function groupOf({
|
|
|
92
100
|
}),
|
|
93
101
|
tokenOf: async (_member: number): Promise<string | null> => null,
|
|
94
102
|
whoIs: async (_token: string): Promise<Who | null> => null,
|
|
103
|
+
heard: async () => {},
|
|
95
104
|
};
|
|
96
105
|
if (!rootToken) return open;
|
|
97
106
|
|
|
98
107
|
const keys = await openKeys(db, rootToken, { from: keysFile, rename: lasts });
|
|
99
|
-
const bdfl = () => bdflOf(members())!.id;
|
|
100
|
-
const nameOf = (id: number) => members().find((m) => m.id === id)?.name ?? "someone";
|
|
101
|
-
keys.keepOnly(members().map((m) => m.id));
|
|
108
|
+
const bdfl = async () => bdflOf(await members())!.id;
|
|
109
|
+
const nameOf = async (id: number) => (await members()).find((m) => m.id === id)?.name ?? "someone";
|
|
110
|
+
await keys.keepOnly((await members()).map((m) => m.id));
|
|
102
111
|
// a link for each member who has none; a key this ROOT_TOKEN can't open is left as it is, and said
|
|
103
|
-
for (const m of members()) if (m.id !== bdfl() && !keys.kept(m.id)) await keys.tokenOf(m.id);
|
|
104
|
-
const shut = keys.unopened();
|
|
112
|
+
for (const m of await members()) if (m.id !== (await bdfl()) && !(await keys.kept(m.id))) await keys.tokenOf(m.id);
|
|
113
|
+
const shut = await keys.unopened();
|
|
105
114
|
if (shut)
|
|
106
115
|
console.warn(
|
|
107
116
|
`${shut} member${shut === 1 ? "'s link doesn't" : "s' links don't"} open with this ROOT_TOKEN (it was replaced, or this is the wrong .env): each is replaced only when it's next asked for`,
|
|
108
117
|
);
|
|
109
|
-
const tokenOf = async (member: number) => (member === bdfl() ? rootToken : await keys.tokenOf(member));
|
|
118
|
+
const tokenOf = async (member: number) => (member === (await bdfl()) ? rootToken : await keys.tokenOf(member));
|
|
110
119
|
|
|
111
120
|
const cookie = (req: Request, value: string, maxAge: number) =>
|
|
112
121
|
new Bun.Cookie(COOKIE, value, {
|
|
@@ -122,9 +131,9 @@ export async function groupOf({
|
|
|
122
131
|
const tokenIn = (req: Request) => new Bun.CookieMap(req.headers.get("cookie") ?? "").get(COOKIE) ?? undefined;
|
|
123
132
|
async function whoIs(token: string | undefined): Promise<Who | null> {
|
|
124
133
|
if (!token) return null;
|
|
125
|
-
if (token === rootToken) return { member: bdfl(), token };
|
|
134
|
+
if (token === rootToken) return { member: await bdfl(), token };
|
|
126
135
|
const who = await keys.whoIs(token);
|
|
127
|
-
return who && members().some((m) => m.id === who.member) ? { ...who, token } : null;
|
|
136
|
+
return who && (await members()).some((m) => m.id === who.member) ? { ...who, token } : null;
|
|
128
137
|
}
|
|
129
138
|
|
|
130
139
|
// The pages open, to tell each member what's theirs, and to close what no longer opens.
|
|
@@ -139,23 +148,23 @@ export async function groupOf({
|
|
|
139
148
|
say(ws, {
|
|
140
149
|
doc: OWN,
|
|
141
150
|
me: who.member,
|
|
142
|
-
bdfl: bdfl(),
|
|
143
|
-
holds: holds?.(who.member) ?? [],
|
|
151
|
+
bdfl: await bdfl(),
|
|
152
|
+
holds: (await holds?.(who.member)) ?? [],
|
|
144
153
|
agent: who.agent?.label ?? null,
|
|
145
154
|
links: mine ? mine.links : {},
|
|
146
155
|
agents: mine ? mine.agents : [],
|
|
147
|
-
notice: (!who.agent && keys.notice(who.member)) || null,
|
|
156
|
+
notice: (!who.agent && (await keys.notice(who.member))) || null,
|
|
148
157
|
});
|
|
149
158
|
}
|
|
150
159
|
}
|
|
151
160
|
async function held(member: number, origin: string) {
|
|
152
161
|
const links: Record<number, string> = {};
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
162
|
+
const top = await bdfl();
|
|
163
|
+
for (const m of await members())
|
|
164
|
+
if (m.id === member || m.inviter === member || member === top) links[m.id] = linkTo(origin, await tokenOf(m.id));
|
|
156
165
|
return {
|
|
157
166
|
links,
|
|
158
|
-
agents: keys.agents(member).map((a) => ({ id: a.id, label: a.label, link: doorTo(origin, a.token) })),
|
|
167
|
+
agents: (await keys.agents(member)).map((a) => ({ id: a.id, label: a.label, link: doorTo(origin, a.token) })),
|
|
159
168
|
};
|
|
160
169
|
}
|
|
161
170
|
const close = (gone: (who: Who) => boolean, why: object, code: number) => {
|
|
@@ -211,10 +220,10 @@ export async function groupOf({
|
|
|
211
220
|
},
|
|
212
221
|
|
|
213
222
|
/** A page opening: closed, if who it's for has gone since it knocked. */
|
|
214
|
-
admit(ws: Socket) {
|
|
223
|
+
async admit(ws: Socket) {
|
|
215
224
|
const who = ws.data.who;
|
|
216
|
-
if (who && !members().some((m) => m.id === who.member)) return void ws.close(4003, "removed");
|
|
217
225
|
sockets.add(ws);
|
|
226
|
+
if (who && !(await members()).some((m) => m.id === who.member)) (sockets.delete(ws), ws.close(4003, "removed"));
|
|
218
227
|
},
|
|
219
228
|
/** A document a page subscribes to, and sent it: a page holding the members is told what's its (and
|
|
220
229
|
* only it: nothing of anyone else's has changed). */
|
|
@@ -224,33 +233,34 @@ export async function groupOf({
|
|
|
224
233
|
left: (ws: Socket) => void sockets.delete(ws),
|
|
225
234
|
|
|
226
235
|
/** A write, as the network allows it: refused, or its ops (a removal bringing those who move up) and what follows. */
|
|
227
|
-
write(doc: string, ops: Op[], who: Who | null, why?: string): Write {
|
|
236
|
+
async write(doc: string, ops: Op[], who: Who | null, why?: string): Promise<Write> {
|
|
228
237
|
if (!who) return { refused: "sign in with your link first" };
|
|
238
|
+
const all = await members();
|
|
229
239
|
const refused =
|
|
230
|
-
mayWrite(who,
|
|
231
|
-
(doc === OWN ? (ops.map((o) => trust(o, who.member,
|
|
240
|
+
mayWrite(who, all, doc === OWN) ??
|
|
241
|
+
(doc === OWN ? (ops.map((o) => trust(o, who.member, all, why)).find((r) => r !== null) ?? null) : null);
|
|
232
242
|
if (refused) return { refused };
|
|
233
243
|
if (doc !== OWN) return { ops, after: async () => {} };
|
|
234
|
-
const before = structuredClone(
|
|
235
|
-
by = nameOf(who.member);
|
|
244
|
+
const before = structuredClone(all),
|
|
245
|
+
by = await nameOf(who.member);
|
|
236
246
|
const removed = (o: Op) => o.op === "remove" && parts(o.path).field === undefined;
|
|
237
247
|
const farewell = `${by} removed you from the group: ${why}`;
|
|
238
248
|
return {
|
|
239
|
-
ops: withMovesUp(ops,
|
|
249
|
+
ops: withMovesUp(ops, all),
|
|
240
250
|
// with the write: a farewell for those removed, their keys gone; a notice for whoever was made
|
|
241
251
|
// read-only or given it back. (A new member's link is made when it's first asked for: an id is
|
|
242
252
|
// never handed out twice, so no old one can be waiting for it.)
|
|
243
|
-
within(applied: Op[]) {
|
|
253
|
+
async within(applied: Op[]) {
|
|
244
254
|
for (const o of applied) {
|
|
245
255
|
const { key, field } = parts(o.path),
|
|
246
256
|
them = Number(key);
|
|
247
|
-
if (removed(o)) keys.remove(them, farewell);
|
|
257
|
+
if (removed(o)) await keys.remove(them, farewell);
|
|
248
258
|
// (made read-only by another when they are already is no news to them)
|
|
249
259
|
else if (
|
|
250
260
|
field === "readonly" &&
|
|
251
261
|
(o.op === "replace") !== (before.find((m) => m.id === them)?.readonly !== undefined)
|
|
252
262
|
)
|
|
253
|
-
keys.tell(them, { kind: o.op === "replace" ? "read-only" : "restored", why: why ?? "", by });
|
|
263
|
+
await keys.tell(them, { kind: o.op === "replace" ? "read-only" : "restored", why: why ?? "", by });
|
|
254
264
|
}
|
|
255
265
|
},
|
|
256
266
|
// once it has: the pages of those removed told why, and closed; every page holding the members told
|
|
@@ -269,25 +279,41 @@ export async function groupOf({
|
|
|
269
279
|
const who = ws.data.who,
|
|
270
280
|
no = (why: string) => ({ refused: m.id, why });
|
|
271
281
|
if (!who) return no("sign in with your link first");
|
|
272
|
-
const refused = mayAsk(m, who, members(), keys.agents(who.member).length);
|
|
282
|
+
const refused = mayAsk(m, who, await members(), (await keys.agents(who.member)).length);
|
|
273
283
|
if (refused) return no(refused);
|
|
274
|
-
|
|
284
|
+
let closing = false;
|
|
285
|
+
if (m.closeNotice) await keys.closeNotice(who.member);
|
|
275
286
|
else if (m.relink !== undefined) {
|
|
276
287
|
const link = linkTo(ws.data.origin, await keys.replace(m.relink));
|
|
277
288
|
// your own: the new link told to this page alone, before the page is closed with the rest
|
|
278
289
|
if (m.relink === who.member) say(ws, { doc: OWN, link });
|
|
279
|
-
|
|
290
|
+
// (the page that asked, once it has its answer)
|
|
291
|
+
const gone = (w: Who) => w.member === m.relink && !w.agent;
|
|
292
|
+
close((w) => gone(w) && w !== who, { replaced: true }, 4001);
|
|
293
|
+
closing = gone(who);
|
|
280
294
|
} else if (m.agent !== undefined) await keys.makeAgent(who.member, m.agent);
|
|
281
295
|
else if (m.takeBack !== undefined) {
|
|
282
|
-
if (!keys.agents(who.member).some((a) => a.id === m.takeBack))
|
|
296
|
+
if (!(await keys.agents(who.member)).some((a) => a.id === m.takeBack))
|
|
283
297
|
return no("that's not one of your agents' links");
|
|
284
|
-
keys.takeBack(m.takeBack);
|
|
298
|
+
await keys.takeBack(m.takeBack);
|
|
285
299
|
close((w) => w.agent?.id === m.takeBack, { takenBack: true }, 4002);
|
|
286
300
|
} else return no("nothing asked");
|
|
287
301
|
await tell();
|
|
302
|
+
// the page that asked closed once it has its answer: a turn on, as the answer goes first
|
|
303
|
+
if (closing)
|
|
304
|
+
setTimeout(() => sockets.has(ws) && (say(ws, { doc: OWN, replaced: true }), ws.close(4001, "signed out")));
|
|
288
305
|
return { done: m.id };
|
|
289
306
|
},
|
|
290
307
|
|
|
308
|
+
/** The members changed on another server on the database: each page whose link no longer opens (its member
|
|
309
|
+
* removed, its link replaced or taken back) closed, and every page holding the members told. */
|
|
310
|
+
async heard() {
|
|
311
|
+
const gone = new Set<Socket>();
|
|
312
|
+
for (const ws of sockets) if (ws.data.who && !(await whoIs(ws.data.who.token))) gone.add(ws);
|
|
313
|
+
for (const ws of gone)
|
|
314
|
+
(say(ws, { doc: OWN, farewell: "This link no longer opens." }), ws.close(4003, "signed out"));
|
|
315
|
+
await tell();
|
|
316
|
+
},
|
|
291
317
|
/** A member's link token, for the terminal and the tests. */
|
|
292
318
|
tokenOf,
|
|
293
319
|
/** Who a link's token is (a member, and the agent's link if it's one), or no one: a removed member's, or none. */
|
package/src/members/keys.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
// every device until someone replaces it in the page. Root's token is ROOT_TOKEN itself, and is never
|
|
5
5
|
// kept. A token is found by its hash. A removed member's token keeps their farewell, shown once. A
|
|
6
6
|
// notice (made read-only, or given it back) waits for its member until they close it. An agent's link
|
|
7
|
-
// is a member's, with a label.
|
|
8
|
-
//
|
|
7
|
+
// is a member's, with a label. The database is all there is of them: nothing is held in memory, so every server on
|
|
8
|
+
// one database (Postgres) signs in the same links, and a member removed is gone from all of them at once.
|
|
9
9
|
|
|
10
10
|
import type { Engine } from "../engine.ts";
|
|
11
11
|
import { existsSync, renameSync } from "node:fs";
|
|
@@ -57,13 +57,17 @@ export async function openKeys(
|
|
|
57
57
|
),
|
|
58
58
|
);
|
|
59
59
|
|
|
60
|
-
if (from && existsSync(from) && !db.query("SELECT 1 FROM member_keys").get()) {
|
|
60
|
+
if (from && existsSync(from) && !(await db.query("SELECT 1 FROM member_keys").get())) {
|
|
61
61
|
const kept: Kept = await Bun.file(from).json();
|
|
62
|
-
db.transaction(() => {
|
|
62
|
+
await db.transaction(async () => {
|
|
63
63
|
for (const [m, k] of Object.entries(kept.members))
|
|
64
|
-
db.run("INSERT INTO member_keys (id, hash, box) VALUES (?, ?, ?)", [
|
|
64
|
+
await db.run("INSERT INTO member_keys (id, hash, box) VALUES (?, ?, ?)", [
|
|
65
|
+
Number(m),
|
|
66
|
+
k.hash,
|
|
67
|
+
JSON.stringify(k.box),
|
|
68
|
+
]);
|
|
65
69
|
for (const a of Object.values(kept.agents))
|
|
66
|
-
db.run("INSERT INTO agents (id, member, label, hash, box) VALUES (?, ?, ?, ?, ?)", [
|
|
70
|
+
await db.run("INSERT INTO agents (id, member, label, hash, box) VALUES (?, ?, ?, ?, ?)", [
|
|
67
71
|
Number(a.id),
|
|
68
72
|
a.member,
|
|
69
73
|
a.label,
|
|
@@ -71,150 +75,162 @@ export async function openKeys(
|
|
|
71
75
|
JSON.stringify(a.box),
|
|
72
76
|
]);
|
|
73
77
|
for (const [h, words] of Object.entries(kept.farewells))
|
|
74
|
-
db.run("INSERT INTO farewells (id, hash, words) VALUES (?, ?, ?)", [
|
|
78
|
+
await db.run("INSERT INTO farewells (id, hash, words) VALUES (?, ?, ?)", [
|
|
79
|
+
await nextId(db, "farewells"),
|
|
80
|
+
h,
|
|
81
|
+
words,
|
|
82
|
+
]);
|
|
75
83
|
for (const [m, n] of Object.entries(kept.notices))
|
|
76
|
-
db.run("INSERT INTO notices (id, kind, why, said_by) VALUES (?, ?, ?, ?)", [
|
|
77
|
-
|
|
84
|
+
await db.run("INSERT INTO notices (id, kind, why, said_by) VALUES (?, ?, ?, ?)", [
|
|
85
|
+
Number(m),
|
|
86
|
+
n.kind,
|
|
87
|
+
n.why,
|
|
88
|
+
n.by,
|
|
89
|
+
]);
|
|
90
|
+
});
|
|
78
91
|
if (rename) renameSync(from, `${from}.imported`);
|
|
79
92
|
}
|
|
80
93
|
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
//
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
() => {},
|
|
90
|
-
);
|
|
91
|
-
for (const r of db.query("SELECT id, box FROM agents").all() as { id: number; box: string }[])
|
|
92
|
-
await unseal(JSON.parse(r.box)).then(
|
|
93
|
-
(t) => agentTokens.set(String(r.id), t),
|
|
94
|
-
() => {},
|
|
95
|
-
);
|
|
94
|
+
// a token sealed with another ROOT_TOKEN can't be opened, and opening never deletes or replaces it (a command, or a
|
|
95
|
+
// start, with the wrong .env would lose every link): it signs in nobody, and is replaced only when its member's link
|
|
96
|
+
// is next asked for, as a rotated ROOT_TOKEN means
|
|
97
|
+
const opened = (box: string) => unseal(JSON.parse(box)).catch(() => null);
|
|
98
|
+
const tokenAt = async (member: number) => {
|
|
99
|
+
const r = (await db.query("SELECT box FROM member_keys WHERE id = ?").get(member)) as { box: string } | null;
|
|
100
|
+
return r ? opened(r.box) : null;
|
|
101
|
+
};
|
|
96
102
|
const making = new Map<number, Promise<string>>();
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
/** A member's agents' links, each with its token: one this ROOT_TOKEN can't open is none. */
|
|
104
|
+
const agentsOf = async (member: number) => {
|
|
105
|
+
const rows = (await db.query("SELECT id, label, box FROM agents WHERE member = ? ORDER BY id").all(member)) as {
|
|
106
|
+
id: number;
|
|
107
|
+
label: string;
|
|
108
|
+
box: string;
|
|
109
|
+
}[];
|
|
110
|
+
const all: { id: string; label: string; token: string }[] = [];
|
|
111
|
+
for (const r of rows) {
|
|
112
|
+
const token = await opened(r.box);
|
|
113
|
+
if (token) all.push({ id: String(r.id), label: r.label, token });
|
|
114
|
+
}
|
|
115
|
+
return all;
|
|
116
|
+
};
|
|
104
117
|
|
|
105
118
|
return {
|
|
106
|
-
/** A member's token: their link's, made if they have none. */
|
|
119
|
+
/** A member's token: their link's, made if they have none (or one this ROOT_TOKEN can't open). */
|
|
107
120
|
async tokenOf(member: number): Promise<string> {
|
|
108
|
-
|
|
121
|
+
const had = await tokenAt(member);
|
|
122
|
+
if (had) return had;
|
|
109
123
|
// one made at a time: two asking at once (two pages told) are both given the one link
|
|
110
124
|
const made = making.get(member) ?? this.replace(member).finally(() => making.delete(member));
|
|
111
125
|
making.set(member, made);
|
|
112
126
|
return made;
|
|
113
127
|
},
|
|
114
128
|
/** Whether a member has a key kept, opened or not. */
|
|
115
|
-
kept: (member: number) => !!db.query("SELECT 1 FROM member_keys WHERE id = ?").get(member),
|
|
129
|
+
kept: async (member: number) => !!(await db.query("SELECT 1 FROM member_keys WHERE id = ?").get(member)),
|
|
116
130
|
/** How many keys are kept that this ROOT_TOKEN can't open. */
|
|
117
|
-
unopened
|
|
131
|
+
async unopened() {
|
|
132
|
+
let n = 0;
|
|
133
|
+
for (const r of (await db.query("SELECT box FROM member_keys").all()) as { box: string }[])
|
|
134
|
+
if (!(await opened(r.box))) n++;
|
|
135
|
+
return n;
|
|
136
|
+
},
|
|
118
137
|
/** Keys for these members alone: anyone else's (the document and the keys out of step) are dropped. */
|
|
119
|
-
keepOnly(ids: number[]) {
|
|
138
|
+
async keepOnly(ids: number[]) {
|
|
120
139
|
// (0 is no one's: a list with none in it is still a list)
|
|
121
140
|
const kept = [0, ...ids.map(Number)].join(", ");
|
|
122
|
-
db.run(`DELETE FROM member_keys WHERE id NOT IN (${kept})`);
|
|
123
|
-
db.run(`DELETE FROM notices WHERE id NOT IN (${kept})`);
|
|
124
|
-
db.run(`DELETE FROM agents WHERE member NOT IN (${kept})`);
|
|
125
|
-
for (const m of tokens.keys()) if (!ids.includes(m)) tokens.delete(m);
|
|
126
|
-
for (const id of agentTokens.keys())
|
|
127
|
-
if (!db.query("SELECT 1 FROM agents WHERE id = ?").get(Number(id))) agentTokens.delete(id);
|
|
141
|
+
await db.run(`DELETE FROM member_keys WHERE id NOT IN (${kept})`);
|
|
142
|
+
await db.run(`DELETE FROM notices WHERE id NOT IN (${kept})`);
|
|
143
|
+
await db.run(`DELETE FROM agents WHERE member NOT IN (${kept})`);
|
|
128
144
|
},
|
|
129
145
|
/** A member's token if they have one, making none. */
|
|
130
|
-
peek: (member: number) =>
|
|
146
|
+
peek: (member: number) => tokenAt(member),
|
|
131
147
|
/** A new link for a member: the old one stops opening. */
|
|
132
148
|
async replace(member: number) {
|
|
133
149
|
const token = fresh(),
|
|
134
150
|
sealed = [await hash(token), JSON.stringify(await seal(token))];
|
|
135
|
-
db.run(
|
|
151
|
+
await db.run(
|
|
136
152
|
"INSERT INTO member_keys (id, hash, box) VALUES (?, ?, ?) ON CONFLICT (id) DO UPDATE SET hash = excluded.hash, box = excluded.box",
|
|
137
153
|
[member, ...sealed],
|
|
138
154
|
);
|
|
139
|
-
tokens.set(member, token);
|
|
140
155
|
return token;
|
|
141
156
|
},
|
|
142
157
|
/** Who a token signs in: a member by their link, or by an agent's link they made; or nobody. */
|
|
143
158
|
async whoIs(token: string): Promise<{ member: number; agent?: Agent } | null> {
|
|
144
159
|
const h = await hash(token);
|
|
145
|
-
const m = db.query("SELECT id FROM member_keys WHERE hash = ?").get(h) as {
|
|
146
|
-
|
|
147
|
-
|
|
160
|
+
const m = (await db.query("SELECT id, box FROM member_keys WHERE hash = ?").get(h)) as {
|
|
161
|
+
id: number;
|
|
162
|
+
box: string;
|
|
163
|
+
} | null;
|
|
164
|
+
if (m && (await opened(m.box))) return { member: m.id };
|
|
165
|
+
const a = (await db.query("SELECT id, member, label, box FROM agents WHERE hash = ?").get(h)) as {
|
|
148
166
|
id: number;
|
|
149
167
|
member: number;
|
|
150
168
|
label: string;
|
|
169
|
+
box: string;
|
|
151
170
|
} | null;
|
|
152
|
-
return a &&
|
|
171
|
+
return a && (await opened(a.box))
|
|
153
172
|
? { member: a.member, agent: { id: String(a.id), member: a.member, label: a.label } }
|
|
154
173
|
: null;
|
|
155
174
|
},
|
|
156
|
-
/** A member gone: their link keeps the words left for them, shown once; their agents' links go.
|
|
157
|
-
|
|
158
|
-
|
|
175
|
+
/** A member gone: their link keeps the words left for them, shown once; their agents' links go. In the write
|
|
176
|
+
* that removes them, as a database's alone: nothing else holds a key. */
|
|
177
|
+
async remove(member: number, farewell: string) {
|
|
178
|
+
const k = (await db.query("SELECT hash FROM member_keys WHERE id = ?").get(member)) as { hash: string } | null;
|
|
159
179
|
if (k)
|
|
160
|
-
db.run("INSERT INTO farewells (id, hash, words) VALUES (?, ?, ?)", [
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
180
|
+
await db.run("INSERT INTO farewells (id, hash, words) VALUES (?, ?, ?)", [
|
|
181
|
+
await nextId(db, "farewells"),
|
|
182
|
+
k.hash,
|
|
183
|
+
farewell,
|
|
184
|
+
]);
|
|
185
|
+
await db.run("DELETE FROM member_keys WHERE id = ?", [member]);
|
|
186
|
+
await db.run("DELETE FROM notices WHERE id = ?", [member]);
|
|
187
|
+
await db.run("DELETE FROM agents WHERE member = ?", [member]);
|
|
166
188
|
},
|
|
167
189
|
/** The words left for whoever held this token, once: after this, it opens nothing. */
|
|
168
190
|
async farewell(token: string) {
|
|
169
191
|
const h = await hash(token);
|
|
170
|
-
const f = db.query("SELECT words FROM farewells WHERE hash = ?").get(h) as { words: string } | null;
|
|
192
|
+
const f = (await db.query("SELECT words FROM farewells WHERE hash = ?").get(h)) as { words: string } | null;
|
|
171
193
|
if (!f) return null;
|
|
172
|
-
db.run("DELETE FROM farewells WHERE hash = ?", [h]);
|
|
194
|
+
await db.run("DELETE FROM farewells WHERE hash = ?", [h]);
|
|
173
195
|
return f.words;
|
|
174
196
|
},
|
|
175
|
-
notice(member: number): Notice | undefined {
|
|
176
|
-
const n = db.query("SELECT kind, why, said_by FROM notices WHERE id = ?").get(member) as {
|
|
197
|
+
async notice(member: number): Promise<Notice | undefined> {
|
|
198
|
+
const n = (await db.query("SELECT kind, why, said_by FROM notices WHERE id = ?").get(member)) as {
|
|
177
199
|
kind: Notice["kind"];
|
|
178
200
|
why: string;
|
|
179
201
|
said_by: string;
|
|
180
202
|
} | null;
|
|
181
203
|
return n ? { kind: n.kind, why: n.why, by: n.said_by } : undefined;
|
|
182
204
|
},
|
|
183
|
-
tell(member: number, notice: Notice)
|
|
205
|
+
tell: (member: number, notice: Notice) =>
|
|
184
206
|
db.run(
|
|
185
207
|
"INSERT INTO notices (id, kind, why, said_by) VALUES (?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET kind = excluded.kind, why = excluded.why, said_by = excluded.said_by",
|
|
186
208
|
[member, notice.kind, notice.why, notice.by],
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
closeNotice(member: number) {
|
|
190
|
-
db.run("DELETE FROM notices WHERE id = ?", [member]);
|
|
191
|
-
},
|
|
209
|
+
),
|
|
210
|
+
closeNotice: (member: number) => db.run("DELETE FROM notices WHERE id = ?", [member]),
|
|
192
211
|
/** An agent's link for a member, labelled for its via. */
|
|
193
212
|
async makeAgent(member: number, label: string) {
|
|
194
213
|
const token = fresh(),
|
|
195
214
|
sealed = [await hash(token), JSON.stringify(await seal(token))];
|
|
196
|
-
const id = nextId(db, "agents");
|
|
197
|
-
db.run("INSERT INTO agents (id, member, label, hash, box) VALUES (?, ?, ?, ?, ?)", [
|
|
215
|
+
const id = await nextId(db, "agents");
|
|
216
|
+
await db.run("INSERT INTO agents (id, member, label, hash, box) VALUES (?, ?, ?, ?, ?)", [
|
|
198
217
|
id,
|
|
199
218
|
member,
|
|
200
219
|
label,
|
|
201
220
|
...sealed,
|
|
202
221
|
]);
|
|
203
|
-
agentTokens.set(String(id), token);
|
|
204
222
|
return String(id);
|
|
205
223
|
},
|
|
206
224
|
/** A member's agents' links, with their tokens. */
|
|
207
|
-
agents:
|
|
208
|
-
agentsOf(member).map(({ id, label }) => ({ id: String(id), label, token: agentTokens.get(String(id))! })),
|
|
225
|
+
agents: agentsOf,
|
|
209
226
|
/** An agent's link taken back: it opens nothing. */
|
|
210
|
-
takeBack(id: string) {
|
|
211
|
-
const a = db.query("SELECT id, member, label FROM agents WHERE id = ?").get(Number(id)) as {
|
|
227
|
+
async takeBack(id: string) {
|
|
228
|
+
const a = (await db.query("SELECT id, member, label FROM agents WHERE id = ?").get(Number(id))) as {
|
|
212
229
|
id: number;
|
|
213
230
|
member: number;
|
|
214
231
|
label: string;
|
|
215
232
|
} | null;
|
|
216
|
-
db.run("DELETE FROM agents WHERE id = ?", [Number(id)]);
|
|
217
|
-
agentTokens.delete(id);
|
|
233
|
+
await db.run("DELETE FROM agents WHERE id = ?", [Number(id)]);
|
|
218
234
|
return a ? { id: String(a.id), member: a.member, label: a.label } : null;
|
|
219
235
|
},
|
|
220
236
|
};
|
package/src/members/panel.css
CHANGED
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
box-sizing: border-box;
|
|
63
63
|
/* as wide as its words, as the screen allows: fixed at the middle, it would fit only the half to its right */
|
|
64
64
|
width: max-content;
|
|
65
|
-
max-width: min(
|
|
65
|
+
max-width: min(var(--size-said), calc(100% - 2 * var(--space-16)));
|
|
66
66
|
padding: var(--space-4) var(--space-4) var(--space-4) var(--space-16);
|
|
67
67
|
border-radius: 4px;
|
|
68
68
|
background: var(--ink);
|
|
@@ -77,38 +77,43 @@
|
|
|
77
77
|
.notice button {
|
|
78
78
|
color: inherit;
|
|
79
79
|
}
|
|
80
|
-
/* the group opens from the button that names you, under it, and flips to keep on the page
|
|
80
|
+
/* the group opens from the button that names you, under it, and flips to keep on the page; on a phone it's a
|
|
81
|
+
phone's sheet (src/surface.ts). Its title, the tabs, the way to invite and to sign out stay where they are, and
|
|
82
|
+
only the list of people (or agents) scrolls, however many there are */
|
|
81
83
|
.who {
|
|
82
84
|
anchor-name: --who;
|
|
83
85
|
}
|
|
84
|
-
/* the group is a sheet (src/parts.css), anchored under its button; on a phone, rising from the foot */
|
|
85
86
|
#group {
|
|
86
87
|
position-anchor: --who;
|
|
87
88
|
position-area: block-end span-inline-end;
|
|
88
89
|
position-try-fallbacks: flip-inline;
|
|
89
90
|
inset: auto;
|
|
90
|
-
margin:
|
|
91
|
+
margin: var(--space-4) 0 0;
|
|
91
92
|
}
|
|
92
|
-
@media (max-width: 40rem) {
|
|
93
|
-
#group {
|
|
94
|
-
position-area: none;
|
|
95
|
-
inset: auto 0 0;
|
|
96
|
-
margin: 0;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
/* the sheet holds still: its title, the tabs, the way to invite and to sign out stay where they are, and only the
|
|
100
|
-
list of people (or agents) scrolls, however many there are */
|
|
101
93
|
#group:popover-open {
|
|
102
94
|
display: flex;
|
|
103
95
|
flex-direction: column;
|
|
104
96
|
max-height: min(85dvh, 36rem);
|
|
105
97
|
overflow: hidden;
|
|
106
98
|
}
|
|
107
|
-
#group >
|
|
99
|
+
#group > header,
|
|
100
|
+
.leave {
|
|
108
101
|
flex: none;
|
|
109
102
|
}
|
|
110
|
-
.
|
|
111
|
-
|
|
103
|
+
.group {
|
|
104
|
+
display: flex;
|
|
105
|
+
flex-direction: column;
|
|
106
|
+
min-height: 0;
|
|
107
|
+
gap: var(--space-10);
|
|
108
|
+
}
|
|
109
|
+
wa-tab-group.alone::part(nav) {
|
|
110
|
+
display: none;
|
|
111
|
+
}
|
|
112
|
+
wa-tab-group::part(body) {
|
|
113
|
+
padding-top: var(--space-8);
|
|
114
|
+
}
|
|
115
|
+
/* the tab shown (Web Awesome hides the other, as long as nothing here says how it's drawn) */
|
|
116
|
+
:is(.people, .agents)[active] {
|
|
112
117
|
display: flex;
|
|
113
118
|
flex-direction: column;
|
|
114
119
|
flex: 0 1 auto;
|
|
@@ -187,8 +192,9 @@
|
|
|
187
192
|
margin-top: 6px;
|
|
188
193
|
border-top: 1px solid var(--line);
|
|
189
194
|
}
|
|
190
|
-
form input {
|
|
195
|
+
form wa-input {
|
|
191
196
|
flex: 1;
|
|
197
|
+
min-width: 0;
|
|
192
198
|
}
|
|
193
199
|
/* the way out, and all you can see to take with you, side by side */
|
|
194
200
|
.leave {
|