@lunora/client 1.0.0-alpha.303 → 1.0.0-alpha.305

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.
@@ -1722,6 +1722,9 @@ type WSState = "idle" | "connecting" | "open" | "closed";
1722
1722
  * `polling` outranks `connecting`: while the fallback is running a reconnect is
1723
1723
  * still armed in the background, and reporting that attempt would flicker the
1724
1724
  * indicator between two states while data is in fact arriving on the slow path.
1725
+ *
1726
+ * One shard's status (`shardConnectionStatus`) uses the same values for that shard's
1727
+ * connection alone.
1725
1728
  */
1726
1729
  type ConnectionStatus = "connected" | "connecting" | "idle" | "offline" | "polling";
1727
1730
  /**
@@ -2271,7 +2274,7 @@ declare class LunoraClient {
2271
2274
  */
2272
2275
  private tabCoordinator;
2273
2276
  /**
2274
- * The leader's last-broadcast aggregate {@link ConnectionStatus}, mirrored
2277
+ * The leader's last-broadcast aggregate {@link ConnectionStatus} and per-shard map, mirrored
2275
2278
  * on a follower tab — which owns no `ShardConnection` of its own to
2276
2279
  * compute a status from (see `computeStatus`). `undefined` until the
2277
2280
  * leader's first broadcast (falls back to `"idle"`), and reset back to
@@ -2389,9 +2392,10 @@ declare class LunoraClient {
2389
2392
  * settled forever and shared with nobody — from a cookie app that simply
2390
2393
  * has not asked yet. Nothing in the client's own configuration says which
2391
2394
  * it is (`authBasePath` has a default), so the answer is declared: a
2392
- * `getCurrentUser()` call, or `@lunora/client/auth`'s identity store
2393
- * attaching itself, is what makes `null` provisional. An app that does
2394
- * neither keeps its read cache and its offline queue exactly as before.
2395
+ * `getCurrentUser()` call, `@lunora/client/auth`'s identity store attaching
2396
+ * itself, or — page-wide — `lunoraSessionSync()` being built is what makes
2397
+ * `null` provisional. An app that does none of these keeps its read cache
2398
+ * and its offline queue exactly as before.
2395
2399
  */
2396
2400
  private identityResolutionExpected;
2397
2401
  /**
@@ -2412,6 +2416,31 @@ declare class LunoraClient {
2412
2416
  * See `identityFingerprint` for the fingerprint shape.
2413
2417
  */
2414
2418
  private readonly queuedIdentities;
2419
+ /**
2420
+ * What a write composed before this client's identity first settled is
2421
+ * stamped with — see `writeIdentityStamp`. Random per instance, so a
2422
+ * write persisted by an earlier page can never claim this page's binding.
2423
+ */
2424
+ private provisionalStamp;
2425
+ /**
2426
+ * Provisional stamps retired by a session change before anything was bound
2427
+ * — see `retireProvisionalStamp`. A write carrying one was composed under a
2428
+ * session that is gone, so it never replays.
2429
+ */
2430
+ private readonly retiredProvisionalStamps;
2431
+ /**
2432
+ * The `identityQuestions` count when the current provisional stamp was minted. An
2433
+ * answer to a question asked before then — a socket upgraded under the session a
2434
+ * change replaced — describes that session, so it may not bind the stamp.
2435
+ */
2436
+ private provisionalStampQuestion;
2437
+ /** The identity this client settled on first, which `provisionalStamp` stands for; `undefined` until then. */
2438
+ private provisionalBinding;
2439
+ /** Set once this client asks `/get-session` on the page-wide declaration alone — see `resolveDeclaredSession`. */
2440
+ private resolvesDeclaredSession;
2441
+ /** Timed re-asks of a declared-session probe that failed while the connection stayed live, and the pending one. */
2442
+ private declaredSessionFailures;
2443
+ private declaredSessionRetryTimer;
2415
2444
  /**
2416
2445
  * Distinct shard keys with a mutation currently sitting in `offlineQueue`
2417
2446
  * — fresh writes queued this session (`enqueueOfflineMutation`) or writes
@@ -2428,6 +2457,15 @@ declare class LunoraClient {
2428
2457
  private readonly authTokenListeners;
2429
2458
  /** Subscribers to aggregate connection-status changes (see `onConnectionStatus`). */
2430
2459
  private readonly statusListeners;
