@weapp-vite/miniprogram-automator 1.2.23 → 1.3.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/README.md CHANGED
@@ -141,6 +141,22 @@ await miniProgram.flushConsole()
141
141
 
142
142
  先用 `addListener` 被动注册,再显式等待 `enableLog`,可以记录初始化期间的日志。`flushConsole()` 等待调用时已收到的错误属性检查完成;每次查询最多等待 1 秒,失败会在原参数上保留 `inspectionError`。断开连接时尚未完成的日志也会携带该诊断发布。只有宿主明确返回 CDP 命令不支持时才回退 SDK 格式;其他初始化错误仍然抛出。重复调用 `enableLog` 会刷新 Runtime 订阅,并保留已选择的格式。
143
143
 
144
+ ### 4.7 AppService 堆内存探测
145
+
146
+ `getAppServiceHeapUsage({ timeout })` 通过现有 `App.CDPCommand` 调用 `Runtime.getHeapUsage`,默认超时 2500ms。支持时返回 `{ status: 'available', source: 'appservice-cdp-runtime', usedSize, totalSize }`,两个值均为字节。仅当宿主明确不支持该协议或方法时,返回 `status: 'unsupported'` 及 `protocol-unimplemented` / `method-not-found` 原因。
147
+
148
+ ```ts
149
+ const heap = await miniProgram.getAppServiceHeapUsage({ timeout: 2_500 })
150
+ if (heap.status === 'available') {
151
+ console.log({ usedBytes: heap.usedSize, allocatedBytes: heap.totalSize })
152
+ }
153
+ else {
154
+ console.log({ unsupported: heap.reason })
155
+ }
156
+ ```
157
+
158
+ 空结果、缺失字段、字符串、负数、非有限数值或已用量超过分配量都会报错;连接超时与其他协议错误原样传播,不会转换为不支持或零内存。每次调用重新探测,不缓存旧会话的能力结果,也不主动触发 GC。该指标仅覆盖 AppService JS 堆,不是 renderer/native 内存、进程 RSS、峰值或可归因的组件独占内存。实际可用性取决于连接的 DevTools/基础库,仍须在目标宿主确认;headless 不提供真实 IDE 堆证据。
159
+
144
160
  ## 5. 主要导出
145
161
 
146
162
  | 导出 | 说明 |
@@ -164,3 +180,9 @@ pnpm --filter @weapp-vite/miniprogram-automator typecheck
164
180
 
165
181
  - 仓库:https://github.com/weapp-vite/weapp-vite
166
182
  - `weapp-ide-cli`:[../weapp-ide-cli/README.md](../weapp-ide-cli/README.md)
183
+
184
+ ## 启动预算与取消
185
+
186
+ 微信 DevTools 的 `launch` / `connect` 使用一次总 `timeout`(默认 30 秒)。路径准备、端口租约、HTTP 适配、WebSocket、版本与 App ready 均消耗剩余预算,自动重试不重置 deadline。可传 `signal` 取消;成功后连接归调用方管理。
187
+
188
+ 失败保留 `cause`,`error.operation` 包含阶段、最后成功阶段、耗时、剩余预算、尝试次数、未退出操作数量及本次资源清理状态。清理 `pending` 表示在预算内未确认退出,不能当作资源已释放。迟到连接只断开自身 WebSocket;启动器只终止持有句柄的 CLI 子进程,不据此认领 IDE 宿主或清理其他项目。无需在 App ready 后再固定等待五秒。
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { OPERATION_TIMEOUT_CODE, OperationDiagnostics, OperationErrorCategory, OperationLifecycle, OperationStepOptions, classifyOperationError, isRecoverableOperationError, readWechatLoginState } from "./operation.mjs";
1
2
  import { EventEmitter } from "node:events";
2
3
  import WebSocket from "ws";
3
4
  import { Buffer } from "node:buffer";
@@ -31,6 +32,22 @@ declare function get(label?: string): SmartappLogger;
31
32
  declare function currentTimestamp(): number;
32
33
  declare function delay(ms: number): Promise<void>;
33
34
  //#endregion
35
+ //#region src/appServiceHeap.d.ts
36
+ interface AppServiceHeapUsageOptions {
37
+ /** 单次协议请求的超时预算,单位毫秒。 */
38
+ timeout?: number;
39
+ }
40
+ type AppServiceHeapUsage = {
41
+ status: 'available';
42
+ source: 'appservice-cdp-runtime';
43
+ usedSize: number;
44
+ totalSize: number;
45
+ } | {
46
+ status: 'unsupported';
47
+ source: 'appservice-cdp-runtime';
48
+ reason: 'protocol-unimplemented' | 'method-not-found';
49
+ };
50
+ //#endregion
34
51
  //#region src/Transport.d.ts
