@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.
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var chunk3EIJDQJO_cjs = require('../chunk-3EIJDQJO.cjs');
4
+
5
+
6
+
7
+ Object.defineProperty(exports, "HandlerRegistry", {
8
+ enumerable: true,
9
+ get: function () { return chunk3EIJDQJO_cjs.HandlerRegistry; }
10
+ });
11
+ Object.defineProperty(exports, "LiveSessionTransport", {
12
+ enumerable: true,
13
+ get: function () { return chunk3EIJDQJO_cjs.LiveSessionTransport; }
14
+ });
15
+ Object.defineProperty(exports, "SCREENSHOT_ACTION", {
16
+ enumerable: true,
17
+ get: function () { return chunk3EIJDQJO_cjs.SCREENSHOT_ACTION; }
18
+ });
19
+ Object.defineProperty(exports, "SCREENSHOT_CAPABILITY", {
20
+ enumerable: true,
21
+ get: function () { return chunk3EIJDQJO_cjs.SCREENSHOT_CAPABILITY; }
22
+ });
23
+ Object.defineProperty(exports, "SCREENSHOT_TIMEOUT_MS", {
24
+ enumerable: true,
25
+ get: function () { return chunk3EIJDQJO_cjs.SCREENSHOT_TIMEOUT_MS; }
26
+ });
27
+ Object.defineProperty(exports, "WrapperTransportError", {
28
+ enumerable: true,
29
+ get: function () { return chunk3EIJDQJO_cjs.WrapperTransportError; }
30
+ });
31
+ Object.defineProperty(exports, "captureScreenshotReply", {
32
+ enumerable: true,
33
+ get: function () { return chunk3EIJDQJO_cjs.captureScreenshotReply; }
34
+ });
35
+ Object.defineProperty(exports, "registerScreenshotCapture", {
36
+ enumerable: true,
37
+ get: function () { return chunk3EIJDQJO_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-WI4MTFWJ.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";
@@ -768,10 +875,26 @@ var LiveSessionTransport = class extends BaseTransport {
768
875
  if (!config.runnerUrl) {
769
876
  throw new WrapperTransportError("INVALID_CONFIG", `Transport kind 'live' requires runnerUrl`);
770
877
  }
878
+ if (!config.appId?.trim()) {
879
+ throw new WrapperTransportError(
880
+ "INVALID_CONFIG",
881
+ `Transport kind 'live' requires a non-blank appId (the runner refuses a register frame without one)`
882
+ );
883
+ }
771
884
  this.url = config.runnerUrl;
772
885
  this.appId = config.appId;
773
886
  this.appName = config.appName;
774
887
  this.opts = config.options ?? {};
888
+ const page = this.opts.screenshotPage;
889
+ if (page !== void 0) {
890
+ if (page === null || typeof page.screenshot !== "function" || typeof page.evaluate !== "function") {
891
+ throw new WrapperTransportError(
892
+ "INVALID_CONFIG",
893
+ `options.screenshotPage must expose screenshot() and evaluate() (a Playwright Page does)`
894
+ );
895
+ }
896
+ registerScreenshotCapture(this.registry, page);
897
+ }
775
898
  const fromInjection = injected?.webSocketCtor;
776
899
  const fromGlobal = typeof globalThis.WebSocket === "function" ? globalThis.WebSocket : void 0;
777
900
  const resolved = fromInjection ?? fromGlobal;
@@ -796,12 +919,14 @@ var LiveSessionTransport = class extends BaseTransport {
796
919
  type: "register",
797
920
  transport: "websocket",
798
921
  appId: this.appId,
799
- appName: this.appName,
922
+ // appName defaults to appId: the runner requires a non-blank one.
923
+ appName: this.appName?.trim() || this.appId,
800
924
  appType: this.opts.appType ?? "wrapper"
801
925
  };
802
926
  if (this.opts.framework !== void 0) frame.framework = this.opts.framework;
803
927
  if (this.opts.version !== void 0) frame.version = this.opts.version;
804
- if (this.opts.capabilities !== void 0) frame.capabilities = this.opts.capabilities;
928
+ const capabilities = this.advertisedCapabilities();
929
+ if (capabilities !== void 0) frame.capabilities = capabilities;
805
930
  if (this.opts.origin !== void 0) frame.origin = this.opts.origin;
806
931
  if (this.opts.pageUrl !== void 0) frame.pageUrl = this.opts.pageUrl;
807
932
  ws.send(JSON.stringify(frame));
@@ -841,6 +966,18 @@ var LiveSessionTransport = class extends BaseTransport {
841
966
  }
842
967
  }
843
968
  }
969
+ /**
970
+ * The register frame's `capabilities`: the caller's list, plus
971
+ * `"screenshot"` exactly when this transport registered the handler for it.
972
+ * `undefined` (field omitted) when there is nothing to advertise.
973
+ */
974
+ advertisedCapabilities() {
975
+ const declared = this.opts.capabilities;
976
+ if (this.opts.screenshotPage === void 0) return declared;
977
+ const list = [...declared ?? []];
978
+ if (!list.includes(SCREENSHOT_CAPABILITY)) list.push(SCREENSHOT_CAPABILITY);
979
+ return list;
980
+ }
844
981
  async buildContext() {
845
982
  return { kind: "live", commandId: null };
846
983
  }
@@ -1202,10 +1339,10 @@ async function loginDrive(page, { email, password, timeoutMs, log = () => {
1202
1339
  { timeout: timeoutMs }
1203
1340
  );
1204
1341
  } catch {
1205
- const errorText2 = await scrapeErrorCard(page);
1342
+ const errorText3 = await scrapeErrorCard(page);
1206
1343
  const finalUrl2 = page.url();
1207
1344
  log(`stalled ${finalUrl2}`);
1208
- return { ok: false, finalUrl: finalUrl2, landingPath: pathOf(finalUrl2), errorText: errorText2 };
1345
+ return { ok: false, finalUrl: finalUrl2, landingPath: pathOf(finalUrl2), errorText: errorText3 };
1209
1346
  }
1210
1347
  }
1211
1348
  await page.waitForLoadState("networkidle").catch(() => {
@@ -1219,8 +1356,8 @@ async function loginDrive(page, { email, password, timeoutMs, log = () => {
1219
1356
  const landingPath = await page.evaluate(() => location.pathname).catch(() => pathOf(finalUrl));
1220
1357
  const ok = !isAppLoginPath(finalUrl) && !isCognito(finalUrl) && !/\/auth\/callback/.test(pathOf(finalUrl));
1221
1358
  log(ok ? `authed ${finalUrl}` : `stalled ${finalUrl}`);
1222
- const errorText = ok ? null : await scrapeErrorCard(page);
1223
- return { ok, finalUrl, landingPath, errorText };
1359
+ const errorText2 = ok ? null : await scrapeErrorCard(page);
1360
+ return { ok, finalUrl, landingPath, errorText: errorText2 };
1224
1361
  }
1225
1362
 
1226
1363
  // src/login-web-cli.ts
@@ -1405,9 +1542,9 @@ ${USAGE}`);
1405
1542
  const onAppLoginPage = isAppLoginPath(finalUrl);
1406
1543
  const stillOnCognito = isCognito(finalUrl);
1407
1544
  const ok = !onAppLoginPage && !stillOnCognito && landingPath.includes(success);
1408
- let errorText = r.errorText;
1409
- if (!ok && !errorText) {
1410
- errorText = await scrapeErrorCard(page);
1545
+ let errorText2 = r.errorText;
1546
+ if (!ok && !errorText2) {
1547
+ errorText2 = await scrapeErrorCard(page);
1411
1548
  }
1412
1549
  let postLoginClicked = null;
1413
1550
  if (ok && postClick) {
@@ -1497,7 +1634,7 @@ ${USAGE}`);
1497
1634
  landingPath,
1498
1635
  onAppLoginPage,
1499
1636
  stillOnCognito,
1500
- errorText,
1637
+ errorText: errorText2,
1501
1638
  uiBridgeRoute,
1502
1639
  uiBridgeRegistered: snap?.registration?.totalRegistered ?? null,
1503
1640
  postLoginClicked,