@tinycloud/sdk-services 3.2.0-beta.3 → 3.2.0-beta.7

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.
Files changed (45) hide show
  1. package/dist/{BaseService-BDQXjWMx.d.cts → BaseService-4wwJq1Gn.d.cts} +1 -1
  2. package/dist/{BaseService-Di_hVfUV.d.ts → BaseService-BGr2slUz.d.ts} +1 -1
  3. package/dist/encryption/index.cjs +5 -1
  4. package/dist/encryption/index.cjs.map +1 -1
  5. package/dist/encryption/index.d.cts +2 -2
  6. package/dist/encryption/index.d.ts +2 -2
  7. package/dist/encryption/index.js +5 -1
  8. package/dist/encryption/index.js.map +1 -1
  9. package/dist/index.cjs +190 -11
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +157 -6
  12. package/dist/index.d.ts +157 -6
  13. package/dist/index.js +184 -11
  14. package/dist/index.js.map +1 -1
  15. package/dist/internal/decrypt-transport-response-error.d.cts +1 -1
  16. package/dist/internal/decrypt-transport-response-error.d.ts +1 -1
  17. package/dist/kv/index.cjs +56 -11
  18. package/dist/kv/index.cjs.map +1 -1
  19. package/dist/kv/index.d.cts +5 -4
  20. package/dist/kv/index.d.ts +5 -4
  21. package/dist/kv/index.js +56 -11
  22. package/dist/kv/index.js.map +1 -1
  23. package/dist/kv/replication/index.cjs +44 -19
  24. package/dist/kv/replication/index.cjs.map +1 -1
  25. package/dist/kv/replication/index.d.cts +4 -3
  26. package/dist/kv/replication/index.d.ts +4 -3
  27. package/dist/kv/replication/index.js +44 -19
  28. package/dist/kv/replication/index.js.map +1 -1
  29. package/dist/kv/replication/prefix.cjs +43 -0
  30. package/dist/kv/replication/prefix.cjs.map +1 -0
  31. package/dist/kv/replication/prefix.d.cts +6 -0
  32. package/dist/kv/replication/prefix.d.ts +6 -0
  33. package/dist/kv/replication/prefix.js +17 -0
  34. package/dist/kv/replication/prefix.js.map +1 -0
  35. package/dist/{scope-BmLxwyl-.d.ts → scope-CoKKmMiI.d.ts} +5 -5
  36. package/dist/{scope-D855kRDx.d.cts → scope-D7wmjbMH.d.cts} +5 -5
  37. package/dist/sql/index.cjs +44 -2
  38. package/dist/sql/index.cjs.map +1 -1
  39. package/dist/sql/index.d.cts +2 -2
  40. package/dist/sql/index.d.ts +2 -2
  41. package/dist/sql/index.js +44 -2
  42. package/dist/sql/index.js.map +1 -1
  43. package/dist/{types-7A5rHWRY.d.cts → types-D-9A3Sut.d.cts} +6 -0
  44. package/dist/{types-7A5rHWRY.d.ts → types-D-9A3Sut.d.ts} +6 -0
  45. package/package.json +14 -1
@@ -1,4 +1,4 @@
1
- import { P as PermissionHint } from '../types-7A5rHWRY.cjs';
1
+ import { P as PermissionHint } from '../types-D-9A3Sut.cjs';
2
2
 
3
3
  /** A reachable node returned an HTTP failure for a decrypt request. */
