@lunora/server 1.0.0-alpha.3 → 1.0.0-alpha.30

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 (41) hide show
  1. package/LICENSE.md +6 -0
  2. package/README.md +50 -0
  3. package/dist/data-model.d.mts +231 -156
  4. package/dist/data-model.d.ts +231 -156
  5. package/dist/index.d.mts +1191 -811
  6. package/dist/index.d.ts +1191 -811
  7. package/dist/index.mjs +20 -15
  8. package/dist/packem_shared/{LunoraEnvError-DjFkpkSP.mjs → LunoraEnvError-BGmd1Qs0.mjs} +4 -4
  9. package/dist/packem_shared/LunoraError-WbxmrpxR.mjs +9 -0
  10. package/dist/packem_shared/{PRESENCE_DEFAULT_TTL_MS-UQuUI5sV.mjs → PRESENCE_DEFAULT_TTL_MS-Cr0i4mTv.mjs} +5 -5
  11. package/dist/packem_shared/{bindOrm-Ce57S3N9.mjs → bindOrm-CaY7Wq9Z.mjs} +42 -23
  12. package/dist/packem_shared/buildRlsReadRegistry-2uk_GfiH.mjs +107 -0
  13. package/dist/packem_shared/{composePluginMiddleware-Ck5_TUO8.mjs → composePluginMiddleware-z62dttBo.mjs} +11 -8
  14. package/dist/packem_shared/{createPolicyDsl-De67zPDS.mjs → createPolicyDsl-By3QB4he.mjs} +4 -1
  15. package/dist/packem_shared/createSecrets-DwaR2rNG.mjs +58 -0
  16. package/dist/packem_shared/{defineAggregateIndex-B20MIOmj.mjs → defineAggregateIndex-cdo0g-un.mjs} +108 -8
  17. package/dist/packem_shared/defineIdentity-DiX4zM9x.mjs +35 -0
  18. package/dist/packem_shared/{defineMigration-CAJLr6fx.mjs → defineMigration-Hx01yIht.mjs} +3 -1
  19. package/dist/packem_shared/defineMutator-EIXAWhs9.mjs +11 -0
  20. package/dist/packem_shared/defineShape-C5scNOrf.mjs +18 -0
  21. package/dist/packem_shared/{defineStorageRule-qu0mpilX.mjs → defineStorageRule-B5nL4Z1P.mjs} +4 -1
  22. package/dist/packem_shared/functions-Di9FUNkf.mjs +5 -0
  23. package/dist/packem_shared/{httpAction-B7FYUEgr.mjs → httpAction-DCXoYPIk.mjs} +42 -27
  24. package/dist/packem_shared/{initLunora-CATvPsVt.mjs → initLunora-D0Wuki7S.mjs} +36 -7
  25. package/dist/packem_shared/{mask-eCUYOwhd.mjs → mask-BepaW7YN.mjs} +132 -9
  26. package/dist/packem_shared/policy-tag-DvpVH2tv.mjs +13 -0
  27. package/dist/packem_shared/{protectPublic-BjFkQ_Or.mjs → protectPublic-BlcGpiRc.mjs} +1 -1
  28. package/dist/packem_shared/{rls-Bi9HiyDC.mjs → rls-BaDQf7MG.mjs} +34 -6
  29. package/dist/packem_shared/{run-middleware-CYQOuoV6.mjs → run-middleware-I6EiQfxL.mjs} +3 -1
  30. package/dist/packem_shared/{storageRules-4a30FSpI.mjs → storageRules-6QxzDOcx.mjs} +1 -1
  31. package/dist/packem_shared/types.d-C4CMJK8x.d.mts +141 -0
  32. package/dist/packem_shared/types.d-DdYF8E18.d.ts +141 -0
  33. package/dist/rls/testing.d.mts +31 -31
  34. package/dist/rls/testing.d.ts +31 -31
  35. package/dist/rls/testing.mjs +1 -1
  36. package/dist/types.d.mts +957 -433
  37. package/dist/types.d.ts +957 -433
  38. package/package.json +6 -5
  39. package/dist/packem_shared/LunoraError-DhggBJZF.mjs +0 -51
  40. package/dist/packem_shared/types.d-BDY0FYHK.d.ts +0 -135
  41. package/dist/packem_shared/types.d-DmvyEMD6.d.mts +0 -135
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/server",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.30",
4
4
  "description": "Server primitives for Lunora: defineSchema, defineTable, query, mutation, and action",
5
5
  "keywords": [
6
6
  "backend",
@@ -25,7 +25,7 @@
25
25
  "directory": "packages/server"
26
26
  },
