@sparkvault/sdk-js 2.2.4 → 4.0.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/auto-init.d.ts +1 -1
- package/dist/entropy/index.d.ts +28 -0
- package/dist/entropy/types.d.ts +32 -0
- package/dist/folders/index.d.ts +77 -0
- package/dist/folders/types.d.ts +64 -0
- package/dist/http.d.ts +3 -1
- package/dist/index.d.ts +994 -78
- package/dist/ingots/index.d.ts +183 -0
- package/dist/ingots/types.d.ts +286 -0
- package/dist/{identity → products/identity}/api.d.ts +40 -4
- package/dist/{identity → products/identity}/embedder.d.ts +1 -1
- package/dist/{identity → products/identity}/handlers/totp-handler.d.ts +1 -1
- package/dist/{identity → products/identity}/index.d.ts +7 -7
- package/dist/{identity → products/identity}/renderer.d.ts +49 -3
- package/dist/{identity → products/identity}/state.d.ts +0 -17
- package/dist/{identity → products/identity}/types.d.ts +53 -0
- package/dist/{identity → products/identity}/utils.d.ts +1 -1
- package/dist/products/identity/views/backup-verify.d.ts +41 -0
- package/dist/{identity → products/identity}/views/index.d.ts +1 -0
- package/dist/products/index.d.ts +18 -0
- package/dist/sparks/index.d.ts +65 -0
- package/dist/sparks/types.d.ts +174 -0
- package/dist/sparkvault.cjs.js +1573 -366
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +1567 -367
- package/dist/sparkvault.esm.js.map +1 -1
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/dist/vaults/index.d.ts +102 -34
- package/dist/vaults/types.d.ts +121 -57
- package/dist/vaults/upload/api.d.ts +1 -1
- package/package.json +1 -1
- /package/dist/{identity → products/identity}/container.d.ts +0 -0
- /package/dist/{identity → products/identity}/handlers/index.d.ts +0 -0
- /package/dist/{identity → products/identity}/handlers/passkey-handler.d.ts +0 -0
- /package/dist/{identity → products/identity}/handlers/sparklink-handler.d.ts +0 -0
- /package/dist/{identity → products/identity}/inline-container.d.ts +0 -0
- /package/dist/{identity → products/identity}/methods.d.ts +0 -0
- /package/dist/{identity → products/identity}/modal.d.ts +0 -0
- /package/dist/{identity → products/identity}/styles.d.ts +0 -0
- /package/dist/{identity → products/identity}/utils/cooldown-timer.d.ts +0 -0
- /package/dist/{identity → products/identity}/utils/index.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/base.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/error.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/icons.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/identity-input.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/loading.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/method-select.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/origin-not-allowed.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/passkey-prompt.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/passkey.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/product-not-configured.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/service-unavailable.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/sparklink-waiting.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/totp-verify.d.ts +0 -0
- /package/dist/{identity → products/identity}/views/two-factor.d.ts +0 -0
package/dist/sparkvault.cjs.js
CHANGED
|
@@ -341,6 +341,12 @@ class HttpClient {
|
|
|
341
341
|
post(path, body, options) {
|
|
342
342
|
return this.request(path, { ...options, method: 'POST', body });
|
|
343
343
|
}
|
|
344
|
+
put(path, body, options) {
|
|
345
|
+
return this.request(path, { ...options, method: 'PUT', body });
|
|
346
|
+
}
|
|
347
|
+
patch(path, body, options) {
|
|
348
|
+
return this.request(path, { ...options, method: 'PATCH', body });
|
|
349
|
+
}
|
|
344
350
|
delete(path, options) {
|
|
345
351
|
return this.request(path, { ...options, method: 'DELETE' });
|
|
346
352
|
}
|
|
@@ -756,83 +762,6 @@ function base64urlToString(base64url) {
|
|
|
756
762
|
* Consolidates duplicated logic from renderer, api, and index.
|
|
757
763
|
*/
|
|
758
764
|
// Re-export timer utilities
|
|
759
|
-
/**
|
|
760
|
-
* Common multi-part public suffixes where the last two labels form the TLD.
|
|
761
|
-
* Domains under these suffixes need three labels for a valid registrable domain
|
|
762
|
-
* (e.g., "app.client.co.uk" → registrable domain is "client.co.uk").
|
|
763
|
-
*/
|
|
764
|
-
const MULTI_PART_TLDS = new Set([
|
|
765
|
-
'co.uk', 'co.jp', 'co.kr', 'co.nz', 'co.za', 'co.in', 'co.id', 'co.th',
|
|
766
|
-
'com.au', 'com.br', 'com.cn', 'com.mx', 'com.sg', 'com.tw', 'com.hk',
|
|
767
|
-
'com.ar', 'com.co', 'com.my', 'com.ph', 'com.pk', 'com.tr', 'com.ua',
|
|
768
|
-
'com.vn', 'com.ng', 'com.eg', 'com.sa', 'com.pe', 'com.ec',
|
|
769
|
-
'net.au', 'net.br', 'net.cn', 'net.nz',
|
|
770
|
-
'org.au', 'org.br', 'org.cn', 'org.nz', 'org.uk',
|
|
771
|
-
'ac.uk', 'gov.uk', 'ac.jp', 'ne.jp', 'or.jp',
|
|
772
|
-
'ac.kr', 'go.kr', 'or.kr',
|
|
773
|
-
'ac.nz', 'govt.nz',
|
|
774
|
-
'ac.za', 'gov.za',
|
|
775
|
-
'ac.in', 'gov.in', 'net.in', 'org.in',
|
|
776
|
-
]);
|
|
777
|
-
/**
|
|
778
|
-
* Extract the root (registrable) domain from a hostname for cross-subdomain cookies.
|
|
779
|
-
* Returns the domain prefixed with a dot (e.g., ".client.com") or null when
|
|
780
|
-
* the domain attribute should not be set (loopback hosts, IP addresses).
|
|
781
|
-
*
|
|
782
|
-
* @param hostname - The hostname to extract the root domain from
|
|
783
|
-
* @returns Root domain with leading dot, or null
|
|
784
|
-
*/
|
|
785
|
-
function getRootDomain(hostname) {
|
|
786
|
-
// Loopback host — browsers reject domain attribute for localhost
|
|
787
|
-
if (hostname === 'localhost' || hostname === '127.0.0.1')
|
|
788
|
-
return null;
|
|
789
|
-
// IPv4 addresses
|
|
790
|
-
if (/^\d{1,3}(\.\d{1,3}){3}$/.test(hostname))
|
|
791
|
-
return null;
|
|
792
|
-
// IPv6 addresses
|
|
793
|
-
if (hostname.includes(':'))
|
|
794
|
-
return null;
|
|
795
|
-
const parts = hostname.split('.');
|
|
796
|
-
// Single-label hostnames (no dots)
|
|
797
|
-
if (parts.length <= 1)
|
|
798
|
-
return null;
|
|
799
|
-
// Two-part domain (e.g., "client.com") — already the root
|
|
800
|
-
if (parts.length === 2)
|
|
801
|
-
return `.${hostname}`;
|
|
802
|
-
// Check for multi-part TLD (e.g., "co.uk" in "app.client.co.uk")
|
|
803
|
-
const lastTwo = parts.slice(-2).join('.');
|
|
804
|
-
if (MULTI_PART_TLDS.has(lastTwo) && parts.length >= 3) {
|
|
805
|
-
return `.${parts.slice(-3).join('.')}`;
|
|
806
|
-
}
|
|
807
|
-
// Standard TLD — root domain is last two parts (e.g., "app.client.com" → ".client.com")
|
|
808
|
-
return `.${lastTwo}`;
|
|
809
|
-
}
|
|
810
|
-
/**
|
|
811
|
-
* Get cookie value by name
|
|
812
|
-
* @param name - Cookie name
|
|
813
|
-
* @returns Cookie value or null if not found
|
|
814
|
-
*/
|
|
815
|
-
function getCookie(name) {
|
|
816
|
-
const match = document.cookie.match(new RegExp(`(^| )${name}=([^;]+)`));
|
|
817
|
-
return match ? match[2] : null;
|
|
818
|
-
}
|
|
819
|
-
/**
|
|
820
|
-
* Set cookie with expiration on the root domain for cross-subdomain access.
|
|
821
|
-
* The domain is automatically extracted from the current hostname and prefixed
|
|
822
|
-
* with a dot (e.g., ".client.com") so the cookie is shared across all subdomains.
|
|
823
|
-
*
|
|
824
|
-
* @param name - Cookie name
|
|
825
|
-
* @param value - Cookie value
|
|
826
|
-
* @param days - Days until expiration
|
|
827
|
-
*/
|
|
828
|
-
function setCookie(name, value, days) {
|
|
829
|
-
const date = new Date();
|
|
830
|
-
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
831
|
-
const expires = `expires=${date.toUTCString()}`;
|
|
832
|
-
const domain = getRootDomain(window.location.hostname);
|
|
833
|
-
const domainAttr = domain ? `; domain=${domain}` : '';
|
|
834
|
-
document.cookie = `${name}=${value}; ${expires}; path=/${domainAttr}; SameSite=Lax`;
|
|
835
|
-
}
|
|
836
765
|
/**
|
|
837
766
|
* Generate cryptographically secure random state string
|
|
838
767
|
* Used for OAuth CSRF protection
|
|
@@ -1059,6 +988,42 @@ class IdentityApi extends SparkVaultApi {
|
|
|
1059
988
|
},
|
|
1060
989
|
});
|
|
1061
990
|
}
|
|
991
|
+
/**
|
|
992
|
+
* Send a one-time code to a new backup identifier (the inverse of the
|
|
993
|
+
* identifier just logged in with). Authorized by the login's verification
|
|
994
|
+
* token. Returns a pending-attach `session_id`, or `already_verified` when
|
|
995
|
+
* the identifier is already on the identity.
|
|
996
|
+
*/
|
|
997
|
+
async sendBackupVerification(params) {
|
|
998
|
+
return this.request('POST', '/backup-verification/send', {
|
|
999
|
+
verification_token: params.verificationToken,
|
|
1000
|
+
type: params.type,
|
|
1001
|
+
value: params.value,
|
|
1002
|
+
...(params.method ? { method: params.method } : {}),
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Confirm the backup-identifier code and attach it to the identity. A 409
|
|
1007
|
+
* `backup_conflict` (identifier owned by another identity) surfaces as a
|
|
1008
|
+
* thrown IdentityApiError with code `backup_conflict`.
|
|
1009
|
+
*/
|
|
1010
|
+
async verifyBackupVerification(params) {
|
|
1011
|
+
return this.request('POST', '/backup-verification/verify', {
|
|
1012
|
+
session_id: params.sessionId,
|
|
1013
|
+
code: params.code,
|
|
1014
|
+
});
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Skip a post-login recommendation, suppressing it server-side for 30 days.
|
|
1018
|
+
* Authorized by the login's verification token. Best-effort — a failure here
|
|
1019
|
+
* must not block completing the login.
|
|
1020
|
+
*/
|
|
1021
|
+
async dismissRecommendation(params) {
|
|
1022
|
+
await this.request('POST', '/recommendations/dismiss', {
|
|
1023
|
+
verification_token: params.verificationToken,
|
|
1024
|
+
key: params.key,
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1062
1027
|
/**
|
|
1063
1028
|
* Get OAuth redirect URL for social provider
|
|
1064
1029
|
*/
|
|
@@ -1114,9 +1079,32 @@ class IdentityApi extends SparkVaultApi {
|
|
|
1114
1079
|
sessionId: typeof data.session_id === 'string' ? data.session_id : undefined,
|
|
1115
1080
|
refreshToken: typeof data.refresh_token === 'string' ? data.refresh_token : undefined,
|
|
1116
1081
|
redirect: typeof data.redirect === 'string' ? data.redirect : undefined,
|
|
1082
|
+
recommendations: mapRecommendations(data.recommendations),
|
|
1117
1083
|
};
|
|
1118
1084
|
}
|
|
1119
1085
|
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Map the API's snake_case recommendation list to the SDK's typed shape,
|
|
1088
|
+
* dropping any entries the SDK does not understand. Returns undefined when
|
|
1089
|
+
* absent so callers treat it as "no nudges".
|
|
1090
|
+
*/
|
|
1091
|
+
function mapRecommendations(raw) {
|
|
1092
|
+
if (!Array.isArray(raw))
|
|
1093
|
+
return undefined;
|
|
1094
|
+
const result = [];
|
|
1095
|
+
for (const item of raw) {
|
|
1096
|
+
if (!item || typeof item !== 'object')
|
|
1097
|
+
continue;
|
|
1098
|
+
const rec = item;
|
|
1099
|
+
if (rec.type === 'passkey') {
|
|
1100
|
+
result.push({ type: 'passkey' });
|
|
1101
|
+
}
|
|
1102
|
+
else if (rec.type === 'backup_identifier' && (rec.missing_type === 'email' || rec.missing_type === 'phone')) {
|
|
1103
|
+
result.push({ type: 'backup_identifier', missingType: rec.missing_type });
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
return result;
|
|
1107
|
+
}
|
|
1120
1108
|
class IdentityApiError extends SparkVaultError {
|
|
1121
1109
|
constructor(message, code, statusCode, details) {
|
|
1122
1110
|
super(message, code, statusCode, details);
|
|
@@ -1286,12 +1274,6 @@ class VerificationState {
|
|
|
1286
1274
|
get accountId() {
|
|
1287
1275
|
return this._sdkConfig?.accountId ?? '';
|
|
1288
1276
|
}
|
|
1289
|
-
/**
|
|
1290
|
-
* Check if passkey method is enabled in config
|
|
1291
|
-
*/
|
|
1292
|
-
isPasskeyEnabled() {
|
|
1293
|
-
return this._sdkConfig?.methods?.includes('passkey') ?? false;
|
|
1294
|
-
}
|
|
1295
1277
|
// --- View State ---
|
|
1296
1278
|
get viewState() {
|
|
1297
1279
|
return this._viewState;
|
|
@@ -1336,33 +1318,6 @@ class VerificationState {
|
|
|
1336
1318
|
setPendingResult(result) {
|
|
1337
1319
|
this._passkey.pendingResult = result;
|
|
1338
1320
|
}
|
|
1339
|
-
/**
|
|
1340
|
-
* Check if we should show passkey registration prompt
|
|
1341
|
-
* Based on: passkey enabled, the current SVID doesn't have one, not dismissed
|
|
1342
|
-
*/
|
|
1343
|
-
shouldShowPasskeyPrompt() {
|
|
1344
|
-
if (!this.isPasskeyEnabled())
|
|
1345
|
-
return false;
|
|
1346
|
-
if (this.isPasskeyPromptDismissed())
|
|
1347
|
-
return false;
|
|
1348
|
-
if (this._passkey.hasPasskey === true)
|
|
1349
|
-
return false;
|
|
1350
|
-
return true;
|
|
1351
|
-
}
|
|
1352
|
-
/**
|
|
1353
|
-
* Check if passkey prompt was dismissed (30-day cookie)
|
|
1354
|
-
*/
|
|
1355
|
-
isPasskeyPromptDismissed() {
|
|
1356
|
-
const cookieName = `sv_passkey_prompt_dismissed_${this.accountId}`;
|
|
1357
|
-
return getCookie(cookieName) !== null;
|
|
1358
|
-
}
|
|
1359
|
-
/**
|
|
1360
|
-
* Dismiss passkey prompt for 30 days
|
|
1361
|
-
*/
|
|
1362
|
-
dismissPasskeyPrompt() {
|
|
1363
|
-
const cookieName = `sv_passkey_prompt_dismissed_${this.accountId}`;
|
|
1364
|
-
setCookie(cookieName, '1', 30);
|
|
1365
|
-
}
|
|
1366
1321
|
// --- TOTP State ---
|
|
1367
1322
|
get totp() {
|
|
1368
1323
|
return this._totp;
|
|
@@ -1816,6 +1771,7 @@ class TotpHandler {
|
|
|
1816
1771
|
sessionId: response.session_id,
|
|
1817
1772
|
refreshToken: response.refresh_token,
|
|
1818
1773
|
redirect: response.redirect,
|
|
1774
|
+
recommendations: mapRecommendations(response.recommendations),
|
|
1819
1775
|
},
|
|
1820
1776
|
};
|
|
1821
1777
|
}
|
|
@@ -4879,6 +4835,147 @@ class PasskeyPromptView {
|
|
|
4879
4835
|
}
|
|
4880
4836
|
}
|
|
4881
4837
|
|
|
4838
|
+
/**
|
|
4839
|
+
* Backup Verification Input View
|
|
4840
|
+
*
|
|
4841
|
+
* Post-login nudge to add the inverse identifier as a recovery backup:
|
|
4842
|
+
* collects a new phone (for an email login) or email (for a phone login),
|
|
4843
|
+
* then hands it off to send a one-time code. Includes a "Not now" skip that
|
|
4844
|
+
* suppresses this method's prompt for 30 days (handled by the renderer).
|
|
4845
|
+
*
|
|
4846
|
+
* Sibling of PasskeyPromptView — same low-friction, skippable shape.
|
|
4847
|
+
*/
|
|
4848
|
+
class BackupVerifyView {
|
|
4849
|
+
constructor(props) {
|
|
4850
|
+
this.inputElement = null;
|
|
4851
|
+
this.submitButton = null;
|
|
4852
|
+
this.skipLink = null;
|
|
4853
|
+
this.errorContainer = null;
|
|
4854
|
+
this.focusTimeoutId = null;
|
|
4855
|
+
this.props = props;
|
|
4856
|
+
this.boundHandleInput = () => this.handleInput();
|
|
4857
|
+
this.boundHandleKeyDown = (e) => {
|
|
4858
|
+
if (e.key === 'Enter') {
|
|
4859
|
+
e.preventDefault();
|
|
4860
|
+
this.handleSubmit();
|
|
4861
|
+
}
|
|
4862
|
+
};
|
|
4863
|
+
this.boundHandleSubmit = () => this.handleSubmit();
|
|
4864
|
+
this.boundHandleSkip = () => this.props.onSkip();
|
|
4865
|
+
}
|
|
4866
|
+
get isPhone() {
|
|
4867
|
+
return this.props.missingType === 'phone';
|
|
4868
|
+
}
|
|
4869
|
+
render() {
|
|
4870
|
+
const container = div('sv-email-view');
|
|
4871
|
+
const headerSection = div('sv-email-header');
|
|
4872
|
+
const title = document.createElement('h3');
|
|
4873
|
+
title.className = 'sv-title';
|
|
4874
|
+
title.textContent = this.isPhone ? 'Add a backup phone number' : 'Add a backup email';
|
|
4875
|
+
const subtitle = document.createElement('p');
|
|
4876
|
+
subtitle.className = 'sv-email-subtitle';
|
|
4877
|
+
subtitle.textContent = this.isPhone
|
|
4878
|
+
? 'Add a phone number so you can always get back into your account.'
|
|
4879
|
+
: 'Add an email so you can always get back into your account.';
|
|
4880
|
+
headerSection.appendChild(title);
|
|
4881
|
+
headerSection.appendChild(subtitle);
|
|
4882
|
+
container.appendChild(headerSection);
|
|
4883
|
+
this.errorContainer = div('sv-email-error-container');
|
|
4884
|
+
if (this.props.error) {
|
|
4885
|
+
this.errorContainer.appendChild(errorMessage(this.props.error));
|
|
4886
|
+
}
|
|
4887
|
+
container.appendChild(this.errorContainer);
|
|
4888
|
+
const formSection = div('sv-email-form');
|
|
4889
|
+
const inputWrapper = div('sv-email-input-wrapper');
|
|
4890
|
+
const label = document.createElement('label');
|
|
4891
|
+
label.className = 'sv-email-label';
|
|
4892
|
+
label.htmlFor = 'sv-backup-input';
|
|
4893
|
+
label.textContent = this.isPhone ? 'Phone number' : 'Email address';
|
|
4894
|
+
this.inputElement = document.createElement('input');
|
|
4895
|
+
this.inputElement.type = this.isPhone ? 'tel' : 'email';
|
|
4896
|
+
this.inputElement.id = 'sv-backup-input';
|
|
4897
|
+
this.inputElement.className = 'sv-email-input';
|
|
4898
|
+
this.inputElement.placeholder = this.isPhone ? '(555) 123-4567' : 'you@example.com';
|
|
4899
|
+
this.inputElement.autocomplete = this.isPhone ? 'tel' : 'email';
|
|
4900
|
+
this.inputElement.required = true;
|
|
4901
|
+
this.inputElement.maxLength = 254;
|
|
4902
|
+
this.inputElement.addEventListener('input', this.boundHandleInput);
|
|
4903
|
+
this.inputElement.addEventListener('keydown', this.boundHandleKeyDown);
|
|
4904
|
+
inputWrapper.appendChild(label);
|
|
4905
|
+
inputWrapper.appendChild(this.inputElement);
|
|
4906
|
+
formSection.appendChild(inputWrapper);
|
|
4907
|
+
this.submitButton = document.createElement('button');
|
|
4908
|
+
this.submitButton.className = 'sv-btn sv-btn-primary';
|
|
4909
|
+
this.submitButton.textContent = 'Send code';
|
|
4910
|
+
this.submitButton.addEventListener('click', this.boundHandleSubmit);
|
|
4911
|
+
formSection.appendChild(this.submitButton);
|
|
4912
|
+
this.skipLink = document.createElement('button');
|
|
4913
|
+
this.skipLink.className = 'sv-skip-link';
|
|
4914
|
+
this.skipLink.textContent = 'Not now';
|
|
4915
|
+
this.skipLink.addEventListener('click', this.boundHandleSkip);
|
|
4916
|
+
formSection.appendChild(this.skipLink);
|
|
4917
|
+
container.appendChild(formSection);
|
|
4918
|
+
this.focusTimeoutId = setTimeout(() => this.inputElement?.focus(), 50);
|
|
4919
|
+
return container;
|
|
4920
|
+
}
|
|
4921
|
+
handleInput() {
|
|
4922
|
+
this.clearError();
|
|
4923
|
+
// Live-format phone numbers as the user types (email needs no formatting).
|
|
4924
|
+
if (!this.inputElement || !this.isPhone)
|
|
4925
|
+
return;
|
|
4926
|
+
const value = this.inputElement.value;
|
|
4927
|
+
const cursorPos = this.inputElement.selectionStart ?? value.length;
|
|
4928
|
+
const formatted = formatPhoneDisplay(value);
|
|
4929
|
+
if (formatted !== value) {
|
|
4930
|
+
this.inputElement.value = formatted;
|
|
4931
|
+
const newPos = Math.min(cursorPos + (formatted.length - value.length), formatted.length);
|
|
4932
|
+
this.inputElement.setSelectionRange(newPos, newPos);
|
|
4933
|
+
}
|
|
4934
|
+
}
|
|
4935
|
+
clearError() {
|
|
4936
|
+
if (this.errorContainer)
|
|
4937
|
+
clearChildren(this.errorContainer);
|
|
4938
|
+
this.inputElement?.classList.remove('sv-email-input-error');
|
|
4939
|
+
}
|
|
4940
|
+
showError(message) {
|
|
4941
|
+
if (this.errorContainer) {
|
|
4942
|
+
clearChildren(this.errorContainer);
|
|
4943
|
+
this.errorContainer.appendChild(errorMessage(message));
|
|
4944
|
+
}
|
|
4945
|
+
this.inputElement?.classList.add('sv-email-input-error');
|
|
4946
|
+
this.inputElement?.focus();
|
|
4947
|
+
}
|
|
4948
|
+
handleSubmit() {
|
|
4949
|
+
if (!this.inputElement || !this.submitButton)
|
|
4950
|
+
return;
|
|
4951
|
+
const raw = this.inputElement.value;
|
|
4952
|
+
const normalized = this.isPhone ? validatePhone(raw) : validateEmail(raw);
|
|
4953
|
+
if (!normalized) {
|
|
4954
|
+
this.showError(this.isPhone ? 'Please enter a valid phone number' : 'Please enter a valid email address');
|
|
4955
|
+
return;
|
|
4956
|
+
}
|
|
4957
|
+
this.submitButton.disabled = true;
|
|
4958
|
+
this.submitButton.textContent = 'Please wait...';
|
|
4959
|
+
this.props.onSubmit(normalized);
|
|
4960
|
+
}
|
|
4961
|
+
destroy() {
|
|
4962
|
+
if (this.focusTimeoutId !== null) {
|
|
4963
|
+
clearTimeout(this.focusTimeoutId);
|
|
4964
|
+
this.focusTimeoutId = null;
|
|
4965
|
+
}
|
|
4966
|
+
if (this.inputElement) {
|
|
4967
|
+
this.inputElement.removeEventListener('input', this.boundHandleInput);
|
|
4968
|
+
this.inputElement.removeEventListener('keydown', this.boundHandleKeyDown);
|
|
4969
|
+
}
|
|
4970
|
+
if (this.submitButton) {
|
|
4971
|
+
this.submitButton.removeEventListener('click', this.boundHandleSubmit);
|
|
4972
|
+
}
|
|
4973
|
+
if (this.skipLink) {
|
|
4974
|
+
this.skipLink.removeEventListener('click', this.boundHandleSkip);
|
|
4975
|
+
}
|
|
4976
|
+
}
|
|
4977
|
+
}
|
|
4978
|
+
|
|
4882
4979
|
/**
|
|
4883
4980
|
* SparkLink Waiting View
|
|
4884
4981
|
*
|
|
@@ -5605,6 +5702,10 @@ class IdentityRenderer {
|
|
|
5605
5702
|
// View management
|
|
5606
5703
|
this.currentView = null;
|
|
5607
5704
|
this.focusTimeoutId = null;
|
|
5705
|
+
// Post-login recommendation pipeline: the verified result to finalize with,
|
|
5706
|
+
// and the remaining server-recommended steps to walk before finalizing.
|
|
5707
|
+
this.postLoginResult = null;
|
|
5708
|
+
this.postLoginQueue = [];
|
|
5608
5709
|
// SparkLink WebSocket
|
|
5609
5710
|
this.sparkLinkWs = null;
|
|
5610
5711
|
// SparkLink WebSocket-loss fallback: the login kindling lets the SDK reclaim
|
|
@@ -5920,6 +6021,26 @@ class IdentityRenderer {
|
|
|
5920
6021
|
onSkip: () => this.handlePasskeyPromptSkip(state.pendingResult),
|
|
5921
6022
|
error: state.error,
|
|
5922
6023
|
});
|
|
6024
|
+
case 'backup-verify':
|
|
6025
|
+
return new BackupVerifyView({
|
|
6026
|
+
missingType: state.missingType,
|
|
6027
|
+
error: state.error,
|
|
6028
|
+
onSubmit: (value) => this.handleBackupSubmit(value),
|
|
6029
|
+
onSkip: () => this.handleBackupSkip(),
|
|
6030
|
+
});
|
|
6031
|
+
case 'backup-code':
|
|
6032
|
+
return new TotpVerifyView({
|
|
6033
|
+
email: state.value,
|
|
6034
|
+
method: state.method,
|
|
6035
|
+
error: state.error,
|
|
6036
|
+
onSubmit: (code) => this.handleBackupCodeSubmit(code),
|
|
6037
|
+
onResend: () => this.handleBackupResend(),
|
|
6038
|
+
onBack: () => this.setState({
|
|
6039
|
+
view: 'backup-verify',
|
|
6040
|
+
missingType: state.missingType,
|
|
6041
|
+
pendingResult: state.pendingResult,
|
|
6042
|
+
}),
|
|
6043
|
+
});
|
|
5923
6044
|
case 'sparklink-waiting':
|
|
5924
6045
|
return new SparkLinkWaitingView({
|
|
5925
6046
|
email: state.email,
|
|
@@ -6178,23 +6299,7 @@ class IdentityRenderer {
|
|
|
6178
6299
|
return;
|
|
6179
6300
|
}
|
|
6180
6301
|
if (result.success && result.result) {
|
|
6181
|
-
|
|
6182
|
-
// we head to the destination immediately.
|
|
6183
|
-
if (!result.result.redirect && await this.shouldShowPasskeyPrompt()) {
|
|
6184
|
-
this.setState({
|
|
6185
|
-
view: 'passkey-prompt',
|
|
6186
|
-
recipient: this.recipient,
|
|
6187
|
-
pendingResult: result.result,
|
|
6188
|
-
});
|
|
6189
|
-
return;
|
|
6190
|
-
}
|
|
6191
|
-
this.close();
|
|
6192
|
-
// Hand the full result (including redirect) to the host first so the
|
|
6193
|
-
// page-level handler can act before we navigate away.
|
|
6194
|
-
this.safeOnSuccess(result.result);
|
|
6195
|
-
if (typeof result.result.redirect === 'string' && result.result.redirect) {
|
|
6196
|
-
navigateWith(this.options, result.result.redirect);
|
|
6197
|
-
}
|
|
6302
|
+
this.runPostLogin(result.result);
|
|
6198
6303
|
}
|
|
6199
6304
|
else if (result.errorCode === 'identity_locked') {
|
|
6200
6305
|
// The identity is locked down — a dedicated, actionable state, not a
|
|
@@ -6372,15 +6477,10 @@ class IdentityRenderer {
|
|
|
6372
6477
|
* User clicked "Back" or cancelled registration after being prompted.
|
|
6373
6478
|
*/
|
|
6374
6479
|
handlePasskeyRegistrationSkip() {
|
|
6375
|
-
|
|
6376
|
-
|
|
6377
|
-
|
|
6378
|
-
|
|
6379
|
-
this.safeOnSuccess(pendingResult);
|
|
6380
|
-
if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
|
|
6381
|
-
navigateWith(this.options, pendingResult.redirect);
|
|
6382
|
-
}
|
|
6383
|
-
}
|
|
6480
|
+
this.verificationState.setPendingResult(null);
|
|
6481
|
+
// Treat as skipping the passkey recommendation, then advance the pipeline.
|
|
6482
|
+
this.dismissRecommendation('passkey');
|
|
6483
|
+
this.advancePostLogin();
|
|
6384
6484
|
}
|
|
6385
6485
|
handleSocialLogin(provider) {
|
|
6386
6486
|
this.setState({ view: 'oauth-pending', provider });
|
|
@@ -6440,89 +6540,244 @@ class IdentityRenderer {
|
|
|
6440
6540
|
}
|
|
6441
6541
|
}
|
|
6442
6542
|
/**
|
|
6443
|
-
*
|
|
6543
|
+
* Run the post-login step pipeline for a completed primary login.
|
|
6544
|
+
*
|
|
6545
|
+
* The server returns an ordered `recommendations` list (passkey upsell,
|
|
6546
|
+
* backup verification, …); this walks it, rendering the step views the SDK
|
|
6547
|
+
* supports and finalizing when the queue drains. A redirect target skips the
|
|
6548
|
+
* pipeline. A future nudge is one new recommendation type + view case — no
|
|
6549
|
+
* primary-login handler changes.
|
|
6550
|
+
*/
|
|
6551
|
+
runPostLogin(result) {
|
|
6552
|
+
if (result.redirect) {
|
|
6553
|
+
this.finalizeLogin(result);
|
|
6554
|
+
return;
|
|
6555
|
+
}
|
|
6556
|
+
this.postLoginResult = result;
|
|
6557
|
+
this.postLoginQueue = [...(result.recommendations ?? [])];
|
|
6558
|
+
this.advancePostLogin();
|
|
6559
|
+
}
|
|
6560
|
+
/**
|
|
6561
|
+
* Show the next supported recommendation, or finalize when the queue is
|
|
6562
|
+
* empty. The single hand-off from each step back into the pipeline.
|
|
6444
6563
|
*/
|
|
6445
|
-
|
|
6446
|
-
|
|
6447
|
-
if (!
|
|
6448
|
-
return
|
|
6564
|
+
advancePostLogin() {
|
|
6565
|
+
const result = this.postLoginResult;
|
|
6566
|
+
if (!result)
|
|
6567
|
+
return;
|
|
6568
|
+
const next = this.postLoginQueue.shift();
|
|
6569
|
+
if (!next) {
|
|
6570
|
+
this.postLoginResult = null;
|
|
6571
|
+
this.finalizeLogin(result);
|
|
6572
|
+
return;
|
|
6449
6573
|
}
|
|
6450
|
-
|
|
6451
|
-
|
|
6452
|
-
|
|
6453
|
-
|
|
6454
|
-
|
|
6455
|
-
|
|
6574
|
+
if (next.type === 'passkey') {
|
|
6575
|
+
this.setState({ view: 'passkey-prompt', recipient: this.recipient, pendingResult: result });
|
|
6576
|
+
}
|
|
6577
|
+
else if (next.type === 'backup_identifier') {
|
|
6578
|
+
this.setState({ view: 'backup-verify', missingType: next.missingType, pendingResult: result });
|
|
6579
|
+
}
|
|
6580
|
+
else {
|
|
6581
|
+
// A recommendation type this SDK build does not render — skip it.
|
|
6582
|
+
this.advancePostLogin();
|
|
6456
6583
|
}
|
|
6457
|
-
return !passkey.hasPasskey;
|
|
6458
6584
|
}
|
|
6459
6585
|
/**
|
|
6460
|
-
*
|
|
6461
|
-
*
|
|
6586
|
+
* Best-effort: tell the server the person skipped a recommendation so it is
|
|
6587
|
+
* suppressed for 30 days. Never blocks completing the login.
|
|
6462
6588
|
*/
|
|
6463
|
-
|
|
6464
|
-
|
|
6465
|
-
|
|
6466
|
-
|
|
6589
|
+
dismissRecommendation(key) {
|
|
6590
|
+
const token = this.postLoginResult?.token;
|
|
6591
|
+
if (!token)
|
|
6592
|
+
return;
|
|
6593
|
+
void this.api.dismissRecommendation({ verificationToken: token, key }).catch(() => { });
|
|
6594
|
+
}
|
|
6595
|
+
/**
|
|
6596
|
+
* Complete the login: hand the result to the host before tearing down the
|
|
6597
|
+
* modal (so its onSuccess can act while the browsing context is alive), then
|
|
6598
|
+
* navigate any redirect the flow returned.
|
|
6599
|
+
*/
|
|
6600
|
+
finalizeLogin(result) {
|
|
6601
|
+
this.safeOnSuccess(result);
|
|
6602
|
+
this.close();
|
|
6603
|
+
if (typeof result.redirect === 'string' && result.redirect) {
|
|
6604
|
+
navigateWith(this.options, result.redirect);
|
|
6605
|
+
}
|
|
6606
|
+
}
|
|
6607
|
+
/**
|
|
6608
|
+
* Send a code to the new backup identifier the person entered. Already-attached
|
|
6609
|
+
* identifiers short-circuit straight to completion; otherwise advance to code
|
|
6610
|
+
* entry.
|
|
6611
|
+
*/
|
|
6612
|
+
async handleBackupSubmit(value) {
|
|
6613
|
+
if (this.viewState.view !== 'backup-verify')
|
|
6614
|
+
return;
|
|
6615
|
+
const { missingType, pendingResult } = this.viewState;
|
|
6467
6616
|
this.setState({ view: 'loading' });
|
|
6468
|
-
|
|
6469
|
-
|
|
6470
|
-
|
|
6471
|
-
|
|
6472
|
-
|
|
6473
|
-
|
|
6474
|
-
if (
|
|
6475
|
-
|
|
6617
|
+
try {
|
|
6618
|
+
const response = await this.api.sendBackupVerification({
|
|
6619
|
+
verificationToken: pendingResult.token,
|
|
6620
|
+
type: missingType,
|
|
6621
|
+
value,
|
|
6622
|
+
});
|
|
6623
|
+
if (response.already_verified || !response.session_id) {
|
|
6624
|
+
this.advancePostLogin();
|
|
6625
|
+
return;
|
|
6476
6626
|
}
|
|
6477
|
-
}
|
|
6478
|
-
else if (result.errorType === 'cancelled') {
|
|
6479
|
-
// User cancelled browser dialog - go back to prompt so they can skip or try again
|
|
6480
6627
|
this.setState({
|
|
6481
|
-
view: '
|
|
6482
|
-
|
|
6628
|
+
view: 'backup-code',
|
|
6629
|
+
missingType,
|
|
6630
|
+
value,
|
|
6631
|
+
method: response.method ?? (missingType === 'phone' ? 'sms' : 'email'),
|
|
6632
|
+
sessionId: response.session_id,
|
|
6483
6633
|
pendingResult,
|
|
6484
6634
|
});
|
|
6485
6635
|
}
|
|
6486
|
-
|
|
6487
|
-
// Registration failed - show error with retry option
|
|
6488
|
-
const errorMessage = result.error || 'Failed to create passkey. Please try again.';
|
|
6636
|
+
catch (error) {
|
|
6489
6637
|
this.setState({
|
|
6490
|
-
view: '
|
|
6491
|
-
|
|
6638
|
+
view: 'backup-verify',
|
|
6639
|
+
missingType,
|
|
6492
6640
|
pendingResult,
|
|
6493
|
-
error:
|
|
6641
|
+
error: error instanceof Error ? error.message : 'Could not send a code. Please try again.',
|
|
6494
6642
|
});
|
|
6495
6643
|
}
|
|
6496
6644
|
}
|
|
6497
6645
|
/**
|
|
6498
|
-
*
|
|
6646
|
+
* Verify the backup code and attach the identifier, then finish the original
|
|
6647
|
+
* login. An identifier owned by another account (409 backup_conflict) bounces
|
|
6648
|
+
* back to the input so the person can try a different one.
|
|
6499
6649
|
*/
|
|
6500
|
-
|
|
6501
|
-
|
|
6502
|
-
|
|
6503
|
-
|
|
6504
|
-
|
|
6505
|
-
|
|
6506
|
-
|
|
6507
|
-
|
|
6650
|
+
async handleBackupCodeSubmit(code) {
|
|
6651
|
+
if (this.viewState.view !== 'backup-code')
|
|
6652
|
+
return;
|
|
6653
|
+
const { missingType, value, method, sessionId, pendingResult } = this.viewState;
|
|
6654
|
+
try {
|
|
6655
|
+
await this.api.verifyBackupVerification({ sessionId, code });
|
|
6656
|
+
this.advancePostLogin();
|
|
6657
|
+
}
|
|
6658
|
+
catch (error) {
|
|
6659
|
+
const errorCode = error instanceof IdentityApiError ? error.code : '';
|
|
6660
|
+
if (errorCode === 'backup_conflict') {
|
|
6661
|
+
this.setState({
|
|
6662
|
+
view: 'backup-verify',
|
|
6663
|
+
missingType,
|
|
6664
|
+
pendingResult,
|
|
6665
|
+
error: error instanceof Error ? error.message : 'That identifier is already linked to another account.',
|
|
6666
|
+
});
|
|
6667
|
+
return;
|
|
6668
|
+
}
|
|
6669
|
+
this.setState({
|
|
6670
|
+
view: 'backup-code',
|
|
6671
|
+
missingType,
|
|
6672
|
+
value,
|
|
6673
|
+
method,
|
|
6674
|
+
sessionId,
|
|
6675
|
+
pendingResult,
|
|
6676
|
+
error: error instanceof Error ? error.message : 'That code is not valid. Please try again.',
|
|
6677
|
+
});
|
|
6508
6678
|
}
|
|
6509
6679
|
}
|
|
6510
6680
|
/**
|
|
6511
|
-
*
|
|
6512
|
-
* The server echoes back the connectionId on init; this is used to route
|
|
6513
|
-
* the SparkLink verification result back to this SDK instance.
|
|
6514
|
-
*
|
|
6515
|
-
* Returns:
|
|
6516
|
-
* - { connectionId: string } on success
|
|
6517
|
-
* - { error: 'csp' } if blocked by Content Security Policy
|
|
6518
|
-
* - { error: 'connection' } for other connection failures
|
|
6681
|
+
* Resend the backup code, refreshing the pending-attach session.
|
|
6519
6682
|
*/
|
|
6520
|
-
|
|
6521
|
-
this.
|
|
6522
|
-
|
|
6523
|
-
const
|
|
6524
|
-
|
|
6525
|
-
|
|
6683
|
+
async handleBackupResend() {
|
|
6684
|
+
if (this.viewState.view !== 'backup-code')
|
|
6685
|
+
return;
|
|
6686
|
+
const { missingType, value, method, pendingResult } = this.viewState;
|
|
6687
|
+
try {
|
|
6688
|
+
const response = await this.api.sendBackupVerification({
|
|
6689
|
+
verificationToken: pendingResult.token,
|
|
6690
|
+
type: missingType,
|
|
6691
|
+
value,
|
|
6692
|
+
method,
|
|
6693
|
+
});
|
|
6694
|
+
if (response.session_id) {
|
|
6695
|
+
this.setState({
|
|
6696
|
+
view: 'backup-code',
|
|
6697
|
+
missingType,
|
|
6698
|
+
value,
|
|
6699
|
+
method: response.method ?? method,
|
|
6700
|
+
sessionId: response.session_id,
|
|
6701
|
+
pendingResult,
|
|
6702
|
+
});
|
|
6703
|
+
}
|
|
6704
|
+
}
|
|
6705
|
+
catch {
|
|
6706
|
+
// Keep the current code view; the user can press resend again.
|
|
6707
|
+
}
|
|
6708
|
+
}
|
|
6709
|
+
/**
|
|
6710
|
+
* "Not now" on the backup prompt: suppress this identifier type for 30 days
|
|
6711
|
+
* (server-side) and advance to the next step.
|
|
6712
|
+
*/
|
|
6713
|
+
handleBackupSkip() {
|
|
6714
|
+
if (this.viewState.view !== 'backup-verify')
|
|
6715
|
+
return;
|
|
6716
|
+
const { missingType } = this.viewState;
|
|
6717
|
+
this.dismissRecommendation(missingType === 'phone' ? 'backup_phone' : 'backup_email');
|
|
6718
|
+
this.advancePostLogin();
|
|
6719
|
+
}
|
|
6720
|
+
/**
|
|
6721
|
+
* Handle user clicking "Add Passkey" in the prompt.
|
|
6722
|
+
* Directly triggers browser passkey creation without intermediate screen.
|
|
6723
|
+
*/
|
|
6724
|
+
async handlePasskeyPromptAdd(pendingResult) {
|
|
6725
|
+
// Store the pending result in case user cancels passkey registration
|
|
6726
|
+
this.verificationState.setPendingResult(pendingResult);
|
|
6727
|
+
// Show loading while browser passkey dialog is active
|
|
6728
|
+
this.setState({ view: 'loading' });
|
|
6729
|
+
// Directly trigger passkey registration
|
|
6730
|
+
const result = await this.passkeyHandler.register(pendingResult.token);
|
|
6731
|
+
if (result.success && result.result) {
|
|
6732
|
+
// Passkey created — adopt its fresh token/session as the result to
|
|
6733
|
+
// finalize with (and to authorize any later backup-attach step), then
|
|
6734
|
+
// advance to the next recommendation.
|
|
6735
|
+
this.postLoginResult = result.result;
|
|
6736
|
+
this.advancePostLogin();
|
|
6737
|
+
}
|
|
6738
|
+
else if (result.errorType === 'cancelled') {
|
|
6739
|
+
// User cancelled browser dialog - go back to prompt so they can skip or try again
|
|
6740
|
+
this.setState({
|
|
6741
|
+
view: 'passkey-prompt',
|
|
6742
|
+
recipient: this.recipient,
|
|
6743
|
+
pendingResult,
|
|
6744
|
+
});
|
|
6745
|
+
}
|
|
6746
|
+
else {
|
|
6747
|
+
// Registration failed - show error with retry option
|
|
6748
|
+
const errorMessage = result.error || 'Failed to create passkey. Please try again.';
|
|
6749
|
+
this.setState({
|
|
6750
|
+
view: 'passkey-prompt',
|
|
6751
|
+
recipient: this.recipient,
|
|
6752
|
+
pendingResult,
|
|
6753
|
+
error: errorMessage,
|
|
6754
|
+
});
|
|
6755
|
+
}
|
|
6756
|
+
}
|
|
6757
|
+
/**
|
|
6758
|
+
* Handle user clicking "Not now" / "Skip" in the passkey prompt: suppress it
|
|
6759
|
+
* for 30 days (server-side) and advance to the next step.
|
|
6760
|
+
*/
|
|
6761
|
+
handlePasskeyPromptSkip(_pendingResult) {
|
|
6762
|
+
this.dismissRecommendation('passkey');
|
|
6763
|
+
this.advancePostLogin();
|
|
6764
|
+
}
|
|
6765
|
+
/**
|
|
6766
|
+
* Open a WebSocket connection and return the connectionId.
|
|
6767
|
+
* The server echoes back the connectionId on init; this is used to route
|
|
6768
|
+
* the SparkLink verification result back to this SDK instance.
|
|
6769
|
+
*
|
|
6770
|
+
* Returns:
|
|
6771
|
+
* - { connectionId: string } on success
|
|
6772
|
+
* - { error: 'csp' } if blocked by Content Security Policy
|
|
6773
|
+
* - { error: 'connection' } for other connection failures
|
|
6774
|
+
*/
|
|
6775
|
+
openSparkLinkWebSocket() {
|
|
6776
|
+
this.cleanupSparkLink();
|
|
6777
|
+
const wsDomain = this.sdkConfig?.wsDomain || 'ws.sparkvault.com';
|
|
6778
|
+
const wsUrl = `wss://${wsDomain}`;
|
|
6779
|
+
return new Promise((resolve) => {
|
|
6780
|
+
let resolved = false;
|
|
6526
6781
|
let cspViolationDetected = false;
|
|
6527
6782
|
// Listen for CSP violations to provide a more helpful error message
|
|
6528
6783
|
const cspHandler = (event) => {
|
|
@@ -6664,27 +6919,9 @@ class IdentityRenderer {
|
|
|
6664
6919
|
if (kindling) {
|
|
6665
6920
|
void this.api.consumeSparkLinkResult(kindling).catch(() => { });
|
|
6666
6921
|
}
|
|
6667
|
-
//
|
|
6668
|
-
// a
|
|
6669
|
-
|
|
6670
|
-
this.setState({
|
|
6671
|
-
view: 'passkey-prompt',
|
|
6672
|
-
recipient: this.recipient,
|
|
6673
|
-
pendingResult: result,
|
|
6674
|
-
});
|
|
6675
|
-
return;
|
|
6676
|
-
}
|
|
6677
|
-
// Hand the full result (including redirect) to the host BEFORE tearing down
|
|
6678
|
-
// the modal, so the host's onSuccess can act (logging, navigation) without
|
|
6679
|
-
// racing the teardown of the browsing context.
|
|
6680
|
-
this.safeOnSuccess(result);
|
|
6681
|
-
this.close();
|
|
6682
|
-
// SDK-side fallback navigation. If the host already navigated this is
|
|
6683
|
-
// a no-op (the document is already unloading); if not, this rescues
|
|
6684
|
-
// simple-mode flows whose host forgot to act on result.redirect.
|
|
6685
|
-
if (typeof result.redirect === 'string' && result.redirect) {
|
|
6686
|
-
navigateWith(this.options, result.redirect);
|
|
6687
|
-
}
|
|
6922
|
+
// Run the post-login step pipeline (passkey upsell, then backup
|
|
6923
|
+
// verification). With a redirect target it finalizes immediately.
|
|
6924
|
+
this.runPostLogin(result);
|
|
6688
6925
|
}
|
|
6689
6926
|
/**
|
|
6690
6927
|
* Arm the WebSocket-loss fallback for the waiting screen.
|
|
@@ -7431,13 +7668,13 @@ function attachClickHandler(selector, onClick) {
|
|
|
7431
7668
|
* Supports passkey, TOTP, SparkLink, and social authentication.
|
|
7432
7669
|
*
|
|
7433
7670
|
* @example Dialog mode (immediate)
|
|
7434
|
-
* const result = await sv.identity.verify();
|
|
7671
|
+
* const result = await sv.products.identity.verify();
|
|
7435
7672
|
*
|
|
7436
7673
|
* @example Dialog mode (attached to clicks)
|
|
7437
|
-
* sv.identity.attach('.login-btn');
|
|
7674
|
+
* sv.products.identity.attach('.login-btn');
|
|
7438
7675
|
*
|
|
7439
7676
|
* @example Inline mode
|
|
7440
|
-
* const result = await sv.identity.verify({ target: '#auth-container' });
|
|
7677
|
+
* const result = await sv.products.identity.verify({ target: '#auth-container' });
|
|
7441
7678
|
*/
|
|
7442
7679
|
function isTokenClaims(payload) {
|
|
7443
7680
|
if (typeof payload !== 'object' || payload === null) {
|
|
@@ -7472,11 +7709,11 @@ class IdentityModule {
|
|
|
7472
7709
|
* - With `target`: Renders inline into the specified element
|
|
7473
7710
|
*
|
|
7474
7711
|
* @example Dialog mode
|
|
7475
|
-
* const result = await sv.identity.verify();
|
|
7712
|
+
* const result = await sv.products.identity.verify();
|
|
7476
7713
|
* console.log(result.token, result.identity);
|
|
7477
7714
|
*
|
|
7478
7715
|
* @example Inline mode
|
|
7479
|
-
* const result = await sv.identity.verify({
|
|
7716
|
+
* const result = await sv.products.identity.verify({
|
|
7480
7717
|
* target: '#auth-container',
|
|
7481
7718
|
* email: 'user@example.com'
|
|
7482
7719
|
* });
|
|
@@ -7576,7 +7813,7 @@ class IdentityModule {
|
|
|
7576
7813
|
* @returns Cleanup function to remove event listeners
|
|
7577
7814
|
*
|
|
7578
7815
|
* @example
|
|
7579
|
-
* const cleanup = sv.identity.attach('.login-btn', {
|
|
7816
|
+
* const cleanup = sv.products.identity.attach('.login-btn', {
|
|
7580
7817
|
* onSuccess: (result) => console.log('Verified:', result.identity)
|
|
7581
7818
|
* });
|
|
7582
7819
|
*
|
|
@@ -7781,6 +8018,19 @@ class IdentityModule {
|
|
|
7781
8018
|
}
|
|
7782
8019
|
}
|
|
7783
8020
|
|
|
8021
|
+
/**
|
|
8022
|
+
* Products Module
|
|
8023
|
+
*
|
|
8024
|
+
* Namespace for SparkVault first-party products. Each product is a
|
|
8025
|
+
* self-contained surface (UI + API) the account can enable independently.
|
|
8026
|
+
* Currently exposes `identity`; future products attach here alongside it.
|
|
8027
|
+
*/
|
|
8028
|
+
class ProductsModule {
|
|
8029
|
+
constructor(config) {
|
|
8030
|
+
this.identity = new IdentityModule(config);
|
|
8031
|
+
}
|
|
8032
|
+
}
|
|
8033
|
+
|
|
7784
8034
|
/**
|
|
7785
8035
|
* Upload API
|
|
7786
8036
|
*
|
|
@@ -7917,7 +8167,7 @@ class UploadApi extends SparkVaultApi {
|
|
|
7917
8167
|
* Upload a file to Forge over the TUS resumable protocol (v1.0.0).
|
|
7918
8168
|
*
|
|
7919
8169
|
* The single canonical browser transfer path — the upload widget and the
|
|
7920
|
-
* authenticated `
|
|
8170
|
+
* authenticated `ingots.upload` flow both go through here (CLAUDE.md: one
|
|
7921
8171
|
* transfer path). Creates the session, PATCHes chunks sized by the server's
|
|
7922
8172
|
* `X-Chunk-Size` (50MB default, which keeps every request under CDN body
|
|
7923
8173
|
* caps such as Cloudflare's 100MB), validates every server-reported offset,
|
|
@@ -10870,15 +11120,15 @@ class VaultUploadModule {
|
|
|
10870
11120
|
/**
|
|
10871
11121
|
* Vaults Module
|
|
10872
11122
|
*
|
|
10873
|
-
* Create and manage encrypted vaults
|
|
10874
|
-
*
|
|
11123
|
+
* Create and manage encrypted vaults (the control plane). Uses Triple
|
|
11124
|
+
* Zero-Trust encryption (SVMK + AMK + VMK). Ingot, folder, spark, and entropy
|
|
11125
|
+
* operations live in their own modules; this module owns the vault record,
|
|
11126
|
+
* its sharing/upload configuration, and the browser upload widget.
|
|
10875
11127
|
*/
|
|
10876
11128
|
class VaultsModule {
|
|
10877
11129
|
constructor(config, http) {
|
|
10878
|
-
this.config = config;
|
|
10879
11130
|
this.http = http;
|
|
10880
11131
|
this.uploadModule = new VaultUploadModule(config);
|
|
10881
|
-
this.uploadApi = new UploadApi(config);
|
|
10882
11132
|
// Create callable that also has attach/close/pop/render methods
|
|
10883
11133
|
const uploadFn = ((options) => this.uploadModule.upload(options));
|
|
10884
11134
|
uploadFn.attach = (selector, options) => this.uploadModule.attach(selector, options);
|
|
@@ -10899,14 +11149,12 @@ class VaultsModule {
|
|
|
10899
11149
|
this.validateCreateOptions(options);
|
|
10900
11150
|
const response = await this.http.post('/v1/vaults', {
|
|
10901
11151
|
name: options.name,
|
|
10902
|
-
hosted_vmk: options.hostedVmk ?? false,
|
|
10903
11152
|
});
|
|
10904
11153
|
return {
|
|
10905
11154
|
id: response.data.vault_id,
|
|
10906
11155
|
name: response.data.name,
|
|
10907
11156
|
vmk: response.data.vmk,
|
|
10908
11157
|
createdAt: response.data.created_at,
|
|
10909
|
-
hostedVmk: response.data.hosted_vmk,
|
|
10910
11158
|
};
|
|
10911
11159
|
}
|
|
10912
11160
|
/**
|
|
@@ -10915,16 +11163,13 @@ class VaultsModule {
|
|
|
10915
11163
|
*
|
|
10916
11164
|
* @example
|
|
10917
11165
|
* const unsealed = await sv.vaults.unseal('vlt_abc', vmk);
|
|
10918
|
-
* console.log('
|
|
11166
|
+
* console.log('Token:', unsealed.vatToken);
|
|
10919
11167
|
*/
|
|
10920
11168
|
async unseal(vaultId, vmk) {
|
|
10921
|
-
|
|
10922
|
-
throw new ValidationError('Vault ID is required');
|
|
10923
|
-
}
|
|
11169
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
10924
11170
|
if (!vmk) {
|
|
10925
11171
|
throw new ValidationError('VMK is required');
|
|
10926
11172
|
}
|
|
10927
|
-
const cleanId = vaultId.startsWith('vlt_') ? vaultId : `vlt_${vaultId}`;
|
|
10928
11173
|
const response = await this.http.post(`/v1/vaults/${cleanId}/unseal`, { vmk });
|
|
10929
11174
|
if (!response.data.vat) {
|
|
10930
11175
|
throw new ValidationError('Unseal response did not include a vault access token');
|
|
@@ -10952,120 +11197,243 @@ class VaultsModule {
|
|
|
10952
11197
|
createdAt: v.created_at,
|
|
10953
11198
|
ingotCount: v.ingot_count,
|
|
10954
11199
|
storageBytes: v.storage_bytes,
|
|
10955
|
-
hostedVmk: v.hosted_vmk,
|
|
10956
11200
|
}));
|
|
10957
11201
|
}
|
|
10958
11202
|
/**
|
|
10959
|
-
*
|
|
10960
|
-
*
|
|
11203
|
+
* Get a single vault's detail.
|
|
11204
|
+
*
|
|
11205
|
+
* @example
|
|
11206
|
+
* const vault = await sv.vaults.get('vlt_abc');
|
|
11207
|
+
* console.log(vault.status, vault.ingotCount);
|
|
11208
|
+
*/
|
|
11209
|
+
async get(vaultId) {
|
|
11210
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11211
|
+
const response = await this.http.get(`/v1/vaults/${cleanId}`);
|
|
11212
|
+
const v = response.data;
|
|
11213
|
+
return {
|
|
11214
|
+
id: v.vault_id,
|
|
11215
|
+
name: v.name,
|
|
11216
|
+
description: v.description ?? null,
|
|
11217
|
+
status: v.status,
|
|
11218
|
+
ingotCount: v.ingot_count ?? 0,
|
|
11219
|
+
storageBytes: v.storage_bytes ?? 0,
|
|
11220
|
+
createdAt: v.created_at,
|
|
11221
|
+
lastUnsealedAt: v.last_unsealed_at ?? null,
|
|
11222
|
+
unsealCount: v.unseal_count ?? 0,
|
|
11223
|
+
};
|
|
11224
|
+
}
|
|
11225
|
+
/**
|
|
11226
|
+
* Rename a vault.
|
|
11227
|
+
*
|
|
11228
|
+
* @example
|
|
11229
|
+
* await sv.vaults.update('vlt_abc', { name: 'Renamed Vault' });
|
|
11230
|
+
*/
|
|
11231
|
+
async update(vaultId, options) {
|
|
11232
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11233
|
+
if (!options.name?.trim()) {
|
|
11234
|
+
throw new ValidationError('Vault name is required');
|
|
11235
|
+
}
|
|
11236
|
+
const response = await this.http.put(`/v1/vaults/${cleanId}`, {
|
|
11237
|
+
name: options.name,
|
|
11238
|
+
});
|
|
11239
|
+
const v = response.data;
|
|
11240
|
+
return {
|
|
11241
|
+
id: v.vault_id,
|
|
11242
|
+
name: v.name,
|
|
11243
|
+
description: v.description ?? null,
|
|
11244
|
+
updatedAt: v.updated_at,
|
|
11245
|
+
};
|
|
11246
|
+
}
|
|
11247
|
+
/**
|
|
11248
|
+
* Seal a vault, revoking all active access tokens.
|
|
11249
|
+
*
|
|
11250
|
+
* @example
|
|
11251
|
+
* await sv.vaults.seal('vlt_abc');
|
|
11252
|
+
*/
|
|
11253
|
+
async seal(vaultId) {
|
|
11254
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11255
|
+
await this.http.post(`/v1/vaults/${cleanId}/seal`);
|
|
11256
|
+
}
|
|
11257
|
+
/**
|
|
11258
|
+
* Delete a vault and all its ingots. This action is irreversible!
|
|
10961
11259
|
*
|
|
10962
11260
|
* @example
|
|
10963
11261
|
* await sv.vaults.delete('vlt_abc', vmk);
|
|
10964
11262
|
*/
|
|
10965
11263
|
async delete(vaultId, vmk) {
|
|
10966
|
-
const cleanId =
|
|
11264
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
10967
11265
|
await this.http.delete(`/v1/vaults/${cleanId}`, {
|
|
10968
11266
|
headers: { 'X-VMK': vmk },
|
|
10969
11267
|
});
|
|
10970
11268
|
}
|
|
10971
11269
|
/**
|
|
10972
|
-
*
|
|
11270
|
+
* Get a vault's public-sharing configuration.
|
|
10973
11271
|
*
|
|
10974
11272
|
* @example
|
|
10975
|
-
* const
|
|
10976
|
-
* file: myFile,
|
|
10977
|
-
* name: 'document.pdf'
|
|
10978
|
-
* });
|
|
11273
|
+
* const sharing = await sv.vaults.getSharingConfig('vlt_abc');
|
|
10979
11274
|
*/
|
|
10980
|
-
async
|
|
10981
|
-
this.
|
|
10982
|
-
const
|
|
10983
|
-
|
|
10984
|
-
|
|
10985
|
-
|
|
10986
|
-
|
|
10987
|
-
|
|
10988
|
-
|
|
10989
|
-
|
|
10990
|
-
|
|
10991
|
-
|
|
10992
|
-
|
|
10993
|
-
|
|
11275
|
+
async getSharingConfig(vaultId) {
|
|
11276
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11277
|
+
const response = await this.http.get(`/v1/vaults/${cleanId}/sharing`);
|
|
11278
|
+
return mapSharingConfig$1(response.data);
|
|
11279
|
+
}
|
|
11280
|
+
/**
|
|
11281
|
+
* Enable public sharing for a vault.
|
|
11282
|
+
*
|
|
11283
|
+
* @example
|
|
11284
|
+
* await sv.vaults.enableSharing('vlt_abc', vmk, { sessionLengthSeconds: 3600 });
|
|
11285
|
+
*/
|
|
11286
|
+
async enableSharing(vaultId, vmk, options = {}) {
|
|
11287
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11288
|
+
if (!vmk) {
|
|
11289
|
+
throw new ValidationError('VMK is required');
|
|
10994
11290
|
}
|
|
10995
|
-
const
|
|
10996
|
-
|
|
10997
|
-
|
|
10998
|
-
|
|
10999
|
-
|
|
11291
|
+
const response = await this.http.post(`/v1/vaults/${cleanId}/sharing/enable`, {
|
|
11292
|
+
vmk,
|
|
11293
|
+
all_ingots_public: options.allIngotsPublic,
|
|
11294
|
+
session_length_seconds: options.sessionLengthSeconds,
|
|
11295
|
+
});
|
|
11296
|
+
return mapSharingConfig$1(response.data);
|
|
11297
|
+
}
|
|
11298
|
+
/**
|
|
11299
|
+
* Disable public sharing for a vault.
|
|
11300
|
+
*
|
|
11301
|
+
* @example
|
|
11302
|
+
* await sv.vaults.disableSharing('vlt_abc');
|
|
11303
|
+
*/
|
|
11304
|
+
async disableSharing(vaultId) {
|
|
11305
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11306
|
+
const response = await this.http.post(`/v1/vaults/${cleanId}/sharing/disable`);
|
|
11307
|
+
return mapSharingConfig$1(response.data);
|
|
11308
|
+
}
|
|
11309
|
+
/**
|
|
11310
|
+
* Update a vault's sharing configuration (without changing enabled state).
|
|
11311
|
+
*
|
|
11312
|
+
* @example
|
|
11313
|
+
* await sv.vaults.updateSharingConfig('vlt_abc', { allIngotsPublic: true });
|
|
11314
|
+
*/
|
|
11315
|
+
async updateSharingConfig(vaultId, config) {
|
|
11316
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11317
|
+
const response = await this.http.put(`/v1/vaults/${cleanId}/sharing`, {
|
|
11318
|
+
all_ingots_public: config.allIngotsPublic,
|
|
11319
|
+
session_length_seconds: config.sessionLengthSeconds,
|
|
11320
|
+
});
|
|
11321
|
+
return mapSharingConfig$1(response.data);
|
|
11322
|
+
}
|
|
11323
|
+
/**
|
|
11324
|
+
* Get a vault's access-log retention window.
|
|
11325
|
+
*
|
|
11326
|
+
* @example
|
|
11327
|
+
* const { retentionSeconds } = await sv.vaults.getAccessLogRetention('vlt_abc');
|
|
11328
|
+
*/
|
|
11329
|
+
async getAccessLogRetention(vaultId) {
|
|
11330
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11331
|
+
const response = await this.http.get(`/v1/vaults/${cleanId}/access-log-retention`);
|
|
11000
11332
|
return {
|
|
11001
|
-
|
|
11002
|
-
|
|
11003
|
-
contentType: created.content_type,
|
|
11004
|
-
size: created.size_bytes,
|
|
11005
|
-
uploadExpiresAt: created.expires_at,
|
|
11333
|
+
vaultId: response.data.vault_id,
|
|
11334
|
+
retentionSeconds: response.data.access_log_retention_seconds,
|
|
11006
11335
|
};
|
|
11007
11336
|
}
|
|
11008
11337
|
/**
|
|
11009
|
-
*
|
|
11338
|
+
* Update a vault's access-log retention window.
|
|
11010
11339
|
*
|
|
11011
11340
|
* @example
|
|
11012
|
-
*
|
|
11013
|
-
* const url = URL.createObjectURL(blob);
|
|
11341
|
+
* await sv.vaults.updateAccessLogRetention('vlt_abc', 7776000);
|
|
11014
11342
|
*/
|
|
11015
|
-
async
|
|
11016
|
-
const cleanId =
|
|
11017
|
-
|
|
11018
|
-
|
|
11019
|
-
|
|
11020
|
-
|
|
11021
|
-
|
|
11022
|
-
});
|
|
11023
|
-
if (!response.data.download_url) {
|
|
11024
|
-
throw new ValidationError('Download endpoint did not return a download URL');
|
|
11025
|
-
}
|
|
11026
|
-
this.validateDownloadUrl(response.data.download_url);
|
|
11027
|
-
return await fetchBlobWithTimeout(response.data.download_url);
|
|
11028
|
-
}
|
|
11029
|
-
catch (err) {
|
|
11030
|
-
lastError = err instanceof Error ? err : new Error(String(err));
|
|
11031
|
-
if (!isRetryableDownloadError(lastError) || attempt === DOWNLOAD_MAX_ATTEMPTS) {
|
|
11032
|
-
throw lastError;
|
|
11033
|
-
}
|
|
11034
|
-
await delayExponential(attempt);
|
|
11035
|
-
}
|
|
11036
|
-
}
|
|
11037
|
-
throw lastError ?? new Error('Download failed');
|
|
11343
|
+
async updateAccessLogRetention(vaultId, seconds) {
|
|
11344
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11345
|
+
const response = await this.http.put(`/v1/vaults/${cleanId}/access-log-retention`, { access_log_retention_seconds: seconds });
|
|
11346
|
+
return {
|
|
11347
|
+
vaultId: response.data.vault_id,
|
|
11348
|
+
retentionSeconds: response.data.access_log_retention_seconds,
|
|
11349
|
+
};
|
|
11038
11350
|
}
|
|
11039
11351
|
/**
|
|
11040
|
-
*
|
|
11352
|
+
* Get a vault's upload-channel configuration.
|
|
11041
11353
|
*
|
|
11042
11354
|
* @example
|
|
11043
|
-
* const
|
|
11044
|
-
* ingots.forEach(i => console.log(i.name, i.size));
|
|
11355
|
+
* const upload = await sv.vaults.getUploadConfig('vlt_abc');
|
|
11045
11356
|
*/
|
|
11046
|
-
async
|
|
11047
|
-
const
|
|
11048
|
-
|
|
11049
|
-
|
|
11050
|
-
return response.data.ingots.map((i) => ({
|
|
11051
|
-
id: i.ingot_id,
|
|
11052
|
-
name: i.name,
|
|
11053
|
-
contentType: i.content_type,
|
|
11054
|
-
size: i.size_bytes,
|
|
11055
|
-
createdAt: i.created_at,
|
|
11056
|
-
}));
|
|
11357
|
+
async getUploadConfig(vaultId) {
|
|
11358
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11359
|
+
const response = await this.http.get(`/v1/vaults/${cleanId}/upload`);
|
|
11360
|
+
return mapUploadSettings(response.data);
|
|
11057
11361
|
}
|
|
11058
11362
|
/**
|
|
11059
|
-
*
|
|
11363
|
+
* Enable the public upload portal for a vault.
|
|
11060
11364
|
*
|
|
11061
11365
|
* @example
|
|
11062
|
-
* await sv.vaults.
|
|
11366
|
+
* await sv.vaults.enableUploadPortal('vlt_abc', vmk);
|
|
11063
11367
|
*/
|
|
11064
|
-
async
|
|
11065
|
-
|
|
11066
|
-
|
|
11067
|
-
|
|
11068
|
-
|
|
11368
|
+
async enableUploadPortal(vaultId, vmk) {
|
|
11369
|
+
return this.toggleUploadChannel(vaultId, 'portal', 'enable', vmk);
|
|
11370
|
+
}
|
|
11371
|
+
/**
|
|
11372
|
+
* Disable the public upload portal for a vault.
|
|
11373
|
+
*
|
|
11374
|
+
* @example
|
|
11375
|
+
* await sv.vaults.disableUploadPortal('vlt_abc');
|
|
11376
|
+
*/
|
|
11377
|
+
async disableUploadPortal(vaultId) {
|
|
11378
|
+
return this.toggleUploadChannel(vaultId, 'portal', 'disable');
|
|
11379
|
+
}
|
|
11380
|
+
/**
|
|
11381
|
+
* Enable the upload widget for a vault.
|
|
11382
|
+
*
|
|
11383
|
+
* @example
|
|
11384
|
+
* await sv.vaults.enableUploadWidget('vlt_abc', vmk);
|
|
11385
|
+
*/
|
|
11386
|
+
async enableUploadWidget(vaultId, vmk) {
|
|
11387
|
+
return this.toggleUploadChannel(vaultId, 'widget', 'enable', vmk);
|
|
11388
|
+
}
|
|
11389
|
+
/**
|
|
11390
|
+
* Disable the upload widget for a vault.
|
|
11391
|
+
*
|
|
11392
|
+
* @example
|
|
11393
|
+
* await sv.vaults.disableUploadWidget('vlt_abc');
|
|
11394
|
+
*/
|
|
11395
|
+
async disableUploadWidget(vaultId) {
|
|
11396
|
+
return this.toggleUploadChannel(vaultId, 'widget', 'disable');
|
|
11397
|
+
}
|
|
11398
|
+
/**
|
|
11399
|
+
* Update a vault's upload settings (max size, notification email).
|
|
11400
|
+
*
|
|
11401
|
+
* @example
|
|
11402
|
+
* await sv.vaults.updateUploadConfig('vlt_abc', { maxSizeBytes: 1048576 });
|
|
11403
|
+
*/
|
|
11404
|
+
async updateUploadConfig(vaultId, config) {
|
|
11405
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11406
|
+
const body = {};
|
|
11407
|
+
if (config.maxSizeBytes !== undefined)
|
|
11408
|
+
body.max_size_bytes = config.maxSizeBytes;
|
|
11409
|
+
if (config.notificationEmail !== undefined)
|
|
11410
|
+
body.notification_email = config.notificationEmail;
|
|
11411
|
+
const response = await this.http.put(`/v1/vaults/${cleanId}/upload`, body);
|
|
11412
|
+
return mapUploadSettings(response.data);
|
|
11413
|
+
}
|
|
11414
|
+
async toggleUploadChannel(vaultId, channel, action, vmk) {
|
|
11415
|
+
const cleanId = this.normalizeVaultId(vaultId);
|
|
11416
|
+
if (action === 'enable' && !vmk) {
|
|
11417
|
+
throw new ValidationError('VMK is required');
|
|
11418
|
+
}
|
|
11419
|
+
const response = await this.http.post(`/v1/vaults/${cleanId}/upload/${channel}/${action}`, action === 'enable' ? { vmk } : undefined);
|
|
11420
|
+
const d = response.data;
|
|
11421
|
+
return {
|
|
11422
|
+
vaultId: d.vault_id,
|
|
11423
|
+
uploadPortalEnabled: d.upload_portal_enabled ?? (channel === 'portal' && action === 'enable'),
|
|
11424
|
+
uploadPortalEnabledAt: channel === 'portal' && action === 'enable' ? d.enabled_at ?? null : null,
|
|
11425
|
+
uploadWidgetEnabled: d.upload_widget_enabled ?? (channel === 'widget' && action === 'enable'),
|
|
11426
|
+
uploadWidgetEnabledAt: channel === 'widget' && action === 'enable' ? d.enabled_at ?? null : null,
|
|
11427
|
+
maxSizeBytes: null,
|
|
11428
|
+
notificationEmail: null,
|
|
11429
|
+
portalUrl: d.portal_url,
|
|
11430
|
+
};
|
|
11431
|
+
}
|
|
11432
|
+
normalizeVaultId(vaultId) {
|
|
11433
|
+
if (!vaultId) {
|
|
11434
|
+
throw new ValidationError('Vault ID is required');
|
|
11435
|
+
}
|
|
11436
|
+
return vaultId.startsWith('vlt_') ? vaultId : `vlt_${vaultId}`;
|
|
11069
11437
|
}
|
|
11070
11438
|
validateCreateOptions(options) {
|
|
11071
11439
|
if (!options.name?.trim()) {
|
|
@@ -11075,55 +11443,510 @@ class VaultsModule {
|
|
|
11075
11443
|
throw new ValidationError('Vault name must be 255 characters or less');
|
|
11076
11444
|
}
|
|
11077
11445
|
}
|
|
11078
|
-
validateUploadOptions(options) {
|
|
11079
|
-
if (!options.file) {
|
|
11080
|
-
throw new ValidationError('File is required');
|
|
11081
|
-
}
|
|
11082
|
-
if (options.file.size === 0) {
|
|
11083
|
-
throw new ValidationError('File cannot be empty');
|
|
11084
|
-
}
|
|
11085
|
-
}
|
|
11086
|
-
validateDownloadUrl(downloadUrl) {
|
|
11087
|
-
let parsed;
|
|
11088
|
-
try {
|
|
11089
|
-
parsed = new URL(downloadUrl);
|
|
11090
|
-
}
|
|
11091
|
-
catch {
|
|
11092
|
-
throw new ValidationError('Invalid download URL from server');
|
|
11093
|
-
}
|
|
11094
|
-
if (parsed.protocol !== 'https:') {
|
|
11095
|
-
throw new ValidationError('Download URL must use HTTPS');
|
|
11096
|
-
}
|
|
11097
|
-
const allowedHosts = this.config.allowedDownloadHostPatterns;
|
|
11098
|
-
if (!allowedHosts.some(pattern => pattern.test(parsed.hostname))) {
|
|
11099
|
-
throw new ValidationError('Invalid download URL from server');
|
|
11100
|
-
}
|
|
11101
|
-
}
|
|
11102
11446
|
}
|
|
11103
|
-
function
|
|
11104
|
-
|
|
11105
|
-
|
|
11106
|
-
|
|
11107
|
-
|
|
11447
|
+
function mapSharingConfig$1(wire) {
|
|
11448
|
+
return {
|
|
11449
|
+
vaultId: wire.vault_id,
|
|
11450
|
+
publicSharingEnabled: wire.public_sharing_enabled,
|
|
11451
|
+
allIngotsPublic: wire.all_ingots_public,
|
|
11452
|
+
sessionLengthSeconds: wire.session_length_seconds ?? null,
|
|
11453
|
+
enabledAt: wire.enabled_at ?? null,
|
|
11454
|
+
};
|
|
11455
|
+
}
|
|
11456
|
+
function mapUploadSettings(wire) {
|
|
11457
|
+
return {
|
|
11458
|
+
vaultId: wire.vault_id,
|
|
11459
|
+
uploadPortalEnabled: wire.upload_portal_enabled,
|
|
11460
|
+
uploadPortalEnabledAt: wire.upload_portal_enabled_at ?? null,
|
|
11461
|
+
uploadWidgetEnabled: wire.upload_widget_enabled,
|
|
11462
|
+
uploadWidgetEnabledAt: wire.upload_widget_enabled_at ?? null,
|
|
11463
|
+
maxSizeBytes: wire.max_size_bytes ?? null,
|
|
11464
|
+
notificationEmail: wire.notification_email ?? null,
|
|
11465
|
+
portalUrl: wire.portal_url,
|
|
11466
|
+
};
|
|
11108
11467
|
}
|
|
11468
|
+
|
|
11469
|
+
/**
|
|
11470
|
+
* Ingots Module
|
|
11471
|
+
*
|
|
11472
|
+
* Data-plane operations on the contents of an *unsealed* vault. Every method
|
|
11473
|
+
* takes the {@link UnsealedVault} returned by `sv.vaults.unseal(...)` and
|
|
11474
|
+
* authenticates with its short-lived Vault Access Token (VAT).
|
|
11475
|
+
*
|
|
11476
|
+
* Uploads and downloads run over the single canonical Forge TUS transfer path
|
|
11477
|
+
* (shared with the public upload widget), with server-side finalization and
|
|
11478
|
+
* download-host allowlisting enforced — no per-size branching.
|
|
11479
|
+
*/
|
|
11109
11480
|
const DOWNLOAD_MAX_ATTEMPTS = 3;
|
|
11110
11481
|
const DOWNLOAD_RETRY_BASE_MS = 500;
|
|
11111
|
-
|
|
11112
|
-
|
|
11113
|
-
|
|
11114
|
-
|
|
11115
|
-
|
|
11116
|
-
|
|
11117
|
-
return false;
|
|
11118
|
-
// Network errors carry a status code only when the SDK HTTP client attached
|
|
11119
|
-
// one; treat any explicitly-4xx response as a permanent failure.
|
|
11120
|
-
const status = err.statusCode;
|
|
11121
|
-
if (typeof status === 'number' && status >= 400 && status < 500 && status !== 408 && status !== 429) {
|
|
11122
|
-
return false;
|
|
11482
|
+
const DOWNLOAD_FETCH_TIMEOUT_MS = 300000;
|
|
11483
|
+
class IngotsModule {
|
|
11484
|
+
constructor(config, http) {
|
|
11485
|
+
this.config = config;
|
|
11486
|
+
this.http = http;
|
|
11487
|
+
this.uploadApi = new UploadApi(config);
|
|
11123
11488
|
}
|
|
11124
|
-
|
|
11125
|
-
|
|
11126
|
-
|
|
11489
|
+
/**
|
|
11490
|
+
* Create an ingot upload session (discrete primitive).
|
|
11491
|
+
*
|
|
11492
|
+
* Reserves the ingot record and returns the Forge upload URL (carrying the
|
|
11493
|
+
* ISTK upload session token). Use this when you want to drive the TUS
|
|
11494
|
+
* transfer yourself; otherwise prefer {@link upload}.
|
|
11495
|
+
*
|
|
11496
|
+
* @example
|
|
11497
|
+
* const session = await sv.ingots.createUpload({
|
|
11498
|
+
* vault: unsealed, name: 'report.pdf',
|
|
11499
|
+
* contentType: 'application/pdf', sizeBytes: file.size,
|
|
11500
|
+
* });
|
|
11501
|
+
* console.log(session.uploadUrl);
|
|
11502
|
+
*/
|
|
11503
|
+
async createUpload(options) {
|
|
11504
|
+
if (!options.name?.trim()) {
|
|
11505
|
+
throw new ValidationError('Ingot name is required');
|
|
11506
|
+
}
|
|
11507
|
+
if (!Number.isFinite(options.sizeBytes) || options.sizeBytes < 1) {
|
|
11508
|
+
throw new ValidationError('sizeBytes must be a positive number');
|
|
11509
|
+
}
|
|
11510
|
+
if (options.folder && options.folderId) {
|
|
11511
|
+
throw new ValidationError('Provide folder or folderId, not both');
|
|
11512
|
+
}
|
|
11513
|
+
const created = await this.requestUploadSession(options.vault, {
|
|
11514
|
+
name: options.name,
|
|
11515
|
+
content_type: options.contentType,
|
|
11516
|
+
size_bytes: options.sizeBytes,
|
|
11517
|
+
folder: options.folder,
|
|
11518
|
+
folderId: options.folderId,
|
|
11519
|
+
});
|
|
11520
|
+
return {
|
|
11521
|
+
id: created.ingot_id,
|
|
11522
|
+
name: created.name,
|
|
11523
|
+
contentType: created.content_type,
|
|
11524
|
+
size: created.size_bytes,
|
|
11525
|
+
uploadUrl: created.forge_url,
|
|
11526
|
+
expiresAt: created.expires_at,
|
|
11527
|
+
};
|
|
11528
|
+
}
|
|
11529
|
+
/**
|
|
11530
|
+
* Upload a file as a new ingot (high-level flow).
|
|
11531
|
+
*
|
|
11532
|
+
* Creates the upload session, streams the file to Forge over TUS, then
|
|
11533
|
+
* confirms the server finalized the upload before resolving.
|
|
11534
|
+
*
|
|
11535
|
+
* @example
|
|
11536
|
+
* const ingot = await sv.ingots.upload(unsealed, {
|
|
11537
|
+
* file: myFile, name: 'document.pdf'
|
|
11538
|
+
* });
|
|
11539
|
+
*/
|
|
11540
|
+
async upload(vault, options) {
|
|
11541
|
+
this.validateFile(options.file);
|
|
11542
|
+
if (options.folder && options.folderId) {
|
|
11543
|
+
throw new ValidationError('Provide folder or folderId, not both');
|
|
11544
|
+
}
|
|
11545
|
+
const name = options.name ?? getBlobName(options.file);
|
|
11546
|
+
const contentType = options.contentType ?? options.file.type ?? 'application/octet-stream';
|
|
11547
|
+
const created = await this.requestUploadSession(vault, {
|
|
11548
|
+
name,
|
|
11549
|
+
content_type: contentType,
|
|
11550
|
+
size_bytes: options.file.size,
|
|
11551
|
+
folder: options.folder,
|
|
11552
|
+
folderId: options.folderId,
|
|
11553
|
+
});
|
|
11554
|
+
return this.transferAndFinalize(created, options.file, options.onProgress);
|
|
11555
|
+
}
|
|
11556
|
+
/**
|
|
11557
|
+
* Replace an existing ingot's content (high-level flow).
|
|
11558
|
+
*
|
|
11559
|
+
* @example
|
|
11560
|
+
* await sv.ingots.replace(unsealed, 'ing_abc', { file: updatedFile });
|
|
11561
|
+
*/
|
|
11562
|
+
async replace(vault, ingotId, options) {
|
|
11563
|
+
this.validateFile(options.file);
|
|
11564
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11565
|
+
const name = options.name ?? getBlobName(options.file);
|
|
11566
|
+
const contentType = options.contentType ?? options.file.type ?? 'application/octet-stream';
|
|
11567
|
+
const response = await this.http.put(`/v1/vaults/${vault.id}/ingots/${cleanId}`, { name, content_type: contentType, size_bytes: options.file.size }, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11568
|
+
if (!response.data.forge_url) {
|
|
11569
|
+
throw new ValidationError('Replace endpoint did not return a Forge upload URL');
|
|
11570
|
+
}
|
|
11571
|
+
return this.transferAndFinalize(response.data, options.file, options.onProgress);
|
|
11572
|
+
}
|
|
11573
|
+
/**
|
|
11574
|
+
* Rename an ingot.
|
|
11575
|
+
*
|
|
11576
|
+
* @example
|
|
11577
|
+
* await sv.ingots.rename(unsealed, 'ing_abc', 'new-name.pdf');
|
|
11578
|
+
*/
|
|
11579
|
+
async rename(vault, ingotId, name) {
|
|
11580
|
+
if (!name?.trim()) {
|
|
11581
|
+
throw new ValidationError('Ingot name is required');
|
|
11582
|
+
}
|
|
11583
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11584
|
+
const response = await this.http.patch(`/v1/vaults/${vault.id}/ingots/${cleanId}`, { name }, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11585
|
+
return mapIngot(response.data);
|
|
11586
|
+
}
|
|
11587
|
+
/**
|
|
11588
|
+
* Move an ingot to a folder, or to the vault root with `null`.
|
|
11589
|
+
*
|
|
11590
|
+
* @example
|
|
11591
|
+
* await sv.ingots.move(unsealed, 'ing_abc', 'fld_xyz');
|
|
11592
|
+
* await sv.ingots.move(unsealed, 'ing_abc', null); // root
|
|
11593
|
+
*/
|
|
11594
|
+
async move(vault, ingotId, folderId) {
|
|
11595
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11596
|
+
const response = await this.http.patch(`/v1/vaults/${vault.id}/ingots/${cleanId}`, { folder_id: folderId }, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11597
|
+
return mapIngot(response.data);
|
|
11598
|
+
}
|
|
11599
|
+
/**
|
|
11600
|
+
* Delete an ingot.
|
|
11601
|
+
*
|
|
11602
|
+
* @example
|
|
11603
|
+
* await sv.ingots.delete(unsealed, 'ing_abc');
|
|
11604
|
+
*/
|
|
11605
|
+
async delete(vault, ingotId) {
|
|
11606
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11607
|
+
await this.http.delete(`/v1/vaults/${vault.id}/ingots/${cleanId}`, {
|
|
11608
|
+
headers: { 'X-Vault-Access-Token': vault.vatToken },
|
|
11609
|
+
});
|
|
11610
|
+
}
|
|
11611
|
+
/**
|
|
11612
|
+
* Get ingot metadata.
|
|
11613
|
+
*
|
|
11614
|
+
* @example
|
|
11615
|
+
* const ingot = await sv.ingots.get(unsealed, 'ing_abc');
|
|
11616
|
+
*/
|
|
11617
|
+
async get(vault, ingotId) {
|
|
11618
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11619
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/ingots/${cleanId}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11620
|
+
return mapIngot(response.data);
|
|
11621
|
+
}
|
|
11622
|
+
/**
|
|
11623
|
+
* List ingots in an unsealed vault.
|
|
11624
|
+
*
|
|
11625
|
+
* @example
|
|
11626
|
+
* const { ingots } = await sv.ingots.list(unsealed);
|
|
11627
|
+
*/
|
|
11628
|
+
async list(vault, options = {}) {
|
|
11629
|
+
const query = buildQuery$1({
|
|
11630
|
+
limit: options.limit,
|
|
11631
|
+
cursor: options.cursor,
|
|
11632
|
+
sort_by: options.sortBy,
|
|
11633
|
+
sort_order: options.sortOrder,
|
|
11634
|
+
});
|
|
11635
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/ingots${query}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11636
|
+
return mapIngotPage(response.data);
|
|
11637
|
+
}
|
|
11638
|
+
/**
|
|
11639
|
+
* Search ingots in an unsealed vault.
|
|
11640
|
+
*
|
|
11641
|
+
* @example
|
|
11642
|
+
* const { ingots } = await sv.ingots.search(unsealed, { q: 'invoice' });
|
|
11643
|
+
*/
|
|
11644
|
+
async search(vault, options = {}) {
|
|
11645
|
+
const query = buildQuery$1({
|
|
11646
|
+
q: options.q,
|
|
11647
|
+
file_type: options.fileType,
|
|
11648
|
+
min_size: options.minSize,
|
|
11649
|
+
max_size: options.maxSize,
|
|
11650
|
+
limit: options.limit,
|
|
11651
|
+
cursor: options.cursor,
|
|
11652
|
+
sort_order: options.sortOrder,
|
|
11653
|
+
});
|
|
11654
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/ingots/search${query}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11655
|
+
return mapIngotPage(response.data);
|
|
11656
|
+
}
|
|
11657
|
+
/**
|
|
11658
|
+
* List the contents (ingots) of a folder, or the vault root.
|
|
11659
|
+
*
|
|
11660
|
+
* @example
|
|
11661
|
+
* const { ingots } = await sv.ingots.listContents(unsealed, { folderId: 'fld_x' });
|
|
11662
|
+
*/
|
|
11663
|
+
async listContents(vault, options = {}) {
|
|
11664
|
+
const query = buildQuery$1({
|
|
11665
|
+
folder_id: options.folderId,
|
|
11666
|
+
limit: options.limit,
|
|
11667
|
+
cursor: options.cursor,
|
|
11668
|
+
sort_by: options.sortBy,
|
|
11669
|
+
sort_order: options.sortOrder,
|
|
11670
|
+
type: options.type,
|
|
11671
|
+
});
|
|
11672
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/contents${query}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11673
|
+
return mapIngotPage(response.data);
|
|
11674
|
+
}
|
|
11675
|
+
/**
|
|
11676
|
+
* Create a signed download link for an ingot (discrete primitive).
|
|
11677
|
+
*
|
|
11678
|
+
* Validates the returned URL against the download-host allowlist. Use this
|
|
11679
|
+
* when you want the URL itself; otherwise prefer {@link download}.
|
|
11680
|
+
*
|
|
11681
|
+
* @example
|
|
11682
|
+
* const link = await sv.ingots.createDownloadLink(unsealed, 'ing_abc');
|
|
11683
|
+
* window.location.href = link.downloadUrl;
|
|
11684
|
+
*/
|
|
11685
|
+
async createDownloadLink(vault, ingotId) {
|
|
11686
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11687
|
+
const response = await this.http.post(`/v1/vaults/${vault.id}/ingots/${cleanId}/download`, undefined, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11688
|
+
if (!response.data.download_url) {
|
|
11689
|
+
throw new ValidationError('Download endpoint did not return a download URL');
|
|
11690
|
+
}
|
|
11691
|
+
this.validateDownloadUrl(response.data.download_url);
|
|
11692
|
+
return {
|
|
11693
|
+
downloadUrl: response.data.download_url,
|
|
11694
|
+
id: response.data.ingot_id,
|
|
11695
|
+
name: response.data.name,
|
|
11696
|
+
size: response.data.size_bytes,
|
|
11697
|
+
contentType: response.data.content_type,
|
|
11698
|
+
};
|
|
11699
|
+
}
|
|
11700
|
+
/**
|
|
11701
|
+
* Download an ingot as a Blob (high-level flow).
|
|
11702
|
+
*
|
|
11703
|
+
* Requests a fresh signed link on every attempt (server URLs are
|
|
11704
|
+
* short-lived) and retries transient transport failures with exponential
|
|
11705
|
+
* backoff. Validation failures (bad host, non-HTTPS) are never retried.
|
|
11706
|
+
*
|
|
11707
|
+
* @example
|
|
11708
|
+
* const blob = await sv.ingots.download(unsealed, 'ing_abc');
|
|
11709
|
+
* const url = URL.createObjectURL(blob);
|
|
11710
|
+
*/
|
|
11711
|
+
async download(vault, ingotId) {
|
|
11712
|
+
let lastError = null;
|
|
11713
|
+
for (let attempt = 1; attempt <= DOWNLOAD_MAX_ATTEMPTS; attempt++) {
|
|
11714
|
+
try {
|
|
11715
|
+
const link = await this.createDownloadLink(vault, ingotId);
|
|
11716
|
+
return await fetchBlobWithTimeout(link.downloadUrl, DOWNLOAD_FETCH_TIMEOUT_MS);
|
|
11717
|
+
}
|
|
11718
|
+
catch (err) {
|
|
11719
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
11720
|
+
if (!isRetryableDownloadError(lastError) || attempt === DOWNLOAD_MAX_ATTEMPTS) {
|
|
11721
|
+
throw lastError;
|
|
11722
|
+
}
|
|
11723
|
+
await delayExponential(attempt);
|
|
11724
|
+
}
|
|
11725
|
+
}
|
|
11726
|
+
throw lastError ?? new Error('Download failed');
|
|
11727
|
+
}
|
|
11728
|
+
/**
|
|
11729
|
+
* Get an ingot's sharing configuration.
|
|
11730
|
+
*
|
|
11731
|
+
* @example
|
|
11732
|
+
* const sharing = await sv.ingots.getSharingConfig(unsealed, 'ing_abc');
|
|
11733
|
+
*/
|
|
11734
|
+
async getSharingConfig(vault, ingotId) {
|
|
11735
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11736
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/ingots/${cleanId}/sharing`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11737
|
+
return mapSharingConfig(response.data);
|
|
11738
|
+
}
|
|
11739
|
+
/**
|
|
11740
|
+
* Update an ingot's sharing configuration.
|
|
11741
|
+
*
|
|
11742
|
+
* @example
|
|
11743
|
+
* await sv.ingots.updateSharingConfig(unsealed, 'ing_abc', { shared: true });
|
|
11744
|
+
*/
|
|
11745
|
+
async updateSharingConfig(vault, ingotId, config) {
|
|
11746
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11747
|
+
const response = await this.http.put(`/v1/vaults/${vault.id}/ingots/${cleanId}/sharing`, { shared: config.shared, visibility: config.visibility }, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11748
|
+
return mapSharingConfig(response.data);
|
|
11749
|
+
}
|
|
11750
|
+
/**
|
|
11751
|
+
* Invite an identity to a shared ingot. The identity type (email or phone)
|
|
11752
|
+
* is inferred server-side from the identity value.
|
|
11753
|
+
*
|
|
11754
|
+
* @example
|
|
11755
|
+
* await sv.ingots.addInvite(unsealed, 'ing_abc', 'user@example.com');
|
|
11756
|
+
* await sv.ingots.addInvite(unsealed, 'ing_abc', 'user@example.com', { expiresInSeconds: 86400 });
|
|
11757
|
+
*/
|
|
11758
|
+
async addInvite(vault, ingotId, identity, options = {}) {
|
|
11759
|
+
if (!identity?.trim()) {
|
|
11760
|
+
throw new ValidationError('Invite identity is required');
|
|
11761
|
+
}
|
|
11762
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11763
|
+
const response = await this.http.post(`/v1/vaults/${vault.id}/ingots/${cleanId}/sharing/invite`, { identity, expires_in_seconds: options.expiresInSeconds }, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11764
|
+
return mapInvite(response.data);
|
|
11765
|
+
}
|
|
11766
|
+
/**
|
|
11767
|
+
* Revoke an ingot sharing invite.
|
|
11768
|
+
*
|
|
11769
|
+
* @example
|
|
11770
|
+
* await sv.ingots.revokeInvite(unsealed, 'ing_abc', 'inv_123');
|
|
11771
|
+
*/
|
|
11772
|
+
async revokeInvite(vault, ingotId, inviteId) {
|
|
11773
|
+
if (!inviteId?.trim()) {
|
|
11774
|
+
throw new ValidationError('Invite ID is required');
|
|
11775
|
+
}
|
|
11776
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11777
|
+
await this.http.delete(`/v1/vaults/${vault.id}/ingots/${cleanId}/sharing/invite/${inviteId}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11778
|
+
}
|
|
11779
|
+
/**
|
|
11780
|
+
* Get an ingot's access logs.
|
|
11781
|
+
*
|
|
11782
|
+
* @example
|
|
11783
|
+
* const { logs } = await sv.ingots.getAccessLogs(unsealed, 'ing_abc');
|
|
11784
|
+
*/
|
|
11785
|
+
async getAccessLogs(vault, ingotId, options = {}) {
|
|
11786
|
+
const cleanId = normalizeIngotId(ingotId);
|
|
11787
|
+
const query = buildQuery$1({ limit: options.limit, cursor: options.cursor });
|
|
11788
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/ingots/${cleanId}/audit-logs${query}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11789
|
+
const entries = response.data.entries ?? [];
|
|
11790
|
+
return {
|
|
11791
|
+
logs: entries.map(mapAccessLog),
|
|
11792
|
+
nextCursor: response.data.next_cursor,
|
|
11793
|
+
};
|
|
11794
|
+
}
|
|
11795
|
+
/**
|
|
11796
|
+
* Request a Forge upload session for a new ingot.
|
|
11797
|
+
* Shared by createUpload and upload — the single ingot-create path.
|
|
11798
|
+
*/
|
|
11799
|
+
async requestUploadSession(vault, body) {
|
|
11800
|
+
const payload = {
|
|
11801
|
+
name: body.name,
|
|
11802
|
+
content_type: body.content_type ?? 'application/octet-stream',
|
|
11803
|
+
size_bytes: body.size_bytes,
|
|
11804
|
+
};
|
|
11805
|
+
// folder_path (find-or-create) and folder_id (existing folder) are
|
|
11806
|
+
// mutually exclusive server-side; a path takes precedence when both given.
|
|
11807
|
+
if (body.folder) {
|
|
11808
|
+
payload.folder_path = body.folder;
|
|
11809
|
+
}
|
|
11810
|
+
else if (body.folderId) {
|
|
11811
|
+
payload.folder_id = body.folderId;
|
|
11812
|
+
}
|
|
11813
|
+
const response = await this.http.post(`/v1/vaults/${vault.id}/ingots`, payload, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11814
|
+
if (!response.data.forge_url) {
|
|
11815
|
+
throw new ValidationError('Upload endpoint did not return a Forge upload URL');
|
|
11816
|
+
}
|
|
11817
|
+
return response.data;
|
|
11818
|
+
}
|
|
11819
|
+
/**
|
|
11820
|
+
* Run the canonical TUS transfer for a created/replaced ingot, then confirm
|
|
11821
|
+
* the server finalized the upload before reporting success.
|
|
11822
|
+
*/
|
|
11823
|
+
async transferAndFinalize(created, file, onProgress) {
|
|
11824
|
+
const uploadUrl = await this.uploadApi.uploadFileToForge(file, created.forge_url, onProgress);
|
|
11825
|
+
// Confirm the server actually finalized the upload before reporting
|
|
11826
|
+
// success — the TUS loop's offsets alone don't prove the final chunk
|
|
11827
|
+
// committed and the ingot went active.
|
|
11828
|
+
await this.uploadApi.verifyUploadFinalized(uploadUrl, file.size);
|
|
11829
|
+
return {
|
|
11830
|
+
id: created.ingot_id,
|
|
11831
|
+
name: created.name,
|
|
11832
|
+
contentType: created.content_type,
|
|
11833
|
+
size: created.size_bytes,
|
|
11834
|
+
};
|
|
11835
|
+
}
|
|
11836
|
+
validateFile(file) {
|
|
11837
|
+
if (!file) {
|
|
11838
|
+
throw new ValidationError('File is required');
|
|
11839
|
+
}
|
|
11840
|
+
if (file.size === 0) {
|
|
11841
|
+
throw new ValidationError('File cannot be empty');
|
|
11842
|
+
}
|
|
11843
|
+
}
|
|
11844
|
+
validateDownloadUrl(downloadUrl) {
|
|
11845
|
+
let parsed;
|
|
11846
|
+
try {
|
|
11847
|
+
parsed = new URL(downloadUrl);
|
|
11848
|
+
}
|
|
11849
|
+
catch {
|
|
11850
|
+
throw new ValidationError('Invalid download URL from server');
|
|
11851
|
+
}
|
|
11852
|
+
if (parsed.protocol !== 'https:') {
|
|
11853
|
+
throw new ValidationError('Download URL must use HTTPS');
|
|
11854
|
+
}
|
|
11855
|
+
const allowedHosts = this.config.allowedDownloadHostPatterns;
|
|
11856
|
+
if (!allowedHosts.some((pattern) => pattern.test(parsed.hostname))) {
|
|
11857
|
+
throw new ValidationError('Invalid download URL from server');
|
|
11858
|
+
}
|
|
11859
|
+
}
|
|
11860
|
+
}
|
|
11861
|
+
function getBlobName(file) {
|
|
11862
|
+
if (typeof File !== 'undefined' && file instanceof File && file.name) {
|
|
11863
|
+
return file.name;
|
|
11864
|
+
}
|
|
11865
|
+
return 'unnamed';
|
|
11866
|
+
}
|
|
11867
|
+
function normalizeIngotId(ingotId) {
|
|
11868
|
+
if (!ingotId) {
|
|
11869
|
+
throw new ValidationError('Ingot ID is required');
|
|
11870
|
+
}
|
|
11871
|
+
return ingotId.startsWith('ing_') ? ingotId : `ing_${ingotId}`;
|
|
11872
|
+
}
|
|
11873
|
+
function mapIngot(wire) {
|
|
11874
|
+
return {
|
|
11875
|
+
id: wire.ingot_id,
|
|
11876
|
+
name: wire.name,
|
|
11877
|
+
contentType: wire.content_type,
|
|
11878
|
+
size: wire.size_bytes,
|
|
11879
|
+
status: wire.status ?? 'active',
|
|
11880
|
+
createdAt: wire.created_at ?? 0,
|
|
11881
|
+
folderId: wire.folder_id ?? null,
|
|
11882
|
+
};
|
|
11883
|
+
}
|
|
11884
|
+
function mapIngotPage(response) {
|
|
11885
|
+
return {
|
|
11886
|
+
ingots: (response.ingots ?? []).map(mapIngot),
|
|
11887
|
+
nextCursor: response.next_cursor,
|
|
11888
|
+
};
|
|
11889
|
+
}
|
|
11890
|
+
function mapSharingConfig(wire) {
|
|
11891
|
+
return {
|
|
11892
|
+
shared: wire.shared,
|
|
11893
|
+
visibility: wire.visibility,
|
|
11894
|
+
invites: wire.invites?.map(mapInvite),
|
|
11895
|
+
};
|
|
11896
|
+
}
|
|
11897
|
+
function mapInvite(wire) {
|
|
11898
|
+
return {
|
|
11899
|
+
id: wire.id ?? wire.invite_id ?? '',
|
|
11900
|
+
identity: wire.identity,
|
|
11901
|
+
identityType: wire.identity_type ?? 'email',
|
|
11902
|
+
};
|
|
11903
|
+
}
|
|
11904
|
+
function mapAccessLog(entry) {
|
|
11905
|
+
const eventType = entry.event_type ?? entry.action ?? '';
|
|
11906
|
+
const action = eventType.includes('download') ? 'download' : 'view';
|
|
11907
|
+
const actor = entry.actor;
|
|
11908
|
+
return {
|
|
11909
|
+
id: entry.log_id ?? entry.event_id ?? entry.id ?? String(entry.timestamp ?? entry.created_at ?? ''),
|
|
11910
|
+
action,
|
|
11911
|
+
identity: actor?.email ??
|
|
11912
|
+
entry.actor_email ??
|
|
11913
|
+
entry.identity ??
|
|
11914
|
+
actor?.id ??
|
|
11915
|
+
actor?.user_id ??
|
|
11916
|
+
actor?.api_key_id ??
|
|
11917
|
+
entry.actor_id ??
|
|
11918
|
+
'Unknown',
|
|
11919
|
+
ipAddress: entry.ip_address ?? '',
|
|
11920
|
+
userAgent: entry.user_agent ?? '',
|
|
11921
|
+
timestamp: entry.timestamp ?? entry.created_at ?? 0,
|
|
11922
|
+
};
|
|
11923
|
+
}
|
|
11924
|
+
function buildQuery$1(params) {
|
|
11925
|
+
const search = new URLSearchParams();
|
|
11926
|
+
for (const [key, value] of Object.entries(params)) {
|
|
11927
|
+
if (value === undefined || value === null || value === '')
|
|
11928
|
+
continue;
|
|
11929
|
+
search.append(key, String(value));
|
|
11930
|
+
}
|
|
11931
|
+
const query = search.toString();
|
|
11932
|
+
return query ? `?${query}` : '';
|
|
11933
|
+
}
|
|
11934
|
+
function delayExponential(attempt) {
|
|
11935
|
+
return new Promise((resolve) => setTimeout(resolve, DOWNLOAD_RETRY_BASE_MS * 2 ** (attempt - 1)));
|
|
11936
|
+
}
|
|
11937
|
+
function isRetryableDownloadError(err) {
|
|
11938
|
+
// Server contract / client-side validation errors aren't fixed by retrying.
|
|
11939
|
+
if (err instanceof ValidationError)
|
|
11940
|
+
return false;
|
|
11941
|
+
// Network errors carry a status code only when one was attached; treat any
|
|
11942
|
+
// explicitly-4xx response (except 408/429) as a permanent failure.
|
|
11943
|
+
const status = err.statusCode;
|
|
11944
|
+
if (typeof status === 'number' && status >= 400 && status < 500 && status !== 408 && status !== 429) {
|
|
11945
|
+
return false;
|
|
11946
|
+
}
|
|
11947
|
+
return true;
|
|
11948
|
+
}
|
|
11949
|
+
async function fetchBlobWithTimeout(url, timeout) {
|
|
11127
11950
|
const controller = new AbortController();
|
|
11128
11951
|
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
11129
11952
|
try {
|
|
@@ -11133,13 +11956,386 @@ async function fetchBlobWithTimeout(url, timeout = 300000) {
|
|
|
11133
11956
|
error.statusCode = response.status;
|
|
11134
11957
|
throw error;
|
|
11135
11958
|
}
|
|
11136
|
-
return response.blob();
|
|
11959
|
+
return await response.blob();
|
|
11137
11960
|
}
|
|
11138
11961
|
finally {
|
|
11139
11962
|
clearTimeout(timeoutId);
|
|
11140
11963
|
}
|
|
11141
11964
|
}
|
|
11142
11965
|
|
|
11966
|
+
/**
|
|
11967
|
+
* Folders Module
|
|
11968
|
+
*
|
|
11969
|
+
* Organize ingots within an *unsealed* vault. Every method takes the
|
|
11970
|
+
* {@link UnsealedVault} returned by `sv.vaults.unseal(...)` and authenticates
|
|
11971
|
+
* with its short-lived Vault Access Token (VAT).
|
|
11972
|
+
*/
|
|
11973
|
+
class FoldersModule {
|
|
11974
|
+
constructor(http) {
|
|
11975
|
+
this.http = http;
|
|
11976
|
+
}
|
|
11977
|
+
/**
|
|
11978
|
+
* List all folders in a vault (flat list — build the tree client-side).
|
|
11979
|
+
*
|
|
11980
|
+
* @example
|
|
11981
|
+
* const folders = await sv.folders.list(unsealed);
|
|
11982
|
+
*/
|
|
11983
|
+
async list(vault) {
|
|
11984
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/folders`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11985
|
+
return (response.data.folders ?? []).map(mapFolder);
|
|
11986
|
+
}
|
|
11987
|
+
/**
|
|
11988
|
+
* Get a single folder.
|
|
11989
|
+
*
|
|
11990
|
+
* @example
|
|
11991
|
+
* const folder = await sv.folders.get(unsealed, 'fld_abc');
|
|
11992
|
+
*/
|
|
11993
|
+
async get(vault, folderId) {
|
|
11994
|
+
const cleanId = requireFolderId(folderId);
|
|
11995
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/folders/${cleanId}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
11996
|
+
return mapFolder(response.data);
|
|
11997
|
+
}
|
|
11998
|
+
/**
|
|
11999
|
+
* Get a folder's breadcrumb trail (Root → … → folder).
|
|
12000
|
+
*
|
|
12001
|
+
* @example
|
|
12002
|
+
* const trail = await sv.folders.getBreadcrumb(unsealed, 'fld_abc');
|
|
12003
|
+
*/
|
|
12004
|
+
async getBreadcrumb(vault, folderId) {
|
|
12005
|
+
const cleanId = requireFolderId(folderId);
|
|
12006
|
+
const response = await this.http.get(`/v1/vaults/${vault.id}/folders/${cleanId}`, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
12007
|
+
return (response.data.breadcrumb ?? []).map((item) => ({
|
|
12008
|
+
id: item.folder_id,
|
|
12009
|
+
name: item.name,
|
|
12010
|
+
}));
|
|
12011
|
+
}
|
|
12012
|
+
/**
|
|
12013
|
+
* Create a folder.
|
|
12014
|
+
*
|
|
12015
|
+
* @example
|
|
12016
|
+
* const folder = await sv.folders.create(unsealed, { name: 'Invoices' });
|
|
12017
|
+
*/
|
|
12018
|
+
async create(vault, data) {
|
|
12019
|
+
if (!data.name?.trim()) {
|
|
12020
|
+
throw new ValidationError('Folder name is required');
|
|
12021
|
+
}
|
|
12022
|
+
const response = await this.http.post(`/v1/vaults/${vault.id}/folders`, { name: data.name, parent_id: data.parentId ?? null }, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
12023
|
+
return mapFolder(response.data);
|
|
12024
|
+
}
|
|
12025
|
+
/**
|
|
12026
|
+
* Update a folder (rename, move, pin/unpin).
|
|
12027
|
+
*
|
|
12028
|
+
* @example
|
|
12029
|
+
* await sv.folders.update(unsealed, 'fld_abc', { name: 'Renamed' });
|
|
12030
|
+
*/
|
|
12031
|
+
async update(vault, folderId, data) {
|
|
12032
|
+
const cleanId = requireFolderId(folderId);
|
|
12033
|
+
const body = {};
|
|
12034
|
+
if (data.name !== undefined)
|
|
12035
|
+
body.name = data.name;
|
|
12036
|
+
if (data.parentId !== undefined)
|
|
12037
|
+
body.parent_id = data.parentId;
|
|
12038
|
+
if (data.pinned !== undefined)
|
|
12039
|
+
body.pinned = data.pinned;
|
|
12040
|
+
const response = await this.http.put(`/v1/vaults/${vault.id}/folders/${cleanId}`, body, { headers: { 'X-Vault-Access-Token': vault.vatToken } });
|
|
12041
|
+
return mapFolder(response.data);
|
|
12042
|
+
}
|
|
12043
|
+
/**
|
|
12044
|
+
* Move a folder under a new parent, or to the vault root with `null`.
|
|
12045
|
+
*
|
|
12046
|
+
* @example
|
|
12047
|
+
* await sv.folders.move(unsealed, 'fld_abc', 'fld_parent');
|
|
12048
|
+
*/
|
|
12049
|
+
move(vault, folderId, newParentId) {
|
|
12050
|
+
return this.update(vault, folderId, { parentId: newParentId });
|
|
12051
|
+
}
|
|
12052
|
+
/**
|
|
12053
|
+
* Pin or unpin a folder.
|
|
12054
|
+
*
|
|
12055
|
+
* @example
|
|
12056
|
+
* await sv.folders.togglePin(unsealed, 'fld_abc', true);
|
|
12057
|
+
*/
|
|
12058
|
+
togglePin(vault, folderId, pinned) {
|
|
12059
|
+
return this.update(vault, folderId, { pinned });
|
|
12060
|
+
}
|
|
12061
|
+
/**
|
|
12062
|
+
* Delete a folder.
|
|
12063
|
+
*
|
|
12064
|
+
* @example
|
|
12065
|
+
* await sv.folders.delete(unsealed, 'fld_abc');
|
|
12066
|
+
*/
|
|
12067
|
+
async delete(vault, folderId) {
|
|
12068
|
+
const cleanId = requireFolderId(folderId);
|
|
12069
|
+
await this.http.delete(`/v1/vaults/${vault.id}/folders/${cleanId}`, {
|
|
12070
|
+
headers: { 'X-Vault-Access-Token': vault.vatToken },
|
|
12071
|
+
});
|
|
12072
|
+
}
|
|
12073
|
+
/**
|
|
12074
|
+
* Move an ingot into a folder, or to the vault root with `null`.
|
|
12075
|
+
*
|
|
12076
|
+
* @example
|
|
12077
|
+
* await sv.folders.moveIngot(unsealed, 'ing_abc', 'fld_xyz');
|
|
12078
|
+
*/
|
|
12079
|
+
async moveIngot(vault, ingotId, folderId) {
|
|
12080
|
+
if (!ingotId) {
|
|
12081
|
+
throw new ValidationError('Ingot ID is required');
|
|
12082
|
+
}
|
|
12083
|
+
const cleanIngotId = ingotId.startsWith('ing_') ? ingotId : `ing_${ingotId}`;
|
|
12084
|
+
await this.http.patch(`/v1/vaults/${vault.id}/ingots/${cleanIngotId}`, { folder_id: folderId }, {
|
|
12085
|
+
headers: { 'X-Vault-Access-Token': vault.vatToken },
|
|
12086
|
+
});
|
|
12087
|
+
}
|
|
12088
|
+
}
|
|
12089
|
+
function requireFolderId(folderId) {
|
|
12090
|
+
if (!folderId) {
|
|
12091
|
+
throw new ValidationError('Folder ID is required');
|
|
12092
|
+
}
|
|
12093
|
+
return folderId;
|
|
12094
|
+
}
|
|
12095
|
+
function mapFolder(wire) {
|
|
12096
|
+
return {
|
|
12097
|
+
id: wire.folder_id,
|
|
12098
|
+
name: wire.name,
|
|
12099
|
+
parentId: wire.parent_id ?? null,
|
|
12100
|
+
pinned: wire.pinned ?? false,
|
|
12101
|
+
createdAt: wire.created_at ?? 0,
|
|
12102
|
+
updatedAt: wire.updated_at ?? 0,
|
|
12103
|
+
};
|
|
12104
|
+
}
|
|
12105
|
+
|
|
12106
|
+
/**
|
|
12107
|
+
* Sparks Module
|
|
12108
|
+
*
|
|
12109
|
+
* Create, read, share, and delete end-to-end encrypted, read-once ephemeral
|
|
12110
|
+
* secrets. Sparks are account-scoped — no vault or VAT is involved.
|
|
12111
|
+
*/
|
|
12112
|
+
class SparksModule {
|
|
12113
|
+
constructor(http) {
|
|
12114
|
+
this.http = http;
|
|
12115
|
+
}
|
|
12116
|
+
/**
|
|
12117
|
+
* List sparks for the account.
|
|
12118
|
+
*
|
|
12119
|
+
* @example
|
|
12120
|
+
* const { sparks } = await sv.sparks.list();
|
|
12121
|
+
*/
|
|
12122
|
+
async list(options = {}) {
|
|
12123
|
+
const query = buildQuery({
|
|
12124
|
+
kindling: options.kindling,
|
|
12125
|
+
status: options.status,
|
|
12126
|
+
limit: options.limit,
|
|
12127
|
+
cursor: options.cursor,
|
|
12128
|
+
});
|
|
12129
|
+
const response = await this.http.get(`/v1/sparks${query}`);
|
|
12130
|
+
return {
|
|
12131
|
+
sparks: (response.data.sparks ?? []).map(mapSummary),
|
|
12132
|
+
nextCursor: response.data.next_cursor,
|
|
12133
|
+
};
|
|
12134
|
+
}
|
|
12135
|
+
/**
|
|
12136
|
+
* Read (and burn) a spark. Reading is destructive — the spark can be read
|
|
12137
|
+
* exactly once.
|
|
12138
|
+
*
|
|
12139
|
+
* @example
|
|
12140
|
+
* const spark = await sv.sparks.get('spk_abc');
|
|
12141
|
+
* console.log(spark.payload);
|
|
12142
|
+
*/
|
|
12143
|
+
async get(sparkId) {
|
|
12144
|
+
requireSparkId(sparkId);
|
|
12145
|
+
const response = await this.http.get(`/v1/sparks/${encodeURIComponent(sparkId)}`);
|
|
12146
|
+
const data = response.data;
|
|
12147
|
+
return {
|
|
12148
|
+
id: data.spark_id,
|
|
12149
|
+
payload: data.payload,
|
|
12150
|
+
contentType: data.content_type ?? 'text/plain',
|
|
12151
|
+
size: data.size_bytes,
|
|
12152
|
+
status: data.status,
|
|
12153
|
+
createdAt: data.created_at,
|
|
12154
|
+
burnedAt: data.burned_at,
|
|
12155
|
+
filename: data.filename,
|
|
12156
|
+
};
|
|
12157
|
+
}
|
|
12158
|
+
/**
|
|
12159
|
+
* Create a spark.
|
|
12160
|
+
*
|
|
12161
|
+
* @example
|
|
12162
|
+
* const spark = await sv.sparks.create({ payload: 'top secret', ttlMinutes: 60 });
|
|
12163
|
+
*/
|
|
12164
|
+
async create(options) {
|
|
12165
|
+
if (typeof options.payload !== 'string' || options.payload.length === 0) {
|
|
12166
|
+
throw new ValidationError('Spark payload is required');
|
|
12167
|
+
}
|
|
12168
|
+
const body = {
|
|
12169
|
+
payload: options.payload,
|
|
12170
|
+
content_type: options.contentType,
|
|
12171
|
+
ttl_minutes: options.ttlMinutes,
|
|
12172
|
+
kindling: options.kindling,
|
|
12173
|
+
filename: options.filename,
|
|
12174
|
+
};
|
|
12175
|
+
if (options.withKindling) {
|
|
12176
|
+
body.with_kindling = true;
|
|
12177
|
+
}
|
|
12178
|
+
const response = await this.http.post('/v1/sparks', body);
|
|
12179
|
+
const data = response.data;
|
|
12180
|
+
return {
|
|
12181
|
+
id: data.spark_id,
|
|
12182
|
+
size: data.size_bytes,
|
|
12183
|
+
status: data.status,
|
|
12184
|
+
createdAt: data.created_at,
|
|
12185
|
+
expiresAt: data.expires_at,
|
|
12186
|
+
ttlMinutes: data.ttl_minutes,
|
|
12187
|
+
filename: data.filename,
|
|
12188
|
+
kindling: data.kindling,
|
|
12189
|
+
};
|
|
12190
|
+
}
|
|
12191
|
+
/**
|
|
12192
|
+
* Delete a spark.
|
|
12193
|
+
*
|
|
12194
|
+
* @example
|
|
12195
|
+
* await sv.sparks.delete('spk_abc');
|
|
12196
|
+
*/
|
|
12197
|
+
async delete(sparkId) {
|
|
12198
|
+
requireSparkId(sparkId);
|
|
12199
|
+
await this.http.delete(`/v1/sparks/${encodeURIComponent(sparkId)}`);
|
|
12200
|
+
}
|
|
12201
|
+
/**
|
|
12202
|
+
* Share a spark via a public SparkLink (x.sv URL).
|
|
12203
|
+
*
|
|
12204
|
+
* @example
|
|
12205
|
+
* const share = await sv.sparks.share('spk_abc', { visibility: 'public' });
|
|
12206
|
+
* console.log(share.shareUrl);
|
|
12207
|
+
*/
|
|
12208
|
+
async share(sparkId, options = {}) {
|
|
12209
|
+
requireSparkId(sparkId);
|
|
12210
|
+
const response = await this.http.patch(`/v1/sparks/${encodeURIComponent(sparkId)}/share`, {
|
|
12211
|
+
visibility: options.visibility,
|
|
12212
|
+
invites: options.invites,
|
|
12213
|
+
expires_in_seconds: options.expiresInSeconds,
|
|
12214
|
+
});
|
|
12215
|
+
return mapShare(response.data);
|
|
12216
|
+
}
|
|
12217
|
+
/**
|
|
12218
|
+
* Get a spark's sharing status.
|
|
12219
|
+
*
|
|
12220
|
+
* @example
|
|
12221
|
+
* const share = await sv.sparks.getShare('spk_abc');
|
|
12222
|
+
*/
|
|
12223
|
+
async getShare(sparkId) {
|
|
12224
|
+
requireSparkId(sparkId);
|
|
12225
|
+
const response = await this.http.get(`/v1/sparks/${encodeURIComponent(sparkId)}/share`);
|
|
12226
|
+
return mapShare(response.data);
|
|
12227
|
+
}
|
|
12228
|
+
/**
|
|
12229
|
+
* Unshare a spark (delete its SparkLink).
|
|
12230
|
+
*
|
|
12231
|
+
* @example
|
|
12232
|
+
* await sv.sparks.unshare('spk_abc');
|
|
12233
|
+
*/
|
|
12234
|
+
async unshare(sparkId) {
|
|
12235
|
+
requireSparkId(sparkId);
|
|
12236
|
+
const response = await this.http.delete(`/v1/sparks/${encodeURIComponent(sparkId)}/share`);
|
|
12237
|
+
return mapShare(response.data);
|
|
12238
|
+
}
|
|
12239
|
+
}
|
|
12240
|
+
function requireSparkId(sparkId) {
|
|
12241
|
+
if (!sparkId || typeof sparkId !== 'string') {
|
|
12242
|
+
throw new ValidationError('Spark ID is required');
|
|
12243
|
+
}
|
|
12244
|
+
}
|
|
12245
|
+
function mapSummary(wire) {
|
|
12246
|
+
return {
|
|
12247
|
+
id: wire.spark_id,
|
|
12248
|
+
contentType: wire.content_type ?? 'text/plain',
|
|
12249
|
+
size: wire.size_bytes,
|
|
12250
|
+
status: wire.status,
|
|
12251
|
+
createdAt: wire.created_at,
|
|
12252
|
+
expiresAt: wire.expires_at,
|
|
12253
|
+
burnedAt: wire.burned_at ?? null,
|
|
12254
|
+
timeRemaining: wire.time_remaining ?? 0,
|
|
12255
|
+
filename: wire.filename,
|
|
12256
|
+
kindling: wire.kindling,
|
|
12257
|
+
};
|
|
12258
|
+
}
|
|
12259
|
+
function mapShare(wire) {
|
|
12260
|
+
return {
|
|
12261
|
+
shared: wire.shared,
|
|
12262
|
+
shareUrl: wire.share_url,
|
|
12263
|
+
linkCode: wire.link_code,
|
|
12264
|
+
visibility: wire.visibility,
|
|
12265
|
+
invites: wire.invites,
|
|
12266
|
+
expiresAt: wire.expires_at,
|
|
12267
|
+
createdAt: wire.created_at,
|
|
12268
|
+
};
|
|
12269
|
+
}
|
|
12270
|
+
function buildQuery(params) {
|
|
12271
|
+
const search = new URLSearchParams();
|
|
12272
|
+
for (const [key, value] of Object.entries(params)) {
|
|
12273
|
+
if (value === undefined || value === '')
|
|
12274
|
+
continue;
|
|
12275
|
+
search.append(key, String(value));
|
|
12276
|
+
}
|
|
12277
|
+
const query = search.toString();
|
|
12278
|
+
return query ? `?${query}` : '';
|
|
12279
|
+
}
|
|
12280
|
+
|
|
12281
|
+
/**
|
|
12282
|
+
* Entropy Module
|
|
12283
|
+
*
|
|
12284
|
+
* Generate cryptographically strong random values from the SparkVault
|
|
12285
|
+
* hardware-backed entropy service.
|
|
12286
|
+
*/
|
|
12287
|
+
class EntropyModule {
|
|
12288
|
+
constructor(http) {
|
|
12289
|
+
this.http = http;
|
|
12290
|
+
}
|
|
12291
|
+
/**
|
|
12292
|
+
* Generate random entropy in the requested format.
|
|
12293
|
+
*
|
|
12294
|
+
* @example
|
|
12295
|
+
* const { value } = await sv.entropy.generate({ bytes: 32, format: 'hex' });
|
|
12296
|
+
*/
|
|
12297
|
+
async generate(options) {
|
|
12298
|
+
this.validateByteCount(options.bytes);
|
|
12299
|
+
const response = await this.http.post('/v1/entropy/generate', {
|
|
12300
|
+
num_bytes: options.bytes,
|
|
12301
|
+
format: options.format ?? 'hex',
|
|
12302
|
+
});
|
|
12303
|
+
const data = response.data;
|
|
12304
|
+
return {
|
|
12305
|
+
value: data.value,
|
|
12306
|
+
numBytes: data.num_bytes,
|
|
12307
|
+
format: data.format,
|
|
12308
|
+
referenceId: data.reference_id,
|
|
12309
|
+
};
|
|
12310
|
+
}
|
|
12311
|
+
/**
|
|
12312
|
+
* Generate `n` random bytes as a Uint8Array.
|
|
12313
|
+
*
|
|
12314
|
+
* @example
|
|
12315
|
+
* const key = await sv.entropy.generateBytes(32);
|
|
12316
|
+
*/
|
|
12317
|
+
async generateBytes(n) {
|
|
12318
|
+
this.validateByteCount(n);
|
|
12319
|
+
const result = await this.generate({ bytes: n, format: 'base64url' });
|
|
12320
|
+
if (typeof result.value !== 'string' || result.value.length === 0) {
|
|
12321
|
+
throw new SparkVaultError('Entropy endpoint returned no value', 'invalid_response');
|
|
12322
|
+
}
|
|
12323
|
+
const bytes = base64urlDecode(result.value);
|
|
12324
|
+
if (bytes.length !== n) {
|
|
12325
|
+
throw new SparkVaultError(`Entropy length mismatch: expected ${n}, got ${bytes.length}`, 'invalid_response');
|
|
12326
|
+
}
|
|
12327
|
+
return bytes;
|
|
12328
|
+
}
|
|
12329
|
+
validateByteCount(bytes) {
|
|
12330
|
+
if (!Number.isInteger(bytes) || bytes < 1 || bytes > 1024) {
|
|
12331
|
+
throw new ValidationError('Entropy byte count must be an integer from 1 to 1024', {
|
|
12332
|
+
field: 'bytes',
|
|
12333
|
+
received: bytes,
|
|
12334
|
+
});
|
|
12335
|
+
}
|
|
12336
|
+
}
|
|
12337
|
+
}
|
|
12338
|
+
|
|
11143
12339
|
/**
|
|
11144
12340
|
* SparkVault Debug Logger
|
|
11145
12341
|
*
|
|
@@ -11238,7 +12434,7 @@ const logger = {
|
|
|
11238
12434
|
*
|
|
11239
12435
|
* <script>
|
|
11240
12436
|
* // SDK is ready - just use it
|
|
11241
|
-
* SparkVault.identity.attach('.login-btn', {
|
|
12437
|
+
* SparkVault.products.identity.attach('.login-btn', {
|
|
11242
12438
|
* onSuccess: (result) => console.log('Verified:', result.identity)
|
|
11243
12439
|
* });
|
|
11244
12440
|
* </script>
|
|
@@ -11290,7 +12486,7 @@ function autoInit(SparkVaultClass) {
|
|
|
11290
12486
|
/**
|
|
11291
12487
|
* SparkVault JavaScript SDK
|
|
11292
12488
|
*
|
|
11293
|
-
* A unified SDK for Identity and
|
|
12489
|
+
* A unified SDK for Identity, Vaults, Ingots, Folders, Sparks, and Entropy.
|
|
11294
12490
|
*
|
|
11295
12491
|
* @example CDN Usage (Recommended)
|
|
11296
12492
|
* ```html
|
|
@@ -11300,8 +12496,8 @@ function autoInit(SparkVaultClass) {
|
|
|
11300
12496
|
* ></script>
|
|
11301
12497
|
*
|
|
11302
12498
|
* <script>
|
|
11303
|
-
* // SDK auto-initializes. Use JavaScript API:
|
|
11304
|
-
* SparkVault.identity.attach('.login-btn', {
|
|
12499
|
+
* // SDK auto-initializes. Use the JavaScript API:
|
|
12500
|
+
* SparkVault.products.identity.attach('.login-btn', {
|
|
11305
12501
|
* onSuccess: (result) => console.log('Verified:', result.identity)
|
|
11306
12502
|
* });
|
|
11307
12503
|
* </script>
|
|
@@ -11313,7 +12509,7 @@ function autoInit(SparkVaultClass) {
|
|
|
11313
12509
|
*
|
|
11314
12510
|
* const sv = SparkVault.init({ accountId: 'acc_your_account' });
|
|
11315
12511
|
*
|
|
11316
|
-
* const result = await sv.identity.verify();
|
|
12512
|
+
* const result = await sv.products.identity.verify();
|
|
11317
12513
|
* console.log(result.identity, result.identityType);
|
|
11318
12514
|
* ```
|
|
11319
12515
|
*/
|
|
@@ -11322,8 +12518,12 @@ class SparkVault {
|
|
|
11322
12518
|
validateConfig(config);
|
|
11323
12519
|
this.config = resolveConfig(config);
|
|
11324
12520
|
const http = new HttpClient(this.config);
|
|
11325
|
-
this.
|
|
12521
|
+
this.products = new ProductsModule(this.config);
|
|
11326
12522
|
this.vaults = new VaultsModule(this.config, http);
|
|
12523
|
+
this.ingots = new IngotsModule(this.config, http);
|
|
12524
|
+
this.folders = new FoldersModule(http);
|
|
12525
|
+
this.sparks = new SparksModule(http);
|
|
12526
|
+
this.entropy = new EntropyModule(http);
|
|
11327
12527
|
this.health = new HealthModule(this.config);
|
|
11328
12528
|
}
|
|
11329
12529
|
/**
|
|
@@ -11354,15 +12554,22 @@ if (typeof window !== 'undefined') {
|
|
|
11354
12554
|
|
|
11355
12555
|
exports.AuthenticationError = AuthenticationError;
|
|
11356
12556
|
exports.AuthorizationError = AuthorizationError;
|
|
12557
|
+
exports.EntropyModule = EntropyModule;
|
|
12558
|
+
exports.FoldersModule = FoldersModule;
|
|
12559
|
+
exports.IdentityModule = IdentityModule;
|
|
12560
|
+
exports.IngotsModule = IngotsModule;
|
|
11357
12561
|
exports.NetworkError = NetworkError;
|
|
11358
12562
|
exports.PlanRequiredError = PlanRequiredError;
|
|
11359
12563
|
exports.PopupBlockedError = PopupBlockedError;
|
|
12564
|
+
exports.ProductsModule = ProductsModule;
|
|
11360
12565
|
exports.QuotaExceededError = QuotaExceededError;
|
|
11361
12566
|
exports.SparkVault = SparkVault;
|
|
11362
12567
|
exports.SparkVaultError = SparkVaultError;
|
|
12568
|
+
exports.SparksModule = SparksModule;
|
|
11363
12569
|
exports.TimeoutError = TimeoutError;
|
|
11364
12570
|
exports.UserCancelledError = UserCancelledError;
|
|
11365
12571
|
exports.ValidationError = ValidationError;
|
|
12572
|
+
exports.VaultsModule = VaultsModule;
|
|
11366
12573
|
exports.default = SparkVault;
|
|
11367
12574
|
exports.logger = logger;
|
|
11368
12575
|
exports.setDebugMode = setDebugMode;
|