@spacelr/sdk 0.19.2 → 0.19.4

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 CHANGED
@@ -469,8 +469,8 @@ interface UploadProgress {
469
469
  type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
470
470
  /**
471
471
  * Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
472
- * A transient refresh failure (network/transport OR a 5xx) is NOT reported
473
- * (#759/#762).
472
+ * A transient refresh failure (network/transport, a 5xx, a 429 or a 408) is NOT
473
+ * reported (#759/#762/#1080).
474
474
  * See the `onAuthLost` JSDoc in `client.ts` for the full contract.
475
475
  */
476
476
  type AuthLostReason = 'refresh-failed' | 'unauthorized';
@@ -541,9 +541,12 @@ declare class TokenManager {
541
541
  * gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
542
542
  * - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
543
543
  * is momentarily down (#760/#762) — which is retryable infrastructure, not
544
- * a token rejection.
545
- * A real auth rejection (401/invalid_grant) or any other non-5xx server error
546
- * is NOT transient and is treated as a server-confirmed loss.
544
+ * a token rejection;
545
+ * - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
546
+ * everyone behind one NAT, and a 408 never completed. Neither judged the
547
+ * refresh token.
548
+ * A real auth rejection (401/invalid_grant) or any other 4xx server error is
549
+ * NOT transient and is treated as a server-confirmed loss.
547
550
  */
548
551
  private isTransientTransportError;
549
552
  private isTokenExpired;
@@ -2450,9 +2453,10 @@ interface SpacelrClient {
2450
2453
  * session and the next request retries. That covers a network/transport
2451
2454
  * failure (offline, tunnel, captive portal, unreachable gateway; #759) AND a
2452
2455
  * **5xx** server response, e.g. a `503` the gateway returns while
2453
- * oauth-service is momentarily down (#760/#762). This lets a local-first app
2456
+ * oauth-service is momentarily down (#760/#762). A **429** (rate limit, shared
2457
+ * per IP) and a **408** are transient too (#1080). This lets a local-first app
2454
2458
  * stay signed in through an outage instead of ejecting the user. Only a
2455
- * server-confirmed rejection (a 401/invalid_grant, or another non-5xx server
2459
+ * server-confirmed rejection (a 401/invalid_grant, or another 4xx server
2456
2460
  * error) fires.
2457
2461
  *
2458
2462
  * On a confirmed loss the SDK **clears the stored tokens itself**, so
package/dist/index.d.ts CHANGED
@@ -469,8 +469,8 @@ interface UploadProgress {
469
469
  type RefreshCallback = (refreshToken: string) => Promise<StoredTokens>;
470
470
  /**
471
471
  * Reasons the SDK reports a **server-confirmed** auth loss via `onAuthLost`.
472
- * A transient refresh failure (network/transport OR a 5xx) is NOT reported
473
- * (#759/#762).
472
+ * A transient refresh failure (network/transport, a 5xx, a 429 or a 408) is NOT
473
+ * reported (#759/#762/#1080).
474
474
  * See the `onAuthLost` JSDoc in `client.ts` for the full contract.
475
475
  */
476
476
  type AuthLostReason = 'refresh-failed' | 'unauthorized';
@@ -541,9 +541,12 @@ declare class TokenManager {
541
541
  * gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
542
542
  * - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
543
543
  * is momentarily down (#760/#762) — which is retryable infrastructure, not
544
- * a token rejection.
545
- * A real auth rejection (401/invalid_grant) or any other non-5xx server error
546
- * is NOT transient and is treated as a server-confirmed loss.
544
+ * a token rejection;
545
+ * - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
546
+ * everyone behind one NAT, and a 408 never completed. Neither judged the
547
+ * refresh token.
548
+ * A real auth rejection (401/invalid_grant) or any other 4xx server error is
549
+ * NOT transient and is treated as a server-confirmed loss.
547
550
  */
548
551
  private isTransientTransportError;
549
552
  private isTokenExpired;
@@ -2450,9 +2453,10 @@ interface SpacelrClient {
2450
2453
  * session and the next request retries. That covers a network/transport
2451
2454
  * failure (offline, tunnel, captive portal, unreachable gateway; #759) AND a
2452
2455
  * **5xx** server response, e.g. a `503` the gateway returns while
2453
- * oauth-service is momentarily down (#760/#762). This lets a local-first app
2456
+ * oauth-service is momentarily down (#760/#762). A **429** (rate limit, shared
2457
+ * per IP) and a **408** are transient too (#1080). This lets a local-first app
2454
2458
  * stay signed in through an outage instead of ejecting the user. Only a
2455
- * server-confirmed rejection (a 401/invalid_grant, or another non-5xx server
2459
+ * server-confirmed rejection (a 401/invalid_grant, or another 4xx server
2456
2460
  * error) fires.
2457
2461
  *
2458
2462
  * On a confirmed loss the SDK **clears the stored tokens itself**, so
package/dist/index.js CHANGED
@@ -624,16 +624,20 @@ var TokenManager = class {
624
624
  * gateway) surfaced as `SpacelrNetworkError`/`SpacelrTimeoutError` (#759);
625
625
  * - a 5xx server response — e.g. a 503 the gateway returns when oauth-service
626
626
  * is momentarily down (#760/#762) — which is retryable infrastructure, not
627
- * a token rejection.
628
- * A real auth rejection (401/invalid_grant) or any other non-5xx server error
629
- * is NOT transient and is treated as a server-confirmed loss.
627
+ * a token rejection;
628
+ * - a 429 or 408 (#1080) — the rate limit is per IP, so it is shared by
629
+ * everyone behind one NAT, and a 408 never completed. Neither judged the
630
+ * refresh token.
631
+ * A real auth rejection (401/invalid_grant) or any other 4xx server error is
632
+ * NOT transient and is treated as a server-confirmed loss.
630
633
  */
631
634
  isTransientTransportError(error) {
632
635
  if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
633
636
  return true;
634
637
  }
635
638
  if (error instanceof SpacelrError && typeof error.statusCode === "number") {
636
- return error.statusCode >= 500 && error.statusCode <= 599;
639
+ const status = error.statusCode;
640
+ return status === 429 || status === 408 || status >= 500 && status <= 599;
637
641
  }
638
642
  return false;
639
643
  }
@@ -2711,6 +2715,19 @@ var AuthModule = class _AuthModule {
2711
2715
  };
2712
2716
 
2713
2717
  // libs/sdk/src/modules/storage.module.ts
2718
+ var THROTTLE_RETRY_MS = 6e4;
2719
+ var THROTTLE_MAX_RETRIES = 2;
2720
+ async function retryThrottled(call) {
2721
+ for (let attempt = 0; ; attempt++) {
2722
+ try {
2723
+ return await call();
2724
+ } catch (error) {
2725
+ const throttled = error instanceof SpacelrError && error.statusCode === 429;
2726
+ if (!throttled || attempt >= THROTTLE_MAX_RETRIES) throw error;
2727
+ await new Promise((resolve) => setTimeout(resolve, THROTTLE_RETRY_MS));
2728
+ }
2729
+ }
2730
+ }
2714
2731
  var StorageModule = class {
2715
2732
  constructor(http, tokenManager, config) {
2716
2733
  this.http = http;
@@ -2780,10 +2797,12 @@ var StorageModule = class {
2780
2797
  percentage: fileSize > 0 ? Math.min(100, Math.round(totalLoaded / fileSize * 100)) : 0
2781
2798
  });
2782
2799
  } : void 0;
2783
- const result = await this.http.uploadForm(
2784
- `/storage/files/${fileId}/multipart/upload-part`,
2785
- formData,
2786
- partProgress
2800
+ const result = await retryThrottled(
2801
+ () => this.http.uploadForm(
2802
+ `/storage/files/${fileId}/multipart/upload-part`,
2803
+ formData,
2804
+ partProgress
2805
+ )
2787
2806
  );
2788
2807
  partProgressMap.delete(partNumber);
2789
2808
  completedBytes += chunkSize;
@@ -2803,11 +2822,10 @@ var StorageModule = class {
2803
2822
  partProgressMap.clear();
2804
2823
  }
2805
2824
  completedParts.sort((a, b) => a.partNumber - b.partNumber);
2806
- await this.completeMultipartUpload(fileId, completedParts);
2807
- return this.getFileInfo(fileId);
2825
+ return await retryThrottled(() => this.completeMultipartUpload(fileId, completedParts));
2808
2826
  } catch (error) {
2809
2827
  try {
2810
- await this.abortMultipartUpload(fileId);
2828
+ await retryThrottled(() => this.abortMultipartUpload(fileId));
2811
2829
  } catch {
2812
2830
  }
2813
2831
  throw error;