cloudrop 0.0.0-stage → 0.1.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/CHANGELOG.md +56 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/dist/adapters/drizzle.d.ts +204 -0
- package/dist/adapters/drizzle.js +216 -0
- package/dist/adapters/drizzle.js.map +1 -0
- package/dist/adapters/prisma.d.ts +81 -0
- package/dist/adapters/prisma.js +32 -0
- package/dist/adapters/prisma.js.map +1 -0
- package/dist/adapters/sql.d.ts +98 -0
- package/dist/adapters/sql.js +70 -0
- package/dist/adapters/sql.js.map +1 -0
- package/dist/client-Dj6ON4MN.d.ts +214 -0
- package/dist/client-Y1zUEbCj.js +1561 -0
- package/dist/client-Y1zUEbCj.js.map +1 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +3 -0
- package/dist/d1-BY2c5bcL.js +16 -0
- package/dist/d1-BY2c5bcL.js.map +1 -0
- package/dist/errors-CUxMmFAe.js +40 -0
- package/dist/errors-CUxMmFAe.js.map +1 -0
- package/dist/executor-CEA7WV2W.d.ts +27 -0
- package/dist/index.d.ts +421 -0
- package/dist/index.js +3007 -0
- package/dist/index.js.map +1 -0
- package/dist/input-DTC476_I.js +13 -0
- package/dist/input-DTC476_I.js.map +1 -0
- package/dist/next.d.ts +9 -0
- package/dist/next.js +14 -0
- package/dist/next.js.map +1 -0
- package/dist/node.d.ts +32 -0
- package/dist/node.js +100 -0
- package/dist/node.js.map +1 -0
- package/dist/part-size-CvMoobfC.js +9 -0
- package/dist/part-size-CvMoobfC.js.map +1 -0
- package/dist/react.d.ts +123 -0
- package/dist/react.js +355 -0
- package/dist/react.js.map +1 -0
- package/dist/route-CspI1Ypt.js +72 -0
- package/dist/route-CspI1Ypt.js.map +1 -0
- package/dist/store-BfCJejGh.d.ts +382 -0
- package/dist/tables-DZnw2sPK.d.ts +11 -0
- package/dist/transactional-DnvnGsVH.js +672 -0
- package/dist/transactional-DnvnGsVH.js.map +1 -0
- package/dist/types-BZUOaz_B.js +5 -0
- package/dist/types-BZUOaz_B.js.map +1 -0
- package/dist/types-BrAx3dA4.d.ts +185 -0
- package/package.json +110 -4
|
@@ -0,0 +1,1561 @@
|
|
|
1
|
+
import { i as ok, n as configError, r as fail, t as CloudropError } from "./errors-CUxMmFAe.js";
|
|
2
|
+
import { n as isPlainObject, t as invalidInput } from "./input-DTC476_I.js";
|
|
3
|
+
import { n as sizeOfPart, t as countParts } from "./part-size-CvMoobfC.js";
|
|
4
|
+
// src/client/arguments.ts
|
|
5
|
+
const isList = (value) => typeof value === "object" && value !== null && "length" in value && typeof value.length === "number";
|
|
6
|
+
const refuse = (message) => fail(invalidInput(message));
|
|
7
|
+
function encodesAsJson(value) {
|
|
8
|
+
try {
|
|
9
|
+
JSON.stringify(value);
|
|
10
|
+
return true;
|
|
11
|
+
} catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function readFiles(files) {
|
|
16
|
+
if (files instanceof File) return ok([files]);
|
|
17
|
+
if (!isList(files)) return refuse("Pass the file as a File, such as one from <input type=\"file\">.");
|
|
18
|
+
const list = Array.from(files);
|
|
19
|
+
if (!list.every((file) => file instanceof File)) return refuse("Pass the files as File objects, such as input.files from <input type=\"file\" multiple>.");
|
|
20
|
+
return list.length === 0 ? refuse("Pass at least one file to upload.") : ok(list);
|
|
21
|
+
}
|
|
22
|
+
function readArguments(route, files, options) {
|
|
23
|
+
if (typeof route !== "string" || route === "") return refuse("Name the upload route as text, such as upload(\"avatar\", file).");
|
|
24
|
+
const read = readFiles(files);
|
|
25
|
+
if (read.error) return read;
|
|
26
|
+
if (options !== void 0 && !isPlainObject(options)) return refuse("Pass the upload options as an object, such as { metadata, signal }.");
|
|
27
|
+
const { metadata, signal = new AbortController().signal } = options ?? {};
|
|
28
|
+
if (!(signal instanceof AbortSignal)) return refuse("options.signal must be an AbortSignal, such as new AbortController().signal.");
|
|
29
|
+
if (!encodesAsJson(metadata)) return refuse("options.metadata must be JSON data, such as objects, strings and numbers: it holds a value JSON cannot encode, such as a BigInt or a circular reference.");
|
|
30
|
+
return ok({
|
|
31
|
+
route,
|
|
32
|
+
files: read.data,
|
|
33
|
+
metadata,
|
|
34
|
+
signal
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
// src/client/failures.ts
|
|
38
|
+
const NEEDS_NEW_TICKET = /* @__PURE__ */ new Set([
|
|
39
|
+
"UPLOAD_EXPIRED",
|
|
40
|
+
"INVALID_TOKEN",
|
|
41
|
+
"UPLOAD_REJECTED",
|
|
42
|
+
"VERIFICATION_FAILED",
|
|
43
|
+
"NO_SUCH_UPLOAD"
|
|
44
|
+
]);
|
|
45
|
+
const withFile = (error, { id, file }) => new CloudropError(error.code, error.message, {
|
|
46
|
+
...error,
|
|
47
|
+
file: {
|
|
48
|
+
id,
|
|
49
|
+
name: file.name
|
|
50
|
+
},
|
|
51
|
+
cause: error
|
|
52
|
+
});
|
|
53
|
+
const aborted = () => new CloudropError("ABORTED", "The upload was cancelled. Start it again to upload the file.");
|
|
54
|
+
const uploadInUse = () => new CloudropError("UPLOAD_IN_USE", "This upload is being sent in this browser, in this tab or another. Finish or cancel it there, then try again.");
|
|
55
|
+
const notResumable = () => new CloudropError("NOT_FOUND", "This upload can no longer be resumed: it finished, was cancelled or expired. Start it again.");
|
|
56
|
+
const notSameFile = () => new CloudropError("INVALID_INPUT", "This is not the file the upload started with. Pick the same file again to resume it.");
|
|
57
|
+
const notListed = () => new CloudropError("NOT_FOUND", "This upload is no longer in this browser's list: it finished, or was cancelled or discarded. Read the list again.");
|
|
58
|
+
const lockRefused = () => new CloudropError("STORAGE_ERROR", "The browser did not let this page lock the upload, so it was left as it was. Reload the page, then try again.");
|
|
59
|
+
const storageUnavailable = () => new CloudropError("STORAGE_ERROR", "The browser's storage for unfinished uploads could not be read or written. Uploads still work; resuming after a reload may not.");
|
|
60
|
+
const serverUnreachable = () => new CloudropError("NETWORK_ERROR", "The upload server could not be reached. Check the connection, then try again.", { retryable: true });
|
|
61
|
+
const bucketUnreachable = () => new CloudropError("NETWORK_ERROR", "The file could not reach the storage service, or the storage service refused it. Try again, and contact the site owner if it keeps failing.", { retryable: true });
|
|
62
|
+
const serverBusy = () => new CloudropError("RATE_LIMITED", "The upload server is busy. Try again in a moment.", { retryable: true });
|
|
63
|
+
const unreadableAnswer = (status) => new CloudropError("SERVER_ERROR", `The upload server sent an answer this client cannot read (HTTP ${status}). Check that basePath points at the cloudrop handler and that the server runs the same cloudrop version.`, { retryable: status >= 500 });
|
|
64
|
+
const signatureMismatch = () => new CloudropError("UPLOAD_REJECTED", "The storage service refused the file because its bytes do not match what the server signed. Pick the file again and start a new upload.");
|
|
65
|
+
const linkRefused = () => new CloudropError("UPLOAD_REJECTED", "The storage service refused the upload link, also after it was renewed. Check the server's clock and the storage credentials' permissions, then start the upload again.");
|
|
66
|
+
const uploadRejected = (status) => new CloudropError("UPLOAD_REJECTED", `The storage service refused the file (HTTP ${status}). Start the upload again, and contact the site owner if it keeps failing.`);
|
|
67
|
+
const storageBusy = () => new CloudropError("RATE_LIMITED", "The storage service is busy. Try again in a moment.", { retryable: true });
|
|
68
|
+
const storageDown = () => new CloudropError("STORAGE_ERROR", "The storage service failed. Try again in a moment.", { retryable: true });
|
|
69
|
+
// src/client/locks.ts
|
|
70
|
+
function lockUpload(id) {
|
|
71
|
+
let release;
|
|
72
|
+
const until = new Promise((resolve) => {
|
|
73
|
+
release = resolve;
|
|
74
|
+
});
|
|
75
|
+
const locks = globalThis.navigator?.locks;
|
|
76
|
+
return {
|
|
77
|
+
taken: new Promise((resolve) => {
|
|
78
|
+
if (!locks) return resolve(void 0);
|
|
79
|
+
locks.request(`cloudrop:upload:${id}`, { ifAvailable: true }, (lock) => {
|
|
80
|
+
resolve(lock !== null);
|
|
81
|
+
return lock && until;
|
|
82
|
+
}).catch(() => resolve(void 0));
|
|
83
|
+
}),
|
|
84
|
+
release
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
// src/client/records.ts
|
|
88
|
+
const STORE = "uploads";
|
|
89
|
+
const EXPIRY_MARGIN_MS = 6e4;
|
|
90
|
+
const expiresSoon$1 = (expiresAt, now) => !(Date.parse(expiresAt) - now >= EXPIRY_MARGIN_MS);
|
|
91
|
+
const noRecords = {
|
|
92
|
+
enabled: false,
|
|
93
|
+
save: () => void 0,
|
|
94
|
+
remove: async () => ok(void 0),
|
|
95
|
+
list: async () => ok([]),
|
|
96
|
+
clear: async () => ok(0)
|
|
97
|
+
};
|
|
98
|
+
function open(factory) {
|
|
99
|
+
return new Promise((resolve, reject) => {
|
|
100
|
+
const request = factory.open("cloudrop", 1);
|
|
101
|
+
request.onupgradeneeded = () => request.result.createObjectStore(STORE, { keyPath: "id" });
|
|
102
|
+
request.onsuccess = () => resolve(request.result);
|
|
103
|
+
request.onerror = () => reject(request.error);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const SHAPE = {
|
|
107
|
+
id: "string",
|
|
108
|
+
namespace: "string",
|
|
109
|
+
route: "string",
|
|
110
|
+
name: "string",
|
|
111
|
+
size: "number",
|
|
112
|
+
type: "string",
|
|
113
|
+
lastModified: "number",
|
|
114
|
+
fingerprint: "string",
|
|
115
|
+
token: "string",
|
|
116
|
+
key: "string",
|
|
117
|
+
partSize: "number",
|
|
118
|
+
partCount: "number",
|
|
119
|
+
expiresAt: "string",
|
|
120
|
+
savedAt: "number"
|
|
121
|
+
};
|
|
122
|
+
const isRecord = (value) => typeof value === "object" && value !== null && Object.entries(SHAPE).every(([field, type]) => typeof value[field] === type);
|
|
123
|
+
function transact(database, work) {
|
|
124
|
+
return new Promise((resolve, reject) => {
|
|
125
|
+
const transaction = database.transaction(STORE, "readwrite");
|
|
126
|
+
let value;
|
|
127
|
+
transaction.oncomplete = () => resolve(value);
|
|
128
|
+
transaction.onabort = () => reject(transaction.error);
|
|
129
|
+
work(transaction.objectStore(STORE), (settled) => {
|
|
130
|
+
value = settled;
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
const onResult = (request, use) => {
|
|
135
|
+
request.onsuccess = () => use(request.result);
|
|
136
|
+
};
|
|
137
|
+
function createRecords(enabled, now = () => Date.now()) {
|
|
138
|
+
const factory = enabled && globalThis.navigator?.locks ? globalThis.indexedDB : void 0;
|
|
139
|
+
if (factory == null) return noRecords;
|
|
140
|
+
let database;
|
|
141
|
+
const connect = () => {
|
|
142
|
+
const drop = () => {
|
|
143
|
+
if (database === opening) database = void 0;
|
|
144
|
+
};
|
|
145
|
+
const opening = open(factory).then((connection) => {
|
|
146
|
+
connection.onclose = drop;
|
|
147
|
+
connection.onversionchange = () => {
|
|
148
|
+
connection.close();
|
|
149
|
+
drop();
|
|
150
|
+
};
|
|
151
|
+
return connection;
|
|
152
|
+
});
|
|
153
|
+
opening.catch(drop);
|
|
154
|
+
return opening;
|
|
155
|
+
};
|
|
156
|
+
let queue = Promise.resolve();
|
|
157
|
+
const enqueue = (work) => {
|
|
158
|
+
const result = queue.then(async () => {
|
|
159
|
+
const ready = await work;
|
|
160
|
+
database ??= connect();
|
|
161
|
+
const connection = await database;
|
|
162
|
+
try {
|
|
163
|
+
return ok(await transact(connection, ready));
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error instanceof DOMException && error.name === "InvalidStateError") database = void 0;
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}).catch(() => fail(storageUnavailable()));
|
|
169
|
+
queue = result;
|
|
170
|
+
return result;
|
|
171
|
+
};
|
|
172
|
+
const usable = (store, id, value) => {
|
|
173
|
+
if (isRecord(value) && value.partCount <= 1e4 && value.partCount === countParts(value.size, value.partSize) && !expiresSoon$1(value.expiresAt, now())) return true;
|
|
174
|
+
if (value !== void 0) store.delete(id);
|
|
175
|
+
return false;
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
enabled: true,
|
|
179
|
+
save(pending) {
|
|
180
|
+
const work = Promise.resolve(pending).then((record) => (store) => {
|
|
181
|
+
store.put(record);
|
|
182
|
+
onResult(store.getAll(), (all) => {
|
|
183
|
+
for (const stale of all) usable(store, stale.id, stale);
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
work.catch(() => void 0);
|
|
187
|
+
enqueue(work);
|
|
188
|
+
},
|
|
189
|
+
remove: (id) => enqueue((store) => store.delete(id)),
|
|
190
|
+
list: (namespace) => enqueue((store, settle) => onResult(store.getAll(), (all) => {
|
|
191
|
+
settle(all.filter((record) => usable(store, record.id, record) && record.namespace === namespace).sort((a, b) => b.savedAt - a.savedAt));
|
|
192
|
+
})),
|
|
193
|
+
clear: (namespace) => enqueue((store, settle) => onResult(store.getAll(), (all) => {
|
|
194
|
+
const doomed = namespace === null ? all : all.filter((record) => record.namespace === namespace);
|
|
195
|
+
for (const record of doomed) store.delete(record.id);
|
|
196
|
+
settle(doomed.length);
|
|
197
|
+
}))
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
const BASE_DELAY_MS = 500;
|
|
201
|
+
const MAX_DELAY_MS = 2e4;
|
|
202
|
+
function retryDelay(failedAttempts, retryAfterMs = 0) {
|
|
203
|
+
const ceiling = Math.min(MAX_DELAY_MS, BASE_DELAY_MS * 2 ** (failedAttempts - 1));
|
|
204
|
+
return Math.min(MAX_DELAY_MS, Math.max(retryAfterMs, Math.random() * ceiling));
|
|
205
|
+
}
|
|
206
|
+
function parseRetryAfter(value, now = Date.now()) {
|
|
207
|
+
if (value === null) return 0;
|
|
208
|
+
const waitMs = /^\d+$/.test(value.trim()) ? Number(value) * 1e3 : Date.parse(value) - now;
|
|
209
|
+
return Number.isFinite(waitMs) ? Math.max(waitMs, 0) : 0;
|
|
210
|
+
}
|
|
211
|
+
const retryAfters = /* @__PURE__ */ new WeakMap();
|
|
212
|
+
const retryAfterOf = (error) => retryAfters.get(error) ?? 0;
|
|
213
|
+
function waitAtLeast(error, retryAfterMs) {
|
|
214
|
+
retryAfters.set(error, retryAfterMs);
|
|
215
|
+
return error;
|
|
216
|
+
}
|
|
217
|
+
function pause(ms, signal) {
|
|
218
|
+
return new Promise((resolve) => {
|
|
219
|
+
if (signal.aborted) return resolve();
|
|
220
|
+
const done = () => {
|
|
221
|
+
clearTimeout(timer);
|
|
222
|
+
signal.removeEventListener("abort", done);
|
|
223
|
+
resolve();
|
|
224
|
+
};
|
|
225
|
+
const timer = setTimeout(done, ms);
|
|
226
|
+
signal.addEventListener("abort", done, { once: true });
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
async function withRetry(run, signal, first = 1) {
|
|
230
|
+
for (let attempt = first;; attempt++) {
|
|
231
|
+
if (signal.aborted) return fail(aborted());
|
|
232
|
+
const result = await run(attempt);
|
|
233
|
+
const { error } = result;
|
|
234
|
+
if (!error?.retryable || attempt === 6) return result;
|
|
235
|
+
await pause(retryDelay(attempt, retryAfterOf(error)), signal);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// src/client/parts.ts
|
|
239
|
+
const PARTS_IN_FLIGHT = 4;
|
|
240
|
+
const RENEW_MARGIN_MS = 6e4;
|
|
241
|
+
const expiresSoon = (target) => Date.parse(target.expiresAt) - Date.now() < RENEW_MARGIN_MS;
|
|
242
|
+
function signFailure(error) {
|
|
243
|
+
if (error.code === "ABORTED") return {
|
|
244
|
+
kind: "aborted",
|
|
245
|
+
error
|
|
246
|
+
};
|
|
247
|
+
return error.retryable ? {
|
|
248
|
+
kind: "retry",
|
|
249
|
+
error,
|
|
250
|
+
retryAfterMs: retryAfterOf(error)
|
|
251
|
+
} : {
|
|
252
|
+
kind: "rejected",
|
|
253
|
+
error
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function nextStep(outcome, attempt, renewed) {
|
|
257
|
+
if (outcome.kind === "stored") return { kind: "done" };
|
|
258
|
+
if (!("retryAfterMs" in outcome)) return {
|
|
259
|
+
kind: "fail",
|
|
260
|
+
error: outcome.error
|
|
261
|
+
};
|
|
262
|
+
if (attempt >= 6 || outcome.kind === "expired" && renewed) return {
|
|
263
|
+
kind: "fail",
|
|
264
|
+
error: outcome.error
|
|
265
|
+
};
|
|
266
|
+
const renew = (outcome.kind === "opaque" || outcome.kind === "expired") && !renewed;
|
|
267
|
+
return {
|
|
268
|
+
kind: "again",
|
|
269
|
+
renew,
|
|
270
|
+
waitMs: renew && outcome.kind === "expired" ? 0 : retryDelay(attempt, outcome.retryAfterMs)
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
function signAhead(run, partNumber) {
|
|
274
|
+
if (run.batch) return run.batch;
|
|
275
|
+
const unsigned = run.partNumbers.filter((n) => n !== partNumber && !run.sender.urls.has(n));
|
|
276
|
+
const call = run.sender.sign([partNumber, ...unsigned].slice(0, 20)).then((signed) => {
|
|
277
|
+
run.batch = null;
|
|
278
|
+
if (signed.error) return signed.error;
|
|
279
|
+
for (const part of signed.data) run.sender.urls.set(part.partNumber, part);
|
|
280
|
+
return null;
|
|
281
|
+
});
|
|
282
|
+
run.batch = call;
|
|
283
|
+
return call;
|
|
284
|
+
}
|
|
285
|
+
async function urlFor(run, partNumber, renew) {
|
|
286
|
+
let known = run.sender.urls.get(partNumber);
|
|
287
|
+
for (let pass = 1; known === void 0; pass++) {
|
|
288
|
+
if (pass > 2) return fail(unreadableAnswer(200));
|
|
289
|
+
const error = await signAhead(run, partNumber);
|
|
290
|
+
if (error) return fail(error);
|
|
291
|
+
known = run.sender.urls.get(partNumber);
|
|
292
|
+
}
|
|
293
|
+
if (!renew && !expiresSoon(known)) return ok(known);
|
|
294
|
+
const renewed = await run.sender.sign([partNumber]);
|
|
295
|
+
if (renewed.error) return renewed;
|
|
296
|
+
const [part] = renewed.data;
|
|
297
|
+
if (part === void 0) return fail(unreadableAnswer(200));
|
|
298
|
+
run.sender.urls.set(partNumber, part);
|
|
299
|
+
return ok(part);
|
|
300
|
+
}
|
|
301
|
+
const stopped = (run) => run.sender.signal.aborted ? aborted() : run.failure;
|
|
302
|
+
async function sendPart(run, partNumber) {
|
|
303
|
+
let renew = false;
|
|
304
|
+
let renewed = false;
|
|
305
|
+
for (let attempt = 1;; attempt++) {
|
|
306
|
+
if (stopped(run)) return stopped(run);
|
|
307
|
+
const target = await urlFor(run, partNumber, renew);
|
|
308
|
+
if (stopped(run)) return stopped(run);
|
|
309
|
+
const step = nextStep(target.error ? signFailure(target.error) : await run.sender.put(target.data, partNumber, run.stop.signal), attempt, renewed);
|
|
310
|
+
if (step.kind === "done") return null;
|
|
311
|
+
if (step.kind === "fail") return step.error;
|
|
312
|
+
renew = step.renew || renew && target.error !== null;
|
|
313
|
+
renewed ||= step.renew;
|
|
314
|
+
await pause(step.waitMs, run.stop.signal);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
async function sendParts(sender, partNumbers) {
|
|
318
|
+
const run = {
|
|
319
|
+
sender,
|
|
320
|
+
partNumbers,
|
|
321
|
+
batch: null,
|
|
322
|
+
failure: null,
|
|
323
|
+
stop: new AbortController()
|
|
324
|
+
};
|
|
325
|
+
const cancel = () => run.stop.abort();
|
|
326
|
+
sender.signal.addEventListener("abort", cancel, { once: true });
|
|
327
|
+
const queue = [...partNumbers];
|
|
328
|
+
const worker = async () => {
|
|
329
|
+
for (let next = queue.shift(); next !== void 0 && run.failure === null; next = queue.shift()) {
|
|
330
|
+
const error = await sendPart(run, next);
|
|
331
|
+
run.failure ??= error;
|
|
332
|
+
if (run.failure) run.stop.abort();
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
await Promise.all(Array.from({ length: Math.min(PARTS_IN_FLIGHT, queue.length) }, worker));
|
|
336
|
+
sender.signal.removeEventListener("abort", cancel);
|
|
337
|
+
return run.failure ? fail(run.failure) : ok(void 0);
|
|
338
|
+
}
|
|
339
|
+
// src/client/upload.ts
|
|
340
|
+
const REPAIR_ROUNDS = 2;
|
|
341
|
+
function claim({ claims }, id) {
|
|
342
|
+
const made = { id };
|
|
343
|
+
claims.add(made);
|
|
344
|
+
return () => claims.delete(made);
|
|
345
|
+
}
|
|
346
|
+
function bodyOf(ticket, file, partNumber) {
|
|
347
|
+
if (ticket.kind === "single") return file;
|
|
348
|
+
const start = (partNumber - 1) * ticket.partSize;
|
|
349
|
+
return file.slice(start, start + sizeOfPart(file.size, ticket.partSize, partNumber));
|
|
350
|
+
}
|
|
351
|
+
function newUpload(ticket, entry, unlock, probe = false) {
|
|
352
|
+
const signed = ticket.kind === "single" ? [{
|
|
353
|
+
partNumber: 1,
|
|
354
|
+
...ticket.put
|
|
355
|
+
}] : ticket.parts;
|
|
356
|
+
return {
|
|
357
|
+
ticket,
|
|
358
|
+
entry,
|
|
359
|
+
mayBeComplete: false,
|
|
360
|
+
repairs: 0,
|
|
361
|
+
dropped: false,
|
|
362
|
+
unlock,
|
|
363
|
+
probe,
|
|
364
|
+
urls: new Map(signed.map((part) => [part.partNumber, part]))
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function moveTo(entry, status) {
|
|
368
|
+
if (status === "uploading") entry.meter.restart(Date.now());
|
|
369
|
+
entry.status = status;
|
|
370
|
+
entry.changed(true);
|
|
371
|
+
}
|
|
372
|
+
function senderFor(context, { ticket, entry, urls }, signal) {
|
|
373
|
+
const { file, rank } = entry;
|
|
374
|
+
const { pool } = context;
|
|
375
|
+
const put = async (target, partNumber, stop) => {
|
|
376
|
+
const place = context.order === "smallest-first" ? [
|
|
377
|
+
file.size,
|
|
378
|
+
rank,
|
|
379
|
+
partNumber
|
|
380
|
+
] : [rank, partNumber];
|
|
381
|
+
const release = await pool.acquire(place, stop);
|
|
382
|
+
try {
|
|
383
|
+
if (release === null || stop.aborted) return {
|
|
384
|
+
kind: "aborted",
|
|
385
|
+
error: aborted()
|
|
386
|
+
};
|
|
387
|
+
if (entry.status === "queued") moveTo(entry, "uploading");
|
|
388
|
+
const body = bodyOf(ticket, file, partNumber);
|
|
389
|
+
const outcome = await context.put(target, body, signal, (sent) => {
|
|
390
|
+
const added = entry.progress.sent(partNumber, sent, body.size);
|
|
391
|
+
if (added === 0) return;
|
|
392
|
+
pool.sent(added);
|
|
393
|
+
entry.meter.add(added, Date.now());
|
|
394
|
+
entry.changed(false);
|
|
395
|
+
});
|
|
396
|
+
if (outcome.kind === "stored") entry.landed.add(partNumber);
|
|
397
|
+
if (outcome.kind === "opaque" || outcome.kind === "retry" && outcome.error.code === "RATE_LIMITED") pool.slowDown();
|
|
398
|
+
return outcome;
|
|
399
|
+
} finally {
|
|
400
|
+
release?.();
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
if (ticket.kind === "multipart") return {
|
|
404
|
+
signal,
|
|
405
|
+
urls,
|
|
406
|
+
put,
|
|
407
|
+
sign: (parts) => context.endpoints.signParts(ticket.token, parts, signal)
|
|
408
|
+
};
|
|
409
|
+
const sign = async () => {
|
|
410
|
+
const renewed = await context.endpoints.signSingle(ticket.token, signal);
|
|
411
|
+
return renewed.error ? renewed : ok([{
|
|
412
|
+
partNumber: 1,
|
|
413
|
+
...renewed.data
|
|
414
|
+
}]);
|
|
415
|
+
};
|
|
416
|
+
return {
|
|
417
|
+
signal,
|
|
418
|
+
urls,
|
|
419
|
+
put,
|
|
420
|
+
sign
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function probed(upload, done, missing) {
|
|
424
|
+
if (done.error && !missing) return;
|
|
425
|
+
const { entry, ticket } = upload;
|
|
426
|
+
upload.probe = false;
|
|
427
|
+
const { partSize } = ticket;
|
|
428
|
+
const lacking = new Set(missing);
|
|
429
|
+
for (const part of entry.landed) {
|
|
430
|
+
const size = sizeOfPart(entry.file.size, partSize, part);
|
|
431
|
+
if (!lacking.has(part)) entry.progress.sent(part, size, size);
|
|
432
|
+
}
|
|
433
|
+
if (missing) upload.repairs--;
|
|
434
|
+
}
|
|
435
|
+
async function sendAndComplete(context, upload, signal) {
|
|
436
|
+
const { ticket, entry } = upload;
|
|
437
|
+
const sender = senderFor(context, upload, signal);
|
|
438
|
+
const partNumbers = Array.from({ length: ticket.kind === "single" ? 1 : ticket.partCount }, (_, index) => index + 1);
|
|
439
|
+
const complete = async () => {
|
|
440
|
+
const done = await context.endpoints.complete(ticket.token);
|
|
441
|
+
const code = done.error?.code;
|
|
442
|
+
if (code === "PARTS_MISSING" || code === "NO_SUCH_UPLOAD") upload.mayBeComplete = false;
|
|
443
|
+
else upload.mayBeComplete ||= code === "NETWORK_ERROR" || code === "SERVER_ERROR";
|
|
444
|
+
return done;
|
|
445
|
+
};
|
|
446
|
+
for (;;) {
|
|
447
|
+
const sent = await sendParts(sender, partNumbers.filter((partNumber) => !entry.landed.has(partNumber)));
|
|
448
|
+
if (sent.error) return fail(sent.error);
|
|
449
|
+
if (signal.aborted) return fail(aborted());
|
|
450
|
+
moveTo(entry, "completing");
|
|
451
|
+
const done = await withRetry(complete, entry.cancel.signal);
|
|
452
|
+
const missing = done.error?.code === "PARTS_MISSING" ? done.error.parts : void 0;
|
|
453
|
+
if (upload.probe) probed(upload, done, missing);
|
|
454
|
+
if (!missing?.length || upload.repairs === REPAIR_ROUNDS || entry.cancel.signal.aborted) return done;
|
|
455
|
+
upload.repairs++;
|
|
456
|
+
for (const partNumber of missing) entry.landed.delete(partNumber);
|
|
457
|
+
moveTo(entry, "uploading");
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
// src/client/discard.ts
|
|
461
|
+
async function discard(context, upload) {
|
|
462
|
+
const { id, expiresAt } = upload ?? {};
|
|
463
|
+
if (typeof id !== "string") return fail(invalidInput("Pass discard() an upload from resumable()."));
|
|
464
|
+
const release = claim(context, id);
|
|
465
|
+
try {
|
|
466
|
+
const { data, error } = await context.records.list(context.namespace);
|
|
467
|
+
if (error) return fail(error);
|
|
468
|
+
const record = data.find((saved) => saved.id === id);
|
|
469
|
+
if (!record) return typeof expiresAt === "string" && expiresSoon$1(expiresAt, Date.now()) ? ok({ aborted: false }) : fail(notListed());
|
|
470
|
+
const lock = lockUpload(id);
|
|
471
|
+
const taken = await lock.taken;
|
|
472
|
+
if (!taken) return fail(taken === false ? uploadInUse() : lockRefused());
|
|
473
|
+
try {
|
|
474
|
+
const answer = await context.endpoints.abort(record.token, { keep: true });
|
|
475
|
+
if (answer.error && !NEEDS_NEW_TICKET.has(answer.error.code)) return fail(answer.error);
|
|
476
|
+
await context.records.remove(id);
|
|
477
|
+
return ok({ aborted: answer.error === null });
|
|
478
|
+
} finally {
|
|
479
|
+
lock.release();
|
|
480
|
+
}
|
|
481
|
+
} finally {
|
|
482
|
+
release();
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
// src/client/answers.ts
|
|
486
|
+
const isString = (value) => typeof value === "string";
|
|
487
|
+
const isTime = (value) => isString(value) && !Number.isNaN(Date.parse(value));
|
|
488
|
+
const isSafeInteger = (value) => Number.isSafeInteger(value);
|
|
489
|
+
const isCount = (value) => isSafeInteger(value) && value > 0;
|
|
490
|
+
function isSignedRequest(value) {
|
|
491
|
+
return isPlainObject(value) && isString(value.url) && isPlainObject(value.headers) && Object.values(value.headers).every(isString) && isTime(value.expiresAt);
|
|
492
|
+
}
|
|
493
|
+
function isSignedPart(value, isWanted) {
|
|
494
|
+
return isPlainObject(value) && isSafeInteger(value.partNumber) && isWanted(value.partNumber) && isSignedRequest(value);
|
|
495
|
+
}
|
|
496
|
+
function hasTicketFields(value) {
|
|
497
|
+
return isPlainObject(value) && isString(value.id) && isString(value.key) && isString(value.token) && isTime(value.expiresAt);
|
|
498
|
+
}
|
|
499
|
+
function isSingle(value) {
|
|
500
|
+
return hasTicketFields(value) && value.kind === "single" && isSignedRequest(value.put);
|
|
501
|
+
}
|
|
502
|
+
function isMultipart(value, size) {
|
|
503
|
+
if (!hasTicketFields(value) || value.kind !== "multipart") return false;
|
|
504
|
+
const { partSize, partCount, parts } = value;
|
|
505
|
+
return isCount(partSize) && isCount(partCount) && partCount === countParts(size, partSize) && Array.isArray(parts) && parts.every((part) => isSignedPart(part, (partNumber) => partNumber >= 1 && partNumber <= partCount));
|
|
506
|
+
}
|
|
507
|
+
function isUploadedFile(value) {
|
|
508
|
+
return isPlainObject(value) && isString(value.id) && isString(value.key) && isString(value.name) && isString(value.type) && typeof value.size === "number" && (value.url === null || isString(value.url));
|
|
509
|
+
}
|
|
510
|
+
function readError(value) {
|
|
511
|
+
if (!isPlainObject(value)) return null;
|
|
512
|
+
const { code, message, retryable, parts } = value;
|
|
513
|
+
if (!isString(code) || !isString(message) || typeof retryable !== "boolean") return null;
|
|
514
|
+
const body = {
|
|
515
|
+
code,
|
|
516
|
+
message,
|
|
517
|
+
retryable
|
|
518
|
+
};
|
|
519
|
+
return Array.isArray(parts) && parts.every(isCount) ? {
|
|
520
|
+
...body,
|
|
521
|
+
parts
|
|
522
|
+
} : body;
|
|
523
|
+
}
|
|
524
|
+
function readFileTicket(value, size) {
|
|
525
|
+
if (isSingle(value) || isMultipart(value, size)) return value;
|
|
526
|
+
return isPlainObject(value) && value.kind === "rejected" ? readError(value.error) : null;
|
|
527
|
+
}
|
|
528
|
+
function readTickets(json, files) {
|
|
529
|
+
if (!isPlainObject(json) || !Array.isArray(json.files) || json.files.length !== files.length) return null;
|
|
530
|
+
const answers = json.files;
|
|
531
|
+
const tickets = files.map((file, index) => readFileTicket(answers[index], file.size));
|
|
532
|
+
return tickets.every((ticket) => ticket !== null) ? tickets : null;
|
|
533
|
+
}
|
|
534
|
+
function readSignedRequest(json) {
|
|
535
|
+
return isPlainObject(json) && isSignedRequest(json.put) ? json.put : null;
|
|
536
|
+
}
|
|
537
|
+
function readSignedParts(json, asked) {
|
|
538
|
+
if (!isPlainObject(json) || !Array.isArray(json.parts) || json.parts.length !== asked.length) return null;
|
|
539
|
+
const unanswered = new Set(asked);
|
|
540
|
+
return json.parts.every((part) => isSignedPart(part, (partNumber) => unanswered.delete(partNumber))) ? json.parts : null;
|
|
541
|
+
}
|
|
542
|
+
function readCompleted(json) {
|
|
543
|
+
return isPlainObject(json) && isUploadedFile(json.file) && "data" in json ? {
|
|
544
|
+
file: json.file,
|
|
545
|
+
data: json.data
|
|
546
|
+
} : null;
|
|
547
|
+
}
|
|
548
|
+
function readErrorBody(json) {
|
|
549
|
+
return isPlainObject(json) ? readError(json.error) : null;
|
|
550
|
+
}
|
|
551
|
+
// src/client/endpoints.ts
|
|
552
|
+
const ASK_TIMEOUT_MS = 3e4;
|
|
553
|
+
const COMPLETE_TIMEOUT_MS = 12e4;
|
|
554
|
+
const ABORT_TIMEOUT_MS = 1e4;
|
|
555
|
+
function parseJson(text) {
|
|
556
|
+
try {
|
|
557
|
+
return JSON.parse(text);
|
|
558
|
+
} catch {
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
const refusal = (status) => status === 429 ? serverBusy() : unreadableAnswer(status);
|
|
563
|
+
const toError = (body) => new CloudropError(body.code, body.message, body);
|
|
564
|
+
function createEndpoints(basePath, fetch) {
|
|
565
|
+
async function post(endpoint, body, { signal, keepalive, timeoutMs = ASK_TIMEOUT_MS }, read) {
|
|
566
|
+
const controller = new AbortController();
|
|
567
|
+
const stop = () => controller.abort();
|
|
568
|
+
const timeout = setTimeout(stop, timeoutMs);
|
|
569
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
570
|
+
if (signal?.aborted) stop();
|
|
571
|
+
let response;
|
|
572
|
+
let text;
|
|
573
|
+
try {
|
|
574
|
+
response = await fetch(`${basePath}/${endpoint}`, {
|
|
575
|
+
method: "POST",
|
|
576
|
+
headers: { "content-type": "application/json" },
|
|
577
|
+
body: JSON.stringify(body),
|
|
578
|
+
credentials: "include",
|
|
579
|
+
signal: controller.signal,
|
|
580
|
+
keepalive
|
|
581
|
+
});
|
|
582
|
+
text = await response.text();
|
|
583
|
+
} catch {
|
|
584
|
+
return fail(signal?.aborted ? aborted() : serverUnreachable());
|
|
585
|
+
} finally {
|
|
586
|
+
clearTimeout(timeout);
|
|
587
|
+
signal?.removeEventListener("abort", stop);
|
|
588
|
+
}
|
|
589
|
+
const json = parseJson(text);
|
|
590
|
+
const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"));
|
|
591
|
+
if (!response.ok) {
|
|
592
|
+
const body = readErrorBody(json);
|
|
593
|
+
return fail(waitAtLeast(body ? toError(body) : refusal(response.status), retryAfterMs));
|
|
594
|
+
}
|
|
595
|
+
const data = read(json, retryAfterMs);
|
|
596
|
+
return data === null ? fail(unreadableAnswer(response.status)) : ok(data);
|
|
597
|
+
}
|
|
598
|
+
return {
|
|
599
|
+
start(route, files, metadata, idempotencyKey, signal) {
|
|
600
|
+
return post("upload", {
|
|
601
|
+
route,
|
|
602
|
+
files: files.map(({ name, size, type }) => ({
|
|
603
|
+
name,
|
|
604
|
+
size,
|
|
605
|
+
type
|
|
606
|
+
})),
|
|
607
|
+
metadata,
|
|
608
|
+
idempotencyKey
|
|
609
|
+
}, { signal }, (json, retryAfterMs) => readTickets(json, files)?.map((ticket) => "kind" in ticket ? ticket : waitAtLeast(toError(ticket), retryAfterMs)) ?? null);
|
|
610
|
+
},
|
|
611
|
+
signSingle: (token, signal) => post("sign", { token }, { signal }, readSignedRequest),
|
|
612
|
+
signParts: (token, parts, signal) => post("sign", {
|
|
613
|
+
token,
|
|
614
|
+
parts
|
|
615
|
+
}, { signal }, (json) => readSignedParts(json, parts)),
|
|
616
|
+
complete: (token) => post("complete", { token }, { timeoutMs: COMPLETE_TIMEOUT_MS }, readCompleted),
|
|
617
|
+
abort: (token, options) => post("abort", {
|
|
618
|
+
token,
|
|
619
|
+
...options
|
|
620
|
+
}, {
|
|
621
|
+
keepalive: options?.keep !== true,
|
|
622
|
+
timeoutMs: ABORT_TIMEOUT_MS
|
|
623
|
+
}, () => true)
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
// src/client/progress.ts
|
|
627
|
+
const TIME_CONSTANT_MS = 3e3;
|
|
628
|
+
function createProgress() {
|
|
629
|
+
const best = /* @__PURE__ */ new Map();
|
|
630
|
+
let bytes = 0;
|
|
631
|
+
return {
|
|
632
|
+
get bytes() {
|
|
633
|
+
return bytes;
|
|
634
|
+
},
|
|
635
|
+
sent(part, sent, partSize) {
|
|
636
|
+
const before = best.get(part) ?? 0;
|
|
637
|
+
const reached = Math.min(sent, partSize);
|
|
638
|
+
if (reached <= before) return 0;
|
|
639
|
+
best.set(part, reached);
|
|
640
|
+
bytes += reached - before;
|
|
641
|
+
return reached - before;
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function createMeter() {
|
|
646
|
+
let rate = 0;
|
|
647
|
+
let last = 0;
|
|
648
|
+
let pending = 0;
|
|
649
|
+
return {
|
|
650
|
+
add(bytes, at) {
|
|
651
|
+
const elapsed = at - last;
|
|
652
|
+
if (elapsed <= 0) {
|
|
653
|
+
pending += bytes;
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
656
|
+
const alpha = 1 - Math.exp(-elapsed / TIME_CONSTANT_MS);
|
|
657
|
+
rate += alpha * ((bytes + pending) * 1e3 / elapsed - rate);
|
|
658
|
+
pending = 0;
|
|
659
|
+
last = at;
|
|
660
|
+
},
|
|
661
|
+
speed: (at) => rate * Math.exp(-Math.max(0, at - last) / TIME_CONSTANT_MS),
|
|
662
|
+
restart(at) {
|
|
663
|
+
rate = 0;
|
|
664
|
+
last = at;
|
|
665
|
+
pending = 0;
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
// src/client/entry.ts
|
|
670
|
+
function createEntry(file, rank, changed) {
|
|
671
|
+
const entry = {
|
|
672
|
+
id: String(rank),
|
|
673
|
+
file,
|
|
674
|
+
rank,
|
|
675
|
+
status: "queued",
|
|
676
|
+
result: null,
|
|
677
|
+
error: null,
|
|
678
|
+
progress: createProgress(),
|
|
679
|
+
meter: createMeter(),
|
|
680
|
+
cancel: new AbortController(),
|
|
681
|
+
pass: null,
|
|
682
|
+
wake: null,
|
|
683
|
+
networkPaused: false,
|
|
684
|
+
landed: /* @__PURE__ */ new Set(),
|
|
685
|
+
upload: null,
|
|
686
|
+
firstOutcome: null,
|
|
687
|
+
run: Promise.resolve(),
|
|
688
|
+
changed
|
|
689
|
+
};
|
|
690
|
+
entry.cancel.signal.addEventListener("abort", () => {
|
|
691
|
+
entry.pass?.abort();
|
|
692
|
+
entry.wake?.();
|
|
693
|
+
});
|
|
694
|
+
return entry;
|
|
695
|
+
}
|
|
696
|
+
function outcomeOf({ id, file, status, result, error }) {
|
|
697
|
+
const { name } = file;
|
|
698
|
+
if (status === "done" && result) return {
|
|
699
|
+
id,
|
|
700
|
+
name,
|
|
701
|
+
status,
|
|
702
|
+
result
|
|
703
|
+
};
|
|
704
|
+
return {
|
|
705
|
+
id,
|
|
706
|
+
name,
|
|
707
|
+
status: status === "aborted" ? "aborted" : "failed",
|
|
708
|
+
error
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
function settled(entry) {
|
|
712
|
+
entry.firstOutcome ??= outcomeOf(entry);
|
|
713
|
+
entry.changed(true);
|
|
714
|
+
}
|
|
715
|
+
function settle(entry, status, error) {
|
|
716
|
+
entry.error = error;
|
|
717
|
+
entry.status = status;
|
|
718
|
+
settled(entry);
|
|
719
|
+
}
|
|
720
|
+
async function held(entry) {
|
|
721
|
+
while (entry.status === "paused" && !entry.cancel.signal.aborted) await new Promise((resolve) => {
|
|
722
|
+
entry.wake = resolve;
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
function pauseFile(entry, byNetwork = false) {
|
|
726
|
+
if (entry.status === "paused" && !byNetwork) entry.networkPaused = false;
|
|
727
|
+
if (entry.status !== "queued" && entry.status !== "uploading") return;
|
|
728
|
+
entry.networkPaused = byNetwork;
|
|
729
|
+
entry.status = "paused";
|
|
730
|
+
entry.changed(true);
|
|
731
|
+
entry.pass?.abort();
|
|
732
|
+
}
|
|
733
|
+
function resumeFile(entry) {
|
|
734
|
+
if (entry.status !== "paused") return;
|
|
735
|
+
entry.networkPaused = false;
|
|
736
|
+
entry.status = "queued";
|
|
737
|
+
entry.changed(true);
|
|
738
|
+
entry.wake?.();
|
|
739
|
+
}
|
|
740
|
+
// src/client/fingerprint.ts
|
|
741
|
+
const EDGE = 65536;
|
|
742
|
+
async function fingerprintOf(file) {
|
|
743
|
+
const edges = file.size <= 2 * EDGE ? file : new Blob([file.slice(0, EDGE), file.slice(-65536)]);
|
|
744
|
+
const digest = await crypto.subtle.digest("SHA-256", await edges.arrayBuffer());
|
|
745
|
+
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
746
|
+
}
|
|
747
|
+
async function isSameFile(record, file) {
|
|
748
|
+
try {
|
|
749
|
+
return file.name === record.name && file.size === record.size && file.type === record.type && file.lastModified === record.lastModified && await fingerprintOf(file) === record.fingerprint;
|
|
750
|
+
} catch {
|
|
751
|
+
return false;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
// src/client/persist.ts
|
|
755
|
+
function uploadOf({ context, args }, entry, ticket) {
|
|
756
|
+
const { records, namespace } = context;
|
|
757
|
+
let unlock = () => {};
|
|
758
|
+
if (ticket.kind === "multipart" && records.enabled) {
|
|
759
|
+
const lock = lockUpload(ticket.id);
|
|
760
|
+
const release = claim(context, ticket.id);
|
|
761
|
+
unlock = () => {
|
|
762
|
+
release();
|
|
763
|
+
lock.release();
|
|
764
|
+
};
|
|
765
|
+
const { file } = entry;
|
|
766
|
+
records.save(lock.taken.then((taken) => taken ? fingerprintOf(file) : Promise.reject()).then((fingerprint) => ({
|
|
767
|
+
id: ticket.id,
|
|
768
|
+
namespace,
|
|
769
|
+
route: args.route,
|
|
770
|
+
metadata: args.metadata === void 0 ? void 0 : JSON.parse(JSON.stringify(args.metadata)),
|
|
771
|
+
name: file.name,
|
|
772
|
+
size: file.size,
|
|
773
|
+
type: file.type,
|
|
774
|
+
lastModified: file.lastModified,
|
|
775
|
+
fingerprint,
|
|
776
|
+
token: ticket.token,
|
|
777
|
+
key: ticket.key,
|
|
778
|
+
partSize: ticket.partSize,
|
|
779
|
+
partCount: ticket.partCount,
|
|
780
|
+
expiresAt: ticket.expiresAt,
|
|
781
|
+
savedAt: Date.now()
|
|
782
|
+
})));
|
|
783
|
+
}
|
|
784
|
+
return newUpload(ticket, entry, unlock);
|
|
785
|
+
}
|
|
786
|
+
function letGo(context, upload, answered) {
|
|
787
|
+
if (upload.ticket.kind === "multipart") context.records.remove(upload.ticket.id);
|
|
788
|
+
if (answered) answered.then(upload.unlock);
|
|
789
|
+
else upload.unlock();
|
|
790
|
+
}
|
|
791
|
+
// src/client/file-run.ts
|
|
792
|
+
function drop(context, upload) {
|
|
793
|
+
upload.dropped = true;
|
|
794
|
+
const answered = context.endpoints.abort(upload.ticket.token).then(() => void 0);
|
|
795
|
+
letGo(context, upload, answered);
|
|
796
|
+
return answered;
|
|
797
|
+
}
|
|
798
|
+
function giveUp(context, upload) {
|
|
799
|
+
const { error } = upload.entry;
|
|
800
|
+
if (!(upload.ticket.kind === "multipart" && context.records.enabled && error?.retryable) || NEEDS_NEW_TICKET.has(error.code)) return void drop(context, upload);
|
|
801
|
+
upload.dropped = true;
|
|
802
|
+
upload.unlock();
|
|
803
|
+
}
|
|
804
|
+
function abortFile(context, entry) {
|
|
805
|
+
const { status, upload } = entry;
|
|
806
|
+
if (status === "done" || status === "aborted") return;
|
|
807
|
+
if (status !== "failed") {
|
|
808
|
+
entry.cancel.abort();
|
|
809
|
+
if (!upload) settle(entry, "aborted", withFile(aborted(), entry));
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
context.leftovers.release(entry);
|
|
813
|
+
settle(entry, "aborted", withFile(aborted(), entry));
|
|
814
|
+
if (!upload || upload.dropped) return;
|
|
815
|
+
if (upload.mayBeComplete) letGo(context, upload);
|
|
816
|
+
else drop(context, upload);
|
|
817
|
+
}
|
|
818
|
+
async function end(context, upload, error) {
|
|
819
|
+
const { entry, mayBeComplete } = upload;
|
|
820
|
+
const failed = !entry.cancel.signal.aborted;
|
|
821
|
+
if (failed && context.keepFailed) {
|
|
822
|
+
if (!mayBeComplete) context.leftovers.hold(entry);
|
|
823
|
+
return settle(entry, "failed", withFile(error, entry));
|
|
824
|
+
}
|
|
825
|
+
settle(entry, failed ? "failed" : "aborted", withFile(failed ? error : aborted(), entry));
|
|
826
|
+
if (failed) {
|
|
827
|
+
if (mayBeComplete) upload.unlock();
|
|
828
|
+
else giveUp(context, upload);
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
if (mayBeComplete) letGo(context, upload);
|
|
832
|
+
else await drop(context, upload);
|
|
833
|
+
}
|
|
834
|
+
const GONE = /* @__PURE__ */ new Set([
|
|
835
|
+
"UPLOAD_EXPIRED",
|
|
836
|
+
"INVALID_TOKEN",
|
|
837
|
+
"NO_SUCH_UPLOAD"
|
|
838
|
+
]);
|
|
839
|
+
async function runFile(context, upload) {
|
|
840
|
+
const { entry } = upload;
|
|
841
|
+
const { cancel } = entry;
|
|
842
|
+
while (!cancel.signal.aborted) {
|
|
843
|
+
if (entry.status === "paused") {
|
|
844
|
+
await held(entry);
|
|
845
|
+
continue;
|
|
846
|
+
}
|
|
847
|
+
const pass = new AbortController();
|
|
848
|
+
entry.pass = pass;
|
|
849
|
+
const result = await sendAndComplete(context, upload, pass.signal);
|
|
850
|
+
if (!result.error) {
|
|
851
|
+
letGo(context, upload);
|
|
852
|
+
entry.result = result.data;
|
|
853
|
+
entry.status = "done";
|
|
854
|
+
settled(entry);
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
const { code } = result.error;
|
|
858
|
+
if (upload.probe && GONE.has(code) && !cancel.signal.aborted) return result.error;
|
|
859
|
+
if (!(code === "ABORTED" && pass.signal.aborted && !cancel.signal.aborted)) {
|
|
860
|
+
await end(context, upload, result.error);
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
await end(context, upload, aborted());
|
|
865
|
+
}
|
|
866
|
+
// src/client/launch.ts
|
|
867
|
+
const newRequestKey = () => Array.from(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
868
|
+
async function refused(job, entry, refusal, attempt) {
|
|
869
|
+
if (!refusal.retryable || attempt === 6) return settle(entry, "failed", withFile(refusal, entry));
|
|
870
|
+
await launch(job, [entry], attempt + 1, retryDelay(attempt, retryAfterOf(refusal)));
|
|
871
|
+
return entry.run;
|
|
872
|
+
}
|
|
873
|
+
async function runEntry(job, entry, answer, index, whole, attempt) {
|
|
874
|
+
if (entry.status === "aborted") {
|
|
875
|
+
const late = answer.error ? void 0 : answer.data[index];
|
|
876
|
+
if (late && !(late instanceof CloudropError)) job.context.endpoints.abort(late.token);
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
if (answer.error) return settle(entry, "failed", whole ? answer.error : withFile(answer.error, entry));
|
|
880
|
+
const ticket = answer.data[index] ?? unreadableAnswer(200);
|
|
881
|
+
if (ticket instanceof CloudropError) return refused(job, entry, ticket, attempt);
|
|
882
|
+
entry.upload = uploadOf(job, entry, ticket);
|
|
883
|
+
await job.runUpload(entry.upload);
|
|
884
|
+
}
|
|
885
|
+
function askAlone(job, entry, ask, next, waitMs) {
|
|
886
|
+
const { signal } = entry.cancel;
|
|
887
|
+
return job.watch(async () => {
|
|
888
|
+
for (let wait = waitMs;; wait = 0) {
|
|
889
|
+
if (entry.status === "paused") wait = 0;
|
|
890
|
+
await held(entry);
|
|
891
|
+
if (signal.aborted) return fail(aborted());
|
|
892
|
+
const pass = new AbortController();
|
|
893
|
+
entry.pass = pass;
|
|
894
|
+
if (wait > 0) await pause(wait, pass.signal);
|
|
895
|
+
const answer = await withRetry(ask, pass.signal, next());
|
|
896
|
+
if (answer.error?.code !== "ABORTED" || !pass.signal.aborted || signal.aborted) return answer;
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
}
|
|
900
|
+
async function launch(job, entries, first, waitMs = 0) {
|
|
901
|
+
const { context, args } = job;
|
|
902
|
+
const files = entries.map((entry) => entry.file);
|
|
903
|
+
const own = first === void 0 ? void 0 : entries[0];
|
|
904
|
+
const signal = own ? own.cancel.signal : job.signal;
|
|
905
|
+
let attempt = (first ?? 1) - 1;
|
|
906
|
+
const key = newRequestKey();
|
|
907
|
+
const ask = (at) => {
|
|
908
|
+
attempt = at;
|
|
909
|
+
return context.endpoints.start(args.route, files, args.metadata, key, signal);
|
|
910
|
+
};
|
|
911
|
+
const started = (own ? askAlone(job, own, ask, () => attempt + 1, waitMs) : withRetry(ask, signal)).then((answer) => answer.error && signal.aborted ? fail(aborted()) : answer);
|
|
912
|
+
const whole = (first ?? 1) === 1 && entries.length === job.entries.length;
|
|
913
|
+
entries.forEach((entry, index) => {
|
|
914
|
+
entry.run = started.then((answer) => runEntry(job, entry, answer, index, whole, attempt)).finally(job.idle);
|
|
915
|
+
});
|
|
916
|
+
const { error } = await started;
|
|
917
|
+
return whole ? error : null;
|
|
918
|
+
}
|
|
919
|
+
// src/client/lifecycle.ts
|
|
920
|
+
function watchConnectivity(offline, online) {
|
|
921
|
+
if (typeof globalThis.addEventListener !== "function") return () => {};
|
|
922
|
+
addEventListener("offline", offline);
|
|
923
|
+
addEventListener("online", online);
|
|
924
|
+
if (globalThis.navigator?.onLine === false) offline();
|
|
925
|
+
else online();
|
|
926
|
+
return () => {
|
|
927
|
+
removeEventListener("offline", offline);
|
|
928
|
+
removeEventListener("online", online);
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
function unexpired({ upload }) {
|
|
932
|
+
return upload && Date.parse(upload.ticket.expiresAt) > Date.now() ? upload : null;
|
|
933
|
+
}
|
|
934
|
+
function createLeftovers(giveUp) {
|
|
935
|
+
const held = /* @__PURE__ */ new Set();
|
|
936
|
+
const sync = () => {
|
|
937
|
+
for (const entry of held) if (!unexpired(entry)) held.delete(entry);
|
|
938
|
+
if (typeof globalThis.addEventListener !== "function") return;
|
|
939
|
+
if (held.size > 0) addEventListener("pagehide", pagehide);
|
|
940
|
+
else removeEventListener("pagehide", pagehide);
|
|
941
|
+
};
|
|
942
|
+
function pagehide() {
|
|
943
|
+
for (const entry of held) {
|
|
944
|
+
const upload = unexpired(entry);
|
|
945
|
+
if (upload) giveUp(upload);
|
|
946
|
+
}
|
|
947
|
+
held.clear();
|
|
948
|
+
sync();
|
|
949
|
+
}
|
|
950
|
+
return {
|
|
951
|
+
hold(entry) {
|
|
952
|
+
held.add(entry);
|
|
953
|
+
sync();
|
|
954
|
+
},
|
|
955
|
+
release(entry) {
|
|
956
|
+
held.delete(entry);
|
|
957
|
+
sync();
|
|
958
|
+
}
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
// src/client/job-run.ts
|
|
962
|
+
const settledStatus = ({ status }) => status === "done" || status === "failed" || status === "aborted";
|
|
963
|
+
const busy = (entry) => !settledStatus(entry) || entry.status === "failed" && !!entry.upload && !entry.upload.dropped && !entry.upload.mayBeComplete;
|
|
964
|
+
function createJob(context, args, entries) {
|
|
965
|
+
const controller = new AbortController();
|
|
966
|
+
const { signal } = controller;
|
|
967
|
+
const abort = () => controller.abort();
|
|
968
|
+
const idle = () => {
|
|
969
|
+
if (!entries.some(busy)) args.signal.removeEventListener("abort", abort);
|
|
970
|
+
};
|
|
971
|
+
signal.addEventListener("abort", () => {
|
|
972
|
+
for (const entry of entries) abortFile(context, entry);
|
|
973
|
+
idle();
|
|
974
|
+
});
|
|
975
|
+
let running = 0;
|
|
976
|
+
let unwatch = () => {};
|
|
977
|
+
const offline = () => {
|
|
978
|
+
for (const entry of entries) pauseFile(entry, true);
|
|
979
|
+
};
|
|
980
|
+
const online = () => {
|
|
981
|
+
for (const entry of entries) if (entry.networkPaused) resumeFile(entry);
|
|
982
|
+
};
|
|
983
|
+
const job = {
|
|
984
|
+
context,
|
|
985
|
+
args,
|
|
986
|
+
entries,
|
|
987
|
+
signal,
|
|
988
|
+
abort,
|
|
989
|
+
runUpload: (upload) => job.watch(() => runFile(context, upload).then((gone) => gone && startOver(job, upload.entry))),
|
|
990
|
+
watch(run) {
|
|
991
|
+
running += 1;
|
|
992
|
+
if (running === 1) unwatch = watchConnectivity(offline, online);
|
|
993
|
+
return run().finally(() => {
|
|
994
|
+
running -= 1;
|
|
995
|
+
if (running === 0) unwatch();
|
|
996
|
+
});
|
|
997
|
+
},
|
|
998
|
+
listen() {
|
|
999
|
+
args.signal.addEventListener("abort", abort);
|
|
1000
|
+
if (args.signal.aborted) abort();
|
|
1001
|
+
},
|
|
1002
|
+
idle
|
|
1003
|
+
};
|
|
1004
|
+
return job;
|
|
1005
|
+
}
|
|
1006
|
+
function forgetTicket(context, entry) {
|
|
1007
|
+
if (entry.upload && !entry.upload.dropped) letGo(context, entry.upload);
|
|
1008
|
+
entry.upload = null;
|
|
1009
|
+
entry.landed.clear();
|
|
1010
|
+
entry.progress = createProgress();
|
|
1011
|
+
}
|
|
1012
|
+
async function startOver(job, entry) {
|
|
1013
|
+
forgetTicket(job.context, entry);
|
|
1014
|
+
entry.status = "queued";
|
|
1015
|
+
entry.changed(true);
|
|
1016
|
+
await launch(job, [entry], 1);
|
|
1017
|
+
await entry.run;
|
|
1018
|
+
}
|
|
1019
|
+
async function allSettled(entries) {
|
|
1020
|
+
while (!entries.every(settledStatus)) await Promise.all(entries.map((entry) => entry.run));
|
|
1021
|
+
return entries.map(outcomeOf);
|
|
1022
|
+
}
|
|
1023
|
+
function resultOf(files) {
|
|
1024
|
+
return {
|
|
1025
|
+
data: { files },
|
|
1026
|
+
error: files.find((outcome) => outcome.status !== "done")?.error ?? null
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
function firstRun(job) {
|
|
1030
|
+
const { entries } = job;
|
|
1031
|
+
job.listen();
|
|
1032
|
+
const refused = launch(job, entries);
|
|
1033
|
+
const runs = entries.map((entry) => entry.run);
|
|
1034
|
+
return Promise.all([refused, ...runs]).then(([error]) => error ? fail(error) : resultOf(entries.map((entry) => entry.firstOutcome)));
|
|
1035
|
+
}
|
|
1036
|
+
// src/client/job-snapshot.ts
|
|
1037
|
+
const fraction = (part, whole, finished) => whole > 0 ? part / whole : finished ? 1 : 0;
|
|
1038
|
+
const etaOf = (left, speed) => speed > 0 ? left / speed : null;
|
|
1039
|
+
function fileSnapshot(entry, now) {
|
|
1040
|
+
const { id, file, status, result, error } = entry;
|
|
1041
|
+
const { name, size, type } = file;
|
|
1042
|
+
const bytes = entry.progress.bytes;
|
|
1043
|
+
const speed = status === "uploading" ? entry.meter.speed(now) : 0;
|
|
1044
|
+
return {
|
|
1045
|
+
id,
|
|
1046
|
+
name,
|
|
1047
|
+
size,
|
|
1048
|
+
type,
|
|
1049
|
+
status,
|
|
1050
|
+
bytes,
|
|
1051
|
+
progress: fraction(bytes, size, status === "done"),
|
|
1052
|
+
speed,
|
|
1053
|
+
eta: etaOf(size - bytes, speed),
|
|
1054
|
+
error,
|
|
1055
|
+
result
|
|
1056
|
+
};
|
|
1057
|
+
}
|
|
1058
|
+
const sum = (files, key) => files.reduce((total, file) => total + file[key], 0);
|
|
1059
|
+
function snapshotOf(entries) {
|
|
1060
|
+
const now = Date.now();
|
|
1061
|
+
const files = entries.map((entry) => fileSnapshot(entry, now));
|
|
1062
|
+
const counting = (status) => files.filter((file) => file.status === status).length;
|
|
1063
|
+
const counted = files.filter((file) => file.status !== "failed" && file.status !== "aborted");
|
|
1064
|
+
const bytes = sum(counted, "bytes");
|
|
1065
|
+
const size = sum(counted, "size");
|
|
1066
|
+
const speed = sum(counted, "speed");
|
|
1067
|
+
const done = counting("done");
|
|
1068
|
+
return {
|
|
1069
|
+
files,
|
|
1070
|
+
totals: {
|
|
1071
|
+
bytes,
|
|
1072
|
+
size,
|
|
1073
|
+
progress: fraction(bytes, size, done > 0 && done === counted.length),
|
|
1074
|
+
speed,
|
|
1075
|
+
eta: etaOf(size - bytes, speed),
|
|
1076
|
+
done,
|
|
1077
|
+
failed: counting("failed"),
|
|
1078
|
+
aborted: counting("aborted"),
|
|
1079
|
+
count: files.length
|
|
1080
|
+
}
|
|
1081
|
+
};
|
|
1082
|
+
}
|
|
1083
|
+
// src/client/retry-file.ts
|
|
1084
|
+
function needsTicket({ upload, error }) {
|
|
1085
|
+
if (!upload) return true;
|
|
1086
|
+
if (upload.mayBeComplete) return false;
|
|
1087
|
+
return upload.dropped || Date.parse(upload.ticket.expiresAt) <= Date.now() || NEEDS_NEW_TICKET.has(error?.code);
|
|
1088
|
+
}
|
|
1089
|
+
function requeue(job, entry, fresh) {
|
|
1090
|
+
job.context.leftovers.release(entry);
|
|
1091
|
+
if (fresh) forgetTicket(job.context, entry);
|
|
1092
|
+
else if (entry.upload) {
|
|
1093
|
+
entry.upload.repairs = 0;
|
|
1094
|
+
if (entry.error?.code === "PARTS_MISSING") for (const part of entry.error.parts ?? []) entry.landed.delete(part);
|
|
1095
|
+
}
|
|
1096
|
+
entry.error = null;
|
|
1097
|
+
entry.status = "queued";
|
|
1098
|
+
}
|
|
1099
|
+
async function retryFiles(job, entries) {
|
|
1100
|
+
const failed = entries.filter((entry) => entry.status === "failed");
|
|
1101
|
+
const fresh = failed.filter(needsTicket);
|
|
1102
|
+
for (const entry of failed) requeue(job, entry, fresh.includes(entry));
|
|
1103
|
+
failed[0]?.changed(true);
|
|
1104
|
+
if (failed.length > 0) job.listen();
|
|
1105
|
+
for (const entry of failed) {
|
|
1106
|
+
const { upload } = entry;
|
|
1107
|
+
if (upload) entry.run = job.runUpload(upload).finally(job.idle);
|
|
1108
|
+
}
|
|
1109
|
+
const refused = fresh.length > 0 ? launch(job, fresh, fresh.length === 1 ? 1 : void 0) : null;
|
|
1110
|
+
return {
|
|
1111
|
+
outcomes: await allSettled(entries),
|
|
1112
|
+
refused: await refused
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
// src/client/store.ts
|
|
1116
|
+
const THROTTLE_MS = 100;
|
|
1117
|
+
const REFRESH_MS = 1e3;
|
|
1118
|
+
function createStore(build, active) {
|
|
1119
|
+
let snapshot = build();
|
|
1120
|
+
let lastMs = -1e400;
|
|
1121
|
+
let timer;
|
|
1122
|
+
let dueMs = 1e400;
|
|
1123
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1124
|
+
const schedule = (atMs) => {
|
|
1125
|
+
if (timer !== void 0 && dueMs <= atMs) return;
|
|
1126
|
+
clearTimeout(timer);
|
|
1127
|
+
dueMs = atMs;
|
|
1128
|
+
timer = setTimeout(publish, Math.max(0, atMs - Date.now()));
|
|
1129
|
+
};
|
|
1130
|
+
function publish() {
|
|
1131
|
+
clearTimeout(timer);
|
|
1132
|
+
timer = void 0;
|
|
1133
|
+
dueMs = 1e400;
|
|
1134
|
+
snapshot = build();
|
|
1135
|
+
lastMs = Date.now();
|
|
1136
|
+
for (const listener of [...listeners]) try {
|
|
1137
|
+
listener();
|
|
1138
|
+
} catch (error) {
|
|
1139
|
+
setTimeout(() => {
|
|
1140
|
+
throw error;
|
|
1141
|
+
});
|
|
1142
|
+
}
|
|
1143
|
+
if (active()) schedule(lastMs + REFRESH_MS);
|
|
1144
|
+
}
|
|
1145
|
+
return {
|
|
1146
|
+
get: () => snapshot,
|
|
1147
|
+
subscribe(listener) {
|
|
1148
|
+
listeners.add(listener);
|
|
1149
|
+
return () => {
|
|
1150
|
+
listeners.delete(listener);
|
|
1151
|
+
};
|
|
1152
|
+
},
|
|
1153
|
+
changed: (urgent) => urgent ? publish() : schedule(lastMs + THROTTLE_MS)
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
// src/client/job.ts
|
|
1157
|
+
function handleOf(job, entry, retry) {
|
|
1158
|
+
return {
|
|
1159
|
+
id: entry.id,
|
|
1160
|
+
name: entry.file.name,
|
|
1161
|
+
pause: () => pauseFile(entry),
|
|
1162
|
+
resume: () => resumeFile(entry),
|
|
1163
|
+
abort: () => {
|
|
1164
|
+
abortFile(job.context, entry);
|
|
1165
|
+
job.idle();
|
|
1166
|
+
},
|
|
1167
|
+
retry: async () => (await retry(job, [entry])).outcomes[0]
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
function startJob(context, route, files, options) {
|
|
1171
|
+
return createUploadJob(context, readArguments(route, files, options), firstRun);
|
|
1172
|
+
}
|
|
1173
|
+
function createUploadJob(context, args, run, retry = retryFiles) {
|
|
1174
|
+
const entries = (args.error ? [] : args.data.files).map((file) => createEntry(file, context.nextRank(), (urgent) => store.changed(urgent)));
|
|
1175
|
+
const store = createStore(() => snapshotOf(entries), () => entries.some((entry) => entry.status === "uploading"));
|
|
1176
|
+
const job = args.error ? null : createJob(context, args.data, entries);
|
|
1177
|
+
const done = args.error ? Promise.resolve(args) : run(job);
|
|
1178
|
+
return {
|
|
1179
|
+
files: job ? entries.map((entry) => handleOf(job, entry, retry)) : [],
|
|
1180
|
+
done,
|
|
1181
|
+
abort: () => job?.abort(),
|
|
1182
|
+
pause: () => {
|
|
1183
|
+
for (const entry of entries) pauseFile(entry);
|
|
1184
|
+
},
|
|
1185
|
+
resume: () => entries.forEach(resumeFile),
|
|
1186
|
+
retry: async () => {
|
|
1187
|
+
if (!job) return done;
|
|
1188
|
+
const { outcomes, refused } = await retry(job, job.entries);
|
|
1189
|
+
return refused ? fail(refused) : resultOf(outcomes);
|
|
1190
|
+
},
|
|
1191
|
+
subscribe: store.subscribe,
|
|
1192
|
+
getSnapshot: store.get
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
// src/client/pool.ts
|
|
1196
|
+
const AUTO_START = 4;
|
|
1197
|
+
const AUTO_MAX = 8;
|
|
1198
|
+
const WINDOW_MS = 2e3;
|
|
1199
|
+
const LONGEST_WINDOW_MS = 4e3;
|
|
1200
|
+
const RISE = 1.1;
|
|
1201
|
+
const before = (a, b) => {
|
|
1202
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) if (a[i] !== b[i]) return (a[i] ?? 0) < (b[i] ?? 0);
|
|
1203
|
+
return false;
|
|
1204
|
+
};
|
|
1205
|
+
function createPool(size, now = () => Date.now()) {
|
|
1206
|
+
const auto = size === "auto";
|
|
1207
|
+
let slots = auto ? AUTO_START : size;
|
|
1208
|
+
let inUse = 0;
|
|
1209
|
+
const waiting = [];
|
|
1210
|
+
let windowStart = 0;
|
|
1211
|
+
let windowBytes = 0;
|
|
1212
|
+
let lastRate = 0;
|
|
1213
|
+
let hold = false;
|
|
1214
|
+
const restart = (rate) => {
|
|
1215
|
+
lastRate = rate;
|
|
1216
|
+
windowStart = now();
|
|
1217
|
+
windowBytes = 0;
|
|
1218
|
+
};
|
|
1219
|
+
const update = () => {
|
|
1220
|
+
const elapsed = now() - windowStart;
|
|
1221
|
+
if (auto && elapsed >= WINDOW_MS) {
|
|
1222
|
+
const rate = elapsed > LONGEST_WINDOW_MS ? 0 : windowBytes / elapsed;
|
|
1223
|
+
if (!hold && waiting.length > 0 && lastRate > 0 && rate >= lastRate * RISE && slots < AUTO_MAX) slots++;
|
|
1224
|
+
restart(rate);
|
|
1225
|
+
hold = false;
|
|
1226
|
+
}
|
|
1227
|
+
while (inUse < slots) {
|
|
1228
|
+
const next = waiting.shift();
|
|
1229
|
+
if (!next) return;
|
|
1230
|
+
inUse++;
|
|
1231
|
+
next.grant(releaser());
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
const releaser = () => {
|
|
1235
|
+
let used = false;
|
|
1236
|
+
return () => {
|
|
1237
|
+
if (used) return;
|
|
1238
|
+
used = true;
|
|
1239
|
+
inUse--;
|
|
1240
|
+
update();
|
|
1241
|
+
};
|
|
1242
|
+
};
|
|
1243
|
+
return {
|
|
1244
|
+
acquire(rank, signal) {
|
|
1245
|
+
if (signal.aborted) return Promise.resolve(null);
|
|
1246
|
+
if (inUse === 0) restart(0);
|
|
1247
|
+
update();
|
|
1248
|
+
if (inUse < slots) {
|
|
1249
|
+
inUse++;
|
|
1250
|
+
return Promise.resolve(releaser());
|
|
1251
|
+
}
|
|
1252
|
+
return new Promise((resolve) => {
|
|
1253
|
+
const drop = () => {
|
|
1254
|
+
waiting.splice(waiting.indexOf(entry), 1);
|
|
1255
|
+
resolve(null);
|
|
1256
|
+
};
|
|
1257
|
+
const entry = {
|
|
1258
|
+
rank,
|
|
1259
|
+
grant: (release) => {
|
|
1260
|
+
signal.removeEventListener("abort", drop);
|
|
1261
|
+
resolve(release);
|
|
1262
|
+
}
|
|
1263
|
+
};
|
|
1264
|
+
const at = waiting.findIndex((other) => before(rank, other.rank));
|
|
1265
|
+
waiting.splice(at === -1 ? waiting.length : at, 0, entry);
|
|
1266
|
+
signal.addEventListener("abort", drop, { once: true });
|
|
1267
|
+
});
|
|
1268
|
+
},
|
|
1269
|
+
sent(bytes) {
|
|
1270
|
+
update();
|
|
1271
|
+
windowBytes += bytes;
|
|
1272
|
+
},
|
|
1273
|
+
slowDown() {
|
|
1274
|
+
if (!auto || hold) return;
|
|
1275
|
+
slots = Math.max(1, Math.floor(slots / 2));
|
|
1276
|
+
restart(lastRate);
|
|
1277
|
+
hold = true;
|
|
1278
|
+
}
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
// src/client/resume.ts
|
|
1282
|
+
async function resumable(context, route) {
|
|
1283
|
+
const listed = await context.records.list(context.namespace);
|
|
1284
|
+
if (listed.error) return listed;
|
|
1285
|
+
const claimed = new Set(Array.from(context.claims, ({ id }) => id));
|
|
1286
|
+
return ok(listed.data.filter((record) => (route === void 0 || record.route === route) && !claimed.has(record.id)).map(({ id, route, name, size, type, lastModified, expiresAt }) => ({
|
|
1287
|
+
id,
|
|
1288
|
+
route,
|
|
1289
|
+
name,
|
|
1290
|
+
size,
|
|
1291
|
+
type,
|
|
1292
|
+
lastModified,
|
|
1293
|
+
expiresAt
|
|
1294
|
+
})));
|
|
1295
|
+
}
|
|
1296
|
+
async function forget(context, options) {
|
|
1297
|
+
const all = options !== void 0;
|
|
1298
|
+
if (all && !(isPlainObject(options) && options.all === true && Object.keys(options).length === 1)) return fail(invalidInput("Call forget() for this client's unfinished uploads, or forget({ all: true }) for every one in this browser."));
|
|
1299
|
+
const cleared = await context.records.clear(all ? null : context.namespace);
|
|
1300
|
+
return cleared.error ? cleared : ok({ forgotten: cleared.data });
|
|
1301
|
+
}
|
|
1302
|
+
async function check(context, id, file) {
|
|
1303
|
+
const release = claim(context, id);
|
|
1304
|
+
const refuse = (refusal) => {
|
|
1305
|
+
release();
|
|
1306
|
+
return refusal;
|
|
1307
|
+
};
|
|
1308
|
+
const { data, error } = await context.records.list(context.namespace);
|
|
1309
|
+
const record = data?.find((saved) => saved.id === id);
|
|
1310
|
+
if (!record) return refuse(error ?? notResumable());
|
|
1311
|
+
if (!await isSameFile(record, file)) return refuse(notSameFile());
|
|
1312
|
+
const lock = lockUpload(id);
|
|
1313
|
+
const taken = await lock.taken;
|
|
1314
|
+
if (taken) {
|
|
1315
|
+
const unlock = () => {
|
|
1316
|
+
release();
|
|
1317
|
+
lock.release();
|
|
1318
|
+
};
|
|
1319
|
+
return {
|
|
1320
|
+
record,
|
|
1321
|
+
unlock
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
return refuse(taken === false ? uploadInUse() : notResumable());
|
|
1325
|
+
}
|
|
1326
|
+
async function resumeRun(job, id, passed) {
|
|
1327
|
+
const { context, args } = job;
|
|
1328
|
+
const [entry] = job.entries;
|
|
1329
|
+
job.listen();
|
|
1330
|
+
const checked = await check(context, id, entry.file);
|
|
1331
|
+
if (entry.cancel.signal.aborted) {
|
|
1332
|
+
if (!(checked instanceof CloudropError)) checked.unlock();
|
|
1333
|
+
job.idle();
|
|
1334
|
+
return entry.error;
|
|
1335
|
+
}
|
|
1336
|
+
if (checked instanceof CloudropError) {
|
|
1337
|
+
settle(entry, "failed", checked);
|
|
1338
|
+
job.idle();
|
|
1339
|
+
return checked;
|
|
1340
|
+
}
|
|
1341
|
+
const { record, unlock } = checked;
|
|
1342
|
+
passed();
|
|
1343
|
+
args.route = record.route;
|
|
1344
|
+
args.metadata = record.metadata;
|
|
1345
|
+
const ticket = {
|
|
1346
|
+
...record,
|
|
1347
|
+
kind: "multipart",
|
|
1348
|
+
parts: []
|
|
1349
|
+
};
|
|
1350
|
+
for (let part = 1; part <= record.partCount; part++) entry.landed.add(part);
|
|
1351
|
+
const upload = newUpload(ticket, entry, unlock, true);
|
|
1352
|
+
entry.upload = upload;
|
|
1353
|
+
await job.runUpload(upload).finally(job.idle);
|
|
1354
|
+
return null;
|
|
1355
|
+
}
|
|
1356
|
+
async function checkAgain(job, run) {
|
|
1357
|
+
const [entry] = job.entries;
|
|
1358
|
+
entry.error = null;
|
|
1359
|
+
entry.status = "queued";
|
|
1360
|
+
const refused = run(job);
|
|
1361
|
+
entry.changed(true);
|
|
1362
|
+
return {
|
|
1363
|
+
refused: await refused,
|
|
1364
|
+
outcomes: await allSettled(job.entries)
|
|
1365
|
+
};
|
|
1366
|
+
}
|
|
1367
|
+
function resumeJob(context, upload, file, options) {
|
|
1368
|
+
const id = upload?.id;
|
|
1369
|
+
const args = typeof id === "string" && file instanceof File ? readArguments("resume", file, options) : fail(invalidInput("Pass resume() an upload from resumable() and the File the user picked again."));
|
|
1370
|
+
let checksPassed = false;
|
|
1371
|
+
const run = (job) => {
|
|
1372
|
+
const resumed = resumeRun(job, id, () => {
|
|
1373
|
+
checksPassed = true;
|
|
1374
|
+
});
|
|
1375
|
+
job.entries[0].run = resumed.then(() => {});
|
|
1376
|
+
return resumed;
|
|
1377
|
+
};
|
|
1378
|
+
return createUploadJob(context, args, async (job) => {
|
|
1379
|
+
const refused = await run(job);
|
|
1380
|
+
return refused ? fail(refused) : resultOf([job.entries[0].firstOutcome]);
|
|
1381
|
+
}, (job, entries) => checksPassed || job.entries[0]?.status !== "failed" ? retryFiles(job, entries) : checkAgain(job, run));
|
|
1382
|
+
}
|
|
1383
|
+
// src/client/bucket.ts
|
|
1384
|
+
const STORAGE_CODE = /<Code>([A-Za-z]{1,64})<\/Code>/;
|
|
1385
|
+
function classifyAnswer(status, body, retryAfter) {
|
|
1386
|
+
const storageCode = STORAGE_CODE.exec(body)?.[1];
|
|
1387
|
+
const retryAfterMs = parseRetryAfter(retryAfter);
|
|
1388
|
+
if (status === 403) return storageCode === "SignatureDoesNotMatch" ? {
|
|
1389
|
+
kind: "rejected",
|
|
1390
|
+
error: signatureMismatch()
|
|
1391
|
+
} : {
|
|
1392
|
+
kind: "expired",
|
|
1393
|
+
error: linkRefused(),
|
|
1394
|
+
retryAfterMs
|
|
1395
|
+
};
|
|
1396
|
+
if (status === 429 || status === 503) return {
|
|
1397
|
+
kind: "retry",
|
|
1398
|
+
error: storageBusy(),
|
|
1399
|
+
retryAfterMs
|
|
1400
|
+
};
|
|
1401
|
+
if (status === 408 || status >= 500 && status !== 501) return {
|
|
1402
|
+
kind: "retry",
|
|
1403
|
+
error: storageDown(),
|
|
1404
|
+
retryAfterMs
|
|
1405
|
+
};
|
|
1406
|
+
return {
|
|
1407
|
+
kind: "rejected",
|
|
1408
|
+
error: uploadRejected(status)
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
function unreachable(signal) {
|
|
1412
|
+
return signal.aborted ? {
|
|
1413
|
+
kind: "aborted",
|
|
1414
|
+
error: aborted()
|
|
1415
|
+
} : {
|
|
1416
|
+
kind: "opaque",
|
|
1417
|
+
error: bucketUnreachable(),
|
|
1418
|
+
retryAfterMs: 0
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
async function putObject(fetch, target, body, signal) {
|
|
1422
|
+
let response;
|
|
1423
|
+
try {
|
|
1424
|
+
response = await fetch(target.url, {
|
|
1425
|
+
method: "PUT",
|
|
1426
|
+
headers: target.headers,
|
|
1427
|
+
body,
|
|
1428
|
+
credentials: "omit",
|
|
1429
|
+
signal
|
|
1430
|
+
});
|
|
1431
|
+
} catch {
|
|
1432
|
+
return unreachable(signal);
|
|
1433
|
+
}
|
|
1434
|
+
if (response.ok || response.status === 412) {
|
|
1435
|
+
await response.body?.cancel().catch(() => void 0);
|
|
1436
|
+
return { kind: "stored" };
|
|
1437
|
+
}
|
|
1438
|
+
const text = await response.text().catch(() => "");
|
|
1439
|
+
return classifyAnswer(response.status, text, response.headers.get("retry-after"));
|
|
1440
|
+
}
|
|
1441
|
+
// src/client/transport.ts
|
|
1442
|
+
const STALL_MS = 3e4;
|
|
1443
|
+
const SLOWEST_BYTES_PER_MS = 102.4;
|
|
1444
|
+
const putWithXhr = (target, body, signal, onBytes) => new Promise((resolve) => {
|
|
1445
|
+
if (signal.aborted) return resolve(unreachable(signal));
|
|
1446
|
+
const xhr = new globalThis.XMLHttpRequest();
|
|
1447
|
+
const cancel = () => xhr.abort();
|
|
1448
|
+
let stall;
|
|
1449
|
+
const watch = () => {
|
|
1450
|
+
clearTimeout(stall);
|
|
1451
|
+
stall = setTimeout(cancel, STALL_MS);
|
|
1452
|
+
};
|
|
1453
|
+
const settle = (outcome) => {
|
|
1454
|
+
clearTimeout(stall);
|
|
1455
|
+
signal.removeEventListener("abort", cancel);
|
|
1456
|
+
resolve(outcome);
|
|
1457
|
+
};
|
|
1458
|
+
xhr.upload.onprogress = (event) => {
|
|
1459
|
+
watch();
|
|
1460
|
+
onBytes(event.loaded);
|
|
1461
|
+
};
|
|
1462
|
+
xhr.onload = () => {
|
|
1463
|
+
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 412) {
|
|
1464
|
+
onBytes(body.size);
|
|
1465
|
+
return settle({ kind: "stored" });
|
|
1466
|
+
}
|
|
1467
|
+
if (xhr.status === 0) return settle(unreachable(signal));
|
|
1468
|
+
settle(classifyAnswer(xhr.status, xhr.responseText, xhr.getResponseHeader("retry-after")));
|
|
1469
|
+
};
|
|
1470
|
+
const lost = () => settle(unreachable(signal));
|
|
1471
|
+
xhr.onerror = lost;
|
|
1472
|
+
xhr.ontimeout = lost;
|
|
1473
|
+
xhr.onabort = lost;
|
|
1474
|
+
try {
|
|
1475
|
+
xhr.open("PUT", target.url);
|
|
1476
|
+
for (const [name, value] of Object.entries(target.headers)) xhr.setRequestHeader(name, value);
|
|
1477
|
+
watch();
|
|
1478
|
+
xhr.send(body);
|
|
1479
|
+
} catch {
|
|
1480
|
+
return settle(unreachable(signal));
|
|
1481
|
+
}
|
|
1482
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
1483
|
+
});
|
|
1484
|
+
function createPut(fetchOption, send) {
|
|
1485
|
+
if (fetchOption === void 0 && typeof globalThis.XMLHttpRequest === "function") return putWithXhr;
|
|
1486
|
+
return async (target, body, signal, onBytes) => {
|
|
1487
|
+
const controller = new AbortController();
|
|
1488
|
+
const stop = () => controller.abort();
|
|
1489
|
+
const timeout = setTimeout(stop, STALL_MS + body.size / SLOWEST_BYTES_PER_MS);
|
|
1490
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
1491
|
+
if (signal.aborted) stop();
|
|
1492
|
+
try {
|
|
1493
|
+
const outcome = await putObject(send, target, body, controller.signal);
|
|
1494
|
+
if (outcome.kind === "stored") onBytes(body.size);
|
|
1495
|
+
return outcome.kind === "aborted" ? unreachable(signal) : outcome;
|
|
1496
|
+
} finally {
|
|
1497
|
+
clearTimeout(timeout);
|
|
1498
|
+
signal.removeEventListener("abort", stop);
|
|
1499
|
+
}
|
|
1500
|
+
};
|
|
1501
|
+
}
|
|
1502
|
+
// src/client.ts
|
|
1503
|
+
function oneFile(result) {
|
|
1504
|
+
if (result.error) return fail(result.error);
|
|
1505
|
+
const [outcome] = result.data.files;
|
|
1506
|
+
return ok(outcome.result);
|
|
1507
|
+
}
|
|
1508
|
+
const BASE_PATH = /^(?:https?:\/\/[^/?#\s]+)?(?:\/[^/?#\s]+)+$/;
|
|
1509
|
+
function readOptions(options) {
|
|
1510
|
+
const { basePath = "/api/cloudrop", fetch, concurrency = "auto", order = "added", persist = true, namespace = "" } = options ?? {};
|
|
1511
|
+
if (typeof basePath !== "string" || !BASE_PATH.test(basePath)) throw configError("basePath must be where the cloudrop handler is mounted, such as \"/api/cloudrop\" or \"https://api.example.com/api/cloudrop\": no trailing slash, query or fragment.");
|
|
1512
|
+
if (fetch !== void 0 && typeof fetch !== "function") throw configError("The fetch option must be a function.");
|
|
1513
|
+
if (concurrency !== "auto" && !(Number.isInteger(concurrency) && concurrency >= 1 && concurrency <= 16)) throw configError("concurrency must be \"auto\" or a whole number from 1 to 16.");
|
|
1514
|
+
if (order !== "added" && order !== "smallest-first") throw configError("order must be \"added\" or \"smallest-first\".");
|
|
1515
|
+
if (typeof persist !== "boolean") throw configError("persist must be true or false.");
|
|
1516
|
+
if (typeof namespace !== "string" || namespace.length > 256) throw configError("namespace must be a string of at most 256 characters, such as the signed-in user's id.");
|
|
1517
|
+
if (typeof (fetch ?? globalThis.fetch) !== "function") throw configError("No fetch is available here. Pass createCloudropClient({ fetch }).");
|
|
1518
|
+
return {
|
|
1519
|
+
basePath,
|
|
1520
|
+
fetch,
|
|
1521
|
+
concurrency,
|
|
1522
|
+
order,
|
|
1523
|
+
persist,
|
|
1524
|
+
namespace
|
|
1525
|
+
};
|
|
1526
|
+
}
|
|
1527
|
+
function createCloudropClient(options = {}) {
|
|
1528
|
+
const { basePath, fetch, concurrency, order, persist, namespace } = readOptions(options);
|
|
1529
|
+
const send = fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
1530
|
+
let rank = 0;
|
|
1531
|
+
const context = {
|
|
1532
|
+
endpoints: createEndpoints(basePath, send),
|
|
1533
|
+
put: createPut(fetch, send),
|
|
1534
|
+
pool: createPool(concurrency),
|
|
1535
|
+
order,
|
|
1536
|
+
nextRank: () => ++rank,
|
|
1537
|
+
leftovers: createLeftovers((held) => giveUp(context, held)),
|
|
1538
|
+
keepFailed: true,
|
|
1539
|
+
records: createRecords(persist),
|
|
1540
|
+
namespace,
|
|
1541
|
+
claims: /* @__PURE__ */ new Set()
|
|
1542
|
+
};
|
|
1543
|
+
function upload(route, files, options) {
|
|
1544
|
+
const { done } = startJob({
|
|
1545
|
+
...context,
|
|
1546
|
+
keepFailed: false
|
|
1547
|
+
}, route, files, options);
|
|
1548
|
+
return isList(files) ? done : done.then(oneFile);
|
|
1549
|
+
}
|
|
1550
|
+
return {
|
|
1551
|
+
start: (route, files, ...rest) => startJob(context, route, files, rest[0]),
|
|
1552
|
+
upload,
|
|
1553
|
+
resumable: (route) => resumable(context, route),
|
|
1554
|
+
resume: (upload, file, options) => resumeJob(context, upload, file, options),
|
|
1555
|
+
discard: (upload) => discard(context, upload),
|
|
1556
|
+
forget: (options) => forget(context, options)
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1559
|
+
export { isList as n, createCloudropClient as t };
|
|
1560
|
+
|
|
1561
|
+
//# sourceMappingURL=client-Y1zUEbCj.js.map
|