@sparkvault/sdk-js 2.1.3 → 2.2.1

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.
@@ -39,9 +39,22 @@ export declare class WidgetEmbedder {
39
39
  private previousBodyOverflow;
40
40
  private readonly trustedOrigin;
41
41
  private readonly boundHandleMessage;
42
+ private readonly boundHandleCspViolation;
42
43
  constructor(config: ResolvedConfig, options: VerifyOptions, callbacks: EmbedderCallbacks);
43
44
  start(): void;
45
+ /**
46
+ * Nudge the browser to paint the just-inserted iframe on the next frame.
47
+ * Reading layout forces a reflow; toggling a transform forces the compositor
48
+ * to flush the new layer — the workaround for iOS Safari leaving a fixed
49
+ * full-viewport iframe invisible until an unrelated repaint.
50
+ */
51
+ private forceComposite;
44
52
  close(): void;
53
+ /**
54
+ * Host CSP blocked the widget iframe (frame-src/child-src). The frame can
55
+ * never load, so fail over to the in-page flow immediately.
56
+ */
57
+ private handleCspViolation;
45
58
  private clearReadyTimer;
46
59
  /**
47
60
  * The widget frame never became interactive. Tear down, release the host
@@ -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
  */
@@ -6897,7 +6897,7 @@ class IdentityRenderer {
6897
6897
  }
6898
6898
  }
6899
6899
 
