@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.mjs CHANGED
@@ -10012,8 +10012,9 @@ function isRetryableChunkFailure(error) {
10012
10012
  return false;
10013
10013
  // Verdicts, not blips: a cancel is the user's decision, and an invalid or
10014
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.
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.
10017
10018
  if (error.code === 'cancelled' || error.code === 'invalid_offset')
10018
10019
  return false;
10019
10020
  const status = error.statusCode ?? 0;
@@ -10145,6 +10146,13 @@ class UploadApi extends SparkVaultApi {
10145
10146
  }
10146
10147
  }
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;
10148
10156
  while (offset < file.size) {
10149
10157
  if (this.isAborted()) {
10150
10158
  throw new UploadApiError('Upload cancelled', 'cancelled', 0);
@@ -10153,6 +10161,9 @@ class UploadApi extends SparkVaultApi {
10153
10161
  try {
10154
10162
  offset = await this.patchChunk(uploadUrl, file.slice(offset, chunkEnd), offset, file.size, monotonicProgress);
10155
10163
  failedAttempts = 0;
10164
+ if (offset >= file.size) {
10165
+ finalizeAcknowledged = true;
10166
+ }
10156
10167
  continue;
10157
10168
  }
10158
10169
  catch (error) {
@@ -10161,26 +10172,29 @@ class UploadApi extends SparkVaultApi {
10161
10172
  throw error;
10162
10173
  }
10163
10174
  }
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());
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.
10167
10182
  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;
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;
10178
10188
  }
10179
- failedAttempts += 1;
10180
- if (failedAttempts >= CHUNK_MAX_ATTEMPTS) {
10181
- throw headError;
10189
+ catch (headError) {
10190
+ if (!isRetryableChunkFailure(headError)) {
10191
+ throw headError;
10192
+ }
10193
+ failedAttempts += 1;
10194
+ if (failedAttempts >= CHUNK_MAX_ATTEMPTS) {
10195
+ throw headError;
10196
+ }
10182
10197
  }
10183
- continue;
10184
10198
  }
10185
10199
  // Forge advances a session's offset only in whole committed chunks, so a
10186
10200
  // legitimate resume point is always chunk-aligned (or the file end). A
@@ -10198,8 +10212,69 @@ class UploadApi extends SparkVaultApi {
10198
10212
  }
10199
10213
  offset = serverOffset;
10200
10214
  }
10215
+ if (!finalizeAcknowledged) {
10216
+ await this.confirmServerFinalized(uploadUrl, file.size);
10217
+ }
10201
10218
  return uploadUrl;
10202
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
+ }
10203
10278
  /**
10204
10279
  * Read the offset a failed chunk should resume from.
10205
10280
  *
@@ -15001,7 +15076,7 @@ class SparkVault {
15001
15076
  }
15002
15077
  /** Get the SDK version */
15003
15078
  static get version() {
15004
- return '9.2.0';
15079
+ return '9.2.1';
15005
15080
  }
15006
15081
  }
15007
15082
  if (typeof window !== 'undefined') {