@lunora/server 1.0.0-alpha.21 → 1.0.0-alpha.210

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 (97) 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 +3618 -1548
  11. package/dist/index.d.ts +3618 -1548
  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-mmP-6BcT.mjs +1 -0
  17. package/dist/packem_shared/DEFAULT_LIMIT-pT_tV8R0.mjs +1 -0
  18. package/dist/packem_shared/DOCUMENT_HISTORY_REDACTED_FIELDS-B5Zp-7SI.mjs +1 -0
  19. package/dist/packem_shared/LunoraEnvError-bkdMu_-N.mjs +4 -0
  20. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-DFpy5J8G.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/defineMigration-CXOS0Bvq.mjs +1 -0
  39. package/dist/packem_shared/defineModule-OecXWM3U.mjs +1 -0
  40. package/dist/packem_shared/defineMutator-B4ipDtDR.mjs +1 -0
  41. package/dist/packem_shared/defineRoom-CoGi-xwm.mjs +1 -0
  42. package/dist/packem_shared/defineShape-CwKm5KLp.mjs +1 -0
  43. package/dist/packem_shared/defineStorageRule-Dv4nJE0H.mjs +1 -0
  44. package/dist/packem_shared/fnv1a-D3ueNTWB.mjs +1 -0
  45. package/dist/packem_shared/functions-B2-H4PQT.mjs +1 -0
  46. package/dist/packem_shared/httpAction-D2S7bQzl.mjs +6 -0
  47. package/dist/packem_shared/initLunora-B-7Xs3Qq.mjs +1 -0
  48. package/dist/packem_shared/isPerDispatchMiddleware-DDQPatSm.mjs +1 -0
  49. package/dist/packem_shared/mask-D-jRCAhy.mjs +1 -0
  50. package/dist/packem_shared/middleware-3ySZVrpt.mjs +1 -0
  51. package/dist/packem_shared/onConnect-BLRoOpv2.mjs +1 -0
  52. package/dist/packem_shared/onQueryChange-iuLzmyPs.mjs +1 -0
  53. package/dist/packem_shared/onWhisper-CfDbFLQh.mjs +1 -0
  54. package/dist/packem_shared/platformAdmin-kczXQme8.mjs +1 -0
  55. package/dist/packem_shared/plugin-Bm9wIpiI.mjs +1 -0
  56. package/dist/packem_shared/policy-tag-V0lqFzgS.mjs +1 -0
  57. package/dist/packem_shared/protectPublic-Bi5_95aP.mjs +1 -0
  58. package/dist/packem_shared/rls-Dwj6TlY_.mjs +1 -0
  59. package/dist/packem_shared/run-middleware-BefYq7Xd.mjs +1 -0
  60. package/dist/packem_shared/serveStorageObject-aqBFrLyo.mjs +1 -0
  61. package/dist/packem_shared/storageRules-CBLioxF7.mjs +1 -0
  62. package/dist/packem_shared/types.d-B7FLdYUD.d.mts +148 -0
  63. package/dist/packem_shared/types.d-BCtjbIzl.d.ts +148 -0
  64. package/dist/rls/testing.d.mts +42 -34
  65. package/dist/rls/testing.d.ts +42 -34
  66. package/dist/rls/testing.mjs +1 -49
  67. package/dist/types.d.mts +2234 -623
  68. package/dist/types.d.ts +2234 -623
  69. package/dist/types.mjs +0 -31
  70. package/package.json +24 -6
  71. package/dist/packem_shared/LunoraEnvError-BGmd1Qs0.mjs +0 -187
  72. package/dist/packem_shared/LunoraError-WbxmrpxR.mjs +0 -9
  73. package/dist/packem_shared/PRESENCE_DEFAULT_TTL_MS-BtPCiNLW.mjs +0 -114
  74. package/dist/packem_shared/asBucketStorage-Cnxd9y2q.mjs +0 -11
  75. package/dist/packem_shared/bindOrm-Bfgl7f-Q.mjs +0 -146
  76. package/dist/packem_shared/buildRlsReadRegistry-D54vUQe4.mjs +0 -107
  77. package/dist/packem_shared/composePluginMiddleware-CcTj1_v2.mjs +0 -103
  78. package/dist/packem_shared/createPolicyDsl-By3QB4he.mjs +0 -32
  79. package/dist/packem_shared/createSecrets-DwaR2rNG.mjs +0 -58
  80. package/dist/packem_shared/defineAggregateIndex-znq4ygKQ.mjs +0 -295
  81. package/dist/packem_shared/defineIdentity-DiX4zM9x.mjs +0 -35
  82. package/dist/packem_shared/defineMigration-Hx01yIht.mjs +0 -10
  83. package/dist/packem_shared/defineMutator-EIXAWhs9.mjs +0 -11
  84. package/dist/packem_shared/defineShape-C5scNOrf.mjs +0 -18
  85. package/dist/packem_shared/defineStorageRule-B5nL4Z1P.mjs +0 -23
  86. package/dist/packem_shared/functions-Di9FUNkf.mjs +0 -5
  87. package/dist/packem_shared/httpAction-DCXoYPIk.mjs +0 -355
  88. package/dist/packem_shared/initLunora-sQUqaejx.mjs +0 -100
  89. package/dist/packem_shared/mask-BT1YgRbF.mjs +0 -291
  90. package/dist/packem_shared/onConnect-CIPXKPyw.mjs +0 -13
  91. package/dist/packem_shared/policy-tag-DvpVH2tv.mjs +0 -13
  92. package/dist/packem_shared/protectPublic-BlcGpiRc.mjs +0 -15
  93. package/dist/packem_shared/rls-B_ZWgslr.mjs +0 -595
  94. package/dist/packem_shared/run-middleware-I6EiQfxL.mjs +0 -20
  95. package/dist/packem_shared/storageRules-6QxzDOcx.mjs +0 -88
  96. package/dist/packem_shared/types.d-BB3pjV0m.d.mts +0 -141
  97. package/dist/packem_shared/types.d-Cxl6ndhm.d.ts +0 -141
