@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/dist/tus.d.ts
CHANGED
|
@@ -12,8 +12,7 @@ export interface ParsedForgeUrl {
|
|
|
12
12
|
*
|
|
13
13
|
* A resume that cannot use the session throws a `TusUploadError` whose
|
|
14
14
|
* `sessionLost` is true exactly when the session is worthless for this file
|
|
15
|
-
* and the caller should drop it and start fresh:
|
|
16
|
-
* for it has passed (refused before any request), Forge no longer has it
|
|
15
|
+
* and the caller should drop it and start fresh: Forge no longer has it
|
|
17
16
|
* (404/410), or its `Upload-Length` is not this file's size (it belongs to a
|
|
18
17
|
* different file). A HEAD that keeps failing transiently (5xx, timeouts) past
|
|
19
18
|
* the upload's retry budget is NOT `sessionLost`; the session is intact and
|
|
@@ -23,20 +22,12 @@ export interface TusResumeSession {
|
|
|
23
22
|
uploadUrl: string;
|
|
24
23
|
/** The session's ISTK. Falls back to the one in `forgeUrl` when omitted. */
|
|
25
24
|
istk?: string;
|
|
26
|
-
/**
|
|
27
|
-
* The session's expiry as the caller holds it (`expiresAt` from
|
|
28
|
-
* `onSessionCreated`, kept current through `onSessionExpiry`). Null or
|
|
29
|
-
* omitted is unknown, and Forge's HEAD decides.
|
|
30
|
-
*/
|
|
31
|
-
expiresAt?: number | null;
|
|
32
25
|
}
|
|
33
26
|
/** Where a tus session stands server-side, as reported by HEAD. */
|
|
34
27
|
export interface TusSessionProbe {
|
|
35
28
|
offset: number;
|
|
36
29
|
length: number;
|
|
37
30
|
chunkSize: number;
|
|
38
|
-
/** When the session lapses (epoch ms, Forge's `Upload-Expires`), or null when the HEAD named none. */
|
|
39
|
-
expiresAt: number | null;
|
|
40
31
|
}
|
|
41
32
|
export interface TusUploadFromUriOptions {
|
|
42
33
|
fileUri: string;
|
|
@@ -58,36 +49,22 @@ export interface TusUploadFromUriOptions {
|
|
|
58
49
|
*/
|
|
59
50
|
onSessionCreated?: (session: TusSessionInfo) => void;
|
|
60
51
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
* while it runs (the transfer engine's range, the single-chunk
|
|
66
|
-
* `backgroundUploader` PATCH): either may have moved it unread. A caller
|
|
67
|
-
* that persists the session keeps its `expiresAt` current with this, so a
|
|
68
|
-
* session it judges expired (`isSessionExpired`) really is. A throw here
|
|
69
|
-
* fails the upload.
|
|
70
|
-
*/
|
|
71
|
-
onSessionExpiry?: (expiresAt: number | null) => void;
|
|
72
|
-
/**
|
|
73
|
-
* `background` asks for a transfer that survives suspension and rides the
|
|
74
|
-
* best engine the app provides: the OS transfer engine
|
|
75
|
-
* (`backgroundTransfer`) for the whole remaining range of any file, else
|
|
76
|
-
* the single-chunk `backgroundUploader` (no progress, no abort; whether it
|
|
77
|
-
* outlives a suspension is the uploader's) for a file that fits one chunk,
|
|
78
|
-
* else XHR. Default `foreground`: every chunk goes over XHR.
|
|
52
|
+
* `background` asks for a transfer that survives suspension: the OS
|
|
53
|
+
* transfer engine (`backgroundTransfer`) takes the whole remaining range of
|
|
54
|
+
* any file, and without one every chunk goes over XHR. Default
|
|
55
|
+
* `foreground`: every chunk goes over XHR.
|
|
79
56
|
*/
|
|
80
57
|
transport?: UploadTransport;
|
|
81
58
|
/**
|
|
82
59
|
* Network policy carried into the OS transfer engine on every request it
|
|
83
|
-
* sends (see `UploadTransferPolicy`). The XHR
|
|
84
|
-
*
|
|
60
|
+
* sends (see `UploadTransferPolicy`). The XHR path ignores it: JS gates
|
|
61
|
+
* connectivity for it. Defaults: both allowed.
|
|
85
62
|
*/
|
|
86
63
|
transferPolicy?: UploadTransferPolicy;
|
|
87
64
|
/**
|
|
88
65
|
* The device media library's id for the file at `fileUri`, carried into
|
|
89
66
|
* the OS transfer engine's job (`BackgroundTransferJob.sourceAssetId`).
|
|
90
|
-
* The
|
|
67
|
+
* The XHR path reads the file in this process and ignores it.
|
|
91
68
|
*/
|
|
92
69
|
sourceAssetId?: string;
|
|
93
70
|
onProgress?: UploadProgressCallback;
|
|
@@ -135,28 +112,6 @@ interface TusErrorContext {
|
|
|
135
112
|
* what "gone" is.
|
|
136
113
|
*/
|
|
137
114
|
export declare function isSessionGone(status: number): boolean;
|
|
138
|
-
/**
|
|
139
|
-
* How far past the expiry Forge named a session still counts as live: the
|
|
140
|
-
* allowance for a device clock running ahead of Forge's. `Upload-Expires` is
|
|
141
|
-
* read off Forge's clock and compared with the device's; a phone on automatic
|
|
142
|
-
* time is within seconds of Forge, one set by hand is typically minutes fast.
|
|
143
|
-
* The two mistakes cost very differently: a live session judged expired is
|
|
144
|
-
* dropped and its file sent again from byte 0, an expired one judged live
|
|
145
|
-
* costs a single HEAD (Forge answers 410, `sessionLost`). So the margin errs
|
|
146
|
-
* long, and stays small against the 72 h idle window a session lives from its
|
|
147
|
-
* last commit (`@sparkvault/app-sdk/upload-expiry`): a session the clock says
|
|
148
|
-
* lapsed hours ago is still dropped without a request.
|
|
149
|
-
*/
|
|
150
|
-
export declare const SESSION_EXPIRY_CLOCK_MARGIN_MS: number;
|
|
151
|
-
/**
|
|
152
|
-
* True when the expiry Forge named for a session (`TusSessionInfo.expiresAt`,
|
|
153
|
-
* kept current through `onSessionExpiry`) is past by more than the clock
|
|
154
|
-
* margin: Forge refuses every request to a session past its expiry, so a
|
|
155
|
-
* caller drops it without asking. Within the margin the session is live for
|
|
156
|
-
* all the device clock can tell, and Forge's HEAD decides. An unknown expiry
|
|
157
|
-
* (null) never has passed: only Forge's HEAD can say.
|
|
158
|
-
*/
|
|
159
|
-
export declare function isSessionExpired(expiresAt: number | null | undefined, now?: number): boolean;
|
|
160
115
|
/**
|
|
161
116
|
* Translate what the OS transfer adapter reports into the SDK's one error
|
|
162
117
|
* taxonomy, so a caller sees the same `TusUploadError` phases and typed gate
|
|
@@ -247,7 +202,7 @@ export declare class MobileTusUploader {
|
|
|
247
202
|
* or wherever the server says after a failure.
|
|
248
203
|
*/
|
|
249
204
|
private sendChunkAt;
|
|
250
|
-
/**
|
|
205
|
+
/** Read the chunk at `offset` and PATCH it over XHR, resolving with Forge's answer. */
|
|
251
206
|
private sendChunk;
|
|
252
207
|
/**
|
|
253
208
|
* Where the upload stands after a failed chunk PATCH, in the server's
|
|
@@ -319,16 +274,6 @@ export declare class MobileTusUploader {
|
|
|
319
274
|
uploadUrl: string;
|
|
320
275
|
istk: string;
|
|
321
276
|
}): Promise<TusSessionProbe | null>;
|
|
322
|
-
/**
|
|
323
|
-
* Whether the remaining transfer is ONE PATCH the single-request uploader
|
|
324
|
-
* (`backgroundUploader`) can send: nothing uploaded yet and the whole file
|
|
325
|
-
* inside a single chunk. The uploader sends whole files and Forge rejects
|
|
326
|
-
* any non-final chunk that is not exactly `X-Chunk-Size`, so a multi-chunk
|
|
327
|
-
* file has no legal single request, and a partially uploaded session would
|
|
328
|
-
* resend bytes Forge holds. A zero-byte file stays on the XHR path (it
|
|
329
|
-
* sends no PATCH at all there).
|
|
330
|
-
*/
|
|
331
|
-
private fitsOneBackgroundRequest;
|
|
332
277
|
/**
|
|
333
278
|
* Release a session the caller kept on Forge (`abortBehavior: 'keep'`)
|
|
334
279
|
* and will not resume after all: the tus DELETE lets Forge drop the chunk
|
package/dist/tus.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { FORGE_URL_REFUSAL, readForgeSessionUrl, readForgeTransferUrl, resolveForgeSessionUrl } from './app-sdk/forge-url.js';
|
|
2
2
|
import { SPARKVAULT_OWNED_HOST_PATTERNS } from './app-sdk/platform-domains.js';
|
|
3
|
-
import { HEAD_AND_RECEIPT_TIMEOUT_MS, RECEIVED_STILL_SAVING, TRANSFER_VERDICT,
|
|
3
|
+
import { HEAD_AND_RECEIPT_TIMEOUT_MS, RECEIVED_STILL_SAVING, TRANSFER_VERDICT, classifyTransferResponse, createTransferBudget, resumeOffsetProblem, retryDelayMs, transferJitterKey, } from './app-sdk/transfer-retry.js';
|
|
4
4
|
import { UPLOAD_REFUSAL_CODES, UPLOAD_REFUSAL_HEADER, VAULT_DELETION_SCHEDULED, VAULT_DELETION_SCHEDULED_MESSAGE, } from './app-sdk/upload-publication.js';
|
|
5
5
|
import { BackgroundTransferError, gateErrorFromBody, SparkVaultMobileError, SparkVaultValidationError, TusUploadError, } from './errors.js';
|
|
6
6
|
const TUS_VERSION = '1.0.0';
|
|
@@ -140,30 +140,6 @@ function sessionLostError(status, context, phase, cause = null, coreCode = null)
|
|
|
140
140
|
coreCode,
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
|
-
/**
|
|
144
|
-
* How far past the expiry Forge named a session still counts as live: the
|
|
145
|
-
* allowance for a device clock running ahead of Forge's. `Upload-Expires` is
|
|
146
|
-
* read off Forge's clock and compared with the device's; a phone on automatic
|
|
147
|
-
* time is within seconds of Forge, one set by hand is typically minutes fast.
|
|
148
|
-
* The two mistakes cost very differently: a live session judged expired is
|
|
149
|
-
* dropped and its file sent again from byte 0, an expired one judged live
|
|
150
|
-
* costs a single HEAD (Forge answers 410, `sessionLost`). So the margin errs
|
|
151
|
-
* long, and stays small against the 72 h idle window a session lives from its
|
|
152
|
-
* last commit (`@sparkvault/app-sdk/upload-expiry`): a session the clock says
|
|
153
|
-
* lapsed hours ago is still dropped without a request.
|
|
154
|
-
*/
|
|
155
|
-
export const SESSION_EXPIRY_CLOCK_MARGIN_MS = 5 * 60 * 1000;
|
|
156
|
-
/**
|
|
157
|
-
* True when the expiry Forge named for a session (`TusSessionInfo.expiresAt`,
|
|
158
|
-
* kept current through `onSessionExpiry`) is past by more than the clock
|
|
159
|
-
* margin: Forge refuses every request to a session past its expiry, so a
|
|
160
|
-
* caller drops it without asking. Within the margin the session is live for
|
|
161
|
-
* all the device clock can tell, and Forge's HEAD decides. An unknown expiry
|
|
162
|
-
* (null) never has passed: only Forge's HEAD can say.
|
|
163
|
-
*/
|
|
164
|
-
export function isSessionExpired(expiresAt, now = Date.now()) {
|
|
165
|
-
return typeof expiresAt === 'number' && now > expiresAt + SESSION_EXPIRY_CLOCK_MARGIN_MS;
|
|
166
|
-
}
|
|
167
143
|
/** An answer that arrived: its status, its headers and the body of one that is not a 2xx. */
|
|
168
144
|
function answerOf(status, header, body) {
|
|
169
145
|
return { status, header, body, envelope: parseErrorBody(body), unanswered: null };
|
|
@@ -269,41 +245,6 @@ function errorOf(answer, outcome, request, context, cause = null) {
|
|
|
269
245
|
phase: request.phase,
|
|
270
246
|
});
|
|
271
247
|
}
|
|
272
|
-
/**
|
|
273
|
-
* The session's expiry as its latest answer named it, reported to the
|
|
274
|
-
* caller (`onSessionExpiry`) each time it changes. An answer that names none
|
|
275
|
-
* leaves it unknown: the request may have moved it unread (a commit whose
|
|
276
|
-
* answer was lost, a deferred finalization), and a caller that dropped a
|
|
277
|
-
* session on an expiry Forge has since moved on would re-send a whole file.
|
|
278
|
-
* A request cut short by the caller's abort keeps the last named value: it
|
|
279
|
-
* can only have slid the expiry by that one request's duration.
|
|
280
|
-
*/
|
|
281
|
-
class SessionExpiry {
|
|
282
|
-
constructor(known, report) {
|
|
283
|
-
this.known = known;
|
|
284
|
-
this.report = report;
|
|
285
|
-
}
|
|
286
|
-
/** Forge answered (or did not): the expiry is what the answer names. */
|
|
287
|
-
read(answer) {
|
|
288
|
-
this.become(parseUploadExpires(answer.header(UPLOAD_EXPIRES_HEADER)));
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* A request JS cannot follow while it runs is about to leave: the OS
|
|
292
|
-
* transfer engine's range, whose chunks commit (each sliding the expiry)
|
|
293
|
-
* with answers this upload never reads, possibly while the app is not
|
|
294
|
-
* running; or the single-chunk `backgroundUploader` PATCH, which reports
|
|
295
|
-
* nothing until it answers.
|
|
296
|
-
*/
|
|
297
|
-
forget() {
|
|
298
|
-
this.become(null);
|
|
299
|
-
}
|
|
300
|
-
become(expiresAt) {
|
|
301
|
-
if (expiresAt === this.known)
|
|
302
|
-
return;
|
|
303
|
-
this.known = expiresAt;
|
|
304
|
-
this.report?.(expiresAt);
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
248
|
/** A backoff the caller's abort cuts short: resolves after `ms`, rejects as cancelled the moment the signal fires. */
|
|
308
249
|
function abortableDelay(ms, signal) {
|
|
309
250
|
return new Promise((resolve, reject) => {
|
|
@@ -409,21 +350,6 @@ async function fetchAnswer(config, url, init, label, context) {
|
|
|
409
350
|
}
|
|
410
351
|
return answerOf(response.status, name => response.headers.get(name), response.ok ? '' : await response.text().catch(() => ''));
|
|
411
352
|
}
|
|
412
|
-
/**
|
|
413
|
-
* Case-insensitive header lookup for adapter results. Native HTTP stacks
|
|
414
|
-
* hand headers back in whatever case they please (iOS lower-cases them),
|
|
415
|
-
* while Forge writes `Upload-Offset`.
|
|
416
|
-
*/
|
|
417
|
-
function getHeaderIgnoreCase(headers, name) {
|
|
418
|
-
if (!headers)
|
|
419
|
-
return null;
|
|
420
|
-
const wanted = name.toLowerCase();
|
|
421
|
-
for (const key of Object.keys(headers)) {
|
|
422
|
-
if (key.toLowerCase() === wanted)
|
|
423
|
-
return headers[key];
|
|
424
|
-
}
|
|
425
|
-
return null;
|
|
426
|
-
}
|
|
427
353
|
function resolveSessionSource(options, allowedHostPatterns) {
|
|
428
354
|
if (options.resume) {
|
|
429
355
|
const uploadUrl = readKeptSessionUrl(options.resume.uploadUrl, allowedHostPatterns);
|
|
@@ -432,11 +358,7 @@ function resolveSessionSource(options, allowedHostPatterns) {
|
|
|
432
358
|
if (!istk) {
|
|
433
359
|
throw new SparkVaultValidationError('resume.istk or forgeUrl is required to resume a TUS upload session');
|
|
434
360
|
}
|
|
435
|
-
|
|
436
|
-
if (expiresAt !== null && !Number.isFinite(expiresAt)) {
|
|
437
|
-
throw new SparkVaultValidationError('resume.expiresAt is an epoch-ms expiry or null', { received: expiresAt });
|
|
438
|
-
}
|
|
439
|
-
return { mode: 'resume', uploadUrl, istk, expiresAt };
|
|
361
|
+
return { mode: 'resume', uploadUrl, istk };
|
|
440
362
|
}
|
|
441
363
|
if (!options.forgeUrl) {
|
|
442
364
|
throw new SparkVaultValidationError('forgeUrl is required to create a TUS upload session');
|
|
@@ -444,9 +366,9 @@ function resolveSessionSource(options, allowedHostPatterns) {
|
|
|
444
366
|
return { mode: 'create', parsed: parseForgeUrl(options.forgeUrl, allowedHostPatterns) };
|
|
445
367
|
}
|
|
446
368
|
/**
|
|
447
|
-
* The session a create's 2xx opened: where it lives
|
|
448
|
-
*
|
|
449
|
-
*
|
|
369
|
+
* The session a create's 2xx opened: where it lives and its chunk geometry.
|
|
370
|
+
* Every chunk carries the file's bytes, so the session must live under the
|
|
371
|
+
* endpoint it was created at.
|
|
450
372
|
*/
|
|
451
373
|
function readCreatedSession(answer, endpoint, context) {
|
|
452
374
|
const uploadUrl = resolveForgeSessionUrl(answer.header('Location'), endpoint);
|
|
@@ -468,13 +390,13 @@ function readCreatedSession(answer, endpoint, context) {
|
|
|
468
390
|
phase: 'create',
|
|
469
391
|
});
|
|
470
392
|
}
|
|
471
|
-
return { uploadUrl, chunkSize
|
|
393
|
+
return { uploadUrl, chunkSize };
|
|
472
394
|
}
|
|
473
395
|
/**
|
|
474
396
|
* Where a HEAD's 2xx says the session stands: offset, length and chunk
|
|
475
397
|
* geometry, all required (a 2xx without them is not Forge, a captive
|
|
476
|
-
* portal's page being the classic case)
|
|
477
|
-
*
|
|
398
|
+
* portal's page being the classic case). An offset past the length can only
|
|
399
|
+
* be a corrupt session.
|
|
478
400
|
*/
|
|
479
401
|
function readSessionProbe(answer, phase, context) {
|
|
480
402
|
const offset = parseIntegerHeader(answer.header('Upload-Offset'), 0);
|
|
@@ -483,7 +405,7 @@ function readSessionProbe(answer, phase, context) {
|
|
|
483
405
|
if (offset === null || length === null || chunkSize === null || offset > length) {
|
|
484
406
|
throw new TusUploadError('TUS HEAD response missing or invalid Upload-Offset / Upload-Length / X-Chunk-Size headers', { ...context, phase });
|
|
485
407
|
}
|
|
486
|
-
return { offset, length, chunkSize
|
|
408
|
+
return { offset, length, chunkSize };
|
|
487
409
|
}
|
|
488
410
|
/**
|
|
489
411
|
* A session offset the server named (a HEAD's, or a 409's `Upload-Offset`),
|
|
@@ -597,45 +519,6 @@ function sendChunkOverXhr(transfer, chunk, offset, stallTimeoutMs, onChunkProgre
|
|
|
597
519
|
xhr.send({ base64: chunk.base64 });
|
|
598
520
|
});
|
|
599
521
|
}
|
|
600
|
-
/**
|
|
601
|
-
* PATCH a whole file as the session's only chunk through the app's
|
|
602
|
-
* single-request uploader (`backgroundUploader`) and resolve with Forge's
|
|
603
|
-
* answer, or with no answer when the adapter got none. The request reports
|
|
604
|
-
* no progress and takes no abort; whether it outlives a suspension is the
|
|
605
|
-
* uploader's (MobileBackgroundUploader). Its answer is read exactly like the
|
|
606
|
-
* XHR path's, so callers see one error taxonomy.
|
|
607
|
-
*/
|
|
608
|
-
async function sendWholeFileInBackground(uploader, transfer, fileUri, offset) {
|
|
609
|
-
let result;
|
|
610
|
-
try {
|
|
611
|
-
result = await uploader.uploadFile(transfer.uploadUrl, fileUri, {
|
|
612
|
-
method: 'PATCH',
|
|
613
|
-
headers: {
|
|
614
|
-
'Tus-Resumable': TUS_VERSION,
|
|
615
|
-
'Upload-Offset': String(offset),
|
|
616
|
-
'Content-Type': 'application/offset+octet-stream',
|
|
617
|
-
'X-ISTK': transfer.istk,
|
|
618
|
-
},
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
catch (err) {
|
|
622
|
-
// The adapter only throws when no HTTP response came back (offline, DNS,
|
|
623
|
-
// the OS cancelled the task); an HTTP failure is a status below.
|
|
624
|
-
return noAnswer(new TusUploadError(`Background chunk upload failed: ${getErrorMessage(err)}`, {
|
|
625
|
-
cause: err instanceof Error ? err : null,
|
|
626
|
-
...transfer.context,
|
|
627
|
-
phase: 'network',
|
|
628
|
-
}));
|
|
629
|
-
}
|
|
630
|
-
if (!Number.isSafeInteger(result.status) || result.status <= 0) {
|
|
631
|
-
return noAnswer(new TusUploadError(`Background chunk upload got no answer (status ${String(result.status)})`, {
|
|
632
|
-
...transfer.context,
|
|
633
|
-
phase: 'network',
|
|
634
|
-
}));
|
|
635
|
-
}
|
|
636
|
-
const answered = result.status >= 200 && result.status < 300;
|
|
637
|
-
return answerOf(result.status, name => getHeaderIgnoreCase(result.headers, name), !answered && typeof result.body === 'string' ? result.body : '');
|
|
638
|
-
}
|
|
639
522
|
/**
|
|
640
523
|
* Translate what the OS transfer adapter reports into the SDK's one error
|
|
641
524
|
* taxonomy, so a caller sees the same `TusUploadError` phases and typed gate
|
|
@@ -783,20 +666,12 @@ export class MobileTusUploader {
|
|
|
783
666
|
let position;
|
|
784
667
|
if (source.mode === 'resume') {
|
|
785
668
|
debug?.log(`Resuming TUS upload for ${options.filename}`);
|
|
786
|
-
//
|
|
787
|
-
//
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
throw new TusUploadError(`TUS session expired at ${new Date(heldExpiry).toISOString()}`, {
|
|
791
|
-
...context,
|
|
792
|
-
phase: 'create',
|
|
793
|
-
sessionLost: true,
|
|
794
|
-
});
|
|
795
|
-
}
|
|
796
|
-
const expiry = new SessionExpiry(source.expiresAt, options.onSessionExpiry);
|
|
797
|
-
const link = { uploadUrl: source.uploadUrl, istk, context, expiry, signal };
|
|
669
|
+
// Only Forge knows whether it still holds the session: its HEAD
|
|
670
|
+
// answers a gone one 404/410 (`sessionLost`), at the cost of that one
|
|
671
|
+
// request, and a live one with where it stands.
|
|
672
|
+
const link = { uploadUrl: source.uploadUrl, istk, context, signal };
|
|
798
673
|
const probe = await this.readResumePoint(link, budget, fileSize);
|
|
799
|
-
transfer = { ...link,
|
|
674
|
+
transfer = { ...link, fileSize, chunkSize: probe.chunkSize, budget };
|
|
800
675
|
position = { offset: checkedServerOffset(probe.offset, transfer, 'create', context), published: false };
|
|
801
676
|
debug?.log(`TUS session for ${options.filename} is at offset ${position.offset}/${fileSize}`);
|
|
802
677
|
}
|
|
@@ -808,7 +683,6 @@ export class MobileTusUploader {
|
|
|
808
683
|
uploadUrl: created.uploadUrl,
|
|
809
684
|
istk,
|
|
810
685
|
context,
|
|
811
|
-
expiry: new SessionExpiry(created.expiresAt, options.onSessionExpiry),
|
|
812
686
|
signal,
|
|
813
687
|
fileSize,
|
|
814
688
|
chunkSize: created.chunkSize,
|
|
@@ -821,21 +695,18 @@ export class MobileTusUploader {
|
|
|
821
695
|
uploadUrl: created.uploadUrl,
|
|
822
696
|
istk,
|
|
823
697
|
chunkSize: created.chunkSize,
|
|
824
|
-
expiresAt: created.expiresAt,
|
|
825
698
|
});
|
|
826
699
|
}
|
|
827
700
|
// The OS transfer engine, when the app provides one, takes the whole
|
|
828
701
|
// remaining range in a single hand-off: it chains the chunks itself and
|
|
829
702
|
// keeps going while the app is suspended or killed, which no JS loop
|
|
830
703
|
// can. Any size and any offset qualify, so a resumed multi-GB video
|
|
831
|
-
// rides it too;
|
|
832
|
-
//
|
|
833
|
-
//
|
|
834
|
-
// running instead of PATCHing over it.
|
|
704
|
+
// rides it too; XHR in the loop below is the fallback. Start-or-attach
|
|
705
|
+
// on the session URL means a relaunch that resumes the same session
|
|
706
|
+
// re-joins the job the OS is still running instead of PATCHing over it.
|
|
835
707
|
const backgroundTransfer = options.transport === 'background' ? this.config.backgroundTransfer : undefined;
|
|
836
708
|
if (backgroundTransfer && position.offset < fileSize) {
|
|
837
709
|
ensureNotAborted(signal);
|
|
838
|
-
transfer.expiry.forget();
|
|
839
710
|
const policy = options.transferPolicy ?? {};
|
|
840
711
|
const finalOffset = await transferRemainderInBackground(backgroundTransfer, {
|
|
841
712
|
uploadUrl: transfer.uploadUrl,
|
|
@@ -860,14 +731,12 @@ export class MobileTusUploader {
|
|
|
860
731
|
// Forge answers only once the ingot is published. An abort that lands
|
|
861
732
|
// after the OS delivered the last byte changes nothing: the transfer
|
|
862
733
|
// is complete, and the loop below (with its abort check) has nothing
|
|
863
|
-
// left to do.
|
|
864
|
-
// upload is never reported as cancelled.
|
|
734
|
+
// left to do. A finished upload is never reported as cancelled.
|
|
865
735
|
position = { offset: fileSize, published: true };
|
|
866
736
|
}
|
|
867
737
|
const chunks = {
|
|
868
738
|
reader: fileReader,
|
|
869
739
|
uri: cleanUri,
|
|
870
|
-
transport: options.transport,
|
|
871
740
|
onChunkProgress: (offset, chunkBytesUploaded) => report(offset + chunkBytesUploaded),
|
|
872
741
|
};
|
|
873
742
|
while (!position.published) {
|
|
@@ -909,9 +778,9 @@ export class MobileTusUploader {
|
|
|
909
778
|
if (err instanceof SparkVaultMobileError)
|
|
910
779
|
throw err;
|
|
911
780
|
// Only a fileReader read or a caller's hook (onSessionCreated,
|
|
912
|
-
//
|
|
913
|
-
//
|
|
914
|
-
//
|
|
781
|
+
// onProgress) can throw anything else here: every request classifies
|
|
782
|
+
// its own failure (tusRequest, the XHR chunk sender, the OS engine's
|
|
783
|
+
// mapping). Those are the caller's errors, wrapped with the file
|
|
915
784
|
// context and nothing inferred about them.
|
|
916
785
|
throw TusUploadError.fromError(error, {
|
|
917
786
|
filename: options.filename,
|
|
@@ -1004,7 +873,6 @@ export class MobileTusUploader {
|
|
|
1004
873
|
signal: link.signal,
|
|
1005
874
|
timeoutMs: HEAD_AND_RECEIPT_TIMEOUT_MS,
|
|
1006
875
|
}, 'TUS HEAD request', link.context);
|
|
1007
|
-
link.expiry?.read(answer);
|
|
1008
876
|
const outcome = readOutcome(answer, request);
|
|
1009
877
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1010
878
|
return { probe: readSessionProbe(answer, request.phase, link.context) };
|
|
@@ -1019,7 +887,6 @@ export class MobileTusUploader {
|
|
|
1019
887
|
*/
|
|
1020
888
|
async sendChunkAt(transfer, offset, chunks) {
|
|
1021
889
|
const answer = await this.sendChunk(transfer, offset, chunks);
|
|
1022
|
-
transfer.expiry.read(answer);
|
|
1023
890
|
const outcome = readOutcome(answer, CHUNK_REQUEST);
|
|
1024
891
|
if (outcome.verdict !== TRANSFER_VERDICT.OK) {
|
|
1025
892
|
return this.positionAfterFailure(transfer, failureOf(answer, outcome, CHUNK_REQUEST, transfer.context), offset);
|
|
@@ -1042,21 +909,8 @@ export class MobileTusUploader {
|
|
|
1042
909
|
transfer.budget.progressed();
|
|
1043
910
|
return { offset: confirmed, published: confirmed === transfer.fileSize };
|
|
1044
911
|
}
|
|
1045
|
-
/**
|
|
912
|
+
/** Read the chunk at `offset` and PATCH it over XHR, resolving with Forge's answer. */
|
|
1046
913
|
async sendChunk(transfer, offset, chunks) {
|
|
1047
|
-
const backgroundUploader = chunks.transport === 'background' ? this.config.backgroundUploader : undefined;
|
|
1048
|
-
if (backgroundUploader && this.fitsOneBackgroundRequest(offset, transfer.fileSize, transfer.chunkSize)) {
|
|
1049
|
-
// The uploader takes no abort, so an abort that fires while this
|
|
1050
|
-
// request is in flight cannot stop it. It only ever carries a whole
|
|
1051
|
-
// single-chunk file, so a 2xx here means the transfer is complete: the
|
|
1052
|
-
// loop ends and the upload finishes normally (the caller verifies the
|
|
1053
|
-
// ingot), because a finished upload must never be reported as
|
|
1054
|
-
// cancelled, and a DELETE now would drop bytes Forge has already
|
|
1055
|
-
// finalized. Only an answer that leaves bytes missing reaches the abort
|
|
1056
|
-
// check before the next request.
|
|
1057
|
-
transfer.expiry.forget();
|
|
1058
|
-
return sendWholeFileInBackground(backgroundUploader, transfer, chunks.uri, offset);
|
|
1059
|
-
}
|
|
1060
914
|
const chunkLength = Math.min(transfer.chunkSize, transfer.fileSize - offset);
|
|
1061
915
|
const chunk = await this.readChunk(chunks.reader, chunks.uri, offset, chunkLength);
|
|
1062
916
|
return sendChunkOverXhr(transfer, chunk, offset, this.config.tusChunkTimeoutMs, chunkBytesUploaded => chunks.onChunkProgress(offset, chunkBytesUploaded));
|
|
@@ -1140,7 +994,6 @@ export class MobileTusUploader {
|
|
|
1140
994
|
signal: transfer.signal,
|
|
1141
995
|
timeoutMs: HEAD_AND_RECEIPT_TIMEOUT_MS,
|
|
1142
996
|
}, 'TUS finalization request', transfer.context);
|
|
1143
|
-
transfer.expiry.read(answer);
|
|
1144
997
|
const outcome = readOutcome(answer, RECEIPT_REQUEST);
|
|
1145
998
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1146
999
|
return { offset: transfer.fileSize, published: true };
|
|
@@ -1211,25 +1064,13 @@ export class MobileTusUploader {
|
|
|
1211
1064
|
*/
|
|
1212
1065
|
async probeSession(session) {
|
|
1213
1066
|
const uploadUrl = readKeptSessionUrl(session.uploadUrl, this.config.allowedDownloadHostPatterns);
|
|
1214
|
-
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {}
|
|
1067
|
+
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {} }, RESUME_HEAD_REQUEST);
|
|
1215
1068
|
if ('probe' in head)
|
|
1216
1069
|
return head.probe;
|
|
1217
1070
|
if (head.failure.outcome.verdict === TRANSFER_VERDICT.GONE)
|
|
1218
1071
|
return null;
|
|
1219
1072
|
throw head.failure.error;
|
|
1220
1073
|
}
|
|
1221
|
-
/**
|
|
1222
|
-
* Whether the remaining transfer is ONE PATCH the single-request uploader
|
|
1223
|
-
* (`backgroundUploader`) can send: nothing uploaded yet and the whole file
|
|
1224
|
-
* inside a single chunk. The uploader sends whole files and Forge rejects
|
|
1225
|
-
* any non-final chunk that is not exactly `X-Chunk-Size`, so a multi-chunk
|
|
1226
|
-
* file has no legal single request, and a partially uploaded session would
|
|
1227
|
-
* resend bytes Forge holds. A zero-byte file stays on the XHR path (it
|
|
1228
|
-
* sends no PATCH at all there).
|
|
1229
|
-
*/
|
|
1230
|
-
fitsOneBackgroundRequest(offset, fileSize, chunkSize) {
|
|
1231
|
-
return offset === 0 && fileSize > 0 && fileSize <= chunkSize;
|
|
1232
|
-
}
|
|
1233
1074
|
/**
|
|
1234
1075
|
* Release a session the caller kept on Forge (`abortBehavior: 'keep'`)
|
|
1235
1076
|
* and will not resume after all: the tus DELETE lets Forge drop the chunk
|
|
@@ -1274,7 +1115,7 @@ export class MobileTusUploader {
|
|
|
1274
1115
|
// The OS job, when there is one, is cancelled before the DELETE so the
|
|
1275
1116
|
// engine stops PATCHing a session Forge is about to forget (see
|
|
1276
1117
|
// terminateSession); without an engine the DELETE leaves the device
|
|
1277
|
-
// synchronously
|
|
1118
|
+
// synchronously.
|
|
1278
1119
|
const backgroundTransfer = this.config.backgroundTransfer;
|
|
1279
1120
|
const terminate = backgroundTransfer
|
|
1280
1121
|
? this.cancelBackgroundTransfer(backgroundTransfer, uploadUrl, debug).then(() => this.requestTerminate(uploadUrl, istk))
|