@sparkvault/sdk-js 15.9.1 → 15.10.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/README.md CHANGED
@@ -214,8 +214,12 @@ mutation automatically. A timeout can occur after publication: it is an
214
214
  uncertain outcome, not proof that nothing changed.
215
215
  `history(vault, ref)` lists retained publications;
216
216
  restore validates historical values against the current Type and creates a new
217
- version. Configure retention with the vault's `structuredIngotVersionLimit`
218
- setting. `delete(vault, ref, { expectedRevision })` deletes only the inspected
217
+ version. Retention is the Vault's `ingotVersionLimit` setting, shared with
218
+ ordinary files and defaulting to zero previous versions. An interactive company
219
+ administrator sets it with `vaults.updateVersionHistory(vaultId, { versionLimit })`
220
+ (0–50). Current content is additional; lowering the limit takes effect on each
221
+ ingot's next successful content change. The old `structuredIngotVersionLimit`
222
+ property and `updateStructuredIngotSettings` method remain compatibility aliases. `delete(vault, ref, { expectedRevision })` deletes only the inspected
219
223
  publication and its retained history. Omitting the option preserves deliberate
220
224
  unconditional deletion. Older server responses may omit `revision`; do not
221
225
  silently drop the guard when a workflow requires conditional writes.
@@ -227,6 +231,14 @@ read-modify-write of file contents, `ingots.downloadWithRevision` returns
227
231
  metadata describes the earlier mint, so it should not be used as proof of the
228
232
  downloaded content's revision.
229
233
 
234
+ `ingots.listVersions(vault, ingotId)` lists previous file publications newest first,
235
+ excluding current content. `ingots.downloadVersion(vault, ingotId, versionId)`
236
+ downloads one retained publication and verifies the content-version header on the
237
+ received bytes. It refuses a pruned version or a response that cannot prove the
238
+ requested publication; it never substitutes current content. Use
239
+ `ingots.createVersionDownloadLink` only when your integration needs the URL itself.
240
+ Structured objects keep their product-specific history and type-aware restore.
241
+
230
242
  Create rejects an existing name; it never overwrites. Names follow the server's
231
243
  Unicode caseless matching rules. An optional stable `idempotencyKey` contains
232
244
  8–128 printable ASCII characters. A successful replay has `replayed: true` and
package/dist/index.cjs CHANGED
@@ -13714,6 +13714,11 @@ async function attemptWith(source, state, options) {
13714
13714
  assertDownloadUrl(link, options.isAllowedHost);
13715
13715
  }
