@sparkvault/sdk-js 2.0.2 → 2.0.4
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/identity/api.d.ts +11 -1
- package/dist/identity/handlers/totp-handler.d.ts +8 -0
- package/dist/identity/renderer.d.ts +1 -0
- package/dist/identity/types.d.ts +26 -0
- package/dist/identity/views/index.d.ts +1 -0
- package/dist/identity/views/two-factor.d.ts +37 -0
- package/dist/sparkvault.cjs.js +327 -1
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +327 -1
- 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 +28 -0
- package/package.json +1 -1
package/dist/identity/api.d.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import type { ResolvedConfig } from '../config';
|
|
8
8
|
import { SparkVaultApi } from '../api-base';
|
|
9
9
|
import { SparkVaultError } from '../errors';
|
|
10
|
-
import type { SdkConfig, TotpSendResponse, TotpVerifyResponse, PasskeyChallengeResponse, PasskeyVerifyResponse, SparkLinkSendResponse, AuthContext } from './types';
|
|
10
|
+
import type { SdkConfig, TotpSendResponse, TotpVerifyResponse, PasskeyChallengeResponse, PasskeyVerifyResponse, SecondFactorVerifyResponse, SparkLinkSendResponse, AuthContext } from './types';
|
|
11
11
|
export declare class IdentityApi extends SparkVaultApi {
|
|
12
12
|
private readonly config;
|
|
13
13
|
/** Cached config promise - allows preloading and deduplication */
|
|
@@ -69,6 +69,16 @@ export declare class IdentityApi extends SparkVaultApi {
|
|
|
69
69
|
recipient: string;
|
|
70
70
|
authContext?: AuthContext;
|
|
71
71
|
}): Promise<TotpVerifyResponse>;
|
|
72
|
+
/**
|
|
73
|
+
* Submit a second factor (authenticator-app code or recovery code) to finish
|
|
74
|
+
* a login that the primary factor left in a `second_factor_required` state.
|
|
75
|
+
* The ticket is the opaque pending-login handle returned by the primary
|
|
76
|
+
* verify call.
|
|
77
|
+
*/
|
|
78
|
+
submitSecondFactor(params: {
|
|
79
|
+
ticket: string;
|
|
80
|
+
code: string;
|
|
81
|
+
}): Promise<SecondFactorVerifyResponse>;
|
|
72
82
|
/**
|
|
73
83
|
* Start passkey registration from a freshly verified identifier.
|
|
74
84
|
*/
|
|
@@ -31,6 +31,14 @@ export interface TotpVerifyResult {
|
|
|
31
31
|
error?: string;
|
|
32
32
|
/** Machine-readable API error code (e.g. 'identity_locked'). */
|
|
33
33
|
errorCode?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Set when the code was correct but the account has 2FA enabled — the login
|
|
36
|
+
* is not complete until a second factor is submitted against this ticket.
|
|
37
|
+
*/
|
|
38
|
+
secondFactor?: {
|
|
39
|
+
ticket: string;
|
|
40
|
+
methods: string[];
|
|
41
|
+
};
|
|
34
42
|
}
|
|
35
43
|
/**
|
|
36
44
|
* Handles TOTP code sending and verification
|
package/dist/identity/types.d.ts
CHANGED
|
@@ -168,6 +168,12 @@ export interface TotpVerifyResponse {
|
|
|
168
168
|
retry_after?: number;
|
|
169
169
|
/** Returned on failure - when the backoff expires */
|
|
170
170
|
expires_at?: number;
|
|
171
|
+
/** Set when the primary factor succeeded but 2FA is still owed */
|
|
172
|
+
second_factor_required?: boolean;
|
|
173
|
+
/** Opaque pending-login handle to submit the second factor against */
|
|
174
|
+
ticket?: string;
|
|
175
|
+
/** Second-factor methods the user can use, e.g. ['authenticator','recovery_code'] */
|
|
176
|
+
methods?: string[];
|
|
171
177
|
}
|
|
172
178
|
export interface PasskeyChallengeResponse {
|
|
173
179
|
challenge: string;
|
|
@@ -192,6 +198,21 @@ export interface PasskeyVerifyResponse {
|
|
|
192
198
|
/** Redirect URL for OIDC/simple mode flows */
|
|
193
199
|
redirect?: string;
|
|
194
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Result of completing a login via the second factor. On success this is the
|
|
203
|
+
* same shape as any verified login; a wrong code surfaces as a thrown error.
|
|
204
|
+
*/
|
|
205
|
+
export interface SecondFactorVerifyResponse {
|
|
206
|
+
verified?: boolean;
|
|
207
|
+
token?: string;
|
|
208
|
+
identity?: string;
|
|
209
|
+
identity_type?: VerifiedIdentityType;
|
|
210
|
+
svid?: string;
|
|
211
|
+
session_id?: string;
|
|
212
|
+
refresh_token?: string;
|
|
213
|
+
/** Redirect URL the caller should navigate to. */
|
|
214
|
+
redirect?: string;
|
|
215
|
+
}
|
|
195
216
|
export interface SparkLinkSendResponse {
|
|
196
217
|
sparkId: string;
|
|
197
218
|
expiresAt: number;
|
|
@@ -212,6 +233,11 @@ export type IdentityViewState = {
|
|
|
212
233
|
kindling: string;
|
|
213
234
|
error?: string;
|
|
214
235
|
backoffExpires?: number;
|
|
236
|
+
} | {
|
|
237
|
+
view: 'two-factor';
|
|
238
|
+
ticket: string;
|
|
239
|
+
methods: string[];
|
|
240
|
+
error?: string;
|
|
215
241
|
} | {
|
|
216
242
|
view: 'passkey';
|
|
217
243
|
recipient: string;
|
|
@@ -8,6 +8,7 @@ export * from './loading';
|
|
|
8
8
|
export * from './identity-input';
|
|
9
9
|
export * from './method-select';
|
|
10
10
|
export * from './totp-verify';
|
|
11
|
+
export * from './two-factor';
|
|
11
12
|
export * from './passkey';
|
|
12
13
|
export * from './passkey-prompt';
|
|
13
14
|
export * from './sparklink-waiting';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Two-Factor View
|
|
3
|
+
*
|
|
4
|
+
* Shown after a primary factor succeeds for an account with 2FA enabled.
|
|
5
|
+
* Collects a 6-digit authenticator-app code, or (if offered) a recovery code,
|
|
6
|
+
* and submits it to complete the login.
|
|
7
|
+
*/
|
|
8
|
+
import type { View } from './base';
|
|
9
|
+
export interface TwoFactorViewProps {
|
|
10
|
+
/** Second-factor methods the server offers, e.g. ['authenticator','recovery_code']. */
|
|
11
|
+
methods: string[];
|
|
12
|
+
error?: string;
|
|
13
|
+
onSubmit: (code: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare class TwoFactorView implements View {
|
|
16
|
+
private readonly props;
|
|
17
|
+
private input;
|
|
18
|
+
private submitButton;
|
|
19
|
+
private toggleButton;
|
|
20
|
+
private errorContainer;
|
|
21
|
+
private promptEl;
|
|
22
|
+
private recoveryMode;
|
|
23
|
+
private isSubmitting;
|
|
24
|
+
private focusTimeoutId;
|
|
25
|
+
private readonly boundSubmit;
|
|
26
|
+
private readonly boundToggle;
|
|
27
|
+
private readonly boundKeydown;
|
|
28
|
+
private readonly boundInput;
|
|
29
|
+
constructor(props: TwoFactorViewProps);
|
|
30
|
+
render(): HTMLElement;
|
|
31
|
+
private toggleRecovery;
|
|
32
|
+
private clearError;
|
|
33
|
+
/** Surface a server-side error and re-enable the form for another attempt. */
|
|
34
|
+
showError(message: string): void;
|
|
35
|
+
private handleSubmit;
|
|
36
|
+
destroy(): void;
|
|
37
|
+
}
|
package/dist/sparkvault.cjs.js
CHANGED
|
@@ -1068,6 +1068,18 @@ class IdentityApi extends SparkVaultApi {
|
|
|
1068
1068
|
...this.buildAuthContextParams(params.authContext),
|
|
1069
1069
|
});
|
|
1070
1070
|
}
|
|
1071
|
+
/**
|
|
1072
|
+
* Submit a second factor (authenticator-app code or recovery code) to finish
|
|
1073
|
+
* a login that the primary factor left in a `second_factor_required` state.
|
|
1074
|
+
* The ticket is the opaque pending-login handle returned by the primary
|
|
1075
|
+
* verify call.
|
|
1076
|
+
*/
|
|
1077
|
+
async submitSecondFactor(params) {
|
|
1078
|
+
return this.request('POST', '/second-factor/verify', {
|
|
1079
|
+
ticket: params.ticket,
|
|
1080
|
+
code: params.code,
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1071
1083
|
/**
|
|
1072
1084
|
* Start passkey registration from a freshly verified identifier.
|
|
1073
1085
|
*/
|
|
@@ -1812,6 +1824,14 @@ class TotpHandler {
|
|
|
1812
1824
|
recipient: this.state.recipient,
|
|
1813
1825
|
authContext: this.state.authContext,
|
|
1814
1826
|
});
|
|
1827
|
+
// Primary factor accepted, but the account requires a second factor.
|
|
1828
|
+
// Not a success and not a retry — hand the ticket up to drive the 2FA step.
|
|
1829
|
+
if (response.second_factor_required && response.ticket) {
|
|
1830
|
+
return {
|
|
1831
|
+
success: false,
|
|
1832
|
+
secondFactor: { ticket: response.ticket, methods: response.methods ?? ['authenticator'] },
|
|
1833
|
+
};
|
|
1834
|
+
}
|
|
1815
1835
|
if (response.verified && (response.token || response.redirect)) {
|
|
1816
1836
|
return {
|
|
1817
1837
|
success: true,
|
|
@@ -4560,6 +4580,144 @@ class TotpVerifyView {
|
|
|
4560
4580
|
}
|
|
4561
4581
|
}
|
|
4562
4582
|
|
|
4583
|
+
/**
|
|
4584
|
+
* Two-Factor View
|
|
4585
|
+
*
|
|
4586
|
+
* Shown after a primary factor succeeds for an account with 2FA enabled.
|
|
4587
|
+
* Collects a 6-digit authenticator-app code, or (if offered) a recovery code,
|
|
4588
|
+
* and submits it to complete the login.
|
|
4589
|
+
*/
|
|
4590
|
+
class TwoFactorView {
|
|
4591
|
+
constructor(props) {
|
|
4592
|
+
this.input = null;
|
|
4593
|
+
this.submitButton = null;
|
|
4594
|
+
this.toggleButton = null;
|
|
4595
|
+
this.errorContainer = null;
|
|
4596
|
+
this.promptEl = null;
|
|
4597
|
+
this.recoveryMode = false;
|
|
4598
|
+
this.isSubmitting = false;
|
|
4599
|
+
this.focusTimeoutId = null;
|
|
4600
|
+
this.props = props;
|
|
4601
|
+
this.boundSubmit = () => this.handleSubmit();
|
|
4602
|
+
this.boundToggle = () => this.toggleRecovery();
|
|
4603
|
+
this.boundKeydown = (e) => { if (e.key === 'Enter')
|
|
4604
|
+
this.handleSubmit(); };
|
|
4605
|
+
this.boundInput = () => this.clearError();
|
|
4606
|
+
}
|
|
4607
|
+
render() {
|
|
4608
|
+
const container = div();
|
|
4609
|
+
container.style.display = 'flex';
|
|
4610
|
+
container.style.flexDirection = 'column';
|
|
4611
|
+
const title = document.createElement('h3');
|
|
4612
|
+
title.className = 'sv-title';
|
|
4613
|
+
title.textContent = 'Two-step verification';
|
|
4614
|
+
this.promptEl = document.createElement('p');
|
|
4615
|
+
this.promptEl.className = 'sv-subtitle';
|
|
4616
|
+
this.promptEl.innerHTML = 'Enter the 6-digit code from your authenticator app.';
|
|
4617
|
+
this.errorContainer = div();
|
|
4618
|
+
if (this.props.error) {
|
|
4619
|
+
this.errorContainer.appendChild(errorMessage(this.props.error));
|
|
4620
|
+
}
|
|
4621
|
+
this.input = document.createElement('input');
|
|
4622
|
+
this.input.type = 'text';
|
|
4623
|
+
this.input.inputMode = 'numeric';
|
|
4624
|
+
this.input.autocomplete = 'one-time-code';
|
|
4625
|
+
this.input.maxLength = 6;
|
|
4626
|
+
this.input.className = 'sv-input';
|
|
4627
|
+
this.input.setAttribute('aria-label', 'Verification code');
|
|
4628
|
+
this.input.placeholder = '000000';
|
|
4629
|
+
this.input.addEventListener('keydown', this.boundKeydown);
|
|
4630
|
+
this.input.addEventListener('input', this.boundInput);
|
|
4631
|
+
this.submitButton = document.createElement('button');
|
|
4632
|
+
this.submitButton.className = 'sv-btn sv-btn-primary';
|
|
4633
|
+
this.submitButton.textContent = 'Verify';
|
|
4634
|
+
this.submitButton.addEventListener('click', this.boundSubmit);
|
|
4635
|
+
container.appendChild(title);
|
|
4636
|
+
container.appendChild(this.promptEl);
|
|
4637
|
+
container.appendChild(this.errorContainer);
|
|
4638
|
+
container.appendChild(this.input);
|
|
4639
|
+
container.appendChild(this.submitButton);
|
|
4640
|
+
if (this.props.methods?.includes('recovery_code')) {
|
|
4641
|
+
this.toggleButton = document.createElement('button');
|
|
4642
|
+
this.toggleButton.className = 'sv-back-link';
|
|
4643
|
+
this.toggleButton.textContent = 'Use a recovery code instead';
|
|
4644
|
+
this.toggleButton.addEventListener('click', this.boundToggle);
|
|
4645
|
+
container.appendChild(this.toggleButton);
|
|
4646
|
+
}
|
|
4647
|
+
this.focusTimeoutId = setTimeout(() => this.input?.focus(), 50);
|
|
4648
|
+
return container;
|
|
4649
|
+
}
|
|
4650
|
+
toggleRecovery() {
|
|
4651
|
+
if (!this.input || !this.promptEl || !this.toggleButton)
|
|
4652
|
+
return;
|
|
4653
|
+
this.recoveryMode = !this.recoveryMode;
|
|
4654
|
+
this.input.value = '';
|
|
4655
|
+
this.clearError();
|
|
4656
|
+
if (this.recoveryMode) {
|
|
4657
|
+
this.input.inputMode = 'text';
|
|
4658
|
+
this.input.removeAttribute('maxlength');
|
|
4659
|
+
this.input.placeholder = 'XXXX-XXXX-XXXX-XXXX';
|
|
4660
|
+
this.promptEl.innerHTML = 'Enter one of your recovery codes.';
|
|
4661
|
+
this.toggleButton.textContent = 'Use your authenticator app instead';
|
|
4662
|
+
}
|
|
4663
|
+
else {
|
|
4664
|
+
this.input.inputMode = 'numeric';
|
|
4665
|
+
this.input.maxLength = 6;
|
|
4666
|
+
this.input.placeholder = '000000';
|
|
4667
|
+
this.promptEl.innerHTML = 'Enter the 6-digit code from your authenticator app.';
|
|
4668
|
+
this.toggleButton.textContent = 'Use a recovery code instead';
|
|
4669
|
+
}
|
|
4670
|
+
this.input.focus();
|
|
4671
|
+
}
|
|
4672
|
+
clearError() {
|
|
4673
|
+
if (this.errorContainer)
|
|
4674
|
+
clearChildren(this.errorContainer);
|
|
4675
|
+
this.input?.classList.remove('sv-input-error');
|
|
4676
|
+
}
|
|
4677
|
+
/** Surface a server-side error and re-enable the form for another attempt. */
|
|
4678
|
+
showError(message) {
|
|
4679
|
+
if (this.errorContainer) {
|
|
4680
|
+
clearChildren(this.errorContainer);
|
|
4681
|
+
this.errorContainer.appendChild(errorMessage(message));
|
|
4682
|
+
}
|
|
4683
|
+
this.input?.classList.add('sv-input-error');
|
|
4684
|
+
this.isSubmitting = false;
|
|
4685
|
+
if (this.submitButton) {
|
|
4686
|
+
this.submitButton.disabled = false;
|
|
4687
|
+
this.submitButton.textContent = 'Verify';
|
|
4688
|
+
}
|
|
4689
|
+
this.input?.focus();
|
|
4690
|
+
this.input?.select();
|
|
4691
|
+
}
|
|
4692
|
+
handleSubmit() {
|
|
4693
|
+
if (this.isSubmitting || !this.submitButton || !this.input)
|
|
4694
|
+
return;
|
|
4695
|
+
const code = this.input.value.trim();
|
|
4696
|
+
if (!code) {
|
|
4697
|
+
this.showError(this.recoveryMode ? 'Enter a recovery code' : 'Enter the 6-digit code');
|
|
4698
|
+
return;
|
|
4699
|
+
}
|
|
4700
|
+
if (!this.recoveryMode && !/^\d{6}$/.test(code)) {
|
|
4701
|
+
this.showError('Enter the complete 6-digit code');
|
|
4702
|
+
return;
|
|
4703
|
+
}
|
|
4704
|
+
this.isSubmitting = true;
|
|
4705
|
+
this.submitButton.disabled = true;
|
|
4706
|
+
this.submitButton.textContent = 'Verifying...';
|
|
4707
|
+
this.props.onSubmit(code);
|
|
4708
|
+
}
|
|
4709
|
+
destroy() {
|
|
4710
|
+
if (this.focusTimeoutId !== null) {
|
|
4711
|
+
clearTimeout(this.focusTimeoutId);
|
|
4712
|
+
this.focusTimeoutId = null;
|
|
4713
|
+
}
|
|
4714
|
+
this.input?.removeEventListener('keydown', this.boundKeydown);
|
|
4715
|
+
this.input?.removeEventListener('input', this.boundInput);
|
|
4716
|
+
this.submitButton?.removeEventListener('click', this.boundSubmit);
|
|
4717
|
+
this.toggleButton?.removeEventListener('click', this.boundToggle);
|
|
4718
|
+
}
|
|
4719
|
+
}
|
|
4720
|
+
|
|
4563
4721
|
/**
|
|
4564
4722
|
* Passkey View
|
|
4565
4723
|
*
|
|
@@ -5728,6 +5886,12 @@ class IdentityRenderer {
|
|
|
5728
5886
|
onResend: () => this.handleTotpResend(),
|
|
5729
5887
|
onBack: () => this.showMethodSelect(),
|
|
5730
5888
|
});
|
|
5889
|
+
case 'two-factor':
|
|
5890
|
+
return new TwoFactorView({
|
|
5891
|
+
methods: state.methods,
|
|
5892
|
+
error: state.error,
|
|
5893
|
+
onSubmit: (code) => this.handleSecondFactorSubmit(code),
|
|
5894
|
+
});
|
|
5731
5895
|
case 'passkey': {
|
|
5732
5896
|
// If coming from passkey prompt with a pending result, "back" should skip registration
|
|
5733
5897
|
const { passkey } = this.verificationState;
|
|
@@ -5985,6 +6149,14 @@ class IdentityRenderer {
|
|
|
5985
6149
|
return;
|
|
5986
6150
|
const currentMethod = this.verificationState.totp.method ?? 'email';
|
|
5987
6151
|
const result = await this.totpHandler.verify(code);
|
|
6152
|
+
if (result.secondFactor) {
|
|
6153
|
+
this.setState({
|
|
6154
|
+
view: 'two-factor',
|
|
6155
|
+
ticket: result.secondFactor.ticket,
|
|
6156
|
+
methods: result.secondFactor.methods,
|
|
6157
|
+
});
|
|
6158
|
+
return;
|
|
6159
|
+
}
|
|
5988
6160
|
if (result.success && result.result) {
|
|
5989
6161
|
// Passkey upsell only when there's no redirect target — with one,
|
|
5990
6162
|
// we head to the destination immediately.
|
|
@@ -6050,6 +6222,34 @@ class IdentityRenderer {
|
|
|
6050
6222
|
});
|
|
6051
6223
|
}
|
|
6052
6224
|
}
|
|
6225
|
+
async handleSecondFactorSubmit(code) {
|
|
6226
|
+
if (this.viewState.view !== 'two-factor')
|
|
6227
|
+
return;
|
|
6228
|
+
const { ticket, methods } = this.viewState;
|
|
6229
|
+
try {
|
|
6230
|
+
const response = await this.api.submitSecondFactor({ ticket, code });
|
|
6231
|
+
const result = {
|
|
6232
|
+
token: response.token ?? '',
|
|
6233
|
+
identity: response.identity ?? this.recipient,
|
|
6234
|
+
identityType: response.identity_type ?? this.identityType,
|
|
6235
|
+
svid: response.svid,
|
|
6236
|
+
sessionId: response.session_id,
|
|
6237
|
+
refreshToken: response.refresh_token,
|
|
6238
|
+
redirect: response.redirect,
|
|
6239
|
+
};
|
|
6240
|
+
this.close();
|
|
6241
|
+
this.safeOnSuccess(result);
|
|
6242
|
+
if (typeof result.redirect === 'string' && result.redirect) {
|
|
6243
|
+
window.location.href = result.redirect;
|
|
6244
|
+
}
|
|
6245
|
+
}
|
|
6246
|
+
catch (error) {
|
|
6247
|
+
const message = error instanceof Error ? error.message : 'That code is not valid. Please try again.';
|
|
6248
|
+
// The login ticket is not consumed on a wrong code, so the user can retry
|
|
6249
|
+
// against the same ticket — re-render the view carrying the error.
|
|
6250
|
+
this.setState({ view: 'two-factor', ticket, methods, error: message });
|
|
6251
|
+
}
|
|
6252
|
+
}
|
|
6053
6253
|
async handleTotpResend() {
|
|
6054
6254
|
if (this.viewState.view !== 'totp-verify')
|
|
6055
6255
|
return;
|
|
@@ -7098,6 +7298,19 @@ function isInitiateUploadResponse(data) {
|
|
|
7098
7298
|
const d = data;
|
|
7099
7299
|
return typeof d.forge_url === 'string' && typeof d.ingot_id === 'string';
|
|
7100
7300
|
}
|
|
7301
|
+
/** tus protocol version used for the resumable upload session. */
|
|
7302
|
+
const TUS_VERSION = '1.0.0';
|
|
7303
|
+
/**
|
|
7304
|
+
* Server-side finalization confirmation poll.
|
|
7305
|
+
*
|
|
7306
|
+
* After the last chunk is PATCHed, Forge advances the session offset (and
|
|
7307
|
+
* flips the ingot to `active`) only once the chunk has actually committed to
|
|
7308
|
+
* S3. We re-read the server's *own persisted* `Upload-Offset` via a TUS HEAD
|
|
7309
|
+
* — independent of the offset the PATCH response claimed — and require it to
|
|
7310
|
+
* reach the declared total before reporting success.
|
|
7311
|
+
*/
|
|
7312
|
+
const FINALIZE_POLL_MAX = 10;
|
|
7313
|
+
const FINALIZE_POLL_MS = 1000;
|
|
7101
7314
|
class UploadApi extends SparkVaultApi {
|
|
7102
7315
|
constructor(config) {
|
|
7103
7316
|
super(config.timeout);
|
|
@@ -7169,6 +7382,104 @@ class UploadApi extends SparkVaultApi {
|
|
|
7169
7382
|
ingotId: response.data.ingot_id,
|
|
7170
7383
|
};
|
|
7171
7384
|
}
|
|
7385
|
+
/**
|
|
7386
|
+
* Confirm the upload was finalized SERVER-SIDE before reporting success.
|
|
7387
|
+
*
|
|
7388
|
+
* The TUS PATCH loop trusts a server-provided `Upload-Offset`; a buggy or
|
|
7389
|
+
* malicious Forge could return an inflated offset before every chunk was
|
|
7390
|
+
* actually committed and encrypted. This re-reads the server's own
|
|
7391
|
+
* persisted offset via a TUS HEAD (which Forge only advances on a real S3
|
|
7392
|
+
* commit) and requires it to reach the declared total — the web equivalent
|
|
7393
|
+
* of the mobile SDK's `verifyIngotActive` (status === 'active') guarantee.
|
|
7394
|
+
*
|
|
7395
|
+
* Polls because the final chunk's commit and the offset write can lag the
|
|
7396
|
+
* PATCH acknowledgement by a moment. Throws on a non-2xx HEAD, on a session
|
|
7397
|
+
* that never reaches the expected offset within the poll budget, or on a
|
|
7398
|
+
* cancelled session — never reports a half-finished upload as done.
|
|
7399
|
+
*
|
|
7400
|
+
* @param uploadUrl - The TUS session URL (Forge `Location`, ISTK-bound).
|
|
7401
|
+
* @param expectedOffset - The declared total upload size in bytes.
|
|
7402
|
+
*/
|
|
7403
|
+
async verifyUploadFinalized(uploadUrl, expectedOffset) {
|
|
7404
|
+
let lastOffset = null;
|
|
7405
|
+
for (let attempt = 1; attempt <= FINALIZE_POLL_MAX; attempt++) {
|
|
7406
|
+
if (this.isAborted()) {
|
|
7407
|
+
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
7408
|
+
}
|
|
7409
|
+
const offset = await this.headUploadOffset(uploadUrl);
|
|
7410
|
+
lastOffset = offset;
|
|
7411
|
+
if (offset >= expectedOffset) {
|
|
7412
|
+
return;
|
|
7413
|
+
}
|
|
7414
|
+
if (attempt < FINALIZE_POLL_MAX) {
|
|
7415
|
+
await new Promise(resolve => setTimeout(resolve, FINALIZE_POLL_MS));
|
|
7416
|
+
}
|
|
7417
|
+
}
|
|
7418
|
+
throw new UploadApiError('Upload could not be confirmed: the server never finalized the file. ' +
|
|
7419
|
+
`Confirmed ${lastOffset ?? 0} of ${expectedOffset} bytes.`, 'finalization_unconfirmed', 0, { confirmedBytes: lastOffset ?? 0, expectedBytes: expectedOffset });
|
|
7420
|
+
}
|
|
7421
|
+
/**
|
|
7422
|
+
* Read the server's persisted upload offset via a TUS HEAD request.
|
|
7423
|
+
*
|
|
7424
|
+
* Uses raw `fetch` rather than the JSON envelope helper: TUS responses
|
|
7425
|
+
* carry an empty body and surface state in the `Upload-Offset` header.
|
|
7426
|
+
* Honors the session abort signal and request timeout like every other
|
|
7427
|
+
* client call.
|
|
7428
|
+
*/
|
|
7429
|
+
async headUploadOffset(uploadUrl) {
|
|
7430
|
+
const closeSignal = this.getAbortSignal();
|
|
7431
|
+
const timeoutController = new AbortController();
|
|
7432
|
+
const timeoutId = setTimeout(() => timeoutController.abort(), this.timeoutMs);
|
|
7433
|
+
// Combine timeout + session-close into one signal. Mirror the base
|
|
7434
|
+
// client's fallback for browsers without AbortSignal.any: link the close
|
|
7435
|
+
// signal into the timeout controller and remove the listener in finally.
|
|
7436
|
+
let combinedSignal;
|
|
7437
|
+
let onCloseAbort = null;
|
|
7438
|
+
if ('any' in AbortSignal) {
|
|
7439
|
+
combinedSignal = AbortSignal.any([timeoutController.signal, closeSignal]);
|
|
7440
|
+
}
|
|
7441
|
+
else {
|
|
7442
|
+
combinedSignal = timeoutController.signal;
|
|
7443
|
+
onCloseAbort = () => timeoutController.abort();
|
|
7444
|
+
closeSignal.addEventListener('abort', onCloseAbort);
|
|
7445
|
+
}
|
|
7446
|
+
try {
|
|
7447
|
+
const response = await fetch(uploadUrl, {
|
|
7448
|
+
method: 'HEAD',
|
|
7449
|
+
headers: { 'Tus-Resumable': TUS_VERSION },
|
|
7450
|
+
signal: combinedSignal,
|
|
7451
|
+
});
|
|
7452
|
+
if (!response.ok) {
|
|
7453
|
+
throw new UploadApiError(`Upload finalization check failed: ${response.status}`, 'finalization_check_failed', response.status);
|
|
7454
|
+
}
|
|
7455
|
+
const header = response.headers.get('Upload-Offset');
|
|
7456
|
+
const offset = header !== null ? parseInt(header, 10) : NaN;
|
|
7457
|
+
if (!Number.isFinite(offset) || offset < 0) {
|
|
7458
|
+
throw new UploadApiError('Upload finalization check returned an invalid offset', 'finalization_check_failed', response.status);
|
|
7459
|
+
}
|
|
7460
|
+
return offset;
|
|
7461
|
+
}
|
|
7462
|
+
catch (error) {
|
|
7463
|
+
if (error instanceof SparkVaultError)
|
|
7464
|
+
throw error;
|
|
7465
|
+
if (error != null && typeof error === 'object' && 'name' in error && error.name === 'AbortError') {
|
|
7466
|
+
if (this.isAborted()) {
|
|
7467
|
+
throw new UploadApiError('Upload cancelled', 'cancelled', 0);
|
|
7468
|
+
}
|
|
7469
|
+
throw new UploadApiError(`Upload finalization check timed out after ${this.timeoutMs}ms`, 'timeout', 0);
|
|
7470
|
+
}
|
|
7471
|
+
if (error instanceof TypeError) {
|
|
7472
|
+
throw new UploadApiError('Unable to confirm upload. Please check your connection and try again.', 'network_error', 0);
|
|
7473
|
+
}
|
|
7474
|
+
throw new UploadApiError(error instanceof Error ? error.message : 'An unexpected error occurred', 'unknown_error', 0);
|
|
7475
|
+
}
|
|
7476
|
+
finally {
|
|
7477
|
+
clearTimeout(timeoutId);
|
|
7478
|
+
if (onCloseAbort) {
|
|
7479
|
+
closeSignal.removeEventListener('abort', onCloseAbort);
|
|
7480
|
+
}
|
|
7481
|
+
}
|
|
7482
|
+
}
|
|
7172
7483
|
}
|
|
7173
7484
|
|
|
7174
7485
|
/**
|
|
@@ -9591,7 +9902,13 @@ class UploadRenderer {
|
|
|
9591
9902
|
bytesUploaded: 0,
|
|
9592
9903
|
});
|
|
9593
9904
|
// Upload file using tus (resumable upload)
|
|
9594
|
-
await this.uploadWithTus(file, forgeUrl, ingotId, requestId);
|
|
9905
|
+
const uploadUrl = await this.uploadWithTus(file, forgeUrl, ingotId, requestId);
|
|
9906
|
+
// Confirm the server actually finalized the upload before celebrating.
|
|
9907
|
+
// The TUS loop trusts a server-provided offset; this re-reads the
|
|
9908
|
+
// server's own persisted offset and requires it to reach the file size,
|
|
9909
|
+
// mirroring the mobile SDK's active-status check. A non-finalized
|
|
9910
|
+
// upload throws here and never reaches the success ceremony.
|
|
9911
|
+
await this.api.verifyUploadFinalized(uploadUrl, file.size);
|
|
9595
9912
|
// Run ceremony animation
|
|
9596
9913
|
await this.runCeremony(file, ingotId, requestId);
|
|
9597
9914
|
// Complete
|
|
@@ -9672,6 +9989,7 @@ class UploadRenderer {
|
|
|
9672
9989
|
const newOffset = await this.uploadChunkWithProgress(uploadUrl, chunk, chunkStart, totalSize, TUS_VERSION, file, ingotId, requestId);
|
|
9673
9990
|
offset = newOffset;
|
|
9674
9991
|
}
|
|
9992
|
+
return uploadUrl;
|
|
9675
9993
|
}
|
|
9676
9994
|
/**
|
|
9677
9995
|
* Upload a single chunk using XMLHttpRequest for progress tracking.
|
|
@@ -9715,6 +10033,14 @@ class UploadRenderer {
|
|
|
9715
10033
|
// Get new offset from server response
|
|
9716
10034
|
const newOffsetHeader = xhr.getResponseHeader('Upload-Offset');
|
|
9717
10035
|
const newOffset = newOffsetHeader ? parseInt(newOffsetHeader, 10) : chunkStart + chunk.size;
|
|
10036
|
+
// Defense-in-depth: the server must never report more bytes accepted
|
|
10037
|
+
// than the file contains. A `newOffset > totalSize` is a malformed or
|
|
10038
|
+
// inflated response — reject it rather than letting an over-reported
|
|
10039
|
+
// offset short-circuit the upload loop and skip real chunks.
|
|
10040
|
+
if (!Number.isFinite(newOffset) || newOffset < chunkStart || newOffset > totalSize) {
|
|
10041
|
+
reject(new Error(`Server returned invalid upload offset (${newOffset})`));
|
|
10042
|
+
return;
|
|
10043
|
+
}
|
|
9718
10044
|
resolve(newOffset);
|
|
9719
10045
|
}
|
|
9720
10046
|
else {
|