35
52
  /** Transport 的实现。 */
36
53
  declare class Transport extends EventEmitter {
@@ -50,6 +67,7 @@ interface ToolInfo {
50
67
  /** Connection 的实现。 */
51
68
  declare class Connection extends EventEmitter {
52
69
  private transport;
70
+ private disposed;
53
71
  private callbacks;
54
72
  private useAppServicePageProtocol;
55
73
  private useAppServicePageMethod;
@@ -63,7 +81,7 @@ declare class Connection extends EventEmitter {
63
81
  get prefersAppServicePageMethod(): boolean;
64
82
  private onMessage;
65
83
  private onClose;
66
- static create(url: string, timeout?: number): Promise<Connection>;
84
+ static create(url: string, timeout?: number, signal?: AbortSignal): Promise<Connection>;
67
85
  }
68
86
  //#endregion
69
87
  //#region src/structuredConsole.d.ts
@@ -381,6 +399,8 @@ declare class MiniProgram extends EventEmitter {
381
399
  restorePluginWxMethod(pluginId: string, method: string): Promise<void>;
382
400
  evaluate(appFunction: AutomatorCallable | string, ...args: any[]): Promise<any>;
383
401
  evaluateWithOptions(appFunction: AutomatorCallable | string, options?: EvaluateOptions, ...args: any[]): Promise<any>;
402
+ /** 探测并读取 AppService JS 堆;明确不支持时返回原因,连接故障与无效响应继续抛出。 */
403
+ getAppServiceHeapUsage(options?: AppServiceHeapUsageOptions): Promise<AppServiceHeapUsage>;
384
404
  pageScrollTo(scrollTop: number): Promise<void>;
385
405
  close(): Promise<void>;
386
406
  remote(auto?: boolean): Promise<void>;
@@ -392,7 +412,7 @@ declare class MiniProgram extends EventEmitter {
392
412
  exposeFunction(name: string, bindingFunction: AutomatorCallable): Promise<void>;
393
413
  checkVersion(timeout?: number): Promise<void>;
394
414
  /**
395
- * @description 等待小程序 App 域协议可用,避免返回半就绪自动化会话。
415
+ * @description 等待原生协议返回真实页面标识和路径,避免返回半就绪自动化会话。
396
416
  */
397
417
  waitForAppReady(timeout?: number): Promise<void>;
398
418
  screenshot(options?: IScreenshotOptions): Promise<string | undefined>;
@@ -605,11 +625,13 @@ export declare function normalizePlatform(platform?: MiniprogramAutomatorPlatfor
605
625
  //#region src/Launcher.d.ts
606
626
  export interface IConnectOptions {
607
627
  wsEndpoint: string;
628
+ signal?: AbortSignal;
608
629
  timeout?: number;
609
630
  platform?: MiniprogramAutomatorPlatform;
610
631
  }
611
632
  /** ILaunchOptions 的类型定义。 */
612
633
  export interface ILaunchOptions {
634
+ signal?: AbortSignal;
613
635
  platform?: MiniprogramAutomatorPlatform;
614
636
  cliPath?: string;
615
637
  connectType?: string;
@@ -669,6 +691,17 @@ declare class Automator {
669
691
  launch(options: ILaunchOptions): Promise<any>;
670
692
  }
671
693
  //#endregion
694
+ //#region src/launcher/portLease.d.ts
695
+ interface AutomatorPortLease {
696
+ path?: string;
697
+ port: number;
698
+ release: () => Promise<void>;
699
+ }
700
+ /**
701
+ * @description 选择并短暂租约一个自动化端口,避免并发启动选择到同一端口。
702
+ */
703
+ export declare function acquireAutomatorPortLease(preferredPort?: number): Promise<AutomatorPortLease>;
704
+ //#endregion
672
705
  //#region src/util.d.ts
673
706
  /** decodeQrCode 的方法封装。 */
674
707
  export declare function decodeQrCode(qrCode: string): Promise<string>;
@@ -705,4 +738,4 @@ export declare const SmartappAutomator: {
705
738
  };
706
739
  };
707
740
  //#endregion
708
- export { Automator, Connection, type ConsoleLogOptions, type ConsoleRemoteObject, Element, Launcher, MiniProgram, Native, Page, type StructuredConsoleEntry, Transport };
741
+ export { type AppServiceHeapUsage, type AppServiceHeapUsageOptions, Automator, type AutomatorPortLease, Connection, type ConsoleLogOptions, type ConsoleRemoteObject, Element, Launcher, MiniProgram, Native, OPERATION_TIMEOUT_CODE, OperationDiagnostics, OperationErrorCategory, OperationLifecycle, OperationStepOptions, Page, type StructuredConsoleEntry, Transport, classifyOperationError, isRecoverableOperationError, readWechatLoginState };