27
27
  "files": [
28
- "dist",
28
+ "./dist",
29
29
  "README.md",
30
30
  "LICENSE.md",
31
31
  "__assets__"
@@ -62,10 +62,11 @@
62
62
  "access": "public"
63
63
  },
64
64
  "dependencies": {
65
- "@lunora/scheduler": "1.0.0-alpha.1",
66
- "@lunora/values": "1.0.0-alpha.2",
65
+ "@lunora/errors": "1.0.0-alpha.7",
66
+ "@lunora/scheduler": "1.0.0-alpha.11",
67
+ "@lunora/values": "1.0.0-alpha.10",
67
68
  "drizzle-orm": "^0.45.2",
68
- "hono": "^4.12.26"
69
+ "hono": "^4.12.30"
69
70
  },
70
71
  "engines": {
71
72
  "node": "^22.15.0 || >=24.11.0"
@@ -1,51 +0,0 @@
1
- const CODE_STATUS = {
2
- BAD_REQUEST: 400,
3
- CONFLICT: 409,
4
- /**
5
- * `count()` invoked against a table whose context carries an active RLS
6
- * policy. The operation itself is unsupported in an RLS-restricted reader
7
- * (kitcn's documented constraint) — the request is well-formed and the
8
- * caller is authorized, so this is a 422 (semantic conflict) rather than a
9
- * 403 (policy denial).
10
- */
11
- COUNT_RLS_UNSUPPORTED: 422,
12
- FORBIDDEN: 403,
13
- INTERNAL_SERVER_ERROR: 500,
14
- /**
15
- * An analytical reduction (`aggregate` / `groupBy`) was invoked over a
16
- * column that the procedure's `mask()` middleware redacts. A masked column
17
- * can't be summed, averaged, or grouped without leaking the very values the
18
- * mask hides (a group key *is* the raw value; an aggregate is computed from
19
- * it), so the operation fails closed. The request is well-formed and the
20
- * caller is authorized — this is a 422 (semantic conflict), mirroring
21
- * `COUNT_RLS_UNSUPPORTED`.
22
- */
23
- MASK_UNSUPPORTED: 422,
24
- NOT_FOUND: 404,
25
- NOT_IMPLEMENTED: 501,
26
- /**
27
- * A write policy's `when` returned a relation-crossing predicate
28
- * (`some`/`none`/`every`/`is`/`isNot`). The in-memory write-policy evaluator
29
- * has no child fetcher and cannot resolve a relation node, so the policy is
30
- * unsupported as written. Relation predicates are valid in *read* policies
31
- * and query `where` clauses (the pre-resolver handles them there). The
32
- * request is well-formed; this is a 422 (semantic conflict), mirroring the
33
- * sibling `*_UNSUPPORTED` codes.
34
- */
35
- RELATION_PREDICATE_UNSUPPORTED: 422,
36
- TOO_MANY_REQUESTS: 429,
37
- UNAUTHORIZED: 401,
38
- UNPROCESSABLE: 422
39
- };
40
- class LunoraError extends Error {
41
- name = "LunoraError";
42
- code;
43
- status;
44
- constructor(code, message) {
45
- super(message ?? code);
46
- this.code = code;
47
- this.status = CODE_STATUS[code];
48
- }
49
- }
50
-
51
- export { LunoraError };
@@ -1,135 +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> {
49
- on: PolicyOperation;
50
- table: T;
51
- when: (context: PolicyContext<Context>) => 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> {
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
- readonly identity?: Record<string, unknown> | null;
72
- readonly roles: ReadonlyArray<string>;
73
- readonly userId: null | string;
74
- };
75
- readonly ctx: Context;
76
- readonly row?: Record<string, unknown>;
77
- }
78
- /** A registered policy as stored in the policy table. */
79
- interface Policy<Context = unknown> {
80
- readonly on: PolicyOperation;
81
- readonly table: string;
82
- readonly when: (context: PolicyContext<Context>) => PolicyDecision;
83
- }
84
- /**
85
- * Input accepted by `definePolicy`. The branded result is the same
86
- * shape; we keep the input/output split so callers can read JSDoc on the
87
- * constructor without the type re-exposing `Policy` internals.
88
- */
89
- interface DefinePolicyInput<Context = unknown> {
90
- on: PolicyOperation;
91
- /** Logical table name the policy applies to. */
92
- table: string;
93
- /**
94
- * Decision function. Returning a `WhereInput` (read only) AND-merges the
95
- * predicate; `true` allows; `false` denies; `undefined` skips this policy.
96
- *
97
- * NOTE: `count()` is **unsupported** on a policy-restricted table — the
98
- * reader throws `LunoraError("COUNT_RLS_UNSUPPORTED")` (422). This mirrors
99
- * kitcn's documented behavior.
100
- */
101
- when: (context: PolicyContext<Context>) => PolicyDecision;
102
- }
103
- /**
104
- * A named, abstract capability a policy can check with `ctx.auth.can(...)`,
105
- * instead of branching on raw role strings. Declare one with
106
- * `definePermission`; grant it to a role via {@link Role.permissions};
107
- * register the roles with the middleware via {@link RlsOptions.roles}.
108
- */
109
- interface Permission {
110
- readonly description?: string;
111
- readonly name: string;
112
- }
113
- /**
114
- * A role declaration. Roles are string labels attached to the request's
115
- * identity (via better-auth claims today). `permissions` lists the
116
- * capabilities the role grants — at request time the middleware unions the
117
- * permissions of every role in `ctx.auth.roles` so a policy can ask
118
- * `ctx.auth.can(permission)` rather than enumerate roles.
119
- */
120
- interface Role {
121
- readonly description?: string;
122
- readonly name: string;
123
- /** Permissions this role grants — by {@link Permission} object or bare name. */
124
- readonly permissions?: ReadonlyArray<Permission | string>;
125
- }
126
- /**
127
- * Options for the `rls(policies, options)` middleware. `roles` registers the
128
- * role→permission grants that back `ctx.auth.can(...)`; a role not listed here
129
- * grants no permissions (so `can` is conservative — it fails closed for
130
- * unknown roles).
131
- */
132
- interface RlsOptions {
133
- readonly roles?: ReadonlyArray<Role>;
134
- }
135
- 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,135 +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&lt;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> {
49
- on: PolicyOperation;
50
- table: T;
51
- when: (context: PolicyContext<Context>) => 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> {
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
- readonly identity?: Record<string, unknown> | null;
72
- readonly roles: ReadonlyArray<string>;
73
- readonly userId: null | string;
74
- };
75
- readonly ctx: Context;
76
- readonly row?: Record<string, unknown>;
77
- }
78
- /** A registered policy as stored in the policy table. */
79
- interface Policy<Context = unknown> {
80
- readonly on: PolicyOperation;
81
- readonly table: string;
82
- readonly when: (context: PolicyContext<Context>) => PolicyDecision;
83
- }
84
- /**
85
- * Input accepted by `definePolicy`. The branded result is the same
86
- * shape; we keep the input/output split so callers can read JSDoc on the
87
- * constructor without the type re-exposing `Policy` internals.
88
- */
89
- interface DefinePolicyInput<Context = unknown> {
90
- on: PolicyOperation;
91
- /** Logical table name the policy applies to. */
92
- table: string;
93
- /**
94
- * Decision function. Returning a `WhereInput` (read only) AND-merges the
95
- * predicate; `true` allows; `false` denies; `undefined` skips this policy.
96
- *
97
- * NOTE: `count()` is **unsupported** on a policy-restricted table — the
98
- * reader throws `LunoraError("COUNT_RLS_UNSUPPORTED")` (422). This mirrors
99
- * kitcn's documented behavior.
100
- */
101
- when: (context: PolicyContext<Context>) => PolicyDecision;
102
- }
103
- /**
104
- * A named, abstract capability a policy can check with `ctx.auth.can(...)`,
105
- * instead of branching on raw role strings. Declare one with
106
- * `definePermission`; grant it to a role via {@link Role.permissions};
107
- * register the roles with the middleware via {@link RlsOptions.roles}.
108
- */
109
- interface Permission {
110
- readonly description?: string;
111
- readonly name: string;
112
- }
113
- /**
114
- * A role declaration. Roles are string labels attached to the request's
115
- * identity (via better-auth claims today). `permissions` lists the
116
- * capabilities the role grants — at request time the middleware unions the
117
- * permissions of every role in `ctx.auth.roles` so a policy can ask
118
- * `ctx.auth.can(permission)` rather than enumerate roles.
119
- */
120
- interface Role {
121
- readonly description?: string;
122
- readonly name: string;
123
- /** Permissions this role grants — by {@link Permission} object or bare name. */
124
- readonly permissions?: ReadonlyArray<Permission | string>;
125
- }
126
- /**
127
- * Options for the `rls(policies, options)` middleware. `roles` registers the
128
- * role→permission grants that back `ctx.auth.can(...)`; a role not listed here
129
- * grants no permissions (so `can` is conservative — it fails closed for
130
- * unknown roles).
131
- */
132
- interface RlsOptions {
133
- readonly roles?: ReadonlyArray<Role>;
134
- }
135
- 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 };