@pgxsinkit/contracts 0.2.7 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/apply-strategy.d.ts +13 -1
- package/dist/apply-strategy.d.ts.map +1 -1
- package/dist/config.d.ts +256 -87
- package/dist/config.d.ts.map +1 -1
- package/dist/convergence-model.d.ts +2 -2
- package/dist/convergence-model.d.ts.map +1 -1
- package/dist/event-stream.d.ts +1 -1
- package/dist/fingerprint.d.ts +31 -11
- package/dist/fingerprint.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +480 -202
- package/dist/index.js.map +16 -14
- package/dist/predicate.d.ts +162 -0
- package/dist/predicate.d.ts.map +1 -0
- package/dist/projection.d.ts +2 -2
- package/dist/projection.d.ts.map +1 -1
- package/dist/registry-diff.d.ts.map +1 -1
- package/dist/registry-invariant.d.ts +26 -14
- package/dist/registry-invariant.d.ts.map +1 -1
- package/dist/registry.d.ts +74 -48
- package/dist/registry.d.ts.map +1 -1
- package/dist/sql-role-guard.d.ts +1 -1
- package/dist/stream-envelope.d.ts +84 -0
- package/dist/stream-envelope.d.ts.map +1 -0
- package/dist/supabase-rls.d.ts +32 -36
- package/dist/supabase-rls.d.ts.map +1 -1
- package/package.json +3 -2
- package/skills/registry-authoring/SKILL.md +185 -185
- package/skills/registry-authoring/references/storage-engine-and-build.md +29 -0
- package/src/apply-strategy.ts +23 -4
- package/src/config.ts +340 -116
- package/src/convergence-model.ts +2 -2
- package/src/event-stream.ts +1 -1
- package/src/fingerprint.ts +76 -14
- package/src/index.ts +2 -0
- package/src/predicate.ts +274 -0
- package/src/projection.ts +6 -2
- package/src/registry-diff.ts +15 -4
- package/src/registry-invariant.ts +34 -19
- package/src/registry.ts +324 -78
- package/src/sql-role-guard.ts +1 -1
- package/src/stream-envelope.ts +91 -0
- package/src/supabase-rls.ts +76 -74
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { SQL } from "drizzle-orm";
|
|
2
2
|
import { getTableConfig, PgDialect } from "drizzle-orm/pg-core";
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { DENY_ALL_PREDICATE, type JwtClaims } from "./config";
|
|
5
|
+
import { isOrPredicate, type Predicate } from "./predicate";
|
|
5
6
|
import { getSyncRegistryRowClasses, type SyncTableEntry, type SyncTableRegistry } from "./registry";
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -12,7 +13,7 @@ import { getSyncRegistryRowClasses, type SyncTableEntry, type SyncTableRegistry
|
|
|
12
13
|
* It exists because a privacy/visibility invariant spanning many entries drifts in two structural ways that
|
|
13
14
|
* per-entry review cannot catch:
|
|
14
15
|
*
|
|
15
|
-
* 1. **Read/write asymmetry** — the
|
|
16
|
+
* 1. **Read/write asymmetry** — the shape's row filter and the Postgres RLS policies are two engines, so one
|
|
16
17
|
* can be tightened (or loosened) without the other. The assertion audits BOTH surfaces of every bound
|
|
17
18
|
* entry in one predicate, so "readable but unwritable" (or the reverse) is a test failure, not a bug report.
|
|
18
19
|
* 2. **Under-enumeration** — an invariant expressed over a hand-maintained list of tables silently stops
|
|
@@ -20,11 +21,23 @@ import { getSyncRegistryRowClasses, type SyncTableEntry, type SyncTableRegistry
|
|
|
20
21
|
* inverts that: with a declared `rowClasses` vocabulary, a new entry cannot even be registered without
|
|
21
22
|
* being classified, so it joins its class's invariants by construction.
|
|
22
23
|
*
|
|
23
|
-
* It audits
|
|
24
|
-
* is a closure — only its presence and `RowFilterSpec.revision` are hashed). The trade: it sees exactly
|
|
25
|
-
* claims fixtures you give it, and nothing else.
|
|
24
|
+
* It audits RESOLVED output, so it sees things the fingerprint structurally cannot (the `customPredicate`
|
|
25
|
+
* BODY is a closure — only its presence and `RowFilterSpec.revision` are hashed). The trade: it sees exactly
|
|
26
|
+
* the claims fixtures you give it, and nothing else.
|
|
26
27
|
*/
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Whether a predicate admits no row at all — the read half of "this persona sees nothing".
|
|
31
|
+
*
|
|
32
|
+
* Recognises the {@link DENY_ALL_PREDICATE} singleton by reference AND any structurally empty `or`,
|
|
33
|
+
* because both are always-FALSE on the wire and an invariant is asking about the outcome, not about
|
|
34
|
+
* which spelling produced it.
|
|
35
|
+
*/
|
|
36
|
+
export function deniesAllRows(predicate: Predicate | null): boolean {
|
|
37
|
+
if (predicate === DENY_ALL_PREDICATE) return true;
|
|
38
|
+
return predicate != null && isOrPredicate(predicate) && predicate.or.length === 0;
|
|
39
|
+
}
|
|
40
|
+
|
|
28
41
|
/** One rendered write policy on a bound entry's Postgres table, as the invariant predicate sees it. */
|
|
29
42
|
export interface RenderedPolicy {
|
|
30
43
|
/** The policy name as declared (e.g. `widgets_select_owner_or_admin`). */
|
|
@@ -48,16 +61,19 @@ export interface RegistryInvariantCell {
|
|
|
48
61
|
fixtureName: string;
|
|
49
62
|
claims: JwtClaims;
|
|
50
63
|
/**
|
|
51
|
-
* The read
|
|
52
|
-
*
|
|
64
|
+
* The read predicate this entry resolves to FOR THESE CLAIMS — the real read pipeline's output, exactly
|
|
65
|
+
* what the control plane compiles into the shape at subscribe.
|
|
53
66
|
*
|
|
54
67
|
* `null` means **unfiltered**, and covers both ways that arises: the entry declares no shape/`rowFilter`
|
|
55
|
-
* at all, or its `
|
|
56
|
-
* row is visible" answer — e.g. an admin persona). Both are the same statement about what the
|
|
57
|
-
* receives, which is what an invariant reasons about; distinguish them via
|
|
58
|
-
* predicate genuinely needs to.
|
|
68
|
+
* at all, or its `customPredicate` returned `null` for these claims (the documented "bypass filtering,
|
|
69
|
+
* every row is visible" answer — e.g. an admin persona). Both are the same statement about what the
|
|
70
|
+
* client receives, which is what an invariant reasons about; distinguish them via
|
|
71
|
+
* `entry.shape?.rowFilter` if a predicate genuinely needs to.
|
|
72
|
+
*
|
|
73
|
+
* An AST, not SQL text: an invariant matches on structure ({@link deniesAllRows}, the `isXPredicate`
|
|
74
|
+
* guards) rather than on a rendered string that could drift on formatting alone.
|
|
59
75
|
*/
|
|
60
|
-
|
|
76
|
+
readPredicate: Predicate | null;
|
|
61
77
|
/** Every RLS policy attached to the entry's Postgres table, rendered to inline SQL text. */
|
|
62
78
|
renderedPolicies: RenderedPolicy[];
|
|
63
79
|
}
|
|
@@ -111,10 +127,9 @@ function renderPolicies(entry: SyncTableEntry): RenderedPolicy[] {
|
|
|
111
127
|
}));
|
|
112
128
|
}
|
|
113
129
|
|
|
114
|
-
/** The read
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
return rowFilter == null ? null : buildRowFilterShape(rowFilter, claims);
|
|
130
|
+
/** The read predicate for these claims, through the real pipeline the control plane uses. */
|
|
131
|
+
function resolvePredicate(entry: SyncTableEntry, claims: JwtClaims): Predicate | null {
|
|
132
|
+
return entry.shape?.rowFilter?.customPredicate?.(claims) ?? null;
|
|
118
133
|
}
|
|
119
134
|
|
|
120
135
|
function boundEntries(
|
|
@@ -142,8 +157,8 @@ function boundEntries(
|
|
|
142
157
|
* name: "private rows are never visible to an anonymous caller",
|
|
143
158
|
* appliesTo: ["private"],
|
|
144
159
|
* claimsFixtures: { anonymous: {}, member: { sub: "u-1" } },
|
|
145
|
-
* holds: ({ fixtureName,
|
|
146
|
-
* fixtureName !== "anonymous" ||
|
|
160
|
+
* holds: ({ fixtureName, readPredicate }) =>
|
|
161
|
+
* fixtureName !== "anonymous" || deniesAllRows(readPredicate) || "anonymous read is not denied",
|
|
147
162
|
* });
|
|
148
163
|
* ```
|
|
149
164
|
*
|
|
@@ -199,7 +214,7 @@ export function assertRegistryInvariant(registry: SyncTableRegistry, spec: Regis
|
|
|
199
214
|
rowClass: entry.rowClass,
|
|
200
215
|
fixtureName,
|
|
201
216
|
claims,
|
|
202
|
-
|
|
217
|
+
readPredicate: resolvePredicate(entry, claims),
|
|
203
218
|
renderedPolicies,
|
|
204
219
|
});
|
|
205
220
|
if (verdict === true) {
|