@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.
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var chunkZ32CC665_cjs = require('../chunk-Z32CC665.cjs');
4
+
5
+
6
+
7
+ Object.defineProperty(exports, "HandlerRegistry", {
8
+ enumerable: true,
9
+ get: function () { return chunkZ32CC665_cjs.HandlerRegistry; }
10
+ });
11
+ Object.defineProperty(exports, "LiveSessionTransport", {
12
+ enumerable: true,
13
+ get: function () { return chunkZ32CC665_cjs.LiveSessionTransport; }
14
+ });
15
+ Object.defineProperty(exports, "SCREENSHOT_ACTION", {
16
+ enumerable: true,
17
+ get: function () { return chunkZ32CC665_cjs.SCREENSHOT_ACTION; }
18
+ });
19
+ Object.defineProperty(exports, "SCREENSHOT_CAPABILITY", {
20
+ enumerable: true,
21
+ get: function () { return chunkZ32CC665_cjs.SCREENSHOT_CAPABILITY; }
22
+ });
23
+ Object.defineProperty(exports, "SCREENSHOT_TIMEOUT_MS", {
24
+ enumerable: true,
25
+ get: function () { return chunkZ32CC665_cjs.SCREENSHOT_TIMEOUT_MS; }
26
+ });
27
+ Object.defineProperty(exports, "WrapperTransportError", {
28
+ enumerable: true,
29
+ get: function () { return chunkZ32CC665_cjs.WrapperTransportError; }
30
+ });
31
+ Object.defineProperty(exports, "captureScreenshotReply", {
32
+ enumerable: true,
33
+ get: function () { return chunkZ32CC665_cjs.captureScreenshotReply; }
34
+ });
35
+ Object.defineProperty(exports, "registerScreenshotCapture", {
36
+ enumerable: true,
37
+ get: function () { return chunkZ32CC665_cjs.registerScreenshotCapture; }
38
+ });
39
+ //# sourceMappingURL=index.cjs.map
40
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * `@qontinui/ui-bridge-wrapper/live` — the BROWSER-SAFE entry.
3
+ *
4
+ * Everything a browser (an extension content script, a page) needs to be a
5
+ * live wrapper instance on the runner's `/ui-bridge/ws`: the live transport,
6
+ * its handler registry and error type, and the `getScreenshot` contract.
7
+ *
8
+ * NODE-FREE by contract. Nothing reachable from this file may import a Node
9
+ * builtin (`node:fs`, `module`, …), `playwright`, `inject-cli` or
10
+ * `InjectedTransport`, or touch `Buffer` / `process` / `require`. The package
11
+ * root (`.`) re-exports Node-only transports, so a bundle of it carries
12
+ * top-level `require("module")`-style calls that throw in a content script;
13
+ * this entry exists so a browser bundle never walks that graph.
14
+ * `ui-bridge-extension`'s `tests/content-bundle-node-free.test.ts` fails the
15
+ * build output if that ever regresses.
16
+ *
17
+ * SHARED CLASSES. This entry and the package root are built with code
18
+ * splitting, so within ONE module format they share a single copy of
19
+ * `WrapperTransportError`, `HandlerRegistry` and `LiveSessionTransport`: a
20
+ * `./live` error thrown through a root registry keeps its code
21
+ * (`tests/live-entry-shared.test.ts`). Across formats they do NOT — the CJS
22
+ * build (`require`) and the ESM build (`import`) are separate copies, the
23
+ * usual dual-package hazard — so do not mix a `require` of one entry with an
24
+ * `import` of the other in one process.
25
+ */
26
+ export { WrapperTransportError } from '../types.js';
27
+ export type { WrapperTransport, WrapperTransportKind, WrapperTransportStatus, TransportConfig, CommandResponse, } from '../types.js';
28
+ export { HandlerRegistry, type WrapperHandler } from '../handler-registry.js';
29
+ export { LiveSessionTransport, type LiveContext, type LiveTransportOptions, } from '../transports/live.js';
30
+ export { SCREENSHOT_CAPABILITY, SCREENSHOT_ACTION, SCREENSHOT_TIMEOUT_MS, registerScreenshotCapture, captureScreenshotReply, type ScreenshotReply, type ScreenshotRegistrar, type ScreenshotCaptureOptions, } from '../screenshot-capture.js';
31
+ export type { FramePage } from '../frame-page.js';
32
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/live/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,YAAY,EACV,gBAAgB,EAChB,oBAAoB,EACpB,sBAAsB,EACtB,eAAe,EACf,eAAe,GAChB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAC9E,OAAO,EACL,oBAAoB,EACpB,KAAK,WAAW,EAChB,KAAK,oBAAoB,GAC1B,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,qBAAqB,EACrB,iBAAiB,EACjB,qBAAqB,EACrB,yBAAyB,EACzB,sBAAsB,EACtB,KAAK,eAAe,EACpB,KAAK,mBAAmB,EACxB,KAAK,wBAAwB,GAC9B,MAAM,0BAA0B,CAAC;AAClC,YAAY,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { HandlerRegistry, LiveSessionTransport, SCREENSHOT_ACTION, SCREENSHOT_CAPABILITY, SCREENSHOT_TIMEOUT_MS, WrapperTransportError, captureScreenshotReply, registerScreenshotCapture } from '../chunk-IKHDNIU4.js';
2
+ //# sourceMappingURL=index.js.map
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
@@ -753,6 +753,113 @@ function isRegistrationMetadata(value) {
753
753
  return typeof v.userId === "string" && typeof v.sessionId === "string";
754
754
  }
