@livo-tv/blocks 1.6.1 → 1.8.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
@@ -4695,6 +4695,24 @@ function partRanges(fileSize, partSize) {
4695
4695
  };
4696
4696
  });
4697
4697
  }
4698
+ var BYTE_UNITS = ["B", "KB", "MB", "GB", "TB"];
4699
+ function formatUploadBytes(bytes) {
4700
+ if (!Number.isFinite(bytes) || bytes <= 0) {
4701
+ return "0 B";
4702
+ }
4703
+ let value = bytes;
4704
+ let unit = 0;
4705
+ while (value >= 1024 && unit < BYTE_UNITS.length - 1) {
4706
+ value /= 1024;
4707
+ unit += 1;
4708
+ }
4709
+ if (unit === 0 || value >= 10) {
4710
+ return `${Math.round(value)} ${BYTE_UNITS[unit]}`;
4711
+ }
4712
+ const tenths = Math.round(value * 10) / 10;
4713
+ const label = Number.isInteger(tenths) ? String(tenths) : tenths.toFixed(1);
4714
+ return `${label} ${BYTE_UNITS[unit]}`;
4715
+ }
4698
4716
  function progressFromBytes(loaded, total) {
4699
4717
  const safeTotal = Math.max(0, total);
4700
4718
  const safeLoaded = Math.min(Math.max(0, loaded), safeTotal);
@@ -4883,6 +4901,9 @@ function statusLabel(status, labels) {
4883
4901
  }
4884
4902
  }
4885
4903
  }
