@sparkvault/sdk-js 2.1.2 → 2.2.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.
@@ -6915,6 +6915,17 @@ const RESULT_TYPE = 'sparkvault-identity-result';
6915
6915
  const REDIRECT_TYPE = 'sparkvault-identity-redirect';
6916
6916
  /** Social-login start request posted by the widget. */
6917
6917
  const SOCIAL_TYPE = 'sparkvault-identity-social';
6918
+ /** Sent by the widget once its modal has mounted — proof the frame is alive. */
6919
+ const READY_TYPE = 'sparkvault-identity-ready';
6920
+ /**
6921
+ * How long to wait for the widget frame to signal it has mounted before giving
6922
+ * up and recovering in-page. The frame paints its own backdrop + spinner the
6923
+ * instant it loads and posts `ready` the moment its modal mounts; a frame that
6924
+ * is blocked from framing, 404s, or whose SDK never loads will never post — so
6925
+ * this bounds the "scroll locked, nothing on screen" failure to a few seconds
6926
+ * instead of leaving the host stuck until a manual refresh.
6927
+ */
6928
+ const FRAME_READY_TIMEOUT_MS = 8000;
6918
6929
  class WidgetEmbedder {
6919
6930
  constructor(config, options, callbacks) {
6920
6931
  this.config = config;
@@ -6922,6 +6933,9 @@ class WidgetEmbedder {
6922
6933
  this.callbacks = callbacks;
6923
6934
  this.iframe = null;
6924
6935
  this.settled = false;
6936
+ this.ready = false;
6937
+ this.failed = false;
6938
+ this.readyTimer = null;
6925
6939
  this.previousBodyOverflow = '';
6926
6940
  // Only ever trust messages from the Identity origin the SDK is configured
6927
6941
  // against — never the broader sparkvault.com family.
@@ -6946,14 +6960,20 @@ class WidgetEmbedder {
6946
6960
  'background:transparent',
6947
6961
  'z-index:2147483647',
6948
6962
  ].join(';');
6963
+ // A network-level load failure (DNS, connection reset) fires onerror; an
6964
+ // HTTP error body or a frame blocked from framing does not, which is what
6965
+ // the readiness timeout below covers.
6966
+ iframe.onerror = () => this.handleFrameUnavailable();
6949
6967
  this.iframe = iframe;
6950
6968
  // Lock host scroll while the modal (rendered inside the iframe) is open.
6951
6969
  this.previousBodyOverflow = document.body.style.overflow;
6952
6970
  document.body.style.overflow = 'hidden';
6953
6971
  window.addEventListener('message', this.boundHandleMessage);
6954
6972
  document.body.appendChild(iframe);
6973
+ this.readyTimer = setTimeout(() => this.handleFrameUnavailable(), FRAME_READY_TIMEOUT_MS);
6955
6974
  }
6956
6975
  close() {
6976
+ this.clearReadyTimer();
6957
6977
  if (this.iframe?.parentNode) {
6958
6978
  this.iframe.parentNode.removeChild(this.iframe);
6959
6979
  }
@@ -6961,6 +6981,25 @@ class WidgetEmbedder {
6961
6981
  window.removeEventListener('message', this.boundHandleMessage);
6962
6982
  document.body.style.overflow = this.previousBodyOverflow;
6963
6983
  }
6984
+ clearReadyTimer() {
6985
+ if (this.readyTimer !== null) {
6986
+ clearTimeout(this.readyTimer);
6987
+ this.readyTimer = null;
6988
+ }
6989
+ }
6990
+ /**
6991
+ * The widget frame never became interactive. Tear down, release the host
6992
+ * scroll lock, and hand control back so the flow can run in-page. Guarded so
6993
+ * a late onerror or timeout firing after a real result (or after the frame
6994
+ * has proven itself with `ready`) is a no-op.
6995
+ */
6996
+ handleFrameUnavailable() {
6997
+ if (this.settled || this.failed || this.ready)
6998
+ return;
6999
+ this.failed = true;
7000
+ this.close();
7001
+ this.callbacks.onFrameUnavailable();
7002
+ }
6964
7003
  /**
6965
7004
  * Begin a social-login redirect from the host. State and redirect_uri are
6966
7005
  * the customer's top origin so `consumeRedirectResult` validates them on
@@ -7029,6 +7068,14 @@ class WidgetEmbedder {
7029
7068
  const data = event.data;
7030
7069
  if (!data || typeof data.type !== 'string')
7031
7070
  return;
7071
+ // The frame mounted its modal — it is alive and owns the interaction now.
7072
+ // Cancel the readiness timeout so a slow-but-working frame is never torn
7073
+ // down and recovered in-page.
7074
+ if (data.type === READY_TYPE) {
7075
+ this.ready = true;
7076
+ this.clearReadyTimer();
7077
+ return;
7078
+ }
7032
7079
  // Social login: start it from the host so the OAuth state and redirect_uri
7033
7080
  // live on the customer's top origin (where the return is consumed).
7034
7081
  if (data.type === SOCIAL_TYPE && typeof data.provider === 'string') {
@@ -7039,6 +7086,7 @@ class WidgetEmbedder {
7039
7086
  // cross-origin frame, so it asks us to. Navigating away tears down the
7040
7087
  // iframe; on return the host SDK consumes the redirect token.
7041
7088
  if (data.type === REDIRECT_TYPE && typeof data.url === 'string') {
7089
+ this.clearReadyTimer();
7042
7090
  window.location.href = data.url;
7043
7091
  return;
7044
7092
  }
@@ -7413,19 +7461,40 @@ class IdentityModule {
7413
7461
  rejectRef(error);
7414
7462
  },
7415
7463
  };
7464
+ const startLocalRender = (container) => {
7465
+ this.renderer = new IdentityRenderer(container, this.api, mergedOptions, hostCallbacks);
7466
+ this.renderer.start().catch((error) => {
7467
+ options.onError?.(error);
7468
+ rejectRef(error);
7469
+ });
7470
+ };
7416
7471
  if (!useLocal) {
7417
- this.embedder = new WidgetEmbedder(this.config, mergedOptions, hostCallbacks);
7472
+ // When the preloaded config has already failed *permanently* — this origin
7473
+ // is not a verified domain, or the account hasn't configured Identity —
7474
+ // the hosted widget cannot help: it never frames an unverified origin, so
7475
+ // embedding it would only hang until the readiness timeout. Render the
7476
+ // error dialog in-page right away instead. Transient failures fall through
7477
+ // to the iframe so one-tap passkey login is preserved.
7478
+ if (await this.preloadedConfigPermanentlyFailed()) {
7479
+ startLocalRender(new ModalContainer());
7480
+ return settledPromise;
7481
+ }
7482
+ this.embedder = new WidgetEmbedder(this.config, mergedOptions, {
7483
+ ...hostCallbacks,
7484
+ onFrameUnavailable: () => {
7485
+ // The hosted widget iframe could not load or mount (blocked from
7486
+ // framing, network failure, blank render, or its in-frame SDK never
7487
+ // loaded). Recover by running the verification flow in this document
7488
+ // — the same local path inline rendering uses — so login still works
7489
+ // and the host scroll lock the embedder set has already been released.
7490
+ this.embedder = null;
7491
+ startLocalRender(new ModalContainer());
7492
+ },
7493
+ });
7418
7494
  this.embedder.start();
7419
7495
  return settledPromise;
7420
7496
  }
7421
- const container = isInline
7422
- ? this.createInlineContainer(options.target)
7423
- : new ModalContainer();
7424
- this.renderer = new IdentityRenderer(container, this.api, mergedOptions, hostCallbacks);
7425
- this.renderer.start().catch((error) => {
7426
- options.onError?.(error);
7427
- rejectRef(error);
7428
- });
7497
+ startLocalRender(isInline ? this.createInlineContainer(options.target) : new ModalContainer());
7429
7498
  return settledPromise;
7430
7499
  }
7431
7500
  /**
@@ -7593,6 +7662,33 @@ class IdentityModule {
7593
7662
  this.renderer = null;
7594
7663
  }
7595
7664
  }
7665
+ /**
7666
+ * Whether the preloaded config has already settled as a *permanent* failure
7667
+ * the hosted widget can't surface (origin not allowed, or product not
7668
+ * configured). Used to skip the iframe and show the error dialog in-page
7669
+ * immediately. Returns false — proceed to embed — when config is not
7670
+ * preloaded, still in flight (raced against a short timeout so the common
7671
+ * path is never delayed), resolved, or failed only transiently (so one-tap
7672
+ * passkey login via the frame is preserved).
7673
+ */
7674
+ async preloadedConfigPermanentlyFailed() {
7675
+ if (!this.api.isConfigPreloaded())
7676
+ return false;
7677
+ const PENDING = Symbol('pending');
7678
+ try {
7679
+ await Promise.race([
7680
+ this.api.getConfig(),
7681
+ new Promise((resolve) => setTimeout(() => resolve(PENDING), 50)),
7682
+ ]);
7683
+ // Resolved with config, or still pending after the timeout — either way,
7684
+ // don't block the iframe path.
7685
+ return false;
7686
+ }
7687
+ catch (error) {
7688
+ return (error instanceof IdentityApiError &&
7689
+ (error.code === 'ORIGIN_NOT_ALLOWED' || error.code === 'product_not_configured'));
7690
+ }
7691
+ }
7596
7692
  /**
7597
7693
  * Resolve target to an HTMLElement.
7598
7694
  */