@lunora/db 1.0.0-alpha.35 → 1.0.0-alpha.37

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.
@@ -1 +1 @@
1
- import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-BCgkk3A6.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
1
+ import{createCheckpointRegistry as t,lunoraCollectionOptions as r}from"../packem_shared/CHECKPOINT_FALLBACK_MS-6GmTGWsH.mjs";import{defineCollections as n}from"../packem_shared/defineCollections-4CPSF9OJ.mjs";export{t as createCheckpointRegistry,n as defineCollections,r as lunoraCollectionOptions};
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import{applyPlanToCollections as r,applyPlanToDb as i}from"./packem_shared/applyPlanToCollections-C29s6OoX.mjs";import{CHECKPOINT_FALLBACK_MS as a,createCheckpointRegistry as p,getShardCheckpoints as c,lunoraCollectionOptions as l,releaseShardCheckpoints as s,shardCheckpointStats as C}from"./packem_shared/CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{defineCollections as T}from"./packem_shared/defineCollections-BCgkk3A6.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as u,bindMutators as x,defineMutator as A,initMutators as M}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-DDE7m9p9.mjs";import{OUTBOX_MUTATION_FN_NAME as E,createExecutorOutboxSink as _,createOptimisticOnlineDetector as h,makeDiffEmit as N,runOutboxMutation as S,toMap as d}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,u as DIRECT_TRANSACTION_METADATA_KEY,E as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,x as bindMutators,p as createCheckpointRegistry,_ as createExecutorOutboxSink,h as createOptimisticOnlineDetector,T as defineCollections,A as defineMutator,c as getShardCheckpoints,M as initMutators,l as lunoraCollectionOptions,N as makeDiffEmit,s as releaseShardCheckpoints,S as runOutboxMutation,C as shardCheckpointStats,d as toMap};
1
+ import{applyPlanToCollections as r,applyPlanToDb as i}from"./packem_shared/applyPlanToCollections-C29s6OoX.mjs";import{CHECKPOINT_FALLBACK_MS as a,createCheckpointRegistry as p,getShardCheckpoints as c,lunoraCollectionOptions as l,releaseShardCheckpoints as s,shardCheckpointStats as C}from"./packem_shared/CHECKPOINT_FALLBACK_MS-6GmTGWsH.mjs";import{defineCollections as T}from"./packem_shared/defineCollections-4CPSF9OJ.mjs";import{DIRECT_TRANSACTION_METADATA_KEY as u,bindMutators as x,defineMutator as A,initMutators as M}from"./packem_shared/DIRECT_TRANSACTION_METADATA_KEY-D1XuUJtq.mjs";import{OUTBOX_MUTATION_FN_NAME as E,createExecutorOutboxSink as _,createOptimisticOnlineDetector as h,makeDiffEmit as N,runOutboxMutation as S,toMap as d}from"./packem_shared/OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const t="0.0.0";export{a as CHECKPOINT_FALLBACK_MS,u as DIRECT_TRANSACTION_METADATA_KEY,E as OUTBOX_MUTATION_FN_NAME,t as VERSION,r as applyPlanToCollections,i as applyPlanToDb,x as bindMutators,p as createCheckpointRegistry,_ as createExecutorOutboxSink,h as createOptimisticOnlineDetector,T as defineCollections,A as defineMutator,c as getShardCheckpoints,M as initMutators,l as lunoraCollectionOptions,N as makeDiffEmit,s as releaseShardCheckpoints,S as runOutboxMutation,C as shardCheckpointStats,d as toMap};
@@ -1 +1 @@
1
- import{bindMutators as r,defineMutator as i,initMutators as a}from"../packem_shared/DIRECT_TRANSACTION_METADATA_KEY-DDE7m9p9.mjs";export{r as bindMutators,i as defineMutator,a as initMutators};
1
+ import{bindMutators as r,defineMutator as i,initMutators as a}from"../packem_shared/DIRECT_TRANSACTION_METADATA_KEY-D1XuUJtq.mjs";export{r as bindMutators,i as defineMutator,a as initMutators};
@@ -0,0 +1 @@
1
+ import{LunoraError as y}from"@lunora/errors";import{BTreeIndex as b}from"@tanstack/db";import{toMap as C,makeDiffEmit as T}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const I=()=>{let e=Number.NEGATIVE_INFINITY;const r=[];return{advance:o=>{if(!(o<=e)){e=o;for(let a=r.length-1;a>=0;a-=1){const i=r[a];i&&i.threshold<=e&&(i.resolve(),r.splice(a,1))}}},await:o=>o<=e?Promise.resolve():new Promise(a=>{r.push({resolve:a,threshold:o})}),passed:o=>o<=e,waiting:()=>r.length}},M=3e3;let g=!1;const N=e=>{g||(g=!0,console.warn(`[@lunora/db] released an optimistic overlay via the ${String(e.waitedMs)}ms checkpoint fallback: the server confirmed ${e.kind} ${String(e.watermark)} but no sync frame ever echoed it. A dropped shape poke or \`settled\` frame is the usual cause — inspect the subscription rather than raising \`fallbackMs\`. (Reported once per process.)`))},S=(e={})=>{const r=e.fallbackMs??M,o=e.onFallback??N,a=I(),i=I();let d=0;const c=new Set,l=(t,s)=>{for(const n of c)n.kind===t&&n.threshold<=s&&(clearTimeout(n.handle),c.delete(n))},p=(t,s)=>{if(r<=0)return;const n=t==="checkpoint"?a:i;if(n.passed(s))return;for(const k of c)if(k.kind===t&&k.threshold>=s)return;const u={handle:setTimeout(()=>{if(c.delete(u),!n.passed(s)){d+=1,n.advance(s);try{o({kind:t,waitedMs:r,watermark:s})}catch{}}},r),kind:t,threshold:s};c.add(u)};return{acknowledge:({checkpoint:t,mutationId:s})=>{t!==void 0&&p("checkpoint",t),s!==void 0&&p("mutationId",s)},awaitCheckpoint:t=>a.await(t),awaitMutationId:t=>i.await(t),dispose:()=>{for(const t of c)clearTimeout(t.handle);c.clear()},resolve:({checkpoint:t,mutationId:s})=>{t!==void 0&&(a.advance(t),l("checkpoint",t)),s!==void 0&&(i.advance(s),l("mutationId",s))},stats:()=>({fallbacks:d,pendingCheckpointWaiters:a.waiting(),pendingMutationWaiters:i.waiting()})}},m=new WeakMap,E=(e,r,o)=>{let a=m.get(e);a||(a=new Map,m.set(e,a));const i=r??"",d=a.get(i);if(d)return d;const c=S(o);return a.set(i,c),c},w=new WeakSet,K=e=>{w.add(e)},x=e=>w.has(e),F=e=>{const r=m.get(e);if(r){for(const o of r.values())o.resolve({checkpoint:Number.POSITIVE_INFINITY,mutationId:Number.POSITIVE_INFINITY}),o.dispose();m.delete(e)}},O=e=>{const r=m.get(e),o={};if(r)for(const[a,i]of r)o[a]=i.stats();return o},P=e=>{if(e.list===void 0==(e.shape===void 0))throw new y("INTERNAL","lunoraCollectionOptions: pass exactly one of `list` or `shape`");const r=e.getKey??(n=>n._id),o=e.checkpoints??E(e.client,e.shape?.shardKey??e.shardKey);K(o);const a=new Map;let i,d,c,l,p;const t=(n,u)=>{const k=h=>{i?.(C(h,r)),u?.(),e.shape===void 0&&(o.resolve({mutationId:p??e.client.confirmedMutationWatermark(e.shardKey)}),p=void 0)},f=h=>c?.(h),v=h=>{h.mutationId!==void 0&&(p=h.mutationId),o.resolve(h)};return e.shape!==void 0?e.client.subscribeShape({args:n,name:e.shape.name},k,{onCheckpoint:v,onError:f,shardKey:e.shape.shardKey}):e.client.subscribe(e.list,n,k,{onCheckpoint:v,onError:f,shardKey:e.shardKey})},s={autoIndex:"eager",defaultIndexType:b,getKey:r,id:e.id??e.list?.__lunoraRef??`shape:${e.shape?.name??""}`,...e.load==="eager"?{startSync:!0}:{},sync:{sync:n=>(i=T(a,n),c=u=>{n.markReady(),e.onError?.(u)},e.scopeBy===void 0?d=t(e.shape?.args??{},()=>{n.markReady()}):(n.markReady(),l!==void 0&&(d=t(l,void 0))),()=>{i=void 0,c=void 0,d?.(),d=void 0,a.clear()})}};return{checkpoints:o,config:s,scope:n=>{e.scopeBy!==void 0&&(l=n,d?.(),d=void 0,i?.(new Map),n!==void 0&&i!==void 0&&(d=t(n,void 0)))}}};export{M as CHECKPOINT_FALLBACK_MS,S as createCheckpointRegistry,E as getShardCheckpoints,x as hasCheckpointsAttached,P as lunoraCollectionOptions,K as markCheckpointsAttached,F as releaseShardCheckpoints,O as shardCheckpointStats};
@@ -0,0 +1 @@
1
+ import{LunoraError as d}from"@lunora/errors";import{createTransaction as w}from"@tanstack/db";import{getShardCheckpoints as M,hasCheckpointsAttached as g}from"./CHECKPOINT_FALLBACK_MS-6GmTGWsH.mjs";import{runOutboxMutation as _}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const v="__tanstack_db_direct",k="defineMutator: `serverRef` must be a generated mutator reference (api.mutators.*) or a 'namespace:fn' string",T=r=>{const t=typeof r=="string"?r:r?.__lunoraRef;if(typeof t!="string"||t.length===0)throw new d("INTERNAL",k);return t},A=r=>({__lunoraClientMutator:!0,apply:r.apply,serverRef:T(r.serverRef)}),E=(r,t,l)=>{let i=0;const h=()=>(i=Math.max(i,r.confirmedMutationWatermark(t.shardKey))+1,i);let u=Promise.resolve();const p=(o,a)=>{const s=u.then(async()=>{for(let c=0;;c+=1){const n=h();try{const{applied:e}=await r.callMutator(o,a,{clientSeq:n,shardKey:t.shardKey});if(e)return n}catch(e){throw i=r.confirmedMutationWatermark(t.shardKey),e}if(c>=32)throw new d("INTERNAL",`lunora: custom mutator "${o}" could not claim a fresh client sequence after ${String(32)} attempts`)}});return u=s.then(()=>{},()=>{}),s},y=()=>{if(t.checkpoints===!1)return;if(t.checkpoints)return t.checkpoints;const o=M(r,t.shardKey);return g(o)?o:void 0},R=(o,a,s,c)=>{const{onWriteRejected:n}=t;n&&o.isPersisted.promise.catch(e=>{const m=e instanceof Error?e:new Error(String(e));try{n({args:c,code:m.code,error:m,mutator:a,serverRef:s})}catch{}})},f={};for(const[o,a]of Object.entries(l))f[o]=s=>{const c=w({autoCommit:!0,metadata:{[v]:!0,serverRef:a.serverRef},mutationFn:async()=>{let n=0;await _(async()=>{n=await p(a.serverRef,s)});const e=y();e&&(e.acknowledge({mutationId:n}),await e.awaitMutationId(n))}});return c.mutate(()=>{a.apply({collections:t.collections},s)}),R(c,o,a.serverRef,s),c};return f},b=()=>({bindMutators:E,defineMutator:A});export{v as DIRECT_TRANSACTION_METADATA_KEY,E as bindMutators,A as defineMutator,b as initMutators};
@@ -1 +1 @@
1
- import{createCollection as I,safeRandomUUID as R}from"@tanstack/db";import{startOfflineExecutor as U,NonRetriableError as w}from"@tanstack/offline-transactions";import{lunoraCollectionOptions as _}from"./CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{createOutboxCarrier as b,createOptimisticOnlineDetector as E,OUTBOX_MUTATION_FN_NAME as C,registerOutboxCarrier as M,runOutboxMutation as N}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const S=(a,K,e={})=>{const c={},p={},l={},y=Object.entries(K);for(const[t,o]of y){const n=o.insert,{config:d,scope:m}=_({client:a,getKey:o.getKey,id:t,list:o.list,...o.load===void 0?{}:{load:o.load},onError:o.onError,scopeBy:o.scopeBy,shardKey:o.shardKey});c[t]=I(d),o.scopeBy!==void 0&&(p[t]=m),n&&(l[t]=async({idempotencyKey:r,transaction:i})=>{for(const[f,x]of i.mutations.entries()){const O=x.modified,F=`${r}:${String(f)}`;try{await N(()=>a.mutation(n.mutation,n.toArgs(O),{mutationId:F}))}catch(u){if(u instanceof w&&e.onWriteRejected)try{e.onWriteRejected({code:u.code,collection:t,error:u,row:O})}catch{}throw u}}})}l[C]=async({transaction:t})=>{const o=t.metadata;if(o){if(o.identity!==a.currentIdentity())throw new w("outbox write dropped: identity changed since it was queued");await N(()=>a.mutation({__lunoraRef:o.functionPath},o.args,{mutationId:o.idempotencyKey,shardKey:o.shardKey}))}};const g=b(),s=U({collections:{...c,[C]:g},mutationFns:l,onlineDetector:E(),...e.onLeadershipChange?{onLeadershipChange:e.onLeadershipChange}:{},...e.onStorageFailure?{onStorageFailure:e.onStorageFailure}:{},onUnknownMutationFn:(t,o)=>{try{e.onWriteRejected?.({code:"UNKNOWN_MUTATION_FN",collection:t,error:new Error(`offline write dropped: mutation "${t}" no longer exists (removed or renamed in a deploy?)`),row:o.mutations[0]?.modified})}catch{}}});M(s,g);const h={};for(const[t,o]of y){const n=o.insert,d=c[t];if(!n||!d)continue;const m=s.createOfflineAction({mutationFnName:t,onMutate:({id:r,input:i})=>{d.insert(n.optimistic(i,r))}});h[t]=r=>{const i=R(),f=m({id:i,input:r});return{id:i,transaction:f}}}return{actions:h,collections:c,executor:s,pendingCount:()=>s.getPendingCount(),scope:p}};export{S as defineCollections};
1
+ import{createCollection as I,safeRandomUUID as R}from"@tanstack/db";import{startOfflineExecutor as U,NonRetriableError as w}from"@tanstack/offline-transactions";import{lunoraCollectionOptions as _}from"./CHECKPOINT_FALLBACK_MS-6GmTGWsH.mjs";import{createOutboxCarrier as b,createOptimisticOnlineDetector as E,OUTBOX_MUTATION_FN_NAME as C,registerOutboxCarrier as M,runOutboxMutation as N}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const S=(a,K,e={})=>{const c={},p={},l={},y=Object.entries(K);for(const[t,o]of y){const n=o.insert,{config:d,scope:m}=_({client:a,getKey:o.getKey,id:t,list:o.list,...o.load===void 0?{}:{load:o.load},onError:o.onError,scopeBy:o.scopeBy,shardKey:o.shardKey});c[t]=I(d),o.scopeBy!==void 0&&(p[t]=m),n&&(l[t]=async({idempotencyKey:r,transaction:i})=>{for(const[f,x]of i.mutations.entries()){const O=x.modified,F=`${r}:${String(f)}`;try{await N(()=>a.mutation(n.mutation,n.toArgs(O),{mutationId:F}))}catch(u){if(u instanceof w&&e.onWriteRejected)try{e.onWriteRejected({code:u.code,collection:t,error:u,row:O})}catch{}throw u}}})}l[C]=async({transaction:t})=>{const o=t.metadata;if(o){if(o.identity!==a.currentIdentity())throw new w("outbox write dropped: identity changed since it was queued");await N(()=>a.mutation({__lunoraRef:o.functionPath},o.args,{mutationId:o.idempotencyKey,shardKey:o.shardKey}))}};const g=b(),s=U({collections:{...c,[C]:g},mutationFns:l,onlineDetector:E(),...e.onLeadershipChange?{onLeadershipChange:e.onLeadershipChange}:{},...e.onStorageFailure?{onStorageFailure:e.onStorageFailure}:{},onUnknownMutationFn:(t,o)=>{try{e.onWriteRejected?.({code:"UNKNOWN_MUTATION_FN",collection:t,error:new Error(`offline write dropped: mutation "${t}" no longer exists (removed or renamed in a deploy?)`),row:o.mutations[0]?.modified})}catch{}}});M(s,g);const h={};for(const[t,o]of y){const n=o.insert,d=c[t];if(!n||!d)continue;const m=s.createOfflineAction({mutationFnName:t,onMutate:({id:r,input:i})=>{d.insert(n.optimistic(i,r))}});h[t]=r=>{const i=R(),f=m({id:i,input:r});return{id:i,transaction:f}}}return{actions:h,collections:c,executor:s,pendingCount:()=>s.getPendingCount(),scope:p}};export{S as defineCollections};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/db",
3
- "version": "1.0.0-alpha.35",
3
+ "version": "1.0.0-alpha.37",
4
4
  "description": "TanStack DB binding: typed, live-synced collections and a durable offline outbox over the Lunora client",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -54,8 +54,8 @@
