@tinycloud/sdk-services 2.7.0 → 2.8.0-beta.0

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.
@@ -1,4 +1,4 @@
1
- import { P as PermissionHint } from '../types-DAY1O1kW.cjs';
1
+ import { P as PermissionHint } from '../types-KCcgZiab.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-DAY1O1kW.js';
1
+ import { P as PermissionHint } from '../types-KCcgZiab.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
@@ -42,6 +42,9 @@ var ErrorCodes = {
42
42
  // KV-specific errors
43
43
  KV_NOT_FOUND: "KV_NOT_FOUND",
44
44
  KV_WRITE_FAILED: "KV_WRITE_FAILED",
45
+ KV_PRECONDITION_FAILED: "KV_PRECONDITION_FAILED",
46
+ KV_CONFLICT: "KV_CONFLICT",
47
+ KV_RESPONSE_TOO_LARGE: "KV_RESPONSE_TOO_LARGE",
45
48
  // SQL-specific errors
46
49
  SQL_ERROR: "SQL_ERROR",
47
50
  SQL_PERMISSION_DENIED: "SQL_PERMISSION_DENIED",
@@ -643,7 +646,7 @@ var KVService = class extends BaseService {
643
646
  * @param signal - Optional abort signal
644
647
  * @returns Fetch response
645
648
  */
646
- async invokeOperation(path, action, body, signal) {
649
+ async invokeOperation(path, action, body, signal, extraHeaders) {
647
650
  const session = this.context.session;
648
651
  const headers = this.context.invoke(
649
652
  session,
@@ -651,9 +654,10 @@ var KVService = class extends BaseService {
651
654
  path,
652
655
  action
653
656
  );
657
+ const requestHeaders = Array.isArray(headers) ? [...headers, ...Object.entries(extraHeaders ?? {})] : { ...headers, ...extraHeaders };
654
658
  return this.context.fetch(`${this.host}/invoke`, {
655
659
  method: "POST",
656
- headers,
660
+ headers: requestHeaders,
657
661
  body,
658
662
  signal: this.combineSignals(signal)
659
663
  });
@@ -835,13 +839,21 @@ var KVService = class extends BaseService {
835
839
  if (!this.requireAuth()) {
836
840
  return err(authRequiredError("kv"));
837
841
  }
842
+ if (options?.maxResponseBytes !== void 0 && (!Number.isSafeInteger(options.maxResponseBytes) || options.maxResponseBytes <= 0)) {
843
+ return err(serviceError(
844
+ ErrorCodes.INVALID_INPUT,
845
+ "KV maxResponseBytes must be a positive safe integer",
846
+ "kv"
847
+ ));
848
+ }
838
849
  const path = this.getFullPath(key, options?.prefix);
839
850
  try {
840
851
  const response = await this.invokeOperation(
841
852
  path,
842
853
  KVAction.GET,
843
854
  void 0,
844
- options?.signal
855
+ options?.signal,
856
+ options?.maxResponseBytes === void 0 ? void 0 : { "x-tinycloud-max-response-bytes": String(options.maxResponseBytes) }
845
857
  );
846
858
  if (!response.ok) {
847
859
  if (response.status === 401 || response.status === 403) {
@@ -859,6 +871,14 @@ var KVService = class extends BaseService {
859
871
  return this.classifyNotFound(response, key);
860
872
  }
861
873
  const errorText = await response.text();
874
+ if (response.status === 413) {
875
+ return err(serviceError(
876
+ ErrorCodes.KV_RESPONSE_TOO_LARGE,
877
+ `KV value at key "${key}" exceeds the requested response limit`,
878
+ "kv",
879
+ { meta: { status: response.status, statusText: response.statusText } }
880
+ ));
881
+ }
862
882
  return err(
863
883
  serviceError(
864
884
  ErrorCodes.NETWORK_ERROR,
@@ -890,6 +910,13 @@ var KVService = class extends BaseService {
890
910
  if (!this.requireAuth()) {
891
911
  return err(authRequiredError("kv"));
892
912
  }
913
+ if (options?.ifMatch !== void 0 && options.ifNoneMatch !== void 0) {
914
+ return err(serviceError(
915
+ ErrorCodes.INVALID_INPUT,
916
+ "KV put cannot combine ifMatch and ifNoneMatch",
917
+ "kv"
918
+ ));
919
+ }
893
920
  const path = this.getFullPath(key, options?.prefix);
894
921
  const body = this.serializePutValue(value, options?.contentType);
895
922
  try {
@@ -897,7 +924,11 @@ var KVService = class extends BaseService {
897
924
  path,
898
925
  KVAction.PUT,
899
926
  body,
900
- options?.signal
927
+ options?.signal,
928
+ {
929
+ ...options?.ifMatch === void 0 ? {} : { "if-match": options.ifMatch },
930
+ ...options?.ifNoneMatch === void 0 ? {} : { "if-none-match": options.ifNoneMatch }
931
+ }
901
932
  );
902
933
  if (!response.ok) {
903
934
  if (response.status === 401) {
@@ -910,6 +941,22 @@ var KVService = class extends BaseService {
910
941
  }));
911
942
  }
912
943
  const errorText = await response.text();
944
+ if (response.status === 412) {
945
+ return err(serviceError(
946
+ ErrorCodes.KV_PRECONDITION_FAILED,
947
+ `KV precondition failed for key "${key}"`,
948
+ "kv",
949
+ { meta: { status: response.status, statusText: response.statusText } }
950
+ ));
951
+ }
952
+ if (response.status === 503 && (options?.ifMatch !== void 0 || options?.ifNoneMatch !== void 0)) {
953
+ return err(serviceError(
954
+ ErrorCodes.KV_CONFLICT,
955
+ `Concurrent KV update conflicted for key "${key}"`,
956
+ "kv",
957
+ { meta: { status: response.status, statusText: response.statusText } }
958
+ ));
959
+ }
913
960
  const quotaError = this.handleQuotaErrorResponse(
914
961
  response,
915
962
  errorText,
@@ -1045,6 +1092,13 @@ var KVService = class extends BaseService {
1045
1092
  if (!this.requireAuth()) {
1046
1093
  return err(authRequiredError("kv"));
1047
1094
  }
1095
+ if (options?.limit !== void 0 && (!Number.isInteger(options.limit) || options.limit < 1 || options.limit > 1e3)) {
1096
+ return err(serviceError(
1097
+ ErrorCodes.INVALID_INPUT,
1098
+ "KV list limit must be an integer from 1 through 1000",
1099
+ "kv"
1100
+ ));
1101
+ }
1048
1102
  let listPath = options?.prefix ?? this._config.prefix ?? "";
1049
1103
  if (options?.path) {
1050
1104
  listPath = listPath ? `${listPath}/${options.path}` : options.path;
@@ -1054,7 +1108,8 @@ var KVService = class extends BaseService {
1054
1108
  listPath,
1055
1109
  KVAction.LIST,
1056
1110
  void 0,
1057
- options?.signal
1111
+ options?.signal,
1112
+ options?.limit === void 0 ? void 0 : { "x-tinycloud-limit": String(options.limit) }
1058
1113
  );
1059
1114
  if (!response.ok) {
1060
1115
  if (response.status === 401) {
@@ -1084,7 +1139,10 @@ var KVService = class extends BaseService {
1084
1139
  (key) => key.startsWith(prefixWithSlash) ? key.slice(prefixWithSlash.length) : key
1085
1140
  );
1086
1141
  }
1087
- return ok({ keys });
1142
+ return ok({
1143
+ keys,
1144
+ ...response.headers.get("x-tinycloud-truncated") === null ? {} : { truncated: response.headers.get("x-tinycloud-truncated") === "true" }
1145
+ });
1088
1146
  } catch (error) {
1089
1147
  return err(wrapError("kv", error));
1090
1148
  }
@@ -1104,7 +1162,8 @@ var KVService = class extends BaseService {
1104
1162
  path,
1105
1163
  KVAction.DELETE,
1106
1164
  void 0,
1107
- options?.signal
1165
+ options?.signal,
1166
+ options?.ifMatch === void 0 ? void 0 : { "if-match": options.ifMatch }
1108
1167
  );
1109
1168
  if (!response.ok) {
1110
1169
  if (response.status === 401) {
@@ -1120,6 +1179,22 @@ var KVService = class extends BaseService {
1120
1179
  return this.classifyNotFound(response, key);
1121
1180
  }
1122
1181
  const errorText = await response.text();
1182
+ if (response.status === 412) {
1183
+ return err(serviceError(
1184
+ ErrorCodes.KV_PRECONDITION_FAILED,
1185
+ `KV precondition failed for key "${key}"`,
1186
+ "kv",
1187
+ { meta: { status: response.status, statusText: response.statusText } }
1188
+ ));
1189
+ }
1190
+ if (response.status === 503 && options?.ifMatch !== void 0) {
1191
+ return err(serviceError(
1192
+ ErrorCodes.KV_CONFLICT,
1193
+ `Concurrent KV delete conflicted for key "${key}"`,
1194
+ "kv",
1195
+ { meta: { status: response.status, statusText: response.statusText } }
1196
+ ));
1197
+ }
1123
1198
  return err(
1124
1199
  serviceError(
1125
1200
  ErrorCodes.NETWORK_ERROR,
@@ -1129,7 +1204,10 @@ var KVService = class extends BaseService {
1129
1204
  )
1130
1205
  );
1131
1206
  }
1132
- return ok(void 0);
1207
+ return ok({
1208
+ data: void 0,
1209
+ headers: this.createResponseHeaders(response.headers)
1210
+ });
1133
1211
  } catch (error) {
1134
1212
  return err(wrapError("kv", error));
1135
1213
  }