@weapp-vite/miniprogram-automator 1.2.6 → 1.2.7

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.d.mts CHANGED
@@ -44,13 +44,20 @@ declare class Transport extends EventEmitter {
44
44
  interface SendOptions {
45
45
  timeout?: number;
46
46
  }
47
+ interface ToolInfo {
48
+ version?: string;
49
+ }
47
50
  /** Connection 的实现。 */
48
51
  declare class Connection extends EventEmitter {
49
52
  private transport;
50
53
  private callbacks;
54
+ private useAppServicePageProtocol;
51
55
  constructor(transport: Transport);
52
56
  send(method: string, params?: Record<string, any>, options?: SendOptions): Promise<any>;
53
57
  dispose(): void;
58
+ /** 根据 DevTools 版本选择稳定的 Page 协议实现。 */
59
+ configureToolInfo(info: ToolInfo): void;
60
+ get prefersAppServicePageProtocol(): boolean;
54
61
  private onMessage;
55
62
  private onClose;
56
63
  static create(url: string, timeout?: number): Promise<Connection>;
@@ -248,7 +255,7 @@ declare class Page {
248
255
  query: any;
249
256
  private id;
250
257
  private elementMap;
251
- private preferRoutePageFallback;
258
+ private preferAppServicePageProtocol;
252
259
  constructor(connection: Connection, options: IPageOptions);
253
260
  updateFromOptions(options: IPageOptions): void;
254
261
  waitFor(condition: WaitCondition): Promise<void>;
package/dist/index.mjs CHANGED
@@ -506,10 +506,13 @@ const debugProtocol = createDebug("automator:protocol");
506
506
  const closeErrTip = "Connection closed, check if wechat web devTools is still running";
507
507
  const REQUEST_TIMEOUT = 3e4;
508
508
  const CONNECT_TIMEOUT = 3e4;
509
+ /** Page frame 定向消息失效、需要切换到 App-service Page 协议的 DevTools 版本。 */
510
+ const APP_SERVICE_PAGE_PROTOCOL_VERSIONS = /* @__PURE__ */ new Set(["2.01.2510290"]);
509
511
  /** Connection 的实现。 */
510
512
  var Connection = class Connection extends EventEmitter {
511
513
  transport;
512
514
  callbacks = /* @__PURE__ */ new Map();
515
+ useAppServicePageProtocol = false;
513
516
  constructor(transport) {
514
517
  super();
515
518
  this.transport = transport;
@@ -550,6 +553,13 @@ var Connection = class Connection extends EventEmitter {
550
553
  dispose() {
551
554
  this.transport.close();
552
555
  }
556
+ /** 根据 DevTools 版本选择稳定的 Page 协议实现。 */
557
+ configureToolInfo(info) {
558
+ this.useAppServicePageProtocol = APP_SERVICE_PAGE_PROTOCOL_VERSIONS.has(String(info.version ?? ""));
559
+ }
560
+ get prefersAppServicePageProtocol() {
561
+ return this.useAppServicePageProtocol;
562
+ }
553
563
  onMessage = (message) => {
554
564
  debugProtocol(`${dateFormat("yyyy-mm-dd HH:MM:ss:l")} ◀ RECV ${message}`);
555
565
  const { id, method, error, result, params } = JSON.parse(message);
@@ -701,7 +711,7 @@ async function acquireAutomatorPortLease(preferredPort) {
701
711
  }
702
712
  //#endregion
703
713
  //#region package.json
704
- var version = "1.2.6";
714
+ var version = "1.2.7";
705
715
  //#endregion
706
716
  //#region src/Native.ts
707
717
  /** Native 的实现。 */
@@ -823,12 +833,13 @@ var Page = class Page {
823
833
  query = {};
824
834
  id;
825
835
  elementMap = /* @__PURE__ */ new Map();
826
- preferRoutePageFallback = false;
836
+ preferAppServicePageProtocol = false;
827
837
  constructor(connection, options) {
828
838
  this.connection = connection;
829
839
  this.id = options.id;
830
840
  this.path = options.path;
831
841
  this.query = options.query;
842
+ this.preferAppServicePageProtocol = connection.prefersAppServicePageProtocol;
832
843
  }
833
844
  updateFromOptions(options) {
834
845
  this.path = options.path;
@@ -846,7 +857,7 @@ var Page = class Page {
846
857
  if (isStr(condition)) await waitUntil(async () => (await this.$$(condition)).length > 0);
847
858
  }
848
859
  async $(selector, options = {}) {
849
- if (options.routeOnly || this.preferRoutePageFallback) return await this.queryRouteElement(selector, options);
860
+ if (options.routeOnly || options.fallback !== false && this.preferAppServicePageProtocol) return await this.queryRouteElement(selector, options);
850
861
  try {
851
862
  const element = await this.send("Page.getElement", { selector }, { timeout: options.timeout ?? PAGE_QUERY_TIMEOUT });
852
863
  return Element.create(this.connection, {
@@ -855,14 +866,14 @@ var Page = class Page {
855
866
  }, this.elementMap);
856
867
  } catch (error) {
857
868
  if (options.fallback !== false && isRecoverablePageProtocolError(error, "Page.getElement")) {
858
- this.preferRoutePageFallback = true;
869
+ this.preferAppServicePageProtocol = true;
859
870
  return await this.queryRouteElement(selector, options);
860
871
  }
861
872
  return null;
862
873
  }
863
874
  }
864
875
  async $$(selector, options = {}) {
865
- if (options.routeOnly || this.preferRoutePageFallback) return await this.queryRouteElements(selector, options);
876
+ if (options.routeOnly || options.fallback !== false && this.preferAppServicePageProtocol) return await this.queryRouteElements(selector, options);
866
877
  try {
867
878
  const { elements } = await this.send("Page.getElements", { selector }, { timeout: options.timeout ?? PAGE_QUERY_TIMEOUT });
868
879
  return elements.map((element) => {
@@ -873,7 +884,7 @@ var Page = class Page {
873
884
  });
874
885
  } catch (error) {
875
886
  if (options.fallback === false || !isRecoverablePageProtocolError(error, "Page.getElements")) throw error;
876
- this.preferRoutePageFallback = true;
887
+ this.preferAppServicePageProtocol = true;
877
888
  return await this.queryRouteElements(selector, options);
878
889
  }
879
890
  }
@@ -1360,13 +1371,13 @@ var Page = class Page {
1360
1371
  const payload = {};
1361
1372
  if (path) payload.path = path;
1362
1373
  const timeout = options.timeout ?? PAGE_DATA_TIMEOUT;
1363
- if (options.routeOnly || this.preferRoutePageFallback) return await this.readRouteData(path, timeout);
1374
+ if (options.routeOnly || options.fallback !== false && this.preferAppServicePageProtocol) return await this.readRouteData(path, timeout);
1364
1375
  try {
1365
1376
  return (await this.send("Page.getData", payload, { timeout: Math.min(timeout, PAGE_DATA_PROTOCOL_TIMEOUT) })).data;
1366
1377
  } catch (error) {
1367
1378
  if (options.fallback === false) throw error;
1368
1379
  if (!isRecoverablePageProtocolError(error, "Page.getData")) throw error;
1369
- this.preferRoutePageFallback = true;
1380
+ this.preferAppServicePageProtocol = true;
1370
1381
  return await this.readRouteData(path, timeout);
1371
1382
  }
1372
1383
  }
@@ -1425,7 +1436,7 @@ var Page = class Page {
1425
1436
  }, { timeout })).result;
1426
1437
  }
1427
1438
  async setData(data) {
1428
- if (this.preferRoutePageFallback) {
1439
+ if (this.preferAppServicePageProtocol) {
1429
1440
  await this.setRouteData(data);
1430
1441
  return;
1431
1442
  }
@@ -1433,7 +1444,7 @@ var Page = class Page {
1433
1444
  await this.send("Page.setData", { data }, { timeout: PAGE_SET_DATA_PROTOCOL_TIMEOUT });
1434
1445
  } catch (error) {
1435
1446
  if (!isRecoverablePageProtocolError(error, "Page.setData") && !isPageStackStaleError(error)) throw error;
1436
- this.preferRoutePageFallback = true;
1447
+ this.preferAppServicePageProtocol = true;
1437
1448
  await this.setRouteData(data);
1438
1449
  }
1439
1450
  }
@@ -1448,7 +1459,7 @@ var Page = class Page {
1448
1459
  return await this.callMethodWithOptions(method, {}, ...args);
1449
1460
  }
1450
1461
  async callMethodWithOptions(method, options = {}, ...args) {
1451
- if (options.routeOnly || this.preferRoutePageFallback) return await this.callRouteMethod(method, args, options.timeout);
1462
+ if (options.routeOnly || options.fallback !== false && this.preferAppServicePageProtocol) return await this.callRouteMethod(method, args, options.timeout);
1452
1463
  try {
1453
1464
  return (await this.send("Page.callMethod", {
1454
1465
  method,
@@ -1457,7 +1468,7 @@ var Page = class Page {
1457
1468
  } catch (error) {
1458
1469
  if (options.fallback === false) throw error;
1459
1470
  if (!isRecoverablePageProtocolError(error, "Page.callMethod") && !isPageStackStaleError(error)) throw error;
1460
- this.preferRoutePageFallback = true;
1471
+ this.preferAppServicePageProtocol = true;
1461
1472
  }
1462
1473
  return await this.callRouteMethod(method, args, options.timeout);
1463
1474
  }
@@ -1943,7 +1954,9 @@ var MiniProgram = class extends EventEmitter {
1943
1954
  await this.send("App.addBinding", { name });
1944
1955
  }
1945
1956
  async checkVersion() {
1946
- const sdkVersion = (await this.send("Tool.getInfo")).SDKVersion;
1957
+ const toolInfo = await this.send("Tool.getInfo");
1958
+ this.connection.configureToolInfo(toolInfo);
1959
+ const sdkVersion = toolInfo.SDKVersion;
1947
1960
  if (sdkVersion !== "dev" && cmpVersion(sdkVersion, "2.7.3") < 0) throw new Error(`SDKVersion is currently ${sdkVersion}, while automator(${version}) requires at least version 2.7.3`);
1948
1961
  }
1949
1962
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-vite/miniprogram-automator",
3
3
  "type": "module",
4
- "version": "1.2.6",
4
+ "version": "1.2.7",
5
5
  "description": "完全兼容微信 miniprogram-automator 的现代化替代实现",
6
6
  "author": "ice breaker <1324318532@qq.com>",
7
7
  "license": "MIT",