cdp-client-tool 1.1.1 → 2.0.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/Client.d.ts CHANGED
@@ -1,60 +1,12 @@
1
- import { Browser } from "puppeteer-core";
2
- import { Logger } from "./logger";
1
+ import { Runner } from "./core/Runner";
3
2
  import { ClientOptions } from "./common";
4
- type ScriptJobType = 'local' | 'remote';
5
- type ScriptJobStatus = 'pending' | 'running' | 'success' | 'failed';
6
- export type ScriptJob = {
7
- id: string;
8
- type: ScriptJobType;
9
- filename?: string;
10
- description?: string;
11
- params?: any;
12
- createdAt: number;
13
- startedAt?: number;
14
- finishedAt?: number;
15
- status: ScriptJobStatus;
16
- errorMessage?: string;
17
- };
18
- export type ExecQueueResult = {
19
- status: 'executing';
20
- jobId: string;
21
- } | {
22
- status: 'queued';
23
- jobId: string;
24
- position: number;
25
- } | {
26
- status: 'overflow';
27
- reason: string;
28
- };
29
- export type ScriptQueueSnapshot = {
30
- running: ScriptJob | null;
31
- pending: ScriptJob[];
32
- capacity: number;
33
- };
3
+ import { WsHandler } from "./core/WsHandler";
34
4
  export declare class Client {
35
- private options;
36
- browser: Browser | undefined;
37
- logger: Logger;
38
- running: boolean;
39
- private scriptQueue;
40
- private runningJob;
41
- private readonly queueCapacity;
42
- private jobIdSeq;
43
- constructor(options?: ClientOptions);
44
- private nextJobId;
45
- runCatchFunction(func: Function): (...args: any[]) => Promise<any>;
46
- private init;
47
- getLocalModule(filename: string): any;
48
- getStringModule(moduleString: string): any;
49
- get ctx(): {
50
- greeting: string;
51
- browser: Browser;
52
- logger: Logger;
5
+ runner: Runner;
6
+ ws: WsHandler;
7
+ get deps(): {
8
+ runner: Runner;
9
+ ws: WsHandler;
53
10
  };
54
- getScriptQueueSnapshot(): ScriptQueueSnapshot;
55
- enqueueRemoteScript(raw: Buffer, params?: any): ExecQueueResult;
56
- enqueueLocalScript(filename: string, params?: any): ExecQueueResult;
57
- private enqueueJob;
58
- private runJob;
11
+ constructor(options: ClientOptions);
59
12
  }
60
- export {};
package/dist/common.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ManagerOptions, SocketOptions } from "socket.io-client";
2
- import { Client } from "./Client";
2
+ import { Runner } from "./core/Runner";
3
+ import { WsHandler } from "./core/WsHandler";
3
4
  export declare enum EVENTS {
4
5
  IS_FILE_EXIST = "is_file_exist",
5
6
  WRITE_FILE = "write_file",
@@ -8,7 +9,27 @@ export declare enum EVENTS {
8
9
  READ_FILE = "read_file",
9
10
  EXEC_LOCAL_SCRIPT = "exec_local_script",
10
11
  EXEC_REMOTE_SCRIPT = "exec_remote_script",
11
- SCRIPT_QUEUE = "script_queue"
12
+ SCRIPT_QUEUE = "script_queue",
13
+ INTERRUPT_SCRIPT = "interrupt_script",
14
+ UNDO_SCRIPT = "undo_script"
15
+ }
16
+ export type Context = {
17
+ runner: Runner;
18
+ ws: WsHandler;
19
+ };
20
+ export type ScriptJob = {
21
+ id: string;
22
+ type: 'local' | 'remote';
23
+ filename?: string;
24
+ script?: string;
25
+ params?: any;
26
+ };
27
+ export declare enum PushJobResult {
28
+ SUCCESS = "success",
29
+ SUCCESS_IN_QUEUE = "success_in_queue",
30
+ FAILED = "failed",
31
+ QUEUE_FULL = "queue_full",
32
+ FILE_SAVE_FAILED = "file_save_failed"
12
33
  }
13
34
  export type GatewayConfig = {
14
35
  name: string;
@@ -17,11 +38,13 @@ export type GatewayConfig = {
17
38
  onSocketInit?: (socket: any) => void;
18
39
  };
19
40
  export type ClientOptions = {
20
- deviceName: string;
21
- onInit?: (ctx: Client['ctx']) => void;
22
- onError?: (error: any) => void;
41
+ capacity?: number;
42
+ timeout?: number;
43
+ minInterval?: number;
23
44
  gateways: GatewayConfig[];
45
+ deviceName?: string;
24
46
  };
25
47
  export declare function resolveAndValidate(basePath: string): string;
26
48
  export declare function getSctiptFilePath(filename: string): void;
27
49
  export declare function getScreenshotFilePath(filename: string): void;
50
+ export declare function saveScriptFile(filename: string, script: string): Promise<void>;
@@ -0,0 +1,69 @@
1
+ import { EVENTS } from "../common";
2
+ export type EventPayloads = {
3
+ [EVENTS.IS_FILE_EXIST]: {
4
+ payload: {
5
+ path: string;
6
+ };
7
+ };
8
+ [EVENTS.WRITE_FILE]: {
9
+ payload: {
10
+ filename: string;
11
+ content: string | Uint8Array;
12
+ flags?: string;
13
+ };
14
+ };
15
+ [EVENTS.RM]: {
16
+ payload: {
17
+ path: string;
18
+ recursive?: boolean;
19
+ };
20
+ };
21
+ [EVENTS.READ_DIR]: {
22
+ payload: {
23
+ path: string;
24
+ };
25
+ };
26
+ [EVENTS.READ_FILE]: {
27
+ payload: {
28
+ path: string;
29
+ };
30
+ };
31
+ [EVENTS.EXEC_LOCAL_SCRIPT]: {
32
+ payload: {
33
+ filename: string;
34
+ params?: any;
35
+ };
36
+ };
37
+ [EVENTS.EXEC_REMOTE_SCRIPT]: {
38
+ payload: {
39
+ raw: Buffer;
40
+ params?: any;
41
+ };
42
+ };
43
+ [EVENTS.SCRIPT_QUEUE]: {
44
+ payload: Record<string, never>;
45
+ };
46
+ [EVENTS.INTERRUPT_SCRIPT]: {
47
+ payload: {
48
+ jobId: string;
49
+ };
50
+ };
51
+ [EVENTS.UNDO_SCRIPT]: {
52
+ payload: {
53
+ jobId: string;
54
+ };
55
+ };
56
+ };
57
+ export type EventKey = keyof EventPayloads;
58
+ export type HandlerContext<K extends EventKey = EventKey> = {
59
+ data: EventPayloads[K];
60
+ callback: (...args: any[]) => void;
61
+ gateway: import("../common").GatewayConfig;
62
+ ctx: import("../common").Context;
63
+ };
64
+ export type HandlerFor<K extends EventKey> = HandlerContext<K>;
65
+ export declare function getHandlerRegistry(instance: object): Array<{
66
+ event: EventKey;
67
+ key: string;
68
+ }>;
69
+ export declare function Handler<K extends EventKey>(eventName: K): (target: object, propertyKey: string, _descriptor?: PropertyDescriptor) => void;
@@ -0,0 +1,28 @@
1
+ import { Context, ScriptJob } from 'src/common';
2
+ type RunnerOptions = {
3
+ capacity: number;
4
+ minInterval: number;
5
+ timeout: number;
6
+ };
7
+ export declare class Runner {
8
+ private readonly ctx;
9
+ private readonly options;
10
+ private queue;
11
+ private last_finished_at?;
12
+ private jobIdSeq;
13
+ private worker?;
14
+ constructor(ctx: Context, options: RunnerOptions);
15
+ private nextJobId;
16
+ getQueueLength(): number;
17
+ interruptJob(jobId: string): {
18
+ ok: boolean;
19
+ reason?: string;
20
+ };
21
+ getScriptQueueSnapshot(): {
22
+ running: any;
23
+ pending: ScriptJob[];
24
+ capacity: number;
25
+ };
26
+ execJob(job: ScriptJob): any;
27
+ }
28
+ export {};
@@ -0,0 +1,15 @@
1
+ import { Worker } from 'node:worker_threads';
2
+ import { ScriptJob } from 'src/common';
3
+ type ScriptWorkerOptions = {
4
+ job: ScriptJob;
5
+ timeout: number;
6
+ };
7
+ export declare class ScriptWorker {
8
+ private readonly options;
9
+ worker: Worker;
10
+ private timeoutTimer?;
11
+ constructor(options: ScriptWorkerOptions);
12
+ terminate(): void;
13
+ run(): Promise<void>;
14
+ }
15
+ export {};
@@ -0,0 +1,25 @@
1
+ import { Context, EVENTS, GatewayConfig } from "../common";
2
+ import { HandlerContext } from "./Handler.decorator";
3
+ type WsHandlerOptions = {
4
+ gateways: GatewayConfig[];
5
+ deviceName?: string;
6
+ onInit?: (ctx: Context) => void;
7
+ };
8
+ export declare class WsHandler {
9
+ private readonly ctx;
10
+ private readonly options;
11
+ constructor(ctx: Context, options: WsHandlerOptions);
12
+ init(): void;
13
+ private attachHandlers;
14
+ readDir({ data, callback }: HandlerContext<typeof EVENTS.READ_DIR>): Promise<void>;
15
+ readFile({ data, callback }: HandlerContext<typeof EVENTS.READ_FILE>): Promise<void>;
16
+ writeFile({ data, callback }: HandlerContext<typeof EVENTS.WRITE_FILE>): Promise<void>;
17
+ rm({ data, callback }: HandlerContext<typeof EVENTS.RM>): Promise<void>;
18
+ execLocalScript({ data, callback }: HandlerContext<typeof EVENTS.EXEC_LOCAL_SCRIPT>): Promise<void>;
19
+ execRemoteScript({ data, callback }: HandlerContext<typeof EVENTS.EXEC_REMOTE_SCRIPT>): Promise<void>;
20
+ private mapExecResult;
21
+ interruptScript({ data, callback }: HandlerContext<typeof EVENTS.INTERRUPT_SCRIPT>): Promise<void>;
22
+ undoScript({ data, callback }: HandlerContext<typeof EVENTS.UNDO_SCRIPT>): Promise<void>;
23
+ scriptQueue({ callback }: HandlerContext<typeof EVENTS.SCRIPT_QUEUE>): Promise<void>;
24
+ }
25
+ export {};
@@ -0,0 +1,3 @@
1
+ export { Handler, getHandlerRegistry, HandlerContext, HandlerFor, EventPayloads, type EventKey, } from "./Handler.decorator";
2
+ export { WsHandler } from "./WsHandler";
3
+ export { Runner } from "./Runner";
@@ -1,4 +1,6 @@
1
1
  import { Client } from "./Client";
2
2
  export type excuteFn = (ctx: Client['ctx'] & {
3
3
  params?: any;
4
+ abortSignal?: AbortSignal;
5
+ throwIfAborted?: () => void;
4
6
  }) => Promise<void>;