@abraca/dabra 2.93.0 → 2.95.0

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/index.d.ts CHANGED
@@ -397,6 +397,28 @@ interface AbracadabraClientConfig {
397
397
  * concurrent invocations.
398
398
  */
399
399
  onAuthFailed?: (ctx: AuthFailureContext) => void;
400
+ /**
401
+ * How long before a token's `exp` the session is re-minted, in ms.
402
+ *
403
+ * Only meaningful once a session renewer is installed — which
404
+ * {@link AbracadabraClient.loginWithKey} does for you. Capped at a third of
405
+ * the token's own lifetime, so a server issuing short-lived tokens is not
406
+ * re-authenticated on every check.
407
+ *
408
+ * Default 20 minutes: comfortably inside the server's 4-hour crypto-auth
409
+ * TTL and wider than any polling interval a consumer is likely to run.
410
+ */
411
+ tokenRenewMarginMs?: number;
412
+ /**
413
+ * Fallback interval, in ms, for a token whose `exp` cannot be read.
414
+ * Default 60s.
415
+ *
416
+ * Renewal is normally scheduled from the token's own expiry, so this is not
417
+ * a poll. `0` disables the timer entirely — the session is then renewed
418
+ * only on demand (before a socket handshake, or an explicit
419
+ * {@link AbracadabraClient.ensureFreshToken}).
420
+ */
421
+ tokenRenewCheckMs?: number;
400
422
  }
