@qontinui/ui-bridge-wrapper 0.6.0 → 0.7.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.
@@ -4,13 +4,55 @@
4
4
  var url = require('url');
5
5
  var fs = require('fs');
6
6
  var module$1 = require('module');
7
+ var ui_bridge_cli_args_star = require('@qontinui/ui-bridge-cli-args');
7
8
 
8
9
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
10
+ function _interopNamespace(e) {
11
+ if (e && e.__esModule) return e;
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () { return e[k]; }
20
+ });
21
+ }
22
+ });
23
+ }
24
+ n.default = e;
25
+ return Object.freeze(n);
26
+ }
27
+
28
+ var ui_bridge_cli_args_star__namespace = /*#__PURE__*/_interopNamespace(ui_bridge_cli_args_star);
29
+
30
+ var __defProp = Object.defineProperty;
31
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
32
+ var __getOwnPropNames = Object.getOwnPropertyNames;
33
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
34
+ var __copyProps = (to, from, except, desc) => {
35
+ if (from && typeof from === "object" || typeof from === "function") {
36
+ for (let key of __getOwnPropNames(from))
37
+ if (!__hasOwnProp.call(to, key) && key !== except)
38
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
39
+ }
40
+ return to;
41
+ };
42
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget);
43
+
9
44
  // src/types.ts
10
45
  var WrapperTransportError = class extends Error {
11
46
  code;
12
47
  retryable;
13
48
  cause;
49
+ /**
50
+ * Optional structured diagnostic payload (error-code-specific shape) so
51
+ * programmatic callers can branch on data instead of parsing the message —
52
+ * e.g. `INJECTED_EXPECT_SELECTOR_UNMET` attaches
53
+ * `{ registeredElements, elementCount }`.
54
+ */
55
+ details;
14
56
  constructor(code, message, options = {}) {
15
57
  super(message);
16
58
  this.name = "WrapperTransportError";
@@ -19,6 +61,9 @@ var WrapperTransportError = class extends Error {
19
61
  if (options.cause !== void 0) {
20
62
  this.cause = options.cause;
21
63
  }
64
+ if (options.details !== void 0) {
65
+ this.details = options.details;
66
+ }
22
67
  }
23
68
  };
24
69
 
@@ -349,6 +394,8 @@ var HeadedTransport = class extends HeadlessTransport {
349
394
  };
350
395
  var require2 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('login-web-cli.cjs', document.baseURI).href)));
351
396
  var cachedBundle = null;
397
+ var DIGEST_MAX_ELEMENTS = 10;
398
+ var DIGEST_TIMEOUT_MS = 5e3;
352
399
  function loadInjectedBundle() {
353
400
  if (cachedBundle !== null) return cachedBundle;
354
401
  let bundlePath;
@@ -475,14 +522,66 @@ var InjectedTransport = class extends HeadlessTransport {
475
522
  }
476
523
  if (state.settledByTimeout && !state.expectSatisfied) {
477
524
  const sel = this.injected.expectSelector;
525
+ const baseMessage = sel ? `The injected runtime settled at its cap without the expected control (selector '${sel}') appearing \u2014 ${state.elementCount} element(s) were registered, but none matched. The control may mount slower than the settle cap (raise options.settleTimeoutMs), the selector may be wrong, or the inject failed. Treat as BLOCKED/UNVERIFIED, not a clean page.` : `The injected runtime settled at its cap with an empty registry (0 elements). The page never rendered interactive content within the settle cap, or the inject failed. Raise options.settleTimeoutMs for a slow page; otherwise treat as BLOCKED/UNVERIFIED.`;
526
+ const digest = await this.registeredElementDigest(page);
527
+ if (digest === null) {
528
+ throw new WrapperTransportError("INJECTED_EXPECT_SELECTOR_UNMET", baseMessage, {
529
+ retryable: true
530
+ });
531
+ }
532
+ const summary = digest.length > 0 ? ` Registered: ${digest.map((el) => el.role ? `${el.id} (${el.role})` : el.id).join(", ")}${state.elementCount > digest.length ? ", \u2026" : ""}` : "";
478
533
  throw new WrapperTransportError(
479
534
  "INJECTED_EXPECT_SELECTOR_UNMET",
480
- sel ? `The injected runtime settled at its cap without the expected control (selector '${sel}') appearing \u2014 ${state.elementCount} element(s) were registered, but none matched. The control may mount slower than the settle cap (raise options.settleTimeoutMs), the selector may be wrong, or the inject failed. Treat as BLOCKED/UNVERIFIED, not a clean page.` : `The injected runtime settled at its cap with an empty registry (0 elements). The page never rendered interactive content within the settle cap, or the inject failed. Raise options.settleTimeoutMs for a slow page; otherwise treat as BLOCKED/UNVERIFIED.`,
481
- { retryable: true }
535
+ `${baseMessage}${summary}`,
536
+ {
537
+ retryable: true,
538
+ details: { registeredElements: digest, elementCount: state.elementCount }
539
+ }
482
540
  );
483
541
  }
484
542
  }
