@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.cjs CHANGED
@@ -30,7 +30,9 @@ function isTransientHttpStatus(status) {
30
30
  }
31
31
 
32
32
  /**
33
- * The upload publication contract between Forge and Core's activation.
33
+ * The upload publication contract between Forge and Core's activation, and
34
+ * the facts of a transfer token both services spell: its shape, and Core's
35
+ * revocation of an upload's token.
34
36
  *
35
37
  * Forge stamps UPLOAD_PUBLICATION_PROTOCOL on every upload session it creates
36
38
  * and on the final state it hands Core's activation (POST
@@ -83,6 +85,33 @@ const UPLOAD_FINAL_REFUSAL_CODES = Object.freeze([
83
85
  UPLOAD_REFUSAL_CODES.UPLOAD_SUPERSEDED
84
86
  ]);
85
87
 
88
+ /**
89
+ * Core's revocation of an upload's encrypt token: the `status` it writes on
90
+ * the token's forge-transactions record, and the `revoked_reason` beside it,
91
+ * written by the write that ends the upload's staging. Each reason is the
92
+ * refusal every reader of the token answers the upload with from then on,
93
+ * as Core's activation refuses such an upload: Core's mint of a token it
94
+ * revoked before issuing it, and Forge on every request the token, or a
95
+ * session it opened, makes.
96
+ *
97
+ * - UPLOAD_SUPERSEDED: a later staging of its ingot superseded the upload
98
+ * (410 UPLOAD_SUPERSEDED, in UPLOAD_SUPERSEDED_MESSAGE's words), which no
99
+ * new upload remedies;
100
+ * - INGOT_DELETED: the ingot it was staged onto was deleted, by a delete or
101
+ * the cascade of its folder, vault or account (a 404 naming the ingot, as
102
+ * activation answers an upload whose ingot is gone), which ends the upload
103
+ * alone: a new upload can store the file again.
104
+ */
105
+ const UPLOAD_TOKEN_REVOCATION = Object.freeze({
106
+ status: 'revoked',
107
+ reasons: Object.freeze({
108
+ UPLOAD_SUPERSEDED: UPLOAD_REFUSAL_CODES.UPLOAD_SUPERSEDED,
109
+ INGOT_DELETED: 'INGOT_DELETED'
110
+ })
111
+ });
112
+
113
+ Object.values(UPLOAD_TOKEN_REVOCATION.reasons);
114
+
86
115
  /**
87
116
  * The response header that names the code Core refused an upload with for
88
117
  * good: Forge sends it on every terminal answer to a tus request, a HEAD's
@@ -11768,6 +11797,46 @@ const REFUSAL_WORDING = new Map([
11768
11797
  ]);
11769
11798
  /** The copy for a final refusal of a code this SDK has no words of its own for. */
11770
11799
  const REFUSED_MESSAGE = 'This upload could not be saved: the server refused it.';
11800
+ /**
11801
+ * The error of an upload Core refused for good (UPLOAD_FINAL_REFUSAL_CODES):
11802
+ * `upload_refused` in Core's own sentence, this SDK's for an answer that
11803
+ * carried none (a HEAD's), Core's code in `details.coreCode`; never retried,
11804
+ * and no new upload of the same file remedies it. The one reading of that
11805
+ * verdict, whichever answer carries it: a transfer answer naming Core's code
11806
+ * ({@link readTransferAnswer}), or Core's own refusal of the request that
11807
+ * stages the upload ({@link stagingRefusal}).
11808
+ */
11809
+ function uploadRefusedError(coreCode, message, status, outcome) {
11810
+ return new UploadApiError(message ?? REFUSAL_WORDING.get(coreCode) ?? REFUSED_MESSAGE, 'upload_refused', status, { status, coreCode }, outcome);
11811
+ }
11812
+ /**
11813
+ * Core's refusal of a request that stages an upload — a create, a
11814
+ * replacement, the widget's initiation — read as the upload's verdict: a final
11815
+ * refusal of Core's (UPLOAD_FINAL_REFUSAL_CODES, the answer's own code: a
11816
+ * later staging of the same file superseded it before its token was minted,
11817
+ * 410 UPLOAD_SUPERSEDED) is the `upload_refused` a transfer answer naming it
11818
+ * throws ({@link uploadRefusedError}); null for any other refusal.
11819
+ */
11820
+ function stagingRefusal(code, message, status, retryAfterMs) {
11821
+ if (!UPLOAD_FINAL_REFUSAL_CODES.includes(code))
11822
+ return null;
11823
+ return uploadRefusedError(code, message, status, {
11824
+ verdict: TRANSFER_VERDICT.REFUSED,
11825
+ retryAfterMs,
11826
+ offset: null,
11827
+ coreCode: code,
11828
+ });
11829
+ }
11830
+ /**
11831
+ * The error a request that stages an upload throws, from the error Core's
11832
+ * answer made: its final refusal of the upload as `upload_refused`
11833
+ * ({@link stagingRefusal}), any other error as it is.
11834
+ */
11835
+ function stagingRefusalOf(error) {
11836
+ if (!(error instanceof SparkVaultError) || error instanceof UploadApiError || error.statusCode === undefined)
11837
+ return error;
11838
+ return stagingRefusal(error.code, error.message, error.statusCode, error.retryAfterMs) ?? error;
11839
+ }
11771
11840
  function cancelledError() {
11772
11841
  return new UploadApiError('Upload cancelled', 'cancelled', 0);
11773
11842
  }
