@blueshed/joined 0.3.6 → 0.3.7
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 +15 -1
- package/docs/stories.md +2 -0
- package/examples/cafe/generated/supplies.rules.ts +2 -0
- package/examples/cafe/stories.jsonl +3 -0
- package/package.json +1 -1
- package/src/compile.ts +33 -5
- package/src/definitions.ts +148 -45
- package/src/stories.ts +23 -6
- package/src/write.ts +19 -1
|
@@ -849,6 +849,12 @@ Working: `src/members/`; `tests/sign-in.test.ts`; `tests/mcp.test.ts` (the door)
|
|
|
849
849
|
stories: a `does` grants its ops to its teller's named role as the story finds them, its premise written on the
|
|
850
850
|
seeds (none: anyone).
|
|
851
851
|
The grants refuse the rest, in the write's transaction: "you may not add at /accounts/-".
|
|
852
|
+
A who-path follows a link to what it names: `[of=@]` is the item it's asked of, `[role=director,accountant]`
|
|
853
|
+
any of those, and `@who>…` asks the item `who` links to whatever follows, a term or a group in brackets. Who
|
|
854
|
+
keeps a firm, by its ties: `@member | firms/ties[of=@][role=director,accountant]@who>(@member |
|
|
855
|
+
firms/ties[of=@][role=employee]@who>@member)`, its own member, a director, an accountant and who works there.
|
|
856
|
+
Say it as one role, as the chain is written out: a role inside another would make a teller two roles to the
|
|
857
|
+
same item. `tests/fixtures/firms`, `tests/fixtures/firm`.
|
|
852
858
|
- `owns`: who sees what, one who-path for the document or one per collection (`@members` is the item's
|
|
853
859
|
own). A page is sent whole, and told, only what its person sees: an item they come to see is added to
|
|
854
860
|
their page, and one they see no longer (or that's gone) is removed. Back from away, it's sent whole again.
|
|
@@ -949,6 +955,14 @@ Working: `src/members/`; `tests/sign-in.test.ts`; `tests/mcp.test.ts` (the door)
|
|
|
949
955
|
- `addedWith`: an item added only in the write adding what a link of it points at, never after: `{"addedWith":
|
|
950
956
|
{"lines": {"order": "an order's lines are set when it's placed"}}}`. A trigger, in both: the item linked to is one
|
|
951
957
|
the write added (the rows a write changed, `joined_touched`).
|
|
958
|
+
- `person`: what's a person's alone, whatever their agent may otherwise do: `{"person": {"supplies": {"add": "an
|
|
959
|
+
order to a supplier is a person's to place"}}}`, by collection, each op (`add`, `remove`, or with its field:
|
|
960
|
+
`"replace price"`, `"add people[]"`) and its words. A write made via anything (the member's agent's link at the MCP
|
|
961
|
+
door, a route's write as a member) is refused in them, after the grants; on their own page it goes as the grants
|
|
962
|
+
say. Checked by a `cannot` story told `via` (`"as": "buyer", "via": "claude"`: sent by an agent's link made for the
|
|
963
|
+
teller, under that label), refused in its words; a `does` told `via` shows what an agent still may. Working: the
|
|
964
|
+
cafe's supplies. Not yet: a move to one value (`status>approved`), and the door's tools and `may` leaving the op
|
|
965
|
+
out (#259).
|
|
952
966
|
- `within` and `apart`: an item's lists as the write leaves them. `{"within": {"events": [{"lists": ["keen", "asked",
|
|
953
967
|
"declined"], "of": "people", "refused": "an answer is from someone on the event"}]}}`: each id of those lists is in
|
|
954
968
|
its `of` list too; `{"apart": {"events": [{"lists": ["keen", "asked", "declined"], "refused": "one answer each"}]}}`:
|
|
@@ -974,7 +988,7 @@ Working: `src/members/`; `tests/sign-in.test.ts`; `tests/mcp.test.ts` (the door)
|
|
|
974
988
|
when the group is open too, of nobody (`member` 0). The compiler can't read it, so write a `cannot` story,
|
|
975
989
|
`refused` in its words, for each rule in it: the stories told say so of one that refuses nothing they tell
|
|
976
990
|
(`<app>'s trust.ts refuses nothing its stories tell`), and no more than that. Look first for a definition that
|
|
977
|
-
says it (`mine`, `unique`, `noOverlap`, `notEmpty`, `moves`, `takes`, `frozen`, `addedWith`, `within`, `apart`): the examples
|
|
991
|
+
says it (`mine`, `unique`, `noOverlap`, `notEmpty`, `moves`, `takes`, `frozen`, `addedWith`, `within`, `apart`, `person`): the examples
|
|
978
992
|
have no `trust.ts` (Peter, 30 Nov: their rules said once, the database's on Postgres). One that truly can't be
|
|
979
993
|
declared is a trigger in each dialect's SQL, not TypeScript.
|
|
980
994
|
- **A story's words for them:** `premise` (each name's writes before it, in order; an anonymous story's, a list), `unseen` (who must not
|
package/docs/stories.md
CHANGED
|
@@ -50,6 +50,7 @@ Members add who writes, who sees, and who mustn't:
|
|
|
50
50
|
line := cast | story
|
|
51
51
|
cast := { "cast": { name: member | null, … } }
|
|
52
52
|
story += { "as": name,
|
|
53
|
+
"via"?: label, an agent's link's label: a-z, 0-9, spaces, hyphens
|
|
53
54
|
"seen"?: [name], "unseen"?: [name],
|
|
54
55
|
"premise"?: { name: [write] } } in place of the core's [write]
|
|
55
56
|
write += { "why"?: words }
|
|
@@ -59,6 +60,7 @@ name := a member's name, or a cast name
|
|
|
59
60
|
| Word | Means |
|
|
60
61
|
| ------- | ------------------------------------------------------ |
|
|
61
62
|
| as | the member who writes it |
|
|
63
|
+
| via | it's made by their agent's link, not on their page |
|
|
62
64
|
| seen | who must see the change |
|
|
63
65
|
| unseen | who must never see it |
|
|
64
66
|
| cast | other names for a member's pages, or `null` for nobody |
|
|
@@ -61,6 +61,8 @@ export const window = null;
|
|
|
61
61
|
export const names: Record<string, readonly [(m: number, read: (doc: string) => any, doc: string) => boolean, string]> = {
|
|
62
62
|
"supplier": [(m, read, doc) => (read("staff")["posts"] ?? []).some((i: any) => String(i["role"]) === "supplier" && (i["members"] === m || (Array.isArray(i["members"]) && i["members"].includes(m)))), "supplier names someone who isn't among staff's posts with role supplier"],
|
|
63
63
|
};
|
|
64
|
+
/** What's a person's alone, by collection: each op a write made via anything (an agent's link, a route) is refused, and the words (its definitions' person). */
|
|
65
|
+
export const person = {"supplies":{"add":"an order to a supplier is a person's to place"}};
|
|
64
66
|
/** The document as one statement over its tables (generated/supplies.queries.ts). */
|
|
65
67
|
export { query, postgres, items, triggers } from "./supplies.queries.ts";
|
|
66
68
|
|
|
@@ -14,10 +14,12 @@
|
|
|
14
14
|
{"documents":{"books":{"entries":{"from":"supplies","when":"delivered","sides":{"buyer":"bought","supplier":"sold"},"fields":["title","buyer","supplier"],"total":{"sum":"qty","times":"price"}}}}}
|
|
15
15
|
{"addedWith":{"lines":{"order":"an order's lines are set when it's placed"}}}
|
|
16
16
|
{"frozen":{"supplies":{"by":"delivered","fields":["price","terms"],"refused":"a delivered supply keeps its price and terms"}}}
|
|
17
|
+
{"person":{"supplies":{"add":"an order to a supplier is a person's to place"}}}
|
|
17
18
|
{"story":"Peter puts Dev in the kitchen too","as":"manager","does":{"doc":"staff","ops":[{"op":"add","path":"/posts/-","value":{"role":"kitchen","members":[4]}}]},"seen":["manager"]}
|
|
18
19
|
{"story":"Peter takes Nora off the till","as":"manager","does":{"doc":"staff","ops":[{"op":"replace","path":"/posts/2/members","value":[1]}]},"seen":["manager"]}
|
|
19
20
|
{"story":"Wai can't give anyone a role","as":"wai","cannot":{"doc":"staff","ops":[{"op":"add","path":"/posts/-","value":{"role":"till","members":[4]}}]},"refused":"you may not add at /posts/-"}
|
|
20
21
|
{"story":"Peter puts muffins on the menu","as":"manager","does":{"doc":"menu","ops":[{"op":"add","path":"/items/-","value":{"name":"Muffin","price":300,"stock":12}}]},"seen":["manager","wai"]}
|
|
22
|
+
{"story":"Peter's agent still puts scones on the menu","as":"manager","via":"claude","does":{"doc":"menu","ops":[{"op":"add","path":"/items/-","value":{"name":"Scone","price":280,"stock":8}}]},"seen":["manager","wai"]}
|
|
21
23
|
{"story":"Peter prices a latte at £3.80","as":"manager","does":{"doc":"menu","ops":[{"op":"replace","path":"/items/3/price","value":380}]},"seen":["manager","wai"]}
|
|
22
24
|
{"story":"Peter renames the flat white: it's oat milk now","as":"manager","does":{"doc":"menu","ops":[{"op":"replace","path":"/items/2/name","value":"Flat white, oat"}]},"seen":["manager","wai"]}
|
|
23
25
|
{"story":"Wai can't change a price","as":"wai","cannot":{"doc":"menu","ops":[{"op":"replace","path":"/items/3/price","value":1}]},"refused":"you may not replace at /items/3/price"}
|
|
@@ -36,6 +38,7 @@
|
|
|
36
38
|
{"story":"Bo can't cancel once the kitchen has started","cannot":{"doc":"order?code=Bo7pL2mRv8Yd","ops":[{"op":"replace","path":"/orders/2/status","value":"cancelled"}]},"refused":"that isn't a move you can make"}
|
|
37
39
|
{"story":"Ann can't touch Jo's order","cannot":{"doc":"order?code=q3Zt9xKcW1aB","ops":[{"op":"replace","path":"/orders/4/status","value":"cancelled"}]},"refused":"you may not replace at /orders/4/status"}
|
|
38
40
|
{"story":"Peter orders twenty croissants from Nick","as":"buyer","does":{"doc":"supplies","ops":[{"op":"add","path":"/supplies/-","value":{"title":"Croissants, Monday","item":5,"qty":20,"price":120,"buyer":1,"supplier":2,"terms":{"days":14,"by":"invoice"},"delivered":false}}]},"seen":["buyer","roaster"],"unseen":["wai"]}
|
|
41
|
+
{"story":"Peter's agent can't order from Nick: an order to a supplier is a person's to place","as":"buyer","via":"claude","cannot":{"doc":"supplies","ops":[{"op":"add","path":"/supplies/-","value":{"title":"Croissants, Tuesday","item":5,"qty":20,"price":120,"buyer":1,"supplier":2,"terms":{"days":14,"by":"invoice"},"delivered":false}}]},"refused":"an order to a supplier is a person's to place"}
|
|
39
42
|
{"story":"Nick sets his price","as":"roaster","does":{"doc":"supplies","ops":[{"op":"replace","path":"/supplies/1/price","value":110}]},"seen":["roaster","buyer"]}
|
|
40
43
|
{"story":"Nick gives thirty days to pay","as":"roaster","does":{"doc":"supplies","ops":[{"op":"replace","path":"/supplies/1/terms/days","value":30}]},"seen":["roaster","buyer"]}
|
|
41
44
|
{"story":"Peter can't set his own terms","as":"buyer","cannot":{"doc":"supplies","ops":[{"op":"replace","path":"/supplies/1/terms/days","value":90}]},"refused":"you may not replace at /supplies/1/terms/days"}
|
package/package.json
CHANGED
package/src/compile.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
kindFrozen,
|
|
23
23
|
type Definitions,
|
|
24
24
|
type Frozen,
|
|
25
|
+
type LinkTo,
|
|
25
26
|
type Counted,
|
|
26
27
|
type Query,
|
|
27
28
|
type Where,
|
|
@@ -40,6 +41,8 @@ type Write = { doc: string; ops: Op[]; why?: string };
|
|
|
40
41
|
export type Story = {
|
|
41
42
|
story: string;
|
|
42
43
|
as?: string;
|
|
44
|
+
/** Made via something, by its label (the member's agent's link), never by their own hand on a page. */
|
|
45
|
+
via?: string;
|
|
43
46
|
does?: Write;
|
|
44
47
|
cannot?: Write;
|
|
45
48
|
seen?: string[];
|
|
@@ -1248,6 +1251,7 @@ export function definedFor(d: Definitions, m: Model, stories: Story[]): Definiti
|
|
|
1248
1251
|
"within",
|
|
1249
1252
|
"apart",
|
|
1250
1253
|
"notEmpty",
|
|
1254
|
+
"person",
|
|
1251
1255
|
] as const) {
|
|
1252
1256
|
const mine = has(d[k] as Record<string, unknown> | undefined);
|
|
1253
1257
|
if (mine && Object.keys(mine).length) out[k] = mine;
|
|
@@ -1378,6 +1382,7 @@ const DEFINED = [
|
|
|
1378
1382
|
"within",
|
|
1379
1383
|
"apart",
|
|
1380
1384
|
"notEmpty",
|
|
1385
|
+
"person",
|
|
1381
1386
|
];
|
|
1382
1387
|
|
|
1383
1388
|
/** Why a definition is checked by no story, or null: each must be, or it's a guess. A kind, by a story on an
|
|
@@ -1513,6 +1518,10 @@ export function unchecked(
|
|
|
1513
1518
|
] as const)
|
|
1514
1519
|
for (const words of all)
|
|
1515
1520
|
if (!refusing(words)) return `${key} is checked by no story: none cannot, refused in "${words}"`;
|
|
1521
|
+
// what's a person's alone, by a cannot told via something, refused in its words
|
|
1522
|
+
for (const words of Object.values(defs.person ?? {}).flatMap((ops) => Object.values(ops)))
|
|
1523
|
+
if (!stories.some((s) => s.cannot && s.via && s.refused === words))
|
|
1524
|
+
return `person is checked by no story: none told via an agent cannot, refused in "${words}"`;
|
|
1516
1525
|
return settingsUnchecked(defs, stories, grants) ?? visitorsRefused(defs, stories, rolesOfTeller);
|
|
1517
1526
|
}
|
|
1518
1527
|
|
|
@@ -1539,7 +1548,7 @@ ${names.join("\n")}
|
|
|
1539
1548
|
};
|
|
1540
1549
|
|
|
1541
1550
|
/** The stories language (docs/stories.md): the words a story, a write and an op may have, and no others. */
|
|
1542
|
-
const STORY = ["story", "as", "does", "cannot", "seen", "unseen", "refused", "then", "premise"],
|
|
1551
|
+
const STORY = ["story", "as", "via", "does", "cannot", "seen", "unseen", "refused", "then", "premise"],
|
|
1543
1552
|
WRITE = ["doc", "ops", "why"],
|
|
1544
1553
|
OP = ["op", "path", "value"];
|
|
1545
1554
|
|
|
@@ -1563,6 +1572,9 @@ export function unspoken(line: any): string | null {
|
|
|
1563
1572
|
if (odd) return `${odd} isn't a story's word: ${STORY.join(", ")}`;
|
|
1564
1573
|
if (typeof line.story !== "string") return "a story has its words";
|
|
1565
1574
|
if (line.as !== undefined && typeof line.as !== "string") return "as is a member's name";
|
|
1575
|
+
if (line.via !== undefined && line.as === undefined) return "via is a members' word: a story with it has as";
|
|
1576
|
+
if (line.via !== undefined && !(typeof line.via === "string" && /^[a-z0-9 -]{1,40}$/.test(line.via)))
|
|
1577
|
+
return "via is an agent's label: 1 to 40 of a-z, 0-9, spaces and hyphens";
|
|
1566
1578
|
if (!line.does === !line.cannot) return "a story does, or cannot: one of them";
|
|
1567
1579
|
if (line.refused !== undefined && !line.cannot) return "refused goes with cannot";
|
|
1568
1580
|
if (line.then !== undefined && !line.does) return "then goes with does";
|
|
@@ -1833,6 +1845,7 @@ export function compile(
|
|
|
1833
1845
|
fixed = false,
|
|
1834
1846
|
readOnly?: Query,
|
|
1835
1847
|
prefix = "",
|
|
1848
|
+
linkTo?: LinkTo,
|
|
1836
1849
|
): { rules: string; elements: string; rows: string | null } {
|
|
1837
1850
|
const HEAD = `// Written by src/compile.ts from ${from}: change ${derivedFrom ? "it and its example" : fixed ? "its seed" : "the stories"}, not this.\n`;
|
|
1838
1851
|
for (const name of Object.keys(collections)) {
|
|
@@ -1921,7 +1934,7 @@ export const member = true;
|
|
|
1921
1934
|
: ""
|
|
1922
1935
|
}/** The parts of ${doc} a page may hold alone, by its query's parameters (its definitions' window): null, none. */
|
|
1923
1936
|
export const window = ${JSON.stringify(defs.window ?? null)};
|
|
1924
|
-
${defs.window && !defs.where?.[doc] ? windowCode(defs.window) : ""}${namesCode(defs, Object.values(collections))}${defs.moves ? `/** Each field whose value moves only as the stories move it, each step by its roles (its definitions' moves). */\nexport const moves = ${JSON.stringify(defs.moves)};\n` : ""}${defs.guests ? `/** The field holding an item's own code: a page holding ${doc} by it is the item's guest (its definitions' guests). */\nexport const guests = ${JSON.stringify(defs.guests)};\n` : ""}${settingsCode(doc, defs)}${defs.visitors ? `/** The field of its kind's root holding the level its guest is, the role guest: that level (its definitions' visitors). */\nexport const visitors = ${JSON.stringify(defs.visitors)};\n` : ""}${derivedFrom ? "" : `/** The document as one statement over its tables (generated/${prefix}queries.ts). */\nexport { query, postgres${readOnly ? "" : ", items, triggers"} } from "./${prefix}queries.ts";\n`}${derivedFrom ? `/** The documents this one is derived from (its derive.ts): what the server derives it again on a write to. */\nexport const derived = ${JSON.stringify(derivedFrom)};\n` : ""}`;
|
|
1937
|
+
${defs.window && !defs.where?.[doc] ? windowCode(defs.window) : ""}${namesCode(defs, Object.values(collections))}${defs.moves ? `/** Each field whose value moves only as the stories move it, each step by its roles (its definitions' moves). */\nexport const moves = ${JSON.stringify(defs.moves)};\n` : ""}${defs.person ? `/** What's a person's alone, by collection: each op a write made via anything (an agent's link, a route) is refused, and the words (its definitions' person). */\nexport const person = ${JSON.stringify(defs.person)};\n` : ""}${defs.guests ? `/** The field holding an item's own code: a page holding ${doc} by it is the item's guest (its definitions' guests). */\nexport const guests = ${JSON.stringify(defs.guests)};\n` : ""}${settingsCode(doc, defs)}${defs.visitors ? `/** The field of its kind's root holding the level its guest is, the role guest: that level (its definitions' visitors). */\nexport const visitors = ${JSON.stringify(defs.visitors)};\n` : ""}${derivedFrom ? "" : `/** The document as one statement over its tables (generated/${prefix}queries.ts). */\nexport { query, postgres${readOnly ? "" : ", items, triggers"} } from "./${prefix}queries.ts";\n`}${derivedFrom ? `/** The documents this one is derived from (its derive.ts): what the server derives it again on a write to. */\nexport const derived = ${JSON.stringify(derivedFrom)};\n` : ""}`;
|
|
1925
1938
|
let elements = `${HEAD}
|
|
1926
1939
|
// Each collection is an element, defined here: on a page, it holds the document, and its items
|
|
1927
1940
|
// are put inside it. Each item is an element, defined by the page: as it is, or by a view that
|
|
@@ -2064,7 +2077,7 @@ export function write(from: Element, ops: ${Doc}Op[], why?: string) { return sen
|
|
|
2064
2077
|
rules += `\n/** The lists that are each member's own (its definitions' mine), or links to one: a member gives and takes only their own id. */\nexport const mine: Record<string, string[]> = ${JSON.stringify(own)};\n${Object.keys(mineSaid(defs)).length ? `/** The words a write giving or taking another's is refused in, where its mine says them. */\nexport const mineSaid: Record<string, Record<string, string>> = ${JSON.stringify(mineSaid(defs))};\n` : ""}`;
|
|
2065
2078
|
// a derived document is derived for each page, its member's: it has no roles, and is published to no topic
|
|
2066
2079
|
if (!derivedFrom && !readOnly)
|
|
2067
|
-
rules += whoRules(doc, Object.keys(collections), defs, grants, defs.kind && holders[defs.kind.root]);
|
|
2080
|
+
rules += whoRules(doc, Object.keys(collections), defs, grants, defs.kind && holders[defs.kind.root], linkTo);
|
|
2068
2081
|
// a derived or read-only document is rows too, which none may change: the kit reads it as any other (a timeline of
|
|
2069
2082
|
// a sequence derived from starts, results as rows)
|
|
2070
2083
|
const rows = fixed ? null : rowsOf({ doc, collections }, defs, link, HEAD, prefix, grants);
|
|
@@ -2482,7 +2495,20 @@ export async function compiled(dir = APPS) {
|
|
|
2482
2495
|
.filter(([a]) => a !== app)
|
|
2483
2496
|
.flatMap(([, m]) => Object.keys(m.collections).map((c) => [c, m.doc])),
|
|
2484
2497
|
);
|
|
2485
|
-
/**
|
|
2498
|
+
/** Where a link of a collection goes, by the links of the app whose document has it (a relation's who, to entities),
|
|
2499
|
+
* for a who-path's `@field>term`, in any app's. */
|
|
2500
|
+
const linkTo: LinkTo = (coll, field) => {
|
|
2501
|
+
const [app, c] =
|
|
2502
|
+
Object.entries(models).flatMap(([a, m]) =>
|
|
2503
|
+
Object.entries(m.collections)
|
|
2504
|
+
.filter(([name, x]) => (coll === undefined || name === coll) && x.fields[field])
|
|
2505
|
+
.map(([, x]) => [a, x] as const),
|
|
2506
|
+
)[0] ?? [];
|
|
2507
|
+
const l = app && defs[app]!.links?.[field];
|
|
2508
|
+
const to = typeof l === "string" ? l : l ? l.to : field;
|
|
2509
|
+
const at = c && Object.values(models).find((m) => m.collections[to]);
|
|
2510
|
+
return at ? { coll: to, doc: at.doc, list: c!.fields[field] === "ids" } : undefined;
|
|
2511
|
+
};
|
|
2486
2512
|
/** What each person sees of each of a document's collections (its owns), as SQL of a row, `t`: a collection owns
|
|
2487
2513
|
* doesn't name, everyone's. */
|
|
2488
2514
|
const ownsOf = (app: string): Record<string, string> => {
|
|
@@ -2496,6 +2522,7 @@ export async function compiled(dir = APPS) {
|
|
|
2496
2522
|
list: (c: string, f: string) => types(c)[f] === "ids",
|
|
2497
2523
|
own: Object.keys(m.collections),
|
|
2498
2524
|
...(kind ? { kind: { column: m.doc, root: kind.root } } : {}),
|
|
2525
|
+
link: linkTo,
|
|
2499
2526
|
};
|
|
2500
2527
|
return Object.fromEntries(
|
|
2501
2528
|
Object.entries(by).map(([c, path]) => [c, `(${whoSql(path, c, "t", who, `${app}'s owns`)})`]),
|
|
@@ -2507,7 +2534,7 @@ export async function compiled(dir = APPS) {
|
|
|
2507
2534
|
const grantsFor = (app: string) => {
|
|
2508
2535
|
if (granted[app]) return granted[app]!;
|
|
2509
2536
|
const roles = Object.entries(defs[app]!.roles ?? {}).map(
|
|
2510
|
-
([r, path]) => [r, whoFn(whoCode(path, defs[app]!.roles!, app, !!defs[app]!.kind, [r]))] as const,
|
|
2537
|
+
([r, path]) => [r, whoFn(whoCode(path, defs[app]!.roles!, app, !!defs[app]!.kind, [r], linkTo))] as const,
|
|
2511
2538
|
);
|
|
2512
2539
|
const memberOf = (name: string) => {
|
|
2513
2540
|
const as = name in casts[app]! ? casts[app]![name] : name;
|
|
@@ -2706,6 +2733,7 @@ export async function compiled(dir = APPS) {
|
|
|
2706
2733
|
statics.has(app),
|
|
2707
2734
|
readOnly[app],
|
|
2708
2735
|
split.has(app) ? `${app}.` : "",
|
|
2736
|
+
linkTo,
|
|
2709
2737
|
),
|
|
2710
2738
|
};
|
|
2711
2739
|
}
|
package/src/definitions.ts
CHANGED
|
@@ -59,6 +59,10 @@
|
|
|
59
59
|
// held as within is.
|
|
60
60
|
// notEmpty fields of words that are never none, nor only spaces: { notes: ["text"] }, refused "text cannot be empty"
|
|
61
61
|
// (a trigger). One a store holds already, from before it was said, is left until it's changed.
|
|
62
|
+
// person what's a person's alone, by collection: { supplies: { add: "an order to a supplier is a person's to place" } }.
|
|
63
|
+
// Each op (add, remove, or with its field: "replace price", "add people[]") is refused in those words to a
|
|
64
|
+
// write made via anything (the member's agent's link, a route), whatever their roles; by their own hand on
|
|
65
|
+
// a page it goes as the grants say (src/write.ts).
|
|
62
66
|
// waits { by: "moderated", posts: "waiting", refused: "…" }: in a kind, while the root item's field is true, an
|
|
63
67
|
// add to posts gives its field that moves (moves) as that first step, or it's refused in those words.
|
|
64
68
|
//
|
|
@@ -69,6 +73,9 @@
|
|
|
69
73
|
// holdings@members the members named by any item of a collection of this document; posts[role=trader]@members
|
|
70
74
|
// those of the items whose field is that; members[!inviter] the items with no inviter,
|
|
71
75
|
// themselves members; fund/posts@members, a collection of another document
|
|
76
|
+
// ties[of=@][role=director,accountant]@who>@member
|
|
77
|
+
// a link followed: the ties whose of is the item asked of, with either role, and the item who
|
|
78
|
+
// links to asked what follows `>`, a term or a group in brackets, ( … | … ): the member it names
|
|
72
79
|
|
|
73
80
|
import type { Op } from "./ops.ts";
|
|
74
81
|
|
|
@@ -115,6 +122,9 @@ export type Definitions = {
|
|
|
115
122
|
apart?: Record<string, { lists: string[]; refused: string }[]>;
|
|
116
123
|
/** Fields of words that are never none, nor only spaces: by collection. */
|
|
117
124
|
notEmpty?: Record<string, string[]>;
|
|
125
|
+
/** What's a person's alone: by collection, each op (add, remove, or with its field: "replace price") and the words
|
|
126
|
+
* it's refused in to a write made via anything (an agent's link, a route). */
|
|
127
|
+
person?: Record<string, Record<string, string>>;
|
|
118
128
|
/** In a kind, a yes-or-no field of the root item: while it's true, an add to each collection named gives its field
|
|
119
129
|
* that moves as this first step, or it's refused in these words (a board held for approval, a post waiting).
|
|
120
130
|
* src/settings.ts. */
|
|
@@ -158,17 +168,73 @@ export type Takes = { while: string; refused: string } & Record<string, string>;
|
|
|
158
168
|
* each of its lines' qty times its price. */
|
|
159
169
|
export type Total = { of: string; by: string; sum: string; times?: string };
|
|
160
170
|
|
|
171
|
+
/** A who-path's terms: split at each "|" outside a group's brackets. */
|
|
172
|
+
const termsOf = (path: string) => {
|
|
173
|
+
const out: string[] = [];
|
|
174
|
+
let depth = 0,
|
|
175
|
+
from = 0;
|
|
176
|
+
for (let i = 0; i < path.length; i++) {
|
|
177
|
+
if (path[i] === "(") depth++;
|
|
178
|
+
else if (path[i] === ")") depth--;
|
|
179
|
+
else if (path[i] === "|" && !depth) {
|
|
180
|
+
out.push(path.slice(from, i).trim());
|
|
181
|
+
from = i + 1;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return [...out, path.slice(from).trim()];
|
|
185
|
+
};
|
|
186
|
+
/** A term that's a group, `( … | … )`: the who-path inside it. */
|
|
187
|
+
const groupOf = (t: string) => (t.startsWith("(") && t.endsWith(")") ? t.slice(1, -1) : undefined);
|
|
188
|
+
/** A term of the item's own field: `@field`, the members it names, or `@field>term`, what it links to, asked a term. */
|
|
189
|
+
const FIELD = /^@([a-z][a-z0-9-]*)(?:>(.+))?$/s;
|
|
190
|
+
/** A term of a collection: `[doc/]collection`, any filters (`[field=value]`, `[field=a,b]` any of them, `[!field]`
|
|
191
|
+
* none, `[field=@]` the item it's asked of), then `@field`, the members it names, or `@field>term`: the item that one-id
|
|
192
|
+
* or list link of its points at, asked the term (a relation's who, `>@member`, the member who's that entity). */
|
|
193
|
+
export const TERM = /^(?:([a-z][a-z0-9-]*)\/)?([a-z][a-z0-9-]*)((?:\[[^\]]*\])*)(?:@([a-z][a-z0-9-]*)(?:>(.+))?)?$/s;
|
|
194
|
+
/** A filter of a term: a field none, any of some values, or the item a who-path is asked of. */
|
|
195
|
+
type Filter = { not: boolean; field: string; values?: string[]; self?: true };
|
|
196
|
+
/** A term of a collection, read; refused in words where it isn't one. */
|
|
197
|
+
export function termOf(t: string, where: string) {
|
|
198
|
+
const [, of, coll, fs = "", to, then] = t.match(TERM) ?? [];
|
|
199
|
+
const filters = [...fs.matchAll(/\[([^\]]*)\]/g)].map(([, f]): Filter | null => {
|
|
200
|
+
const [, not, field, value] = f!.match(/^(!?)([a-z][a-z0-9-]*)(?:=(.+))?$/) ?? [];
|
|
201
|
+
if (!field || !!not === (value !== undefined)) return null;
|
|
202
|
+
return {
|
|
203
|
+
not: !!not,
|
|
204
|
+
field,
|
|
205
|
+
...(value === "@" ? { self: true as const } : value ? { values: value.split(",") } : {}),
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
if (!coll || filters.includes(null)) throw new Error(`${where}: ${t} isn't a who-path`);
|
|
209
|
+
return { of, coll, filters: filters as Filter[], to, then };
|
|
210
|
+
}
|
|
161
211
|
/** A who-path of collections in words (links' holds): "staff's posts with role supplier". */
|
|
162
|
-
export const pathSaid = (path: string) =>
|
|
163
|
-
path
|
|
164
|
-
.split("|")
|
|
212
|
+
export const pathSaid = (path: string): string =>
|
|
213
|
+
termsOf(path)
|
|
165
214
|
.map((t) => {
|
|
166
|
-
const
|
|
167
|
-
|
|
215
|
+
const group = groupOf(t);
|
|
216
|
+
if (group !== undefined) return pathSaid(group);
|
|
217
|
+
const own = t.match(FIELD);
|
|
218
|
+
if (own) return own[2] ? `what its ${own[1]} is, ${pathSaid(own[2])}` : `its ${own[1]}`;
|
|
219
|
+
const { of, coll, filters, to, then } = termOf(t, "a who-path");
|
|
220
|
+
const said = filters.map((f) =>
|
|
221
|
+
f.not ? `with no ${f.field}` : f.self ? `whose ${f.field} is it` : `with ${f.field} ${f.values!.join(" or ")}`,
|
|
222
|
+
);
|
|
223
|
+
return `${of ? `${of}'s ` : ""}${coll}${said.length ? ` ${said.join(" and ")}` : ""}${then ? `, whose ${to} is ${pathSaid(then)}` : ""}`;
|
|
168
224
|
})
|
|
169
225
|
.join(" or ");
|
|
170
|
-
|
|
171
|
-
|
|
226
|
+
/** Where a link goes, for a who-path's `@field>term`: of a collection's field (none: whichever collection has the
|
|
227
|
+
* field), the collection it's to, the document that holds it, and whether the field is a list of ids. */
|
|
228
|
+
export type LinkTo = (
|
|
229
|
+
coll: string | undefined,
|
|
230
|
+
field: string,
|
|
231
|
+
) => { coll: string; doc: string; list: boolean } | undefined;
|
|
232
|
+
/** Where a term's `@field>` goes, refused in words where it's no link. */
|
|
233
|
+
function linked(t: string, coll: string | undefined, to: string, where: string, link?: LinkTo) {
|
|
234
|
+
const l = link?.(coll, to);
|
|
235
|
+
if (!l) throw new Error(`${where}: ${t}: ${to} isn't a link to a collection`);
|
|
236
|
+
return l;
|
|
237
|
+
}
|
|
172
238
|
|
|
173
239
|
/** The roles named on the way to a role named in a who-path, it last (`seen`, those before it); refused where it's
|
|
174
240
|
* one of them, as a role can't be itself. */
|
|
@@ -182,50 +248,63 @@ const onTo = (role: string, seen: string[], where: string) => {
|
|
|
182
248
|
|
|
183
249
|
/** A who-path as a JavaScript expression of m (the member), item, read and doc (this document), true when m
|
|
184
250
|
* is one of them: a role in it, that role's who-path (`roles`, each by name; `seen`, those named on the way to it);
|
|
185
|
-
* `item` the item a role's @field is of. In a kind (`kind`), `item` is the root's `
|
|
186
|
-
*
|
|
187
|
-
*
|
|
251
|
+
* `item` the item a role's @field is of, and `[field=@]`'s. In a kind (`kind`), `item` is the root's and `at` the item
|
|
252
|
+
* a write is to: an @field is the item's where it has that field (a post's author), else the root's (the board's
|
|
253
|
+
* owners). After `@field>`, the item that links to is `item`, as no kind's root. */
|
|
188
254
|
export function whoCode(
|
|
189
255
|
path: string,
|
|
190
256
|
roles: Record<string, string>,
|
|
191
257
|
where: string,
|
|
192
258
|
kind = false,
|
|
193
259
|
seen: string[] = [],
|
|
260
|
+
link?: LinkTo,
|
|
194
261
|
): string {
|
|
195
262
|
const names = (v: string) => `(${v} === m || (Array.isArray(${v}) && ${v}.includes(m)))`;
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
.
|
|
263
|
+
// the item (or items) an id or ids (`v`) of a link name, each asked a who-path as the item
|
|
264
|
+
const follow = (v: string, l: { coll: string; doc: string }, then: string) =>
|
|
265
|
+
`([] as unknown[]).concat(${v} ?? []).some((id: unknown) => ((item: any, at: any) => ${whoCode(then, roles, where, false, seen, link)})((read(${JSON.stringify(l.doc)})[${JSON.stringify(l.coll)}] ?? []).find((r: any) => r.id === id), undefined))`;
|
|
266
|
+
return termsOf(path)
|
|
199
267
|
.map((t) => {
|
|
200
268
|
if (t === "member") return "true";
|
|
201
|
-
if (roles[t] !== undefined) return `(${whoCode(roles[t], roles, where, kind, onTo(t, seen, where))})`;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
269
|
+
if (roles[t] !== undefined) return `(${whoCode(roles[t], roles, where, kind, onTo(t, seen, where), link)})`;
|
|
270
|
+
const group = groupOf(t);
|
|
271
|
+
if (group !== undefined) return `(${whoCode(group, roles, where, kind, seen, link)})`;
|
|
272
|
+
const own = t.match(FIELD);
|
|
273
|
+
if (own) {
|
|
274
|
+
const f = JSON.stringify(own[1]);
|
|
275
|
+
const v = kind ? `(at != null && ${f} in at ? at : item)?.[${f}]` : `item?.[${f}]`;
|
|
276
|
+
if (own[2]) return follow(v, linked(t, undefined, own[1]!, where, link), own[2]);
|
|
277
|
+
return kind ? `((v: any) => v === m || (Array.isArray(v) && v.includes(m)))(${v})` : names(v);
|
|
207
278
|
}
|
|
208
|
-
const
|
|
209
|
-
if (!coll || (not && value)) throw new Error(`${where}: ${t} isn't a who-path`);
|
|
279
|
+
const { of, coll, filters, to, then } = termOf(t, where);
|
|
210
280
|
const at = (f: string) => `i[${JSON.stringify(f)}]`;
|
|
211
|
-
const cond =
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
281
|
+
const cond =
|
|
282
|
+
filters
|
|
283
|
+
.map((f) =>
|
|
284
|
+
f.not
|
|
285
|
+
? `${at(f.field)} === undefined`
|
|
286
|
+
: f.self
|
|
287
|
+
? `${at(f.field)} === item?.id`
|
|
288
|
+
: f.values!.length === 1
|
|
289
|
+
? `String(${at(f.field)}) === ${JSON.stringify(f.values![0])}`
|
|
290
|
+
: `${JSON.stringify(f.values)}.includes(String(${at(f.field)}))`,
|
|
291
|
+
)
|
|
292
|
+
.join(" && ") || "true";
|
|
293
|
+
const end = !to ? "i.id === m" : then ? follow(at(to), linked(t, coll, to, where, link), then) : names(at(to));
|
|
294
|
+
return `(read(${of ? JSON.stringify(of) : "doc"})[${JSON.stringify(coll)}] ?? []).some((i: any) => ${cond} && ${end})`;
|
|
217
295
|
})
|
|
218
296
|
.join(" || ");
|
|
219
297
|
}
|
|
220
298
|
|
|
221
299
|
/** What a who-path is asked of, as SQL: the roles (each a who-path), which fields are lists (a table of pairs), the
|
|
222
|
-
* document's own collections,
|
|
223
|
-
*
|
|
300
|
+
* document's own collections, of a kind, the column its rows are kept with their instance by and its root's table
|
|
301
|
+
* (`$key`, the instance), and where a link goes, for `@field>`. */
|
|
224
302
|
export type WhoOf = {
|
|
225
303
|
roles: Record<string, string>;
|
|
226
304
|
list: (coll: string, field: string) => boolean;
|
|
227
305
|
own: string[];
|
|
228
306
|
kind?: { column: string; root: string };
|
|
307
|
+
link?: LinkTo;
|
|
229
308
|
};
|
|
230
309
|
/** A who-path as SQL of a row (`at`, of collection `coll`): true when $member is one of them. The same path its code
|
|
231
310
|
* is written from (whoCode), so the query holds what's asked of an item. A role's @field is its kind's root item's. */
|
|
@@ -243,30 +322,53 @@ export function whoSql(
|
|
|
243
322
|
who.list(c, f)
|
|
244
323
|
? `EXISTS (SELECT 1 FROM ${q(`${c}_${f}`)} p WHERE p.${q(c)} = ${a}.id AND p.${q(f)} = $member)`
|
|
245
324
|
: `${a}.${q(f)} = $member`;
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
325
|
+
// the row (`w`, named apart from every row it's within) a link of a row of `c` names, asked a who-path as no kind's
|
|
326
|
+
// root: the row's id is `row`, its one id `one`
|
|
327
|
+
const n = /^w\d+$/.test(at) ? Number(at.slice(1)) + 1 : 1;
|
|
328
|
+
const follow = (t: string, c: string, f: string, row: string, one: string, then: string) => {
|
|
329
|
+
const l = linked(t, c, f, where, who.link),
|
|
330
|
+
w = `w${n}`;
|
|
331
|
+
const inner = whoSql(then, l.coll, w, { ...who, kind: undefined }, where, false, seen);
|
|
332
|
+
return l.list
|
|
333
|
+
? `EXISTS (SELECT 1 FROM ${q(`${c}_${f}`)} p${n} JOIN ${q(l.coll)} ${w} ON ${w}.id = p${n}.${q(f)} WHERE p${n}.${q(c)} = ${row} AND (${inner}))`
|
|
334
|
+
: `EXISTS (SELECT 1 FROM ${q(l.coll)} ${w} WHERE ${w}.id = ${one} AND (${inner}))`;
|
|
335
|
+
};
|
|
336
|
+
return termsOf(path)
|
|
249
337
|
.map((t) => {
|
|
250
338
|
if (t === "member") return "TRUE";
|
|
251
339
|
if (who.roles[t]) return `(${whoSql(who.roles[t]!, coll, at, who, where, true, onTo(t, seen, where))})`;
|
|
252
|
-
|
|
253
|
-
|
|
340
|
+
const group = groupOf(t);
|
|
341
|
+
if (group !== undefined) return `(${whoSql(group, coll, at, who, where, root, seen)})`;
|
|
342
|
+
const own = t.match(FIELD);
|
|
343
|
+
if (own) {
|
|
344
|
+
const f = own[1]!;
|
|
254
345
|
// in a role of a kind, the root item's field
|
|
255
|
-
if (root && who.kind)
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
346
|
+
if (root && who.kind) {
|
|
347
|
+
const r = who.kind.root;
|
|
348
|
+
if (own[2]) return follow(t, r, f, "$key", `(SELECT r.${q(f)} FROM ${q(r)} r WHERE r.id = $key)`, own[2]);
|
|
349
|
+
return who.list(r, f)
|
|
350
|
+
? `EXISTS (SELECT 1 FROM ${q(`${r}_${f}`)} p WHERE p.${q(r)} = $key AND p.${q(f)} = $member)`
|
|
351
|
+
: `(SELECT r.${q(f)} FROM ${q(r)} r WHERE r.id = $key) = $member`;
|
|
352
|
+
}
|
|
353
|
+
if (own[2]) return follow(t, coll, f, `${at}.id`, `${at}.${q(f)}`, own[2]);
|
|
259
354
|
return names(coll, at, f);
|
|
260
355
|
}
|
|
261
|
-
const
|
|
262
|
-
|
|
356
|
+
const { of, coll: c, filters, to, then } = termOf(t, where);
|
|
357
|
+
const text = (v: string) => `'${v.replaceAll("'", "''")}'`;
|
|
263
358
|
const cond = [
|
|
264
|
-
...(
|
|
265
|
-
|
|
266
|
-
|
|
359
|
+
...filters.map((f) =>
|
|
360
|
+
f.not
|
|
361
|
+
? `i.${q(f.field)} IS NULL`
|
|
362
|
+
: f.self
|
|
363
|
+
? // the item it's asked of: in a role of a kind, its root item, the instance's
|
|
364
|
+
`i.${q(f.field)} = ${root && who.kind ? "$key" : `${at}.id`}`
|
|
365
|
+
: f.values!.length === 1
|
|
366
|
+
? `CAST(i.${q(f.field)} AS TEXT) = ${text(f.values![0]!)}`
|
|
367
|
+
: `CAST(i.${q(f.field)} AS TEXT) IN (${f.values!.map(text).join(", ")})`,
|
|
368
|
+
),
|
|
267
369
|
// the document's own collection, of a kind: its instance's rows
|
|
268
370
|
...(!of && who.kind && who.own.includes(c) ? [`i.${q(who.kind.column)} = $key`] : []),
|
|
269
|
-
to ?
|
|
371
|
+
!to ? "i.id = $member" : then ? follow(t, c, to, "i.id", `i.${q(to)}`, then) : names(c, "i", to),
|
|
270
372
|
];
|
|
271
373
|
return `EXISTS (SELECT 1 FROM ${q(c)} i WHERE ${cond.join(" AND ")})`;
|
|
272
374
|
})
|
|
@@ -291,6 +393,7 @@ export function whoRules(
|
|
|
291
393
|
defs: Definitions,
|
|
292
394
|
grants: Record<string, string[]>,
|
|
293
395
|
root?: string,
|
|
396
|
+
link?: LinkTo,
|
|
294
397
|
) {
|
|
295
398
|
const roles = Object.keys(defs.roles ?? {});
|
|
296
399
|
const where = `${doc}'s definitions`;
|
|
@@ -312,7 +415,7 @@ export function whoRules(
|
|
|
312
415
|
? `(read(${JSON.stringify(root)})[${JSON.stringify(k.root)}] ?? []).find((i: any) => i.id === Number(doc.split(/[:?]/)[1]))`
|
|
313
416
|
: "at";
|
|
314
417
|
out += `\n/** Who someone is to ${doc} (and to an item of it, \`at\`), beyond a member: each role whose who-path finds them. */\n`;
|
|
315
|
-
out += `export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {\n const item: any = ${item};\n return [${roles.map((r) => `\n ${whoCode(defs.roles![r]!, defs.roles!, where, !!k, [r])} ? ${JSON.stringify(r)} : ""`).join(",")},\n ].filter(Boolean);\n}\n`;
|
|
418
|
+
out += `export function rolesOf(m: number, read: (doc: string) => any, doc: string, at?: any): string[] {\n const item: any = ${item};\n return [${roles.map((r) => `\n ${whoCode(defs.roles![r]!, defs.roles!, where, !!k, [r], link)} ? ${JSON.stringify(r)} : ""`).join(",")},\n ].filter(Boolean);\n}\n`;
|
|
316
419
|
return out + sight(doc, collections, defs, roles, where);
|
|
317
420
|
}
|
|
318
421
|
|
package/src/stories.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { tmpdir } from "node:os";
|
|
|
7
7
|
import { APPS, ENGINE, OWN, type Story } from "./compile.ts";
|
|
8
8
|
import type { Dialect } from "./engine.ts";
|
|
9
9
|
import { start } from "./server.ts";
|
|
10
|
+
import { openKeys } from "./members/keys.ts";
|
|
10
11
|
import type { Member } from "./members/trust.ts";
|
|
11
12
|
import type { Op } from "./ops.ts";
|
|
12
13
|
import { madeBy, partsOf, type Part, type Told } from "./together.ts";
|
|
@@ -64,14 +65,25 @@ export async function fresh({
|
|
|
64
65
|
);
|
|
65
66
|
const server = await start({ port: 0, dir, db, engine, keys, files, rootToken: open ? "" : root });
|
|
66
67
|
/** A name's token: a member's, by their name (or the cast's), or undefined for nobody. */
|
|
67
|
-
const
|
|
68
|
+
const memberFor = async (name: string, cast: Record<string, string | null>) => {
|
|
68
69
|
const as = name in cast ? cast[name] : name;
|
|
69
70
|
if (as === null) return undefined;
|
|
70
71
|
const m = ((await server.read("members")).members as Member[]).find(
|
|
71
72
|
(x) => x.name.toLowerCase() === as!.toLowerCase(),
|
|
72
73
|
);
|
|
73
74
|
if (!m) throw new Error(`no member named ${as}`);
|
|
74
|
-
return
|
|
75
|
+
return m.id;
|
|
76
|
+
};
|
|
77
|
+
const tokenFor = async (name: string, cast: Record<string, string | null> = {}) => {
|
|
78
|
+
const m = await memberFor(name, cast);
|
|
79
|
+
return m === undefined ? undefined : (await server.tokenOf(m))!;
|
|
80
|
+
};
|
|
81
|
+
/** A name's agent's link's token, made for them under a label: what a story told via it is sent by. */
|
|
82
|
+
const agentFor = async (name: string, label: string, cast: Record<string, string | null> = {}) => {
|
|
83
|
+
const m = (await memberFor(name, cast))!,
|
|
84
|
+
held = await openKeys(server.store.db, root);
|
|
85
|
+
await held.makeAgent(m, label);
|
|
86
|
+
return (await held.agents(m)).at(-1)!.token;
|
|
75
87
|
};
|
|
76
88
|
return {
|
|
77
89
|
server,
|
|
@@ -80,6 +92,7 @@ export async function fresh({
|
|
|
80
92
|
files,
|
|
81
93
|
root,
|
|
82
94
|
tokenFor,
|
|
95
|
+
agentFor,
|
|
83
96
|
stop: async () => {
|
|
84
97
|
await server.stop(true);
|
|
85
98
|
await server.store.close();
|
|
@@ -164,7 +177,7 @@ export async function tell(doc: string, dir = APPS, engine = ENGINE) {
|
|
|
164
177
|
for (const s of await toldOf(doc, dir)) {
|
|
165
178
|
const anonymous = s.as === undefined,
|
|
166
179
|
as = s.as ?? ANYONE;
|
|
167
|
-
const { server, tokenFor, stop } = await fresh({ dir, open: anonymous, engine });
|
|
180
|
+
const { server, tokenFor, agentFor, stop } = await fresh({ dir, open: anonymous, engine });
|
|
168
181
|
// the story's own document: of a kind, its instance (shelf:1); of several writes, its first's (src/together.ts),
|
|
169
182
|
// and each other's there is before it (not one the write makes, board:$1)
|
|
170
183
|
const write = s.does ?? s.cannot!,
|
|
@@ -176,7 +189,9 @@ export async function tell(doc: string, dir = APPS, engine = ENGINE) {
|
|
|
176
189
|
? s.premise.map((w) => [ANYONE, w] as const)
|
|
177
190
|
: Object.entries(s.premise ?? {}).flatMap(([n, ws]) => ws.map((w) => [n, w] as const));
|
|
178
191
|
const names = [...new Set([...premise.map(([n]) => n), as, ...(s.seen ?? []), ...(s.unseen ?? [])])];
|
|
179
|
-
|
|
192
|
+
// a story told via an agent is sent by its teller's agent's link, made for it under that label
|
|
193
|
+
const tokenOf = (n: string) =>
|
|
194
|
+
n === ANYONE ? undefined : n === as && s.via ? agentFor(n, s.via, cast) : tokenFor(n, cast);
|
|
180
195
|
// the premise's pages first; the story's own once the premise has run, so it may be someone the premise made
|
|
181
196
|
const pages: Record<string, Awaited<ReturnType<typeof page>>> = {};
|
|
182
197
|
const open = async (ns: string[]) => {
|
|
@@ -291,6 +306,7 @@ export async function tell(doc: string, dir = APPS, engine = ENGINE) {
|
|
|
291
306
|
const short = told === "whole" ? !m.ops.length : told ? !heard.length : heard.join() !== ops.join();
|
|
292
307
|
if (short) wrong.push(`${s.story}: ${n} heard only ${heard.join(", ")}`);
|
|
293
308
|
if (m.by !== writer) wrong.push(`${s.story}: ${n} heard it as by ${m.by}, not ${writer}`);
|
|
309
|
+
if ((m.via ?? null) !== (s.via ?? null)) wrong.push(`${s.story}: ${n} heard it as via ${m.via ?? "nothing"}`);
|
|
294
310
|
}
|
|
295
311
|
for (const n of (await Promise.all(unseen)).filter(Boolean)) wrong.push(`${s.story}: ${n} heard it, unseen`);
|
|
296
312
|
// what it leaves: each path as its teller holds it, on a page of theirs that holds the document anew
|
|
@@ -358,8 +374,9 @@ export async function inBrowser(app: string, path: (doc: string) => string | nul
|
|
|
358
374
|
const cast = await castOf(app, dir);
|
|
359
375
|
const wrong: string[] = [];
|
|
360
376
|
for (const s of await toldOf(app, dir)) {
|
|
361
|
-
// (a write of several documents is no one element's: a page sends it by together(), joined/page
|
|
362
|
-
|
|
377
|
+
// (a write of several documents is no one element's: a page sends it by together(), joined/page; one told via an
|
|
378
|
+
// agent is no page's)
|
|
379
|
+
const write = Array.isArray(s.does) || s.via ? undefined : s.does;
|
|
363
380
|
const at = write && path(write.doc);
|
|
364
381
|
if (!write || at === null || at === undefined) continue;
|
|
365
382
|
const name = write.doc.split(/[:?]/)[0]!;
|
package/src/write.ts
CHANGED
|
@@ -52,6 +52,9 @@ export type Rules = StoreRules & {
|
|
|
52
52
|
moves?: Record<string, string>;
|
|
53
53
|
/** The field holding an item's own code, by collection: a page holding the document by it is the item's guest. */
|
|
54
54
|
guests?: Record<string, string>;
|
|
55
|
+
/** What's a person's alone (definitions' person): by collection, each op and the words it's refused in to a write
|
|
56
|
+
* made via anything. */
|
|
57
|
+
person?: Record<string, Record<string, string>>;
|
|
55
58
|
/** Of a kind, its root item's own fields that govern it (definitions' frozen and waits). */
|
|
56
59
|
settings?: Settings;
|
|
57
60
|
/** The field of a kind's root holding its guest's level (definitions' visitors, src/visitors.ts). */
|
|
@@ -274,6 +277,19 @@ export function writes({
|
|
|
274
277
|
}
|
|
275
278
|
return null;
|
|
276
279
|
}
|
|
280
|
+
/** Why a write made via something (an agent's link, a route) is refused, in its definitions' words, or null: an op
|
|
281
|
+
* that's a person's alone (definitions' person), theirs by their own hand on a page. */
|
|
282
|
+
function byHand(doc: string, ops: Op[]) {
|
|
283
|
+
const { person } = rulesOf(doc);
|
|
284
|
+
for (const o of ops) {
|
|
285
|
+
const { coll } = parts(o.path),
|
|
286
|
+
[op, at] = grantOf(o).split(" ") as [string, string],
|
|
287
|
+
field = at.slice(coll.length + 1);
|
|
288
|
+
const said = person?.[coll]?.[field ? `${op} ${field}` : op];
|
|
289
|
+
if (said) return said;
|
|
290
|
+
}
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
277
293
|
/** Why a write's value isn't the kind of thing its op takes (an item added is its fields; a list, ids), or null: asked
|
|
278
294
|
* before any rule reads it, so a rule may. */
|
|
279
295
|
function misshapen(doc: string, ops: Op[]) {
|
|
@@ -654,7 +670,7 @@ export function writes({
|
|
|
654
670
|
doc: string,
|
|
655
671
|
ops: Op[],
|
|
656
672
|
why: string | undefined,
|
|
657
|
-
who: { member: number } | null,
|
|
673
|
+
who: { member: number; agent?: unknown } | null,
|
|
658
674
|
group: (
|
|
659
675
|
doc: string,
|
|
660
676
|
ops: Op[],
|
|
@@ -687,6 +703,8 @@ export function writes({
|
|
|
687
703
|
anyoneIs(doc, who?.member ?? null),
|
|
688
704
|
undoing,
|
|
689
705
|
) ??
|
|
706
|
+
// what's a person's alone, of a write made via anything: after the grants, so only someone they allow hears it
|
|
707
|
+
(who?.agent ? byHand(doc, ops) : null) ??
|
|
690
708
|
namedIn(doc, ops) ??
|
|
691
709
|
// of a kind, what its root item's settings say of a write the grants allow (a frozen board, one held for approval)
|
|
692
710
|
settled(
|