@qontinui/ui-bridge-wrapper 0.7.2 → 0.9.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.
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createRequire } from 'module';
2
2
  import { readFileSync } from 'fs';
3
+ import { randomBytes } from 'crypto';
3
4
 
4
5
  // src/types.ts
5
6
  var WrapperTransportError = class extends Error {
@@ -226,6 +227,8 @@ var HeadlessTransport = class extends BaseTransport {
226
227
  targetUrl,
227
228
  viewportWidth: typeof raw["viewportWidth"] === "number" ? raw["viewportWidth"] : void 0,
228
229
  viewportHeight: typeof raw["viewportHeight"] === "number" ? raw["viewportHeight"] : void 0,
230
+ deviceScaleFactor: typeof raw["deviceScaleFactor"] === "number" ? raw["deviceScaleFactor"] : void 0,
231
+ colorScheme: raw["colorScheme"] === "light" || raw["colorScheme"] === "dark" ? raw["colorScheme"] : void 0,
229
232
  uiBridgeBase: typeof raw["uiBridgeBase"] === "string" ? raw["uiBridgeBase"] : void 0,
230
233
  waitForUiBridgeMs: typeof raw["waitForUiBridgeMs"] === "number" ? raw["waitForUiBridgeMs"] : void 0,
231
234
  userAgent: typeof raw["userAgent"] === "string" ? raw["userAgent"] : void 0,
@@ -260,6 +263,7 @@ Underlying import error: ${cause}`
260
263
  }
261
264
  const initScripts = await this.buildInitScripts();
262
265
  const relayAuth = this.relayAuth();
266
+ const pinnedTabId = this.pinnedTabId();
263
267
  let tab;
264
268
  try {
265
269
  tab = await launchHeadlessTab({
@@ -269,13 +273,16 @@ Underlying import error: ${cause}`
269
273
  waitForUiBridgeMs: this.options.waitForUiBridgeMs,
270
274
  viewportWidth: this.options.viewportWidth,
271
275
  viewportHeight: this.options.viewportHeight,
276
+ ...this.options.deviceScaleFactor !== void 0 ? { deviceScaleFactor: this.options.deviceScaleFactor } : {},
277
+ ...this.options.colorScheme !== void 0 ? { colorScheme: this.options.colorScheme } : {},
272
278
  userAgent: this.options.userAgent,
273
279
  forwardConsole: this.options.forwardConsole,
274
280
  initScripts: initScripts.length > 0 ? initScripts : void 0,
275
281
  launchArgs: this.options.launchArgs && this.options.launchArgs.length > 0 ? this.options.launchArgs : void 0,
276
282
  storageStatePath: this.options.storageStatePath,
277
283
  ...relayAuth.authToken ? { authToken: relayAuth.authToken } : {},
278
- ...relayAuth.callerUserId ? { callerUserId: relayAuth.callerUserId } : {}
284
+ ...relayAuth.callerUserId ? { callerUserId: relayAuth.callerUserId } : {},
285
+ ...pinnedTabId ? { pinnedTabId } : {}
279
286
  });