485
543
  }
544
+ /**
545
+ * Best-effort digest of the first {@link DIGEST_MAX_ELEMENTS} registered
546
+ * interactive elements, read through the in-page dispatcher's
547
+ * `getControlSnapshot` action (the snapshot's per-element projection is
548
+ * already redaction-safe — we never read raw DOM labels here). Returns
549
+ * `null` on ANY failure or timeout: this feeds error diagnostics only, and a
550
+ * broken page realm must never mask the error being reported.
551
+ */
552
+ async registeredElementDigest(page) {
553
+ let timer;
554
+ try {
555
+ const snapshotPromise = page.evaluate(
556
+ () => window.__uiBridgeInjected.execute("getControlSnapshot", { skipSettle: true })
557
+ );
558
+ const snapshot = await Promise.race([
559
+ snapshotPromise,
560
+ new Promise((_, reject) => {
561
+ timer = setTimeout(() => reject(new Error("digest fetch timed out")), DIGEST_TIMEOUT_MS);
562
+ })
563
+ ]);
564
+ if (!snapshot || typeof snapshot !== "object") return null;
565
+ const elements = snapshot.elements;
566
+ if (!Array.isArray(elements)) return null;
567
+ const digest = [];
568
+ for (const el of elements.slice(0, DIGEST_MAX_ELEMENTS)) {
569
+ if (!el || typeof el !== "object") continue;
570
+ const e = el;
571
+ if (typeof e.id !== "string") continue;
572
+ digest.push({
573
+ id: e.id,
574
+ ...typeof e.role === "string" ? { role: e.role } : {},
575
+ ...typeof e.label === "string" ? { label: e.label } : {}
576
+ });
577
+ }
578
+ return digest;
579
+ } catch {
580
+ return null;
581
+ } finally {
582
+ if (timer !== void 0) clearTimeout(timer);
583
+ }
584
+ }
486
585
  async buildContext() {
487
586
  if (!this.tab) {
488
587
  throw new WrapperTransportError(
@@ -751,6 +850,16 @@ function createTransport(config) {
751
850
  }
752
851
 
753
852
  // src/session-storage-capture.ts
853
+ var SESSION_STORAGE_EXCLUDED_KEYS = ["__uiBridge_tabId"];
854
+ function withoutExcludedKeys(kv) {
855
+ const excluded = SESSION_STORAGE_EXCLUDED_KEYS;
856
+ const out = {};
857
+ for (const key of Object.keys(kv)) {
858
+ if (excluded.includes(key)) continue;
859
+ out[key] = kv[key];
860
+ }
861
+ return out;
862
+ }
754
863
  function isCapturableOrigin(origin) {
755
864
  return typeof origin === "string" && origin.length > 0 && origin !== "null";
756
865
  }
@@ -768,7 +877,8 @@ function buildMultiOriginSessionStorageMap(frames) {
768
877
  if (frame.kv === null) continue;
769
878
  const origin = frameOrigin(frame.url);
770
879
  if (origin === null) continue;
771
- out[origin] = { ...out[origin] ?? {}, ...frame.kv };
880
+ const kv = withoutExcludedKeys(frame.kv);
881
+ out[origin] = { ...out[origin] ?? {}, ...kv };
772
882
  }
773
883
  return out;
774
884
  }
@@ -776,72 +886,15 @@ function mergeMultiOriginSessionStorageIntoArtifact(artifact, map) {
776
886
  const existing = artifact.__sessionStorage && typeof artifact.__sessionStorage === "object" ? artifact.__sessionStorage : {};
777
887
  const merged = { ...existing };
778
888
  for (const origin of Object.keys(map)) {
779
- merged[origin] = { ...map[origin] };
889
+ merged[origin] = withoutExcludedKeys(map[origin]);
780
890
  }
781
891
  artifact.__sessionStorage = merged;
782
892
  return artifact;
783
893
  }
784
894
 
785
895
  // src/cli-args.ts
786
- var COMMON_FLAGS = /* @__PURE__ */ new Set(["--help", "-h", "--quiet"]);
787
- function isFlagShaped(value) {
788
- return value.startsWith("--");
789
- }
790
- function consumeValue(flag, raw, error) {
791
- if (raw === void 0) return null;
792
- if (isFlagShaped(raw)) {
793
- throw error(`${flag} expects a value but got the flag '${raw}' \u2014 did you omit ${flag}'s argument?`);
794
- }
795
- return raw;
796
- }
797
- function rejectEmptyValue(flag, value, error) {
798
- if (value !== null && value.trim() === "") {
799
- throw error(
800
- `${flag} was given an empty value \u2014 omit the flag to use its default, or pass a non-empty value`
801
- );
802
- }
803
- return value;
804
- }
805
- var ArgReader = class {
806
- argv;
807
- error;
808
- /**
809
- * @param argv Already sliced past `node script`.
810
- * @param known All flags the bin recognizes (union the bin's set with {@link COMMON_FLAGS}).
811
- * @param error Factory producing the bin's domain error type.
812
- */
813
- constructor(argv, known, error) {
814
- this.argv = argv;
815
- this.error = error;
816
- for (const arg of argv) {
817
- if (isFlagShaped(arg) && !known.has(arg)) {
818
- throw error(`Unknown flag: ${arg}`);
819
- }
820
- }
821
- }
822
- /** True if `name` appears as a bare boolean flag. */
823
- has(name) {
824
- return this.argv.includes(name);
825
- }
826
- /**
827
- * Value following the value-flag `name`, or `null` when the flag is absent OR
828
- * has no following token (the caller defaults it). Throws when the following
829
- * token is flag-shaped — the swallow this kit exists to kill. Delegates to
830
- * {@link consumeValue} so the guard (and its message) live in exactly one
831
- * place for both the indexOf-style and switch-style parsers.
832
- */
833
- value(name) {
834
- const i = this.argv.indexOf(name);
835
- if (i < 0 || i + 1 >= this.argv.length) return null;
836
- return consumeValue(name, this.argv[i + 1], this.error);
837
- }
838
- };
839
- function makeLogger(prefix, quiet) {
840
- if (quiet) return () => {
841
- };
842
- return (m) => process.stderr.write(`${prefix} ${m}
843
- `) ;
844
- }
896
+ var cli_args_exports = {};
897
+ __reExport(cli_args_exports, ui_bridge_cli_args_star__namespace);
845
898
 
846
899
  // src/login-drive.ts
847
900
  var isCognito = (u) => /auth\.qontinui\.io|amazoncognito\.com|\/oauth2\//i.test(u);
@@ -1039,7 +1092,7 @@ var LoginCliArgError = class extends Error {
1039
1092
  };
1040
1093
  function parseArgs(argv) {
1041
1094
  const mkError = (m) => new LoginCliArgError(m);
1042
- const reader = new ArgReader(argv, KNOWN_FLAGS, mkError);
1095
+ const reader = new cli_args_exports.ArgReader(argv, KNOWN_FLAGS, mkError);
1043
1096
  const valueOf = (name) => reader.value(name);
1044
1097
  const has = (name) => reader.has(name);
1045
1098
  const help = has("--help") || has("-h");
@@ -1049,12 +1102,12 @@ function parseArgs(argv) {
1049
1102
  throw new LoginCliArgError(`--timeout expects a positive integer (got ${timeoutRaw})`);
1050
1103
  }
1051
1104
  const args = {
1052
- url: rejectEmptyValue("--url", valueOf("--url"), mkError) ?? "https://qontinui.io/login",
1105
+ url: (0, cli_args_exports.rejectEmptyValue)("--url", valueOf("--url"), mkError) ?? "https://qontinui.io/login",
1053
1106
  email: valueOf("--email") ?? process.env.UIB_LOGIN_EMAIL ?? "",
1054
1107
  password: valueOf("--password") ?? process.env.UIB_LOGIN_PASSWORD ?? "",
1055
1108
  // An EMPTY --success would make `landingPath.includes('')` true on any
1056
1109
  // landing — ok:true while parked on the login page. Reject it.
1057
- success: rejectEmptyValue("--success", valueOf("--success"), mkError) ?? "/dashboard",
1110
+ success: (0, cli_args_exports.rejectEmptyValue)("--success", valueOf("--success"), mkError) ?? "/dashboard",
1058
1111
  timeoutMs,
1059
1112
  headed: has("--headed"),
1060
1113
  postClick: valueOf("--post-login-click"),
@@ -1082,7 +1135,7 @@ var KNOWN_FLAGS = /* @__PURE__ */ new Set([
1082
1135
  "--screenshot",
1083
1136
  "--storage-state-out",
1084
1137
  "--keep-open",
1085
- ...COMMON_FLAGS
1138
+ ...cli_args_exports.COMMON_FLAGS
1086
1139
  ]);
1087
1140
  function validateArgs(args) {
1088
1141
  if (args.help) return;
@@ -1125,7 +1178,7 @@ ${USAGE}`);
1125
1178
  screenshotPath,
1126
1179
  storageStateOut
1127
1180
  } = args;
1128
- const log = makeLogger("[login-web]", args.quiet);
1181
+ const log = (0, cli_args_exports.makeLogger)("[login-web]", args.quiet);
1129
1182
  const transport = createTransport({
1130
1183
  kind: "injected",
1131
1184
  options: { targetUrl: url, waitForSettle: false, readyTimeoutMs: 45e3, headed }