13716
13716
  catch (error) {
13717
+ // A successful HTTP response that failed local validation is an answer,
13718
+ // not a lost connection. Retrying cannot make its publication safe to use.
13719
+ if (error instanceof SparkVaultError && error.code === 'invalid_response') {
13720
+ return { ok: false, error, outcome: FINAL };
13721
+ }
13717
13722
  if (!(error instanceof SparkVaultError) || typeof error.statusCode !== 'number') {
13718
13723
  return { ok: false, error, outcome: UNANSWERED };
13719
13724
  }
@@ -13780,6 +13785,12 @@ async function attemptDownload(link, state, options) {
13780
13785
  const outcome = classifyTransferResponse({ status: response.status, headers: (name) => response.headers.get(name) });
13781
13786
  return { ok: false, error: await refusedDownloadError(response, outcome, controller), outcome };
13782
13787
  }
13788
+ // Every response contributes bytes, including a 206 continuation. Never
13789
+ // let a resumed response bypass the caller's publication precondition.
13790
+ const contentVersion = response.headers.get('x-content-version') || null;
13791
+ if (expectedContentVersion && contentVersion && contentVersion !== expectedContentVersion) {
13792
+ return { ok: false, error: new SparkVaultError(CONTENT_CHANGED_MESSAGE, 'content_changed'), outcome: FINAL };
13793
+ }
13783
13794
  if (resume && response.status === 206) {
13784
13795
  const continued = continuation(response, resume);
13785
13796
  if (continued instanceof SparkVaultError)
@@ -13788,10 +13799,6 @@ async function attemptDownload(link, state, options) {
13788
13799
  }
13789
13800
  else {
13790
13801
  received.discard();
13791
- const contentVersion = response.headers.get('x-content-version') || null;
13792
- if (expectedContentVersion && contentVersion && contentVersion !== expectedContentVersion) {
13793
- return { ok: false, error: new SparkVaultError(CONTENT_CHANGED_MESSAGE, 'content_changed'), outcome: FINAL };
13794
- }
13795
13802
  held = {
13796
13803
  contentType: response.headers.get('content-type') ?? '',
13797
13804
  contentVersion,
@@ -13898,7 +13905,9 @@ function resumeValidator(headers) {
13898
13905
  */
13899
13906
  function continuation(response, resume) {
13900
13907
  const etag = response.headers.get('etag');
13901
- if (etag !== null && etag.replace(/^W\//, '') !== resume.validator) {
13908
+ const contentVersion = response.headers.get('x-content-version');
13909
+ if ((etag !== null && etag.replace(/^W\//, '') !== resume.validator) ||
13910
+ (contentVersion && resume.held.contentVersion && contentVersion !== resume.held.contentVersion)) {
13902
13911
  return new SparkVaultError(CONTENT_CHANGED_MESSAGE, 'content_changed');
13903
13912
  }
13904
13913
  const range = CONTENT_RANGE.exec(response.headers.get('content-range') ?? '');
@@ -14059,6 +14068,26 @@ function readRevision(value) {
14059
14068
  }
14060
14069
  }
14061
14070
 
14071
+ /** Core files and Structured Ingots expose the same retained publication metadata. */
14072
+ function mapIngotHistory(data) {
14073
+ return {
14074
+ id: data.ingot_id,
14075
+ versionLimit: data.version_limit,
14076
+ versions: data.versions.map(version => ({
14077
+ versionId: version.version_id,
14078
+ publishedAt: version.published_at,
14079
+ sizeBytes: version.size_bytes,
14080
+ ...(version.content_type === undefined ? {} : { contentType: version.content_type }),
14081
+ })),
14082
+ };
14083
+ }
14084
+ function requireVersionId(value) {
14085
+ if (typeof value !== 'string' || !/^[A-Za-z0-9:_-]{1,128}$/.test(value)) {
14086
+ throw new ValidationError('versionId must name a retained publication');
14087
+ }
14088
+ return value;
14089
+ }
14090
+
14062
14091
  /**
14063
14092
  * Folders Module
14064
14093
  *
@@ -15765,6 +15794,49 @@ class IngotsModule {
15765
15794
  });
15766
15795
  return mapFolderContents(response.data);
15767
15796
  }
15797
+ /** List previous publications of a file, newest first. Structured objects use their product's history method. */
15798
+ async listVersions(vault, ingotId) {
15799
+ const vaultId = requireVaultId(vault);
15800
+ const id = requireResourceId(ingotId, 'ingotId');
15801
+ const { data } = await this.http.get(apiPath `/v1/vaults/${vaultId}/ingots/${id}/versions`, { vat: vault.vatToken });
15802
+ return mapIngotHistory(data);
15803
+ }
15804
+ /** Mint a one-use link to a retained file publication; use downloadVersion to verify the served bytes. */
15805
+ async createVersionDownloadLink(vault, ingotId, versionId) {
15806
+ const vaultId = requireVaultId(vault);
15807
+ const id = requireResourceId(ingotId, 'ingotId');
15808
+ const version = requireVersionId(versionId);
15809
+ const { data } = await this.http.post(apiPath `/v1/vaults/${vaultId}/ingots/${id}/versions/${version}/download`, undefined, { vat: vault.vatToken });
15810
+ if (data.ingot_id !== id || data.content_version !== version) {
15811
+ throw new SparkVaultError('The server returned a different retained publication', 'invalid_response');
15812
+ }
15813
+ assertDownloadUrl(data.download_url, this.allowsTransferHost);
15814
+ return {
15815
+ id: data.ingot_id,
15816
+ downloadUrl: data.download_url,
15817
+ size: data.size_bytes,
15818
+ contentVersion: data.content_version,
15819
+ expiresAt: data.expires_at,
15820
+ ...(data.revision === undefined ? {} : { revision: readRevision(data.revision) }),
15821
+ ...(data.name === undefined ? {} : { name: data.name }),
15822
+ ...(data.content_type === undefined ? {} : { contentType: data.content_type }),
15823
+ };
15824
+ }
15825
+ /** Download a retained file publication; never substitutes the current bytes if history is unavailable. */
15826
+ async downloadVersion(vault, ingotId, versionId) {
15827
+ requireVaultId(vault);
15828
+ requireResourceId(ingotId, 'ingotId');
15829
+ const version = requireVersionId(versionId);
15830
+ const mint = async () => (await this.createVersionDownloadLink(vault, ingotId, version)).downloadUrl;
15831
+ const download = await fetchMintedDownload(mint, {
15832
+ isAllowedHost: this.allowsTransferHost, expectedContentVersion: version,
15833
+ });
15834
+ // Older transfer servers can omit the header; metadata from the mint is not proof of the bytes served.
15835
+ if (download.contentVersion !== version) {
15836
+ throw new SparkVaultError('The download did not identify the requested retained publication', 'invalid_response');
15837
+ }
15838
+ return download.blob;
15839
+ }
15768
15840
  /**
15769
15841
  * Create a signed download link for an ingot (discrete primitive).
15770
15842
  *
@@ -21714,16 +21786,13 @@ class StructuredIngotsModule {
21714
21786
  const id = requireVault(vault);
21715
21787
  const record = requireRef(ref);
21716
21788
  const { data } = await this.http.get(apiPath `/v1/products/structured-ingots/${id}/${record}/versions`, { vat: vault.vatToken });
21717
- return { id: data.ingot_id, versionLimit: data.version_limit, versions: data.versions.map((version) => ({
21718
- versionId: version.version_id, publishedAt: version.published_at, sizeBytes: version.size_bytes,
21719
- })) };
21789
+ return mapIngotHistory(data);
21720
21790
  }
21721
21791
  async restore(vault, ref, versionId, options) {
21722
21792
  const id = requireVault(vault);
21723
21793
  const record = requireRef(ref);
21724
21794
  const precondition = mutationPrecondition(options);
21725
- if (typeof versionId !== 'string' || !/^[A-Za-z0-9:_-]{1,128}$/.test(versionId))
21726
- throw new ValidationError('versionId must name a retained publication');
21795
+ requireVersionId(versionId);
21727
21796
  const { data } = await this.http.post(apiPath `/v1/products/structured-ingots/${id}/${record}/versions/${versionId}/restore`, precondition, { vat: vault.vatToken });
21728
21797
  return mutation(data);
21729
21798
  }
@@ -21892,8 +21961,70 @@ function unsealRequestBody(key, ttlSeconds) {
21892
21961
  return ttlSeconds === undefined ? { [field]: trimmed } : { [field]: trimmed, ttl_seconds: ttlSeconds };
21893
21962
  }
21894
21963
 
21895
- /** Maximum previous publications retained for each structured object. */
21896
- const MAX_STRUCTURED_INGOT_VERSIONS = 50;
21964
+ /**
21965
+ * The apps that manage ingots, each by the id its ingots carry as `managed_by`:
21966
+ * the one statement of those ids, of the name each app goes by, of whether an
21967
+ * app manages a row (isManagedIngot), and of whose rows Core seals server-side
21968
+ * (isServerSealedIngot). Each app marks every ingot it creates
21969
+ * with its own (Structured Ingots also calls Core as it, since Core lets only
21970
+ * the internal invoker an ingot's `managed_by` names create or touch one);
21971
+ * Core, the console, the mobile app and the Slack app ask isManagedIngot of a
21972
+ * row, and the mobile app names the app on the row's sheet. An app's ingot is
21973
+ * the app's: Core's file routes act on it only for the app, the vault shares
21974
+ * it with no one, the app included, and no request files it in a folder
21975
+ * (docs/APPS.md → Managed Ingots).
21976
+ *
21977
+ * A value is stored on every ingot its app creates and never changes on the
21978
+ * row (`managed_by` is immutable), so an id here is never renamed: a new
21979
+ * value would leave every existing ingot naming one no app answers to.
21980
+ *
21981
+ * BROWSER-SAFE: data and pure functions, no imports.
21982
+ */
21983
+
21984
+ const MANAGED_BY = Object.freeze({
21985
+ /** A Structured Ingots record, Identity's directory profiles among them. */
21986
+ STRUCTURED_INGOTS: 'structured-ingots',
21987
+ /** An encrypted catalog of a Structured Ingots type's immutable definitions. */
21988
+ STRUCTURED_INGOT_TYPES: 'structured-ingot-types',
21989
+ /** A help desk message body, email original, attachment or requester export part. */
21990
+ HELP_DESK: 'product-help-desk',
21991
+ /** A Messaging message, compacted page or attachment. */
21992
+ MESSAGING: 'product-messaging'
21993
+ });
21994
+
21995
+ /** Each app's name, as UI copy names the product (docs/DESIGN.md → Voice & copy). */
21996
+ new Map([
21997
+ [MANAGED_BY.STRUCTURED_INGOTS, 'Structured Ingots'],
21998
+ [MANAGED_BY.STRUCTURED_INGOT_TYPES, 'Structured Ingots'],
21999
+ [MANAGED_BY.HELP_DESK, 'Secure Help Desk'],
22000
+ [MANAGED_BY.MESSAGING, 'Messaging']
22001
+ ]);
22002
+
22003
+ /**
22004
+ * The apps inside Core whose ingots Core writes server-side, sealing content
22005
+ * it is handed in one call (apps/api UnifiedIngotService.sealIngotContent):
22006
+ * each such ingot lies at the vault root, at an id its app derives from its
22007
+ * own keys, and holds no name claim. Every other ingot, a Structured Ingots
22008
+ * record's included, is created through Core's upload path and holds its name
22009
+ * by a claim.
22010
+ */
22011
+ new Set([MANAGED_BY.HELP_DESK, MANAGED_BY.MESSAGING]);
22012
+
22013
+ /** Maximum previous publications retained for each user file or object. */
22014
+ const MAX_INGOT_VERSIONS = 50;
22015
+
22016
+ /** Existing vaults retain only current content until retention is enabled. */
22017
+ function ingotVersionLimit(vault) {
22018
+ const limit = vault?.ingot_version_limit === undefined
22019
+ ? (vault?.structured_ingot_version_limit === undefined ? 0 : vault.structured_ingot_version_limit)
22020
+ : vault.ingot_version_limit;
22021
+ // Malformed stored settings must never silently disable retention and purge
22022
+ // history. Every writer uses the same bounded integer contract.
22023
+ if (!Number.isSafeInteger(limit) || limit < 0 || limit > MAX_INGOT_VERSIONS) {
22024
+ throw new RangeError('Invalid ingot version limit');
22025
+ }
22026
+ return limit;
22027
+ }
21897
22028
 
21898
22029
  /**
21899
22030
  * Upload Widget Chrome
@@ -24639,7 +24770,7 @@ class VaultsModule {
24639
24770
  createdAt: v.created_at,
24640
24771
  ingotCount: v.ingot_count,
24641
24772
  storageBytes: v.storage_bytes,
24642
- ...(v.structured_ingot_version_limit === undefined ? {} : { structuredIngotVersionLimit: v.structured_ingot_version_limit }),
24773
+ ...mapVersionHistory(v),
24643
24774
  ...(v.structured_ingots_enabled === undefined ? {} : { structuredIngotsEnabled: v.structured_ingots_enabled }),
24644
24775
  ...mapLifecycle(v),
24645
24776
  })),
@@ -24678,19 +24809,23 @@ class VaultsModule {
24678
24809
  };
24679
24810
  }
24680
24811
  /**
24681
- * Retain this many previous versions of each structured object (0–50).
24682
- * Interactive company administrators only. Applies on each object's next
24683
- * successful change; retained versions count toward storage usage.
24812
+ * Retain this many previous versions of each file or structured object (0–50,
24813
+ * default 0). Interactive company administrators only. Applies on each ingot's
24814
+ * next successful content change; retained versions count toward storage usage.
24684
24815
  */
24685
- async updateStructuredIngotSettings(vaultId, settings) {
24816
+ async updateVersionHistory(vaultId, settings) {
24686
24817
  const id = requireResourceId(vaultId, 'vaultId');
24687
24818
  const limit = settings?.versionLimit;
24688
- if (!Number.isSafeInteger(limit) || limit < 0 || limit > MAX_STRUCTURED_INGOT_VERSIONS) {
24689
- throw new ValidationError(`versionLimit must be a whole number from 0 to ${MAX_STRUCTURED_INGOT_VERSIONS}`);
24819
+ if (!Number.isSafeInteger(limit) || limit < 0 || limit > MAX_INGOT_VERSIONS) {
24820
+ throw new ValidationError(`versionLimit must be a whole number from 0 to ${MAX_INGOT_VERSIONS}`);
24690
24821
  }
24691
- const response = await this.http.put(apiPath `/v1/vaults/${id}/structured-ingot-settings`, { version_limit: limit });
24822
+ const response = await this.http.put(apiPath `/v1/vaults/${id}/version-history`, { version_limit: limit });
24692
24823
  return { vaultId: response.data.vault_id, versionLimit: response.data.version_limit };
24693
24824
  }
24825
+ /** @deprecated Use updateVersionHistory; retention applies to files and structured objects. */
24826
+ async updateStructuredIngotSettings(vaultId, settings) {
24827
+ return this.updateVersionHistory(vaultId, settings);
24828
+ }
24694
24829
  /**
24695
24830
  * Rename a vault.
24696
24831
  *
@@ -24954,6 +25089,12 @@ function mapLifecycle(v) {
24954
25089
  deletionScheduleId: v.deletion_schedule_id,
24955
25090
  };
24956
25091
  }
25092
+ function mapVersionHistory(v) {
25093
+ if (v.ingot_version_limit === undefined && v.structured_ingot_version_limit === undefined)
25094
+ return {};
25095
+ const limit = ingotVersionLimit(v);
25096
+ return { ingotVersionLimit: limit, structuredIngotVersionLimit: limit };
25097
+ }
24957
25098
  function mapVaultDetail(v) {
24958
25099
  return {
24959
25100
  id: v.vault_id,
@@ -24962,7 +25103,7 @@ function mapVaultDetail(v) {
24962
25103
  ...mapLifecycle(v),
24963
25104
  ingotCount: v.ingot_count ?? 0,
24964
25105
  storageBytes: v.storage_bytes ?? 0,
24965
- ...(v.structured_ingot_version_limit === undefined ? {} : { structuredIngotVersionLimit: v.structured_ingot_version_limit }),
25106
+ ...mapVersionHistory(v),
24966
25107
  ...(v.structured_ingots_enabled === undefined ? {} : { structuredIngotsEnabled: v.structured_ingots_enabled }),
24967
25108
  createdAt: v.created_at,
24968
25109
  lastUnsealedAt: v.last_unsealed_at ?? null,
@@ -25756,7 +25897,7 @@ class SparkVault {
25756
25897
  }
25757
25898
  /** Get the SDK version */
25758
25899
  static get version() {
25759
- return '15.9.1';
25900
+ return '15.10.0';
25760
25901
  }
25761
25902
  }
25762
25903
  if (typeof window !== 'undefined') {