cloudrop 0.1.1 → 0.3.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +115 -0
  2. package/README.md +34 -4
  3. package/dist/adapters/drizzle.d.ts +1 -1
  4. package/dist/adapters/drizzle.js +3 -3
  5. package/dist/adapters/drizzle.js.map +1 -1
  6. package/dist/adapters/prisma.d.ts +1 -1
  7. package/dist/adapters/prisma.js +1 -1
  8. package/dist/adapters/sql.d.ts +4 -1
  9. package/dist/adapters/sql.js +8 -1
  10. package/dist/adapters/sql.js.map +1 -1
  11. package/dist/cli.js +2045 -0
  12. package/dist/{client-Dj6ON4MN.d.ts → client-CP_ajLF4.d.ts} +43 -25
  13. package/dist/{client-Y1zUEbCj.js → client-DxP3uv8V.js} +172 -44
  14. package/dist/client-DxP3uv8V.js.map +1 -0
  15. package/dist/client.d.ts +3 -3
  16. package/dist/client.js +1 -1
  17. package/dist/errors-CUxMmFAe.js.map +1 -1
  18. package/dist/index.d.ts +92 -7
  19. package/dist/index.js +432 -152
  20. package/dist/index.js.map +1 -1
  21. package/dist/next.d.ts +1 -1
  22. package/dist/node.d.ts +1 -1
  23. package/dist/react.d.ts +18 -4
  24. package/dist/react.js +23 -3
  25. package/dist/react.js.map +1 -1
  26. package/dist/{route-CspI1Ypt.js → route-C8-YjzdY.js} +3 -2
  27. package/dist/{route-CspI1Ypt.js.map → route-C8-YjzdY.js.map} +1 -1
  28. package/dist/{store-BfCJejGh.d.ts → store-B539nY3J.d.ts} +41 -3
  29. package/dist/testing.d.ts +81 -0
  30. package/dist/testing.js +1243 -0
  31. package/dist/testing.js.map +1 -0
  32. package/dist/{transactional-DnvnGsVH.js → transactional-CiNwMiDb.js} +215 -105
  33. package/dist/transactional-CiNwMiDb.js.map +1 -0
  34. package/dist/{types-BrAx3dA4.d.ts → types-CFfMw-n_.d.ts} +1 -1
  35. package/docs/README.md +30 -0
  36. package/docs/bucket-setup.md +226 -0
  37. package/docs/client.md +386 -0
  38. package/docs/command.md +129 -0
  39. package/docs/deployment.md +169 -0
  40. package/docs/errors.md +150 -0
  41. package/docs/hono.md +276 -0
  42. package/docs/react.md +508 -0
  43. package/docs/resume.md +253 -0
  44. package/docs/server.md +603 -0
  45. package/docs/store.md +804 -0
  46. package/docs/testing-your-app.md +328 -0
  47. package/docs/troubleshooting.md +176 -0
  48. package/package.json +16 -8
  49. package/dist/client-Y1zUEbCj.js.map +0 -1
  50. package/dist/transactional-DnvnGsVH.js.map +0 -1
@@ -1,6 +1,36 @@
1
- import { E as CloudropError, _ as RouteResult, g as RouteName, h as RouteMetadata, i as CompleteResponse, k as Result, u as InferableCloudrop } from "./types-BrAx3dA4.js";
1
+ import { E as CloudropError, _ as RouteResult, g as RouteName, h as RouteMetadata, i as CompleteResponse, k as Result, u as InferableCloudrop } from "./types-CFfMw-n_.js";
2
2
  /** How the client sends every request: the endpoints' POSTs and the bucket's PUTs. */
3
3
  type ClientFetch = (url: string, init: RequestInit) => Promise<Response>;
