@sparkvault/sdk-mobile 9.0.11 → 10.0.1
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 +15 -5
- package/dist/app-sdk/ids.d.ts +1 -0
- package/dist/app-sdk/ids.js +1 -0
- package/dist/app-sdk/ids.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 +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 +26 -185
- package/dist/tus.js.map +1 -1
- package/dist/types.d.ts +14 -54
- package/package.json +6 -6
- package/src/app-sdk/ids.js +1 -0
- 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 +49 -254
- package/src/types.ts +14 -52
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;
|
|
@@ -260,15 +234,17 @@ export function isCancelledTransfer(err: unknown, signal: AbortSignal | undefine
|
|
|
260
234
|
}
|
|
261
235
|
|
|
262
236
|
/**
|
|
263
|
-
* A file chunk kept in the base64 form the
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
237
|
+
* A file chunk kept in the base64 form the fileReader returned; the SDK never
|
|
238
|
+
* decodes it: React Native's networking accepts a `{ base64 }` request body
|
|
239
|
+
* verbatim (convertRequestBody passes plain objects through; RCTNetworking.mm /
|
|
240
|
+
* NetworkingModule.kt decode the `base64` key natively), so handing the string
|
|
241
|
+
* straight to XHR skips both JS-thread char-loops that a Uint8Array round-trip
|
|
242
|
+
* would cost — the pure-JS base64 decode AND React Native's re-encode of
|
|
243
|
+
* ArrayBuffer bodies back to base64. On Hermes those two passes block the JS
|
|
244
|
+
* thread for seconds per chunk, which starves touch handling for the whole
|
|
245
|
+
* upload session. A fileReader that reads the bytes into JS pays one encode
|
|
246
|
+
* pass per chunk (the mobile app's does: expo-file-system's one native ranged
|
|
247
|
+
* base64 read takes its position as a 32-bit Int on Android).
|
|
272
248
|
*/
|
|
273
249
|
interface TusChunk {
|
|
274
250
|
base64: string;
|
|
@@ -325,32 +301,6 @@ function sessionLostError(
|
|
|
325
301
|
});
|
|
326
302
|
}
|
|
327
303
|
|
|
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
304
|
/**
|
|
355
305
|
* One answer to a transfer request, or its absence: what the transfer policy
|
|
356
306
|
* reads (the status and the headers) and what an error built from it says
|
|
@@ -523,57 +473,14 @@ function errorOf(
|
|
|
523
473
|
});
|
|
524
474
|
}
|
|
525
475
|
|
|
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
476
|
/**
|
|
568
477
|
* 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.
|
|
478
|
+
* file it is for, and the caller's abort.
|
|
571
479
|
*/
|
|
572
480
|
interface SessionLink {
|
|
573
481
|
readonly uploadUrl: string;
|
|
574
482
|
readonly istk: string;
|
|
575
483
|
readonly context: TusErrorContext;
|
|
576
|
-
readonly expiry: SessionExpiry | null;
|
|
577
484
|
readonly signal?: AbortSignal;
|
|
578
485
|
}
|
|
579
486
|
|
|
@@ -588,7 +495,6 @@ interface TusTransfer extends SessionLink {
|
|
|
588
495
|
readonly fileSize: number;
|
|
589
496
|
readonly chunkSize: number;
|
|
590
497
|
readonly budget: TransferBudget;
|
|
591
|
-
readonly expiry: SessionExpiry;
|
|
592
498
|
}
|
|
593
499
|
|
|
594
500
|
/** Where an upload stands: the server's offset, and whether the server confirmed the ingot published. */
|
|
@@ -597,11 +503,10 @@ interface TransferPosition {
|
|
|
597
503
|
readonly published: boolean;
|
|
598
504
|
}
|
|
599
505
|
|
|
600
|
-
/** The file a chunk is read from, and how
|
|
506
|
+
/** The file a chunk is read from, and who hears how much of each chunk has left. */
|
|
601
507
|
interface ChunkSource {
|
|
602
508
|
readonly reader: MobileFileReader;
|
|
603
509
|
readonly uri: string;
|
|
604
|
-
readonly transport?: UploadTransport;
|
|
605
510
|
/** Bytes of the chunk at `offset` that have left the device so far. */
|
|
606
511
|
readonly onChunkProgress: (offset: number, chunkBytesUploaded: number) => void;
|
|
607
512
|
}
|
|
@@ -733,20 +638,6 @@ async function fetchAnswer(
|
|
|
733
638
|
);
|
|
734
639
|
}
|
|
735
640
|
|
|
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
641
|
/**
|
|
751
642
|
* Where a tus upload starts: a fresh session at the Forge endpoint, or a
|
|
752
643
|
* persisted one resumed by URL. Resolved up front so a misconfigured call
|
|
@@ -754,7 +645,7 @@ function getHeaderIgnoreCase(headers: Record<string, string> | undefined, name:
|
|
|
754
645
|
*/
|
|
755
646
|
type TusSessionSource =
|
|
756
647
|
| { mode: 'create'; parsed: ParsedForgeUrl }
|
|
757
|
-
| { mode: 'resume'; uploadUrl: string; istk: string
|
|
648
|
+
| { mode: 'resume'; uploadUrl: string; istk: string };
|
|
758
649
|
|
|
759
650
|
function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatterns: readonly RegExp[]): TusSessionSource {
|
|
760
651
|
if (options.resume) {
|
|
@@ -764,11 +655,7 @@ function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatte
|
|
|
764
655
|
if (!istk) {
|
|
765
656
|
throw new SparkVaultValidationError('resume.istk or forgeUrl is required to resume a TUS upload session');
|
|
766
657
|
}
|
|
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 };
|
|
658
|
+
return { mode: 'resume', uploadUrl, istk };
|
|
772
659
|
}
|
|
773
660
|
if (!options.forgeUrl) {
|
|
774
661
|
throw new SparkVaultValidationError('forgeUrl is required to create a TUS upload session');
|
|
@@ -777,15 +664,15 @@ function resolveSessionSource(options: TusUploadFromUriOptions, allowedHostPatte
|
|
|
777
664
|
}
|
|
778
665
|
|
|
779
666
|
/**
|
|
780
|
-
* The session a create's 2xx opened: where it lives
|
|
781
|
-
*
|
|
782
|
-
*
|
|
667
|
+
* The session a create's 2xx opened: where it lives and its chunk geometry.
|
|
668
|
+
* Every chunk carries the file's bytes, so the session must live under the
|
|
669
|
+
* endpoint it was created at.
|
|
783
670
|
*/
|
|
784
671
|
function readCreatedSession(
|
|
785
672
|
answer: TransferAnswer,
|
|
786
673
|
endpoint: string,
|
|
787
674
|
context: TusErrorContext
|
|
788
|
-
): { uploadUrl: string; chunkSize: number
|
|
675
|
+
): { uploadUrl: string; chunkSize: number } {
|
|
789
676
|
const uploadUrl = resolveForgeSessionUrl(answer.header('Location'), endpoint);
|
|
790
677
|
if (!uploadUrl) {
|
|
791
678
|
throw new TusUploadError('TUS create response Location header is missing or outside the upload endpoint', {
|
|
@@ -807,14 +694,14 @@ function readCreatedSession(
|
|
|
807
694
|
});
|
|
808
695
|
}
|
|
809
696
|
|
|
810
|
-
return { uploadUrl, chunkSize
|
|
697
|
+
return { uploadUrl, chunkSize };
|
|
811
698
|
}
|
|
812
699
|
|
|
813
700
|
/**
|
|
814
701
|
* Where a HEAD's 2xx says the session stands: offset, length and chunk
|
|
815
702
|
* geometry, all required (a 2xx without them is not Forge, a captive
|
|
816
|
-
* portal's page being the classic case)
|
|
817
|
-
*
|
|
703
|
+
* portal's page being the classic case). An offset past the length can only
|
|
704
|
+
* be a corrupt session.
|
|
818
705
|
*/
|
|
819
706
|
function readSessionProbe(answer: TransferAnswer, phase: TusUploadError['phase'], context: TusErrorContext): TusSessionProbe {
|
|
820
707
|
const offset = parseIntegerHeader(answer.header('Upload-Offset'), 0);
|
|
@@ -826,7 +713,7 @@ function readSessionProbe(answer: TransferAnswer, phase: TusUploadError['phase']
|
|
|
826
713
|
{ ...context, phase }
|
|
827
714
|
);
|
|
828
715
|
}
|
|
829
|
-
return { offset, length, chunkSize
|
|
716
|
+
return { offset, length, chunkSize };
|
|
830
717
|
}
|
|
831
718
|
|
|
832
719
|
/**
|
|
@@ -964,60 +851,11 @@ function sendChunkOverXhr(
|
|
|
964
851
|
CHUNK_ABSOLUTE_CAP_MS
|
|
965
852
|
);
|
|
966
853
|
// React Native-only body shape (see TusChunk): the native layer decodes
|
|
967
|
-
// `base64` itself, so the
|
|
854
|
+
// `base64` itself, so the SDK never decodes the chunk on the JS thread.
|
|
968
855
|
xhr.send({ base64: chunk.base64 } as unknown as XMLHttpRequestBodyInit);
|
|
969
856
|
});
|
|
970
857
|
}
|
|
971
858
|
|
|
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
859
|
/**
|
|
1022
860
|
* Translate what the OS transfer adapter reports into the SDK's one error
|
|
1023
861
|
* taxonomy, so a caller sees the same `TusUploadError` phases and typed gate
|
|
@@ -1183,20 +1021,12 @@ export class MobileTusUploader {
|
|
|
1183
1021
|
let position: TransferPosition;
|
|
1184
1022
|
if (source.mode === 'resume') {
|
|
1185
1023
|
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 };
|
|
1024
|
+
// Only Forge knows whether it still holds the session: its HEAD
|
|
1025
|
+
// answers a gone one 404/410 (`sessionLost`), at the cost of that one
|
|
1026
|
+
// request, and a live one with where it stands.
|
|
1027
|
+
const link: SessionLink = { uploadUrl: source.uploadUrl, istk, context, signal };
|
|
1198
1028
|
const probe = await this.readResumePoint(link, budget, fileSize);
|
|
1199
|
-
transfer = { ...link,
|
|
1029
|
+
transfer = { ...link, fileSize, chunkSize: probe.chunkSize, budget };
|
|
1200
1030
|
position = { offset: checkedServerOffset(probe.offset, transfer, 'create', context), published: false };
|
|
1201
1031
|
debug?.log(`TUS session for ${options.filename} is at offset ${position.offset}/${fileSize}`);
|
|
1202
1032
|
} else {
|
|
@@ -1207,7 +1037,6 @@ export class MobileTusUploader {
|
|
|
1207
1037
|
uploadUrl: created.uploadUrl,
|
|
1208
1038
|
istk,
|
|
1209
1039
|
context,
|
|
1210
|
-
expiry: new SessionExpiry(created.expiresAt, options.onSessionExpiry),
|
|
1211
1040
|
signal,
|
|
1212
1041
|
fileSize,
|
|
1213
1042
|
chunkSize: created.chunkSize,
|
|
@@ -1220,7 +1049,6 @@ export class MobileTusUploader {
|
|
|
1220
1049
|
uploadUrl: created.uploadUrl,
|
|
1221
1050
|
istk,
|
|
1222
1051
|
chunkSize: created.chunkSize,
|
|
1223
|
-
expiresAt: created.expiresAt,
|
|
1224
1052
|
});
|
|
1225
1053
|
}
|
|
1226
1054
|
|
|
@@ -1228,14 +1056,12 @@ export class MobileTusUploader {
|
|
|
1228
1056
|
// remaining range in a single hand-off: it chains the chunks itself and
|
|
1229
1057
|
// keeps going while the app is suspended or killed, which no JS loop
|
|
1230
1058
|
// 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.
|
|
1059
|
+
// rides it too; XHR in the loop below is the fallback. Start-or-attach
|
|
1060
|
+
// on the session URL means a relaunch that resumes the same session
|
|
1061
|
+
// re-joins the job the OS is still running instead of PATCHing over it.
|
|
1235
1062
|
const backgroundTransfer = options.transport === 'background' ? this.config.backgroundTransfer : undefined;
|
|
1236
1063
|
if (backgroundTransfer && position.offset < fileSize) {
|
|
1237
1064
|
ensureNotAborted(signal);
|
|
1238
|
-
transfer.expiry.forget();
|
|
1239
1065
|
const policy = options.transferPolicy ?? {};
|
|
1240
1066
|
const finalOffset = await transferRemainderInBackground(
|
|
1241
1067
|
backgroundTransfer,
|
|
@@ -1267,15 +1093,13 @@ export class MobileTusUploader {
|
|
|
1267
1093
|
// Forge answers only once the ingot is published. An abort that lands
|
|
1268
1094
|
// after the OS delivered the last byte changes nothing: the transfer
|
|
1269
1095
|
// is complete, and the loop below (with its abort check) has nothing
|
|
1270
|
-
// left to do.
|
|
1271
|
-
// upload is never reported as cancelled.
|
|
1096
|
+
// left to do. A finished upload is never reported as cancelled.
|
|
1272
1097
|
position = { offset: fileSize, published: true };
|
|
1273
1098
|
}
|
|
1274
1099
|
|
|
1275
1100
|
const chunks: ChunkSource = {
|
|
1276
1101
|
reader: fileReader,
|
|
1277
1102
|
uri: cleanUri,
|
|
1278
|
-
transport: options.transport,
|
|
1279
1103
|
onChunkProgress: (offset, chunkBytesUploaded) => report(offset + chunkBytesUploaded),
|
|
1280
1104
|
};
|
|
1281
1105
|
while (!position.published) {
|
|
@@ -1314,9 +1138,9 @@ export class MobileTusUploader {
|
|
|
1314
1138
|
// in a generic TusUploadError.
|
|
1315
1139
|
if (err instanceof SparkVaultMobileError) throw err;
|
|
1316
1140
|
// Only a fileReader read or a caller's hook (onSessionCreated,
|
|
1317
|
-
//
|
|
1318
|
-
//
|
|
1319
|
-
//
|
|
1141
|
+
// onProgress) can throw anything else here: every request classifies
|
|
1142
|
+
// its own failure (tusRequest, the XHR chunk sender, the OS engine's
|
|
1143
|
+
// mapping). Those are the caller's errors, wrapped with the file
|
|
1320
1144
|
// context and nothing inferred about them.
|
|
1321
1145
|
throw TusUploadError.fromError(error, {
|
|
1322
1146
|
filename: options.filename,
|
|
@@ -1343,7 +1167,7 @@ export class MobileTusUploader {
|
|
|
1343
1167
|
budget: TransferBudget,
|
|
1344
1168
|
context: TusErrorContext,
|
|
1345
1169
|
signal?: AbortSignal
|
|
1346
|
-
): Promise<{ uploadUrl: string; chunkSize: number
|
|
1170
|
+
): Promise<{ uploadUrl: string; chunkSize: number }> {
|
|
1347
1171
|
const jitterKey = transferJitterKey(parsed.tusEndpoint);
|
|
1348
1172
|
for (;;) {
|
|
1349
1173
|
ensureNotAborted(signal);
|
|
@@ -1435,7 +1259,6 @@ export class MobileTusUploader {
|
|
|
1435
1259
|
'TUS HEAD request',
|
|
1436
1260
|
link.context
|
|
1437
1261
|
);
|
|
1438
|
-
link.expiry?.read(answer);
|
|
1439
1262
|
const outcome = readOutcome(answer, request);
|
|
1440
1263
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1441
1264
|
return { probe: readSessionProbe(answer, request.phase, link.context) };
|
|
@@ -1451,7 +1274,6 @@ export class MobileTusUploader {
|
|
|
1451
1274
|
*/
|
|
1452
1275
|
private async sendChunkAt(transfer: TusTransfer, offset: number, chunks: ChunkSource): Promise<TransferPosition> {
|
|
1453
1276
|
const answer = await this.sendChunk(transfer, offset, chunks);
|
|
1454
|
-
transfer.expiry.read(answer);
|
|
1455
1277
|
const outcome = readOutcome(answer, CHUNK_REQUEST);
|
|
1456
1278
|
if (outcome.verdict !== TRANSFER_VERDICT.OK) {
|
|
1457
1279
|
return this.positionAfterFailure(transfer, failureOf(answer, outcome, CHUNK_REQUEST, transfer.context), offset);
|
|
@@ -1475,21 +1297,8 @@ export class MobileTusUploader {
|
|
|
1475
1297
|
return { offset: confirmed, published: confirmed === transfer.fileSize };
|
|
1476
1298
|
}
|
|
1477
1299
|
|
|
1478
|
-
/**
|
|
1300
|
+
/** Read the chunk at `offset` and PATCH it over XHR, resolving with Forge's answer. */
|
|
1479
1301
|
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
1302
|
const chunkLength = Math.min(transfer.chunkSize, transfer.fileSize - offset);
|
|
1494
1303
|
const chunk = await this.readChunk(chunks.reader, chunks.uri, offset, chunkLength);
|
|
1495
1304
|
return sendChunkOverXhr(
|
|
@@ -1593,7 +1402,6 @@ export class MobileTusUploader {
|
|
|
1593
1402
|
'TUS finalization request',
|
|
1594
1403
|
transfer.context
|
|
1595
1404
|
);
|
|
1596
|
-
transfer.expiry.read(answer);
|
|
1597
1405
|
const outcome = readOutcome(answer, RECEIPT_REQUEST);
|
|
1598
1406
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1599
1407
|
return { offset: transfer.fileSize, published: true };
|
|
@@ -1670,25 +1478,12 @@ export class MobileTusUploader {
|
|
|
1670
1478
|
*/
|
|
1671
1479
|
async probeSession(session: { uploadUrl: string; istk: string }): Promise<TusSessionProbe | null> {
|
|
1672
1480
|
const uploadUrl = readKeptSessionUrl(session.uploadUrl, this.config.allowedDownloadHostPatterns);
|
|
1673
|
-
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {}
|
|
1481
|
+
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {} }, RESUME_HEAD_REQUEST);
|
|
1674
1482
|
if ('probe' in head) return head.probe;
|
|
1675
1483
|
if (head.failure.outcome.verdict === TRANSFER_VERDICT.GONE) return null;
|
|
1676
1484
|
throw head.failure.error;
|
|
1677
1485
|
}
|
|
1678
1486
|
|
|
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
1487
|
/**
|
|
1693
1488
|
* Release a session the caller kept on Forge (`abortBehavior: 'keep'`)
|
|
1694
1489
|
* and will not resume after all: the tus DELETE lets Forge drop the chunk
|
|
@@ -1737,7 +1532,7 @@ export class MobileTusUploader {
|
|
|
1737
1532
|
// The OS job, when there is one, is cancelled before the DELETE so the
|
|
1738
1533
|
// engine stops PATCHing a session Forge is about to forget (see
|
|
1739
1534
|
// terminateSession); without an engine the DELETE leaves the device
|
|
1740
|
-
// synchronously
|
|
1535
|
+
// synchronously.
|
|
1741
1536
|
const backgroundTransfer = this.config.backgroundTransfer;
|
|
1742
1537
|
const terminate = backgroundTransfer
|
|
1743
1538
|
? this.cancelBackgroundTransfer(backgroundTransfer, uploadUrl, debug).then(() => this.requestTerminate(uploadUrl, istk))
|
|
@@ -1795,7 +1590,7 @@ export class MobileTusUploader {
|
|
|
1795
1590
|
position: number,
|
|
1796
1591
|
length: number
|
|
1797
1592
|
): Promise<TusChunk> {
|
|
1798
|
-
// Keep the chunk in the base64 form the
|
|
1593
|
+
// Keep the chunk in the base64 form the fileReader returned (see
|
|
1799
1594
|
// TusChunk) — decoding it here would burn seconds of JS-thread time per
|
|
1800
1595
|
// upload for bytes that are re-encoded before they ever leave the device.
|
|
1801
1596
|
const base64 = await fileReader.readAsBase64(fileUri, { position, length });
|