@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 +116 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +116 -25
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -976,6 +976,8 @@ type CreateMultipartUploadControllerOptions = {
|
|
|
976
976
|
adapter: MultipartUploadAdapter;
|
|
977
977
|
onProgress?: (progress: UploadProgress) => void;
|
|
978
978
|
xhrFactory?: () => XMLHttpRequest;
|
|
979
|
+
sleep?: (ms: number) => Promise<void>;
|
|
980
|
+
completedParts?: MultipartUploadPart[];
|
|
979
981
|
};
|
|
980
982
|
type MultipartUploadController = {
|
|
981
983
|
start: () => Promise<void>;
|
package/dist/index.d.ts
CHANGED
|
@@ -976,6 +976,8 @@ type CreateMultipartUploadControllerOptions = {
|
|
|
976
976
|
adapter: MultipartUploadAdapter;
|
|
977
977
|
onProgress?: (progress: UploadProgress) => void;
|
|
978
978
|
xhrFactory?: () => XMLHttpRequest;
|
|
979
|
+
sleep?: (ms: number) => Promise<void>;
|
|
980
|
+
completedParts?: MultipartUploadPart[];
|
|
979
981
|
};
|
|
980
982
|
type MultipartUploadController = {
|
|
981
983
|
start: () => Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -4669,6 +4669,24 @@ function partRanges(fileSize, partSize) {
|
|
|
4669
4669
|
};
|
|
4670
4670
|
});
|
|
4671
4671
|
}
|
|
4672
|
+
var BYTE_UNITS = ["B", "KB", "MB", "GB", "TB"];
|
|
4673
|
+
function formatUploadBytes(bytes) {
|
|
4674
|
+
if (!Number.isFinite(bytes) || bytes <= 0) {
|
|
4675
|
+
return "0 B";
|
|
4676
|
+
}
|
|
4677
|
+
let value = bytes;
|
|
4678
|
+
let unit = 0;
|
|
4679
|
+
while (value >= 1024 && unit < BYTE_UNITS.length - 1) {
|
|
4680
|
+
value /= 1024;
|
|
4681
|
+
unit += 1;
|
|
4682
|
+
}
|
|
4683
|
+
if (unit === 0 || value >= 10) {
|
|
4684
|
+
return `${Math.round(value)} ${BYTE_UNITS[unit]}`;
|
|
4685
|
+
}
|
|
4686
|
+
const tenths = Math.round(value * 10) / 10;
|
|
4687
|
+
const label = Number.isInteger(tenths) ? String(tenths) : tenths.toFixed(1);
|
|
4688
|
+
return `${label} ${BYTE_UNITS[unit]}`;
|
|
4689
|
+
}
|
|
4672
4690
|
function progressFromBytes(loaded, total) {
|
|
4673
4691
|
const safeTotal = Math.max(0, total);
|
|
4674
4692
|
const safeLoaded = Math.min(Math.max(0, loaded), safeTotal);
|
|
@@ -4857,6 +4875,9 @@ function statusLabel(status, labels) {
|
|
|
4857
4875
|
}
|
|
4858
4876
|
}
|
|
4859
4877
|
}
|
|
4878
|
+
function progressLabel(job) {
|
|
4879
|
+
return `${job.percent}% \xB7 ${formatUploadBytes(job.loaded)} / ${formatUploadBytes(job.total)}`;
|
|
4880
|
+
}
|
|
4860
4881
|
function FileUploadTray({
|
|
4861
4882
|
jobs,
|
|
4862
4883
|
labels: labelOverrides,
|
|
@@ -4879,16 +4900,18 @@ function FileUploadTray({
|
|
|
4879
4900
|
const canResume = job.status === "paused";
|
|
4880
4901
|
const canCancel = job.status === "uploading" || job.status === "paused" || job.status === "error";
|
|
4881
4902
|
return /* @__PURE__ */ jsxs("li", { className: "grid min-w-0 gap-1.5", children: [
|
|
4903
|
+
/* @__PURE__ */ jsx(
|
|
4904
|
+
"p",
|
|
4905
|
+
{
|
|
4906
|
+
className: "h-[3lh] min-w-0 line-clamp-3 text-sm leading-5 font-medium break-all",
|
|
4907
|
+
title: job.name,
|
|
4908
|
+
children: job.name
|
|
4909
|
+
}
|
|
4910
|
+
),
|
|
4882
4911
|
/* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-center gap-2", children: [
|
|
4883
|
-
/* @__PURE__ */ jsx(
|
|
4884
|
-
"p",
|
|
4885
|
-
{
|
|
4886
|
-
className: "min-w-0 flex-1 truncate text-sm font-medium",
|
|
4887
|
-
title: job.name,
|
|
4888
|
-
children: job.name
|
|
4889
|
-
}
|
|
4890
|
-
),
|
|
4891
4912
|
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground shrink-0 text-xs", children: statusLabel(job.status, labels) }),
|
|
4913
|
+
/* @__PURE__ */ jsx("p", { className: "text-muted-foreground shrink-0 text-xs tabular-nums", children: progressLabel(job) }),
|
|
4914
|
+
/* @__PURE__ */ jsx("span", { className: "min-w-0 flex-1" }),
|
|
4892
4915
|
canPause ? /* @__PURE__ */ jsx(
|
|
4893
4916
|
Button,
|
|
4894
4917
|
{
|
|
@@ -5030,6 +5053,37 @@ function FileUploadMenu({
|
|
|
5030
5053
|
}
|
|
5031
5054
|
|
|
5032
5055
|
// src/components/upload/createMultipartUploadController.ts
|
|
5056
|
+
var PART_ATTEMPTS = 3;
|
|
5057
|
+
var PART_RETRY_BACKOFF_MS = [500, 1500];
|
|
5058
|
+
function defaultSleep(ms) {
|
|
5059
|
+
return new Promise((resolve) => {
|
|
5060
|
+
setTimeout(resolve, ms);
|
|
5061
|
+
});
|
|
5062
|
+
}
|
|
5063
|
+
function isAbortError(error) {
|
|
5064
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
5065
|
+
}
|
|
5066
|
+
function isMissingEtagError(error) {
|
|
5067
|
+
return error instanceof Error && error.message.includes("missing ETag");
|
|
5068
|
+
}
|
|
5069
|
+
function isRetryableStatus(status) {
|
|
5070
|
+
return status === 0 || status === 403 || status === 408 || status === 429 || status >= 500;
|
|
5071
|
+
}
|
|
5072
|
+
function putPartFailure(partNumber, status) {
|
|
5073
|
+
return new Error(`Upload failed (part ${partNumber}, status ${status})`);
|
|
5074
|
+
}
|
|
5075
|
+
function parsePutStatus(error) {
|
|
5076
|
+
if (!(error instanceof Error)) return null;
|
|
5077
|
+
const match = error.message.match(/status (\d+)\)$/);
|
|
5078
|
+
if (!match?.[1]) return null;
|
|
5079
|
+
return Number(match[1]);
|
|
5080
|
+
}
|
|
5081
|
+
function isRetryablePutError(error) {
|
|
5082
|
+
if (isAbortError(error) || isMissingEtagError(error)) return false;
|
|
5083
|
+
const status = parsePutStatus(error);
|
|
5084
|
+
if (status === null) return false;
|
|
5085
|
+
return isRetryableStatus(status);
|
|
5086
|
+
}
|
|
5033
5087
|
function readEtag(xhr) {
|
|
5034
5088
|
const raw = xhr.getResponseHeader("ETag") ?? xhr.getResponseHeader("etag") ?? "";
|
|
5035
5089
|
const etag = raw.trim();
|
|
@@ -5053,10 +5107,10 @@ function putPart(input) {
|
|
|
5053
5107
|
}
|
|
5054
5108
|
return;
|
|
5055
5109
|
}
|
|
5056
|
-
reject(
|
|
5110
|
+
reject(putPartFailure(input.partNumber, input.xhr.status));
|
|
5057
5111
|
};
|
|
5058
5112
|
input.xhr.onerror = () => {
|
|
5059
|
-
reject(
|
|
5113
|
+
reject(putPartFailure(input.partNumber, 0));
|
|
5060
5114
|
};
|
|
5061
5115
|
input.xhr.onabort = () => {
|
|
5062
5116
|
reject(new DOMException("The operation was aborted.", "AbortError"));
|
|
@@ -5066,11 +5120,21 @@ function putPart(input) {
|
|
|
5066
5120
|
}
|
|
5067
5121
|
function createMultipartUploadController(options) {
|
|
5068
5122
|
const ranges = partRanges(options.file.size, options.partSize);
|
|
5069
|
-
const
|
|
5123
|
+
const sleep = options.sleep ?? defaultSleep;
|
|
5070
5124
|
const state = { status: "idle" };
|
|
5125
|
+
const seeded = /* @__PURE__ */ new Map();
|
|
5126
|
+
for (const part of options.completedParts ?? []) {
|
|
5127
|
+
if (part.partNumber < 1 || part.partNumber > ranges.length) continue;
|
|
5128
|
+
seeded.set(part.partNumber, part);
|
|
5129
|
+
}
|
|
5130
|
+
const completed = [...seeded.values()].sort(
|
|
5131
|
+
(left, right) => left.partNumber - right.partNumber
|
|
5132
|
+
);
|
|
5133
|
+
const done = new Set(completed.map((part) => part.partNumber));
|
|
5071
5134
|
let currentXhr = null;
|
|
5072
5135
|
let run = null;
|
|
5073
|
-
let resumeIndex =
|
|
5136
|
+
let resumeIndex = ranges.findIndex((range) => !done.has(range.partNumber));
|
|
5137
|
+
if (resumeIndex < 0) resumeIndex = ranges.length;
|
|
5074
5138
|
function currentStatus() {
|
|
5075
5139
|
return state.status;
|
|
5076
5140
|
}
|
|
@@ -5087,6 +5151,40 @@ function createMultipartUploadController(options) {
|
|
|
5087
5151
|
progressFromBytes(completedBytes + currentPartLoaded, options.file.size)
|
|
5088
5152
|
);
|
|
5089
5153
|
}
|
|
5154
|
+
async function putPartWithRetry(partNumber, body) {
|
|
5155
|
+
let lastError;
|
|
5156
|
+
for (let attempt = 1; attempt <= PART_ATTEMPTS; attempt += 1) {
|
|
5157
|
+
if (state.status !== "uploading") {
|
|
5158
|
+
throw lastError ?? new DOMException("The operation was aborted.", "AbortError");
|
|
5159
|
+
}
|
|
5160
|
+
const url = await options.adapter.requestPartUrl(partNumber);
|
|
5161
|
+
if (state.status !== "uploading") {
|
|
5162
|
+
throw new DOMException("The operation was aborted.", "AbortError");
|
|
5163
|
+
}
|
|
5164
|
+
const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
|
|
5165
|
+
currentXhr = xhr;
|
|
5166
|
+
try {
|
|
5167
|
+
return await putPart({
|
|
5168
|
+
url,
|
|
5169
|
+
body,
|
|
5170
|
+
xhr,
|
|
5171
|
+
partNumber,
|
|
5172
|
+
onProgress: (loaded) => emitProgress(loaded)
|
|
5173
|
+
});
|
|
5174
|
+
} catch (error) {
|
|
5175
|
+
lastError = error;
|
|
5176
|
+
if (currentXhr === xhr) currentXhr = null;
|
|
5177
|
+
if (isStopped() || !isRetryablePutError(error) || attempt === PART_ATTEMPTS) {
|
|
5178
|
+
throw error;
|
|
5179
|
+
}
|
|
5180
|
+
const delay = PART_RETRY_BACKOFF_MS[attempt - 1] ?? PART_RETRY_BACKOFF_MS[PART_RETRY_BACKOFF_MS.length - 1];
|
|
5181
|
+
await sleep(delay);
|
|
5182
|
+
} finally {
|
|
5183
|
+
if (currentXhr === xhr) currentXhr = null;
|
|
5184
|
+
}
|
|
5185
|
+
}
|
|
5186
|
+
throw lastError;
|
|
5187
|
+
}
|
|
5090
5188
|
async function uploadFrom(startIndex) {
|
|
5091
5189
|
state.status = "uploading";
|
|
5092
5190
|
for (let index = startIndex; index < ranges.length; index += 1) {
|
|
@@ -5094,27 +5192,19 @@ function createMultipartUploadController(options) {
|
|
|
5094
5192
|
resumeIndex = index;
|
|
5095
5193
|
const range = ranges[index];
|
|
5096
5194
|
if (!range) continue;
|
|
5097
|
-
|
|
5098
|
-
if (state.status !== "uploading") return;
|
|
5099
|
-
const xhr = options.xhrFactory ? options.xhrFactory() : new XMLHttpRequest();
|
|
5100
|
-
currentXhr = xhr;
|
|
5195
|
+
if (done.has(range.partNumber)) continue;
|
|
5101
5196
|
const body = options.file.slice(range.start, range.end);
|
|
5102
5197
|
try {
|
|
5103
|
-
const etag = await
|
|
5104
|
-
|
|
5105
|
-
body,
|
|
5106
|
-
xhr,
|
|
5107
|
-
onProgress: (loaded) => emitProgress(loaded)
|
|
5108
|
-
});
|
|
5198
|
+
const etag = await putPartWithRetry(range.partNumber, body);
|
|
5199
|
+
if (state.status !== "uploading") return;
|
|
5109
5200
|
completed.push({ partNumber: range.partNumber, etag });
|
|
5201
|
+
done.add(range.partNumber);
|
|
5110
5202
|
emitProgress(0);
|
|
5111
5203
|
} catch (error) {
|
|
5112
5204
|
currentXhr = null;
|
|
5113
5205
|
if (isStopped()) return;
|
|
5114
5206
|
state.status = "error";
|
|
5115
5207
|
throw error;
|
|
5116
|
-
} finally {
|
|
5117
|
-
if (currentXhr === xhr) currentXhr = null;
|
|
5118
5208
|
}
|
|
5119
5209
|
}
|
|
5120
5210
|
if (state.status !== "uploading") return;
|
|
@@ -5125,7 +5215,8 @@ function createMultipartUploadController(options) {
|
|
|
5125
5215
|
}
|
|
5126
5216
|
async function start() {
|
|
5127
5217
|
if (run) return run;
|
|
5128
|
-
|
|
5218
|
+
emitProgress(0);
|
|
5219
|
+
run = uploadFrom(resumeIndex).catch((error) => {
|
|
5129
5220
|
if (isStopped()) return;
|
|
5130
5221
|
state.status = "error";
|
|
5131
5222
|
throw error;
|