@tinycloud/operations 0.3.2-beta.2 → 0.3.2-beta.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.
@@ -2261,6 +2261,7 @@ var KVService = class extends BaseService {
2261
2261
  }
2262
2262
  seen.add(path);
2263
2263
  }
2264
+ let requestMayHaveDispatched = false;
2264
2265
  try {
2265
2266
  const body = new FormData();
2266
2267
  for (let index = 0; index < items.length; index++) {
@@ -2278,14 +2279,31 @@ var KVService = class extends BaseService {
2278
2279
  action: KVAction.PUT
2279
2280
  }))
2280
2281
  );
2281
- const response = await this.context.fetch(`${this.host}/invoke`, {
2282
+ const url = `${this.host}/invoke`;
2283
+ const init = {
2282
2284
  method: "POST",
2283
2285
  headers,
2284
2286
  body,
2285
2287
  signal: this.combineSignals(options?.signal)
2286
- });
2288
+ };
2289
+ const fetchFn = this.context.fetch;
2290
+ requestMayHaveDispatched = true;
2291
+ const response = await fetchFn(url, init);
2287
2292
  if (!response.ok) {
2288
- const errorText = await response.text();
2293
+ let errorText;
2294
+ try {
2295
+ errorText = await response.text();
2296
+ } catch (textError) {
2297
+ const cause = textError instanceof Error ? textError : new Error(String(textError));
2298
+ return err(
2299
+ serviceError(
2300
+ ErrorCodes.KV_WRITE_FAILED,
2301
+ `Failed to batch put ${items.length} key(s): ${response.status} - <response body could not be read: ${cause.message}>`,
2302
+ "kv",
2303
+ { cause, meta: { status: response.status, statusText: response.statusText } }
2304
+ )
2305
+ );
2306
+ }
2289
2307
  if (response.status === 401 || response.status === 403) {
2290
2308
  const { resource, action } = parseAuthError(errorText);
2291
2309
  return err(authUnauthorizedError("kv", errorText, {
@@ -2311,19 +2329,73 @@ var KVService = class extends BaseService {
2311
2329
  )
2312
2330
  );
2313
2331
  }
2314
- const batchResponse = this.normalizeBatchPutResponse(await response.json());
2315
- if (!batchResponse || batchResponse.count !== batchResponse.written.length) {
2332
+ let rawBody;
2333
+ try {
2334
+ rawBody = await response.json();
2335
+ } catch (jsonError) {
2336
+ const cause = jsonError instanceof Error ? jsonError : new Error(String(jsonError));
2337
+ return err(
2338
+ serviceError(
2339
+ ErrorCodes.NETWORK_ERROR,
2340
+ `KV batchPut response was not valid JSON: ${cause.message}`,
2341
+ "kv",
2342
+ {
2343
+ cause,
2344
+ meta: {
2345
+ requestMayHaveDispatched: true,
2346
+ responseReceived: true,
2347
+ status: response.status,
2348
+ outcome: "batch-unconfirmed"
2349
+ }
2350
+ }
2351
+ )
2352
+ );
2353
+ }
2354
+ const batchResponse = this.normalizeBatchPutResponse(rawBody);
2355
+ if (!batchResponse) {
2316
2356
  return err(
2317
2357
  serviceError(
2318
2358
  ErrorCodes.NETWORK_ERROR,
2319
2359
  "KV batchPut response did not include matching written keys and count",
2320
- "kv"
2360
+ "kv",
2361
+ {
2362
+ meta: {
2363
+ requestMayHaveDispatched: true,
2364
+ responseReceived: true,
2365
+ status: response.status,
2366
+ outcome: "batch-unconfirmed"
2367
+ }
2368
+ }
2369
+ )
2370
+ );
2371
+ }
2372
+ const requestedPaths = new Set(paths);
2373
+ const writtenPaths = new Set(batchResponse.written);
2374
+ const matchesRequest = batchResponse.count === paths.length && batchResponse.written.length === paths.length && writtenPaths.size === paths.length && batchResponse.written.every((key) => requestedPaths.has(key));
2375
+ if (!matchesRequest) {
2376
+ return err(
2377
+ serviceError(
2378
+ ErrorCodes.NETWORK_ERROR,
2379
+ `KV batchPut response did not confirm all ${paths.length} requested key(s) were written`,
2380
+ "kv",
2381
+ {
2382
+ meta: {
2383
+ requestMayHaveDispatched: true,
2384
+ responseReceived: true,
2385
+ status: response.status,
2386
+ outcome: "batch-unconfirmed"
2387
+ }
2388
+ }
2321
2389
  )
2322
2390
  );
2323
2391
  }
2324
2392
  return ok(batchResponse);
2325
2393
  } catch (error) {
2326
- return err(wrapError("kv", error));
2394
+ const wrapped = wrapError("kv", error);
2395
+ return err({
2396
+ ...wrapped,
2397
+ meta: { ...wrapped.meta, requestMayHaveDispatched }
2398
+ });
2327
2399
  }
2328
2400
  });
2329
2401
  }