@sparkvault/sdk-js 9.1.1 → 9.2.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/dist/index.cjs +263 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +43 -10
- package/dist/index.mjs +263 -23
- package/dist/index.mjs.map +1 -1
- package/dist/ingots/index.d.ts +5 -0
- package/dist/notify/types.d.ts +17 -4
- package/dist/products/notify/types.d.ts +21 -6
- package/dist/products/notify/types.test-d.d.ts +34 -0
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/dist/vaults/upload/api.d.ts +22 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -658,13 +658,26 @@ interface NotificationRow {
|
|
|
658
658
|
read_at: number | null;
|
|
659
659
|
archived_at: number | null;
|
|
660
660
|
/**
|
|
661
|
-
* The grant to open. Present only
|
|
662
|
-
*
|
|
663
|
-
*
|
|
661
|
+
* The grant to open. Present only while a live reveal stands behind it — a
|
|
662
|
+
* SEALED (secure) send that has not been recalled. A cleartext send delivers its
|
|
663
|
+
* content inline in the channel and mints no grant; a recalled row's grant is
|
|
664
|
+
* revoked and the pointer is withheld rather than echoed. Guard before building
|
|
665
|
+
* a URL from it.
|
|
664
666
|
*/
|
|
665
667
|
sparklink_code?: string;
|
|
666
|
-
/**
|
|
668
|
+
/**
|
|
669
|
+
* True only when there is a secure part left to reveal — exactly when
|
|
670
|
+
* `sparklink_code` is present. `false` does NOT mean "cleartext row": a recalled
|
|
671
|
+
* row is unlocked too, with nothing behind it. Branch on `recalled` before
|
|
672
|
+
* treating an unlocked row as plain text.
|
|
673
|
+
*/
|
|
667
674
|
locked: boolean;
|
|
675
|
+
/**
|
|
676
|
+
* The sender took this notification back. The grant is revoked and
|
|
677
|
+
* `sparklink_code` is withheld, so render "no longer available" rather than a
|
|
678
|
+
* tap-to-reveal that opens nothing.
|
|
679
|
+
*/
|
|
680
|
+
recalled: boolean;
|
|
668
681
|
thread_id?: string;
|
|
669
682
|
}
|
|
670
683
|
interface ListNotificationsOptions {
|
|
@@ -999,8 +1012,14 @@ interface MarkNotifyInboxAllResponse {
|
|
|
999
1012
|
* - `suppressed` — the PERSON chose silence (site off, category muted, every
|
|
1000
1013
|
* offered channel opted out). Not a fault, and not something to route around.
|
|
1001
1014
|
* - `rate_limited` — skipped by the per-recipient abuse ceiling for that minute.
|
|
1015
|
+
* - `recalled` — the sender took it back before a channel reached this recipient.
|
|
1016
|
+
* The reveal is revoked and no further delivery will be attempted. It ranks
|
|
1017
|
+
* BELOW `delivered`: a channel that already reached someone reached them, so a
|
|
1018
|
+
* recall afterwards leaves them `delivered`. It covers recipients the recall
|
|
1019
|
+
* itself marked AND, on a whole-audience recall, everyone the fan-out was
|
|
1020
|
+
* stopped before ever writing a row for.
|
|
1002
1021
|
*/
|
|
1003
|
-
type NotifyRecipientState = 'pending' | 'delivered' | 'undeliverable' | 'failed' | 'suppressed' | 'rate_limited';
|
|
1022
|
+
type NotifyRecipientState = 'pending' | 'delivered' | 'undeliverable' | 'failed' | 'suppressed' | 'rate_limited' | 'recalled';
|
|
1004
1023
|
interface NotifySendStatusRecipient {
|
|
1005
1024
|
recipient_id: string;
|
|
1006
1025
|
/** The derived per-recipient state — the field to branch on. */
|
|
@@ -1062,8 +1081,9 @@ interface NotifySendStatusCounts {
|
|
|
1062
1081
|
read: number;
|
|
1063
1082
|
archived: number;
|
|
1064
1083
|
/**
|
|
1065
|
-
* The
|
|
1066
|
-
* `
|
|
1084
|
+
* The seven state counters below PARTITION the audience — one per
|
|
1085
|
+
* `NotifyRecipientState`:
|
|
1086
|
+
* `pending + delivered + undeliverable + failed + suppressed + rate_limited + recalled === total`.
|
|
1067
1087
|
* (`enqueued` / `seen` / `read` / `archived` are independent progress markers
|
|
1068
1088
|
* and do not partition anything.)
|
|
1069
1089
|
*/
|
|
@@ -1078,6 +1098,11 @@ interface NotifySendStatusCounts {
|
|
|
1078
1098
|
suppressed: number;
|
|
1079
1099
|
/** Recipients skipped by the per-recipient ceiling. */
|
|
1080
1100
|
rate_limited: number;
|
|
1101
|
+
/**
|
|
1102
|
+
* Recipients the sender took back before a channel reached them. A recalled
|
|
1103
|
+
* send therefore settles instead of sitting on `pending` until it expires.
|
|
1104
|
+
*/
|
|
1105
|
+
recalled: number;
|
|
1081
1106
|
}
|
|
1082
1107
|
/**
|
|
1083
1108
|
* The sender's per-recipient delivery rollup. Metadata only by construction: it
|
|
@@ -1099,9 +1124,12 @@ interface NotifySendStatus {
|
|
|
1099
1124
|
* - `failed` — every recipient resolved and none delivered.
|
|
1100
1125
|
*
|
|
1101
1126
|
* The list feeds it the send row's best-effort counters; this view recomputes
|
|
1102
|
-
* them from the authoritative per-recipient rows, so the list
|
|
1103
|
-
*
|
|
1104
|
-
* that
|
|
1127
|
+
* them from the authoritative per-recipient rows, so the list normally lags
|
|
1128
|
+
* rather than disagrees. A whole-audience RECALL is the exception: it stops the
|
|
1129
|
+
* fan-out where it stands, so the audience past that point never produces a row
|
|
1130
|
+
* or a counter — this view settles them `recalled`, while the list is left on
|
|
1131
|
+
* `sending` with counters nothing further will advance. This view is the one to
|
|
1132
|
+
* trust; for WHY a given recipient did not get it, read that recipient's `state`.
|
|
1105
1133
|
*/
|
|
1106
1134
|
status: 'unknown' | 'pending' | 'sending' | 'delivered' | 'partial' | 'failed';
|
|
1107
1135
|
channels: NotifyChannel[];
|
|
@@ -2494,6 +2522,11 @@ declare class IngotsModule {
|
|
|
2494
2522
|
* short-lived) and retries transient transport failures with exponential
|
|
2495
2523
|
* backoff. Validation failures (bad host, non-HTTPS) are never retried.
|
|
2496
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
|
+
*
|
|
2497
2530
|
* @example
|
|
2498
2531
|
* const blob = await sv.ingots.download(unsealed, 'ing_abc');
|
|
2499
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,66 @@ 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
|
+
// Both carry statusCode 0, so they must be excluded by code before the
|
|
10016
|
+
// status heuristic below can see them.
|
|
10017
|
+
if (error.code === 'cancelled' || error.code === 'invalid_offset')
|
|
10018
|
+
return false;
|
|
10019
|
+
const status = error.statusCode ?? 0;
|
|
10020
|
+
return status === 0 || status === 409 || status >= 500;
|
|
10021
|
+
}
|
|
10022
|
+
const RESUME_CHECK = {
|
|
10023
|
+
name: 'Upload resume check',
|
|
10024
|
+
code: 'resume_check_failed',
|
|
10025
|
+
networkMessage: 'Unable to resume upload. Please check your connection and try again.',
|
|
10026
|
+
};
|
|
10027
|
+
const FINALIZATION_CHECK = {
|
|
10028
|
+
name: 'Upload finalization check',
|
|
10029
|
+
code: 'finalization_check_failed',
|
|
10030
|
+
networkMessage: 'Unable to confirm upload. Please check your connection and try again.',
|
|
10031
|
+
};
|
|
9960
10032
|
class UploadApi extends SparkVaultApi {
|
|
9961
10033
|
constructor(config) {
|
|
9962
10034
|
super(config.timeout);
|
|
@@ -10039,6 +10111,10 @@ class UploadApi extends SparkVaultApi {
|
|
|
10039
10111
|
* every server-reported offset, and honors the session abort signal plus
|
|
10040
10112
|
* per-request timeouts.
|
|
10041
10113
|
*
|
|
10114
|
+
* A chunk that fails in transit is retried against the session's *true*
|
|
10115
|
+
* offset rather than failing the whole transfer — see {@link CHUNK_MAX_ATTEMPTS}
|
|
10116
|
+
* and {@link readResumeOffset}.
|
|
10117
|
+
*
|
|
10042
10118
|
* Returns the TUS session URL so the caller can confirm server-side
|
|
10043
10119
|
* finalization via {@link verifyUploadFinalized} before reporting success.
|
|
10044
10120
|
*/
|
|
@@ -10056,15 +10132,93 @@ class UploadApi extends SparkVaultApi {
|
|
|
10056
10132
|
? session.location
|
|
10057
10133
|
: `${url.origin}${session.location}`;
|
|
10058
10134
|
let offset = 0;
|
|
10135
|
+
let failedAttempts = 0;
|
|
10136
|
+
// Retries make raw progress non-monotonic (a chunk that streamed to 90%
|
|
10137
|
+
// then failed re-reports from its start on resend); callers get only the
|
|
10138
|
+
// high-water mark so a progress bar never runs backwards.
|
|
10139
|
+
let progressHighWater = 0;
|
|
10140
|
+
const monotonicProgress = onProgress
|
|
10141
|
+
? (sent, total) => {
|
|
10142
|
+
if (sent > progressHighWater) {
|
|
10143
|
+
progressHighWater = sent;
|
|
10144
|
+
onProgress(sent, total);
|
|
10145
|
+
}
|
|
10146
|
+
}
|
|
10147
|
+
: undefined;
|
|
10059
10148
|
while (offset < file.size) {
|
|
10060
10149
|
if (this.isAborted()) {
|
|
10061
10150
|
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10062
10151
|
}
|
|
10063
10152
|
const chunkEnd = Math.min(offset + session.chunkSize, file.size);
|
|
10064
|
-
|
|
10153
|
+
try {
|
|
10154
|
+
offset = await this.patchChunk(uploadUrl, file.slice(offset, chunkEnd), offset, file.size, monotonicProgress);
|
|
10155
|
+
failedAttempts = 0;
|
|
10156
|
+
continue;
|
|
10157
|
+
}
|
|
10158
|
+
catch (error) {
|
|
10159
|
+
failedAttempts += 1;
|
|
10160
|
+
if (failedAttempts >= CHUNK_MAX_ATTEMPTS || !isRetryableChunkFailure(error)) {
|
|
10161
|
+
throw error;
|
|
10162
|
+
}
|
|
10163
|
+
}
|
|
10164
|
+
// The backoff must not outlive a cancel: raced against the session's
|
|
10165
|
+
// abort signal so cancellation stays prompt mid-retry.
|
|
10166
|
+
await abortableDelay(CHUNK_RETRY_BASE_MS * 2 ** (failedAttempts - 1), this.getAbortSignal());
|
|
10167
|
+
let serverOffset;
|
|
10168
|
+
try {
|
|
10169
|
+
serverOffset = await this.readResumeOffset(uploadUrl, file.size);
|
|
10170
|
+
}
|
|
10171
|
+
catch (headError) {
|
|
10172
|
+
// The blip that killed the PATCH is the likeliest thing to also kill
|
|
10173
|
+
// this HEAD moments later. A transient resync failure spends the same
|
|
10174
|
+
// budget as a failed chunk — never the whole transfer, which is the
|
|
10175
|
+
// exact scenario the retry exists for.
|
|
10176
|
+
if (!isRetryableChunkFailure(headError)) {
|
|
10177
|
+
throw headError;
|
|
10178
|
+
}
|
|
10179
|
+
failedAttempts += 1;
|
|
10180
|
+
if (failedAttempts >= CHUNK_MAX_ATTEMPTS) {
|
|
10181
|
+
throw headError;
|
|
10182
|
+
}
|
|
10183
|
+
continue;
|
|
10184
|
+
}
|
|
10185
|
+
// Forge advances a session's offset only in whole committed chunks, so a
|
|
10186
|
+
// legitimate resume point is always chunk-aligned (or the file end). A
|
|
10187
|
+
// misaligned offset is a corrupt session, and resuming from it would
|
|
10188
|
+
// send a short non-final chunk Forge rejects anyway — fail it as the
|
|
10189
|
+
// protocol violation it is instead.
|
|
10190
|
+
if (serverOffset % session.chunkSize !== 0 && serverOffset !== file.size) {
|
|
10191
|
+
throw new UploadApiError(`Server returned misaligned upload offset (${serverOffset})`, 'invalid_offset', 0);
|
|
10192
|
+
}
|
|
10193
|
+
if (serverOffset > offset) {
|
|
10194
|
+
// Bytes reached the server despite the failure, so the transfer is
|
|
10195
|
+
// alive: the budget is earned back rather than spent down across the
|
|
10196
|
+
// many chunks of a large file.
|
|
10197
|
+
failedAttempts = 0;
|
|
10198
|
+
}
|
|
10199
|
+
offset = serverOffset;
|
|
10065
10200
|
}
|
|
10066
10201
|
return uploadUrl;
|
|
10067
10202
|
}
|
|
10203
|
+
/**
|
|
10204
|
+
* Read the offset a failed chunk should resume from.
|
|
10205
|
+
*
|
|
10206
|
+
* Forge commits a chunk before it answers, so a PATCH that died in transit
|
|
10207
|
+
* may have landed in full or not at all (never in part — the chunk object
|
|
10208
|
+
* is written atomically before the offset advances); only the session's own
|
|
10209
|
+
* persisted offset knows which, and the client's arithmetic is worthless
|
|
10210
|
+
* here. An offset past the declared total is a protocol violation rather
|
|
10211
|
+
* than a resume point: honoring it would end the loop early and report a
|
|
10212
|
+
* partial upload as complete, the same trap {@link patchChunk} guards
|
|
10213
|
+
* against on a PATCH's own offset.
|
|
10214
|
+
*/
|
|
10215
|
+
async readResumeOffset(uploadUrl, totalSize) {
|
|
10216
|
+
const offset = await this.headUploadOffset(uploadUrl, RESUME_CHECK);
|
|
10217
|
+
if (offset > totalSize) {
|
|
10218
|
+
throw new UploadApiError(`Server returned invalid upload offset (${offset})`, 'invalid_offset', 0);
|
|
10219
|
+
}
|
|
10220
|
+
return offset;
|
|
10221
|
+
}
|
|
10068
10222
|
/**
|
|
10069
10223
|
* Create the TUS upload session. Forge reads only `Tus-Resumable`,
|
|
10070
10224
|
* `Upload-Length`, and `X-ISTK` on create, and returns the session URL in
|
|
@@ -10217,27 +10371,31 @@ class UploadApi extends SparkVaultApi {
|
|
|
10217
10371
|
if (this.isAborted()) {
|
|
10218
10372
|
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10219
10373
|
}
|
|
10220
|
-
const offset = await this.headUploadOffset(uploadUrl);
|
|
10374
|
+
const offset = await this.headUploadOffset(uploadUrl, FINALIZATION_CHECK);
|
|
10221
10375
|
lastOffset = offset;
|
|
10222
10376
|
if (offset >= expectedOffset) {
|
|
10223
10377
|
return;
|
|
10224
10378
|
}
|
|
10225
10379
|
if (attempt < FINALIZE_POLL_MAX) {
|
|
10226
|
-
await
|
|
10380
|
+
await delay(FINALIZE_POLL_MS);
|
|
10227
10381
|
}
|
|
10228
10382
|
}
|
|
10229
10383
|
throw new UploadApiError('Upload could not be confirmed: the server never finalized the file. ' +
|
|
10230
10384
|
`Confirmed ${lastOffset ?? 0} of ${expectedOffset} bytes.`, 'finalization_unconfirmed', 0, { confirmedBytes: lastOffset ?? 0, expectedBytes: expectedOffset });
|
|
10231
10385
|
}
|
|
10232
10386
|
/**
|
|
10233
|
-
* Read the server's persisted upload offset via a TUS HEAD request
|
|
10387
|
+
* Read the server's persisted upload offset via a TUS HEAD request — the one
|
|
10388
|
+
* place the client asks Forge where a session really stands, for both
|
|
10389
|
+
* mid-upload resume and end-of-upload confirmation.
|
|
10234
10390
|
*
|
|
10235
10391
|
* Uses raw `fetch` rather than the JSON envelope helper: TUS responses
|
|
10236
10392
|
* carry an empty body and surface state in the `Upload-Offset` header.
|
|
10237
10393
|
* Honors the session abort signal and request timeout like every other
|
|
10238
10394
|
* client call.
|
|
10395
|
+
*
|
|
10396
|
+
* @param check - Which caller this read serves; shapes the failure copy.
|
|
10239
10397
|
*/
|
|
10240
|
-
async headUploadOffset(uploadUrl) {
|
|
10398
|
+
async headUploadOffset(uploadUrl, check) {
|
|
10241
10399
|
const closeSignal = this.getAbortSignal();
|
|
10242
10400
|
const { signal: combinedSignal, dispose } = combineTimeoutAndCloseSignals(this.timeoutMs, closeSignal);
|
|
10243
10401
|
try {
|
|
@@ -10247,12 +10405,12 @@ class UploadApi extends SparkVaultApi {
|
|
|
10247
10405
|
signal: combinedSignal,
|
|
10248
10406
|
});
|
|
10249
10407
|
if (!response.ok) {
|
|
10250
|
-
throw new UploadApiError(
|
|
10408
|
+
throw new UploadApiError(`${check.name} failed: ${response.status}`, check.code, response.status);
|
|
10251
10409
|
}
|
|
10252
10410
|
const header = response.headers.get('Upload-Offset');
|
|
10253
10411
|
const offset = header !== null ? parseInt(header, 10) : NaN;
|
|
10254
10412
|
if (!Number.isFinite(offset) || offset < 0) {
|
|
10255
|
-
throw new UploadApiError(
|
|
10413
|
+
throw new UploadApiError(`${check.name} returned an invalid offset`, check.code, response.status);
|
|
10256
10414
|
}
|
|
10257
10415
|
return offset;
|
|
10258
10416
|
}
|
|
@@ -10263,10 +10421,10 @@ class UploadApi extends SparkVaultApi {
|
|
|
10263
10421
|
if (this.isAborted()) {
|
|
10264
10422
|
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
10265
10423
|
}
|
|
10266
|
-
throw new UploadApiError(
|
|
10424
|
+
throw new UploadApiError(`${check.name} timed out after ${this.timeoutMs}ms`, 'timeout', 0);
|
|
10267
10425
|
}
|
|
10268
10426
|
if (error instanceof TypeError) {
|
|
10269
|
-
throw new UploadApiError(
|
|
10427
|
+
throw new UploadApiError(check.networkMessage, 'network_error', 0);
|
|
10270
10428
|
}
|
|
10271
10429
|
throw new UploadApiError(error instanceof Error ? error.message : 'An unexpected error occurred', 'unknown_error', 0);
|
|
10272
10430
|
}
|
|
@@ -13389,7 +13547,31 @@ function mapUploadSettings(wire) {
|
|
|
13389
13547
|
*/
|
|
13390
13548
|
const DOWNLOAD_MAX_ATTEMPTS = 3;
|
|
13391
13549
|
const DOWNLOAD_RETRY_BASE_MS = 500;
|
|
13392
|
-
|
|
13550
|
+
/**
|
|
13551
|
+
* Time-to-first-byte bound: how long the download host may take to return
|
|
13552
|
+
* response headers. This bounds only the wait for a transfer to start — a
|
|
13553
|
+
* signed link either begins serving promptly or is not going to.
|
|
13554
|
+
*/
|
|
13555
|
+
const DOWNLOAD_TTFB_TIMEOUT_MS = 30000;
|
|
13556
|
+
/**
|
|
13557
|
+
* Liveness bound on the response body: the longest tolerated gap between two
|
|
13558
|
+
* chunks, rearmed on every chunk. Progress, not wall clock, bounds the
|
|
13559
|
+
* transfer — a large file over a slow link still completes, while a stalled
|
|
13560
|
+
* connection fails promptly enough to retry.
|
|
13561
|
+
*/
|
|
13562
|
+
const DOWNLOAD_IDLE_TIMEOUT_MS = 60000;
|
|
13563
|
+
/**
|
|
13564
|
+
* Whole-transfer bound used only where the environment exposes no readable
|
|
13565
|
+
* response body: `blob()` consumes the stream opaquely, so there are no chunk
|
|
13566
|
+
* arrivals to time and wall clock is the only bound available.
|
|
13567
|
+
*/
|
|
13568
|
+
const DOWNLOAD_BODY_TIMEOUT_MS = 300000;
|
|
13569
|
+
/**
|
|
13570
|
+
* Bytes held on the JS heap before they are folded into a Blob part. Caps heap
|
|
13571
|
+
* growth at one window regardless of file size; the platform's blob store owns
|
|
13572
|
+
* everything already transferred.
|
|
13573
|
+
*/
|
|
13574
|
+
const DOWNLOAD_BLOB_FLUSH_BYTES = 8 * 1024 * 1024;
|
|
13393
13575
|
class IngotsModule {
|
|
13394
13576
|
constructor(config, http) {
|
|
13395
13577
|
this.config = config;
|
|
@@ -13622,6 +13804,11 @@ class IngotsModule {
|
|
|
13622
13804
|
* short-lived) and retries transient transport failures with exponential
|
|
13623
13805
|
* backoff. Validation failures (bad host, non-HTTPS) are never retried.
|
|
13624
13806
|
*
|
|
13807
|
+
* Each attempt is bounded by liveness, never by total duration: a deadline
|
|
13808
|
+
* on the response headers, then an idle deadline rearmed on every body
|
|
13809
|
+
* chunk. A stalled transfer restarts from byte zero: this path requests no
|
|
13810
|
+
* byte range, so a full retry is the only recovery.
|
|
13811
|
+
*
|
|
13625
13812
|
* @example
|
|
13626
13813
|
* const blob = await sv.ingots.download(unsealed, 'ing_abc');
|
|
13627
13814
|
* const url = URL.createObjectURL(blob);
|
|
@@ -13631,7 +13818,7 @@ class IngotsModule {
|
|
|
13631
13818
|
for (let attempt = 1; attempt <= DOWNLOAD_MAX_ATTEMPTS; attempt++) {
|
|
13632
13819
|
try {
|
|
13633
13820
|
const link = await this.createDownloadLink(vault, ingotId);
|
|
13634
|
-
return await
|
|
13821
|
+
return await fetchBlobWithLivenessBounds(link.downloadUrl);
|
|
13635
13822
|
}
|
|
13636
13823
|
catch (err) {
|
|
13637
13824
|
lastError = err instanceof Error ? err : new Error(String(err));
|
|
@@ -13880,21 +14067,74 @@ function isRetryableDownloadError(err) {
|
|
|
13880
14067
|
}
|
|
13881
14068
|
return true;
|
|
13882
14069
|
}
|
|
13883
|
-
|
|
14070
|
+
/**
|
|
14071
|
+
* Await one transfer step under a deadline. Expiry aborts the in-flight fetch,
|
|
14072
|
+
* releasing the connection and erroring the response body, and surfaces a
|
|
14073
|
+
* retryable {@link TimeoutError}. The aborted step's own rejection lands on the
|
|
14074
|
+
* handler installed here, so it never escapes as an unhandled rejection.
|
|
14075
|
+
*/
|
|
14076
|
+
function withDeadline(step, timeout, controller, message) {
|
|
14077
|
+
return new Promise((resolve, reject) => {
|
|
14078
|
+
const timeoutId = setTimeout(() => {
|
|
14079
|
+
controller.abort();
|
|
14080
|
+
reject(new TimeoutError(message));
|
|
14081
|
+
}, timeout);
|
|
14082
|
+
step.then((value) => {
|
|
14083
|
+
clearTimeout(timeoutId);
|
|
14084
|
+
resolve(value);
|
|
14085
|
+
}, (error) => {
|
|
14086
|
+
clearTimeout(timeoutId);
|
|
14087
|
+
reject(error);
|
|
14088
|
+
});
|
|
14089
|
+
});
|
|
14090
|
+
}
|
|
14091
|
+
/**
|
|
14092
|
+
* Fetch a signed download URL as a Blob under liveness bounds: one deadline
|
|
14093
|
+
* for the response headers, then an idle deadline rearmed on every body chunk.
|
|
14094
|
+
* Nothing caps the transfer as a whole, so file size never decides whether a
|
|
14095
|
+
* download can finish.
|
|
14096
|
+
*/
|
|
14097
|
+
async function fetchBlobWithLivenessBounds(url) {
|
|
13884
14098
|
const controller = new AbortController();
|
|
13885
|
-
const
|
|
13886
|
-
|
|
13887
|
-
|
|
13888
|
-
|
|
13889
|
-
|
|
13890
|
-
|
|
13891
|
-
|
|
14099
|
+
const response = await withDeadline(fetch(url, { signal: controller.signal }), DOWNLOAD_TTFB_TIMEOUT_MS, controller, 'Download timed out waiting for response headers');
|
|
14100
|
+
if (!response.ok) {
|
|
14101
|
+
// The TTFB timer cleared when headers arrived, so nothing else would ever
|
|
14102
|
+
// reap this connection: abort it or a retrying caller leaks one socket
|
|
14103
|
+
// per failed attempt.
|
|
14104
|
+
controller.abort();
|
|
14105
|
+
const error = new Error(`Download failed with status ${response.status}`);
|
|
14106
|
+
error.statusCode = response.status;
|
|
14107
|
+
throw error;
|
|
14108
|
+
}
|
|
14109
|
+
const contentType = response.headers.get('content-type') ?? '';
|
|
14110
|
+
// Where the environment exposes no readable body, the transfer keeps
|
|
14111
|
+
// whole-transfer wall-clock semantics: blob() surfaces no chunk arrivals, so
|
|
14112
|
+
// there is no liveness to observe.
|
|
14113
|
+
if (!response.body) {
|
|
14114
|
+
return withDeadline(response.blob(), DOWNLOAD_BODY_TIMEOUT_MS, controller, 'Download timed out');
|
|
14115
|
+
}
|
|
14116
|
+
const reader = response.body.getReader();
|
|
14117
|
+
const parts = [];
|
|
14118
|
+
let pending = [];
|
|
14119
|
+
let pendingBytes = 0;
|
|
14120
|
+
for (;;) {
|
|
14121
|
+
const { done, value } = await withDeadline(reader.read(), DOWNLOAD_IDLE_TIMEOUT_MS, controller, 'Download stalled: no data received');
|
|
14122
|
+
if (done)
|
|
14123
|
+
break;
|
|
14124
|
+
pending.push(value);
|
|
14125
|
+
pendingBytes += value.byteLength;
|
|
14126
|
+
if (pendingBytes >= DOWNLOAD_BLOB_FLUSH_BYTES) {
|
|
14127
|
+
// Fetch body chunks are ArrayBuffer-backed; the lib types over-generalize
|
|
14128
|
+
// to ArrayBufferLike, which the Blob constructor's part type rejects.
|
|
14129
|
+
parts.push(new Blob(pending));
|
|
14130
|
+
pending = [];
|
|
14131
|
+
pendingBytes = 0;
|
|
13892
14132
|
}
|
|
13893
|
-
return await response.blob();
|
|
13894
14133
|
}
|
|
13895
|
-
|
|
13896
|
-
|
|
14134
|
+
if (pending.length > 0) {
|
|
14135
|
+
parts.push(new Blob(pending));
|
|
13897
14136
|
}
|
|
14137
|
+
return new Blob(parts, { type: contentType });
|
|
13898
14138
|
}
|
|
13899
14139
|
|
|
13900
14140
|
/**
|
|
@@ -14761,7 +15001,7 @@ class SparkVault {
|
|
|
14761
15001
|
}
|
|
14762
15002
|
/** Get the SDK version */
|
|
14763
15003
|
static get version() {
|
|
14764
|
-
return '9.
|
|
15004
|
+
return '9.2.0';
|
|
14765
15005
|
}
|
|
14766
15006
|
}
|
|
14767
15007
|
if (typeof window !== 'undefined') {
|