2460
+ /** Subscribers to one shard's connection status, keyed by `shardKey ?? ""` (see `onShardConnectionStatus`). */
2461
+ private readonly shardStatusListeners;
2462
+ /** The status each listened-to shard last reported, so a socket transition on another shard notifies nobody here. */
2463
+ private readonly lastShardStatus;
2464
+ /**
2465
+ * The per-shard map this tab last sent its followers. A shard can reconnect while another shard's socket keeps the
2466
+ * aggregate `"connected"`, so the leader re-broadcasts on a change of this map as well as of the aggregate.
2467
+ */
2468
+ private lastBroadcastShards;
2431
2469
  /** Room handles' held-send flushers, keyed by the shard connection they send on. */
2432
2470
  private readonly roomFlushes;
2433
2471
  /** Room handles' pending-send resets, run when the identity changes so queued sends never cross identities. */
@@ -2592,13 +2630,28 @@ declare class LunoraClient {
2592
2630
  setAuthToken(token: string | null, subject?: string | null): void;
2593
2631
  getAuthToken(): string | null;
2594
2632
  /**
2595
- * The current identity fingerprint (the same stamp queued offline writes
2596
- * carry). Exposed so a durable {@link OutboxSink}'s replay handler — which
2597
- * owns its own at-least-once replay outside the built-in `OfflineQueue` —
2598
- * can drop a persisted write whose captured `identity` no longer matches the
2599
- * signed-in user, the guard the queue path applies in `flushOfflineQueue`.
2633
+ * The current identity fingerprint: a `subj:` label, a token hash, or `null`
2634
+ * for nobody. Key per-identity state on it. To stamp a durable write for a
2635
+ * later {@link replayIdentityVerdict}, use {@link writeIdentityStamp}, which
2636
+ * differs from this before the identity first settles.
2600
2637
  */
2601
2638
  currentIdentity(): string | null;
2639
+ /**
2640
+ * The identity stamp for a write composed now: what a durable
2641
+ * {@link OutboxSink} stores beside the write and later hands to
2642
+ * {@link replayIdentityVerdict}.
2643
+ *
2644
+ * The same as {@link currentIdentity}, except before this client's identity
2645
+ * first settles. There the fingerprint is a provisional `null` that the
2646
+ * session resolve in progress is about to name, and a `null` stamp reads as
2647
+ * anonymous: once the user's own session answered, the replay judged `null`
2648
+ * against `subj:<id>` as a different user and dropped the write, so a click
2649
+ * on page load, before `/get-session` returned, was lost. The provisional
2650
+ * stamp stands for whichever identity this client settles on first. Any
2651
+ * other client (a reload) reads it as the `null` it replaces, because it
2652
+ * cannot know which session minted it.
2653
+ */
2654
+ writeIdentityStamp(): string | null;
2602
2655
  /**
2603
2656
  * Declare that this client's identity is resolved by something — call it
2604
2657
  * before the first `getCurrentUser()`, not after.
@@ -3028,6 +3081,18 @@ declare class LunoraClient {
3028
3081
  * unsubscribe function.
3029
3082
  */
3030
3083
  onConnectionStatus(listener: (status: ConnectionStatus) => void): Unsubscribe;
3084
+ /**
3085
+ * Current status of the one socket for `shardKey` (`undefined` and `""` are the same shard). `"idle"` when
3086
+ * the client holds no connection for it. Unlike {@link connectionStatus}, a drop on this shard shows here even
3087
+ * while another shard's socket stays open.
3088
+ */
3089
+ shardConnectionStatus(shardKey?: string): ConnectionStatus;
3090
+ /**
3091
+ * Subscribe to one shard's connection-status changes (see {@link shardConnectionStatus}). Invokes `listener`
3092
+ * immediately with the current status, then on every transition of that shard only. Returns an unsubscribe
3093
+ * function.
3094
+ */
3095
+ onShardConnectionStatus(shardKey: string | undefined, listener: (status: ConnectionStatus) => void): Unsubscribe;
3031
3096
  /**
3032
3097
  * Whether the client is caught up: a live connection, every subscription
3033
3098
  * answered by the server, and no write waiting in the offline queue. Use this
@@ -3872,6 +3937,11 @@ declare class LunoraClient {
3872
3937
  private installPageHideFlush;
3873
3938
  /** Derive the aggregate status from the per-shard socket states. */
3874
3939
  private computeStatus;
3940
+ /** One shard's status: what {@link computeStatus} would report were this shard the client's only connection. */
3941
+ private computeShardStatus;
3942
+ /** Every connection's status, the per-shard map a leader sends its followers. */
3943
+ private shardStatuses;
3944
+ private broadcastConnectionStatus;
3875
3945
  /**
3876
3946
  * The CDC cursor this client's view of `shardKey` is at — the highest
3877
3947
  * `serverCursor` any live subscription on that shard has advanced to.
@@ -4439,6 +4509,50 @@ declare class LunoraClient {
4439
4509
  * fall back to the stale token-hash and wrongly reject the same user's write.
4440
4510
  */
4441
4511
  private isSameCredentialUnderTokenHash;
4512
+ /**
4513
+ * Fix what `provisionalStamp` stands for, once: the first identity this
4514
+ * client establishes. Only an identity actually established may bind it — a
4515
+ * response that names nobody (a 403, a superseded probe) binds nothing, or a
4516
+ * real answer after it could not rebind the stamp and would drop the write.
4517
+ */
4518
+ private bindProvisionalStamp;
4519
+ /**
4520
+ * Bind the provisional stamp to a bearer identity the app set itself. Bearer only:
4521
+ * a cookie answer arrives as `setAuthToken(null, subject)` and binds in
4522
+ * `adoptCookieSubject`, which can tell a stale answer from a current one; and
4523
+ * `null` names nobody, which only an adopted "no session" answer may bind.
4524
+ */
4525
+ private bindBearerProvisionalStamp;
4526
+ /**
4527
+ * A session change reported before anything bound the provisional stamp —
4528
+ * a sign-in or sign-out here, or in another tab — means the cookie the
4529
+ * pending writes were composed under is gone. Binding them to the identity
4530
+ * that answers next would run one user's write as another's, so the stamp
4531
+ * is retired (its writes are rejected) and writes from now on get a new one.
4532
+ */
4533
+ private retireProvisionalStamp;
4534
+ /**
4535
+ * Ask `/get-session` for a client whose only reason to expect a session is
4536
+ * the page-wide declaration (`lunoraSessionSync()`). No identity store is
4537
+ * attached to ask for it, and a socket that authenticates with a ticket or a
4538
+ * token, or none opening at all, never delivers the cookie identity, so
4539
+ * without this its early writes were held and its read cache refused for the
4540
+ * life of the page. A probe that fails is asked again when the connection is
4541
+ * live again (see the constructor), or after a capped backoff for as long as it
4542
+ * keeps failing while the connection stays live and no transition is coming.
4543
+ */
4544
+ private resolveDeclaredSession;
4545
+ /** One declared-session probe, then the retry it may need. */
4546
+ private askDeclaredSession;
4547
+ /** Re-ask a declared-session probe that got no answer while the connection stayed live, at a capped backoff. */
4548
+ private retryDeclaredSession;
4549
+ /**
4550
+ * The identity a write stamp stands for. This client's provisional stamp is
4551
+ * the identity it settled on first (`undefined` until then); another
4552
+ * client's reads as the `null` it replaced, since this one cannot know which
4553
+ * session minted it; any other stamp is itself.
4554
+ */
4555
+ private stampedIdentity;
4442
4556
  /**
4443
4557
  * Release one {@link sessionProbesInFlight} hold and re-flush the writes
4444
4558
  * that hold alone was holding.
@@ -1722,6 +1722,9 @@ type WSState = "idle" | "connecting" | "open" | "closed";
1722
1722
  * `polling` outranks `connecting`: while the fallback is running a reconnect is
1723
1723
  * still armed in the background, and reporting that attempt would flicker the
1724
1724
  * indicator between two states while data is in fact arriving on the slow path.
1725
+ *
1726
+ * One shard's status (`shardConnectionStatus`) uses the same values for that shard's
1727
+ * connection alone.
1725
1728
  */
1726
1729
  type ConnectionStatus = "connected" | "connecting" | "idle" | "offline" | "polling";
1727
1730
  /**
@@ -2271,7 +2274,7 @@ declare class LunoraClient {
2271
2274
  */
2272
2275
  private tabCoordinator;
2273
2276
  /**
2274
- * The leader's last-broadcast aggregate {@link ConnectionStatus}, mirrored
2277
+ * The leader's last-broadcast aggregate {@link ConnectionStatus} and per-shard map, mirrored
2275
2278
  * on a follower tab — which owns no `ShardConnection` of its own to
2276
2279
  * compute a status from (see `computeStatus`). `undefined` until the
2277
2280
  * leader's first broadcast (falls back to `"idle"`), and reset back to
@@ -2389,9 +2392,10 @@ declare class LunoraClient {
2389
2392
  * settled forever and shared with nobody — from a cookie app that simply
2390
2393
  * has not asked yet. Nothing in the client's own configuration says which
2391
2394
  * it is (`authBasePath` has a default), so the answer is declared: a
2392
- * `getCurrentUser()` call, or `@lunora/client/auth`'s identity store
2393
- * attaching itself, is what makes `null` provisional. An app that does
2394
- * neither keeps its read cache and its offline queue exactly as before.
2395
+ * `getCurrentUser()` call, `@lunora/client/auth`'s identity store attaching
2396
+ * itself, or — page-wide — `lunoraSessionSync()` being built is what makes
2397
+ * `null` provisional. An app that does none of these keeps its read cache
2398
+ * and its offline queue exactly as before.
2395
2399
  */
2396
2400
  private identityResolutionExpected;
2397
2401
  /**
@@ -2412,6 +2416,31 @@ declare class LunoraClient {
2412
2416
  * See `identityFingerprint` for the fingerprint shape.
2413
2417
  */
2414
2418
  private readonly queuedIdentities;
2419
+ /**
2420
+ * What a write composed before this client's identity first settled is
2421
+ * stamped with — see `writeIdentityStamp`. Random per instance, so a
2422
+ * write persisted by an earlier page can never claim this page's binding.
2423
+ */
2424
+ private provisionalStamp;
2425
+ /**
2426
+ * Provisional stamps retired by a session change before anything was bound
2427
+ * — see `retireProvisionalStamp`. A write carrying one was composed under a
2428
+ * session that is gone, so it never replays.
2429
+ */
2430
+ private readonly retiredProvisionalStamps;
2431
+ /**
2432
+ * The `identityQuestions` count when the current provisional stamp was minted. An
2433
+ * answer to a question asked before then — a socket upgraded under the session a
2434
+ * change replaced — describes that session, so it may not bind the stamp.
2435
+ */
2436
+ private provisionalStampQuestion;
2437
+ /** The identity this client settled on first, which `provisionalStamp` stands for; `undefined` until then. */
2438
+ private provisionalBinding;
2439
+ /** Set once this client asks `/get-session` on the page-wide declaration alone — see `resolveDeclaredSession`. */
2440
+ private resolvesDeclaredSession;
2441
+ /** Timed re-asks of a declared-session probe that failed while the connection stayed live, and the pending one. */
2442
+ private declaredSessionFailures;
2443
+ private declaredSessionRetryTimer;
2415
2444
  /**
2416
2445
  * Distinct shard keys with a mutation currently sitting in `offlineQueue`
2417
2446
  * — fresh writes queued this session (`enqueueOfflineMutation`) or writes
@@ -2428,6 +2457,15 @@ declare class LunoraClient {
2428
2457
  private readonly authTokenListeners;
2429
2458
  /** Subscribers to aggregate connection-status changes (see `onConnectionStatus`). */
2430
2459
  private readonly statusListeners;
2460
+ /** Subscribers to one shard's connection status, keyed by `shardKey ?? ""` (see `onShardConnectionStatus`). */
2461
+ private readonly shardStatusListeners;
2462
+ /** The status each listened-to shard last reported, so a socket transition on another shard notifies nobody here. */
2463
+ private readonly lastShardStatus;
2464
+ /**
2465
+ * The per-shard map this tab last sent its followers. A shard can reconnect while another shard's socket keeps the
2466
+ * aggregate `"connected"`, so the leader re-broadcasts on a change of this map as well as of the aggregate.
2467
+ */
2468
+ private lastBroadcastShards;
2431
2469
  /** Room handles' held-send flushers, keyed by the shard connection they send on. */
2432
2470
  private readonly roomFlushes;
2433
2471
  /** Room handles' pending-send resets, run when the identity changes so queued sends never cross identities. */
@@ -2592,13 +2630,28 @@ declare class LunoraClient {
2592
2630
  setAuthToken(token: string | null, subject?: string | null): void;
2593
2631
  getAuthToken(): string | null;
2594
2632
  /**
2595
- * The current identity fingerprint (the same stamp queued offline writes
2596
- * carry). Exposed so a durable {@link OutboxSink}'s replay handler — which
2597
- * owns its own at-least-once replay outside the built-in `OfflineQueue` —
2598
- * can drop a persisted write whose captured `identity` no longer matches the
2599
- * signed-in user, the guard the queue path applies in `flushOfflineQueue`.
2633
+ * The current identity fingerprint: a `subj:` label, a token hash, or `null`
2634
+ * for nobody. Key per-identity state on it. To stamp a durable write for a
2635
+ * later {@link replayIdentityVerdict}, use {@link writeIdentityStamp}, which
2636
+ * differs from this before the identity first settles.
2600
2637
  */
2601
2638
  currentIdentity(): string | null;
2639
+ /**
2640
+ * The identity stamp for a write composed now: what a durable
2641
+ * {@link OutboxSink} stores beside the write and later hands to
2642
+ * {@link replayIdentityVerdict}.
2643
+ *
2644
+ * The same as {@link currentIdentity}, except before this client's identity
2645
+ * first settles. There the fingerprint is a provisional `null` that the
2646
+ * session resolve in progress is about to name, and a `null` stamp reads as
2647
+ * anonymous: once the user's own session answered, the replay judged `null`
2648
+ * against `subj:<id>` as a different user and dropped the write, so a click
2649
+ * on page load, before `/get-session` returned, was lost. The provisional
2650
+ * stamp stands for whichever identity this client settles on first. Any
2651
+ * other client (a reload) reads it as the `null` it replaces, because it
2652
+ * cannot know which session minted it.
2653
+ */
2654
+ writeIdentityStamp(): string | null;
2602
2655
  /**
2603
2656
  * Declare that this client's identity is resolved by something — call it
2604
2657
  * before the first `getCurrentUser()`, not after.
@@ -3028,6 +3081,18 @@ declare class LunoraClient {
3028
3081
  * unsubscribe function.
3029
3082
  */
3030
3083
  onConnectionStatus(listener: (status: ConnectionStatus) => void): Unsubscribe;
3084
+ /**
3085
+ * Current status of the one socket for `shardKey` (`undefined` and `""` are the same shard). `"idle"` when
3086
+ * the client holds no connection for it. Unlike {@link connectionStatus}, a drop on this shard shows here even
3087
+ * while another shard's socket stays open.
3088
+ */
3089
+ shardConnectionStatus(shardKey?: string): ConnectionStatus;
3090
+ /**
3091
+ * Subscribe to one shard's connection-status changes (see {@link shardConnectionStatus}). Invokes `listener`
3092
+ * immediately with the current status, then on every transition of that shard only. Returns an unsubscribe
3093
+ * function.
3094
+ */
3095
+ onShardConnectionStatus(shardKey: string | undefined, listener: (status: ConnectionStatus) => void): Unsubscribe;
3031
3096
  /**
3032
3097
  * Whether the client is caught up: a live connection, every subscription
3033
3098
  * answered by the server, and no write waiting in the offline queue. Use this
@@ -3872,6 +3937,11 @@ declare class LunoraClient {
3872
3937
  private installPageHideFlush;
3873
3938
  /** Derive the aggregate status from the per-shard socket states. */
3874
3939
  private computeStatus;
3940
+ /** One shard's status: what {@link computeStatus} would report were this shard the client's only connection. */
3941
+ private computeShardStatus;
3942
+ /** Every connection's status, the per-shard map a leader sends its followers. */
3943
+ private shardStatuses;
3944
+ private broadcastConnectionStatus;
3875
3945
  /**
3876
3946
  * The CDC cursor this client's view of `shardKey` is at — the highest
3877
3947
  * `serverCursor` any live subscription on that shard has advanced to.
@@ -4439,6 +4509,50 @@ declare class LunoraClient {
4439
4509
  * fall back to the stale token-hash and wrongly reject the same user's write.
4440
4510
  */
4441
4511
  private isSameCredentialUnderTokenHash;
4512
+ /**
4513
+ * Fix what `provisionalStamp` stands for, once: the first identity this
4514
+ * client establishes. Only an identity actually established may bind it — a
4515
+ * response that names nobody (a 403, a superseded probe) binds nothing, or a
4516
+ * real answer after it could not rebind the stamp and would drop the write.
4517
+ */
4518
+ private bindProvisionalStamp;
4519
+ /**
4520
+ * Bind the provisional stamp to a bearer identity the app set itself. Bearer only:
4521
+ * a cookie answer arrives as `setAuthToken(null, subject)` and binds in
4522
+ * `adoptCookieSubject`, which can tell a stale answer from a current one; and
4523
+ * `null` names nobody, which only an adopted "no session" answer may bind.
4524
+ */
4525
+ private bindBearerProvisionalStamp;
4526
+ /**
4527
+ * A session change reported before anything bound the provisional stamp —
4528
+ * a sign-in or sign-out here, or in another tab — means the cookie the
4529
+ * pending writes were composed under is gone. Binding them to the identity
4530
+ * that answers next would run one user's write as another's, so the stamp
4531
+ * is retired (its writes are rejected) and writes from now on get a new one.
4532
+ */
4533
+ private retireProvisionalStamp;
4534
+ /**
4535
+ * Ask `/get-session` for a client whose only reason to expect a session is
4536
+ * the page-wide declaration (`lunoraSessionSync()`). No identity store is
4537
+ * attached to ask for it, and a socket that authenticates with a ticket or a
4538
+ * token, or none opening at all, never delivers the cookie identity, so
4539
+ * without this its early writes were held and its read cache refused for the
4540
+ * life of the page. A probe that fails is asked again when the connection is
4541
+ * live again (see the constructor), or after a capped backoff for as long as it
4542
+ * keeps failing while the connection stays live and no transition is coming.
4543
+ */
4544
+ private resolveDeclaredSession;
4545
+ /** One declared-session probe, then the retry it may need. */
4546
+ private askDeclaredSession;
4547
+ /** Re-ask a declared-session probe that got no answer while the connection stayed live, at a capped backoff. */
4548
+ private retryDeclaredSession;
4549
+ /**
4550
+ * The identity a write stamp stands for. This client's provisional stamp is
4551
+ * the identity it settled on first (`undefined` until then); another
4552
+ * client's reads as the `null` it replaced, since this one cannot know which
4553
+ * session minted it; any other stamp is itself.
4554
+ */
4555
+ private stampedIdentity;
4442
4556
  /**
4443
4557
  * Release one {@link sessionProbesInFlight} hold and re-flush the writes
4444
4558
  * that hold alone was holding.
@@ -1,4 +1,4 @@
1
- import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-CrkO-uRI.js";
1
+ import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-DAYSIRfJ.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-BxU8-4s5.mjs";
1
+ import { L as LunoraClient, P as Preloaded } from "./lunora-client.d-B6SGaCji.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
@@ -1,5 +1,5 @@
1
1
  import { F as FunctionReference, R as ReturnOf, A as ArgsOf } from "../packem_shared/function-reference.d-Br_hsKje.mjs";
2
- import { L as LunoraClient, S as SubscriptionError, a as Unsubscribe } from "../packem_shared/lunora-client.d-BxU8-4s5.mjs";
2
+ import { L as LunoraClient, S as SubscriptionError, a as Unsubscribe } from "../packem_shared/lunora-client.d-B6SGaCji.mjs";
3
3
  export type {
4
4
  /**
5
5
  * `@lunora/client/query` — the framework-neutral live-query state machine shared
@@ -15,7 +15,7 @@ export type {
15
15
  * `lunoraQueryOptions`), so every adapter and server entry keys a Lunora query
16
16
  * the same way.
17
17
  */
18
- b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-BxU8-4s5.mjs";
18
+ b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-B6SGaCji.mjs";
19
19
  import '@lunora/errors';
20
20
  /**
21
21
  * The TanStack Query cache-key contract for Lunora queries. It lives here, not
@@ -1,5 +1,5 @@
1
1
  import { F as FunctionReference, R as ReturnOf, A as ArgsOf } from "../packem_shared/function-reference.d-Br_hsKje.js";
2
- import { L as LunoraClient, S as SubscriptionError, a as Unsubscribe } from "../packem_shared/lunora-client.d-CrkO-uRI.js";
2
+ import { L as LunoraClient, S as SubscriptionError, a as Unsubscribe } from "../packem_shared/lunora-client.d-DAYSIRfJ.js";
3
3
  export type {
4
4
  /**
5
5
  * `@lunora/client/query` — the framework-neutral live-query state machine shared
@@ -15,7 +15,7 @@ export type {
15
15
  * `lunoraQueryOptions`), so every adapter and server entry keys a Lunora query
16
16
  * the same way.
17
17
  */
18
- b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-CrkO-uRI.js";
18
+ b as SubscriptionErrorCallback } from "../packem_shared/lunora-client.d-DAYSIRfJ.js";
19
19
  import '@lunora/errors';
20
20
  /**
21
21
  * The TanStack Query cache-key contract for Lunora queries. It lives here, not
@@ -1,4 +1,4 @@
1
- import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-BxU8-4s5.mjs";
1
+ import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-B6SGaCji.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-ByaSovKg.mjs";
23
+ a as preloadedQueryResult } from "../packem_shared/preload.d-DsEmizfC.mjs";
24
24
  import '@lunora/errors';
25
25
  /**
26
26
  * Structural shape of a better-auth `getSession` call's resolved value.
@@ -1,4 +1,4 @@
1
- import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-CrkO-uRI.js";
1
+ import { P as Preloaded, L as LunoraClient } from "../packem_shared/lunora-client.d-DAYSIRfJ.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-BB2CUNhN.js";
23
+ a as preloadedQueryResult } from "../packem_shared/preload.d-DCe79crj.js";
24
24
  import '@lunora/errors';
25
25
  /**
26
26
  * Structural shape of a better-auth `getSession` call's resolved value.
@@ -1 +1 @@
1
- import{getServerSession as o}from"../packem_shared/getServerSession-BaOoo1p6.mjs";import{deserializePreloaded as l,serializePreloaded as t}from"../packem_shared/deserializePreloaded-DxozvweR.mjs";import{createServerClient as i}from"../packem_shared/createServerClient-6RlA9jJE.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};
1
+ import{getServerSession as o}from"../packem_shared/getServerSession-BaOoo1p6.mjs";import{deserializePreloaded as l,serializePreloaded as t}from"../packem_shared/deserializePreloaded-DxozvweR.mjs";import{createServerClient as i}from"../packem_shared/createServerClient-BPVmVrl9.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunora/client",
3
- "version": "1.0.0-alpha.303",
3
+ "version": "1.0.0-alpha.305",
4
4
  "description": "Lunora browser SDK: WebSocket transport, optimistic updates, and an offline mutation queue",
5
5
  "keywords": [
6
6
  "cloudflare",
@@ -1 +0,0 @@
1
- import{w as o}from"./optimistic-layers-BI61YTO3.mjs";let s=0;const h="lunora-bridge",b=1e3,l=3e3;class p{bc;tabId;heartbeatInterval;leaderTimeout;knownLeader=void 0;leader=!1;running=!1;promotionPending=!1;lastHeartbeat=0;pendingData=new Map;heldData=new Map;heartbeatTimer=void 0;leaderCheckTimer=void 0;onBecomeLeader;onStopBeingLeader;onConnectionStatus;onSubscriptionData;onLeaderClaimAnswered;onSubscriptionError;onSubscriptionSettled;constructor(t={}){if(s+=1,this.tabId=`tab_${String(s)}_${crypto.randomUUID()}`,this.heartbeatInterval=t.heartbeatInterval??b,this.leaderTimeout=t.leaderTimeout??l,this.onBecomeLeader=t.onBecomeLeader,this.onStopBeingLeader=t.onStopBeingLeader,this.onConnectionStatus=t.onConnectionStatus,this.onSubscriptionData=t.onSubscriptionData,this.onLeaderClaimAnswered=t.onLeaderClaimAnswered,this.onSubscriptionError=t.onSubscriptionError,this.onSubscriptionSettled=t.onSubscriptionSettled,typeof BroadcastChannel>"u"){this.bc=void 0;return}this.bc=new BroadcastChannel(t.channelName??h),this.bc.addEventListener("message",e=>{this.handleMessage(e.data)})}start(){if(!this.running){if(this.running=!0,this.bc===void 0){this.becomeLeader();return}this.broadcast({type:"claim-leadership",tabId:this.tabId,ts:Date.now()}),setTimeout(()=>{this.running&&this.knownLeader===void 0&&this.becomeLeader()},this.leaderTimeout),this.leaderCheckTimer=setInterval(()=>{this.checkLeaderHealth()},this.leaderTimeout),this.heartbeatTimer=setInterval(()=>{this.sendHeartbeat()},this.heartbeatInterval)}}stop(){this.running=!1,this.leader&&(this.broadcast({type:"yield-leadership",tabId:this.tabId}),this.leader=!1,this.knownLeader=void 0,this.onStopBeingLeader?.()),this.heartbeatTimer!==void 0&&(clearInterval(this.heartbeatTimer),this.heartbeatTimer=void 0),this.leaderCheckTimer!==void 0&&(clearInterval(this.leaderCheckTimer),this.leaderCheckTimer=void 0),this.bc?.close()}isLeader(){return this.leader}promoteImmediately(){this.running&&this.becomeLeader()}get leaderTabId(){return this.knownLeader}get id(){return this.tabId}get isRunning(){return this.running}broadcastSubscriptionData(t,e,i,a,r,n){this.leader&&(this.pendingData.size===0&&queueMicrotask(()=>{this.flushPendingData()}),this.pendingData.set(t,{type:"subscription-data",tabId:this.tabId,key:t,data:e,...i===void 0?{}:{cursor:i},...a===void 0?{}:{epoch:a},...r===void 0?{}:{identity:r},...n===void 0?{}:{claims:n}}))}broadcastSubscriptionError(t,e,i,a){this.leader&&this.broadcast({type:"subscription-error",tabId:this.tabId,key:t,error:e,...i===void 0?{}:{identity:i},...a===void 0?{}:{claims:a}})}broadcastSubscriptionSettled(t,e,i,a,r,n,d){this.leader&&this.broadcast({type:"subscription-settled",tabId:this.tabId,key:t,...e===void 0?{}:{cursor:e},...i===void 0?{}:{epoch:i},...a===void 0?{}:{lastMutationId:a},...r===void 0?{}:{clientId:r},...n===void 0?{}:{identity:n},...d===void 0?{}:{claims:d}})}broadcastConnectionStatus(t,e){this.leader&&this.broadcast({type:"connection-status",tabId:this.tabId,status:t,...e===void 0?{}:{identity:e}})}broadcast(t){this.flushPendingData(),this.bc?.postMessage(t)}flushPendingData(){if(this.pendingData.size===0)return;const t=[...this.pendingData.values()];if(this.pendingData.clear(),!this.leader)return;let e,i=!0;for(const[a,r]of t.entries()){const n=a<t.length-1;try{this.bc?.postMessage(n?{...r,more:!0}:r),e=r,i=!n}catch{}}!i&&e!==void 0&&this.bc?.postMessage(e)}applySubscriptionData(t){const e=this.heldData.get(t.tabId)??[];if(e.push(t),t.more===!0){this.heldData.set(t.tabId,e);return}this.heldData.delete(t.tabId),o(()=>{for(const i of e)this.onSubscriptionData?.(i.key,i.data,i.cursor,i.epoch,i.identity,i.claims)},()=>this.running)}handleMessage(t){switch(t.type){case"claim-leadership":{this.handleClaimLeadership(t);break}case"connection-status":{if(this.leader||t.tabId===this.tabId)break;this.onConnectionStatus?.(t.status,t.identity);break}case"heartbeat":{this.handleHeartbeat(t);break}case"subscription-data":{if(this.leader||t.tabId===this.tabId)break;this.applySubscriptionData(t);break}case"subscription-error":{if(this.leader||t.tabId===this.tabId)break;this.onSubscriptionError?.(t.key,t.error,t.identity,t.claims);break}case"subscription-settled":{if(this.leader||t.tabId===this.tabId)break;this.onSubscriptionSettled?.(t.key,t.cursor,t.epoch,t.lastMutationId,t.clientId,t.identity,t.claims);break}case"yield-leadership":{this.heldData.delete(t.tabId),this.knownLeader===t.tabId&&(this.knownLeader=void 0,this.running&&!this.leader&&this.claimAndPromote());break}}}handleClaimLeadership(t){if(t.tabId!==this.tabId){if(this.leader){this.broadcast({type:"heartbeat",tabId:this.tabId,ts:Date.now()}),this.onLeaderClaimAnswered?.();return}this.knownLeader===void 0&&t.tabId<this.tabId&&(this.knownLeader=t.tabId,this.lastHeartbeat=t.ts)}}handleHeartbeat(t){if(t.tabId!==this.tabId){if(this.leader){this.resolveLeaderVsLeaderTieBreak(t);return}this.lastHeartbeat=t.ts,this.knownLeader=t.tabId}}resolveLeaderVsLeaderTieBreak(t){t.tabId<this.tabId?(this.leader=!1,this.knownLeader=t.tabId,this.lastHeartbeat=t.ts,this.onStopBeingLeader?.()):this.broadcast({type:"heartbeat",tabId:this.tabId,ts:Date.now()})}becomeLeader(){this.leader||(this.leader=!0,this.knownLeader=this.tabId,this.lastHeartbeat=Date.now(),this.onBecomeLeader?.(),this.broadcast({type:"heartbeat",tabId:this.tabId,ts:Date.now()}))}sendHeartbeat(){this.leader&&this.broadcast({type:"heartbeat",tabId:this.tabId,ts:Date.now()})}checkLeaderHealth(){if(!this.running||this.leader)return;if(this.knownLeader===void 0){this.claimAndPromote();return}Date.now()-this.lastHeartbeat>this.leaderTimeout&&(this.heldData.delete(this.knownLeader),this.knownLeader=void 0,this.claimAndPromote())}claimAndPromote(){this.promotionPending||(this.promotionPending=!0,this.broadcast({type:"claim-leadership",tabId:this.tabId,ts:Date.now()}),setTimeout(()=>{this.promotionPending=!1,this.running&&this.knownLeader===void 0&&this.becomeLeader()},this.leaderTimeout))}}export{p as TabCoordinator};