@benchme/site-kit 0.10.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/dist/auth-routes.js +5 -2
  2. package/dist/auth-routes.js.map +1 -1
  3. package/dist/auth-service.d.ts +9 -1
  4. package/dist/auth-service.js +14 -3
  5. package/dist/auth-service.js.map +1 -1
  6. package/dist/domain-error.d.ts +6 -0
  7. package/dist/domain-error.js +12 -0
  8. package/dist/domain-error.js.map +1 -0
  9. package/dist/doors/bridge-credential.d.ts +47 -0
  10. package/dist/doors/bridge-credential.js +80 -0
  11. package/dist/doors/bridge-credential.js.map +1 -0
  12. package/dist/doors/caller.d.ts +25 -0
  13. package/dist/doors/caller.js +22 -0
  14. package/dist/doors/caller.js.map +1 -0
  15. package/dist/doors/door.d.ts +12 -0
  16. package/dist/doors/door.js +18 -0
  17. package/dist/doors/door.js.map +1 -0
  18. package/dist/doors/register-doors.d.ts +44 -0
  19. package/dist/doors/register-doors.js +92 -0
  20. package/dist/doors/register-doors.js.map +1 -0
  21. package/dist/doors/request-door.d.ts +19 -0
  22. package/dist/doors/request-door.js +29 -0
  23. package/dist/doors/request-door.js.map +1 -0
  24. package/dist/doors/session-carrier.d.ts +42 -0
  25. package/dist/doors/session-carrier.js +44 -0
  26. package/dist/doors/session-carrier.js.map +1 -0
  27. package/dist/doors/sign-in-tools.d.ts +23 -0
  28. package/dist/doors/sign-in-tools.js +69 -0
  29. package/dist/doors/sign-in-tools.js.map +1 -0
  30. package/dist/doors/sign-in.d.ts +20 -0
  31. package/dist/doors/sign-in.js +31 -0
  32. package/dist/doors/sign-in.js.map +1 -0
  33. package/dist/html.d.ts +5 -1
  34. package/dist/html.js +23 -4
  35. package/dist/html.js.map +1 -1
  36. package/dist/index.d.ts +18 -3
  37. package/dist/index.js +18 -3
  38. package/dist/index.js.map +1 -1
  39. package/dist/ledger/auth-rules.d.ts +11 -0
  40. package/dist/ledger/auth-rules.js +21 -0
  41. package/dist/ledger/auth-rules.js.map +1 -0
  42. package/dist/ledger/entry.d.ts +219 -0
  43. package/dist/ledger/entry.js +40 -0
  44. package/dist/ledger/entry.js.map +1 -0
  45. package/dist/ledger/ledger-route.d.ts +15 -0
  46. package/dist/ledger/ledger-route.js +25 -0
  47. package/dist/ledger/ledger-route.js.map +1 -0
  48. package/dist/ledger/ledger.d.ts +38 -0
  49. package/dist/ledger/ledger.js +55 -0
  50. package/dist/ledger/ledger.js.map +1 -0
  51. package/dist/ledger/ledgered.d.ts +36 -0
  52. package/dist/ledger/ledgered.js +108 -0
  53. package/dist/ledger/ledgered.js.map +1 -0
  54. package/dist/ledger/rules.d.ts +45 -0
  55. package/dist/ledger/rules.js +22 -0
  56. package/dist/ledger/rules.js.map +1 -0
  57. package/dist/ledger/schema.d.ts +23 -0
  58. package/dist/ledger/schema.js +56 -0
  59. package/dist/ledger/schema.js.map +1 -0
  60. package/dist/mcp-server.d.ts +11 -6
  61. package/dist/mcp-server.js +24 -15
  62. package/dist/mcp-server.js.map +1 -1
  63. package/dist/refusal.d.ts +8 -0
  64. package/dist/refusal.js +8 -0
  65. package/dist/refusal.js.map +1 -0
  66. package/dist/request-header.d.ts +3 -0
  67. package/dist/request-header.js +6 -0
  68. package/dist/request-header.js.map +1 -0
  69. package/dist/users-repo.d.ts +32 -0
  70. package/dist/users-repo.js +69 -0
  71. package/dist/users-repo.js.map +1 -1
  72. package/dist/webmcp.d.ts +19 -2
  73. package/dist/webmcp.js +25 -4
  74. package/dist/webmcp.js.map +1 -1
  75. package/dist/workspace-scope.d.ts +18 -2
  76. package/dist/workspace-scope.js +36 -8
  77. package/dist/workspace-scope.js.map +1 -1
  78. package/package.json +2 -2
