@qontinui/ui-bridge-wrapper 0.12.0 → 0.14.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
@@ -234,9 +234,60 @@ var HeadlessTransport = class extends BaseTransport {
234
234
  userAgent: typeof raw["userAgent"] === "string" ? raw["userAgent"] : void 0,
235
235
  forwardConsole: typeof raw["forwardConsole"] === "boolean" ? raw["forwardConsole"] : void 0,
236
236
  launchArgs: Array.isArray(raw["launchArgs"]) && raw["launchArgs"].every((a) => typeof a === "string") ? raw["launchArgs"] : void 0,
237
- storageStatePath: typeof raw["storageStatePath"] === "string" ? raw["storageStatePath"] : void 0
237
+ storageStatePath: typeof raw["storageStatePath"] === "string" ? raw["storageStatePath"] : void 0,
238
+ ...this.parseContextOptions(raw)
238
239
  };
239
240
  }
241
+ /**
242
+ * `recordHar`, `recordVideo`, `serviceWorkers` and `initRouter`. Unlike the display
243
+ * options above, a malformed value is REFUSED rather than dropped: dropping
244
+ * `recordHar` would leave a caller waiting for an archive that is never
245
+ * written, and dropping `initRouter` would let requests it meant to answer
246
+ * reach the network.
247
+ */
248
+ parseContextOptions(raw) {
249
+ const out = {};
250
+ const invalid = (what) => new WrapperTransportError("INVALID_CONFIG", `Transport kind '${this.kind}': ${what}`);
251
+ const har = raw["recordHar"];
252
+ if (har !== void 0) {
253
+ if (typeof har !== "object" || har === null || typeof har.path !== "string" || har.path.length === 0) {
254
+ throw invalid("options.recordHar must be an object with a non-empty string `path`");
255
+ }
256
+ out.recordHar = har;
257
+ }
258
+ const video = raw["recordVideo"];
259
+ if (video !== void 0) {
260
+ if (typeof video !== "object" || video === null || typeof video.dir !== "string" || video.dir.length === 0) {
261
+ throw invalid("options.recordVideo must be an object with a non-empty string `dir`");
262
+ }
263
+ const size = video.size;
264
+ if (size !== void 0) {
265
+ const positiveInt = (n) => typeof n === "number" && Number.isInteger(n) && n > 0;
266
+ const s = size;
267
+ if (typeof size !== "object" || s === null || !positiveInt(s.width) || !positiveInt(s.height)) {
268
+ throw invalid(
269
+ "options.recordVideo.size must be { width, height } with positive integer sides"
270
+ );
271
+ }
272
+ }
273
+ out.recordVideo = video;
274
+ }
275
+ const sw = raw["serviceWorkers"];
276
+ if (sw !== void 0) {
277
+ if (sw !== "allow" && sw !== "block") {
278
+ throw invalid(`options.serviceWorkers must be 'allow' or 'block' (got ${String(sw)})`);
279
+ }
280
+ out.serviceWorkers = sw;
281
+ }
282
+ const router = raw["initRouter"];
283
+ if (router !== void 0) {
284
+ if (typeof router !== "function") {
285
+ throw invalid("options.initRouter must be a function (context) => void | Promise<void>");
286
+ }
287
+ out.initRouter = router;
288
+ }
289
+ return out;
290
+ }
240
291
  async onReady() {
241
292
  let launchHeadlessTab;
242
293
  const headlessSpecifier = "@qontinui/ui-bridge-headless";
@@ -264,6 +315,13 @@ Underlying import error: ${cause}`
264
315
  const initScripts = await this.buildInitScripts();
265
316
  const relayAuth = this.relayAuth();
266
317
  const pinnedTabId = this.pinnedTabId();
318
+ const { recordHar, recordVideo, serviceWorkers, initRouter: callerRouter } = this.options;
319
+ const contextOptionsRequested = recordHar !== void 0 || recordVideo !== void 0 || serviceWorkers === "block" || callerRouter !== void 0;
320
+ let routerCalled = false;
321
+ const initRouter = contextOptionsRequested || serviceWorkers !== void 0 ? async (context) => {
322
+ routerCalled = true;
323
+ if (callerRouter) await callerRouter(context);
324
+ } : void 0;
267
325
  let tab;
268
326
  try {
269
327
  tab = await launchHeadlessTab({
@@ -282,7 +340,11 @@ Underlying import error: ${cause}`
282
340
  storageStatePath: this.options.storageStatePath,
283
341
  ...relayAuth.authToken ? { authToken: relayAuth.authToken } : {},
284
342
  ...relayAuth.callerUserId ? { callerUserId: relayAuth.callerUserId } : {},
285
- ...pinnedTabId ? { pinnedTabId } : {}
343
+ ...pinnedTabId ? { pinnedTabId } : {},
344
+ ...recordHar !== void 0 ? { recordHar } : {},
345
+ ...recordVideo !== void 0 ? { recordVideo } : {},
346
+ ...serviceWorkers !== void 0 ? { serviceWorkers } : {},
347
+ ...initRouter ? { initRouter } : {}
286
348
  });
287
349
  } catch (err) {
288
350
  const cause = err instanceof Error ? err.message : String(err);
@@ -304,6 +366,24 @@ Underlying launch error: ${cause}`
304
366
  tabId: tab.tabId,
305
367
  close: tab.close
306
368
  };
369
+ if (contextOptionsRequested && !routerCalled) {
370
+ await this.onClose().catch(() => {
371
+ });
372
+ throw new WrapperTransportError(
373
+ "CONTEXT_OPTIONS_NOT_APPLIED",
374
+ `The installed '@qontinui/ui-bridge-headless' peer launched the tab without calling initRouter, so it predates the recordHar / recordVideo / serviceWorkers / initRouter passthrough (0.8.0, ui-bridge#381) and ignored them: no HAR or video would be written and no route installed. Upgrade it to 0.8.0 or later.`,
375
+ {
376
+ details: {
377
+ requested: {
378
+ recordHar: recordHar !== void 0,
379
+ recordVideo: recordVideo !== void 0,
380
+ serviceWorkers: serviceWorkers ?? null,
381
+ initRouter: callerRouter !== void 0
382
+ }
383
+ }
384
+ }
385
+ );
386
+ }
307
387
  await this.verifyEmulation(tab.page);
308
388
  await this.afterLaunch(tab.page);
309
389
  }
@@ -650,6 +730,7 @@ var InjectedTransport = class extends HeadlessTransport {
650
730
  execute,
651
731
  find: (criteria) => execute("find", criteria ?? {}),
652
732
  act: (id, request) => execute("executeElementAction", { id, request }),
733
+ element: (id) => execute("getElement", { id }),
653
734
  snapshot: (options) => execute("getControlSnapshot", options ?? {}),
654
735
  assert: (spec) => execute("aiAssert", spec)
655
736
  };