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