@sparkvault/sdk-js 9.2.0 → 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 CHANGED
@@ -10016,8 +10016,9 @@ function isRetryableChunkFailure(error) {
10016
10016
  return false;
10017
10017
  // Verdicts, not blips: a cancel is the user's decision, and an invalid or
10018
10018
  // misaligned offset is a protocol violation the server would only repeat.
10019
- // Both carry statusCode 0, so they must be excluded by code before the
10020
- // status heuristic below can see them.
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.
10021
10022
  if (error.code === 'cancelled' || error.code === 'invalid_offset')
10022
10023
  return false;
10023
10024
  const status = error.statusCode ?? 0;
@@ -10149,6 +10150,13 @@ class UploadApi extends SparkVaultApi {
10149
10150
  }
10150
10151
  }
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;
10152
10160
  while (offset < file.size) {
10153
10161
  if (this.isAborted()) {
10154
10162
  throw new UploadApiError('Upload cancelled', 'cancelled', 0);
@@ -10157,6 +10165,9 @@ class UploadApi extends SparkVaultApi {
10157
10165
  try {
10158
10166
  offset = await this.patchChunk(uploadUrl, file.slice(offset, chunkEnd), offset, file.size, monotonicProgress);
10159
10167
  failedAttempts = 0;
10168
+ if (offset >= file.size) {
10169
+ finalizeAcknowledged = true;
10170
+ }
10160
10171
  continue;
10161
10172
  }
10162
10173
  catch (error) {
@@ -10165,26 +10176,29 @@ class UploadApi extends SparkVaultApi {
10165
10176
  throw error;
10166
10177
  }
10167
10178
  }
10168
- // The backoff must not outlive a cancel: raced against the session's
10169
- // abort signal so cancellation stays prompt mid-retry.
10170
- await abortableDelay(CHUNK_RETRY_BASE_MS * 2 ** (failedAttempts - 1), this.getAbortSignal());
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.
10171
10186
  let serverOffset;
10172
- try {
10173
- serverOffset = await this.readResumeOffset(uploadUrl, file.size);
10174
- }
10175
- catch (headError) {
10176
- // The blip that killed the PATCH is the likeliest thing to also kill
10177
- // this HEAD moments later. A transient resync failure spends the same
10178
- // budget as a failed chunk — never the whole transfer, which is the
10179
- // exact scenario the retry exists for.
10180
- if (!isRetryableChunkFailure(headError)) {
10181
- throw headError;
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;
10182
10192
  }
10183
- failedAttempts += 1;
10184
- if (failedAttempts >= CHUNK_MAX_ATTEMPTS) {
10185
- throw headError;
10193
+ catch (headError) {
10194
+ if (!isRetryableChunkFailure(headError)) {
10195
+ throw headError;
10196
+ }
10197
+ failedAttempts += 1;
10198
+ if (failedAttempts >= CHUNK_MAX_ATTEMPTS) {
10199
+ throw headError;
10200
+ }
10186
10201
  }
10187
- continue;
10188
10202
  }
10189
10203
  // Forge advances a session's offset only in whole committed chunks, so a
10190
10204
  // legitimate resume point is always chunk-aligned (or the file end). A
@@ -10202,8 +10216,69 @@ class UploadApi extends SparkVaultApi {
10202
10216
  }
10203
10217
  offset = serverOffset;
10204
10218
  }
10219
+ if (!finalizeAcknowledged) {
10220
+ await this.confirmServerFinalized(uploadUrl, file.size);
10221
+ }
10205
10222
  return uploadUrl;
10206
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
+ }
10207
10282
  /**
10208
10283
  * Read the offset a failed chunk should resume from.
10209
10284
  *
@@ -15005,7 +15080,7 @@ class SparkVault {
15005
15080
  }
15006
15081
  /** Get the SDK version */
15007
15082
  static get version() {
15008
- return '9.2.0';
15083
+ return '9.2.1';
15009
15084
  }
15010
15085
  }
15011
15086
  if (typeof window !== 'undefined') {