6900
- /* global HTMLIFrameElement */
6900
+ /* global HTMLIFrameElement requestAnimationFrame */
6901
6901
  /**
6902
6902
  * Widget Embedder
6903
6903
  *
@@ -6945,6 +6945,7 @@ class WidgetEmbedder {
6945
6945
  // against — never the broader sparkvault.com family.
6946
6946
  this.trustedOrigin = new URL(this.config.identityBaseUrl).origin;
6947
6947
  this.boundHandleMessage = (event) => this.handleMessage(event);
6948
+ this.boundHandleCspViolation = (event) => this.handleCspViolation(event);
6948
6949
  }
6949
6950
  start() {
6950
6951
  const iframe = document.createElement('iframe');
@@ -6964,6 +6965,12 @@ class WidgetEmbedder {
6964
6965
  'background:transparent',
6965
6966
  'z-index:2147483647',
6966
6967
  ].join(';');
6968
+ // Promote to its own compositing layer up front. iOS Safari can otherwise
6969
+ // leave a freshly-inserted fixed, full-viewport iframe loaded but UNPAINTED
6970
+ // until the next composite (a scroll, resize, or app foreground) — the
6971
+ // frame even runs, but the dialog only appears after an app switch.
6972
+ iframe.style.transform = 'translateZ(0)';
6973
+ iframe.style.willChange = 'transform';
6967
6974
  // A network-level load failure (DNS, connection reset) fires onerror; an
6968
6975
  // HTTP error body or a frame blocked from framing does not, which is what
6969
6976
  // the readiness timeout below covers.
@@ -6973,9 +6980,36 @@ class WidgetEmbedder {
6973
6980
  this.previousBodyOverflow = document.body.style.overflow;
6974
6981
  document.body.style.overflow = 'hidden';
6975
6982
  window.addEventListener('message', this.boundHandleMessage);
6983
+ // A host-page CSP whose frame-src/child-src omits the Identity origin blocks
6984
+ // the iframe before it can load and fires no `onerror`. Catch the violation
6985
+ // so we fail over to the in-page flow at once instead of after the timeout.
6986
+ document.addEventListener('securitypolicyviolation', this.boundHandleCspViolation);
6976
6987
  document.body.appendChild(iframe);
6988
+ // Force the composite the iframe insertion may not have triggered on iOS,
6989
+ // so the dialog paints on the click rather than only after an app switch.
6990
+ this.forceComposite(iframe);
6977
6991
  this.readyTimer = setTimeout(() => this.handleFrameUnavailable(), FRAME_READY_TIMEOUT_MS);
6978
6992
  }
6993
+ /**
6994
+ * Nudge the browser to paint the just-inserted iframe on the next frame.
6995
+ * Reading layout forces a reflow; toggling a transform forces the compositor
6996
+ * to flush the new layer — the workaround for iOS Safari leaving a fixed
6997
+ * full-viewport iframe invisible until an unrelated repaint.
6998
+ */
6999
+ forceComposite(iframe) {
7000
+ const paint = () => {
7001
+ if (!this.iframe)
7002
+ return;
7003
+ void iframe.offsetHeight;
7004
+ iframe.style.transform = 'translateZ(0) scale(1)';
7005
+ };
7006
+ if (typeof requestAnimationFrame === 'function') {
7007
+ requestAnimationFrame(paint);
7008
+ }
7009
+ else {
7010
+ setTimeout(paint, 16);
7011
+ }
7012
+ }
6979
7013
  close() {
6980
7014
  this.clearReadyTimer();
6981
7015
  if (this.iframe?.parentNode) {
@@ -6983,8 +7017,22 @@ class WidgetEmbedder {
6983
7017
  }
6984
7018
  this.iframe = null;
6985
7019
  window.removeEventListener('message', this.boundHandleMessage);
7020
+ document.removeEventListener('securitypolicyviolation', this.boundHandleCspViolation);
6986
7021
  document.body.style.overflow = this.previousBodyOverflow;
6987
7022
  }
7023
+ /**
7024
+ * Host CSP blocked the widget iframe (frame-src/child-src). The frame can
7025
+ * never load, so fail over to the in-page flow immediately.
7026
+ */
7027
+ handleCspViolation(event) {
7028
+ const directive = event.effectiveDirective || event.violatedDirective;
7029
+ if (directive !== 'frame-src' && directive !== 'child-src')
7030
+ return;
7031
+ // blockedURI is the framed URL (sometimes just its origin); match either.
7032
+ if (typeof event.blockedURI === 'string' && event.blockedURI.startsWith(this.trustedOrigin)) {
7033
+ this.handleFrameUnavailable();
7034
+ }
7035
+ }
6988
7036
  clearReadyTimer() {
6989
7037
  if (this.readyTimer !== null) {
6990
7038
  clearTimeout(this.readyTimer);
@@ -7473,6 +7521,16 @@ class IdentityModule {
7473
7521
  });
7474
7522
  };
7475
7523
  if (!useLocal) {
7524
+ // When the preloaded config has already failed *permanently* — this origin
7525
+ // is not a verified domain, or the account hasn't configured Identity —
7526
+ // the hosted widget cannot help: it never frames an unverified origin, so
7527
+ // embedding it would only hang until the readiness timeout. Render the
7528
+ // error dialog in-page right away instead. Transient failures fall through
7529
+ // to the iframe so one-tap passkey login is preserved.
7530
+ if (await this.preloadedConfigPermanentlyFailed()) {
7531
+ startLocalRender(new ModalContainer());
7532
+ return settledPromise;
7533
+ }
7476
7534
  this.embedder = new WidgetEmbedder(this.config, mergedOptions, {
7477
7535
  ...hostCallbacks,
7478
7536
  onFrameUnavailable: () => {
@@ -7656,6 +7714,33 @@ class IdentityModule {
7656
7714
  this.renderer = null;
7657
7715
  }
7658
7716
  }
7717
+ /**
7718
+ * Whether the preloaded config has already settled as a *permanent* failure
7719
+ * the hosted widget can't surface (origin not allowed, or product not
7720
+ * configured). Used to skip the iframe and show the error dialog in-page
7721
+ * immediately. Returns false — proceed to embed — when config is not
7722
+ * preloaded, still in flight (raced against a short timeout so the common
7723
+ * path is never delayed), resolved, or failed only transiently (so one-tap
7724
+ * passkey login via the frame is preserved).
7725
+ */
7726
+ async preloadedConfigPermanentlyFailed() {
7727
+ if (!this.api.isConfigPreloaded())
7728
+ return false;
7729
+ const PENDING = Symbol('pending');
7730
+ try {
7731
+ await Promise.race([
7732
+ this.api.getConfig(),
7733
+ new Promise((resolve) => setTimeout(() => resolve(PENDING), 50)),
7734
+ ]);
7735
+ // Resolved with config, or still pending after the timeout — either way,
7736
+ // don't block the iframe path.
7737
+ return false;
7738
+ }
7739
+ catch (error) {
7740
+ return (error instanceof IdentityApiError &&
7741
+ (error.code === 'ORIGIN_NOT_ALLOWED' || error.code === 'product_not_configured'));
7742
+ }
7743
+ }
7659
7744
  /**
7660
7745
  * Resolve target to an HTMLElement.
7661
7746
  */