4904
+ function progressLabel(job) {
4905
+ return `${job.percent}% \xB7 ${formatUploadBytes(job.loaded)} / ${formatUploadBytes(job.total)}`;
4906
+ }
4886
4907
  function FileUploadTray({
4887
4908
  jobs,
4888
4909
  labels: labelOverrides,
@@ -4905,16 +4926,18 @@ function FileUploadTray({
4905
4926
  const canResume = job.status === "paused";
4906
4927
  const canCancel = job.status === "uploading" || job.status === "paused" || job.status === "error";
4907
4928
  return /* @__PURE__ */ jsxRuntime.jsxs("li", { className: "grid min-w-0 gap-1.5", children: [
4929
+ /* @__PURE__ */ jsxRuntime.jsx(
4930
+ "p",
4931
+ {
4932
+ className: "h-[3lh] min-w-0 line-clamp-3 text-sm leading-5 font-medium break-all",
4933
+ title: job.name,
4934
+ children: job.name
4935
+ }
4936
+ ),
4908
4937
  /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [
4909
- /* @__PURE__ */ jsxRuntime.jsx(
4910
- "p",
4911
- {
4912
- className: "min-w-0 flex-1 truncate text-sm font-medium",
4913
- title: job.name,
4914
- children: job.name
4915
- }
4916
- ),
4917
4938
  /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground shrink-0 text-xs", children: statusLabel(job.status, labels) }),
4939
+ /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-muted-foreground shrink-0 text-xs tabular-nums", children: progressLabel(job) }),
4940
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "min-w-0 flex-1" }),
4918
4941
  canPause ? /* @__PURE__ */ jsxRuntime.jsx(
4919
4942
  Button,
4920
4943
  {
@@ -5056,6 +5079,37 @@ function FileUploadMenu({
5056
5079
  }
5057
5080
 
5058
5081
  // src/components/upload/createMultipartUploadController.ts
5082
+ var PART_ATTEMPTS = 3;
5083
+ var PART_RETRY_BACKOFF_MS = [500, 1500];
5084
+ function defaultSleep(ms) {
5085
+ return new Promise((resolve) => {
5086
+ setTimeout(resolve, ms);
5087
+ });
5088
+ }
5089
+ function isAbortError(error) {
5090
+ return error instanceof DOMException && error.name === "AbortError";
5091
+ }
5092
+ function isMissingEtagError(error) {
5093
+ return error instanceof Error && error.message.includes("missing ETag");
5094
+ }
5095
+ function isRetryableStatus(status) {
5096
+ return status === 0 || status === 403 || status === 408 || status === 429 || status >= 500;
5097
+ }
5098
+ function putPartFailure(partNumber, status) {
5099
+ return new Error(`Upload failed (part ${partNumber}, status ${status})`);
5100
+ }
5101
+ function parsePutStatus(error) {
5102
+ if (!(error instanceof Error)) return null;
5103
+ const match = error.message.match(/status (\d+)\)$/);
5104
+ if (!match?.[1]) return null;
5105
+ return Number(match[1]);
5106
+ }
5107
+ function isRetryablePutError(error) {
5108
+ if (isAbortError(error) || isMissingEtagError(error)) return false;
5109
+ const status = parsePutStatus(error);
5110
+ if (status === null) return false;
5111
+ return isRetryableStatus(status);
5112
+ }
5059
5113
  function readEtag(xhr) {
5060
5114
  const raw = xhr.getResponseHeader("ETag") ?? xhr.getResponseHeader("etag") ?? "";
5061
5115
  const etag = raw.trim();
@@ -5079,10 +5133,10 @@ function putPart(input) {
5079
5133
  }
5080
5134
  return;
5081
5135
  }
5082
- reject(new Error(`Upload failed (${input.xhr.status})`));
5136
+ reject(putPartFailure(input.partNumber, input.xhr.status));
5083
5137
  };
5084
5138
  input.xhr.onerror = () => {
5085
- reject(new Error("Upload failed"));
5139
+ reject(putPartFailure(input.partNumber, 0));
5086
5140
  };
5087
5141
  input.xhr.onabort = () => {
5088
5142
  reject(new DOMException("The operation was aborted.", "AbortError"));
@@ -5092,11 +5146,21 @@ function putPart(input) {
5092
5146
  }
5093
5147
  function createMultipartUploadController(options) {
5094
5148
  const ranges = partRanges(options.file.size, options.partSize);
5095
- const completed = [];
5149
+ const sleep = options.sleep ?? defaultSleep;
5096
5150
  const state = { status: "idle" };
5151
+ const seeded = /* @__PURE__ */ new Map();
5152
+ for (const part of options.completedParts ?? []) {
5153
+ if (part.partNumber < 1 || part.partNumber > ranges.length) continue;
5154
+ seeded.set(part.partNumber, part);
5155
+ }
5156
+ const completed = [...seeded.values()].sort(
5157
+ (left, right) => left.partNumber - right.partNumber
5158
+ );
5159
+ const done = new Set(completed.map((part) => part.partNumber));
5097
5160
  let currentXhr = null;
5098
5161
  let run = null;
5099
- let resumeIndex = 0;
5162
+ let resumeIndex = ranges.findIndex((range) => !done.has(range.partNumber));
5163
+ if (resumeIndex < 0) resumeIndex = ranges.length;
5100
5164
  function currentStatus() {
5101
5165
  return state.status;
5102
5166
  }
@@ -5113,6 +5177,40 @@ function createMultipartUploadController(options) {
5113
5177
  progressFromBytes(completedBytes + currentPartLoaded, options.file.size)
5114
5178
  );
5115
5179
  }
5180
+ async function putPartWithRetry(partNumber, body) {
5181
+ let lastError;
5182
+ for (let attempt = 1; attempt <= PART_ATTEMPTS; attempt += 1) {
5183
+ if (state.status !== "uploading") {
5184
+ throw lastError ?? new DOMException("The operation was aborted.", "AbortError");
5185
+ }
5186
+ const url = await options.adapter.requestPartUrl(partNumber);
5187
+ if (state.status !== "uploading") {
5188
+ throw new DOMException("The operation was aborted.", "AbortError");
5189
+ }
5190
+ const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
5191
+ currentXhr = xhr;
5192
+ try {
5193
+ return await putPart({
5194
+ url,
5195
+ body,
5196
+ xhr,
5197
+ partNumber,
5198
+ onProgress: (loaded) => emitProgress(loaded)
5199
+ });
5200
+ } catch (error) {
5201
+ lastError = error;
5202
+ if (currentXhr === xhr) currentXhr = null;
5203
+ if (isStopped() || !isRetryablePutError(error) || attempt === PART_ATTEMPTS) {
5204
+ throw error;
5205
+ }
5206
+ const delay = PART_RETRY_BACKOFF_MS[attempt - 1] ?? PART_RETRY_BACKOFF_MS[PART_RETRY_BACKOFF_MS.length - 1];
5207
+ await sleep(delay);
5208
+ } finally {
5209
+ if (currentXhr === xhr) currentXhr = null;
5210
+ }
5211
+ }
5212
+ throw lastError;
5213
+ }
5116
5214
  async function uploadFrom(startIndex) {
5117
5215
  state.status = "uploading";
5118
5216
  for (let index = startIndex; index < ranges.length; index += 1) {
@@ -5120,27 +5218,19 @@ function createMultipartUploadController(options) {
5120
5218
  resumeIndex = index;
5121
5219
  const range = ranges[index];
5122
5220
  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;
5221
+ if (done.has(range.partNumber)) continue;
5127
5222
  const body = options.file.slice(range.start, range.end);
5128
5223
  try {
5129
- const etag = await putPart({
5130
- url,
5131
- body,
5132
- xhr,
5133
- onProgress: (loaded) => emitProgress(loaded)
5134
- });
5224
+ const etag = await putPartWithRetry(range.partNumber, body);
5225
+ if (state.status !== "uploading") return;
5135
5226
  completed.push({ partNumber: range.partNumber, etag });
5227
+ done.add(range.partNumber);
5136
5228
  emitProgress(0);
5137
5229
  } catch (error) {
5138
5230
  currentXhr = null;
5139
5231
  if (isStopped()) return;
5140
5232
  state.status = "error";
5141
5233
  throw error;
5142
- } finally {
5143
- if (currentXhr === xhr) currentXhr = null;
5144
5234
  }
5145
5235
  }
5146
5236
  if (state.status !== "uploading") return;
@@ -5151,7 +5241,8 @@ function createMultipartUploadController(options) {
5151
5241
  }
5152
5242
  async function start() {
5153
5243
  if (run) return run;
5154
- run = uploadFrom(0).catch((error) => {
5244
+ emitProgress(0);
5245
+ run = uploadFrom(resumeIndex).catch((error) => {
5155
5246
  if (isStopped()) return;
5156
5247
  state.status = "error";
5157
5248
  throw error;