@livo-tv/blocks 1.6.1 → 1.7.0

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
@@ -5056,6 +5056,37 @@ function FileUploadMenu({
5056
5056
  }
5057
5057
 
5058
5058
  // src/components/upload/createMultipartUploadController.ts
5059
+ var PART_ATTEMPTS = 3;
5060
+ var PART_RETRY_BACKOFF_MS = [500, 1500];
5061
+ function defaultSleep(ms) {
5062
+ return new Promise((resolve) => {
5063
+ setTimeout(resolve, ms);
5064
+ });
5065
+ }
5066
+ function isAbortError(error) {
5067
+ return error instanceof DOMException && error.name === "AbortError";
5068
+ }
5069
+ function isMissingEtagError(error) {
5070
+ return error instanceof Error && error.message.includes("missing ETag");
5071
+ }
5072
+ function isRetryableStatus(status) {
5073
+ return status === 0 || status === 403 || status === 408 || status === 429 || status >= 500;
5074
+ }
5075
+ function putPartFailure(partNumber, status) {
5076
+ return new Error(`Upload failed (part ${partNumber}, status ${status})`);
5077
+ }
5078
+ function parsePutStatus(error) {
5079
+ if (!(error instanceof Error)) return null;
5080
+ const match = error.message.match(/status (\d+)\)$/);
5081
+ if (!match?.[1]) return null;
5082
+ return Number(match[1]);
5083
+ }
5084
+ function isRetryablePutError(error) {
5085
+ if (isAbortError(error) || isMissingEtagError(error)) return false;
5086
+ const status = parsePutStatus(error);
5087
+ if (status === null) return false;
5088
+ return isRetryableStatus(status);
5089
+ }
5059
5090
  function readEtag(xhr) {
5060
5091
  const raw = xhr.getResponseHeader("ETag") ?? xhr.getResponseHeader("etag") ?? "";
5061
5092
  const etag = raw.trim();
@@ -5079,10 +5110,10 @@ function putPart(input) {
5079
5110
  }
5080
5111
  return;
5081
5112
  }
5082
- reject(new Error(`Upload failed (${input.xhr.status})`));
5113
+ reject(putPartFailure(input.partNumber, input.xhr.status));
5083
5114
  };
5084
5115
  input.xhr.onerror = () => {
5085
- reject(new Error("Upload failed"));
5116
+ reject(putPartFailure(input.partNumber, 0));
5086
5117
  };
5087
5118
  input.xhr.onabort = () => {
5088
5119
  reject(new DOMException("The operation was aborted.", "AbortError"));
@@ -5092,8 +5123,9 @@ function putPart(input) {
5092
5123
  }
5093
5124
  function createMultipartUploadController(options) {
5094
5125
  const ranges = partRanges(options.file.size, options.partSize);
5095
- const completed = [];
5126
+ const sleep = options.sleep ?? defaultSleep;
5096
5127
  const state = { status: "idle" };
5128
+ const completed = [];
5097
5129
  let currentXhr = null;
5098
5130
  let run = null;
5099
5131
  let resumeIndex = 0;
@@ -5113,6 +5145,40 @@ function createMultipartUploadController(options) {
5113
5145
  progressFromBytes(completedBytes + currentPartLoaded, options.file.size)
5114
5146
  );
5115
5147
  }
5148
+ async function putPartWithRetry(partNumber, body) {
5149
+ let lastError;
5150
+ for (let attempt = 1; attempt <= PART_ATTEMPTS; attempt += 1) {
5151
+ if (state.status !== "uploading") {
5152
+ throw lastError ?? new DOMException("The operation was aborted.", "AbortError");
5153
+ }
5154
+ const url = await options.adapter.requestPartUrl(partNumber);
5155
+ if (state.status !== "uploading") {
5156
+ throw new DOMException("The operation was aborted.", "AbortError");
5157
+ }
5158
+ const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5159
+ currentXhr = xhr;
5160
+ try {
5161
+ return await putPart({
5162
+ url,
5163
+ body,
5164
+ xhr,
5165
+ partNumber,
5166
+ onProgress: (loaded) => emitProgress(loaded)
5167
+ });
5168
+ } catch (error) {
5169
+ lastError = error;
5170
+ if (currentXhr === xhr) currentXhr = null;
5171
+ if (isStopped() || !isRetryablePutError(error) || attempt === PART_ATTEMPTS) {
5172
+ throw error;
5173
+ }
5174
+ const delay = PART_RETRY_BACKOFF_MS[attempt - 1] ?? PART_RETRY_BACKOFF_MS[PART_RETRY_BACKOFF_MS.length - 1];
5175
+ await sleep(delay);
5176
+ } finally {
5177
+ if (currentXhr === xhr) currentXhr = null;
5178
+ }
5179
+ }
5180
+ throw lastError;
5181
+ }
5116
5182
  async function uploadFrom(startIndex) {
5117
5183
  state.status = "uploading";
5118
5184
  for (let index = startIndex; index < ranges.length; index += 1) {
@@ -5120,18 +5186,10 @@ function createMultipartUploadController(options) {
5120
5186
  resumeIndex = index;
5121
5187
  const range = ranges[index];
5122
5188
  if (!range) continue;
5123
- const url = await options.adapter.requestPartUrl(range.partNumber);
5124
- if (state.status !== "uploading") return;
5125
- const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5126
- currentXhr = xhr;
5127
5189
  const body = options.file.slice(range.start, range.end);
5128
5190
  try {
5129
- const etag = await putPart({
5130
- url,
5131
- body,
5132
- xhr,
5133
- onProgress: (loaded) => emitProgress(loaded)
5134
- });
5191
+ const etag = await putPartWithRetry(range.partNumber, body);
5192
+ if (state.status !== "uploading") return;
5135
5193
  completed.push({ partNumber: range.partNumber, etag });
5136
5194
  emitProgress(0);
5137
5195
  } catch (error) {
@@ -5139,8 +5197,6 @@ function createMultipartUploadController(options) {
5139
5197
  if (isStopped()) return;
5140
5198
  state.status = "error";
5141
5199
  throw error;
5142
- } finally {
5143
- if (currentXhr === xhr) currentXhr = null;
5144
5200
  }
5145
5201
  }
5146
5202
  if (state.status !== "uploading") return;