@sparkvault/sdk-mobile 7.0.0 → 7.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/dist/app-sdk/transfer-retry.d.ts +298 -0
- package/dist/app-sdk/transfer-retry.js +535 -0
- package/dist/app-sdk/transfer-retry.js.map +1 -0
- package/dist/app-sdk/upload-publication.d.ts +81 -0
- package/dist/app-sdk/upload-publication.js +93 -0
- package/dist/app-sdk/upload-publication.js.map +1 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +13 -0
- package/dist/config.js.map +1 -1
- package/dist/errors.d.ts +38 -4
- package/dist/errors.js +4 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/ingots.d.ts +30 -18
- package/dist/ingots.js +25 -19
- package/dist/ingots.js.map +1 -1
- package/dist/tus.d.ts +179 -14
- package/dist/tus.js +738 -300
- package/dist/tus.js.map +1 -1
- package/dist/types.d.ts +16 -8
- package/package.json +1 -1
- package/src/app-sdk/transfer-retry.js +563 -0
- package/src/app-sdk/upload-publication.js +102 -0
- package/src/config.ts +40 -0
- package/src/errors.ts +42 -5
- package/src/index.ts +2 -0
- package/src/ingots.ts +57 -26
- package/src/tus.ts +971 -378
- package/src/types.ts +16 -8
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
// GENERATED FILE — do not edit by hand.
|
|
2
|
+
// Source of truth: packages/app-sdk/src/transfer-retry.js
|
|
3
|
+
// Regenerate with: pnpm --filter @sparkvault/sdk-mobile build
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The transfer retry policy: what every first-party client does with the
|
|
7
|
+
* answer to a Forge transfer request (a tus chunk PATCH, an offset HEAD, the
|
|
8
|
+
* receipt PATCH at the final offset), in one place, so the Node uploader
|
|
9
|
+
* (forge-tus), the browser SDK (sdk-js bundles this module) and the mobile SDK
|
|
10
|
+
* (sdk-mobile carries a generated copy) read every answer the same way.
|
|
11
|
+
*
|
|
12
|
+
* It encodes what Google Drive, Google Cloud Storage, OneDrive, Dropbox and
|
|
13
|
+
* AWS document for resumable transfers:
|
|
14
|
+
* - only a request that got no answer, 408, 425, 429 or a 5xx is retried;
|
|
15
|
+
* every other 4xx is the server's verdict on the request;
|
|
16
|
+
* - `Retry-After` is a floor, never a ceiling;
|
|
17
|
+
* - retries back off exponentially, capped, with jitter;
|
|
18
|
+
* - before a chunk is sent again the server is asked for the offset it
|
|
19
|
+
* committed, because a request whose answer was lost may have landed;
|
|
20
|
+
* - a session that is gone (404, 410) is never resumed: only a new upload
|
|
21
|
+
* from byte 0 can succeed;
|
|
22
|
+
* - a transfer's retries are bounded in time and in failures in a row, and
|
|
23
|
+
* both bounds start over whenever the server confirms progress.
|
|
24
|
+
*
|
|
25
|
+
* Forge's own answers beyond the general rule:
|
|
26
|
+
* - the 409, in two kinds: one carrying `Upload-Offset` names where the
|
|
27
|
+
* session stands (continue from there), and one without it means another
|
|
28
|
+
* request holds the session's write lease, a chunk or the finalization
|
|
29
|
+
* (retry after its `Retry-After`);
|
|
30
|
+
* - a terminal answer (a 404, 410 or 412) names the code Core ended the
|
|
31
|
+
* upload with: its error envelope in `error.details.coreCode` for the
|
|
32
|
+
* refusals it words itself, and every one of them in `X-Upload-Refusal`,
|
|
33
|
+
* which is all a HEAD's answer, having no body, can carry; the body's code
|
|
34
|
+
* wins when both name one. The code decides the verdict: a full storage
|
|
35
|
+
* pool or a later upload that replaced this one is a refusal no new upload
|
|
36
|
+
* of the same file remedies, and any other code (the file the upload was
|
|
37
|
+
* for gone, a retired contract, a replacement base that moved on, a code
|
|
38
|
+
* this policy does not know) ends the session, which a new upload can
|
|
39
|
+
* start over;
|
|
40
|
+
* - a 503 carrying `Retry-After` to the receipt (the PATCH at the final
|
|
41
|
+
* offset) is a deferral: Forge holds every byte and is still publishing,
|
|
42
|
+
* and finishes without the client, so it is asked again, each time after a
|
|
43
|
+
* longer backoff, for as long as the idle limit allows, and never counts as
|
|
44
|
+
* a failure. Forge's deferral always names when to ask again; a 503 that
|
|
45
|
+
* does not is an ordinary failure.
|
|
46
|
+
*
|
|
47
|
+
* Isomorphic: no Node imports, no host APIs, no randomness. The jitter only
|
|
48
|
+
* has to spread retries, so it hashes the transfer and the attempt
|
|
49
|
+
* (docs/CONVENTIONS.md → Randomness).
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
import { isTransientHttpStatus } from './transient.js';
|
|
53
|
+
import { UPLOAD_FINAL_REFUSAL_CODES, UPLOAD_REFUSAL_HEADER } from './upload-publication.js';
|
|
54
|
+
|
|
55
|
+
/** What a client does after a transfer request, read from its answer. */
|
|
56
|
+
export const TRANSFER_VERDICT = Object.freeze({
|
|
57
|
+
/** 2xx: the request did what it asked. */
|
|
58
|
+
OK: 'ok',
|
|
59
|
+
/**
|
|
60
|
+
* No answer (a reset, a timeout), 408, 425, 429, a 5xx, or a 409 without
|
|
61
|
+
* `Upload-Offset` (another request holds the session's lease): back off,
|
|
62
|
+
* never sooner than `Retry-After`, then ask the server's offset before
|
|
63
|
+
* sending a chunk again.
|
|
64
|
+
*/
|
|
65
|
+
RETRY: 'retry',
|
|
66
|
+
/** 409 with `Upload-Offset`: the session stands elsewhere; continue from that offset at once. */
|
|
67
|
+
RESYNC: 'resync',
|
|
68
|
+
/**
|
|
69
|
+
* 503 with `Retry-After` to the receipt: every byte is received and the
|
|
70
|
+
* publication is still running. Ask again after a backoff that grows with
|
|
71
|
+
* each deferral, never sooner than `Retry-After`; only the idle limit ends
|
|
72
|
+
* the asking, never the failures in a row.
|
|
73
|
+
*/
|
|
74
|
+
DEFERRED: 'deferred',
|
|
75
|
+
/**
|
|
76
|
+
* 404 or 410 naming no code, or a 404, 410 or 412 naming a code of Core's
|
|
77
|
+
* that is not a final refusal (`coreCode` carries it): the session no
|
|
78
|
+
* longer exists; only a new upload from byte 0 can succeed.
|
|
79
|
+
*/
|
|
80
|
+
GONE: 'gone',
|
|
81
|
+
/**
|
|
82
|
+
* Any other status, or a 404, 410 or 412 naming a final refusal of Core's
|
|
83
|
+
* (`coreCode`: UPLOAD_POOL_FULL, UPLOAD_SUPERSEDED): the server's verdict on
|
|
84
|
+
* this request, which repeating it, or a new upload of the same file,
|
|
85
|
+
* earns again.
|
|
86
|
+
*/
|
|
87
|
+
REFUSED: 'refused'
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
/** The failures a transfer recovers from by trying again: the verdicts a budget's `failed` records. */
|
|
91
|
+
const RETRYABLE_VERDICTS = new Set([TRANSFER_VERDICT.RETRY, TRANSFER_VERDICT.RESYNC]);
|
|
92
|
+
|
|
93
|
+
/** RFC 8470: the server refused a request sent in TLS early data; the same request succeeds once the handshake completes. */
|
|
94
|
+
const TOO_EARLY = 425;
|
|
95
|
+
|
|
96
|
+
/** Forge's answer to a receipt whose publication it deferred. */
|
|
97
|
+
const SERVICE_UNAVAILABLE = 503;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The statuses a terminal answer naming Core's code arrives with: Forge's 410
|
|
101
|
+
* (UPLOAD_GONE, or Core's own relayed), a relayed 404, and Core's own 412.
|
|
102
|
+
*/
|
|
103
|
+
const TERMINAL_STATUSES = new Set([404, 410, 412]);
|
|
104
|
+
|
|
105
|
+
/** Core's error codes are upper-case words joined by underscores; anything else names no code. */
|
|
106
|
+
const CORE_CODE = /^[A-Z][A-Z0-9_]{0,63}$/;
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
const UPLOAD_OFFSET_HEADER = 'Upload-Offset';
|
|
110
|
+
const RETRY_AFTER_HEADER = 'Retry-After';
|
|
111
|
+
|
|
112
|
+
/** The tus `expiration` extension's header: when the session lapses, as an HTTP-date. */
|
|
113
|
+
export const UPLOAD_EXPIRES_HEADER = 'Upload-Expires';
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* How long a client waits for the answer to an offset HEAD or a receipt
|
|
117
|
+
* PATCH, whatever its other requests get: on a received upload, Forge answers
|
|
118
|
+
* either only after its own call to Core (up to 30 s) has published the
|
|
119
|
+
* upload or deferred it, so each gets twice that.
|
|
120
|
+
*/
|
|
121
|
+
export const HEAD_AND_RECEIPT_TIMEOUT_MS = 60_000;
|
|
122
|
+
|
|
123
|
+
/** How a retry's delay spreads past its backoff: up to this much more. */
|
|
124
|
+
const JITTER_SPAN_MS = 1000;
|
|
125
|
+
|
|
126
|
+
/** How long a transfer may sit without progress or a sign of life by default. */
|
|
127
|
+
const DEFAULT_IDLE_LIMIT_MS = 10 * 60 * 1000;
|
|
128
|
+
|
|
129
|
+
/** How many failures in a row, without progress between them, end a transfer by default. */
|
|
130
|
+
const DEFAULT_MAX_CONSECUTIVE_FAILURES = 8;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @typedef {'ok'|'retry'|'resync'|'deferred'|'gone'|'refused'} TransferVerdict
|
|
134
|
+
*/
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* The verdicts a transfer recovers from by trying again that count as
|
|
138
|
+
* failures: the ones a budget's `failed` records.
|
|
139
|
+
* @typedef {'retry'|'resync'} RetryableTransferVerdict
|
|
140
|
+
*/
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @typedef {Object} TransferOutcome
|
|
144
|
+
* @property {TransferVerdict} verdict - What the client does next
|
|
145
|
+
* @property {number|null} retryAfterMs - The answer's `Retry-After`, in ms from
|
|
146
|
+
* the answer (an HTTP-date already past reads as 0); null when absent or unreadable
|
|
147
|
+
* @property {number|null} offset - The answer's `Upload-Offset`; null when absent or unreadable
|
|
148
|
+
* @property {string|null} coreCode - The code Core ended the upload with, as a
|
|
149
|
+
* terminal answer named it (a 404, 410 or 412: its body's details or, those
|
|
150
|
+
* naming none, its `X-Upload-Refusal`): REFUSED for a final refusal
|
|
151
|
+
* (`UPLOAD_POOL_FULL`, `UPLOAD_SUPERSEDED`), GONE for any other code
|
|
152
|
+
* (`NOT_FOUND`, `UPLOAD_PROTOCOL_RETIRED`, a 412's, one this policy does not
|
|
153
|
+
* know); null when the answer names none
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Read the answer to a transfer request the way the policy does.
|
|
158
|
+
*
|
|
159
|
+
* @param {Object} response
|
|
160
|
+
* @param {number} response.status - The HTTP status; 0 when the request got no answer
|
|
161
|
+
* @param {((name: string) => string|null)|null} [response.headers] - Reads a
|
|
162
|
+
* response header by name: a fetch `Headers`' `get` or an XHR's
|
|
163
|
+
* `getResponseHeader`, bound to its owner (`(name) => xhr.getResponseHeader(name)`)
|
|
164
|
+
* @param {unknown} [response.details] - The `details` of the error envelope the
|
|
165
|
+
* answer's body carries (`{ error: { code, message, details } }`), as the
|
|
166
|
+
* runtime's one reader of a failed answer parsed it from the whole body;
|
|
167
|
+
* where Forge names the refusal Core decided (`coreCode`), ahead of the
|
|
168
|
+
* answer's `X-Upload-Refusal`. Anything that is not an object names none.
|
|
169
|
+
* @param {boolean} [response.receipt] - The request was the receipt: the PATCH
|
|
170
|
+
* at the final offset, whose 503 carrying `Retry-After` is a deferral
|
|
171
|
+
* @param {number} [response.now] - When the answer arrived (epoch ms), which an
|
|
172
|
+
* HTTP-date `Retry-After` counts from
|
|
173
|
+
* @returns {TransferOutcome}
|
|
174
|
+
* @throws {TypeError} When the status is not a whole number, headers is not a
|
|
175
|
+
* function, or receipt is not a boolean
|
|
176
|
+
*/
|
|
177
|
+
export function classifyTransferResponse({ status, headers = null, details = null, receipt = false, now = Date.now() }) {
|
|
178
|
+
if (!Number.isSafeInteger(status) || status < 0) {
|
|
179
|
+
throw new TypeError(`A transfer answer's status is a whole number (0 for no answer), not ${status}`);
|
|
180
|
+
}
|
|
181
|
+
if (headers !== null && typeof headers !== 'function') {
|
|
182
|
+
throw new TypeError('A transfer answer\'s headers are read through a function from a header name to its value');
|
|
183
|
+
}
|
|
184
|
+
if (typeof receipt !== 'boolean') {
|
|
185
|
+
throw new TypeError(`Whether a transfer request was the receipt is a boolean, not ${receipt}`);
|
|
186
|
+
}
|
|
187
|
+
if (status === 0) {
|
|
188
|
+
return { verdict: TRANSFER_VERDICT.RETRY, retryAfterMs: null, offset: null, coreCode: null };
|
|
189
|
+
}
|
|
190
|
+
const header = (name) => (headers === null ? null : headers(name));
|
|
191
|
+
const offset = readCount(header(UPLOAD_OFFSET_HEADER));
|
|
192
|
+
const coreCode = TERMINAL_STATUSES.has(status)
|
|
193
|
+
? coreCodeIn(details) ?? coreCodeOf(header(UPLOAD_REFUSAL_HEADER))
|
|
194
|
+
: null;
|
|
195
|
+
const retryAfterMs = readRetryAfterMs(header(RETRY_AFTER_HEADER), now);
|
|
196
|
+
return {
|
|
197
|
+
verdict: verdictFor(status, { offset, coreCode, deferral: receipt && retryAfterMs !== null }),
|
|
198
|
+
retryAfterMs,
|
|
199
|
+
offset,
|
|
200
|
+
coreCode
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* The verdict on a status that came with an answer: `offset` its readable
|
|
206
|
+
* `Upload-Offset`, `coreCode` the code of Core's a terminal answer names,
|
|
207
|
+
* `deferral` whether it answered the receipt with a readable `Retry-After`.
|
|
208
|
+
*/
|
|
209
|
+
function verdictFor(status, { offset, coreCode, deferral }) {
|
|
210
|
+
if (status >= 200 && status < 300) return TRANSFER_VERDICT.OK;
|
|
211
|
+
if (status === 409) return offset === null ? TRANSFER_VERDICT.RETRY : TRANSFER_VERDICT.RESYNC;
|
|
212
|
+
if (coreCode !== null) return UPLOAD_FINAL_REFUSAL_CODES.includes(coreCode) ? TRANSFER_VERDICT.REFUSED : TRANSFER_VERDICT.GONE;
|
|
213
|
+
if (status === 404 || status === 410) return TRANSFER_VERDICT.GONE;
|
|
214
|
+
if (deferral && status === SERVICE_UNAVAILABLE) return TRANSFER_VERDICT.DEFERRED;
|
|
215
|
+
if (status === TOO_EARLY || isTransientHttpStatus(status)) return TRANSFER_VERDICT.RETRY;
|
|
216
|
+
return TRANSFER_VERDICT.REFUSED;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** The code an error envelope's details name (`coreCode`), or null. */
|
|
220
|
+
function coreCodeIn(details) {
|
|
221
|
+
return details && typeof details === 'object' ? coreCodeOf(details.coreCode) : null;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** A value that is one of Core's codes (surrounding whitespace allowed), or null. */
|
|
225
|
+
function coreCodeOf(value) {
|
|
226
|
+
if (typeof value !== 'string') return null;
|
|
227
|
+
const code = value.trim();
|
|
228
|
+
return CORE_CODE.test(code) ? code : null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** A header's non-negative whole number (surrounding whitespace allowed), or null. */
|
|
232
|
+
function readCount(value) {
|
|
233
|
+
if (typeof value !== 'string') return null;
|
|
234
|
+
const text = value.trim();
|
|
235
|
+
if (!/^\d+$/.test(text)) return null;
|
|
236
|
+
const count = Number(text);
|
|
237
|
+
return Number.isSafeInteger(count) ? count : null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** RFC 9110 §10.2.3: `Retry-After` is delay-seconds or an HTTP-date. */
|
|
241
|
+
function readRetryAfterMs(value, now) {
|
|
242
|
+
const seconds = readCount(value);
|
|
243
|
+
if (seconds !== null) {
|
|
244
|
+
const ms = seconds * 1000;
|
|
245
|
+
return Number.isSafeInteger(ms) ? ms : null;
|
|
246
|
+
}
|
|
247
|
+
const at = typeof value === 'string' ? parseHttpDate(value.trim(), now) : null;
|
|
248
|
+
return at === null ? null : Math.max(0, at - now);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
252
|
+
const DAY_NAME = '(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
|
|
253
|
+
const LONG_DAY_NAME = '(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
|
|
254
|
+
const MONTH = `(${MONTHS.join('|')})`;
|
|
255
|
+
const TIME_OF_DAY = '(\\d{2}):(\\d{2}):(\\d{2})';
|
|
256
|
+
|
|
257
|
+
/** IMF-fixdate, what every current server sends: `Sun, 06 Nov 1994 08:49:37 GMT`. */
|
|
258
|
+
const IMF_FIXDATE = new RegExp(`^${DAY_NAME}, (\\d{2}) ${MONTH} (\\d{4}) ${TIME_OF_DAY} GMT$`);
|
|
259
|
+
/** The obsolete RFC 850 form a recipient still accepts: `Sunday, 06-Nov-94 08:49:37 GMT`. */
|
|
260
|
+
const RFC850_DATE = new RegExp(`^${LONG_DAY_NAME}, (\\d{2})-${MONTH}-(\\d{2}) ${TIME_OF_DAY} GMT$`);
|
|
261
|
+
/** The obsolete asctime form a recipient still accepts: `Sun Nov 6 08:49:37 1994`. */
|
|
262
|
+
const ASCTIME_DATE = new RegExp(`^${DAY_NAME} ${MONTH} ( \\d|\\d{2}) ${TIME_OF_DAY} (\\d{4})$`);
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* An HTTP-date (RFC 9110 §5.6.7, all three forms a recipient must accept) as
|
|
266
|
+
* epoch ms, or null when it is not one or names no real moment.
|
|
267
|
+
*
|
|
268
|
+
* @param {string} text
|
|
269
|
+
* @param {number} now - epoch ms, which places an RFC 850 two-digit year
|
|
270
|
+
* @returns {number|null}
|
|
271
|
+
*/
|
|
272
|
+
function parseHttpDate(text, now) {
|
|
273
|
+
let match = IMF_FIXDATE.exec(text);
|
|
274
|
+
if (match) {
|
|
275
|
+
const [, day, month, year, hours, minutes, seconds] = match;
|
|
276
|
+
return utcMs(Number(year), month, Number(day), hours, minutes, seconds);
|
|
277
|
+
}
|
|
278
|
+
match = RFC850_DATE.exec(text);
|
|
279
|
+
if (match) {
|
|
280
|
+
const [, day, month, shortYear, hours, minutes, seconds] = match;
|
|
281
|
+
return utcMs(rfc850Year(Number(shortYear), now), month, Number(day), hours, minutes, seconds);
|
|
282
|
+
}
|
|
283
|
+
match = ASCTIME_DATE.exec(text);
|
|
284
|
+
if (match) {
|
|
285
|
+
const [, month, day, hours, minutes, seconds, year] = match;
|
|
286
|
+
return utcMs(Number(year), month, Number(day.trim()), hours, minutes, seconds);
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** RFC 9110: a two-digit year more than 50 years ahead is the latest past year with those digits. */
|
|
292
|
+
function rfc850Year(shortYear, now) {
|
|
293
|
+
const currentYear = new Date(now).getUTCFullYear();
|
|
294
|
+
const year = currentYear - (currentYear % 100) + shortYear;
|
|
295
|
+
return year > currentYear + 50 ? year - 100 : year;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** The UTC instant of a calendar date and time of day, or null for a date or time that does not exist. */
|
|
299
|
+
function utcMs(year, monthName, day, hours, minutes, seconds) {
|
|
300
|
+
const monthIndex = MONTHS.indexOf(monthName);
|
|
301
|
+
const [h, m, s] = [Number(hours), Number(minutes), Number(seconds)];
|
|
302
|
+
// A leap second (60) is a real time of day; the instant rolls into the next minute.
|
|
303
|
+
if (h > 23 || m > 59 || s > 60) return null;
|
|
304
|
+
const date = new Date(0);
|
|
305
|
+
date.setUTCFullYear(year, monthIndex, day);
|
|
306
|
+
if (date.getUTCFullYear() !== year || date.getUTCMonth() !== monthIndex || date.getUTCDate() !== day) {
|
|
307
|
+
return null;
|
|
308
|
+
}
|
|
309
|
+
return date.getTime() + ((h * 60 + m) * 60 + s) * 1000;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* When a session lapses, read from its `Upload-Expires` (an HTTP-date).
|
|
314
|
+
*
|
|
315
|
+
* @param {unknown} headerValue - The header as received (null when absent)
|
|
316
|
+
* @param {number} [now] - epoch ms, which places an obsolete two-digit year
|
|
317
|
+
* @returns {number|null} epoch ms, or null when absent or not an HTTP-date
|
|
318
|
+
*/
|
|
319
|
+
export function parseUploadExpires(headerValue, now = Date.now()) {
|
|
320
|
+
return typeof headerValue === 'string' ? parseHttpDate(headerValue.trim(), now) : null;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const FNV_OFFSET_BASIS = 0x811c9dc5;
|
|
324
|
+
const FNV_PRIME = 0x01000193;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 32-bit FNV-1a over the text's UTF-8 bytes: the jitter's hash. A retry's
|
|
328
|
+
* delay only has to spread, so it hashes what identifies the retry and draws
|
|
329
|
+
* nothing (docs/CONVENTIONS.md → Randomness).
|
|
330
|
+
*
|
|
331
|
+
* @param {string} text
|
|
332
|
+
* @returns {number} An unsigned 32-bit integer
|
|
333
|
+
* @throws {TypeError} When the text is not a string
|
|
334
|
+
*/
|
|
335
|
+
export function fnv1a32(text) {
|
|
336
|
+
if (typeof text !== 'string') {
|
|
337
|
+
throw new TypeError(`fnv1a32 hashes a string, not ${typeof text}`);
|
|
338
|
+
}
|
|
339
|
+
let hash = FNV_OFFSET_BASIS;
|
|
340
|
+
const mix = (byte) => {
|
|
341
|
+
hash = Math.imul(hash ^ byte, FNV_PRIME) >>> 0;
|
|
342
|
+
};
|
|
343
|
+
for (const character of text) {
|
|
344
|
+
let point = character.codePointAt(0);
|
|
345
|
+
// A lone surrogate has no UTF-8 form; it encodes as U+FFFD, as TextEncoder does.
|
|
346
|
+
if (point >= 0xd800 && point <= 0xdfff) point = 0xfffd;
|
|
347
|
+
if (point < 0x80) {
|
|
348
|
+
mix(point);
|
|
349
|
+
} else if (point < 0x800) {
|
|
350
|
+
mix(0xc0 | (point >> 6));
|
|
351
|
+
mix(0x80 | (point & 0x3f));
|
|
352
|
+
} else if (point < 0x10000) {
|
|
353
|
+
mix(0xe0 | (point >> 12));
|
|
354
|
+
mix(0x80 | ((point >> 6) & 0x3f));
|
|
355
|
+
mix(0x80 | (point & 0x3f));
|
|
356
|
+
} else {
|
|
357
|
+
mix(0xf0 | (point >> 18));
|
|
358
|
+
mix(0x80 | ((point >> 12) & 0x3f));
|
|
359
|
+
mix(0x80 | ((point >> 6) & 0x3f));
|
|
360
|
+
mix(0x80 | (point & 0x3f));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return hash;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** Transfers this runtime has keyed by `transferJitterKey`: with the start time, what tells them apart. */
|
|
367
|
+
let transfersKeyed = 0;
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* What a transfer's retries spread by when no session URL keys them yet (a
|
|
371
|
+
* session create) or ever (a download, whose link carries its token): the
|
|
372
|
+
* scope (the create endpoint, or the kind of transfer), when the transfer
|
|
373
|
+
* started, and this runtime's count of keyed transfers, which tell
|
|
374
|
+
* concurrent transfers apart. A transfer's token is a credential and never
|
|
375
|
+
* feeds anything observable such as a delay, so no part of the key derives
|
|
376
|
+
* from it (docs/CONVENTIONS.md → Randomness).
|
|
377
|
+
*
|
|
378
|
+
* @param {string} scope - The endpoint or the kind of transfer
|
|
379
|
+
* @returns {string} The key, for every retry of this one transfer
|
|
380
|
+
* @throws {TypeError} For a scope that is not a non-empty string
|
|
381
|
+
*/
|
|
382
|
+
export function transferJitterKey(scope) {
|
|
383
|
+
if (typeof scope !== 'string' || scope.length === 0) {
|
|
384
|
+
throw new TypeError('A transfer jitter key needs the scope its transfer runs in');
|
|
385
|
+
}
|
|
386
|
+
transfersKeyed += 1;
|
|
387
|
+
return `${scope}#${Date.now()}-${transfersKeyed}`;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* How long to wait before retry `attempt` of a transfer: `baseMs` doubling per
|
|
392
|
+
* attempt, capped at `capMs`, plus a jitter in [0, 1000) ms hashed from the
|
|
393
|
+
* transfer and the attempt, and never less than the server's `Retry-After`.
|
|
394
|
+
* The same transfer and attempt always wait the same; different transfers
|
|
395
|
+
* failing together spread out.
|
|
396
|
+
*
|
|
397
|
+
* @param {Object} options
|
|
398
|
+
* @param {number} options.attempt - Which retry this is, from 1
|
|
399
|
+
* @param {number|null} [options.retryAfterMs] - The answer's `Retry-After`: a floor
|
|
400
|
+
* @param {number} [options.baseMs] - The first retry's backoff
|
|
401
|
+
* @param {number} [options.capMs] - The longest backoff, before jitter
|
|
402
|
+
* @param {string} options.jitterKey - What identifies the transfer (its session
|
|
403
|
+
* URL, or a `transferJitterKey`)
|
|
404
|
+
* @returns {number} ms
|
|
405
|
+
* @throws {TypeError} For an attempt that is not a whole number from 1, a
|
|
406
|
+
* missing jitter key, or a delay bound that is not a positive number of ms
|
|
407
|
+
*/
|
|
408
|
+
export function retryDelayMs({ attempt, retryAfterMs = null, baseMs = 1000, capMs = 32000, jitterKey }) {
|
|
409
|
+
if (!Number.isSafeInteger(attempt) || attempt < 1) {
|
|
410
|
+
throw new TypeError(`A retry's attempt is a whole number from 1, not ${attempt}`);
|
|
411
|
+
}
|
|
412
|
+
if (typeof jitterKey !== 'string' || jitterKey.length === 0) {
|
|
413
|
+
throw new TypeError('A retry delay needs the key its transfer spreads by');
|
|
414
|
+
}
|
|
415
|
+
if (!isDuration(baseMs) || baseMs === 0 || !isDuration(capMs) || capMs < baseMs) {
|
|
416
|
+
throw new TypeError(`A retry backs off from a positive base (${baseMs} ms) up to a cap no smaller (${capMs} ms)`);
|
|
417
|
+
}
|
|
418
|
+
if (retryAfterMs !== null && !isDuration(retryAfterMs)) {
|
|
419
|
+
throw new TypeError(`A Retry-After floor is a non-negative number of ms, not ${retryAfterMs}`);
|
|
420
|
+
}
|
|
421
|
+
const backoff = Math.min(capMs, baseMs * 2 ** (attempt - 1));
|
|
422
|
+
const jitter = Math.floor((fnv1a32(`${jitterKey}|${attempt}`) / 2 ** 32) * JITTER_SPAN_MS);
|
|
423
|
+
return Math.max(backoff + jitter, retryAfterMs ?? 0);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function isDuration(ms) {
|
|
427
|
+
return Number.isFinite(ms) && ms >= 0;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* @typedef {Object} TransferBudget
|
|
432
|
+
* @property {() => void} progressed - The server confirmed progress (bytes
|
|
433
|
+
* landed, or the offset it names moved forward): the failures in a row and
|
|
434
|
+
* the idle clock start over
|
|
435
|
+
* @property {() => void} alive - A sign of life the server has not confirmed
|
|
436
|
+
* (bytes are leaving, or the device is back online): the idle clock starts
|
|
437
|
+
* over and the failures in a row stand, so attempts that die before the
|
|
438
|
+
* server confirms anything still end the transfer
|
|
439
|
+
* @property {(verdict: RetryableTransferVerdict, retryAfterMs?: number|null) => boolean} failed -
|
|
440
|
+
* Records a retryable answer (RETRY or RESYNC) and says whether another
|
|
441
|
+
* attempt is allowed: the failures in a row stay under the max, and the
|
|
442
|
+
* attempt `retryAfterMs` allows would start inside the idle limit, so a
|
|
443
|
+
* transfer never sleeps on a Retry-After longer than its budget. The limit
|
|
444
|
+
* bounds when a retry is decided; a retry decided inside it still waits its
|
|
445
|
+
* own backoff, so a transfer ends within one backoff of the limit.
|
|
446
|
+
* @property {(retryAfterMs?: number|null) => boolean} deferred - Records a
|
|
447
|
+
* DEFERRED answer and says whether to ask again: only the idle limit bounds
|
|
448
|
+
* it, as `failed` applies it, because a deferral is the server at work,
|
|
449
|
+
* never a failure, so the failures in a row stand
|
|
450
|
+
* @property {number} consecutiveFailures - Retryable failures since the last progress
|
|
451
|
+
* @property {number} attempt - Failures and deferrals since the last progress:
|
|
452
|
+
* the attempt the next wait's delay is computed for, so each deferral backs
|
|
453
|
+
* the next ask off as a failure does, sparing a Core that is struggling to
|
|
454
|
+
* publish
|
|
455
|
+
* @property {number} msSinceProgress - ms since the last progress or sign of life
|
|
456
|
+
*/
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* The retry budget of one transfer: retries are bounded by failures in a row
|
|
460
|
+
* and by time without progress, deferrals by time alone, and both bounds
|
|
461
|
+
* start over whenever the server confirms progress, so a slow transfer that
|
|
462
|
+
* keeps landing bytes is never cut off while a dead one ends in bounded time.
|
|
463
|
+
*
|
|
464
|
+
* @param {Object} [options]
|
|
465
|
+
* @param {number} [options.idleLimitMs] - How long the transfer may go without
|
|
466
|
+
* progress or a sign of life
|
|
467
|
+
* @param {number} [options.maxConsecutiveFailures] - Failures in a row that end the transfer
|
|
468
|
+
* @param {() => number} [options.now] - The clock, epoch ms
|
|
469
|
+
* @returns {TransferBudget}
|
|
470
|
+
* @throws {TypeError} For a limit that is not a positive number, or a clock that is not a function
|
|
471
|
+
*/
|
|
472
|
+
export function createTransferBudget({
|
|
473
|
+
idleLimitMs = DEFAULT_IDLE_LIMIT_MS,
|
|
474
|
+
maxConsecutiveFailures = DEFAULT_MAX_CONSECUTIVE_FAILURES,
|
|
475
|
+
now = () => Date.now()
|
|
476
|
+
} = {}) {
|
|
477
|
+
if (!isDuration(idleLimitMs) || idleLimitMs === 0) {
|
|
478
|
+
throw new TypeError(`A transfer's idle limit is a positive number of ms, not ${idleLimitMs}`);
|
|
479
|
+
}
|
|
480
|
+
if (!Number.isSafeInteger(maxConsecutiveFailures) || maxConsecutiveFailures < 1) {
|
|
481
|
+
throw new TypeError(`A transfer's failure limit is a whole number from 1, not ${maxConsecutiveFailures}`);
|
|
482
|
+
}
|
|
483
|
+
if (typeof now !== 'function') {
|
|
484
|
+
throw new TypeError('A transfer budget reads time from a clock function');
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
let consecutiveFailures = 0;
|
|
488
|
+
let deferrals = 0;
|
|
489
|
+
let lastSignOfLifeAt = now();
|
|
490
|
+
const msIdle = () => now() - lastSignOfLifeAt;
|
|
491
|
+
const startsInsideIdleLimit = (retryAfterMs) => {
|
|
492
|
+
if (retryAfterMs !== null && !isDuration(retryAfterMs)) {
|
|
493
|
+
throw new TypeError(`A Retry-After floor is a non-negative number of ms, not ${retryAfterMs}`);
|
|
494
|
+
}
|
|
495
|
+
return msIdle() + (retryAfterMs ?? 0) < idleLimitMs;
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
return Object.freeze({
|
|
499
|
+
progressed() {
|
|
500
|
+
consecutiveFailures = 0;
|
|
501
|
+
deferrals = 0;
|
|
502
|
+
lastSignOfLifeAt = now();
|
|
503
|
+
},
|
|
504
|
+
alive() {
|
|
505
|
+
lastSignOfLifeAt = now();
|
|
506
|
+
},
|
|
507
|
+
failed(verdict, retryAfterMs = null) {
|
|
508
|
+
if (!RETRYABLE_VERDICTS.has(verdict)) {
|
|
509
|
+
throw new TypeError(`A transfer budget records retryable answers only, not '${verdict}'`);
|
|
510
|
+
}
|
|
511
|
+
const allowed = startsInsideIdleLimit(retryAfterMs);
|
|
512
|
+
consecutiveFailures += 1;
|
|
513
|
+
return allowed && consecutiveFailures < maxConsecutiveFailures;
|
|
514
|
+
},
|
|
515
|
+
deferred(retryAfterMs = null) {
|
|
516
|
+
const allowed = startsInsideIdleLimit(retryAfterMs);
|
|
517
|
+
deferrals += 1;
|
|
518
|
+
return allowed;
|
|
519
|
+
},
|
|
520
|
+
get consecutiveFailures() {
|
|
521
|
+
return consecutiveFailures;
|
|
522
|
+
},
|
|
523
|
+
get attempt() {
|
|
524
|
+
return consecutiveFailures + deferrals;
|
|
525
|
+
},
|
|
526
|
+
get msSinceProgress() {
|
|
527
|
+
return msIdle();
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** Why an offset the server names cannot be resumed from. */
|
|
533
|
+
export const RESUME_OFFSET_PROBLEM = Object.freeze({
|
|
534
|
+
/** Not a non-negative whole number of bytes. */
|
|
535
|
+
INVALID: 'invalid',
|
|
536
|
+
/** Past the upload's length: resuming from it would report a partial upload complete. */
|
|
537
|
+
BEYOND_TOTAL: 'beyond_total',
|
|
538
|
+
/** Inside a chunk: a session commits whole chunks only, so the answer is corrupt. */
|
|
539
|
+
MISALIGNED: 'misaligned'
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Why a session offset the server named (a HEAD's, or a 409's `Upload-Offset`)
|
|
544
|
+
* cannot be resumed from, or null when it can. Forge commits whole chunks, so
|
|
545
|
+
* a session stands at a multiple of its chunk size or at its total.
|
|
546
|
+
*
|
|
547
|
+
* @param {number|null} offset
|
|
548
|
+
* @param {Object} geometry
|
|
549
|
+
* @param {number} geometry.totalSize - The upload's length (a non-negative whole number)
|
|
550
|
+
* @param {number} geometry.chunkSize - The session's chunk size (a positive whole number)
|
|
551
|
+
* @returns {'invalid'|'beyond_total'|'misaligned'|null}
|
|
552
|
+
* @throws {TypeError} When the length is not a non-negative whole number or
|
|
553
|
+
* the chunk size not a positive one
|
|
554
|
+
*/
|
|
555
|
+
export function resumeOffsetProblem(offset, { totalSize, chunkSize }) {
|
|
556
|
+
if (!Number.isSafeInteger(totalSize) || totalSize < 0 || !Number.isSafeInteger(chunkSize) || chunkSize < 1) {
|
|
557
|
+
throw new TypeError(`An upload's geometry is a whole length and a positive chunk size, not ${totalSize} and ${chunkSize}`);
|
|
558
|
+
}
|
|
559
|
+
if (!Number.isSafeInteger(offset) || offset < 0) return RESUME_OFFSET_PROBLEM.INVALID;
|
|
560
|
+
if (offset > totalSize) return RESUME_OFFSET_PROBLEM.BEYOND_TOTAL;
|
|
561
|
+
if (offset !== totalSize && offset % chunkSize !== 0) return RESUME_OFFSET_PROBLEM.MISALIGNED;
|
|
562
|
+
return null;
|
|
563
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// GENERATED FILE — do not edit by hand.
|
|
2
|
+
// Source of truth: packages/app-sdk/src/upload-publication.js
|
|
3
|
+
// Regenerate with: pnpm --filter @sparkvault/sdk-mobile build
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The upload publication contract between Forge and Core's activation.
|
|
7
|
+
*
|
|
8
|
+
* Forge stamps UPLOAD_PUBLICATION_PROTOCOL on every upload session it creates
|
|
9
|
+
* and on the final state it hands Core's activation (POST
|
|
10
|
+
* /v1/vaults/:vault_id/ingots/:ingot_id/activate); activation refuses any
|
|
11
|
+
* other version. The two services deploy separately, so a change to what
|
|
12
|
+
* either side sends or expects moves this number: a session or a finalize
|
|
13
|
+
* from the other side's contract then fails closed ("restart the upload")
|
|
14
|
+
* instead of publishing under a contract the other side does not hold.
|
|
15
|
+
*
|
|
16
|
+
* Version 2: an upload's own metadata (content type, original file name,
|
|
17
|
+
* uploader, content HMAC) travels on its upload token and its session, and
|
|
18
|
+
* activation publishes it from the session; the final state Forge sends
|
|
19
|
+
* carries only the storage facts Forge owns.
|
|
20
|
+
*
|
|
21
|
+
* Isomorphic: no Node imports.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const UPLOAD_PUBLICATION_PROTOCOL = 2;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* An upload's id as both services spell it: `tus_` and the 32 lowercase hex
|
|
28
|
+
* digits of 16 random bytes. An encrypt token that names one (`upload_id`)
|
|
29
|
+
* was staged by Core under it, and Forge keys the upload's session, its chunk
|
|
30
|
+
* prefix and its VAK wrapping by it, so Core finds and settles the upload by
|
|
31
|
+
* the session's id.
|
|
32
|
+
*/
|
|
33
|
+
export const UPLOAD_ID_PATTERN = /^tus_[0-9a-f]{32}$/;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* How much of an ISTK names a transfer's charge on the usage ledger: enough to
|
|
37
|
+
* be unique to the token, never the whole bearer credential.
|
|
38
|
+
*/
|
|
39
|
+
export const TRANSFER_REFERENCE_ISTK_CHARS = 24;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The ledger reference of an upload's transfer charge,
|
|
43
|
+
* `forge_ingot_transfer_upload_<first 24 ISTK chars>`: every charge of one
|
|
44
|
+
* upload carries it — Core's activation, Core's charge of an upload that never
|
|
45
|
+
* publishes, Forge's cancel — so whichever lands first is the upload's one
|
|
46
|
+
* ledger row and the others are duplicates.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} istk - The upload's ISTK, as its session carries it
|
|
49
|
+
* @returns {string}
|
|
50
|
+
* @throws {TypeError} When the ISTK is not a string of at least
|
|
51
|
+
* TRANSFER_REFERENCE_ISTK_CHARS characters
|
|
52
|
+
*/
|
|
53
|
+
export function uploadTransferReference(istk) {
|
|
54
|
+
if (typeof istk !== 'string' || istk.length < TRANSFER_REFERENCE_ISTK_CHARS) {
|
|
55
|
+
throw new TypeError(
|
|
56
|
+
`An upload's transfer reference is named from its ISTK, a string of at least ${TRANSFER_REFERENCE_ISTK_CHARS} characters`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return `forge_ingot_transfer_upload_${istk.slice(0, TRANSFER_REFERENCE_ISTK_CHARS)}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The codes Core's activation refuses an upload with, which Forge's refusal
|
|
64
|
+
* map reads (the status is Core's):
|
|
65
|
+
* - UPLOAD_CONTRACT_MISMATCH (409): a finalize or session from another
|
|
66
|
+
* publication contract than Core's, a mixed deployment that converges — the
|
|
67
|
+
* upload is kept and its finalize tried again.
|
|
68
|
+
* - UPLOAD_PROTOCOL_RETIRED (410): a session from a contract Core no longer
|
|
69
|
+
* publishes, which never can be.
|
|
70
|
+
* - UPLOAD_SUPERSEDED (410): a session a later staging of its ingot
|
|
71
|
+
* superseded, which never can be.
|
|
72
|
+
* - UPLOAD_POOL_FULL (402): an upload the owner's storage pool cannot take at
|
|
73
|
+
* publication, which is final.
|
|
74
|
+
*/
|
|
75
|
+
export const UPLOAD_REFUSAL_CODES = Object.freeze({
|
|
76
|
+
UPLOAD_CONTRACT_MISMATCH: 'UPLOAD_CONTRACT_MISMATCH',
|
|
77
|
+
UPLOAD_PROTOCOL_RETIRED: 'UPLOAD_PROTOCOL_RETIRED',
|
|
78
|
+
UPLOAD_SUPERSEDED: 'UPLOAD_SUPERSEDED',
|
|
79
|
+
UPLOAD_POOL_FULL: 'UPLOAD_POOL_FULL'
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The refusals of Core's that a new upload of the same file cannot remedy:
|
|
84
|
+
* the owner's storage pool cannot take it (UPLOAD_POOL_FULL), or a later
|
|
85
|
+
* upload of its ingot replaced it (UPLOAD_SUPERSEDED). Any other code Core
|
|
86
|
+
* ends an upload with ends its session alone, which a new upload starts
|
|
87
|
+
* over. Every client reads a terminal answer's code by it
|
|
88
|
+
* (transfer-retry.js): one of these is a refusal it reports in Core's words,
|
|
89
|
+
* any other a gone session.
|
|
90
|
+
*/
|
|
91
|
+
export const UPLOAD_FINAL_REFUSAL_CODES = Object.freeze([
|
|
92
|
+
UPLOAD_REFUSAL_CODES.UPLOAD_POOL_FULL,
|
|
93
|
+
UPLOAD_REFUSAL_CODES.UPLOAD_SUPERSEDED
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The response header that names the code Core refused an upload with for
|
|
98
|
+
* good: Forge sends it on every terminal answer to a tus request, a HEAD's
|
|
99
|
+
* (which has no body) included, so a client tells an upload the owner's pool
|
|
100
|
+
* cannot take from one a later upload superseded without reading a body.
|
|
101
|
+
*/
|
|
102
|
+
export const UPLOAD_REFUSAL_HEADER = 'X-Upload-Refusal';
|