@lunora/client 1.0.0-alpha.72 → 1.0.0-alpha.74
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/dist/auth/index.d.mts +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.mjs +1 -1
- package/dist/packem_shared/LunoraClient-BoqhqR-g.mjs +1 -0
- package/dist/packem_shared/{SubscriptionRegistry-BQPU90ST.mjs → SubscriptionRegistry-BQTQZYoI.mjs} +1 -1
- package/dist/packem_shared/applyDelta-Bz-B1XB9.mjs +1 -0
- package/dist/packem_shared/{createAsyncStorageQueryCache-MoM77Nwg.mjs → createAsyncStorageQueryCache-VgJNCeEn.mjs} +1 -1
- package/dist/packem_shared/createLocalStore-CJ0VRKPe.mjs +1 -0
- package/dist/packem_shared/{createServerClient-rFEbvMRr.mjs → createServerClient-CepVN0C6.mjs} +1 -1
- package/dist/packem_shared/{createSnapshotPrecondition-C4bSgHkg.mjs → createSnapshotPrecondition-B7kQcZUF.mjs} +1 -1
- package/dist/packem_shared/{delta-merge-BPM4sFID.mjs → delta-merge-Ds6tHkFX.mjs} +1 -1
- package/dist/packem_shared/{local-store-CNgeGW02.mjs → local-store-irMi9H2G.mjs} +1 -1
- package/dist/packem_shared/{lunora-client.d-6hU156nD.d.mts → lunora-client.d-Cb_0IT8r.d.mts} +21 -0
- package/dist/packem_shared/{lunora-client.d-B0t5v2n9.d.ts → lunora-client.d-CyDr665D.d.ts} +21 -0
- package/dist/packem_shared/{preload.d-Dl-ja0VQ.d.ts → preload.d-8LuiLYlj.d.ts} +1 -1
- package/dist/packem_shared/{preload.d-M7qP6TwL.d.mts → preload.d-BrV8gNAa.d.mts} +1 -1
- package/dist/packem_shared/wire-codec-BOMWQpoF.mjs +1 -0
- package/dist/packem_shared/{wire-key-C9UKT-C3.mjs → wire-key-CmT1XyQI.mjs} +1 -1
- package/dist/query/index.d.mts +2 -2
- package/dist/query/index.d.ts +2 -2
- package/dist/service.mjs +1 -1
- package/dist/ssr/index.d.mts +2 -2
- package/dist/ssr/index.d.ts +2 -2
- package/dist/ssr/index.mjs +1 -1
- package/package.json +1 -1
- package/dist/packem_shared/LunoraClient-U7lWMpjV.mjs +0 -1
- package/dist/packem_shared/applyDelta-BiegnbZR.mjs +0 -1
- package/dist/packem_shared/createLocalStore-v6oiATqG.mjs +0 -1
- package/dist/packem_shared/wire-codec-BfIZrTJe.mjs +0 -1
package/dist/auth/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { U as User, L as LunoraClient } from "../packem_shared/lunora-client.d-
|
|
1
|
+
import { U as User, L as LunoraClient } from "../packem_shared/lunora-client.d-Cb_0IT8r.mjs";
|
|
2
2
|
import '@lunora/errors';
|
|
3
3
|
import "../packem_shared/function-reference.d-Br_hsKje.mjs";
|
|
4
4
|
import '@lunora/runtime';
|
package/dist/auth/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { U as User, L as LunoraClient } from "../packem_shared/lunora-client.d-
|
|
1
|
+
import { U as User, L as LunoraClient } from "../packem_shared/lunora-client.d-CyDr665D.js";
|
|
2
2
|
import '@lunora/errors';
|
|
3
3
|
import "../packem_shared/function-reference.d-Br_hsKje.js";
|
|
4
4
|
import '@lunora/runtime';
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { c as PersistenceAdapter, Q as QueryCacheAdapter, B as BookmarkStorage, C as ConnectionStatus, S as SubscriptionError, H as HttpStreamRef, d as HttpStreamArgsOf, e as HttpStreamChunkOf, f as StreamIterable, O as OfflineQueueOptions, R as ReconnectOptions, L as LunoraClient } from "./packem_shared/lunora-client.d-
|
|
2
|
-
export { type A as ActionCallOptions, type g as BatchSlot, type h as CachedQuery, type i as ClientDebugShard, type j as ClientDebugSnapshot, type k as ClientDebugSubscription, type l as ClientMessage, type m as ClientQueryRef, type n as ClientShapeSubscribeMessage, type o as ClientShapeUnsubscribeMessage, D as DEFAULT_MAX_BUFFER, type F as FunctionArgumentDescriptor, type p as FunctionDescriptor, type G as GlobalFacetResult, type q as GlobalFacetValue, type r as GlobalFilterClause, type s as GlobalTableInfo, type t as GlobalTablePage, type u as HttpStreamCallArgs, type v as LunoraClientError, type w as LunoraClientOptions, type M as MutationCallOptions, type x as MutationSettledEvent, type y as OptimisticLocalStore, type z as OptimisticUpdate, type E as OutboxMutation, type I as OutboxSink, type J as PersistedMutation, type P as Preloaded, type K as RowOp, type N as RpcEnvelope, type T as RpcResponseBody, type V as ScheduleRecord, type W as ScheduleRetryPolicy, type X as SchedulerPoolStatus, type Y as SchedulerStatus, type Z as ServerMessage, type _ as ServerPokeEndMessage, type $ as ServerPokePartMessage, type a0 as ServerPokeStartMessage, type a1 as ShardTrafficEntry, type a2 as ShardTrafficResult, type a3 as StorageListPage, type a4 as StorageObject, type a5 as StoredQuery, type a6 as StreamHandle, type a7 as SubscriptionCallback, type b as SubscriptionErrorCallback, a8 as SubscriptionRegistry, type a9 as SubscriptionState, type aa as SyncWatermark, type a as Unsubscribe, type U as User, type ab as WorkflowInstanceAction, type ac as WorkflowInstanceDetail, type ad as WorkflowInstancePage, type ae as WorkflowInstanceStatus, type af as WorkflowInstanceSummary, type ag as WorkflowStepDetail, type ah as WsTokenProvider, ai as createClientQuery, aj as createLocalStore, ak as createStream } from "./packem_shared/lunora-client.d-
|
|
1
|
+
import { c as PersistenceAdapter, Q as QueryCacheAdapter, B as BookmarkStorage, C as ConnectionStatus, S as SubscriptionError, H as HttpStreamRef, d as HttpStreamArgsOf, e as HttpStreamChunkOf, f as StreamIterable, O as OfflineQueueOptions, R as ReconnectOptions, L as LunoraClient } from "./packem_shared/lunora-client.d-Cb_0IT8r.mjs";
|
|
2
|
+
export { type A as ActionCallOptions, type g as BatchSlot, type h as CachedQuery, type i as ClientDebugShard, type j as ClientDebugSnapshot, type k as ClientDebugSubscription, type l as ClientMessage, type m as ClientQueryRef, type n as ClientShapeSubscribeMessage, type o as ClientShapeUnsubscribeMessage, D as DEFAULT_MAX_BUFFER, type F as FunctionArgumentDescriptor, type p as FunctionDescriptor, type G as GlobalFacetResult, type q as GlobalFacetValue, type r as GlobalFilterClause, type s as GlobalTableInfo, type t as GlobalTablePage, type u as HttpStreamCallArgs, type v as LunoraClientError, type w as LunoraClientOptions, type M as MutationCallOptions, type x as MutationSettledEvent, type y as OptimisticLocalStore, type z as OptimisticUpdate, type E as OutboxMutation, type I as OutboxSink, type J as PersistedMutation, type P as Preloaded, type K as RowOp, type N as RpcEnvelope, type T as RpcResponseBody, type V as ScheduleRecord, type W as ScheduleRetryPolicy, type X as SchedulerPoolStatus, type Y as SchedulerStatus, type Z as ServerMessage, type _ as ServerPokeEndMessage, type $ as ServerPokePartMessage, type a0 as ServerPokeStartMessage, type a1 as ShardTrafficEntry, type a2 as ShardTrafficResult, type a3 as StorageListPage, type a4 as StorageObject, type a5 as StoredQuery, type a6 as StreamHandle, type a7 as SubscriptionCallback, type b as SubscriptionErrorCallback, a8 as SubscriptionRegistry, type a9 as SubscriptionState, type aa as SyncWatermark, type a as Unsubscribe, type U as User, type ab as WorkflowInstanceAction, type ac as WorkflowInstanceDetail, type ad as WorkflowInstancePage, type ae as WorkflowInstanceStatus, type af as WorkflowInstanceSummary, type ag as WorkflowStepDetail, type ah as WsTokenProvider, ai as createClientQuery, aj as createLocalStore, ak as createStream } from "./packem_shared/lunora-client.d-Cb_0IT8r.mjs";
|
|
3
3
|
import { LunoraError, LunoraErrorCode } from '@lunora/errors';
|
|
4
4
|
export type { LunoraErrorCode } from '@lunora/errors';
|
|
5
|
-
export { p as preloadQuery, a as preloadedQueryResult } from "./packem_shared/preload.d-
|
|
5
|
+
export { p as preloadQuery, a as preloadedQueryResult } from "./packem_shared/preload.d-BrV8gNAa.mjs";
|
|
6
6
|
import { F as FunctionReference } from "./packem_shared/function-reference.d-Br_hsKje.mjs";
|
|
7
7
|
export type { A as ArgsOf, R as ReturnOf } from "./packem_shared/function-reference.d-Br_hsKje.mjs";
|
|
8
8
|
export type { AuthCapabilities, AuthConfigInfo, AuthImpersonation, AuthPage, AuthSession, AuthUser, AuthUserFieldSpec, CronJobInfo, KvKeyEntry, KvKeyListResult, KvNamespaceSummary, KvValueResult, PipelineLogColumnMap, PipelineLogCursor, PipelineLogField, PipelineLogPage, PipelineLogQuery, PipelineLogRow, VectorIndexSummary, VectorQueryMatch } from '@lunora/runtime';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { c as PersistenceAdapter, Q as QueryCacheAdapter, B as BookmarkStorage, C as ConnectionStatus, S as SubscriptionError, H as HttpStreamRef, d as HttpStreamArgsOf, e as HttpStreamChunkOf, f as StreamIterable, O as OfflineQueueOptions, R as ReconnectOptions, L as LunoraClient } from "./packem_shared/lunora-client.d-
|
|
2
|
-
export { type A as ActionCallOptions, type g as BatchSlot, type h as CachedQuery, type i as ClientDebugShard, type j as ClientDebugSnapshot, type k as ClientDebugSubscription, type l as ClientMessage, type m as ClientQueryRef, type n as ClientShapeSubscribeMessage, type o as ClientShapeUnsubscribeMessage, D as DEFAULT_MAX_BUFFER, type F as FunctionArgumentDescriptor, type p as FunctionDescriptor, type G as GlobalFacetResult, type q as GlobalFacetValue, type r as GlobalFilterClause, type s as GlobalTableInfo, type t as GlobalTablePage, type u as HttpStreamCallArgs, type v as LunoraClientError, type w as LunoraClientOptions, type M as MutationCallOptions, type x as MutationSettledEvent, type y as OptimisticLocalStore, type z as OptimisticUpdate, type E as OutboxMutation, type I as OutboxSink, type J as PersistedMutation, type P as Preloaded, type K as RowOp, type N as RpcEnvelope, type T as RpcResponseBody, type V as ScheduleRecord, type W as ScheduleRetryPolicy, type X as SchedulerPoolStatus, type Y as SchedulerStatus, type Z as ServerMessage, type _ as ServerPokeEndMessage, type $ as ServerPokePartMessage, type a0 as ServerPokeStartMessage, type a1 as ShardTrafficEntry, type a2 as ShardTrafficResult, type a3 as StorageListPage, type a4 as StorageObject, type a5 as StoredQuery, type a6 as StreamHandle, type a7 as SubscriptionCallback, type b as SubscriptionErrorCallback, a8 as SubscriptionRegistry, type a9 as SubscriptionState, type aa as SyncWatermark, type a as Unsubscribe, type U as User, type ab as WorkflowInstanceAction, type ac as WorkflowInstanceDetail, type ad as WorkflowInstancePage, type ae as WorkflowInstanceStatus, type af as WorkflowInstanceSummary, type ag as WorkflowStepDetail, type ah as WsTokenProvider, ai as createClientQuery, aj as createLocalStore, ak as createStream } from "./packem_shared/lunora-client.d-
|
|
1
|
+
import { c as PersistenceAdapter, Q as QueryCacheAdapter, B as BookmarkStorage, C as ConnectionStatus, S as SubscriptionError, H as HttpStreamRef, d as HttpStreamArgsOf, e as HttpStreamChunkOf, f as StreamIterable, O as OfflineQueueOptions, R as ReconnectOptions, L as LunoraClient } from "./packem_shared/lunora-client.d-CyDr665D.js";
|
|
2
|
+
export { type A as ActionCallOptions, type g as BatchSlot, type h as CachedQuery, type i as ClientDebugShard, type j as ClientDebugSnapshot, type k as ClientDebugSubscription, type l as ClientMessage, type m as ClientQueryRef, type n as ClientShapeSubscribeMessage, type o as ClientShapeUnsubscribeMessage, D as DEFAULT_MAX_BUFFER, type F as FunctionArgumentDescriptor, type p as FunctionDescriptor, type G as GlobalFacetResult, type q as GlobalFacetValue, type r as GlobalFilterClause, type s as GlobalTableInfo, type t as GlobalTablePage, type u as HttpStreamCallArgs, type v as LunoraClientError, type w as LunoraClientOptions, type M as MutationCallOptions, type x as MutationSettledEvent, type y as OptimisticLocalStore, type z as OptimisticUpdate, type E as OutboxMutation, type I as OutboxSink, type J as PersistedMutation, type P as Preloaded, type K as RowOp, type N as RpcEnvelope, type T as RpcResponseBody, type V as ScheduleRecord, type W as ScheduleRetryPolicy, type X as SchedulerPoolStatus, type Y as SchedulerStatus, type Z as ServerMessage, type _ as ServerPokeEndMessage, type $ as ServerPokePartMessage, type a0 as ServerPokeStartMessage, type a1 as ShardTrafficEntry, type a2 as ShardTrafficResult, type a3 as StorageListPage, type a4 as StorageObject, type a5 as StoredQuery, type a6 as StreamHandle, type a7 as SubscriptionCallback, type b as SubscriptionErrorCallback, a8 as SubscriptionRegistry, type a9 as SubscriptionState, type aa as SyncWatermark, type a as Unsubscribe, type U as User, type ab as WorkflowInstanceAction, type ac as WorkflowInstanceDetail, type ad as WorkflowInstancePage, type ae as WorkflowInstanceStatus, type af as WorkflowInstanceSummary, type ag as WorkflowStepDetail, type ah as WsTokenProvider, ai as createClientQuery, aj as createLocalStore, ak as createStream } from "./packem_shared/lunora-client.d-CyDr665D.js";
|
|
3
3
|
import { LunoraError, LunoraErrorCode } from '@lunora/errors';
|
|
4
4
|
export type { LunoraErrorCode } from '@lunora/errors';
|
|
5
|
-
export { p as preloadQuery, a as preloadedQueryResult } from "./packem_shared/preload.d-
|
|
5
|
+
export { p as preloadQuery, a as preloadedQueryResult } from "./packem_shared/preload.d-8LuiLYlj.js";
|
|
6
6
|
import { F as FunctionReference } from "./packem_shared/function-reference.d-Br_hsKje.js";
|
|
7
7
|
export type { A as ArgsOf, R as ReturnOf } from "./packem_shared/function-reference.d-Br_hsKje.js";
|
|
8
8
|
export type { AuthCapabilities, AuthConfigInfo, AuthImpersonation, AuthPage, AuthSession, AuthUser, AuthUserFieldSpec, CronJobInfo, KvKeyEntry, KvKeyListResult, KvNamespaceSummary, KvValueResult, PipelineLogColumnMap, PipelineLogCursor, PipelineLogField, PipelineLogPage, PipelineLogQuery, PipelineLogRow, VectorIndexSummary, VectorQueryMatch } from '@lunora/runtime';
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{anyApi as o}from"./packem_shared/anyApi-CBOws2ZA.mjs";import{RETIRE_AFTER_DURABLE_SEQ_ADVANCE as a,maxSeq as c,reconcileOptimistic as p}from"./packem_shared/RETIRE_AFTER_DURABLE_SEQ_ADVANCE-DoEHtuR8.mjs";import{createAsyncStoragePersistence as m}from"./packem_shared/createAsyncStoragePersistence-CeUW9NTj.mjs";import{createAsyncStorageQueryCache as f}from"./packem_shared/createAsyncStorageQueryCache-
|
|
1
|
+
import{anyApi as o}from"./packem_shared/anyApi-CBOws2ZA.mjs";import{RETIRE_AFTER_DURABLE_SEQ_ADVANCE as a,maxSeq as c,reconcileOptimistic as p}from"./packem_shared/RETIRE_AFTER_DURABLE_SEQ_ADVANCE-DoEHtuR8.mjs";import{createAsyncStoragePersistence as m}from"./packem_shared/createAsyncStoragePersistence-CeUW9NTj.mjs";import{createAsyncStorageQueryCache as f}from"./packem_shared/createAsyncStorageQueryCache-VgJNCeEn.mjs";import{default as x}from"./packem_shared/createInMemoryBookmarkStorage-BooZhW0n.mjs";import{createCallRunner as l}from"./packem_shared/createCallRunner-Bwcrfh76.mjs";import{createClientQuery as d}from"./packem_shared/createClientQuery-B8Nfj-7o.mjs";import{TabCoordinator as R}from"./packem_shared/TabCoordinator-CXD7vulJ.mjs";import{a as S,i as A}from"./packem_shared/delta-merge-Ds6tHkFX.mjs";import{CONFLICT_ERROR_CODE as Q,TransportError as _,getErrorCode as h,getRetryAfterMs as I,isConflictError as M,isForbiddenError as T,isRateLimitedError as g,isUnauthorizedError as F}from"./packem_shared/CONFLICT_ERROR_CODE-BOeeP_Wl.mjs";import{httpStream as O}from"./packem_shared/httpStream-x6q-x4yK.mjs";import{c as P}from"./packem_shared/local-store-irMi9H2G.mjs";import{LunoraClient as k}from"./packem_shared/LunoraClient-BoqhqR-g.mjs";import{createMutatorRunner as q}from"./packem_shared/createMutatorRunner-CuMRJpUx.mjs";import{O as v}from"./packem_shared/offline-queue-xntsWiKt.mjs";import{createInMemoryPersistence as z,createIndexedDbPersistence as K}from"./packem_shared/createInMemoryPersistence-Cz2W6XVj.mjs";import{preloadQuery as W,preloadedQueryResult as X}from"./packem_shared/preloadQuery-uFy24PCR.mjs";import{createInMemoryQueryCache as G,createIndexedDbQueryCache as H,queryCacheKey as J}from"./packem_shared/createInMemoryQueryCache-D2uNHLfM.mjs";import{createReconnect as Z}from"./packem_shared/createReconnect-CjTmjJDH.mjs";import{default as ee}from"./packem_shared/createSnapshotPrecondition-B7kQcZUF.mjs";import{DEFAULT_MAX_BUFFER as oe,createStream as te}from"./packem_shared/DEFAULT_MAX_BUFFER-D3QH2iaq.mjs";import{SubscriptionRegistry as ce}from"./packem_shared/SubscriptionRegistry-BQTQZYoI.mjs";import{ClientServiceWorker as ne}from"./packem_shared/ClientServiceWorker-DXnBXeWK.mjs";import{createReply as ie,sendToSw as fe}from"./packem_shared/createReply-CwFABEJs.mjs";export{Q as CONFLICT_ERROR_CODE,ne as ClientServiceWorker,oe as DEFAULT_MAX_BUFFER,k as LunoraClient,v as OfflineQueue,a as RETIRE_AFTER_DURABLE_SEQ_ADVANCE,ce as SubscriptionRegistry,R as TabCoordinator,_ as TransportError,o as anyApi,S as applyDelta,m as createAsyncStoragePersistence,f as createAsyncStorageQueryCache,l as createCallRunner,d as createClientQuery,x as createInMemoryBookmarkStorage,z as createInMemoryPersistence,G as createInMemoryQueryCache,K as createIndexedDbPersistence,H as createIndexedDbQueryCache,P as createLocalStore,q as createMutatorRunner,Z as createReconnect,ie as createReply,ee as createSnapshotPrecondition,te as createStream,h as getErrorCode,I as getRetryAfterMs,O as httpStream,M as isConflictError,T as isForbiddenError,A as isMutationDelta,g as isRateLimitedError,F as isUnauthorizedError,c as maxSeq,W as preloadQuery,X as preloadedQueryResult,J as queryCacheKey,p as reconcileOptimistic,fe as sendToSw};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{LunoraError as m}from"@lunora/errors";import{i as ie,a as ne,P as ae}from"./delta-merge-Ds6tHkFX.mjs";import{e as W,d as T}from"./wire-codec-BOMWQpoF.mjs";import{s as q}from"./wire-key-CmT1XyQI.mjs";import oe from"./createInMemoryBookmarkStorage-BooZhW0n.mjs";import{ClientQueryStore as ce}from"./createClientQuery-B8Nfj-7o.mjs";import{TabCoordinator as he}from"./TabCoordinator-CXD7vulJ.mjs";import{TransportError as A,getRetryAfterMs as X}from"./CONFLICT_ERROR_CODE-BOeeP_Wl.mjs";import{httpStream as de}from"./httpStream-x6q-x4yK.mjs";import{d as U,n as O,f as R,a as ue,c as le}from"./local-store-irMi9H2G.mjs";import{O as fe,n as $,i as me,r as pe}from"./offline-queue-xntsWiKt.mjs";import{resolvePersistenceAdapter as ye}from"./createInMemoryPersistence-Cz2W6XVj.mjs";import{resolveQueryCacheAdapter as Se,queryCacheKey as P}from"./createInMemoryQueryCache-D2uNHLfM.mjs";import{createReconnect as Q}from"./createReconnect-CjTmjJDH.mjs";import be from"./createSnapshotPrecondition-B7kQcZUF.mjs";import{createStream as Te}from"./DEFAULT_MAX_BUFFER-D3QH2iaq.mjs";import{SubscriptionRegistry as k}from"./SubscriptionRegistry-BQTQZYoI.mjs";const x=500,ve=async o=>{const e=[];let t;for(;;){const s=await o(t);if(e.push(...Array.isArray(s.records)?s.records:[]),s.truncated!==!0||typeof s.cursor!="string"||s.cursor.length===0)return e;if(s.cursor===t)throw new Error("collectPages: the list did not advance its cursor — refusing to page forever");t=s.cursor}},ge=(o,e)=>{if(o.size<e)return;const t=o.keys().next().value;t!==void 0&&o.delete(t)};class w{listeners=new Set;add(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}emit(...e){const[t]=e;for(const s of this.listeners)try{s(t)}catch{}}clear(){this.listeners.clear()}}const L=new Set(["RATE_LIMITED","SHARD_ERROR","SHARD_UNAVAILABLE","TOO_MANY_REQUESTS"]),ke=o=>{if(o instanceof A)return!0;const e=o?.code;return e===void 0||L.has(e)},Z=6e4,Ce=1e3,ee=o=>{if(o===null)return;const e=Number(o);if(Number.isFinite(e))return e>0?e*1e3:void 0;const t=Date.parse(o);if(Number.isNaN(t))return;const s=t-Date.now();return s>0?s:void 0},we={401:"UNAUTHORIZED",403:"FORBIDDEN",404:"NOT_FOUND",413:"PAYLOAD_TOO_LARGE",429:"TOO_MANY_REQUESTS"},M=(o,e,t)=>{const s=ee(t),r=s===void 0?void 0:{retryAfterMs:s},i=`LunoraClient: response carried no error envelope (status ${o.toString()}${e?` ${e}`:""})`;return o<400||o>=500||o===408?new A(i,r):new m(we[o]??"BAD_REQUEST",i,{data:r})},Ee=(o,e)=>{const t=ee(e);if(!(t===void 0||X(o)!==void 0))return o.data===void 0?{retryAfterMs:t}:typeof o.data=="object"&&o.data!==null&&!Array.isArray(o.data)?{...o.data,retryAfterMs:t}:void 0},_e=(o,e=Math.random)=>{const t=Math.min(Ce*2**Math.max(o-1,0),Z);return Math.round(t*(.5+e()*.5))},Ae=o=>o instanceof A||L.has(o?.code??""),Ie=(o,e,t)=>{const s=X(o);return s!==void 0&&s>0?Math.min(s,Z):Ae(o)?_e(e,t):void 0},Oe=983040,Re=o=>typeof TextEncoder>"u"?o.length:new TextEncoder().encode(o).length,Pe="/_lunora/rpc",G="/_lunora/rpc-batch",Me="/_lunora/ws",N=o=>o===void 0||o===""?"":`&bucket=${encodeURIComponent(o)}`,H=o=>{for(let e=o.length-1;e>=0;e-=1)o[e]?.()},z=Symbol("lunora.unmergeableDelta"),He=(o,e)=>{for(const t of e)t.op==="delete"?o.delete(t.key):t.value!==void 0&&o.set(t.key,t.value)},Le="lunora-ping",Fe=3e4,Ue=1e4,xe=250,Ne=5e3,De=64,Be="/_lunora/admin/shard-traffic",Ke="/_lunora/admin/scheduled",We="/_lunora/admin/scheduled/status",qe="/_lunora/admin/scheduled/ws",$e="/_lunora/admin/scheduled/cancel",V="/_lunora/admin/scheduled/dead",Qe="/_lunora/admin/scheduled/dead/retry",Ge="/_lunora/admin/scheduled/dead/cancel",ze="/_lunora/admin/workflows/instances",Ve="/_lunora/admin/workflows/instance",je="/_lunora/admin/workflows/status",D="/_lunora/admin/storage",Je="/_lunora/admin/storage/url",Ye="/_lunora/admin/storage/buckets",Xe="/_lunora/admin/functions",Ze="/_lunora/admin/cron-jobs",et="/_lunora/admin/cron-jobs/run",tt="/_lunora/admin/openapi",st="/_lunora/admin/openrpc",rt="/_lunora/admin/global/tables",it="/_lunora/admin/global/table",nt="/_lunora/admin/global/facet",at="/_lunora/admin/vector/indexes",ot="/_lunora/admin/vector/query",ct="/_lunora/admin/logs/archive",ht="/_lunora/admin/kv/namespaces",dt="/_lunora/admin/kv/keys",B="/_lunora/admin/kv/value",ut="/_lunora/admin/auth/users",lt="/_lunora/admin/auth/sessions",ft="/_lunora/admin/auth/users/create",mt="/_lunora/admin/auth/users/role",pt="/_lunora/admin/auth/users/ban",yt="/_lunora/admin/auth/users/unban",St="/_lunora/admin/auth/users/password",bt="/_lunora/admin/auth/users/remove",Tt="/_lunora/admin/auth/users/impersonate",vt="/_lunora/admin/auth/sessions/revoke",gt="/_lunora/admin/auth/sessions/revoke-all",kt="/_lunora/admin/auth/capabilities",Ct="/_lunora/admin/auth/users/update",wt="/_lunora/admin/auth/accounts",Et="/_lunora/admin/auth/accounts/unlink",_t="/_lunora/admin/auth/passkeys",At="/_lunora/admin/auth/passkeys/delete",It="/_lunora/admin/auth/two-factor/disable",Ot="/_lunora/admin/auth/organizations",Rt="/_lunora/admin/auth/organizations/members",Pt="/_lunora/admin/auth/organizations/invitations",Mt="/_lunora/admin/auth/organizations/members/remove",Ht="/_lunora/admin/auth/organizations/invitations/cancel",Lt="/_lunora/admin/auth/config",Ft="/_lunora/admin/auth/organizations/create",Ut="/_lunora/admin/auth/organizations/update",xt="/_lunora/admin/auth/organizations/remove",Nt="/_lunora/admin/auth/organizations/members/add",Dt="/_lunora/admin/auth/organizations/members/invite",Bt="/_lunora/admin/auth/organizations/members/role",Kt="/_lunora/admin/auth/organizations/teams",Wt="/_lunora/admin/auth/organizations/teams/create",qt="/_lunora/admin/auth/organizations/teams/update",$t="/_lunora/admin/auth/organizations/teams/remove",Qt="/_lunora/admin/auth/organizations/teams/members",Gt="/_lunora/admin/auth/organizations/teams/members/add",zt="/_lunora/admin/auth/organizations/teams/members/remove",Vt="/_lunora/admin/auth/organizations/roles",jt="/_lunora/admin/auth/organizations/roles/create",Jt="/_lunora/admin/auth/organizations/roles/update",Yt="/_lunora/admin/auth/organizations/roles/remove",Xt="/api/auth",Zt="/get-session",j=o=>o.startsWith("https://")?`wss://${o.slice(8)}`:o.startsWith("http://")?`ws://${o.slice(7)}`:o,C=(o,e)=>`${o.endsWith("/")?o.slice(0,-1):o}${e}`,y=(o,e)=>{const t=new URLSearchParams;for(const[r,i]of Object.entries(e))i!==void 0&&i!==""&&t.set(r,String(i));const s=t.toString();return s===""?o:`${o}?${s}`},es=[ae],l=o=>o??"",te=(o,e)=>{const t=o.error,s=typeof t?.code=="string"?t.code:void 0,r=typeof t?.message=="string"?t.message:void 0;return{code:s,messageText:(typeof o.message=="string"?o.message:void 0)??r??e}},ts=o=>{const{code:e,messageText:t}=te(o,"stream error");return e===void 0?new Error(t):new m(e,t)},J=o=>{const{code:e,messageText:t}=te(o,"subscription error");return{message:t,...e===void 0?{}:{code:e}}};function K(o){if(o!==void 0)return e=>{o(e)}}const E=(o,e)=>{for(const t of o)try{t(e)}catch{}},ss=new TextDecoder,rs=o=>{if(typeof o=="string")return o;if(o instanceof ArrayBuffer)return ss.decode(o)},b=(o,e)=>{if(!o.socket||o.wsState!=="open")return!1;try{return o.socket.send(JSON.stringify(e)),!0}catch{return!1}},_=o=>{const e=new Error(o.message??"request failed");return e.code=o.code,o.data!==void 0&&(e.data=T(o.data)),o.hint!==void 0&&(e.hint=o.hint),o.docsUrl!==void 0&&(e.docsUrl=o.docsUrl),e},Y=(o,e)=>{const t=_(o),s=Ee(t,e);return s!==void 0&&(t.data=s),t},v=(o,e)=>{try{return W(o)}catch(t){const s=t instanceof Error?t.message:String(t);throw new TypeError(`LunoraClient: cannot encode ${e} — ${s}`,t instanceof Error?{cause:t}:void 0)}},is=(o,e)=>{const t=Array.from({length:e});for(const s of o){if(typeof s.id!="number"||s.id<0||s.id>=e)continue;const r=s.body;t[s.id]=r&&"error"in r&&r.error?{error:_(r.error),ok:!1}:{ok:!0,value:T(r?.result)}}return t.map(s=>s??{error:new Error("batch call returned no result"),ok:!1})},ns=Promise.resolve();class se{static MAX_POKE_BUFFERS=256;static createClientQuery(e,t){return{defaultValue:t,key:e}}url;wsUrl;clientQueryStore;wsToken;authBasePath;fetchImpl;WebSocketImpl;bookmark;reconnectOptions;connectTimeoutMs;heartbeatIntervalMs;offlineQueue;outbox;clientId;shardCursors=new Map;defaultShardKey;readyResolved=!1;readyResolve;readyPromise;clientWatermarks=new Map;outboxMutationCounter=0;onPersistenceError;persistence;persistenceVersion;outboxLeaderRelease;queryCache;hydratedQueryCache=new Map;pendingCacheWrites=new Map;cacheFlushTimer;subscriptions=new k;tabCoordinator;leaderStatus;leaderWasEverConnected=!1;connections=new Map;defaultConnectionContext;connectionContexts=new Map;connectionContextHolders=new Map;authToken=null;authSubject=void 0;queuedIdentities=new Map;queuedOfflineShardKeys=new Set;closed=!1;authTokenListeners=new w;statusListeners=new w;tokenExpiredListeners=new w;mutationSettledListeners=new w;pendingChangeListeners=new w;whisperHandlers=new Map;lastStatus="idle";nextSubId=0;nextStreamId=0;streams=new Map;adminSocketTeardowns=new Set;offlineFlushes=new Map;replayRetryState=new Map;replayRetryTimers=new Map;shapeSubscriptions=new Map;pokeBuffers=new Map;nextShapeId=0;constructor(e){this.url=e.url,this.wsUrl=e.wsUrl??C(j(e.url),Me),this.wsToken=e.wsToken;const t=e.authBasePath??Xt;this.authBasePath=t.endsWith("/")?t.slice(0,-1):t,this.fetchImpl=e.fetch??(typeof fetch=="function"?fetch.bind(globalThis):void 0),this.WebSocketImpl=e.WebSocket??(typeof WebSocket=="function"?WebSocket:void 0),this.bookmark=e.bookmarkStorage??oe(),this.reconnectOptions=e.reconnect,this.heartbeatIntervalMs=e.heartbeatIntervalMs??Fe,this.connectTimeoutMs=e.connectTimeoutMs??Ue,this.defaultConnectionContext=e.connectionContext,this.persistence=ye(e.persistence,e.outbox===void 0),this.persistenceVersion=e.persistenceVersion,this.queryCache=Se(e.queryCache),this.onPersistenceError=e.offlineQueue?.onPersistenceError,this.clientQueryStore=new ce,e.crossTabSync?(this.tabCoordinator=this.createTabCoordinator(),this.tabCoordinator.start()):this.tabCoordinator=void 0,e.hydrateOnStart&&this.queryCache?this.readyPromise=new Promise(s=>{this.readyResolve=s}):(this.readyResolved=!0,this.readyPromise=ns),this.offlineQueue=new fe(e.offlineQueue,{onEvict:(s,r)=>{this.emitItemSettled(s,"rejected",r)},onSizeChange:s=>{this.pendingChangeListeners.emit(s)},persistence:this.persistence,version:e.persistenceVersion}),this.outbox=e.outbox,this.clientId=e.clientId??`client-${$()}`,this.persistence&&queueMicrotask(()=>{this.hydrateAsOutboxLeader()}),this.queryCache&&queueMicrotask(()=>{(async()=>{try{await this.hydrateQueryCache()}catch{}finally{this.readyResolved=!0,this.readyResolve?.()}})().catch(()=>{})})}setAuthToken(e,t){const s=this.authToken!==e,r=this.identityFingerprint();this.authToken=e,t!==void 0&&(this.authSubject=t);const i=this.identityFingerprint();if(i!==r&&(s?this.rejectQueuedForIdentityChange():(this.restampQueuedIdentity(r,i),this.restampWatermarks(r,i)),this.tabCoordinator)){const n=this.tabCoordinator.isLeader();this.tabCoordinator.stop(),this.tabCoordinator=this.createTabCoordinator(),this.tabCoordinator.start(),n&&this.tabCoordinator.promoteImmediately()}s&&this.authTokenListeners.emit(e)}getAuthToken(){return this.authToken}currentIdentity(){return this.identityFingerprint()}replayIdentityVerdict(e){const t=this.identityFingerprint();return e===t||e!==void 0&&this.isSameCredentialUnderTokenHash(e)?"match":t===null?"unknown":"mismatch"}clientIdentifier(){return this.clientId}confirmedMutationWatermark(e){return this.clientWatermarks.get(this.identityFingerprint()??"")?.get(e??"")??0}async callMutator(e,t,s){const r=s?.clientSeq;if(r!==void 0&&(!Number.isInteger(r)||r<=0))throw new m("INTERNAL",`callMutator: clientSeq must be a positive integer, got ${String(r)}`);const i=s?.shardKey??"",n=this.identityFingerprint();let a;const c=await this.rpc(e,t,s?.shardKey,{captureBookmark:!0,clientId:this.clientId,clientSeq:r,onMutationAck:d=>{a=d}});if(a!==void 0&&a>(this.clientWatermarks.get(n??"")?.get(i)??0)){let d=this.clientWatermarks.get(n??"");d===void 0&&(d=new Map,this.clientWatermarks.set(n??"",d)),d.set(i,a)}return{applied:a===void 0||a===r,result:c}}onAuthTokenChange(e){return this.authTokenListeners.add(e)}async getCurrentUser(){if(this.closed||!this.fetchImpl)return null;const e={};this.authToken&&(e.authorization=`Bearer ${this.authToken}`);try{const t=await this.fetchImpl(C(this.url,`${this.authBasePath}${Zt}`),{credentials:"include",headers:e,method:"GET"});return t.ok?(await t.json())?.user??null:null}catch{return null}}setWsToken(e){if(this.wsToken!==e){this.wsToken=e;for(const t of this.connections.values())if(t.socket)try{t.socket.close()}catch{}}}setConnectionContext(e,t={}){this.assertLeaderOwnedSurface("setConnectionContext");const s=l(t.shardKey);e===void 0?this.connectionContexts.delete(s):this.connectionContexts.set(s,e),this.refreshConnectionContext(s)}acquireConnectionContext(e,t={}){if(this.followsAnotherTab())return()=>{};const s=l(t.shardKey),r={context:e},i=this.connectionContextHolders.get(s);i?i.push(r):this.connectionContextHolders.set(s,[r]),this.refreshConnectionContext(s);let n=!1;return()=>{if(n)return;n=!0;const a=this.connectionContextHolders.get(s);if(!a)return;const c=a.indexOf(r);c!==-1&&a.splice(c,1),a.length===0&&this.connectionContextHolders.delete(s),this.refreshConnectionContext(s)}}whisperSubscribe(e,t,s={}){this.assertLeaderOwnedSurface("whisperSubscribe");const r=l(s.shardKey);let i=this.whisperHandlers.get(r);i||(i=new Map,this.whisperHandlers.set(r,i));let n=i.get(e);const a=n===void 0;if(n||(n=new Set,i.set(e,n)),n.add(t),this.ensureSocket(s.shardKey),a){const c=this.getConnection(s.shardKey);c&&b(c,{topic:e,type:"whisper_subscribe"})}return()=>{const c=this.whisperHandlers.get(r),h=c?.get(e);if(!h?.delete(t)||h.size>0)return;c?.delete(e),c?.size===0&&this.whisperHandlers.delete(r);const d=this.getConnection(s.shardKey);d&&b(d,{topic:e,type:"whisper_unsubscribe"})}}whisper(e,t,s={}){this.assertLeaderOwnedSurface("whisper"),this.ensureSocket(s.shardKey);const r=this.getConnection(s.shardKey);r&&b(r,{data:v(t??null,`whisper data for topic '${e}'`),topic:e,type:"whisper"})}onTokenExpired(e){return this.tokenExpiredListeners.add(e)}connectionStatus(){return this.computeStatus()}onConnectionStatus(e){const t=this.statusListeners.add(e);return e(this.computeStatus()),t}pendingCount(){return this.offlineQueue.size}debug(){const e=[],t=this.clientWatermarks.get(this.identityFingerprint()??""),s=new Set([...this.connections.keys(),...t?.keys()??[]]);for(const i of s){const n=this.connections.get(i);e.push({confirmedMutationWatermark:t?.get(i)??0,hasSocket:n?.socket!==void 0,shardKey:i===""?void 0:i,wasEverConnected:n?.wasEverConnected??!1,wsState:n?.wsState??"idle"})}const r=this.subscriptions.all().map(i=>({acked:i.acked,functionPath:i.fn.__lunoraRef,id:i.id,kind:"query",lastMutationId:i.lastMutationId,pendingOptimisticLayers:i.optimisticLayers.length,serverCursor:i.serverCursor,shardKey:i.shardKey,subscriberCount:i.callbacks.size}));for(const i of this.shapeSubscriptions.values())r.push({acked:i.serverCursor!==void 0,functionPath:`shape:${i.name}`,id:i.id,kind:"shape",lastMutationId:i.lastMutationId,pendingOptimisticLayers:0,rowCount:i.rows.size,serverCursor:i.serverCursor,shardKey:i.shardKey,subscriberCount:i.callbacks.size});return{clientId:this.clientId,closed:this.closed,connectionStatus:this.computeStatus(),pendingWrites:this.offlineQueue.size,shards:e.toSorted((i,n)=>(i.shardKey??"").localeCompare(n.shardKey??"")),subscriptions:r.toSorted((i,n)=>i.functionPath.localeCompare(n.functionPath))}}onPendingChange(e){const t=this.pendingChangeListeners.add(e);return e(this.offlineQueue.size),t}onMutationSettled(e){return this.mutationSettledListeners.add(e)}getWebSocketImpl(){return this.WebSocketImpl}getClientQuery(e){return this.clientQueryStore.get(e)}setClientQuery(e,t){this.clientQueryStore.set(e,t)}subscribeClientQuery(e,t){return this.clientQueryStore.subscribe(e,t)}resetClientQuery(e){this.clientQueryStore.reset(e)}snapshotPrecondition(e,t,s){return be(this,e,t,s)}whenReady(){return this.readyPromise}get isReady(){return this.readyResolved}peekHydratedQuery(e,t,s){if(!this.readyResolved)return;const r=q(t),i=P(e,r,s),n=this.hydratedQueryCache.get(i);if(n!==void 0)return n.identity===this.identityFingerprint()?n.value:void 0}peekActiveQuerySnapshot(e,t,s){const r=k.key(e,t,s),i=this.subscriptions.get(r);return{present:i!==void 0,value:i?.lastValue}}async query(e,t,s={}){return this.assertOpen(),await this.rpc(e.__lunoraRef,t,s.shardKey,{attachBookmark:!0})}async batch(e){if(this.assertOpen(),!this.fetchImpl)throw new m("INTERNAL","LunoraClient: no `fetch` implementation available");if(e.length===0)return[];const t=await this.fetchImpl(C(this.url,G),{body:JSON.stringify({calls:e.map((i,n)=>({args:v(i.args??{},`args for batch call '${i.fn.__lunoraRef}'`),functionPath:i.fn.__lunoraRef,id:n,shardKey:i.shardKey}))}),headers:this.rpcRequestHeaders({attachBookmark:!0}),method:"POST"}),s=t.headers.get("x-d1-bookmark");s&&this.bookmark.set(s);let r;try{r=await t.json()}catch{throw new m("INTERNAL",`LunoraClient: batch response was not JSON (status ${t.status.toString()})`)}if(!t.ok||r.error&&!r.results)throw r.error?_(r.error):new m("INTERNAL",`LunoraClient: batch request failed (status ${t.status.toString()})`);for(const i of r.results??[]){const n=i.body?.commitCursor;typeof i.id=="number"&&typeof n=="number"&&this.recordShardCursor(e[i.id]?.shardKey,n)}return is(r.results??[],e.length)}async mutation(e,t,s={}){this.assertOpen();const r=t,i=s.mutationId??$(),{confirms:n,rollbacks:a}=this.applyOptimisticUpdates(e.__lunoraRef,r,s.shardKey,s.optimistic);s.optimisticUpdate&&this.applyOptimisticUpdate(s.optimisticUpdate,t,s.shardKey,a,n);const c=this.offlineFlushes.get(l(s.shardKey));c!==void 0&&await c;const{hasSocket:h,wasEverConnected:d,wsState:u}=this.connectionGateState(s.shardKey),{queueBeforeFirstConnect:f}=this.offlineQueue,S=d||f,p=this.WebSocketImpl!==void 0&&S;if(u!=="open"&&!h&&p||u==="connecting"&&S)return this.enqueueOfflineMutation(e,r,s.shardKey,i,a,n,s.precondition);try{let I;const re=await this.rpc(e.__lunoraRef,r,s.shardKey,{captureBookmark:!0,mutationId:i,onCommitCursor:F=>{I=F}});for(const F of n)F(I);return re}catch(I){throw H(a),I}}async action(e,t,s={}){return this.assertOpen(),await this.rpc(e.__lunoraRef,t,s.shardKey,{attachBookmark:!0,captureBookmark:!0})}async importRows(e,t,s={}){this.assertOpen();const r=Math.max(1,Math.trunc(s.chunkSize??500)),i=s.toArgs??(h=>({rows:h})),n=t.length;let a=0,c=0;for(let h=0;h<n;h+=r){const d=t.slice(h,h+r),u=c;await this.mutation(e,i(d),{...s.importId===void 0?{}:{mutationId:`${s.importId}:${String(u)}`},shardKey:s.shardKey}),c+=1,a+=d.length,s.onProgress?.({done:a,total:n})}return{chunks:c,imported:a}}async shardTraffic(e){this.assertOpen();const t=await this.adminFetch(Be,"POST",{table:e});return{failed:t.failed??0,ok:t.ok??0,shards:t.shards??[]}}async listScheduledJobs(){return this.assertOpen(),(await this.adminFetch(Ke,"GET")).records??[]}async schedulerStatus(){this.assertOpen();const e=await this.adminFetch(We,"GET");return{backlog:e.backlog??0,inFlight:e.inFlight??0,pools:e.pools??[]}}async cancelScheduledJob(e){return this.assertOpen(),{cancelled:(await this.adminFetch($e,"POST",{id:e})).cancelled===!0}}async listDeadJobs(){return this.assertOpen(),await ve(async e=>await this.adminFetch(e===void 0?V:`${V}?cursor=${encodeURIComponent(e)}`,"GET"))}async retryDeadJob(e){return this.assertOpen(),{retried:(await this.adminFetch(Qe,"POST",{id:e})).retried===!0}}async removeDeadJob(e){return this.assertOpen(),{removed:(await this.adminFetch(Ge,"POST",{id:e})).removed===!0}}async listWorkflowInstances(e){this.assertOpen();const t=new URLSearchParams({name:e.name});e.status!==void 0&&t.set("status",e.status),e.page!==void 0&&t.set("page",String(e.page)),e.perPage!==void 0&&t.set("perPage",String(e.perPage));const s=await this.adminFetch(`${ze}?${t.toString()}`,"GET");return{configured:s.configured,instances:s.instances??[],page:s.page??1,perPage:s.perPage??e.perPage??0,totalCount:s.totalCount}}async getWorkflowInstance(e){this.assertOpen();const t=new URLSearchParams({id:e.id,name:e.name}),s=await this.adminFetch(`${Ve}?${t.toString()}`,"GET");return{createdOn:s.createdOn,endedOn:s.endedOn,error:s.error,id:s.id??e.id,output:s.output,params:s.params,startedOn:s.startedOn,status:s.status??"unknown",steps:s.steps??[]}}async setWorkflowInstanceStatus(e){return this.assertOpen(),{status:(await this.adminFetch(je,"POST",{action:e.action,id:e.id,name:e.name})).status??"unknown"}}subscribeScheduledJobs(e){if(this.assertOpen(),this.WebSocketImpl===void 0)return()=>{};const t=C(j(this.url),qe),s=Q(this.reconnectOptions),r={connectTimer:void 0,heartbeatTimer:void 0,lastFrameAt:0,socket:void 0};let i,n=!1;const a=()=>{n||this.closed||(i=setTimeout(d,s.next()))},c=f=>{if(n||this.WebSocketImpl===void 0)return;const S=f===void 0?t:`${t}?token=${encodeURIComponent(f)}`;this.openManagedSocket(r,S,this.connectTimeoutMs,{onClose:()=>{a()},onMessage:p=>{try{const g=JSON.parse(typeof p.data=="string"?p.data:"");g.type==="jobs"&&Array.isArray(g.records)&&(s.reset(),e(g.records))}catch{}}})},h=async f=>{let S;try{S=await f()}catch{a();return}c(S)},d=()=>{if(n||this.WebSocketImpl===void 0)return;const{wsToken:f}=this;if(typeof f=="function"){h(f).catch(()=>{});return}c(f)};d();const u=()=>{n=!0,i!==void 0&&(clearTimeout(i),i=void 0),r.connectTimer!==void 0&&(clearTimeout(r.connectTimer),r.connectTimer=void 0),this.stopHeartbeat(r),r.socket?.close(),r.socket=void 0};return this.adminSocketTeardowns.add(u),()=>{this.adminSocketTeardowns.delete(u),u()}}async listFunctions(){return this.assertOpen(),(await this.adminFetch(Xe,"GET")).functions??[]}async getCronJobs(){return this.assertOpen(),(await this.adminFetch(Ze,"GET")).jobs??[]}async runCronJob(e){this.assertOpen();const t=await this.adminFetch(et,"POST",{name:e});return{name:t.name??e,ran:t.ran===!0}}async fetchOpenApi(){return this.assertOpen(),await this.adminFetch(tt,"GET")}async fetchOpenRpc(){return this.assertOpen(),await this.adminFetch(st,"GET")}async listStorageObjects(e={}){this.assertOpen();const t=y(D,{bucket:e.bucket,cursor:e.cursor,limit:e.limit,prefix:e.prefix}),s=await this.adminFetch(t,"GET");return{cursor:s.cursor,objects:s.objects??[]}}async deleteStorageObject(e,t){this.assertOpen();const s=`${D}?key=${encodeURIComponent(e)}${N(t?.bucket)}`,r=await this.adminFetch(s,"DELETE");return{deleted:r.deleted===!0,key:r.key??e}}async listStorageBuckets(){return this.assertOpen(),(await this.adminFetch(Ye,"GET")).buckets??[]}async uploadStorageObject(e){this.assertOpen();const t=`${D}?key=${encodeURIComponent(e.key)}${N(e.bucket)}`,s=await this.adminFetch(t,"PUT",e.body,e.contentType);return{etag:s.etag,key:s.key??e.key}}async signedStorageUrl(e,t){this.assertOpen();const s=t?.expiresInSeconds,r=s===void 0?"":`&expiresIn=${encodeURIComponent(s.toString())}`,i=`${Je}?key=${encodeURIComponent(e)}${r}${N(t?.bucket)}`,n=await this.adminFetch(i,"GET");if(typeof n.url!="string")throw new TypeError("LunoraClient: storage URL endpoint returned no `url`");return n.url}async listGlobalTables(){return this.assertOpen(),await this.adminFetch(rt,"GET")}async readGlobalTablePage(e){this.assertOpen();const t=new URLSearchParams({table:e.table});return e.limit!==void 0&&t.set("limit",String(e.limit)),e.offset!==void 0&&t.set("offset",String(e.offset)),e.filters!==void 0&&e.filters.length>0&&t.set("filters",JSON.stringify(W(e.filters))),T(await this.adminFetch(`${it}?${t.toString()}`,"GET"))}async facetGlobalColumn(e){this.assertOpen();const t=new URLSearchParams({column:e.column,table:e.table});return e.limit!==void 0&&t.set("limit",String(e.limit)),e.filters!==void 0&&e.filters.length>0&&t.set("filters",JSON.stringify(W(e.filters))),T(await this.adminFetch(`${nt}?${t.toString()}`,"GET"))}async listVectorIndexes(){return this.assertOpen(),(await this.adminFetch(at,"GET")).indexes??[]}async queryVectorIndex(e){return this.assertOpen(),(await this.adminFetch(ot,"POST",e)).matches??[]}async queryLogArchive(e={}){this.assertOpen();const t=await this.adminFetch(ct,"POST",e);return{nextCursor:t.nextCursor,rows:t.rows??[]}}async listKvNamespaces(){return this.assertOpen(),(await this.adminFetch(ht,"GET")).namespaces??[]}async listKvKeys(e){this.assertOpen();const t=y(dt,{cursor:e.cursor,limit:e.limit,namespace:e.namespace,prefix:e.prefix});return await this.adminFetch(t,"GET")}async getKvValue(e){this.assertOpen();const t=y(B,{key:e.key,namespace:e.namespace});return await this.adminFetch(t,"GET")}async putKvValue(e){this.assertOpen(),await this.adminFetch(B,"PUT",e)}async deleteKvKey(e){this.assertOpen();const t=y(B,{key:e.key,namespace:e.namespace});await this.adminFetch(t,"DELETE")}async listAuthUsers(e={}){this.assertOpen();const t=y(ut,{filterField:e.filterField,filterValue:e.filterValue,limit:e.limit,offset:e.offset,search:e.search,searchField:e.searchField,sortBy:e.sortBy,sortDirection:e.sortDirection});return await this.adminFetch(t,"GET")}async createAuthUser(e){return await this.adminFetch(ft,"POST",e)}async setAuthUserRole(e){return await this.adminFetch(mt,"POST",e)}async banAuthUser(e){return await this.adminFetch(pt,"POST",e)}async unbanAuthUser(e){return await this.adminFetch(yt,"POST",e)}async setAuthUserPassword(e){await this.adminFetch(St,"POST",e)}async removeAuthUser(e){await this.adminFetch(bt,"POST",e)}async impersonateAuthUser(e){return await this.adminFetch(Tt,"POST",e)}async revokeAuthSession(e){await this.adminFetch(vt,"POST",e)}async revokeAuthUserSessions(e){await this.adminFetch(gt,"POST",e)}async getAuthCapabilities(){return await this.adminFetch(kt,"GET")}async updateAuthUser(e){return await this.adminFetch(Ct,"POST",e)}async listAuthAccounts(e){return await this.adminFetch(y(wt,{userId:e.userId}),"GET")}async unlinkAuthAccount(e){await this.adminFetch(Et,"POST",e)}async listAuthPasskeys(e){return await this.adminFetch(y(_t,{userId:e.userId}),"GET")}async deleteAuthPasskey(e){await this.adminFetch(At,"POST",e)}async disableAuthTwoFactor(e){await this.adminFetch(It,"POST",e)}async listAuthOrganizations(e={}){return await this.adminFetch(y(Ot,{limit:e.limit,offset:e.offset}),"GET")}async listAuthOrgMembers(e){const t=y(Rt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async listAuthOrgInvitations(e){const t=y(Pt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async removeAuthOrgMember(e){await this.adminFetch(Mt,"POST",e)}async cancelAuthOrgInvitation(e){await this.adminFetch(Ht,"POST",e)}async getAuthConfig(){return await this.adminFetch(Lt,"GET")}async createAuthOrganization(e){return await this.adminFetch(Ft,"POST",e)}async updateAuthOrganization(e){return await this.adminFetch(Ut,"POST",e)}async deleteAuthOrganization(e){await this.adminFetch(xt,"POST",e)}async addAuthOrgMember(e){return await this.adminFetch(Nt,"POST",e)}async inviteAuthOrgMember(e){return await this.adminFetch(Dt,"POST",e)}async setAuthOrgMemberRole(e){return await this.adminFetch(Bt,"POST",e)}async listAuthOrgTeams(e){const t=y(Kt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async createAuthOrgTeam(e){return await this.adminFetch(Wt,"POST",e)}async updateAuthOrgTeam(e){return await this.adminFetch(qt,"POST",e)}async removeAuthOrgTeam(e){await this.adminFetch($t,"POST",e)}async listAuthOrgTeamMembers(e){const t=y(Qt,{limit:e.limit,offset:e.offset,teamId:e.teamId});return await this.adminFetch(t,"GET")}async addAuthOrgTeamMember(e){return await this.adminFetch(Gt,"POST",e)}async removeAuthOrgTeamMember(e){await this.adminFetch(zt,"POST",e)}async listAuthOrgRoles(e){const t=y(Vt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async createAuthOrgRole(e){return await this.adminFetch(jt,"POST",e)}async updateAuthOrgRole(e){return await this.adminFetch(Jt,"POST",e)}async deleteAuthOrgRole(e){await this.adminFetch(Yt,"POST",e)}async listAuthSessions(e={}){this.assertOpen();const t=y(lt,{limit:e.limit,offset:e.offset,userId:e.userId});return await this.adminFetch(t,"GET")}subscribe(e,t,s,r={}){this.assertOpen();const i=t??{},n=k.key(e.__lunoraRef,i,r.shardKey);let a=this.subscriptions.get(n);const c=K(s),h=K(r.onError),d=K(r.onCheckpoint);if(!a){this.nextSubId+=1;const f=`sub_${this.nextSubId.toString()}`,S=q(i),p=this.takeHydratedCache(e.__lunoraRef,S,r.shardKey);a={acked:!1,args:i,argsKey:S,callbacks:new Set,checkpointCallbacks:new Set,errorCallbacks:new Set,fn:e,id:f,lastValue:p?.value,optimisticLayers:[],serverBase:p?.value,serverCursor:p?.serverCursor,shardKey:r.shardKey,wireArgs:v(i,`args for '${e.__lunoraRef}'`),...p?.serverEpoch===void 0?{}:{serverEpoch:p.serverEpoch}},this.subscriptions.add(a)}if(a.callbacks.add(c),h&&a.errorCallbacks.add(h),d&&a.checkpointCallbacks.add(d),a.lastValue!==void 0)try{c(a.lastValue)}catch{}this.ensureSocket(r.shardKey),this.sendSubscribeIfOpen(a);const u=a;return()=>{u.callbacks.delete(c),h&&u.errorCallbacks.delete(h),d&&u.checkpointCallbacks.delete(d),u.callbacks.size===0&&(this.sendOrQueueUnsubscribe(u.shardKey,u.id,"unsubscribe"),this.subscriptions.remove(u))}}subscribeShape(e,t,s={}){if(this.assertOpen(),this.followsAnotherTab())return()=>{};this.nextShapeId+=1;const r=`shape_${this.nextShapeId.toString()}`,i={args:e.args,callbacks:new Set([t]),errorCallbacks:s.onError?new Set([s.onError]):new Set,id:r,name:e.name,onCheckpoint:s.onCheckpoint,rows:new Map,shardKey:s.shardKey,wireArgs:e.args===void 0?void 0:v(e.args,`shape args for '${e.name}'`)};return this.shapeSubscriptions.set(r,i),this.ensureSocket(s.shardKey),this.sendShapeSubscribeIfOpen(i),()=>{this.shapeSubscriptions.delete(r),this.sendOrQueueUnsubscribe(i.shardKey,r,"shape_unsubscribe")}}stream(e,t,s={}){if(this.assertOpen(),this.WebSocketImpl===void 0)throw new m("INTERNAL","LunoraClient: streams require a WebSocket implementation");this.nextStreamId+=1;const r=`stream_${this.nextStreamId.toString()}`,{shardKey:i}=s,n=t??{},{handle:a,iterable:c}=Te({maxBuffer:s.maxBuffer,onCancel:()=>{this.cancelStream(r,i)}});this.ensureSocket(i);const h=this.getConnection(i),d={id:r,query:{args:v(n,`stream args for '${e.__lunoraRef}'`),functionPath:e.__lunoraRef,shardKey:i},type:"stream"},u={durable:s.durable===!0,generation:void 0,handle:a,lastSeq:0,message:d,shardKey:i,started:!1};this.streams.set(r,u);const f=h?.wsState==="open"&&b(h,d);if(u.started=f,!f&&h===void 0)return this.streams.delete(r),a.fail(new m("STREAM_DISCONNECTED","stream unavailable: this tab is not the cross-tab WebSocket leader, so it holds no socket to stream over")),c;if(!f){for(h.pendingStreams=h.pendingStreams??[];h.pendingStreams.length>=De;){const p=h.pendingStreams.shift()?.id,g=p?this.streams.get(p):void 0;g&&(g.handle.fail(new m("STREAM_QUEUE_OVERFLOW","stream-start frame evicted while socket was unreachable")),this.streams.delete(p))}h.pendingStreams.push(d)}return c}httpStream(e,t,s={}){if(this.closed)throw new m("CLIENT_CLOSED","LunoraClient is closed");if(!this.fetchImpl)throw new m("INTERNAL","LunoraClient: no `fetch` implementation available");const r={...this.authToken?{authorization:`Bearer ${this.authToken}`}:{},...s.headers};return de(e,t,{baseUrl:this.url,fetch:this.fetchImpl,headers:r,maxBuffer:s.maxBuffer,signal:s.signal})}close(){this.closed=!0,this.outboxLeaderRelease?.(),this.outboxLeaderRelease=void 0;for(const e of this.streams.values())e.handle.fail(new m("CLIENT_CLOSED","LunoraClient closed"));this.streams.clear();for(const e of this.connections.values())this.teardownConnection(e);for(const e of this.adminSocketTeardowns)e();this.adminSocketTeardowns.clear();for(const e of this.replayRetryTimers.values())clearTimeout(e);this.replayRetryTimers.clear(),this.replayRetryState.clear(),this.offlineQueue.clear(),this.queuedIdentities.clear(),this.cacheFlushTimer!==void 0&&(clearTimeout(this.cacheFlushTimer),this.cacheFlushTimer=void 0),this.pendingCacheWrites.size>0&&this.flushQueryCacheWrites().catch(()=>{}),this.authTokenListeners.clear(),this.statusListeners.clear(),this.tokenExpiredListeners.clear(),this.mutationSettledListeners.clear(),this.pendingChangeListeners.clear(),this.whisperHandlers.clear(),this.shapeSubscriptions.clear(),this.pokeBuffers.clear(),this.subscriptions.clear(),this.clientQueryStore.clear(),this.hydratedQueryCache.clear(),this.tabCoordinator?.stop(),this.tabCoordinator=void 0}assertOpen(){if(this.closed)throw new m("INTERNAL","LunoraClient is closed")}followsAnotherTab(){const e=this.tabCoordinator;if(e===void 0||e.isLeader())return!1;const t=e.leaderTabId;return t!==void 0&&t!==e.id}assertLeaderOwnedSurface(e){if(this.followsAnotherTab())throw new m("NOT_IMPLEMENTED",`LunoraClient: \`${e}\` is unavailable on a cross-tab follower tab. The \`crossTabSync\` channel only carries data from the leader tab outward, so this call could never reach the server. Turn off \`crossTabSync\`, or keep \`${e}\` on the leader tab.`)}clearConnectionTimers(e){clearTimeout(e.reconnectTimer),clearTimeout(e.connectTimer),clearTimeout(e.stableTimer),e.reconnectTimer=void 0,e.connectTimer=void 0,e.stableTimer=void 0}teardownConnection(e){const t=l(e.shardKey);for(const[s,r]of this.streams)l(r.shardKey)===t&&(r.handle.fail(new m("STREAM_DISCONNECTED","stream terminated: the connection carrying it was torn down")),this.streams.delete(s));if(e.pendingStreams=void 0,e.pendingUnsubscribes=[],this.clearConnectionTimers(e),this.stopHeartbeat(e),e.socket){try{e.socket.close()}catch{}e.socket=void 0}e.wsState="closed"}createTabCoordinator(){const e=`lunora-bridge::${this.url}::${this.identityFingerprint()??"anon"}`;return new he({channelName:e,onBecomeLeader:()=>{for(const t of this.subscriptions.all())this.ensureSocket(t.shardKey),this.sendSubscribeIfOpen(t);this.tabCoordinator?.broadcastConnectionStatus(this.computeStatus(),this.identityFingerprint())},onStopBeingLeader:()=>{for(const[t,s]of this.connections)this.teardownConnection(s),this.connections.delete(t);this.leaderStatus=void 0,this.leaderWasEverConnected=!1},onConnectionStatus:(t,s)=>{if(s!==void 0&&s!==this.identityFingerprint())return;const r=t==="connected"&&this.leaderStatus!=="connected";this.leaderStatus=t,t==="connected"&&(this.leaderWasEverConnected=!0),this.emitConnectionStatus(),r&&this.flushAllOfflineQueues()},onSubscriptionData:(t,s,r,i,n)=>{if(n!==void 0&&n!==this.identityFingerprint())return;const a=this.subscriptions.get(t);a&&(a.serverBase=s,r!==void 0&&(a.serverCursor=r,i!==void 0&&(a.serverEpoch=i),U(a,r)),O(a,R(s,a.optimisticLayers)))},onLeaderClaimAnswered:()=>{this.tabCoordinator?.isLeader()&&this.tabCoordinator.broadcastConnectionStatus(this.computeStatus(),this.identityFingerprint())},onSubscriptionError:(t,s,r)=>{if(r!==void 0&&r!==this.identityFingerprint())return;const i=this.subscriptions.get(t);i&&E(i.errorCallbacks,s)},onSubscriptionSettled:(t,s,r,i,n,a)=>{if(a!==void 0&&a!==this.identityFingerprint())return;const c=this.subscriptions.get(t);if(c){this.ackAndAdvanceCursor(c,s,r),i!==void 0&&n===this.clientId&&(c.lastMutationId=Math.max(c.lastMutationId??0,i));for(const h of c.checkpointCallbacks)h({checkpoint:c.serverCursor,mutationId:c.lastMutationId})}}})}async enqueueOfflineMutation(e,t,s,r,i,n,a){const c=this.identityFingerprint();if(this.outbox){this.outboxMutationCounter+=1;const h=this.outboxMutationCounter;try{await this.outbox.enqueue({args:t,clientId:this.clientId,functionPath:e.__lunoraRef,idempotencyKey:`${this.clientId}:${String(h)}`,identity:c,mutationId:h,shardKey:s})}catch(d){throw H(i),d instanceof Error?d:new Error(String(d))}for(const d of n)d(void 0);return}return new Promise((h,d)=>{const u={args:t,functionPath:e.__lunoraRef,liveAwaiter:!0,id:r,clientId:this.clientId,identity:c,precondition:a,onCommit:f=>{for(const S of n)S(f)},reject:f=>{this.queuedIdentities.delete(r),H(i),d(f instanceof Error?f:new Error(String(f)))},resolve:h,shardKey:s};this.offlineQueue.enqueue(u),this.queuedOfflineShardKeys.add(s),u.id!==void 0&&this.queuedIdentities.set(u.id,c)})}async hydratePersistedQueue(){try{const e=await this.offlineQueue.hydrate();for(const t of e)this.queuedOfflineShardKeys.add(t),this.ensureSocket(t)}catch{}}hydrateAsOutboxLeader(){const e=()=>{this.hydratePersistedQueue().catch(()=>{})},t=globalThis.navigator?.locks;if(!t){e();return}t.request(`lunora:outbox-leader:${this.url}`,()=>(this.closed||e(),new Promise(s=>{if(this.closed){s();return}this.outboxLeaderRelease=s}))).catch(e)}async hydrateQueryCache(){if(this.queryCache)try{const e=await this.queryCache.load(),t=new Map;for(const s of this.subscriptions.all())t.set(P(s.fn.__lunoraRef,s.argsKey,s.shardKey),s);for(const{key:s,...r}of e){if(me(this.persistenceVersion,r.version)){this.queryCache.remove(s).catch(()=>{});continue}const i=t.get(s);if(i){this.seedSubscriptionFromCache(i,r);continue}this.hydratedQueryCache.set(s,r)}}catch{}}seedSubscriptionFromCache(e,t){e.serverBase!==void 0||e.lastValue!==void 0||t.identity!==this.identityFingerprint()||(e.serverBase=t.value,e.serverCursor=t.serverCursor,t.serverEpoch!==void 0&&(e.serverEpoch=t.serverEpoch),O(e,R(t.value,e.optimisticLayers)))}takeHydratedCache(e,t,s){const r=P(e,t,s),i=this.hydratedQueryCache.get(r);if(i!==void 0)return this.hydratedQueryCache.delete(r),i.identity===this.identityFingerprint()?i:void 0}persistQueryValue(e){const t=e.serverBase;if(!this.queryCache||t===void 0)return;const s=P(e.fn.__lunoraRef,e.argsKey,e.shardKey),r=this.getConnection(e.shardKey)?.identity;this.pendingCacheWrites.set(s,{identity:r===void 0?this.identityFingerprint():r,serverCursor:e.serverCursor,ts:Date.now(),value:t,...e.serverEpoch===void 0?{}:{serverEpoch:e.serverEpoch},...this.persistenceVersion===void 0?{}:{version:this.persistenceVersion}}),this.cacheFlushTimer??=setTimeout(()=>{this.flushQueryCacheWrites().catch(()=>{})},xe)}async flushQueryCacheWrites(){this.cacheFlushTimer=void 0;const{queryCache:e}=this;if(!e){this.pendingCacheWrites.clear();return}const t=[...this.pendingCacheWrites.entries()];this.pendingCacheWrites.clear(),await Promise.allSettled(t.map(async([s,r])=>e.put(s,r)))}computeStatus(){if(this.tabCoordinator&&!this.tabCoordinator.isLeader())return this.leaderStatus??"idle";const e=[...this.connections.values()];return e.length===0?"idle":e.some(t=>t.wsState==="open")?"connected":e.some(t=>t.wsState==="connecting")?"connecting":"offline"}emitConnectionStatus(){const e=this.computeStatus();e!==this.lastStatus&&(this.lastStatus=e,this.statusListeners.emit(e),this.tabCoordinator?.isLeader()&&this.tabCoordinator.broadcastConnectionStatus(e,this.identityFingerprint()))}emitItemSettled(e,t,s){this.mutationSettledListeners.emit({args:e.args,code:s===void 0?void 0:s.code,error:s,functionPath:e.functionPath,hadAwaiter:e.liveAwaiter??!1,id:e.id??"",shardKey:e.shardKey,status:t})}applyOptimisticUpdates(e,t,s,r){const i=[],n=[];if(!r)return{confirms:i,rollbacks:n};const a=k.key(e,t,s),c=this.subscriptions.get(a);if(c){const h=ue(c,r);h&&(i.push(h.confirm),n.push(h.rollback))}return{confirms:i,rollbacks:n}}applyOptimisticUpdate(e,t,s,r,i){const{confirms:n,rollbacks:a,store:c}=le(this.subscriptions,s);try{e(c,t)}catch{H(a);return}r.push(...a),i.push(...n)}getConnection(e){return this.connections.get(l(e))}sendOrQueueUnsubscribe(e,t,s){const r=this.getConnection(e);r&&!b(r,{id:t,type:s})&&r.pendingUnsubscribes.push({id:t,type:s})}connectionGateState(e){if(this.tabCoordinator&&!this.tabCoordinator.isLeader()){let s="idle";return this.leaderStatus==="connected"?s="open":this.leaderStatus==="connecting"&&(s="connecting"),{hasSocket:!1,wasEverConnected:this.leaderWasEverConnected,wsState:s}}const t=this.getConnection(e);return{hasSocket:t?.socket!==void 0,wasEverConnected:t?.wasEverConnected??!1,wsState:t?.wsState??"idle"}}getOrCreateConnection(e){const t=l(e);let s=this.connections.get(t);return s||(s={connectTimer:void 0,heartbeatTimer:void 0,lastFrameAt:0,pendingUnsubscribes:[],reconnect:Q(this.reconnectOptions),reconnectTimer:void 0,shardKey:e,socket:void 0,stableTimer:void 0,wasEverConnected:!1,wsState:"idle"},this.connections.set(t,s)),s}wsUrlFor(e,t){const s=[];if(e!==void 0&&s.push(`shard=${encodeURIComponent(e)}`),t!==void 0&&s.push(`token=${encodeURIComponent(t)}`),s.length===0)return this.wsUrl;const r=this.wsUrl.includes("?")?"&":"?";return`${this.wsUrl}${r}${s.join("&")}`}rpcRequestHeaders(e,t){const s={"content-type":"application/json"},r=this.shardCursors.get(this.cursorKeyFor(t));if(r!==void 0&&(s["x-lunora-min-seq"]=r.toString()),this.authToken&&(s.authorization=`Bearer ${this.authToken}`),e.mutationId&&(s["x-lunora-mutation-id"]=e.mutationId),(e.clientId!==void 0||e.mutationId!==void 0)&&(s["x-lunora-client-id"]=e.clientId??this.clientId),e.clientSeq!==void 0&&(s["x-lunora-client-seq"]=e.clientSeq.toString()),e.attachBookmark){const i=this.bookmark.get();i&&(s["x-d1-bookmark"]=i)}return s}async rpc(e,t,s,r={}){if(!this.fetchImpl)throw new A("LunoraClient: no `fetch` implementation available");const i=this.rpcRequestHeaders(r,s),n=await this.fetchImpl(C(this.url,Pe),{body:JSON.stringify({args:v(t,`args for '${e}'`),functionPath:e,shardKey:s}),headers:i,method:"POST"});if(r.captureBookmark){const c=n.headers.get("x-d1-bookmark");c&&this.bookmark.set(c)}let a;try{a=await n.json()}catch{throw M(n.status,n.statusText,n.headers.get("retry-after"))}if("error"in a)throw Y(a.error,n.headers.get("retry-after"));if(!n.ok)throw M(n.status,n.statusText,n.headers.get("retry-after"));return r.onMutationAck?.(a.lastMutationId),r.onCommitCursor?.(a.commitCursor),this.learnDefaultShardKey(n,s),this.recordShardCursor(s,a.commitCursor),T(a.result)}async adminFetch(e,t,s,r){if(!this.fetchImpl)throw new m("INTERNAL","LunoraClient: no `fetch` implementation available");const i={};this.authToken&&(i.authorization=`Bearer ${this.authToken}`);const n=s instanceof ArrayBuffer||s instanceof Blob;let a;s===void 0?a=void 0:n?(a=s,r!==void 0&&(i["content-type"]=r)):(a=JSON.stringify(s),i["content-type"]="application/json");const c=await this.fetchImpl(C(this.url,e),{body:a,headers:i,method:t});let h;try{h=await c.json()}catch{const d=c.statusText?` ${c.statusText}`:"";throw new m("INTERNAL",`LunoraClient: response was not JSON (status ${c.status.toString()}${d})`)}if(typeof h=="object"&&h!==null&&"error"in h){const d=h.error,u=new Error(d.message??"admin request failed");throw u.code=d.code,u}if(!c.ok){const d=c.statusText?` ${c.statusText}`:"";throw new m("INTERNAL",`LunoraClient: admin request failed (status ${c.status.toString()}${d})`)}return h}effectiveConnectionContext(e){const t=this.connectionContextHolders.get(e);return t&&t.length>0?t[t.length-1]?.context:this.connectionContexts.get(e)??this.defaultConnectionContext}refreshConnectionContext(e){const t=this.connections.get(e);t?.wsState==="open"&&this.sendConnectEnvelope(t)}sendConnectEnvelope(e){const t=this.effectiveConnectionContext(l(e.shardKey));b(e,{caps:es,clientId:this.clientId,id:"connect",type:"connect",...t===void 0?{}:{context:t}})}resendShapeSubscriptions(e){for(const t of this.shapeSubscriptions.values())l(t.shardKey)===l(e)&&this.sendShapeSubscribeIfOpen(t)}ensureSocket(e){if(this.closed||this.WebSocketImpl===void 0||this.tabCoordinator&&!this.tabCoordinator.isLeader())return;const t=this.getOrCreateConnection(e);if(!(t.wsState==="open"||t.wsState==="connecting")){if(t.wsState="connecting",this.emitConnectionStatus(),typeof this.wsToken=="function"){this.openSocketWithProvidedToken(t,e,this.wsToken).catch(()=>{});return}this.openSocket(t,e,this.wsToken)}}async openSocketWithProvidedToken(e,t,s){let r;try{r=await s()}catch{this.handleDisconnect(e);return}this.closed||this.WebSocketImpl===void 0||e.wsState!=="connecting"||e.socket!==void 0||this.openSocket(e,t,r)}openManagedSocket(e,t,s,r){const{WebSocketImpl:i}=this;if(i===void 0)throw new m("INTERNAL","no WebSocket implementation available");const n=new i(t);e.socket=n;const a=()=>{this.stopHeartbeat(e),e.connectTimer!==void 0&&(clearTimeout(e.connectTimer),e.connectTimer=void 0),e.socket===n&&(e.socket=void 0)},c=h=>{a(),r.onClose(h)};s>0&&(e.connectTimer=setTimeout(()=>{if(e.connectTimer=void 0,e.socket===n){try{n.close()}catch{}c()}},s)),n.addEventListener("open",()=>{if(e.socket===n){e.connectTimer!==void 0&&(clearTimeout(e.connectTimer),e.connectTimer=void 0),e.lastFrameAt=Date.now();try{r.onOpen?.()}catch(h){console.error("[lunora] connection open handler threw",h)}this.startHeartbeat(e,c)}}),n.addEventListener("message",h=>{if(e.socket===n){e.lastFrameAt=Date.now();try{r.onMessage(h)}catch(d){console.error("[lunora] server frame handler threw",d)}}}),n.addEventListener("close",h=>{e.socket===n&&c(h)}),n.addEventListener("error",()=>{e.socket===n&&c()})}openSocket(e,t,s){this.WebSocketImpl!==void 0&&(e.identity=this.identityFingerprint(),this.openManagedSocket(e,this.wsUrlFor(t,s),this.connectTimeoutMs,{onClose:r=>{r?.code===4001&&this.notifyTokenExpired(),this.handleDisconnect(e)},onMessage:r=>{this.handleServerMessage(r.data,t)},onOpen:()=>{e.wsState="open",e.wasEverConnected=!0,clearTimeout(e.stableTimer),e.stableTimer=setTimeout(()=>{e.wsState==="open"&&e.reconnect.reset()},Ne),this.emitConnectionStatus(),this.sendConnectEnvelope(e),this.markShardPendingAck(t);for(const i of this.subscriptions.all())l(i.shardKey)===l(t)&&this.sendSubscribeIfOpen(i);if(this.resendShapeSubscriptions(t),e.pendingUnsubscribes.length>0){const i=e.pendingUnsubscribes;e.pendingUnsubscribes=[];for(const{id:n,type:a}of i)b(e,{id:n,type:a})}this.flushPendingStreams(e);const r=this.whisperHandlers.get(l(t));if(r)for(const i of r.keys())b(e,{topic:i,type:"whisper_subscribe"});this.flushOfflineQueue(t).catch(()=>{})}}))}flushPendingStreams(e){if(!e.pendingStreams||e.pendingStreams.length===0)return;const t=e.pendingStreams;e.pendingStreams=[];for(const s of t){const r=b(e,s)?this.streams.get(String(s.id)):void 0;r&&(r.started=!0)}}cancelStream(e,t){const s=this.getConnection(t),r=this.streams.get(e);s&&(s.pendingStreams=s.pendingStreams?.filter(i=>i.id!==e),!b(s,{id:e,type:"unsubscribe"})&&r?.durable===!0&&r.started&&s.pendingUnsubscribes.push({id:e,type:"unsubscribe"})),this.streams.delete(e)}handleDisconnect(e){if(this.closed||e.wsState==="idle"||e.wsState==="closed")return;this.stopHeartbeat(e),this.clearConnectionTimers(e),e.socket=void 0,e.wsState="idle",this.emitConnectionStatus(),this.markShardPendingAck(e.shardKey);const t=new Set((e.pendingStreams??[]).map(r=>r.id));for(const[r,i]of this.streams)if(!(l(i.shardKey)!==l(e.shardKey)||t.has(r))){if(i.durable){const n={...i.message,...i.generation===void 0?{}:{generation:i.generation},sinceChunk:i.lastSeq};i.message=n,e.pendingStreams=[...(e.pendingStreams??[]).filter(a=>a.id!==r),n];continue}i.handle.fail(new m("STREAM_DISCONNECTED","stream terminated: WebSocket disconnected")),this.streams.delete(r)}if(this.WebSocketImpl===void 0)return;const s=e.reconnect.next();e.reconnectTimer=setTimeout(()=>{e.reconnectTimer=void 0,this.ensureSocket(e.shardKey)},s)}startHeartbeat(e,t){this.stopHeartbeat(e),!(this.heartbeatIntervalMs<=0)&&(e.heartbeatTimer=setInterval(()=>{if(!e.socket)return;const{socket:s}=e;if(Date.now()-e.lastFrameAt>this.heartbeatIntervalMs*2.5){try{s.close()}catch{}t();return}try{s.send(Le)}catch{}},this.heartbeatIntervalMs))}stopHeartbeat(e){e.heartbeatTimer!==void 0&&(clearInterval(e.heartbeatTimer),e.heartbeatTimer=void 0)}markShardPendingAck(e){const t=l(e);for(const s of this.subscriptions.all())l(s.shardKey)===t&&(s.acked=!1)}sendSubscribeIfOpen(e){const t=this.getConnection(e.shardKey);if(t?.wsState!=="open"||e.acked)return;const s=e.fn.__lunoraTable??e.fn.__lunoraRef,r=e.serverCursor!==void 0&&e.serverBase!==void 0;b(t,{id:e.id,query:{args:e.wireArgs,functionPath:e.fn.__lunoraRef,table:s,...r?{sinceSeq:e.serverCursor}:{},...r&&e.serverEpoch!==void 0?{sinceEpoch:e.serverEpoch}:{}},type:"subscribe"})}sendShapeSubscribeIfOpen(e){const t=this.getConnection(e.shardKey);t?.wsState==="open"&&b(t,{id:e.id,shape:{name:e.name,...e.wireArgs===void 0?{}:{args:e.wireArgs}},type:"shape_subscribe",...e.serverCursor===void 0?{}:{sinceCheckpoint:e.serverCursor},...e.serverEpoch===void 0?{}:{sinceEpoch:e.serverEpoch}})}handleServerMessage(e,t){const s=rs(e);if(s===void 0)return;let r;try{r=JSON.parse(s)}catch{return}switch(r.type!=="error"&&this.getConnection(t)?.reconnect.reset(),r.type){case"ack":{const i=this.subscriptions.getById(r.id);i&&(i.acked=!0);return}case"chunk":{const{data:i,generation:n,id:a,seq:c}=r,h=this.streams.get(a);h&&typeof c=="number"&&(h.lastSeq=c,typeof n=="number"&&(h.generation=n),h.durable=!0),h?.handle.push(T(i));return}case"complete":{this.handleCompleteMessage(r.id);return}case"data":case"delta":{this.handleDataMessage(r);return}case"error":{this.handleErrorMessage(r);break}case"pokeEnd":{this.handlePokeEnd(r,t);break}case"pokePart":{this.handlePokePart(r,t);break}case"pokeStart":{this.handlePokeStart(r,t);break}case"resume":{this.handleResumeMessage(r);break}case"settled":{this.handleSettledMessage(r);break}case"whisper":{this.dispatchWhisper(r,t);break}}}handleErrorMessage(e){if(e.error?.code==="TOKEN_EXPIRED"){this.notifyTokenExpired();return}const{id:s}=e,r=s===void 0?void 0:this.streams.get(s);if(r&&s!==void 0){r.handle.fail(ts(e)),this.streams.delete(s);return}const i=s===void 0?void 0:this.subscriptions.getById(s);if(i){const a=J(e),c=this.identityFingerprint(),h=k.keyOf(i);E(i.errorCallbacks,a),this.tabCoordinator?.isLeader()&&this.tabCoordinator.broadcastSubscriptionError(h,a,c);return}const n=s===void 0?void 0:this.shapeSubscriptions.get(s);n&&E(n.errorCallbacks,J(e))}pokeBufferKey(e,t){return`${l(e)}\0${t}`}handlePokeStart(e,t){ge(this.pokeBuffers,se.MAX_POKE_BUFFERS),this.pokeBuffers.set(this.pokeBufferKey(t,e.pokeId),{baseCheckpoint:e.baseCheckpoint,bases:new Map,epoch:e.epoch,lastMutationId:new Map,parts:new Map,resets:new Set})}handlePokePart(e,t){const s=this.pokeBuffers.get(this.pokeBufferKey(t,e.pokeId));if(!s)return;const r=s.parts.get(e.shapeId)??[];for(const n of e.rowsPatch)r.push(n.value===void 0?n:{...n,value:T(n.value)});s.parts.set(e.shapeId,r),e.lastMutationId!==void 0&&s.lastMutationId.set(e.shapeId,e.lastMutationId);const i=e.baseCheckpoint??s.baseCheckpoint;i!==void 0&&s.bases.set(e.shapeId,i),e.reset===!0&&s.resets.add(e.shapeId)}handlePokeEnd(e,t){const s=this.pokeBufferKey(t,e.pokeId),r=this.pokeBuffers.get(s);if(r){this.pokeBuffers.delete(s);for(const i of r.parts.keys()){const n=this.shapeSubscriptions.get(i);n&&this.applyPokePart(n,r,e)}}}applyPokePart(e,t,s){const r=t.parts.get(e.id)??[],i=t.resets.has(e.id),n=t.bases.get(e.id),a=t.epoch!==void 0&&e.serverEpoch!==void 0&&t.epoch!==e.serverEpoch,c=n!==void 0&&e.serverCursor!==void 0&&e.serverCursor!==n;if(!i&&(a||c)){e.rows.clear(),e.serverCursor=void 0,e.serverEpoch=void 0,this.emitShapeRows(e),this.sendShapeSubscribeIfOpen(e);return}i&&e.rows.clear(),He(e.rows,r),s.checkpoint!==void 0&&(e.serverCursor=s.checkpoint),s.epoch!==void 0&&(e.serverEpoch=s.epoch);const h=t.lastMutationId.get(e.id);h!==void 0&&(e.lastMutationId=h),this.emitShapeRows(e),e.onCheckpoint?.({checkpoint:e.serverCursor,mutationId:e.lastMutationId})}resnapshotSubscription(e){e.acked=!1,e.serverCursor=void 0,e.serverEpoch=void 0,console.warn("[lunora] could not merge a row delta into the cached result; re-subscribing for a full snapshot"),this.sendSubscribeIfOpen(e)}emitShapeRows(e){const t=[...e.rows.values()];for(const s of e.callbacks)try{s(t)}catch{}}handleDataMessage(e){const{id:t}=e,s=t?this.subscriptions.getById(t):void 0;if(!s)return;let r;try{r=this.resolveDataPayload(e,s)}catch(i){E(s.errorCallbacks,{code:"WIRE_DECODE_FAILED",message:`could not decode a server frame for this subscription — ${i instanceof Error?i.message:String(i)}`});return}if(r===z){this.resnapshotSubscription(s);return}if(s.serverBase=r,e.cursor!==void 0&&(s.serverCursor=e.cursor),e.epoch!==void 0&&(s.serverEpoch=e.epoch),e.lastMutationId!==void 0){s.lastMutationId=Math.max(s.lastMutationId??0,e.lastMutationId);for(const i of s.checkpointCallbacks)i({checkpoint:s.serverCursor,mutationId:s.lastMutationId})}if(this.persistQueryValue(s),U(s,s.serverCursor),O(s,R(r,s.optimisticLayers)),this.tabCoordinator?.isLeader()){const i=k.keyOf(s);this.tabCoordinator.broadcastSubscriptionData(i,r,s.serverCursor,s.serverEpoch,this.identityFingerprint())}}handleResumeMessage(e){const t=this.subscriptions.getById(e.id);t&&this.ackAndAdvanceCursor(t,e.cursor,e.epoch)}handleSettledMessage(e){const t=this.subscriptions.getById(e.id);if(t){this.ackAndAdvanceCursor(t,e.cursor,e.epoch),e.lastMutationId!==void 0&&(t.lastMutationId=Math.max(t.lastMutationId??0,e.lastMutationId));for(const s of t.checkpointCallbacks)s({checkpoint:t.serverCursor,mutationId:t.lastMutationId});if(this.tabCoordinator?.isLeader()){const s=k.keyOf(t);this.tabCoordinator.broadcastSubscriptionSettled(s,t.serverCursor,t.serverEpoch,t.lastMutationId,this.clientId,this.identityFingerprint())}}}ackAndAdvanceCursor(e,t,s){e.acked=!0,(t!==void 0&&t!==e.serverCursor||s!==void 0&&s!==e.serverEpoch)&&(t!==void 0&&(e.serverCursor=t),s!==void 0&&(e.serverEpoch=s),this.persistQueryValue(e),U(e,e.serverCursor)&&O(e,R(e.serverBase,e.optimisticLayers)))}resolveDataPayload(e,t){if("data"in e&&e.data!==void 0)return T(e.data);const s=T(e.delta);return ie(s)?t.serverBase===void 0?s:ne(t.serverBase,s)??z:s}dispatchWhisper(e,t){const s=this.whisperHandlers.get(l(t))?.get(e.topic);if(!s)return;const r=T(e.data);for(const i of s)try{i(r,e.from)}catch{}}notifyTokenExpired(){this.tokenExpiredListeners.emit()}handleCompleteMessage(e){const t=this.streams.get(e);if(t){t.handle.complete(),this.streams.delete(e);return}const s=this.subscriptions.getById(e);s&&(s.acked=!1,E(s.errorCallbacks,{code:"SUBSCRIPTION_CANCELLED",message:"subscription was cancelled by the server"}))}unpersist(e){e&&this.persistence?.remove(e).catch(t=>{pe(this.onPersistenceError,"remove",t,e)})}identityFingerprint(){if(this.authSubject!==void 0)return this.authSubject===null?null:`subj:${this.authSubject}`;const e=this.authToken;return e===null?null:this.hashToken(e)}hashToken(e){let t=2166136261,s=5381;for(let r=0;r<e.length;r+=1){const i=e.charCodeAt(r);t^=i,t=Math.imul(t,16777619),s=Math.imul(s,33)+i}return`${e.length.toString(36)}:${(t>>>0).toString(36)}:${(s>>>0).toString(36)}`}isSameCredentialUnderTokenHash(e){if(e===null||e.startsWith("subj:"))return!1;const t=this.authToken;return t===null?!1:this.hashToken(t)===e}rejectQueuedForIdentityChange(){const e=this.offlineQueue.drain();for(const t of e){this.queuedIdentities.delete(t.id??""),this.unpersist(t.id);const s=new Error("offline mutation discarded: auth identity changed before replay");s.code="OFFLINE_IDENTITY_CHANGED",t.reject(s),this.emitItemSettled(t,"rejected",s)}this.clearQueryCacheForIdentityChange()}restampQueuedIdentity(e,t){for(const[s,r]of this.queuedIdentities)r===e&&this.queuedIdentities.set(s,t);this.offlineQueue.restampIdentity(e,t)}restampWatermarks(e,t){const s=e??"",r=this.clientWatermarks.get(s);if(r===void 0)return;this.clientWatermarks.delete(s);const i=t??"",n=this.clientWatermarks.get(i);if(n===void 0)this.clientWatermarks.set(i,r);else for(const[a,c]of r)n.set(a,c)}clearQueryCacheForIdentityChange(){this.cacheFlushTimer!==void 0&&(clearTimeout(this.cacheFlushTimer),this.cacheFlushTimer=void 0),this.pendingCacheWrites.clear(),this.hydratedQueryCache.clear(),this.queryCache?.clear().catch(()=>{})}flushAllOfflineQueues(){for(const e of this.queuedOfflineShardKeys)this.flushOfflineQueue(e).catch(()=>{})}async flushOfflineQueue(e){const t=l(e),s=this.offlineFlushes.get(t);if(s===void 0&&this.offlineQueue.size===0)return;const r=(async()=>{await(s??Promise.resolve());try{await this.drainOfflineQueue(e)}catch{}})();this.offlineFlushes.set(t,r),await r,this.offlineFlushes.get(t)===r&&this.offlineFlushes.delete(t)}async drainOfflineQueue(e){const t=this.offlineQueue.drainConflict();for(const h of t){this.unpersist(h.id),this.queuedIdentities.delete(h.id??"");const d=new Error("offline mutation skipped: precondition failed before replay");d.code="OFFLINE_PRECONDITION_FAILED",this.emitItemSettled(h,"rejected",d)}const s=l(e),r=this.offlineQueue.drain(h=>l(h.shardKey)===s);if(r.length===0)return;const i=this.identityFingerprint(),n=[];for(const h of r)this.passesReplayIdentityGate(h,i)&&n.push(h);if(n.length===0)return;const a=this.encodableOrSettleTerminal(n);if(a.length===0)return;if(a.length===1){await this.replaySequential(a,e),this.scheduleRateLimitedRetry(e);return}const c=[];for(let h=0;h<a.length;h+=x){const d=a.slice(h,h+x),u=await this.replayBatched(d,e);if(c.push(...u.requeue),u.stop){c.push(...a.slice(h+x));break}}c.length>0&&this.offlineQueue.requeue(c),this.scheduleRateLimitedRetry(e)}noteReplayRetryDelay(e,t){const s=l(e),r=this.replayRetryState.get(s),i=(r?.attempts??0)+1,n=Ie(t,i);this.replayRetryState.set(s,{attempts:i,delayMs:n===void 0?r?.delayMs:Math.max(r?.delayMs??0,n)})}scheduleRateLimitedRetry(e){const t=l(e),s=this.replayRetryState.get(t),r=s?.delayMs;if(s===void 0||r===void 0||this.closed||this.offlineQueue.size===0){this.replayRetryState.delete(t);return}s.delayMs=void 0;const i=this.replayRetryTimers.get(t);i!==void 0&&clearTimeout(i),this.replayRetryTimers.set(t,setTimeout(()=>{this.replayRetryTimers.delete(t),this.flushOfflineQueue(e).catch(()=>{})},r))}encodableOrSettleTerminal(e){const t=[];for(const s of e)try{v(s.args,`args for '${s.functionPath}'`),t.push(s)}catch(r){this.settleReplayTerminal(s,r instanceof Error?r:new Error(String(r)))}return t}passesReplayIdentityGate(e,t){const s=e.id===void 0?void 0:this.queuedIdentities.get(e.id),r=s===void 0?e.identity:s;if(r!==void 0&&r!==t&&!this.isSameCredentialUnderTokenHash(r)){this.queuedIdentities.delete(e.id??""),this.unpersist(e.id);const i=new Error("offline mutation skipped: auth identity changed before replay");return i.code="OFFLINE_IDENTITY_CHANGED",e.reject(i),this.emitItemSettled(e,"rejected",i),!1}return this.queuedIdentities.delete(e.id??""),!0}settleReplaySuccess(e,t,s){this.unpersist(e.id),this.recordShardCursor(e.shardKey,s),e.onCommit?.(s),e.resolve(t),this.emitItemSettled(e,"committed")}cursorKeyFor(e){return e??this.defaultShardKey??""}learnDefaultShardKey(e,t){const s=e.headers.get("x-lunora-shard-key");if(s===null||t!==void 0||this.defaultShardKey===s)return;this.defaultShardKey=s;const r=this.shardCursors.get("");r!==void 0&&(this.shardCursors.set(s,Math.max(this.shardCursors.get(s)??0,r)),this.shardCursors.delete(""))}recordShardCursor(e,t){if(typeof t!="number")return;const s=this.cursorKeyFor(e);this.shardCursors.set(s,Math.max(this.shardCursors.get(s)??0,t))}settleReplayTerminal(e,t){this.unpersist(e.id),e.reject(t),this.emitItemSettled(e,"rejected",t)}async replaySequential(e,t){for(let s=0;s<e.length;s+=1){const r=e[s];if(r)try{let i;const n=await this.rpc(r.functionPath,r.args,r.shardKey,{captureBookmark:!0,clientId:r.clientId??this.clientId,mutationId:r.id,onCommitCursor:a=>{i=a}});this.settleReplaySuccess(r,n,i)}catch(i){if(!ke(i)){this.settleReplayTerminal(r,i);continue}this.noteReplayRetryDelay(t,i),this.offlineQueue.requeue(e.slice(s));return}}}async replayBatched(e,t){if(!this.fetchImpl)return{requeue:e,stop:!0};const s=JSON.stringify({calls:e.map((c,h)=>({args:v(c.args,`args for '${c.functionPath}'`),functionPath:c.functionPath,id:h,clientId:c.clientId??this.clientId,mutationId:c.id,shardKey:c.shardKey}))});if(e.length>1&&Re(s)>Oe)return await this.replayBatchedHalves(e,t);let r;try{r=await this.fetchImpl(C(this.url,G),{body:s,headers:this.rpcRequestHeaders({attachBookmark:!0}),method:"POST"})}catch{return{requeue:e,stop:!0}}const i=r.headers.get("x-d1-bookmark");if(i&&this.bookmark.set(i),r.status===413&&e.length>1)return await this.replayBatchedHalves(e,t);let n;const a=r.headers.get("retry-after");try{n=await r.json()}catch{return this.settleWholeBatchError(e,M(r.status,r.statusText,a),t)}if(!n.results){const c=n.error===void 0?M(r.status,r.statusText,a):Y(n.error,a);return this.settleWholeBatchError(e,c,t)}return{requeue:this.settleReplayBatchSlots(e,n.results,t),stop:!1}}settleWholeBatchError(e,t,s){if(t instanceof A||t.code!==void 0&&L.has(t.code))return this.noteReplayRetryDelay(s,t),{requeue:e,stop:!0};for(const r of e)this.settleReplayTerminal(r,t);return{requeue:[],stop:!1}}async replayBatchedHalves(e,t){const s=Math.ceil(e.length/2),r=await this.replayBatched(e.slice(0,s),t);if(r.stop)return{requeue:[...r.requeue,...e.slice(s)],stop:!0};const i=await this.replayBatched(e.slice(s),t);return{requeue:[...r.requeue,...i.requeue],stop:i.stop}}settleReplayBatchSlots(e,t,s){const r=new Map;for(const n of t)typeof n.id=="number"&&n.body!==void 0&&r.set(n.id,n.body);const i=[];for(const[n,a]of e.entries()){const c=r.get(n);c===void 0?i.push(a):"error"in c?L.has(c.error.code)?(this.noteReplayRetryDelay(s,_(c.error)),i.push(a)):this.settleReplayTerminal(a,_(c.error)):this.settleReplaySuccess(a,T(c.result),c.commitCursor)}return i}}export{se as LunoraClient};
|
package/dist/packem_shared/{SubscriptionRegistry-BQPU90ST.mjs → SubscriptionRegistry-BQTQZYoI.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{s as i}from"./wire-key-
|
|
1
|
+
import{s as i}from"./wire-key-CmT1XyQI.mjs";class r{static key(e,y,t){return`${e}::${i(y)}::${t??""}`}static keyOf(e){return`${e.fn.__lunoraRef}::${e.argsKey}::${e.shardKey??""}`}byKey=new Map;byId=new Map;get(e){return this.byKey.get(e)}getById(e){return this.byId.get(e)}add(e){this.byKey.set(r.keyOf(e),e),this.byId.set(e.id,e)}remove(e){const y=r.keyOf(e);this.byKey.get(y)===e&&this.byKey.delete(y),this.byId.delete(e.id)}all(){return[...this.byKey.values()]}clear(){this.byKey.clear(),this.byId.clear()}}export{r as SubscriptionRegistry};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a as e,i}from"./delta-merge-Ds6tHkFX.mjs";export{e as applyDelta,i as isMutationDelta};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{d,e as y}from"./wire-codec-
|
|
1
|
+
import{d,e as y}from"./wire-codec-BOMWQpoF.mjs";import{assertMaxEntries as u}from"./createInMemoryQueryCache-D2uNHLfM.mjs";import{s as f}from"./single-blob-store-DrhzObic.mjs";const m="lunora:query-cache",w=50,p=s=>{const c=u(s.maxEntries??w,"createAsyncStorageQueryCache"),t=f(s.storage,s.key??m),o=async()=>{const e=d(await t.read());return typeof e=="object"&&e!==null&&!Array.isArray(e)?new Map(Object.entries(e)):new Map},n=e=>t.write(y(Object.fromEntries(e))),i=e=>{if(e.size<=c)return;const a=[...e.values()].toSorted((r,l)=>r.ts-l.ts);for(const r of a){if(e.size<=c)break;e.delete(r.key)}};return{clear:t.clear,load:()=>t.serialize(async()=>[...(await o()).values()]),put:(e,a)=>t.serialize(async()=>{const r=await o();r.set(e,{...a,key:e}),i(r),await n(r)}),remove:e=>t.serialize(async()=>{const a=await o();a.delete(e)&&await n(a)})}};export{p as createAsyncStorageQueryCache};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{c as t}from"./local-store-irMi9H2G.mjs";import"./SubscriptionRegistry-BQTQZYoI.mjs";export{t as createLocalStore};
|
package/dist/packem_shared/{createServerClient-rFEbvMRr.mjs → createServerClient-CepVN0C6.mjs}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraClient as r}from"./LunoraClient-
|
|
1
|
+
import{LunoraClient as r}from"./LunoraClient-BoqhqR-g.mjs";const c=e=>{const t=new r({fetch:e.fetch,url:e.url});return e.token!==void 0&&t.setAuthToken(e.token),t};export{c as createServerClient};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{s as a}from"./wire-key-
|
|
1
|
+
import{s as a}from"./wire-key-CmT1XyQI.mjs";const c=(t,r,n,o)=>{const e=t.peekActiveQuerySnapshot(r.__lunoraRef,n,o),p=e.present?a(e.value):void 0;return()=>{const s=t.peekActiveQuerySnapshot(r.__lunoraRef,n,o);return!e.present||!s.present?!0:a(s.value)===p}};export{c as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{i as g}from"./wire-codec-
|
|
1
|
+
import{i as g}from"./wire-codec-BOMWQpoF.mjs";const c="_id",d="_creationTime",u="page",m="pageDelta",y=t=>{if(Array.isArray(t))return t;if(g(t)&&Array.isArray(t[u]))return t[u]},l=t=>{let e;for(const n of t){const o=n[d];if(typeof o=="number"){if(e!==void 0){if(o<e)return!0;if(o>e)return!1}e=o}}return!1},x=(t,e)=>{const n=e[d];if(typeof n!="number")return t.length;const o=l(t);for(const[f,s]of t.entries()){const r=s[d];if(typeof r=="number"&&(o?r<n:r>n))return f}return t.length},A=t=>{if(typeof t!="object"||t===null)return;const e=t[c];return typeof e=="string"?e:void 0},b=t=>{if(typeof t!="object"||t===null)return!1;const e=t;return typeof e.key=="string"&&typeof e.table=="string"&&(e.op==="insert"||e.op==="update"||e.op==="delete")},I=(t,e)=>{const n=[];for(const i of t){if(A(i)===void 0)return;n.push(i)}const{key:o,op:f,row:s}=e;if(f==="delete"){const i=n.filter(p=>p[c]!==o);return i.length===n.length?[...n]:i}if(s===void 0)return;const r=[...n],a=n.findIndex(i=>i[c]===o);return a===-1?r.splice(x(n,s),0,s):r[a]=s,r},E=(t,e)=>{const n=y(t);if(n===void 0)return;const o=I(n,e);return o===void 0||Array.isArray(t)?o:{...t,[u]:o}};export{m as P,E as a,b as i};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{SubscriptionRegistry as m}from"./SubscriptionRegistry-
|
|
1
|
+
import{SubscriptionRegistry as m}from"./SubscriptionRegistry-BQTQZYoI.mjs";const p=(r,o)=>{let e=r;for(const i of o)try{e=i.transform(e)}catch{}return e},u=(r,o)=>{if(o!==r.lastValue){r.lastValue=o;for(const e of r.callbacks)try{e(o)}catch{}}},y=(r,o)=>{let e;try{e=o(r.lastValue)}catch{return}const i={id:Symbol("optimistic"),transform:o};r.optimisticLayers.push(i),u(r,e);const c=()=>{const t=r.optimisticLayers.findIndex(s=>s.id===i.id);return t===-1?!1:(r.optimisticLayers.splice(t,1),!0)},a=()=>{u(r,p(r.serverBase,r.optimisticLayers))};return{confirm:t=>{if(t===void 0){c();return}i.commitCursor=t,r.serverCursor!==void 0&&r.serverCursor>=t&&c()&&a()},rollback:()=>{c()&&a()}}},h=(r,o)=>{if(o===void 0||r.optimisticLayers.length===0)return!1;const e=r.optimisticLayers.length;return r.optimisticLayers=r.optimisticLayers.filter(i=>i.commitCursor===void 0||i.commitCursor>o),r.optimisticLayers.length!==e},L=(r,o)=>{const e=[],i=[],c=(t,s)=>r.get(m.key(t,s,o));return{confirms:e,rollbacks:i,store:{getAllQueries:t=>{const s=[];for(const n of r.all())n.fn.__lunoraRef===t.__lunoraRef&&(n.shardKey??"")===(o??"")&&s.push({args:n.args,value:n.lastValue});return s},getQuery:(t,s)=>c(t.__lunoraRef,s??{})?.lastValue,setQuery:(t,s,n)=>{const f=c(t.__lunoraRef,s??{});if(!f)return;const l=y(f,()=>n);l&&(e.push(l.confirm),i.push(l.rollback))}}}};export{y as a,L as c,h as d,p as f,u as n};
|
package/dist/packem_shared/{lunora-client.d-6hU156nD.d.mts → lunora-client.d-Cb_0IT8r.d.mts}
RENAMED
|
@@ -1839,6 +1839,27 @@ declare class LunoraClient {
|
|
|
1839
1839
|
* signed-in user, the guard the queue path applies in `flushOfflineQueue`.
|
|
1840
1840
|
*/
|
|
1841
1841
|
currentIdentity(): string | null;
|
|
1842
|
+
/**
|
|
1843
|
+
* Verdict on whether a durable write stamped with `stamped` may be replayed
|
|
1844
|
+
* now. The comparison a replay handler must NOT hand-roll.
|
|
1845
|
+
*
|
|
1846
|
+
* `"match"` is the same identity, or the same credential under a token hash
|
|
1847
|
+
* — which a late-resolving subject would otherwise make look different.
|
|
1848
|
+
*
|
|
1849
|
+
* `"unknown"` is nobody signed in *yet*. A durable replay starts when the
|
|
1850
|
+
* executor is constructed, before the app has resolved its session and
|
|
1851
|
+
* called {@link setAuthToken}, so this is the normal state on every reload.
|
|
1852
|
+
* There is no other identity to replay as, so such a write must be HELD,
|
|
1853
|
+
* never dropped — dropping here destroys the queuing user's own offline
|
|
1854
|
+
* writes. It is indistinguishable from an explicit sign-out (the fingerprint
|
|
1855
|
+
* is `null` for both), which is the safe conflation: holding a write for a
|
|
1856
|
+
* signed-out app costs a retry, dropping it costs the write.
|
|
1857
|
+
*
|
|
1858
|
+
* `"mismatch"` is a different identity signed in, and is the one that must be
|
|
1859
|
+
* terminal: replaying would attribute one user's write to another and pass
|
|
1860
|
+
* THEIR row-level security.
|
|
1861
|
+
*/
|
|
1862
|
+
replayIdentityVerdict(stamped: null | string | undefined): "match" | "mismatch" | "unknown";
|
|
1842
1863
|
/** This client's stable identifier — the watermark key the server's custom-mutator protocol advances per `clientSeq`. */
|
|
1843
1864
|
clientIdentifier(): string;
|
|
1844
1865
|
/**
|
|
@@ -1839,6 +1839,27 @@ declare class LunoraClient {
|
|
|
1839
1839
|
* signed-in user, the guard the queue path applies in `flushOfflineQueue`.
|
|
1840
1840
|
*/
|
|
1841
1841
|
currentIdentity(): string | null;
|
|
1842
|
+
/**
|
|
1843
|
+
* Verdict on whether a durable write stamped with `stamped` may be replayed
|
|
1844
|
+
* now. The comparison a replay handler must NOT hand-roll.
|
|
1845
|
+
*
|
|
1846
|
+
* `"match"` is the same identity, or the same credential under a token hash
|
|
1847
|
+
* — which a late-resolving subject would otherwise make look different.
|
|
1848
|
+
*
|
|
1849
|
+
* `"unknown"` is nobody signed in *yet*. A durable replay starts when the
|
|
1850
|
+
* executor is constructed, before the app has resolved its session and
|
|
1851
|
+
* called {@link setAuthToken}, so this is the normal state on every reload.
|
|
1852
|
+
* There is no other identity to replay as, so such a write must be HELD,
|
|
1853
|
+
* never dropped — dropping here destroys the queuing user's own offline
|
|
1854
|
+
* writes. It is indistinguishable from an explicit sign-out (the fingerprint
|
|
1855
|
+
* is `null` for both), which is the safe conflation: holding a write for a
|
|
1856
|
+
* signed-out app costs a retry, dropping it costs the write.
|
|
1857
|
+
*
|
|
1858
|
+
* `"mismatch"` is a different identity signed in, and is the one that must be
|
|
1859
|
+
* terminal: replaying would attribute one user's write to another and pass
|
|
1860
|
+
* THEIR row-level security.
|
|
1861
|
+
*/
|
|
1862
|
+
replayIdentityVerdict(stamped: null | string | undefined): "match" | "mismatch" | "unknown";
|
|
1842
1863
|
/** This client's stable identifier — the watermark key the server's custom-mutator protocol advances per `clientSeq`. */
|
|
1843
1864
|
clientIdentifier(): string;
|
|
1844
1865
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-
|
|
1
|
+
import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-CyDr665D.js";
|
|
2
2
|
import { F as FunctionReference, A as ArgsOf, R as ReturnOf } from "./function-reference.d-Br_hsKje.js";
|
|
3
3
|
/**
|
|
4
4
|
* Run a query once on the server (during SSR) and capture its result in a
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-
|
|
1
|
+
import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-Cb_0IT8r.mjs";
|
|
2
2
|
import { F as FunctionReference, A as ArgsOf, R as ReturnOf } from "./function-reference.d-Br_hsKje.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Run a query once on the server (during SSR) and capture its result in a
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const d=e=>{let t="";for(let o=0;o<e.length;o+=32768)t+=String.fromCharCode(...e.subarray(o,o+32768));return btoa(t)},p=e=>{const t=atob(e),a=new Uint8Array(t.length);for(let o=0;o<t.length;o+=1)a[o]=t.codePointAt(o)??0;return a},i="$lunora.wire$";const w="__proto__",g={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},A={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},l=e=>{if(e===null||typeof e!="object")return!1;const t=Object.getPrototypeOf(e);return t===null||t===Object.prototype},u=(e,t=0)=>{if(t>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(e===void 0)return[i,"undefined"];if(e===null)return null;const a=typeof e;if(a==="bigint")return[i,"bigint",e.toString()];if(a==="number"){const r=e;return Number.isNaN(r)?[i,"nan"]:r===1/0?[i,"inf"]:r===-1/0?[i,"-inf"]:r}if(a!=="object")return e;if(e instanceof Date)return[i,"date",u(e.getTime(),t+1)];if(e instanceof Error){const r=e,n={};for(const s of Object.keys(r))r[s]!==void 0&&(n[s]=u(r[s],t+1));const c=[i,"error",r.name,r.message,n];return r.cause!==void 0&&c.push(u(r.cause,t+1)),c}if(e instanceof URL)return[i,"url",e.href];if(e instanceof Map)return[i,"map",[...e.entries()].map(([r,n])=>[u(r,t+1),u(n,t+1)])];if(e instanceof Set)return[i,"set",[...e].map(r=>u(r,t+1))];if(e instanceof ArrayBuffer)return[i,"bytes",d(new Uint8Array(e)),"ArrayBuffer"];if(ArrayBuffer.isView(e)){const r=e,n=r.constructor.name,c=new Uint8Array(r.buffer,r.byteOffset,r.byteLength);return n==="Uint8Array"?[i,"bytes",d(c)]:[i,"bytes",d(c),n]}if(Array.isArray(e)){const r=e.map(n=>u(n,t+1));return r.length>0&&r[0]===i?[i,"arr",r]:r}if(!l(e)){const r=e.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${r} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=e,f={};for(const r of Object.keys(o)){const n=o[r];if(n===void 0)continue;const c=u(n,t+1);r===w?Object.defineProperty(f,r,{configurable:!0,enumerable:!0,value:c,writable:!0}):f[r]=c}return f},y=(e,t=0)=>{if(t>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(e===null||typeof e!="object")return e;if(Array.isArray(e)){if(e[0]===i)switch(e[1]){case"-inf":return-1/0;case"arr":return e[2].map(r=>y(r,t+1));case"bigint":{const r=e[2];if(typeof r!="string"||r.length>1024||!/^-?\d+$/.test(r))throw new RangeError("wire-codec: invalid or over-long bigint (max 1024 digits)");return BigInt(r)}case"date":{const r=y(e[2],t+1);if(typeof r!="number")throw new TypeError("wire-codec: malformed date — epoch must be a number");return new Date(r)}case"map":{const r=e[2];return new Map(r.map(n=>{if(!Array.isArray(n)||n.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[y(n[0],t+1),y(n[1],t+1)]}))}case"set":return new Set(e[2].map(r=>y(r,t+1)));case"url":{const r=e[2];if(typeof r!="string")throw new TypeError("wire-codec: malformed url — href must be a string");return new URL(r)}case"error":{const r=e[2],n=e[3],c=(Object.hasOwn(A,r)?A[r]:void 0)??Error,s=new c(n);s.name!==r&&Object.defineProperty(s,"name",{configurable:!0,value:r,writable:!0});const b=y(e[4],t+1);if(b===null||typeof b!="object"||Array.isArray(b))throw new TypeError("wire-codec: malformed error — props must be an object");for(const m of Object.keys(b))m===w?Object.defineProperty(s,m,{configurable:!0,enumerable:!0,value:b[m],writable:!0}):s[m]=b[m];return e.length>5&&Object.defineProperty(s,"cause",{configurable:!0,value:y(e[5],t+1),writable:!0}),s}case"bytes":{const r=e[2];if(typeof r!="string")throw new TypeError("wire-codec: malformed bytes — payload must be a base64 string");const n=p(r),c=e[3]??"Uint8Array";if(c==="ArrayBuffer")return n.buffer.byteLength===n.byteLength?n.buffer:n.slice().buffer;const s=Object.hasOwn(g,c)?g[c]:void 0;return s?new s(n.slice().buffer):n}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return e.map(r=>y(r,t+1))}return e.map(f=>y(f,t+1))}const a=e,o={};for(const f of Object.keys(a)){const r=y(a[f],t+1);f===w?Object.defineProperty(o,f,{configurable:!0,enumerable:!0,value:r,writable:!0}):o[f]=r}return o};export{y as d,u as e,l as i};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{e as a}from"./wire-codec-
|
|
1
|
+
import{e as a}from"./wire-codec-BOMWQpoF.mjs";const b=/["\\\u0000-\u001F\uD800-\uDFFF]/,y=t=>b.test(t)?JSON.stringify(t):`"${t}"`,i=t=>{if(t===void 0)return"null";if(typeof t=="bigint")throw new TypeError("stableStringify: cannot use a bigint in a stable JSON cache key — pass it as a string, or use stableWireKey");if(typeof t=="number"){if(Number.isNaN(t))return"nan";if(t===1/0)return"inf";if(t===-1/0)return"-inf";if(Object.is(t,-0))return"-0"}if(typeof t=="string")return y(t);if(t===null||typeof t!="object")return JSON.stringify(t);if(Array.isArray(t)){let r="[";for(let e=0;e<t.length;e++)e>0&&(r+=","),r+=i(t[e]);return r+"]"}const o=Object.getPrototypeOf(t);if(o!==null&&o!==Object.prototype){const r=t.constructor?.name??"value";throw new TypeError(`stableStringify: cannot use a ${r} in a stable JSON cache key — only plain objects, arrays, and JSON primitives are supported (wire-typed values key via stableWireKey)`)}const s=t,c=Object.keys(s).sort();let n="{",f=!0;for(const r of c){const e=s[r];e!==void 0&&(f?f=!1:n+=",",n+=y(r),n+=":",n+=i(e))}return n+"}"},u=t=>i(a(t));export{u as s};
|
package/dist/query/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SubscriptionError, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-
|
|
1
|
+
import { S as SubscriptionError, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-Cb_0IT8r.mjs";
|
|
2
2
|
export type {
|
|
3
3
|
/**
|
|
4
4
|
* `@lunora/client/query` — the framework-neutral live-query state machine shared
|
|
@@ -10,7 +10,7 @@ export type {
|
|
|
10
10
|
* framework — each adapter supplies thin sinks that write into its own ref /
|
|
11
11
|
* store / signal / cache.
|
|
12
12
|
*/
|
|
13
|
-
b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-
|
|
13
|
+
b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-Cb_0IT8r.mjs";
|
|
14
14
|
import { F as FunctionReference, A as ArgsOf, R as ReturnOf } from "../packem_shared/function-reference.d-Br_hsKje.mjs";
|
|
15
15
|
import '@lunora/errors';
|
|
16
16
|
import '@lunora/runtime';
|
package/dist/query/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SubscriptionError, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-
|
|
1
|
+
import { S as SubscriptionError, L as LunoraClient, a as Unsubscribe } from "../packem_shared/lunora-client.d-CyDr665D.js";
|
|
2
2
|
export type {
|
|
3
3
|
/**
|
|
4
4
|
* `@lunora/client/query` — the framework-neutral live-query state machine shared
|
|
@@ -10,7 +10,7 @@ export type {
|
|
|
10
10
|
* framework — each adapter supplies thin sinks that write into its own ref /
|
|
11
11
|
* store / signal / cache.
|
|
12
12
|
*/
|
|
13
|
-
b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-
|
|
13
|
+
b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-CyDr665D.js";
|
|
14
14
|
import { F as FunctionReference, A as ArgsOf, R as ReturnOf } from "../packem_shared/function-reference.d-Br_hsKje.js";
|
|
15
15
|
import '@lunora/errors';
|
|
16
16
|
import '@lunora/runtime';
|
package/dist/service.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{LunoraError as u}from"@lunora/errors";import{e as w,d as f}from"./packem_shared/wire-codec-
|
|
1
|
+
import{LunoraError as u}from"@lunora/errors";import{e as w,d as f}from"./packem_shared/wire-codec-BOMWQpoF.mjs";const v="/_lunora/rpc",$="https://lunora.internal",N=e=>{const t=new Error(e.message??"request failed");return t.code=e.code,e.data!==void 0&&(t.data=f(e.data)),e.hint!==void 0&&(t.hint=e.hint),e.docsUrl!==void 0&&(t.docsUrl=e.docsUrl),t},h=async(e,t,r,a,l)=>{const s=r.__lunoraRef;let d;try{d=JSON.stringify({args:w(a??{}),functionPath:s,...l?.shardKey===void 0?{}:{shardKey:l.shardKey}})}catch(o){const p=o instanceof Error?o.message:String(o);throw new TypeError(`@lunora/client/service: cannot encode args for '${s}' — ${p}`,o instanceof Error?{cause:o}:void 0)}const n=await e.fetch(new Request(`${$}${v}`,{body:d,headers:{"content-type":"application/json"},method:"POST"}));let i;try{i=await n.json()}catch{throw new u("INTERNAL",`@lunora/client/service: ${t} "${s}" got a non-JSON response (status ${n.status.toString()}). Check the service binding points at the Lunora Worker that declares this function.`,{status:n.status})}if(i===null||typeof i!="object")throw new u("INTERNAL",`@lunora/client/service: ${t} "${s}" returned a JSON body that is not an object (status ${n.status.toString()}). Check the service binding points at the Lunora Worker that declares this function.`,{status:n.status});const c=i;if("error"in c)throw N(c.error);if(!n.ok)throw new u("INTERNAL",`@lunora/client/service: ${t} "${s}" failed with status ${n.status.toString()} and no error envelope.`,{status:n.status});return f(c.result)},E=e=>({action:async(t,r,a)=>await h(e,"action",t,r,a),mutation:async(t,r,a)=>await h(e,"mutation",t,r,a),query:async(t,r,a)=>await h(e,"query",t,r,a)});export{v as RPC_PATH,E as createServiceClient};
|
package/dist/ssr/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-
|
|
1
|
+
import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-Cb_0IT8r.mjs";
|
|
2
2
|
export type { A as ArgsOf, F as FunctionReference, R as ReturnOf } from "../packem_shared/function-reference.d-Br_hsKje.mjs";
|
|
3
3
|
export {
|
|
4
4
|
/**
|
|
@@ -20,7 +20,7 @@ p as preloadQuery,
|
|
|
20
20
|
* WebSocket or touches a UI framework, so it is safe to import from any SSR
|
|
21
21
|
* loader.
|
|
22
22
|
*/
|
|
23
|
-
a as preloadedQueryResult } from "../packem_shared/preload.d-
|
|
23
|
+
a as preloadedQueryResult } from "../packem_shared/preload.d-BrV8gNAa.mjs";
|
|
24
24
|
import '@lunora/errors';
|
|
25
25
|
import '@lunora/runtime';
|
|
26
26
|
/**
|
package/dist/ssr/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-
|
|
1
|
+
import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-CyDr665D.js";
|
|
2
2
|
export type { A as ArgsOf, F as FunctionReference, R as ReturnOf } from "../packem_shared/function-reference.d-Br_hsKje.js";
|
|
3
3
|
export {
|
|
4
4
|
/**
|
|
@@ -20,7 +20,7 @@ p as preloadQuery,
|
|
|
20
20
|
* WebSocket or touches a UI framework, so it is safe to import from any SSR
|
|
21
21
|
* loader.
|
|
22
22
|
*/
|
|
23
|
-
a as preloadedQueryResult } from "../packem_shared/preload.d-
|
|
23
|
+
a as preloadedQueryResult } from "../packem_shared/preload.d-8LuiLYlj.js";
|
|
24
24
|
import '@lunora/errors';
|
|
25
25
|
import '@lunora/runtime';
|
|
26
26
|
/**
|
package/dist/ssr/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{getServerSession as o}from"../packem_shared/getServerSession-BaOoo1p6.mjs";import{deserializePreloaded as l,serializePreloaded as t}from"../packem_shared/deserializePreloaded-CHfOFpka.mjs";import{createServerClient as i}from"../packem_shared/createServerClient-
|
|
1
|
+
import{getServerSession as o}from"../packem_shared/getServerSession-BaOoo1p6.mjs";import{deserializePreloaded as l,serializePreloaded as t}from"../packem_shared/deserializePreloaded-CHfOFpka.mjs";import{createServerClient as i}from"../packem_shared/createServerClient-CepVN0C6.mjs";import{preloadQuery as s,preloadedQueryResult as f}from"../packem_shared/preloadQuery-uFy24PCR.mjs";export{i as createServerClient,l as deserializePreloaded,o as getServerSession,s as preloadQuery,f as preloadedQueryResult,t as serializePreloaded};
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{LunoraError as m}from"@lunora/errors";import{i as ie,a as ne,P as ae}from"./delta-merge-BPM4sFID.mjs";import{e as W,d as T}from"./wire-codec-BfIZrTJe.mjs";import{s as q}from"./wire-key-C9UKT-C3.mjs";import oe from"./createInMemoryBookmarkStorage-BooZhW0n.mjs";import{ClientQueryStore as ce}from"./createClientQuery-B8Nfj-7o.mjs";import{TabCoordinator as he}from"./TabCoordinator-CXD7vulJ.mjs";import{TransportError as A,getRetryAfterMs as X}from"./CONFLICT_ERROR_CODE-BOeeP_Wl.mjs";import{httpStream as de}from"./httpStream-x6q-x4yK.mjs";import{d as U,n as O,f as R,a as ue,c as le}from"./local-store-CNgeGW02.mjs";import{O as fe,n as $,i as me,r as pe}from"./offline-queue-xntsWiKt.mjs";import{resolvePersistenceAdapter as ye}from"./createInMemoryPersistence-Cz2W6XVj.mjs";import{resolveQueryCacheAdapter as Se,queryCacheKey as P}from"./createInMemoryQueryCache-D2uNHLfM.mjs";import{createReconnect as Q}from"./createReconnect-CjTmjJDH.mjs";import be from"./createSnapshotPrecondition-C4bSgHkg.mjs";import{createStream as Te}from"./DEFAULT_MAX_BUFFER-D3QH2iaq.mjs";import{SubscriptionRegistry as k}from"./SubscriptionRegistry-BQPU90ST.mjs";const x=500,ve=async o=>{const e=[];let t;for(;;){const s=await o(t);if(e.push(...Array.isArray(s.records)?s.records:[]),s.truncated!==!0||typeof s.cursor!="string"||s.cursor.length===0)return e;if(s.cursor===t)throw new Error("collectPages: the list did not advance its cursor — refusing to page forever");t=s.cursor}},ge=(o,e)=>{if(o.size<e)return;const t=o.keys().next().value;t!==void 0&&o.delete(t)};class w{listeners=new Set;add(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}emit(...e){const[t]=e;for(const s of this.listeners)try{s(t)}catch{}}clear(){this.listeners.clear()}}const L=new Set(["RATE_LIMITED","SHARD_ERROR","SHARD_UNAVAILABLE","TOO_MANY_REQUESTS"]),ke=o=>{if(o instanceof A)return!0;const e=o?.code;return e===void 0||L.has(e)},Z=6e4,Ce=1e3,ee=o=>{if(o===null)return;const e=Number(o);if(Number.isFinite(e))return e>0?e*1e3:void 0;const t=Date.parse(o);if(Number.isNaN(t))return;const s=t-Date.now();return s>0?s:void 0},we={401:"UNAUTHORIZED",403:"FORBIDDEN",404:"NOT_FOUND",413:"PAYLOAD_TOO_LARGE",429:"TOO_MANY_REQUESTS"},M=(o,e,t)=>{const s=ee(t),r=s===void 0?void 0:{retryAfterMs:s},i=`LunoraClient: response carried no error envelope (status ${o.toString()}${e?` ${e}`:""})`;return o<400||o>=500||o===408?new A(i,r):new m(we[o]??"BAD_REQUEST",i,{data:r})},Ee=(o,e)=>{const t=ee(e);if(!(t===void 0||X(o)!==void 0))return o.data===void 0?{retryAfterMs:t}:typeof o.data=="object"&&o.data!==null&&!Array.isArray(o.data)?{...o.data,retryAfterMs:t}:void 0},_e=(o,e=Math.random)=>{const t=Math.min(Ce*2**Math.max(o-1,0),Z);return Math.round(t*(.5+e()*.5))},Ae=o=>o instanceof A||L.has(o?.code??""),Ie=(o,e,t)=>{const s=X(o);return s!==void 0&&s>0?Math.min(s,Z):Ae(o)?_e(e,t):void 0},Oe=983040,Re=o=>typeof TextEncoder>"u"?o.length:new TextEncoder().encode(o).length,Pe="/_lunora/rpc",G="/_lunora/rpc-batch",Me="/_lunora/ws",N=o=>o===void 0||o===""?"":`&bucket=${encodeURIComponent(o)}`,H=o=>{for(let e=o.length-1;e>=0;e-=1)o[e]?.()},z=Symbol("lunora.unmergeableDelta"),He=(o,e)=>{for(const t of e)t.op==="delete"?o.delete(t.key):t.value!==void 0&&o.set(t.key,t.value)},Le="lunora-ping",Fe=3e4,Ue=1e4,xe=250,Ne=5e3,De=64,Be="/_lunora/admin/shard-traffic",Ke="/_lunora/admin/scheduled",We="/_lunora/admin/scheduled/status",qe="/_lunora/admin/scheduled/ws",$e="/_lunora/admin/scheduled/cancel",V="/_lunora/admin/scheduled/dead",Qe="/_lunora/admin/scheduled/dead/retry",Ge="/_lunora/admin/scheduled/dead/cancel",ze="/_lunora/admin/workflows/instances",Ve="/_lunora/admin/workflows/instance",je="/_lunora/admin/workflows/status",D="/_lunora/admin/storage",Je="/_lunora/admin/storage/url",Ye="/_lunora/admin/storage/buckets",Xe="/_lunora/admin/functions",Ze="/_lunora/admin/cron-jobs",et="/_lunora/admin/cron-jobs/run",tt="/_lunora/admin/openapi",st="/_lunora/admin/openrpc",rt="/_lunora/admin/global/tables",it="/_lunora/admin/global/table",nt="/_lunora/admin/global/facet",at="/_lunora/admin/vector/indexes",ot="/_lunora/admin/vector/query",ct="/_lunora/admin/logs/archive",ht="/_lunora/admin/kv/namespaces",dt="/_lunora/admin/kv/keys",B="/_lunora/admin/kv/value",ut="/_lunora/admin/auth/users",lt="/_lunora/admin/auth/sessions",ft="/_lunora/admin/auth/users/create",mt="/_lunora/admin/auth/users/role",pt="/_lunora/admin/auth/users/ban",yt="/_lunora/admin/auth/users/unban",St="/_lunora/admin/auth/users/password",bt="/_lunora/admin/auth/users/remove",Tt="/_lunora/admin/auth/users/impersonate",vt="/_lunora/admin/auth/sessions/revoke",gt="/_lunora/admin/auth/sessions/revoke-all",kt="/_lunora/admin/auth/capabilities",Ct="/_lunora/admin/auth/users/update",wt="/_lunora/admin/auth/accounts",Et="/_lunora/admin/auth/accounts/unlink",_t="/_lunora/admin/auth/passkeys",At="/_lunora/admin/auth/passkeys/delete",It="/_lunora/admin/auth/two-factor/disable",Ot="/_lunora/admin/auth/organizations",Rt="/_lunora/admin/auth/organizations/members",Pt="/_lunora/admin/auth/organizations/invitations",Mt="/_lunora/admin/auth/organizations/members/remove",Ht="/_lunora/admin/auth/organizations/invitations/cancel",Lt="/_lunora/admin/auth/config",Ft="/_lunora/admin/auth/organizations/create",Ut="/_lunora/admin/auth/organizations/update",xt="/_lunora/admin/auth/organizations/remove",Nt="/_lunora/admin/auth/organizations/members/add",Dt="/_lunora/admin/auth/organizations/members/invite",Bt="/_lunora/admin/auth/organizations/members/role",Kt="/_lunora/admin/auth/organizations/teams",Wt="/_lunora/admin/auth/organizations/teams/create",qt="/_lunora/admin/auth/organizations/teams/update",$t="/_lunora/admin/auth/organizations/teams/remove",Qt="/_lunora/admin/auth/organizations/teams/members",Gt="/_lunora/admin/auth/organizations/teams/members/add",zt="/_lunora/admin/auth/organizations/teams/members/remove",Vt="/_lunora/admin/auth/organizations/roles",jt="/_lunora/admin/auth/organizations/roles/create",Jt="/_lunora/admin/auth/organizations/roles/update",Yt="/_lunora/admin/auth/organizations/roles/remove",Xt="/api/auth",Zt="/get-session",j=o=>o.startsWith("https://")?`wss://${o.slice(8)}`:o.startsWith("http://")?`ws://${o.slice(7)}`:o,C=(o,e)=>`${o.endsWith("/")?o.slice(0,-1):o}${e}`,y=(o,e)=>{const t=new URLSearchParams;for(const[r,i]of Object.entries(e))i!==void 0&&i!==""&&t.set(r,String(i));const s=t.toString();return s===""?o:`${o}?${s}`},es=[ae],l=o=>o??"",te=(o,e)=>{const t=o.error,s=typeof t?.code=="string"?t.code:void 0,r=typeof t?.message=="string"?t.message:void 0;return{code:s,messageText:(typeof o.message=="string"?o.message:void 0)??r??e}},ts=o=>{const{code:e,messageText:t}=te(o,"stream error");return e===void 0?new Error(t):new m(e,t)},J=o=>{const{code:e,messageText:t}=te(o,"subscription error");return{message:t,...e===void 0?{}:{code:e}}};function K(o){if(o!==void 0)return e=>{o(e)}}const E=(o,e)=>{for(const t of o)try{t(e)}catch{}},ss=new TextDecoder,rs=o=>{if(typeof o=="string")return o;if(o instanceof ArrayBuffer)return ss.decode(o)},b=(o,e)=>{if(!o.socket||o.wsState!=="open")return!1;try{return o.socket.send(JSON.stringify(e)),!0}catch{return!1}},_=o=>{const e=new Error(o.message??"request failed");return e.code=o.code,o.data!==void 0&&(e.data=T(o.data)),o.hint!==void 0&&(e.hint=o.hint),o.docsUrl!==void 0&&(e.docsUrl=o.docsUrl),e},Y=(o,e)=>{const t=_(o),s=Ee(t,e);return s!==void 0&&(t.data=s),t},v=(o,e)=>{try{return W(o)}catch(t){const s=t instanceof Error?t.message:String(t);throw new TypeError(`LunoraClient: cannot encode ${e} — ${s}`,t instanceof Error?{cause:t}:void 0)}},is=(o,e)=>{const t=Array.from({length:e});for(const s of o){if(typeof s.id!="number"||s.id<0||s.id>=e)continue;const r=s.body;t[s.id]=r&&"error"in r&&r.error?{error:_(r.error),ok:!1}:{ok:!0,value:T(r?.result)}}return t.map(s=>s??{error:new Error("batch call returned no result"),ok:!1})},ns=Promise.resolve();class se{static MAX_POKE_BUFFERS=256;static createClientQuery(e,t){return{defaultValue:t,key:e}}url;wsUrl;clientQueryStore;wsToken;authBasePath;fetchImpl;WebSocketImpl;bookmark;reconnectOptions;connectTimeoutMs;heartbeatIntervalMs;offlineQueue;outbox;clientId;shardCursors=new Map;defaultShardKey;readyResolved=!1;readyResolve;readyPromise;clientWatermarks=new Map;outboxMutationCounter=0;onPersistenceError;persistence;persistenceVersion;outboxLeaderRelease;queryCache;hydratedQueryCache=new Map;pendingCacheWrites=new Map;cacheFlushTimer;subscriptions=new k;tabCoordinator;leaderStatus;leaderWasEverConnected=!1;connections=new Map;defaultConnectionContext;connectionContexts=new Map;connectionContextHolders=new Map;authToken=null;authSubject=void 0;queuedIdentities=new Map;queuedOfflineShardKeys=new Set;closed=!1;authTokenListeners=new w;statusListeners=new w;tokenExpiredListeners=new w;mutationSettledListeners=new w;pendingChangeListeners=new w;whisperHandlers=new Map;lastStatus="idle";nextSubId=0;nextStreamId=0;streams=new Map;adminSocketTeardowns=new Set;offlineFlushes=new Map;replayRetryState=new Map;replayRetryTimers=new Map;shapeSubscriptions=new Map;pokeBuffers=new Map;nextShapeId=0;constructor(e){this.url=e.url,this.wsUrl=e.wsUrl??C(j(e.url),Me),this.wsToken=e.wsToken;const t=e.authBasePath??Xt;this.authBasePath=t.endsWith("/")?t.slice(0,-1):t,this.fetchImpl=e.fetch??(typeof fetch=="function"?fetch.bind(globalThis):void 0),this.WebSocketImpl=e.WebSocket??(typeof WebSocket=="function"?WebSocket:void 0),this.bookmark=e.bookmarkStorage??oe(),this.reconnectOptions=e.reconnect,this.heartbeatIntervalMs=e.heartbeatIntervalMs??Fe,this.connectTimeoutMs=e.connectTimeoutMs??Ue,this.defaultConnectionContext=e.connectionContext,this.persistence=ye(e.persistence,e.outbox===void 0),this.persistenceVersion=e.persistenceVersion,this.queryCache=Se(e.queryCache),this.onPersistenceError=e.offlineQueue?.onPersistenceError,this.clientQueryStore=new ce,e.crossTabSync?(this.tabCoordinator=this.createTabCoordinator(),this.tabCoordinator.start()):this.tabCoordinator=void 0,e.hydrateOnStart&&this.queryCache?this.readyPromise=new Promise(s=>{this.readyResolve=s}):(this.readyResolved=!0,this.readyPromise=ns),this.offlineQueue=new fe(e.offlineQueue,{onEvict:(s,r)=>{this.emitItemSettled(s,"rejected",r)},onSizeChange:s=>{this.pendingChangeListeners.emit(s)},persistence:this.persistence,version:e.persistenceVersion}),this.outbox=e.outbox,this.clientId=e.clientId??`client-${$()}`,this.persistence&&queueMicrotask(()=>{this.hydrateAsOutboxLeader()}),this.queryCache&&queueMicrotask(()=>{(async()=>{try{await this.hydrateQueryCache()}catch{}finally{this.readyResolved=!0,this.readyResolve?.()}})().catch(()=>{})})}setAuthToken(e,t){const s=this.authToken!==e,r=this.identityFingerprint();this.authToken=e,t!==void 0&&(this.authSubject=t);const i=this.identityFingerprint();if(i!==r&&(s?this.rejectQueuedForIdentityChange():(this.restampQueuedIdentity(r,i),this.restampWatermarks(r,i)),this.tabCoordinator)){const n=this.tabCoordinator.isLeader();this.tabCoordinator.stop(),this.tabCoordinator=this.createTabCoordinator(),this.tabCoordinator.start(),n&&this.tabCoordinator.promoteImmediately()}s&&this.authTokenListeners.emit(e)}getAuthToken(){return this.authToken}currentIdentity(){return this.identityFingerprint()}clientIdentifier(){return this.clientId}confirmedMutationWatermark(e){return this.clientWatermarks.get(this.identityFingerprint()??"")?.get(e??"")??0}async callMutator(e,t,s){const r=s?.clientSeq;if(r!==void 0&&(!Number.isInteger(r)||r<=0))throw new m("INTERNAL",`callMutator: clientSeq must be a positive integer, got ${String(r)}`);const i=s?.shardKey??"",n=this.identityFingerprint();let a;const c=await this.rpc(e,t,s?.shardKey,{captureBookmark:!0,clientId:this.clientId,clientSeq:r,onMutationAck:d=>{a=d}});if(a!==void 0&&a>(this.clientWatermarks.get(n??"")?.get(i)??0)){let d=this.clientWatermarks.get(n??"");d===void 0&&(d=new Map,this.clientWatermarks.set(n??"",d)),d.set(i,a)}return{applied:a===void 0||a===r,result:c}}onAuthTokenChange(e){return this.authTokenListeners.add(e)}async getCurrentUser(){if(this.closed||!this.fetchImpl)return null;const e={};this.authToken&&(e.authorization=`Bearer ${this.authToken}`);try{const t=await this.fetchImpl(C(this.url,`${this.authBasePath}${Zt}`),{credentials:"include",headers:e,method:"GET"});return t.ok?(await t.json())?.user??null:null}catch{return null}}setWsToken(e){if(this.wsToken!==e){this.wsToken=e;for(const t of this.connections.values())if(t.socket)try{t.socket.close()}catch{}}}setConnectionContext(e,t={}){this.assertLeaderOwnedSurface("setConnectionContext");const s=l(t.shardKey);e===void 0?this.connectionContexts.delete(s):this.connectionContexts.set(s,e),this.refreshConnectionContext(s)}acquireConnectionContext(e,t={}){if(this.followsAnotherTab())return()=>{};const s=l(t.shardKey),r={context:e},i=this.connectionContextHolders.get(s);i?i.push(r):this.connectionContextHolders.set(s,[r]),this.refreshConnectionContext(s);let n=!1;return()=>{if(n)return;n=!0;const a=this.connectionContextHolders.get(s);if(!a)return;const c=a.indexOf(r);c!==-1&&a.splice(c,1),a.length===0&&this.connectionContextHolders.delete(s),this.refreshConnectionContext(s)}}whisperSubscribe(e,t,s={}){this.assertLeaderOwnedSurface("whisperSubscribe");const r=l(s.shardKey);let i=this.whisperHandlers.get(r);i||(i=new Map,this.whisperHandlers.set(r,i));let n=i.get(e);const a=n===void 0;if(n||(n=new Set,i.set(e,n)),n.add(t),this.ensureSocket(s.shardKey),a){const c=this.getConnection(s.shardKey);c&&b(c,{topic:e,type:"whisper_subscribe"})}return()=>{const c=this.whisperHandlers.get(r),h=c?.get(e);if(!h?.delete(t)||h.size>0)return;c?.delete(e),c?.size===0&&this.whisperHandlers.delete(r);const d=this.getConnection(s.shardKey);d&&b(d,{topic:e,type:"whisper_unsubscribe"})}}whisper(e,t,s={}){this.assertLeaderOwnedSurface("whisper"),this.ensureSocket(s.shardKey);const r=this.getConnection(s.shardKey);r&&b(r,{data:v(t??null,`whisper data for topic '${e}'`),topic:e,type:"whisper"})}onTokenExpired(e){return this.tokenExpiredListeners.add(e)}connectionStatus(){return this.computeStatus()}onConnectionStatus(e){const t=this.statusListeners.add(e);return e(this.computeStatus()),t}pendingCount(){return this.offlineQueue.size}debug(){const e=[],t=this.clientWatermarks.get(this.identityFingerprint()??""),s=new Set([...this.connections.keys(),...t?.keys()??[]]);for(const i of s){const n=this.connections.get(i);e.push({confirmedMutationWatermark:t?.get(i)??0,hasSocket:n?.socket!==void 0,shardKey:i===""?void 0:i,wasEverConnected:n?.wasEverConnected??!1,wsState:n?.wsState??"idle"})}const r=this.subscriptions.all().map(i=>({acked:i.acked,functionPath:i.fn.__lunoraRef,id:i.id,kind:"query",lastMutationId:i.lastMutationId,pendingOptimisticLayers:i.optimisticLayers.length,serverCursor:i.serverCursor,shardKey:i.shardKey,subscriberCount:i.callbacks.size}));for(const i of this.shapeSubscriptions.values())r.push({acked:i.serverCursor!==void 0,functionPath:`shape:${i.name}`,id:i.id,kind:"shape",lastMutationId:i.lastMutationId,pendingOptimisticLayers:0,rowCount:i.rows.size,serverCursor:i.serverCursor,shardKey:i.shardKey,subscriberCount:i.callbacks.size});return{clientId:this.clientId,closed:this.closed,connectionStatus:this.computeStatus(),pendingWrites:this.offlineQueue.size,shards:e.toSorted((i,n)=>(i.shardKey??"").localeCompare(n.shardKey??"")),subscriptions:r.toSorted((i,n)=>i.functionPath.localeCompare(n.functionPath))}}onPendingChange(e){const t=this.pendingChangeListeners.add(e);return e(this.offlineQueue.size),t}onMutationSettled(e){return this.mutationSettledListeners.add(e)}getWebSocketImpl(){return this.WebSocketImpl}getClientQuery(e){return this.clientQueryStore.get(e)}setClientQuery(e,t){this.clientQueryStore.set(e,t)}subscribeClientQuery(e,t){return this.clientQueryStore.subscribe(e,t)}resetClientQuery(e){this.clientQueryStore.reset(e)}snapshotPrecondition(e,t,s){return be(this,e,t,s)}whenReady(){return this.readyPromise}get isReady(){return this.readyResolved}peekHydratedQuery(e,t,s){if(!this.readyResolved)return;const r=q(t),i=P(e,r,s),n=this.hydratedQueryCache.get(i);if(n!==void 0)return n.identity===this.identityFingerprint()?n.value:void 0}peekActiveQuerySnapshot(e,t,s){const r=k.key(e,t,s),i=this.subscriptions.get(r);return{present:i!==void 0,value:i?.lastValue}}async query(e,t,s={}){return this.assertOpen(),await this.rpc(e.__lunoraRef,t,s.shardKey,{attachBookmark:!0})}async batch(e){if(this.assertOpen(),!this.fetchImpl)throw new m("INTERNAL","LunoraClient: no `fetch` implementation available");if(e.length===0)return[];const t=await this.fetchImpl(C(this.url,G),{body:JSON.stringify({calls:e.map((i,n)=>({args:v(i.args??{},`args for batch call '${i.fn.__lunoraRef}'`),functionPath:i.fn.__lunoraRef,id:n,shardKey:i.shardKey}))}),headers:this.rpcRequestHeaders({attachBookmark:!0}),method:"POST"}),s=t.headers.get("x-d1-bookmark");s&&this.bookmark.set(s);let r;try{r=await t.json()}catch{throw new m("INTERNAL",`LunoraClient: batch response was not JSON (status ${t.status.toString()})`)}if(!t.ok||r.error&&!r.results)throw r.error?_(r.error):new m("INTERNAL",`LunoraClient: batch request failed (status ${t.status.toString()})`);for(const i of r.results??[]){const n=i.body?.commitCursor;typeof i.id=="number"&&typeof n=="number"&&this.recordShardCursor(e[i.id]?.shardKey,n)}return is(r.results??[],e.length)}async mutation(e,t,s={}){this.assertOpen();const r=t,i=s.mutationId??$(),{confirms:n,rollbacks:a}=this.applyOptimisticUpdates(e.__lunoraRef,r,s.shardKey,s.optimistic);s.optimisticUpdate&&this.applyOptimisticUpdate(s.optimisticUpdate,t,s.shardKey,a,n);const c=this.offlineFlushes.get(l(s.shardKey));c!==void 0&&await c;const{hasSocket:h,wasEverConnected:d,wsState:u}=this.connectionGateState(s.shardKey),{queueBeforeFirstConnect:f}=this.offlineQueue,S=d||f,p=this.WebSocketImpl!==void 0&&S;if(u!=="open"&&!h&&p||u==="connecting"&&S)return this.enqueueOfflineMutation(e,r,s.shardKey,i,a,n,s.precondition);try{let I;const re=await this.rpc(e.__lunoraRef,r,s.shardKey,{captureBookmark:!0,mutationId:i,onCommitCursor:F=>{I=F}});for(const F of n)F(I);return re}catch(I){throw H(a),I}}async action(e,t,s={}){return this.assertOpen(),await this.rpc(e.__lunoraRef,t,s.shardKey,{attachBookmark:!0,captureBookmark:!0})}async importRows(e,t,s={}){this.assertOpen();const r=Math.max(1,Math.trunc(s.chunkSize??500)),i=s.toArgs??(h=>({rows:h})),n=t.length;let a=0,c=0;for(let h=0;h<n;h+=r){const d=t.slice(h,h+r),u=c;await this.mutation(e,i(d),{...s.importId===void 0?{}:{mutationId:`${s.importId}:${String(u)}`},shardKey:s.shardKey}),c+=1,a+=d.length,s.onProgress?.({done:a,total:n})}return{chunks:c,imported:a}}async shardTraffic(e){this.assertOpen();const t=await this.adminFetch(Be,"POST",{table:e});return{failed:t.failed??0,ok:t.ok??0,shards:t.shards??[]}}async listScheduledJobs(){return this.assertOpen(),(await this.adminFetch(Ke,"GET")).records??[]}async schedulerStatus(){this.assertOpen();const e=await this.adminFetch(We,"GET");return{backlog:e.backlog??0,inFlight:e.inFlight??0,pools:e.pools??[]}}async cancelScheduledJob(e){return this.assertOpen(),{cancelled:(await this.adminFetch($e,"POST",{id:e})).cancelled===!0}}async listDeadJobs(){return this.assertOpen(),await ve(async e=>await this.adminFetch(e===void 0?V:`${V}?cursor=${encodeURIComponent(e)}`,"GET"))}async retryDeadJob(e){return this.assertOpen(),{retried:(await this.adminFetch(Qe,"POST",{id:e})).retried===!0}}async removeDeadJob(e){return this.assertOpen(),{removed:(await this.adminFetch(Ge,"POST",{id:e})).removed===!0}}async listWorkflowInstances(e){this.assertOpen();const t=new URLSearchParams({name:e.name});e.status!==void 0&&t.set("status",e.status),e.page!==void 0&&t.set("page",String(e.page)),e.perPage!==void 0&&t.set("perPage",String(e.perPage));const s=await this.adminFetch(`${ze}?${t.toString()}`,"GET");return{configured:s.configured,instances:s.instances??[],page:s.page??1,perPage:s.perPage??e.perPage??0,totalCount:s.totalCount}}async getWorkflowInstance(e){this.assertOpen();const t=new URLSearchParams({id:e.id,name:e.name}),s=await this.adminFetch(`${Ve}?${t.toString()}`,"GET");return{createdOn:s.createdOn,endedOn:s.endedOn,error:s.error,id:s.id??e.id,output:s.output,params:s.params,startedOn:s.startedOn,status:s.status??"unknown",steps:s.steps??[]}}async setWorkflowInstanceStatus(e){return this.assertOpen(),{status:(await this.adminFetch(je,"POST",{action:e.action,id:e.id,name:e.name})).status??"unknown"}}subscribeScheduledJobs(e){if(this.assertOpen(),this.WebSocketImpl===void 0)return()=>{};const t=C(j(this.url),qe),s=Q(this.reconnectOptions),r={connectTimer:void 0,heartbeatTimer:void 0,lastFrameAt:0,socket:void 0};let i,n=!1;const a=()=>{n||this.closed||(i=setTimeout(d,s.next()))},c=f=>{if(n||this.WebSocketImpl===void 0)return;const S=f===void 0?t:`${t}?token=${encodeURIComponent(f)}`;this.openManagedSocket(r,S,this.connectTimeoutMs,{onClose:()=>{a()},onMessage:p=>{try{const g=JSON.parse(typeof p.data=="string"?p.data:"");g.type==="jobs"&&Array.isArray(g.records)&&(s.reset(),e(g.records))}catch{}}})},h=async f=>{let S;try{S=await f()}catch{a();return}c(S)},d=()=>{if(n||this.WebSocketImpl===void 0)return;const{wsToken:f}=this;if(typeof f=="function"){h(f).catch(()=>{});return}c(f)};d();const u=()=>{n=!0,i!==void 0&&(clearTimeout(i),i=void 0),r.connectTimer!==void 0&&(clearTimeout(r.connectTimer),r.connectTimer=void 0),this.stopHeartbeat(r),r.socket?.close(),r.socket=void 0};return this.adminSocketTeardowns.add(u),()=>{this.adminSocketTeardowns.delete(u),u()}}async listFunctions(){return this.assertOpen(),(await this.adminFetch(Xe,"GET")).functions??[]}async getCronJobs(){return this.assertOpen(),(await this.adminFetch(Ze,"GET")).jobs??[]}async runCronJob(e){this.assertOpen();const t=await this.adminFetch(et,"POST",{name:e});return{name:t.name??e,ran:t.ran===!0}}async fetchOpenApi(){return this.assertOpen(),await this.adminFetch(tt,"GET")}async fetchOpenRpc(){return this.assertOpen(),await this.adminFetch(st,"GET")}async listStorageObjects(e={}){this.assertOpen();const t=y(D,{bucket:e.bucket,cursor:e.cursor,limit:e.limit,prefix:e.prefix}),s=await this.adminFetch(t,"GET");return{cursor:s.cursor,objects:s.objects??[]}}async deleteStorageObject(e,t){this.assertOpen();const s=`${D}?key=${encodeURIComponent(e)}${N(t?.bucket)}`,r=await this.adminFetch(s,"DELETE");return{deleted:r.deleted===!0,key:r.key??e}}async listStorageBuckets(){return this.assertOpen(),(await this.adminFetch(Ye,"GET")).buckets??[]}async uploadStorageObject(e){this.assertOpen();const t=`${D}?key=${encodeURIComponent(e.key)}${N(e.bucket)}`,s=await this.adminFetch(t,"PUT",e.body,e.contentType);return{etag:s.etag,key:s.key??e.key}}async signedStorageUrl(e,t){this.assertOpen();const s=t?.expiresInSeconds,r=s===void 0?"":`&expiresIn=${encodeURIComponent(s.toString())}`,i=`${Je}?key=${encodeURIComponent(e)}${r}${N(t?.bucket)}`,n=await this.adminFetch(i,"GET");if(typeof n.url!="string")throw new TypeError("LunoraClient: storage URL endpoint returned no `url`");return n.url}async listGlobalTables(){return this.assertOpen(),await this.adminFetch(rt,"GET")}async readGlobalTablePage(e){this.assertOpen();const t=new URLSearchParams({table:e.table});return e.limit!==void 0&&t.set("limit",String(e.limit)),e.offset!==void 0&&t.set("offset",String(e.offset)),e.filters!==void 0&&e.filters.length>0&&t.set("filters",JSON.stringify(W(e.filters))),T(await this.adminFetch(`${it}?${t.toString()}`,"GET"))}async facetGlobalColumn(e){this.assertOpen();const t=new URLSearchParams({column:e.column,table:e.table});return e.limit!==void 0&&t.set("limit",String(e.limit)),e.filters!==void 0&&e.filters.length>0&&t.set("filters",JSON.stringify(W(e.filters))),T(await this.adminFetch(`${nt}?${t.toString()}`,"GET"))}async listVectorIndexes(){return this.assertOpen(),(await this.adminFetch(at,"GET")).indexes??[]}async queryVectorIndex(e){return this.assertOpen(),(await this.adminFetch(ot,"POST",e)).matches??[]}async queryLogArchive(e={}){this.assertOpen();const t=await this.adminFetch(ct,"POST",e);return{nextCursor:t.nextCursor,rows:t.rows??[]}}async listKvNamespaces(){return this.assertOpen(),(await this.adminFetch(ht,"GET")).namespaces??[]}async listKvKeys(e){this.assertOpen();const t=y(dt,{cursor:e.cursor,limit:e.limit,namespace:e.namespace,prefix:e.prefix});return await this.adminFetch(t,"GET")}async getKvValue(e){this.assertOpen();const t=y(B,{key:e.key,namespace:e.namespace});return await this.adminFetch(t,"GET")}async putKvValue(e){this.assertOpen(),await this.adminFetch(B,"PUT",e)}async deleteKvKey(e){this.assertOpen();const t=y(B,{key:e.key,namespace:e.namespace});await this.adminFetch(t,"DELETE")}async listAuthUsers(e={}){this.assertOpen();const t=y(ut,{filterField:e.filterField,filterValue:e.filterValue,limit:e.limit,offset:e.offset,search:e.search,searchField:e.searchField,sortBy:e.sortBy,sortDirection:e.sortDirection});return await this.adminFetch(t,"GET")}async createAuthUser(e){return await this.adminFetch(ft,"POST",e)}async setAuthUserRole(e){return await this.adminFetch(mt,"POST",e)}async banAuthUser(e){return await this.adminFetch(pt,"POST",e)}async unbanAuthUser(e){return await this.adminFetch(yt,"POST",e)}async setAuthUserPassword(e){await this.adminFetch(St,"POST",e)}async removeAuthUser(e){await this.adminFetch(bt,"POST",e)}async impersonateAuthUser(e){return await this.adminFetch(Tt,"POST",e)}async revokeAuthSession(e){await this.adminFetch(vt,"POST",e)}async revokeAuthUserSessions(e){await this.adminFetch(gt,"POST",e)}async getAuthCapabilities(){return await this.adminFetch(kt,"GET")}async updateAuthUser(e){return await this.adminFetch(Ct,"POST",e)}async listAuthAccounts(e){return await this.adminFetch(y(wt,{userId:e.userId}),"GET")}async unlinkAuthAccount(e){await this.adminFetch(Et,"POST",e)}async listAuthPasskeys(e){return await this.adminFetch(y(_t,{userId:e.userId}),"GET")}async deleteAuthPasskey(e){await this.adminFetch(At,"POST",e)}async disableAuthTwoFactor(e){await this.adminFetch(It,"POST",e)}async listAuthOrganizations(e={}){return await this.adminFetch(y(Ot,{limit:e.limit,offset:e.offset}),"GET")}async listAuthOrgMembers(e){const t=y(Rt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async listAuthOrgInvitations(e){const t=y(Pt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async removeAuthOrgMember(e){await this.adminFetch(Mt,"POST",e)}async cancelAuthOrgInvitation(e){await this.adminFetch(Ht,"POST",e)}async getAuthConfig(){return await this.adminFetch(Lt,"GET")}async createAuthOrganization(e){return await this.adminFetch(Ft,"POST",e)}async updateAuthOrganization(e){return await this.adminFetch(Ut,"POST",e)}async deleteAuthOrganization(e){await this.adminFetch(xt,"POST",e)}async addAuthOrgMember(e){return await this.adminFetch(Nt,"POST",e)}async inviteAuthOrgMember(e){return await this.adminFetch(Dt,"POST",e)}async setAuthOrgMemberRole(e){return await this.adminFetch(Bt,"POST",e)}async listAuthOrgTeams(e){const t=y(Kt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async createAuthOrgTeam(e){return await this.adminFetch(Wt,"POST",e)}async updateAuthOrgTeam(e){return await this.adminFetch(qt,"POST",e)}async removeAuthOrgTeam(e){await this.adminFetch($t,"POST",e)}async listAuthOrgTeamMembers(e){const t=y(Qt,{limit:e.limit,offset:e.offset,teamId:e.teamId});return await this.adminFetch(t,"GET")}async addAuthOrgTeamMember(e){return await this.adminFetch(Gt,"POST",e)}async removeAuthOrgTeamMember(e){await this.adminFetch(zt,"POST",e)}async listAuthOrgRoles(e){const t=y(Vt,{limit:e.limit,offset:e.offset,organizationId:e.organizationId});return await this.adminFetch(t,"GET")}async createAuthOrgRole(e){return await this.adminFetch(jt,"POST",e)}async updateAuthOrgRole(e){return await this.adminFetch(Jt,"POST",e)}async deleteAuthOrgRole(e){await this.adminFetch(Yt,"POST",e)}async listAuthSessions(e={}){this.assertOpen();const t=y(lt,{limit:e.limit,offset:e.offset,userId:e.userId});return await this.adminFetch(t,"GET")}subscribe(e,t,s,r={}){this.assertOpen();const i=t??{},n=k.key(e.__lunoraRef,i,r.shardKey);let a=this.subscriptions.get(n);const c=K(s),h=K(r.onError),d=K(r.onCheckpoint);if(!a){this.nextSubId+=1;const f=`sub_${this.nextSubId.toString()}`,S=q(i),p=this.takeHydratedCache(e.__lunoraRef,S,r.shardKey);a={acked:!1,args:i,argsKey:S,callbacks:new Set,checkpointCallbacks:new Set,errorCallbacks:new Set,fn:e,id:f,lastValue:p?.value,optimisticLayers:[],serverBase:p?.value,serverCursor:p?.serverCursor,shardKey:r.shardKey,wireArgs:v(i,`args for '${e.__lunoraRef}'`),...p?.serverEpoch===void 0?{}:{serverEpoch:p.serverEpoch}},this.subscriptions.add(a)}if(a.callbacks.add(c),h&&a.errorCallbacks.add(h),d&&a.checkpointCallbacks.add(d),a.lastValue!==void 0)try{c(a.lastValue)}catch{}this.ensureSocket(r.shardKey),this.sendSubscribeIfOpen(a);const u=a;return()=>{u.callbacks.delete(c),h&&u.errorCallbacks.delete(h),d&&u.checkpointCallbacks.delete(d),u.callbacks.size===0&&(this.sendOrQueueUnsubscribe(u.shardKey,u.id,"unsubscribe"),this.subscriptions.remove(u))}}subscribeShape(e,t,s={}){if(this.assertOpen(),this.followsAnotherTab())return()=>{};this.nextShapeId+=1;const r=`shape_${this.nextShapeId.toString()}`,i={args:e.args,callbacks:new Set([t]),errorCallbacks:s.onError?new Set([s.onError]):new Set,id:r,name:e.name,onCheckpoint:s.onCheckpoint,rows:new Map,shardKey:s.shardKey,wireArgs:e.args===void 0?void 0:v(e.args,`shape args for '${e.name}'`)};return this.shapeSubscriptions.set(r,i),this.ensureSocket(s.shardKey),this.sendShapeSubscribeIfOpen(i),()=>{this.shapeSubscriptions.delete(r),this.sendOrQueueUnsubscribe(i.shardKey,r,"shape_unsubscribe")}}stream(e,t,s={}){if(this.assertOpen(),this.WebSocketImpl===void 0)throw new m("INTERNAL","LunoraClient: streams require a WebSocket implementation");this.nextStreamId+=1;const r=`stream_${this.nextStreamId.toString()}`,{shardKey:i}=s,n=t??{},{handle:a,iterable:c}=Te({maxBuffer:s.maxBuffer,onCancel:()=>{this.cancelStream(r,i)}});this.ensureSocket(i);const h=this.getConnection(i),d={id:r,query:{args:v(n,`stream args for '${e.__lunoraRef}'`),functionPath:e.__lunoraRef,shardKey:i},type:"stream"},u={durable:s.durable===!0,generation:void 0,handle:a,lastSeq:0,message:d,shardKey:i,started:!1};this.streams.set(r,u);const f=h?.wsState==="open"&&b(h,d);if(u.started=f,!f&&h===void 0)return this.streams.delete(r),a.fail(new m("STREAM_DISCONNECTED","stream unavailable: this tab is not the cross-tab WebSocket leader, so it holds no socket to stream over")),c;if(!f){for(h.pendingStreams=h.pendingStreams??[];h.pendingStreams.length>=De;){const p=h.pendingStreams.shift()?.id,g=p?this.streams.get(p):void 0;g&&(g.handle.fail(new m("STREAM_QUEUE_OVERFLOW","stream-start frame evicted while socket was unreachable")),this.streams.delete(p))}h.pendingStreams.push(d)}return c}httpStream(e,t,s={}){if(this.closed)throw new m("CLIENT_CLOSED","LunoraClient is closed");if(!this.fetchImpl)throw new m("INTERNAL","LunoraClient: no `fetch` implementation available");const r={...this.authToken?{authorization:`Bearer ${this.authToken}`}:{},...s.headers};return de(e,t,{baseUrl:this.url,fetch:this.fetchImpl,headers:r,maxBuffer:s.maxBuffer,signal:s.signal})}close(){this.closed=!0,this.outboxLeaderRelease?.(),this.outboxLeaderRelease=void 0;for(const e of this.streams.values())e.handle.fail(new m("CLIENT_CLOSED","LunoraClient closed"));this.streams.clear();for(const e of this.connections.values())this.teardownConnection(e);for(const e of this.adminSocketTeardowns)e();this.adminSocketTeardowns.clear();for(const e of this.replayRetryTimers.values())clearTimeout(e);this.replayRetryTimers.clear(),this.replayRetryState.clear(),this.offlineQueue.clear(),this.queuedIdentities.clear(),this.cacheFlushTimer!==void 0&&(clearTimeout(this.cacheFlushTimer),this.cacheFlushTimer=void 0),this.pendingCacheWrites.size>0&&this.flushQueryCacheWrites().catch(()=>{}),this.authTokenListeners.clear(),this.statusListeners.clear(),this.tokenExpiredListeners.clear(),this.mutationSettledListeners.clear(),this.pendingChangeListeners.clear(),this.whisperHandlers.clear(),this.shapeSubscriptions.clear(),this.pokeBuffers.clear(),this.subscriptions.clear(),this.clientQueryStore.clear(),this.hydratedQueryCache.clear(),this.tabCoordinator?.stop(),this.tabCoordinator=void 0}assertOpen(){if(this.closed)throw new m("INTERNAL","LunoraClient is closed")}followsAnotherTab(){const e=this.tabCoordinator;if(e===void 0||e.isLeader())return!1;const t=e.leaderTabId;return t!==void 0&&t!==e.id}assertLeaderOwnedSurface(e){if(this.followsAnotherTab())throw new m("NOT_IMPLEMENTED",`LunoraClient: \`${e}\` is unavailable on a cross-tab follower tab. The \`crossTabSync\` channel only carries data from the leader tab outward, so this call could never reach the server. Turn off \`crossTabSync\`, or keep \`${e}\` on the leader tab.`)}clearConnectionTimers(e){clearTimeout(e.reconnectTimer),clearTimeout(e.connectTimer),clearTimeout(e.stableTimer),e.reconnectTimer=void 0,e.connectTimer=void 0,e.stableTimer=void 0}teardownConnection(e){const t=l(e.shardKey);for(const[s,r]of this.streams)l(r.shardKey)===t&&(r.handle.fail(new m("STREAM_DISCONNECTED","stream terminated: the connection carrying it was torn down")),this.streams.delete(s));if(e.pendingStreams=void 0,e.pendingUnsubscribes=[],this.clearConnectionTimers(e),this.stopHeartbeat(e),e.socket){try{e.socket.close()}catch{}e.socket=void 0}e.wsState="closed"}createTabCoordinator(){const e=`lunora-bridge::${this.url}::${this.identityFingerprint()??"anon"}`;return new he({channelName:e,onBecomeLeader:()=>{for(const t of this.subscriptions.all())this.ensureSocket(t.shardKey),this.sendSubscribeIfOpen(t);this.tabCoordinator?.broadcastConnectionStatus(this.computeStatus(),this.identityFingerprint())},onStopBeingLeader:()=>{for(const[t,s]of this.connections)this.teardownConnection(s),this.connections.delete(t);this.leaderStatus=void 0,this.leaderWasEverConnected=!1},onConnectionStatus:(t,s)=>{if(s!==void 0&&s!==this.identityFingerprint())return;const r=t==="connected"&&this.leaderStatus!=="connected";this.leaderStatus=t,t==="connected"&&(this.leaderWasEverConnected=!0),this.emitConnectionStatus(),r&&this.flushAllOfflineQueues()},onSubscriptionData:(t,s,r,i,n)=>{if(n!==void 0&&n!==this.identityFingerprint())return;const a=this.subscriptions.get(t);a&&(a.serverBase=s,r!==void 0&&(a.serverCursor=r,i!==void 0&&(a.serverEpoch=i),U(a,r)),O(a,R(s,a.optimisticLayers)))},onLeaderClaimAnswered:()=>{this.tabCoordinator?.isLeader()&&this.tabCoordinator.broadcastConnectionStatus(this.computeStatus(),this.identityFingerprint())},onSubscriptionError:(t,s,r)=>{if(r!==void 0&&r!==this.identityFingerprint())return;const i=this.subscriptions.get(t);i&&E(i.errorCallbacks,s)},onSubscriptionSettled:(t,s,r,i,n,a)=>{if(a!==void 0&&a!==this.identityFingerprint())return;const c=this.subscriptions.get(t);if(c){this.ackAndAdvanceCursor(c,s,r),i!==void 0&&n===this.clientId&&(c.lastMutationId=Math.max(c.lastMutationId??0,i));for(const h of c.checkpointCallbacks)h({checkpoint:c.serverCursor,mutationId:c.lastMutationId})}}})}async enqueueOfflineMutation(e,t,s,r,i,n,a){const c=this.identityFingerprint();if(this.outbox){this.outboxMutationCounter+=1;const h=this.outboxMutationCounter;try{await this.outbox.enqueue({args:t,clientId:this.clientId,functionPath:e.__lunoraRef,idempotencyKey:`${this.clientId}:${String(h)}`,identity:c,mutationId:h,shardKey:s})}catch(d){throw H(i),d instanceof Error?d:new Error(String(d))}for(const d of n)d(void 0);return}return new Promise((h,d)=>{const u={args:t,functionPath:e.__lunoraRef,liveAwaiter:!0,id:r,clientId:this.clientId,identity:c,precondition:a,onCommit:f=>{for(const S of n)S(f)},reject:f=>{this.queuedIdentities.delete(r),H(i),d(f instanceof Error?f:new Error(String(f)))},resolve:h,shardKey:s};this.offlineQueue.enqueue(u),this.queuedOfflineShardKeys.add(s),u.id!==void 0&&this.queuedIdentities.set(u.id,c)})}async hydratePersistedQueue(){try{const e=await this.offlineQueue.hydrate();for(const t of e)this.queuedOfflineShardKeys.add(t),this.ensureSocket(t)}catch{}}hydrateAsOutboxLeader(){const e=()=>{this.hydratePersistedQueue().catch(()=>{})},t=globalThis.navigator?.locks;if(!t){e();return}t.request(`lunora:outbox-leader:${this.url}`,()=>(this.closed||e(),new Promise(s=>{if(this.closed){s();return}this.outboxLeaderRelease=s}))).catch(e)}async hydrateQueryCache(){if(this.queryCache)try{const e=await this.queryCache.load(),t=new Map;for(const s of this.subscriptions.all())t.set(P(s.fn.__lunoraRef,s.argsKey,s.shardKey),s);for(const{key:s,...r}of e){if(me(this.persistenceVersion,r.version)){this.queryCache.remove(s).catch(()=>{});continue}const i=t.get(s);if(i){this.seedSubscriptionFromCache(i,r);continue}this.hydratedQueryCache.set(s,r)}}catch{}}seedSubscriptionFromCache(e,t){e.serverBase!==void 0||e.lastValue!==void 0||t.identity!==this.identityFingerprint()||(e.serverBase=t.value,e.serverCursor=t.serverCursor,t.serverEpoch!==void 0&&(e.serverEpoch=t.serverEpoch),O(e,R(t.value,e.optimisticLayers)))}takeHydratedCache(e,t,s){const r=P(e,t,s),i=this.hydratedQueryCache.get(r);if(i!==void 0)return this.hydratedQueryCache.delete(r),i.identity===this.identityFingerprint()?i:void 0}persistQueryValue(e){const t=e.serverBase;if(!this.queryCache||t===void 0)return;const s=P(e.fn.__lunoraRef,e.argsKey,e.shardKey),r=this.getConnection(e.shardKey)?.identity;this.pendingCacheWrites.set(s,{identity:r===void 0?this.identityFingerprint():r,serverCursor:e.serverCursor,ts:Date.now(),value:t,...e.serverEpoch===void 0?{}:{serverEpoch:e.serverEpoch},...this.persistenceVersion===void 0?{}:{version:this.persistenceVersion}}),this.cacheFlushTimer??=setTimeout(()=>{this.flushQueryCacheWrites().catch(()=>{})},xe)}async flushQueryCacheWrites(){this.cacheFlushTimer=void 0;const{queryCache:e}=this;if(!e){this.pendingCacheWrites.clear();return}const t=[...this.pendingCacheWrites.entries()];this.pendingCacheWrites.clear(),await Promise.allSettled(t.map(async([s,r])=>e.put(s,r)))}computeStatus(){if(this.tabCoordinator&&!this.tabCoordinator.isLeader())return this.leaderStatus??"idle";const e=[...this.connections.values()];return e.length===0?"idle":e.some(t=>t.wsState==="open")?"connected":e.some(t=>t.wsState==="connecting")?"connecting":"offline"}emitConnectionStatus(){const e=this.computeStatus();e!==this.lastStatus&&(this.lastStatus=e,this.statusListeners.emit(e),this.tabCoordinator?.isLeader()&&this.tabCoordinator.broadcastConnectionStatus(e,this.identityFingerprint()))}emitItemSettled(e,t,s){this.mutationSettledListeners.emit({args:e.args,code:s===void 0?void 0:s.code,error:s,functionPath:e.functionPath,hadAwaiter:e.liveAwaiter??!1,id:e.id??"",shardKey:e.shardKey,status:t})}applyOptimisticUpdates(e,t,s,r){const i=[],n=[];if(!r)return{confirms:i,rollbacks:n};const a=k.key(e,t,s),c=this.subscriptions.get(a);if(c){const h=ue(c,r);h&&(i.push(h.confirm),n.push(h.rollback))}return{confirms:i,rollbacks:n}}applyOptimisticUpdate(e,t,s,r,i){const{confirms:n,rollbacks:a,store:c}=le(this.subscriptions,s);try{e(c,t)}catch{H(a);return}r.push(...a),i.push(...n)}getConnection(e){return this.connections.get(l(e))}sendOrQueueUnsubscribe(e,t,s){const r=this.getConnection(e);r&&!b(r,{id:t,type:s})&&r.pendingUnsubscribes.push({id:t,type:s})}connectionGateState(e){if(this.tabCoordinator&&!this.tabCoordinator.isLeader()){let s="idle";return this.leaderStatus==="connected"?s="open":this.leaderStatus==="connecting"&&(s="connecting"),{hasSocket:!1,wasEverConnected:this.leaderWasEverConnected,wsState:s}}const t=this.getConnection(e);return{hasSocket:t?.socket!==void 0,wasEverConnected:t?.wasEverConnected??!1,wsState:t?.wsState??"idle"}}getOrCreateConnection(e){const t=l(e);let s=this.connections.get(t);return s||(s={connectTimer:void 0,heartbeatTimer:void 0,lastFrameAt:0,pendingUnsubscribes:[],reconnect:Q(this.reconnectOptions),reconnectTimer:void 0,shardKey:e,socket:void 0,stableTimer:void 0,wasEverConnected:!1,wsState:"idle"},this.connections.set(t,s)),s}wsUrlFor(e,t){const s=[];if(e!==void 0&&s.push(`shard=${encodeURIComponent(e)}`),t!==void 0&&s.push(`token=${encodeURIComponent(t)}`),s.length===0)return this.wsUrl;const r=this.wsUrl.includes("?")?"&":"?";return`${this.wsUrl}${r}${s.join("&")}`}rpcRequestHeaders(e,t){const s={"content-type":"application/json"},r=this.shardCursors.get(this.cursorKeyFor(t));if(r!==void 0&&(s["x-lunora-min-seq"]=r.toString()),this.authToken&&(s.authorization=`Bearer ${this.authToken}`),e.mutationId&&(s["x-lunora-mutation-id"]=e.mutationId),(e.clientId!==void 0||e.mutationId!==void 0)&&(s["x-lunora-client-id"]=e.clientId??this.clientId),e.clientSeq!==void 0&&(s["x-lunora-client-seq"]=e.clientSeq.toString()),e.attachBookmark){const i=this.bookmark.get();i&&(s["x-d1-bookmark"]=i)}return s}async rpc(e,t,s,r={}){if(!this.fetchImpl)throw new A("LunoraClient: no `fetch` implementation available");const i=this.rpcRequestHeaders(r,s),n=await this.fetchImpl(C(this.url,Pe),{body:JSON.stringify({args:v(t,`args for '${e}'`),functionPath:e,shardKey:s}),headers:i,method:"POST"});if(r.captureBookmark){const c=n.headers.get("x-d1-bookmark");c&&this.bookmark.set(c)}let a;try{a=await n.json()}catch{throw M(n.status,n.statusText,n.headers.get("retry-after"))}if("error"in a)throw Y(a.error,n.headers.get("retry-after"));if(!n.ok)throw M(n.status,n.statusText,n.headers.get("retry-after"));return r.onMutationAck?.(a.lastMutationId),r.onCommitCursor?.(a.commitCursor),this.learnDefaultShardKey(n,s),this.recordShardCursor(s,a.commitCursor),T(a.result)}async adminFetch(e,t,s,r){if(!this.fetchImpl)throw new m("INTERNAL","LunoraClient: no `fetch` implementation available");const i={};this.authToken&&(i.authorization=`Bearer ${this.authToken}`);const n=s instanceof ArrayBuffer||s instanceof Blob;let a;s===void 0?a=void 0:n?(a=s,r!==void 0&&(i["content-type"]=r)):(a=JSON.stringify(s),i["content-type"]="application/json");const c=await this.fetchImpl(C(this.url,e),{body:a,headers:i,method:t});let h;try{h=await c.json()}catch{const d=c.statusText?` ${c.statusText}`:"";throw new m("INTERNAL",`LunoraClient: response was not JSON (status ${c.status.toString()}${d})`)}if(typeof h=="object"&&h!==null&&"error"in h){const d=h.error,u=new Error(d.message??"admin request failed");throw u.code=d.code,u}if(!c.ok){const d=c.statusText?` ${c.statusText}`:"";throw new m("INTERNAL",`LunoraClient: admin request failed (status ${c.status.toString()}${d})`)}return h}effectiveConnectionContext(e){const t=this.connectionContextHolders.get(e);return t&&t.length>0?t[t.length-1]?.context:this.connectionContexts.get(e)??this.defaultConnectionContext}refreshConnectionContext(e){const t=this.connections.get(e);t?.wsState==="open"&&this.sendConnectEnvelope(t)}sendConnectEnvelope(e){const t=this.effectiveConnectionContext(l(e.shardKey));b(e,{caps:es,clientId:this.clientId,id:"connect",type:"connect",...t===void 0?{}:{context:t}})}resendShapeSubscriptions(e){for(const t of this.shapeSubscriptions.values())l(t.shardKey)===l(e)&&this.sendShapeSubscribeIfOpen(t)}ensureSocket(e){if(this.closed||this.WebSocketImpl===void 0||this.tabCoordinator&&!this.tabCoordinator.isLeader())return;const t=this.getOrCreateConnection(e);if(!(t.wsState==="open"||t.wsState==="connecting")){if(t.wsState="connecting",this.emitConnectionStatus(),typeof this.wsToken=="function"){this.openSocketWithProvidedToken(t,e,this.wsToken).catch(()=>{});return}this.openSocket(t,e,this.wsToken)}}async openSocketWithProvidedToken(e,t,s){let r;try{r=await s()}catch{this.handleDisconnect(e);return}this.closed||this.WebSocketImpl===void 0||e.wsState!=="connecting"||e.socket!==void 0||this.openSocket(e,t,r)}openManagedSocket(e,t,s,r){const{WebSocketImpl:i}=this;if(i===void 0)throw new m("INTERNAL","no WebSocket implementation available");const n=new i(t);e.socket=n;const a=()=>{this.stopHeartbeat(e),e.connectTimer!==void 0&&(clearTimeout(e.connectTimer),e.connectTimer=void 0),e.socket===n&&(e.socket=void 0)},c=h=>{a(),r.onClose(h)};s>0&&(e.connectTimer=setTimeout(()=>{if(e.connectTimer=void 0,e.socket===n){try{n.close()}catch{}c()}},s)),n.addEventListener("open",()=>{if(e.socket===n){e.connectTimer!==void 0&&(clearTimeout(e.connectTimer),e.connectTimer=void 0),e.lastFrameAt=Date.now();try{r.onOpen?.()}catch(h){console.error("[lunora] connection open handler threw",h)}this.startHeartbeat(e,c)}}),n.addEventListener("message",h=>{if(e.socket===n){e.lastFrameAt=Date.now();try{r.onMessage(h)}catch(d){console.error("[lunora] server frame handler threw",d)}}}),n.addEventListener("close",h=>{e.socket===n&&c(h)}),n.addEventListener("error",()=>{e.socket===n&&c()})}openSocket(e,t,s){this.WebSocketImpl!==void 0&&(e.identity=this.identityFingerprint(),this.openManagedSocket(e,this.wsUrlFor(t,s),this.connectTimeoutMs,{onClose:r=>{r?.code===4001&&this.notifyTokenExpired(),this.handleDisconnect(e)},onMessage:r=>{this.handleServerMessage(r.data,t)},onOpen:()=>{e.wsState="open",e.wasEverConnected=!0,clearTimeout(e.stableTimer),e.stableTimer=setTimeout(()=>{e.wsState==="open"&&e.reconnect.reset()},Ne),this.emitConnectionStatus(),this.sendConnectEnvelope(e),this.markShardPendingAck(t);for(const i of this.subscriptions.all())l(i.shardKey)===l(t)&&this.sendSubscribeIfOpen(i);if(this.resendShapeSubscriptions(t),e.pendingUnsubscribes.length>0){const i=e.pendingUnsubscribes;e.pendingUnsubscribes=[];for(const{id:n,type:a}of i)b(e,{id:n,type:a})}this.flushPendingStreams(e);const r=this.whisperHandlers.get(l(t));if(r)for(const i of r.keys())b(e,{topic:i,type:"whisper_subscribe"});this.flushOfflineQueue(t).catch(()=>{})}}))}flushPendingStreams(e){if(!e.pendingStreams||e.pendingStreams.length===0)return;const t=e.pendingStreams;e.pendingStreams=[];for(const s of t){const r=b(e,s)?this.streams.get(String(s.id)):void 0;r&&(r.started=!0)}}cancelStream(e,t){const s=this.getConnection(t),r=this.streams.get(e);s&&(s.pendingStreams=s.pendingStreams?.filter(i=>i.id!==e),!b(s,{id:e,type:"unsubscribe"})&&r?.durable===!0&&r.started&&s.pendingUnsubscribes.push({id:e,type:"unsubscribe"})),this.streams.delete(e)}handleDisconnect(e){if(this.closed||e.wsState==="idle"||e.wsState==="closed")return;this.stopHeartbeat(e),this.clearConnectionTimers(e),e.socket=void 0,e.wsState="idle",this.emitConnectionStatus(),this.markShardPendingAck(e.shardKey);const t=new Set((e.pendingStreams??[]).map(r=>r.id));for(const[r,i]of this.streams)if(!(l(i.shardKey)!==l(e.shardKey)||t.has(r))){if(i.durable){const n={...i.message,...i.generation===void 0?{}:{generation:i.generation},sinceChunk:i.lastSeq};i.message=n,e.pendingStreams=[...(e.pendingStreams??[]).filter(a=>a.id!==r),n];continue}i.handle.fail(new m("STREAM_DISCONNECTED","stream terminated: WebSocket disconnected")),this.streams.delete(r)}if(this.WebSocketImpl===void 0)return;const s=e.reconnect.next();e.reconnectTimer=setTimeout(()=>{e.reconnectTimer=void 0,this.ensureSocket(e.shardKey)},s)}startHeartbeat(e,t){this.stopHeartbeat(e),!(this.heartbeatIntervalMs<=0)&&(e.heartbeatTimer=setInterval(()=>{if(!e.socket)return;const{socket:s}=e;if(Date.now()-e.lastFrameAt>this.heartbeatIntervalMs*2.5){try{s.close()}catch{}t();return}try{s.send(Le)}catch{}},this.heartbeatIntervalMs))}stopHeartbeat(e){e.heartbeatTimer!==void 0&&(clearInterval(e.heartbeatTimer),e.heartbeatTimer=void 0)}markShardPendingAck(e){const t=l(e);for(const s of this.subscriptions.all())l(s.shardKey)===t&&(s.acked=!1)}sendSubscribeIfOpen(e){const t=this.getConnection(e.shardKey);if(t?.wsState!=="open"||e.acked)return;const s=e.fn.__lunoraTable??e.fn.__lunoraRef,r=e.serverCursor!==void 0&&e.serverBase!==void 0;b(t,{id:e.id,query:{args:e.wireArgs,functionPath:e.fn.__lunoraRef,table:s,...r?{sinceSeq:e.serverCursor}:{},...r&&e.serverEpoch!==void 0?{sinceEpoch:e.serverEpoch}:{}},type:"subscribe"})}sendShapeSubscribeIfOpen(e){const t=this.getConnection(e.shardKey);t?.wsState==="open"&&b(t,{id:e.id,shape:{name:e.name,...e.wireArgs===void 0?{}:{args:e.wireArgs}},type:"shape_subscribe",...e.serverCursor===void 0?{}:{sinceCheckpoint:e.serverCursor},...e.serverEpoch===void 0?{}:{sinceEpoch:e.serverEpoch}})}handleServerMessage(e,t){const s=rs(e);if(s===void 0)return;let r;try{r=JSON.parse(s)}catch{return}switch(r.type!=="error"&&this.getConnection(t)?.reconnect.reset(),r.type){case"ack":{const i=this.subscriptions.getById(r.id);i&&(i.acked=!0);return}case"chunk":{const{data:i,generation:n,id:a,seq:c}=r,h=this.streams.get(a);h&&typeof c=="number"&&(h.lastSeq=c,typeof n=="number"&&(h.generation=n),h.durable=!0),h?.handle.push(T(i));return}case"complete":{this.handleCompleteMessage(r.id);return}case"data":case"delta":{this.handleDataMessage(r);return}case"error":{this.handleErrorMessage(r);break}case"pokeEnd":{this.handlePokeEnd(r,t);break}case"pokePart":{this.handlePokePart(r,t);break}case"pokeStart":{this.handlePokeStart(r,t);break}case"resume":{this.handleResumeMessage(r);break}case"settled":{this.handleSettledMessage(r);break}case"whisper":{this.dispatchWhisper(r,t);break}}}handleErrorMessage(e){if(e.error?.code==="TOKEN_EXPIRED"){this.notifyTokenExpired();return}const{id:s}=e,r=s===void 0?void 0:this.streams.get(s);if(r&&s!==void 0){r.handle.fail(ts(e)),this.streams.delete(s);return}const i=s===void 0?void 0:this.subscriptions.getById(s);if(i){const a=J(e),c=this.identityFingerprint(),h=k.keyOf(i);E(i.errorCallbacks,a),this.tabCoordinator?.isLeader()&&this.tabCoordinator.broadcastSubscriptionError(h,a,c);return}const n=s===void 0?void 0:this.shapeSubscriptions.get(s);n&&E(n.errorCallbacks,J(e))}pokeBufferKey(e,t){return`${l(e)}\0${t}`}handlePokeStart(e,t){ge(this.pokeBuffers,se.MAX_POKE_BUFFERS),this.pokeBuffers.set(this.pokeBufferKey(t,e.pokeId),{baseCheckpoint:e.baseCheckpoint,bases:new Map,epoch:e.epoch,lastMutationId:new Map,parts:new Map,resets:new Set})}handlePokePart(e,t){const s=this.pokeBuffers.get(this.pokeBufferKey(t,e.pokeId));if(!s)return;const r=s.parts.get(e.shapeId)??[];for(const n of e.rowsPatch)r.push(n.value===void 0?n:{...n,value:T(n.value)});s.parts.set(e.shapeId,r),e.lastMutationId!==void 0&&s.lastMutationId.set(e.shapeId,e.lastMutationId);const i=e.baseCheckpoint??s.baseCheckpoint;i!==void 0&&s.bases.set(e.shapeId,i),e.reset===!0&&s.resets.add(e.shapeId)}handlePokeEnd(e,t){const s=this.pokeBufferKey(t,e.pokeId),r=this.pokeBuffers.get(s);if(r){this.pokeBuffers.delete(s);for(const i of r.parts.keys()){const n=this.shapeSubscriptions.get(i);n&&this.applyPokePart(n,r,e)}}}applyPokePart(e,t,s){const r=t.parts.get(e.id)??[],i=t.resets.has(e.id),n=t.bases.get(e.id),a=t.epoch!==void 0&&e.serverEpoch!==void 0&&t.epoch!==e.serverEpoch,c=n!==void 0&&e.serverCursor!==void 0&&e.serverCursor!==n;if(!i&&(a||c)){e.rows.clear(),e.serverCursor=void 0,e.serverEpoch=void 0,this.emitShapeRows(e),this.sendShapeSubscribeIfOpen(e);return}i&&e.rows.clear(),He(e.rows,r),s.checkpoint!==void 0&&(e.serverCursor=s.checkpoint),s.epoch!==void 0&&(e.serverEpoch=s.epoch);const h=t.lastMutationId.get(e.id);h!==void 0&&(e.lastMutationId=h),this.emitShapeRows(e),e.onCheckpoint?.({checkpoint:e.serverCursor,mutationId:e.lastMutationId})}resnapshotSubscription(e){e.acked=!1,e.serverCursor=void 0,e.serverEpoch=void 0,console.warn("[lunora] could not merge a row delta into the cached result; re-subscribing for a full snapshot"),this.sendSubscribeIfOpen(e)}emitShapeRows(e){const t=[...e.rows.values()];for(const s of e.callbacks)try{s(t)}catch{}}handleDataMessage(e){const{id:t}=e,s=t?this.subscriptions.getById(t):void 0;if(!s)return;let r;try{r=this.resolveDataPayload(e,s)}catch(i){E(s.errorCallbacks,{code:"WIRE_DECODE_FAILED",message:`could not decode a server frame for this subscription — ${i instanceof Error?i.message:String(i)}`});return}if(r===z){this.resnapshotSubscription(s);return}if(s.serverBase=r,e.cursor!==void 0&&(s.serverCursor=e.cursor),e.epoch!==void 0&&(s.serverEpoch=e.epoch),e.lastMutationId!==void 0){s.lastMutationId=Math.max(s.lastMutationId??0,e.lastMutationId);for(const i of s.checkpointCallbacks)i({checkpoint:s.serverCursor,mutationId:s.lastMutationId})}if(this.persistQueryValue(s),U(s,s.serverCursor),O(s,R(r,s.optimisticLayers)),this.tabCoordinator?.isLeader()){const i=k.keyOf(s);this.tabCoordinator.broadcastSubscriptionData(i,r,s.serverCursor,s.serverEpoch,this.identityFingerprint())}}handleResumeMessage(e){const t=this.subscriptions.getById(e.id);t&&this.ackAndAdvanceCursor(t,e.cursor,e.epoch)}handleSettledMessage(e){const t=this.subscriptions.getById(e.id);if(t){this.ackAndAdvanceCursor(t,e.cursor,e.epoch),e.lastMutationId!==void 0&&(t.lastMutationId=Math.max(t.lastMutationId??0,e.lastMutationId));for(const s of t.checkpointCallbacks)s({checkpoint:t.serverCursor,mutationId:t.lastMutationId});if(this.tabCoordinator?.isLeader()){const s=k.keyOf(t);this.tabCoordinator.broadcastSubscriptionSettled(s,t.serverCursor,t.serverEpoch,t.lastMutationId,this.clientId,this.identityFingerprint())}}}ackAndAdvanceCursor(e,t,s){e.acked=!0,(t!==void 0&&t!==e.serverCursor||s!==void 0&&s!==e.serverEpoch)&&(t!==void 0&&(e.serverCursor=t),s!==void 0&&(e.serverEpoch=s),this.persistQueryValue(e),U(e,e.serverCursor)&&O(e,R(e.serverBase,e.optimisticLayers)))}resolveDataPayload(e,t){if("data"in e&&e.data!==void 0)return T(e.data);const s=T(e.delta);return ie(s)?t.serverBase===void 0?s:ne(t.serverBase,s)??z:s}dispatchWhisper(e,t){const s=this.whisperHandlers.get(l(t))?.get(e.topic);if(!s)return;const r=T(e.data);for(const i of s)try{i(r,e.from)}catch{}}notifyTokenExpired(){this.tokenExpiredListeners.emit()}handleCompleteMessage(e){const t=this.streams.get(e);if(t){t.handle.complete(),this.streams.delete(e);return}const s=this.subscriptions.getById(e);s&&(s.acked=!1,E(s.errorCallbacks,{code:"SUBSCRIPTION_CANCELLED",message:"subscription was cancelled by the server"}))}unpersist(e){e&&this.persistence?.remove(e).catch(t=>{pe(this.onPersistenceError,"remove",t,e)})}identityFingerprint(){if(this.authSubject!==void 0)return this.authSubject===null?null:`subj:${this.authSubject}`;const e=this.authToken;return e===null?null:this.hashToken(e)}hashToken(e){let t=2166136261,s=5381;for(let r=0;r<e.length;r+=1){const i=e.charCodeAt(r);t^=i,t=Math.imul(t,16777619),s=Math.imul(s,33)+i}return`${e.length.toString(36)}:${(t>>>0).toString(36)}:${(s>>>0).toString(36)}`}isSameCredentialUnderTokenHash(e){if(e===null||e.startsWith("subj:"))return!1;const t=this.authToken;return t===null?!1:this.hashToken(t)===e}rejectQueuedForIdentityChange(){const e=this.offlineQueue.drain();for(const t of e){this.queuedIdentities.delete(t.id??""),this.unpersist(t.id);const s=new Error("offline mutation discarded: auth identity changed before replay");s.code="OFFLINE_IDENTITY_CHANGED",t.reject(s),this.emitItemSettled(t,"rejected",s)}this.clearQueryCacheForIdentityChange()}restampQueuedIdentity(e,t){for(const[s,r]of this.queuedIdentities)r===e&&this.queuedIdentities.set(s,t);this.offlineQueue.restampIdentity(e,t)}restampWatermarks(e,t){const s=e??"",r=this.clientWatermarks.get(s);if(r===void 0)return;this.clientWatermarks.delete(s);const i=t??"",n=this.clientWatermarks.get(i);if(n===void 0)this.clientWatermarks.set(i,r);else for(const[a,c]of r)n.set(a,c)}clearQueryCacheForIdentityChange(){this.cacheFlushTimer!==void 0&&(clearTimeout(this.cacheFlushTimer),this.cacheFlushTimer=void 0),this.pendingCacheWrites.clear(),this.hydratedQueryCache.clear(),this.queryCache?.clear().catch(()=>{})}flushAllOfflineQueues(){for(const e of this.queuedOfflineShardKeys)this.flushOfflineQueue(e).catch(()=>{})}async flushOfflineQueue(e){const t=l(e),s=this.offlineFlushes.get(t);if(s===void 0&&this.offlineQueue.size===0)return;const r=(async()=>{await(s??Promise.resolve());try{await this.drainOfflineQueue(e)}catch{}})();this.offlineFlushes.set(t,r),await r,this.offlineFlushes.get(t)===r&&this.offlineFlushes.delete(t)}async drainOfflineQueue(e){const t=this.offlineQueue.drainConflict();for(const h of t){this.unpersist(h.id),this.queuedIdentities.delete(h.id??"");const d=new Error("offline mutation skipped: precondition failed before replay");d.code="OFFLINE_PRECONDITION_FAILED",this.emitItemSettled(h,"rejected",d)}const s=l(e),r=this.offlineQueue.drain(h=>l(h.shardKey)===s);if(r.length===0)return;const i=this.identityFingerprint(),n=[];for(const h of r)this.passesReplayIdentityGate(h,i)&&n.push(h);if(n.length===0)return;const a=this.encodableOrSettleTerminal(n);if(a.length===0)return;if(a.length===1){await this.replaySequential(a,e),this.scheduleRateLimitedRetry(e);return}const c=[];for(let h=0;h<a.length;h+=x){const d=a.slice(h,h+x),u=await this.replayBatched(d,e);if(c.push(...u.requeue),u.stop){c.push(...a.slice(h+x));break}}c.length>0&&this.offlineQueue.requeue(c),this.scheduleRateLimitedRetry(e)}noteReplayRetryDelay(e,t){const s=l(e),r=this.replayRetryState.get(s),i=(r?.attempts??0)+1,n=Ie(t,i);this.replayRetryState.set(s,{attempts:i,delayMs:n===void 0?r?.delayMs:Math.max(r?.delayMs??0,n)})}scheduleRateLimitedRetry(e){const t=l(e),s=this.replayRetryState.get(t),r=s?.delayMs;if(s===void 0||r===void 0||this.closed||this.offlineQueue.size===0){this.replayRetryState.delete(t);return}s.delayMs=void 0;const i=this.replayRetryTimers.get(t);i!==void 0&&clearTimeout(i),this.replayRetryTimers.set(t,setTimeout(()=>{this.replayRetryTimers.delete(t),this.flushOfflineQueue(e).catch(()=>{})},r))}encodableOrSettleTerminal(e){const t=[];for(const s of e)try{v(s.args,`args for '${s.functionPath}'`),t.push(s)}catch(r){this.settleReplayTerminal(s,r instanceof Error?r:new Error(String(r)))}return t}passesReplayIdentityGate(e,t){const s=e.id===void 0?void 0:this.queuedIdentities.get(e.id),r=s===void 0?e.identity:s;if(r!==void 0&&r!==t&&!this.isSameCredentialUnderTokenHash(r)){this.queuedIdentities.delete(e.id??""),this.unpersist(e.id);const i=new Error("offline mutation skipped: auth identity changed before replay");return i.code="OFFLINE_IDENTITY_CHANGED",e.reject(i),this.emitItemSettled(e,"rejected",i),!1}return this.queuedIdentities.delete(e.id??""),!0}settleReplaySuccess(e,t,s){this.unpersist(e.id),this.recordShardCursor(e.shardKey,s),e.onCommit?.(s),e.resolve(t),this.emitItemSettled(e,"committed")}cursorKeyFor(e){return e??this.defaultShardKey??""}learnDefaultShardKey(e,t){const s=e.headers.get("x-lunora-shard-key");if(s===null||t!==void 0||this.defaultShardKey===s)return;this.defaultShardKey=s;const r=this.shardCursors.get("");r!==void 0&&(this.shardCursors.set(s,Math.max(this.shardCursors.get(s)??0,r)),this.shardCursors.delete(""))}recordShardCursor(e,t){if(typeof t!="number")return;const s=this.cursorKeyFor(e);this.shardCursors.set(s,Math.max(this.shardCursors.get(s)??0,t))}settleReplayTerminal(e,t){this.unpersist(e.id),e.reject(t),this.emitItemSettled(e,"rejected",t)}async replaySequential(e,t){for(let s=0;s<e.length;s+=1){const r=e[s];if(r)try{let i;const n=await this.rpc(r.functionPath,r.args,r.shardKey,{captureBookmark:!0,clientId:r.clientId??this.clientId,mutationId:r.id,onCommitCursor:a=>{i=a}});this.settleReplaySuccess(r,n,i)}catch(i){if(!ke(i)){this.settleReplayTerminal(r,i);continue}this.noteReplayRetryDelay(t,i),this.offlineQueue.requeue(e.slice(s));return}}}async replayBatched(e,t){if(!this.fetchImpl)return{requeue:e,stop:!0};const s=JSON.stringify({calls:e.map((c,h)=>({args:v(c.args,`args for '${c.functionPath}'`),functionPath:c.functionPath,id:h,clientId:c.clientId??this.clientId,mutationId:c.id,shardKey:c.shardKey}))});if(e.length>1&&Re(s)>Oe)return await this.replayBatchedHalves(e,t);let r;try{r=await this.fetchImpl(C(this.url,G),{body:s,headers:this.rpcRequestHeaders({attachBookmark:!0}),method:"POST"})}catch{return{requeue:e,stop:!0}}const i=r.headers.get("x-d1-bookmark");if(i&&this.bookmark.set(i),r.status===413&&e.length>1)return await this.replayBatchedHalves(e,t);let n;const a=r.headers.get("retry-after");try{n=await r.json()}catch{return this.settleWholeBatchError(e,M(r.status,r.statusText,a),t)}if(!n.results){const c=n.error===void 0?M(r.status,r.statusText,a):Y(n.error,a);return this.settleWholeBatchError(e,c,t)}return{requeue:this.settleReplayBatchSlots(e,n.results,t),stop:!1}}settleWholeBatchError(e,t,s){if(t instanceof A||t.code!==void 0&&L.has(t.code))return this.noteReplayRetryDelay(s,t),{requeue:e,stop:!0};for(const r of e)this.settleReplayTerminal(r,t);return{requeue:[],stop:!1}}async replayBatchedHalves(e,t){const s=Math.ceil(e.length/2),r=await this.replayBatched(e.slice(0,s),t);if(r.stop)return{requeue:[...r.requeue,...e.slice(s)],stop:!0};const i=await this.replayBatched(e.slice(s),t);return{requeue:[...r.requeue,...i.requeue],stop:i.stop}}settleReplayBatchSlots(e,t,s){const r=new Map;for(const n of t)typeof n.id=="number"&&n.body!==void 0&&r.set(n.id,n.body);const i=[];for(const[n,a]of e.entries()){const c=r.get(n);c===void 0?i.push(a):"error"in c?L.has(c.error.code)?(this.noteReplayRetryDelay(s,_(c.error)),i.push(a)):this.settleReplayTerminal(a,_(c.error)):this.settleReplaySuccess(a,T(c.result),c.commitCursor)}return i}}export{se as LunoraClient};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{a as e,i}from"./delta-merge-BPM4sFID.mjs";export{e as applyDelta,i as isMutationDelta};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{c as t}from"./local-store-CNgeGW02.mjs";import"./SubscriptionRegistry-BQPU90ST.mjs";export{t as createLocalStore};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
const m=e=>{let t="";for(let o=0;o<e.length;o+=32768)t+=String.fromCharCode(...e.subarray(o,o+32768));return btoa(t)},p=e=>{const t=atob(e),a=new Uint8Array(t.length);for(let o=0;o<t.length;o+=1)a[o]=t.codePointAt(o)??0;return a},i="$lunora.wire$";const w="__proto__",g={BigInt64Array,BigUint64Array,Float32Array,Float64Array,Int8Array,Int16Array,Int32Array,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array},A={Error,EvalError,RangeError,ReferenceError,SyntaxError,TypeError,URIError},l=e=>{if(e===null||typeof e!="object")return!1;const t=Object.getPrototypeOf(e);return t===null||t===Object.prototype},u=(e,t=0)=>{if(t>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(e===void 0)return[i,"undefined"];if(e===null)return null;const a=typeof e;if(a==="bigint")return[i,"bigint",e.toString()];if(a==="number"){const r=e;return Number.isNaN(r)?[i,"nan"]:r===1/0?[i,"inf"]:r===-1/0?[i,"-inf"]:r}if(a!=="object")return e;if(e instanceof Date)return[i,"date",u(e.getTime(),t+1)];if(e instanceof Error){const r=e,n={};for(const f of Object.keys(r))r[f]!==void 0&&(n[f]=u(r[f],t+1));const c=[i,"error",r.name,r.message,n];return r.cause!==void 0&&c.push(u(r.cause,t+1)),c}if(e instanceof URL)return[i,"url",e.href];if(e instanceof Map)return[i,"map",[...e.entries()].map(([r,n])=>[u(r,t+1),u(n,t+1)])];if(e instanceof Set)return[i,"set",[...e].map(r=>u(r,t+1))];if(e instanceof ArrayBuffer)return[i,"bytes",m(new Uint8Array(e)),"ArrayBuffer"];if(ArrayBuffer.isView(e)){const r=e,n=r.constructor.name,c=new Uint8Array(r.buffer,r.byteOffset,r.byteLength);return n==="Uint8Array"?[i,"bytes",m(c)]:[i,"bytes",m(c),n]}if(Array.isArray(e)){const r=e.map(n=>u(n,t+1));return r.length>0&&r[0]===i?[i,"arr",r]:r}if(!l(e)){const r=e.constructor?.name??"value";throw new TypeError(`wire-codec: cannot encode a ${r} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`)}const o=e,s={};for(const r of Object.keys(o)){const n=o[r];if(n===void 0)continue;const c=u(n,t+1);r===w?Object.defineProperty(s,r,{configurable:!0,enumerable:!0,value:c,writable:!0}):s[r]=c}return s},y=(e,t=0)=>{if(t>64)throw new RangeError("wire-codec: value nesting exceeds the 64-level limit");if(e===null||typeof e!="object")return e;if(Array.isArray(e)){if(e[0]===i)switch(e[1]){case"-inf":return-1/0;case"arr":return e[2].map(r=>y(r,t+1));case"bigint":{const r=e[2];if(typeof r!="string"||r.length>1024||!/^-?\d+$/.test(r))throw new RangeError("wire-codec: invalid or over-long bigint (max 1024 digits)");return BigInt(r)}case"date":{if(e.length<3)throw new TypeError("wire-codec: malformed date — missing payload");return new Date(y(e[2],t+1))}case"map":{const r=e[2];return new Map(r.map(n=>{if(!Array.isArray(n)||n.length!==2)throw new TypeError("wire-codec: malformed map entry — expected a [key, value] pair");return[y(n[0],t+1),y(n[1],t+1)]}))}case"set":return new Set(e[2].map(r=>y(r,t+1)));case"url":return new URL(e[2]);case"error":{const r=e[2],n=e[3],c=(Object.hasOwn(A,r)?A[r]:void 0)??Error,f=new c(n);f.name!==r&&Object.defineProperty(f,"name",{configurable:!0,value:r,writable:!0});const b=y(e[4],t+1);if(b===null||typeof b!="object"||Array.isArray(b))throw new TypeError("wire-codec: malformed error — props must be an object");for(const d of Object.keys(b))d===w?Object.defineProperty(f,d,{configurable:!0,enumerable:!0,value:b[d],writable:!0}):f[d]=b[d];return e.length>5&&Object.defineProperty(f,"cause",{configurable:!0,value:y(e[5],t+1),writable:!0}),f}case"bytes":{const r=p(e[2]),n=e[3]??"Uint8Array";if(n==="ArrayBuffer")return r.buffer.byteLength===r.byteLength?r.buffer:r.slice().buffer;const c=Object.hasOwn(g,n)?g[n]:void 0;return c?new c(r.slice().buffer):r}case"inf":return 1/0;case"nan":return Number.NaN;case"undefined":return;default:return e.map(r=>y(r,t+1))}return e.map(s=>y(s,t+1))}const a=e,o={};for(const s of Object.keys(a)){const r=y(a[s],t+1);s===w?Object.defineProperty(o,s,{configurable:!0,enumerable:!0,value:r,writable:!0}):o[s]=r}return o};export{y as d,u as e,l as i};
|