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