@sparkvault/sdk-mobile 9.0.10 → 10.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/client.d.ts +2 -1
- package/dist/client.js +2 -1
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +2 -12
- package/dist/config.js +0 -1
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +15 -13
- package/dist/errors.js +21 -12
- package/dist/errors.js.map +1 -1
- package/dist/http.d.ts +44 -18
- package/dist/http.js +64 -33
- package/dist/http.js.map +1 -1
- package/dist/identity-dialog.d.ts +1 -1
- package/dist/identity-dialog.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/ingots.d.ts +20 -36
- package/dist/ingots.js +18 -45
- package/dist/ingots.js.map +1 -1
- package/dist/tus.d.ts +21 -76
- package/dist/tus.js +61 -222
- package/dist/tus.js.map +1 -1
- package/dist/types.d.ts +12 -56
- package/package.json +6 -6
- package/src/client.ts +2 -1
- package/src/config.ts +1 -13
- package/src/errors.ts +28 -19
- package/src/http.ts +74 -38
- package/src/identity-dialog.tsx +2 -2
- package/src/index.ts +0 -1
- package/src/ingots.ts +31 -73
- package/src/tus.ts +77 -286
- package/src/types.ts +12 -54
package/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,43 +112,21 @@ 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
|
|
163
|
-
* errors whichever engine carried the bytes. Forge's
|
|
164
|
-
* `X-Upload-Refusal`
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
118
|
+
* errors whichever engine carried the bytes. An `http` failure is Forge's
|
|
119
|
+
* answer (its status, body and `X-Upload-Refusal`), read by the one reading
|
|
120
|
+
* the XHR path's answers get (`readOutcome`, `errorOf`): the refusal Core
|
|
121
|
+
* decided is `upload_refused` in Core's words, a 402 the gate error, a gone
|
|
122
|
+
* session `sessionLost`, and any other status keeps its number for the
|
|
123
|
+
* caller's own policy (a 409 during finalize is a retry-later, not a lost
|
|
124
|
+
* session). The remaining kinds are transport outcomes: the caller's own
|
|
125
|
+
* abort is `cancelled`; an OS-ended task (`interrupted`) is a network
|
|
126
|
+
* failure, because the right response is the same as for a dropped
|
|
127
|
+
* connection: resume from the server offset. Anything that is not a
|
|
128
|
+
* `BackgroundTransferError` is an adapter defect and is reported as a
|
|
129
|
+
* network failure with the cause attached, never swallowed.
|
|
175
130
|
*/
|
|
176
131
|
export declare function mapBackgroundTransferError(err: unknown, context?: TusErrorContext): SparkVaultMobileError;
|
|
177
132
|
export declare class MobileTusUploader {
|
|
@@ -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 };
|
|
@@ -189,6 +165,8 @@ const RESUME_HEAD_REQUEST = { label: 'TUS HEAD', phase: 'create', gonePhase: 'cr
|
|
|
189
165
|
const OFFSET_HEAD_REQUEST = { label: 'TUS HEAD', phase: 'upload', gonePhase: 'create', receipt: false };
|
|
190
166
|
const CHUNK_REQUEST = { label: 'Chunk upload', phase: 'upload', gonePhase: 'upload', receipt: false };
|
|
191
167
|
const RECEIPT_REQUEST = { label: 'TUS finalization', phase: 'upload', gonePhase: 'create', receipt: true };
|
|
168
|
+
/** The OS transfer engine's PATCHes, whose failure arrives as the one answer that ended the range. */
|
|
169
|
+
const ENGINE_REQUEST = { label: 'Background chunk upload', phase: 'upload', gonePhase: 'upload', receipt: false };
|
|
192
170
|
/** What a caller hears once Forge kept deferring the publication until the upload's idle limit. */
|
|
193
171
|
const RECEIVED_PUBLISHING_MESSAGE = 'Upload received: Forge holds every byte and is still saving the file; it finishes without this device';
|
|
194
172
|
/**
|
|
@@ -231,17 +209,23 @@ function refusedError(status, envelope, coreCode, context, phase, cause = null)
|
|
|
231
209
|
function failureOf(answer, outcome, request, context) {
|
|
232
210
|
return { error: errorOf(answer, outcome, request, context), outcome, unanswered: answer.unanswered !== null };
|
|
233
211
|
}
|
|
234
|
-
|
|
212
|
+
/**
|
|
213
|
+
* The error a caller sees for an answer (failureOf). `cause` is what
|
|
214
|
+
* reported the answer when this SDK did not receive it itself: the OS
|
|
215
|
+
* engine's failure.
|
|
216
|
+
*/
|
|
217
|
+
function errorOf(answer, outcome, request, context, cause = null) {
|
|
235
218
|
if (answer.unanswered)
|
|
236
219
|
return answer.unanswered;
|
|
237
220
|
if (outcome.verdict === TRANSFER_VERDICT.REFUSED && outcome.coreCode !== null) {
|
|
238
|
-
return refusedError(answer.status, answer.envelope, outcome.coreCode, context, request.phase);
|
|
221
|
+
return refusedError(answer.status, answer.envelope, outcome.coreCode, context, request.phase, cause);
|
|
239
222
|
}
|
|
240
223
|
if (outcome.verdict === TRANSFER_VERDICT.GONE && request.gonePhase !== null) {
|
|
241
|
-
return sessionLostError(answer.status, context, request.gonePhase,
|
|
224
|
+
return sessionLostError(answer.status, context, request.gonePhase, cause, outcome.coreCode);
|
|
242
225
|
}
|
|
243
226
|
if (outcome.verdict === TRANSFER_VERDICT.DEFERRED) {
|
|
244
227
|
return new TusUploadError(RECEIVED_PUBLISHING_MESSAGE, {
|
|
228
|
+
cause,
|
|
245
229
|
httpStatus: answer.status,
|
|
246
230
|
code: RECEIVED_STILL_SAVING,
|
|
247
231
|
retryAfterMs: outcome.retryAfterMs,
|
|
@@ -254,47 +238,13 @@ function errorOf(answer, outcome, request, context) {
|
|
|
254
238
|
if (gateError)
|
|
255
239
|
return gateError;
|
|
256
240
|
return new TusUploadError(`${request.label} failed: ${answer.status} - ${answer.body.substring(0, 200)}`, {
|
|
241
|
+
cause,
|
|
257
242
|
httpStatus: answer.status,
|
|
258
243
|
retryAfterMs: outcome.retryAfterMs,
|
|
259
244
|
...context,
|
|
260
245
|
phase: request.phase,
|
|
261
246
|
});
|
|
262
247
|
}
|
|
263
|
-
/**
|
|
264
|
-
* The session's expiry as its latest answer named it, reported to the
|
|
265
|
-
* caller (`onSessionExpiry`) each time it changes. An answer that names none
|
|
266
|
-
* leaves it unknown: the request may have moved it unread (a commit whose
|
|
267
|
-
* answer was lost, a deferred finalization), and a caller that dropped a
|
|
268
|
-
* session on an expiry Forge has since moved on would re-send a whole file.
|
|
269
|
-
* A request cut short by the caller's abort keeps the last named value: it
|
|
270
|
-
* can only have slid the expiry by that one request's duration.
|
|
271
|
-
*/
|
|
272
|
-
class SessionExpiry {
|
|
273
|
-
constructor(known, report) {
|
|
274
|
-
this.known = known;
|
|
275
|
-
this.report = report;
|
|
276
|
-
}
|
|
277
|
-
/** Forge answered (or did not): the expiry is what the answer names. */
|
|
278
|
-
read(answer) {
|
|
279
|
-
this.become(parseUploadExpires(answer.header(UPLOAD_EXPIRES_HEADER)));
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* A request JS cannot follow while it runs is about to leave: the OS
|
|
283
|
-
* transfer engine's range, whose chunks commit (each sliding the expiry)
|
|
284
|
-
* with answers this upload never reads, possibly while the app is not
|
|
285
|
-
* running; or the single-chunk `backgroundUploader` PATCH, which reports
|
|
286
|
-
* nothing until it answers.
|
|
287
|
-
*/
|
|
288
|
-
forget() {
|
|
289
|
-
this.become(null);
|
|
290
|
-
}
|
|
291
|
-
become(expiresAt) {
|
|
292
|
-
if (expiresAt === this.known)
|
|
293
|
-
return;
|
|
294
|
-
this.known = expiresAt;
|
|
295
|
-
this.report?.(expiresAt);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
248
|
/** A backoff the caller's abort cuts short: resolves after `ms`, rejects as cancelled the moment the signal fires. */
|
|
299
249
|
function abortableDelay(ms, signal) {
|
|
300
250
|
return new Promise((resolve, reject) => {
|
|
@@ -400,21 +350,6 @@ async function fetchAnswer(config, url, init, label, context) {
|
|
|
400
350
|
}
|
|
401
351
|
return answerOf(response.status, name => response.headers.get(name), response.ok ? '' : await response.text().catch(() => ''));
|
|
402
352
|
}
|
|
403
|
-
/**
|
|
404
|
-
* Case-insensitive header lookup for adapter results. Native HTTP stacks
|
|
405
|
-
* hand headers back in whatever case they please (iOS lower-cases them),
|
|
406
|
-
* while Forge writes `Upload-Offset`.
|
|
407
|
-
*/
|
|
408
|
-
function getHeaderIgnoreCase(headers, name) {
|
|
409
|
-
if (!headers)
|
|
410
|
-
return null;
|
|
411
|
-
const wanted = name.toLowerCase();
|
|
412
|
-
for (const key of Object.keys(headers)) {
|
|
413
|
-
if (key.toLowerCase() === wanted)
|
|
414
|
-
return headers[key];
|
|
415
|
-
}
|
|
416
|
-
return null;
|
|
417
|
-
}
|
|
418
353
|
function resolveSessionSource(options, allowedHostPatterns) {
|
|
419
354
|
if (options.resume) {
|
|
420
355
|
const uploadUrl = readKeptSessionUrl(options.resume.uploadUrl, allowedHostPatterns);
|
|
@@ -423,11 +358,7 @@ function resolveSessionSource(options, allowedHostPatterns) {
|
|
|
423
358
|
if (!istk) {
|
|
424
359
|
throw new SparkVaultValidationError('resume.istk or forgeUrl is required to resume a TUS upload session');
|
|
425
360
|
}
|
|
426
|
-
|
|
427
|
-
if (expiresAt !== null && !Number.isFinite(expiresAt)) {
|
|
428
|
-
throw new SparkVaultValidationError('resume.expiresAt is an epoch-ms expiry or null', { received: expiresAt });
|
|
429
|
-
}
|
|
430
|
-
return { mode: 'resume', uploadUrl, istk, expiresAt };
|
|
361
|
+
return { mode: 'resume', uploadUrl, istk };
|
|
431
362
|
}
|
|
432
363
|
if (!options.forgeUrl) {
|
|
433
364
|
throw new SparkVaultValidationError('forgeUrl is required to create a TUS upload session');
|
|
@@ -435,9 +366,9 @@ function resolveSessionSource(options, allowedHostPatterns) {
|
|
|
435
366
|
return { mode: 'create', parsed: parseForgeUrl(options.forgeUrl, allowedHostPatterns) };
|
|
436
367
|
}
|
|
437
368
|
/**
|
|
438
|
-
* The session a create's 2xx opened: where it lives
|
|
439
|
-
*
|
|
440
|
-
*
|
|
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.
|
|
441
372
|
*/
|
|
442
373
|
function readCreatedSession(answer, endpoint, context) {
|
|
443
374
|
const uploadUrl = resolveForgeSessionUrl(answer.header('Location'), endpoint);
|
|
@@ -459,13 +390,13 @@ function readCreatedSession(answer, endpoint, context) {
|
|
|
459
390
|
phase: 'create',
|
|
460
391
|
});
|
|
461
392
|
}
|
|
462
|
-
return { uploadUrl, chunkSize
|
|
393
|
+
return { uploadUrl, chunkSize };
|
|
463
394
|
}
|
|
464
395
|
/**
|
|
465
396
|
* Where a HEAD's 2xx says the session stands: offset, length and chunk
|
|
466
397
|
* geometry, all required (a 2xx without them is not Forge, a captive
|
|
467
|
-
* portal's page being the classic case)
|
|
468
|
-
*
|
|
398
|
+
* portal's page being the classic case). An offset past the length can only
|
|
399
|
+
* be a corrupt session.
|
|
469
400
|
*/
|
|
470
401
|
function readSessionProbe(answer, phase, context) {
|
|
471
402
|
const offset = parseIntegerHeader(answer.header('Upload-Offset'), 0);
|
|
@@ -474,7 +405,7 @@ function readSessionProbe(answer, phase, context) {
|
|
|
474
405
|
if (offset === null || length === null || chunkSize === null || offset > length) {
|
|
475
406
|
throw new TusUploadError('TUS HEAD response missing or invalid Upload-Offset / Upload-Length / X-Chunk-Size headers', { ...context, phase });
|
|
476
407
|
}
|
|
477
|
-
return { offset, length, chunkSize
|
|
408
|
+
return { offset, length, chunkSize };
|
|
478
409
|
}
|
|
479
410
|
/**
|
|
480
411
|
* A session offset the server named (a HEAD's, or a 409's `Upload-Offset`),
|
|
@@ -588,60 +519,21 @@ function sendChunkOverXhr(transfer, chunk, offset, stallTimeoutMs, onChunkProgre
|
|
|
588
519
|
xhr.send({ base64: chunk.base64 });
|
|
589
520
|
});
|
|
590
521
|
}
|
|
591
|
-
/**
|
|
592
|
-
* PATCH a whole file as the session's only chunk through the app's
|
|
593
|
-
* single-request uploader (`backgroundUploader`) and resolve with Forge's
|
|
594
|
-
* answer, or with no answer when the adapter got none. The request reports
|
|
595
|
-
* no progress and takes no abort; whether it outlives a suspension is the
|
|
596
|
-
* uploader's (MobileBackgroundUploader). Its answer is read exactly like the
|
|
597
|
-
* XHR path's, so callers see one error taxonomy.
|
|
598
|
-
*/
|
|
599
|
-
async function sendWholeFileInBackground(uploader, transfer, fileUri, offset) {
|
|
600
|
-
let result;
|
|
601
|
-
try {
|
|
602
|
-
result = await uploader.uploadFile(transfer.uploadUrl, fileUri, {
|
|
603
|
-
method: 'PATCH',
|
|
604
|
-
headers: {
|
|
605
|
-
'Tus-Resumable': TUS_VERSION,
|
|
606
|
-
'Upload-Offset': String(offset),
|
|
607
|
-
'Content-Type': 'application/offset+octet-stream',
|
|
608
|
-
'X-ISTK': transfer.istk,
|
|
609
|
-
},
|
|
610
|
-
});
|
|
611
|
-
}
|
|
612
|
-
catch (err) {
|
|
613
|
-
// The adapter only throws when no HTTP response came back (offline, DNS,
|
|
614
|
-
// the OS cancelled the task); an HTTP failure is a status below.
|
|
615
|
-
return noAnswer(new TusUploadError(`Background chunk upload failed: ${getErrorMessage(err)}`, {
|
|
616
|
-
cause: err instanceof Error ? err : null,
|
|
617
|
-
...transfer.context,
|
|
618
|
-
phase: 'network',
|
|
619
|
-
}));
|
|
620
|
-
}
|
|
621
|
-
if (!Number.isSafeInteger(result.status) || result.status <= 0) {
|
|
622
|
-
return noAnswer(new TusUploadError(`Background chunk upload got no answer (status ${String(result.status)})`, {
|
|
623
|
-
...transfer.context,
|
|
624
|
-
phase: 'network',
|
|
625
|
-
}));
|
|
626
|
-
}
|
|
627
|
-
const answered = result.status >= 200 && result.status < 300;
|
|
628
|
-
return answerOf(result.status, name => getHeaderIgnoreCase(result.headers, name), !answered && typeof result.body === 'string' ? result.body : '');
|
|
629
|
-
}
|
|
630
522
|
/**
|
|
631
523
|
* Translate what the OS transfer adapter reports into the SDK's one error
|
|
632
524
|
* taxonomy, so a caller sees the same `TusUploadError` phases and typed gate
|
|
633
|
-
* errors whichever engine carried the bytes. Forge's
|
|
634
|
-
* `X-Upload-Refusal`
|
|
635
|
-
*
|
|
636
|
-
*
|
|
637
|
-
*
|
|
638
|
-
*
|
|
639
|
-
*
|
|
640
|
-
*
|
|
641
|
-
*
|
|
642
|
-
*
|
|
643
|
-
*
|
|
644
|
-
*
|
|
525
|
+
* errors whichever engine carried the bytes. An `http` failure is Forge's
|
|
526
|
+
* answer (its status, body and `X-Upload-Refusal`), read by the one reading
|
|
527
|
+
* the XHR path's answers get (`readOutcome`, `errorOf`): the refusal Core
|
|
528
|
+
* decided is `upload_refused` in Core's words, a 402 the gate error, a gone
|
|
529
|
+
* session `sessionLost`, and any other status keeps its number for the
|
|
530
|
+
* caller's own policy (a 409 during finalize is a retry-later, not a lost
|
|
531
|
+
* session). The remaining kinds are transport outcomes: the caller's own
|
|
532
|
+
* abort is `cancelled`; an OS-ended task (`interrupted`) is a network
|
|
533
|
+
* failure, because the right response is the same as for a dropped
|
|
534
|
+
* connection: resume from the server offset. Anything that is not a
|
|
535
|
+
* `BackgroundTransferError` is an adapter defect and is reported as a
|
|
536
|
+
* network failure with the cause attached, never swallowed.
|
|
645
537
|
*/
|
|
646
538
|
export function mapBackgroundTransferError(err, context = {}) {
|
|
647
539
|
if (!(err instanceof BackgroundTransferError)) {
|
|
@@ -655,25 +547,19 @@ export function mapBackgroundTransferError(err, context = {}) {
|
|
|
655
547
|
case 'http': {
|
|
656
548
|
const status = err.httpStatus;
|
|
657
549
|
const body = err.body ?? '';
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
if (outcome?.verdict === TRANSFER_VERDICT.REFUSED && outcome.coreCode !== null) {
|
|
667
|
-
return refusedError(status, envelope, outcome.coreCode, context, 'upload', err);
|
|
668
|
-
}
|
|
669
|
-
const gateError = gateErrorFromBody(status, envelope);
|
|
670
|
-
if (gateError)
|
|
671
|
-
return gateError;
|
|
672
|
-
if (outcome?.verdict === TRANSFER_VERDICT.GONE) {
|
|
673
|
-
return sessionLostError(status, context, 'upload', err, outcome.coreCode);
|
|
674
|
-
}
|
|
550
|
+
// A status the adapter lost leaves no answer to read: still an upload
|
|
551
|
+
// failure, never a gate or a lost session.
|
|
552
|
+
if (status === undefined || !Number.isSafeInteger(status) || status <= 0) {
|
|
553
|
+
return new TusUploadError(`${ENGINE_REQUEST.label} failed: unknown status - ${body.substring(0, 200)}`, {
|
|
554
|
+
cause: err,
|
|
555
|
+
...context,
|
|
556
|
+
phase: 'upload',
|
|
557
|
+
});
|
|
675
558
|
}
|
|
676
|
-
|
|
559
|
+
// The engine hands over the one header a refusal Core decided is named
|
|
560
|
+
// in, so a bodiless answer reads as it does on the XHR path.
|
|
561
|
+
const answer = answerOf(status, name => (name.toLowerCase() === UPLOAD_REFUSAL_HEADER.toLowerCase() ? err.uploadRefusal ?? null : null), body);
|
|
562
|
+
return errorOf(answer, readOutcome(answer, ENGINE_REQUEST), ENGINE_REQUEST, context, err);
|
|
677
563
|
}
|
|
678
564
|
case 'cancelled':
|
|
679
565
|
return new TusUploadError('Upload cancelled', { cause: err, ...context, phase: 'cancelled' });
|
|
@@ -780,20 +666,12 @@ export class MobileTusUploader {
|
|
|
780
666
|
let position;
|
|
781
667
|
if (source.mode === 'resume') {
|
|
782
668
|
debug?.log(`Resuming TUS upload for ${options.filename}`);
|
|
783
|
-
//
|
|
784
|
-
//
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
throw new TusUploadError(`TUS session expired at ${new Date(heldExpiry).toISOString()}`, {
|
|
788
|
-
...context,
|
|
789
|
-
phase: 'create',
|
|
790
|
-
sessionLost: true,
|
|
791
|
-
});
|
|
792
|
-
}
|
|
793
|
-
const expiry = new SessionExpiry(source.expiresAt, options.onSessionExpiry);
|
|
794
|
-
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 };
|
|
795
673
|
const probe = await this.readResumePoint(link, budget, fileSize);
|
|
796
|
-
transfer = { ...link,
|
|
674
|
+
transfer = { ...link, fileSize, chunkSize: probe.chunkSize, budget };
|
|
797
675
|
position = { offset: checkedServerOffset(probe.offset, transfer, 'create', context), published: false };
|
|
798
676
|
debug?.log(`TUS session for ${options.filename} is at offset ${position.offset}/${fileSize}`);
|
|
799
677
|
}
|
|
@@ -805,7 +683,6 @@ export class MobileTusUploader {
|
|
|
805
683
|
uploadUrl: created.uploadUrl,
|
|
806
684
|
istk,
|
|
807
685
|
context,
|
|
808
|
-
expiry: new SessionExpiry(created.expiresAt, options.onSessionExpiry),
|
|
809
686
|
signal,
|
|
810
687
|
fileSize,
|
|
811
688
|
chunkSize: created.chunkSize,
|
|
@@ -818,21 +695,18 @@ export class MobileTusUploader {
|
|
|
818
695
|
uploadUrl: created.uploadUrl,
|
|
819
696
|
istk,
|
|
820
697
|
chunkSize: created.chunkSize,
|
|
821
|
-
expiresAt: created.expiresAt,
|
|
822
698
|
});
|
|
823
699
|
}
|
|
824
700
|
// The OS transfer engine, when the app provides one, takes the whole
|
|
825
701
|
// remaining range in a single hand-off: it chains the chunks itself and
|
|
826
702
|
// keeps going while the app is suspended or killed, which no JS loop
|
|
827
703
|
// can. Any size and any offset qualify, so a resumed multi-GB video
|
|
828
|
-
// rides it too;
|
|
829
|
-
//
|
|
830
|
-
//
|
|
831
|
-
// 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.
|
|
832
707
|
const backgroundTransfer = options.transport === 'background' ? this.config.backgroundTransfer : undefined;
|
|
833
708
|
if (backgroundTransfer && position.offset < fileSize) {
|
|
834
709
|
ensureNotAborted(signal);
|
|
835
|
-
transfer.expiry.forget();
|
|
836
710
|
const policy = options.transferPolicy ?? {};
|
|
837
711
|
const finalOffset = await transferRemainderInBackground(backgroundTransfer, {
|
|
838
712
|
uploadUrl: transfer.uploadUrl,
|
|
@@ -857,14 +731,12 @@ export class MobileTusUploader {
|
|
|
857
731
|
// Forge answers only once the ingot is published. An abort that lands
|
|
858
732
|
// after the OS delivered the last byte changes nothing: the transfer
|
|
859
733
|
// is complete, and the loop below (with its abort check) has nothing
|
|
860
|
-
// left to do.
|
|
861
|
-
// upload is never reported as cancelled.
|
|
734
|
+
// left to do. A finished upload is never reported as cancelled.
|
|
862
735
|
position = { offset: fileSize, published: true };
|
|
863
736
|
}
|
|
864
737
|
const chunks = {
|
|
865
738
|
reader: fileReader,
|
|
866
739
|
uri: cleanUri,
|
|
867
|
-
transport: options.transport,
|
|
868
740
|
onChunkProgress: (offset, chunkBytesUploaded) => report(offset + chunkBytesUploaded),
|
|
869
741
|
};
|
|
870
742
|
while (!position.published) {
|
|
@@ -906,9 +778,9 @@ export class MobileTusUploader {
|
|
|
906
778
|
if (err instanceof SparkVaultMobileError)
|
|
907
779
|
throw err;
|
|
908
780
|
// Only a fileReader read or a caller's hook (onSessionCreated,
|
|
909
|
-
//
|
|
910
|
-
//
|
|
911
|
-
//
|
|
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
|
|
912
784
|
// context and nothing inferred about them.
|
|
913
785
|
throw TusUploadError.fromError(error, {
|
|
914
786
|
filename: options.filename,
|
|
@@ -1001,7 +873,6 @@ export class MobileTusUploader {
|
|
|
1001
873
|
signal: link.signal,
|
|
1002
874
|
timeoutMs: HEAD_AND_RECEIPT_TIMEOUT_MS,
|
|
1003
875
|
}, 'TUS HEAD request', link.context);
|
|
1004
|
-
link.expiry?.read(answer);
|
|
1005
876
|
const outcome = readOutcome(answer, request);
|
|
1006
877
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1007
878
|
return { probe: readSessionProbe(answer, request.phase, link.context) };
|
|
@@ -1016,7 +887,6 @@ export class MobileTusUploader {
|
|
|
1016
887
|
*/
|
|
1017
888
|
async sendChunkAt(transfer, offset, chunks) {
|
|
1018
889
|
const answer = await this.sendChunk(transfer, offset, chunks);
|
|
1019
|
-
transfer.expiry.read(answer);
|
|
1020
890
|
const outcome = readOutcome(answer, CHUNK_REQUEST);
|
|
1021
891
|
if (outcome.verdict !== TRANSFER_VERDICT.OK) {
|
|
1022
892
|
return this.positionAfterFailure(transfer, failureOf(answer, outcome, CHUNK_REQUEST, transfer.context), offset);
|
|
@@ -1039,21 +909,8 @@ export class MobileTusUploader {
|
|
|
1039
909
|
transfer.budget.progressed();
|
|
1040
910
|
return { offset: confirmed, published: confirmed === transfer.fileSize };
|
|
1041
911
|
}
|
|
1042
|
-
/**
|
|
912
|
+
/** Read the chunk at `offset` and PATCH it over XHR, resolving with Forge's answer. */
|
|
1043
913
|
async sendChunk(transfer, offset, chunks) {
|
|
1044
|
-
const backgroundUploader = chunks.transport === 'background' ? this.config.backgroundUploader : undefined;
|
|
1045
|
-
if (backgroundUploader && this.fitsOneBackgroundRequest(offset, transfer.fileSize, transfer.chunkSize)) {
|
|
1046
|
-
// The uploader takes no abort, so an abort that fires while this
|
|
1047
|
-
// request is in flight cannot stop it. It only ever carries a whole
|
|
1048
|
-
// single-chunk file, so a 2xx here means the transfer is complete: the
|
|
1049
|
-
// loop ends and the upload finishes normally (the caller verifies the
|
|
1050
|
-
// ingot), because a finished upload must never be reported as
|
|
1051
|
-
// cancelled, and a DELETE now would drop bytes Forge has already
|
|
1052
|
-
// finalized. Only an answer that leaves bytes missing reaches the abort
|
|
1053
|
-
// check before the next request.
|
|
1054
|
-
transfer.expiry.forget();
|
|
1055
|
-
return sendWholeFileInBackground(backgroundUploader, transfer, chunks.uri, offset);
|
|
1056
|
-
}
|
|
1057
914
|
const chunkLength = Math.min(transfer.chunkSize, transfer.fileSize - offset);
|
|
1058
915
|
const chunk = await this.readChunk(chunks.reader, chunks.uri, offset, chunkLength);
|
|
1059
916
|
return sendChunkOverXhr(transfer, chunk, offset, this.config.tusChunkTimeoutMs, chunkBytesUploaded => chunks.onChunkProgress(offset, chunkBytesUploaded));
|
|
@@ -1137,7 +994,6 @@ export class MobileTusUploader {
|
|
|
1137
994
|
signal: transfer.signal,
|
|
1138
995
|
timeoutMs: HEAD_AND_RECEIPT_TIMEOUT_MS,
|
|
1139
996
|
}, 'TUS finalization request', transfer.context);
|
|
1140
|
-
transfer.expiry.read(answer);
|
|
1141
997
|
const outcome = readOutcome(answer, RECEIPT_REQUEST);
|
|
1142
998
|
if (outcome.verdict === TRANSFER_VERDICT.OK) {
|
|
1143
999
|
return { offset: transfer.fileSize, published: true };
|
|
@@ -1208,25 +1064,13 @@ export class MobileTusUploader {
|
|
|
1208
1064
|
*/
|
|
1209
1065
|
async probeSession(session) {
|
|
1210
1066
|
const uploadUrl = readKeptSessionUrl(session.uploadUrl, this.config.allowedDownloadHostPatterns);
|
|
1211
|
-
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {}
|
|
1067
|
+
const head = await this.headSession({ uploadUrl, istk: session.istk, context: {} }, RESUME_HEAD_REQUEST);
|
|
1212
1068
|
if ('probe' in head)
|
|
1213
1069
|
return head.probe;
|
|
1214
1070
|
if (head.failure.outcome.verdict === TRANSFER_VERDICT.GONE)
|
|
1215
1071
|
return null;
|
|
1216
1072
|
throw head.failure.error;
|
|
1217
1073
|
}
|
|
1218
|
-
/**
|
|
1219
|
-
* Whether the remaining transfer is ONE PATCH the single-request uploader
|
|
1220
|
-
* (`backgroundUploader`) can send: nothing uploaded yet and the whole file
|
|
1221
|
-
* inside a single chunk. The uploader sends whole files and Forge rejects
|
|
1222
|
-
* any non-final chunk that is not exactly `X-Chunk-Size`, so a multi-chunk
|
|
1223
|
-
* file has no legal single request, and a partially uploaded session would
|
|
1224
|
-
* resend bytes Forge holds. A zero-byte file stays on the XHR path (it
|
|
1225
|
-
* sends no PATCH at all there).
|
|
1226
|
-
*/
|
|
1227
|
-
fitsOneBackgroundRequest(offset, fileSize, chunkSize) {
|
|
1228
|
-
return offset === 0 && fileSize > 0 && fileSize <= chunkSize;
|
|
1229
|
-
}
|
|
1230
1074
|
/**
|
|
1231
1075
|
* Release a session the caller kept on Forge (`abortBehavior: 'keep'`)
|
|
1232
1076
|
* and will not resume after all: the tus DELETE lets Forge drop the chunk
|
|
@@ -1271,7 +1115,7 @@ export class MobileTusUploader {
|
|
|
1271
1115
|
// The OS job, when there is one, is cancelled before the DELETE so the
|
|
1272
1116
|
// engine stops PATCHing a session Forge is about to forget (see
|
|
1273
1117
|
// terminateSession); without an engine the DELETE leaves the device
|
|
1274
|
-
// synchronously
|
|
1118
|
+
// synchronously.
|
|
1275
1119
|
const backgroundTransfer = this.config.backgroundTransfer;
|
|
1276
1120
|
const terminate = backgroundTransfer
|
|
1277
1121
|
? this.cancelBackgroundTransfer(backgroundTransfer, uploadUrl, debug).then(() => this.requestTerminate(uploadUrl, istk))
|
|
@@ -1324,12 +1168,7 @@ export class MobileTusUploader {
|
|
|
1324
1168
|
if (base64.length % 4 !== 0) {
|
|
1325
1169
|
throw new TusUploadError('fileReader.readAsBase64 must return canonical unwrapped base64 (length was not a multiple of 4)', { phase: 'upload' });
|
|
1326
1170
|
}
|
|
1327
|
-
|
|
1328
|
-
// short read (file truncated/evicted mid-upload) must keep the
|
|
1329
|
-
// offset-fallback arithmetic and the stall guard honest about the bytes
|
|
1330
|
-
// actually sent.
|
|
1331
|
-
const padding = base64.endsWith('==') ? 2 : base64.endsWith('=') ? 1 : 0;
|
|
1332
|
-
return { base64, byteLength: (base64.length / 4) * 3 - padding };
|
|
1171
|
+
return { base64 };
|
|
1333
1172
|
}
|
|
1334
1173
|
}
|
|
1335
1174
|
//# sourceMappingURL=tus.js.map
|