401
423
  declare class AbracadabraClient {
402
424
  private _token;
@@ -418,12 +440,78 @@ declare class AbracadabraClient {
418
440
  /** In-flight mints, keyed like `streamTokens` — collapses the burst a page
419
441
  * of media blocks produces into one request per upload. */
420
442
  private readonly streamTokenInflight;
443
+ private _renew;
444
+ private _renewing;
445
+ private _renewTimer;
446
+ private readonly _tokenListeners;
447
+ private readonly renewMarginMs;
448
+ private readonly renewCheckMs;
421
449
  constructor(config: AbracadabraClientConfig);
422
450
  get token(): string | null;
423
451
  set token(value: string | null);
424
452
  get isAuthenticated(): boolean;
425
453
  /** Check if the current JWT token is present and not expired. */
426
454
  isTokenValid(): boolean;
455
+ /** When the current token stops validating, in ms. `0` when unknown. */
456
+ get tokenExpiresAt(): number;
457
+ /**
458
+ * Install the way this client re-authenticates itself, or `null` to stop.
459
+ *
460
+ * {@link loginWithKey} installs one for you, so a key-authenticated client
461
+ * — every Node runner, and every browser session on a crypto identity —
462
+ * renews without its consumer doing anything. Install one by hand for a
463
+ * password session or any other flow that can mint a fresh token.
464
+ *
465
+ * The function must return the new JWT; adopting it (and notifying open
466
+ * sockets) is this client's job.
467
+ */
468
+ setSessionRenewer(renew: (() => Promise<string>) | null): void;
469
+ /** Whether this client knows how to re-authenticate itself. */
470
+ get canRenewSession(): boolean;
471
+ /**
472
+ * Called whenever the session token changes — a login, a renewal, a device
473
+ * refresh, a key revocation. Providers use this to re-arm sockets that are
474
+ * already open. Returns an unsubscribe function.
475
+ */
476
+ onTokenChanged(listener: (token: string) => void): () => void;
477
+ /**
478
+ * Make sure the token has life left in it, re-minting if it does not.
479
+ *
480
+ * Cheap when the token is fresh — one subtraction — so callers need not
481
+ * reason about when the last renewal was, and concurrent callers share one
482
+ * handshake. Returns whether the session is believed good; `false` is not
483
+ * fatal, and the caller's own failure is still the authority.
484
+ */
485
+ ensureFreshToken(marginMs?: number): Promise<boolean>;
486
+ /**
487
+ * How long before `exp` this session is re-minted.
488
+ *
489
+ * Capped at a third of the token's own lifetime, so a server configured
490
+ * with short-lived tokens is not re-authenticated on every check — without
491
+ * the cap a 45-second token against a 20-minute margin would mint a new
492
+ * session every time anything asked.
493
+ */
494
+ private renewMargin;
495
+ /**
496
+ * Arm a single timer for the moment this token needs replacing.
497
+ *
498
+ * Scheduled from the token's OWN expiry rather than on a fixed interval,
499
+ * and that is not tidiness. A poll can outrun the margin: measured against
500
+ * a server issuing 45-second tokens, a 60-second poll first fired fifteen
501
+ * seconds AFTER the token had already lapsed — long enough for the
502
+ * awareness heartbeat to hit an expired session and have the socket dropped
503
+ * under it. A deadline cannot be outrun by the thing it is a deadline for.
504
+ *
505
+ * Re-armed on every token change, so a renewal schedules the next one.
506
+ */
507
+ private scheduleSessionRenewal;
508
+ private stopSessionRenewal;
509
+ /**
510
+ * Stop renewing and drop every listener. Call when a client is discarded —
511
+ * a long-lived consumer that builds one client per connection would
512
+ * otherwise leave a timer per discarded client.
513
+ */
514
+ destroy(): void;
427
515
  /** Derives ws:// or wss:// URL from the http(s) base URL. */
428
516
  get wsUrl(): string;
429
517
  /** Register a new user with password. */
@@ -1541,6 +1629,8 @@ declare class AbracadabraProvider extends AbracadabraBaseProvider {
1541
1629
  private static readonly DEFAULT_MAX_CHILDREN;
1542
1630
  private readonly maxChildren;
1543
1631
  private abracadabraConfig;
1632
+ /** Detach from the client's token broadcasts. See the constructor. */
1633
+ private unsubscribeToken;
1544
1634
  private readonly boundHandleYSubdocsChange;
1545
1635
  /**
1546
1636
  * Resolves once the document has been pre-populated from the local
@@ -2653,6 +2743,33 @@ declare class AbracadabraWS extends EventEmitter {
2653
2743
  */
2654
2744
  reconnect(): Promise<unknown>;
2655
2745
  send(message: any): void;
2746
+ /** The last token this socket was re-armed with — see {@link refreshAuth}. */
2747
+ private lastRefreshToken;
2748
+ /**
2749
+ * Re-arm this CONNECTION with a freshly minted JWT, in place.
2750
+ *
2751
+ * The server tracks a connection's expiry as one shared value
2752
+ * (`SyncSession`'s `jwt_exp`, an `Arc<AtomicI64>` every session on the socket
2753
+ * points at) and rejects every non-stateless frame once it passes — dropping
2754
+ * the whole socket for an ordinary session. `auth:refresh {"token":…}` moves
2755
+ * that value, and it rides `MSG_STATELESS` precisely because stateless is the
2756
+ * one frame kind an expired session may still send.
2757
+ *
2758
+ * So ONE frame, through any authenticated document, re-arms everything
2759
+ * multiplexed here. Sending it per attached provider would be N identical
2760
+ * frames on a shared socket carrying sixty documents; `lastRefreshToken` is
2761
+ * what keeps it at one.
2762
+ *
2763
+ * Verified against a live server with a 45-second token TTL, on an ORDINARY
2764
+ * (non-elevated) account, refreshing before the lapse so the socket is never
2765
+ * dropped and never re-handshaken: without the frame the socket is closed
2766
+ * past the original expiry, with it `close` never fires at all.
2767
+ *
2768
+ * Returns whether a frame actually went out. `false` means there was nobody
2769
+ * to send it through — harmless, because the next handshake carries the new
2770
+ * token anyway (see the provider's token closure).
2771
+ */
2772
+ refreshAuth(token: string): boolean;
2656
2773
  onClose({
2657
2774
  event
2658
2775
  }: onCloseParameters): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abraca/dabra",
3
- "version": "2.93.0",
3
+ "version": "2.95.0",
4
4
  "description": "abracadabra provider",
5
5
  "keywords": [
6
6
  "abracadabra",
@@ -35,7 +35,7 @@
35
35
  "yjs": "^13.6.8"
36
36
  },
37
37
  "devDependencies": {
38
- "@abraca/schema": "2.93.0"
38
+ "@abraca/schema": "2.95.0"
39
39
  },
40
40
  "scripts": {
41
41
  "test": "node --no-warnings --conditions=source --experimental-transform-types --test 'tests/*.test.ts'"