@sparkvault/sdk-js 2.1.2 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,30 @@ 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
+ this.embedder = new WidgetEmbedder(this.config, mergedOptions, {
7473
+ ...hostCallbacks,
7474
+ onFrameUnavailable: () => {
7475
+ // The hosted widget iframe could not load or mount (blocked from
7476
+ // framing, network failure, blank render, or its in-frame SDK never
7477
+ // loaded). Recover by running the verification flow in this document
7478
+ // — the same local path inline rendering uses — so login still works
7479
+ // and the host scroll lock the embedder set has already been released.
7480
+ this.embedder = null;
7481
+ startLocalRender(new ModalContainer());
7482
+ },
7483
+ });
7418
7484
  this.embedder.start();
7419
7485
  return settledPromise;
7420
7486
  }
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
- });
7487
+ startLocalRender(isInline ? this.createInlineContainer(options.target) : new ModalContainer());
7429
7488
  return settledPromise;
7430
7489
  }
7431
7490
  /**