4
+ /** Headers for the upload server only, or a function, possibly async, that gives them before each request (M8-L5). */
5
+ type ServerHeaders = HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
6
+ interface ClientOptions {
7
+ /** Where the server's handler is mounted: a path on this page's origin or an absolute URL. Default "/api/cloudrop". */
8
+ basePath?: string;
9
+ /**
10
+ * Headers for the requests to the upload server (/upload, /sign, /complete and /abort), never for bucket PUTs: an
11
+ * object, or a function, possibly async, called before each request, such as one that returns a fresh bearer token.
12
+ */
13
+ headers?: ServerHeaders;
14
+ /** Replaces the global fetch, for tests and runtimes that bring their own. Bucket PUTs then go through it too. */
15
+ fetch?: ClientFetch;
16
+ /**
17
+ * How many bucket PUTs this client sends at once, across all its uploads: 1 to 16, or "auto" (the default), which
18
+ * starts at 4, adds one while the bytes sent keep rising, up to 8, and halves on a network error or a busy bucket.
19
+ */
20
+ concurrency?: number | "auto";
21
+ /** Which PUTs go first when they wait for a connection: "added" (the default) or "smallest-first" by file size. */
22
+ order?: "added" | "smallest-first";
23
+ /**
24
+ * Keeps each unfinished multipart upload in the browser's IndexedDB, with its upload token, until the file ends, so
25
+ * that it can resume after a reload. Default true; false keeps nothing on disk.
26
+ */
27
+ persist?: boolean;
28
+ /**
29
+ * Keeps one signed-in user's unfinished uploads apart from another's in the same browser: pass the user's id. At most
30
+ * 256 characters; default "".
31
+ */
32
+ namespace?: string;
33
+ }
4
34
  /** Where one file of a job stands. `done`, `failed` and `aborted` are final. */
5
35
  type FileStatus = "queued" | "uploading" | "paused" | "completing" | "done" | "failed" | "aborted";
6
36
  /** One file as a page renders it (spec §2.1). */
@@ -146,29 +176,6 @@ interface ResumableUpload<TName extends string = string> {
146
176
  /** ISO 8601: when its token expires. A minute before that it is no longer listed. */
147
177
  expiresAt: string;
148
178
  }
149
- interface ClientOptions {
150
- /** Where the server's handler is mounted: a path on this page's origin or an absolute URL. Default "/api/cloudrop". */
151
- basePath?: string;
152
- /** Replaces the global fetch, for tests and runtimes that bring their own. */
153
- fetch?: ClientFetch;
154
- /**
155
- * How many bucket PUTs this client sends at once, across all its uploads: 1 to 16, or "auto" (the default), which
156
- * starts at 4, adds one while the bytes sent keep rising, up to 8, and halves on a network error or a busy bucket.
157
- */
158
- concurrency?: number | "auto";
159
- /** Which PUTs go first when they wait for a connection: "added" (the default) or "smallest-first" by file size. */
160
- order?: "added" | "smallest-first";
161
- /**
162
- * Keeps each unfinished multipart upload in the browser's IndexedDB, with its upload token, until the file ends, so
163
- * that it can resume after a reload. Default true; false keeps nothing on disk.
164
- */
165
- persist?: boolean;
166
- /**
167
- * Keeps one signed-in user's unfinished uploads apart from another's in the same browser: pass the user's id. At most
168
- * 256 characters; default "".
169
- */
170
- namespace?: string;
171
- }
172
179
  interface UploadOptions<TMetadata = unknown> {
173
180
  /** Validated by the route's schema on the server. */
174
181
  metadata?: TMetadata;
@@ -209,6 +216,17 @@ interface CloudropClient<TCloudrop extends InferableCloudrop = InferableCloudrop
209
216
  }): Promise<Result<{
210
217
  forgotten: number;
211
218
  }>>;
219
+ /**
220
+ * Stops the client for good, in any phase and as often as called: start(), upload() and resume() are refused with
221
+ * CLIENT_CLOSED, and every file of every route stops. A file that may have completed is kept. Without options, as
222
+ * when another user signed in in another tab, each multipart upload keeps its record and gets no /abort, so its user
223
+ * can resume it on a new client; `{ abort: true }`, at sign-out, cancels every file as job.abort() does, telling the
224
+ * server. The first call decides; resumable(), discard() and forget() keep working. Resolves once every file it
225
+ * stopped has ended and each /abort it sent was answered, also for every later call; never rejects.
226
+ */
227
+ close(options?: {
228
+ abort: true;
229
+ }): Promise<void>;
212
230
  }
