@tinycloud/sdk-services 2.10.0 → 2.11.0-beta.11

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
@@ -1221,6 +1221,7 @@ var KVService = class extends BaseService {
1221
1221
  }
1222
1222
  seen.add(path);
1223
1223
  }
1224
+ let requestMayHaveDispatched = false;
1224
1225
  try {
1225
1226
  const body = new FormData();
1226
1227
  for (let index = 0; index < items.length; index++) {
@@ -1238,14 +1239,31 @@ var KVService = class extends BaseService {
1238
1239
  action: KVAction.PUT
1239
1240
  }))
1240
1241
  );
1241
- const response = await this.context.fetch(`${this.host}/invoke`, {
1242
+ const url = `${this.host}/invoke`;
1243
+ const init = {
1242
1244
  method: "POST",
1243
1245
  headers,
1244
1246
  body,
1245
1247
  signal: this.combineSignals(options?.signal)
1246
- });
1248
+ };
1249
+ const fetchFn = this.context.fetch;
1250
+ requestMayHaveDispatched = true;
1251
+ const response = await fetchFn(url, init);
1247
1252
  if (!response.ok) {
1248
- const errorText = await response.text();
1253
+ let errorText;
1254
+ try {
1255
+ errorText = await response.text();
1256
+ } catch (textError) {
1257
+ const cause = textError instanceof Error ? textError : new Error(String(textError));
1258
+ return err(
1259
+ serviceError(
1260
+ ErrorCodes.KV_WRITE_FAILED,
1261
+ `Failed to batch put ${items.length} key(s): ${response.status} - <response body could not be read: ${cause.message}>`,
1262
+ "kv",
1263
+ { cause, meta: { status: response.status, statusText: response.statusText } }
1264
+ )
1265
+ );
1266
+ }
1249
1267
  if (response.status === 401 || response.status === 403) {
1250
1268
  const { resource, action } = parseAuthError(errorText);
1251
1269
  return err(authUnauthorizedError("kv", errorText, {
@@ -1271,19 +1289,73 @@ var KVService = class extends BaseService {
1271
1289
  )
1272
1290
  );
1273
1291
  }
1274
- const batchResponse = this.normalizeBatchPutResponse(await response.json());
1275
- if (!batchResponse || batchResponse.count !== batchResponse.written.length) {
1292
+ let rawBody;
1293
+ try {
1294
+ rawBody = await response.json();
1295
+ } catch (jsonError) {
1296
+ const cause = jsonError instanceof Error ? jsonError : new Error(String(jsonError));
1297
+ return err(
1298
+ serviceError(
1299
+ ErrorCodes.NETWORK_ERROR,
1300
+ `KV batchPut response was not valid JSON: ${cause.message}`,
1301
+ "kv",
1302
+ {
1303
+ cause,
1304
+ meta: {
1305
+ requestMayHaveDispatched: true,
1306
+ responseReceived: true,
1307
+ status: response.status,
1308
+ outcome: "batch-unconfirmed"
1309
+ }
1310
+ }
1311
+ )
1312
+ );
1313
+ }
1314
+ const batchResponse = this.normalizeBatchPutResponse(rawBody);
1315
+ if (!batchResponse) {
1276
1316
  return err(
1277
1317
  serviceError(
1278
1318
  ErrorCodes.NETWORK_ERROR,
1279
1319
  "KV batchPut response did not include matching written keys and count",
1280
- "kv"
1320
+ "kv",
1321
+ {
1322
+ meta: {
1323
+ requestMayHaveDispatched: true,
1324
+ responseReceived: true,
1325
+ status: response.status,
1326
+ outcome: "batch-unconfirmed"
1327
+ }
1328
+ }
1329
+ )
1330
+ );
1331
+ }
1332
+ const requestedPaths = new Set(paths);
1333
+ const writtenPaths = new Set(batchResponse.written);
1334
+ const matchesRequest = batchResponse.count === paths.length && batchResponse.written.length === paths.length && writtenPaths.size === paths.length && batchResponse.written.every((key) => requestedPaths.has(key));
1335
+ if (!matchesRequest) {
1336
+ return err(
1337
+ serviceError(
1338
+ ErrorCodes.NETWORK_ERROR,
1339
+ `KV batchPut response did not confirm all ${paths.length} requested key(s) were written`,
1340
+ "kv",
1341
+ {
1342
+ meta: {
1343
+ requestMayHaveDispatched: true,
1344
+ responseReceived: true,
1345
+ status: response.status,
1346
+ outcome: "batch-unconfirmed"
1347
+ }
1348
+ }
1281
1349
  )
1282
1350
  );
1283
1351
  }
1284
1352
  return ok(batchResponse);
1285
1353
  } catch (error) {
1286
- return err(wrapError("kv", error));
1354
+ const wrapped = wrapError("kv", error);
1355
+ return err({
1356
+ ...wrapped,
1357
+ meta: { ...wrapped.meta, requestMayHaveDispatched }
1358
+ });
1287
1359
  }
1288
1360
  });
1289
1361
  }