@sparkvault/sdk-js 9.1.2 → 9.2.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/index.cjs +338 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +338 -23
- package/dist/index.mjs.map +1 -1
- package/dist/ingots/index.d.ts +5 -0
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/dist/vaults/upload/api.d.ts +34 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2522,6 +2522,11 @@ declare class IngotsModule {
|
|
|
2522
2522
|
* short-lived) and retries transient transport failures with exponential
|
|
2523
2523
|
* backoff. Validation failures (bad host, non-HTTPS) are never retried.
|
|
2524
2524
|
*
|
|
2525
|
+
* Each attempt is bounded by liveness, never by total duration: a deadline
|
|
2526
|
+
* on the response headers, then an idle deadline rearmed on every body
|
|
2527
|
+
* chunk. A stalled transfer restarts from byte zero: this path requests no
|
|
2528
|
+
* byte range, so a full retry is the only recovery.
|
|
2529
|
+
*
|
|
2525
2530
|
* @example
|
|
2526
2531
|
* const blob = await sv.ingots.download(unsealed, 'ing_abc');
|
|
2527
2532
|
* const url = URL.createObjectURL(blob);
|
package/dist/index.mjs
CHANGED
|
@@ -9946,6 +9946,18 @@ function parseChunkSizeHeader(raw) {
|
|
|
9946
9946
|
* connection has been completely silent for the full window.
|
|
9947
9947
|
*/
|
|
9948
9948
|
const CHUNK_INACTIVITY_TIMEOUT_MS = 60000;
|
|
9949
|
+
/**
|
|
9950
|
+
* Per-chunk retry budget for the PATCH loop.
|
|
9951
|
+
*
|
|
9952
|
+
* Forge sessions are resumable, so a chunk that fails in transit is a blip to
|
|
9953
|
+
* recover from rather than an upload to abandon — a multi-gigabyte transfer is
|
|
9954
|
+
* otherwise one dropped packet away from starting over. The budget counts only
|
|
9955
|
+
* failures that produced no server-side progress: a retry cycle that finds
|
|
9956
|
+
* bytes actually landed earns the budget back, so a slow-but-alive transfer is
|
|
9957
|
+
* never capped while a genuinely dead one still fails in bounded time.
|
|
9958
|
+
*/
|
|
9959
|
+
const CHUNK_MAX_ATTEMPTS = 3;
|
|
9960
|
+
const CHUNK_RETRY_BASE_MS = 500;
|
|
9949
9961
|
/**
|
|
9950
9962
|
* Server-side finalization confirmation poll.
|
|
9951
9963
|
*
|
|
@@ -9957,6 +9969,67 @@ const CHUNK_INACTIVITY_TIMEOUT_MS = 60000;
|
|
|
9957
9969
|
*/
|
|
9958
9970
|
const FINALIZE_POLL_MAX = 10;
|
|
9959
9971
|
const FINALIZE_POLL_MS = 1000;
|
|
9972
|
+
/** Promise-wrapped delay, for retry backoff and finalization polling. */
|
|
9973
|
+
/**
|
|
9974
|
+
* A delay that a cancel cuts short: resolves after `ms`, rejects with the
|
|
9975
|
+
* canonical cancelled error the moment the signal aborts.
|
|
9976
|
+
*/
|
|
9977
|
+
function abortableDelay(ms, signal) {
|
|
9978
|
+
return new Promise((resolve, reject) => {
|
|
9979
|
+
const cancelled = () => new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
9980
|
+
if (signal.aborted) {
|
|
9981
|
+
reject(cancelled());
|
|
9982
|
+
return;
|
|
9983
|
+
}
|
|
9984
|
+
const onAbort = () => {
|
|
9985
|
+
clearTimeout(timer);
|
|
9986
|
+
reject(cancelled());
|
|
9987
|
+
};
|
|
9988
|
+
const timer = setTimeout(() => {
|
|
9989
|
+
signal.removeEventListener('abort', onAbort);
|
|
9990
|
+
resolve();
|
|
9991
|
+
}, ms);
|
|
9992
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
9993
|
+
});
|
|
9994
|
+
}
|
|
9995
|
+
function delay(ms) {
|
|
9996
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
9997
|
+
}
|
|
9998
|
+
/**
|
|
9999
|
+
* Whether a failed chunk PATCH is worth another attempt.
|
|
10000
|
+
*
|
|
10001
|
+
* A transport-level failure (status 0 — a dropped connection, or the
|
|
10002
|
+
* inactivity watchdog aborting a silent socket) says nothing about whether the
|
|
10003
|
+
* bytes reached Forge, 409 is Forge's documented "another writer holds the
|
|
10004
|
+
* session lease; re-check the offset and retry", and a 5xx is a server-side
|
|
10005
|
+
* blip. Every other outcome is the server's considered answer about this exact
|
|
10006
|
+
* chunk — a rejected offset, a bad ISTK, a vanished session, or a 2xx carrying
|
|
10007
|
+
* a protocol-violating offset — and re-sending would only earn it again. A
|
|
10008
|
+
* cancelled session is the user's decision, never a failure to retry.
|
|
10009
|
+
*/
|
|
10010
|
+
function isRetryableChunkFailure(error) {
|
|
10011
|
+
if (!(error instanceof UploadApiError))
|
|
10012
|
+
return false;
|
|
10013
|
+
// Verdicts, not blips: a cancel is the user's decision, and an invalid or
|
|
10014
|
+
// misaligned offset is a protocol violation the server would only repeat.
|
|
10015
|
+
// The code exclusion is load-bearing for the statusCode-0 sites (the resume
|
|
10016
|
+
// and misalignment guards); patchChunk's own invalid_offset rides a 2xx
|
|
10017
|
+
// status the heuristic below would reject anyway.
|
|
10018
|
+
if (error.code === 'cancelled' || error.code === 'invalid_offset')
|
|
10019
|
+
return false;
|
|
10020
|
+
const status = error.statusCode ?? 0;
|
|
10021
|
+
return status === 0 || status === 409 || status >= 500;
|
|
10022
|
+
}
|
|
10023
|
+
const RESUME_CHECK = {
|
|
10024
|
+
name: 'Upload resume check',
|
|
10025
|
+
code: 'resume_check_failed',
|
|
10026
|
+
networkMessage: 'Unable to resume upload. Please check your connection and try again.',
|
|
10027
|
+
};
|
|
10028
|
+
const FINALIZATION_CHECK = {
|
|
10029
|
+
name: 'Upload finalization check',
|
|
10030
|
+
code: 'finalization_check_failed',
|
|
10031
|
+
networkMessage: 'Unable to confirm upload. Please check your connection and try again.',
|
|
10032
|
+
};
|
|
9960
10033
|
class UploadApi extends SparkVaultApi {
|
|
9961
10034
|
constructor(config) {
|
|
9962
10035
|
super(config.timeout);
|
|
@@ -10039,6 +10112,10 @@ class UploadApi extends SparkVaultApi {
|
|
|
10039
10112
|
* every server-reported offset, and honors the session abort signal plus
|
|
10040
10113
|
* per-request timeouts.
|
|
10041
10114
|
*
|
|
10115
|
+
* A chunk that fails in transit is retried against the session's *true*
|
|
10116
|
+
* offset rather than failing the whole transfer — see {@link CHUNK_MAX_ATTEMPTS}
|
|
10117
|
+
* and {@link readResumeOffset}.
|
|
10118
|
+
*
|
|
10042
10119
|
* Returns the TUS session URL so the caller can confirm server-side
|
|
10043
10120
|
* finalization via {@link verifyUploadFinalized} before reporting success.
|
|
10044
10121
|
*/
|
|
@@ -10056,15 +10133,167 @@ class UploadApi extends SparkVaultApi {
|
|
|
10056
10133
|
? session.location
|
|
10057
10134
|
: `${url.origin}${session.location}`;
|
|
10058
10135
|
let offset = 0;
|
|
10136
|
+
let failedAttempts = 0;
|
|
10137
|
+
// Retries make raw progress non-monotonic (a chunk that streamed to 90%
|
|
10138
|
+
// then failed re-reports from its start on resend); callers get only the
|
|
10139
|
+
// high-water mark so a progress bar never runs backwards.
|
|
10140
|
+
let progressHighWater = 0;
|
|
10141
|
+
const monotonicProgress = onProgress
|
|
10142
|
+
? (sent, total) => {
|
|
10143
|
+
if (sent > progressHighWater) {
|
|
10144
|
+
progressHighWater = sent;
|
|
10145
|
+
onProgress(sent, total);
|
|
10146
|
+
}
|
|
10147
|
+
}
|
|
10148
|
+
: undefined;
|
|
10149
|
+
// A successful final-chunk PATCH is the server's finalization receipt:
|
|
10150
|
+
// Forge finalizes (publishes the ingot, moves the counters) BEFORE it
|
|
10151
|
+
// acknowledges that chunk with a 204. An offset learned from a resume
|
|
10152
|
+
// HEAD carries no such receipt — the server persists the final offset
|
|
10153
|
+
// before it finalizes — so a completion reached through the resume path
|
|
10154
|
+
// must be confirmed explicitly after the loop.
|
|
10155
|
+
let finalizeAcknowledged = false;
|
|
10059
10156
|
while (offset < file.size) {
|
|
10060
10157
|
if (this.isAborted()) {
|
|
10061
10158
|
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10062
10159
|
}
|
|
10063
10160
|
const chunkEnd = Math.min(offset + session.chunkSize, file.size);
|
|
10064
|
-
|
|
10161
|
+
try {
|
|
10162
|
+
offset = await this.patchChunk(uploadUrl, file.slice(offset, chunkEnd), offset, file.size, monotonicProgress);
|
|
10163
|
+
failedAttempts = 0;
|
|
10164
|
+
if (offset >= file.size) {
|
|
10165
|
+
finalizeAcknowledged = true;
|
|
10166
|
+
}
|
|
10167
|
+
continue;
|
|
10168
|
+
}
|
|
10169
|
+
catch (error) {
|
|
10170
|
+
failedAttempts += 1;
|
|
10171
|
+
if (failedAttempts >= CHUNK_MAX_ATTEMPTS || !isRetryableChunkFailure(error)) {
|
|
10172
|
+
throw error;
|
|
10173
|
+
}
|
|
10174
|
+
}
|
|
10175
|
+
// Resync against server truth before ANY further PATCH: the failed
|
|
10176
|
+
// chunk may have landed (the acknowledgment was lost), and a PATCH at
|
|
10177
|
+
// the client's stale offset is an offset mismatch the server rejects
|
|
10178
|
+
// as a terminal 400. The blip that killed the PATCH is also the
|
|
10179
|
+
// likeliest thing to kill this HEAD moments later, so the HEAD is
|
|
10180
|
+
// retried out of the same per-position budget — the backoff races the
|
|
10181
|
+
// session's abort signal so cancellation stays prompt.
|
|
10182
|
+
let serverOffset;
|
|
10183
|
+
for (;;) {
|
|
10184
|
+
await abortableDelay(CHUNK_RETRY_BASE_MS * 2 ** (failedAttempts - 1), this.getAbortSignal());
|
|
10185
|
+
try {
|
|
10186
|
+
serverOffset = await this.readResumeOffset(uploadUrl, file.size);
|
|
10187
|
+
break;
|
|
10188
|
+
}
|
|
10189
|
+
catch (headError) {
|
|
10190
|
+
if (!isRetryableChunkFailure(headError)) {
|
|
10191
|
+
throw headError;
|
|
10192
|
+
}
|
|
10193
|
+
failedAttempts += 1;
|
|
10194
|
+
if (failedAttempts >= CHUNK_MAX_ATTEMPTS) {
|
|
10195
|
+
throw headError;
|
|
10196
|
+
}
|
|
10197
|
+
}
|
|
10198
|
+
}
|
|
10199
|
+
// Forge advances a session's offset only in whole committed chunks, so a
|
|
10200
|
+
// legitimate resume point is always chunk-aligned (or the file end). A
|
|
10201
|
+
// misaligned offset is a corrupt session, and resuming from it would
|
|
10202
|
+
// send a short non-final chunk Forge rejects anyway — fail it as the
|
|
10203
|
+
// protocol violation it is instead.
|
|
10204
|
+
if (serverOffset % session.chunkSize !== 0 && serverOffset !== file.size) {
|
|
10205
|
+
throw new UploadApiError(`Server returned misaligned upload offset (${serverOffset})`, 'invalid_offset', 0);
|
|
10206
|
+
}
|
|
10207
|
+
if (serverOffset > offset) {
|
|
10208
|
+
// Bytes reached the server despite the failure, so the transfer is
|
|
10209
|
+
// alive: the budget is earned back rather than spent down across the
|
|
10210
|
+
// many chunks of a large file.
|
|
10211
|
+
failedAttempts = 0;
|
|
10212
|
+
}
|
|
10213
|
+
offset = serverOffset;
|
|
10214
|
+
}
|
|
10215
|
+
if (!finalizeAcknowledged) {
|
|
10216
|
+
await this.confirmServerFinalized(uploadUrl, file.size);
|
|
10065
10217
|
}
|
|
10066
10218
|
return uploadUrl;
|
|
10067
10219
|
}
|
|
10220
|
+
/**
|
|
10221
|
+
* Confirm the server finalized a fully received upload.
|
|
10222
|
+
*
|
|
10223
|
+
* The server persists the session's final offset BEFORE it finalizes, so an
|
|
10224
|
+
* offset read from a resume HEAD can equal the file size while finalization
|
|
10225
|
+
* failed or is still running — treating that offset as success would report
|
|
10226
|
+
* a stored file whose ingot was never published. The zero-length PATCH at
|
|
10227
|
+
* the final offset is the server's finalization re-drive: it answers 2xx
|
|
10228
|
+
* only once finalization has actually completed, the same receipt a
|
|
10229
|
+
* successful final chunk carries.
|
|
10230
|
+
*/
|
|
10231
|
+
async confirmServerFinalized(uploadUrl, totalSize) {
|
|
10232
|
+
let attempts = 0;
|
|
10233
|
+
for (;;) {
|
|
10234
|
+
if (this.isAborted()) {
|
|
10235
|
+
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10236
|
+
}
|
|
10237
|
+
const closeSignal = this.getAbortSignal();
|
|
10238
|
+
const { signal, dispose } = combineTimeoutAndCloseSignals(this.timeoutMs, closeSignal);
|
|
10239
|
+
try {
|
|
10240
|
+
let response;
|
|
10241
|
+
try {
|
|
10242
|
+
response = await fetch(uploadUrl, {
|
|
10243
|
+
method: 'PATCH',
|
|
10244
|
+
headers: {
|
|
10245
|
+
'Tus-Resumable': TUS_VERSION,
|
|
10246
|
+
'Upload-Offset': String(totalSize),
|
|
10247
|
+
'Content-Type': 'application/offset+octet-stream',
|
|
10248
|
+
},
|
|
10249
|
+
signal,
|
|
10250
|
+
});
|
|
10251
|
+
}
|
|
10252
|
+
catch {
|
|
10253
|
+
if (closeSignal.aborted) {
|
|
10254
|
+
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10255
|
+
}
|
|
10256
|
+
throw new UploadApiError('Upload finalization check failed: network error', 'finalization_check_failed', 0);
|
|
10257
|
+
}
|
|
10258
|
+
if (response.ok) {
|
|
10259
|
+
return;
|
|
10260
|
+
}
|
|
10261
|
+
throw new UploadApiError(`Upload finalization check failed: ${response.status}`, 'finalization_check_failed', response.status);
|
|
10262
|
+
}
|
|
10263
|
+
catch (error) {
|
|
10264
|
+
if (!isRetryableChunkFailure(error)) {
|
|
10265
|
+
throw error;
|
|
10266
|
+
}
|
|
10267
|
+
attempts += 1;
|
|
10268
|
+
if (attempts >= CHUNK_MAX_ATTEMPTS) {
|
|
10269
|
+
throw error;
|
|
10270
|
+
}
|
|
10271
|
+
await abortableDelay(CHUNK_RETRY_BASE_MS * 2 ** (attempts - 1), this.getAbortSignal());
|
|
10272
|
+
}
|
|
10273
|
+
finally {
|
|
10274
|
+
dispose();
|
|
10275
|
+
}
|
|
10276
|
+
}
|
|
10277
|
+
}
|
|
10278
|
+
/**
|
|
10279
|
+
* Read the offset a failed chunk should resume from.
|
|
10280
|
+
*
|
|
10281
|
+
* Forge commits a chunk before it answers, so a PATCH that died in transit
|
|
10282
|
+
* may have landed in full or not at all (never in part — the chunk object
|
|
10283
|
+
* is written atomically before the offset advances); only the session's own
|
|
10284
|
+
* persisted offset knows which, and the client's arithmetic is worthless
|
|
10285
|
+
* here. An offset past the declared total is a protocol violation rather
|
|
10286
|
+
* than a resume point: honoring it would end the loop early and report a
|
|
10287
|
+
* partial upload as complete, the same trap {@link patchChunk} guards
|
|
10288
|
+
* against on a PATCH's own offset.
|
|
10289
|
+
*/
|
|
10290
|
+
async readResumeOffset(uploadUrl, totalSize) {
|
|
10291
|
+
const offset = await this.headUploadOffset(uploadUrl, RESUME_CHECK);
|
|
10292
|
+
if (offset > totalSize) {
|
|
10293
|
+
throw new UploadApiError(`Server returned invalid upload offset (${offset})`, 'invalid_offset', 0);
|
|
10294
|
+
}
|
|
10295
|
+
return offset;
|
|
10296
|
+
}
|
|
10068
10297
|
/**
|
|
10069
10298
|
* Create the TUS upload session. Forge reads only `Tus-Resumable`,
|
|
10070
10299
|
* `Upload-Length`, and `X-ISTK` on create, and returns the session URL in
|
|
@@ -10217,27 +10446,31 @@ class UploadApi extends SparkVaultApi {
|
|
|
10217
10446
|
if (this.isAborted()) {
|
|
10218
10447
|
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10219
10448
|
}
|
|
10220
|
-
const offset = await this.headUploadOffset(uploadUrl);
|
|
10449
|
+
const offset = await this.headUploadOffset(uploadUrl, FINALIZATION_CHECK);
|
|
10221
10450
|
lastOffset = offset;
|
|
10222
10451
|
if (offset >= expectedOffset) {
|
|
10223
10452
|
return;
|
|
10224
10453
|
}
|
|
10225
10454
|
if (attempt < FINALIZE_POLL_MAX) {
|
|
10226
|
-
await
|
|
10455
|
+
await delay(FINALIZE_POLL_MS);
|
|
10227
10456
|
}
|
|
10228
10457
|
}
|
|
10229
10458
|
throw new UploadApiError('Upload could not be confirmed: the server never finalized the file. ' +
|
|
10230
10459
|
`Confirmed ${lastOffset ?? 0} of ${expectedOffset} bytes.`, 'finalization_unconfirmed', 0, { confirmedBytes: lastOffset ?? 0, expectedBytes: expectedOffset });
|
|
10231
10460
|
}
|
|
10232
10461
|
/**
|
|
10233
|
-
* Read the server's persisted upload offset via a TUS HEAD request
|
|
10462
|
+
* Read the server's persisted upload offset via a TUS HEAD request — the one
|
|
10463
|
+
* place the client asks Forge where a session really stands, for both
|
|
10464
|
+
* mid-upload resume and end-of-upload confirmation.
|
|
10234
10465
|
*
|
|
10235
10466
|
* Uses raw `fetch` rather than the JSON envelope helper: TUS responses
|
|
10236
10467
|
* carry an empty body and surface state in the `Upload-Offset` header.
|
|
10237
10468
|
* Honors the session abort signal and request timeout like every other
|
|
10238
10469
|
* client call.
|
|
10470
|
+
*
|
|
10471
|
+
* @param check - Which caller this read serves; shapes the failure copy.
|
|
10239
10472
|
*/
|
|
10240
|
-
async headUploadOffset(uploadUrl) {
|
|
10473
|
+
async headUploadOffset(uploadUrl, check) {
|
|
10241
10474
|
const closeSignal = this.getAbortSignal();
|
|
10242
10475
|
const { signal: combinedSignal, dispose } = combineTimeoutAndCloseSignals(this.timeoutMs, closeSignal);
|
|
10243
10476
|
try {
|
|
@@ -10247,12 +10480,12 @@ class UploadApi extends SparkVaultApi {
|
|
|
10247
10480
|
signal: combinedSignal,
|
|
10248
10481
|
});
|
|
10249
10482
|
if (!response.ok) {
|
|
10250
|
-
throw new UploadApiError(
|
|
10483
|
+
throw new UploadApiError(`${check.name} failed: ${response.status}`, check.code, response.status);
|
|
10251
10484
|
}
|
|
10252
10485
|
const header = response.headers.get('Upload-Offset');
|
|
10253
10486
|
const offset = header !== null ? parseInt(header, 10) : NaN;
|
|
10254
10487
|
if (!Number.isFinite(offset) || offset < 0) {
|
|
10255
|
-
throw new UploadApiError(
|
|
10488
|
+
throw new UploadApiError(`${check.name} returned an invalid offset`, check.code, response.status);
|
|
10256
10489
|
}
|
|
10257
10490
|
return offset;
|
|
10258
10491
|
}
|
|
@@ -10263,10 +10496,10 @@ class UploadApi extends SparkVaultApi {
|
|
|
10263
10496
|
if (this.isAborted()) {
|
|
10264
10497
|
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10265
10498
|
}
|
|
10266
|
-
throw new UploadApiError(
|
|
10499
|
+
throw new UploadApiError(`${check.name} timed out after ${this.timeoutMs}ms`, 'timeout', 0);
|
|
10267
10500
|
}
|
|
10268
10501
|
if (error instanceof TypeError) {
|
|
10269
|
-
throw new UploadApiError(
|
|
10502
|
+
throw new UploadApiError(check.networkMessage, 'network_error', 0);
|
|
10270
10503
|
}
|
|
10271
10504
|
throw new UploadApiError(error instanceof Error ? error.message : 'An unexpected error occurred', 'unknown_error', 0);
|
|
10272
10505
|
}
|
|
@@ -13389,7 +13622,31 @@ function mapUploadSettings(wire) {
|
|
|
13389
13622
|
*/
|
|
13390
13623
|
const DOWNLOAD_MAX_ATTEMPTS = 3;
|
|
13391
13624
|
const DOWNLOAD_RETRY_BASE_MS = 500;
|
|
13392
|
-
|
|
13625
|
+
/**
|
|
13626
|
+
* Time-to-first-byte bound: how long the download host may take to return
|
|
13627
|
+
* response headers. This bounds only the wait for a transfer to start — a
|
|
13628
|
+
* signed link either begins serving promptly or is not going to.
|
|
13629
|
+
*/
|
|
13630
|
+
const DOWNLOAD_TTFB_TIMEOUT_MS = 30000;
|
|
13631
|
+
/**
|
|
13632
|
+
* Liveness bound on the response body: the longest tolerated gap between two
|
|
13633
|
+
* chunks, rearmed on every chunk. Progress, not wall clock, bounds the
|
|
13634
|
+
* transfer — a large file over a slow link still completes, while a stalled
|
|
13635
|
+
* connection fails promptly enough to retry.
|
|
13636
|
+
*/
|
|
13637
|
+
const DOWNLOAD_IDLE_TIMEOUT_MS = 60000;
|
|
13638
|
+
/**
|
|
13639
|
+
* Whole-transfer bound used only where the environment exposes no readable
|
|
13640
|
+
* response body: `blob()` consumes the stream opaquely, so there are no chunk
|
|
13641
|
+
* arrivals to time and wall clock is the only bound available.
|
|
13642
|
+
*/
|
|
13643
|
+
const DOWNLOAD_BODY_TIMEOUT_MS = 300000;
|
|
13644
|
+
/**
|
|
13645
|
+
* Bytes held on the JS heap before they are folded into a Blob part. Caps heap
|
|
13646
|
+
* growth at one window regardless of file size; the platform's blob store owns
|
|
13647
|
+
* everything already transferred.
|
|
13648
|
+
*/
|
|
13649
|
+
const DOWNLOAD_BLOB_FLUSH_BYTES = 8 * 1024 * 1024;
|
|
13393
13650
|
class IngotsModule {
|
|
13394
13651
|
constructor(config, http) {
|
|
13395
13652
|
this.config = config;
|
|
@@ -13622,6 +13879,11 @@ class IngotsModule {
|
|
|
13622
13879
|
* short-lived) and retries transient transport failures with exponential
|
|
13623
13880
|
* backoff. Validation failures (bad host, non-HTTPS) are never retried.
|
|
13624
13881
|
*
|
|
13882
|
+
* Each attempt is bounded by liveness, never by total duration: a deadline
|
|
13883
|
+
* on the response headers, then an idle deadline rearmed on every body
|
|
13884
|
+
* chunk. A stalled transfer restarts from byte zero: this path requests no
|
|
13885
|
+
* byte range, so a full retry is the only recovery.
|
|
13886
|
+
*
|
|
13625
13887
|
* @example
|
|
13626
13888
|
* const blob = await sv.ingots.download(unsealed, 'ing_abc');
|
|
13627
13889
|
* const url = URL.createObjectURL(blob);
|
|
@@ -13631,7 +13893,7 @@ class IngotsModule {
|
|
|
13631
13893
|
for (let attempt = 1; attempt <= DOWNLOAD_MAX_ATTEMPTS; attempt++) {
|
|
13632
13894
|
try {
|
|
13633
13895
|
const link = await this.createDownloadLink(vault, ingotId);
|
|
13634
|
-
return await
|
|
13896
|
+
return await fetchBlobWithLivenessBounds(link.downloadUrl);
|
|
13635
13897
|
}
|
|
13636
13898
|
catch (err) {
|
|
13637
13899
|
lastError = err instanceof Error ? err : new Error(String(err));
|
|
@@ -13880,21 +14142,74 @@ function isRetryableDownloadError(err) {
|
|
|
13880
14142
|
}
|
|
13881
14143
|
return true;
|
|
13882
14144
|
}
|
|
13883
|
-
|
|
14145
|
+
/**
|
|
14146
|
+
* Await one transfer step under a deadline. Expiry aborts the in-flight fetch,
|
|
14147
|
+
* releasing the connection and erroring the response body, and surfaces a
|
|
14148
|
+
* retryable {@link TimeoutError}. The aborted step's own rejection lands on the
|
|
14149
|
+
* handler installed here, so it never escapes as an unhandled rejection.
|
|
14150
|
+
*/
|
|
14151
|
+
function withDeadline(step, timeout, controller, message) {
|
|
14152
|
+
return new Promise((resolve, reject) => {
|
|
14153
|
+
const timeoutId = setTimeout(() => {
|
|
14154
|
+
controller.abort();
|
|
14155
|
+
reject(new TimeoutError(message));
|
|
14156
|
+
}, timeout);
|
|
14157
|
+
step.then((value) => {
|
|
14158
|
+
clearTimeout(timeoutId);
|
|
14159
|
+
resolve(value);
|
|
14160
|
+
}, (error) => {
|
|
14161
|
+
clearTimeout(timeoutId);
|
|
14162
|
+
reject(error);
|
|
14163
|
+
});
|
|
14164
|
+
});
|
|
14165
|
+
}
|
|
14166
|
+
/**
|
|
14167
|
+
* Fetch a signed download URL as a Blob under liveness bounds: one deadline
|
|
14168
|
+
* for the response headers, then an idle deadline rearmed on every body chunk.
|
|
14169
|
+
* Nothing caps the transfer as a whole, so file size never decides whether a
|
|
14170
|
+
* download can finish.
|
|
14171
|
+
*/
|
|
14172
|
+
async function fetchBlobWithLivenessBounds(url) {
|
|
13884
14173
|
const controller = new AbortController();
|
|
13885
|
-
const
|
|
13886
|
-
|
|
13887
|
-
|
|
13888
|
-
|
|
13889
|
-
|
|
13890
|
-
|
|
13891
|
-
|
|
14174
|
+
const response = await withDeadline(fetch(url, { signal: controller.signal }), DOWNLOAD_TTFB_TIMEOUT_MS, controller, 'Download timed out waiting for response headers');
|
|
14175
|
+
if (!response.ok) {
|
|
14176
|
+
// The TTFB timer cleared when headers arrived, so nothing else would ever
|
|
14177
|
+
// reap this connection: abort it or a retrying caller leaks one socket
|
|
14178
|
+
// per failed attempt.
|
|
14179
|
+
controller.abort();
|
|
14180
|
+
const error = new Error(`Download failed with status ${response.status}`);
|
|
14181
|
+
error.statusCode = response.status;
|
|
14182
|
+
throw error;
|
|
14183
|
+
}
|
|
14184
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
14185
|
+
// Where the environment exposes no readable body, the transfer keeps
|
|
14186
|
+
// whole-transfer wall-clock semantics: blob() surfaces no chunk arrivals, so
|
|
14187
|
+
// there is no liveness to observe.
|
|
14188
|
+
if (!response.body) {
|
|
14189
|
+
return withDeadline(response.blob(), DOWNLOAD_BODY_TIMEOUT_MS, controller, 'Download timed out');
|
|
14190
|
+
}
|
|
14191
|
+
const reader = response.body.getReader();
|
|
14192
|
+
const parts = [];
|
|
14193
|
+
let pending = [];
|
|
14194
|
+
let pendingBytes = 0;
|
|
14195
|
+
for (;;) {
|
|
14196
|
+
const { done, value } = await withDeadline(reader.read(), DOWNLOAD_IDLE_TIMEOUT_MS, controller, 'Download stalled: no data received');
|
|
14197
|
+
if (done)
|
|
14198
|
+
break;
|
|
14199
|
+
pending.push(value);
|
|
14200
|
+
pendingBytes += value.byteLength;
|
|
14201
|
+
if (pendingBytes >= DOWNLOAD_BLOB_FLUSH_BYTES) {
|
|
14202
|
+
// Fetch body chunks are ArrayBuffer-backed; the lib types over-generalize
|
|
14203
|
+
// to ArrayBufferLike, which the Blob constructor's part type rejects.
|
|
14204
|
+
parts.push(new Blob(pending));
|
|
14205
|
+
pending = [];
|
|
14206
|
+
pendingBytes = 0;
|
|
13892
14207
|
}
|
|
13893
|
-
return await response.blob();
|
|
13894
14208
|
}
|
|
13895
|
-
|
|
13896
|
-
|
|
14209
|
+
if (pending.length > 0) {
|
|
14210
|
+
parts.push(new Blob(pending));
|
|
13897
14211
|
}
|
|
14212
|
+
return new Blob(parts, { type: contentType });
|
|
13898
14213
|
}
|
|
13899
14214
|
|
|
13900
14215
|
/**
|
|
@@ -14761,7 +15076,7 @@ class SparkVault {
|
|
|
14761
15076
|
}
|
|
14762
15077
|
/** Get the SDK version */
|
|
14763
15078
|
static get version() {
|
|
14764
|
-
return '9.1
|
|
15079
|
+
return '9.2.1';
|
|
14765
15080
|
}
|
|
14766
15081
|
}
|
|
14767
15082
|
if (typeof window !== 'undefined') {
|