@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.mjs
CHANGED
|
@@ -206,7 +206,7 @@ var HttpClient = class {
|
|
|
206
206
|
await this.tokenManager.notifyAuthLost("unauthorized");
|
|
207
207
|
return false;
|
|
208
208
|
}
|
|
209
|
-
const hadRefreshToken = await this.tokenManager.
|
|
209
|
+
const hadRefreshToken = await this.tokenManager.canRenew();
|
|
210
210
|
const refreshed = await this.tokenManager.forceRefresh();
|
|
211
211
|
if (refreshed) return true;
|
|
212
212
|
if (hadRefreshToken) return false;
|
|
@@ -442,6 +442,7 @@ var BrowserTokenStorage = class {
|
|
|
442
442
|
var TokenManager = class {
|
|
443
443
|
constructor(storage, refreshBufferSeconds = 60, refreshLockName = "spacelr_token_refresh") {
|
|
444
444
|
this.refreshCallback = null;
|
|
445
|
+
this.reauthCallback = null;
|
|
445
446
|
this.refreshPromise = null;
|
|
446
447
|
this.tokenRefreshedListeners = /* @__PURE__ */ new Set();
|
|
447
448
|
this.authLostListeners = /* @__PURE__ */ new Set();
|
|
@@ -461,6 +462,18 @@ var TokenManager = class {
|
|
|
461
462
|
setRefreshCallback(callback) {
|
|
462
463
|
this.refreshCallback = callback;
|
|
463
464
|
}
|
|
465
|
+
/**
|
|
466
|
+
* #1073: a session without a refresh token (API-token sign-in) recovers by
|
|
467
|
+
* re-authenticating through this callback instead. `null` removes it.
|
|
468
|
+
*/
|
|
469
|
+
setReauthCallback(callback) {
|
|
470
|
+
this.reauthCallback = callback;
|
|
471
|
+
}
|
|
472
|
+
/** True when an expired or rejected access token can be renewed (refresh token or re-auth). */
|
|
473
|
+
async canRenew() {
|
|
474
|
+
if (this.reauthCallback) return true;
|
|
475
|
+
return this.hasRefreshToken();
|
|
476
|
+
}
|
|
464
477
|
async getAccessToken() {
|
|
465
478
|
const tokens = await this.storage.getTokens();
|
|
466
479
|
if (!tokens) return null;
|
|
@@ -478,10 +491,12 @@ var TokenManager = class {
|
|
|
478
491
|
if (this.pendingClear) await this.pendingClear;
|
|
479
492
|
const needsDefault = tokens.expiresAt === void 0 && !!tokens.refreshToken;
|
|
480
493
|
const normalised = needsDefault ? { ...tokens, expiresAt: Math.floor(Date.now() / 1e3) } : tokens;
|
|
494
|
+
this.reauthCallback = null;
|
|
481
495
|
await this.storage.setTokens(normalised);
|
|
482
496
|
this.authLostEmitted = false;
|
|
483
497
|
}
|
|
484
498
|
async clearTokens() {
|
|
499
|
+
this.reauthCallback = null;
|
|
485
500
|
await this.storage.clearTokens();
|
|
486
501
|
this.refreshPromise = null;
|
|
487
502
|
this.authLostEmitted = false;
|
|
@@ -563,6 +578,7 @@ var TokenManager = class {
|
|
|
563
578
|
async notifyAuthLost(reason) {
|
|
564
579
|
if (this.authLostEmitted) return;
|
|
565
580
|
this.authLostEmitted = true;
|
|
581
|
+
this.reauthCallback = null;
|
|
566
582
|
this.pendingClear = this.storage.clearTokens().catch(() => {
|
|
567
583
|
});
|
|
568
584
|
await this.pendingClear;
|
|
@@ -577,16 +593,20 @@ var TokenManager = class {
|
|
|
577
593
|
* gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
|
|
578
594
|
* - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
|
|
579
595
|
* is momentarily down (#760/#762) — which is retryable infrastructure, not
|
|
580
|
-
* a token rejection
|
|
581
|
-
*
|
|
582
|
-
*
|
|
596
|
+
* a token rejection;
|
|
597
|
+
* - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
|
|
598
|
+
* everyone behind one NAT, and a 408 never completed. Neither judged the
|
|
599
|
+
* refresh token.
|
|
600
|
+
* A real auth rejection (401/invalid_grant) or any other 4xx server error is
|
|
601
|
+
* NOT transient and is treated as a server-confirmed loss.
|
|
583
602
|
*/
|
|
584
603
|
isTransientTransportError(error) {
|
|
585
604
|
if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
|
|
586
605
|
return true;
|
|
587
606
|
}
|
|
588
607
|
if (error instanceof SpacelrError && typeof error.statusCode === "number") {
|
|
589
|
-
|
|
608
|
+
const status = error.statusCode;
|
|
609
|
+
return status === 429 || status === 408 || status >= 500 && status <= 599;
|
|
590
610
|
}
|
|
591
611
|
return false;
|
|
592
612
|
}
|
|
@@ -595,12 +615,15 @@ var TokenManager = class {
|
|
|
595
615
|
return Date.now() >= tokens.expiresAt * 1e3;
|
|
596
616
|
}
|
|
597
617
|
shouldRefresh(tokens) {
|
|
598
|
-
if (!tokens.expiresAt || !tokens.refreshToken) return false;
|
|
618
|
+
if (!tokens.expiresAt || !tokens.refreshToken && !this.reauthCallback) return false;
|
|
599
619
|
const bufferMs = this.refreshBufferSeconds * 1e3;
|
|
600
620
|
return Date.now() >= tokens.expiresAt * 1e3 - bufferMs;
|
|
601
621
|
}
|
|
602
622
|
async tryRefresh(tokens) {
|
|
603
|
-
if (!tokens.refreshToken
|
|
623
|
+
if (!tokens.refreshToken) {
|
|
624
|
+
return this.reauthCallback ? this.singleFlight(() => this.executeReauth()) : null;
|
|
625
|
+
}
|
|
626
|
+
if (!this.refreshCallback) return null;
|
|
604
627
|
if (this.refreshPromise) {
|
|
605
628
|
try {
|
|
606
629
|
return await this.refreshPromise;
|
|
@@ -617,6 +640,43 @@ var TokenManager = class {
|
|
|
617
640
|
this.refreshPromise = null;
|
|
618
641
|
}
|
|
619
642
|
}
|
|
643
|
+
/** Deduplicate concurrent renewals; followers get null on failure, like the leader. */
|
|
644
|
+
async singleFlight(run) {
|
|
645
|
+
if (!this.refreshPromise) {
|
|
646
|
+
this.refreshPromise = run();
|
|
647
|
+
try {
|
|
648
|
+
return await this.refreshPromise;
|
|
649
|
+
} catch {
|
|
650
|
+
return null;
|
|
651
|
+
} finally {
|
|
652
|
+
this.refreshPromise = null;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
try {
|
|
656
|
+
return await this.refreshPromise;
|
|
657
|
+
} catch {
|
|
658
|
+
return null;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
/** #1073: same auth-loss classification as doRefresh, for re-authentication. */
|
|
662
|
+
async executeReauth() {
|
|
663
|
+
const callback = this.reauthCallback;
|
|
664
|
+
const superseded = () => this.reauthCallback !== callback;
|
|
665
|
+
let newTokens;
|
|
666
|
+
try {
|
|
667
|
+
newTokens = await callback();
|
|
668
|
+
} catch (error) {
|
|
669
|
+
if (superseded()) throw new Error("re-authentication superseded");
|
|
670
|
+
if (!this.isTransientTransportError(error)) {
|
|
671
|
+
await this.notifyAuthLost("refresh-failed");
|
|
672
|
+
}
|
|
673
|
+
throw error;
|
|
674
|
+
}
|
|
675
|
+
if (superseded()) throw new Error("re-authentication superseded");
|
|
676
|
+
await this.storage.setTokens(newTokens);
|
|
677
|
+
this.emitTokenRefreshed(newTokens);
|
|
678
|
+
return newTokens;
|
|
679
|
+
}
|
|
620
680
|
async executeRefresh(refreshToken) {
|
|
621
681
|
return this.withCrossTabLock(() => this.doRefresh(refreshToken));
|
|
622
682
|
}
|
|
@@ -674,6 +734,7 @@ var FileVisibility = /* @__PURE__ */ ((FileVisibility2) => {
|
|
|
674
734
|
FileVisibility2["PUBLIC"] = "public";
|
|
675
735
|
return FileVisibility2;
|
|
676
736
|
})(FileVisibility || {});
|
|
737
|
+
var API_TOKEN_GRANT = "urn:spacelr:grant-type:api-token";
|
|
677
738
|
var GrantType = /* @__PURE__ */ ((GrantType2) => {
|
|
678
739
|
GrantType2["AUTHORIZATION_CODE"] = "authorization_code";
|
|
679
740
|
GrantType2["CLIENT_CREDENTIALS"] = "client_credentials";
|
|
@@ -2150,6 +2211,30 @@ var AuthModule = class _AuthModule {
|
|
|
2150
2211
|
}
|
|
2151
2212
|
return url.toString();
|
|
2152
2213
|
}
|
|
2214
|
+
/**
|
|
2215
|
+
* #1073: sign in as a machine user with a project API token (`slr_at_…`).
|
|
2216
|
+
* The server returns a short-lived access token and no refresh token; when
|
|
2217
|
+
* it expires or is rejected, the SDK exchanges the API token again. The API
|
|
2218
|
+
* token stays in memory only and is forgotten on logout. Never use this in a
|
|
2219
|
+
* browser: an API token is a long-lived secret.
|
|
2220
|
+
*/
|
|
2221
|
+
async loginWithApiToken(apiToken) {
|
|
2222
|
+
const exchange = async () => {
|
|
2223
|
+
const response = await this.http.request({
|
|
2224
|
+
method: "POST",
|
|
2225
|
+
path: this.config.tokenEndpoint ?? "/auth/token",
|
|
2226
|
+
body: { grant_type: API_TOKEN_GRANT, client_id: this.config.clientId, api_token: apiToken },
|
|
2227
|
+
skipAuthRefresh: true
|
|
2228
|
+
});
|
|
2229
|
+
return {
|
|
2230
|
+
accessToken: response.access_token,
|
|
2231
|
+
expiresAt: response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0
|
|
2232
|
+
};
|
|
2233
|
+
};
|
|
2234
|
+
const tokens = await exchange();
|
|
2235
|
+
await this.installSession(tokens);
|
|
2236
|
+
this.tokenManager.setReauthCallback(exchange);
|
|
2237
|
+
}
|
|
2153
2238
|
async exchangeCode(params) {
|
|
2154
2239
|
const body = {
|
|
2155
2240
|
grant_type: params.grantType ?? "authorization_code" /* AUTHORIZATION_CODE */,
|