@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.
@@ -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
@@ -75,6 +75,16 @@ export declare class IdentityModule {
75
75
  * Close the identity dialog/inline UI if open.
76
76
  */
77
77
  close(): void;
78
+ /**
79
+ * Whether the preloaded config has already settled as a *permanent* failure
80
+ * the hosted widget can't surface (origin not allowed, or product not
81
+ * configured). Used to skip the iframe and show the error dialog in-page
82
+ * immediately. Returns false — proceed to embed — when config is not
83
+ * preloaded, still in flight (raced against a short timeout so the common
84
+ * path is never delayed), resolved, or failed only transiently (so one-tap
85
+ * passkey login via the frame is preserved).
86
+ */
87
+ private preloadedConfigPermanentlyFailed;
78
88
  /**
79
89
  * Resolve target to an HTMLElement.
80
90
  */
package/dist/index.d.ts CHANGED
@@ -294,6 +294,16 @@ declare class IdentityModule {
294
294
  * Close the identity dialog/inline UI if open.
295
295
  */
296
296
  close(): void;
297
+ /**
298
+ * Whether the preloaded config has already settled as a *permanent* failure
299
+ * the hosted widget can't surface (origin not allowed, or product not
300
+ * configured). Used to skip the iframe and show the error dialog in-page
301
+ * immediately. Returns false — proceed to embed — when config is not
302
+ * preloaded, still in flight (raced against a short timeout so the common
303
+ * path is never delayed), resolved, or failed only transiently (so one-tap
304
+ * passkey login via the frame is preserved).
305
+ */
306
+ private preloadedConfigPermanentlyFailed;
297
307
  /**
298
308
  * Resolve target to an HTMLElement.
299
309
  */
@@ -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,40 @@ 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
+ // When the preloaded config has already failed *permanently* — this origin
7477
+ // is not a verified domain, or the account hasn't configured Identity —
7478
+ // the hosted widget cannot help: it never frames an unverified origin, so
7479
+ // embedding it would only hang until the readiness timeout. Render the
7480
+ // error dialog in-page right away instead. Transient failures fall through
7481
+ // to the iframe so one-tap passkey login is preserved.
7482
+ if (await this.preloadedConfigPermanentlyFailed()) {
7483
+ startLocalRender(new ModalContainer());
7484
+ return settledPromise;
7485
+ }
7486
+ this.embedder = new WidgetEmbedder(this.config, mergedOptions, {
7487
+ ...hostCallbacks,
7488
+ onFrameUnavailable: () => {
7489
+ // The hosted widget iframe could not load or mount (blocked from
7490
+ // framing, network failure, blank render, or its in-frame SDK never
7491
+ // loaded). Recover by running the verification flow in this document
7492
+ // — the same local path inline rendering uses — so login still works
7493
+ // and the host scroll lock the embedder set has already been released.
7494
+ this.embedder = null;
7495
+ startLocalRender(new ModalContainer());
7496
+ },
7497
+ });
7422
7498
  this.embedder.start();
7423
7499
  return settledPromise;
7424
7500
  }
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
- });
7501
+ startLocalRender(isInline ? this.createInlineContainer(options.target) : new ModalContainer());
7433
7502
  return settledPromise;
7434
7503
  }
7435
7504
  /**
@@ -7597,6 +7666,33 @@ class IdentityModule {
7597
7666
  this.renderer = null;
7598
7667
  }
7599
7668
  }
7669
+ /**
7670
+ * Whether the preloaded config has already settled as a *permanent* failure
7671
+ * the hosted widget can't surface (origin not allowed, or product not
7672
+ * configured). Used to skip the iframe and show the error dialog in-page
7673
+ * immediately. Returns false — proceed to embed — when config is not
7674
+ * preloaded, still in flight (raced against a short timeout so the common
7675
+ * path is never delayed), resolved, or failed only transiently (so one-tap
7676
+ * passkey login via the frame is preserved).
7677
+ */
7678
+ async preloadedConfigPermanentlyFailed() {
7679
+ if (!this.api.isConfigPreloaded())
7680
+ return false;
7681
+ const PENDING = Symbol('pending');
7682
+ try {
7683
+ await Promise.race([
7684
+ this.api.getConfig(),
7685
+ new Promise((resolve) => setTimeout(() => resolve(PENDING), 50)),
7686
+ ]);
7687
+ // Resolved with config, or still pending after the timeout — either way,
7688
+ // don't block the iframe path.
7689
+ return false;
7690
+ }
7691
+ catch (error) {
7692
+ return (error instanceof IdentityApiError &&
7693
+ (error.code === 'ORIGIN_NOT_ALLOWED' || error.code === 'product_not_configured'));
7694
+ }
7695
+ }
7600
7696
  /**
7601
7697
  * Resolve target to an HTMLElement.
7602
7698
  */