213
231
  declare function createCloudropClient<TCloudrop extends InferableCloudrop = InferableCloudrop>(options?: ClientOptions): CloudropClient<TCloudrop>;
214
- export { createCloudropClient as a, FileSnapshot as c, ResumableUpload as d, UploadJob as f, UploadOptions as i, FileStatus as l, CloudropClient as n, FileHandle as o, UploadResult as p, UploadArguments as r, FileOutcome as s, ClientOptions as t, JobSnapshot as u };
232
+ export { FileHandle as a, FileStatus as c, UploadJob as d, UploadResult as f, createCloudropClient as i, JobSnapshot as l, UploadArguments as n, FileOutcome as o, ClientOptions as p, UploadOptions as r, FileSnapshot as s, CloudropClient as t, ResumableUpload as u };
@@ -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,12 +92,18 @@ 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 });
64
102
  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
103
  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
104
  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.`);
105
+ const uploadExpired = () => new CloudropError("UPLOAD_EXPIRED", "This upload expired. Start it again to upload the file.");
106
+ const uploadGone = () => new CloudropError("NO_SUCH_UPLOAD", "This upload was aborted or has expired. Start it again.");
67
107
  const storageBusy = () => new CloudropError("RATE_LIMITED", "The storage service is busy. Try again in a moment.", { retryable: true });
68
108
  const storageDown = () => new CloudropError("STORAGE_ERROR", "The storage service failed. Try again in a moment.", { retryable: true });
69
109
  // src/client/locks.ts
@@ -324,20 +364,23 @@ async function sendParts(sender, partNumbers) {
324
364
  };
325
365
  const cancel = () => run.stop.abort();
326
366
  sender.signal.addEventListener("abort", cancel, { once: true });
327
- const queue = [...partNumbers];
367
+ const queue = partNumbers.values();
328
368
  const worker = async () => {
329
- for (let next = queue.shift(); next !== void 0 && run.failure === null; next = queue.shift()) {
369
+ for (const next of queue) {
370
+ if (run.failure) return;
330
371
  const error = await sendPart(run, next);
331
372
  run.failure ??= error;
332
373
  if (run.failure) run.stop.abort();
333
374
  }
334
375
  };
335
- await Promise.all(Array.from({ length: Math.min(PARTS_IN_FLIGHT, queue.length) }, worker));
376
+ await Promise.all(Array.from({ length: Math.min(PARTS_IN_FLIGHT, partNumbers.length) }, worker));
336
377
  sender.signal.removeEventListener("abort", cancel);
337
378
  return run.failure ? fail(run.failure) : ok(void 0);
338
379
  }
339
380
  // src/client/upload.ts
340
381
  const REPAIR_ROUNDS = 2;
382
+ const SIGNING_MARGIN_MS = 9e5;
383
+ const canSign = (ticket) => Date.parse(ticket.expiresAt) - SIGNING_MARGIN_MS > Date.now();
341
384
  function claim({ claims }, id) {
342
385
  const made = { id };
343
386
  claims.add(made);
@@ -421,7 +464,8 @@ function senderFor(context, { ticket, entry, urls }, signal) {
421
464
  };
422
465
  }
423
466
  function probed(upload, done, missing) {
424
- if (done.error && !missing) return;
467
+ if (!upload.probe || done.error && !missing) return null;
468
+ if (missing?.length && !canSign(upload.ticket)) return uploadExpired();
425
469
  const { entry, ticket } = upload;
426
470
  upload.probe = false;
427
471
  const { partSize } = ticket;
@@ -431,6 +475,7 @@ function probed(upload, done, missing) {
431
475
  if (!lacking.has(part)) entry.progress.sent(part, size, size);
432
476
  }
433
477
  if (missing) upload.repairs--;
478
+ return null;
434
479
  }
435
480
  async function sendAndComplete(context, upload, signal) {
436
481
  const { ticket, entry } = upload;
@@ -450,7 +495,8 @@ async function sendAndComplete(context, upload, signal) {
450
495
  moveTo(entry, "completing");
451
496
  const done = await withRetry(complete, entry.cancel.signal);
452
497
  const missing = done.error?.code === "PARTS_MISSING" ? done.error.parts : void 0;
453
- if (upload.probe) probed(upload, done, missing);
498
+ const gone = probed(upload, done, missing);
499
+ if (gone) return fail(gone);
454
500
  if (!missing?.length || upload.repairs === REPAIR_ROUNDS || entry.cancel.signal.aborted) return done;
455
501
  upload.repairs++;
456
502
  for (const partNumber of missing) entry.landed.delete(partNumber);
@@ -559,33 +605,53 @@ function parseJson(text) {
559
605
  return;
560
606
  }
561
607
  }
608
+ const cancelled = () => /* @__PURE__ */ new Error("The request was cancelled or timed out before it was sent.");
609
+ const rejectOnAbort = (signal) => new Promise((_, reject) => {
610
+ signal.addEventListener("abort", () => reject(cancelled()), { once: true });
611
+ if (signal.aborted) reject(cancelled());
612
+ });
613
+ const jsonHeaders = (given) => ({
614
+ ...Object.fromEntries(new Headers(given)),
615
+ "content-type": "application/json"
616
+ });
562
617
  const refusal = (status) => status === 429 ? serverBusy() : unreadableAnswer(status);
563
618
  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) {
619
+ function createEndpoints(basePath, fetch, serverHeaders) {
620
+ const fixedHeaders = jsonHeaders(typeof serverHeaders === "function" ? void 0 : serverHeaders);
621
+ async function send(endpoint, body, { signal, keepalive, timeoutMs = ASK_TIMEOUT_MS }) {
566
622
  const controller = new AbortController();
567
623
  const stop = () => controller.abort();
568
624
  const timeout = setTimeout(stop, timeoutMs);
569
625
  signal?.addEventListener("abort", stop, { once: true });
570
626
  if (signal?.aborted) stop();
571
- let response;
572
- let text;
627
+ let headers;
573
628
  try {
574
- response = await fetch(`${basePath}/${endpoint}`, {
629
+ if (controller.signal.aborted) throw cancelled();
630
+ headers = typeof serverHeaders === "function" ? jsonHeaders(await Promise.race([serverHeaders(), rejectOnAbort(controller.signal)])) : fixedHeaders;
631
+ const response = await fetch(`${basePath}/${endpoint}`, {
575
632
  method: "POST",
576
- headers: { "content-type": "application/json" },
633
+ headers,
577
634
  body: JSON.stringify(body),
578
635
  credentials: "include",
579
636
  signal: controller.signal,
580
637
  keepalive
581
638
  });
582
- text = await response.text();
583
- } catch {
584
- return fail(signal?.aborted ? aborted() : serverUnreachable());
639
+ return ok({
640
+ response,
641
+ text: await response.text()
642
+ });
643
+ } catch (cause) {
644
+ if (signal?.aborted) return fail(aborted());
645
+ return fail(headers ? serverUnreachable() : headersFailed(cause));
585
646
  } finally {
586
647
  clearTimeout(timeout);
587
648
  signal?.removeEventListener("abort", stop);
588
649
  }
650
+ }
651
+ async function post(endpoint, body, options, read) {
652
+ const sent = await send(endpoint, body, options);
653
+ if (sent.error) return sent;
654
+ const { response, text } = sent.data;
589
655
  const json = parseJson(text);
590
656
  const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"));
591
657
  if (!response.ok) {
@@ -791,7 +857,7 @@ function letGo(context, upload, answered) {
791
857
  // src/client/file-run.ts
792
858
  function drop(context, upload) {
793
859
  upload.dropped = true;
794
- const answered = context.endpoints.abort(upload.ticket.token).then(() => void 0);
860
+ const answered = counted(context, context.endpoints.abort(upload.ticket.token).then(() => void 0));
795
861
  letGo(context, upload, answered);
796
862
  return answered;
797
863
  }
@@ -811,7 +877,7 @@ function abortFile(context, entry) {
811
877
  }
812
878
  context.leftovers.release(entry);
813
879
  settle(entry, "aborted", withFile(aborted(), entry));
814
- if (!upload || upload.dropped) return;
880
+ if (!upload || upload.dropped || keptForResume(context, upload)) return;
815
881
  if (upload.mayBeComplete) letGo(context, upload);
816
882
  else drop(context, upload);
817
883
  }
@@ -828,6 +894,7 @@ async function end(context, upload, error) {
828
894
  else giveUp(context, upload);
829
895
  return;
830
896
  }
897
+ if (keptForResume(context, upload)) return;
831
898
  if (mayBeComplete) letGo(context, upload);
832
899
  else await drop(context, upload);
833
900
  }
@@ -873,7 +940,7 @@ async function refused(job, entry, refusal, attempt) {
873
940
  async function runEntry(job, entry, answer, index, whole, attempt) {
874
941
  if (entry.status === "aborted") {
875
942
  const late = answer.error ? void 0 : answer.data[index];
876
- if (late && !(late instanceof CloudropError)) job.context.endpoints.abort(late.token);
943
+ if (late && !(late instanceof CloudropError)) counted(job.context, job.context.endpoints.abort(late.token));
877
944
  return;
878
945
  }
879
946
  if (answer.error) return settle(entry, "failed", whole ? answer.error : withFile(answer.error, entry));
@@ -960,6 +1027,7 @@ function createLeftovers(giveUp) {
960
1027
  }
961
1028
  // src/client/job-run.ts
962
1029
  const settledStatus = ({ status }) => status === "done" || status === "failed" || status === "aborted";
1030
+ const ended = (entries, keepFailed) => entries.every(({ status }) => status === "done" || status === "aborted" || status === "failed" && !keepFailed);
963
1031
  const busy = (entry) => !settledStatus(entry) || entry.status === "failed" && !!entry.upload && !entry.upload.dropped && !entry.upload.mayBeComplete;
964
1032
  function createJob(context, args, entries) {
965
1033
  const controller = new AbortController();
@@ -967,6 +1035,7 @@ function createJob(context, args, entries) {
967
1035
  const abort = () => controller.abort();
968
1036
  const idle = () => {
969
1037
  if (!entries.some(busy)) args.signal.removeEventListener("abort", abort);
1038
+ if (ended(entries, context.keepFailed)) context.lifetime.jobs.delete(job);
970
1039
  };
971
1040
  signal.addEventListener("abort", () => {
972
1041
  for (const entry of entries) abortFile(context, entry);
@@ -1001,6 +1070,7 @@ function createJob(context, args, entries) {
1001
1070
  },
1002
1071
  idle
1003
1072
  };
1073
+ context.lifetime.jobs.add(job);
1004
1074
  return job;
1005
1075
  }
1006
1076
  function forgetTicket(context, entry) {
@@ -1081,10 +1151,14 @@ function snapshotOf(entries) {
1081
1151
  };
1082
1152
  }
1083
1153
  // src/client/retry-file.ts
1084
- function needsTicket({ upload, error }) {
1154
+ function needsTicket({ upload, error, landed }) {
1085
1155
  if (!upload) return true;
1086
1156
  if (upload.mayBeComplete) return false;
1087
- return upload.dropped || Date.parse(upload.ticket.expiresAt) <= Date.now() || NEEDS_NEW_TICKET.has(error?.code);
1157
+ const { ticket } = upload;
1158
+ const expired = Date.parse(ticket.expiresAt) <= Date.now();
1159
+ const partCount = ticket.kind === "single" ? 1 : ticket.partCount;
1160
+ const unsent = error?.code === "PARTS_MISSING" || landed.size < partCount;
1161
+ return upload.dropped || expired || unsent && !canSign(ticket) || NEEDS_NEW_TICKET.has(error?.code);
1088
1162
  }
1089
1163
  function requeue(job, entry, fresh) {
1090
1164
  job.context.leftovers.release(entry);
@@ -1170,7 +1244,8 @@ function handleOf(job, entry, retry) {
1170
1244
  function startJob(context, route, files, options) {
1171
1245
  return createUploadJob(context, readArguments(route, files, options), firstRun);
1172
1246
  }
1173
- function createUploadJob(context, args, run, retry = retryFiles) {
1247
+ function createUploadJob(context, given, run, retry = retryFiles) {
1248
+ const args = context.lifetime.closed ? fail(clientClosed()) : given;
1174
1249
  const entries = (args.error ? [] : args.data.files).map((file) => createEntry(file, context.nextRank(), (urgent) => store.changed(urgent)));
1175
1250
  const store = createStore(() => snapshotOf(entries), () => entries.some((entry) => entry.status === "uploading"));
1176
1251
  const job = args.error ? null : createJob(context, args.data, entries);
@@ -1192,6 +1267,45 @@ function createUploadJob(context, args, run, retry = retryFiles) {
1192
1267
  getSnapshot: store.get
1193
1268
  };
1194
1269
  }
1270
+ // src/client/options.ts
1271
+ const BASE_PATH = /^(?:https?:\/\/[^/?#\s]+)?(?:\/[^/?#\s]+)+$/;
1272
+ function isHeaders(headers) {
1273
+ if (headers === void 0 || typeof headers === "function") return true;
1274
+ if (typeof headers !== "object" || headers === null) return false;
1275
+ try {
1276
+ new Headers(headers);
1277
+ return true;
1278
+ } catch {
1279
+ return false;
1280
+ }
1281
+ }
1282
+ function readConnection({ basePath = "/api/cloudrop", headers, fetch }) {
1283
+ 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.");
1284
+ if (!isHeaders(headers)) throw configError("headers must be the headers for the upload server, such as { authorization: \"Bearer …\" }, or a function that returns them.");
1285
+ if (fetch !== void 0 && typeof fetch !== "function") throw configError("The fetch option must be a function.");
1286
+ if (typeof (fetch ?? globalThis.fetch) !== "function") throw configError("No fetch is available here. Pass createCloudropClient({ fetch }).");
1287
+ return {
1288
+ basePath,
1289
+ headers,
1290
+ fetch
1291
+ };
1292
+ }
1293
+ function readOptions(options) {
1294
+ const given = options ?? {};
1295
+ const { concurrency = "auto", order = "added", persist = true, namespace = "" } = given;
1296
+ const connection = readConnection(given);
1297
+ if (concurrency !== "auto" && !(Number.isInteger(concurrency) && concurrency >= 1 && concurrency <= 16)) throw configError("concurrency must be \"auto\" or a whole number from 1 to 16.");
1298
+ if (order !== "added" && order !== "smallest-first") throw configError("order must be \"added\" or \"smallest-first\".");
1299
+ if (typeof persist !== "boolean") throw configError("persist must be true or false.");
1300
+ 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.");
1301
+ return {
1302
+ ...connection,
1303
+ concurrency,
1304
+ order,
1305
+ persist,
1306
+ namespace
1307
+ };
1308
+ }
1195
1309
  // src/client/pool.ts
1196
1310
  const AUTO_START = 4;
1197
1311
  const AUTO_MAX = 8;
@@ -1299,12 +1413,39 @@ async function forget(context, options) {
1299
1413
  const cleared = await context.records.clear(all ? null : context.namespace);
1300
1414
  return cleared.error ? cleared : ok({ forgotten: cleared.data });
1301
1415
  }
1416
+ const turns = /* @__PURE__ */ new WeakMap();
1417
+ function takeTurn(context, id) {
1418
+ const waiting = turns.get(context) ?? /* @__PURE__ */ new Map();
1419
+ turns.set(context, waiting);
1420
+ const ready = waiting.get(id) ?? Promise.resolve();
1421
+ let resolve = () => {};
1422
+ const mine = new Promise((done) => {
1423
+ resolve = done;
1424
+ });
1425
+ waiting.set(id, mine);
1426
+ return {
1427
+ ready,
1428
+ end: () => {
1429
+ resolve();
1430
+ if (waiting.get(id) === mine) waiting.delete(id);
1431
+ }
1432
+ };
1433
+ }
1302
1434
  async function check(context, id, file) {
1303
1435
  const release = claim(context, id);
1304
1436
  const refuse = (refusal) => {
1305
1437
  release();
1306
1438
  return refusal;
1307
1439
  };
1440
+ const turn = takeTurn(context, id);
1441
+ try {
1442
+ await turn.ready;
1443
+ return await checkInTurn(context, id, file, release, refuse);
1444
+ } finally {
1445
+ turn.end();
1446
+ }
1447
+ }
1448
+ async function checkInTurn(context, id, file, release, refuse) {
1308
1449
  const { data, error } = await context.records.list(context.namespace);
1309
1450
  const record = data?.find((saved) => saved.id === id);
1310
1451
  if (!record) return refuse(error ?? notResumable());
@@ -1403,6 +1544,10 @@ function classifyAnswer(status, body, retryAfter) {
1403
1544
  error: storageDown(),
1404
1545
  retryAfterMs
1405
1546
  };
1547
+ if (status === 404 && storageCode === "NoSuchUpload") return {
1548
+ kind: "rejected",
1549
+ error: uploadGone()
1550
+ };
1406
1551
  return {
1407
1552
  kind: "rejected",
1408
1553
  error: uploadRejected(status)
@@ -1505,31 +1650,12 @@ function oneFile(result) {
1505
1650
  const [outcome] = result.data.files;
1506
1651
  return ok(outcome.result);
1507
1652
  }
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
1653
  function createCloudropClient(options = {}) {
1528
- const { basePath, fetch, concurrency, order, persist, namespace } = readOptions(options);
1654
+ const { basePath, headers, fetch, concurrency, order, persist, namespace } = readOptions(options);
1529
1655
  const send = fetch ?? ((url, init) => globalThis.fetch(url, init));
1530
1656
  let rank = 0;
1531
1657
  const context = {
1532
- endpoints: createEndpoints(basePath, send),
1658
+ endpoints: createEndpoints(basePath, send, headers),
1533
1659
  put: createPut(fetch, send),
1534
1660
  pool: createPool(concurrency),
1535
1661
  order,
@@ -1538,7 +1664,8 @@ function createCloudropClient(options = {}) {
1538
1664
  keepFailed: true,
1539
1665
  records: createRecords(persist),
1540
1666
  namespace,
1541
- claims: /* @__PURE__ */ new Set()
1667
+ claims: /* @__PURE__ */ new Set(),
1668
+ lifetime: createLifetime()
1542
1669
  };
1543
1670
  function upload(route, files, options) {
1544
1671
  const { done } = startJob({
@@ -1553,9 +1680,10 @@ function createCloudropClient(options = {}) {
1553
1680
  resumable: (route) => resumable(context, route),
1554
1681
  resume: (upload, file, options) => resumeJob(context, upload, file, options),
1555
1682
  discard: (upload) => discard(context, upload),
1556
- forget: (options) => forget(context, options)
1683
+ forget: (options) => forget(context, options),
1684
+ close: (options) => closeClient(context, options?.abort === true)
1557
1685
  };
1558
1686
  }
1559
1687
  export { isList as n, createCloudropClient as t };
1560
1688
 
1561
- //# sourceMappingURL=client-Y1zUEbCj.js.map
1689
+ //# sourceMappingURL=client-DxP3uv8V.js.map