@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.d.cts CHANGED
@@ -976,6 +976,7 @@ type CreateMultipartUploadControllerOptions = {
976
976
  adapter: MultipartUploadAdapter;
977
977
  onProgress?: (progress: UploadProgress) => void;
978
978
  xhrFactory?: () => XMLHttpRequest;
979
+ sleep?: (ms: number) => Promise<void>;
979
980
  };
980
981
  type MultipartUploadController = {
981
982
  start: () => Promise<void>;
package/dist/index.d.ts CHANGED
@@ -976,6 +976,7 @@ type CreateMultipartUploadControllerOptions = {
976
976
  adapter: MultipartUploadAdapter;
977
977
  onProgress?: (progress: UploadProgress) => void;
978
978
  xhrFactory?: () => XMLHttpRequest;
979
+ sleep?: (ms: number) => Promise<void>;
979
980
  };
980
981
  type MultipartUploadController = {
981
982
  start: () => Promise<void>;
package/dist/index.js CHANGED
@@ -5030,6 +5030,37 @@ function FileUploadMenu({
5030
5030
  }
5031
5031
 
5032
5032
  // src/components/upload/createMultipartUploadController.ts
5033
+ var PART_ATTEMPTS = 3;
5034
+ var PART_RETRY_BACKOFF_MS = [500, 1500];
5035
+ function defaultSleep(ms) {
5036
+ return new Promise((resolve) => {
5037
+ setTimeout(resolve, ms);
5038
+ });
5039
+ }
5040
+ function isAbortError(error) {
5041
+ return error instanceof DOMException && error.name === "AbortError";
5042
+ }
5043
+ function isMissingEtagError(error) {
5044
+ return error instanceof Error && error.message.includes("missing ETag");
5045
+ }
5046
+ function isRetryableStatus(status) {
5047
+ return status === 0 || status === 403 || status === 408 || status === 429 || status >= 500;
5048
+ }
5049
+ function putPartFailure(partNumber, status) {
5050
+ return new Error(`Upload failed (part ${partNumber}, status ${status})`);
5051
+ }
5052
+ function parsePutStatus(error) {
5053
+ if (!(error instanceof Error)) return null;
5054
+ const match = error.message.match(/status (\d+)\)$/);
5055
+ if (!match?.[1]) return null;
5056
+ return Number(match[1]);
5057
+ }
5058
+ function isRetryablePutError(error) {
5059
+ if (isAbortError(error) || isMissingEtagError(error)) return false;
5060
+ const status = parsePutStatus(error);
5061
+ if (status === null) return false;
5062
+ return isRetryableStatus(status);
5063
+ }
5033
5064
  function readEtag(xhr) {
5034
5065
  const raw = xhr.getResponseHeader("ETag") ?? xhr.getResponseHeader("etag") ?? "";
5035
5066
  const etag = raw.trim();
@@ -5053,10 +5084,10 @@ function putPart(input) {
5053
5084
  }
5054
5085
  return;
5055
5086
  }
5056
- reject(new Error(`Upload failed (${input.xhr.status})`));
5087
+ reject(putPartFailure(input.partNumber, input.xhr.status));
5057
5088
  };
5058
5089
  input.xhr.onerror = () => {
5059
- reject(new Error("Upload failed"));
5090
+ reject(putPartFailure(input.partNumber, 0));
5060
5091
  };
5061
5092
  input.xhr.onabort = () => {
5062
5093
  reject(new DOMException("The operation was aborted.", "AbortError"));
@@ -5066,8 +5097,9 @@ function putPart(input) {
5066
5097
  }
5067
5098
  function createMultipartUploadController(options) {
5068
5099
  const ranges = partRanges(options.file.size, options.partSize);
5069
- const completed = [];
5100
+ const sleep = options.sleep ?? defaultSleep;
5070
5101
  const state = { status: "idle" };
5102
+ const completed = [];
5071
5103
  let currentXhr = null;
5072
5104
  let run = null;
5073
5105
  let resumeIndex = 0;
@@ -5087,6 +5119,40 @@ function createMultipartUploadController(options) {
5087
5119
  progressFromBytes(completedBytes + currentPartLoaded, options.file.size)
5088
5120
  );
5089
5121
  }
5122
+ async function putPartWithRetry(partNumber, body) {
5123
+ let lastError;
5124
+ for (let attempt = 1; attempt <= PART_ATTEMPTS; attempt += 1) {
5125
+ if (state.status !== "uploading") {
5126
+ throw lastError ?? new DOMException("The operation was aborted.", "AbortError");
5127
+ }
5128
+ const url = await options.adapter.requestPartUrl(partNumber);
5129
+ if (state.status !== "uploading") {
5130
+ throw new DOMException("The operation was aborted.", "AbortError");
5131
+ }
5132
+ const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5133
+ currentXhr = xhr;
5134
+ try {
5135
+ return await putPart({
5136
+ url,
5137
+ body,
5138
+ xhr,
5139
+ partNumber,
5140
+ onProgress: (loaded) => emitProgress(loaded)
5141
+ });
5142
+ } catch (error) {
5143
+ lastError = error;
5144
+ if (currentXhr === xhr) currentXhr = null;
5145
+ if (isStopped() || !isRetryablePutError(error) || attempt === PART_ATTEMPTS) {
5146
+ throw error;
5147
+ }
5148
+ const delay = PART_RETRY_BACKOFF_MS[attempt - 1] ?? PART_RETRY_BACKOFF_MS[PART_RETRY_BACKOFF_MS.length - 1];
5149
+ await sleep(delay);
5150
+ } finally {
5151
+ if (currentXhr === xhr) currentXhr = null;
5152
+ }
5153
+ }
5154
+ throw lastError;
5155
+ }
5090
5156
  async function uploadFrom(startIndex) {
5091
5157
  state.status = "uploading";
5092
5158
  for (let index = startIndex; index < ranges.length; index += 1) {
@@ -5094,18 +5160,10 @@ function createMultipartUploadController(options) {
5094
5160
  resumeIndex = index;
5095
5161
  const range = ranges[index];
5096
5162
  if (!range) continue;
5097
- const url = await options.adapter.requestPartUrl(range.partNumber);
5098
- if (state.status !== "uploading") return;
5099
- const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5100
- currentXhr = xhr;
5101
5163
  const body = options.file.slice(range.start, range.end);
5102
5164
  try {
5103
- const etag = await putPart({
5104
- url,
5105
- body,
5106
- xhr,
5107
- onProgress: (loaded) => emitProgress(loaded)
5108
- });
5165
+ const etag = await putPartWithRetry(range.partNumber, body);
5166
+ if (state.status !== "uploading") return;
5109
5167
  completed.push({ partNumber: range.partNumber, etag });
5110
5168
  emitProgress(0);
5111
5169
  } catch (error) {
@@ -5113,8 +5171,6 @@ function createMultipartUploadController(options) {
5113
5171
  if (isStopped()) return;
5114
5172
  state.status = "error";
5115
5173
  throw error;
5116
- } finally {
5117
- if (currentXhr === xhr) currentXhr = null;
5118
5174
  }
5119
5175
  }
5120
5176
  if (state.status !== "uploading") return;