54
54
  "access": "public"
55
55
  },
56
56
  "dependencies": {
57
- "@lunora/client": "1.0.0-alpha.34",
58
- "@lunora/errors": "1.0.0-alpha.10"
57
+ "@lunora/client": "1.0.0-alpha.36",
58
+ "@lunora/errors": "1.0.0-alpha.12"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "@tanstack/db": "^0.6.0",
@@ -1 +0,0 @@
1
- import{LunoraError as y}from"@lunora/errors";import{BTreeIndex as w}from"@tanstack/db";import{toMap as b,makeDiffEmit as C}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const v=()=>{let e=Number.NEGATIVE_INFINITY;const s=[];return{advance:o=>{if(!(o<=e)){e=o;for(let a=s.length-1;a>=0;a-=1){const r=s[a];r&&r.threshold<=e&&(r.resolve(),s.splice(a,1))}}},await:o=>o<=e?Promise.resolve():new Promise(a=>{s.push({resolve:a,threshold:o})}),passed:o=>o<=e,waiting:()=>s.length}},M=3e3;let I=!1;const N=e=>{I||(I=!0,console.warn(`[@lunora/db] released an optimistic overlay via the ${String(e.waitedMs)}ms checkpoint fallback: the server confirmed ${e.kind} ${String(e.watermark)} but no sync frame ever echoed it. A dropped shape poke or \`settled\` frame is the usual cause — inspect the subscription rather than raising \`fallbackMs\`. (Reported once per process.)`))},T=(e={})=>{const s=e.fallbackMs??M,o=e.onFallback??N,a=v(),r=v();let c=0;const i=new Set,l=(n,t)=>{for(const d of i)d.kind===n&&d.threshold<=t&&(clearTimeout(d.handle),i.delete(d))},h=(n,t)=>{if(s<=0)return;const d=n==="checkpoint"?a:r;if(d.passed(t))return;for(const u of i)if(u.kind===n&&u.threshold>=t)return;const p={handle:setTimeout(()=>{if(i.delete(p),!d.passed(t)){c+=1,d.advance(t);try{o({kind:n,waitedMs:s,watermark:t})}catch{}}},s),kind:n,threshold:t};i.add(p)};return{acknowledge:({checkpoint:n,mutationId:t})=>{n!==void 0&&h("checkpoint",n),t!==void 0&&h("mutationId",t)},awaitCheckpoint:n=>a.await(n),awaitMutationId:n=>r.await(n),dispose:()=>{for(const n of i)clearTimeout(n.handle);i.clear()},resolve:({checkpoint:n,mutationId:t})=>{n!==void 0&&(a.advance(n),l("checkpoint",n)),t!==void 0&&(r.advance(t),l("mutationId",t))},stats:()=>({fallbacks:c,pendingCheckpointWaiters:a.waiting(),pendingMutationWaiters:r.waiting()})}},f=new WeakMap,S=(e,s,o)=>{let a=f.get(e);a||(a=new Map,f.set(e,a));const r=s??"",c=a.get(r);if(c)return c;const i=T(o);return a.set(r,i),i},g=new WeakSet,E=e=>{g.add(e)},R=e=>g.has(e),x=e=>{const s=f.get(e);if(s){for(const o of s.values())o.resolve({checkpoint:Number.POSITIVE_INFINITY,mutationId:Number.POSITIVE_INFINITY}),o.dispose();f.delete(e)}},F=e=>{const s=f.get(e),o={};if(s)for(const[a,r]of s)o[a]=r.stats();return o},O=e=>{if(e.list===void 0==(e.shape===void 0))throw new y("INTERNAL","lunoraCollectionOptions: pass exactly one of `list` or `shape`");const s=e.getKey??(t=>t._id),o=e.checkpoints??S(e.client,e.shape?.shardKey??e.shardKey);E(o);const a=new Map;let r,c,i,l;const h=(t,d)=>{const p=k=>{r?.(b(k,s)),d?.(),e.shape===void 0&&o.resolve({mutationId:e.client.confirmedMutationWatermark(e.shardKey)})},u=k=>i?.(k),m=k=>{o.resolve(k)};return e.shape!==void 0?e.client.subscribeShape({args:t,name:e.shape.name},p,{onCheckpoint:m,onError:u,shardKey:e.shape.shardKey}):e.client.subscribe(e.list,t,p,{onCheckpoint:m,onError:u,shardKey:e.shardKey})},n={autoIndex:"eager",defaultIndexType:w,getKey:s,id:e.id??e.list?.__lunoraRef??`shape:${e.shape?.name??""}`,...e.load==="eager"?{startSync:!0}:{},sync:{sync:t=>(r=C(a,t),i=d=>{t.markReady(),e.onError?.(d)},e.scopeBy===void 0?c=h(e.shape?.args??{},()=>{t.markReady()}):(t.markReady(),l!==void 0&&(c=h(l,void 0))),()=>{r=void 0,i=void 0,c?.(),c=void 0,a.clear()})}};return{checkpoints:o,config:n,scope:t=>{e.scopeBy!==void 0&&(l=t,c?.(),c=void 0,r?.(new Map),t!==void 0&&r!==void 0&&(c=h(t,void 0)))}}};export{M as CHECKPOINT_FALLBACK_MS,T as createCheckpointRegistry,S as getShardCheckpoints,R as hasCheckpointsAttached,O as lunoraCollectionOptions,E as markCheckpointsAttached,x as releaseShardCheckpoints,F as shardCheckpointStats};
@@ -1 +0,0 @@
1
- import{LunoraError as l}from"@lunora/errors";import{createTransaction as g}from"@tanstack/db";import{getShardCheckpoints as w,hasCheckpointsAttached as M}from"./CHECKPOINT_FALLBACK_MS-vBJLOBjX.mjs";import{runOutboxMutation as _}from"./OUTBOX_MUTATION_FN_NAME-CebgkYw2.mjs";const v="__tanstack_db_direct",T="defineMutator: `serverRef` must be a generated mutator reference (api.mutators.*) or a 'namespace:fn' string",k=e=>{const t=typeof e=="string"?e:e?.__lunoraRef;if(typeof t!="string"||t.length===0)throw new l("INTERNAL",T);return t},A=e=>({__lunoraClientMutator:!0,apply:e.apply,serverRef:k(e.serverRef)}),E=(e,t,p)=>{let i=0;const d=()=>(i=Math.max(i,e.confirmedMutationWatermark(t.shardKey))+1,i);let u=Promise.resolve();const h=(r,a)=>{const s=u.then(async()=>{for(let c=0;;c+=1){const o=d(),{applied:n}=await e.callMutator(r,a,{clientSeq:o,shardKey:t.shardKey});if(n)return o;if(c>=32)throw new l("INTERNAL",`lunora: custom mutator "${r}" could not claim a fresh client sequence after ${String(32)} attempts`)}});return u=s.then(()=>{},()=>{}),s},R=()=>{if(t.checkpoints===!1)return;if(t.checkpoints)return t.checkpoints;const r=w(e,t.shardKey);return M(r)?r:void 0},y=(r,a,s,c)=>{const{onWriteRejected:o}=t;o&&r.isPersisted.promise.catch(n=>{const m=n instanceof Error?n:new Error(String(n));try{o({args:c,code:m.code,error:m,mutator:a,serverRef:s})}catch{}})},f={};for(const[r,a]of Object.entries(p))f[r]=s=>{const c=g({autoCommit:!0,metadata:{[v]:!0,serverRef:a.serverRef},mutationFn:async()=>{let o=0;await _(async()=>{o=await h(a.serverRef,s)});const n=R();n&&(n.acknowledge({mutationId:o}),await n.awaitMutationId(o))}});return c.mutate(()=>{a.apply({collections:t.collections},s)}),y(c,r,a.serverRef,s),c};return f},K=()=>({bindMutators:E,defineMutator:A});export{v as DIRECT_TRANSACTION_METADATA_KEY,E as bindMutators,A as defineMutator,K as initMutators};