@@ -11908,11 +11977,7 @@ function readTransferAnswer(answer, read) {
11908
11977
  return { outcome, failure: null };
11909
11978
  const { status } = answer;
11910
11979
  if (outcome.verdict === TRANSFER_VERDICT.REFUSED && outcome.coreCode !== null) {
11911
- const message = served?.message ?? REFUSAL_WORDING.get(outcome.coreCode) ?? REFUSED_MESSAGE;
11912
- return {
11913
- outcome,
11914
- failure: new UploadApiError(message, 'upload_refused', status, { status, coreCode: outcome.coreCode }, outcome),
11915
- };
11980
+ return { outcome, failure: uploadRefusedError(outcome.coreCode, served?.message ?? null, status, outcome) };
11916
11981
  }
11917
11982
  if (outcome.verdict === TRANSFER_VERDICT.DEFERRED) {
11918
11983
  return {
@@ -11973,9 +12038,13 @@ class UploadApi extends SparkVaultApi {
11973
12038
  * retries, a cancel aside; a refused API call it raises with its status is
11974
12039
  * Core's verdict, never a transfer's, so of the policy's reading it carries
11975
12040
  * only the floor Core named (`retryAfterMs`), for whoever acts on the
11976
- * refusal.
12041
+ * refusal — and Core's final refusal of the upload a call stages is that
12042
+ * upload's `upload_refused` ({@link stagingRefusal}).
11977
12043
  */
11978
12044
  makeError(message, code, statusCode, details, retryAfterMs = null) {
12045
+ const refused = statusCode > 0 ? stagingRefusal(code, message, statusCode, retryAfterMs) : null;
12046
+ if (refused)
12047
+ return refused;
11979
12048
  const unanswered = statusCode === 0 && code !== 'cancelled';
11980
12049
  return new UploadApiError(message, code, statusCode, details, unanswered
11981
12050
  ? classifyTransferResponse({ status: 0 })
@@ -15999,7 +16068,10 @@ class IngotsModule {
15999
16068
  *
16000
16069
  * Reserves the ingot record and returns the Forge upload URL (carrying the
16001
16070
  * ISTK upload session token). Use this when you want to drive the TUS
16002
- * transfer yourself; otherwise prefer {@link upload}.
16071
+ * transfer yourself; otherwise prefer {@link upload}. A create another
16072
+ * upload of the same file superseded before its token was issued rejects
16073
+ * with code `upload_refused` (`details.coreCode` UPLOAD_SUPERSEDED): the
16074
+ * later upload is the one kept, so a new upload is no remedy.
16003
16075
  *
16004
16076
  * @example
16005
16077
  * const session = await sv.ingots.createUpload({
@@ -16087,7 +16159,10 @@ class IngotsModule {
16087
16159
  * Upload a file as a new ingot (high-level flow).
16088
16160
  *
16089
16161
  * Creates the upload session, streams the file to Forge over TUS, then
16090
- * confirms the server finalized the upload before resolving.
16162
+ * confirms the server finalized the upload before resolving. A file the
16163
+ * server refused for good — another upload of it superseded this one, at
16164
+ * its create or while it streamed, or the storage pool cannot take it —
16165
+ * rejects with code `upload_refused`, as {@link sendUpload} does.
16091
16166
  *
16092
16167
  * @example
16093
16168
  * const ingot = await sv.ingots.upload(unsealed, {
@@ -16113,7 +16188,9 @@ class IngotsModule {
16113
16188
  return this.transferAndFinalize(created, created.created, options.file, options.onProgress);
16114
16189
  }
16115
16190
  /**
16116
- * Replace an existing ingot's content (high-level flow).
16191
+ * Replace an existing ingot's content (high-level flow). Rejects as
16192
+ * {@link upload} does, a replacement another upload of the same ingot
16193
+ * superseded included (`upload_refused`).
16117
16194
  *
16118
16195
  * @example
16119
16196
  * await sv.ingots.replace(unsealed, 'ing_abc', { file: updatedFile });
@@ -16129,7 +16206,9 @@ class IngotsModule {
16129
16206
  content_type: contentType,
16130
16207
  size_bytes: options.file.size,
16131
16208
  ...(options.contentHmac && { content_hmac: options.contentHmac }),
16132
- }, { vat: vault.vatToken });
16209
+ }, { vat: vault.vatToken }).catch((error) => {
16210
+ throw stagingRefusalOf(error);
16211
+ });
16133
16212
  if (!response.data.forge_url) {
16134
16213
  throw new ValidationError('Replace endpoint did not return a Forge upload URL');
16135
16214
  }
@@ -16423,7 +16502,9 @@ class IngotsModule {
16423
16502
  payload.on_name_conflict = body.onNameConflict;
16424
16503
  }
16425
16504
  const vaultId = requireVaultId(vault);
16426
- const response = await this.http.post(apiPath `/v1/vaults/${vaultId}/ingots`, payload, { vat: vault.vatToken });
16505
+ const response = await this.http.post(apiPath `/v1/vaults/${vaultId}/ingots`, payload, { vat: vault.vatToken }).catch((error) => {
16506
+ throw stagingRefusalOf(error);
16507
+ });
16427
16508
  if (!response.data.forge_url) {
16428
16509
  throw new ValidationError('Upload endpoint did not return a Forge upload URL');
16429
16510
  }
@@ -17213,7 +17294,7 @@ class SparkVault {
17213
17294
  }
17214
17295
  /** Get the SDK version */
17215
17296
  static get version() {
17216
- return '11.0.2';
17297
+ return '11.1.0';
17217
17298
  }
17218
17299
  }
17219
17300
  if (typeof window !== 'undefined') {