@lunora/server 1.0.0-alpha.23 → 1.0.0-alpha.230

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 (101) hide show
  1. package/LICENSE.md +213 -0
  2. package/README.md +18 -12
  3. package/dist/data-model.d.mts +392 -170
  4. package/dist/data-model.d.ts +392 -170
  5. package/dist/data-model.mjs +0 -1
  6. package/dist/drizzle.mjs +1 -1
  7. package/dist/in-process.d.mts +101 -0
  8. package/dist/in-process.d.ts +101 -0
  9. package/dist/in-process.mjs +1 -0
  10. package/dist/index.d.mts +3831 -1565
  11. package/dist/index.d.ts +3831 -1565
  12. package/dist/index.mjs +1 -29
  13. package/dist/otel.d.mts +554 -0
  14. package/dist/otel.d.ts +554 -0
  15. package/dist/otel.mjs +1 -0
  16. package/dist/packem_shared/ACTION_CACHE_DEFAULT_TTL_MS-9W5_vf8H.mjs +1 -0
  17. package/dist/packem_shared/DEFAULT_LIMIT-pT_tV8R0.mjs +1 -0
  18. package/dist/packem_shared/DOCUMENT_HISTORY_REDACTED_FIELDS-xyn3tFOE.mjs +1 -0
  19. package/dist/packem_shared/LunoraEnvError-bkdMu_-N.mjs +4 -0
  20. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-CKKVL22B.mjs +1 -0
  21. package/dist/packem_shared/allowAll-DkRAgItV.mjs +1 -0
  22. package/dist/packem_shared/apply-output-C5wZ5EAL.mjs +1 -0
  23. package/dist/packem_shared/asBucketStorage-DoGYOjq3.mjs +1 -0
  24. package/dist/packem_shared/assertShapesDeclareReadPolicies-Bg4Ter7Q.mjs +1 -0
  25. package/dist/packem_shared/beginDeferredDeletes-DhwFBbvs.mjs +1 -0
  26. package/dist/packem_shared/beginDeferredSchedules-C7QLYr6C.mjs +1 -0
  27. package/dist/packem_shared/bindOrm-9fbLRbOb.mjs +1 -0
  28. package/dist/packem_shared/buildMaskRegistry-DpWMtalG.mjs +1 -0
  29. package/dist/packem_shared/compose-middleware-BK9FlblN.mjs +1 -0
  30. package/dist/packem_shared/composePluginMiddleware-ZtDIkbvJ.mjs +1 -0
  31. package/dist/packem_shared/context-identity-B2XYGA0B.mjs +1 -0
  32. package/dist/packem_shared/createPolicyDsl-BZa6SqLJ.mjs +1 -0
  33. package/dist/packem_shared/createSecrets-D6rLB42U.mjs +1 -0
  34. package/dist/packem_shared/createServices-IJvoqi0e.mjs +1 -0
  35. package/dist/packem_shared/defineAggregateIndex-DF8NiKHr.mjs +1 -0
  36. package/dist/packem_shared/defineIdentity-DwkNKwYa.mjs +1 -0
  37. package/dist/packem_shared/defineIdentityGuard-Bt8N65Zs.mjs +1 -0
  38. package/dist/packem_shared/defineLiveStream-5DXpg7Jy.mjs +1 -0
  39. package/dist/packem_shared/defineMigration-CXOS0Bvq.mjs +1 -0
  40. package/dist/packem_shared/defineModule-OecXWM3U.mjs +1 -0
  41. package/dist/packem_shared/defineMutator-DlLFSWbO.mjs +1 -0
  42. package/dist/packem_shared/defineRoom-CoGi-xwm.mjs +1 -0
  43. package/dist/packem_shared/defineShape-CULsDRu5.mjs +1 -0
  44. package/dist/packem_shared/defineStorageRule-Dv4nJE0H.mjs +1 -0
  45. package/dist/packem_shared/defineWebSocket-BHoy6_ox.mjs +1 -0
  46. package/dist/packem_shared/fnv1a-D3ueNTWB.mjs +1 -0
  47. package/dist/packem_shared/functions-QFoBUe9t.mjs +1 -0
  48. package/dist/packem_shared/httpAction-DplHzcUa.mjs +6 -0
  49. package/dist/packem_shared/initLunora-DH7k24N0.mjs +1 -0
  50. package/dist/packem_shared/isPerDispatchMiddleware-DDQPatSm.mjs +1 -0
  51. package/dist/packem_shared/mask-D-jRCAhy.mjs +1 -0
  52. package/dist/packem_shared/middleware-3ySZVrpt.mjs +1 -0
  53. package/dist/packem_shared/onConnect-BLRoOpv2.mjs +1 -0
  54. package/dist/packem_shared/onQueryChange-iuLzmyPs.mjs +1 -0
  55. package/dist/packem_shared/onWhisper-CfDbFLQh.mjs +1 -0
  56. package/dist/packem_shared/platformAdmin-kczXQme8.mjs +1 -0
  57. package/dist/packem_shared/plugin-Bm9wIpiI.mjs +1 -0
  58. package/dist/packem_shared/policy-tag-V0lqFzgS.mjs +1 -0
  59. package/dist/packem_shared/protectPublic-Bi5_95aP.mjs +1 -0
  60. package/dist/packem_shared/rls-Dwj6TlY_.mjs +1 -0
  61. package/dist/packem_shared/run-middleware-BefYq7Xd.mjs +1 -0
  62. package/dist/packem_shared/serveStorageObject-B9GbK4qd.mjs +1 -0
  63. package/dist/packem_shared/storageRules-CBLioxF7.mjs +1 -0
  64. package/dist/packem_shared/types.d-B7FLdYUD.d.mts +148 -0
  65. package/dist/packem_shared/types.d-BCtjbIzl.d.ts +148 -0
  66. package/dist/packem_shared/types.d-CCjAj93e.d.mts +2902 -0
  67. package/dist/packem_shared/types.d-CCjAj93e.d.ts +2902 -0
  68. package/dist/rls/testing.d.mts +42 -34
  69. package/dist/rls/testing.d.ts +42 -34
  70. package/dist/rls/testing.mjs +1 -49
  71. package/dist/types.d.mts +2 -1274
  72. package/dist/types.d.ts +2 -1274
  73. package/dist/types.mjs +0 -31
  74. package/package.json +24 -6
  75. package/dist/packem_shared/LunoraEnvError-BGmd1Qs0.mjs +0 -187
  76. package/dist/packem_shared/LunoraError-WbxmrpxR.mjs +0 -9
  77. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-BAD0QPAg.mjs +0 -114
  78. package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +0 -11
  79. package/dist/packem_shared/bindOrm-Bfgl7f-Q.mjs +0 -146
  80. package/dist/packem_shared/buildRlsReadRegistry-D54vUQe4.mjs +0 -107
  81. package/dist/packem_shared/composePluginMiddleware-CcTj1_v2.mjs +0 -103
  82. package/dist/packem_shared/createPolicyDsl-By3QB4he.mjs +0 -32
  83. package/dist/packem_shared/createSecrets-DwaR2rNG.mjs +0 -58
  84. package/dist/packem_shared/defineAggregateIndex-znq4ygKQ.mjs +0 -295
  85. package/dist/packem_shared/defineIdentity-DiX4zM9x.mjs +0 -35
  86. package/dist/packem_shared/defineMigration-Hx01yIht.mjs +0 -10
  87. package/dist/packem_shared/defineMutator-EIXAWhs9.mjs +0 -11
  88. package/dist/packem_shared/defineShape-C5scNOrf.mjs +0 -18
  89. package/dist/packem_shared/defineStorageRule-B5nL4Z1P.mjs +0 -23
  90. package/dist/packem_shared/functions-Di9FUNkf.mjs +0 -5
  91. package/dist/packem_shared/httpAction-DCXoYPIk.mjs +0 -355
  92. package/dist/packem_shared/initLunora-CmMVk4i7.mjs +0 -107
  93. package/dist/packem_shared/mask-DSBtA3qp.mjs +0 -314
  94. package/dist/packem_shared/onConnect-CIPXKPyw.mjs +0 -13
  95. package/dist/packem_shared/policy-tag-DvpVH2tv.mjs +0 -13
  96. package/dist/packem_shared/protectPublic-BlcGpiRc.mjs +0 -15
  97. package/dist/packem_shared/rls-B_ZWgslr.mjs +0 -595
  98. package/dist/packem_shared/run-middleware-I6EiQfxL.mjs +0 -20
  99. package/dist/packem_shared/storageRules-6QxzDOcx.mjs +0 -88
  100. package/dist/packem_shared/types.d-BB3pjV0m.d.mts +0 -141
  101. package/dist/packem_shared/types.d-Cxl6ndhm.d.ts +0 -141
