@qontinui/ui-bridge-wrapper 0.14.0 → 0.15.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.
@@ -754,6 +754,113 @@ function isRegistrationMetadata(value) {
754
754
  return typeof v.userId === "string" && typeof v.sessionId === "string";
755
755
  }
756
756
 
757
+ // src/frame-page.ts
758
+ var PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
759
+ function pngDimensions(buf) {
760
+ if (buf.length < 24 || PNG_SIGNATURE.some((b, i) => buf[i] !== b)) {
761
+ throw new Error("not a PNG (signature mismatch or truncated header)");
762
+ }
763
+ const chunkType = String.fromCharCode(buf[12], buf[13], buf[14], buf[15]);
764
+ if (chunkType !== "IHDR") {
765
+ throw new Error(`PNG does not start with an IHDR chunk (found ${JSON.stringify(chunkType)})`);
766
+ }
767
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
768
+ return { width: view.getUint32(16), height: view.getUint32(20) };
769
+ }
770
+ async function readPageMetrics(page) {
771
+ return page.evaluate(() => ({
772
+ devicePixelRatio: window.devicePixelRatio,
773
+ innerWidth: window.innerWidth,
774
+ innerHeight: window.innerHeight,
775
+ colorScheme: window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
776
+ }));
777
+ }
778
+
779
+ // src/screenshot-capture.ts
780
+ var SCREENSHOT_CAPABILITY = "screenshot";
781
+ var SCREENSHOT_ACTION = "getScreenshot";
782
+ var SCREENSHOT_TIMEOUT_MS = 1e4;
783
+ var NO_COMPOSITOR_FRAME = /Unable to capture screenshot/;
784
+ var NO_COMPOSITOR_FRAME_RETRY_MS = 16;
785
+ function toBase64(bytes) {
786
+ const CHUNK = 32768;
787
+ let binary = "";
788
+ for (let i = 0; i < bytes.length; i += CHUNK) {
789
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
790
+ }
791
+ return btoa(binary);
792
+ }
793
+ function errorText(err) {
794
+ return err instanceof Error ? err.message : String(err);
795
+ }
796
+ async function captureScreenshotReply(page, options = {}) {
797
+ const timeoutMs = options.timeoutMs ?? SCREENSHOT_TIMEOUT_MS;
798
+ const retryMs = options.retryMs ?? NO_COMPOSITOR_FRAME_RETRY_MS;
799
+ const deadline = Date.now() + timeoutMs;
800
+ let timedOut = false;
801
+ let timer;
802
+ const attempt = async () => {
803
+ let metrics;
804
+ try {
805
+ metrics = await readPageMetrics(page);
806
+ } catch (err) {
807
+ throw new WrapperTransportError(
808
+ "SCREENSHOT_FAILED",
809
+ `could not read the page's viewport: ${errorText(err)}`,
810
+ { cause: err }
811
+ );
812
+ }
813
+ for (; ; ) {
814
+ let png;
815
+ try {
816
+ png = await page.screenshot({ type: "png", scale: "device", fullPage: false });
817
+ } catch (err) {
818
+ const text = errorText(err);
819
+ const noFrameYet = NO_COMPOSITOR_FRAME.test(text);
820
+ if (noFrameYet && !timedOut && Date.now() + retryMs < deadline) {
821
+ await new Promise((r) => setTimeout(r, retryMs));
822
+ if (timedOut) throw err;
823
+ continue;
824
+ }
825
+ throw new WrapperTransportError("SCREENSHOT_FAILED", `screenshot failed: ${text}`, {
826
+ retryable: noFrameYet,
827
+ cause: err
828
+ });
829
+ }
830
+ try {
831
+ pngDimensions(png);
832
+ } catch (err) {
833
+ throw new WrapperTransportError(
834
+ "SCREENSHOT_FAILED",
835
+ `the screenshot is unreadable: ${errorText(err)}`,
836
+ { cause: err }
837
+ );
838
+ }
839
+ return { screenshot: toBase64(png), width: metrics.innerWidth, height: metrics.innerHeight };
840
+ }
841
+ };
842
+ const timeout = new Promise((_, reject) => {
843
+ timer = setTimeout(() => {
844
+ timedOut = true;
845
+ reject(
846
+ new WrapperTransportError(
847
+ "SCREENSHOT_TIMEOUT",
848
+ `screenshot did not complete within ${timeoutMs}ms`,
849
+ { retryable: true }
850
+ )
851
+ );
852
+ }, timeoutMs);
853
+ });
854
+ try {
855
+ return await Promise.race([attempt(), timeout]);
856
+ } finally {
857
+ clearTimeout(timer);
858
+ }
859
+ }
860
+ function registerScreenshotCapture(registrar, page, options = {}) {
861
+ registrar.register(SCREENSHOT_ACTION, () => captureScreenshotReply(page, options));
862
+ }
863
+
757
864
  // src/transports/live.ts
