@docyrus/signin 0.12.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.
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,237 @@ 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
+ started = false;
455
+ observer = null;
456
+ scanTimer = null;
457
+ routeListener = null;
458
+ highlightTimer = null;
459
+ clickTimer = null;
460
+ annotationTimer = null;
461
+ constructor(send) {
462
+ this.send = send;
463
+ }
464
+ /**
465
+ * Begin reporting the element inventory and watching for changes.
466
+ * Idempotent. No-op outside a browser document.
467
+ */
468
+ start() {
469
+ if (this.started || typeof document === "undefined") return;
470
+ this.started = true;
471
+ this.ensureStyle();
472
+ this.postElements();
473
+ this.observer = new MutationObserver(() => this.scheduleScan());
474
+ this.observer.observe(document.body, {
475
+ childList: true,
476
+ subtree: true,
477
+ attributes: true,
478
+ attributeFilter: [
479
+ "id",
480
+ "hidden",
481
+ "aria-label",
482
+ "title",
483
+ "style"
484
+ ]
485
+ });
486
+ this.routeListener = () => this.scheduleScan();
487
+ window.addEventListener("popstate", this.routeListener);
488
+ window.addEventListener("hashchange", this.routeListener);
489
+ }
490
+ /** Stop watching and tear down any injected overlay. Idempotent. */
491
+ stop() {
492
+ if (!this.started) return;
493
+ this.started = false;
494
+ this.observer?.disconnect();
495
+ this.observer = null;
496
+ if (this.routeListener) {
497
+ window.removeEventListener("popstate", this.routeListener);
498
+ window.removeEventListener("hashchange", this.routeListener);
499
+ this.routeListener = null;
500
+ }
501
+ this.clearTimer("scanTimer");
502
+ this.clearTimer("highlightTimer");
503
+ this.clearTimer("clickTimer");
504
+ this.clearTimer("annotationTimer");
505
+ this.clearHighlight();
506
+ this.clearAnnotation();
507
+ }
508
+ /** Dispatch a host `guidy:*` command. Wired to `IframeAuth.onGuidyCommand`. */
509
+ handleCommand(message) {
510
+ if (message.type === "guidy:scan") {
511
+ this.postElements();
512
+ return;
513
+ }
514
+ if (message.type === "guidy:point") {
515
+ this.handlePoint(message);
516
+ }
517
+ }
518
+ /** Collect every visible anchor/button that exposes an id and a label. */
519
+ collectElements() {
520
+ if (typeof document === "undefined") return [];
521
+ const nodes = document.querySelectorAll("a[id], button[id]");
522
+ const seen = /* @__PURE__ */ new Set();
523
+ const result = [];
524
+ nodes.forEach((node) => {
525
+ const { id } = node;
526
+ if (!id || id.startsWith(GUIDY_INTERNAL_PREFIX) || seen.has(id)) return;
527
+ if (!isVisible(node)) return;
528
+ const label = elementLabel(node);
529
+ if (!label) return;
530
+ seen.add(id);
531
+ result.push({
532
+ id,
533
+ label,
534
+ tag: node.tagName.toLowerCase() === "a" ? "a" : "button"
535
+ });
536
+ });
537
+ return result;
538
+ }
539
+ postElements() {
540
+ if (!this.started) return;
541
+ const path = typeof window === "undefined" ? "" : window.location.pathname;
542
+ this.send({
543
+ type: "guidy:elements",
544
+ path,
545
+ elements: this.collectElements()
546
+ });
547
+ }
548
+ scheduleScan() {
549
+ if (this.scanTimer) return;
550
+ this.scanTimer = setTimeout(() => {
551
+ this.scanTimer = null;
552
+ this.postElements();
553
+ }, SCAN_DEBOUNCE_MS);
554
+ }
555
+ handlePoint(message) {
556
+ const element = typeof document === "undefined" ? null : document.getElementById(message.id);
557
+ if (!element) {
558
+ this.send({ type: "guidy:point-ack", id: message.id, ok: false });
559
+ return;
560
+ }
561
+ element.scrollIntoView({ block: "center", behavior: "smooth" });
562
+ this.highlight(element);
563
+ if (message.message) {
564
+ this.annotate(element, message.message);
565
+ }
566
+ if (message.click) {
567
+ this.clearTimer("clickTimer");
568
+ this.clickTimer = setTimeout(() => {
569
+ this.clickTimer = null;
570
+ element.click();
571
+ }, CLICK_DELAY_MS);
572
+ }
573
+ this.send({ type: "guidy:point-ack", id: message.id, ok: true });
574
+ }
575
+ ensureStyle() {
576
+ if (typeof document === "undefined") return;
577
+ if (document.getElementById(HIGHLIGHT_STYLE_ID)) return;
578
+ const style = document.createElement("style");
579
+ style.id = HIGHLIGHT_STYLE_ID;
580
+ style.textContent = `
581
+ .${HIGHLIGHT_CLASS} {
582
+ outline: 2px solid #3b82f6 !important;
583
+ outline-offset: 2px;
584
+ border-radius: 8px;
585
+ animation: guidy-bridge-ping 1s cubic-bezier(0, 0, 0.2, 1) infinite;
586
+ }
587
+ @keyframes guidy-bridge-ping {
588
+ 0% {
589
+ box-shadow: 0 0 0 0 color-mix(in srgb, #3b82f6 50%, transparent);
590
+ }
591
+ 75%, 100% {
592
+ box-shadow: 0 0 0 10px color-mix(in srgb, #3b82f6 0%, transparent);
593
+ }
594
+ }
595
+ #${ANNOTATION_ID} {
596
+ position: fixed;
597
+ z-index: 2147483647;
598
+ max-width: 240px;
599
+ padding: 8px 12px;
600
+ border-radius: 10px;
601
+ background: #111827;
602
+ color: #f9fafb;
603
+ font: 13px/1.4 system-ui, -apple-system, sans-serif;
604
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
605
+ pointer-events: none;
606
+ }
607
+ `;
608
+ document.head.appendChild(style);
609
+ }
610
+ highlight(element) {
611
+ this.clearTimer("highlightTimer");
612
+ this.clearHighlight();
613
+ element.classList.add(HIGHLIGHT_CLASS);
614
+ this.highlightTimer = setTimeout(() => {
615
+ this.highlightTimer = null;
616
+ this.clearHighlight();
617
+ }, HIGHLIGHT_DURATION_MS);
618
+ }
619
+ clearHighlight() {
620
+ if (typeof document === "undefined") return;
621
+ document.querySelectorAll(`.${HIGHLIGHT_CLASS}`).forEach((node) => node.classList.remove(HIGHLIGHT_CLASS));
622
+ }
623
+ annotate(element, message) {
624
+ if (typeof document === "undefined") return;
625
+ this.clearTimer("annotationTimer");
626
+ this.clearAnnotation();
627
+ const bubble = document.createElement("div");
628
+ bubble.id = ANNOTATION_ID;
629
+ bubble.textContent = message;
630
+ document.body.appendChild(bubble);
631
+ const rect = element.getBoundingClientRect();
632
+ const bubbleRect = bubble.getBoundingClientRect();
633
+ const gap = 10;
634
+ let top = rect.bottom + gap;
635
+ if (top + bubbleRect.height > window.innerHeight) {
636
+ top = Math.max(gap, rect.top - bubbleRect.height - gap);
637
+ }
638
+ const left = Math.min(
639
+ Math.max(gap, rect.left),
640
+ window.innerWidth - bubbleRect.width - gap
641
+ );
642
+ bubble.style.top = `${Math.round(top)}px`;
643
+ bubble.style.left = `${Math.round(left)}px`;
644
+ this.annotationTimer = setTimeout(() => {
645
+ this.annotationTimer = null;
646
+ this.clearAnnotation();
647
+ }, HIGHLIGHT_DURATION_MS);
648
+ }
649
+ clearAnnotation() {
650
+ if (typeof document === "undefined") return;
651
+ document.getElementById(ANNOTATION_ID)?.remove();
652
+ }
653
+ clearTimer(key) {
654
+ const timer = this[key];
655
+ if (timer) {
656
+ clearTimeout(timer);
657
+ this[key] = null;
658
+ }
659
+ }
660
+ };
661
+
397
662
  // src/core/sso-auth.ts
