@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.
package/dist/index.js CHANGED
@@ -170,6 +170,7 @@ var IframeAuth = class {
170
170
  onTokensReceived = null;
171
171
  navigationHandlers = /* @__PURE__ */ new Set();
172
172
  notificationHandlers = /* @__PURE__ */ new Set();
173
+ guidyHandlers = /* @__PURE__ */ new Set();
173
174
  routeSyncEnabled = false;
174
175
  originalPushState = null;
175
176
  originalReplaceState = null;
@@ -202,6 +203,7 @@ var IframeAuth = class {
202
203
  this.rejectPendingRefresh("IframeAuth stopped");
203
204
  this.navigationHandlers.clear();
204
205
  this.notificationHandlers.clear();
206
+ this.guidyHandlers.clear();
205
207
  this.teardownRouteSync();
206
208
  }
207
209
  /**
@@ -293,6 +295,25 @@ var IframeAuth = class {
293
295
  this.notificationHandlers.delete(handler);
294
296
  };
295
297
  }
298
+ /**
299
+ * Subscribe to host `guidy:*` command messages (point/scan).
300
+ * Returns an unsubscribe function. Used by the Guidy bridge runtime.
301
+ */
302
+ onGuidyCommand(handler) {
303
+ this.guidyHandlers.add(handler);
304
+ return () => {
305
+ this.guidyHandlers.delete(handler);
306
+ };
307
+ }
308
+ /**
309
+ * Post an app → host message over the same validated channel used for
310
+ * route-change and token-refresh. Used by the Guidy bridge to push its
311
+ * element inventory and point acknowledgements.
312
+ */
313
+ sendToHost(message) {
314
+ if (this.stopped) return;
315
+ postToHost(message);
316
+ }
296
317
  /**
297
318
  * Request fresh tokens from the host.
298
319
  * Posts a "token-refresh-request" to the host, then waits for
@@ -345,6 +366,19 @@ var IframeAuth = class {
345
366
  }
346
367
  if (data.type === "notification") {
347
368
  this.handleNotification(data);
369
+ return;
370
+ }
371
+ if (data.type === "guidy:point" || data.type === "guidy:scan") {
372
+ this.handleGuidyCommand(data);
373
+ }
374
+ }
375
+ handleGuidyCommand(message) {
376
+ for (const handler of this.guidyHandlers) {
377
+ try {
378
+ handler(message);
379
+ } catch (err) {
380
+ console.warn("[docyrus-signin] guidy command handler threw:", err);
381
+ }
348
382
  }
349
383
  }
350
384
  handleSignin(signinData) {
@@ -394,6 +428,250 @@ var IframeAuth = class {
394
428
  }
395
429
  };
396
430
 
431
+ // src/core/guidy-bridge.ts
432
+ var GUIDY_INTERNAL_PREFIX = "guidy-";
433
+ var HIGHLIGHT_CLASS = "guidy-bridge-highlight";
434
+ var HIGHLIGHT_STYLE_ID = "guidy-bridge-style";
435
+ var ANNOTATION_ID = "guidy-bridge-annotation";
436
+ var HIGHLIGHT_DURATION_MS = 5200;
437
+ var SCAN_DEBOUNCE_MS = 250;
438
+ var CLICK_DELAY_MS = 450;
439
+ function elementLabel(node) {
440
+ const aria = node.getAttribute("aria-label")?.trim();
441
+ if (aria) return aria;
442
+ const text = node.textContent?.replace(/\s+/g, " ").trim();
443
+ if (text) return text;
444
+ return node.getAttribute("title")?.trim() ?? "";
445
+ }
446
+ function isVisible(node) {
447
+ const rect = node.getBoundingClientRect();
448
+ if (rect.width === 0 && rect.height === 0) return false;
449
+ const style = window.getComputedStyle(node);
450
+ return style.visibility !== "hidden" && style.display !== "none";
451
+ }
452
+ var GuidyBridge = class {
453
+ send;
454
+ routes;
455
+ started = false;
456
+ observer = null;
457
+ scanTimer = null;
458
+ routeListener = null;
459
+ highlightTimer = null;
460
+ clickTimer = null;
461
+ annotationTimer = null;
462
+ constructor(send, routes = []) {
463
+ this.send = send;
464
+ this.routes = routes;
465
+ }
466
+ /** Replace the declared navigable routes and re-post them to the host. */
467
+ setRoutes(routes) {
468
+ this.routes = routes;
469
+ this.postRoutes();
470
+ }
471
+ /**
472
+ * Begin reporting the element inventory and watching for changes.
473
+ * Idempotent. No-op outside a browser document.
474
+ */
475
+ start() {
476
+ if (this.started || typeof document === "undefined") return;
477
+ this.started = true;
478
+ this.ensureStyle();
479
+ this.postElements();
480
+ this.postRoutes();
481
+ this.observer = new MutationObserver(() => this.scheduleScan());
482
+ this.observer.observe(document.body, {
483
+ childList: true,
484
+ subtree: true,
485
+ attributes: true,
486
+ attributeFilter: [
487
+ "id",
488
+ "hidden",
489
+ "aria-label",
490
+ "title",
491
+ "style"
492
+ ]
493
+ });
494
+ this.routeListener = () => this.scheduleScan();
495
+ window.addEventListener("popstate", this.routeListener);
496
+ window.addEventListener("hashchange", this.routeListener);
497
+ }
498
+ /** Stop watching and tear down any injected overlay. Idempotent. */
499
+ stop() {
500
+ if (!this.started) return;
501
+ this.started = false;
502
+ this.observer?.disconnect();
503
+ this.observer = null;
504
+ if (this.routeListener) {
505
+ window.removeEventListener("popstate", this.routeListener);
506
+ window.removeEventListener("hashchange", this.routeListener);
507
+ this.routeListener = null;
508
+ }
509
+ this.clearTimer("scanTimer");
510
+ this.clearTimer("highlightTimer");
511
+ this.clearTimer("clickTimer");
512
+ this.clearTimer("annotationTimer");
513
+ this.clearHighlight();
514
+ this.clearAnnotation();
515
+ }
516
+ /** Dispatch a host `guidy:*` command. Wired to `IframeAuth.onGuidyCommand`. */
517
+ handleCommand(message) {
518
+ if (message.type === "guidy:scan") {
519
+ this.postElements();
520
+ this.postRoutes();
521
+ return;
522
+ }
523
+ if (message.type === "guidy:point") {
524
+ this.handlePoint(message);
525
+ }
526
+ }
527
+ /** Collect every visible anchor/button that exposes an id and a label. */
528
+ collectElements() {
529
+ if (typeof document === "undefined") return [];
530
+ const nodes = document.querySelectorAll("a[id], button[id]");
531
+ const seen = /* @__PURE__ */ new Set();
532
+ const result = [];
533
+ nodes.forEach((node) => {
534
+ const { id } = node;
535
+ if (!id || id.startsWith(GUIDY_INTERNAL_PREFIX) || seen.has(id)) return;
536
+ if (!isVisible(node)) return;
537
+ const label = elementLabel(node);
538
+ if (!label) return;
539
+ seen.add(id);
540
+ result.push({
541
+ id,
542
+ label,
543
+ tag: node.tagName.toLowerCase() === "a" ? "a" : "button"
544
+ });
545
+ });
546
+ return result;
547
+ }
548
+ postElements() {
549
+ if (!this.started) return;
550
+ const path = typeof window === "undefined" ? "" : window.location.pathname;
551
+ this.send({
552
+ type: "guidy:elements",
553
+ path,
554
+ elements: this.collectElements()
555
+ });
556
+ }
557
+ postRoutes() {
558
+ if (!this.started) return;
559
+ this.send({ type: "guidy:routes", routes: this.routes });
560
+ }
561
+ scheduleScan() {
562
+ if (this.scanTimer) return;
563
+ this.scanTimer = setTimeout(() => {
564
+ this.scanTimer = null;
565
+ this.postElements();
566
+ }, SCAN_DEBOUNCE_MS);
567
+ }
568
+ handlePoint(message) {
569
+ const element = typeof document === "undefined" ? null : document.getElementById(message.id);
570
+ if (!element) {
571
+ this.send({ type: "guidy:point-ack", id: message.id, ok: false });
572
+ return;
573
+ }
574
+ element.scrollIntoView({ block: "center", behavior: "smooth" });
575
+ this.highlight(element);
576
+ if (message.message) {
577
+ this.annotate(element, message.message);
578
+ }
579
+ if (message.click) {
580
+ this.clearTimer("clickTimer");
581
+ this.clickTimer = setTimeout(() => {
582
+ this.clickTimer = null;
583
+ element.click();
584
+ }, CLICK_DELAY_MS);
585
+ }
586
+ this.send({ type: "guidy:point-ack", id: message.id, ok: true });
587
+ }
588
+ ensureStyle() {
589
+ if (typeof document === "undefined") return;
590
+ if (document.getElementById(HIGHLIGHT_STYLE_ID)) return;
591
+ const style = document.createElement("style");
592
+ style.id = HIGHLIGHT_STYLE_ID;
593
+ style.textContent = `
594
+ .${HIGHLIGHT_CLASS} {
595
+ outline: 2px solid #3b82f6 !important;
596
+ outline-offset: 2px;
597
+ border-radius: 8px;
598
+ animation: guidy-bridge-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
599
+ }
600
+ @keyframes guidy-bridge-ping {
601
+ 0% {
602
+ box-shadow: 0 0 0 0 color-mix(in srgb, #3b82f6 50%, transparent);
603
+ }
604
+ 75%, 100% {
605
+ box-shadow: 0 0 0 10px color-mix(in srgb, #3b82f6 0%, transparent);
606
+ }
607
+ }
608
+ #${ANNOTATION_ID} {
609
+ position: fixed;
610
+ z-index: 2147483647;
611
+ max-width: 240px;
612
+ padding: 8px 12px;
613
+ border-radius: 10px;
614
+ background: #111827;
615
+ color: #f9fafb;
616
+ font: 13px/1.4 system-ui, -apple-system, sans-serif;
617
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
618
+ pointer-events: none;
619
+ }
620
+ `;
621
+ document.head.appendChild(style);
622
+ }
623
+ highlight(element) {
624
+ this.clearTimer("highlightTimer");
625
+ this.clearHighlight();
626
+ element.classList.add(HIGHLIGHT_CLASS);
627
+ this.highlightTimer = setTimeout(() => {
628
+ this.highlightTimer = null;
629
+ this.clearHighlight();
630
+ }, HIGHLIGHT_DURATION_MS);
631
+ }
632
+ clearHighlight() {
633
+ if (typeof document === "undefined") return;
634
+ document.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => node.classList.remove(HIGHLIGHT_CLASS));
635
+ }
636
+ annotate(element, message) {
637
+ if (typeof document === "undefined") return;
638
+ this.clearTimer("annotationTimer");
639
+ this.clearAnnotation();
640
+ const bubble = document.createElement("div");
641
+ bubble.id = ANNOTATION_ID;
642
+ bubble.textContent = message;
643
+ document.body.appendChild(bubble);
644
+ const rect = element.getBoundingClientRect();
645
+ const bubbleRect = bubble.getBoundingClientRect();
646
+ const gap = 10;
647
+ let top = rect.bottom + gap;
648
+ if (top + bubbleRect.height > window.innerHeight) {
649
+ top = Math.max(gap, rect.top - bubbleRect.height - gap);
650
+ }
651
+ const left = Math.min(
652
+ Math.max(gap, rect.left),
653
+ window.innerWidth - bubbleRect.width - gap
654
+ );
655
+ bubble.style.top = `${Math.round(top)}px`;
656
+ bubble.style.left = `${Math.round(left)}px`;
657
+ this.annotationTimer = setTimeout(() => {
658
+ this.annotationTimer = null;
659
+ this.clearAnnotation();
660
+ }, HIGHLIGHT_DURATION_MS);
661
+ }
662
+ clearAnnotation() {
663
+ if (typeof document === "undefined") return;
664
+ document.getElementById(ANNOTATION_ID)?.remove();
665
+ }
666
+ clearTimer(key) {
667
+ const timer = this[key];
668
+ if (timer) {
669
+ clearTimeout(timer);
670
+ this[key] = null;
671
+ }
672
+ }
673
+ };
674
+
397
675
  // src/core/sso-auth.ts
