@sparkvault/sdk-js 1.25.1 → 1.27.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.
@@ -77,7 +77,7 @@ const DEFAULT_ALLOWED_DOWNLOAD_HOST_PATTERNS = [
77
77
  /\.cloudfront\.net$/i,
78
78
  ];
79
79
  const API_URL = 'https://api.sparkvault.com';
80
- const IDENTITY_URL = 'https://api.sparkvault.com/v1/apps/identity';
80
+ const IDENTITY_URL = 'https://api.sparkvault.com/v1/products/identity';
81
81
  function normalizeApiBaseUrl(url) {
82
82
  const trimmed = url.replace(/\/+$/, '');
83
83
  return trimmed.endsWith('/v1') ? trimmed.slice(0, -3) : trimmed;
@@ -152,6 +152,14 @@ function defaultIsRetryable(error) {
152
152
  }
153
153
  return false;
154
154
  }
155
+ /**
156
+ * Return a random unit interval value using Web Crypto.
157
+ */
158
+ function randomUnit() {
159
+ const value = new Uint32Array(1);
160
+ crypto.getRandomValues(value);
161
+ return value[0] / 0x100000000;
162
+ }
155
163
  /**
156
164
  * Calculate delay with exponential backoff and jitter
157
165
  */
@@ -160,7 +168,7 @@ function calculateDelay(attempt, baseDelayMs, maxDelayMs, jitterFactor) {
160
168
  const exponentialDelay = baseDelayMs * Math.pow(2, attempt);
161
169
  const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
162
170
  // Add jitter: random value within jitter range
163
- const jitter = cappedDelay * jitterFactor * Math.random();
171
+ const jitter = cappedDelay * jitterFactor * randomUnit();
164
172
  return cappedDelay + jitter;
165
173
  }
166
174
  /**
@@ -894,7 +902,7 @@ function generateState() {
894
902
  /**
895
903
  * Identity API Client
896
904
  *
897
- * Handles all HTTP communication with the Identity App endpoints.
905
+ * Handles all HTTP communication with the Identity Product endpoints.
898
906
  * Single responsibility: API calls only.
899
907
  */
