@tinycloud/operations 0.3.2-beta.3 → 0.3.2-beta.6

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