398
676
  var SsoAuth = class {
399
677
  config;
@@ -560,6 +838,7 @@ var AuthManager = class {
560
838
  user = null;
561
839
  standaloneAuth = null;
562
840
  iframeAuth = null;
841
+ guidyBridge = null;
563
842
  reactNativeAuth = null;
564
843
  tokenRefreshTimer = null;
565
844
  listeners = /* @__PURE__ */ new Set();
@@ -724,6 +1003,31 @@ var AuthManager = class {
724
1003
  if (this.config.syncRouteToHost) {
725
1004
  this.iframeAuth.enableRouteSync();
726
1005
  }
1006
+ if (this.config.enableGuidyBridge) {
1007
+ this.startGuidyBridge();
1008
+ }
1009
+ }
1010
+ /**
1011
+ * Start the Guidy bridge runtime: report the app's clickable inventory to
1012
+ * the host and execute host scan/point/click commands. Iframe mode only.
1013
+ */
1014
+ startGuidyBridge() {
1015
+ if (!this.iframeAuth || this.guidyBridge) return;
1016
+ const bridge = new GuidyBridge(
1017
+ (message) => this.iframeAuth?.sendToHost(message),
1018
+ this.config.guidyRoutes ?? []
1019
+ );
1020
+ this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1021
+ bridge.start();
1022
+ this.guidyBridge = bridge;
1023
+ }
1024
+ /**
1025
+ * Iframe mode: replace the routes the Guidy bridge advertises to the host.
1026
+ * No-op unless the bridge is running (`enableGuidyBridge`).
1027
+ */
1028
+ setGuidyRoutes(routes) {
1029
+ this.config.guidyRoutes = routes;
1030
+ this.guidyBridge?.setRoutes(routes);
727
1031
  }
728
1032
  /**
729
1033
  * Iframe/WebView mode: start auto-syncing the embedded app's route
@@ -858,6 +1162,10 @@ var AuthManager = class {
858
1162
  await this.reactNativeAuth.logout();
859
1163
  }
860
1164
  if (this.mode === "iframe" && this.iframeAuth) {
1165
+ if (this.guidyBridge) {
1166
+ this.guidyBridge.stop();
1167
+ this.guidyBridge = null;
1168
+ }
861
1169
  this.iframeAuth.stop();
862
1170
  }
863
1171
  if (this.config.ssr) {
@@ -1028,6 +1336,10 @@ var AuthManager = class {
1028
1336
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1029
1337
  destroy() {
1030
1338
  this.clearTokenRefreshTimer();
1339
+ if (this.guidyBridge) {
1340
+ this.guidyBridge.stop();
1341
+ this.guidyBridge = null;
1342
+ }
1031
1343
  if (this.iframeAuth) this.iframeAuth.stop();
1032
1344
  this.listeners.clear();
1033
1345
  this.navigationHandlers.clear();
@@ -1219,6 +1531,11 @@ function DocyrusAuthProvider({
1219
1531
  managerRef.current?.notifyHostRouteChange(payload);
1220
1532
  };
1221
1533
  }, []);
1534
+ const setGuidyRoutes = useMemo(() => {
1535
+ return (routes) => {
1536
+ managerRef.current?.setGuidyRoutes(routes);
1537
+ };
1538
+ }, []);
1222
1539
  const value = useMemo(() => ({
1223
1540
  status,
1224
1541
  mode,
@@ -1236,6 +1553,7 @@ function DocyrusAuthProvider({
1236
1553
  onHostNotification,
1237
1554
  enableHostRouteSync,
1238
1555
  notifyRouteChange,
1556
+ setGuidyRoutes,
1239
1557
  error
1240
1558
  }), [
1241
1559
  status,
@@ -1254,6 +1572,7 @@ function DocyrusAuthProvider({
1254
1572
  onHostNotification,
1255
1573
  enableHostRouteSync,
1256
1574
  notifyRouteChange,
1575
+ setGuidyRoutes,
1257
1576
  error
1258
1577
  ]);
1259
1578
  return /* @__PURE__ */ jsx(DocyrusAuthContext, { value, children });
@@ -1299,6 +1618,12 @@ function useDocyrusHostNotification(handler) {
1299
1618
  });
1300
1619
  }, [onHostNotification]);