4
4
  declare class DecryptTransportResponseError extends Error {
@@ -1,4 +1,4 @@
1
- import { P as PermissionHint } from '../types-7A5rHWRY.js';
1
+ import { P as PermissionHint } from '../types-D-9A3Sut.js';
2
2
 
3
3
  /** A reachable node returned an HTTP failure for a decrypt request. */
4
4
  declare class DecryptTransportResponseError extends Error {
package/dist/kv/index.cjs CHANGED
@@ -70,6 +70,8 @@ var ErrorCodes = {
70
70
  // Storage quota errors
71
71
  STORAGE_QUOTA_EXCEEDED: "STORAGE_QUOTA_EXCEEDED",
72
72
  STORAGE_LIMIT_REACHED: "STORAGE_LIMIT_REACHED",
73
+ /** The node answered the usage read without usage numbers (it predates the read). */
74
+ STORAGE_STATUS_UNAVAILABLE: "STORAGE_STATUS_UNAVAILABLE",
73
75
  // DuckDB-specific errors
74
76
  DUCKDB_ERROR: "DUCKDB_ERROR",
75
77
  DUCKDB_PERMISSION_DENIED: "DUCKDB_PERMISSION_DENIED",
@@ -99,7 +101,9 @@ var TelemetryEvents = {
99
101
  REPLICATION_WRITE: "replication.write",
100
102
  REPLICATION_SYNC: "replication.sync",
101
103
  REPLICATION_STATE: "replication.state",
102
- REPLICATION_DIVERGENCE: "replication.divergence"
104
+ REPLICATION_DIVERGENCE: "replication.divergence",
105
+ /** Emitted once, on the first write rejected because storage is full. */
106
+ STORAGE_FULL: "storage.full"
103
107
  };
104
108
  function ok(data) {
105
109
  return { ok: true, data };
@@ -375,8 +379,43 @@ function parseStorageQuotaBytes(responseText) {
375
379
  limitBytes: parseInt(match[2], 10)
376
380
  };
377
381
  }
382
+ var STORAGE_REJECTION_BODY_ERRORS = {
383
+ storage_quota_exceeded: true,
384
+ storage_limit_reached: true
385
+ };
386
+ function isByteCount(value) {
387
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
388
+ }
389
+ function byteCountsOf(value) {
390
+ if (typeof value !== "object" || value === null) return void 0;
391
+ const { usedBytes, limitBytes } = value;
392
+ return isByteCount(usedBytes) && isByteCount(limitBytes) ? { usedBytes, limitBytes } : void 0;
393
+ }
394
+ function parseStorageRejection(responseText) {
395
+ let body;
396
+ try {
397
+ body = JSON.parse(responseText);
398
+ } catch {
399
+ return parseStorageQuotaBytes(responseText);
400
+ }
401
+ if (typeof body !== "object" || body === null) return void 0;
402
+ const record = body;
403
+ const message = typeof record.message === "string" ? record.message : "";
404
+ const space = byteCountsOf(record.space) ?? parseStorageQuotaBytes(message);
405
+ const isStorageBody = typeof record.error === "string" && STORAGE_REJECTION_BODY_ERRORS[record.error] === true;
406
+ if (!space && !isStorageBody) {
407
+ return void 0;
408
+ }
409
+ const details = { ...space };
410
+ const account = byteCountsOf(record.account);
411
+ if (account) {
412
+ const plan = record.account.plan;
413
+ details.account = typeof plan === "string" && plan !== "" ? { ...account, plan } : account;
414
+ }
415
+ return details;
416
+ }
378
417
  function storageRejectionError(service, status, meta, responseText) {
379
- const quotaMeta = { ...meta, ...parseStorageQuotaBytes(responseText) };
418
+ const quotaMeta = { ...meta, ...parseStorageRejection(responseText) };
380
419
  return status === 402 ? storageQuotaExceededError(service, STORAGE_FULL_MESSAGE, quotaMeta) : storageLimitReachedError(service, STORAGE_WRITE_TOO_LARGE_MESSAGE, quotaMeta);
381
420
  }
382
421
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
@@ -899,7 +938,7 @@ var KVService = class extends BaseService {
899
938
  if (response.status !== 402 && response.status !== 413) {
900
939
  return void 0;
901
940
  }
902
- if (response.status === 413 && !parseStorageQuotaBytes(errorText)) {
941
+ if (response.status === 413 && !parseStorageRejection(errorText)) {
903
942
  return void 0;
904
943
  }
905
944
  return err(
@@ -1939,6 +1978,7 @@ var KVService = class extends BaseService {
1939
1978
  "kv"
1940
1979
  ));
1941
1980
  }
1981
+ const requestPrefix = options.prefix === "/" ? "" : options.prefix;
1942
1982
  const headers = {};
1943
1983
  if (options.limit !== void 0) headers["x-tinycloud-limit"] = String(options.limit);
1944
1984
  if (options.cursor !== void 0) headers["x-tinycloud-cursor"] = options.cursor;
@@ -1948,7 +1988,7 @@ var KVService = class extends BaseService {
1948
1988
  const request = this.createRequestSignal(options.signal, options.timeout);
1949
1989
  try {
1950
1990
  const response = await this.invokeOperation(
1951
- options.prefix,
1991
+ requestPrefix,
1952
1992
  KVAction.SYNC,
1953
1993
  void 0,
1954
1994
  request.signal,
@@ -1964,7 +2004,7 @@ var KVService = class extends BaseService {
1964
2004
  } catch {
1965
2005
  body = void 0;
1966
2006
  }
1967
- const page = this.normalizeChangesResponse(body);
2007
+ const page = this.normalizeChangesResponse(body, options.prefix);
1968
2008
  if (!page) {
1969
2009
  return err(serviceError(
1970
2010
  ErrorCodes.NETWORK_ERROR,
@@ -2044,7 +2084,7 @@ var KVService = class extends BaseService {
2044
2084
  { meta }
2045
2085
  ));
2046
2086
  }
2047
- normalizeChangesResponse(data) {
2087
+ normalizeChangesResponse(data, requestedPrefix) {
2048
2088
  if (!isJsonObject(data)) return void 0;
2049
2089
  const { changes, more, cursor, source, authority } = data;
2050
2090
  if (!Array.isArray(changes) || typeof more !== "boolean" || typeof cursor !== "string" || !isJsonObject(source) || typeof source.nodeDid !== "string" || typeof source.space !== "string" || typeof source.prefix !== "string" || !isJsonObject(authority)) {
@@ -2076,7 +2116,11 @@ var KVService = class extends BaseService {
2076
2116
  changes: parsed,
2077
2117
  more,
2078
2118
  cursor,
2079
- source: { nodeDid: source.nodeDid, space: source.space, prefix: source.prefix },
2119
+ source: {
2120
+ nodeDid: source.nodeDid,
2121
+ space: source.space,
2122
+ prefix: requestedPrefix === "/" && source.prefix === "" ? "/" : source.prefix
2123
+ },
2080
2124
  authority: { notBefore, expiresAt, retainUntil }
2081
2125
  };
2082
2126
  }
@@ -2371,13 +2415,14 @@ ${canonical.space}
2371
2415
  ${canonical.principal}`;
2372
2416
  }
2373
2417
 
2374
- // src/kv/replication/scope.ts
2418
+ // src/kv/replication/prefix.ts
2375
2419
  function kvPrefixCovers(prefix, key) {
2376
- if (prefix === "") return true;
2377
- if (prefix.endsWith("/")) return key.startsWith(prefix);
2378
- return key === prefix || key.startsWith(`${prefix}/`);
2420
+ return prefix === "/" || key.startsWith(prefix);
2379
2421
  }
2422
+
2423
+ // src/kv/replication/scope.ts
2380
2424
  function requiresSecretsOptIn(space, prefix) {
2425
+ if (prefix === "/") return true;
2381
2426
  if (space === "secrets" || space.endsWith(":secrets")) return true;
2382
2427
  return prefix.split("/", 1)[0] === "vault";
2383
2428
  }