@tinycloud/sdk-services 3.1.0-beta.1 → 3.1.0-beta.2

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/kv/index.cjs CHANGED
@@ -183,6 +183,20 @@ function storageLimitReachedError(service, message, meta) {
183
183
  meta
184
184
  };
185
185
  }
186
+ var STORAGE_FULL_MESSAGE = "TinyCloud storage is full, so this change was not saved. Reading still works. Free up space or upgrade your plan to save again.";
187
+ var STORAGE_WRITE_TOO_LARGE_MESSAGE = "This change is larger than the TinyCloud storage you have left, so it was not saved. Reading still works. Free up space or upgrade your plan to save it.";
188
+ function parseStorageQuotaBytes(responseText) {
189
+ const match = responseText.match(/Used:\s*(\d+)\s*bytes,\s*Limit:\s*(\d+)\s*bytes/i);
190
+ if (!match) return void 0;
191
+ return {
192
+ usedBytes: parseInt(match[1], 10),
193
+ limitBytes: parseInt(match[2], 10)
194
+ };
195
+ }
196
+ function storageRejectionError(service, status, meta, responseText) {
197
+ const quotaMeta = { ...meta, ...parseStorageQuotaBytes(responseText) };
198
+ return status === 402 ? storageQuotaExceededError(service, STORAGE_FULL_MESSAGE, quotaMeta) : storageLimitReachedError(service, STORAGE_WRITE_TOO_LARGE_MESSAGE, quotaMeta);
199
+ }
186
200
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
187
201
  if (error instanceof Error) {
188
202
  if (error.name === "AbortError") {
@@ -573,47 +587,21 @@ var KVService = class extends BaseService {
573
587
  get config() {
574
588
  return this._config;
575
589
  }
576
- // Parses "Used: X bytes, Limit: Y bytes" from tinycloud-node error responses
577
- parseQuotaInfo(errorText) {
578
- const match = errorText.match(
579
- /Used:\s*(\d+)\s*bytes,\s*Limit:\s*(\d+)\s*bytes/i
580
- );
581
- if (match) {
582
- return {
583
- usedBytes: parseInt(match[1], 10),
584
- limitBytes: parseInt(match[2], 10)
585
- };
586
- }
587
- return void 0;
588
- }
589
590
  handleQuotaErrorResponse(response, errorText, key) {
590
- if (response.status === 402) {
591
- const quotaInfo = this.parseQuotaInfo(errorText);
592
- return err(
593
- storageQuotaExceededError(
594
- "kv",
595
- `Storage quota exceeded for key ${JSON.stringify(key)}: ${errorText}`,
596
- {
597
- status: response.status,
598
- ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
599
- }
600
- )
601
- );
591
+ if (response.status !== 402 && response.status !== 413) {
592
+ return void 0;
602
593
  }
603
- if (response.status === 413) {
604
- const quotaInfo = this.parseQuotaInfo(errorText);
605
- return err(
606
- storageLimitReachedError(
607
- "kv",
608
- `Storage limit reached for key ${JSON.stringify(key)}: ${errorText}`,
609
- {
610
- status: response.status,
611
- ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
612
- }
613
- )
614
- );
594
+ if (response.status === 413 && !parseStorageQuotaBytes(errorText)) {
595
+ return void 0;
615
596
  }
616
- return void 0;
597
+ return err(
598
+ storageRejectionError(
599
+ "kv",
600
+ response.status,
601
+ { status: response.status, statusText: response.statusText, key },
602
+ errorText
603
+ )
604
+ );
617
605
  }
618
606
  /**
619
607
  * Classify a KV 404 by reading the response body once.