1301
1620
  }
1621
+ function useDocyrusGuidyRoutes(routes) {
1622
+ const { setGuidyRoutes } = useDocyrusAuth();
1623
+ useEffect(() => {
1624
+ setGuidyRoutes(routes);
1625
+ }, [setGuidyRoutes, routes]);
1626
+ }
1302
1627
  var ICON_SIZE = 20;
1303
1628
  function DocyrusLogo({ size = ICON_SIZE }) {
1304
1629
  return /* @__PURE__ */ jsx(
@@ -1383,6 +1708,6 @@ function SignInButton({
1383
1708
  );
1384
1709
  }
1385
1710
 
1386
- export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1711
+ export { AuthManager, DEFAULT_API_URL, DEFAULT_CALLBACK_PATH, DEFAULT_OAUTH_SCOPES, DEFAULT_SSR_COOKIE_KEY, DocyrusAuthContext, DocyrusAuthProvider, GuidyBridge, IframeAuth, SignInButton, StandaloneOAuth2Auth, clearTokenCookie, detectAuthMode, getAllRoles, getTokenFromCookie, hasPermission, hasRole, syncTokenToCookie, useDocyrusAuth, useDocyrusClient, useDocyrusGuidyRoutes, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1387
1712
  //# sourceMappingURL=index.js.map
1388
1713
  //# sourceMappingURL=index.js.map