cloudrop 0.1.0 → 0.2.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 +80 -0
- package/README.md +11 -3
- package/dist/adapters/drizzle.d.ts +1 -1
- package/dist/adapters/drizzle.js +3 -3
- package/dist/adapters/drizzle.js.map +1 -1
- package/dist/adapters/prisma.d.ts +1 -1
- package/dist/adapters/prisma.js +1 -1
- package/dist/adapters/sql.d.ts +1 -1
- package/dist/adapters/sql.js +1 -1
- package/dist/{client-Dj6ON4MN.d.ts → client-CP_ajLF4.d.ts} +43 -25
- package/dist/{client-Y1zUEbCj.js → client-ePVHwvMv.js} +126 -40
- package/dist/client-ePVHwvMv.js.map +1 -0
- package/dist/client.d.ts +3 -3
- package/dist/client.js +1 -1
- package/dist/errors-CUxMmFAe.js.map +1 -1
- package/dist/index.d.ts +67 -6
- package/dist/index.js +357 -114
- package/dist/index.js.map +1 -1
- package/dist/next.d.ts +1 -1
- package/dist/node.d.ts +1 -1
- package/dist/react.d.ts +18 -4
- package/dist/react.js +23 -3
- package/dist/react.js.map +1 -1
- package/dist/{route-CspI1Ypt.js → route-C8-YjzdY.js} +3 -2
- package/dist/{route-CspI1Ypt.js.map → route-C8-YjzdY.js.map} +1 -1
- package/dist/{store-BfCJejGh.d.ts → store-B539nY3J.d.ts} +41 -3
- package/dist/{transactional-DnvnGsVH.js → transactional-CiNwMiDb.js} +215 -105
- package/dist/transactional-CiNwMiDb.js.map +1 -0
- package/dist/{types-BrAx3dA4.d.ts → types-CFfMw-n_.d.ts} +1 -1
- package/package.json +7 -7
- package/dist/client-Y1zUEbCj.js.map +0 -1
- package/dist/transactional-DnvnGsVH.js.map +0 -1
|
@@ -34,6 +34,39 @@ function readArguments(route, files, options) {
|
|
|
34
34
|
signal
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
+
// src/client/close.ts
|
|
38
|
+
const createLifetime = () => ({
|
|
39
|
+
closed: null,
|
|
40
|
+
jobs: /* @__PURE__ */ new Set(),
|
|
41
|
+
aborts: /* @__PURE__ */ new Set()
|
|
42
|
+
});
|
|
43
|
+
function counted({ lifetime }, sent) {
|
|
44
|
+
lifetime.aborts.add(sent);
|
|
45
|
+
sent.finally(() => lifetime.aborts.delete(sent));
|
|
46
|
+
return sent;
|
|
47
|
+
}
|
|
48
|
+
function closeClient({ lifetime }, abort) {
|
|
49
|
+
if (lifetime.closed) return lifetime.closed.stopped;
|
|
50
|
+
let resolve;
|
|
51
|
+
const promise = new Promise((settle) => {
|
|
52
|
+
resolve = settle;
|
|
53
|
+
});
|
|
54
|
+
lifetime.closed = {
|
|
55
|
+
abort,
|
|
56
|
+
stopped: promise
|
|
57
|
+
};
|
|
58
|
+
const jobs = [...lifetime.jobs];
|
|
59
|
+
for (const job of jobs) job.abort();
|
|
60
|
+
const runs = jobs.flatMap((job) => job.entries.map((entry) => entry.run));
|
|
61
|
+
resolve(Promise.allSettled(runs).then(() => Promise.allSettled(lifetime.aborts)).then(() => void 0));
|
|
62
|
+
return promise;
|
|
63
|
+
}
|
|
64
|
+
function keptForResume(context, upload) {
|
|
65
|
+
if (!(context.lifetime.closed?.abort === false) || upload.ticket.kind !== "multipart" || !context.records.enabled) return false;
|
|
66
|
+
upload.dropped = true;
|
|
67
|
+
upload.unlock();
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
37
70
|
// src/client/failures.ts
|
|
38
71
|
const NEEDS_NEW_TICKET = /* @__PURE__ */ new Set([
|
|
39
72
|
"UPLOAD_EXPIRED",
|
|
@@ -51,6 +84,7 @@ const withFile = (error, { id, file }) => new CloudropError(error.code, error.me
|
|
|
51
84
|
cause: error
|
|
52
85
|
});
|
|
53
86
|
const aborted = () => new CloudropError("ABORTED", "The upload was cancelled. Start it again to upload the file.");
|
|
87
|
+
const clientClosed = () => new CloudropError("CLIENT_CLOSED", "This upload client was closed, so it starts no more uploads. Use a new client, such as the one of the user who is signed in now.");
|
|
54
88
|
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
89
|
const notResumable = () => new CloudropError("NOT_FOUND", "This upload can no longer be resumed: it finished, was cancelled or expired. Start it again.");
|
|
56
90
|
const notSameFile = () => new CloudropError("INVALID_INPUT", "This is not the file the upload started with. Pick the same file again to resume it.");
|
|
@@ -58,6 +92,10 @@ const notListed = () => new CloudropError("NOT_FOUND", "This upload is no longer
|
|
|
58
92
|
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
93
|
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
94
|
const serverUnreachable = () => new CloudropError("NETWORK_ERROR", "The upload server could not be reached. Check the connection, then try again.", { retryable: true });
|
|
95
|
+
const headersFailed = (cause) => new CloudropError("NETWORK_ERROR", "The headers option gave no headers for a request to the upload server, so it was not sent. Check the function passed as headers, then try again.", {
|
|
96
|
+
retryable: true,
|
|
97
|
+
cause
|
|
98
|
+
});
|
|
61
99
|
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
100
|
const serverBusy = () => new CloudropError("RATE_LIMITED", "The upload server is busy. Try again in a moment.", { retryable: true });
|
|
63
101
|
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 });
|
|
@@ -324,15 +362,16 @@ async function sendParts(sender, partNumbers) {
|
|
|
324
362
|
};
|
|
325
363
|
const cancel = () => run.stop.abort();
|
|
326
364
|
sender.signal.addEventListener("abort", cancel, { once: true });
|
|
327
|
-
const queue =
|
|
365
|
+
const queue = partNumbers.values();
|
|
328
366
|
const worker = async () => {
|
|
329
|
-
for (
|
|
367
|
+
for (const next of queue) {
|
|
368
|
+
if (run.failure) return;
|
|
330
369
|
const error = await sendPart(run, next);
|
|
331
370
|
run.failure ??= error;
|
|
332
371
|
if (run.failure) run.stop.abort();
|
|
333
372
|
}
|
|
334
373
|
};
|
|
335
|
-
await Promise.all(Array.from({ length: Math.min(PARTS_IN_FLIGHT,
|
|
374
|
+
await Promise.all(Array.from({ length: Math.min(PARTS_IN_FLIGHT, partNumbers.length) }, worker));
|
|
336
375
|
sender.signal.removeEventListener("abort", cancel);
|
|
337
376
|
return run.failure ? fail(run.failure) : ok(void 0);
|
|
338
377
|
}
|
|
@@ -559,33 +598,53 @@ function parseJson(text) {
|
|
|
559
598
|
return;
|
|
560
599
|
}
|
|
561
600
|
}
|
|
601
|
+
const cancelled = () => /* @__PURE__ */ new Error("The request was cancelled or timed out before it was sent.");
|
|
602
|
+
const rejectOnAbort = (signal) => new Promise((_, reject) => {
|
|
603
|
+
signal.addEventListener("abort", () => reject(cancelled()), { once: true });
|
|
604
|
+
if (signal.aborted) reject(cancelled());
|
|
605
|
+
});
|
|
606
|
+
const jsonHeaders = (given) => ({
|
|
607
|
+
...Object.fromEntries(new Headers(given)),
|
|
608
|
+
"content-type": "application/json"
|
|
609
|
+
});
|
|
562
610
|
const refusal = (status) => status === 429 ? serverBusy() : unreadableAnswer(status);
|
|
563
611
|
const toError = (body) => new CloudropError(body.code, body.message, body);
|
|
564
|
-
function createEndpoints(basePath, fetch) {
|
|
565
|
-
|
|
612
|
+
function createEndpoints(basePath, fetch, serverHeaders) {
|
|
613
|
+
const fixedHeaders = jsonHeaders(typeof serverHeaders === "function" ? void 0 : serverHeaders);
|
|
614
|
+
async function send(endpoint, body, { signal, keepalive, timeoutMs = ASK_TIMEOUT_MS }) {
|
|
566
615
|
const controller = new AbortController();
|
|
567
616
|
const stop = () => controller.abort();
|
|
568
617
|
const timeout = setTimeout(stop, timeoutMs);
|
|
569
618
|
signal?.addEventListener("abort", stop, { once: true });
|
|
570
619
|
if (signal?.aborted) stop();
|
|
571
|
-
let
|
|
572
|
-
let text;
|
|
620
|
+
let headers;
|
|
573
621
|
try {
|
|
574
|
-
|
|
622
|
+
if (controller.signal.aborted) throw cancelled();
|
|
623
|
+
headers = typeof serverHeaders === "function" ? jsonHeaders(await Promise.race([serverHeaders(), rejectOnAbort(controller.signal)])) : fixedHeaders;
|
|
624
|
+
const response = await fetch(`${basePath}/${endpoint}`, {
|
|
575
625
|
method: "POST",
|
|
576
|
-
headers
|
|
626
|
+
headers,
|
|
577
627
|
body: JSON.stringify(body),
|
|
578
628
|
credentials: "include",
|
|
579
629
|
signal: controller.signal,
|
|
580
630
|
keepalive
|
|
581
631
|
});
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
632
|
+
return ok({
|
|
633
|
+
response,
|
|
634
|
+
text: await response.text()
|
|
635
|
+
});
|
|
636
|
+
} catch (cause) {
|
|
637
|
+
if (signal?.aborted) return fail(aborted());
|
|
638
|
+
return fail(headers ? serverUnreachable() : headersFailed(cause));
|
|
585
639
|
} finally {
|
|
586
640
|
clearTimeout(timeout);
|
|
587
641
|
signal?.removeEventListener("abort", stop);
|
|
588
642
|
}
|
|
643
|
+
}
|
|
644
|
+
async function post(endpoint, body, options, read) {
|
|
645
|
+
const sent = await send(endpoint, body, options);
|
|
646
|
+
if (sent.error) return sent;
|
|
647
|
+
const { response, text } = sent.data;
|
|
589
648
|
const json = parseJson(text);
|
|
590
649
|
const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"));
|
|
591
650
|
if (!response.ok) {
|
|
@@ -791,7 +850,7 @@ function letGo(context, upload, answered) {
|
|
|
791
850
|
// src/client/file-run.ts
|
|
792
851
|
function drop(context, upload) {
|
|
793
852
|
upload.dropped = true;
|
|
794
|
-
const answered = context.endpoints.abort(upload.ticket.token).then(() => void 0);
|
|
853
|
+
const answered = counted(context, context.endpoints.abort(upload.ticket.token).then(() => void 0));
|
|
795
854
|
letGo(context, upload, answered);
|
|
796
855
|
return answered;
|
|
797
856
|
}
|
|
@@ -811,7 +870,7 @@ function abortFile(context, entry) {
|
|
|
811
870
|
}
|
|
812
871
|
context.leftovers.release(entry);
|
|
813
872
|
settle(entry, "aborted", withFile(aborted(), entry));
|
|
814
|
-
if (!upload || upload.dropped) return;
|
|
873
|
+
if (!upload || upload.dropped || keptForResume(context, upload)) return;
|
|
815
874
|
if (upload.mayBeComplete) letGo(context, upload);
|
|
816
875
|
else drop(context, upload);
|
|
817
876
|
}
|
|
@@ -828,6 +887,7 @@ async function end(context, upload, error) {
|
|
|
828
887
|
else giveUp(context, upload);
|
|
829
888
|
return;
|
|
830
889
|
}
|
|
890
|
+
if (keptForResume(context, upload)) return;
|
|
831
891
|
if (mayBeComplete) letGo(context, upload);
|
|
832
892
|
else await drop(context, upload);
|
|
833
893
|
}
|
|
@@ -873,7 +933,7 @@ async function refused(job, entry, refusal, attempt) {
|
|
|
873
933
|
async function runEntry(job, entry, answer, index, whole, attempt) {
|
|
874
934
|
if (entry.status === "aborted") {
|
|
875
935
|
const late = answer.error ? void 0 : answer.data[index];
|
|
876
|
-
if (late && !(late instanceof CloudropError)) job.context.endpoints.abort(late.token);
|
|
936
|
+
if (late && !(late instanceof CloudropError)) counted(job.context, job.context.endpoints.abort(late.token));
|
|
877
937
|
return;
|
|
878
938
|
}
|
|
879
939
|
if (answer.error) return settle(entry, "failed", whole ? answer.error : withFile(answer.error, entry));
|
|
@@ -960,6 +1020,7 @@ function createLeftovers(giveUp) {
|
|
|
960
1020
|
}
|
|
961
1021
|
// src/client/job-run.ts
|
|
962
1022
|
const settledStatus = ({ status }) => status === "done" || status === "failed" || status === "aborted";
|
|
1023
|
+
const ended = (entries, keepFailed) => entries.every(({ status }) => status === "done" || status === "aborted" || status === "failed" && !keepFailed);
|
|
963
1024
|
const busy = (entry) => !settledStatus(entry) || entry.status === "failed" && !!entry.upload && !entry.upload.dropped && !entry.upload.mayBeComplete;
|
|
964
1025
|
function createJob(context, args, entries) {
|
|
965
1026
|
const controller = new AbortController();
|
|
@@ -967,6 +1028,7 @@ function createJob(context, args, entries) {
|
|
|
967
1028
|
const abort = () => controller.abort();
|
|
968
1029
|
const idle = () => {
|
|
969
1030
|
if (!entries.some(busy)) args.signal.removeEventListener("abort", abort);
|
|
1031
|
+
if (ended(entries, context.keepFailed)) context.lifetime.jobs.delete(job);
|
|
970
1032
|
};
|
|
971
1033
|
signal.addEventListener("abort", () => {
|
|
972
1034
|
for (const entry of entries) abortFile(context, entry);
|
|
@@ -1001,6 +1063,7 @@ function createJob(context, args, entries) {
|
|
|
1001
1063
|
},
|
|
1002
1064
|
idle
|
|
1003
1065
|
};
|
|
1066
|
+
context.lifetime.jobs.add(job);
|
|
1004
1067
|
return job;
|
|
1005
1068
|
}
|
|
1006
1069
|
function forgetTicket(context, entry) {
|
|
@@ -1170,7 +1233,8 @@ function handleOf(job, entry, retry) {
|
|
|
1170
1233
|
function startJob(context, route, files, options) {
|
|
1171
1234
|
return createUploadJob(context, readArguments(route, files, options), firstRun);
|
|
1172
1235
|
}
|
|
1173
|
-
function createUploadJob(context,
|
|
1236
|
+
function createUploadJob(context, given, run, retry = retryFiles) {
|
|
1237
|
+
const args = context.lifetime.closed ? fail(clientClosed()) : given;
|
|
1174
1238
|
const entries = (args.error ? [] : args.data.files).map((file) => createEntry(file, context.nextRank(), (urgent) => store.changed(urgent)));
|
|
1175
1239
|
const store = createStore(() => snapshotOf(entries), () => entries.some((entry) => entry.status === "uploading"));
|
|
1176
1240
|
const job = args.error ? null : createJob(context, args.data, entries);
|
|
@@ -1192,6 +1256,45 @@ function createUploadJob(context, args, run, retry = retryFiles) {
|
|
|
1192
1256
|
getSnapshot: store.get
|
|
1193
1257
|
};
|
|
1194
1258
|
}
|
|
1259
|
+
// src/client/options.ts
|
|
1260
|
+
const BASE_PATH = /^(?:https?:\/\/[^/?#\s]+)?(?:\/[^/?#\s]+)+$/;
|
|
1261
|
+
function isHeaders(headers) {
|
|
1262
|
+
if (headers === void 0 || typeof headers === "function") return true;
|
|
1263
|
+
if (typeof headers !== "object" || headers === null) return false;
|
|
1264
|
+
try {
|
|
1265
|
+
new Headers(headers);
|
|
1266
|
+
return true;
|
|
1267
|
+
} catch {
|
|
1268
|
+
return false;
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
function readConnection({ basePath = "/api/cloudrop", headers, fetch }) {
|
|
1272
|
+
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.");
|
|
1273
|
+
if (!isHeaders(headers)) throw configError("headers must be the headers for the upload server, such as { authorization: \"Bearer …\" }, or a function that returns them.");
|
|
1274
|
+
if (fetch !== void 0 && typeof fetch !== "function") throw configError("The fetch option must be a function.");
|
|
1275
|
+
if (typeof (fetch ?? globalThis.fetch) !== "function") throw configError("No fetch is available here. Pass createCloudropClient({ fetch }).");
|
|
1276
|
+
return {
|
|
1277
|
+
basePath,
|
|
1278
|
+
headers,
|
|
1279
|
+
fetch
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
function readOptions(options) {
|
|
1283
|
+
const given = options ?? {};
|
|
1284
|
+
const { concurrency = "auto", order = "added", persist = true, namespace = "" } = given;
|
|
1285
|
+
const connection = readConnection(given);
|
|
1286
|
+
if (concurrency !== "auto" && !(Number.isInteger(concurrency) && concurrency >= 1 && concurrency <= 16)) throw configError("concurrency must be \"auto\" or a whole number from 1 to 16.");
|
|
1287
|
+
if (order !== "added" && order !== "smallest-first") throw configError("order must be \"added\" or \"smallest-first\".");
|
|
1288
|
+
if (typeof persist !== "boolean") throw configError("persist must be true or false.");
|
|
1289
|
+
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.");
|
|
1290
|
+
return {
|
|
1291
|
+
...connection,
|
|
1292
|
+
concurrency,
|
|
1293
|
+
order,
|
|
1294
|
+
persist,
|
|
1295
|
+
namespace
|
|
1296
|
+
};
|
|
1297
|
+
}
|
|
1195
1298
|
// src/client/pool.ts
|
|
1196
1299
|
const AUTO_START = 4;
|
|
1197
1300
|
const AUTO_MAX = 8;
|
|
@@ -1505,31 +1608,12 @@ function oneFile(result) {
|
|
|
1505
1608
|
const [outcome] = result.data.files;
|
|
1506
1609
|
return ok(outcome.result);
|
|
1507
1610
|
}
|
|
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
1611
|
function createCloudropClient(options = {}) {
|
|
1528
|
-
const { basePath, fetch, concurrency, order, persist, namespace } = readOptions(options);
|
|
1612
|
+
const { basePath, headers, fetch, concurrency, order, persist, namespace } = readOptions(options);
|
|
1529
1613
|
const send = fetch ?? ((url, init) => globalThis.fetch(url, init));
|
|
1530
1614
|
let rank = 0;
|
|
1531
1615
|
const context = {
|
|
1532
|
-
endpoints: createEndpoints(basePath, send),
|
|
1616
|
+
endpoints: createEndpoints(basePath, send, headers),
|
|
1533
1617
|
put: createPut(fetch, send),
|
|
1534
1618
|
pool: createPool(concurrency),
|
|
1535
1619
|
order,
|
|
@@ -1538,7 +1622,8 @@ function createCloudropClient(options = {}) {
|
|
|
1538
1622
|
keepFailed: true,
|
|
1539
1623
|
records: createRecords(persist),
|
|
1540
1624
|
namespace,
|
|
1541
|
-
claims: /* @__PURE__ */ new Set()
|
|
1625
|
+
claims: /* @__PURE__ */ new Set(),
|
|
1626
|
+
lifetime: createLifetime()
|
|
1542
1627
|
};
|
|
1543
1628
|
function upload(route, files, options) {
|
|
1544
1629
|
const { done } = startJob({
|
|
@@ -1553,9 +1638,10 @@ function createCloudropClient(options = {}) {
|
|
|
1553
1638
|
resumable: (route) => resumable(context, route),
|
|
1554
1639
|
resume: (upload, file, options) => resumeJob(context, upload, file, options),
|
|
1555
1640
|
discard: (upload) => discard(context, upload),
|
|
1556
|
-
forget: (options) => forget(context, options)
|
|
1641
|
+
forget: (options) => forget(context, options),
|
|
1642
|
+
close: (options) => closeClient(context, options?.abort === true)
|
|
1557
1643
|
};
|
|
1558
1644
|
}
|
|
1559
1645
|
export { isList as n, createCloudropClient as t };
|
|
1560
1646
|
|
|
1561
|
-
//# sourceMappingURL=client-
|
|
1647
|
+
//# sourceMappingURL=client-ePVHwvMv.js.map
|