@qontinui/ui-bridge-wrapper 0.14.0 → 0.15.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.
@@ -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";
@@ -773,6 +880,16 @@ var LiveSessionTransport = class extends BaseTransport {
773
880
  this.appId = config.appId;
774
881
  this.appName = config.appName;
775
882
  this.opts = config.options ?? {};
883
+ const page = this.opts.screenshotPage;
884
+ if (page !== void 0) {
885
+ if (page === null || typeof page.screenshot !== "function" || typeof page.evaluate !== "function") {
886
+ throw new WrapperTransportError(
887
+ "INVALID_CONFIG",
888
+ `options.screenshotPage must expose screenshot() and evaluate() (a Playwright Page does)`
889
+ );
890
+ }
891
+ registerScreenshotCapture(this.registry, page);
892
+ }
776
893
  const fromInjection = injected?.webSocketCtor;
777
894
  const fromGlobal = typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : void 0;
778
895
  const resolved = fromInjection ?? fromGlobal;
@@ -802,7 +919,8 @@ var LiveSessionTransport = class extends BaseTransport {
802
919
  };
803
920
  if (this.opts.framework !== void 0) frame.framework = this.opts.framework;
804
921
  if (this.opts.version !== void 0) frame.version = this.opts.version;
805
- if (this.opts.capabilities !== void 0) frame.capabilities = this.opts.capabilities;
922
+ const capabilities = this.advertisedCapabilities();
923
+ if (capabilities !== void 0) frame.capabilities = capabilities;
806
924
  if (this.opts.origin !== void 0) frame.origin = this.opts.origin;
807
925
  if (this.opts.pageUrl !== void 0) frame.pageUrl = this.opts.pageUrl;
808
926
  ws.send(JSON.stringify(frame));
@@ -842,6 +960,18 @@ var LiveSessionTransport = class extends BaseTransport {
842
960
  }
843
961
  }
844
962
  }
963
+ /**
964
+ * The register frame's `capabilities`: the caller's list, plus
965
+ * `"screenshot"` exactly when this transport registered the handler for it.
966
+ * `undefined` (field omitted) when there is nothing to advertise.
967
+ */
968
+ advertisedCapabilities() {
969
+ const declared = this.opts.capabilities;
970
+ if (this.opts.screenshotPage === void 0) return declared;
971
+ const list = [...declared ?? []];
972
+ if (!list.includes(SCREENSHOT_CAPABILITY)) list.push(SCREENSHOT_CAPABILITY);
973
+ return list;
974
+ }
845
975
  async buildContext() {
846
976
  return { kind: "live", commandId: null };
847
977
  }
@@ -1092,18 +1222,6 @@ function writePrivateFile(path$1, data) {
1092
1222
  throw err;
1093
1223
  }
1094
1224
  }
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
1225
  function fileNameStart(path) {
1108
1226
  return Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\")) + 1;
1109
1227
  }
@@ -1117,14 +1235,6 @@ function frameTokenInDirectory(template) {
1117
1235
  function frameTokenInFileName(template) {
1118
1236
  return template.slice(fileNameStart(template)).includes(FRAME_INDEX_TOKEN);
1119
1237
  }
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
1238
  function isRecord(v) {
1129
1239
  return typeof v === "object" && v !== null && !Array.isArray(v);
1130
1240
  }
@@ -1252,8 +1362,6 @@ var CAPTURE_TIMELINE_ACTION = "captureTimeline";
1252
1362
  var MAX_TIMELINE_FRAMES = 120;
1253
1363
  var MAX_TIMELINE_INTERVAL_MS = 1e4;
1254
1364
  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
1365
  var MAX_SLEEPS_PER_FRAME_WAIT = 3;
1258
1366
  function asRecord(payload) {
1259
1367
  if (payload === void 0 || payload === null) return {};