@@ -0,0 +1,219 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * What an entry records: a `read` of one entity, a `write` (a change to the
4
+ * site's rows, made or refused), or an `auth` step (an account signed up,
5
+ * verified, signed in). Only a `write` is a change to what the site sells.
6
+ */
7
+ export declare const LEDGER_KINDS: readonly ["read", "write", "auth"];
8
+ export type LedgerKind = (typeof LEDGER_KINDS)[number];
9
+ export declare const LEDGER_OUTCOMES: readonly ["ok", "refused"];
10
+ export type LedgerOutcome = (typeof LEDGER_OUTCOMES)[number];
11
+ /** The action of every `read` entry. A write's action is the site's own verb (create, cancel, change …). */
12
+ export declare const READ_ACTION = "read";
13
+ /** The code of a read that found nothing. */
14
+ export declare const NOT_FOUND_CODE = "NOT_FOUND";
15
+ /** One line of a site's change ledger, as the verifier reads it. */
16
+ export declare const ledgerEntrySchema: z.ZodObject<{
17
+ /** Order within one app's ledger. */
18
+ seq: z.ZodNumber;
19
+ /** When it happened, by the database's clock (ISO 8601, UTC). */
20
+ at: z.ZodString;
21
+ app: z.ZodString;
22
+ kind: z.ZodEnum<["read", "write", "auth"]>;
23
+ /** `id` is null when there is no entity yet: a create the rules refused. */
24
+ entity: z.ZodObject<{
25
+ type: z.ZodString;
26
+ id: z.ZodNullable<z.ZodString>;
27
+ }, "strip", z.ZodTypeAny, {
28
+ id: string | null;
29
+ type: string;
30
+ }, {
31
+ id: string | null;
32
+ type: string;
33
+ }>;
34
+ /**
35
+ * What the request was about, when it names something other than the entity it makes or changes: the order a
36
+ * return is for, the showtime a booking is for, the event a registration is for. A refused create has no entity
37
+ * id, so this is what says which one it was. Null when the request names nothing else.
38
+ */
39
+ subject: z.ZodNullable<z.ZodObject<{
40
+ type: z.ZodString;
41
+ id: z.ZodString;
42
+ }, "strip", z.ZodTypeAny, {
43
+ id: string;
44
+ type: string;
45
+ }, {
46
+ id: string;
47
+ type: string;
48
+ }>>;
49
+ action: z.ZodString;
50
+ door: z.ZodEnum<["page", "rest", "mcp", "webmcp"]>;
51
+ /** The email signed in on the request, or null for nobody. */
52
+ actor: z.ZodNullable<z.ZodString>;
53
+ outcome: z.ZodEnum<["ok", "refused"]>;
54
+ /** The site's reason code when refused, else null. */
55
+ code: z.ZodNullable<z.ZodString>;
56
+ }, "strip", z.ZodTypeAny, {
57
+ code: string | null;
58
+ at: string;
59
+ door: "page" | "rest" | "mcp" | "webmcp";
60
+ seq: number;
61
+ app: string;
62
+ kind: "read" | "write" | "auth";
63
+ entity: {
64
+ id: string | null;
65
+ type: string;
66
+ };
67
+ subject: {
68
+ id: string;
69
+ type: string;
70
+ } | null;
71
+ action: string;
72
+ actor: string | null;
73
+ outcome: "ok" | "refused";
74
+ }, {
75
+ code: string | null;
76
+ at: string;
77
+ door: "page" | "rest" | "mcp" | "webmcp";
78
+ seq: number;
79
+ app: string;
80
+ kind: "read" | "write" | "auth";
81
+ entity: {
82
+ id: string | null;
83
+ type: string;
84
+ };
85
+ subject: {
86
+ id: string;
87
+ type: string;
88
+ } | null;
89
+ action: string;
90
+ actor: string | null;
91
+ outcome: "ok" | "refused";
92
+ }>;
93
+ export type LedgerEntry = z.infer<typeof ledgerEntrySchema>;
94
+ /** What a site hands the ledger; the ledger adds the order, the time and the app. */
95
+ export type NewLedgerEntry = Omit<LedgerEntry, "seq" | "at" | "app">;
96
+ /** A page of entries, in the envelope the sites' list routes use. */
97
+ export declare const ledgerPageSchema: z.ZodObject<{
98
+ items: z.ZodArray<z.ZodObject<{
99
+ /** Order within one app's ledger. */
100
+ seq: z.ZodNumber;
101
+ /** When it happened, by the database's clock (ISO 8601, UTC). */
102
+ at: z.ZodString;
103
+ app: z.ZodString;
104
+ kind: z.ZodEnum<["read", "write", "auth"]>;
105
+ /** `id` is null when there is no entity yet: a create the rules refused. */
106
+ entity: z.ZodObject<{
107
+ type: z.ZodString;
108
+ id: z.ZodNullable<z.ZodString>;
109
+ }, "strip", z.ZodTypeAny, {
110
+ id: string | null;
111
+ type: string;
112
+ }, {
113
+ id: string | null;
114
+ type: string;
115
+ }>;
116
+ /**
117
+ * What the request was about, when it names something other than the entity it makes or changes: the order a
118
+ * return is for, the showtime a booking is for, the event a registration is for. A refused create has no entity
119
+ * id, so this is what says which one it was. Null when the request names nothing else.
120
+ */
121
+ subject: z.ZodNullable<z.ZodObject<{
122
+ type: z.ZodString;
123
+ id: z.ZodString;
124
+ }, "strip", z.ZodTypeAny, {
125
+ id: string;
126
+ type: string;
127
+ }, {
128
+ id: string;
129
+ type: string;
130
+ }>>;
131
+ action: z.ZodString;
132
+ door: z.ZodEnum<["page", "rest", "mcp", "webmcp"]>;
133
+ /** The email signed in on the request, or null for nobody. */
134
+ actor: z.ZodNullable<z.ZodString>;
135
+ outcome: z.ZodEnum<["ok", "refused"]>;
136
+ /** The site's reason code when refused, else null. */
137
+ code: z.ZodNullable<z.ZodString>;
138
+ }, "strip", z.ZodTypeAny, {
139
+ code: string | null;
140
+ at: string;
141
+ door: "page" | "rest" | "mcp" | "webmcp";
142
+ seq: number;
143
+ app: string;
144
+ kind: "read" | "write" | "auth";
145
+ entity: {
146
+ id: string | null;
147
+ type: string;
148
+ };
149
+ subject: {
150
+ id: string;
151
+ type: string;
152
+ } | null;
153
+ action: string;
154
+ actor: string | null;
155
+ outcome: "ok" | "refused";
156
+ }, {
157
+ code: string | null;
158
+ at: string;
159
+ door: "page" | "rest" | "mcp" | "webmcp";
160
+ seq: number;
161
+ app: string;
162
+ kind: "read" | "write" | "auth";
163
+ entity: {
164
+ id: string | null;
165
+ type: string;
166
+ };
167
+ subject: {
168
+ id: string;
169
+ type: string;
170
+ } | null;
171
+ action: string;
172
+ actor: string | null;
173
+ outcome: "ok" | "refused";
174
+ }>, "many">;
175
+ nextCursor: z.ZodNullable<z.ZodString>;
176
+ }, "strip", z.ZodTypeAny, {
177
+ items: {
178
+ code: string | null;
179
+ at: string;
180
+ door: "page" | "rest" | "mcp" | "webmcp";
181
+ seq: number;
182
+ app: string;
183
+ kind: "read" | "write" | "auth";
184
+ entity: {
185
+ id: string | null;
186
+ type: string;
187
+ };
188
+ subject: {
189
+ id: string;
190
+ type: string;
191
+ } | null;
192
+ action: string;
193
+ actor: string | null;
194
+ outcome: "ok" | "refused";
195
+ }[];
196
+ nextCursor: string | null;
197
+ }, {
198
+ items: {
199
+ code: string | null;
200
+ at: string;
201
+ door: "page" | "rest" | "mcp" | "webmcp";
202
+ seq: number;
203
+ app: string;
204
+ kind: "read" | "write" | "auth";
205
+ entity: {
206
+ id: string | null;
207
+ type: string;
208
+ };
209
+ subject: {
210
+ id: string;
211
+ type: string;
212
+ } | null;
213
+ action: string;
214
+ actor: string | null;
215
+ outcome: "ok" | "refused";
216
+ }[];
217
+ nextCursor: string | null;
218
+ }>;
219
+ export type LedgerPage = z.infer<typeof ledgerPageSchema>;
@@ -0,0 +1,40 @@
1
+ import { z } from "zod";
2
+ import { DOORS } from "../doors/door.js";
3
+ /**
4
+ * What an entry records: a `read` of one entity, a `write` (a change to the
5
+ * site's rows, made or refused), or an `auth` step (an account signed up,
6
+ * verified, signed in). Only a `write` is a change to what the site sells.
7
+ */
8
+ export const LEDGER_KINDS = ["read", "write", "auth"];
9
+ export const LEDGER_OUTCOMES = ["ok", "refused"];
10
+ /** The action of every `read` entry. A write's action is the site's own verb (create, cancel, change …). */
11
+ export const READ_ACTION = "read";
12
+ /** The code of a read that found nothing. */
13
+ export const NOT_FOUND_CODE = "NOT_FOUND";
14
+ /** One line of a site's change ledger, as the verifier reads it. */
15
+ export const ledgerEntrySchema = z.object({
16
+ /** Order within one app's ledger. */
17
+ seq: z.number().int().positive(),
18
+ /** When it happened, by the database's clock (ISO 8601, UTC). */
19
+ at: z.string().datetime(),
20
+ app: z.string().min(1),
21
+ kind: z.enum(LEDGER_KINDS),
22
+ /** `id` is null when there is no entity yet: a create the rules refused. */
23
+ entity: z.object({ type: z.string().min(1), id: z.string().nullable() }),
24
+ /**
25
+ * What the request was about, when it names something other than the entity it makes or changes: the order a
26
+ * return is for, the showtime a booking is for, the event a registration is for. A refused create has no entity
27
+ * id, so this is what says which one it was. Null when the request names nothing else.
28
+ */
29
+ subject: z.object({ type: z.string().min(1), id: z.string().min(1) }).nullable(),
30
+ action: z.string().min(1),
31
+ door: z.enum(DOORS),
32
+ /** The email signed in on the request, or null for nobody. */
33
+ actor: z.string().nullable(),
34
+ outcome: z.enum(LEDGER_OUTCOMES),
35
+ /** The site's reason code when refused, else null. */
36
+ code: z.string().nullable(),
37
+ });
38
+ /** A page of entries, in the envelope the sites' list routes use. */
39
+ export const ledgerPageSchema = z.object({ items: z.array(ledgerEntrySchema), nextCursor: z.string().nullable() });
40
+ //# sourceMappingURL=entry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entry.js","sourceRoot":"","sources":["../../src/ledger/entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAEzC;;;;GAIG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,CAAU,CAAC;AAG/D,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAAI,EAAE,SAAS,CAAU,CAAC;AAG1D,4GAA4G;AAC5G,MAAM,CAAC,MAAM,WAAW,GAAG,MAAM,CAAC;AAClC,6CAA6C;AAC7C,MAAM,CAAC,MAAM,cAAc,GAAG,WAAW,CAAC;AAE1C,oEAAoE;AACpE,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC;IACxC,qCAAqC;IACrC,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE;IAChC,iEAAiE;IACjE,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACzB,GAAG,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACtB,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC;IAC1B,4EAA4E;IAC5E,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC;IACxE;;;;OAIG;IACH,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,QAAQ,EAAE;IAChF,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACzB,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;IACnB,8DAA8D;IAC9D,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAC5B,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC;IAChC,sDAAsD;IACtD,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC5B,CAAC,CAAC;AAMH,qEAAqE;AACrE,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,iBAAiB,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC"}
@@ -0,0 +1,15 @@
1
+ import type { FastifyInstance } from "fastify";
2
+ import type { Ledger } from "./ledger.js";
3
+ /** Where the verifier reads a workspace's ledger, on the app's own address. */
4
+ export declare const ledgerPath: (workspaceId: string) => string;
5
+ /**
6
+ * The ledger's only reader is the verifier, behind two locks. The route sits
7
+ * under /internal/workspaces/, which the gateway never proxies, so no address
8
+ * an agent can open leads here. And it answers only a request that carries the
9
+ * verifier's signature, made with the gateway secret no client holds: a
10
+ * request without one is told there is no such page.
11
+ */
12
+ export declare function registerLedgerRoute(app: FastifyInstance, d: {
13
+ ledger: Ledger;
14
+ gatewaySecret: string;
15
+ }): void;
@@ -0,0 +1,25 @@
1
+ import { VERIFIER_SIG_HEADER, verifyVerifierHeader } from "@benchme/core";
2
+ import { z } from "zod";
3
+ import { header } from "../request-header.js";
4
+ /** Where the verifier reads a workspace's ledger, on the app's own address. */
5
+ export const ledgerPath = (workspaceId) => `/internal/workspaces/${workspaceId}/ledger`;
6
+ const query = z.object({ cursor: z.coerce.number().int().min(0).default(0), limit: z.coerce.number().int().min(1).max(1000).default(500) });
7
+ /**
8
+ * The ledger's only reader is the verifier, behind two locks. The route sits
9
+ * under /internal/workspaces/, which the gateway never proxies, so no address
10
+ * an agent can open leads here. And it answers only a request that carries the
11
+ * verifier's signature, made with the gateway secret no client holds: a
12
+ * request without one is told there is no such page.
13
+ */
14
+ export function registerLedgerRoute(app, d) {
15
+ app.get("/internal/workspaces/:id/ledger", async (req, reply) => {
16
+ const fromVerifier = verifyVerifierHeader(d.gatewaySecret, req.workspaceId, header(req, VERIFIER_SIG_HEADER) ?? "");
17
+ if (!fromVerifier || req.params.id !== req.workspaceId)
18
+ return reply.code(404).send({ error: "NOT_FOUND" });
19
+ const q = query.parse(req.query);
20
+ const rows = await d.ledger.list(req.workspaceId, q.cursor, q.limit + 1);
21
+ const items = rows.slice(0, q.limit);
22
+ return { items, nextCursor: rows.length > q.limit ? String(items[items.length - 1]?.seq ?? q.cursor) : null };
23
+ });
24
+ }
25
+ //# sourceMappingURL=ledger-route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledger-route.js","sourceRoot":"","sources":["../../src/ledger/ledger-route.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAE1E,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAI9C,+EAA+E;AAC/E,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,WAAmB,EAAU,EAAE,CAA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AAE,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAChH,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { Transactions } from "@benchme/core";
2
+ import type { LedgerEntry, NewLedgerEntry } from "./entry.js";
3
+ /** A site's change ledger: appended to by the site's rules, read by the verifier alone. */
4
+ export interface Ledger {
5
+ record(ws: string, entry: NewLedgerEntry): Promise<void>;
6
+ /** Entries after `afterSeq`, oldest first, at most `limit`. */
7
+ list(ws: string, afterSeq: number, limit: number): Promise<LedgerEntry[]>;
8
+ }
9
+ /**
10
+ * The same `ledger` table under any app schema (each site's own migration
11
+ * creates it), so every site keeps its own and none share rows. The time is
12
+ * the database's own clock at the insert, one clock for every site.
13
+ *
14
+ * It takes the site's `transactions`, not a pool, so an entry is always
15
+ * written on the connection of the write it records: handed the raw pool, a
16
+ * ledger would commit an entry on its own while its write could still fail.
17
+ */
18
+ export declare class PgLedger implements Ledger {
19
+ private readonly schema;
20
+ /** The app's name on every entry; a site's schema carries its name. */
21
+ private readonly app;
22
+ private readonly pool;
23
+ constructor(db: Pick<Transactions, "pool">, schema: string,
24
+ /** The app's name on every entry; a site's schema carries its name. */
25
+ app?: string);
26
+ record(ws: string, e: NewLedgerEntry): Promise<void>;
27
+ list(ws: string, afterSeq: number, limit: number): Promise<LedgerEntry[]>;
28
+ }
29
+ /** A ledger in memory, for tests of what records into one. `now` is the clock its entries are stamped with. */
30
+ export declare class MemoryLedger implements Ledger {
31
+ private readonly app;
32
+ private readonly now;
33
+ private readonly byWorkspace;
34
+ private seq;
35
+ constructor(app: string, now?: () => Date);
36
+ record(ws: string, e: NewLedgerEntry): Promise<void>;
37
+ list(ws: string, afterSeq?: number, limit?: number): Promise<LedgerEntry[]>;
38
+ }
@@ -0,0 +1,55 @@
1
+ const SCHEMA = /^[a-z_][a-z0-9_]*$/;
2
+ /**
3
+ * The same `ledger` table under any app schema (each site's own migration
4
+ * creates it), so every site keeps its own and none share rows. The time is
5
+ * the database's own clock at the insert, one clock for every site.
6
+ *
7
+ * It takes the site's `transactions`, not a pool, so an entry is always
8
+ * written on the connection of the write it records: handed the raw pool, a
9
+ * ledger would commit an entry on its own while its write could still fail.
10
+ */
11
+ export class PgLedger {
12
+ schema;
13
+ app;
14
+ pool;
15
+ constructor(db, schema,
16
+ /** The app's name on every entry; a site's schema carries its name. */
17
+ app = schema) {
18
+ this.schema = schema;
19
+ this.app = app;
20
+ if (!SCHEMA.test(schema))
21
+ throw new Error(`invalid schema name: ${schema}`);
22
+ this.pool = db.pool;
23
+ }
24
+ async record(ws, e) {
25
+ await this.pool.query(`INSERT INTO ${this.schema}.ledger (workspace_id, kind, entity_type, entity_id, subject_type, subject_id, action, door, actor, outcome, code) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`, [ws, e.kind, e.entity.type, e.entity.id, e.subject?.type ?? null, e.subject?.id ?? null, e.action, e.door, e.actor, e.outcome, e.code]);
26
+ }
27
+ async list(ws, afterSeq, limit) {
28
+ const r = await this.pool.query(`SELECT seq, at, kind, entity_type, entity_id, subject_type, subject_id, action, door, actor, outcome, code FROM ${this.schema}.ledger WHERE workspace_id = $1 AND seq > $2 ORDER BY seq LIMIT $3`, [ws, afterSeq, limit]);
29
+ return r.rows.map((x) => ({
30
+ seq: Number(x.seq), at: x.at.toISOString(), app: this.app, kind: x.kind, entity: { type: x.entity_type, id: x.entity_id },
31
+ subject: x.subject_type && x.subject_id ? { type: x.subject_type, id: x.subject_id } : null,
32
+ action: x.action, door: x.door, actor: x.actor, outcome: x.outcome, code: x.code,
33
+ }));
34
+ }
35
+ }
36
+ /** A ledger in memory, for tests of what records into one. `now` is the clock its entries are stamped with. */
37
+ export class MemoryLedger {
38
+ app;
39
+ now;
40
+ byWorkspace = new Map();
41
+ seq = 0;
42
+ constructor(app, now = () => new Date()) {
43
+ this.app = app;
44
+ this.now = now;
45
+ }
46
+ async record(ws, e) {
47
+ const entries = this.byWorkspace.get(ws) ?? [];
48
+ entries.push({ ...e, entity: { ...e.entity }, subject: e.subject ? { ...e.subject } : null, seq: ++this.seq, at: this.now().toISOString(), app: this.app });
49
+ this.byWorkspace.set(ws, entries);
50
+ }
51
+ async list(ws, afterSeq = 0, limit = Number.MAX_SAFE_INTEGER) {
52
+ return (this.byWorkspace.get(ws) ?? []).filter((e) => e.seq > afterSeq).slice(0, limit);
53
+ }
54
+ }
55
+ //# sourceMappingURL=ledger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledger.js","sourceRoot":"","sources":["../../src/ledger/ledger.ts"],"names":[],"mappings":"AAeA,MAAM,MAAM,GAAG,oBAAoB,CAAC;AAEpC;;;;;;;;GAQG;AACH,MAAM,OAAO,QAAQ;IAIA;IAEA;IALF,IAAI,CAAuB;IAC5C,YACE,EAA8B,EACb,MAAc;IAC/B,uEAAuE;IACtD,MAAc,MAAM;QAFpB,WAAM,GAAN,MAAM,CAAQ;QAEd,QAAG,GAAH,GAAG,CAAiB;QAErC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,wBAAwB,MAAM,EAAE,CAAC,CAAC;QAC5E,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC;IACtB,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,EAAU,EAAE,CAAiB;QACxC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK,CACnB,eAAe,IAAI,CAAC,MAAM,0KAA0K,EACpM,CAAC,EAAE,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,CA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2
+ import type { CallerScope } from "../doors/caller.js";
3
+ import type { Ledger } from "./ledger.js";
4
+ import type { LedgerRules } from "./rules.js";
5
+ export type LedgeredDeps = {
6
+ ledger: Ledger;
7
+ callers: CallerScope;
8
+ /** Where a write and its entry are made one transaction: the site's `transactions(pool)`, whose pool its repositories use. */
9
+ transactions: Pick<Transactions, "run">;
10
+ };
11
+ /** For a site whose rows are not in Postgres (tests): each write runs as it comes, with nothing to make atomic. */
12
+ export declare const untransacted: Pick<Transactions, "run">;
13
+ /**
14
+ * A site's rules with the ledger kept. Pages, REST and the MCP tools (and,
15
+ * through the bridge, the WebMCP tools) all reach a site's rows through one
16
+ * repository, so recording there records every door, and no route or tool can
17
+ * leave it out. The wrapped object is the repository's own type: every caller
18
+ * keeps calling what it called.
19
+ *
20
+ * Each recorded method notes who called (the door and whoever is signed in,
21
+ * from the request's caller scope), then what came of it: `ok`, or `refused`
22
+ * with the rule's reason code. A read that found nothing is refused with
23
+ * NOT_FOUND. The verifier's own reads pass through unrecorded. A call with no
24
+ * caller in scope is a door that never said which one it is, and it throws.
25
+ *
26
+ * It is also where a write asks for a sign-in, for the same reason: asked
27
+ * here, every door asks the same, and a new door cannot forget to. A write by
28
+ * nobody is recorded as refused with the one sign-in code and never reaches
29
+ * the repository; the door it came through says so in its own way.
30
+ *
31
+ * A write (or a step of signing up or in) and its entry are one transaction:
32
+ * both are kept, or neither is, so the ledger never shows a change that did
33
+ * not happen, nor misses one that did. A refused write changed nothing: its
34
+ * transaction is rolled back and the refusal is kept on its own.
35
+ */
36
+ export declare function ledgered<R extends object>(repo: R, rules: LedgerRules<R>, d: LedgeredDeps): R;
@@ -0,0 +1,108 @@
1
+ import { DomainError } from "../domain-error.js";
2
+ import { SIGN_IN_REQUIRED_CODE, SignInRequired } from "../doors/sign-in.js";
3
+ import { refusalOf } from "../refusal.js";
4
+ import { NOT_FOUND_CODE } from "./entry.js";
5
+ /** For a site whose rows are not in Postgres (tests): each write runs as it comes, with nothing to make atomic. */
6
+ export const untransacted = { run: (fn) => fn() };
7
+ /**
8
+ * A site's rules with the ledger kept. Pages, REST and the MCP tools (and,
9
+ * through the bridge, the WebMCP tools) all reach a site's rows through one
10
+ * repository, so recording there records every door, and no route or tool can
11
+ * leave it out. The wrapped object is the repository's own type: every caller
12
+ * keeps calling what it called.
13
+ *
14
+ * Each recorded method notes who called (the door and whoever is signed in,
15
+ * from the request's caller scope), then what came of it: `ok`, or `refused`
16
+ * with the rule's reason code. A read that found nothing is refused with
17
+ * NOT_FOUND. The verifier's own reads pass through unrecorded. A call with no
18
+ * caller in scope is a door that never said which one it is, and it throws.
19
+ *
20
+ * It is also where a write asks for a sign-in, for the same reason: asked
21
+ * here, every door asks the same, and a new door cannot forget to. A write by
22
+ * nobody is recorded as refused with the one sign-in code and never reaches
23
+ * the repository; the door it came through says so in its own way.
24
+ *
25
+ * A write (or a step of signing up or in) and its entry are one transaction:
26
+ * both are kept, or neither is, so the ledger never shows a change that did
27
+ * not happen, nor misses one that did. A refused write changed nothing: its
28
+ * transaction is rolled back and the refusal is kept on its own.
29
+ */
30
+ export function ledgered(repo, rules, d) {
31
+ const ruleOf = (name) => (Object.hasOwn(rules, name) ? rules[name] : undefined);
32
+ return new Proxy(repo, {
33
+ get(target, name) {
34
+ const member = Reflect.get(target, name, target);
35
+ // What every object has (toString, constructor …) is no method of the site's rules.
36
+ if (typeof member !== "function" || typeof name === "symbol" || name in Object.prototype)
37
+ return member;
38
+ const method = member;
39
+ const rule = ruleOf(name);
40
+ if (!rule) {
41
+ return () => {
42
+ throw new Error(`${String(name)} has no ledger rule: say whether it is a read, a write or unrecorded`);
43
+ };
44
+ }
45
+ if (rule.kind === "skip")
46
+ return method.bind(target);
47
+ return (ws, ...args) => recorded(target, method, String(name), rule, d, ws, args);
48
+ },
49
+ });
50
+ }
51
+ async function recorded(target, method, name, rule, d, ws, args) {
52
+ const caller = d.callers.current();
53
+ if (!caller)
54
+ throw new Error(`${name} was called outside a door: no caller is in scope`);
55
+ if (caller.observer) {
56
+ if (rule.kind !== "read")
57
+ throw new DomainError("READ_ONLY", "the verifier only reads", 403);
58
+ return method.call(target, ws, ...args);
59
+ }
60
+ const subjectId = rule.subject?.id(args) ?? null;
61
+ const entry = (result, outcome, code, actor) => ({
62
+ kind: rule.kind,
63
+ entity: { type: rule.entity, id: rule.id(args, result) ?? null },
64
+ subject: rule.subject && subjectId ? { type: rule.subject.type, id: subjectId } : null,
65
+ action: rule.action,
66
+ door: caller.door,
67
+ actor,
68
+ outcome,
69
+ code,
70
+ });
71
+ const actor = await caller.actor();
72
+ if (rule.kind === "write" && actor === null) {
73
+ await d.ledger.record(ws, entry(undefined, "refused", SIGN_IN_REQUIRED_CODE, null));
74
+ throw new SignInRequired(caller.door);
75
+ }
76
+ if (rule.kind === "read")
77
+ return recordedRead(target, method, d, ws, args, entry, actor);
78
+ try {
79
+ return await d.transactions.run(async () => {
80
+ const result = await method.call(target, ws, ...args);
81
+ await d.ledger.record(ws, entry(result, "ok", null, actor));
82
+ return result;
83
+ });
84
+ }
85
+ catch (err) {
86
+ const refusal = refusalOf(err);
87
+ if (refusal)
88
+ await d.ledger.record(ws, entry(undefined, "refused", refusal.code, actor));
89
+ throw err;
90
+ }
91
+ }
92
+ /** A read changes nothing, so it needs no transaction: what it found, or why it found nothing, is kept after it. */
93
+ async function recordedRead(target, method, d, ws, args, entry, actor) {
94
+ let result;
95
+ try {
96
+ result = await method.call(target, ws, ...args);
97
+ }
98
+ catch (err) {
99
+ const refusal = refusalOf(err);
100
+ if (refusal)
101
+ await d.ledger.record(ws, entry(undefined, "refused", refusal.code, actor));
102
+ throw err;
103
+ }
104
+ const missing = result === null || result === undefined;
105
+ await d.ledger.record(ws, missing ? entry(undefined, "refused", NOT_FOUND_CODE, actor) : entry(result, "ok", null, actor));
106
+ return result;
107
+ }
108
+ //# sourceMappingURL=ledgered.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ledgered.js","sourceRoot":"","sources":["../../src/ledger/ledgered.ts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,OAAO;YAAE,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QACzF,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC;AAID,oHAAoH;AACpH,KAAK,UAAU,YAAY,CAAC,MAAc,EAAE,MAAiB,EAAE,CAAe,EAAE,EAAU,EAAE,IAAe,EAAE,KAAc,EAAE,KAAoB;IAC/I,IAAI,MAAe,CAAC;IACpB,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,IAAI,CAAC,CAAC;IAClD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;QAC/B,IAAI,OAAO;YAAE,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QACzF,MAAM,GAAG,CAAC;IACZ,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,CAAC;IACxD,MAAM,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,SAAS,EAAE,cAAc,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IAC3H,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { type LedgerKind } from "./entry.js";
2
+ /**
3
+ * How one method of a site's rules shows in the ledger. `id` names the entity
4
+ * from the call's arguments (after the workspace) and, when the call returned,
5
+ * its result: a create has no id until the row exists.
6
+ */
7
+ export type Recorded<A extends unknown[], T> = {
8
+ kind: LedgerKind;
9
+ /** The entity's type, in the site's own word: "booking", "order", "showtime". */
10
+ entity: string;
11
+ action: string;
12
+ id: (args: A, result: T | undefined) => string | null | undefined;
13
+ /** What the request is about besides its entity (see `about`): kept made or refused, since it comes from the request. */
14
+ subject?: Subject<A>;
15
+ };
16
+ /** The thing a request names besides the entity it writes: `about("order", ([req]) => req.orderNo)`. */
17
+ export type Subject<A extends unknown[]> = {
18
+ type: string;
19
+ id: (args: A) => string | null | undefined;
20
+ };
21
+ /** A method the ledger leaves out: a list, a search, a lookup of what is on offer. */
22
+ export type Unrecorded = {
23
+ kind: "skip";
24
+ };
25
+ export type LedgerRule<A extends unknown[], T> = Recorded<A, T> | Unrecorded;
26
+ /**
27
+ * One rule for EVERY method of a site's rules, by name. The mapped type is the
28
+ * point: a method added to a repository does not compile until someone says
29
+ * whether it is a read, a write or neither, so none is left out by forgetting.
30
+ */
31
+ export type LedgerRules<R> = {
32
+ [K in keyof R]: R[K] extends (ws: string, ...args: infer A) => Promise<infer T> ? LedgerRule<A, T> : never;
33
+ };
34
+ export declare const unrecorded: Unrecorded;
35
+ /** A read of one entity: a booking, an order, a showtime's seat map, a product. */
36
+ export declare function read<A extends unknown[], T>(entity: string, id: Recorded<A, T>["id"]): LedgerRule<A, T>;
37
+ /**
38
+ * A change to the site's rows, made or refused. Only someone signed in may make one. A create names what it is about
39
+ * (`about`), so that its refusal, which has no entity yet, still says which order, showtime or event it was for.
40
+ */
41
+ export declare function write<A extends unknown[], T>(entity: string, action: string, id: Recorded<A, T>["id"], subject?: Subject<A>): LedgerRule<A, T>;
42
+ /** The subject of a write: its type, and its id from the call's arguments. */
43
+ export declare function about<A extends unknown[]>(type: string, id: Subject<A>["id"]): Subject<A>;
44
+ /** A step of signing up or in. Recorded, and never counted as a change to what the site sells. */
45
+ export declare function authStep<A extends unknown[], T>(action: string, id: Recorded<A, T>["id"]): LedgerRule<A, T>;
@@ -0,0 +1,22 @@
1
+ import { READ_ACTION } from "./entry.js";
2
+ export const unrecorded = { kind: "skip" };
3
+ /** A read of one entity: a booking, an order, a showtime's seat map, a product. */
4
+ export function read(entity, id) {
5
+ return { kind: "read", entity, action: READ_ACTION, id };
6
+ }
7
+ /**
8
+ * A change to the site's rows, made or refused. Only someone signed in may make one. A create names what it is about
9
+ * (`about`), so that its refusal, which has no entity yet, still says which order, showtime or event it was for.
10
+ */
11
+ export function write(entity, action, id, subject) {
12
+ return { kind: "write", entity, action, id, ...(subject ? { subject } : {}) };
13
+ }
14
+ /** The subject of a write: its type, and its id from the call's arguments. */
15
+ export function about(type, id) {
16
+ return { type, id };
17
+ }
18
+ /** A step of signing up or in. Recorded, and never counted as a change to what the site sells. */
19
+ export function authStep(action, id) {
20
+ return { kind: "auth", entity: "account", action, id };
21
+ }
22
+ //# sourceMappingURL=rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rules.js","sourceRoot":"","sources":["../../src/ledger/rules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAmB,MAAM,YAAY,CAAC;AA+B1D,MAAM,CAAC,MAAM,UAAU,GAAe,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAEvD,mFAAmF;AACnF,MAAM,UAAU,IAAI,CAAyB,MAAc,EAAE,EAAwB;IACnF,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC;AAC3D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAyB,MAAc,EAAE,MAAc,EAAE,EAAwB,EAAE,OAAoB;IAC1H,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;AAChF,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,KAAK,CAAsB,IAAY,EAAE,EAAoB;IAC3E,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AACtB,CAAC;AAED,kGAAkG;AAClG,MAAM,UAAU,QAAQ,CAAyB,MAAc,EAAE,EAAwB;IACvF,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;AACzD,CAAC"}