755
755
 
756
+ // src/frame-page.ts
757
+ var PNG_SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10];
758
+ function pngDimensions(buf) {
759
+ if (buf.length < 24 || PNG_SIGNATURE.some((b, i) => buf[i] !== b)) {
760
+ throw new Error("not a PNG (signature mismatch or truncated header)");
761
+ }
762
+ const chunkType = String.fromCharCode(buf[12], buf[13], buf[14], buf[15]);
763
+ if (chunkType !== "IHDR") {
764
+ throw new Error(`PNG does not start with an IHDR chunk (found ${JSON.stringify(chunkType)})`);
765
+ }
766
+ const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
767
+ return { width: view.getUint32(16), height: view.getUint32(20) };
768
+ }
769
+ async function readPageMetrics(page) {
770
+ return page.evaluate(() => ({
771
+ devicePixelRatio: window.devicePixelRatio,
772
+ innerWidth: window.innerWidth,
773
+ innerHeight: window.innerHeight,
774
+ colorScheme: window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
775
+ }));
776
+ }
777
+
778
+ // src/screenshot-capture.ts
779
+ var SCREENSHOT_CAPABILITY = "screenshot";
780
+ var SCREENSHOT_ACTION = "getScreenshot";
781
+ var SCREENSHOT_TIMEOUT_MS = 1e4;
782
+ var NO_COMPOSITOR_FRAME = /Unable to capture screenshot/;
783
+ var NO_COMPOSITOR_FRAME_RETRY_MS = 16;
784
+ function toBase64(bytes) {
785
+ const CHUNK = 32768;
786
+ let binary = "";
787
+ for (let i = 0; i < bytes.length; i += CHUNK) {
788
+ binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
789
+ }
790
+ return btoa(binary);
791
+ }
792
+ function errorText(err) {
793
+ return err instanceof Error ? err.message : String(err);
794
+ }
795
+ async function captureScreenshotReply(page, options = {}) {
796
+ const timeoutMs = options.timeoutMs ?? SCREENSHOT_TIMEOUT_MS;
797
+ const retryMs = options.retryMs ?? NO_COMPOSITOR_FRAME_RETRY_MS;
798
+ const deadline = Date.now() + timeoutMs;
799
+ let timedOut = false;
800
+ let timer;
801
+ const attempt = async () => {
802
+ let metrics;
803
+ try {
804
+ metrics = await readPageMetrics(page);
805
+ } catch (err) {
806
+ throw new WrapperTransportError(
807
+ "SCREENSHOT_FAILED",
808
+ `could not read the page's viewport: ${errorText(err)}`,
809
+ { cause: err }
810
+ );
811
+ }
812
+ for (; ; ) {
813
+ let png;
814
+ try {
815
+ png = await page.screenshot({ type: "png", scale: "device", fullPage: false });
816
+ } catch (err) {
817
+ const text = errorText(err);
818
+ const noFrameYet = NO_COMPOSITOR_FRAME.test(text);
819
+ if (noFrameYet && !timedOut && Date.now() + retryMs < deadline) {
820
+ await new Promise((r) => setTimeout(r, retryMs));
821
+ if (timedOut) throw err;
822
+ continue;
823
+ }
824
+ throw new WrapperTransportError("SCREENSHOT_FAILED", `screenshot failed: ${text}`, {
825
+ retryable: noFrameYet,
826
+ cause: err
827
+ });
828
+ }
829
+ try {
830
+ pngDimensions(png);
831
+ } catch (err) {
832
+ throw new WrapperTransportError(
833
+ "SCREENSHOT_FAILED",
834
+ `the screenshot is unreadable: ${errorText(err)}`,
835
+ { cause: err }
836
+ );
837
+ }
838
+ return { screenshot: toBase64(png), width: metrics.innerWidth, height: metrics.innerHeight };
839
+ }
840
+ };
841
+ const timeout = new Promise((_, reject) => {
842
+ timer = setTimeout(() => {
843
+ timedOut = true;
844
+ reject(
845
+ new WrapperTransportError(
846
+ "SCREENSHOT_TIMEOUT",
847
+ `screenshot did not complete within ${timeoutMs}ms`,
848
+ { retryable: true }
849
+ )
850
+ );
851
+ }, timeoutMs);
852
+ });
853
+ try {
854
+ return await Promise.race([attempt(), timeout]);
855
+ } finally {
856
+ clearTimeout(timer);
857
+ }
858
+ }
859
+ function registerScreenshotCapture(registrar, page, options = {}) {
860
+ registrar.register(SCREENSHOT_ACTION, () => captureScreenshotReply(page, options));
861
+ }
862
+
756
863
  // src/transports/live.ts
