@docyrus/signin 0.11.0 → 0.13.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.
@@ -231,10 +231,13 @@ var StandaloneOAuth2Auth = class {
231
231
  // src/core/iframe-auth.ts
232
232
  function postToHost(data) {
233
233
  const rnwv = window.ReactNativeWebView;
234
+ const tap = window.__DOCYRUS_DEVTOOLS_MESSAGE_TAP__;
234
235
  if (rnwv && typeof rnwv.postMessage === "function") {
236
+ if (typeof tap === "function") tap("out", data, { transport: "webview" });
235
237
  rnwv.postMessage(JSON.stringify(data));
236
238
  return;
237
239
  }
240
+ if (typeof tap === "function") tap("out", data, { transport: "iframe", targetOrigin: "*" });
238
241
  window.parent.postMessage(data, "*");
239
242
  }
240
243
  var IframeAuth = class {
@@ -244,6 +247,7 @@ var IframeAuth = class {
244
247
  onTokensReceived = null;
245
248
  navigationHandlers = /* @__PURE__ */ new Set();
246
249
  notificationHandlers = /* @__PURE__ */ new Set();
250
+ guidyHandlers = /* @__PURE__ */ new Set();
247
251
  routeSyncEnabled = false;
248
252
  originalPushState = null;
249
253
  originalReplaceState = null;
@@ -276,6 +280,7 @@ var IframeAuth = class {
276
280
  this.rejectPendingRefresh("IframeAuth stopped");
277
281
  this.navigationHandlers.clear();
278
282
  this.notificationHandlers.clear();
283
+ this.guidyHandlers.clear();
279
284
  this.teardownRouteSync();
280
285
  }
281
286
  /**
@@ -367,6 +372,25 @@ var IframeAuth = class {
367
372
  this.notificationHandlers.delete(handler);
368
373
  };
369
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
+ }
370
394
  /**
371
395
  * Request fresh tokens from the host.
372
396
  * Posts a "token-refresh-request" to the host, then waits for
@@ -419,6 +443,19 @@ var IframeAuth = class {
419
443
  }
420
444
  if (data.type === "notification") {
421
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
+ }
422
459
  }
423
460
  }
424
461
  handleSignin(signinData) {
@@ -468,6 +505,237 @@ var IframeAuth = class {
468
505
  }
469
506
  };
470
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
+ started = false;
532
+ observer = null;
533
+ scanTimer = null;
534
+ routeListener = null;
535
+ highlightTimer = null;
536
+ clickTimer = null;
537
+ annotationTimer = null;
538
+ constructor(send) {
539
+ this.send = send;
540
+ }
541
+ /**
542
+ * Begin reporting the element inventory and watching for changes.
543
+ * Idempotent. No-op outside a browser document.
544
+ */
545
+ start() {
546
+ if (this.started || typeof document === "undefined") return;
547
+ this.started = true;
548
+ this.ensureStyle();
549
+ this.postElements();
550
+ this.observer = new MutationObserver(() => this.scheduleScan());
551
+ this.observer.observe(document.body, {
552
+ childList: true,
553
+ subtree: true,
554
+ attributes: true,
555
+ attributeFilter: [
556
+ "id",
557
+ "hidden",
558
+ "aria-label",
559
+ "title",
560
+ "style"
561
+ ]
562
+ });
563
+ this.routeListener = () => this.scheduleScan();
564
+ window.addEventListener("popstate", this.routeListener);
565
+ window.addEventListener("hashchange", this.routeListener);
566
+ }
567
+ /** Stop watching and tear down any injected overlay. Idempotent. */
568
+ stop() {
569
+ if (!this.started) return;
570
+ this.started = false;
571
+ this.observer?.disconnect();
572
+ this.observer = null;
573
+ if (this.routeListener) {
574
+ window.removeEventListener("popstate", this.routeListener);
575
+ window.removeEventListener("hashchange", this.routeListener);
576
+ this.routeListener = null;
577
+ }
578
+ this.clearTimer("scanTimer");
579
+ this.clearTimer("highlightTimer");
580
+ this.clearTimer("clickTimer");
581
+ this.clearTimer("annotationTimer");
582
+ this.clearHighlight();
583
+ this.clearAnnotation();
584
+ }
585
+ /** Dispatch a host `guidy:*` command. Wired to `IframeAuth.onGuidyCommand`. */
586
+ handleCommand(message) {
587
+ if (message.type === "guidy:scan") {
588
+ this.postElements();
589
+ return;
590
+ }
591
+ if (message.type === "guidy:point") {
592
+ this.handlePoint(message);
593
+ }
594
+ }
595
+ /** Collect every visible anchor/button that exposes an id and a label. */
596
+ collectElements() {
597
+ if (typeof document === "undefined") return [];
598
+ const nodes = document.querySelectorAll("a[id], button[id]");
599
+ const seen = /* @__PURE__ */ new Set();
600
+ const result = [];
601
+ nodes.forEach((node) => {
602
+ const { id } = node;
603
+ if (!id || id.startsWith(GUIDY_INTERNAL_PREFIX) || seen.has(id)) return;
604
+ if (!isVisible(node)) return;
605
+ const label = elementLabel(node);
606
+ if (!label) return;
607
+ seen.add(id);
608
+ result.push({
609
+ id,
610
+ label,
611
+ tag: node.tagName.toLowerCase() === "a" ? "a" : "button"
612
+ });
613
+ });
614
+ return result;
615
+ }
616
+ postElements() {
617
+ if (!this.started) return;
618
+ const path = typeof window === "undefined" ? "" : window.location.pathname;
619
+ this.send({
620
+ type: "guidy:elements",
621
+ path,
622
+ elements: this.collectElements()
623
+ });
624
+ }
625
+ scheduleScan() {
626
+ if (this.scanTimer) return;
627
+ this.scanTimer = setTimeout(() => {
628
+ this.scanTimer = null;
629
+ this.postElements();
630
+ }, SCAN_DEBOUNCE_MS);
631
+ }
632
+ handlePoint(message) {
633
+ const element = typeof document === "undefined" ? null : document.getElementById(message.id);
634
+ if (!element) {
635
+ this.send({ type: "guidy:point-ack", id: message.id, ok: false });
636
+ return;
637
+ }
638
+ element.scrollIntoView({ block: "center", behavior: "smooth" });
639
+ this.highlight(element);
640
+ if (message.message) {
641
+ this.annotate(element, message.message);
642
+ }
643
+ if (message.click) {
644
+ this.clearTimer("clickTimer");
645
+ this.clickTimer = setTimeout(() => {
646
+ this.clickTimer = null;
647
+ element.click();
648
+ }, CLICK_DELAY_MS);
649
+ }
650
+ this.send({ type: "guidy:point-ack", id: message.id, ok: true });
651
+ }
652
+ ensureStyle() {
653
+ if (typeof document === "undefined") return;
654
+ if (document.getElementById(HIGHLIGHT_STYLE_ID)) return;
655
+ const style = document.createElement("style");
656
+ style.id = HIGHLIGHT_STYLE_ID;
657
+ style.textContent = `
658
+ .${HIGHLIGHT_CLASS} {
659
+ outline: 2px solid #3b82f6 !important;
660
+ outline-offset: 2px;
661
+ border-radius: 8px;
662
+ animation: guidy-bridge-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
663
+ }
664
+ @keyframes guidy-bridge-ping {
665
+ 0% {
666
+ box-shadow: 0 0 0 0 color-mix(in srgb, #3b82f6 50%, transparent);
667
+ }
668
+ 75%, 100% {
669
+ box-shadow: 0 0 0 10px color-mix(in srgb, #3b82f6 0%, transparent);
670
+ }
671
+ }
672
+ #${ANNOTATION_ID} {
673
+ position: fixed;
674
+ z-index: 2147483647;
675
+ max-width: 240px;
676
+ padding: 8px 12px;
677
+ border-radius: 10px;
678
+ background: #111827;
679
+ color: #f9fafb;
680
+ font: 13px/1.4 system-ui, -apple-system, sans-serif;
681
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
682
+ pointer-events: none;
683
+ }
684
+ `;
685
+ document.head.appendChild(style);
686
+ }
687
+ highlight(element) {
688
+ this.clearTimer("highlightTimer");
689
+ this.clearHighlight();
690
+ element.classList.add(HIGHLIGHT_CLASS);
691
+ this.highlightTimer = setTimeout(() => {
692
+ this.highlightTimer = null;
693
+ this.clearHighlight();
694
+ }, HIGHLIGHT_DURATION_MS);
695
+ }
696
+ clearHighlight() {
697
+ if (typeof document === "undefined") return;
698
+ document.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => node.classList.remove(HIGHLIGHT_CLASS));
699
+ }
700
+ annotate(element, message) {
701
+ if (typeof document === "undefined") return;
702
+ this.clearTimer("annotationTimer");
703
+ this.clearAnnotation();
704
+ const bubble = document.createElement("div");
705
+ bubble.id = ANNOTATION_ID;
706
+ bubble.textContent = message;
707
+ document.body.appendChild(bubble);
708
+ const rect = element.getBoundingClientRect();
709
+ const bubbleRect = bubble.getBoundingClientRect();
710
+ const gap = 10;
711
+ let top = rect.bottom + gap;
712
+ if (top + bubbleRect.height > window.innerHeight) {
713
+ top = Math.max(gap, rect.top - bubbleRect.height - gap);
714
+ }
715
+ const left = Math.min(
716
+ Math.max(gap, rect.left),
717
+ window.innerWidth - bubbleRect.width - gap
718
+ );
719
+ bubble.style.top = `${Math.round(top)}px`;
720
+ bubble.style.left = `${Math.round(left)}px`;
721
+ this.annotationTimer = setTimeout(() => {
722
+ this.annotationTimer = null;
723
+ this.clearAnnotation();
724
+ }, HIGHLIGHT_DURATION_MS);
725
+ }
726
+ clearAnnotation() {
727
+ if (typeof document === "undefined") return;
728
+ document.getElementById(ANNOTATION_ID)?.remove();
729
+ }
730
+ clearTimer(key) {
731
+ const timer = this[key];
732
+ if (timer) {
733
+ clearTimeout(timer);
734
+ this[key] = null;
735
+ }
736
+ }
737
+ };
738
+
471
739
  // src/core/sso-auth.ts
472
740
  var SsoAuth = class {
473
741
  config;
@@ -552,6 +820,7 @@ var AuthManager = class {
552
820
  user = null;
553
821
  standaloneAuth = null;
554
822
  iframeAuth = null;
823
+ guidyBridge = null;
555
824
  reactNativeAuth = null;
556
825
  tokenRefreshTimer = null;
557
826
  listeners = /* @__PURE__ */ new Set();
@@ -716,6 +985,20 @@ var AuthManager = class {
716
985
  if (this.config.syncRouteToHost) {
717
986
  this.iframeAuth.enableRouteSync();
718
987
  }
988
+ if (this.config.enableGuidyBridge) {
989
+ this.startGuidyBridge();
990
+ }
991
+ }
992
+ /**
993
+ * Start the Guidy bridge runtime: report the app's clickable inventory to
994
+ * the host and execute host scan/point/click commands. Iframe mode only.
995
+ */
996
+ startGuidyBridge() {
997
+ if (!this.iframeAuth || this.guidyBridge) return;
998
+ const bridge = new GuidyBridge((message) => this.iframeAuth?.sendToHost(message));
999
+ this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1000
+ bridge.start();
1001
+ this.guidyBridge = bridge;
719
1002
  }
720
1003
  /**
721
1004
  * Iframe/WebView mode: start auto-syncing the embedded app's route
@@ -850,6 +1133,10 @@ var AuthManager = class {
850
1133
  await this.reactNativeAuth.logout();
851
1134
  }
852
1135
  if (this.mode === "iframe" && this.iframeAuth) {
1136
+ if (this.guidyBridge) {
1137
+ this.guidyBridge.stop();
1138
+ this.guidyBridge = null;
1139
+ }
853
1140
  this.iframeAuth.stop();
854
1141
  }
855
1142
  if (this.config.ssr) {
@@ -1020,6 +1307,10 @@ var AuthManager = class {
1020
1307
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1021
1308
  destroy() {
1022
1309
  this.clearTokenRefreshTimer();
1310
+ if (this.guidyBridge) {
1311
+ this.guidyBridge.stop();
1312
+ this.guidyBridge = null;
1313
+ }
1023
1314
  if (this.iframeAuth) this.iframeAuth.stop();
1024
1315
  this.listeners.clear();
1025
1316
  this.navigationHandlers.clear();