398
663
  var SsoAuth = class {
399
664
  config;
@@ -560,6 +825,7 @@ var AuthManager = class {
560
825
  user = null;
561
826
  standaloneAuth = null;
562
827
  iframeAuth = null;
828
+ guidyBridge = null;
563
829
  reactNativeAuth = null;
564
830
  tokenRefreshTimer = null;
565
831
  listeners = /* @__PURE__ */ new Set();
@@ -724,6 +990,20 @@ var AuthManager = class {
724
990
  if (this.config.syncRouteToHost) {
725
991
  this.iframeAuth.enableRouteSync();
726
992
  }
993
+ if (this.config.enableGuidyBridge) {
994
+ this.startGuidyBridge();
995
+ }
996
+ }
997
+ /**
998
+ * Start the Guidy bridge runtime: report the app's clickable inventory to
999
+ * the host and execute host scan/point/click commands. Iframe mode only.
1000
+ */
1001
+ startGuidyBridge() {
1002
+ if (!this.iframeAuth || this.guidyBridge) return;
1003
+ const bridge = new GuidyBridge((message) => this.iframeAuth?.sendToHost(message));
1004
+ this.iframeAuth.onGuidyCommand((message) => bridge.handleCommand(message));
1005
+ bridge.start();
1006
+ this.guidyBridge = bridge;
727
1007
  }
728
1008
  /**
729
1009
  * Iframe/WebView mode: start auto-syncing the embedded app's route
@@ -858,6 +1138,10 @@ var AuthManager = class {
858
1138
  await this.reactNativeAuth.logout();
859
1139
  }
860
1140
  if (this.mode === "iframe" && this.iframeAuth) {
1141
+ if (this.guidyBridge) {
1142
+ this.guidyBridge.stop();
1143
+ this.guidyBridge = null;
1144
+ }
861
1145
  this.iframeAuth.stop();
862
1146
  }
863
1147
  if (this.config.ssr) {
@@ -1028,6 +1312,10 @@ var AuthManager = class {
1028
1312
  /** Cleanup: remove listeners, timers, stop iframe auth. */
1029
1313
  destroy() {
1030
1314
  this.clearTokenRefreshTimer();
1315
+ if (this.guidyBridge) {
1316
+ this.guidyBridge.stop();
1317
+ this.guidyBridge = null;
1318
+ }
1031
1319
  if (this.iframeAuth) this.iframeAuth.stop();
1032
1320
  this.listeners.clear();
1033
1321
  this.navigationHandlers.clear();
@@ -1383,6 +1671,6 @@ function SignInButton({
1383
1671
  );
1384
1672
  }
1385
1673
 
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 };
1674
+ 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, useDocyrusHostNavigation, useDocyrusHostNotification, useDocyrusHostRouteSync };
1387
1675
  //# sourceMappingURL=index.js.map
1388
1676
  //# sourceMappingURL=index.js.map