@spacelr/sdk 0.19.3 → 0.19.5
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 +33 -7
- package/dist/index.d.ts +33 -7
- package/dist/index.js +92 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +92 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -467,10 +467,12 @@ interface UploadProgress {
|
|
|
467
467
|
}
|
|
468
468
|
|
|
469
469
|
type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
|
|
470
|
+
/** #1073: renews a session that has no refresh token (API-token sign-in). */
|
|
471
|
+
type ReauthCallback = () => Promise<StoredTokens>;
|
|
470
472
|
/**
|
|
471
473
|
* Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
|
|
472
|
-
* A transient refresh failure (network/transport
|
|
473
|
-
* (#759/#762).
|
|
474
|
+
* A transient refresh failure (network/transport, a 5xx, a 429 or a 408) is NOT
|
|
475
|
+
* reported (#759/#762/#1080).
|
|
474
476
|
* See the `onAuthLost` JSDoc in `client.ts` for the full contract.
|
|
475
477
|
*/
|
|
476
478
|
type AuthLostReason = 'refresh-failed' | 'unauthorized';
|
|
@@ -481,6 +483,7 @@ declare class TokenManager {
|
|
|
481
483
|
private refreshBufferSeconds;
|
|
482
484
|
private refreshLockName;
|
|
483
485
|
private refreshCallback;
|
|
486
|
+
private reauthCallback;
|
|
484
487
|
private refreshPromise;
|
|
485
488
|
private tokenRefreshedListeners;
|
|
486
489
|
private authLostListeners;
|
|
@@ -488,6 +491,13 @@ declare class TokenManager {
|
|
|
488
491
|
private pendingClear;
|
|
489
492
|
constructor(storage?: TokenStorage, refreshBufferSeconds?: number, refreshLockName?: string);
|
|
490
493
|
setRefreshCallback(callback: RefreshCallback): void;
|
|
494
|
+
/**
|
|
495
|
+
* #1073: a session without a refresh token (API-token sign-in) recovers by
|
|
496
|
+
* re-authenticating through this callback instead. `null` removes it.
|
|
497
|
+
*/
|
|
498
|
+
setReauthCallback(callback: ReauthCallback | null): void;
|
|
499
|
+
/** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
|
|
500
|
+
canRenew(): Promise<boolean>;
|
|
491
501
|
getAccessToken(): Promise<string | null>;
|
|
492
502
|
setTokens(tokens: StoredTokens): Promise<void>;
|
|
493
503
|
clearTokens(): Promise<void>;
|
|
@@ -541,14 +551,21 @@ declare class TokenManager {
|
|
|
541
551
|
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
542
552
|
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
543
553
|
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
544
|
-
* a token rejection
|
|
545
|
-
*
|
|
546
|
-
*
|
|
554
|
+
* a token rejection;
|
|
555
|
+
* - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
|
|
556
|
+
* everyone behind one NAT, and a 408 never completed. Neither judged the
|
|
557
|
+
* refresh token.
|
|
558
|
+
* A real auth rejection (401/invalid_grant) or any other 4xx server error is
|
|
559
|
+
* NOT transient and is treated as a server-confirmed loss.
|
|
547
560
|
*/
|
|
548
561
|
private isTransientTransportError;
|
|
549
562
|
private isTokenExpired;
|
|
550
563
|
private shouldRefresh;
|
|
551
564
|
private tryRefresh;
|
|
565
|
+
/** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
|
|
566
|
+
private singleFlight;
|
|
567
|
+
/** #1073: same auth-loss classification as doRefresh, for re-authentication. */
|
|
568
|
+
private executeReauth;
|
|
552
569
|
private executeRefresh;
|
|
553
570
|
private doRefresh;
|
|
554
571
|
private safeGetTokens;
|
|
@@ -1173,6 +1190,14 @@ declare class AuthModule {
|
|
|
1173
1190
|
}>;
|
|
1174
1191
|
getUserInfo(): Promise<UserInfo>;
|
|
1175
1192
|
getAuthorizationUrl(params: AuthorizationUrlParams): string;
|
|
1193
|
+
/**
|
|
1194
|
+
* #1073: sign in as a machine user with a project API token (`slr_at_…`).
|
|
1195
|
+
* The server returns a short-lived access token and no refresh token; when
|
|
1196
|
+
* it expires or is rejected, the SDK exchanges the API token again. The API
|
|
1197
|
+
* token stays in memory only and is forgotten on logout. Never use this in a
|
|
1198
|
+
* browser: an API token is a long-lived secret.
|
|
1199
|
+
*/
|
|
1200
|
+
loginWithApiToken(apiToken: string): Promise<void>;
|
|
1176
1201
|
exchangeCode(params: ExchangeCodeParams): Promise<TokenResponse>;
|
|
1177
1202
|
generatePKCE(): Promise<PKCEChallenge>;
|
|
1178
1203
|
getOpenIDConfiguration(): Promise<OpenIDConfiguration>;
|
|
@@ -2450,9 +2475,10 @@ interface SpacelrClient {
|
|
|
2450
2475
|
* session and the next request retries. That covers a network/transport
|
|
2451
2476
|
* failure (offline, tunnel, captive portal, unreachable gateway; #759) AND a
|
|
2452
2477
|
* **5xx** server response, e.g. a `503` the gateway returns while
|
|
2453
|
-
* oauth-service is momentarily down (#760/#762).
|
|
2478
|
+
* oauth-service is momentarily down (#760/#762). A **429** (rate limit, shared
|
|
2479
|
+
* per IP) and a **408** are transient too (#1080). This lets a local-first app
|
|
2454
2480
|
* stay signed in through an outage instead of ejecting the user. Only a
|
|
2455
|
-
* server-confirmed rejection (a 401/invalid_grant, or another
|
|
2481
|
+
* server-confirmed rejection (a 401/invalid_grant, or another 4xx server
|
|
2456
2482
|
* error) fires.
|
|
2457
2483
|
*
|
|
2458
2484
|
* On a confirmed loss the SDK **clears the stored tokens itself**, so
|
package/dist/index.d.ts
CHANGED
|
@@ -467,10 +467,12 @@ interface UploadProgress {
|
|
|
467
467
|
}
|
|
468
468
|
|
|
469
469
|
type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
|
|
470
|
+
/** #1073: renews a session that has no refresh token (API-token sign-in). */
|
|
471
|
+
type ReauthCallback = () => Promise<StoredTokens>;
|
|
470
472
|
/**
|
|
471
473
|
* Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
|
|
472
|
-
* A transient refresh failure (network/transport
|
|
473
|
-
* (#759/#762).
|
|
474
|
+
* A transient refresh failure (network/transport, a 5xx, a 429 or a 408) is NOT
|
|
475
|
+
* reported (#759/#762/#1080).
|
|
474
476
|
* See the `onAuthLost` JSDoc in `client.ts` for the full contract.
|
|
475
477
|
*/
|
|
476
478
|
type AuthLostReason = 'refresh-failed' | 'unauthorized';
|
|
@@ -481,6 +483,7 @@ declare class TokenManager {
|
|
|
481
483
|
private refreshBufferSeconds;
|
|
482
484
|
private refreshLockName;
|
|
483
485
|
private refreshCallback;
|
|
486
|
+
private reauthCallback;
|
|
484
487
|
private refreshPromise;
|
|
485
488
|
private tokenRefreshedListeners;
|
|
486
489
|
private authLostListeners;
|
|
@@ -488,6 +491,13 @@ declare class TokenManager {
|
|
|
488
491
|
private pendingClear;
|
|
489
492
|
constructor(storage?: TokenStorage, refreshBufferSeconds?: number, refreshLockName?: string);
|
|
490
493
|
setRefreshCallback(callback: RefreshCallback): void;
|
|
494
|
+
/**
|
|
495
|
+
* #1073: a session without a refresh token (API-token sign-in) recovers by
|
|
496
|
+
* re-authenticating through this callback instead. `null` removes it.
|
|
497
|
+
*/
|
|
498
|
+
setReauthCallback(callback: ReauthCallback | null): void;
|
|
499
|
+
/** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
|
|
500
|
+
canRenew(): Promise<boolean>;
|
|
491
501
|
getAccessToken(): Promise<string | null>;
|
|
492
502
|
setTokens(tokens: StoredTokens): Promise<void>;
|
|
493
503
|
clearTokens(): Promise<void>;
|
|
@@ -541,14 +551,21 @@ declare class TokenManager {
|
|
|
541
551
|
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
542
552
|
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
543
553
|
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
544
|
-
* a token rejection
|
|
545
|
-
*
|
|
546
|
-
*
|
|
554
|
+
* a token rejection;
|
|
555
|
+
* - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
|
|
556
|
+
* everyone behind one NAT, and a 408 never completed. Neither judged the
|
|
557
|
+
* refresh token.
|
|
558
|
+
* A real auth rejection (401/invalid_grant) or any other 4xx server error is
|
|
559
|
+
* NOT transient and is treated as a server-confirmed loss.
|
|
547
560
|
*/
|
|
548
561
|
private isTransientTransportError;
|
|
549
562
|
private isTokenExpired;
|
|
550
563
|
private shouldRefresh;
|
|
551
564
|
private tryRefresh;
|
|
565
|
+
/** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
|
|
566
|
+
private singleFlight;
|
|
567
|
+
/** #1073: same auth-loss classification as doRefresh, for re-authentication. */
|
|
568
|
+
private executeReauth;
|
|
552
569
|
private executeRefresh;
|
|
553
570
|
private doRefresh;
|
|
554
571
|
private safeGetTokens;
|
|
@@ -1173,6 +1190,14 @@ declare class AuthModule {
|
|
|
1173
1190
|
}>;
|
|
1174
1191
|
getUserInfo(): Promise<UserInfo>;
|
|
1175
1192
|
getAuthorizationUrl(params: AuthorizationUrlParams): string;
|
|
1193
|
+
/**
|
|
1194
|
+
* #1073: sign in as a machine user with a project API token (`slr_at_…`).
|
|
1195
|
+
* The server returns a short-lived access token and no refresh token; when
|
|
1196
|
+
* it expires or is rejected, the SDK exchanges the API token again. The API
|
|
1197
|
+
* token stays in memory only and is forgotten on logout. Never use this in a
|
|
1198
|
+
* browser: an API token is a long-lived secret.
|
|
1199
|
+
*/
|
|
1200
|
+
loginWithApiToken(apiToken: string): Promise<void>;
|
|
1176
1201
|
exchangeCode(params: ExchangeCodeParams): Promise<TokenResponse>;
|
|
1177
1202
|
generatePKCE(): Promise<PKCEChallenge>;
|
|
1178
1203
|
getOpenIDConfiguration(): Promise<OpenIDConfiguration>;
|
|
@@ -2450,9 +2475,10 @@ interface SpacelrClient {
|
|
|
2450
2475
|
* session and the next request retries. That covers a network/transport
|
|
2451
2476
|
* failure (offline, tunnel, captive portal, unreachable gateway; #759) AND a
|
|
2452
2477
|
* **5xx** server response, e.g. a `503` the gateway returns while
|
|
2453
|
-
* oauth-service is momentarily down (#760/#762).
|
|
2478
|
+
* oauth-service is momentarily down (#760/#762). A **429** (rate limit, shared
|
|
2479
|
+
* per IP) and a **408** are transient too (#1080). This lets a local-first app
|
|
2454
2480
|
* stay signed in through an outage instead of ejecting the user. Only a
|
|
2455
|
-
* server-confirmed rejection (a 401/invalid_grant, or another
|
|
2481
|
+
* server-confirmed rejection (a 401/invalid_grant, or another 4xx server
|
|
2456
2482
|
* error) fires.
|
|
2457
2483
|
*
|
|
2458
2484
|
* On a confirmed loss the SDK **clears the stored tokens itself**, so
|
package/dist/index.js
CHANGED
|
@@ -253,7 +253,7 @@ var HttpClient = class {
|
|
|
253
253
|
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
254
254
|
return false;
|
|
255
255
|
}
|
|
256
|
-
const hadRefreshToken = await this.tokenManager.
|
|
256
|
+
const hadRefreshToken = await this.tokenManager.canRenew();
|
|
257
257
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
258
258
|
if (refreshed) return true;
|
|
259
259
|
if (hadRefreshToken) return false;
|
|
@@ -489,6 +489,7 @@ var BrowserTokenStorage = class {
|
|
|
489
489
|
var TokenManager = class {
|
|
490
490
|
constructor(storage, refreshBufferSeconds = 60, refreshLockName = "spacelr_token_refresh") {
|
|
491
491
|
this.refreshCallback = null;
|
|
492
|
+
this.reauthCallback = null;
|
|
492
493
|
this.refreshPromise = null;
|
|
493
494
|
this.tokenRefreshedListeners = /* @__PURE__ */ new Set();
|
|
494
495
|
this.authLostListeners = /* @__PURE__ */ new Set();
|
|
@@ -508,6 +509,18 @@ var TokenManager = class {
|
|
|
508
509
|
setRefreshCallback(callback) {
|
|
509
510
|
this.refreshCallback = callback;
|
|
510
511
|
}
|
|
512
|
+
/**
|
|
513
|
+
* #1073: a session without a refresh token (API-token sign-in) recovers by
|
|
514
|
+
* re-authenticating through this callback instead. `null` removes it.
|
|
515
|
+
*/
|
|
516
|
+
setReauthCallback(callback) {
|
|
517
|
+
this.reauthCallback = callback;
|
|
518
|
+
}
|
|
519
|
+
/** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
|
|
520
|
+
async canRenew() {
|
|
521
|
+
if (this.reauthCallback) return true;
|
|
522
|
+
return this.hasRefreshToken();
|
|
523
|
+
}
|
|
511
524
|
async getAccessToken() {
|
|
512
525
|
const tokens = await this.storage.getTokens();
|
|
513
526
|
if (!tokens) return null;
|
|
@@ -525,10 +538,12 @@ var TokenManager = class {
|
|
|
525
538
|
if (this.pendingClear) await this.pendingClear;
|
|
526
539
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
527
540
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
541
|
+
this.reauthCallback = null;
|
|
528
542
|
await this.storage.setTokens(normalised);
|
|
529
543
|
this.authLostEmitted = false;
|
|
530
544
|
}
|
|
531
545
|
async clearTokens() {
|
|
546
|
+
this.reauthCallback = null;
|
|
532
547
|
await this.storage.clearTokens();
|
|
533
548
|
this.refreshPromise = null;
|
|
534
549
|
this.authLostEmitted = false;
|
|
@@ -610,6 +625,7 @@ var TokenManager = class {
|
|
|
610
625
|
async notifyAuthLost(reason) {
|
|
611
626
|
if (this.authLostEmitted) return;
|
|
612
627
|
this.authLostEmitted = true;
|
|
628
|
+
this.reauthCallback = null;
|
|
613
629
|
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
614
630
|
});
|
|
615
631
|
await this.pendingClear;
|
|
@@ -624,16 +640,20 @@ var TokenManager = class {
|
|
|
624
640
|
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
625
641
|
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
626
642
|
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
627
|
-
* a token rejection
|
|
628
|
-
*
|
|
629
|
-
*
|
|
643
|
+
* a token rejection;
|
|
644
|
+
* - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
|
|
645
|
+
* everyone behind one NAT, and a 408 never completed. Neither judged the
|
|
646
|
+
* refresh token.
|
|
647
|
+
* A real auth rejection (401/invalid_grant) or any other 4xx server error is
|
|
648
|
+
* NOT transient and is treated as a server-confirmed loss.
|
|
630
649
|
*/
|
|
631
650
|
isTransientTransportError(error) {
|
|
632
651
|
if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
|
|
633
652
|
return true;
|
|
634
653
|
}
|
|
635
654
|
if (error instanceof SpacelrError && typeof error.statusCode === "number") {
|
|
636
|
-
|
|
655
|
+
const status = error.statusCode;
|
|
656
|
+
return status === 429 || status === 408 || status >= 500 && status <= 599;
|
|
637
657
|
}
|
|
638
658
|
return false;
|
|
639
659
|
}
|
|
@@ -642,12 +662,15 @@ var TokenManager = class {
|
|
|
642
662
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
643
663
|
}
|
|
644
664
|
shouldRefresh(tokens) {
|
|
645
|
-
if (!tokens.expiresAt || !tokens.refreshToken) return false;
|
|
665
|
+
if (!tokens.expiresAt || !tokens.refreshToken && !this.reauthCallback) return false;
|
|
646
666
|
const bufferMs = this.refreshBufferSeconds * 1e3;
|
|
647
667
|
return Date.now() >= tokens.expiresAt * 1e3 - bufferMs;
|
|
648
668
|
}
|
|
649
669
|
async tryRefresh(tokens) {
|
|
650
|
-
if (!tokens.refreshToken
|
|
670
|
+
if (!tokens.refreshToken) {
|
|
671
|
+
return this.reauthCallback ? this.singleFlight(() => this.executeReauth()) : null;
|
|
672
|
+
}
|
|
673
|
+
if (!this.refreshCallback) return null;
|
|
651
674
|
if (this.refreshPromise) {
|
|
652
675
|
try {
|
|
653
676
|
return await this.refreshPromise;
|
|
@@ -664,6 +687,43 @@ var TokenManager = class {
|
|
|
664
687
|
this.refreshPromise = null;
|
|
665
688
|
}
|
|
666
689
|
}
|
|
690
|
+
/** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
|
|
691
|
+
async singleFlight(run) {
|
|
692
|
+
if (!this.refreshPromise) {
|
|
693
|
+
this.refreshPromise = run();
|
|
694
|
+
try {
|
|
695
|
+
return await this.refreshPromise;
|
|
696
|
+
} catch {
|
|
697
|
+
return null;
|
|
698
|
+
} finally {
|
|
699
|
+
this.refreshPromise = null;
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
try {
|
|
703
|
+
return await this.refreshPromise;
|
|
704
|
+
} catch {
|
|
705
|
+
return null;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
/** #1073: same auth-loss classification as doRefresh, for re-authentication. */
|
|
709
|
+
async executeReauth() {
|
|
710
|
+
const callback = this.reauthCallback;
|
|
711
|
+
const superseded = () => this.reauthCallback !== callback;
|
|
712
|
+
let newTokens;
|
|
713
|
+
try {
|
|
714
|
+
newTokens = await callback();
|
|
715
|
+
} catch (error) {
|
|
716
|
+
if (superseded()) throw new Error("re-authentication superseded");
|
|
717
|
+
if (!this.isTransientTransportError(error)) {
|
|
718
|
+
await this.notifyAuthLost("refresh-failed");
|
|
719
|
+
}
|
|
720
|
+
throw error;
|
|
721
|
+
}
|
|
722
|
+
if (superseded()) throw new Error("re-authentication superseded");
|
|
723
|
+
await this.storage.setTokens(newTokens);
|
|
724
|
+
this.emitTokenRefreshed(newTokens);
|
|
725
|
+
return newTokens;
|
|
726
|
+
}
|
|
667
727
|
async executeRefresh(refreshToken) {
|
|
668
728
|
return this.withCrossTabLock(() => this.doRefresh(refreshToken));
|
|
669
729
|
}
|
|
@@ -721,6 +781,7 @@ var FileVisibility = /* @__PURE__ */ ((FileVisibility2) => {
|
|
|
721
781
|
FileVisibility2["PUBLIC"] = "public";
|
|
722
782
|
return FileVisibility2;
|
|
723
783
|
})(FileVisibility || {});
|
|
784
|
+
var API_TOKEN_GRANT = "urn:spacelr:grant-type:api-token";
|
|
724
785
|
var GrantType = /* @__PURE__ */ ((GrantType2) => {
|
|
725
786
|
GrantType2["AUTHORIZATION_CODE"] = "authorization_code";
|
|
726
787
|
GrantType2["CLIENT_CREDENTIALS"] = "client_credentials";
|
|
@@ -2197,6 +2258,30 @@ var AuthModule = class _AuthModule {
|
|
|
2197
2258
|
}
|
|
2198
2259
|
return url.toString();
|
|
2199
2260
|
}
|
|
2261
|
+
/**
|
|
2262
|
+
* #1073: sign in as a machine user with a project API token (`slr_at_…`).
|
|
2263
|
+
* The server returns a short-lived access token and no refresh token; when
|
|
2264
|
+
* it expires or is rejected, the SDK exchanges the API token again. The API
|
|
2265
|
+
* token stays in memory only and is forgotten on logout. Never use this in a
|
|
2266
|
+
* browser: an API token is a long-lived secret.
|
|
2267
|
+
*/
|
|
2268
|
+
async loginWithApiToken(apiToken) {
|
|
2269
|
+
const exchange = async () => {
|
|
2270
|
+
const response = await this.http.request({
|
|
2271
|
+
method: "POST",
|
|
2272
|
+
path: this.config.tokenEndpoint ?? "/auth/token",
|
|
2273
|
+
body: { grant_type: API_TOKEN_GRANT, client_id: this.config.clientId, api_token: apiToken },
|
|
2274
|
+
skipAuthRefresh: true
|
|
2275
|
+
});
|
|
2276
|
+
return {
|
|
2277
|
+
accessToken: response.access_token,
|
|
2278
|
+
expiresAt: response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0
|
|
2279
|
+
};
|
|
2280
|
+
};
|
|
2281
|
+
const tokens = await exchange();
|
|
2282
|
+
await this.installSession(tokens);
|
|
2283
|
+
this.tokenManager.setReauthCallback(exchange);
|
|
2284
|
+
}
|
|
2200
2285
|
async exchangeCode(params) {
|
|
2201
2286
|
const body = {
|
|
2202
2287
|
grant_type: params.grantType ?? "authorization_code" /* AUTHORIZATION_CODE */,
|