@lunora/server 1.0.0-alpha.12 → 1.0.0-alpha.121
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.md +219 -0
- package/README.md +46 -0
- package/dist/data-model.d.mts +273 -164
- package/dist/data-model.d.ts +273 -164
- package/dist/data-model.mjs +0 -1
- package/dist/drizzle.mjs +1 -1
- package/dist/index.d.mts +3090 -1593
- package/dist/index.d.ts +3090 -1593
- package/dist/index.mjs +1 -29
- package/dist/otel.d.mts +554 -0
- package/dist/otel.d.ts +554 -0
- package/dist/otel.mjs +1 -0
- package/dist/packem_shared/ACTION_CACHE_DEFAULT_TTL_MS-BUng3KOy.mjs +1 -0
- package/dist/packem_shared/DEFAULT_LIMIT-vNtdsa5y.mjs +1 -0
- package/dist/packem_shared/DOCUMENT_HISTORY_REDACTED_FIELDS-g-fdcytr.mjs +1 -0
- package/dist/packem_shared/LunoraEnvError-A5I-PzMh.mjs +3 -0
- package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-DheiApL3.mjs +1 -0
- package/dist/packem_shared/allowAll-DkRAgItV.mjs +1 -0
- package/dist/packem_shared/apply-output-C5wZ5EAL.mjs +1 -0
- package/dist/packem_shared/asBucketStorage-DoGYOjq3.mjs +1 -0
- package/dist/packem_shared/assertShapesDeclareReadPolicies-DHLO-AVP.mjs +1 -0
- package/dist/packem_shared/beginDeferredDeletes-DhwFBbvs.mjs +1 -0
- package/dist/packem_shared/beginDeferredSchedules-gJlbW6h9.mjs +1 -0
- package/dist/packem_shared/bindOrm-D_hFUjmr.mjs +1 -0
- package/dist/packem_shared/buildMaskRegistry-DpWMtalG.mjs +1 -0
- package/dist/packem_shared/compose-middleware-MCpZZKh7.mjs +1 -0
- package/dist/packem_shared/composePluginMiddleware-D4tocdAs.mjs +1 -0
- package/dist/packem_shared/context-identity-B2XYGA0B.mjs +1 -0
- package/dist/packem_shared/createPolicyDsl-BZa6SqLJ.mjs +1 -0
- package/dist/packem_shared/createSecrets-D6rLB42U.mjs +1 -0
- package/dist/packem_shared/defineAggregateIndex-COm6F0QJ.mjs +1 -0
- package/dist/packem_shared/defineIdentity-DwkNKwYa.mjs +1 -0
- package/dist/packem_shared/defineMigration-CXOS0Bvq.mjs +1 -0
- package/dist/packem_shared/defineMutator-DKy8UtbB.mjs +1 -0
- package/dist/packem_shared/defineShape-DtBuu6Yh.mjs +1 -0
- package/dist/packem_shared/defineStorageRule-Dv4nJE0H.mjs +1 -0
- package/dist/packem_shared/fnv1a-D3ueNTWB.mjs +1 -0
- package/dist/packem_shared/functions-B2-H4PQT.mjs +1 -0
- package/dist/packem_shared/httpAction-DfKw8ZZt.mjs +5 -0
- package/dist/packem_shared/initLunora-BzC2zEfH.mjs +1 -0
- package/dist/packem_shared/mask-rh0M0XlH.mjs +1 -0
- package/dist/packem_shared/middleware-DciZ1Q3A.mjs +1 -0
- package/dist/packem_shared/onConnect-BLRoOpv2.mjs +1 -0
- package/dist/packem_shared/onQueryChange-CatdYnH5.mjs +1 -0
- package/dist/packem_shared/plugin-DY0N7ZWx.mjs +1 -0
- package/dist/packem_shared/policy-tag-V0lqFzgS.mjs +1 -0
- package/dist/packem_shared/protectPublic-C-STrTgA.mjs +1 -0
- package/dist/packem_shared/rls-DCBTL6B2.mjs +1 -0
- package/dist/packem_shared/run-middleware-laenw2jQ.mjs +1 -0
- package/dist/packem_shared/serveStorageObject-BzZEP-B5.mjs +1 -0
- package/dist/packem_shared/stable-key-B_BlboiY.mjs +1 -0
- package/dist/packem_shared/storageRules-DvM18uSL.mjs +1 -0
- package/dist/packem_shared/types.d-B7FLdYUD.d.mts +148 -0
- package/dist/packem_shared/types.d-BCtjbIzl.d.ts +148 -0
- package/dist/packem_shared/wire-codec-CH4vwdFT.mjs +1 -0
- package/dist/rls/testing.d.mts +42 -34
- package/dist/rls/testing.d.ts +42 -34
- package/dist/rls/testing.mjs +1 -49
- package/dist/types.d.mts +1883 -513
- package/dist/types.d.ts +1883 -513
- package/dist/types.mjs +1 -31
- package/package.json +18 -4
- package/dist/packem_shared/LunoraEnvError-DjFkpkSP.mjs +0 -187
- package/dist/packem_shared/LunoraError-DN7Zhhvu.mjs +0 -54
- package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-D8viLY1S.mjs +0 -114
- package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +0 -11
- package/dist/packem_shared/bindOrm-Ce57S3N9.mjs +0 -128
- package/dist/packem_shared/buildRlsReadRegistry-Do1CSBTr.mjs +0 -107
- package/dist/packem_shared/composePluginMiddleware-Ck5_TUO8.mjs +0 -100
- package/dist/packem_shared/createPolicyDsl-De67zPDS.mjs +0 -29
- package/dist/packem_shared/createSecrets-TsIP9lOa.mjs +0 -55
- package/dist/packem_shared/defineAggregateIndex-ZdyU78gh.mjs +0 -291
- package/dist/packem_shared/defineIdentity-B_9YD46A.mjs +0 -34
- package/dist/packem_shared/defineMigration-CAJLr6fx.mjs +0 -8
- package/dist/packem_shared/defineMutator-EIXAWhs9.mjs +0 -11
- package/dist/packem_shared/defineShape-CJ27Wx7o.mjs +0 -17
- package/dist/packem_shared/defineStorageRule-qu0mpilX.mjs +0 -20
- package/dist/packem_shared/functions-Di9FUNkf.mjs +0 -5
- package/dist/packem_shared/httpAction-FLwfsePg.mjs +0 -340
- package/dist/packem_shared/initLunora-lxwHTEV3.mjs +0 -100
- package/dist/packem_shared/mask-CWfMjb5Y.mjs +0 -256
- package/dist/packem_shared/onConnect-CIPXKPyw.mjs +0 -13
- package/dist/packem_shared/policy-tag-DvpVH2tv.mjs +0 -13
- package/dist/packem_shared/protectPublic-BjFkQ_Or.mjs +0 -15
- package/dist/packem_shared/rls-DhNgKFeW.mjs +0 -572
- package/dist/packem_shared/run-middleware-CYQOuoV6.mjs +0 -18
- package/dist/packem_shared/storageRules-Cje6Woea.mjs +0 -88
- package/dist/packem_shared/types.d-BB3pjV0m.d.mts +0 -141
- package/dist/packem_shared/types.d-Cxl6ndhm.d.ts +0 -141
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { LunoraError } from './LunoraError-DN7Zhhvu.mjs';
|
|
2
|
-
|
|
3
|
-
const permissionName = (permission) => typeof permission === "string" ? permission : permission.name;
|
|
4
|
-
const indexRolePermissions = (roles = []) => {
|
|
5
|
-
const byRole = /* @__PURE__ */ new Map();
|
|
6
|
-
for (const role of roles) {
|
|
7
|
-
const granted = /* @__PURE__ */ new Set();
|
|
8
|
-
for (const permission of role.permissions ?? []) {
|
|
9
|
-
granted.add(permissionName(permission));
|
|
10
|
-
}
|
|
11
|
-
byRole.set(role.name, granted);
|
|
12
|
-
}
|
|
13
|
-
return byRole;
|
|
14
|
-
};
|
|
15
|
-
const prefixMatches = (prefix, key) => prefix === void 0 || key.startsWith(prefix);
|
|
16
|
-
const resolveSignedUrlOperation = (args) => {
|
|
17
|
-
const options = args[1];
|
|
18
|
-
const method = typeof options === "object" && options !== null ? options.method : void 0;
|
|
19
|
-
return typeof method === "string" && method.toUpperCase() === "PUT" ? "write" : "read";
|
|
20
|
-
};
|
|
21
|
-
const GUARDED_METHODS = [
|
|
22
|
-
["delete", "delete"],
|
|
23
|
-
["download", "read"],
|
|
24
|
-
["generateUploadUrl", "write"],
|
|
25
|
-
["getMetadata", "read"],
|
|
26
|
-
["getSignedUrl", resolveSignedUrlOperation],
|
|
27
|
-
["getUrl", "read"],
|
|
28
|
-
["store", "write"]
|
|
29
|
-
];
|
|
30
|
-
const storageRules = (rules, options = {}) => {
|
|
31
|
-
const rolePermissions = indexRolePermissions(options.roles);
|
|
32
|
-
return async ({ ctx, next }) => {
|
|
33
|
-
const auth = ctx.auth ?? {};
|
|
34
|
-
const identity = await auth.getIdentity?.() ?? null;
|
|
35
|
-
const roles = auth.roles ?? [];
|
|
36
|
-
const granted = /* @__PURE__ */ new Set();
|
|
37
|
-
for (const roleName of roles) {
|
|
38
|
-
for (const name of rolePermissions.get(roleName) ?? []) {
|
|
39
|
-
granted.add(name);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
const authContext = {
|
|
43
|
-
can: (permission) => granted.has(permissionName(permission)),
|
|
44
|
-
identity,
|
|
45
|
-
roles,
|
|
46
|
-
// eslint-disable-next-line unicorn/no-null -- StorageRuleContext.auth.userId is a public `null | string` type
|
|
47
|
-
userId: auth.userId ?? null
|
|
48
|
-
};
|
|
49
|
-
const assertAllowed = (op, key, bucketName) => {
|
|
50
|
-
const applicable = rules.filter((rule) => rule.on === op && rule.bucket === bucketName);
|
|
51
|
-
if (applicable.length === 0) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const context = { auth: authContext, ctx, key };
|
|
55
|
-
const allowed = applicable.some((rule) => prefixMatches(rule.prefix, key) && rule.when(context) === true);
|
|
56
|
-
if (!allowed) {
|
|
57
|
-
throw new LunoraError("FORBIDDEN", `storage ${op} on "${key}" in bucket "${bucketName}" denied by access rule`);
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
const wrapStorage = (storage2) => {
|
|
61
|
-
const bucketName = storage2.bucketName ?? "default";
|
|
62
|
-
const wrapped = { bucketName };
|
|
63
|
-
for (const [method, op] of GUARDED_METHODS) {
|
|
64
|
-
const original = storage2[method];
|
|
65
|
-
if (typeof original === "function") {
|
|
66
|
-
wrapped[method] = (...args) => {
|
|
67
|
-
const key = typeof args[0] === "string" ? args[0] : "";
|
|
68
|
-
const operation = typeof op === "function" ? op(args) : op;
|
|
69
|
-
assertAllowed(operation, key, bucketName);
|
|
70
|
-
return original(...args);
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const { bucket } = storage2;
|
|
75
|
-
if (typeof bucket === "function") {
|
|
76
|
-
wrapped.bucket = (name) => wrapStorage(bucket(name));
|
|
77
|
-
}
|
|
78
|
-
return wrapped;
|
|
79
|
-
};
|
|
80
|
-
const storage = ctx.storage;
|
|
81
|
-
if (storage === void 0) {
|
|
82
|
-
return next();
|
|
83
|
-
}
|
|
84
|
-
return next({ ctx: { storage: wrapStorage(storage) } });
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export { storageRules };
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { WhereOf } from "../data-model.mjs";
|
|
2
|
-
/** Structural mirror of `@lunora/do`'s `WhereInput`. */
|
|
3
|
-
interface WhereInput {
|
|
4
|
-
[field: string]: unknown;
|
|
5
|
-
AND?: WhereInput[];
|
|
6
|
-
NOT?: WhereInput;
|
|
7
|
-
OR?: WhereInput[];
|
|
8
|
-
}
|
|
9
|
-
/** Operations a policy can gate. `read` covers `get`/`findMany`/`query`/`count`. */
|
|
10
|
-
type PolicyOperation = "delete" | "insert" | "read" | "update";
|
|
11
|
-
/**
|
|
12
|
-
* A policy's `when` decision:
|
|
13
|
-
*
|
|
14
|
-
* - `WhereInput`: a row-shape predicate. On reads it is AND-merged into every
|
|
15
|
-
* query against the table — the row is invisible unless it matches. On writes
|
|
16
|
-
* it is evaluated against the candidate document (`insert`) or the pre-write
|
|
17
|
-
* row (`update`/`delete`); a mismatch denies the write with
|
|
18
|
-
* `LunoraError("FORBIDDEN")`. Same operator set as the SQL compiler
|
|
19
|
-
* (`eq`/`ne`/`in`/`notIn`/`lt`/`lte`/`gt`/`gte`/`isNull`/`contains` +
|
|
20
|
-
* `AND`/`OR`/`NOT`).
|
|
21
|
-
* - `true`: unrestricted. On reads no predicate is merged; on writes the row is
|
|
22
|
-
* allowed.
|
|
23
|
-
* - `false`: deny. On reads the table is forced to match zero rows (a sentinel
|
|
24
|
-
* predicate); on writes the operation throws `LunoraError("FORBIDDEN")`.
|
|
25
|
-
*
|
|
26
|
-
* Returning `undefined` opts this specific policy out (rare; useful when
|
|
27
|
-
* branching on `ctx.auth.roles`).
|
|
28
|
-
*/
|
|
29
|
-
type PolicyDecision = WhereInput | boolean | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Relation-aware twin of {@link PolicyDecision}, parameterized over the
|
|
32
|
-
* generated `DataModel` (`DM`) + `Relations` (`REL`) maps and a table `T`. A
|
|
33
|
-
* read policy may now return a Prisma-style relation predicate (the
|
|
34
|
-
* `@lunora/do` pre-resolver resolves it via a semijoin), so the typed
|
|
35
|
-
* authoring surface accepts `WhereOf<DM, REL, T>` — column predicates **and**
|
|
36
|
-
* `is`/`isNot`/`some`/`none`/`every` over `T`'s declared relations — in
|
|
37
|
-
* addition to the `boolean`/`undefined` decisions. Used by the project-bound
|
|
38
|
-
* `definePolicy` from `createPolicyDsl`.
|
|
39
|
-
*/
|
|
40
|
-
type PolicyDecisionOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> = WhereOf<DM, REL, T> | boolean | undefined;
|
|
41
|
-
/**
|
|
42
|
-
* Relation-aware input for a project-bound `definePolicy` (see
|
|
43
|
-
* `createPolicyDsl`). `table` is constrained to a real table name and
|
|
44
|
-
* `when`'s return type is the table-specific {@link PolicyDecisionOf} — so an
|
|
45
|
-
* unknown table, a stray column, or a relation predicate naming a relation the
|
|
46
|
-
* table does not declare is a compile error rather than a silent runtime deny.
|
|
47
|
-
*/
|
|
48
|
-
interface TypedDefinePolicyInput<DM, REL extends Record<keyof DM, object>, T extends keyof DM, Context = unknown, Identity = Record<string, unknown>> {
|
|
49
|
-
on: PolicyOperation;
|
|
50
|
-
table: T;
|
|
51
|
-
when: (context: PolicyContext<Context, Identity>) => PolicyDecisionOf<DM, REL, T>;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Context handed to a policy. `auth.roles` is the per-request role list,
|
|
55
|
-
* sourced from the identity resolver (better-auth claims today). `auth.can`
|
|
56
|
-
* answers whether any of those roles grants a permission (see
|
|
57
|
-
* {@link Permission} / {@link RlsOptions}). `row` is present only on write
|
|
58
|
-
* policies (`insert`/`update`/`delete`) — for `update` and `delete` it is the
|
|
59
|
-
* pre-write row; for `insert` it is the candidate document. `ctx` is the full
|
|
60
|
-
* procedure context the middleware closed over.
|
|
61
|
-
*/
|
|
62
|
-
interface PolicyContext<Context = unknown, Identity = Record<string, unknown>> {
|
|
63
|
-
readonly auth: {
|
|
64
|
-
/**
|
|
65
|
-
* `true` when any of the request's `roles` grants `permission` (passed
|
|
66
|
-
* by {@link Permission} object or its `name`). Always `false` when no
|
|
67
|
-
* roles were handed to the middleware (`rls(policies, { roles })`), or
|
|
68
|
-
* when none of the request's roles lists the permission.
|
|
69
|
-
*/
|
|
70
|
-
readonly can: (permission: Permission | string) => boolean;
|
|
71
|
-
/**
|
|
72
|
-
* The resolved identity, typed to the `Identity` type parameter bound on
|
|
73
|
-
* `createPolicyDsl` — e.g. the app's `defineIdentity(...)` claim type via
|
|
74
|
-
* the `InferIdentity` helper; otherwise the untyped claim bag.
|
|
75
|
-
* `undefined`/`null` when the request is anonymous.
|
|
76
|
-
*/
|
|
77
|
-
readonly identity?: Identity | null;
|
|
78
|
-
readonly roles: ReadonlyArray<string>;
|
|
79
|
-
readonly userId: null | string;
|
|
80
|
-
};
|
|
81
|
-
readonly ctx: Context;
|
|
82
|
-
readonly row?: Record<string, unknown>;
|
|
83
|
-
}
|
|
84
|
-
/** A registered policy as stored in the policy table. */
|
|
85
|
-
interface Policy<Context = unknown> {
|
|
86
|
-
readonly on: PolicyOperation;
|
|
87
|
-
readonly table: string;
|
|
88
|
-
readonly when: (context: PolicyContext<Context>) => PolicyDecision;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Input accepted by `definePolicy`. The branded result is the same
|
|
92
|
-
* shape; we keep the input/output split so callers can read JSDoc on the
|
|
93
|
-
* constructor without the type re-exposing `Policy` internals.
|
|
94
|
-
*/
|
|
95
|
-
interface DefinePolicyInput<Context = unknown> {
|
|
96
|
-
on: PolicyOperation;
|
|
97
|
-
/** Logical table name the policy applies to. */
|
|
98
|
-
table: string;
|
|
99
|
-
/**
|
|
100
|
-
* Decision function. Returning a `WhereInput` (read only) AND-merges the
|
|
101
|
-
* predicate; `true` allows; `false` denies; `undefined` skips this policy.
|
|
102
|
-
*
|
|
103
|
-
* NOTE: `count()` is **unsupported** on a policy-restricted table — the
|
|
104
|
-
* reader throws `LunoraError("COUNT_RLS_UNSUPPORTED")` (422). This mirrors
|
|
105
|
-
* kitcn's documented behavior.
|
|
106
|
-
*/
|
|
107
|
-
when: (context: PolicyContext<Context>) => PolicyDecision;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* A named, abstract capability a policy can check with `ctx.auth.can(...)`,
|
|
111
|
-
* instead of branching on raw role strings. Declare one with
|
|
112
|
-
* `definePermission`; grant it to a role via {@link Role.permissions};
|
|
113
|
-
* register the roles with the middleware via {@link RlsOptions.roles}.
|
|
114
|
-
*/
|
|
115
|
-
interface Permission {
|
|
116
|
-
readonly description?: string;
|
|
117
|
-
readonly name: string;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* A role declaration. Roles are string labels attached to the request's
|
|
121
|
-
* identity (via better-auth claims today). `permissions` lists the
|
|
122
|
-
* capabilities the role grants — at request time the middleware unions the
|
|
123
|
-
* permissions of every role in `ctx.auth.roles` so a policy can ask
|
|
124
|
-
* `ctx.auth.can(permission)` rather than enumerate roles.
|
|
125
|
-
*/
|
|
126
|
-
interface Role {
|
|
127
|
-
readonly description?: string;
|
|
128
|
-
readonly name: string;
|
|
129
|
-
/** Permissions this role grants — by {@link Permission} object or bare name. */
|
|
130
|
-
readonly permissions?: ReadonlyArray<Permission | string>;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Options for the `rls(policies, options)` middleware. `roles` registers the
|
|
134
|
-
* role→permission grants that back `ctx.auth.can(...)`; a role not listed here
|
|
135
|
-
* grants no permissions (so `can` is conservative — it fails closed for
|
|
136
|
-
* unknown roles).
|
|
137
|
-
*/
|
|
138
|
-
interface RlsOptions {
|
|
139
|
-
readonly roles?: ReadonlyArray<Role>;
|
|
140
|
-
}
|
|
141
|
-
export { DefinePolicyInput as D, PolicyOperation as P, Role as R, TypedDefinePolicyInput as T, WhereInput as W, Policy as a, Permission as b, RlsOptions as c, PolicyContext as d, PolicyDecision as e, PolicyDecisionOf as f };
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { WhereOf } from "../data-model.js";
|
|
2
|
-
/** Structural mirror of `@lunora/do`'s `WhereInput`. */
|
|
3
|
-
interface WhereInput {
|
|
4
|
-
[field: string]: unknown;
|
|
5
|
-
AND?: WhereInput[];
|
|
6
|
-
NOT?: WhereInput;
|
|
7
|
-
OR?: WhereInput[];
|
|
8
|
-
}
|
|
9
|
-
/** Operations a policy can gate. `read` covers `get`/`findMany`/`query`/`count`. */
|
|
10
|
-
type PolicyOperation = "delete" | "insert" | "read" | "update";
|
|
11
|
-
/**
|
|
12
|
-
* A policy's `when` decision:
|
|
13
|
-
*
|
|
14
|
-
* - `WhereInput`: a row-shape predicate. On reads it is AND-merged into every
|
|
15
|
-
* query against the table — the row is invisible unless it matches. On writes
|
|
16
|
-
* it is evaluated against the candidate document (`insert`) or the pre-write
|
|
17
|
-
* row (`update`/`delete`); a mismatch denies the write with
|
|
18
|
-
* `LunoraError("FORBIDDEN")`. Same operator set as the SQL compiler
|
|
19
|
-
* (`eq`/`ne`/`in`/`notIn`/`lt`/`lte`/`gt`/`gte`/`isNull`/`contains` +
|
|
20
|
-
* `AND`/`OR`/`NOT`).
|
|
21
|
-
* - `true`: unrestricted. On reads no predicate is merged; on writes the row is
|
|
22
|
-
* allowed.
|
|
23
|
-
* - `false`: deny. On reads the table is forced to match zero rows (a sentinel
|
|
24
|
-
* predicate); on writes the operation throws `LunoraError("FORBIDDEN")`.
|
|
25
|
-
*
|
|
26
|
-
* Returning `undefined` opts this specific policy out (rare; useful when
|
|
27
|
-
* branching on `ctx.auth.roles`).
|
|
28
|
-
*/
|
|
29
|
-
type PolicyDecision = WhereInput | boolean | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Relation-aware twin of {@link PolicyDecision}, parameterized over the
|
|
32
|
-
* generated `DataModel` (`DM`) + `Relations` (`REL`) maps and a table `T`. A
|
|
33
|
-
* read policy may now return a Prisma-style relation predicate (the
|
|
34
|
-
* `@lunora/do` pre-resolver resolves it via a semijoin), so the typed
|
|
35
|
-
* authoring surface accepts `WhereOf<DM, REL, T>` — column predicates **and**
|
|
36
|
-
* `is`/`isNot`/`some`/`none`/`every` over `T`'s declared relations — in
|
|
37
|
-
* addition to the `boolean`/`undefined` decisions. Used by the project-bound
|
|
38
|
-
* `definePolicy` from `createPolicyDsl`.
|
|
39
|
-
*/
|
|
40
|
-
type PolicyDecisionOf<DM, REL extends Record<keyof DM, object>, T extends keyof DM> = WhereOf<DM, REL, T> | boolean | undefined;
|
|
41
|
-
/**
|
|
42
|
-
* Relation-aware input for a project-bound `definePolicy` (see
|
|
43
|
-
* `createPolicyDsl`). `table` is constrained to a real table name and
|
|
44
|
-
* `when`'s return type is the table-specific {@link PolicyDecisionOf} — so an
|
|
45
|
-
* unknown table, a stray column, or a relation predicate naming a relation the
|
|
46
|
-
* table does not declare is a compile error rather than a silent runtime deny.
|
|
47
|
-
*/
|
|
48
|
-
interface TypedDefinePolicyInput<DM, REL extends Record<keyof DM, object>, T extends keyof DM, Context = unknown, Identity = Record<string, unknown>> {
|
|
49
|
-
on: PolicyOperation;
|
|
50
|
-
table: T;
|
|
51
|
-
when: (context: PolicyContext<Context, Identity>) => PolicyDecisionOf<DM, REL, T>;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Context handed to a policy. `auth.roles` is the per-request role list,
|
|
55
|
-
* sourced from the identity resolver (better-auth claims today). `auth.can`
|
|
56
|
-
* answers whether any of those roles grants a permission (see
|
|
57
|
-
* {@link Permission} / {@link RlsOptions}). `row` is present only on write
|
|
58
|
-
* policies (`insert`/`update`/`delete`) — for `update` and `delete` it is the
|
|
59
|
-
* pre-write row; for `insert` it is the candidate document. `ctx` is the full
|
|
60
|
-
* procedure context the middleware closed over.
|
|
61
|
-
*/
|
|
62
|
-
interface PolicyContext<Context = unknown, Identity = Record<string, unknown>> {
|
|
63
|
-
readonly auth: {
|
|
64
|
-
/**
|
|
65
|
-
* `true` when any of the request's `roles` grants `permission` (passed
|
|
66
|
-
* by {@link Permission} object or its `name`). Always `false` when no
|
|
67
|
-
* roles were handed to the middleware (`rls(policies, { roles })`), or
|
|
68
|
-
* when none of the request's roles lists the permission.
|
|
69
|
-
*/
|
|
70
|
-
readonly can: (permission: Permission | string) => boolean;
|
|
71
|
-
/**
|
|
72
|
-
* The resolved identity, typed to the `Identity` type parameter bound on
|
|
73
|
-
* `createPolicyDsl` — e.g. the app's `defineIdentity(...)` claim type via
|
|
74
|
-
* the `InferIdentity` helper; otherwise the untyped claim bag.
|
|
75
|
-
* `undefined`/`null` when the request is anonymous.
|
|
76
|
-
*/
|
|
77
|
-
readonly identity?: Identity | null;
|
|
78
|
-
readonly roles: ReadonlyArray<string>;
|
|
79
|
-
readonly userId: null | string;
|
|
80
|
-
};
|
|
81
|
-
readonly ctx: Context;
|
|
82
|
-
readonly row?: Record<string, unknown>;
|
|
83
|
-
}
|
|
84
|
-
/** A registered policy as stored in the policy table. */
|
|
85
|
-
interface Policy<Context = unknown> {
|
|
86
|
-
readonly on: PolicyOperation;
|
|
87
|
-
readonly table: string;
|
|
88
|
-
readonly when: (context: PolicyContext<Context>) => PolicyDecision;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Input accepted by `definePolicy`. The branded result is the same
|
|
92
|
-
* shape; we keep the input/output split so callers can read JSDoc on the
|
|
93
|
-
* constructor without the type re-exposing `Policy` internals.
|
|
94
|
-
*/
|
|
95
|
-
interface DefinePolicyInput<Context = unknown> {
|
|
96
|
-
on: PolicyOperation;
|
|
97
|
-
/** Logical table name the policy applies to. */
|
|
98
|
-
table: string;
|
|
99
|
-
/**
|
|
100
|
-
* Decision function. Returning a `WhereInput` (read only) AND-merges the
|
|
101
|
-
* predicate; `true` allows; `false` denies; `undefined` skips this policy.
|
|
102
|
-
*
|
|
103
|
-
* NOTE: `count()` is **unsupported** on a policy-restricted table — the
|
|
104
|
-
* reader throws `LunoraError("COUNT_RLS_UNSUPPORTED")` (422). This mirrors
|
|
105
|
-
* kitcn's documented behavior.
|
|
106
|
-
*/
|
|
107
|
-
when: (context: PolicyContext<Context>) => PolicyDecision;
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* A named, abstract capability a policy can check with `ctx.auth.can(...)`,
|
|
111
|
-
* instead of branching on raw role strings. Declare one with
|
|
112
|
-
* `definePermission`; grant it to a role via {@link Role.permissions};
|
|
113
|
-
* register the roles with the middleware via {@link RlsOptions.roles}.
|
|
114
|
-
*/
|
|
115
|
-
interface Permission {
|
|
116
|
-
readonly description?: string;
|
|
117
|
-
readonly name: string;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* A role declaration. Roles are string labels attached to the request's
|
|
121
|
-
* identity (via better-auth claims today). `permissions` lists the
|
|
122
|
-
* capabilities the role grants — at request time the middleware unions the
|
|
123
|
-
* permissions of every role in `ctx.auth.roles` so a policy can ask
|
|
124
|
-
* `ctx.auth.can(permission)` rather than enumerate roles.
|
|
125
|
-
*/
|
|
126
|
-
interface Role {
|
|
127
|
-
readonly description?: string;
|
|
128
|
-
readonly name: string;
|
|
129
|
-
/** Permissions this role grants — by {@link Permission} object or bare name. */
|
|
130
|
-
readonly permissions?: ReadonlyArray<Permission | string>;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Options for the `rls(policies, options)` middleware. `roles` registers the
|
|
134
|
-
* role→permission grants that back `ctx.auth.can(...)`; a role not listed here
|
|
135
|
-
* grants no permissions (so `can` is conservative — it fails closed for
|
|
136
|
-
* unknown roles).
|
|
137
|
-
*/
|
|
138
|
-
interface RlsOptions {
|
|
139
|
-
readonly roles?: ReadonlyArray<Role>;
|
|
140
|
-
}
|
|
141
|
-
export { DefinePolicyInput as D, PolicyOperation as P, Role as R, TypedDefinePolicyInput as T, WhereInput as W, Policy as a, Permission as b, RlsOptions as c, PolicyContext as d, PolicyDecision as e, PolicyDecisionOf as f };
|