@sparkvault/sdk-js 9.2.2 → 9.2.4

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
@@ -10047,32 +10047,27 @@ function summarizeErrorBody(body) {
10047
10047
  const RESUME_CHECK = {
10048
10048
  name: 'Upload resume check',
10049
10049
  code: 'resume_check_failed',
10050
+ serverFailureMessage: 'Upload could not be resumed: the server no longer recognizes this upload session.',
10050
10051
  networkMessage: 'Unable to resume upload. Please check your connection and try again.',
10051
10052
  };
10052
10053
  const FINALIZATION_CHECK = {
10053
10054
  name: 'Upload finalization check',
10054
10055
  code: 'finalization_check_failed',
10056
+ serverFailureMessage: 'Upload could not be confirmed: the server did not finalize the file.',
10055
10057
  networkMessage: 'Unable to confirm upload. Please check your connection and try again.',
10056
10058
  };
10057
- /**
10058
- * Turn a terminal confirmation failure into something a person can read.
10059
- *
10060
- * The upload widget prints `error.message` verbatim, and a bare status line
10061
- * ("Upload finalization check failed: 500") tells the user nothing about their
10062
- * file. Only the status-carrying failure is rewritten: a timeout or a
10063
- * connection failure already explains itself in the user's terms, and its code
10064
- * is load-bearing taxonomy. The status and the server's own detail move into
10065
- * `details`, where support can still read them.
10066
- */
10067
- function asFinalizationFailure(error) {
10068
- if (!(error instanceof UploadApiError) || error.code !== FINALIZATION_CHECK.code) {
10069
- return error;
10070
- }
10071
- return new UploadApiError('Upload could not be confirmed: the server did not finalize the file.', error.code, error.statusCode ?? 0, { ...error.details, status: error.statusCode ?? 0 });
10072
- }
10073
10059
  class UploadApi extends SparkVaultApi {
10074
10060
  constructor(config) {
10075
10061
  super(config.timeout);
10062
+ /**
10063
+ * Vault upload info by vault id, memoized for the life of this client.
10064
+ *
10065
+ * The client is built once with the SDK and the map dies with the page, so
10066
+ * the memo cannot outlive the page it decorates — which is why there is no
10067
+ * TTL and no invalidation API to get wrong. It deliberately survives
10068
+ * `resetSession()`, alongside the other long-lived per-client state.
10069
+ */
10070
+ this.vaultInfoMemo = new Map();
10076
10071
  this.config = config;
10077
10072
  }
10078
10073
  makeError(message, code, statusCode, details) {
@@ -10089,9 +10084,47 @@ class UploadApi extends SparkVaultApi {
10089
10084
  return { data, status };
10090
10085
  }
10091
10086
  /**
10092
- * Get vault upload info (public endpoint).
10087
+ * Get vault upload info (public endpoint), fetched at most once per vault.
10088
+ *
10089
+ * Every widget interaction — `upload()`, `pop()`, `render()`, and each click
10090
+ * an `attach()` handler serves — starts by asking for this, and the answer is
10091
+ * pure display and geometry: the vault's name, its branding, and the size it
10092
+ * advertises. It authorizes nothing. The server re-validates the vault, its
10093
+ * upload source, the request origin, and the file size on the POST that
10094
+ * actually creates the upload, and that re-validation is the correctness
10095
+ * backstop for this whole flow — a memoized answer can never widen what an
10096
+ * upload is allowed to do, and the backstop must stay in place.
10097
+ *
10098
+ * Only a successful answer is kept: a rejection evicts its entry so the next
10099
+ * open retries against the server, and an entry whose request was aborted by
10100
+ * a closing widget is never handed to the widget that replaces it.
10093
10101
  */
10094
10102
  async getVaultUploadInfo(vaultId) {
10103
+ const memoized = this.vaultInfoMemo.get(vaultId);
10104
+ if (memoized && !memoized.pendingSignal?.aborted) {
10105
+ return memoized.promise;
10106
+ }
10107
+ const entry = {
10108
+ pendingSignal: this.getAbortSignal(),
10109
+ promise: this.fetchVaultUploadInfo(vaultId),
10110
+ };
10111
+ this.vaultInfoMemo.set(vaultId, entry);
10112
+ try {
10113
+ const config = await entry.promise;
10114
+ entry.pendingSignal = null;
10115
+ return config;
10116
+ }
10117
+ catch (error) {
10118
+ // Identity guard: a widget that reopened while this request was failing
10119
+ // has its own entry in the map, and evicting by key alone would throw
10120
+ // away its in-flight fetch.
10121
+ if (this.vaultInfoMemo.get(vaultId) === entry) {
10122
+ this.vaultInfoMemo.delete(vaultId);
10123
+ }
10124
+ throw error;
10125
+ }
10126
+ }
10127
+ async fetchVaultUploadInfo(vaultId) {
10095
10128
  const url = `${this.config.apiBaseUrl}/v1/files/in/${vaultId}`;
10096
10129
  const response = await this.request(url, { method: 'GET' });
10097
10130
  if (!isVaultUploadInfoResponse(response.data)) {
@@ -10282,7 +10315,7 @@ class UploadApi extends SparkVaultApi {
10282
10315
  if (attempt >= FINALIZE_MAX_ATTEMPTS ||
10283
10316
  consecutiveTimeouts >= FINALIZE_MAX_CONSECUTIVE_TIMEOUTS ||
10284
10317
  !isRetryableTransferFailure(error)) {
10285
- throw asFinalizationFailure(error);
10318
+ throw error;
10286
10319
  }
10287
10320
  await abortableDelay(FINALIZE_RETRY_MS, this.getAbortSignal());
10288
10321
  }
@@ -10488,7 +10521,7 @@ class UploadApi extends SparkVaultApi {
10488
10521
  // attempt. What it says is the only server-side detail this client
10489
10522
  // ever sees, so it rides along in `details` for support.
10490
10523
  const detail = summarizeErrorBody(await response.text().catch(() => ''));
10491
- throw new UploadApiError(`${check.name} failed: ${response.status}`, check.code, response.status, detail ? { serverDetail: detail } : undefined);
10524
+ throw new UploadApiError(check.serverFailureMessage, check.code, response.status, { status: response.status, ...(detail ? { serverDetail: detail } : {}) });
10492
10525
  }
10493
10526
  return response;
10494
10527
  }
@@ -15075,7 +15108,7 @@ class SparkVault {
15075
15108
  }
15076
15109
  /** Get the SDK version */
15077
15110
  static get version() {
15078
- return '9.2.2';
15111
+ return '9.2.4';
15079
15112
  }
15080
15113
  }
15081
15114
  if (typeof window !== 'undefined') {