757
864
  var LiveSessionTransport = class extends BaseTransport {
758
865
  kind = "live";
@@ -772,6 +879,16 @@ var LiveSessionTransport = class extends BaseTransport {
772
879
  this.appId = config.appId;
773
880
  this.appName = config.appName;
774
881
  this.opts = config.options ?? {};
882
+ const page = this.opts.screenshotPage;
883
+ if (page !== void 0) {
884
+ if (page === null || typeof page.screenshot !== "function" || typeof page.evaluate !== "function") {
885
+ throw new WrapperTransportError(
886
+ "INVALID_CONFIG",
887
+ `options.screenshotPage must expose screenshot() and evaluate() (a Playwright Page does)`
888
+ );
889
+ }
890
+ registerScreenshotCapture(this.registry, page);
891
+ }
775
892
  const fromInjection = injected?.webSocketCtor;
776
893
  const fromGlobal = typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : void 0;
777
894
  const resolved = fromInjection ?? fromGlobal;
@@ -801,7 +918,8 @@ var LiveSessionTransport = class extends BaseTransport {
801
918
  };
802
919
  if (this.opts.framework !== void 0) frame.framework = this.opts.framework;
803
920
  if (this.opts.version !== void 0) frame.version = this.opts.version;
804
- if (this.opts.capabilities !== void 0) frame.capabilities = this.opts.capabilities;
921
+ const capabilities = this.advertisedCapabilities();
922
+ if (capabilities !== void 0) frame.capabilities = capabilities;
805
923
  if (this.opts.origin !== void 0) frame.origin = this.opts.origin;
806
924
  if (this.opts.pageUrl !== void 0) frame.pageUrl = this.opts.pageUrl;
807
925
  ws.send(JSON.stringify(frame));
@@ -841,6 +959,18 @@ var LiveSessionTransport = class extends BaseTransport {
841
959
  }
842
960
  }
843
961
  }