900
908
  class IdentityApi extends SparkVaultApi {
@@ -970,8 +978,6 @@ class IdentityApi extends SparkVaultApi {
970
978
  * (email: MX lookup; phone: E.164 format)
971
979
  * - has_passkey: whether any passkeys are registered for this identity
972
980
  * (cross-tenant — single global RP)
973
- * - email_valid: legacy alias for identity_valid, present for backward
974
- * compatibility with older SDK versions
975
981
  */
976
982
  async checkPasskey(identity, identityType = 'email') {
977
983
  const body = identityType === 'phone' ? { phone: identity } : { email: identity };
@@ -1022,8 +1028,10 @@ class IdentityApi extends SparkVaultApi {
1022
1028
  /**
1023
1029
  * Start passkey registration for an identity (email or phone).
1024
1030
  */
1025
- async startPasskeyRegister(identity, identityType = 'email') {
1026
- const body = identityType === 'phone' ? { phone: identity } : { email: identity };
1031
+ async startPasskeyRegister(identity, identityType = 'email', registrationToken) {
1032
+ const body = identityType === 'phone'
1033
+ ? { phone: identity, registration_token: registrationToken }
1034
+ : { email: identity, registration_token: registrationToken };
1027
1035
  // Backend returns { options: PublicKeyCredentialCreationOptions, session: {...} }
1028
1036
  // Extract and flatten to match PasskeyChallengeResponse
1029
1037
  const response = await this.request('POST', '/passkey/register', body);
@@ -1471,10 +1479,8 @@ class PasskeyHandler {
1471
1479
  try {
1472
1480
  const response = await this.api.checkPasskey(this.state.recipient, this.state.identityType);
1473
1481
  this.state.setPasskeyStatus(response.has_passkey);
1474
- // Prefer identity_valid; fall back to email_valid for older backends.
1475
- const valid = response.identity_valid ?? response.email_valid;
1476
1482
  return {
1477
- emailValid: valid,
1483
+ emailValid: response.identity_valid,
1478
1484
  hasPasskey: response.has_passkey,
1479
1485
  };
1480
1486
  }
@@ -1487,8 +1493,8 @@ class PasskeyHandler {
1487
1493
  /**
1488
1494
  * Register a new passkey for the user via popup
1489
1495
  */
1490
- async register() {
1491
- const result = await this.openPasskeyPopup('register');
1496
+ async register(registrationToken) {
1497
+ const result = await this.openPasskeyPopup('register', registrationToken);
1492
1498
  if (result.success) {
1493
1499
  // Update state - user now has a passkey
1494
1500
  this.state.setPasskeyStatus(true);
@@ -1504,7 +1510,7 @@ class PasskeyHandler {
1504
1510
  /**
1505
1511
  * Open passkey popup and wait for result
1506
1512
  */
1507
- async openPasskeyPopup(mode) {
1513
+ async openPasskeyPopup(mode, registrationToken) {
1508
1514
  return new Promise((resolve) => {
1509
1515
  // Build popup URL. Pass identity as `email` or `phone` (mutually
1510
1516
  // exclusive) so the popup can render and POST the correct field.
@@ -1527,7 +1533,10 @@ class PasskeyHandler {
1527
1533
  session_id: authContext.simpleMode.sessionId,
1528
1534
  }));
1529
1535
  }
1530
- const popupUrl = `${this.baseUrl}/${this.accountId}/passkey/popup?${params}`;
1536
+ const hash = mode === 'register' && registrationToken
1537
+ ? `#${new URLSearchParams({ registration_token: registrationToken }).toString()}`
1538
+ : '';
1539
+ const popupUrl = `${this.baseUrl}/${this.accountId}/passkey/popup?${params}${hash}`;
1531
1540
  // Calculate popup position (centered)
1532
1541
  const left = Math.round((window.screen.width - POPUP_WIDTH) / 2);
1533
1542
  const top = Math.round((window.screen.height - POPUP_HEIGHT) / 2);
@@ -1572,7 +1581,7 @@ class PasskeyHandler {
1572
1581
  });
1573
1582
  }
1574
1583
  else {
1575
- resolve(this.handlePopupError(data.error, data.message));
1584
+ resolve(this.handlePopupError(data.error, data.message, data.errorCode));
1576
1585
  }
1577
1586
  };
1578
1587
  window.addEventListener('message', handleMessage);
@@ -1623,7 +1632,7 @@ class PasskeyHandler {
1623
1632
  /**
1624
1633
  * Handle popup error response
1625
1634
  */
1626
- handlePopupError(error, message) {
1635
+ handlePopupError(error, message, errorCode) {
1627
1636
  if (error === 'cancelled') {
1628
1637
  return {
1629
1638
  success: false,
@@ -1631,6 +1640,14 @@ class PasskeyHandler {
1631
1640
  errorType: 'cancelled',
1632
1641
  };
1633
1642
  }
1643
+ if (errorCode === 'identity_locked' || error === 'identity_locked') {
1644
+ return {
1645
+ success: false,
1646
+ error: message || 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
1647
+ errorType: 'unknown',
1648
+ errorCode: 'identity_locked',
1649
+ };
1650
+ }
1634
1651
  if (message?.includes('No passkey') || message?.includes('not found')) {
1635
1652
  return {
1636
1653
  success: false,
@@ -1642,6 +1659,7 @@ class PasskeyHandler {
1642
1659
  success: false,
1643
1660
  error: message || error || 'Passkey authentication failed',
1644
1661
  errorType: 'unknown',
1662
+ errorCode,
1645
1663
  };
1646
1664
  }
1647
1665
  }
@@ -1763,9 +1781,11 @@ class TotpHandler {
1763
1781
  }
1764
1782
  catch (error) {
1765
1783
  const message = error instanceof Error ? error.message : 'Verification failed';
1784
+ const errorCode = (error instanceof SparkVaultError) ? error.code : undefined;
1766
1785
  return {
1767
1786
  success: false,
1768
1787
  error: message,
1788
+ errorCode,
1769
1789
  };
1770
1790
  }
1771
1791
  }
@@ -4863,38 +4883,46 @@ class ErrorView {
4863
4883
  // Content section
4864
4884
  const contentSection = div();
4865
4885
  contentSection.className = 'sv-error-content';
4886
+ // A locked-down identity is a dedicated, non-error state: a specific
4887
+ // title, no debug error-code badge, and no "Try Again" (retrying the
4888
+ // login cannot clear a lockdown — only managing it at auth.sv can).
4889
+ const isLocked = this.props.code === 'identity_locked';
4866
4890
  const title = document.createElement('h3');
4867
4891
  title.className = 'sv-error-title';
4868
- title.textContent = 'Something went wrong';
4892
+ title.textContent = isLocked ? 'This SparkVault Identity is locked' : 'Something went wrong';
4869
4893
  const message = document.createElement('p');
4870
4894
  message.className = 'sv-error-message';
4871
4895
  message.textContent = this.props.message;
4872
- // Error code badge
4873
- const codeBadge = document.createElement('div');
4874
- codeBadge.className = 'sv-error-code-badge';
4875
- const codeLabel = document.createElement('span');
4876
- codeLabel.className = 'sv-error-code-label';
4877
- codeLabel.textContent = 'Error code';
4878
- const codeValue = document.createElement('code');
4879
- codeValue.className = 'sv-error-code-value';
4880
- codeValue.textContent = this.props.code;
4881
- codeBadge.appendChild(codeLabel);
4882
- codeBadge.appendChild(codeValue);
4883
4896
  contentSection.appendChild(title);
4884
4897
  contentSection.appendChild(message);
4885
- contentSection.appendChild(codeBadge);
4898
+ if (!isLocked) {
4899
+ // Error code badge — useful for debugging genuine errors only.
4900
+ const codeBadge = document.createElement('div');
4901
+ codeBadge.className = 'sv-error-code-badge';
4902
+ const codeLabel = document.createElement('span');
4903
+ codeLabel.className = 'sv-error-code-label';
4904
+ codeLabel.textContent = 'Error code';
4905
+ const codeValue = document.createElement('code');
4906
+ codeValue.className = 'sv-error-code-value';
4907
+ codeValue.textContent = this.props.code;
4908
+ codeBadge.appendChild(codeLabel);
4909
+ codeBadge.appendChild(codeValue);
4910
+ contentSection.appendChild(codeBadge);
4911
+ }
4886
4912
  // Action buttons with proper styling
4887
4913
  const buttonContainer = div();
4888
4914
  buttonContainer.className = 'sv-error-actions';
4889
- this.retryButton = document.createElement('button');
4890
- this.retryButton.className = 'sv-btn sv-btn-error-primary';
4891
- this.retryButton.textContent = 'Try Again';
4892
- this.retryButton.addEventListener('click', this.boundHandleRetry);
4915
+ if (!isLocked) {
4916
+ this.retryButton = document.createElement('button');
4917
+ this.retryButton.className = 'sv-btn sv-btn-error-primary';
4918
+ this.retryButton.textContent = 'Try Again';
4919
+ this.retryButton.addEventListener('click', this.boundHandleRetry);
4920
+ buttonContainer.appendChild(this.retryButton);
4921
+ }
4893
4922
  this.closeButton = document.createElement('button');
4894
4923
  this.closeButton.className = 'sv-btn sv-btn-error-secondary';
4895
4924
  this.closeButton.textContent = 'Close';
4896
4925
  this.closeButton.addEventListener('click', this.boundHandleClose);
4897
- buttonContainer.appendChild(this.retryButton);
4898
4926
  buttonContainer.appendChild(this.closeButton);
4899
4927
  container.appendChild(iconContainer);
4900
4928
  container.appendChild(contentSection);
@@ -5013,15 +5041,15 @@ class ServiceUnavailableView {
5013
5041
  }
5014
5042
 
5015
5043
  /**
5016
- * App Not Activated View
5044
+ * Product Not Configured View
5017
5045
  *
5018
- * Full-dialog terminal screen shown when the SparkVault Identity app has not
5019
- * been activated for the account that the JS SDK is configured against. The
5046
+ * Full-dialog terminal screen shown when the SparkVault Identity Product has
5047
+ * not been configured for the account that the JS SDK is configured against. The
5020
5048
  * audience is the website owner / developer — this is a misconfiguration
5021
5049
  * surface, not an end-user error — so the dialog drops customer branding and
5022
5050
  * presents itself as an unmistakable SparkVault notice.
5023
5051
  */
5024
- const ACTIVATION_URL = 'https://app.sparkvault.com/apps';
5052
+ const ACTIVATION_URL = 'https://app.sparkvault.com/products/identity';
5025
5053
  function createBadge() {
5026
5054
  const badge = div('sv-not-activated-badge');
5027
5055
  const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
@@ -5096,7 +5124,7 @@ function createSparkVaultMark() {
5096
5124
  mark.appendChild(label);
5097
5125
  return mark;
5098
5126
  }
5099
- class AppNotActivatedView {
5127
+ class ProductNotConfiguredView {
5100
5128
  constructor(props) {
5101
5129
  this.dismissButton = null;
5102
5130
  this.props = props;
@@ -5120,12 +5148,12 @@ class AppNotActivatedView {
5120
5148
  const title = document.createElement('h2');
5121
5149
  title.className = 'sv-not-activated-title';
5122
5150
  title.id = 'sv-modal-title';
5123
- title.textContent = 'Identity app not activated';
5151
+ title.textContent = 'Identity Product not configured';
5124
5152
  container.appendChild(title);
5125
5153
  const message = document.createElement('p');
5126
5154
  message.className = 'sv-not-activated-message';
5127
5155
  message.textContent =
5128
- 'Sign-in is unavailable because the site owner hasn’t activated SparkVault Identity for this account. Activate the app to enable passwordless authentication.';
5156
+ 'Sign-in is unavailable because the site owner has not configured SparkVault Identity for this account.';
5129
5157
  container.appendChild(message);
5130
5158
  const ownerCard = div('sv-not-activated-owner');
5131
5159
  const ownerLabel = document.createElement('span');
@@ -5134,7 +5162,7 @@ class AppNotActivatedView {
5134
5162
  const ownerHint = document.createElement('span');
5135
5163
  ownerHint.className = 'sv-not-activated-owner-hint';
5136
5164
  ownerHint.textContent =
5137
- 'Sign in to your SparkVault account and install the Identity app to start accepting sign-ins.';
5165
+ 'Sign in to SparkVault and configure the Identity Product to start accepting sign-ins.';
5138
5166
  ownerCard.appendChild(ownerLabel);
5139
5167
  ownerCard.appendChild(ownerHint);
5140
5168
  container.appendChild(ownerCard);
@@ -5145,7 +5173,7 @@ class AppNotActivatedView {
5145
5173
  cta.target = '_blank';
5146
5174
  cta.rel = 'noopener noreferrer';
5147
5175
  const ctaLabel = document.createElement('span');
5148
- ctaLabel.textContent = 'Activate on SparkVault';
5176
+ ctaLabel.textContent = 'Configure in SparkVault';
5149
5177
  cta.appendChild(ctaLabel);
5150
5178
  cta.appendChild(createExternalLinkIcon());
5151
5179
  actions.appendChild(cta);
@@ -5200,6 +5228,20 @@ class IdentityRenderer {
5200
5228
  this.verificationState.setAuthContext(options.authContext);
5201
5229
  }
5202
5230
  }
5231
+ /**
5232
+ * Fire the host's onSuccess callback, swallowing any exception so a
5233
+ * misbehaving host handler can't block the post-success redirect the
5234
+ * caller is depending on. The verify flow continues regardless.
5235
+ */
5236
+ safeOnSuccess(result) {
5237
+ try {
5238
+ this.callbacks.onSuccess(result);
5239
+ }
5240
+ catch (err) {
5241
+ // eslint-disable-next-line no-console
5242
+ console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5243
+ }
5244
+ }
5203
5245
  // Convenience accessors for state
5204
5246
  get recipient() {
5205
5247
  return this.verificationState.recipient;
@@ -5222,7 +5264,7 @@ class IdentityRenderer {
5222
5264
  if (this.api.isConfigPreloaded()) {
5223
5265
  // Config fetch already started - race it against a 50ms timeout
5224
5266
  // If config is ready (or almost ready), we skip the loading flash.
5225
- // A rejected preload (e.g. app_not_installed) must not abort start() —
5267
+ // A rejected preload (e.g. product_not_configured) must not abort start() —
5226
5268
  // swallow here and let the in-modal try/catch below route to the
5227
5269
  // correct error view.
5228
5270
  const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve(null), 50));
@@ -5276,13 +5318,13 @@ class IdentityRenderer {
5276
5318
  }
5277
5319
  /**
5278
5320
  * Map a config-fetch error to the appropriate terminal view.
5279
- * `app_not_installed` is a misconfiguration (account hasn't installed the
5280
- * Identity app) — show a developer-targeted "Not Activated" screen with a
5321
+ * `product_not_configured` is a misconfiguration (the account has not
5322
+ * configured Identity) — show a developer-targeted terminal screen with a
5281
5323
  * link to SparkVault.com instead of the generic service-unavailable view.
5282
5324
  */
5283
5325
  viewStateForConfigError(error) {
5284
- if (error instanceof IdentityApiError && error.code === 'app_not_installed') {
5285
- return { view: 'app-not-activated' };
5326
+ if (error instanceof IdentityApiError && error.code === 'product_not_configured') {
5327
+ return { view: 'product-not-configured' };
5286
5328
  }
5287
5329
  return { view: 'service-unavailable' };
5288
5330
  }
@@ -5370,7 +5412,7 @@ class IdentityRenderer {
5370
5412
  // Full-dialog terminal screens own the entire modal; toggle the container
5371
5413
  // chrome accordingly so the customer header doesn't bleed into a
5372
5414
  // SparkVault-owned message.
5373
- this.container.setMinimalChrome(this.viewState.view === 'app-not-activated');
5415
+ this.container.setMinimalChrome(this.viewState.view === 'product-not-configured');
5374
5416
  const view = this.createViewForState(this.viewState);
5375
5417
  this.currentView = view;
5376
5418
  clearChildren(body);
@@ -5472,8 +5514,8 @@ class IdentityRenderer {
5472
5514
  onRetry: () => this.retryConfigLoad(),
5473
5515
  onClose: () => this.handleClose(),
5474
5516
  });
5475
- case 'app-not-activated':
5476
- return new AppNotActivatedView({
5517
+ case 'product-not-configured':
5518
+ return new ProductNotConfiguredView({
5477
5519
  onClose: () => this.handleClose(),
5478
5520
  });
5479
5521
  }
@@ -5670,17 +5712,20 @@ class IdentityRenderer {
5670
5712
  this.close();
5671
5713
  // Hand the full result (including redirect) to the host first so the
5672
5714
  // page-level handler can act before we navigate away.
5673
- try {
5674
- this.callbacks.onSuccess(result.result);
5675
- }
5676
- catch (err) {
5677
- // eslint-disable-next-line no-console
5678
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5679
- }
5715
+ this.safeOnSuccess(result.result);
5680
5716
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5681
5717
  window.location.href = result.result.redirect;
5682
5718
  }
5683
5719
  }
5720
+ else if (result.errorCode === 'identity_locked') {
5721
+ // The identity is locked down — a dedicated, actionable state, not a
5722
+ // generic "invalid code" retry. The person must manage it at auth.sv.
5723
+ this.setState({
5724
+ view: 'error',
5725
+ code: 'identity_locked',
5726
+ message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
5727
+ });
5728
+ }
5684
5729
  else {
5685
5730
  // Check if error is expiry - auto-resend if so
5686
5731
  const errorMsg = result.error?.toLowerCase() ?? '';
@@ -5743,17 +5788,19 @@ class IdentityRenderer {
5743
5788
  : await this.passkeyHandler.verify();
5744
5789
  if (result.success && result.result) {
5745
5790
  this.close();
5746
- try {
5747
- this.callbacks.onSuccess(result.result);
5748
- }
5749
- catch (err) {
5750
- // eslint-disable-next-line no-console
5751
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5752
- }
5791
+ this.safeOnSuccess(result.result);
5753
5792
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5754
5793
  window.location.href = result.result.redirect;
5755
5794
  }
5756
5795
  }
5796
+ else if (result.errorCode === 'identity_locked') {
5797
+ // Dedicated state — retrying cannot unlock the identity.
5798
+ this.setState({
5799
+ view: 'error',
5800
+ code: 'identity_locked',
5801
+ message: 'This SparkVault Identity is locked. Visit auth.sv to manage it.',
5802
+ });
5803
+ }
5757
5804
  else {
5758
5805
  // Handle different error types
5759
5806
  if (result.errorType === 'not_found') {
@@ -5791,13 +5838,7 @@ class IdentityRenderer {
5791
5838
  if (pendingResult) {
5792
5839
  this.verificationState.setPendingResult(null);
5793
5840
  this.close();
5794
- try {
5795
- this.callbacks.onSuccess(pendingResult);
5796
- }
5797
- catch (err) {
5798
- // eslint-disable-next-line no-console
5799
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5800
- }
5841
+ this.safeOnSuccess(pendingResult);
5801
5842
  if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
5802
5843
  window.location.href = pendingResult.redirect;
5803
5844
  }
@@ -5880,17 +5921,11 @@ class IdentityRenderer {
5880
5921
  // Show loading while browser passkey dialog is active
5881
5922
  this.setState({ view: 'loading' });
5882
5923
  // Directly trigger passkey registration
5883
- const result = await this.passkeyHandler.register();
5924
+ const result = await this.passkeyHandler.register(pendingResult.token);
5884
5925
  if (result.success && result.result) {
5885
5926
  // Passkey created successfully - use the new token
5886
5927
  this.close();
5887
- try {
5888
- this.callbacks.onSuccess(result.result);
5889
- }
5890
- catch (err) {
5891
- // eslint-disable-next-line no-console
5892
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5893
- }
5928
+ this.safeOnSuccess(result.result);
5894
5929
  if (typeof result.result.redirect === 'string' && result.result.redirect) {
5895
5930
  window.location.href = result.result.redirect;
5896
5931
  }
@@ -5922,13 +5957,7 @@ class IdentityRenderer {
5922
5957
  this.verificationState.dismissPasskeyPrompt();
5923
5958
  // Complete the verification
5924
5959
  this.close();
5925
- try {
5926
- this.callbacks.onSuccess(pendingResult);
5927
- }
5928
- catch (err) {
5929
- // eslint-disable-next-line no-console
5930
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
5931
- }
5960
+ this.safeOnSuccess(pendingResult);
5932
5961
  if (typeof pendingResult.redirect === 'string' && pendingResult.redirect) {
5933
5962
  window.location.href = pendingResult.redirect;
5934
5963
  }
@@ -6033,19 +6062,6 @@ class IdentityRenderer {
6033
6062
  catch {
6034
6063
  return;
6035
6064
  }
6036
- // Diagnostic: dump the parsed message so we can see on the client
6037
- // EXACTLY what arrives. Keys + redirect prefix tell us whether the
6038
- // field made it across API Gateway. Remove once we've confirmed.
6039
- // eslint-disable-next-line no-console
6040
- console.info('[SparkVault SDK] sparklink WS message', {
6041
- type: data.type,
6042
- keys: Object.keys(data),
6043
- hasRedirect: typeof data.redirect === 'string',
6044
- redirectPrefix: typeof data.redirect === 'string'
6045
- ? data.redirect.slice(0, 80)
6046
- : null,
6047
- rawLength: event.data.length,
6048
- });
6049
6065
  switch (data.type) {
6050
6066
  case 'sparklink_verified':
6051
6067
  this.handleSparkLinkVerified({
@@ -6107,13 +6123,7 @@ class IdentityRenderer {
6107
6123
  // Hand the full result (including redirect) to the host first so the
6108
6124
  // page-level handler can do app-specific work (logging, navigation,
6109
6125
  // etc.) before we steal the browsing context with a navigation.
6110
- try {
6111
- this.callbacks.onSuccess(result);
6112
- }
6113
- catch (err) {
6114
- // eslint-disable-next-line no-console
6115
- console.warn('[SparkVault SDK] onSuccess threw, continuing to redirect', err);
6116
- }
6126
+ this.safeOnSuccess(result);
6117
6127
  // SDK-side fallback navigation. If the host already navigated this is
6118
6128
  // a no-op (the document is already unloading); if not, this rescues
6119
6129
  // simple-mode flows whose host forgot to act on result.redirect.
@@ -8628,7 +8638,9 @@ function hexToBase64url(hex) {
8628
8638
  */
8629
8639
  function generateRequestId() {
8630
8640
  const timestamp = Date.now().toString(16);
8631
- const random = Math.random().toString(16).substring(2, 10);
8641
+ const randomBytes = new Uint8Array(4);
8642
+ crypto.getRandomValues(randomBytes);
8643
+ const random = Array.from(randomBytes, byte => byte.toString(16).padStart(2, '0')).join('');
8632
8644
  return `${timestamp}${random}`;
8633
8645
  }
8634
8646
  /**
@@ -8678,6 +8690,14 @@ class UploadRenderer {
8678
8690
  this.config = config;
8679
8691
  // Update branding
8680
8692
  this.container.updateBranding(config.branding);
8693
+ // Programmatic mode: a File was supplied, so skip the drop-zone form
8694
+ // and upload it straight away. Lets a host site own file selection
8695
+ // (its own button, picker, or drop target) and hand the File to the SDK.
8696
+ if (this.options.file) {
8697
+ this.selectedFile = this.options.file;
8698
+ await this.startUpload(config);
8699
+ return;
8700
+ }
8681
8701
  // Show form
8682
8702
  this.setState({ view: 'form', config });
8683
8703
  }
@@ -9237,6 +9257,13 @@ class UploadRenderer {
9237
9257
  percentage: 100,
9238
9258
  phase: 'complete',
9239
9259
  });
9260
+ // Silent mode: skip the success screen — fire onSuccess and close so
9261
+ // the host site can render its own post-upload UI.
9262
+ if (this.options.hideSuccessScreen) {
9263
+ this.callbacks.onSuccess(result);
9264
+ this.close();
9265
+ return;
9266
+ }
9240
9267
  this.setState({ view: 'complete', result, config });
9241
9268
  // Notify success
9242
9269
  this.callbacks.onSuccess(result);