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

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.js CHANGED
@@ -2050,6 +2050,7 @@ var KVService = class extends BaseService {
2050
2050
  }
2051
2051
  seen.add(path);
2052
2052
  }
2053
+ let requestMayHaveDispatched = false;
2053
2054
  try {
2054
2055
  const body = new FormData();
2055
2056
  for (let index = 0; index < items.length; index++) {
@@ -2067,14 +2068,31 @@ var KVService = class extends BaseService {
2067
2068
  action: KVAction.PUT
2068
2069
  }))
2069
2070
  );
2070
- const response = await this.context.fetch(`${this.host}/invoke`, {
2071
+ const url = `${this.host}/invoke`;
2072
+ const init = {
2071
2073
  method: "POST",
2072
2074
  headers,
2073
2075
  body,
2074
2076
  signal: this.combineSignals(options?.signal)
2075
- });
2077
+ };
2078
+ const fetchFn = this.context.fetch;
2079
+ requestMayHaveDispatched = true;
2080
+ const response = await fetchFn(url, init);
2076
2081
  if (!response.ok) {
2077
- const errorText = await response.text();
2082
+ let errorText;
2083
+ try {
2084
+ errorText = await response.text();
2085
+ } catch (textError) {
2086
+ const cause = textError instanceof Error ? textError : new Error(String(textError));
2087
+ return err(
2088
+ serviceError(
2089
+ ErrorCodes.KV_WRITE_FAILED,
2090
+ `Failed to batch put ${items.length} key(s): ${response.status} - <response body could not be read: ${cause.message}>`,
2091
+ "kv",
2092
+ { cause, meta: { status: response.status, statusText: response.statusText } }
2093
+ )
2094
+ );
2095
+ }
2078
2096
  if (response.status === 401 || response.status === 403) {
2079
2097
  const { resource, action } = parseAuthError(errorText);
2080
2098
  return err(authUnauthorizedError("kv", errorText, {
@@ -2100,19 +2118,73 @@ var KVService = class extends BaseService {
2100
2118
  )
2101
2119
  );
2102
2120
  }
2103
- const batchResponse = this.normalizeBatchPutResponse(await response.json());
2104
- if (!batchResponse || batchResponse.count !== batchResponse.written.length) {
2121
+ let rawBody;
2122
+ try {
2123
+ rawBody = await response.json();
2124
+ } catch (jsonError) {
2125
+ const cause = jsonError instanceof Error ? jsonError : new Error(String(jsonError));
2126
+ return err(
2127
+ serviceError(
2128
+ ErrorCodes.NETWORK_ERROR,
2129
+ `KV batchPut response was not valid JSON: ${cause.message}`,
2130
+ "kv",
2131
+ {
2132
+ cause,
2133
+ meta: {
2134
+ requestMayHaveDispatched: true,
2135
+ responseReceived: true,
2136
+ status: response.status,
2137
+ outcome: "batch-unconfirmed"
2138
+ }
2139
+ }
2140
+ )
2141
+ );
2142
+ }
2143
+ const batchResponse = this.normalizeBatchPutResponse(rawBody);
2144
+ if (!batchResponse) {
2105
2145
  return err(
2106
2146
  serviceError(
2107
2147
  ErrorCodes.NETWORK_ERROR,
2108
2148
  "KV batchPut response did not include matching written keys and count",
2109
- "kv"
2149
+ "kv",
2150
+ {
2151
+ meta: {
2152
+ requestMayHaveDispatched: true,
2153
+ responseReceived: true,
2154
+ status: response.status,
2155
+ outcome: "batch-unconfirmed"
2156
+ }
2157
+ }
2158
+ )
2159
+ );
2160
+ }
2161
+ const requestedPaths = new Set(paths);
2162
+ const writtenPaths = new Set(batchResponse.written);
2163
+ const matchesRequest = batchResponse.count === paths.length && batchResponse.written.length === paths.length && writtenPaths.size === paths.length && batchResponse.written.every((key) => requestedPaths.has(key));
2164
+ if (!matchesRequest) {
2165
+ return err(
2166
+ serviceError(
2167
+ ErrorCodes.NETWORK_ERROR,
2168
+ `KV batchPut response did not confirm all ${paths.length} requested key(s) were written`,
2169
+ "kv",
2170
+ {
2171
+ meta: {
2172
+ requestMayHaveDispatched: true,
2173
+ responseReceived: true,
2174
+ status: response.status,
2175
+ outcome: "batch-unconfirmed"
2176
+ }
2177
+ }
2110
2178
  )
2111
2179
  );
2112
2180
  }
2113
2181
  return ok(batchResponse);
2114
2182
  } catch (error) {
2115
- return err(wrapError("kv", error));
2183
+ const wrapped = wrapError("kv", error);
2184
+ return err({
2185
+ ...wrapped,
2186
+ meta: { ...wrapped.meta, requestMayHaveDispatched }
2187
+ });
2116
2188
  }
2117
2189
  });
2118
2190
  }