@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/index.js CHANGED
@@ -3676,6 +3676,7 @@ var init_dist2 = __esm({
3676
3676
  }
3677
3677
  seen.add(path);
3678
3678
  }
3679
+ let requestMayHaveDispatched = false;
3679
3680
  try {
3680
3681
  const body = new FormData();
3681
3682
  for (let index = 0; index < items.length; index++) {
@@ -3693,14 +3694,31 @@ var init_dist2 = __esm({
3693
3694
  action: KVAction.PUT
3694
3695
  }))
3695
3696
  );
3696
- const response = await this.context.fetch(`${this.host}/invoke`, {
3697
+ const url = `${this.host}/invoke`;
3698
+ const init = {
3697
3699
  method: "POST",
3698
3700
  headers,
3699
3701
  body,
3700
3702
  signal: this.combineSignals(options?.signal)
3701
- });
3703
+ };
3704
+ const fetchFn = this.context.fetch;
3705
+ requestMayHaveDispatched = true;
3706
+ const response = await fetchFn(url, init);
3702
3707
  if (!response.ok) {
3703
- const errorText = await response.text();
3708
+ let errorText;
3709
+ try {
3710
+ errorText = await response.text();
3711
+ } catch (textError) {
3712
+ const cause = textError instanceof Error ? textError : new Error(String(textError));
3713
+ return err(
3714
+ serviceError(
3715
+ ErrorCodes.KV_WRITE_FAILED,
3716
+ `Failed to batch put ${items.length} key(s): ${response.status} - <response body could not be read: ${cause.message}>`,
3717
+ "kv",
3718
+ { cause, meta: { status: response.status, statusText: response.statusText } }
3719
+ )
3720
+ );
3721
+ }
3704
3722
  if (response.status === 401 || response.status === 403) {
3705
3723
  const { resource, action } = parseAuthError(errorText);
3706
3724
  return err(authUnauthorizedError("kv", errorText, {
@@ -3726,19 +3744,73 @@ var init_dist2 = __esm({
3726
3744
  )
3727
3745
  );
3728
3746
  }
3729
- const batchResponse = this.normalizeBatchPutResponse(await response.json());
3730
- if (!batchResponse || batchResponse.count !== batchResponse.written.length) {
3747
+ let rawBody;
3748
+ try {
3749
+ rawBody = await response.json();
3750
+ } catch (jsonError) {
3751
+ const cause = jsonError instanceof Error ? jsonError : new Error(String(jsonError));
3752
+ return err(
3753
+ serviceError(
3754
+ ErrorCodes.NETWORK_ERROR,
3755
+ `KV batchPut response was not valid JSON: ${cause.message}`,
3756
+ "kv",
3757
+ {
3758
+ cause,
3759
+ meta: {
3760
+ requestMayHaveDispatched: true,
3761
+ responseReceived: true,
3762
+ status: response.status,
3763
+ outcome: "batch-unconfirmed"
3764
+ }
3765
+ }
3766
+ )
3767
+ );
3768
+ }
3769
+ const batchResponse = this.normalizeBatchPutResponse(rawBody);
3770
+ if (!batchResponse) {
3731
3771
  return err(
3732
3772
  serviceError(
3733
3773
  ErrorCodes.NETWORK_ERROR,
3734
3774
  "KV batchPut response did not include matching written keys and count",
3735
- "kv"
3775
+ "kv",
3776
+ {
3777
+ meta: {
3778
+ requestMayHaveDispatched: true,
3779
+ responseReceived: true,
3780
+ status: response.status,
3781
+ outcome: "batch-unconfirmed"
3782
+ }
3783
+ }
3784
+ )
3785
+ );
3786
+ }
3787
+ const requestedPaths = new Set(paths);
3788
+ const writtenPaths = new Set(batchResponse.written);
3789
+ const matchesRequest = batchResponse.count === paths.length && batchResponse.written.length === paths.length && writtenPaths.size === paths.length && batchResponse.written.every((key) => requestedPaths.has(key));
3790
+ if (!matchesRequest) {
3791
+ return err(
3792
+ serviceError(
3793
+ ErrorCodes.NETWORK_ERROR,
3794
+ `KV batchPut response did not confirm all ${paths.length} requested key(s) were written`,
3795
+ "kv",
3796
+ {
3797
+ meta: {
3798
+ requestMayHaveDispatched: true,
3799
+ responseReceived: true,
3800
+ status: response.status,
3801
+ outcome: "batch-unconfirmed"
3802
+ }
3803
+ }
3736
3804
  )
3737
3805
  );
3738
3806
  }
3739
3807
  return ok(batchResponse);
3740
3808
  } catch (error) {
3741
- return err(wrapError("kv", error));
3809
+ const wrapped = wrapError("kv", error);
3810
+ return err({
3811
+ ...wrapped,
3812
+ meta: { ...wrapped.meta, requestMayHaveDispatched }
3813
+ });
3742
3814
  }
3743
3815
  });
3744
3816
  }