280
287
  } catch (err) {
281
288
  const cause = err instanceof Error ? err.message : String(err);
@@ -297,8 +304,39 @@ Underlying launch error: ${cause}`
297
304
  tabId: tab.tabId,
298
305
  close: tab.close
299
306
  };
307
+ await this.verifyEmulation(tab.page);
300
308
  await this.afterLaunch(tab.page);
301
309
  }
310
+ /**
311
+ * Read back the requested device scale factor and colour scheme from the
312
+ * page itself, and refuse a launch whose page reports anything else.
313
+ */
314
+ async verifyEmulation(page) {
315
+ const wantDpr = this.options.deviceScaleFactor;
316
+ const wantScheme = this.options.colorScheme;
317
+ if (wantDpr === void 0 && wantScheme === void 0) return;
318
+ const seen = await page.evaluate(() => ({
319
+ dpr: window.devicePixelRatio,
320
+ dark: window.matchMedia("(prefers-color-scheme: dark)").matches
321
+ }));
322
+ const problems = [];
323
+ if (wantDpr !== void 0 && Math.abs(seen.dpr - wantDpr) > 1e-9) {
324
+ problems.push(`devicePixelRatio ${seen.dpr} (requested ${wantDpr})`);
325
+ }
326
+ if (wantScheme !== void 0 && seen.dark !== (wantScheme === "dark")) {
327
+ problems.push(
328
+ `prefers-color-scheme ${seen.dark ? "dark" : "light"} (requested ${wantScheme})`
329
+ );
330
+ }
331
+ if (problems.length === 0) return;
332
+ await this.onClose().catch(() => {
333
+ });
334
+ throw new WrapperTransportError(
335
+ "EMULATION_NOT_APPLIED",
336
+ `The launched page reports ${problems.join(" and ")}. The installed '@qontinui/ui-bridge-headless' peer most likely predates deviceScaleFactor/colorScheme support (added after 0.5.0) and ignored them; upgrade it. A page measured at an emulation it is not actually rendering at would be stamped with the wrong values.`,
337
+ { details: { requested: { deviceScaleFactor: wantDpr, colorScheme: wantScheme }, seen } }
338
+ );
339
+ }
302
340
  /**
303
341
  * Subclass hook: init-scripts to register on the browser context before the
304
342
  * first navigation (via `context.addInitScript`). Default: none. Injected
@@ -316,6 +354,13 @@ Underlying launch error: ${cause}`
316
354
  relayAuth() {
317
355
  return {};
318
356
  }
357
+ /**
358
+ * Subclass hook: the tab id this launch pinned, so the registration poll
359
+ * returns that tab only. Default: none. Injected mode returns `--tab-id`.
360
+ */
361
+ pinnedTabId() {
362
+ return void 0;
363
+ }
319
364
  /**
320
365
  * Subclass hook: run after launch + navigation, before `ready()` resolves.
321
366
  * Default: no-op. Injected mode overrides it to await the in-page runtime.
@@ -378,6 +423,14 @@ function loadInjectedBundle() {
378
423
  }
379
424
  var InjectedTransport = class extends HeadlessTransport {
380
425
  injected;
426
+ /**
427
+ * The pinned tab's key: 32 random bytes, base64url, generated once per
428
+ * transport (one launch) and kept in this process. The runner binds a tab
429
+ * that presents it to the key's digest, so the pin survives an origin hop
430
+ * while a page that merely learns the pinned id cannot attach as the tab.
431
+ * Lives only in the injected global — never in a storage artifact.
432
+ */
433
+ tabKey = randomBytes(32).toString("base64url");
381
434
  constructor(config, registry) {
382
435
  const raw = config.options ?? {};
383
436
  const headed = raw["headed"] === true;
@@ -412,9 +465,11 @@ var InjectedTransport = class extends HeadlessTransport {
412
465
  if (this.injected.appId) cfg.appId = this.injected.appId;
413
466
  if (this.injected.appName) cfg.appName = this.injected.appName;
414
467
  }
415
- if (this.injected.tabId) cfg.tabId = this.injected.tabId;
416
- if (this.injected.settleQuietMs !== void 0)
417
- cfg.settleQuietMs = this.injected.settleQuietMs;
468
+ if (this.injected.tabId) {
469
+ cfg.tabId = this.injected.tabId;
470
+ cfg.tabKey = this.tabKey;
471
+ }
472
+ if (this.injected.settleQuietMs !== void 0) cfg.settleQuietMs = this.injected.settleQuietMs;
418
473
  if (this.injected.settleTimeoutMs !== void 0)
419
474
  cfg.settleTimeoutMs = this.injected.settleTimeoutMs;
420
475
  if (this.injected.expectSelector !== void 0)
@@ -433,6 +488,9 @@ var InjectedTransport = class extends HeadlessTransport {
433
488
  ...this.injected.registrationMetadata?.userId ? { callerUserId: this.injected.registrationMetadata.userId } : {}
434
489
  };
435
490
  }
491
+ pinnedTabId() {
492
+ return this.injected.tabId;
493
+ }
436
494
  async buildInitScripts() {
437
495
  const scripts = [];
438
496
  const cfg = this.injectedConfig();