@blueshed/joined 0.3.0 → 0.3.2
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/reference.md +57 -18
- package/docs/design.md +93 -38
- package/docs/introduction.md +9 -9
- package/docs/light-and-shadow.svg +1 -0
- package/docs/links-and-trust.svg +1 -0
- package/docs/stories-to-code.svg +2 -1
- package/docs/wire-op.svg +1 -0
- package/examples/app.css +9 -0
- package/examples/cafe/cafe.css +87 -69
- package/examples/cafe/generated/staff.elements.ts +7 -2
- package/examples/cafe/generated/staff.rules.ts +3 -0
- package/examples/cafe/generated/supplies.rules.ts +5 -0
- package/examples/cafe/kitchen.css +11 -19
- package/examples/cafe/kitchen.ts +6 -2
- package/examples/cafe/migrations/002-guard-supplies.ts +15 -0
- package/examples/cafe/order.html +2 -1
- package/examples/cafe/order.ts +1 -1
- package/examples/cafe/staff.json +5 -0
- package/examples/cafe/stories.jsonl +4 -2
- package/examples/cafe/supplies.html +1 -0
- package/examples/cafe/supplies.ts +45 -28
- package/examples/cafe/till.ts +14 -10
- package/examples/cafe/views.ts +7 -5
- package/examples/chart/arches.ts +5 -3
- package/examples/chart/chart.css +7 -2
- package/examples/desk/desk.css +6 -10
- package/examples/desk/generated/elements.ts +1 -1
- package/examples/desk/index.html +1 -1
- package/examples/desk/lanes.css +21 -19
- package/examples/desk/stories.jsonl +1 -1
- package/examples/desk/tally.css +1 -1
- package/examples/docs.ts +7 -4
- package/examples/index.html +16 -7
- package/examples/notes/generated/queries.ts +8 -2
- package/examples/notes/index.html +1 -1
- package/examples/notes/stories.jsonl +3 -0
- package/examples/page.ts +9 -2
- package/examples/people/links.ts +7 -9
- package/examples/people/people.css +1 -1
- package/examples/people/view.ts +1 -1
- package/examples/trip/generated/elements.ts +1 -1
- package/examples/trip/stays.css +1 -1
- package/examples/trip/stays.ts +1 -1
- package/examples/trips/generated/elements.ts +1 -1
- package/examples/trips/trips.css +24 -23
- package/examples/welcome.css +41 -33
- package/package.json +1 -1
- package/src/attachment.ts +17 -2
- package/src/board.css +2 -1
- package/src/board.ts +4 -4
- package/src/cli.ts +14 -2
- package/src/compile.ts +255 -77
- package/src/definitions.ts +62 -14
- package/src/demo.css +2 -5
- package/src/draft.ts +10 -4
- package/src/drawer.ts +30 -4
- package/src/engine.ts +14 -5
- package/src/fields.ts +88 -15
- package/src/file-value.ts +12 -2
- package/src/files.ts +113 -35
- package/src/icon-names.ts +4 -0
- package/src/keep.ts +16 -3
- package/src/map.css +23 -16
- package/src/map.ts +3 -3
- package/src/mcp.ts +24 -3
- package/src/members/group.ts +41 -17
- package/src/members/keys.ts +3 -3
- package/src/members/panel.css +17 -18
- package/src/members/panel.ts +9 -2
- package/src/migrate.ts +58 -21
- package/src/migrations/005-change-kept.ts +7 -0
- package/src/month.css +6 -2
- package/src/month.ts +5 -5
- package/src/ops.ts +40 -1
- package/src/page.css +3 -2
- package/src/page.ts +64 -16
- package/src/parts.css +178 -86
- package/src/parts.ts +60 -20
- package/src/plane.css +9 -4
- package/src/plane.ts +4 -2
- package/src/reading.css +1 -5
- package/src/rows.css +2 -21
- package/src/rows.ts +123 -39
- package/src/server.ts +157 -30
- package/src/sheet.ts +4 -2
- package/src/slots.css +0 -4
- package/src/specs.ts +1 -1
- package/src/starter.ts +15 -1
- package/src/store.ts +40 -18
- package/src/surface.ts +7 -2
- package/src/time.ts +2 -0
- package/src/timeline.ts +14 -5
- package/src/together.ts +15 -17
- package/src/tokens.css +12 -3
- package/src/tokens.json +38 -7
- package/src/tool.ts +6 -7
- package/src/upkeep.ts +31 -4
- package/src/views.css +8 -9
- package/src/views.ts +23 -6
- package/src/week.css +1 -2
- package/src/week.ts +11 -7
- package/src/write.ts +130 -15
- package/starter/CHANGELOG.md +8 -0
- package/starter/CLAUDE.md +7 -1
- package/starter/bunfig +2 -1
- package/starter/githooks/pre-commit +13 -0
- package/starter/prettierignore +1 -0
- package/starter/tests/records.test.ts +40 -0
- package/starter/todo.jsonl +0 -0
package/src/files.ts
CHANGED
|
@@ -12,9 +12,13 @@
|
|
|
12
12
|
// who may put a file in a document may send one for it (the server asks that: `may`). A file no item names, nor any
|
|
13
13
|
// change kept (an undo puts it back), is let go by the store's upkeep once it's older than the history (and the week of
|
|
14
14
|
// backups, which may name it), so a file chosen and never saved goes too (`letGo`). A Postgres store's upkeep isn't
|
|
15
|
-
// joined's, so neither is letting its files go.
|
|
15
|
+
// joined's, so neither is letting its files go. A kind that keeps its files apart (`kind: { files: "apart" }`) keeps
|
|
16
|
+
// each instance's in a folder of its own, `book-1/<name>`: served at /files/book-1/<name> only to whoever may hold
|
|
17
|
+
// `book:1` and see an item naming it (as is any instance's file, from its folder or the shared one), and handed back
|
|
18
|
+
// with it (/export/book-1).
|
|
16
19
|
|
|
17
|
-
import { existsSync, readdirSync, statSync, unlinkSync } from "node:fs";
|
|
20
|
+
import { existsSync, readdirSync, renameSync, statSync, unlinkSync } from "node:fs";
|
|
21
|
+
import { folderOf } from "./file-value.ts";
|
|
18
22
|
import type { BunFile, S3File, S3Options } from "bun";
|
|
19
23
|
import type { Engine } from "./engine.ts";
|
|
20
24
|
import { fileName, MOST, TOO_BIG } from "./file-value.ts";
|
|
@@ -50,13 +54,18 @@ function markdown(named: string, bytes: Uint8Array) {
|
|
|
50
54
|
const SANDBOX = "sandbox; default-src 'none'; style-src 'unsafe-inline'";
|
|
51
55
|
/** A kept file's name: its content's SHA-256, and its type's extension (the compiler's file form, after `file:`). */
|
|
52
56
|
const NAME = /^[0-9a-f]{64}\.[a-z0-9]{1,8}$/;
|
|
57
|
+
/** A kept file's place: its name, in its instance's folder where its kind keeps them apart (`book-1/<name>`). */
|
|
58
|
+
const KEPT = /^(?:[a-z][a-z0-9-]*-\d+\/)?[0-9a-f]{64}\.[a-z0-9]{1,8}$/;
|
|
53
59
|
/** Each file a value names, in any text: a change's ops. */
|
|
54
60
|
const NAMED = new RegExp(`file:(${NAME.source.slice(1, -1)})`, "g");
|
|
55
61
|
|
|
56
|
-
/** Where the files are kept, a folder or a bucket, each asked the same
|
|
57
|
-
*
|
|
62
|
+
/** Where the files are kept, a folder or a bucket, each asked the same four things: a file by its name (to ask
|
|
63
|
+
* whether it's there, to send), one put whole (never seen half written, nor written over while it's being sent: a
|
|
64
|
+
* folder's is written beside it and renamed over it, a bucket's object is put whole), each file kept with when it
|
|
65
|
+
* was, and one let go. */
|
|
58
66
|
export type Files = {
|
|
59
67
|
file(name: string): BunFile | S3File;
|
|
68
|
+
put(name: string, bytes: Uint8Array): Promise<unknown>;
|
|
60
69
|
each(): AsyncIterable<{ name: string; at: number }>;
|
|
61
70
|
drop(name: string): Promise<unknown>;
|
|
62
71
|
};
|
|
@@ -68,10 +77,16 @@ export function filesAt(where: string, s3?: S3Options): Files {
|
|
|
68
77
|
if (!bucket)
|
|
69
78
|
return {
|
|
70
79
|
file: (name) => Bun.file(`${where}/${name}`),
|
|
71
|
-
|
|
80
|
+
put: async (name, bytes) => {
|
|
81
|
+
const part = `${where}/${name}.${crypto.randomUUID()}.part`;
|
|
82
|
+
await Bun.write(part, bytes);
|
|
83
|
+
renameSync(part, `${where}/${name}`);
|
|
84
|
+
},
|
|
85
|
+
// (none sent yet: no folder); an instance's folder's files by their place in it
|
|
72
86
|
async *each() {
|
|
73
|
-
for (const name of existsSync(where) ? readdirSync(where) : [])
|
|
74
|
-
|
|
87
|
+
for (const name of existsSync(where) ? readdirSync(where, { recursive: true }) : [])
|
|
88
|
+
if (statSync(`${where}/${name}`).isFile())
|
|
89
|
+
yield { name: String(name), at: statSync(`${where}/${name}`).mtimeMs };
|
|
75
90
|
},
|
|
76
91
|
drop: async (name) => unlinkSync(`${where}/${name}`),
|
|
77
92
|
};
|
|
@@ -79,6 +94,7 @@ export function filesAt(where: string, s3?: S3Options): Files {
|
|
|
79
94
|
prefix = bucket[2] && !bucket[2].endsWith("/") ? `${bucket[2]}/` : bucket[2]!;
|
|
80
95
|
return {
|
|
81
96
|
file: (name) => client.file(prefix + name),
|
|
97
|
+
put: (name, bytes) => client.file(prefix + name).write(bytes),
|
|
82
98
|
async *each() {
|
|
83
99
|
let continuationToken: string | undefined;
|
|
84
100
|
do {
|
|
@@ -97,7 +113,7 @@ export type Kept = { file: string } | { refused: string };
|
|
|
97
113
|
|
|
98
114
|
/** A file sent, kept by its content's hash in `files`: a photo made a WebP, an SVG and a PDF as they are, and a
|
|
99
115
|
* Markdown file as it is where it's `named` so; anything else refused. */
|
|
100
|
-
export async function keep(files: Files, bytes: Uint8Array, named = ""): Promise<Kept> {
|
|
116
|
+
export async function keep(files: Files, bytes: Uint8Array, named = "", folder = ""): Promise<Kept> {
|
|
101
117
|
if (!bytes.length) return { refused: "That file is empty." };
|
|
102
118
|
if (bytes.length > MOST) return { refused: TOO_BIG };
|
|
103
119
|
const head = new TextDecoder().decode(bytes.subarray(0, 1024));
|
|
@@ -124,7 +140,7 @@ export async function keep(files: Files, bytes: Uint8Array, named = ""): Promise
|
|
|
124
140
|
}
|
|
125
141
|
const name = `${new Bun.CryptoHasher("sha256").update(out).digest("hex")}.${ext}`;
|
|
126
142
|
// written, kept already or not: the same bytes, its age started again, so upkeep doesn't let it go before it's saved
|
|
127
|
-
await files.
|
|
143
|
+
await files.put(folder ? `${folder}/${name}` : name, out);
|
|
128
144
|
return { file: `file:${name}` };
|
|
129
145
|
}
|
|
130
146
|
|
|
@@ -144,10 +160,39 @@ export const putsFile = (
|
|
|
144
160
|
);
|
|
145
161
|
});
|
|
146
162
|
|
|
163
|
+
/** A file kept, served with its type: for good where anyone may be sent it, else kept by the browser alone. */
|
|
164
|
+
const serve = (name: string, file: BunFile | S3File, shared: boolean) =>
|
|
165
|
+
new Response(file.stream(), {
|
|
166
|
+
headers: {
|
|
167
|
+
"content-type": TYPES[name.split(".").at(-1)!] ?? "application/octet-stream",
|
|
168
|
+
"cache-control": shared ? "public, max-age=31536000, immutable" : "private, max-age=31536000, immutable",
|
|
169
|
+
"x-content-type-options": "nosniff",
|
|
170
|
+
...(name.endsWith(".svg") ? { "content-security-policy": SANDBOX } : {}),
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
/** A request's body, read as it comes, or null once it's more than `most` bytes: the rest is never read, so one sent
|
|
175
|
+
* in parts, its size unsaid, can't take the server's memory. */
|
|
176
|
+
export async function upTo(req: Request, most: number) {
|
|
177
|
+
const parts: Uint8Array[] = [];
|
|
178
|
+
let size = 0;
|
|
179
|
+
for await (const part of req.body ?? []) {
|
|
180
|
+
if ((size += part.length) > most) return null;
|
|
181
|
+
parts.push(part);
|
|
182
|
+
}
|
|
183
|
+
return new Uint8Array(Buffer.concat(parts));
|
|
184
|
+
}
|
|
185
|
+
|
|
147
186
|
/** The routes: a file sent for a document (`POST /files?doc=board:1`, its bytes the body, `&name=` its file's name), by whoever `may` lets (why
|
|
148
|
-
* not, or null), one that says it's too big refused before it's read
|
|
149
|
-
* cached for good
|
|
150
|
-
|
|
187
|
+
* not, or null), one that says it's too big refused before it's read, kept in its instance's folder where `apart`
|
|
188
|
+
* says its kind keeps them so; a file kept, by its name, with its type, cached for good; and an instance's file, by its
|
|
189
|
+
* folder and name (`/files/book-1/<name>`), to whoever `holds` lets hold it, from its folder or the shared one. */
|
|
190
|
+
export function routes(
|
|
191
|
+
files: Files,
|
|
192
|
+
may: (req: Request, doc: string) => Promise<string | null>,
|
|
193
|
+
holds: (req: Request, doc: string, name: string) => Promise<string | null>,
|
|
194
|
+
apart: (doc: string) => Promise<boolean>,
|
|
195
|
+
) {
|
|
151
196
|
return {
|
|
152
197
|
"/files": {
|
|
153
198
|
POST: async (req: Request) => {
|
|
@@ -155,8 +200,16 @@ export function routes(files: Files, may: (req: Request, doc: string) => Promise
|
|
|
155
200
|
if (why) return Response.json({ refused: why }, { status: 403 });
|
|
156
201
|
if (Number(req.headers.get("content-length")) > MOST)
|
|
157
202
|
return Response.json({ refused: TOO_BIG }, { status: 413 });
|
|
158
|
-
const url = new URL(req.url)
|
|
159
|
-
|
|
203
|
+
const url = new URL(req.url),
|
|
204
|
+
doc = url.searchParams.get("doc")!,
|
|
205
|
+
bytes = await upTo(req, MOST);
|
|
206
|
+
if (!bytes) return Response.json({ refused: TOO_BIG }, { status: 413 });
|
|
207
|
+
const kept = await keep(
|
|
208
|
+
files,
|
|
209
|
+
bytes,
|
|
210
|
+
url.searchParams.get("name") ?? "",
|
|
211
|
+
(await apart(doc)) ? folderOf(doc) : "",
|
|
212
|
+
);
|
|
160
213
|
return Response.json(kept, { status: "refused" in kept ? 422 : 200 });
|
|
161
214
|
},
|
|
162
215
|
},
|
|
@@ -164,35 +217,60 @@ export function routes(files: Files, may: (req: Request, doc: string) => Promise
|
|
|
164
217
|
const { name } = req.params,
|
|
165
218
|
file = files.file(name);
|
|
166
219
|
return NAME.test(name) && (await file.exists())
|
|
167
|
-
?
|
|
168
|
-
headers: {
|
|
169
|
-
"content-type": TYPES[name.split(".").at(-1)!] ?? "application/octet-stream",
|
|
170
|
-
"cache-control": "public, max-age=31536000, immutable",
|
|
171
|
-
"x-content-type-options": "nosniff",
|
|
172
|
-
...(name.endsWith(".svg") ? { "content-security-policy": SANDBOX } : {}),
|
|
173
|
-
},
|
|
174
|
-
})
|
|
220
|
+
? serve(name, file, true)
|
|
175
221
|
: new Response("Not found", { status: 404 });
|
|
176
222
|
},
|
|
223
|
+
"/files/:folder/:name": async (req: Request & { params: { folder: string; name: string } }) => {
|
|
224
|
+
const { folder, name } = req.params,
|
|
225
|
+
at = /^([a-z][a-z0-9-]*)-(\d+)$/.exec(folder),
|
|
226
|
+
code = new URL(req.url).searchParams.get("code");
|
|
227
|
+
if (!at || !NAME.test(name)) return new Response("Not found", { status: 404 });
|
|
228
|
+
const why = await holds(
|
|
229
|
+
req,
|
|
230
|
+
`${at[1]}:${at[2]}${code === null ? "" : `?code=${encodeURIComponent(code)}`}`,
|
|
231
|
+
name,
|
|
232
|
+
);
|
|
233
|
+
if (why) return Response.json({ refused: why }, { status: 403 });
|
|
234
|
+
const own = files.file(`${folder}/${name}`),
|
|
235
|
+
shared = files.file(name);
|
|
236
|
+
return (await own.exists())
|
|
237
|
+
? serve(name, own, false)
|
|
238
|
+
: (await shared.exists())
|
|
239
|
+
? serve(name, shared, false)
|
|
240
|
+
: new Response("Not found", { status: 404 });
|
|
241
|
+
},
|
|
177
242
|
};
|
|
178
243
|
}
|
|
179
244
|
|
|
180
|
-
/** Each file the store names: in an item's file field now, or in a change it keeps, which an undo may
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
245
|
+
/** Each file the store names, by its place: in an item's file field now, or in a change it keeps, which an undo may
|
|
246
|
+
* put back; an instance's of a kind that keeps its files apart, in its folder (`book-1/<name>`). */
|
|
247
|
+
export async function named(
|
|
248
|
+
db: Engine,
|
|
249
|
+
rules: {
|
|
250
|
+
doc?: string;
|
|
251
|
+
collections: Record<string, { fields: Record<string, Type> }>;
|
|
252
|
+
kind?: { column: string; files?: "apart" } | null;
|
|
253
|
+
}[],
|
|
254
|
+
) {
|
|
255
|
+
const names = new Set<string>(),
|
|
256
|
+
apart = new Set(rules.flatMap((r) => (r.kind?.files === "apart" && r.doc ? [r.doc] : [])));
|
|
257
|
+
for (const { collections, kind } of rules) {
|
|
258
|
+
const column = kind?.files === "apart" ? kind.column : null;
|
|
184
259
|
for (const [coll, c] of Object.entries(collections))
|
|
185
260
|
for (const f of Object.keys(c.fields).filter((f) => c.fields[f] === "file"))
|
|
186
|
-
for (const { v } of (await db
|
|
187
|
-
v:
|
|
188
|
-
|
|
189
|
-
names.add(fileName(String(v)));
|
|
190
|
-
|
|
261
|
+
for (const { v, k } of (await db
|
|
262
|
+
.query(`SELECT "${f}" AS v, ${column ? `"${column}"` : "NULL"} AS k FROM "${coll}" WHERE "${f}" IS NOT NULL`)
|
|
263
|
+
.all()) as { v: unknown; k: unknown }[])
|
|
264
|
+
names.add(column ? `${column}-${k}/${fileName(String(v))}` : fileName(String(v)));
|
|
265
|
+
}
|
|
266
|
+
for (const { doc, ops } of (await db
|
|
191
267
|
.query(
|
|
192
|
-
"SELECT CAST(ops AS TEXT) || ' ' || COALESCE(CAST(inverse AS TEXT), '') AS ops FROM changes WHERE CAST(ops AS TEXT) LIKE '%file:%' OR CAST(inverse AS TEXT) LIKE '%file:%'",
|
|
268
|
+
"SELECT doc, CAST(ops AS TEXT) || ' ' || COALESCE(CAST(inverse AS TEXT), '') AS ops FROM changes WHERE CAST(ops AS TEXT) LIKE '%file:%' OR CAST(inverse AS TEXT) LIKE '%file:%'",
|
|
193
269
|
)
|
|
194
|
-
.all()) as { ops: unknown }[])
|
|
195
|
-
|
|
270
|
+
.all()) as { doc: string; ops: unknown }[]) {
|
|
271
|
+
const folder = apart.has(doc.split(":")[0]!) ? folderOf(doc) : "";
|
|
272
|
+
for (const [, name] of String(ops).matchAll(NAMED)) names.add(folder ? `${folder}/${name}` : name!);
|
|
273
|
+
}
|
|
196
274
|
return names;
|
|
197
275
|
}
|
|
198
276
|
|
|
@@ -200,6 +278,6 @@ export async function named(db: Engine, rules: { collections: Record<string, { f
|
|
|
200
278
|
export async function letGo(files: Files, names: Set<string>, before: number) {
|
|
201
279
|
let gone = 0;
|
|
202
280
|
for await (const { name, at } of files.each())
|
|
203
|
-
if (
|
|
281
|
+
if (KEPT.test(name) && !names.has(name) && at < before) (await files.drop(name), gone++);
|
|
204
282
|
return gone;
|
|
205
283
|
}
|
package/src/icon-names.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
ChartGantt,
|
|
10
10
|
Check,
|
|
11
11
|
CloudOff,
|
|
12
|
+
ChevronDown,
|
|
12
13
|
ChevronLeft,
|
|
13
14
|
ChevronRight,
|
|
14
15
|
Columns3,
|
|
@@ -31,6 +32,7 @@ import {
|
|
|
31
32
|
Map as Mapped,
|
|
32
33
|
MapPin,
|
|
33
34
|
Maximize2,
|
|
35
|
+
Minus,
|
|
34
36
|
Plus,
|
|
35
37
|
Search,
|
|
36
38
|
SquareKanban,
|
|
@@ -55,6 +57,7 @@ export const ICONS = {
|
|
|
55
57
|
calendar: CalendarDays,
|
|
56
58
|
"calendar-add": CalendarPlus,
|
|
57
59
|
check: Check,
|
|
60
|
+
choose: ChevronDown,
|
|
58
61
|
close: X,
|
|
59
62
|
copy: Copy,
|
|
60
63
|
down: ThumbsDown,
|
|
@@ -79,6 +82,7 @@ export const ICONS = {
|
|
|
79
82
|
"read-only": Lock,
|
|
80
83
|
search: Search,
|
|
81
84
|
"give-back": LockOpen,
|
|
85
|
+
less: Minus,
|
|
82
86
|
remove: Trash2,
|
|
83
87
|
stay: BedDouble,
|
|
84
88
|
timeline: ChartGantt,
|
package/src/keep.ts
CHANGED
|
@@ -13,9 +13,20 @@ const typing = (el: Element | null) =>
|
|
|
13
13
|
(el instanceof HTMLInputElement &&
|
|
14
14
|
!["button", "submit", "reset", "checkbox", "radio", "range", "color", "file", "image"].includes(el.type));
|
|
15
15
|
|
|
16
|
+
/** What in a row may hold the focus a hand gives it, in its order. */
|
|
17
|
+
const FOCUSABLE = "button, input, select, textarea, a[href], [tabindex], [contenteditable], wa-checkbox, wa-switch";
|
|
18
|
+
/** The focus a row made again keeps: on the row, or on the control at the same place in it (a tick just pressed),
|
|
19
|
+
* once it's drawn (a Web Awesome control takes the focus only then). */
|
|
20
|
+
function refocus(was: HTMLElement, now: HTMLElement, active: Element | null) {
|
|
21
|
+
const at = (
|
|
22
|
+
was === active ? now : now.querySelectorAll(FOCUSABLE)[[...was.querySelectorAll(FOCUSABLE)].indexOf(active!)]
|
|
23
|
+
) as (HTMLElement & { updateComplete?: Promise<unknown> }) | undefined;
|
|
24
|
+
void (at?.updateComplete ?? Promise.resolve()).then(() => at?.focus({ preventScroll: true }));
|
|
25
|
+
}
|
|
26
|
+
|
|
16
27
|
/** A display's list kept by key: a row is made again only when what it shows has changed (and never while
|
|
17
|
-
* something in it is being typed in: a button just pressed doesn't hold it
|
|
18
|
-
* are moved into their order, never made again. */
|
|
28
|
+
* something in it is being typed in: a button just pressed doesn't hold it, and keeps the focus in the row made
|
|
29
|
+
* again), one gone is taken away, and the rest are moved into their order, never made again. */
|
|
19
30
|
export function keep(list: HTMLElement, kept: Map<string, { shows: string; el: HTMLElement }>, rows: Row[]) {
|
|
20
31
|
const wanted = new Set(rows.map((r) => r.key));
|
|
21
32
|
const active = (list.getRootNode() as ShadowRoot | Document).activeElement;
|
|
@@ -24,8 +35,10 @@ export function keep(list: HTMLElement, kept: Map<string, { shows: string; el: H
|
|
|
24
35
|
let k = kept.get(row.key);
|
|
25
36
|
const busy = typing(active) && k?.el.contains(active);
|
|
26
37
|
if (!k || (k.shows !== row.shows && !busy)) {
|
|
27
|
-
const el = row.make()
|
|
38
|
+
const el = row.make(),
|
|
39
|
+
held = !!k && !!active && k.el.contains(active);
|
|
28
40
|
k?.el.replaceWith(el);
|
|
41
|
+
if (held) refocus(k!.el, el, active);
|
|
29
42
|
kept.set(row.key, (k = { shows: row.shows, el }));
|
|
30
43
|
}
|
|
31
44
|
if (list.children[i] !== k.el) list.insertBefore(k.el, list.children[i] ?? null);
|
package/src/map.css
CHANGED
|
@@ -18,28 +18,37 @@
|
|
|
18
18
|
height: calc(var(--space-64) * 4);
|
|
19
19
|
-webkit-user-drag: none;
|
|
20
20
|
}
|
|
21
|
+
/* a pin: what's tapped is its box, a finger wide on a phone or under a finger; what's drawn is its dot (::before) */
|
|
21
22
|
.pin {
|
|
23
|
+
--hit: var(--size-pin);
|
|
22
24
|
position: absolute;
|
|
23
25
|
z-index: 1;
|
|
24
26
|
box-sizing: border-box;
|
|
25
|
-
width: var(--
|
|
26
|
-
height: var(--
|
|
27
|
-
margin: calc(var(--
|
|
27
|
+
width: var(--hit);
|
|
28
|
+
height: var(--hit);
|
|
29
|
+
margin: calc(var(--hit) / -2) 0 0 calc(var(--hit) / -2);
|
|
28
30
|
padding: 0;
|
|
29
|
-
border:
|
|
31
|
+
border: 0;
|
|
30
32
|
border-radius: var(--radius-dot);
|
|
31
|
-
background:
|
|
32
|
-
box-shadow: var(--drop);
|
|
33
|
+
background: none;
|
|
33
34
|
cursor: pointer;
|
|
34
35
|
touch-action: none;
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
.pin::before {
|
|
38
|
+
content: "";
|
|
39
|
+
position: absolute;
|
|
40
|
+
inset: calc(50% - var(--size-pin) / 2);
|
|
41
|
+
box-sizing: border-box;
|
|
42
|
+
border: calc(var(--space-4) / 2) solid var(--card);
|
|
43
|
+
border-radius: var(--radius-dot);
|
|
44
|
+
background: var(--hue, var(--accent));
|
|
45
|
+
}
|
|
46
|
+
@media (max-width: 40rem), (pointer: coarse) {
|
|
47
|
+
.pin {
|
|
48
|
+
--hit: var(--size-finger);
|
|
42
49
|
}
|
|
50
|
+
}
|
|
51
|
+
@media (pointer: coarse) {
|
|
43
52
|
.zoom .icon-only {
|
|
44
53
|
width: var(--size-finger);
|
|
45
54
|
height: var(--size-finger);
|
|
@@ -47,6 +56,8 @@
|
|
|
47
56
|
}
|
|
48
57
|
.pin:is([open], [peeked]) {
|
|
49
58
|
z-index: 2;
|
|
59
|
+
}
|
|
60
|
+
.pin:is([open], [peeked])::before {
|
|
50
61
|
scale: 1.3;
|
|
51
62
|
outline: calc(var(--space-4) / 2) solid var(--ink);
|
|
52
63
|
}
|
|
@@ -94,12 +105,8 @@ small a {
|
|
|
94
105
|
border-inline-start: var(--size-stripe) solid var(--hue, var(--accent));
|
|
95
106
|
border-radius: var(--radius);
|
|
96
107
|
background: var(--card);
|
|
97
|
-
box-shadow: var(--drop);
|
|
98
108
|
color: var(--ink);
|
|
99
109
|
}
|
|
100
|
-
.peek[hidden] {
|
|
101
|
-
display: none;
|
|
102
|
-
}
|
|
103
110
|
.peek header {
|
|
104
111
|
display: flex;
|
|
105
112
|
align-items: center;
|
package/src/map.ts
CHANGED
|
@@ -21,7 +21,7 @@ export class JMap extends Reading {
|
|
|
21
21
|
static override sheets = [SHEET];
|
|
22
22
|
#map = el("div", { className: "map", role: "application", ariaLabel: "Map" });
|
|
23
23
|
#tiles = el("div", { className: "tiles", ariaHidden: "true" });
|
|
24
|
-
#peek = el("section", { className: "peek", hidden: true, ariaLive: "polite" });
|
|
24
|
+
#peek = el("section", { className: "peek", part: "peek", hidden: true, ariaLive: "polite" });
|
|
25
25
|
#pins = new Map<Item, HTMLButtonElement>();
|
|
26
26
|
#view: View = { z: 0, x: 0, y: 0 };
|
|
27
27
|
/** Where a person moved it: kept, so the pins coming and going don't move it again. */
|
|
@@ -44,7 +44,7 @@ export class JMap extends Reading {
|
|
|
44
44
|
this.#press = press;
|
|
45
45
|
const zoom = el(
|
|
46
46
|
"div",
|
|
47
|
-
{ className: "zoom" },
|
|
47
|
+
{ className: "zoom", part: "zoom" },
|
|
48
48
|
iconButton("zoom-in", "Nearer", () => step(1)),
|
|
49
49
|
iconButton("zoom-out", "Farther", () => step(-1)),
|
|
50
50
|
);
|
|
@@ -89,7 +89,7 @@ export class JMap extends Reading {
|
|
|
89
89
|
for (const { it, p } of placed) {
|
|
90
90
|
let pin = this.#pins.get(it);
|
|
91
91
|
if (!pin) {
|
|
92
|
-
pin = el("button", { type: "button", className: "pin" });
|
|
92
|
+
pin = el("button", { type: "button", className: "pin", part: "item pin" });
|
|
93
93
|
this.#hold(pin, it);
|
|
94
94
|
this.#pins.set(it, pin);
|
|
95
95
|
this.#map.insertBefore(pin, this.#peek);
|
package/src/mcp.ts
CHANGED
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
import { FORMS, SAID } from "./compile.ts";
|
|
17
17
|
import { skill, SKILLS } from "./skill.ts";
|
|
18
18
|
import type { Who } from "./members/group.ts";
|
|
19
|
-
import {
|
|
19
|
+
import { upTo } from "./files.ts";
|
|
20
|
+
import { isOp, MESSAGE, NTH, OPS, parts, type Op, type Type } from "./ops.ts";
|
|
20
21
|
import type { Doc } from "./store.ts";
|
|
21
22
|
import type { Rules } from "./write.ts";
|
|
22
23
|
|
|
@@ -199,7 +200,8 @@ function readOf(doc: Doc, args: { collection?: unknown; ids?: unknown }): Doc |
|
|
|
199
200
|
/** Who comes to a door that's anyone's: nobody, as a stranger's page is. */
|
|
200
201
|
const ANYONE: Who = { member: 0, token: "" };
|
|
201
202
|
|
|
202
|
-
/** The door: POST answers MCP's messages, by the agent its token names; a GET is refused, as no stream is kept
|
|
203
|
+
/** The door: POST answers MCP's messages, by the agent its token names; a GET is refused, as no stream is kept; an
|
|
204
|
+
* OPTIONS says it's POST, and nothing of another site's (no CORS), so a browser's page elsewhere is never let post. */
|
|
203
205
|
export function door(d: Door) {
|
|
204
206
|
const rpc = (id: unknown, result: unknown) => ({ jsonrpc: "2.0", id, result });
|
|
205
207
|
const fail = (id: unknown, code: number, message: string) => ({ jsonrpc: "2.0", id, error: { code, message } });
|
|
@@ -210,6 +212,7 @@ export function door(d: Door) {
|
|
|
210
212
|
name: string;
|
|
211
213
|
description: string;
|
|
212
214
|
inputSchema: Schema;
|
|
215
|
+
annotations: Schema;
|
|
213
216
|
run(args: any): Promise<Result> | Result;
|
|
214
217
|
}[] = [];
|
|
215
218
|
for (const r of d.rules) {
|
|
@@ -236,6 +239,8 @@ export function door(d: Door) {
|
|
|
236
239
|
name: `read_${r.doc}`,
|
|
237
240
|
description: `Read ${r.doc}, as you see it, with its version${colls.length ? `: whole, or one collection's items, or some of them by id` : ""}. It holds ${described(r)}.`,
|
|
238
241
|
inputSchema: { type: "object", properties: { ...key, ...part }, required: Object.keys(key) },
|
|
242
|
+
// a read changes nothing: a client needn't ask before it
|
|
243
|
+
annotations: { readOnlyHint: true },
|
|
239
244
|
run: async (args) => {
|
|
240
245
|
const no = await d.holds(name(args), who.member);
|
|
241
246
|
if (no) return refused(no);
|
|
@@ -273,9 +278,12 @@ export function door(d: Door) {
|
|
|
273
278
|
},
|
|
274
279
|
required: [...Object.keys(key), "ops"],
|
|
275
280
|
},
|
|
281
|
+
// a write that may replace or remove what's there undoes what someone made; one that only adds doesn't
|
|
282
|
+
annotations: { readOnlyHint: false, destructiveHint: ops.some((o) => o.op !== "add") },
|
|
276
283
|
run: async (args) => {
|
|
277
284
|
if (!Array.isArray(args.ops) || !args.ops.length)
|
|
278
285
|
return refused("ops: one write's ops, a list of at least one");
|
|
286
|
+
if (!args.ops.every(isOp)) return refused(OPS);
|
|
279
287
|
const no = await d.holds(name(args), who.member);
|
|
280
288
|
if (no) return refused(no);
|
|
281
289
|
const w = await d.write(
|
|
@@ -344,6 +352,7 @@ export function door(d: Door) {
|
|
|
344
352
|
|
|
345
353
|
return {
|
|
346
354
|
GET: () => new Response("this door is POST alone", { status: 405, headers: { allow: "POST" } }),
|
|
355
|
+
OPTIONS: () => new Response(null, { status: 204, headers: { allow: "POST" } }),
|
|
347
356
|
async POST(req: Request & { params: { token?: string } }) {
|
|
348
357
|
// an agent's link alone (never a member's own, a removed agent's or one unknown); where nobody's a member, anyone
|
|
349
358
|
const who = d.anyone ? ANYONE : await d.whoIs(req.params.token!);
|
|
@@ -354,9 +363,21 @@ export function door(d: Door) {
|
|
|
354
363
|
status: 401,
|
|
355
364
|
},
|
|
356
365
|
);
|
|
357
|
-
|
|
366
|
+
// read as it comes, no more than a page's message may be
|
|
367
|
+
const body = await upTo(req, MESSAGE);
|
|
368
|
+
if (!body)
|
|
369
|
+
return Response.json(fail(null, -32600, `that message is too big: under ${MESSAGE >> 20} MB, please`), {
|
|
370
|
+
status: 413,
|
|
371
|
+
});
|
|
372
|
+
let m: Message | undefined;
|
|
373
|
+
try {
|
|
374
|
+
m = JSON.parse(new TextDecoder().decode(body));
|
|
375
|
+
} catch {}
|
|
358
376
|
if (!m || typeof m !== "object")
|
|
359
377
|
return Response.json(fail(null, -32700, "that isn't a JSON-RPC message"), { status: 400 });
|
|
378
|
+
// a batch, said as one: MCP has had none since 2025-06-18, and an older client sends one message at a time too
|
|
379
|
+
if (Array.isArray(m))
|
|
380
|
+
return Response.json(fail(null, -32600, "a batch isn't answered: send one message at a time"), { status: 400 });
|
|
360
381
|
// modern, where the request says its version in its _meta: its headers saying the same, and a version it speaks
|
|
361
382
|
const version = m.params?._meta?.[`${MCP}protocolVersion`];
|
|
362
383
|
if (version !== undefined) {
|
package/src/members/group.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
// show them why, and then never. Without ROOT_TOKEN the group is open: anyone may read and write,
|
|
11
11
|
// and only the network's shape (who moves up) is kept.
|
|
12
12
|
|
|
13
|
+
import { timingSafeEqual } from "node:crypto";
|
|
13
14
|
import type { Engine } from "../engine.ts";
|
|
14
15
|
import { demoRoutes } from "./demo.ts";
|
|
15
16
|
import { parts, type Op } from "../ops.ts";
|
|
@@ -54,7 +55,10 @@ const farewellPage = (
|
|
|
54
55
|
words: string,
|
|
55
56
|
) => `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>joined</title>
|
|
56
57
|
<style>body{font:16px/1.5 system-ui,sans-serif;max-width:34rem;margin:3rem auto;padding:0 1rem;color:#222;background:#fff}</style></head>
|
|
57
|
-
<body><h1>You've been removed from the group</h1><p>${Bun.escapeHTML(words)}</p><p>This link won't open again.</p
|
|
58
|
+
<body><h1>You've been removed from the group</h1><p>${Bun.escapeHTML(words)}</p><p>This link won't open again.</p>
|
|
59
|
+
<script>fetch(location.pathname, { method: "POST" })</script></body></html>`;
|
|
60
|
+
/** What a sign-in's answer says of where it came from: nothing, so its link (its token) never leaves in a Referer. */
|
|
61
|
+
const NO_REFERRER = { "referrer-policy": "no-referrer" };
|
|
58
62
|
|
|
59
63
|
/** The group's doors, for a server that reads its documents by name and keeps them in `db`, where the
|
|
60
64
|
* keys are too (a keys file from before the database, `keysFile`, is brought in once). */
|
|
@@ -127,11 +131,19 @@ export async function groupOf({
|
|
|
127
131
|
}).serialize();
|
|
128
132
|
/** Home, this browser signed in by a token, for a year. */
|
|
129
133
|
const signedIn = (req: Request, token: string) =>
|
|
130
|
-
new Response(null, {
|
|
134
|
+
new Response(null, {
|
|
135
|
+
status: 302,
|
|
136
|
+
headers: { location: "/", "set-cookie": cookie(req, token, YEAR), ...NO_REFERRER },
|
|
137
|
+
});
|
|
138
|
+
/** Whether a token is ROOT_TOKEN, asked in the same time whatever it is (=== stops at the first that differs). */
|
|
139
|
+
const same = (token: string, root: string) => {
|
|
140
|
+
const [a, b] = [Buffer.from(token), Buffer.from(root)];
|
|
141
|
+
return a.length === b.length && root !== "" && timingSafeEqual(a, b);
|
|
142
|
+
};
|
|
131
143
|
const tokenIn = (req: Request) => new Bun.CookieMap(req.headers.get("cookie") ?? "").get(COOKIE) ?? undefined;
|
|
132
144
|
async function whoIs(token: string | undefined): Promise<Who | null> {
|
|
133
145
|
if (!token) return null;
|
|
134
|
-
if (token
|
|
146
|
+
if (rootToken && same(token, rootToken)) return { member: await bdfl(), token };
|
|
135
147
|
const who = await keys.whoIs(token);
|
|
136
148
|
return who && (await members()).some((m) => m.id === who.member) ? { ...who, token } : null;
|
|
137
149
|
}
|
|
@@ -174,20 +186,30 @@ export async function groupOf({
|
|
|
174
186
|
|
|
175
187
|
return {
|
|
176
188
|
routes: {
|
|
177
|
-
// A link signs this browser in
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
189
|
+
// A link signs this browser in, and does nothing else: a chat's preview of it fetches it too, so what an
|
|
190
|
+
// invitation does is done as its page opens (admit). A removed member's shows them why, until their page says
|
|
191
|
+
// they've seen it (its script, POST), which a preview never runs.
|
|
192
|
+
"/auth/:token": {
|
|
193
|
+
GET: async (req: Bun.BunRequest<"/auth/:token">) => {
|
|
194
|
+
try {
|
|
195
|
+
const token = req.params.token;
|
|
196
|
+
if (await whoIs(token)) return signedIn(req, token);
|
|
197
|
+
const words = await keys.farewell(token, false);
|
|
198
|
+
if (words !== null)
|
|
199
|
+
return new Response(farewellPage(words), {
|
|
200
|
+
headers: {
|
|
201
|
+
"content-type": "text/html; charset=utf-8",
|
|
202
|
+
"set-cookie": cookie(req, "", 0),
|
|
203
|
+
...NO_REFERRER,
|
|
204
|
+
},
|
|
205
|
+
});
|
|
206
|
+
} catch (e) {
|
|
207
|
+
console.error("a sign-in failed:", e);
|
|
208
|
+
}
|
|
209
|
+
return new Response(null, { status: 302, headers: { location: "/?link=invalid", ...NO_REFERRER } });
|
|
210
|
+
},
|
|
211
|
+
POST: async (req: Bun.BunRequest<"/auth/:token">) =>
|
|
212
|
+
new Response(null, { status: (await keys.farewell(req.params.token, true)) === null ? 404 : 204 }),
|
|
191
213
|
},
|
|
192
214
|
// This browser forgets who it was; the link still works.
|
|
193
215
|
"/out": {
|
|
@@ -224,6 +246,8 @@ export async function groupOf({
|
|
|
224
246
|
const who = ws.data.who;
|
|
225
247
|
sockets.add(ws);
|
|
226
248
|
if (who && !(await members()).some((m) => m.id === who.member)) (sockets.delete(ws), ws.close(4003, "removed"));
|
|
249
|
+
// in by their link, their page open: the act their invitation names, done for them as them, once
|
|
250
|
+
else if (who && !who.agent) void accepted?.(who);
|
|
227
251
|
},
|
|
228
252
|
/** A document a page subscribes to, and sent it: a page holding the members is told what's its (and
|
|
229
253
|
* only it: nothing of anyone else's has changed). */
|
package/src/members/keys.ts
CHANGED
|
@@ -186,12 +186,12 @@ export async function openKeys(
|
|
|
186
186
|
await db.run("DELETE FROM notices WHERE id = ?", [member]);
|
|
187
187
|
await db.run("DELETE FROM agents WHERE member = ?", [member]);
|
|
188
188
|
},
|
|
189
|
-
/** The words left for whoever held this token
|
|
190
|
-
async farewell(token: string) {
|
|
189
|
+
/** The words left for whoever held this token; `seen`, and they're let go: after this, it opens nothing. */
|
|
190
|
+
async farewell(token: string, seen: boolean) {
|
|
191
191
|
const h = await hash(token);
|
|
192
192
|
const f = (await db.query("SELECT words FROM farewells WHERE hash = ?").get(h)) as { words: string } | null;
|
|
193
193
|
if (!f) return null;
|
|
194
|
-
await db.run("DELETE FROM farewells WHERE hash = ?", [h]);
|
|
194
|
+
if (seen) await db.run("DELETE FROM farewells WHERE hash = ?", [h]);
|
|
195
195
|
return f.words;
|
|
196
196
|
},
|
|
197
197
|
async notice(member: number): Promise<Notice | undefined> {
|