@spacelr/sdk 0.10.11 → 0.11.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.mts +83 -6
- package/dist/index.d.ts +83 -6
- package/dist/index.js +92 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +92 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -171,17 +171,27 @@ var HttpClient = class {
|
|
|
171
171
|
* caller should retry with a refreshed token. 403 is never routed here —
|
|
172
172
|
* it means "forbidden for this action" and must not trigger logout.
|
|
173
173
|
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
174
|
+
* Auth-loss ownership (#759 review):
|
|
175
|
+
* - When a refresh WAS possible (a refresh token existed), `doRefresh` owns
|
|
176
|
+
* the outcome: it clears + emits on a server-confirmed rejection, and stays
|
|
177
|
+
* silent on a transient transport failure. We must not second-guess it, so
|
|
178
|
+
* we do NOT emit here — that avoids a double clear/emit and stops a transient
|
|
179
|
+
* blip during a 401 retry from ejecting the user.
|
|
180
|
+
* - When no refresh was possible (no refresh token), this 401 IS the confirmed
|
|
181
|
+
* loss and we own it: `notifyAuthLost` clears the dead session then emits.
|
|
182
|
+
* The single clear always runs before any listener, so a listener that
|
|
183
|
+
* re-logs-in via `setTokens()` can never have its fresh session erased.
|
|
176
184
|
*/
|
|
177
185
|
async recoverFromAuthFailure(isRetry) {
|
|
178
186
|
if (isRetry) {
|
|
179
|
-
this.tokenManager.
|
|
187
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
180
188
|
return false;
|
|
181
189
|
}
|
|
190
|
+
const hadRefreshToken = await this.tokenManager.hasRefreshToken();
|
|
182
191
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
183
192
|
if (refreshed) return true;
|
|
184
|
-
|
|
193
|
+
if (hadRefreshToken) return false;
|
|
194
|
+
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
185
195
|
return false;
|
|
186
196
|
}
|
|
187
197
|
async uploadFormOnce(url, headers, formData, timeoutMs) {
|
|
@@ -413,6 +423,11 @@ var TokenManager = class {
|
|
|
413
423
|
// that hits `/auth/logout` with a dead token) don't re-emit and loop.
|
|
414
424
|
// Cleared by setTokens() / clearTokens() to re-arm for the next session.
|
|
415
425
|
this.authLostEmitted = false;
|
|
426
|
+
// The in-flight auth-loss storage wipe, if any. `setTokens` awaits it before
|
|
427
|
+
// writing so a re-login that lands DURING the wipe (e.g. an external
|
|
428
|
+
// auth-components bridge calling setTokens concurrently with a background
|
|
429
|
+
// refresh failure) is applied AFTER the wipe and never erased (#759 review).
|
|
430
|
+
this.pendingClear = null;
|
|
416
431
|
this.storage = storage ?? new MemoryTokenStorage();
|
|
417
432
|
this.refreshBufferSeconds = refreshBufferSeconds;
|
|
418
433
|
this.refreshLockName = refreshLockName;
|
|
@@ -434,6 +449,7 @@ var TokenManager = class {
|
|
|
434
449
|
return tokens.accessToken;
|
|
435
450
|
}
|
|
436
451
|
async setTokens(tokens) {
|
|
452
|
+
if (this.pendingClear) await this.pendingClear;
|
|
437
453
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
438
454
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
439
455
|
await this.storage.setTokens(normalised);
|
|
@@ -466,6 +482,16 @@ var TokenManager = class {
|
|
|
466
482
|
const refreshed = await this.tryRefresh(tokens);
|
|
467
483
|
return refreshed?.accessToken ?? null;
|
|
468
484
|
}
|
|
485
|
+
/**
|
|
486
|
+
* Whether the stored session currently carries a refresh token. Used by the
|
|
487
|
+
* 401-recovery path to decide whether a refresh was even possible, so it can
|
|
488
|
+
* let `doRefresh` own the auth-loss classification instead of second-guessing
|
|
489
|
+
* it. A rejecting storage backend reads as "no refresh token".
|
|
490
|
+
*/
|
|
491
|
+
async hasRefreshToken() {
|
|
492
|
+
const tokens = await this.safeGetTokens();
|
|
493
|
+
return !!tokens?.refreshToken;
|
|
494
|
+
}
|
|
469
495
|
onTokenRefreshed(listener) {
|
|
470
496
|
this.tokenRefreshedListeners.add(listener);
|
|
471
497
|
return () => this.tokenRefreshedListeners.delete(listener);
|
|
@@ -477,6 +503,9 @@ var TokenManager = class {
|
|
|
477
503
|
emitAuthLost(reason) {
|
|
478
504
|
if (this.authLostEmitted) return;
|
|
479
505
|
this.authLostEmitted = true;
|
|
506
|
+
this.dispatchAuthLost(reason);
|
|
507
|
+
}
|
|
508
|
+
dispatchAuthLost(reason) {
|
|
480
509
|
for (const listener of this.authLostListeners) {
|
|
481
510
|
try {
|
|
482
511
|
listener(reason);
|
|
@@ -484,6 +513,57 @@ var TokenManager = class {
|
|
|
484
513
|
}
|
|
485
514
|
}
|
|
486
515
|
}
|
|
516
|
+
/**
|
|
517
|
+
* Handle a server-confirmed auth loss: drop the dead session, THEN notify.
|
|
518
|
+
*
|
|
519
|
+
* The stored tokens are cleared **before** listeners run (#759) so that a
|
|
520
|
+
* reload lands on the login screen and `hasStoredSession()` reads false — and,
|
|
521
|
+
* critically, so a listener that re-logs-in via `setTokens()` (synchronously
|
|
522
|
+
* OR after an await) writes its new session strictly AFTER the clear and can
|
|
523
|
+
* never be erased by it. The clear is a storage-only wipe: we do NOT reset
|
|
524
|
+
* `authLostEmitted` (as `clearTokens()` would), so the emit stays deduped for
|
|
525
|
+
* this lost session. A rejecting storage backend is swallowed — best-effort,
|
|
526
|
+
* the consumer is routed to logout regardless. `refreshPromise` is left to
|
|
527
|
+
* `doRefresh`'s own `finally`; nulling it here could clobber a newer in-flight
|
|
528
|
+
* refresh.
|
|
529
|
+
*
|
|
530
|
+
* The token-clearing auth-loss path. Called from `doRefresh` (single-flighted
|
|
531
|
+
* via `refreshPromise`) on a server-confirmed refresh rejection, and from
|
|
532
|
+
* `HttpClient` only when a 401 could not be refreshed AND no refresh was even
|
|
533
|
+
* attempted (no refresh token) — these are mutually exclusive, so there is
|
|
534
|
+
* exactly one clear per loss and it runs BEFORE any listener. A re-login a
|
|
535
|
+
* listener performs is therefore never erased.
|
|
536
|
+
*/
|
|
537
|
+
async notifyAuthLost(reason) {
|
|
538
|
+
if (this.authLostEmitted) return;
|
|
539
|
+
this.authLostEmitted = true;
|
|
540
|
+
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
541
|
+
});
|
|
542
|
+
await this.pendingClear;
|
|
543
|
+
this.pendingClear = null;
|
|
544
|
+
this.dispatchAuthLost(reason);
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* A refresh failure that is transient (retryable), not a server-confirmed auth
|
|
548
|
+
* loss — so it must NOT eject a local-first app; the next request retries.
|
|
549
|
+
* Covers:
|
|
550
|
+
* - a transport failure (offline, tunnel, captive portal, unreachable
|
|
551
|
+
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
552
|
+
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
553
|
+
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
554
|
+
* a token rejection.
|
|
555
|
+
* A real auth rejection (401/invalid_grant) or any other non-5xx server error
|
|
556
|
+
* is NOT transient and is treated as a server-confirmed loss.
|
|
557
|
+
*/
|
|
558
|
+
isTransientTransportError(error) {
|
|
559
|
+
if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
if (error instanceof SpacelrError && typeof error.statusCode === "number") {
|
|
563
|
+
return error.statusCode >= 500 && error.statusCode <= 599;
|
|
564
|
+
}
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
487
567
|
isTokenExpired(tokens) {
|
|
488
568
|
if (!tokens.expiresAt) return false;
|
|
489
569
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
@@ -522,15 +602,18 @@ var TokenManager = class {
|
|
|
522
602
|
return current;
|
|
523
603
|
}
|
|
524
604
|
const activeRefreshToken = current?.refreshToken || refreshToken;
|
|
605
|
+
let newTokens;
|
|
525
606
|
try {
|
|
526
|
-
|
|
527
|
-
await this.storage.setTokens(newTokens);
|
|
528
|
-
this.emitTokenRefreshed(newTokens);
|
|
529
|
-
return newTokens;
|
|
607
|
+
newTokens = await callback(activeRefreshToken);
|
|
530
608
|
} catch (error) {
|
|
531
|
-
this.
|
|
609
|
+
if (!this.isTransientTransportError(error)) {
|
|
610
|
+
await this.notifyAuthLost("refresh-failed");
|
|
611
|
+
}
|
|
532
612
|
throw error;
|
|
533
613
|
}
|
|
614
|
+
await this.storage.setTokens(newTokens);
|
|
615
|
+
this.emitTokenRefreshed(newTokens);
|
|
616
|
+
return newTokens;
|
|
534
617
|
}
|
|
535
618
|
async safeGetTokens() {
|
|
536
619
|
try {
|