962
+ /**
963
+ * The register frame's `capabilities`: the caller's list, plus
964
+ * `"screenshot"` exactly when this transport registered the handler for it.
965
+ * `undefined` (field omitted) when there is nothing to advertise.
966
+ */
967
+ advertisedCapabilities() {
968
+ const declared = this.opts.capabilities;
969
+ if (this.opts.screenshotPage === void 0) return declared;
970
+ const list = [...declared ?? []];
971
+ if (!list.includes(SCREENSHOT_CAPABILITY)) list.push(SCREENSHOT_CAPABILITY);
972
+ return list;
973
+ }
844
974
  async buildContext() {
845
975
  return { kind: "live", commandId: null };
846
976
  }
@@ -1202,10 +1332,10 @@ async function loginDrive(page, { email, password, timeoutMs, log = () => {
1202
1332
  { timeout: timeoutMs }
1203
1333
  );
1204
1334
  } catch {
1205
- const errorText2 = await scrapeErrorCard(page);
1335
+ const errorText3 = await scrapeErrorCard(page);
1206
1336
  const finalUrl2 = page.url();
1207
1337
  log(`stalled ${finalUrl2}`);
1208
- return { ok: false, finalUrl: finalUrl2, landingPath: pathOf(finalUrl2), errorText: errorText2 };
1338
+ return { ok: false, finalUrl: finalUrl2, landingPath: pathOf(finalUrl2), errorText: errorText3 };
1209
1339
  }
1210
1340
  }
1211
1341
  await page.waitForLoadState("networkidle").catch(() => {
@@ -1219,8 +1349,8 @@ async function loginDrive(page, { email, password, timeoutMs, log = () => {
1219
1349
  const landingPath = await page.evaluate(() => location.pathname).catch(() => pathOf(finalUrl));
1220
1350
  const ok = !isAppLoginPath(finalUrl) && !isCognito(finalUrl) && !/\/auth\/callback/.test(pathOf(finalUrl));
1221
1351
  log(ok ? `authed ${finalUrl}` : `stalled ${finalUrl}`);
1222
- const errorText = ok ? null : await scrapeErrorCard(page);
1223
- return { ok, finalUrl, landingPath, errorText };
1352
+ const errorText2 = ok ? null : await scrapeErrorCard(page);
1353
+ return { ok, finalUrl, landingPath, errorText: errorText2 };
1224
1354
  }
1225
1355
 
1226
1356
  // src/login-web-cli.ts
@@ -1405,9 +1535,9 @@ ${USAGE}`);
1405
1535
  const onAppLoginPage = isAppLoginPath(finalUrl);
1406
1536
  const stillOnCognito = isCognito(finalUrl);
1407
1537
  const ok = !onAppLoginPage && !stillOnCognito && landingPath.includes(success);
1408
- let errorText = r.errorText;
1409
- if (!ok && !errorText) {
1410
- errorText = await scrapeErrorCard(page);
1538
+ let errorText2 = r.errorText;
1539
+ if (!ok && !errorText2) {
1540
+ errorText2 = await scrapeErrorCard(page);
1411
1541
  }
1412
1542
  let postLoginClicked = null;
1413
1543
  if (ok && postClick) {
@@ -1497,7 +1627,7 @@ ${USAGE}`);
1497
1627
  landingPath,
1498
1628
  onAppLoginPage,
1499
1629
  stillOnCognito,
1500
- errorText,
1630
+ errorText: errorText2,
1501
1631
  uiBridgeRoute,
1502
1632
  uiBridgeRegistered: snap?.registration?.totalRegistered ?? null,
1503
1633
  postLoginClicked,