@@ -1 +0,0 @@
1
-
package/dist/drizzle.mjs CHANGED
@@ -1 +1 @@
1
- export * from 'drizzle-orm/sqlite-core';
1
+ export*from"drizzle-orm/sqlite-core";
@@ -0,0 +1,101 @@
1
+ import { SqlExec } from '@lunora/shard-engine';
2
+ import { DatabaseWriter, Schema } from "./types.mjs";
3
+ import '@lunora/values';
4
+ /** The kind of a registered Lunora function. */
5
+ type FunctionKind = "action" | "mutation" | "query";
6
+ /**
7
+ * A caller-supplied identity. `userId` is the subject a handler reads from
8
+ * `ctx.auth.userId`; every other field is returned from
9
+ * `ctx.auth.getIdentity()` (mirroring a decoded JWT's claims).
10
+ */
11
+ interface InProcessIdentity extends Record<string, unknown> {
12
+ userId?: null | string;
13
+ }
14
+ /** The `userId` + claims a dispatch runs under, after production's normalisation. */
15
+ interface ResolvedIdentity {
16
+ readonly claims: Record<string, unknown> | null;
17
+ readonly userId: null | string;
18
+ }
19
+ /**
20
+ * Reduce a caller-supplied {@link InProcessIdentity} to what production would
21
+ * actually hand a shard.
22
+ *
23
+ * Mirrors `@lunora/runtime`'s `create-worker.ts` identity forwarding: an identity
24
+ * whose `userId` is not a non-empty string is dropped to anonymous outright, and
25
+ * the forwarded claims are the identity MINUS `userId` (`null` when nothing is
26
+ * left), because the shard reads the subject from its own header and surfaces
27
+ * only the remaining claims through `ctx.auth.getIdentity()`.
28
+ * @param identity the caller-supplied identity, or `null` for anonymous
29
+ * @returns the identity production would forward
30
+ */
31
+ declare const resolveInProcessIdentity: (identity: InProcessIdentity | null) => ResolvedIdentity;
32
+ /** Options accepted by {@link createInProcessRuntime}. */
33
+ interface InProcessRuntimeOptions {
34
+ /**
35
+ * Enforce the secure-by-default RLS guard on {@link InProcessRuntime.createWriters}'s
36
+ * `database` — the same `enforceRls: true` production's generated
37
+ * `buildCtx` always passes. A no-op unless the schema is `.rls("required")`.
38
+ * @default true
39
+ */
40
+ enforceRls?: boolean;
41
+ /**
42
+ * The `ctx.scheduler` the consumer installs, when it installs one. A
43
+ * scheduler wrapped with `withDeferredSchedules` has the jobs a mutation
44
+ * schedules held until its COMMIT and dropped on its ROLLBACK, exactly as the
45
+ * generated shard's `runMutationTransaction` does.
46
+ */
47
+ scheduler?: unknown;
48
+ /** The synchronous SQLite engine the store runs on. */
49
+ sql: SqlExec;
50
+ }
51
+ /** The engine-bound half of a function dispatch, shared by every identity view. */
52
+ interface InProcessRuntime {
53
+ /**
54
+ * The guarded `ctx.db` writer for one identity, plus the trusted, UNGUARDED
55
+ * writer behind it (`rawDatabase` — what production's admin/migration paths
56
+ * use). Both carry the per-table facade (`ctx.db.<table>`).
57
+ */
58
+ createWriters: (identity: ResolvedIdentity) => {
59
+ database: DatabaseWriter;
60
+ rawDatabase: DatabaseWriter;
61
+ };
62
+ /** Start a new top-level dispatch with a full resource budget. Call before every query/action entry. */
63
+ resetHeadroom: () => void;
64
+ /**
65
+ * Run `body` as one top-level entry: serialized behind every other entry that
66
+ * came through here, inside a BEGIN/COMMIT span that rolls back on a throw,
67
+ * with a fresh resource budget. Every mutation entry must use it; a read that
68
+ * must not observe another entry's uncommitted writes may use it too.
69
+ */
70
+ runInTransaction: <R>(body: () => Promise<R> | R) => Promise<R>;
71
+ }
72
+ /**
73
+ * Migrate `schema` onto `options.sql` and return the dispatch wiring around it.
74
+ * @param schema the app schema from `defineSchema`
75
+ * @param options the engine, plus the RLS and scheduler settings
76
+ * @returns the writer factory and the mutation transaction runner
77
+ */
78
+ declare const createInProcessRuntime: (schema: Schema, options: InProcessRuntimeOptions) => InProcessRuntime;
79
+ /**
80
+ * The kind of a registered function object, or `undefined` for anything else
81
+ * (an inline callback, a plain value).
82
+ * @param value the candidate reference
83
+ * @returns its kind, when it is a registered function
84
+ */
85
+ declare const registeredFunctionKind: (value: unknown) => FunctionKind | undefined;
86
+ /**
87
+ * Invoke a registered function's handler, refusing a reference of the wrong
88
+ * kind and — unless `allowInternal` — an `internal*` function, which is
89
+ * unreachable from the external RPC boundary in production.
90
+ * @param expected which kind of function the call site dispatches
91
+ * @param reference the registered function object
92
+ * @param reference.handler its handler, invoked with `(context, args)`
93
+ * @param context the `ctx` to hand the handler
94
+ * @param args the call's arguments (`{}` when absent)
95
+ * @param allowInternal whether this is a server-to-server (`ctx.run*`) dispatch
96
+ * @returns the handler's result
97
+ */
98
+ declare const runRegisteredFunction: (expected: FunctionKind, reference: {
99
+ handler: (context: unknown, args: never) => unknown;
100
+ }, context: unknown, args: unknown, allowInternal: boolean) => Promise<unknown>;
101
+ export { type FunctionKind, type InProcessIdentity, type InProcessRuntime, type InProcessRuntimeOptions, type ResolvedIdentity, createInProcessRuntime, registeredFunctionKind, resolveInProcessIdentity, runRegisteredFunction };
@@ -0,0 +1,101 @@
1
+ import { SqlExec } from '@lunora/shard-engine';
2
+ import { DatabaseWriter, Schema } from "./types.js";
3
+ import '@lunora/values';
4
+ /** The kind of a registered Lunora function. */
5
+ type FunctionKind = "action" | "mutation" | "query";
6
+ /**
7
+ * A caller-supplied identity. `userId` is the subject a handler reads from
8
+ * `ctx.auth.userId`; every other field is returned from
9
+ * `ctx.auth.getIdentity()` (mirroring a decoded JWT's claims).
10
+ */
11
+ interface InProcessIdentity extends Record<string, unknown> {
12
+ userId?: null | string;
13
+ }
14
+ /** The `userId` + claims a dispatch runs under, after production's normalisation. */
15
+ interface ResolvedIdentity {
16
+ readonly claims: Record<string, unknown> | null;
17
+ readonly userId: null | string;
18
+ }
19
+ /**
20
+ * Reduce a caller-supplied {@link InProcessIdentity} to what production would
21
+ * actually hand a shard.
22
+ *
23
+ * Mirrors `@lunora/runtime`'s `create-worker.ts` identity forwarding: an identity
24
+ * whose `userId` is not a non-empty string is dropped to anonymous outright, and
25
+ * the forwarded claims are the identity MINUS `userId` (`null` when nothing is
26
+ * left), because the shard reads the subject from its own header and surfaces
27
+ * only the remaining claims through `ctx.auth.getIdentity()`.
28
+ * @param identity the caller-supplied identity, or `null` for anonymous
29
+ * @returns the identity production would forward
30
+ */
31
+ declare const resolveInProcessIdentity: (identity: InProcessIdentity | null) => ResolvedIdentity;
32
+ /** Options accepted by {@link createInProcessRuntime}. */
33
+ interface InProcessRuntimeOptions {
34
+ /**
35
+ * Enforce the secure-by-default RLS guard on {@link InProcessRuntime.createWriters}'s
36
+ * `database` — the same `enforceRls: true` production's generated
37
+ * `buildCtx` always passes. A no-op unless the schema is `.rls("required")`.
38
+ * @default true
39
+ */
40
+ enforceRls?: boolean;
41
+ /**
42
+ * The `ctx.scheduler` the consumer installs, when it installs one. A
43
+ * scheduler wrapped with `withDeferredSchedules` has the jobs a mutation
44
+ * schedules held until its COMMIT and dropped on its ROLLBACK, exactly as the
45
+ * generated shard's `runMutationTransaction` does.
46
+ */
47
+ scheduler?: unknown;
48
+ /** The synchronous SQLite engine the store runs on. */
49
+ sql: SqlExec;
50
+ }
51
+ /** The engine-bound half of a function dispatch, shared by every identity view. */
52
+ interface InProcessRuntime {
53
+ /**
54
+ * The guarded `ctx.db` writer for one identity, plus the trusted, UNGUARDED
55
+ * writer behind it (`rawDatabase` — what production's admin/migration paths
56
+ * use). Both carry the per-table facade (`ctx.db.<table>`).
57
+ */
58
+ createWriters: (identity: ResolvedIdentity) => {
59
+ database: DatabaseWriter;
60
+ rawDatabase: DatabaseWriter;
61
+ };
62
+ /** Start a new top-level dispatch with a full resource budget. Call before every query/action entry. */
63
+ resetHeadroom: () => void;
64
+ /**
65
+ * Run `body` as one top-level entry: serialized behind every other entry that
66
+ * came through here, inside a BEGIN/COMMIT span that rolls back on a throw,
67
+ * with a fresh resource budget. Every mutation entry must use it; a read that
68
+ * must not observe another entry's uncommitted writes may use it too.
69
+ */
70
+ runInTransaction: <R>(body: () => Promise<R> | R) => Promise<R>;
71
+ }
72
+ /**
73
+ * Migrate `schema` onto `options.sql` and return the dispatch wiring around it.
74
+ * @param schema the app schema from `defineSchema`
75
+ * @param options the engine, plus the RLS and scheduler settings
76
+ * @returns the writer factory and the mutation transaction runner
77
+ */
78
+ declare const createInProcessRuntime: (schema: Schema, options: InProcessRuntimeOptions) => InProcessRuntime;
79
+ /**
80
+ * The kind of a registered function object, or `undefined` for anything else
81
+ * (an inline callback, a plain value).
82
+ * @param value the candidate reference
83
+ * @returns its kind, when it is a registered function
84
+ */
85
+ declare const registeredFunctionKind: (value: unknown) => FunctionKind | undefined;
86
+ /**
87
+ * Invoke a registered function's handler, refusing a reference of the wrong
88
+ * kind and — unless `allowInternal` — an `internal*` function, which is
89
+ * unreachable from the external RPC boundary in production.
90
+ * @param expected which kind of function the call site dispatches
91
+ * @param reference the registered function object
92
+ * @param reference.handler its handler, invoked with `(context, args)`
93
+ * @param context the `ctx` to hand the handler
94
+ * @param args the call's arguments (`{}` when absent)
95
+ * @param allowInternal whether this is a server-to-server (`ctx.run*`) dispatch
96
+ * @returns the handler's result
97
+ */
98
+ declare const runRegisteredFunction: (expected: FunctionKind, reference: {
99
+ handler: (context: unknown, args: never) => unknown;
100
+ }, context: unknown, args: unknown, allowInternal: boolean) => Promise<unknown>;
101
+ export { type FunctionKind, type InProcessIdentity, type InProcessRuntime, type InProcessRuntimeOptions, type ResolvedIdentity, createInProcessRuntime, registeredFunctionKind, resolveInProcessIdentity, runRegisteredFunction };
@@ -0,0 +1 @@
1
+ import{LunoraError as I}from"@lunora/errors";import{runShardMigrations as R,TransactionHeadroomTracker as h,createShardCtxDb as T,RLS_UNWRAP_SYMBOL as g}from"@lunora/shard-engine";import{beginDeferredSchedules as p}from"./packem_shared/beginDeferredSchedules-C7QLYr6C.mjs";import{bindTableFacade as y}from"./packem_shared/bindOrm-9fbLRbOb.mjs";const k=e=>{if(e===null||typeof e.userId!="string"||e.userId.length===0)return{claims:null,userId:null};const{userId:r,...s}=e;return{claims:Object.keys(s).length>0?s:null,userId:r}};class S extends h{current=new h;reset(){this.current=new h}headroom(){return this.current.headroom()}recordRead(r){this.current.recordRead(r)}recordWrite(r){this.current.recordWrite(r)}}const x=(e,r)=>{const{scheduler:s,sql:a}=r,i=e;R(a,i);const c=new S,m=t=>{const n=t;for(const o of Object.keys(i.tables))n[o]=y(t,o);return t};let u=!1;const w=t=>{const n=T({auth:{identity:t.claims,userId:t.userId},enforceRls:r.enforceRls??!0,headroom:c,inTransaction:()=>u,schema:i,sql:a}),o=n[g]??n;return{database:m(n),rawDatabase:m(o)}},l=t=>{a.exec.call(a,t)};let f=Promise.resolve();return{createWriters:w,resetHeadroom:()=>{c.reset()},runInTransaction:t=>{const n=async()=>{const b=p({scheduler:s});c.reset(),l("BEGIN"),u=!0;try{const d=await t();return l("COMMIT"),u=!1,await b(!0),d}catch(d){u=!1;try{l("ROLLBACK")}catch{}throw await b(!1),d}},o=f.then(n);return f=o.then(()=>{},()=>{}),o}}},v=e=>{if(typeof e!="object"||e===null)return;const{kind:r}=e;if(r==="query"||r==="mutation"||r==="action")return r},A=(e,r,s,a,i)=>{const c=v(r);if(c!==e)throw new I("INTERNAL",`expected a registered ${e}, received a ${c??"non-function"} reference`);if(!i&&r.visibility==="internal")throw new I("INTERNAL",`This ${e} is an internal function — it is unreachable from the external RPC boundary in production. Call it through ctx.run${e.charAt(0).toUpperCase()}${e.slice(1)} from another function instead.`);return Promise.resolve(r.handler(s,a??{}))};export{x as createInProcessRuntime,v as registeredFunctionKind,k as resolveInProcessIdentity,A as runRegisteredFunction};