@sparkvault/sdk-mobile 9.0.11 → 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/config.d.ts +2 -12
- package/dist/config.js +0 -1
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +14 -11
- package/dist/errors.js +21 -12
- package/dist/errors.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 +9 -16
- package/dist/ingots.js +0 -3
- package/dist/ingots.js.map +1 -1
- package/dist/tus.d.ts +9 -64
- package/dist/tus.js +24 -183
- package/dist/tus.js.map +1 -1
- package/dist/types.d.ts +10 -53
- package/package.json +6 -6
- package/src/config.ts +1 -13
- package/src/errors.ts +27 -17
- package/src/identity-dialog.tsx +2 -2
- package/src/index.ts +0 -1
- package/src/ingots.ts +10 -19
- package/src/tus.ts +36 -243
- package/src/types.ts +10 -51
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;
|
|
@@ -325,32 +299,6 @@ function sessionLostError(
|
|
|
325
299
|
});
|
|
326
300
|
}
|
|
327
301
|
|
|
328
|
-
/**
|
|
329
|
-
* How far past the expiry Forge named a session still counts as live: the
|
|
330
|
-
* allowance for a device clock running ahead of Forge's. `Upload-Expires` is
|
|
331
|
-
* read off Forge's clock and compared with the device's; a phone on automatic
|
|
332
|
-
* time is within seconds of Forge, one set by hand is typically minutes fast.
|
|
333
|
-
* The two mistakes cost very differently: a live session judged expired is
|
|
334
|
-
* dropped and its file sent again from byte 0, an expired one judged live
|
|
335
|
-
* costs a single HEAD (Forge answers 410, `sessionLost`). So the margin errs
|
|
336
|
-
* long, and stays small against the 72 h idle window a session lives from its
|
|
337
|
-
* last commit (`@sparkvault/app-sdk/upload-expiry`): a session the clock says
|
|
338
|
-
* lapsed hours ago is still dropped without a request.
|
|
339
|
-
*/
|
|
340
|
-
export const SESSION_EXPIRY_CLOCK_MARGIN_MS = 5 * 60 * 1000;
|
|
341
|
-
|
|
342
|
-
/**
|
|
343
|
-
* True when the expiry Forge named for a session (`TusSessionInfo.expiresAt`,
|
|
344
|
-
* kept current through `onSessionExpiry`) is past by more than the clock
|
|
345
|
-
* margin: Forge refuses every request to a session past its expiry, so a
|
|
346
|
-
* caller drops it without asking. Within the margin the session is live for
|
|
347
|
-
* all the device clock can tell, and Forge's HEAD decides. An unknown expiry
|
|
348
|
-
* (null) never has passed: only Forge's HEAD can say.
|
|
349
|
-
*/
|
|
350
|
-
export function isSessionExpired(expiresAt: number | null | undefined, now: number = Date.now()): boolean {
|
|
351
|
-
return typeof expiresAt === 'number' && now > expiresAt + SESSION_EXPIRY_CLOCK_MARGIN_MS;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
302
|
/**
|
|
355
303
|
* One answer to a transfer request, or its absence: what the transfer policy
|
|
356
304
|
* reads (the status and the headers) and what an error built from it says
|
|
@@ -523,57 +471,14 @@ function errorOf(
|
|
|
523
471
|
});
|
|
524
472
|
}
|
|
525
473
|
|
|
526
|
-
/**
|
|
527
|
-
* The session's expiry as its latest answer named it, reported to the
|
|
528
|
-
* caller (`onSessionExpiry`) each time it changes. An answer that names none
|
|
529
|
-
* leaves it unknown: the request may have moved it unread (a commit whose
|
|
530
|
-
* answer was lost, a deferred finalization), and a caller that dropped a
|
|
531
|
-
* session on an expiry Forge has since moved on would re-send a whole file.
|
|
532
|
-
* A request cut short by the caller's abort keeps the last named value: it
|
|
533
|
-
* can only have slid the expiry by that one request's duration.
|
|
534
|
-
*/
|
|
535
|
-
class SessionExpiry {
|
|
536
|
-
private known: number | null;
|
|
537
|
-
private readonly report?: (expiresAt: number | null) => void;
|
|
538
|
-
|
|
539
|
-
constructor(known: number | null, report?: (expiresAt: number | null) => void) {
|
|
540
|
-
this.known = known;
|
|
541
|
-
this.report = report;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
/** Forge answered (or did not): the expiry is what the answer names. */
|
|
545
|
-
read(answer: TransferAnswer): void {
|
|
546
|
-
this.become(parseUploadExpires(answer.header(UPLOAD_EXPIRES_HEADER)));
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
/**
|
|
550
|
-
* A request JS cannot follow while it runs is about to leave: the OS
|
|
551
|
-
* transfer engine's range, whose chunks commit (each sliding the expiry)
|
|
552
|
-
* with answers this upload never reads, possibly while the app is not
|
|
553
|
-
* running; or the single-chunk `backgroundUploader` PATCH, which reports
|
|
554
|
-
* nothing until it answers.
|
|
555
|
-
*/
|
|
556
|
-
forget(): void {
|
|
557
|
-
this.become(null);
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
private become(expiresAt: number | null): void {
|
|
561
|
-
if (expiresAt === this.known) return;
|
|
562
|
-
this.known = expiresAt;
|
|
563
|
-
this.report?.(expiresAt);
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
|
|
567
474
|
/**
|
|
568
475
|
* What every request to one session carries: where it lives, its token, the
|
|
569
|
-
* file it is for, the
|
|
570
|
-
* probe), and the caller's abort.
|
|
476
|
+
* file it is for, and the caller's abort.
|
|
571
477
|
*/
|
|
572
478
|
interface SessionLink {
|
|
573
479
|
readonly uploadUrl: string;
|
|
574
480
|
readonly istk: string;
|
|
575
481
|
readonly context: TusErrorContext;
|
|
576
|
-
readonly expiry: SessionExpiry | null;
|
|
577
482
|
readonly signal?: AbortSignal;
|
|
578
483
|
}
|
|
579
484
|
|
|
@@ -588,7 +493,6 @@ interface TusTransfer extends SessionLink {
|
|
|
588
493
|
readonly fileSize: number;
|
|
589
494
|
readonly chunkSize: number;
|
|
590
495
|
readonly budget: TransferBudget;
|
|
591
|
-
readonly expiry: SessionExpiry;
|
|
592
496
|
}
|
|
593
497
|
|
|
594
498
|
/** Where an upload stands: the server's offset, and whether the server confirmed the ingot published. */
|
|
@@ -597,11 +501,10 @@ interface TransferPosition {
|
|
|
597
501
|
readonly published: boolean;
|
|
598
502
|
}
|
|
599
503
|
|
|
600
|
-
/** 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. */
|
|
601
505
|
interface ChunkSource {
|
|
602
506
|
readonly reader: MobileFileReader;
|
|
603
507
|
readonly uri: string;
|
|
604
|
-
readonly transport?: UploadTransport;
|
|
605
508
|
/** Bytes of the chunk at `offset` that have left the device so far. */
|
|
606
509
|
readonly onChunkProgress: (offset: number, chunkBytesUploaded: number) => void;
|
|
607
510
|
}
|
|
@@ -733,20 +636,6 @@ async function fetchAnswer(
|
|
|
733
636
|
);
|
|
734
637
|
}
|
|
735
638
|
|
|
736
|
-
/**
|
|
737
|
-
* Case-insensitive header lookup for adapter results. Native HTTP stacks
|
|
738
|
-
* hand headers back in whatever case they please (iOS lower-cases them),
|
|
739
|
-
* while Forge writes `Upload-Offset`.
|
|
740
|
-
*/
|
|
741
|
-
function getHeaderIgnoreCase(headers: Record<string, string> | undefined, name: string): string | null {
|
|
742
|
-
if (!headers) return null;
|
|
743
|
-
const wanted = name.toLowerCase();
|
|
744
|
-
for (const key of Object.keys(headers)) {
|
|
745
|
-
if (key.toLowerCase() === wanted) return headers[key];
|
|
746
|
-
}
|
|
747
|
-
return null;
|
|
748
|
-
}
|
|
749
|
-
|
|
750
639
|
/**
|
|
751
640
|
* Where a tus upload starts: a fresh session at the Forge endpoint, or a
|
|
752
641
|
* persisted one resumed by URL. Resolved up front so a misconfigured call
|
|
@@ -754,7 +643,7 @@ function getHeaderIgnoreCase(headers: Record<string, string> | undefined, name:
|
|
|
754
643
|
*/
|
|
755
644
|
type TusSessionSource =
|
|
756
645
|
| { mode: 'create'; parsed: ParsedForgeUrl }
|
|
757
|
-
| { mode: 'resume'; uploadUrl: string; istk: string
|
|
646
|
+
| { mode: 'resume'; uploadUrl: string; istk: string };
|
|
758
647
|
|
|
759
648
|
function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatterns: readonly RegExp[]): TusSessionSource {
|
|
760
649
|
if (options.resume) {
|
|
@@ -764,11 +653,7 @@ function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatte
|
|
|
764
653
|
if (!istk) {
|
|
765
654
|
throw new SparkVaultValidationError('resume.istk or forgeUrl is required to resume a TUS upload session');
|
|
766
655
|
}
|
|
767
|
-
|
|
768
|
-
if (expiresAt !== null && !Number.isFinite(expiresAt)) {
|
|
769
|
-
throw new SparkVaultValidationError('resume.expiresAt is an epoch-ms expiry or null', { received: expiresAt });
|
|
770
|
-
}
|
|
771
|
-
return { mode: 'resume', uploadUrl, istk, expiresAt };
|
|
656
|
+
return { mode: 'resume', uploadUrl, istk };
|
|
772
657
|
}
|
|
773
658
|
if (!options.forgeUrl) {
|
|
774
659
|
throw new SparkVaultValidationError('forgeUrl is required to create a TUS upload session');
|
|
@@ -777,15 +662,15 @@ function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatte
|
|
|
777
662
|
}
|
|
778
663
|
|
|
779
664
|
/**
|
|
780
|
-
* The session a create's 2xx opened: where it lives
|
|
781
|
-
*
|
|
782
|
-
*
|
|
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.
|
|
783
668
|
*/
|
|
784
669
|
function readCreatedSession(
|
|
785
670
|
answer: TransferAnswer,
|
|
786
671
|
endpoint: string,
|
|
787
672
|
context: TusErrorContext
|
|
788
|
-
): { uploadUrl: string; chunkSize: number
|
|
673
|
+
): { uploadUrl: string; chunkSize: number } {
|
|
789
674
|
const uploadUrl = resolveForgeSessionUrl(answer.header('Location'), endpoint);
|
|
790
675
|
if (!uploadUrl) {
|
|
791
676
|
throw new TusUploadError('TUS create response Location header is missing or outside the upload endpoint', {
|
|
@@ -807,14 +692,14 @@ function readCreatedSession(
|
|
|
807
692
|
});
|
|
808
693
|
}
|
|
809
694
|
|
|
810
|
-
return { uploadUrl, chunkSize
|
|
695
|
+
return { uploadUrl, chunkSize };
|
|
811
696
|
}
|
|
812
697
|
|
|
813
698
|
/**
|
|
814
699
|
* Where a HEAD's 2xx says the session stands: offset, length and chunk
|
|
815
700
|
* geometry, all required (a 2xx without them is not Forge, a captive
|
|
816
|
-
* portal's page being the classic case)
|
|
817
|
-
*
|
|
701
|
+
* portal's page being the classic case). An offset past the length can only
|
|
702
|
+
* be a corrupt session.
|
|
818
703
|
*/
|
|
819
704
|
function readSessionProbe(answer: TransferAnswer, phase: TusUploadError['phase'], context: TusErrorContext): TusSessionProbe {
|
|
820
705
|
const offset = parseIntegerHeader(answer.header('Upload-Offset'), 0);
|
|
@@ -826,7 +711,7 @@ function readSessionProbe(answer: TransferAnswer, phase: TusUploadError['phase']
|
|
|
826
711
|
{ ...context, phase }
|
|
827
712
|
);
|
|
828
713
|
}
|
|
829
|
-
return { offset, length, chunkSize
|
|
714
|
+
return { offset, length, chunkSize };
|
|
830
715
|
}
|
|
831
716
|
|
|
832
717
|
/**
|
|
@@ -969,55 +854,6 @@ function sendChunkOverXhr(
|
|
|
969
854
|
});
|
|
970
855
|
}
|
|
971
856
|
|
|
972
|
-
/**
|
|
973
|
-
* PATCH a whole file as the session's only chunk through the app's
|
|
974
|
-
* single-request uploader (`backgroundUploader`) and resolve with Forge's
|
|
975
|
-
* answer, or with no answer when the adapter got none. The request reports
|
|
976
|
-
* no progress and takes no abort; whether it outlives a suspension is the
|
|
977
|
-
* uploader's (MobileBackgroundUploader). Its answer is read exactly like the
|
|
978
|
-
* XHR path's, so callers see one error taxonomy.
|
|
979
|
-
*/
|
|
980
|
-
async function sendWholeFileInBackground(
|
|
981
|
-
uploader: MobileBackgroundUploader,
|
|
982
|
-
transfer: TusTransfer,
|
|
983
|
-
fileUri: string,
|
|
984
|
-
offset: number
|
|
985
|
-
): Promise<TransferAnswer> {
|
|
986
|
-
let result: Awaited<ReturnType<MobileBackgroundUploader['uploadFile']>>;
|
|
987
|
-
try {
|
|
988
|
-
result = await uploader.uploadFile(transfer.uploadUrl, fileUri, {
|
|
989
|
-
method: 'PATCH',
|
|
990
|
-
headers: {
|
|
991
|
-
'Tus-Resumable': TUS_VERSION,
|
|
992
|
-
'Upload-Offset': String(offset),
|
|
993
|
-
'Content-Type': 'application/offset+octet-stream',
|
|
994
|
-
'X-ISTK': transfer.istk,
|
|
995
|
-
},
|
|
996
|
-
});
|
|
997
|
-
} catch (err) {
|
|
998
|
-
// The adapter only throws when no HTTP response came back (offline, DNS,
|
|
999
|
-
// the OS cancelled the task); an HTTP failure is a status below.
|
|
1000
|
-
return noAnswer(new TusUploadError(`Background chunk upload failed: ${getErrorMessage(err)}`, {
|
|
1001
|
-
cause: err instanceof Error ? err : null,
|
|
1002
|
-
...transfer.context,
|
|
1003
|
-
phase: 'network',
|
|
1004
|
-
}));
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
if (!Number.isSafeInteger(result.status) || result.status <= 0) {
|
|
1008
|
-
return noAnswer(new TusUploadError(`Background chunk upload got no answer (status ${String(result.status)})`, {
|
|
1009
|
-
...transfer.context,
|
|
1010
|
-
phase: 'network',
|
|
1011
|
-
}));
|
|
1012
|
-
}
|
|
1013
|
-
const answered = result.status >= 200 && result.status < 300;
|
|
1014
|
-
return answerOf(
|
|
1015
|
-
result.status,
|
|
1016
|
-
name => getHeaderIgnoreCase(result.headers, name),
|
|
1017
|
-
!answered && typeof result.body === 'string' ? result.body : ''
|
|
1018
|
-
);
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
857
|
/**
|
|
1022
858
|
* Translate what the OS transfer adapter reports into the SDK's one error
|
|
1023
859
|
* taxonomy, so a caller sees the same `TusUploadError` phases and typed gate
|
|
@@ -1183,20 +1019,12 @@ export class MobileTusUploader {
|
|
|
1183
1019
|
let position: TransferPosition;
|
|
1184
1020
|
if (source.mode === 'resume') {
|
|
1185
1021
|
debug?.log(`Resuming TUS upload for ${options.filename}`);
|
|
1186
|
-
//
|
|
1187
|
-
//
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
throw new TusUploadError(`TUS session expired at ${new Date(heldExpiry).toISOString()}`, {
|
|
1191
|
-
...context,
|
|
1192
|
-
phase: 'create',
|
|
1193
|
-
sessionLost: true,
|
|
1194
|
-
});
|
|
1195
|
-
}
|
|
1196
|
-
const expiry = new SessionExpiry(source.expiresAt, options.onSessionExpiry);
|
|
1197
|
-
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 };
|
|
1198
1026
|
const probe = await this.readResumePoint(link, budget, fileSize);
|
|
1199
|
-
transfer = { ...link,
|
|
1027
|
+
transfer = { ...link, fileSize, chunkSize: probe.chunkSize, budget };
|
|
1200
1028
|
position = { offset: checkedServerOffset(probe.offset, transfer, 'create', context), published: false };
|
|
1201
1029
|
debug?.log(`TUS session for ${options.filename} is at offset ${position.offset}/${fileSize}`);
|
|
1202
1030
|
} else {
|
|
@@ -1207,7 +1035,6 @@ export class MobileTusUploader {
|
|
|
1207
1035
|
uploadUrl: created.uploadUrl,
|
|
1208
1036
|
istk,
|
|
1209
1037
|
context,
|
|
1210
|
-
expiry: new SessionExpiry(created.expiresAt, options.onSessionExpiry),
|
|
1211
1038
|
signal,
|
|
1212
1039
|
fileSize,
|
|
1213
1040
|
chunkSize: created.chunkSize,
|
|
@@ -1220,7 +1047,6 @@ export class MobileTusUploader {
|
|
|
1220
1047
|
uploadUrl: created.uploadUrl,
|
|
1221
1048
|
istk,
|
|
1222
1049
|
chunkSize: created.chunkSize,
|
|
1223
|
-
expiresAt: created.expiresAt,
|
|
1224
1050
|
});
|
|
1225
1051
|
}
|
|
1226
1052
|
|
|
@@ -1228,14 +1054,12 @@ export class MobileTusUploader {
|
|
|
1228
1054
|
// remaining range in a single hand-off: it chains the chunks itself and
|
|
1229
1055
|
// keeps going while the app is suspended or killed, which no JS loop
|
|
1230
1056
|
// can. Any size and any offset qualify, so a resumed multi-GB video
|
|
1231
|
-
// rides it too;
|
|
1232
|
-
//
|
|
1233
|
-
//
|
|
1234
|
-
// 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.
|
|
1235
1060
|
const backgroundTransfer = options.transport === 'background' ? this.config.backgroundTransfer : undefined;
|
|
1236
1061
|
if (backgroundTransfer && position.offset < fileSize) {
|
|
1237
1062
|
ensureNotAborted(signal);
|
|
1238
|
-
transfer.expiry.forget();
|
|
1239
1063
|
const policy = options.transferPolicy ?? {};
|
|
1240
1064
|
const finalOffset = await transferRemainderInBackground(
|
|
1241
1065
|
backgroundTransfer,
|
|
@@ -1267,15 +1091,13 @@ export class MobileTusUploader {
|
|
|
1267
1091
|
// Forge answers only once the ingot is published. An abort that lands
|
|
1268
1092
|
// after the OS delivered the last byte changes nothing: the transfer
|
|
1269
1093
|
// is complete, and the loop below (with its abort check) has nothing
|
|
1270
|
-
// left to do.
|
|
1271
|
-
// upload is never reported as cancelled.
|
|
1094
|
+
// left to do. A finished upload is never reported as cancelled.
|
|
1272
1095
|
position = { offset: fileSize, published: true };
|
|
1273
1096
|
}
|
|
1274
1097
|
|
|
1275
1098
|
const chunks: ChunkSource = {
|
|
1276
1099
|
reader: fileReader,
|
|
1277
1100
|
uri: cleanUri,
|
|
1278
|
-
transport: options.transport,
|
|
1279
1101
|
onChunkProgress: (offset, chunkBytesUploaded) => report(offset + chunkBytesUploaded),
|
|
1280
1102
|
};
|
|
1281
1103
|
while (!position.published) {
|
|
@@ -1314,9 +1136,9 @@ export class MobileTusUploader {
|
|
|
1314
1136
|
// in a generic TusUploadError.
|
|
1315
1137
|
if (err instanceof SparkVaultMobileError) throw err;
|
|
1316
1138
|
// Only a fileReader read or a caller's hook (onSessionCreated,
|
|
1317
|
-
//
|
|
1318
|
-
//
|
|
1319
|
-
//
|
|
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
|
|
1320
1142
|
// context and nothing inferred about them.
|
|
1321
1143
|
throw TusUploadError.fromError(error, {
|
|
1322
1144
|
filename: options.filename,
|
|
@@ -1343,7 +1165,7 @@ export class MobileTusUploader {
|
|
|
1343
1165
|
budget: TransferBudget,
|
|
1344
1166
|
context: TusErrorContext,
|
|
1345
1167
|
signal?: AbortSignal
|
|
1346
|
-
): Promise<{ uploadUrl: string; chunkSize: number
|
|
1168
|
+
): Promise<{ uploadUrl: string; chunkSize: number }> {
|
|
1347
1169
|
const jitterKey = transferJitterKey(parsed.tusEndpoint);
|
|
1348
1170
|
for (;;) {
|
|
1349
1171
|
ensureNotAborted(signal);
|
|
@@ -1435,7 +1257,6 @@ export class MobileTusUploader {
|
|
|
1435
1257
|
'TUS HEAD request',
|
|
1436
1258
|
link.context
|
|
1437
1259
|
);
|
|
1438
|
-
link.expiry?.read(answer);
|
|
1439
1260
|
const outcome = readOutcome(answer, request);
|
|
1440
1261
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1441
1262
|
return { probe: readSessionProbe(answer, request.phase, link.context) };
|
|
@@ -1451,7 +1272,6 @@ export class MobileTusUploader {
|
|
|
1451
1272
|
*/
|
|
1452
1273
|
private async sendChunkAt(transfer: TusTransfer, offset: number, chunks: ChunkSource): Promise<TransferPosition> {
|
|
1453
1274
|
const answer = await this.sendChunk(transfer, offset, chunks);
|
|
1454
|
-
transfer.expiry.read(answer);
|
|
1455
1275
|
const outcome = readOutcome(answer, CHUNK_REQUEST);
|
|
1456
1276
|
if (outcome.verdict !== TRANSFER_VERDICT.OK) {
|
|
1457
1277
|
return this.positionAfterFailure(transfer, failureOf(answer, outcome, CHUNK_REQUEST, transfer.context), offset);
|
|
@@ -1475,21 +1295,8 @@ export class MobileTusUploader {
|
|
|
1475
1295
|
return { offset: confirmed, published: confirmed === transfer.fileSize };
|
|
1476
1296
|
}
|
|
1477
1297
|
|
|
1478
|
-
/**
|
|
1298
|
+
/** Read the chunk at `offset` and PATCH it over XHR, resolving with Forge's answer. */
|
|
1479
1299
|
private async sendChunk(transfer: TusTransfer, offset: number, chunks: ChunkSource): Promise<TransferAnswer> {
|
|
1480
|
-
const backgroundUploader = chunks.transport === 'background' ? this.config.backgroundUploader : undefined;
|
|
1481
|
-
if (backgroundUploader && this.fitsOneBackgroundRequest(offset, transfer.fileSize, transfer.chunkSize)) {
|
|
1482
|
-
// The uploader takes no abort, so an abort that fires while this
|
|
1483
|
-
// request is in flight cannot stop it. It only ever carries a whole
|
|
1484
|
-
// single-chunk file, so a 2xx here means the transfer is complete: the
|
|
1485
|
-
// loop ends and the upload finishes normally (the caller verifies the
|
|
1486
|
-
// ingot), because a finished upload must never be reported as
|
|
1487
|
-
// cancelled, and a DELETE now would drop bytes Forge has already
|
|
1488
|
-
// finalized. Only an answer that leaves bytes missing reaches the abort
|
|
1489
|
-
// check before the next request.
|
|
1490
|
-
transfer.expiry.forget();
|
|
1491
|
-
return sendWholeFileInBackground(backgroundUploader, transfer, chunks.uri, offset);
|
|
1492
|
-
}
|
|
1493
1300
|
const chunkLength = Math.min(transfer.chunkSize, transfer.fileSize - offset);
|
|
1494
1301
|
const chunk = await this.readChunk(chunks.reader, chunks.uri, offset, chunkLength);
|
|
1495
1302
|
return sendChunkOverXhr(
|
|
@@ -1593,7 +1400,6 @@ export class MobileTusUploader {
|
|
|
1593
1400
|
'TUS finalization request',
|
|
1594
1401
|
transfer.context
|
|
1595
1402
|
);
|
|
1596
|
-
transfer.expiry.read(answer);
|
|
1597
1403
|
const outcome = readOutcome(answer, RECEIPT_REQUEST);
|
|
1598
1404
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1599
1405
|
return { offset: transfer.fileSize, published: true };
|
|
@@ -1670,25 +1476,12 @@ export class MobileTusUploader {
|
|
|
1670
1476
|
*/
|
|
1671
1477
|
async probeSession(session: { uploadUrl: string; istk: string }): Promise<TusSessionProbe | null> {
|
|
1672
1478
|
const uploadUrl = readKeptSessionUrl(session.uploadUrl, this.config.allowedDownloadHostPatterns);
|
|
1673
|
-
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {}
|
|
1479
|
+
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {} }, RESUME_HEAD_REQUEST);
|
|
1674
1480
|
if ('probe' in head) return head.probe;
|
|
1675
1481
|
if (head.failure.outcome.verdict === TRANSFER_VERDICT.GONE) return null;
|
|
1676
1482
|
throw head.failure.error;
|
|
1677
1483
|
}
|
|
1678
1484
|
|
|
1679
|
-
/**
|
|
1680
|
-
* Whether the remaining transfer is ONE PATCH the single-request uploader
|
|
1681
|
-
* (`backgroundUploader`) can send: nothing uploaded yet and the whole file
|
|
1682
|
-
* inside a single chunk. The uploader sends whole files and Forge rejects
|
|
1683
|
-
* any non-final chunk that is not exactly `X-Chunk-Size`, so a multi-chunk
|
|
1684
|
-
* file has no legal single request, and a partially uploaded session would
|
|
1685
|
-
* resend bytes Forge holds. A zero-byte file stays on the XHR path (it
|
|
1686
|
-
* sends no PATCH at all there).
|
|
1687
|
-
*/
|
|
1688
|
-
private fitsOneBackgroundRequest(offset: number, fileSize: number, chunkSize: number): boolean {
|
|
1689
|
-
return offset === 0 && fileSize > 0 && fileSize <= chunkSize;
|
|
1690
|
-
}
|
|
1691
|
-
|
|
1692
1485
|
/**
|
|
1693
1486
|
* Release a session the caller kept on Forge (`abortBehavior: 'keep'`)
|
|
1694
1487
|
* and will not resume after all: the tus DELETE lets Forge drop the chunk
|
|
@@ -1737,7 +1530,7 @@ export class MobileTusUploader {
|
|
|
1737
1530
|
// The OS job, when there is one, is cancelled before the DELETE so the
|
|
1738
1531
|
// engine stops PATCHing a session Forge is about to forget (see
|
|
1739
1532
|
// terminateSession); without an engine the DELETE leaves the device
|
|
1740
|
-
// synchronously
|
|
1533
|
+
// synchronously.
|
|
1741
1534
|
const backgroundTransfer = this.config.backgroundTransfer;
|
|
1742
1535
|
const terminate = backgroundTransfer
|
|
1743
1536
|
? this.cancelBackgroundTransfer(backgroundTransfer, uploadUrl, debug).then(() => this.requestTerminate(uploadUrl, istk))
|
package/src/types.ts
CHANGED
|
@@ -1034,23 +1034,15 @@ export interface MobileFileReader {
|
|
|
1034
1034
|
|
|
1035
1035
|
/**
|
|
1036
1036
|
* A tus upload session as Forge created it: the session URL, the per-upload
|
|
1037
|
-
* token every request on it must carry, the chunk geometry Forge fixed
|
|
1038
|
-
*
|
|
1039
|
-
*
|
|
1040
|
-
* Forge holds.
|
|
1037
|
+
* token every request on it must carry, and the chunk geometry Forge fixed
|
|
1038
|
+
* for it. Persist it to resume the upload after a failed attempt or a
|
|
1039
|
+
* relaunch (`ingots.resumeUpload`) instead of re-sending bytes Forge holds;
|
|
1040
|
+
* the resume asks Forge (HEAD) whether it still holds the session.
|
|
1041
1041
|
*/
|
|
1042
1042
|
export interface TusSessionInfo {
|
|
1043
1043
|
uploadUrl: string;
|
|
1044
1044
|
istk: string;
|
|
1045
1045
|
chunkSize: number;
|
|
1046
|
-
/**
|
|
1047
|
-
* When the session lapses (epoch ms, Forge's `Upload-Expires`), or null
|
|
1048
|
-
* when Forge named no expiry. Forge slides it forward on every committed
|
|
1049
|
-
* chunk, so a caller that persists the session keeps this current through
|
|
1050
|
-
* the upload's `onSessionExpiry`; past it, Forge refuses every request to
|
|
1051
|
-
* the session (`isSessionExpired`).
|
|
1052
|
-
*/
|
|
1053
|
-
expiresAt: number | null;
|
|
1054
1046
|
}
|
|
1055
1047
|
|
|
1056
1048
|
/**
|
|
@@ -1076,15 +1068,11 @@ export interface IngotUploadSession extends TusSessionInfo {
|
|
|
1076
1068
|
/**
|
|
1077
1069
|
* Which transport a tus upload rides. `foreground` (the default) streams
|
|
1078
1070
|
* chunks over XHR with progress events, abort, and the stall watchdog.
|
|
1079
|
-
* `background` asks for the transfer to survive the app being suspended
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1082
|
-
*
|
|
1083
|
-
*
|
|
1084
|
-
* chunk, sent as one PATCH with no progress and no abort; whether it
|
|
1085
|
-
* outlives a suspension is the uploader's, see MobileBackgroundUploader),
|
|
1086
|
-
* else plain XHR. The upload semantics are the same on every engine; only
|
|
1087
|
-
* what survives a suspension differs.
|
|
1071
|
+
* `background` asks for the transfer to survive the app being suspended: the
|
|
1072
|
+
* OS transfer engine (`backgroundTransfer`: any file size, any offset,
|
|
1073
|
+
* progress and abort, chunks chained natively while the app is suspended)
|
|
1074
|
+
* takes it when the app provides one, and plain XHR otherwise. The upload
|
|
1075
|
+
* semantics are the same on both; only what survives a suspension differs.
|
|
1088
1076
|
*/
|
|
1089
1077
|
export type UploadTransport = 'foreground' | 'background';
|
|
1090
1078
|
|
|
@@ -1139,7 +1127,7 @@ export interface MobileBackgroundTransfer {
|
|
|
1139
1127
|
cancel(uploadUrl: string): Promise<void>;
|
|
1140
1128
|
}
|
|
1141
1129
|
|
|
1142
|
-
/** Network policy an upload carries into the OS transfer engine; ignored by the XHR
|
|
1130
|
+
/** Network policy an upload carries into the OS transfer engine; ignored by the XHR path (JS gates it). Defaults: both true. */
|
|
1143
1131
|
export interface UploadTransferPolicy {
|
|
1144
1132
|
allowsCellularAccess?: boolean;
|
|
1145
1133
|
allowsConstrainedNetworkAccess?: boolean;
|
|
@@ -1160,35 +1148,6 @@ export interface UploadTransferPolicy {
|
|
|
1160
1148
|
*/
|
|
1161
1149
|
export type UploadAbortBehavior = 'terminate' | 'keep';
|
|
1162
1150
|
|
|
1163
|
-
/**
|
|
1164
|
-
* App-provided adapter that PATCHes a whole local file as the request body.
|
|
1165
|
-
* The fallback behind `backgroundTransfer`: the tus uploader reaches for it
|
|
1166
|
-
* only when no OS transfer engine is configured and the file fits one chunk;
|
|
1167
|
-
* everything else keeps the streaming XHR path.
|
|
1168
|
-
*
|
|
1169
|
-
* The SDK does not ask for an OS background session here, and a host should
|
|
1170
|
-
* think twice before backing this with one. A background session hands the
|
|
1171
|
-
* body's path to a system daemon that opens the file in its own process,
|
|
1172
|
-
* outside whatever sandbox extensions the app holds; a file the app can read
|
|
1173
|
-
* is not necessarily one the daemon can. Where `fileUri` may point outside
|
|
1174
|
-
* the app's container - a photo library original, a document-picker result -
|
|
1175
|
-
* a background session fails at open() before a byte moves, and reports it
|
|
1176
|
-
* as a transport error indistinguishable from a dead connection. A host that
|
|
1177
|
-
* wants suspension-survival should stage the body into its own container
|
|
1178
|
-
* first, the way the OS transfer engine does, or supply
|
|
1179
|
-
* `backgroundTransfer` instead. Note that opting out can take an explicit
|
|
1180
|
-
* flag: expo-file-system's `uploadAsync` writes `sessionType: BACKGROUND`
|
|
1181
|
-
* into its options before spreading the caller's, so omitting the key selects
|
|
1182
|
-
* a background session rather than declining one.
|
|
1183
|
-
*/
|
|
1184
|
-
export interface MobileBackgroundUploader {
|
|
1185
|
-
uploadFile(
|
|
1186
|
-
url: string,
|
|
1187
|
-
fileUri: string,
|
|
1188
|
-
options: { method: 'PATCH'; headers: Record<string, string> }
|
|
1189
|
-
): Promise<{ status: number; headers: Record<string, string>; body: string }>;
|
|
1190
|
-
}
|
|
1191
|
-
|
|
1192
1151
|
export interface MobileDownloadProgress {
|
|
1193
1152
|
bytesWritten: number;
|
|
1194
1153
|
bytesTotal?: number;
|