@sparkvault/sdk-js 15.9.0 → 15.9.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/README.md CHANGED
@@ -181,8 +181,9 @@ await records.create<Profile>(vault, {
181
181
  idempotencyKey: 'signup-customer-42',
182
182
  });
183
183
  const profile = await records.get<Profile>(vault, ':profile-customer-42');
184
+ if (!profile.revision) throw new Error('This server does not support guarded object revisions');
184
185
  await records.patch<Profile>(vault, profile.id, {
185
- expectedVersion: profile.version,
186
+ expectedRevision: profile.revision,
186
187
  set: { preferences: { contactAllowed: true } },
187
188
  });
188
189
  ```
@@ -201,13 +202,30 @@ A replay returns `created: false`, `replayed: true` and the original version-one
201
202
  definition receipt, even if the Type has since been published or archived.
202
203
  Call `types.get(vault, typeId)` for its current version and archive state.
203
204
 
204
- PATCH and `restore(vault, ref, versionId, { expectedVersion })` require the
205
- current object version. A concurrent change throws an error with
205
+ PATCH and `restore(vault, ref, versionId, { expectedRevision })` accept the
206
+ opaque `revision` returned by a read. It binds both the object's identity and
207
+ the publication read, so a deletion and recreation under the same name cannot
208
+ pass the guard. Use the returned object ID for mutations where possible.
209
+ The older `expectedVersion` numeric guard remains supported; it does not
210
+ identify an object recreated under a reused name. Supplying both guards
211
+ requires both to match. A concurrent change throws an error with
206
212
  `statusCode === 412`; reread and reconcile the change. The SDK never retries a
207
- mutation automatically. `history(vault, ref)` lists retained publications;
213
+ mutation automatically. A timeout can occur after publication: it is an
214
+ uncertain outcome, not proof that nothing changed.
215
+ `history(vault, ref)` lists retained publications;
208
216
  restore validates historical values against the current Type and creates a new
209
217
  version. Configure retention with the vault's `structuredIngotVersionLimit`
210
- setting. `delete(vault, ref)` deletes an object and its retained publications.
218
+ setting. `delete(vault, ref, { expectedRevision })` deletes only the inspected
219
+ publication and its retained history. Omitting the option preserves deliberate
220
+ unconditional deletion. Older server responses may omit `revision`; do not
221
+ silently drop the guard when a workflow requires conditional writes.
222
+
223
+ Ordinary vault files share the same contract: `ingots.get` exposes `revision`,
224
+ and `ingots.replace` and `ingots.delete` accept `expectedRevision`. For a
225
+ read-modify-write of file contents, `ingots.downloadWithRevision` returns
226
+ `{ blob, revision }` from the bytes Forge actually served. A download link's
227
+ metadata describes the earlier mint, so it should not be used as proof of the
228
+ downloaded content's revision.
211
229
 
212
230
  Create rejects an existing name; it never overwrites. Names follow the server's
213
231
  Unicode caseless matching rules. An optional stable `idempotencyKey` contains
package/dist/index.cjs CHANGED
@@ -13272,6 +13272,47 @@ function readIngotAccess(entry) {
13272
13272
  };
13273
13273
  }
13274
13274
 
13275
+ /**
13276
+ * Opaque optimistic-concurrency identity for one publication of one ingot.
13277
+ * A legacy publication has no content marker, but still names its immutable
13278
+ * ingot id: deleting and recreating a logical name never reuses its revision.
13279
+ * This is a precondition, not a credential or a content digest.
13280
+ */
13281
+
13282
+ const INGOT_REVISION_MAX_LENGTH = 4096;
13283
+ const MAX_CONTENT_VERSION_LENGTH = 256;
13284
+
13285
+ /** @returns {string} A portable, canonical revision for the actual publication. */
13286
+ function formatIngotRevision(ingotId, contentVersion) {
13287
+ if (!isResourceIdOf('ingot', ingotId) ||
13288
+ (contentVersion !== null && (typeof contentVersion !== 'string' || !contentVersion.length || contentVersion.length > MAX_CONTENT_VERSION_LENGTH))) {
13289
+ throw new TypeError('An ingot revision requires an ingot id and its content version');
13290
+ }
13291
+ try {
13292
+ return `ir1:${ingotId}:${contentVersion === null ? '' : encodeURIComponent(contentVersion)}`;
13293
+ } catch {
13294
+ throw new TypeError('An ingot revision requires a valid content version');
13295
+ }
13296
+ }
13297
+
13298
+ /** @returns {{ ingotId: string, contentVersion: string|null }} */
13299
+ function parseIngotRevision(revision) {
13300
+ if (typeof revision !== 'string' || revision.length > INGOT_REVISION_MAX_LENGTH) {
13301
+ throw new TypeError('revision must be an opaque ingot revision returned by the server');
13302
+ }
13303
+ const parts = revision.split(':');
13304
+ if (parts.length !== 3 || parts[0] !== 'ir1') {
13305
+ throw new TypeError('revision must be an opaque ingot revision returned by the server');
13306
+ }
13307
+ try {
13308
+ const contentVersion = parts[2] === '' ? null : decodeURIComponent(parts[2]);
13309
+ if (formatIngotRevision(parts[1], contentVersion) !== revision) throw new TypeError();
13310
+ return { ingotId: parts[1], contentVersion };
13311
+ } catch {
13312
+ throw new TypeError('revision must be an opaque ingot revision returned by the server');
13313
+ }
13314
+ }
13315
+
13275
13316
  /**
13276
13317
  * SparkVault Debug Logger
13277
13318
  *
@@ -13993,6 +14034,31 @@ class ReceivedBytes {
13993
14034
  }
13994
14035
  }
13995
14036
 
14037
+ /** Validate an opaque server revision without interpreting it as a credential. */
14038
+ function expectedRevision(value) {
14039
+ if (value === undefined)
14040
+ return undefined;
14041
+ try {
14042
+ parseIngotRevision(value);
14043
+ return value;
14044
+ }
14045
+ catch {
14046
+ throw new ValidationError('expectedRevision must be a revision returned by an ingot read');
14047
+ }
14048
+ }
14049
+ /** Older servers omit the revision; never invent a precondition from metadata. */
14050
+ function readRevision(value) {
14051
+ if (value === undefined)
14052
+ return undefined;
14053
+ try {
14054
+ parseIngotRevision(value);
14055
+ return value;
14056
+ }
14057
+ catch {
14058
+ throw new SparkVaultError('The server returned an invalid ingot revision', 'invalid_response');
14059
+ }
14060
+ }
14061
+
13996
14062
  /**
13997
14063
  * Folders Module
13998
14064
  *
@@ -15536,12 +15602,16 @@ class IngotsModule {
15536
15602
  * before the replacement is staged where there is no XMLHttpRequest.
15537
15603
  *
15538
15604
  * @example
15539
- * await sv.ingots.replace(unsealed, 'ing_abc', { file: updatedFile });
15605
+ * const original = await sv.ingots.downloadWithRevision(unsealed, 'ing_abc');
15606
+ * await sv.ingots.replace(unsealed, 'ing_abc', {
15607
+ * file: updatedFile, expectedRevision: original.revision,
15608
+ * });
15540
15609
  */
15541
15610
  async replace(vault, ingotId, options) {
15542
15611
  this.validateFile(options.file);
15543
15612
  const vaultId = requireVaultId(vault);
15544
15613
  const id = requireResourceId(ingotId, 'ingotId');
15614
+ const revision = expectedRevision(options.expectedRevision);
15545
15615
  const name = options.name ?? getBlobName(options.file);
15546
15616
  const contentType = sentContentType(options);
15547
15617
  assertBrowserTransfer();
@@ -15551,6 +15621,7 @@ class IngotsModule {
15551
15621
  ...(contentType !== undefined && { content_type: contentType }),
15552
15622
  size_bytes: options.file.size,
15553
15623
  ...(options.contentHmac && { content_hmac: options.contentHmac }),
15624
+ ...(revision === undefined ? {} : { expected_revision: revision }),
15554
15625
  }, { vat: vault.vatToken }).catch((error) => {
15555
15626
  throw stagingRefusalOf(error);
15556
15627
  });
@@ -15592,12 +15663,16 @@ class IngotsModule {
15592
15663
  * Delete an ingot.
15593
15664
  *
15594
15665
  * @example
15595
- * await sv.ingots.delete(unsealed, 'ing_abc');
15666
+ * const original = await sv.ingots.get(unsealed, 'ing_abc');
15667
+ * if (!original.revision) throw new Error('This server does not support guarded deletion');
15668
+ * await sv.ingots.delete(unsealed, original.id, { expectedRevision: original.revision });
15596
15669
  */
15597
- async delete(vault, ingotId) {
15670
+ async delete(vault, ingotId, options = {}) {
15598
15671
  const vaultId = requireVaultId(vault);
15599
15672
  const id = requireResourceId(ingotId, 'ingotId');
15600
- await this.http.delete(apiPath `/v1/vaults/${vaultId}/ingots/${id}`, { vat: vault.vatToken });
15673
+ await this.http.delete(apiPath `/v1/vaults/${vaultId}/ingots/${id}`, {
15674
+ vat: vault.vatToken, query: { expected_revision: expectedRevision(options.expectedRevision) },
15675
+ });
15601
15676
  }
15602
15677
  /**
15603
15678
  * Get ingot metadata.
@@ -15717,6 +15792,7 @@ class IngotsModule {
15717
15792
  return {
15718
15793
  downloadUrl: response.data.download_url,
15719
15794
  contentVersion: response.data.content_version ?? null,
15795
+ ...(response.data.revision === undefined ? {} : { revision: readRevision(response.data.revision) }),
15720
15796
  id: response.data.ingot_id,
15721
15797
  name: response.data.name,
15722
15798
  size: response.data.size_bytes,
@@ -15750,8 +15826,19 @@ class IngotsModule {
15750
15826
  * link.click();
15751
15827
  */
15752
15828
  async download(vault, ingotId, options = {}) {
15829
+ return (await this.downloadWithRevision(vault, ingotId, options)).blob;
15830
+ }
15831
+ /**
15832
+ * Download bytes with their identity-bound revision. Use the returned revision
15833
+ * for replace/delete preconditions; the link's earlier metadata may already
15834
+ * have been superseded when Forge begins serving the bytes.
15835
+ */
15836
+ async downloadWithRevision(vault, ingotId, options = {}) {
15837
+ requireVaultId(vault);
15838
+ const id = requireResourceId(ingotId, 'ingotId');
15753
15839
  const mint = async () => (await this.createDownloadLink(vault, ingotId, options)).downloadUrl;
15754
- return (await fetchMintedDownload(mint, { isAllowedHost: this.allowsTransferHost })).blob;
15840
+ const download = await fetchMintedDownload(mint, { isAllowedHost: this.allowsTransferHost });
15841
+ return { blob: download.blob, revision: formatIngotRevision(id, download.contentVersion) };
15755
15842
  }
15756
15843
  /**
15757
15844
  * Get an ingot's sharing configuration.
@@ -15947,6 +16034,7 @@ function mapIngot(wire) {
15947
16034
  folderId: wire.folder_id ?? null,
15948
16035
  contentHmac: wire.content_hmac ?? null,
15949
16036
  contentVersion: wire.content_version ?? null,
16037
+ ...(wire.revision === undefined ? {} : { revision: readRevision(wire.revision) }),
15950
16038
  };
15951
16039
  }
15952
16040
  function mapFolderContents(wire) {
@@ -21393,6 +21481,16 @@ function integer(value, field, minimum, maximum) {
21393
21481
  return value;
21394
21482
  }
21395
21483
  function expectedVersion(value) { return integer(value, 'expectedVersion', 1, 0xFFFFFFFF); }
21484
+ function mutationPrecondition(options) {
21485
+ const revision = expectedRevision(options.expectedRevision);
21486
+ const version = options.expectedVersion;
21487
+ if (version !== undefined)
21488
+ expectedVersion(version);
21489
+ if (revision === undefined && version === undefined) {
21490
+ throw new ValidationError('expectedRevision or expectedVersion is required');
21491
+ }
21492
+ return { expected_revision: revision, expected_version: version };
21493
+ }
21396
21494
  function keysQuery(keys) {
21397
21495
  if (keys === undefined)
21398
21496
  return undefined;
@@ -21524,6 +21622,7 @@ class StructuredIngotTypesModule {
21524
21622
 
21525
21623
  function mutation(wire) {
21526
21624
  return { id: wire.ingot_id, version: wire.version, keyCount: wire.key_count, type: readPin(wire.type),
21625
+ ...(wire.revision === undefined ? {} : { revision: readRevision(wire.revision) }),
21527
21626
  ...(wire.created_at === undefined ? {} : { createdAt: wire.created_at }),
21528
21627
  ...(wire.updated_at === undefined ? {} : { updatedAt: wire.updated_at }),
21529
21628
  ...(wire.created === undefined ? {} : { created: wire.created }),
@@ -21568,6 +21667,7 @@ class StructuredIngotsModule {
21568
21667
  if (data.inline !== true)
21569
21668
  throw new SparkVaultError('This server did not return a typed record response', 'invalid_response');
21570
21669
  return { id: data.ingot_id, name: data.name, version: data.version, keyCount: data.key_count,
21670
+ ...(data.revision === undefined ? {} : { revision: readRevision(data.revision) }),
21571
21671
  values: data.values, missingKeys: data.missing_keys, type: readPin(data.type),
21572
21672
  binaryPaths: data.binary_paths ?? data.binary_keys?.map((key) => [key]) ?? [],
21573
21673
  createdAt: data.created_at, updatedAt: data.updated_at };
@@ -21576,6 +21676,7 @@ class StructuredIngotsModule {
21576
21676
  const id = requireVault(vault);
21577
21677
  const { data } = await this.http.get(apiPath `/v1/products/structured-ingots/${id}`, { vat: vault.vatToken, query: pageQuery(options) });
21578
21678
  return page(data, data.ingots.map((record) => ({ id: record.ingot_id, name: record.name, sizeBytes: record.size_bytes,
21679
+ ...(record.revision == null ? {} : { revision: readRevision(record.revision) }),
21579
21680
  createdAt: record.created_at, updatedAt: record.updated_at ?? null })));
21580
21681
  }
21581
21682
  iterate(vault, options = {}) {
@@ -21592,20 +21693,22 @@ class StructuredIngotsModule {
21592
21693
  async patch(vault, ref, options) {
21593
21694
  const id = requireVault(vault);
21594
21695
  const record = requireRef(ref);
21595
- expectedVersion(options.expectedVersion);
21696
+ const precondition = mutationPrecondition(options);
21596
21697
  if (options.set !== undefined)
21597
21698
  validateValues(options.set);
21598
21699
  if (options.delete !== undefined && (!Array.isArray(options.delete) || options.delete.some((key) => typeof key !== 'string' || !key.length))) {
21599
21700
  throw new ValidationError('delete must contain non-empty field names');
21600
21701
  }
21601
21702
  validateBinaryPaths(options.binaryPaths);
21602
- const { data } = await this.http.patch(apiPath `/v1/products/structured-ingots/${id}/${record}`, { expected_version: options.expectedVersion, set: options.set, delete: options.delete, type: wirePin(options.type), binary_paths: options.binaryPaths }, { vat: vault.vatToken });
21703
+ const { data } = await this.http.patch(apiPath `/v1/products/structured-ingots/${id}/${record}`, { ...precondition, set: options.set, delete: options.delete, type: wirePin(options.type), binary_paths: options.binaryPaths }, { vat: vault.vatToken });
21603
21704
  return mutation(data);
21604
21705
  }
21605
- async delete(vault, ref) {
21706
+ async delete(vault, ref, options = {}) {
21606
21707
  const id = requireVault(vault);
21607
21708
  const record = requireRef(ref);
21608
- await this.http.delete(apiPath `/v1/products/structured-ingots/${id}/${record}`, { vat: vault.vatToken });
21709
+ await this.http.delete(apiPath `/v1/products/structured-ingots/${id}/${record}`, {
21710
+ vat: vault.vatToken, query: { expected_revision: expectedRevision(options.expectedRevision) },
21711
+ });
21609
21712
  }
21610
21713
  async history(vault, ref) {
21611
21714
  const id = requireVault(vault);
@@ -21618,10 +21721,10 @@ class StructuredIngotsModule {
21618
21721
  async restore(vault, ref, versionId, options) {
21619
21722
  const id = requireVault(vault);
21620
21723
  const record = requireRef(ref);
21621
- expectedVersion(options.expectedVersion);
21724
+ const precondition = mutationPrecondition(options);
21622
21725
  if (typeof versionId !== 'string' || !/^[A-Za-z0-9:_-]{1,128}$/.test(versionId))
21623
21726
  throw new ValidationError('versionId must name a retained publication');
21624
- const { data } = await this.http.post(apiPath `/v1/products/structured-ingots/${id}/${record}/versions/${versionId}/restore`, { expected_version: options.expectedVersion }, { vat: vault.vatToken });
21727
+ const { data } = await this.http.post(apiPath `/v1/products/structured-ingots/${id}/${record}/versions/${versionId}/restore`, precondition, { vat: vault.vatToken });
21625
21728
  return mutation(data);
21626
21729
  }
21627
21730
  /** Bounded parallel reads with independent, ordered success/error outcomes. */
@@ -25653,7 +25756,7 @@ class SparkVault {
25653
25756
  }
25654
25757
  /** Get the SDK version */
25655
25758
  static get version() {
25656
- return '15.9.0';
25759
+ return '15.9.1';
25657
25760
  }
25658
25761
  }
25659
25762
  if (typeof window !== 'undefined') {