@blueshed/joined 0.3.2 → 0.3.4
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 +13 -5
- package/examples/cafe/migrations/003-guard-supplies.ts +6 -0
- package/examples/film.jpg +0 -0
- package/examples/index.html +4 -4
- package/examples/trip/stories.jsonl +1 -0
- package/examples/welcome.css +6 -1
- package/package.json +1 -1
- package/src/attachment.ts +2 -2
- package/src/compile.ts +45 -9
- package/src/drawer.ts +18 -2
- package/src/engine.ts +11 -2
- package/src/file-value.ts +4 -3
- package/src/files.ts +2 -7
- package/src/ics.ts +1 -1
- package/src/link.ts +11 -6
- package/src/map.ts +2 -1
- package/src/mcp.ts +5 -2
- package/src/members/demo.ts +6 -3
- package/src/members/group.ts +2 -1
- package/src/members/panel.css +0 -4
- package/src/migrate.ts +125 -24
- package/src/page.ts +42 -37
- package/src/postgres-server.ts +69 -14
- package/src/proxy.ts +12 -0
- package/src/reading.ts +21 -3
- package/src/server.ts +32 -25
- package/src/store.ts +27 -18
- package/src/upkeep.ts +9 -8
- package/src/write.ts +71 -21
|
@@ -114,7 +114,8 @@ A story is one JSON line in dzql's words:
|
|
|
114
114
|
- **A field** is a key an add gives or a replace names. Its type is its values': a string, or one
|
|
115
115
|
read off its form (a date-time `2026-11-07T19:00:00`, a time zone `Asia/Tokyo`, an instant `2026-11-07T10:00:00Z`, a duration `PT2H`, a place `geo:35.69,139.70`, a file `file:<its SHA-256>.webp`, one kept by `/files`, §8),
|
|
116
116
|
a boolean, an integer or a decimal (a value with a fraction), or a list of ids (§6). A value not its
|
|
117
|
-
form is refused in words.
|
|
117
|
+
form is refused in words. Some names are refused, for a field or a collection: one every JavaScript object has
|
|
118
|
+
(`constructor`), one that would run script (`onclick`), and `aria-…`.
|
|
118
119
|
- **A field is needed** if every add gives it. A story more that leaves it out lets it go:
|
|
119
120
|
`bun run compile` says so, naming the story. Where no story adds an item (a race's fleets, only replaced), the
|
|
120
121
|
seed's items are all there are: a field every one of them has is typed as there (`JFleetData.start: string`),
|
|
@@ -212,7 +213,10 @@ Working: `examples/trip/stories.jsonl`; the model's rules in `tests/compile.test
|
|
|
212
213
|
or the seed may leave it out), a list of ids a table of pairs, `<collection>_<field>`. The same
|
|
213
214
|
history makes the tables in SQLite now and in Postgres later. A rename it can't know: it says so
|
|
214
215
|
(`by hand:`), and a person makes it `renameColumn`. A column made optional, its type as it was, the
|
|
215
|
-
compiler writes itself (in a `guardTable`); a type changed, or a column made needed, is a person's.
|
|
216
|
+
compiler writes itself (in a `guardTable`); a type changed, or a column made needed, is a person's. A needed column
|
|
217
|
+
added where rows are says what they take (`addColumn(table, column, { …, fill: "…" })`).
|
|
218
|
+
A column dropped as another is added is refused where it holds values: write the rename, or say they go
|
|
219
|
+
(`{ lose: true }`). A migration changed after it ran is warned of at start.
|
|
216
220
|
A `guardTable` that makes a SQLite table again keeps the rows linking to it (its foreign keys are off
|
|
217
221
|
while it runs, and checked whole before it commits). Never change one that has run anywhere.
|
|
218
222
|
- **Rows too: `export const up = (db, { nextId }) => …`** beside `changes` (or alone), run after them in
|
|
@@ -712,7 +716,9 @@ Working: `src/members/`; `tests/sign-in.test.ts`; `tests/mcp.test.ts` (the door)
|
|
|
712
716
|
- **A search on a change's path costs what the page holds:** `querySelector` on a display's shadow seeks past every
|
|
713
717
|
block before the part it wants, and on the document past every item. Keep a display's own parts once found, and
|
|
714
718
|
read a kind of element from a live list (`getElementsByTagName`). `bun run scale` times it; Chrome's profiler
|
|
715
|
-
(`view.cdp("Profiler.start")`) says where.
|
|
719
|
+
(`view.cdp("Profiler.start")`) says where. Time it to the frame that's drawn, not to the applier's write: the 0.4 ms
|
|
720
|
+
once said here left the readings' drawing out. Met on the trip, 3 Dec 2026: a page holding one month redraws in 7 ms,
|
|
721
|
+
of 1,000 events or 100,000.
|
|
716
722
|
- **Bun writes nothing copying a file into one it's asked `exists()` of** (`Bun.write(file, Bun.file(from))`, Bun
|
|
717
723
|
1.4.4): write to the path instead. `joined init` made every file empty, met on its first try from the packed
|
|
718
724
|
tarball, 3 Nov 2026.
|
|
@@ -754,7 +760,8 @@ Working: `src/members/`; `tests/sign-in.test.ts`; `tests/mcp.test.ts` (the door)
|
|
|
754
760
|
folder (`postgres/` in the data folder), or in memory. The engine (`src/engine.ts`) is awaited, on either:
|
|
755
761
|
the values go beside the SQL as parameters, one write at a time (on Postgres, by a lock every server takes), and
|
|
756
762
|
each commit is notified (`NOTIFY joined`), so several servers on one database tell their pages each other's
|
|
757
|
-
writes.
|
|
763
|
+
writes. A connection lost is made again: a write under way as it goes is refused whole, and one not begun waits.
|
|
764
|
+
`joined link` reads either store. The compiler writes each document's query, items and triggers in both dialects
|
|
758
765
|
(`generated/queries.ts`'s `postgres`). The stories are told on both (`tests/helpers.ts`'s `ENGINES`);
|
|
759
766
|
`fresh({ engine })` and `tell(doc, dir, engine)` take one. Old changes and unused files are let go on both;
|
|
760
767
|
joined vacuums and backs up a SQLite file alone (Postgres has its own).
|
|
@@ -770,7 +777,8 @@ Working: `src/members/`; `tests/sign-in.test.ts`; `tests/mcp.test.ts` (the door)
|
|
|
770
777
|
`members.keys.json`. A store that lasts renames those
|
|
771
778
|
`.imported`, never deletes them, and never renames a seed; a store in memory (`JOINED_DB=:memory:`) renames nothing.
|
|
772
779
|
- A store that lasts is kept up when idle (`src/upkeep.ts`): vacuumed once enough is written, and
|
|
773
|
-
backed up once a day (`VACUUM INTO`) to `backups/` beside it (`JOINED_BACKUPS`), the last seven kept.
|
|
780
|
+
backed up once a day (`VACUUM INTO`) to `backups/` beside it (`JOINED_BACKUPS`), the last seven kept. Never idle,
|
|
781
|
+
the backup is made an hour late, and the rest once a day.
|
|
774
782
|
A change is kept 90 days (`JOINED_HISTORY`), and any of a document's last 500 for good (what a page
|
|
775
783
|
catching up is sent); an older one is let go before the vacuum.
|
|
776
784
|
The files the documents name are in `files/` beside it, as its backups are, or a bucket (`JOINED_FILES`:
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Made by `bun run compile` from the stories: what they say beyond the migrations before it.
|
|
2
|
+
import { guardTable } from "joined/migrate";
|
|
3
|
+
|
|
4
|
+
export const changes = [
|
|
5
|
+
guardTable("supplies", { columns: { title: { type: "text", is: { type: "string", said: "title is a string" } }, item: { type: "integer", is: { type: "integer", said: "item is a whole number" }, to: { table: "items", said: "item links only to items the menu document holds", held: ["supply in supplies links to it", "supplies in supplies link to it"] } }, qty: { type: "integer", is: { type: "integer", said: "qty is a whole number" } }, price: { type: "integer", is: { type: "integer", said: "price is a whole number" } }, buyer: { type: "integer", is: { type: "integer", said: "buyer is a whole number" }, to: { table: "members", said: "buyer links only to members the members document holds", onDelete: "set null" }, nullable: true }, supplier: { type: "integer", is: { type: "integer", said: "supplier is a whole number" }, to: { table: "members", said: "supplier links only to members the members document holds", onDelete: "set null" }, nullable: true }, "terms.days": { type: "integer", is: { type: "integer", said: "terms.days is a whole number" } }, "terms.by": { type: "text", is: { type: "string", said: "terms.by is a string" } }, delivered: { type: "boolean", is: { type: "boolean", said: "delivered is a boolean" } } }, strict: true, needs: { columns: ["title", "item", "qty", "price", "buyer", "supplier", "terms.days", "terms.by", "delivered"], said: "a supply needs title (string), item (an id in items), qty (integer), price (integer), buyer (an id in members), supplier (an id in members), terms.days (integer), terms.by (string), delivered (boolean)" } }),
|
|
6
|
+
];
|
package/examples/film.jpg
CHANGED
|
Binary file
|
package/examples/index.html
CHANGED
|
@@ -141,12 +141,12 @@
|
|
|
141
141
|
<figure class="film" id="film">
|
|
142
142
|
<!-- its src is set by page.ts: the bundler would serve the film whole, and Safari plays a video only in ranges -->
|
|
143
143
|
<video preload="none" playsinline poster="./film.jpg" width="1280" height="720"></video>
|
|
144
|
-
<!-- the film's own play:
|
|
144
|
+
<!-- the film's own play: never over the poster's words, as a browser's own would be -->
|
|
145
145
|
<button type="button" class="act ink play" data-icon="play">Play the film · 1:50</button>
|
|
146
146
|
<figcaption class="note">
|
|
147
|
-
One change, from Peter's
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
One change, from Peter's gesture to Nick's screen (1:50), filmed on joined 0.3.2. What it isn't, yet: used by
|
|
148
|
+
many, its largest test a trip of 100,000 events (a page showing one month of it redraws as fast as at 1,000, but
|
|
149
|
+
saving a change takes longer as a trip grows); and it's young: no ecosystem, one person and an AI.
|
|
150
150
|
</figcaption>
|
|
151
151
|
</figure>
|
|
152
152
|
<script type="module" src="./page.ts"></script>
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
{"story":"Not in Tokyo after all: Peter clears the dinner's zone, and it floats, at 19:00 wherever the calendar is","as":"peter","does":{"doc":"trip:1","ops":[{"op":"remove","path":"/events/2/zone"}]},"seen":["peter","phone","nick"]}
|
|
19
19
|
{"story":"The dinner's place isn't settled yet: Peter clears it, and its pin leaves every map","as":"peter","does":{"doc":"trip:1","ops":[{"op":"remove","path":"/events/2/location"},{"op":"remove","path":"/events/2/coordinates"}]},"seen":["peter","phone","nora"]}
|
|
20
20
|
{"story":"Only an organiser clears a place: Nora can't","as":"nora","cannot":{"doc":"trip:1","ops":[{"op":"remove","path":"/events/1/location"}]},"refused":"you may not remove at /events/1/location"}
|
|
21
|
+
{"story":"Nor by replacing it with nothing: Nora can't","as":"nora","cannot":{"doc":"trip:1","ops":[{"op":"replace","path":"/events/1/location","value":null}]},"refused":"a replace gives a value: /events/1/location is cleared by a remove"}
|
|
21
22
|
{"story":"Peter drops the early visit to Fushimi Inari","as":"peter","does":{"doc":"trip:1","ops":[{"op":"remove","path":"/events/4"}]},"seen":["peter","phone","nick"]}
|
|
22
23
|
{"story":"Only an organiser drops an event: Nick can't","as":"nick","cannot":{"doc":"trip:1","ops":[{"op":"remove","path":"/events/1"}]},"refused":"you may not remove at /events/1"}
|
|
23
24
|
{"story":"Peter asks Aiko to the dinner too: it links to the three of them","as":"peter","does":{"doc":"trip:1","ops":[{"op":"replace","path":"/events/2/people","value":[1,2,3]}]},"seen":["peter","phone"]}
|
package/examples/welcome.css
CHANGED
|
@@ -276,7 +276,8 @@ section.quotes figcaption {
|
|
|
276
276
|
font: var(--font-small);
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
-
/* the film: its own play
|
|
279
|
+
/* the film: its own play in the poster's corner, at its start (the poster's words are at its middle); on a phone,
|
|
280
|
+
where it would lie over them, under the poster */
|
|
280
281
|
figure.film {
|
|
281
282
|
position: relative;
|
|
282
283
|
margin: var(--space-48) 0 0;
|
|
@@ -306,4 +307,8 @@ figure.film figcaption {
|
|
|
306
307
|
.welcome .brand {
|
|
307
308
|
flex: 1;
|
|
308
309
|
}
|
|
310
|
+
figure.film .play {
|
|
311
|
+
position: static;
|
|
312
|
+
margin-top: var(--space-8);
|
|
313
|
+
}
|
|
309
314
|
}
|
package/package.json
CHANGED
package/src/attachment.ts
CHANGED
|
@@ -43,8 +43,8 @@ export function attachment(doc: () => string, put: (file: string) => Promise<Ans
|
|
|
43
43
|
const choose = Object.assign(document.createElement("label"), { className: "act small" });
|
|
44
44
|
choose.append(icon("upload"), words, input);
|
|
45
45
|
const line = Object.assign(document.createElement("p"), { className: "said", role: "status" });
|
|
46
|
-
// the file just chosen, as this browser has it: shown until the server serves it, which it does only
|
|
47
|
-
// names it (a drawer's draft, before Save)
|
|
46
|
+
// the file just chosen, as this browser has it: shown until the server serves it, which an apart kind's it does only
|
|
47
|
+
// once an item names it (a drawer's draft, before Save)
|
|
48
48
|
let chosen: { value: string; url: string } | null = null;
|
|
49
49
|
picture.addEventListener("error", () => {
|
|
50
50
|
if (
|
package/src/compile.ts
CHANGED
|
@@ -445,6 +445,8 @@ export function modelFrom(doc: string, example: Record<string, unknown>, from: s
|
|
|
445
445
|
const model: Model = { doc, collections: {} };
|
|
446
446
|
for (const [name, items] of Object.entries(example)) {
|
|
447
447
|
if (!Array.isArray(items)) continue;
|
|
448
|
+
const odd = unelemented(name);
|
|
449
|
+
if (odd) throw new Error(`${from}: ${odd}`);
|
|
448
450
|
if (!items.length) throw new Error(`${from}: ${name} has no item to say what it holds`);
|
|
449
451
|
const c: Coll = {
|
|
450
452
|
fields: {},
|
|
@@ -547,12 +549,26 @@ const ELEMENTS_OWN = [
|
|
|
547
549
|
"tabindex",
|
|
548
550
|
"translate",
|
|
549
551
|
];
|
|
552
|
+
/** The attributes that run script as something happens to their element: a field named so would run a member's words. */
|
|
553
|
+
const HANDLERS =
|
|
554
|
+
/^on(abort|animation\w+|auxclick|before\w+|blur|cancel|canplay\w*|change|click|close|command|contextmenu|copy|cuechange|cut|dblclick|drag\w*|drop|durationchange|emptied|ended|error|focus\w*|formdata|fullscreen\w+|gotpointercapture|hashchange|input|invalid|key\w+|languagechange|load\w*|lostpointercapture|message\w*|mouse\w+|offline|page\w+|paste|pause|play\w*|pointer\w+|popstate|progress|ratechange|rejectionhandled|reset|resize|scroll\w*|securitypolicyviolation|seek\w+|select\w*|slotchange|stalled|storage|submit|suspend|timeupdate|toggle|touch\w+|transition\w+|unhandledrejection|unload|volumechange|waiting|webkit\w+|wheel)$/;
|
|
555
|
+
/** A name every JavaScript object has already, which no collection nor field can be. */
|
|
556
|
+
const EVERY_OBJECTS = (name: string) => name in {};
|
|
557
|
+
/** Why a collection can't be named so, or null: it's its element's name, after j-. (One with an underscore is refused
|
|
558
|
+
* as it's compiled.) */
|
|
559
|
+
const unelemented = (name: string) =>
|
|
560
|
+
!/^[a-z][a-z0-9_-]*$/.test(name) || EVERY_OBJECTS(name)
|
|
561
|
+
? `${name} cannot be a collection's name (a-z, 0-9 and -, and not one every JavaScript object has)`
|
|
562
|
+
: null;
|
|
550
563
|
/** Why a field can't be named so, or null: it's its item element's attribute, so a-z, 0-9 and -; not key or may (an
|
|
551
564
|
* item's id, and what its person may do to it: the applier's own); nor an attribute every element has. */
|
|
552
565
|
export function unnamed(f: string): string | null {
|
|
553
566
|
const last = f.split(".").at(-1)!;
|
|
554
567
|
if (!/^[a-z][a-z0-9-]*$/.test(last) || f === "key" || f === "may")
|
|
555
568
|
return `${f} cannot be an attribute (a-z, 0-9 and -, and not key or may)`;
|
|
569
|
+
if (EVERY_OBJECTS(last)) return `${f} cannot be a field: every JavaScript object has one of that name`;
|
|
570
|
+
if (HANDLERS.test(last) || last.startsWith("aria-"))
|
|
571
|
+
return `${f} cannot be a field: its item's element is given each field as an attribute, and ${last} is ${last.startsWith("aria-") ? "what it says to a screen reader" : "script it would run"}: name it otherwise`;
|
|
556
572
|
if (ELEMENTS_OWN.includes(last))
|
|
557
573
|
return `${f} cannot be a field: its item's element is given each field as an attribute, and ${last} is every HTML element's own (it would change how the element is drawn or found): name it otherwise (a boat's class, design)`;
|
|
558
574
|
return null;
|
|
@@ -686,7 +702,7 @@ const REF = "number | `$${number}`";
|
|
|
686
702
|
/** What removing an item another links to is refused with, once and of many: what links to it, and where. */
|
|
687
703
|
const heldSaid = (coll: string, doc: string): [string, string] => [
|
|
688
704
|
`${itemOf(coll)} in ${doc} links to it`,
|
|
689
|
-
`${
|
|
705
|
+
`${coll} in ${doc} link to it`,
|
|
690
706
|
];
|
|
691
707
|
|
|
692
708
|
/** The tables a model is: each collection one, a field a column (null where no story needs it, or an
|
|
@@ -1527,6 +1543,14 @@ const STORY = ["story", "as", "does", "cannot", "seen", "unseen", "refused", "th
|
|
|
1527
1543
|
WRITE = ["doc", "ops", "why"],
|
|
1528
1544
|
OP = ["op", "path", "value"];
|
|
1529
1545
|
|
|
1546
|
+
/** A line of stories.jsonl, read; undefined, if it isn't JSON. */
|
|
1547
|
+
const lineOf = (text: string): any => {
|
|
1548
|
+
try {
|
|
1549
|
+
return JSON.parse(text);
|
|
1550
|
+
} catch {
|
|
1551
|
+
return undefined;
|
|
1552
|
+
}
|
|
1553
|
+
};
|
|
1530
1554
|
/** Why a line of stories.jsonl isn't in the stories language, or null if it is. */
|
|
1531
1555
|
export function unspoken(line: any): string | null {
|
|
1532
1556
|
if (typeof line !== "object" || line === null || Array.isArray(line)) return "a line is an object";
|
|
@@ -1555,19 +1579,27 @@ export function unspoken(line: any): string | null {
|
|
|
1555
1579
|
if (line.premise !== undefined && Array.isArray(line.premise) !== anonymous)
|
|
1556
1580
|
return anonymous ? "an anonymous story's premise is a list of writes" : "a premise is each name's writes";
|
|
1557
1581
|
const before = anonymous ? (line.premise ?? []) : Object.values(line.premise ?? {}).flat();
|
|
1582
|
+
if (Object.values(anonymous ? {} : (line.premise ?? {})).some((ws) => !Array.isArray(ws)))
|
|
1583
|
+
return "a premise is each name's writes";
|
|
1558
1584
|
// a does (or cannot) of several writes, all or none (src/together.ts); each of a premise's, a write of its own
|
|
1559
1585
|
const writes = partsOf(line.does ?? line.cannot);
|
|
1560
1586
|
const unnamed = [writes, ...before.map((w: unknown) => [w])].map(unwritten).find(Boolean);
|
|
1561
1587
|
if (unnamed) return unnamed;
|
|
1562
1588
|
for (const w of [...writes, ...before] as any[]) {
|
|
1589
|
+
if (typeof w.doc !== "string") return "a write says its document: doc, a name";
|
|
1590
|
+
if (!Array.isArray(w.ops)) return "a write says its ops, a list";
|
|
1563
1591
|
const word = Object.keys(w).find((k) => !WRITE.includes(k));
|
|
1564
1592
|
if (word) return `${word} isn't a write's word: ${WRITE.join(", ")}`;
|
|
1565
1593
|
if (anonymous && "why" in w) return "why is a members' word: a story with it has as";
|
|
1566
|
-
for (const o of w.ops
|
|
1594
|
+
for (const o of w.ops) {
|
|
1595
|
+
if (typeof o !== "object" || o === null) return "an op is an object: its op, its path, and its value";
|
|
1567
1596
|
const word = Object.keys(o).find((k) => !OP.includes(k));
|
|
1568
1597
|
if (word) return `${word} isn't an op's word: ${OP.join(", ")}`;
|
|
1569
1598
|
if (!["add", "replace", "remove"].includes(o.op)) return `${o.op} isn't an op: add, replace or remove`;
|
|
1570
1599
|
if ((o.op === "remove") === "value" in o) return `${o.op} ${o.value === undefined ? "needs" : "has no"} value`;
|
|
1600
|
+
if (typeof o.path !== "string" || !o.path.startsWith("/")) return "an op says its path: /collection/id/field";
|
|
1601
|
+
if (o.op === "add" && parts(o.path).field === undefined && (typeof o.value !== "object" || o.value === null))
|
|
1602
|
+
return "an add's item is an object of its fields";
|
|
1571
1603
|
}
|
|
1572
1604
|
}
|
|
1573
1605
|
return null;
|
|
@@ -1669,6 +1701,8 @@ export function modelOf(stories: Story[], links: Definitions["links"] = {}): Mod
|
|
|
1669
1701
|
const own = /^\/[a-z][a-z0-9-]*$/.test(o.path) && o.op !== "add";
|
|
1670
1702
|
const { coll: name, key: id, field, link, more } = parts(own ? `/${model.doc}/1${o.path}` : o.path);
|
|
1671
1703
|
if (!name || more.length) throw new Error(`${where}: no ${o.op} at ${o.path}`);
|
|
1704
|
+
const odd = unelemented(name);
|
|
1705
|
+
if (odd) throw new Error(`${where}: ${odd}`);
|
|
1672
1706
|
const c = (model.collections[name] ??= {
|
|
1673
1707
|
fields: {},
|
|
1674
1708
|
required: [],
|
|
@@ -1711,7 +1745,7 @@ export function modelOf(stories: Story[], links: Definitions["links"] = {}): Mod
|
|
|
1711
1745
|
}
|
|
1712
1746
|
// an id added to a list, or taken from it: the list is the field's, by its id
|
|
1713
1747
|
else if (item && field && link !== undefined) {
|
|
1714
|
-
if (o.op === "add" && link === "-" &&
|
|
1748
|
+
if (o.op === "add" && link === "-" && typeOf([o.value], true) === "ids")
|
|
1715
1749
|
(typed(field, [o.value]), c.link.includes(field) || c.link.push(field));
|
|
1716
1750
|
else if (o.op === "remove" && /^\d+$/.test(link))
|
|
1717
1751
|
(c.unlink.includes(field) || c.unlink.push(field), later.push({ where, c, f: field, why: "unlinked" }));
|
|
@@ -2277,12 +2311,14 @@ export async function compiled(dir = APPS) {
|
|
|
2277
2311
|
if (derivedFrom[app] || statics.has(app)) continue;
|
|
2278
2312
|
linesOf[app] = (await Bun.file(`${at}/stories.jsonl`).text())
|
|
2279
2313
|
.split("\n")
|
|
2280
|
-
.
|
|
2281
|
-
.
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2314
|
+
.map((l, i) => [l, i + 1] as const)
|
|
2315
|
+
.filter(([l]) => l.trim())
|
|
2316
|
+
.map(([l, n]) => {
|
|
2317
|
+
const line = lineOf(l),
|
|
2318
|
+
why = line === undefined ? "it isn't JSON" : unspoken(line);
|
|
2319
|
+
if (why) throw new Error(`${app}'s stories, line ${n}: ${why}`);
|
|
2320
|
+
return line;
|
|
2321
|
+
});
|
|
2286
2322
|
}
|
|
2287
2323
|
const toldBy = toldOf(
|
|
2288
2324
|
Object.fromEntries(Object.entries(linesOf).map(([app, ls]) => [app, ls.filter((l) => l.story)])),
|
package/src/drawer.ts
CHANGED
|
@@ -17,6 +17,20 @@ export { el };
|
|
|
17
17
|
|
|
18
18
|
let forms = 0;
|
|
19
19
|
|
|
20
|
+
/** What several of an item's own ops would send, as one write: each kept back as it's asked, then all sent together. */
|
|
21
|
+
const together = (item: Item, ask: () => void) =>
|
|
22
|
+
new Promise<Answer>((answer) => {
|
|
23
|
+
const asked: CustomEvent[] = [],
|
|
24
|
+
keep = (e: Event) => (e.stopImmediatePropagation(), void asked.push(e as CustomEvent));
|
|
25
|
+
item.addEventListener("ops", keep);
|
|
26
|
+
ask();
|
|
27
|
+
item.removeEventListener("ops", keep);
|
|
28
|
+
const ops = asked.flatMap((e) => e.detail.ops),
|
|
29
|
+
detail = { ...asked[0]?.detail, ops, answer };
|
|
30
|
+
if (!ops.length) return answer(undefined);
|
|
31
|
+
item.dispatchEvent(new CustomEvent("ops", { bubbles: true, composed: true, detail }));
|
|
32
|
+
});
|
|
33
|
+
|
|
20
34
|
/** What a drawer asks of the display it's in: where it's put, what things are called, and how a write is sent. */
|
|
21
35
|
export type Host = {
|
|
22
36
|
root: ShadowRoot;
|
|
@@ -238,8 +252,10 @@ export class Drawer {
|
|
|
238
252
|
? await this.host.add(this.spec, { ...this.#given, ...this.#filled, ...edits })
|
|
239
253
|
: stays
|
|
240
254
|
? `${stays} can't be left empty`
|
|
241
|
-
:
|
|
242
|
-
|
|
255
|
+
: await together(item, () => {
|
|
256
|
+
if (Object.keys(edits).length) item.replace!(edits);
|
|
257
|
+
if (cleared.length) item.removeField!(...cleared);
|
|
258
|
+
});
|
|
243
259
|
// what was saved, if it's open still: one opened meanwhile (a new one asked) is left as it is
|
|
244
260
|
if (this.open !== was) return;
|
|
245
261
|
this.#draft.refuse(why ?? "");
|
package/src/engine.ts
CHANGED
|
@@ -23,6 +23,11 @@ export type Engine = {
|
|
|
23
23
|
/** The transaction's foreign keys held to at its commit, not each statement: rows that link to each other, put in
|
|
24
24
|
* together. */
|
|
25
25
|
defer(): Promise<unknown>;
|
|
26
|
+
/** What was deferred held to now: a Postgres table with triggers still to run takes no new trigger. */
|
|
27
|
+
settled(): Promise<unknown>;
|
|
28
|
+
/** A statement run on the connection now, and on each made in its place (a Postgres server's, lost): what's the
|
|
29
|
+
* connection's own. */
|
|
30
|
+
each(sql: string): Promise<unknown>;
|
|
26
31
|
/** A table's columns, by name (none: no table). */
|
|
27
32
|
columns(table: string): Promise<string[]>;
|
|
28
33
|
/** The schema a connection's own tables are in (the rows a write changed). */
|
|
@@ -48,7 +53,7 @@ type Connection = { exec(sql: string, args?: unknown[]): Promise<Answer> };
|
|
|
48
53
|
function engine(
|
|
49
54
|
dialect: Dialect,
|
|
50
55
|
c: Connection,
|
|
51
|
-
more: Pick<Engine, "defer" | "columns" | "temp" | "listen" | "notify" | "close">,
|
|
56
|
+
more: Pick<Engine, "defer" | "settled" | "each" | "columns" | "temp" | "listen" | "notify" | "close">,
|
|
52
57
|
): Engine {
|
|
53
58
|
const inside = new AsyncLocalStorage<{ depth: number }>();
|
|
54
59
|
/** The last transaction asked for: each waits for the one before, and what's outside waits for them all. */
|
|
@@ -150,6 +155,8 @@ export function sqlite(file: string, { readonly = false } = {}): Engine {
|
|
|
150
155
|
{ exec },
|
|
151
156
|
{
|
|
152
157
|
defer: () => exec("PRAGMA defer_foreign_keys = ON"),
|
|
158
|
+
settled: async () => {},
|
|
159
|
+
each: exec,
|
|
153
160
|
columns: async (table) =>
|
|
154
161
|
((await exec("SELECT name FROM pragma_table_info(?)", [table])).rows as { name: string }[]).map((c) => c.name),
|
|
155
162
|
temp: "temp",
|
|
@@ -198,7 +205,7 @@ export function postgresOf(
|
|
|
198
205
|
query(sql: string, args: unknown[]): Promise<Answer & { types: Map<string, number> }>;
|
|
199
206
|
many(sql: string): Promise<unknown>;
|
|
200
207
|
},
|
|
201
|
-
more: Pick<Engine, "listen" | "notify" | "close"
|
|
208
|
+
more: Pick<Engine, "listen" | "notify" | "close"> & Partial<Pick<Engine, "each">>,
|
|
202
209
|
): Engine {
|
|
203
210
|
const exec = async (sql: string, args?: unknown[]): Promise<Answer> => {
|
|
204
211
|
try {
|
|
@@ -216,6 +223,8 @@ export function postgresOf(
|
|
|
216
223
|
{ exec },
|
|
217
224
|
{
|
|
218
225
|
defer: () => exec("SET CONSTRAINTS ALL DEFERRED"),
|
|
226
|
+
settled: () => exec("SET CONSTRAINTS ALL IMMEDIATE"),
|
|
227
|
+
each: exec,
|
|
219
228
|
columns: async (table) =>
|
|
220
229
|
(
|
|
221
230
|
await exec(
|
package/src/file-value.ts
CHANGED
|
@@ -15,9 +15,10 @@ export const folderOf = (doc: string) => {
|
|
|
15
15
|
return name ? `${name}-${key}` : "";
|
|
16
16
|
};
|
|
17
17
|
/** Where a file field's value is served: `file:<name>` at /files/<name>; or, given the item's document and that's a
|
|
18
|
-
* kind's instance, at /files/<its folder>/<name>, to whoever may hold it (a guest by its
|
|
18
|
+
* kind's instance, at /files/<its folder>/<name>, to whoever may hold it (a guest by its code, as the page holds the
|
|
19
|
+
* document by it). */
|
|
19
20
|
export const fileUrl = (value: string, doc = "") => {
|
|
20
21
|
const folder = folderOf(doc),
|
|
21
|
-
|
|
22
|
-
return folder ? `/files/${folder}/${fileName(value)}${
|
|
22
|
+
held = /\?.*/.exec(doc)?.[0] ?? "";
|
|
23
|
+
return folder ? `/files/${folder}/${fileName(value)}${held}` : `/files/${fileName(value)}`;
|
|
23
24
|
};
|
package/src/files.ts
CHANGED
|
@@ -222,14 +222,9 @@ export function routes(
|
|
|
222
222
|
},
|
|
223
223
|
"/files/:folder/:name": async (req: Request & { params: { folder: string; name: string } }) => {
|
|
224
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");
|
|
225
|
+
at = /^([a-z][a-z0-9-]*)-(\d+)$/.exec(folder);
|
|
227
226
|
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
|
-
);
|
|
227
|
+
const why = await holds(req, `${at[1]}:${at[2]}${new URL(req.url).search}`, name);
|
|
233
228
|
if (why) return Response.json({ refused: why }, { status: 403 });
|
|
234
229
|
const own = files.file(`${folder}/${name}`),
|
|
235
230
|
shared = files.file(name);
|
package/src/ics.ts
CHANGED
|
@@ -22,7 +22,7 @@ export type IcsOut = IcsEvent & { uid: string; description?: string; url?: strin
|
|
|
22
22
|
const esc = (s: string) =>
|
|
23
23
|
s
|
|
24
24
|
.replace(/\\/g, "\\\\")
|
|
25
|
-
.replace(/\n/g, "\\n")
|
|
25
|
+
.replace(/\r\n?|\n/g, "\\n")
|
|
26
26
|
.replace(/([,;])/g, "\\$1");
|
|
27
27
|
const unesc = (s: string) => s.replace(/\\(.)/g, (_, c: string) => (c.toLowerCase() === "n" ? "\n" : c));
|
|
28
28
|
/** A line folded at 75 bytes, as RFC 5545 asks (a folded line's leading space one of them), never inside a letter:
|
package/src/link.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// bun run link <name>: a member's link, printed, for the terminal (Bun reads ROOT_TOKEN from .env).
|
|
2
|
-
// It reads the members and the keys as the running server keeps them, and changes nothing:
|
|
3
|
-
//
|
|
2
|
+
// It reads the members and the keys as the running server keeps them, and changes nothing: a
|
|
3
|
+
// SQLite store is opened read-only.
|
|
4
4
|
|
|
5
|
-
import { DB } from "./compile.ts";
|
|
6
|
-
import { sqlite } from "./engine.ts";
|
|
5
|
+
import { DB, ENGINE } from "./compile.ts";
|
|
6
|
+
import { open, sqlite, type Dialect } from "./engine.ts";
|
|
7
7
|
import { openKeys } from "./members/keys.ts";
|
|
8
8
|
import { bdflOf, type Member } from "./members/trust.ts";
|
|
9
9
|
|
|
@@ -14,10 +14,15 @@ export const baseOf = (env: Record<string, string | undefined> = Bun.env) =>
|
|
|
14
14
|
/** A member's link, by name: the BDFL's is ROOT_TOKEN's, anyone else's is from the keys. */
|
|
15
15
|
export async function linkFor(
|
|
16
16
|
name: string | undefined,
|
|
17
|
-
{
|
|
17
|
+
{
|
|
18
|
+
root = Bun.env.ROOT_TOKEN,
|
|
19
|
+
base = baseOf(),
|
|
20
|
+
db = DB,
|
|
21
|
+
engine = ENGINE,
|
|
22
|
+
}: { root?: string; base?: string; db?: string; engine?: Dialect },
|
|
18
23
|
) {
|
|
19
24
|
if (!name || !root) throw new Error("bun run link <name>, with ROOT_TOKEN set (in .env)");
|
|
20
|
-
const store = sqlite(db, { readonly: true });
|
|
25
|
+
const store = engine === "sqlite" ? sqlite(db, { readonly: true }) : await open(db, engine);
|
|
21
26
|
// a column left empty is a field not given, as the store reads it: the BDFL has no inviter
|
|
22
27
|
const all = ((await store.query("SELECT id, name, inviter FROM members").all()) as Record<string, unknown>[]).map(
|
|
23
28
|
(r) => Object.fromEntries(Object.entries(r).filter(([, v]) => v !== null)) as Member,
|
package/src/map.ts
CHANGED
|
@@ -85,7 +85,8 @@ export class JMap extends Reading {
|
|
|
85
85
|
}
|
|
86
86
|
this.#look(this.#view, false);
|
|
87
87
|
}
|
|
88
|
-
|
|
88
|
+
const here = new Set(placed.map((p) => p.it));
|
|
89
|
+
for (const [it, pin] of this.#pins) if (!here.has(it)) (pin.remove(), this.#pins.delete(it));
|
|
89
90
|
for (const { it, p } of placed) {
|
|
90
91
|
let pin = this.#pins.get(it);
|
|
91
92
|
if (!pin) {
|
package/src/mcp.ts
CHANGED
|
@@ -218,6 +218,9 @@ export function door(d: Door) {
|
|
|
218
218
|
for (const r of d.rules) {
|
|
219
219
|
// the document a call names: a kind's instance by its key (trip:1), or the one
|
|
220
220
|
const name = (args: { key?: unknown }) => (r.kind ? `${r.doc}:${args.key}` : r.doc);
|
|
221
|
+
// (a whole number alone, so it names an instance and nothing more: no code, no window)
|
|
222
|
+
const unkeyed = (args: { key?: unknown }) =>
|
|
223
|
+
r.kind && !/^[1-9]\d*$/.test(String(args.key)) ? `key: which ${r.doc}, a whole number` : null;
|
|
221
224
|
if (!r.kind && (await d.holds(r.doc, who.member))) continue;
|
|
222
225
|
const key = r.kind
|
|
223
226
|
? { key: { type: "integer", description: `which ${r.doc}: the id of one of ${r.kind.doc}'s ${r.kind.root}` } }
|
|
@@ -242,7 +245,7 @@ export function door(d: Door) {
|
|
|
242
245
|
// a read changes nothing: a client needn't ask before it
|
|
243
246
|
annotations: { readOnlyHint: true },
|
|
244
247
|
run: async (args) => {
|
|
245
|
-
const no = await d.holds(name(args), who.member);
|
|
248
|
+
const no = unkeyed(args) ?? (await d.holds(name(args), who.member));
|
|
246
249
|
if (no) return refused(no);
|
|
247
250
|
const read = readOf(await d.read(name(args), who.member), args);
|
|
248
251
|
return typeof read === "string" ? refused(read) : answered(read);
|
|
@@ -284,7 +287,7 @@ export function door(d: Door) {
|
|
|
284
287
|
if (!Array.isArray(args.ops) || !args.ops.length)
|
|
285
288
|
return refused("ops: one write's ops, a list of at least one");
|
|
286
289
|
if (!args.ops.every(isOp)) return refused(OPS);
|
|
287
|
-
const no = await d.holds(name(args), who.member);
|
|
290
|
+
const no = unkeyed(args) ?? (await d.holds(name(args), who.member));
|
|
288
291
|
if (no) return refused(no);
|
|
289
292
|
const w = await d.write(
|
|
290
293
|
name(args),
|
package/src/members/demo.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
// A demo on this machine, never live (start refuses one in production): anyone here may be any member, without their
|
|
2
2
|
// link, from the root page's line, so a demo of Nora's or Nick's pages always has a way back to the BDFL. It's asked
|
|
3
3
|
// of this machine alone: from its loopback, by its own name (a name that only resolves to it, a page elsewhere, is
|
|
4
|
-
// refused), and never by a page on another site (a picture's address there would sign this
|
|
4
|
+
// refused), never passed on by a proxy, and never by a page on another site (a picture's address there would sign this
|
|
5
|
+
// browser in).
|
|
5
6
|
|
|
7
|
+
import { passedOn } from "../proxy.ts";
|
|
6
8
|
import type { Member } from "./trust.ts";
|
|
7
9
|
|
|
8
10
|
const LOOPBACK = ["127.0.0.1", "::1", "::ffff:127.0.0.1"];
|
|
9
11
|
const HOSTS = ["localhost", "127.0.0.1", "[::1]"];
|
|
10
12
|
|
|
11
13
|
/** Whether a request is this machine's own: from its loopback (its address, `ip`), by its own name, from no other
|
|
12
|
-
* site. */
|
|
14
|
+
* site, and not passed on by a proxy here (a tunnel, for anyone). */
|
|
13
15
|
export const fromHere = (req: Request, ip?: string) =>
|
|
14
16
|
LOOPBACK.includes(ip ?? "") &&
|
|
15
17
|
HOSTS.includes(new URL(req.url).hostname) &&
|
|
16
|
-
req.headers.get("sec-fetch-site") !== "cross-site"
|
|
18
|
+
req.headers.get("sec-fetch-site") !== "cross-site" &&
|
|
19
|
+
!passedOn(req);
|
|
17
20
|
|
|
18
21
|
/** The demo's routes: who there is to be, the BDFL first, and who this browser is (`me`, none signed out); and
|
|
19
22
|
* being one, this browser signed in as them by the server (`signIn`), their link never shown. Refused to anyone not
|
package/src/members/group.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { timingSafeEqual } from "node:crypto";
|
|
|
14
14
|
import type { Engine } from "../engine.ts";
|
|
15
15
|
import { demoRoutes } from "./demo.ts";
|
|
16
16
|
import { parts, type Op } from "../ops.ts";
|
|
17
|
+
import { schemeOf } from "../proxy.ts";
|
|
17
18
|
import { openKeys, type Agent } from "./keys.ts";
|
|
18
19
|
import { doc as OWN } from "./generated/rules.ts";
|
|
19
20
|
import { bdflOf, mayAsk, mayWrite, trust, withMovesUp, type Asked, type Member } from "./trust.ts";
|
|
@@ -126,7 +127,7 @@ export async function groupOf({
|
|
|
126
127
|
path: "/",
|
|
127
128
|
httpOnly: true,
|
|
128
129
|
sameSite: "lax",
|
|
129
|
-
secure:
|
|
130
|
+
secure: schemeOf(req) === "https",
|
|
130
131
|
maxAge,
|
|
131
132
|
}).serialize();
|
|
132
133
|
/** Home, this browser signed in by a token, for a year. */
|
package/src/members/panel.css
CHANGED