@sparkvault/sdk-js 9.2.3 → 9.3.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 CHANGED
@@ -10059,6 +10059,15 @@ const FINALIZATION_CHECK = {
10059
10059
  class UploadApi extends SparkVaultApi {
10060
10060
  constructor(config) {
10061
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();
10062
10071
  this.config = config;
10063
10072
  }
10064
10073
  makeError(message, code, statusCode, details) {
@@ -10075,9 +10084,47 @@ class UploadApi extends SparkVaultApi {
10075
10084
  return { data, status };
10076
10085
  }
10077
10086
  /**
10078
- * 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.
10079
10101
  */
10080
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) {
10081
10128
  const url = `${this.config.apiBaseUrl}/v1/files/in/${vaultId}`;
10082
10129
  const response = await this.request(url, { method: 'GET' });
10083
10130
  if (!isVaultUploadInfoResponse(response.data)) {
@@ -13212,6 +13259,8 @@ class VaultUploadModule {
13212
13259
  * operations live in their own modules; this module owns the vault record,
13213
13260
  * its sharing/upload configuration, and the browser upload widget.
13214
13261
  */
13262
+ /** Marks a Delegated Vault Access Key token apart from a Vault Master Key. */
13263
+ const DVAK_TOKEN_PREFIX = 'dvak_';
13215
13264
  /** A string every char of which fits in a single header byte (latin1). */
13216
13265
  function isLatin1(value) {
13217
13266
  for (let i = 0; i < value.length; i++) {
@@ -13256,16 +13305,20 @@ class VaultsModule {
13256
13305
  * Unseal a vault to access its contents.
13257
13306
  * Returns a short-lived Vault Access Token (VAT).
13258
13307
  *
13308
+ * @param vaultId - The vault to unseal.
13309
+ * @param key - The vault's VMK, or a DVAK token issued for it.
13310
+ *
13259
13311
  * @example
13260
13312
  * const unsealed = await sv.vaults.unseal('vlt_abc', vmk);
13313
+ * const delegated = await sv.vaults.unseal('vlt_abc', 'dvak_...');
13261
13314
  * console.log('Token:', unsealed.vatToken);
13262
13315
  */
13263
- async unseal(vaultId, vmk) {
13316
+ async unseal(vaultId, key) {
13264
13317
  const cleanId = this.normalizeVaultId(vaultId);
13265
- if (!vmk) {
13266
- throw new ValidationError('VMK is required');
13318
+ if (!key) {
13319
+ throw new ValidationError('A VMK or DVAK token is required');
13267
13320
  }
13268
- const response = await this.http.post(`/v1/vaults/${cleanId}/unseal`, { vmk });
13321
+ const response = await this.http.post(`/v1/vaults/${cleanId}/unseal`, key.startsWith(DVAK_TOKEN_PREFIX) ? { dvak_token: key } : { vmk: key });
13269
13322
  if (!response.data.vat) {
13270
13323
  throw new ValidationError('Unseal response did not include a vault access token');
13271
13324
  }
@@ -15061,7 +15114,7 @@ class SparkVault {
15061
15114
  }
15062
15115
  /** Get the SDK version */
15063
15116
  static get version() {
15064
- return '9.2.3';
15117
+ return '9.3.0';
15065
15118
  }
15066
15119
  }
15067
15120
  if (typeof window !== 'undefined') {