@docyrus/signin 0.12.0 → 0.14.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.
@@ -247,6 +247,7 @@ var IframeAuth = class {
247
247
  onTokensReceived = null;
248
248
  navigationHandlers = /* @__PURE__ */ new Set();
249
249
  notificationHandlers = /* @__PURE__ */ new Set();
250
+ guidyHandlers = /* @__PURE__ */ new Set();
250
251
  routeSyncEnabled = false;
251
252
  originalPushState = null;
252
253
  originalReplaceState = null;
@@ -279,6 +280,7 @@ var IframeAuth = class {
279
280
  this.rejectPendingRefresh("IframeAuth stopped");
280
281
  this.navigationHandlers.clear();
281
282
  this.notificationHandlers.clear();
283
+ this.guidyHandlers.clear();
282
284
  this.teardownRouteSync();
283
285
  }
284
286
  /**
@@ -370,6 +372,25 @@ var IframeAuth = class {
370
372
  this.notificationHandlers.delete(handler);
371
373
  };
372
374
  }
375
+ /**
376
+ * Subscribe to host `guidy:*` command messages (point/scan).
377
+ * Returns an unsubscribe function. Used by the Guidy bridge runtime.
378
+ */
379
+ onGuidyCommand(handler) {
380
+ this.guidyHandlers.add(handler);
381
+ return () => {
382
+ this.guidyHandlers.delete(handler);
383
+ };
384
+ }
385
+ /**
386
+ * Post an app → host message over the same validated channel used for
387
+ * route-change and token-refresh. Used by the Guidy bridge to push its
388
+ * element inventory and point acknowledgements.
389
+ */
390
+ sendToHost(message) {
391
+ if (this.stopped) return;
392
+ postToHost(message);
393
+ }
373
394
  /**
374
395
  * Request fresh tokens from the host.
375
396
  * Posts a "token-refresh-request" to the host, then waits for
@@ -422,6 +443,19 @@ var IframeAuth = class {
422
443
  }
423
444
  if (data.type === "notification") {
424
445
  this.handleNotification(data);
446
+ return;
447
+ }
448
+ if (data.type === "guidy:point" || data.type === "guidy:scan") {
449
+ this.handleGuidyCommand(data);
450
+ }
451
+ }
452
+ handleGuidyCommand(message) {
453
+ for (const handler of this.guidyHandlers) {
454
+ try {
455
+ handler(message);
456
+ } catch (err) {
457
+ console.warn("[docyrus-signin] guidy command handler threw:", err);
458
+ }
425
459
  }
426
460
  }
427
461
  handleSignin(signinData) {
@@ -471,6 +505,250 @@ var IframeAuth = class {
471
505
  }
472
506
  };
473
507
 
508
+ // src/core/guidy-bridge.ts
509
+ var GUIDY_INTERNAL_PREFIX = "guidy-";
510
+ var HIGHLIGHT_CLASS = "guidy-bridge-highlight";
511
+ var HIGHLIGHT_STYLE_ID = "guidy-bridge-style";
512
+ var ANNOTATION_ID = "guidy-bridge-annotation";
513
+ var HIGHLIGHT_DURATION_MS = 5200;
514
+ var SCAN_DEBOUNCE_MS = 250;
515
+ var CLICK_DELAY_MS = 450;
516
+ function elementLabel(node) {
517
+ const aria = node.getAttribute("aria-label")?.trim();
518
+ if (aria) return aria;
519
+ const text = node.textContent?.replace(/\s+/g, " ").trim();
520
+ if (text) return text;
521
+ return node.getAttribute("title")?.trim() ?? "";
522
+ }
523
+ function isVisible(node) {
524
+ const rect = node.getBoundingClientRect();
525
+ if (rect.width === 0 && rect.height === 0) return false;
526
+ const style = window.getComputedStyle(node);
527
+ return style.visibility !== "hidden" && style.display !== "none";
528
+ }
529
+ var GuidyBridge = class {
530
+ send;
531
+ routes;
532
+ started = false;
533
+ observer = null;
534
+ scanTimer = null;
535
+ routeListener = null;
536
+ highlightTimer = null;
537
+ clickTimer = null;
538
+ annotationTimer = null;
539
+ constructor(send, routes = []) {
540
+ this.send = send;
541
+ this.routes = routes;
542
+ }
543
+ /** Replace the declared navigable routes and re-post them to the host. */
544
+ setRoutes(routes) {
545
+ this.routes = routes;
546
+ this.postRoutes();
547
+ }
548
+ /**
549
+ * Begin reporting the element inventory and watching for changes.
550
+ * Idempotent. No-op outside a browser document.
551
+ */
552
+ start() {
553
+ if (this.started || typeof document === "undefined") return;
554
+ this.started = true;
555
+ this.ensureStyle();
556
+ this.postElements();
557
+ this.postRoutes();
558
+ this.observer = new MutationObserver(() => this.scheduleScan());
559
+ this.observer.observe(document.body, {
560
+ childList: true,
561
+ subtree: true,
562
+ attributes: true,
563
+ attributeFilter: [
564
+ "id",
565
+ "hidden",
566
+ "aria-label",
567
+ "title",
568
+ "style"
569
+ ]
570
+ });
571
+ this.routeListener = () => this.scheduleScan();
572
+ window.addEventListener("popstate", this.routeListener);
573
+ window.addEventListener("hashchange", this.routeListener);
574
+ }
575
+ /** Stop watching and tear down any injected overlay. Idempotent. */
576
+ stop() {
577
+ if (!this.started) return;
578
+ this.started = false;
579
+ this.observer?.disconnect();
580
+ this.observer = null;
581
+ if (this.routeListener) {
582
+ window.removeEventListener("popstate", this.routeListener);
583
+ window.removeEventListener("hashchange", this.routeListener);
584
+ this.routeListener = null;
585
+ }
586
+ this.clearTimer("scanTimer");
587
+ this.clearTimer("highlightTimer");
588
+ this.clearTimer("clickTimer");
589
+ this.clearTimer("annotationTimer");
590
+ this.clearHighlight();
591
+ this.clearAnnotation();
592
+ }
593
+ /** Dispatch a host `guidy:*` command. Wired to `IframeAuth.onGuidyCommand`. */
594
+ handleCommand(message) {
595
+ if (message.type === "guidy:scan") {
596
+ this.postElements();
597
+ this.postRoutes();
598
+ return;
599
+ }
600
+ if (message.type === "guidy:point") {
601
+ this.handlePoint(message);
602
+ }
603
+ }
604
+ /** Collect every visible anchor/button that exposes an id and a label. */
605
+ collectElements() {
606
+ if (typeof document === "undefined") return [];
607
+ const nodes = document.querySelectorAll("a[id], button[id]");
608
+ const seen = /* @__PURE__ */ new Set();
609
+ const result = [];
610
+ nodes.forEach((node) => {
611
+ const { id } = node;
612
+ if (!id || id.startsWith(GUIDY_INTERNAL_PREFIX) || seen.has(id)) return;
613
+ if (!isVisible(node)) return;
614
+ const label = elementLabel(node);
615
+ if (!label) return;
616
+ seen.add(id);
617
+ result.push({
618
+ id,
619
+ label,
620
+ tag: node.tagName.toLowerCase() === "a" ? "a" : "button"
621
+ });
622
+ });
623
+ return result;
624
+ }
625
+ postElements() {
626
+ if (!this.started) return;
627
+ const path = typeof window === "undefined" ? "" : window.location.pathname;
628
+ this.send({
629
+ type: "guidy:elements",
630
+ path,
631
+ elements: this.collectElements()
632
+ });
633
+ }
634
+ postRoutes() {
635
+ if (!this.started) return;
636
+ this.send({ type: "guidy:routes", routes: this.routes });
637
+ }
638
+ scheduleScan() {
639
+ if (this.scanTimer) return;
640
+ this.scanTimer = setTimeout(() => {
641
+ this.scanTimer = null;
642
+ this.postElements();
643
+ }, SCAN_DEBOUNCE_MS);
644
+ }
645
+ handlePoint(message) {
646
+ const element = typeof document === "undefined" ? null : document.getElementById(message.id);
647
+ if (!element) {
648
+ this.send({ type: "guidy:point-ack", id: message.id, ok: false });
649
+ return;
650
+ }
651
+ element.scrollIntoView({ block: "center", behavior: "smooth" });
652
+ this.highlight(element);
653
+ if (message.message) {
654
+ this.annotate(element, message.message);
655
+ }
656
+ if (message.click) {
657
+ this.clearTimer("clickTimer");
658
+ this.clickTimer = setTimeout(() => {
659
+ this.clickTimer = null;
660
+ element.click();
661
+ }, CLICK_DELAY_MS);
662
+ }
663
+ this.send({ type: "guidy:point-ack", id: message.id, ok: true });
664
+ }
665
+ ensureStyle() {
666
+ if (typeof document === "undefined") return;
667
+ if (document.getElementById(HIGHLIGHT_STYLE_ID)) return;
668
+ const style = document.createElement("style");
669
+ style.id = HIGHLIGHT_STYLE_ID;
670
+ style.textContent = `
671
+ .${HIGHLIGHT_CLASS} {
672
+ outline: 2px solid #3b82f6 !important;
673
+ outline-offset: 2px;
674
+ border-radius: 8px;
675
+ animation: guidy-bridge-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
676
+ }
677
+ @keyframes guidy-bridge-ping {
678
+ 0% {
679
+ box-shadow: 0 0 0 0 color-mix(in srgb, #3b82f6 50%, transparent);
680
+ }
681
+ 75%, 100% {
682
+ box-shadow: 0 0 0 10px color-mix(in srgb, #3b82f6 0%, transparent);
683
+ }
684
+ }
685
+ #${ANNOTATION_ID} {
686
+ position: fixed;
687
+ z-index: 2147483647;
688
+ max-width: 240px;
689
+ padding: 8px 12px;
690
+ border-radius: 10px;
691
+ background: #111827;
692
+ color: #f9fafb;
693
+ font: 13px/1.4 system-ui, -apple-system, sans-serif;
694
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
695
+ pointer-events: none;
696
+ }
697
+ `;
698
+ document.head.appendChild(style);
699
+ }
700
+ highlight(element) {
701
+ this.clearTimer("highlightTimer");
702
+ this.clearHighlight();
703
+ element.classList.add(HIGHLIGHT_CLASS);
704
+ this.highlightTimer = setTimeout(() => {
705
+ this.highlightTimer = null;
706
+ this.clearHighlight();
707
+ }, HIGHLIGHT_DURATION_MS);
708
+ }
709
+ clearHighlight() {
710
+ if (typeof document === "undefined") return;
711
+ document.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => node.classList.remove(HIGHLIGHT_CLASS));
712
+ }
713
+ annotate(element, message) {
714
+ if (typeof document === "undefined") return;
715
+ this.clearTimer("annotationTimer");
716
+ this.clearAnnotation();
717
+ const bubble = document.createElement("div");
718
+ bubble.id = ANNOTATION_ID;
719
+ bubble.textContent = message;
720
+ document.body.appendChild(bubble);
721
+ const rect = element.getBoundingClientRect();
722
+ const bubbleRect = bubble.getBoundingClientRect();
723
+ const gap = 10;
724
+ let top = rect.bottom + gap;
725
+ if (top + bubbleRect.height > window.innerHeight) {
726
+ top = Math.max(gap, rect.top - bubbleRect.height - gap);
727
+ }
728
+ const left = Math.min(
729
+ Math.max(gap, rect.left),
730
+ window.innerWidth - bubbleRect.width - gap
731
+ );
732
+ bubble.style.top = `${Math.round(top)}px`;
733
+ bubble.style.left = `${Math.round(left)}px`;
734
+ this.annotationTimer = setTimeout(() => {
735
+ this.annotationTimer = null;
736
+ this.clearAnnotation();
737
+ }, HIGHLIGHT_DURATION_MS);
738
+ }
739
+ clearAnnotation() {
740
+ if (typeof document === "undefined") return;
741
+ document.getElementById(ANNOTATION_ID)?.remove();
742
+ }
743
+ clearTimer(key) {
744
+ const timer = this[key];
745
+ if (timer) {
746
+ clearTimeout(timer);
747
+ this[key] = null;
748
+ }
749
+ }
750
+ };
751
+
474
752
  // src/core/sso-auth.ts