758
865
  var LiveSessionTransport = class extends BaseTransport {
759
866
  kind = "live";
@@ -769,10 +876,26 @@ var LiveSessionTransport = class extends BaseTransport {
769
876
  if (!config.runnerUrl) {
770
877
  throw new WrapperTransportError("INVALID_CONFIG", `Transport kind 'live' requires runnerUrl`);
771
878
  }
879
+ if (!config.appId?.trim()) {
880
+ throw new WrapperTransportError(
881
+ "INVALID_CONFIG",
882
+ `Transport kind 'live' requires a non-blank appId (the runner refuses a register frame without one)`
883
+ );
884
+ }
772
885
  this.url = config.runnerUrl;
773
886
  this.appId = config.appId;
774
887
  this.appName = config.appName;
775
888
  this.opts = config.options ?? {};
889
+ const page = this.opts.screenshotPage;
890
+ if (page !== void 0) {
891
+ if (page === null || typeof page.screenshot !== "function" || typeof page.evaluate !== "function") {
892
+ throw new WrapperTransportError(
893
+ "INVALID_CONFIG",
894
+ `options.screenshotPage must expose screenshot() and evaluate() (a Playwright Page does)`
895
+ );
896
+ }
897
+ registerScreenshotCapture(this.registry, page);
898
+ }
776
899
  const fromInjection = injected?.webSocketCtor;
777
900
  const fromGlobal = typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : void 0;
778
901
  const resolved = fromInjection ?? fromGlobal;
@@ -797,12 +920,14 @@ var LiveSessionTransport = class extends BaseTransport {
797
920
  type: "register",
798
921
  transport: "websocket",
799
922
  appId: this.appId,
800
- appName: this.appName,
923
+ // appName defaults to appId: the runner requires a non-blank one.
924
+ appName: this.appName?.trim() || this.appId,
801
925
  appType: this.opts.appType ?? "wrapper"
802
926
  };
803
927
  if (this.opts.framework !== void 0) frame.framework = this.opts.framework;
804
928
  if (this.opts.version !== void 0) frame.version = this.opts.version;
805
- if (this.opts.capabilities !== void 0) frame.capabilities = this.opts.capabilities;
929
+ const capabilities = this.advertisedCapabilities();
930
+ if (capabilities !== void 0) frame.capabilities = capabilities;
806
931
  if (this.opts.origin !== void 0) frame.origin = this.opts.origin;
807
932
  if (this.opts.pageUrl !== void 0) frame.pageUrl = this.opts.pageUrl;
808
933
  ws.send(JSON.stringify(frame));
@@ -842,6 +967,18 @@ var LiveSessionTransport = class extends BaseTransport {
842
967
  }
843
968
  }
844
969
  }
970
+ /**
971
+ * The register frame's `capabilities`: the caller's list, plus
972
+ * `"screenshot"` exactly when this transport registered the handler for it.
973
+ * `undefined` (field omitted) when there is nothing to advertise.
974
+ */
975
+ advertisedCapabilities() {
976
+ const declared = this.opts.capabilities;
977
+ if (this.opts.screenshotPage === void 0) return declared;
978
+ const list = [...declared ?? []];
979
+ if (!list.includes(SCREENSHOT_CAPABILITY)) list.push(SCREENSHOT_CAPABILITY);
980
+ return list;
981
+ }
845
982
  async buildContext() {
846
983
  return { kind: "live", commandId: null };
847
984
  }
@@ -1092,18 +1229,6 @@ function writePrivateFile(path$1, data) {
1092
1229
  throw err;
1093
1230
  }
1094
1231
  }
1095
- var PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
1096
- function pngDimensions(buf) {
1097
- if (buf.length < 24 || PNG_SIGNATURE.some((b, i) => buf[i] !== b)) {
1098
- throw new Error("not a PNG (signature mismatch or truncated header)");
1099
- }
1100
- const chunkType = String.fromCharCode(buf[12], buf[13], buf[14], buf[15]);
1101
- if (chunkType !== "IHDR") {
1102
- throw new Error(`PNG does not start with an IHDR chunk (found ${JSON.stringify(chunkType)})`);
1103
- }
1104
- const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
1105
- return { width: view.getUint32(16), height: view.getUint32(20) };
1106
- }
1107
1232
  function fileNameStart(path) {
1108
1233
  return Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1;
1109
1234
  }
@@ -1117,14 +1242,6 @@ function frameTokenInDirectory(template) {
1117
1242
  function frameTokenInFileName(template) {
1118
1243
  return template.slice(fileNameStart(template)).includes(FRAME_INDEX_TOKEN);
1119
1244
  }
1120
- async function readPageMetrics(page) {
1121
- return page.evaluate(() => ({
1122
- devicePixelRatio: window.devicePixelRatio,
1123
- innerWidth: window.innerWidth,
1124
- innerHeight: window.innerHeight,
1125
- colorScheme: window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
1126
- }));
1127
- }
1128
1245
  function isRecord(v) {
1129
1246
  return typeof v === "object" && v !== null && !Array.isArray(v);
1130
1247
  }
@@ -1252,8 +1369,6 @@ var CAPTURE_TIMELINE_ACTION = "captureTimeline";
1252
1369
  var MAX_TIMELINE_FRAMES = 120;
1253
1370
  var MAX_TIMELINE_INTERVAL_MS = 1e4;
1254
1371
  var DEFAULT_TIMELINE_READY_TIMEOUT_MS = 3e4;
1255
- var NO_COMPOSITOR_FRAME = /Unable to capture screenshot/;
1256
- var NO_COMPOSITOR_FRAME_RETRY_MS = 16;
1257
1372
  var MAX_SLEEPS_PER_FRAME_WAIT = 3;
1258
1373
  function asRecord(payload) {
1259
1374
  if (payload === void 0 || payload === null) return {};