@sparkvault/sdk-mobile 9.0.10 → 10.0.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/README.md +2 -2
- package/dist/client.d.ts +2 -1
- package/dist/client.js +2 -1
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +2 -12
- package/dist/config.js +0 -1
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +15 -13
- package/dist/errors.js +21 -12
- package/dist/errors.js.map +1 -1
- package/dist/http.d.ts +44 -18
- package/dist/http.js +64 -33
- package/dist/http.js.map +1 -1
- package/dist/identity-dialog.d.ts +1 -1
- package/dist/identity-dialog.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/ingots.d.ts +20 -36
- package/dist/ingots.js +18 -45
- package/dist/ingots.js.map +1 -1
- package/dist/tus.d.ts +21 -76
- package/dist/tus.js +61 -222
- package/dist/tus.js.map +1 -1
- package/dist/types.d.ts +12 -56
- package/package.json +6 -6
- package/src/client.ts +2 -1
- package/src/config.ts +1 -13
- package/src/errors.ts +28 -19
- package/src/http.ts +74 -38
- package/src/identity-dialog.tsx +2 -2
- package/src/index.ts +0 -1
- package/src/ingots.ts +31 -73
- package/src/tus.ts +77 -286
- package/src/types.ts +12 -54
package/src/tus.ts
CHANGED
|
@@ -5,10 +5,8 @@ import {
|
|
|
5
5
|
HEAD_AND_RECEIPT_TIMEOUT_MS,
|
|
6
6
|
RECEIVED_STILL_SAVING,
|
|
7
7
|
TRANSFER_VERDICT,
|
|
8
|
-
UPLOAD_EXPIRES_HEADER,
|
|
9
8
|
classifyTransferResponse,
|
|
10
9
|
createTransferBudget,
|
|
11
|
-
parseUploadExpires,
|
|
12
10
|
resumeOffsetProblem,
|
|
13
11
|
retryDelayMs,
|
|
14
12
|
transferJitterKey,
|
|
@@ -32,7 +30,6 @@ import type {
|
|
|
32
30
|
BackgroundTransferJob,
|
|
33
31
|
DebugLogger,
|
|
34
32
|
MobileBackgroundTransfer,
|
|
35
|
-
MobileBackgroundUploader,
|
|
36
33
|
MobileFileReader,
|
|
37
34
|
TusSessionInfo,
|
|
38
35
|
UploadAbortBehavior,
|
|
@@ -53,8 +50,7 @@ export interface ParsedForgeUrl {
|
|
|
53
50
|
*
|
|
54
51
|
* A resume that cannot use the session throws a `TusUploadError` whose
|
|
55
52
|
* `sessionLost` is true exactly when the session is worthless for this file
|
|
56
|
-
* and the caller should drop it and start fresh:
|
|
57
|
-
* for it has passed (refused before any request), Forge no longer has it
|
|
53
|
+
* and the caller should drop it and start fresh: Forge no longer has it
|
|
58
54
|
* (404/410), or its `Upload-Length` is not this file's size (it belongs to a
|
|
59
55
|
* different file). A HEAD that keeps failing transiently (5xx, timeouts) past
|
|
60
56
|
* the upload's retry budget is NOT `sessionLost`; the session is intact and
|
|
@@ -64,12 +60,6 @@ export interface TusResumeSession {
|
|
|
64
60
|
uploadUrl: string;
|
|
65
61
|
/** The session's ISTK. Falls back to the one in `forgeUrl` when omitted. */
|
|
66
62
|
istk?: string;
|
|
67
|
-
/**
|
|
68
|
-
* The session's expiry as the caller holds it (`expiresAt` from
|
|
69
|
-
* `onSessionCreated`, kept current through `onSessionExpiry`). Null or
|
|
70
|
-
* omitted is unknown, and Forge's HEAD decides.
|
|
71
|
-
*/
|
|
72
|
-
expiresAt?: number | null;
|
|
73
63
|
}
|
|
74
64
|
|
|
75
65
|
/** Where a tus session stands server-side, as reported by HEAD. */
|
|
@@ -77,8 +67,6 @@ export interface TusSessionProbe {
|
|
|
77
67
|
offset: number;
|
|
78
68
|
length: number;
|
|
79
69
|
chunkSize: number;
|
|
80
|
-
/** When the session lapses (epoch ms, Forge's `Upload-Expires`), or null when the HEAD named none. */
|
|
81
|
-
expiresAt: number | null;
|
|
82
70
|
}
|
|
83
71
|
|
|
84
72
|
export interface TusUploadFromUriOptions {
|
|
@@ -101,36 +89,22 @@ export interface TusUploadFromUriOptions {
|
|
|
101
89
|
*/
|
|
102
90
|
onSessionCreated?: (session: TusSessionInfo) => void;
|
|
103
91
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* while it runs (the transfer engine's range, the single-chunk
|
|
109
|
-
* `backgroundUploader` PATCH): either may have moved it unread. A caller
|
|
110
|
-
* that persists the session keeps its `expiresAt` current with this, so a
|
|
111
|
-
* session it judges expired (`isSessionExpired`) really is. A throw here
|
|
112
|
-
* fails the upload.
|
|
113
|
-
*/
|
|
114
|
-
onSessionExpiry?: (expiresAt: number | null) => void;
|
|
115
|
-
/**
|
|
116
|
-
* `background` asks for a transfer that survives suspension and rides the
|
|
117
|
-
* best engine the app provides: the OS transfer engine
|
|
118
|
-
* (`backgroundTransfer`) for the whole remaining range of any file, else
|
|
119
|
-
* the single-chunk `backgroundUploader` (no progress, no abort; whether it
|
|
120
|
-
* outlives a suspension is the uploader's) for a file that fits one chunk,
|
|
121
|
-
* else XHR. Default `foreground`: every chunk goes over XHR.
|
|
92
|
+
* `background` asks for a transfer that survives suspension: the OS
|
|
93
|
+
* transfer engine (`backgroundTransfer`) takes the whole remaining range of
|
|
94
|
+
* any file, and without one every chunk goes over XHR. Default
|
|
95
|
+
* `foreground`: every chunk goes over XHR.
|
|
122
96
|
*/
|
|
123
97
|
transport?: UploadTransport;
|
|
124
98
|
/**
|
|
125
99
|
* Network policy carried into the OS transfer engine on every request it
|
|
126
|
-
* sends (see `UploadTransferPolicy`). The XHR
|
|
127
|
-
*
|
|
100
|
+
* sends (see `UploadTransferPolicy`). The XHR path ignores it: JS gates
|
|
101
|
+
* connectivity for it. Defaults: both allowed.
|
|
128
102
|
*/
|
|
129
103
|
transferPolicy?: UploadTransferPolicy;
|
|
130
104
|
/**
|
|
131
105
|
* The device media library's id for the file at `fileUri`, carried into
|
|
132
106
|
* the OS transfer engine's job (`BackgroundTransferJob.sourceAssetId`).
|
|
133
|
-
* The
|
|
107
|
+
* The XHR path reads the file in this process and ignores it.
|
|
134
108
|
*/
|
|
135
109
|
sourceAssetId?: string;
|
|
136
110
|
onProgress?: UploadProgressCallback;
|
|
@@ -272,8 +246,6 @@ export function isCancelledTransfer(err: unknown, signal: AbortSignal | undefine
|
|
|
272
246
|
*/
|
|
273
247
|
interface TusChunk {
|
|
274
248
|
base64: string;
|
|
275
|
-
/** Decoded payload size — the read length requested from the file. */
|
|
276
|
-
byteLength: number;
|
|
277
249
|
}
|
|
278
250
|
|
|
279
251
|
/**
|
|
@@ -327,32 +299,6 @@ function sessionLostError(
|
|
|
327
299
|
});
|
|
328
300
|
}
|
|
329
301
|
|
|
330
|
-
/**
|
|
331
|
-
* How far past the expiry Forge named a session still counts as live: the
|
|
332
|
-
* allowance for a device clock running ahead of Forge's. `Upload-Expires` is
|
|
333
|
-
* read off Forge's clock and compared with the device's; a phone on automatic
|
|
334
|
-
* time is within seconds of Forge, one set by hand is typically minutes fast.
|
|
335
|
-
* The two mistakes cost very differently: a live session judged expired is
|
|
336
|
-
* dropped and its file sent again from byte 0, an expired one judged live
|
|
337
|
-
* costs a single HEAD (Forge answers 410, `sessionLost`). So the margin errs
|
|
338
|
-
* long, and stays small against the 72 h idle window a session lives from its
|
|
339
|
-
* last commit (`@sparkvault/app-sdk/upload-expiry`): a session the clock says
|
|
340
|
-
* lapsed hours ago is still dropped without a request.
|
|
341
|
-
*/
|
|
342
|
-
export const SESSION_EXPIRY_CLOCK_MARGIN_MS = 5 * 60 * 1000;
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* True when the expiry Forge named for a session (`TusSessionInfo.expiresAt`,
|
|
346
|
-
* kept current through `onSessionExpiry`) is past by more than the clock
|
|
347
|
-
* margin: Forge refuses every request to a session past its expiry, so a
|
|
348
|
-
* caller drops it without asking. Within the margin the session is live for
|
|
349
|
-
* all the device clock can tell, and Forge's HEAD decides. An unknown expiry
|
|
350
|
-
* (null) never has passed: only Forge's HEAD can say.
|
|
351
|
-
*/
|
|
352
|
-
export function isSessionExpired(expiresAt: number | null | undefined, now: number = Date.now()): boolean {
|
|
353
|
-
return typeof expiresAt === 'number' && now > expiresAt + SESSION_EXPIRY_CLOCK_MARGIN_MS;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
302
|
/**
|
|
357
303
|
* One answer to a transfer request, or its absence: what the transfer policy
|
|
358
304
|
* reads (the status and the headers) and what an error built from it says
|
|
@@ -422,6 +368,8 @@ const RESUME_HEAD_REQUEST: TusRequestKind = { label: 'TUS HEAD', phase: 'create'
|
|
|
422
368
|
const OFFSET_HEAD_REQUEST: TusRequestKind = { label: 'TUS HEAD', phase: 'upload', gonePhase: 'create', receipt: false };
|
|
423
369
|
const CHUNK_REQUEST: TusRequestKind = { label: 'Chunk upload', phase: 'upload', gonePhase: 'upload', receipt: false };
|
|
424
370
|
const RECEIPT_REQUEST: TusRequestKind = { label: 'TUS finalization', phase: 'upload', gonePhase: 'create', receipt: true };
|
|
371
|
+
/** The OS transfer engine's PATCHes, whose failure arrives as the one answer that ended the range. */
|
|
372
|
+
const ENGINE_REQUEST: TusRequestKind = { label: 'Background chunk upload', phase: 'upload', gonePhase: 'upload', receipt: false };
|
|
425
373
|
|
|
426
374
|
/** What a caller hears once Forge kept deferring the publication until the upload's idle limit. */
|
|
427
375
|
const RECEIVED_PUBLISHING_MESSAGE =
|
|
@@ -482,21 +430,28 @@ function failureOf(
|
|
|
482
430
|
return { error: errorOf(answer, outcome, request, context), outcome, unanswered: answer.unanswered !== null };
|
|
483
431
|
}
|
|
484
432
|
|
|
433
|
+
/**
|
|
434
|
+
* The error a caller sees for an answer (failureOf). `cause` is what
|
|
435
|
+
* reported the answer when this SDK did not receive it itself: the OS
|
|
436
|
+
* engine's failure.
|
|
437
|
+
*/
|
|
485
438
|
function errorOf(
|
|
486
439
|
answer: TransferAnswer,
|
|
487
440
|
outcome: TransferOutcome,
|
|
488
441
|
request: TusRequestKind,
|
|
489
|
-
context: TusErrorContext
|
|
442
|
+
context: TusErrorContext,
|
|
443
|
+
cause: Error | null = null
|
|
490
444
|
): SparkVaultMobileError {
|
|
491
445
|
if (answer.unanswered) return answer.unanswered;
|
|
492
446
|
if (outcome.verdict === TRANSFER_VERDICT.REFUSED && outcome.coreCode !== null) {
|
|
493
|
-
return refusedError(answer.status, answer.envelope, outcome.coreCode, context, request.phase);
|
|
447
|
+
return refusedError(answer.status, answer.envelope, outcome.coreCode, context, request.phase, cause);
|
|
494
448
|
}
|
|
495
449
|
if (outcome.verdict === TRANSFER_VERDICT.GONE && request.gonePhase !== null) {
|
|
496
|
-
return sessionLostError(answer.status, context, request.gonePhase,
|
|
450
|
+
return sessionLostError(answer.status, context, request.gonePhase, cause, outcome.coreCode);
|
|
497
451
|
}
|
|
498
452
|
if (outcome.verdict === TRANSFER_VERDICT.DEFERRED) {
|
|
499
453
|
return new TusUploadError(RECEIVED_PUBLISHING_MESSAGE, {
|
|
454
|
+
cause,
|
|
500
455
|
httpStatus: answer.status,
|
|
501
456
|
code: RECEIVED_STILL_SAVING,
|
|
502
457
|
retryAfterMs: outcome.retryAfterMs,
|
|
@@ -508,6 +463,7 @@ function errorOf(
|
|
|
508
463
|
const gateError = gateErrorFromBody(answer.status, answer.envelope);
|
|
509
464
|
if (gateError) return gateError;
|
|
510
465
|
return new TusUploadError(`${request.label} failed: ${answer.status} - ${answer.body.substring(0, 200)}`, {
|
|
466
|
+
cause,
|
|
511
467
|
httpStatus: answer.status,
|
|
512
468
|
retryAfterMs: outcome.retryAfterMs,
|
|
513
469
|
...context,
|
|
@@ -515,57 +471,14 @@ function errorOf(
|
|
|
515
471
|
});
|
|
516
472
|
}
|
|
517
473
|
|
|
518
|
-
/**
|
|
519
|
-
* The session's expiry as its latest answer named it, reported to the
|
|
520
|
-
* caller (`onSessionExpiry`) each time it changes. An answer that names none
|
|
521
|
-
* leaves it unknown: the request may have moved it unread (a commit whose
|
|
522
|
-
* answer was lost, a deferred finalization), and a caller that dropped a
|
|
523
|
-
* session on an expiry Forge has since moved on would re-send a whole file.
|
|
524
|
-
* A request cut short by the caller's abort keeps the last named value: it
|
|
525
|
-
* can only have slid the expiry by that one request's duration.
|
|
526
|
-
*/
|
|
527
|
-
class SessionExpiry {
|
|
528
|
-
private known: number | null;
|
|
529
|
-
private readonly report?: (expiresAt: number | null) => void;
|
|
530
|
-
|
|
531
|
-
constructor(known: number | null, report?: (expiresAt: number | null) => void) {
|
|
532
|
-
this.known = known;
|
|
533
|
-
this.report = report;
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
/** Forge answered (or did not): the expiry is what the answer names. */
|
|
537
|
-
read(answer: TransferAnswer): void {
|
|
538
|
-
this.become(parseUploadExpires(answer.header(UPLOAD_EXPIRES_HEADER)));
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/**
|
|
542
|
-
* A request JS cannot follow while it runs is about to leave: the OS
|
|
543
|
-
* transfer engine's range, whose chunks commit (each sliding the expiry)
|
|
544
|
-
* with answers this upload never reads, possibly while the app is not
|
|
545
|
-
* running; or the single-chunk `backgroundUploader` PATCH, which reports
|
|
546
|
-
* nothing until it answers.
|
|
547
|
-
*/
|
|
548
|
-
forget(): void {
|
|
549
|
-
this.become(null);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
private become(expiresAt: number | null): void {
|
|
553
|
-
if (expiresAt === this.known) return;
|
|
554
|
-
this.known = expiresAt;
|
|
555
|
-
this.report?.(expiresAt);
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
474
|
/**
|
|
560
475
|
* What every request to one session carries: where it lives, its token, the
|
|
561
|
-
* file it is for, the
|
|
562
|
-
* probe), and the caller's abort.
|
|
476
|
+
* file it is for, and the caller's abort.
|
|
563
477
|
*/
|
|
564
478
|
interface SessionLink {
|
|
565
479
|
readonly uploadUrl: string;
|
|
566
480
|
readonly istk: string;
|
|
567
481
|
readonly context: TusErrorContext;
|
|
568
|
-
readonly expiry: SessionExpiry | null;
|
|
569
482
|
readonly signal?: AbortSignal;
|
|
570
483
|
}
|
|
571
484
|
|
|
@@ -580,7 +493,6 @@ interface TusTransfer extends SessionLink {
|
|
|
580
493
|
readonly fileSize: number;
|
|
581
494
|
readonly chunkSize: number;
|
|
582
495
|
readonly budget: TransferBudget;
|
|
583
|
-
readonly expiry: SessionExpiry;
|
|
584
496
|
}
|
|
585
497
|
|
|
586
498
|
/** Where an upload stands: the server's offset, and whether the server confirmed the ingot published. */
|
|
@@ -589,11 +501,10 @@ interface TransferPosition {
|
|
|
589
501
|
readonly published: boolean;
|
|
590
502
|
}
|
|
591
503
|
|
|
592
|
-
/** The file a chunk is read from, and how
|
|
504
|
+
/** The file a chunk is read from, and who hears how much of each chunk has left. */
|
|
593
505
|
interface ChunkSource {
|
|
594
506
|
readonly reader: MobileFileReader;
|
|
595
507
|
readonly uri: string;
|
|
596
|
-
readonly transport?: UploadTransport;
|
|
597
508
|
/** Bytes of the chunk at `offset` that have left the device so far. */
|
|
598
509
|
readonly onChunkProgress: (offset: number, chunkBytesUploaded: number) => void;
|
|
599
510
|
}
|
|
@@ -725,20 +636,6 @@ async function fetchAnswer(
|
|
|
725
636
|
);
|
|
726
637
|
}
|
|
727
638
|
|
|
728
|
-
/**
|
|
729
|
-
* Case-insensitive header lookup for adapter results. Native HTTP stacks
|
|
730
|
-
* hand headers back in whatever case they please (iOS lower-cases them),
|
|
731
|
-
* while Forge writes `Upload-Offset`.
|
|
732
|
-
*/
|
|
733
|
-
function getHeaderIgnoreCase(headers: Record<string, string> | undefined, name: string): string | null {
|
|
734
|
-
if (!headers) return null;
|
|
735
|
-
const wanted = name.toLowerCase();
|
|
736
|
-
for (const key of Object.keys(headers)) {
|
|
737
|
-
if (key.toLowerCase() === wanted) return headers[key];
|
|
738
|
-
}
|
|
739
|
-
return null;
|
|
740
|
-
}
|
|
741
|
-
|
|
742
639
|
/**
|
|
743
640
|
* Where a tus upload starts: a fresh session at the Forge endpoint, or a
|
|
744
641
|
* persisted one resumed by URL. Resolved up front so a misconfigured call
|
|
@@ -746,7 +643,7 @@ function getHeaderIgnoreCase(headers: Record<string, string> | undefined, name:
|
|
|
746
643
|
*/
|
|
747
644
|
type TusSessionSource =
|
|
748
645
|
| { mode: 'create'; parsed: ParsedForgeUrl }
|
|
749
|
-
| { mode: 'resume'; uploadUrl: string; istk: string
|
|
646
|
+
| { mode: 'resume'; uploadUrl: string; istk: string };
|
|
750
647
|
|
|
751
648
|
function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatterns: readonly RegExp[]): TusSessionSource {
|
|
752
649
|
if (options.resume) {
|
|
@@ -756,11 +653,7 @@ function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatte
|
|
|
756
653
|
if (!istk) {
|
|
757
654
|
throw new SparkVaultValidationError('resume.istk or forgeUrl is required to resume a TUS upload session');
|
|
758
655
|
}
|
|
759
|
-
|
|
760
|
-
if (expiresAt !== null && !Number.isFinite(expiresAt)) {
|
|
761
|
-
throw new SparkVaultValidationError('resume.expiresAt is an epoch-ms expiry or null', { received: expiresAt });
|
|
762
|
-
}
|
|
763
|
-
return { mode: 'resume', uploadUrl, istk, expiresAt };
|
|
656
|
+
return { mode: 'resume', uploadUrl, istk };
|
|
764
657
|
}
|
|
765
658
|
if (!options.forgeUrl) {
|
|
766
659
|
throw new SparkVaultValidationError('forgeUrl is required to create a TUS upload session');
|
|
@@ -769,15 +662,15 @@ function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatte
|
|
|
769
662
|
}
|
|
770
663
|
|
|
771
664
|
/**
|
|
772
|
-
* The session a create's 2xx opened: where it lives
|
|
773
|
-
*
|
|
774
|
-
*
|
|
665
|
+
* The session a create's 2xx opened: where it lives and its chunk geometry.
|
|
666
|
+
* Every chunk carries the file's bytes, so the session must live under the
|
|
667
|
+
* endpoint it was created at.
|
|
775
668
|
*/
|
|
776
669
|
function readCreatedSession(
|
|
777
670
|
answer: TransferAnswer,
|
|
778
671
|
endpoint: string,
|
|
779
672
|
context: TusErrorContext
|
|
780
|
-
): { uploadUrl: string; chunkSize: number
|
|
673
|
+
): { uploadUrl: string; chunkSize: number } {
|
|
781
674
|
const uploadUrl = resolveForgeSessionUrl(answer.header('Location'), endpoint);
|
|
782
675
|
if (!uploadUrl) {
|
|
783
676
|
throw new TusUploadError('TUS create response Location header is missing or outside the upload endpoint', {
|
|
@@ -799,14 +692,14 @@ function readCreatedSession(
|
|
|
799
692
|
});
|
|
800
693
|
}
|
|
801
694
|
|
|
802
|
-
return { uploadUrl, chunkSize
|
|
695
|
+
return { uploadUrl, chunkSize };
|
|
803
696
|
}
|
|
804
697
|
|
|
805
698
|
/**
|
|
806
699
|
* Where a HEAD's 2xx says the session stands: offset, length and chunk
|
|
807
700
|
* geometry, all required (a 2xx without them is not Forge, a captive
|
|
808
|
-
* portal's page being the classic case)
|
|
809
|
-
*
|
|
701
|
+
* portal's page being the classic case). An offset past the length can only
|
|
702
|
+
* be a corrupt session.
|
|
810
703
|
*/
|
|
811
704
|
function readSessionProbe(answer: TransferAnswer, phase: TusUploadError['phase'], context: TusErrorContext): TusSessionProbe {
|
|
812
705
|
const offset = parseIntegerHeader(answer.header('Upload-Offset'), 0);
|
|
@@ -818,7 +711,7 @@ function readSessionProbe(answer: TransferAnswer, phase: TusUploadError['phase']
|
|
|
818
711
|
{ ...context, phase }
|
|
819
712
|
);
|
|
820
713
|
}
|
|
821
|
-
return { offset, length, chunkSize
|
|
714
|
+
return { offset, length, chunkSize };
|
|
822
715
|
}
|
|
823
716
|
|
|
824
717
|
/**
|
|
@@ -961,70 +854,21 @@ function sendChunkOverXhr(
|
|
|
961
854
|
});
|
|
962
855
|
}
|
|
963
856
|
|
|
964
|
-
/**
|
|
965
|
-
* PATCH a whole file as the session's only chunk through the app's
|
|
966
|
-
* single-request uploader (`backgroundUploader`) and resolve with Forge's
|
|
967
|
-
* answer, or with no answer when the adapter got none. The request reports
|
|
968
|
-
* no progress and takes no abort; whether it outlives a suspension is the
|
|
969
|
-
* uploader's (MobileBackgroundUploader). Its answer is read exactly like the
|
|
970
|
-
* XHR path's, so callers see one error taxonomy.
|
|
971
|
-
*/
|
|
972
|
-
async function sendWholeFileInBackground(
|
|
973
|
-
uploader: MobileBackgroundUploader,
|
|
974
|
-
transfer: TusTransfer,
|
|
975
|
-
fileUri: string,
|
|
976
|
-
offset: number
|
|
977
|
-
): Promise<TransferAnswer> {
|
|
978
|
-
let result: Awaited<ReturnType<MobileBackgroundUploader['uploadFile']>>;
|
|
979
|
-
try {
|
|
980
|
-
result = await uploader.uploadFile(transfer.uploadUrl, fileUri, {
|
|
981
|
-
method: 'PATCH',
|
|
982
|
-
headers: {
|
|
983
|
-
'Tus-Resumable': TUS_VERSION,
|
|
984
|
-
'Upload-Offset': String(offset),
|
|
985
|
-
'Content-Type': 'application/offset+octet-stream',
|
|
986
|
-
'X-ISTK': transfer.istk,
|
|
987
|
-
},
|
|
988
|
-
});
|
|
989
|
-
} catch (err) {
|
|
990
|
-
// The adapter only throws when no HTTP response came back (offline, DNS,
|
|
991
|
-
// the OS cancelled the task); an HTTP failure is a status below.
|
|
992
|
-
return noAnswer(new TusUploadError(`Background chunk upload failed: ${getErrorMessage(err)}`, {
|
|
993
|
-
cause: err instanceof Error ? err : null,
|
|
994
|
-
...transfer.context,
|
|
995
|
-
phase: 'network',
|
|
996
|
-
}));
|
|
997
|
-
}
|
|
998
|
-
|
|
999
|
-
if (!Number.isSafeInteger(result.status) || result.status <= 0) {
|
|
1000
|
-
return noAnswer(new TusUploadError(`Background chunk upload got no answer (status ${String(result.status)})`, {
|
|
1001
|
-
...transfer.context,
|
|
1002
|
-
phase: 'network',
|
|
1003
|
-
}));
|
|
1004
|
-
}
|
|
1005
|
-
const answered = result.status >= 200 && result.status < 300;
|
|
1006
|
-
return answerOf(
|
|
1007
|
-
result.status,
|
|
1008
|
-
name => getHeaderIgnoreCase(result.headers, name),
|
|
1009
|
-
!answered && typeof result.body === 'string' ? result.body : ''
|
|
1010
|
-
);
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
857
|
/**
|
|
1014
858
|
* Translate what the OS transfer adapter reports into the SDK's one error
|
|
1015
859
|
* taxonomy, so a caller sees the same `TusUploadError` phases and typed gate
|
|
1016
|
-
* errors whichever engine carried the bytes. Forge's
|
|
1017
|
-
* `X-Upload-Refusal`
|
|
1018
|
-
*
|
|
1019
|
-
*
|
|
1020
|
-
*
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
1023
|
-
*
|
|
1024
|
-
*
|
|
1025
|
-
*
|
|
1026
|
-
*
|
|
1027
|
-
*
|
|
860
|
+
* errors whichever engine carried the bytes. An `http` failure is Forge's
|
|
861
|
+
* answer (its status, body and `X-Upload-Refusal`), read by the one reading
|
|
862
|
+
* the XHR path's answers get (`readOutcome`, `errorOf`): the refusal Core
|
|
863
|
+
* decided is `upload_refused` in Core's words, a 402 the gate error, a gone
|
|
864
|
+
* session `sessionLost`, and any other status keeps its number for the
|
|
865
|
+
* caller's own policy (a 409 during finalize is a retry-later, not a lost
|
|
866
|
+
* session). The remaining kinds are transport outcomes: the caller's own
|
|
867
|
+
* abort is `cancelled`; an OS-ended task (`interrupted`) is a network
|
|
868
|
+
* failure, because the right response is the same as for a dropped
|
|
869
|
+
* connection: resume from the server offset. Anything that is not a
|
|
870
|
+
* `BackgroundTransferError` is an adapter defect and is reported as a
|
|
871
|
+
* network failure with the cause attached, never swallowed.
|
|
1028
872
|
*/
|
|
1029
873
|
export function mapBackgroundTransferError(
|
|
1030
874
|
err: unknown,
|
|
@@ -1042,28 +886,23 @@ export function mapBackgroundTransferError(
|
|
|
1042
886
|
case 'http': {
|
|
1043
887
|
const status = err.httpStatus;
|
|
1044
888
|
const body = err.body ?? '';
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
: null;
|
|
1054
|
-
if (outcome?.verdict === TRANSFER_VERDICT.REFUSED && outcome.coreCode !== null) {
|
|
1055
|
-
return refusedError(status, envelope, outcome.coreCode, context, 'upload', err);
|
|
1056
|
-
}
|
|
1057
|
-
const gateError = gateErrorFromBody(status, envelope);
|
|
1058
|
-
if (gateError) return gateError;
|
|
1059
|
-
if (outcome?.verdict === TRANSFER_VERDICT.GONE) {
|
|
1060
|
-
return sessionLostError(status, context, 'upload', err, outcome.coreCode);
|
|
1061
|
-
}
|
|
889
|
+
// A status the adapter lost leaves no answer to read: still an upload
|
|
890
|
+
// failure, never a gate or a lost session.
|
|
891
|
+
if (status === undefined || !Number.isSafeInteger(status) || status <= 0) {
|
|
892
|
+
return new TusUploadError(`${ENGINE_REQUEST.label} failed: unknown status - ${body.substring(0, 200)}`, {
|
|
893
|
+
cause: err,
|
|
894
|
+
...context,
|
|
895
|
+
phase: 'upload',
|
|
896
|
+
});
|
|
1062
897
|
}
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
898
|
+
// The engine hands over the one header a refusal Core decided is named
|
|
899
|
+
// in, so a bodiless answer reads as it does on the XHR path.
|
|
900
|
+
const answer = answerOf(
|
|
901
|
+
status,
|
|
902
|
+
name => (name.toLowerCase() === UPLOAD_REFUSAL_HEADER.toLowerCase() ? err.uploadRefusal ?? null : null),
|
|
903
|
+
body
|
|
1066
904
|
);
|
|
905
|
+
return errorOf(answer, readOutcome(answer, ENGINE_REQUEST), ENGINE_REQUEST, context, err);
|
|
1067
906
|
}
|
|
1068
907
|
case 'cancelled':
|
|
1069
908
|
return new TusUploadError('Upload cancelled', { cause: err, ...context, phase: 'cancelled' });
|
|
@@ -1180,20 +1019,12 @@ export class MobileTusUploader {
|
|
|
1180
1019
|
let position: TransferPosition;
|
|
1181
1020
|
if (source.mode === 'resume') {
|
|
1182
1021
|
debug?.log(`Resuming TUS upload for ${options.filename}`);
|
|
1183
|
-
//
|
|
1184
|
-
//
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
throw new TusUploadError(`TUS session expired at ${new Date(heldExpiry).toISOString()}`, {
|
|
1188
|
-
...context,
|
|
1189
|
-
phase: 'create',
|
|
1190
|
-
sessionLost: true,
|
|
1191
|
-
});
|
|
1192
|
-
}
|
|
1193
|
-
const expiry = new SessionExpiry(source.expiresAt, options.onSessionExpiry);
|
|
1194
|
-
const link: SessionLink = { uploadUrl: source.uploadUrl, istk, context, expiry, signal };
|
|
1022
|
+
// Only Forge knows whether it still holds the session: its HEAD
|
|
1023
|
+
// answers a gone one 404/410 (`sessionLost`), at the cost of that one
|
|
1024
|
+
// request, and a live one with where it stands.
|
|
1025
|
+
const link: SessionLink = { uploadUrl: source.uploadUrl, istk, context, signal };
|
|
1195
1026
|
const probe = await this.readResumePoint(link, budget, fileSize);
|
|
1196
|
-
transfer = { ...link,
|
|
1027
|
+
transfer = { ...link, fileSize, chunkSize: probe.chunkSize, budget };
|
|
1197
1028
|
position = { offset: checkedServerOffset(probe.offset, transfer, 'create', context), published: false };
|
|
1198
1029
|
debug?.log(`TUS session for ${options.filename} is at offset ${position.offset}/${fileSize}`);
|
|
1199
1030
|
} else {
|
|
@@ -1204,7 +1035,6 @@ export class MobileTusUploader {
|
|
|
1204
1035
|
uploadUrl: created.uploadUrl,
|
|
1205
1036
|
istk,
|
|
1206
1037
|
context,
|
|
1207
|
-
expiry: new SessionExpiry(created.expiresAt, options.onSessionExpiry),
|
|
1208
1038
|
signal,
|
|
1209
1039
|
fileSize,
|
|
1210
1040
|
chunkSize: created.chunkSize,
|
|
@@ -1217,7 +1047,6 @@ export class MobileTusUploader {
|
|
|
1217
1047
|
uploadUrl: created.uploadUrl,
|
|
1218
1048
|
istk,
|
|
1219
1049
|
chunkSize: created.chunkSize,
|
|
1220
|
-
expiresAt: created.expiresAt,
|
|
1221
1050
|
});
|
|
1222
1051
|
}
|
|
1223
1052
|
|
|
@@ -1225,14 +1054,12 @@ export class MobileTusUploader {
|
|
|
1225
1054
|
// remaining range in a single hand-off: it chains the chunks itself and
|
|
1226
1055
|
// keeps going while the app is suspended or killed, which no JS loop
|
|
1227
1056
|
// can. Any size and any offset qualify, so a resumed multi-GB video
|
|
1228
|
-
// rides it too;
|
|
1229
|
-
//
|
|
1230
|
-
//
|
|
1231
|
-
// running instead of PATCHing over it.
|
|
1057
|
+
// rides it too; XHR in the loop below is the fallback. Start-or-attach
|
|
1058
|
+
// on the session URL means a relaunch that resumes the same session
|
|
1059
|
+
// re-joins the job the OS is still running instead of PATCHing over it.
|
|
1232
1060
|
const backgroundTransfer = options.transport === 'background' ? this.config.backgroundTransfer : undefined;
|
|
1233
1061
|
if (backgroundTransfer && position.offset < fileSize) {
|
|
1234
1062
|
ensureNotAborted(signal);
|
|
1235
|
-
transfer.expiry.forget();
|
|
1236
1063
|
const policy = options.transferPolicy ?? {};
|
|
1237
1064
|
const finalOffset = await transferRemainderInBackground(
|
|
1238
1065
|
backgroundTransfer,
|
|
@@ -1264,15 +1091,13 @@ export class MobileTusUploader {
|
|
|
1264
1091
|
// Forge answers only once the ingot is published. An abort that lands
|
|
1265
1092
|
// after the OS delivered the last byte changes nothing: the transfer
|
|
1266
1093
|
// is complete, and the loop below (with its abort check) has nothing
|
|
1267
|
-
// left to do.
|
|
1268
|
-
// upload is never reported as cancelled.
|
|
1094
|
+
// left to do. A finished upload is never reported as cancelled.
|
|
1269
1095
|
position = { offset: fileSize, published: true };
|
|
1270
1096
|
}
|
|
1271
1097
|
|
|
1272
1098
|
const chunks: ChunkSource = {
|
|
1273
1099
|
reader: fileReader,
|
|
1274
1100
|
uri: cleanUri,
|
|
1275
|
-
transport: options.transport,
|
|
1276
1101
|
onChunkProgress: (offset, chunkBytesUploaded) => report(offset + chunkBytesUploaded),
|
|
1277
1102
|
};
|
|
1278
1103
|
while (!position.published) {
|
|
@@ -1311,9 +1136,9 @@ export class MobileTusUploader {
|
|
|
1311
1136
|
// in a generic TusUploadError.
|
|
1312
1137
|
if (err instanceof SparkVaultMobileError) throw err;
|
|
1313
1138
|
// Only a fileReader read or a caller's hook (onSessionCreated,
|
|
1314
|
-
//
|
|
1315
|
-
//
|
|
1316
|
-
//
|
|
1139
|
+
// onProgress) can throw anything else here: every request classifies
|
|
1140
|
+
// its own failure (tusRequest, the XHR chunk sender, the OS engine's
|
|
1141
|
+
// mapping). Those are the caller's errors, wrapped with the file
|
|
1317
1142
|
// context and nothing inferred about them.
|
|
1318
1143
|
throw TusUploadError.fromError(error, {
|
|
1319
1144
|
filename: options.filename,
|
|
@@ -1340,7 +1165,7 @@ export class MobileTusUploader {
|
|
|
1340
1165
|
budget: TransferBudget,
|
|
1341
1166
|
context: TusErrorContext,
|
|
1342
1167
|
signal?: AbortSignal
|
|
1343
|
-
): Promise<{ uploadUrl: string; chunkSize: number
|
|
1168
|
+
): Promise<{ uploadUrl: string; chunkSize: number }> {
|
|
1344
1169
|
const jitterKey = transferJitterKey(parsed.tusEndpoint);
|
|
1345
1170
|
for (;;) {
|
|
1346
1171
|
ensureNotAborted(signal);
|
|
@@ -1432,7 +1257,6 @@ export class MobileTusUploader {
|
|
|
1432
1257
|
'TUS HEAD request',
|
|
1433
1258
|
link.context
|
|
1434
1259
|
);
|
|
1435
|
-
link.expiry?.read(answer);
|
|
1436
1260
|
const outcome = readOutcome(answer, request);
|
|
1437
1261
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1438
1262
|
return { probe: readSessionProbe(answer, request.phase, link.context) };
|
|
@@ -1448,7 +1272,6 @@ export class MobileTusUploader {
|
|
|
1448
1272
|
*/
|
|
1449
1273
|
private async sendChunkAt(transfer: TusTransfer, offset: number, chunks: ChunkSource): Promise<TransferPosition> {
|
|
1450
1274
|
const answer = await this.sendChunk(transfer, offset, chunks);
|
|
1451
|
-
transfer.expiry.read(answer);
|
|
1452
1275
|
const outcome = readOutcome(answer, CHUNK_REQUEST);
|
|
1453
1276
|
if (outcome.verdict !== TRANSFER_VERDICT.OK) {
|
|
1454
1277
|
return this.positionAfterFailure(transfer, failureOf(answer, outcome, CHUNK_REQUEST, transfer.context), offset);
|
|
@@ -1472,21 +1295,8 @@ export class MobileTusUploader {
|
|
|
1472
1295
|
return { offset: confirmed, published: confirmed === transfer.fileSize };
|
|
1473
1296
|
}
|
|
1474
1297
|
|
|
1475
|
-
/**
|
|
1298
|
+
/** Read the chunk at `offset` and PATCH it over XHR, resolving with Forge's answer. */
|
|
1476
1299
|
private async sendChunk(transfer: TusTransfer, offset: number, chunks: ChunkSource): Promise<TransferAnswer> {
|
|
1477
|
-
const backgroundUploader = chunks.transport === 'background' ? this.config.backgroundUploader : undefined;
|
|
1478
|
-
if (backgroundUploader && this.fitsOneBackgroundRequest(offset, transfer.fileSize, transfer.chunkSize)) {
|
|
1479
|
-
// The uploader takes no abort, so an abort that fires while this
|
|
1480
|
-
// request is in flight cannot stop it. It only ever carries a whole
|
|
1481
|
-
// single-chunk file, so a 2xx here means the transfer is complete: the
|
|
1482
|
-
// loop ends and the upload finishes normally (the caller verifies the
|
|
1483
|
-
// ingot), because a finished upload must never be reported as
|
|
1484
|
-
// cancelled, and a DELETE now would drop bytes Forge has already
|
|
1485
|
-
// finalized. Only an answer that leaves bytes missing reaches the abort
|
|
1486
|
-
// check before the next request.
|
|
1487
|
-
transfer.expiry.forget();
|
|
1488
|
-
return sendWholeFileInBackground(backgroundUploader, transfer, chunks.uri, offset);
|
|
1489
|
-
}
|
|
1490
1300
|
const chunkLength = Math.min(transfer.chunkSize, transfer.fileSize - offset);
|
|
1491
1301
|
const chunk = await this.readChunk(chunks.reader, chunks.uri, offset, chunkLength);
|
|
1492
1302
|
return sendChunkOverXhr(
|
|
@@ -1590,7 +1400,6 @@ export class MobileTusUploader {
|
|
|
1590
1400
|
'TUS finalization request',
|
|
1591
1401
|
transfer.context
|
|
1592
1402
|
);
|
|
1593
|
-
transfer.expiry.read(answer);
|
|
1594
1403
|
const outcome = readOutcome(answer, RECEIPT_REQUEST);
|
|
1595
1404
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1596
1405
|
return { offset: transfer.fileSize, published: true };
|
|
@@ -1667,25 +1476,12 @@ export class MobileTusUploader {
|
|
|
1667
1476
|
*/
|
|
1668
1477
|
async probeSession(session: { uploadUrl: string; istk: string }): Promise<TusSessionProbe | null> {
|
|
1669
1478
|
const uploadUrl = readKeptSessionUrl(session.uploadUrl, this.config.allowedDownloadHostPatterns);
|
|
1670
|
-
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {}
|
|
1479
|
+
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {} }, RESUME_HEAD_REQUEST);
|
|
1671
1480
|
if ('probe' in head) return head.probe;
|
|
1672
1481
|
if (head.failure.outcome.verdict === TRANSFER_VERDICT.GONE) return null;
|
|
1673
1482
|
throw head.failure.error;
|
|
1674
1483
|
}
|
|
1675
1484
|
|
|
1676
|
-
/**
|
|
1677
|
-
* Whether the remaining transfer is ONE PATCH the single-request uploader
|
|
1678
|
-
* (`backgroundUploader`) can send: nothing uploaded yet and the whole file
|
|
1679
|
-
* inside a single chunk. The uploader sends whole files and Forge rejects
|
|
1680
|
-
* any non-final chunk that is not exactly `X-Chunk-Size`, so a multi-chunk
|
|
1681
|
-
* file has no legal single request, and a partially uploaded session would
|
|
1682
|
-
* resend bytes Forge holds. A zero-byte file stays on the XHR path (it
|
|
1683
|
-
* sends no PATCH at all there).
|
|
1684
|
-
*/
|
|
1685
|
-
private fitsOneBackgroundRequest(offset: number, fileSize: number, chunkSize: number): boolean {
|
|
1686
|
-
return offset === 0 && fileSize > 0 && fileSize <= chunkSize;
|
|
1687
|
-
}
|
|
1688
|
-
|
|
1689
1485
|
/**
|
|
1690
1486
|
* Release a session the caller kept on Forge (`abortBehavior: 'keep'`)
|
|
1691
1487
|
* and will not resume after all: the tus DELETE lets Forge drop the chunk
|
|
@@ -1734,7 +1530,7 @@ export class MobileTusUploader {
|
|
|
1734
1530
|
// The OS job, when there is one, is cancelled before the DELETE so the
|
|
1735
1531
|
// engine stops PATCHing a session Forge is about to forget (see
|
|
1736
1532
|
// terminateSession); without an engine the DELETE leaves the device
|
|
1737
|
-
// synchronously
|
|
1533
|
+
// synchronously.
|
|
1738
1534
|
const backgroundTransfer = this.config.backgroundTransfer;
|
|
1739
1535
|
const terminate = backgroundTransfer
|
|
1740
1536
|
? this.cancelBackgroundTransfer(backgroundTransfer, uploadUrl, debug).then(() => this.requestTerminate(uploadUrl, istk))
|
|
@@ -1808,11 +1604,6 @@ export class MobileTusUploader {
|
|
|
1808
1604
|
);
|
|
1809
1605
|
}
|
|
1810
1606
|
|
|
1811
|
-
|
|
1812
|
-
// short read (file truncated/evicted mid-upload) must keep the
|
|
1813
|
-
// offset-fallback arithmetic and the stall guard honest about the bytes
|
|
1814
|
-
// actually sent.
|
|
1815
|
-
const padding = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0;
|
|
1816
|
-
return { base64, byteLength: (base64.length / 4) * 3 - padding };
|
|
1607
|
+
return { base64 };
|
|
1817
1608
|
}
|
|
1818
1609
|
}
|