475
753
  var SsoAuth = class {
476
754
  config;
@@ -555,6 +833,7 @@ var AuthManager = class {
555
833
  user = null;
556
834
  standaloneAuth = null;
557
835
  iframeAuth = null;
836
+ guidyBridge = null;
558
837
  reactNativeAuth = null;
559
838
  tokenRefreshTimer = null;
560
839
  listeners = /* @__PURE__ */ new Set();
@@ -719,6 +998,31 @@ var AuthManager = class {
719
998
  if (this.config.syncRouteToHost) {
720
999
  this.iframeAuth.enableRouteSync();
721
1000
  }
1001
+ if (this.config.enableGuidyBridge) {
1002
+ this.startGuidyBridge();
1003
+ }
1004
+ }
1005
+ /**
1006
+ * Start the Guidy bridge runtime: report the app's clickable inventory to
1007
+ * the host and execute host scan/point/click commands. Iframe mode only.
1008
+ */
1009
+ startGuidyBridge() {
1010
+ if (!this.iframeAuth || this.guidyBridge) return;
1011
+ const bridge = new GuidyBridge(
1012
+ (message) => this.iframeAuth?.sendToHost(message),
1013
+ this.config.guidyRoutes ?? []
1014
+ );
1015
+ this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1016
+ bridge.start();
1017
+ this.guidyBridge = bridge;
1018
+ }
1019
+ /**
1020
+ * Iframe mode: replace the routes the Guidy bridge advertises to the host.
1021
+ * No-op unless the bridge is running (`enableGuidyBridge`).
1022
+ */
1023
+ setGuidyRoutes(routes) {
1024
+ this.config.guidyRoutes = routes;
1025
+ this.guidyBridge?.setRoutes(routes);
722
1026
  }
723
1027
  /**
724
1028
  * Iframe/WebView mode: start auto-syncing the embedded app's route
@@ -853,6 +1157,10 @@ var AuthManager = class {
853
1157
  await this.reactNativeAuth.logout();
854
1158
  }
855
1159
  if (this.mode === "iframe" && this.iframeAuth) {
1160
+ if (this.guidyBridge) {
1161
+ this.guidyBridge.stop();
1162
+ this.guidyBridge = null;
1163
+ }
856
1164
  this.iframeAuth.stop();
857
1165
  }
858
1166
  if (this.config.ssr) {
@@ -1023,6 +1331,10 @@ var AuthManager = class {
1023
1331
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1024
1332
  destroy() {
1025
1333
  this.clearTokenRefreshTimer();
1334
+ if (this.guidyBridge) {
1335
+ this.guidyBridge.stop();
1336
+ this.guidyBridge = null;
1337
+ }
1026
1338
  if (this.iframeAuth) this.iframeAuth.stop();
1027
1339
  this.listeners.clear();
1028
1340
  this.navigationHandlers.clear();
@@ -1214,6 +1526,11 @@ function DocyrusAuthProvider({
1214
1526
  managerRef.current?.notifyHostRouteChange(payload);
1215
1527
  };
1216
1528
  }, []);
1529
+ const setGuidyRoutes = useMemo(() => {
1530
+ return (routes) => {
1531
+ managerRef.current?.setGuidyRoutes(routes);
1532
+ };
1533
+ }, []);
1217
1534
  const value = useMemo(() => ({
1218
1535
  status,
1219
1536
  mode,
@@ -1231,6 +1548,7 @@ function DocyrusAuthProvider({
1231
1548
  onHostNotification,
1232
1549
  enableHostRouteSync,
1233
1550
  notifyRouteChange,
1551
+ setGuidyRoutes,
1234
1552
  error
1235
1553
  }), [
1236
1554
  status,
@@ -1249,6 +1567,7 @@ function DocyrusAuthProvider({
1249
1567
  onHostNotification,
1250
1568
  enableHostRouteSync,
1251
1569
  notifyRouteChange,
1570
+ setGuidyRoutes,
1252
1571
  error
1253
1572
  ]);
1254
1573
  return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });