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

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/index.cjs CHANGED
@@ -2191,6 +2191,7 @@ var KVService = class extends BaseService {
2191
2191
  }
2192
2192
  seen.add(path);
2193
2193
  }
2194
+ let requestMayHaveDispatched = false;
2194
2195
  try {
2195
2196
  const body = new FormData();
2196
2197
  for (let index = 0; index < items.length; index++) {
@@ -2208,14 +2209,31 @@ var KVService = class extends BaseService {
2208
2209
  action: KVAction.PUT
2209
2210
  }))
2210
2211
  );
2211
- const response = await this.context.fetch(`${this.host}/invoke`, {
2212
+ const url = `${this.host}/invoke`;
2213
+ const init = {
2212
2214
  method: "POST",
2213
2215
  headers,
2214
2216
  body,
2215
2217
  signal: this.combineSignals(options?.signal)
2216
- });
2218
+ };
2219
+ const fetchFn = this.context.fetch;
2220
+ requestMayHaveDispatched = true;
2221
+ const response = await fetchFn(url, init);
2217
2222
  if (!response.ok) {
2218
- const errorText = await response.text();
2223
+ let errorText;
2224
+ try {
2225
+ errorText = await response.text();
2226
+ } catch (textError) {
2227
+ const cause = textError instanceof Error ? textError : new Error(String(textError));
2228
+ return err(
2229
+ serviceError(
2230
+ ErrorCodes.KV_WRITE_FAILED,
2231
+ `Failed to batch put ${items.length} key(s): ${response.status} - <response body could not be read: ${cause.message}>`,
2232
+ "kv",
2233
+ { cause, meta: { status: response.status, statusText: response.statusText } }
2234
+ )
2235
+ );
2236
+ }
2219
2237
  if (response.status === 401 || response.status === 403) {
2220
2238
  const { resource, action } = parseAuthError(errorText);
2221
2239
  return err(authUnauthorizedError("kv", errorText, {
@@ -2241,19 +2259,73 @@ var KVService = class extends BaseService {
2241
2259
  )
2242
2260
  );
2243
2261
  }
2244
- const batchResponse = this.normalizeBatchPutResponse(await response.json());
2245
- if (!batchResponse || batchResponse.count !== batchResponse.written.length) {
2262
+ let rawBody;
2263
+ try {
2264
+ rawBody = await response.json();
2265
+ } catch (jsonError) {
2266
+ const cause = jsonError instanceof Error ? jsonError : new Error(String(jsonError));
2267
+ return err(
2268
+ serviceError(
2269
+ ErrorCodes.NETWORK_ERROR,
2270
+ `KV batchPut response was not valid JSON: ${cause.message}`,
2271
+ "kv",
2272
+ {
2273
+ cause,
2274
+ meta: {
2275
+ requestMayHaveDispatched: true,
2276
+ responseReceived: true,
2277
+ status: response.status,
2278
+ outcome: "batch-unconfirmed"
2279
+ }
2280
+ }
2281
+ )
2282
+ );
2283
+ }
2284
+ const batchResponse = this.normalizeBatchPutResponse(rawBody);
2285
+ if (!batchResponse) {
2246
2286
  return err(
2247
2287
  serviceError(
2248
2288
  ErrorCodes.NETWORK_ERROR,
2249
2289
  "KV batchPut response did not include matching written keys and count",
2250
- "kv"
2290
+ "kv",
2291
+ {
2292
+ meta: {
2293
+ requestMayHaveDispatched: true,
2294
+ responseReceived: true,
2295
+ status: response.status,
2296
+ outcome: "batch-unconfirmed"
2297
+ }
2298
+ }
2299
+ )
2300
+ );
2301
+ }
2302
+ const requestedPaths = new Set(paths);
2303
+ const writtenPaths = new Set(batchResponse.written);
2304
+ const matchesRequest = batchResponse.count === paths.length && batchResponse.written.length === paths.length && writtenPaths.size === paths.length && batchResponse.written.every((key) => requestedPaths.has(key));
2305
+ if (!matchesRequest) {
2306
+ return err(
2307
+ serviceError(
2308
+ ErrorCodes.NETWORK_ERROR,
2309
+ `KV batchPut response did not confirm all ${paths.length} requested key(s) were written`,
2310
+ "kv",
2311
+ {
2312
+ meta: {
2313
+ requestMayHaveDispatched: true,
2314
+ responseReceived: true,
2315
+ status: response.status,
2316
+ outcome: "batch-unconfirmed"
2317
+ }
2318
+ }
2251
2319
  )
2252
2320
  );
2253
2321
  }
2254
2322
  return ok(batchResponse);
2255
2323
  } catch (error) {
2256
- return err(wrapError("kv", error));
2324
+ const wrapped = wrapError("kv", error);
2325
+ return err({
2326
+ ...wrapped,
2327
+ meta: { ...wrapped.meta, requestMayHaveDispatched }
2328
+ });
2257
2329
  }
2258
2330
  });
2259
2331
  }