@sparkvault/sdk-js 2.0.1 → 2.0.3
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/api-base.d.ts +1 -1
- package/dist/identity/api.d.ts +2 -2
- package/dist/identity/renderer.d.ts +9 -3
- package/dist/identity/types.d.ts +3 -0
- package/dist/identity/views/index.d.ts +1 -0
- package/dist/identity/views/origin-not-allowed.d.ts +25 -0
- package/dist/sparkvault.cjs.js +337 -23
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +337 -23
- package/dist/sparkvault.esm.js.map +1 -1
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/dist/vaults/upload/api.d.ts +30 -2
- package/package.json +1 -1
package/dist/sparkvault.esm.js
CHANGED
|
@@ -633,8 +633,8 @@ class SparkVaultApi {
|
|
|
633
633
|
});
|
|
634
634
|
const json = await response.json().catch(() => null);
|
|
635
635
|
if (!response.ok) {
|
|
636
|
-
const { message, code } = extractApiError(json);
|
|
637
|
-
throw this.makeError(message, code, response.status);
|
|
636
|
+
const { message, code, details } = extractApiError(json);
|
|
637
|
+
throw this.makeError(message, code, response.status, details);
|
|
638
638
|
}
|
|
639
639
|
return { data: json, status: response.status };
|
|
640
640
|
}
|
|
@@ -680,7 +680,10 @@ function extractApiError(json) {
|
|
|
680
680
|
(typeof root.message === 'string' ? root.message : null) ??
|
|
681
681
|
'Request failed';
|
|
682
682
|
const code = typeof errorObj.code === 'string' ? errorObj.code : 'api_error';
|
|
683
|
-
|
|
683
|
+
const details = typeof errorObj.details === 'object' && errorObj.details !== null
|
|
684
|
+
? errorObj.details
|
|
685
|
+
: undefined;
|
|
686
|
+
return { message, code, details };
|
|
684
687
|
}
|
|
685
688
|
|
|
686
689
|
/**
|
|
@@ -951,8 +954,8 @@ class IdentityApi extends SparkVaultApi {
|
|
|
951
954
|
get baseUrl() {
|
|
952
955
|
return `${this.config.identityBaseUrl}/${this.config.accountId}`;
|
|
953
956
|
}
|
|
954
|
-
makeError(message, code, statusCode) {
|
|
955
|
-
return new IdentityApiError(message, code, statusCode);
|
|
957
|
+
makeError(message, code, statusCode, details) {
|
|
958
|
+
return new IdentityApiError(message, code, statusCode, details);
|
|
956
959
|
}
|
|
957
960
|
async request(method, endpoint, body) {
|
|
958
961
|
const { data } = await this.rawRequest(`${this.baseUrl}${endpoint}`, {
|
|
@@ -1176,8 +1179,8 @@ class IdentityApi extends SparkVaultApi {
|
|
|
1176
1179
|
}
|
|
1177
1180
|
}
|
|
1178
1181
|
class IdentityApiError extends SparkVaultError {
|
|
1179
|
-
constructor(message, code, statusCode) {
|
|
1180
|
-
super(message, code, statusCode);
|
|
1182
|
+
constructor(message, code, statusCode, details) {
|
|
1183
|
+
super(message, code, statusCode, details);
|
|
1181
1184
|
this.name = 'IdentityApiError';
|
|
1182
1185
|
Object.setPrototypeOf(this, IdentityApiError.prototype);
|
|
1183
1186
|
}
|
|
@@ -5111,7 +5114,7 @@ class ServiceUnavailableView {
|
|
|
5111
5114
|
* presents itself as an unmistakable SparkVault notice.
|
|
5112
5115
|
*/
|
|
5113
5116
|
const ACTIVATION_URL = 'https://app.sparkvault.com/products/identity';
|
|
5114
|
-
function createBadge() {
|
|
5117
|
+
function createBadge$1() {
|
|
5115
5118
|
const badge = div('sv-not-activated-badge');
|
|
5116
5119
|
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
5117
5120
|
svg.setAttribute('viewBox', '0 0 64 64');
|
|
@@ -5165,7 +5168,7 @@ function createBadge() {
|
|
|
5165
5168
|
badge.appendChild(svg);
|
|
5166
5169
|
return badge;
|
|
5167
5170
|
}
|
|
5168
|
-
function createExternalLinkIcon() {
|
|
5171
|
+
function createExternalLinkIcon$1() {
|
|
5169
5172
|
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
5170
5173
|
svg.setAttribute('viewBox', '0 0 16 16');
|
|
5171
5174
|
svg.setAttribute('width', '14');
|
|
@@ -5181,7 +5184,7 @@ function createExternalLinkIcon() {
|
|
|
5181
5184
|
svg.appendChild(arrow);
|
|
5182
5185
|
return svg;
|
|
5183
5186
|
}
|
|
5184
|
-
function createSparkVaultMark() {
|
|
5187
|
+
function createSparkVaultMark$1() {
|
|
5185
5188
|
const mark = div('sv-not-activated-mark');
|
|
5186
5189
|
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
5187
5190
|
svg.setAttribute('viewBox', '0 0 16 16');
|
|
@@ -5207,7 +5210,7 @@ class ProductNotConfiguredView {
|
|
|
5207
5210
|
render() {
|
|
5208
5211
|
const container = div('sv-not-activated-view');
|
|
5209
5212
|
const eyebrow = div('sv-not-activated-eyebrow');
|
|
5210
|
-
eyebrow.appendChild(createSparkVaultMark());
|
|
5213
|
+
eyebrow.appendChild(createSparkVaultMark$1());
|
|
5211
5214
|
const eyebrowDivider = document.createElement('span');
|
|
5212
5215
|
eyebrowDivider.className = 'sv-not-activated-eyebrow-divider';
|
|
5213
5216
|
eyebrowDivider.textContent = '•';
|
|
@@ -5217,7 +5220,7 @@ class ProductNotConfiguredView {
|
|
|
5217
5220
|
eyebrowLabel.textContent = 'Identity';
|
|
5218
5221
|
eyebrow.appendChild(eyebrowLabel);
|
|
5219
5222
|
container.appendChild(eyebrow);
|
|
5220
|
-
const badge = createBadge();
|
|
5223
|
+
const badge = createBadge$1();
|
|
5221
5224
|
container.appendChild(badge);
|
|
5222
5225
|
const title = document.createElement('h2');
|
|
5223
5226
|
title.className = 'sv-not-activated-title';
|
|
@@ -5249,6 +5252,173 @@ class ProductNotConfiguredView {
|
|
|
5249
5252
|
const ctaLabel = document.createElement('span');
|
|
5250
5253
|
ctaLabel.textContent = 'Configure in SparkVault';
|
|
5251
5254
|
cta.appendChild(ctaLabel);
|
|
5255
|
+
cta.appendChild(createExternalLinkIcon$1());
|
|
5256
|
+
actions.appendChild(cta);
|
|
5257
|
+
this.dismissButton = document.createElement('button');
|
|
5258
|
+
this.dismissButton.className = 'sv-not-activated-dismiss';
|
|
5259
|
+
this.dismissButton.type = 'button';
|
|
5260
|
+
this.dismissButton.textContent = 'Dismiss';
|
|
5261
|
+
this.dismissButton.addEventListener('click', this.boundHandleClose);
|
|
5262
|
+
actions.appendChild(this.dismissButton);
|
|
5263
|
+
container.appendChild(actions);
|
|
5264
|
+
return container;
|
|
5265
|
+
}
|
|
5266
|
+
destroy() {
|
|
5267
|
+
if (this.dismissButton) {
|
|
5268
|
+
this.dismissButton.removeEventListener('click', this.boundHandleClose);
|
|
5269
|
+
}
|
|
5270
|
+
}
|
|
5271
|
+
}
|
|
5272
|
+
|
|
5273
|
+
/**
|
|
5274
|
+
* Origin Not Allowed View
|
|
5275
|
+
*
|
|
5276
|
+
* Full-dialog terminal screen shown when the embedding site's origin is not in
|
|
5277
|
+
* the account's verified-domains allowlist. The audience is the website owner /
|
|
5278
|
+
* developer — this is a misconfiguration surface, not an end-user error, and it
|
|
5279
|
+
* is permanent (retrying never clears an origin block) — so the dialog drops
|
|
5280
|
+
* customer branding, offers no "Try Again", names the blocked origin, and
|
|
5281
|
+
* points to the place that fixes it. Reuses the SparkVault-notice styling from
|
|
5282
|
+
* the product-not-configured view.
|
|
5283
|
+
*/
|
|
5284
|
+
const DOMAINS_URL = 'https://app.sparkvault.com/company/domains';
|
|
5285
|
+
function createBadge() {
|
|
5286
|
+
const badge = div('sv-not-activated-badge');
|
|
5287
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
5288
|
+
svg.setAttribute('viewBox', '0 0 64 64');
|
|
5289
|
+
svg.setAttribute('width', '72');
|
|
5290
|
+
svg.setAttribute('height', '72');
|
|
5291
|
+
svg.setAttribute('fill', 'none');
|
|
5292
|
+
svg.setAttribute('aria-hidden', 'true');
|
|
5293
|
+
svg.classList.add('sv-not-activated-badge-svg');
|
|
5294
|
+
const surface = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
5295
|
+
surface.setAttribute('cx', '32');
|
|
5296
|
+
surface.setAttribute('cy', '32');
|
|
5297
|
+
surface.setAttribute('r', '29.5');
|
|
5298
|
+
surface.setAttribute('fill', '#F8FAFC');
|
|
5299
|
+
surface.setAttribute('stroke', '#E2E8F0');
|
|
5300
|
+
surface.setAttribute('stroke-width', '1');
|
|
5301
|
+
svg.appendChild(surface);
|
|
5302
|
+
// Globe with a slash — an unrecognized domain.
|
|
5303
|
+
const globe = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
|
|
5304
|
+
globe.setAttribute('cx', '32');
|
|
5305
|
+
globe.setAttribute('cy', '32');
|
|
5306
|
+
globe.setAttribute('r', '13');
|
|
5307
|
+
globe.setAttribute('fill', '#FFFFFF');
|
|
5308
|
+
globe.setAttribute('stroke', '#CBD5E1');
|
|
5309
|
+
globe.setAttribute('stroke-width', '2');
|
|
5310
|
+
svg.appendChild(globe);
|
|
5311
|
+
const meridian = document.createElementNS('http://www.w3.org/2000/svg', 'ellipse');
|
|
5312
|
+
meridian.setAttribute('cx', '32');
|
|
5313
|
+
meridian.setAttribute('cy', '32');
|
|
5314
|
+
meridian.setAttribute('rx', '5.5');
|
|
5315
|
+
meridian.setAttribute('ry', '13');
|
|
5316
|
+
meridian.setAttribute('fill', 'none');
|
|
5317
|
+
meridian.setAttribute('stroke', '#CBD5E1');
|
|
5318
|
+
meridian.setAttribute('stroke-width', '2');
|
|
5319
|
+
svg.appendChild(meridian);
|
|
5320
|
+
const equator = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
5321
|
+
equator.setAttribute('d', 'M19 32H45');
|
|
5322
|
+
equator.setAttribute('stroke', '#CBD5E1');
|
|
5323
|
+
equator.setAttribute('stroke-width', '2');
|
|
5324
|
+
equator.setAttribute('stroke-linecap', 'round');
|
|
5325
|
+
svg.appendChild(equator);
|
|
5326
|
+
const slash = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
5327
|
+
slash.setAttribute('d', 'M23 23L41 41');
|
|
5328
|
+
slash.setAttribute('stroke', '#4F46E5');
|
|
5329
|
+
slash.setAttribute('stroke-width', '2.5');
|
|
5330
|
+
slash.setAttribute('stroke-linecap', 'round');
|
|
5331
|
+
svg.appendChild(slash);
|
|
5332
|
+
badge.appendChild(svg);
|
|
5333
|
+
return badge;
|
|
5334
|
+
}
|
|
5335
|
+
function createExternalLinkIcon() {
|
|
5336
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
5337
|
+
svg.setAttribute('viewBox', '0 0 16 16');
|
|
5338
|
+
svg.setAttribute('width', '14');
|
|
5339
|
+
svg.setAttribute('height', '14');
|
|
5340
|
+
svg.setAttribute('fill', 'none');
|
|
5341
|
+
svg.classList.add('sv-not-activated-cta-icon');
|
|
5342
|
+
const arrow = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
5343
|
+
arrow.setAttribute('d', 'M6 3h7v7M13 3l-8 8');
|
|
5344
|
+
arrow.setAttribute('stroke', 'currentColor');
|
|
5345
|
+
arrow.setAttribute('stroke-width', '2');
|
|
5346
|
+
arrow.setAttribute('stroke-linecap', 'round');
|
|
5347
|
+
arrow.setAttribute('stroke-linejoin', 'round');
|
|
5348
|
+
svg.appendChild(arrow);
|
|
5349
|
+
return svg;
|
|
5350
|
+
}
|
|
5351
|
+
function createSparkVaultMark() {
|
|
5352
|
+
const mark = div('sv-not-activated-mark');
|
|
5353
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
5354
|
+
svg.setAttribute('viewBox', '0 0 16 16');
|
|
5355
|
+
svg.setAttribute('width', '14');
|
|
5356
|
+
svg.setAttribute('height', '14');
|
|
5357
|
+
svg.setAttribute('fill', 'none');
|
|
5358
|
+
const flame = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
5359
|
+
flame.setAttribute('d', 'M8 1.5c.6 1.8 2 2.7 2 4.4 0 .9-.5 1.6-1.2 1.6-.6 0-1-.4-1-.9 0-.4.2-.8.2-1.2 0-.7-.5-1.1-1-1.1-1.4 0-2.5 1.5-2.5 3.4 0 2.4 1.9 4.3 4.5 4.3s4.5-1.9 4.5-4.3c0-3.6-3-4.6-5.5-6.2z');
|
|
5360
|
+
flame.setAttribute('fill', 'currentColor');
|
|
5361
|
+
svg.appendChild(flame);
|
|
5362
|
+
const label = document.createElement('span');
|
|
5363
|
+
label.textContent = 'SparkVault';
|
|
5364
|
+
mark.appendChild(svg);
|
|
5365
|
+
mark.appendChild(label);
|
|
5366
|
+
return mark;
|
|
5367
|
+
}
|
|
5368
|
+
class OriginNotAllowedView {
|
|
5369
|
+
constructor(props) {
|
|
5370
|
+
this.dismissButton = null;
|
|
5371
|
+
this.props = props;
|
|
5372
|
+
this.boundHandleClose = () => this.props.onClose();
|
|
5373
|
+
}
|
|
5374
|
+
render() {
|
|
5375
|
+
const container = div('sv-not-activated-view');
|
|
5376
|
+
const eyebrow = div('sv-not-activated-eyebrow');
|
|
5377
|
+
eyebrow.appendChild(createSparkVaultMark());
|
|
5378
|
+
const eyebrowDivider = document.createElement('span');
|
|
5379
|
+
eyebrowDivider.className = 'sv-not-activated-eyebrow-divider';
|
|
5380
|
+
eyebrowDivider.textContent = '•';
|
|
5381
|
+
eyebrow.appendChild(eyebrowDivider);
|
|
5382
|
+
const eyebrowLabel = document.createElement('span');
|
|
5383
|
+
eyebrowLabel.className = 'sv-not-activated-eyebrow-label';
|
|
5384
|
+
eyebrowLabel.textContent = 'Identity';
|
|
5385
|
+
eyebrow.appendChild(eyebrowLabel);
|
|
5386
|
+
container.appendChild(eyebrow);
|
|
5387
|
+
const badge = createBadge();
|
|
5388
|
+
container.appendChild(badge);
|
|
5389
|
+
const title = document.createElement('h2');
|
|
5390
|
+
title.className = 'sv-not-activated-title';
|
|
5391
|
+
title.id = 'sv-modal-title';
|
|
5392
|
+
title.textContent = 'This domain isn’t allowed';
|
|
5393
|
+
container.appendChild(title);
|
|
5394
|
+
const message = document.createElement('p');
|
|
5395
|
+
message.className = 'sv-not-activated-message';
|
|
5396
|
+
message.textContent =
|
|
5397
|
+
'Sign-in is blocked because this website’s domain is not on the SparkVault Identity allowlist for this account.';
|
|
5398
|
+
container.appendChild(message);
|
|
5399
|
+
const ownerCard = div('sv-not-activated-owner');
|
|
5400
|
+
const ownerLabel = document.createElement('span');
|
|
5401
|
+
ownerLabel.className = 'sv-not-activated-owner-label';
|
|
5402
|
+
ownerLabel.textContent = 'Are you the site owner?';
|
|
5403
|
+
const ownerHint = document.createElement('span');
|
|
5404
|
+
ownerHint.className = 'sv-not-activated-owner-hint';
|
|
5405
|
+
// textContent (not innerHTML) — the origin is server-reported but we never
|
|
5406
|
+
// trust it into the DOM as markup.
|
|
5407
|
+
ownerHint.textContent = this.props.origin
|
|
5408
|
+
? `Add ${this.props.origin} to your verified domains in SparkVault, then reload this page.`
|
|
5409
|
+
: 'Add this website’s domain to your verified domains in SparkVault, then reload this page.';
|
|
5410
|
+
ownerCard.appendChild(ownerLabel);
|
|
5411
|
+
ownerCard.appendChild(ownerHint);
|
|
5412
|
+
container.appendChild(ownerCard);
|
|
5413
|
+
const actions = div('sv-not-activated-actions');
|
|
5414
|
+
const cta = document.createElement('a');
|
|
5415
|
+
cta.className = 'sv-btn sv-btn-primary sv-not-activated-cta';
|
|
5416
|
+
cta.href = DOMAINS_URL;
|
|
5417
|
+
cta.target = '_blank';
|
|
5418
|
+
cta.rel = 'noopener noreferrer';
|
|
5419
|
+
const ctaLabel = document.createElement('span');
|
|
5420
|
+
ctaLabel.textContent = 'Manage domains in SparkVault';
|
|
5421
|
+
cta.appendChild(ctaLabel);
|
|
5252
5422
|
cta.appendChild(createExternalLinkIcon());
|
|
5253
5423
|
actions.appendChild(cta);
|
|
5254
5424
|
this.dismissButton = document.createElement('button');
|
|
@@ -5393,13 +5563,25 @@ class IdentityRenderer {
|
|
|
5393
5563
|
}
|
|
5394
5564
|
/**
|
|
5395
5565
|
* Map a config-fetch error to the appropriate terminal view.
|
|
5396
|
-
*
|
|
5397
|
-
*
|
|
5398
|
-
*
|
|
5566
|
+
*
|
|
5567
|
+
* Two failures are permanent developer-misconfigurations, not transient
|
|
5568
|
+
* outages, so they get dedicated terminal screens (no "Try Again" — retrying
|
|
5569
|
+
* can never clear them) instead of the generic service-unavailable view:
|
|
5570
|
+
* - `product_not_configured`: the account has not configured Identity.
|
|
5571
|
+
* - `ORIGIN_NOT_ALLOWED`: this site's domain is not on the account's
|
|
5572
|
+
* allowlist. Surfacing it as "Temporarily Unavailable / try again" sends
|
|
5573
|
+
* the developer chasing a non-existent outage.
|
|
5574
|
+
* Everything else (network errors, 5xx) is genuinely transient.
|
|
5399
5575
|
*/
|
|
5400
5576
|
viewStateForConfigError(error) {
|
|
5401
|
-
if (error instanceof IdentityApiError
|
|
5402
|
-
|
|
5577
|
+
if (error instanceof IdentityApiError) {
|
|
5578
|
+
if (error.code === 'product_not_configured') {
|
|
5579
|
+
return { view: 'product-not-configured' };
|
|
5580
|
+
}
|
|
5581
|
+
if (error.code === 'ORIGIN_NOT_ALLOWED') {
|
|
5582
|
+
const origin = typeof error.details?.origin === 'string' ? error.details.origin : undefined;
|
|
5583
|
+
return { view: 'origin-not-allowed', origin };
|
|
5584
|
+
}
|
|
5403
5585
|
}
|
|
5404
5586
|
return { view: 'service-unavailable' };
|
|
5405
5587
|
}
|
|
@@ -5489,7 +5671,8 @@ class IdentityRenderer {
|
|
|
5489
5671
|
// Full-dialog terminal screens own the entire modal; toggle the container
|
|
5490
5672
|
// chrome accordingly so the customer header doesn't bleed into a
|
|
5491
5673
|
// SparkVault-owned message.
|
|
5492
|
-
|
|
5674
|
+
const minimalChromeViews = ['product-not-configured', 'origin-not-allowed'];
|
|
5675
|
+
this.container.setMinimalChrome(minimalChromeViews.includes(this.viewState.view));
|
|
5493
5676
|
const view = this.createViewForState(this.viewState);
|
|
5494
5677
|
this.currentView = view;
|
|
5495
5678
|
clearChildren(body);
|
|
@@ -5602,6 +5785,11 @@ class IdentityRenderer {
|
|
|
5602
5785
|
return new ProductNotConfiguredView({
|
|
5603
5786
|
onClose: () => this.handleClose(),
|
|
5604
5787
|
});
|
|
5788
|
+
case 'origin-not-allowed':
|
|
5789
|
+
return new OriginNotAllowedView({
|
|
5790
|
+
origin: state.origin,
|
|
5791
|
+
onClose: () => this.handleClose(),
|
|
5792
|
+
});
|
|
5605
5793
|
}
|
|
5606
5794
|
}
|
|
5607
5795
|
showIdentityInput(error) {
|
|
@@ -6877,8 +7065,8 @@ class IdentityModule {
|
|
|
6877
7065
|
* SparkVaultError — no duplicate `httpStatus` field.
|
|
6878
7066
|
*/
|
|
6879
7067
|
class UploadApiError extends SparkVaultError {
|
|
6880
|
-
constructor(message, code, statusCode) {
|
|
6881
|
-
super(message, code, statusCode);
|
|
7068
|
+
constructor(message, code, statusCode, details) {
|
|
7069
|
+
super(message, code, statusCode, details);
|
|
6882
7070
|
this.name = 'UploadApiError';
|
|
6883
7071
|
Object.setPrototypeOf(this, UploadApiError.prototype);
|
|
6884
7072
|
}
|
|
@@ -6906,13 +7094,26 @@ function isInitiateUploadResponse(data) {
|
|
|
6906
7094
|
const d = data;
|
|
6907
7095
|
return typeof d.forge_url === 'string' && typeof d.ingot_id === 'string';
|
|
6908
7096
|
}
|
|
7097
|
+
/** tus protocol version used for the resumable upload session. */
|
|
7098
|
+
const TUS_VERSION = '1.0.0';
|
|
7099
|
+
/**
|
|
7100
|
+
* Server-side finalization confirmation poll.
|
|
7101
|
+
*
|
|
7102
|
+
* After the last chunk is PATCHed, Forge advances the session offset (and
|
|
7103
|
+
* flips the ingot to `active`) only once the chunk has actually committed to
|
|
7104
|
+
* S3. We re-read the server's *own persisted* `Upload-Offset` via a TUS HEAD
|
|
7105
|
+
* — independent of the offset the PATCH response claimed — and require it to
|
|
7106
|
+
* reach the declared total before reporting success.
|
|
7107
|
+
*/
|
|
7108
|
+
const FINALIZE_POLL_MAX = 10;
|
|
7109
|
+
const FINALIZE_POLL_MS = 1000;
|
|
6909
7110
|
class UploadApi extends SparkVaultApi {
|
|
6910
7111
|
constructor(config) {
|
|
6911
7112
|
super(config.timeout);
|
|
6912
7113
|
this.config = config;
|
|
6913
7114
|
}
|
|
6914
|
-
makeError(message, code, statusCode) {
|
|
6915
|
-
return new UploadApiError(message, code, statusCode);
|
|
7115
|
+
makeError(message, code, statusCode, details) {
|
|
7116
|
+
return new UploadApiError(message, code, statusCode, details);
|
|
6916
7117
|
}
|
|
6917
7118
|
/**
|
|
6918
7119
|
* Issue a request and unwrap the standard `{ data, meta }` envelope.
|
|
@@ -6977,6 +7178,104 @@ class UploadApi extends SparkVaultApi {
|
|
|
6977
7178
|
ingotId: response.data.ingot_id,
|
|
6978
7179
|
};
|
|
6979
7180
|
}
|
|
7181
|
+
/**
|
|
7182
|
+
* Confirm the upload was finalized SERVER-SIDE before reporting success.
|
|
7183
|
+
*
|
|
7184
|
+
* The TUS PATCH loop trusts a server-provided `Upload-Offset`; a buggy or
|
|
7185
|
+
* malicious Forge could return an inflated offset before every chunk was
|
|
7186
|
+
* actually committed and encrypted. This re-reads the server's own
|
|
7187
|
+
* persisted offset via a TUS HEAD (which Forge only advances on a real S3
|
|
7188
|
+
* commit) and requires it to reach the declared total — the web equivalent
|
|
7189
|
+
* of the mobile SDK's `verifyIngotActive` (status === 'active') guarantee.
|
|
7190
|
+
*
|
|
7191
|
+
* Polls because the final chunk's commit and the offset write can lag the
|
|
7192
|
+
* PATCH acknowledgement by a moment. Throws on a non-2xx HEAD, on a session
|
|
7193
|
+
* that never reaches the expected offset within the poll budget, or on a
|
|
7194
|
+
* cancelled session — never reports a half-finished upload as done.
|
|
7195
|
+
*
|
|
7196
|
+
* @param uploadUrl - The TUS session URL (Forge `Location`, ISTK-bound).
|
|
7197
|
+
* @param expectedOffset - The declared total upload size in bytes.
|
|
7198
|
+
*/
|
|
7199
|
+
async verifyUploadFinalized(uploadUrl, expectedOffset) {
|
|
7200
|
+
let lastOffset = null;
|
|
7201
|
+
for (let attempt = 1; attempt <= FINALIZE_POLL_MAX; attempt++) {
|
|
7202
|
+
if (this.isAborted()) {
|
|
7203
|
+
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
7204
|
+
}
|
|
7205
|
+
const offset = await this.headUploadOffset(uploadUrl);
|
|
7206
|
+
lastOffset = offset;
|
|
7207
|
+
if (offset >= expectedOffset) {
|
|
7208
|
+
return;
|
|
7209
|
+
}
|
|
7210
|
+
if (attempt < FINALIZE_POLL_MAX) {
|
|
7211
|
+
await new Promise(resolve => setTimeout(resolve, FINALIZE_POLL_MS));
|
|
7212
|
+
}
|
|
7213
|
+
}
|
|
7214
|
+
throw new UploadApiError('Upload could not be confirmed: the server never finalized the file. ' +
|
|
7215
|
+
`Confirmed ${lastOffset ?? 0} of ${expectedOffset} bytes.`, 'finalization_unconfirmed', 0, { confirmedBytes: lastOffset ?? 0, expectedBytes: expectedOffset });
|
|
7216
|
+
}
|
|
7217
|
+
/**
|
|
7218
|
+
* Read the server's persisted upload offset via a TUS HEAD request.
|
|
7219
|
+
*
|
|
7220
|
+
* Uses raw `fetch` rather than the JSON envelope helper: TUS responses
|
|
7221
|
+
* carry an empty body and surface state in the `Upload-Offset` header.
|
|
7222
|
+
* Honors the session abort signal and request timeout like every other
|
|
7223
|
+
* client call.
|
|
7224
|
+
*/
|
|
7225
|
+
async headUploadOffset(uploadUrl) {
|
|
7226
|
+
const closeSignal = this.getAbortSignal();
|
|
7227
|
+
const timeoutController = new AbortController();
|
|
7228
|
+
const timeoutId = setTimeout(() => timeoutController.abort(), this.timeoutMs);
|
|
7229
|
+
// Combine timeout + session-close into one signal. Mirror the base
|
|
7230
|
+
// client's fallback for browsers without AbortSignal.any: link the close
|
|
7231
|
+
// signal into the timeout controller and remove the listener in finally.
|
|
7232
|
+
let combinedSignal;
|
|
7233
|
+
let onCloseAbort = null;
|
|
7234
|
+
if ('any' in AbortSignal) {
|
|
7235
|
+
combinedSignal = AbortSignal.any([timeoutController.signal, closeSignal]);
|
|
7236
|
+
}
|
|
7237
|
+
else {
|
|
7238
|
+
combinedSignal = timeoutController.signal;
|
|
7239
|
+
onCloseAbort = () => timeoutController.abort();
|
|
7240
|
+
closeSignal.addEventListener('abort', onCloseAbort);
|
|
7241
|
+
}
|
|
7242
|
+
try {
|
|
7243
|
+
const response = await fetch(uploadUrl, {
|
|
7244
|
+
method: 'HEAD',
|
|
7245
|
+
headers: { 'Tus-Resumable': TUS_VERSION },
|
|
7246
|
+
signal: combinedSignal,
|
|
7247
|
+
});
|
|
7248
|
+
if (!response.ok) {
|
|
7249
|
+
throw new UploadApiError(`Upload finalization check failed: ${response.status}`, 'finalization_check_failed', response.status);
|
|
7250
|
+
}
|
|
7251
|
+
const header = response.headers.get('Upload-Offset');
|
|
7252
|
+
const offset = header !== null ? parseInt(header, 10) : NaN;
|
|
7253
|
+
if (!Number.isFinite(offset) || offset < 0) {
|
|
7254
|
+
throw new UploadApiError('Upload finalization check returned an invalid offset', 'finalization_check_failed', response.status);
|
|
7255
|
+
}
|
|
7256
|
+
return offset;
|
|
7257
|
+
}
|
|
7258
|
+
catch (error) {
|
|
7259
|
+
if (error instanceof SparkVaultError)
|
|
7260
|
+
throw error;
|
|
7261
|
+
if (error != null && typeof error === 'object' && 'name' in error && error.name === 'AbortError') {
|
|
7262
|
+
if (this.isAborted()) {
|
|
7263
|
+
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
7264
|
+
}
|
|
7265
|
+
throw new UploadApiError(`Upload finalization check timed out after ${this.timeoutMs}ms`, 'timeout', 0);
|
|
7266
|
+
}
|
|
7267
|
+
if (error instanceof TypeError) {
|
|
7268
|
+
throw new UploadApiError('Unable to confirm upload. Please check your connection and try again.', 'network_error', 0);
|
|
7269
|
+
}
|
|
7270
|
+
throw new UploadApiError(error instanceof Error ? error.message : 'An unexpected error occurred', 'unknown_error', 0);
|
|
7271
|
+
}
|
|
7272
|
+
finally {
|
|
7273
|
+
clearTimeout(timeoutId);
|
|
7274
|
+
if (onCloseAbort) {
|
|
7275
|
+
closeSignal.removeEventListener('abort', onCloseAbort);
|
|
7276
|
+
}
|
|
7277
|
+
}
|
|
7278
|
+
}
|
|
6980
7279
|
}
|
|
6981
7280
|
|
|
6982
7281
|
/**
|
|
@@ -9399,7 +9698,13 @@ class UploadRenderer {
|
|
|
9399
9698
|
bytesUploaded: 0,
|
|
9400
9699
|
});
|
|
9401
9700
|
// Upload file using tus (resumable upload)
|
|
9402
|
-
await this.uploadWithTus(file, forgeUrl, ingotId, requestId);
|
|
9701
|
+
const uploadUrl = await this.uploadWithTus(file, forgeUrl, ingotId, requestId);
|
|
9702
|
+
// Confirm the server actually finalized the upload before celebrating.
|
|
9703
|
+
// The TUS loop trusts a server-provided offset; this re-reads the
|
|
9704
|
+
// server's own persisted offset and requires it to reach the file size,
|
|
9705
|
+
// mirroring the mobile SDK's active-status check. A non-finalized
|
|
9706
|
+
// upload throws here and never reaches the success ceremony.
|
|
9707
|
+
await this.api.verifyUploadFinalized(uploadUrl, file.size);
|
|
9403
9708
|
// Run ceremony animation
|
|
9404
9709
|
await this.runCeremony(file, ingotId, requestId);
|
|
9405
9710
|
// Complete
|
|
@@ -9480,6 +9785,7 @@ class UploadRenderer {
|
|
|
9480
9785
|
const newOffset = await this.uploadChunkWithProgress(uploadUrl, chunk, chunkStart, totalSize, TUS_VERSION, file, ingotId, requestId);
|
|
9481
9786
|
offset = newOffset;
|
|
9482
9787
|
}
|
|
9788
|
+
return uploadUrl;
|
|
9483
9789
|
}
|
|
9484
9790
|
/**
|
|
9485
9791
|
* Upload a single chunk using XMLHttpRequest for progress tracking.
|
|
@@ -9523,6 +9829,14 @@ class UploadRenderer {
|
|
|
9523
9829
|
// Get new offset from server response
|
|
9524
9830
|
const newOffsetHeader = xhr.getResponseHeader('Upload-Offset');
|
|
9525
9831
|
const newOffset = newOffsetHeader ? parseInt(newOffsetHeader, 10) : chunkStart + chunk.size;
|
|
9832
|
+
// Defense-in-depth: the server must never report more bytes accepted
|
|
9833
|
+
// than the file contains. A `newOffset > totalSize` is a malformed or
|
|
9834
|
+
// inflated response — reject it rather than letting an over-reported
|
|
9835
|
+
// offset short-circuit the upload loop and skip real chunks.
|
|
9836
|
+
if (!Number.isFinite(newOffset) || newOffset < chunkStart || newOffset > totalSize) {
|
|
9837
|
+
reject(new Error(`Server returned invalid upload offset (${newOffset})`));
|
|
9838
|
+
return;
|
|
9839
|
+
}
|
|
9526
9840
|
resolve(newOffset);
|
|
9527
9841
|
}
|
|
9528
9842
|
else {
|