@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.
@@ -19,6 +19,13 @@ interface EmbedderCallbacks {
19
19
  onSuccess: (result: VerifyResult) => void;
20
20
  onError: (error: Error) => void;
21
21
  onCancel: () => void;
22
+ /**
23
+ * The hosted widget frame never became interactive (blocked from framing,
24
+ * failed to load, or its in-frame SDK never mounted). The host should recover
25
+ * by running the flow in-page so login still works and the scroll lock is
26
+ * released.
27
+ */
28
+ onFrameUnavailable: () => void;
22
29
  }
23
30
  export declare class WidgetEmbedder {
24
31
  private readonly config;
@@ -26,12 +33,23 @@ export declare class WidgetEmbedder {
26
33
  private readonly callbacks;
27
34
  private iframe;
28
35
  private settled;
36
+ private ready;
37
+ private failed;
38
+ private readyTimer;
29
39
  private previousBodyOverflow;
30
40
  private readonly trustedOrigin;
31
41
  private readonly boundHandleMessage;
32
42
  constructor(config: ResolvedConfig, options: VerifyOptions, callbacks: EmbedderCallbacks);
33
43
  start(): void;
34
44
  close(): void;
45
+ private clearReadyTimer;
46
+ /**
47
+ * The widget frame never became interactive. Tear down, release the host
48
+ * scroll lock, and hand control back so the flow can run in-page. Guarded so
49
+ * a late onerror or timeout firing after a real result (or after the frame
50
+ * has proven itself with `ready`) is a no-op.
51
+ */
52
+ private handleFrameUnavailable;
35
53
  /**
36
54
  * Begin a social-login redirect from the host. State and redirect_uri are
37
55
  * the customer's top origin so `consumeRedirectResult` validates them on
@@ -6919,6 +6919,17 @@ const RESULT_TYPE = 'sparkvault-identity-result';
6919
6919
  const REDIRECT_TYPE = 'sparkvault-identity-redirect';
6920
6920
  /** Social-login start request posted by the widget. */
6921
6921
  const SOCIAL_TYPE = 'sparkvault-identity-social';
6922
+ /** Sent by the widget once its modal has mounted — proof the frame is alive. */
6923
+ const READY_TYPE = 'sparkvault-identity-ready';
6924
+ /**
6925
+ * How long to wait for the widget frame to signal it has mounted before giving
6926
+ * up and recovering in-page. The frame paints its own backdrop + spinner the
6927
+ * instant it loads and posts `ready` the moment its modal mounts; a frame that
6928
+ * is blocked from framing, 404s, or whose SDK never loads will never post — so
6929
+ * this bounds the "scroll locked, nothing on screen" failure to a few seconds
6930
+ * instead of leaving the host stuck until a manual refresh.
6931
+ */
6932
+ const FRAME_READY_TIMEOUT_MS = 8000;
6922
6933
  class WidgetEmbedder {
6923
6934
  constructor(config, options, callbacks) {
6924
6935
  this.config = config;
@@ -6926,6 +6937,9 @@ class WidgetEmbedder {
6926
6937
  this.callbacks = callbacks;
6927
6938
  this.iframe = null;
6928
6939
  this.settled = false;
6940
+ this.ready = false;
6941
+ this.failed = false;
6942
+ this.readyTimer = null;
6929
6943
  this.previousBodyOverflow = '';
6930
6944
  // Only ever trust messages from the Identity origin the SDK is configured
6931
6945
  // against — never the broader sparkvault.com family.
@@ -6950,14 +6964,20 @@ class WidgetEmbedder {
6950
6964
  'background:transparent',
6951
6965
  'z-index:2147483647',
6952
6966
  ].join(';');
6967
+ // A network-level load failure (DNS, connection reset) fires onerror; an
6968
+ // HTTP error body or a frame blocked from framing does not, which is what
6969
+ // the readiness timeout below covers.
6970
+ iframe.onerror = () => this.handleFrameUnavailable();
6953
6971
  this.iframe = iframe;
6954
6972
  // Lock host scroll while the modal (rendered inside the iframe) is open.
6955
6973
  this.previousBodyOverflow = document.body.style.overflow;
6956
6974
  document.body.style.overflow = 'hidden';
6957
6975
  window.addEventListener('message', this.boundHandleMessage);
6958
6976
  document.body.appendChild(iframe);
6977
+ this.readyTimer = setTimeout(() => this.handleFrameUnavailable(), FRAME_READY_TIMEOUT_MS);
6959
6978
  }
6960
6979
  close() {
6980
+ this.clearReadyTimer();
6961
6981
  if (this.iframe?.parentNode) {
6962
6982
  this.iframe.parentNode.removeChild(this.iframe);
6963
6983
  }
@@ -6965,6 +6985,25 @@ class WidgetEmbedder {
6965
6985
  window.removeEventListener('message', this.boundHandleMessage);
6966
6986
  document.body.style.overflow = this.previousBodyOverflow;
6967
6987
  }
6988
+ clearReadyTimer() {
6989
+ if (this.readyTimer !== null) {
6990
+ clearTimeout(this.readyTimer);
6991
+ this.readyTimer = null;
6992
+ }
6993
+ }
6994
+ /**
6995
+ * The widget frame never became interactive. Tear down, release the host
6996
+ * scroll lock, and hand control back so the flow can run in-page. Guarded so
6997
+ * a late onerror or timeout firing after a real result (or after the frame
6998
+ * has proven itself with `ready`) is a no-op.
6999
+ */
7000
+ handleFrameUnavailable() {
7001
+ if (this.settled || this.failed || this.ready)
7002
+ return;
7003
+ this.failed = true;
7004
+ this.close();
7005
+ this.callbacks.onFrameUnavailable();
7006
+ }
6968
7007
  /**
6969
7008
  * Begin a social-login redirect from the host. State and redirect_uri are
6970
7009
  * the customer's top origin so `consumeRedirectResult` validates them on
@@ -7033,6 +7072,14 @@ class WidgetEmbedder {
7033
7072
  const data = event.data;
7034
7073
  if (!data || typeof data.type !== 'string')
7035
7074
  return;
7075
+ // The frame mounted its modal — it is alive and owns the interaction now.
7076
+ // Cancel the readiness timeout so a slow-but-working frame is never torn
7077
+ // down and recovered in-page.
7078
+ if (data.type === READY_TYPE) {
7079
+ this.ready = true;
7080
+ this.clearReadyTimer();
7081
+ return;
7082
+ }
7036
7083
  // Social login: start it from the host so the OAuth state and redirect_uri
7037
7084
  // live on the customer's top origin (where the return is consumed).
7038
7085
  if (data.type === SOCIAL_TYPE && typeof data.provider === 'string') {
@@ -7043,6 +7090,7 @@ class WidgetEmbedder {
7043
7090
  // cross-origin frame, so it asks us to. Navigating away tears down the
7044
7091
  // iframe; on return the host SDK consumes the redirect token.
7045
7092
  if (data.type === REDIRECT_TYPE && typeof data.url === 'string') {
7093
+ this.clearReadyTimer();
7046
7094
  window.location.href = data.url;
7047
7095
  return;
7048
7096
  }
@@ -7417,19 +7465,30 @@ class IdentityModule {
7417
7465
  rejectRef(error);
7418
7466
  },
7419
7467
  };
7468
+ const startLocalRender = (container) => {
7469
+ this.renderer = new IdentityRenderer(container, this.api, mergedOptions, hostCallbacks);
7470
+ this.renderer.start().catch((error) => {
7471
+ options.onError?.(error);
7472
+ rejectRef(error);
7473
+ });
7474
+ };
7420
7475
  if (!useLocal) {
7421
- this.embedder = new WidgetEmbedder(this.config, mergedOptions, hostCallbacks);
7476
+ this.embedder = new WidgetEmbedder(this.config, mergedOptions, {
7477
+ ...hostCallbacks,
7478
+ onFrameUnavailable: () => {
7479
+ // The hosted widget iframe could not load or mount (blocked from
7480
+ // framing, network failure, blank render, or its in-frame SDK never
7481
+ // loaded). Recover by running the verification flow in this document
7482
+ // — the same local path inline rendering uses — so login still works
7483
+ // and the host scroll lock the embedder set has already been released.
7484
+ this.embedder = null;
7485
+ startLocalRender(new ModalContainer());
7486
+ },
7487
+ });
7422
7488
  this.embedder.start();
7423
7489
  return settledPromise;
7424
7490
  }
7425
- const container = isInline
7426
- ? this.createInlineContainer(options.target)
7427
- : new ModalContainer();
7428
- this.renderer = new IdentityRenderer(container, this.api, mergedOptions, hostCallbacks);
7429
- this.renderer.start().catch((error) => {
7430
- options.onError?.(error);
7431
- rejectRef(error);
7432
- });
7491
+ startLocalRender(isInline ? this.createInlineContainer(options.target) : new ModalContainer());
7433
7492
  return settledPromise;
7434
7493
  }
7435
7494
  /**