@@ -0,0 +1,148 @@
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, read
55
+ * from the `roles` CLAIM on the resolved identity — a `string[]` or a
56
+ * comma-separated string returned by `WorkerOptions.resolveIdentity` (and
57
+ * validated by `defineIdentity(...)` at the worker's trust boundary). A
58
+ * resolver that forwards no `roles` claim yields no roles, so every
59
+ * role-branched policy is evaluated against `[]`. `auth.can`
60
+ * answers whether any of those roles grants a permission (see
61
+ * {@link Permission} / {@link RlsOptions}). `row` is present only on write
62
+ * policies (`insert`/`update`/`delete`) — for `update` and `delete` it is the
63
+ * pre-write row; for `insert` it is the candidate document. `ctx` is the full
64
+ * procedure context the middleware closed over.
65
+ */
66
+ interface PolicyContext<Context = unknown, Identity = Record<string, unknown>> {
67
+ readonly auth: {
68
+ /**
69
+ * `true` when any of the request's `roles` grants `permission` (passed
70
+ * by {@link Permission} object or its `name`). Always `false` when no
71
+ * roles were handed to the middleware (`rls(policies, { roles })`), or
72
+ * when none of the request's roles lists the permission.
73
+ */
74
+ readonly can: (permission: Permission | string) => boolean;
75
+ /**
76
+ * The resolved identity, typed to the `Identity` type parameter bound on
77
+ * `createPolicyDsl` — e.g. the app's `defineIdentity(...)` claim type via
78
+ * the `InferIdentity` helper; otherwise the untyped claim bag.
79
+ * `undefined`/`null` when the request is anonymous.
80
+ */
81
+ readonly identity?: Identity | null;
82
+ readonly roles: ReadonlyArray<string>;
83
+ readonly userId: null | string;
84
+ };
85
+ readonly ctx: Context;
86
+ readonly row?: Record<string, unknown>;
87
+ }
88
+ /** A registered policy as stored in the policy table. */
89
+ interface Policy<Context = unknown> {
90
+ readonly on: PolicyOperation;
91
+ readonly table: string;
92
+ readonly when: (context: PolicyContext<Context>) => PolicyDecision;
93
+ }
94
+ /**
95
+ * Input accepted by `definePolicy`. The branded result is the same
96
+ * shape; we keep the input/output split so callers can read JSDoc on the
97
+ * constructor without the type re-exposing `Policy` internals.
98
+ */
99
+ interface DefinePolicyInput<Context = unknown> {
100
+ on: PolicyOperation;
101
+ /** Logical table name the policy applies to. */
102
+ table: string;
103
+ /**
104
+ * Decision function. Returning a `WhereInput` (read only) AND-merges the
105
+ * predicate; `true` allows; `false` denies; `undefined` skips this policy.
106
+ *
107
+ * NOTE: `count()` is **unsupported** on a policy-restricted table — the
108
+ * reader throws `LunoraError("COUNT_RLS_UNSUPPORTED")` (422). This mirrors
109
+ * kitcn's documented behavior.
110
+ */
111
+ when: (context: PolicyContext<Context>) => PolicyDecision;
112
+ }
113
+ /**
114
+ * A named, abstract capability a policy can check with `ctx.auth.can(...)`,
115
+ * instead of branching on raw role strings. Declare one with
116
+ * `definePermission`; grant it to a role via {@link Role.permissions};
117
+ * register the roles with the middleware via {@link RlsOptions.roles}.
118
+ */
119
+ interface Permission {
120
+ readonly description?: string;
121
+ readonly name: string;
122
+ }
123
+ /**
124
+ * A role declaration. Roles are string labels the request carries in the
125
+ * `roles` claim of its resolved identity — `WorkerOptions.resolveIdentity`
126
+ * forwards every non-`userId` key verbatim, so a resolver returning
127
+ * `{ userId, roles: ["admin"] }` (or the comma-joined `"user,admin"` that
128
+ * better-auth's `admin()` plugin stores) is what puts them there. `permissions`
129
+ * lists the capabilities the role grants — at request time the middleware
130
+ * unions the permissions of every role in `ctx.auth.roles` so a policy can ask
131
+ * `ctx.auth.can(permission)` rather than enumerate roles.
132
+ */
133
+ interface Role {
134
+ readonly description?: string;
135
+ readonly name: string;
136
+ /** Permissions this role grants — by {@link Permission} object or bare name. */
137
+ readonly permissions?: ReadonlyArray<Permission | string>;
138
+ }
139
+ /**
140
+ * Options for the `rls(policies, options)` middleware. `roles` registers the
141
+ * role→permission grants that back `ctx.auth.can(...)`; a role not listed here
142
+ * grants no permissions (so `can` is conservative — it fails closed for
143
+ * unknown roles).
144
+ */
145
+ interface RlsOptions {
146
+ readonly roles?: ReadonlyArray<Role>;
147
+ }
148
+ export { DefinePolicyInput as D, PolicyOperation as P, Role as R, TypedDefinePolicyInput as T, WhereInput as W, Policy as a, RlsOptions as b, Permission as c, PolicyContext as d, PolicyDecision as e, PolicyDecisionOf as f };