@sparkvault/sdk-js 11.0.2 → 11.1.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.d.ts CHANGED
@@ -2714,7 +2714,10 @@ declare class IngotsModule {
2714
2714
  *
2715
2715
  * Reserves the ingot record and returns the Forge upload URL (carrying the
2716
2716
  * ISTK upload session token). Use this when you want to drive the TUS
2717
- * transfer yourself; otherwise prefer {@link upload}.
2717
+ * transfer yourself; otherwise prefer {@link upload}. A create another
2718
+ * upload of the same file superseded before its token was issued rejects
2719
+ * with code `upload_refused` (`details.coreCode` UPLOAD_SUPERSEDED): the
2720
+ * later upload is the one kept, so a new upload is no remedy.
2718
2721
  *
2719
2722
  * @example
2720
2723
  * const session = await sv.ingots.createUpload({
@@ -2757,7 +2760,10 @@ declare class IngotsModule {
2757
2760
  * Upload a file as a new ingot (high-level flow).
2758
2761
  *
2759
2762
  * Creates the upload session, streams the file to Forge over TUS, then
2760
- * confirms the server finalized the upload before resolving.
2763
+ * confirms the server finalized the upload before resolving. A file the
2764
+ * server refused for good — another upload of it superseded this one, at
2765
+ * its create or while it streamed, or the storage pool cannot take it —
2766
+ * rejects with code `upload_refused`, as {@link sendUpload} does.
2761
2767
  *
2762
2768
  * @example
2763
2769
  * const ingot = await sv.ingots.upload(unsealed, {
@@ -2766,7 +2772,9 @@ declare class IngotsModule {
2766
2772
  */
2767
2773
  upload(vault: UnsealedVault, options: UploadIngotOptions): Promise<CreatedIngot>;
2768
2774
  /**
2769
- * Replace an existing ingot's content (high-level flow).
2775
+ * Replace an existing ingot's content (high-level flow). Rejects as
2776
+ * {@link upload} does, a replacement another upload of the same ingot
2777
+ * superseded included (`upload_refused`).
2770
2778
  *
2771
2779
  * @example
2772
2780
  * await sv.ingots.replace(unsealed, 'ing_abc', { file: updatedFile });
package/dist/index.mjs CHANGED
@@ -26,7 +26,9 @@ function isTransientHttpStatus(status) {
26
26
  }
27
27
 
28
28
  /**
29
- * The upload publication contract between Forge and Core's activation.
29
+ * The upload publication contract between Forge and Core's activation, and
30
+ * the facts of a transfer token both services spell: its shape, and Core's
31
+ * revocation of an upload's token.
30
32
  *
31
33
  * Forge stamps UPLOAD_PUBLICATION_PROTOCOL on every upload session it creates
32
34
  * and on the final state it hands Core's activation (POST
@@ -79,6 +81,33 @@ const UPLOAD_FINAL_REFUSAL_CODES = Object.freeze([
79
81
  UPLOAD_REFUSAL_CODES.UPLOAD_SUPERSEDED
80
82
  ]);
81
83
 
84
+ /**
85
+ * Core's revocation of an upload's encrypt token: the `status` it writes on
86
+ * the token's forge-transactions record, and the `revoked_reason` beside it,
87
+ * written by the write that ends the upload's staging. Each reason is the
88
+ * refusal every reader of the token answers the upload with from then on,
89
+ * as Core's activation refuses such an upload: Core's mint of a token it
90
+ * revoked before issuing it, and Forge on every request the token, or a
91
+ * session it opened, makes.
92
+ *
93
+ * - UPLOAD_SUPERSEDED: a later staging of its ingot superseded the upload
94
+ * (410 UPLOAD_SUPERSEDED, in UPLOAD_SUPERSEDED_MESSAGE's words), which no
95
+ * new upload remedies;
96
+ * - INGOT_DELETED: the ingot it was staged onto was deleted, by a delete or
97
+ * the cascade of its folder, vault or account (a 404 naming the ingot, as
98
+ * activation answers an upload whose ingot is gone), which ends the upload
99
+ * alone: a new upload can store the file again.
100
+ */
101
+ const UPLOAD_TOKEN_REVOCATION = Object.freeze({
102
+ status: 'revoked',
103
+ reasons: Object.freeze({
104
+ UPLOAD_SUPERSEDED: UPLOAD_REFUSAL_CODES.UPLOAD_SUPERSEDED,
105
+ INGOT_DELETED: 'INGOT_DELETED'
106
+ })
107
+ });
108
+
109
+ Object.values(UPLOAD_TOKEN_REVOCATION.reasons);
110
+
82
111
  /**
83
112
  * The response header that names the code Core refused an upload with for
84
113
  * good: Forge sends it on every terminal answer to a tus request, a HEAD's
@@ -11764,6 +11793,46 @@ const REFUSAL_WORDING = new Map([
11764
11793
  ]);
11765
11794
  /** The copy for a final refusal of a code this SDK has no words of its own for. */
11766
11795
  const REFUSED_MESSAGE = 'This upload could not be saved: the server refused it.';
11796
+ /**
11797
+ * The error of an upload Core refused for good (UPLOAD_FINAL_REFUSAL_CODES):
11798
+ * `upload_refused` in Core's own sentence, this SDK's for an answer that
11799
+ * carried none (a HEAD's), Core's code in `details.coreCode`; never retried,
11800
+ * and no new upload of the same file remedies it. The one reading of that
11801
+ * verdict, whichever answer carries it: a transfer answer naming Core's code
11802
+ * ({@link readTransferAnswer}), or Core's own refusal of the request that
11803
+ * stages the upload ({@link stagingRefusal}).
11804
+ */
11805
+ function uploadRefusedError(coreCode, message, status, outcome) {
11806
+ return new UploadApiError(message ?? REFUSAL_WORDING.get(coreCode) ?? REFUSED_MESSAGE, 'upload_refused', status, { status, coreCode }, outcome);
11807
+ }
11808
+ /**
11809
+ * Core's refusal of a request that stages an upload — a create, a
11810
+ * replacement, the widget's initiation — read as the upload's verdict: a final
11811
+ * refusal of Core's (UPLOAD_FINAL_REFUSAL_CODES, the answer's own code: a
11812
+ * later staging of the same file superseded it before its token was minted,
11813
+ * 410 UPLOAD_SUPERSEDED) is the `upload_refused` a transfer answer naming it
11814
+ * throws ({@link uploadRefusedError}); null for any other refusal.
11815
+ */
11816
+ function stagingRefusal(code, message, status, retryAfterMs) {
11817
+ if (!UPLOAD_FINAL_REFUSAL_CODES.includes(code))
11818
+ return null;
11819
+ return uploadRefusedError(code, message, status, {
11820
+ verdict: TRANSFER_VERDICT.REFUSED,
11821
+ retryAfterMs,
11822
+ offset: null,
11823
+ coreCode: code,
11824
+ });
11825
+ }
11826
+ /**
11827
+ * The error a request that stages an upload throws, from the error Core's
11828
+ * answer made: its final refusal of the upload as `upload_refused`
11829
+ * ({@link stagingRefusal}), any other error as it is.
11830
+ */
11831
+ function stagingRefusalOf(error) {
11832
+ if (!(error instanceof SparkVaultError) || error instanceof UploadApiError || error.statusCode === undefined)
11833
+ return error;
11834
+ return stagingRefusal(error.code, error.message, error.statusCode, error.retryAfterMs) ?? error;
11835
+ }
11767
11836
  function cancelledError() {
11768
11837
  return new UploadApiError('Upload cancelled', 'cancelled', 0);
11769
11838
  }
@@ -11904,11 +11973,7 @@ function readTransferAnswer(answer, read) {
11904
11973
  return { outcome, failure: null };
11905
11974
  const { status } = answer;
11906
11975
  if (outcome.verdict === TRANSFER_VERDICT.REFUSED && outcome.coreCode !== null) {
11907
- const message = served?.message ?? REFUSAL_WORDING.get(outcome.coreCode) ?? REFUSED_MESSAGE;
11908
- return {
11909
- outcome,
11910
- failure: new UploadApiError(message, 'upload_refused', status, { status, coreCode: outcome.coreCode }, outcome),
11911
- };
11976
+ return { outcome, failure: uploadRefusedError(outcome.coreCode, served?.message ?? null, status, outcome) };
11912
11977
  }
11913
11978
  if (outcome.verdict === TRANSFER_VERDICT.DEFERRED) {
11914
11979
  return {
@@ -11969,9 +12034,13 @@ class UploadApi extends SparkVaultApi {
11969
12034
  * retries, a cancel aside; a refused API call it raises with its status is
11970
12035
  * Core's verdict, never a transfer's, so of the policy's reading it carries
11971
12036
  * only the floor Core named (`retryAfterMs`), for whoever acts on the
11972
- * refusal.
12037
+ * refusal — and Core's final refusal of the upload a call stages is that
12038
+ * upload's `upload_refused` ({@link stagingRefusal}).
11973
12039
  */
11974
12040
  makeError(message, code, statusCode, details, retryAfterMs = null) {
12041
+ const refused = statusCode > 0 ? stagingRefusal(code, message, statusCode, retryAfterMs) : null;
12042
+ if (refused)
12043
+ return refused;
11975
12044
  const unanswered = statusCode === 0 && code !== 'cancelled';
11976
12045
  return new UploadApiError(message, code, statusCode, details, unanswered
11977
12046
  ? classifyTransferResponse({ status: 0 })
@@ -15995,7 +16064,10 @@ class IngotsModule {
15995
16064
  *
15996
16065
  * Reserves the ingot record and returns the Forge upload URL (carrying the
15997
16066
  * ISTK upload session token). Use this when you want to drive the TUS
15998
- * transfer yourself; otherwise prefer {@link upload}.
16067
+ * transfer yourself; otherwise prefer {@link upload}. A create another
16068
+ * upload of the same file superseded before its token was issued rejects
16069
+ * with code `upload_refused` (`details.coreCode` UPLOAD_SUPERSEDED): the
16070
+ * later upload is the one kept, so a new upload is no remedy.
15999
16071
  *
16000
16072
  * @example
16001
16073
  * const session = await sv.ingots.createUpload({
@@ -16083,7 +16155,10 @@ class IngotsModule {
16083
16155
  * Upload a file as a new ingot (high-level flow).
16084
16156
  *
16085
16157
  * Creates the upload session, streams the file to Forge over TUS, then
16086
- * confirms the server finalized the upload before resolving.
16158
+ * confirms the server finalized the upload before resolving. A file the
16159
+ * server refused for good — another upload of it superseded this one, at
16160
+ * its create or while it streamed, or the storage pool cannot take it —
16161
+ * rejects with code `upload_refused`, as {@link sendUpload} does.
16087
16162
  *
16088
16163
  * @example
16089
16164
  * const ingot = await sv.ingots.upload(unsealed, {
@@ -16109,7 +16184,9 @@ class IngotsModule {
16109
16184
  return this.transferAndFinalize(created, created.created, options.file, options.onProgress);
16110
16185
  }
16111
16186
  /**
16112
- * Replace an existing ingot's content (high-level flow).
16187
+ * Replace an existing ingot's content (high-level flow). Rejects as
16188
+ * {@link upload} does, a replacement another upload of the same ingot
16189
+ * superseded included (`upload_refused`).
16113
16190
  *
16114
16191
  * @example
16115
16192
  * await sv.ingots.replace(unsealed, 'ing_abc', { file: updatedFile });
@@ -16125,7 +16202,9 @@ class IngotsModule {
16125
16202
  content_type: contentType,
16126
16203
  size_bytes: options.file.size,
16127
16204
  ...(options.contentHmac && { content_hmac: options.contentHmac }),
16128
- }, { vat: vault.vatToken });
16205
+ }, { vat: vault.vatToken }).catch((error) => {
16206
+ throw stagingRefusalOf(error);
16207
+ });
16129
16208
  if (!response.data.forge_url) {
16130
16209
  throw new ValidationError('Replace endpoint did not return a Forge upload URL');
16131
16210
  }
@@ -16419,7 +16498,9 @@ class IngotsModule {
16419
16498
  payload.on_name_conflict = body.onNameConflict;
16420
16499
  }
16421
16500
  const vaultId = requireVaultId(vault);
16422
- const response = await this.http.post(apiPath `/v1/vaults/${vaultId}/ingots`, payload, { vat: vault.vatToken });
16501
+ const response = await this.http.post(apiPath `/v1/vaults/${vaultId}/ingots`, payload, { vat: vault.vatToken }).catch((error) => {
16502
+ throw stagingRefusalOf(error);
16503
+ });
16423
16504
  if (!response.data.forge_url) {
16424
16505
  throw new ValidationError('Upload endpoint did not return a Forge upload URL');
16425
16506
  }
@@ -17209,7 +17290,7 @@ class SparkVault {
17209
17290
  }
17210
17291
  /** Get the SDK version */
17211
17292
  static get version() {
17212
- return '11.0.2';
17293
+ return '11.1.0';
17213
17294
  }
17214
17295
  }
17215
17296
  if (typeof window !== 'undefined') {