@hira-core/sdk 1.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.
@@ -0,0 +1,266 @@
1
+ import * as puppeteer from 'puppeteer-core';
2
+ import { Browser, Page, Frame, ElementHandle } from 'puppeteer-core';
3
+ import { EventEmitter } from 'events';
4
+ import { IWorkerLogMessage, ProfileStatus } from '@packages/shared';
5
+
6
+ interface IGpmProfile {
7
+ id: string;
8
+ name: string;
9
+ raw_proxy?: string;
10
+ browser_type: "chromium" | "firefox";
11
+ browser_version: string;
12
+ group_id?: string;
13
+ profile_path: string;
14
+ note: string;
15
+ created_at: string;
16
+ }
17
+ type IInputType = "text" | "number" | "boolean" | "select" | "textarea";
18
+ type InputTypeMap = {
19
+ text: string;
20
+ number: number;
21
+ boolean: boolean;
22
+ select: string;
23
+ textarea: string;
24
+ };
25
+ interface IInputField {
26
+ key: string;
27
+ label: string;
28
+ type: IInputType;
29
+ required?: boolean;
30
+ defaultValue?: string | number | boolean;
31
+ placeholder?: string;
32
+ options?: {
33
+ label: string;
34
+ value: string | number;
35
+ }[];
36
+ }
37
+ interface IFlowConfig {
38
+ globalInput: readonly IInputField[];
39
+ profileInput: readonly IInputField[];
40
+ }
41
+ type InferFields<T extends readonly IInputField[]> = {
42
+ [K in T[number] as K["key"]]: K["type"] extends keyof InputTypeMap ? InputTypeMap[K["type"]] : unknown;
43
+ };
44
+ type InferGlobalInput<T extends IFlowConfig> = InferFields<T["globalInput"]>;
45
+ type InferProfileInput<T extends IFlowConfig> = InferFields<T["profileInput"]>;
46
+ interface IProfileSetting<TProfileInput = Record<string, unknown>> {
47
+ profileName: string;
48
+ data?: TProfileInput;
49
+ }
50
+ interface IAntidetectConfig<TProfileInput = Record<string, unknown>> {
51
+ name: string;
52
+ profileSettings: IProfileSetting<TProfileInput>[];
53
+ }
54
+ interface IExecutionConfig {
55
+ concurrency: number;
56
+ delayBetweenProfilesMs?: number;
57
+ maxRetries?: number;
58
+ keepOpenOnError?: boolean;
59
+ }
60
+ interface IBrowserWindowConfig {
61
+ width: number;
62
+ height: number;
63
+ scale: number;
64
+ }
65
+ interface IFlowRunParams<TConfig extends IFlowConfig = IFlowConfig> {
66
+ antidetect: IAntidetectConfig<Partial<InferProfileInput<TConfig>>>;
67
+ execution: IExecutionConfig;
68
+ window: IBrowserWindowConfig;
69
+ globalInput?: InferGlobalInput<TConfig>;
70
+ }
71
+ type FlowLogLevel = "info" | "warn" | "error" | "debug" | "success";
72
+ interface IFlowLogEntry {
73
+ level: FlowLogLevel;
74
+ message: string;
75
+ timestamp: string;
76
+ context?: string;
77
+ metadata?: any;
78
+ }
79
+ type LogContext = string | number | boolean | null | undefined | Error | Record<string, unknown> | Array<unknown> | unknown;
80
+ interface ILogger {
81
+ log(message: string, context?: LogContext): void;
82
+ info(message: string, context?: LogContext): void;
83
+ warn(message: string, context?: LogContext): void;
84
+ error(message: string, context?: LogContext): void;
85
+ debug(message: string, context?: LogContext): void;
86
+ success(message: string, context?: LogContext): void;
87
+ logWithProfile(level: string, message: string, profileName: string, context?: LogContext): void;
88
+ }
89
+ interface IScriptContext<TConfig extends IFlowConfig = IFlowConfig> {
90
+ browser: Browser;
91
+ page: Page;
92
+ profile: IGpmProfile;
93
+ index: number;
94
+ globalInput: InferGlobalInput<TConfig>;
95
+ profileInput: InferProfileInput<TConfig>;
96
+ logger: ILogger;
97
+ }
98
+ interface IBrowserAdapter {
99
+ open(profileName: string, index: number, windowConfig?: IBrowserWindowConfig): Promise<{
100
+ browser: Browser;
101
+ page: Page;
102
+ profile: IGpmProfile;
103
+ }>;
104
+ close(profileId: string): Promise<void>;
105
+ }
106
+ interface IGpmStartProfileParams {
107
+ profile_id: string;
108
+ addination_args?: string;
109
+ win_scale?: number;
110
+ win_pos?: string;
111
+ win_size?: string;
112
+ }
113
+ interface IResGpmStartProfileData {
114
+ success: boolean;
115
+ profile_id: string;
116
+ browser_location: string;
117
+ remote_debugging_address: string;
118
+ driver_path: string;
119
+ }
120
+ interface IGpmRes<T> {
121
+ success: boolean;
122
+ data?: T;
123
+ message: string;
124
+ }
125
+ interface IGpmResListProfile {
126
+ success: boolean;
127
+ data: IGpmProfile[];
128
+ pagination: {
129
+ total: number;
130
+ page: number;
131
+ page_size: number;
132
+ total_page: number;
133
+ };
134
+ message: string;
135
+ }
136
+ interface IGpmListProfilesParams {
137
+ group_id?: string;
138
+ page?: number;
139
+ per_page?: number;
140
+ sort?: number;
141
+ search?: string[];
142
+ }
143
+
144
+ declare abstract class BaseFlow<TConfig extends IFlowConfig = IFlowConfig> {
145
+ protected logger: ILogger;
146
+ protected browserAdapter: IBrowserAdapter;
147
+ readonly flowConfig: TConfig;
148
+ constructor(logger: ILogger, browserAdapter: IBrowserAdapter, config: TConfig);
149
+ protected log(message: string, profileName?: string): void;
150
+ protected error(message: string, profileName?: string): void;
151
+ protected sleep(ms: number): Promise<void>;
152
+ run(params: IFlowRunParams<TConfig>): Promise<void>;
153
+ protected processProfileWithRetry(setting: IProfileSetting<Partial<InferProfileInput<TConfig>>>, index: number, params: IFlowRunParams<TConfig>): Promise<void>;
154
+ private processProfile;
155
+ abstract script(context: IScriptContext<TConfig>): Promise<unknown>;
156
+ createProfileSetting(profileName: string, data?: Partial<InferProfileInput<TConfig>>): IProfileSetting<Partial<InferProfileInput<TConfig>>>;
157
+ createRunParams(params: IFlowRunParams<TConfig>): IFlowRunParams<TConfig>;
158
+ }
159
+ type FlowRunParams<T extends BaseFlow<any>> = T extends BaseFlow<infer TConfig> ? IFlowRunParams<TConfig> : never;
160
+ type FlowProfileSetting<T extends BaseFlow<any>> = T extends BaseFlow<infer TConfig> ? IProfileSetting<Partial<InferProfileInput<TConfig>>> : never;
161
+
162
+ declare enum AntidetectProvider {
163
+ GPM = "gpm",
164
+ HIDEMIUM = "hidemium",
165
+ GENLOGIN = "genlogin"
166
+ }
167
+ declare abstract class AntidetectBaseFlow<TConfig extends IFlowConfig = IFlowConfig> extends BaseFlow<TConfig> {
168
+ protected provider: AntidetectProvider;
169
+ constructor(provider: AntidetectProvider, logger: ILogger, config: TConfig);
170
+ get currentAdapter(): IBrowserAdapter;
171
+ }
172
+
173
+ declare class GpmService {
174
+ private api;
175
+ private apiUrl;
176
+ constructor(apiUrl?: string);
177
+ startProfile(params: IGpmStartProfileParams, maxRetries?: number, delayMs?: number): Promise<IResGpmStartProfileData | null>;
178
+ stopProfile(id: string, maxRetries?: number, delayMs?: number): Promise<boolean>;
179
+ getProfiles(params?: IGpmListProfilesParams): Promise<IGpmProfile[]>;
180
+ createProfile(config: Partial<IGpmProfile>): Promise<IGpmProfile | null>;
181
+ deleteProfile(id: string): Promise<boolean>;
182
+ updateProxy(id: string, proxy: string): Promise<boolean>;
183
+ checkHealth(): Promise<boolean>;
184
+ connectToBrowser(debugAddress: string, maxRetries?: number, delayMs?: number): Promise<Browser>;
185
+ }
186
+
187
+ declare class GpmStandaloneAdapter implements IBrowserAdapter {
188
+ private apiUrl;
189
+ private screenResolution;
190
+ readonly service: GpmService;
191
+ private readonly openedProfiles;
192
+ private readonly pendingProfiles;
193
+ private readonly logger;
194
+ private readonly boundLoggers;
195
+ constructor(logger: ILogger);
196
+ private getBoundLogger;
197
+ open(profileName: string, index: number, windowConfig: IBrowserWindowConfig): Promise<{
198
+ browser: puppeteer.Browser;
199
+ page: puppeteer.Page;
200
+ profile: IGpmProfile;
201
+ }>;
202
+ close(profileId: string): Promise<void>;
203
+ closeAll(delayBetweenMs?: number): Promise<void>;
204
+ }
205
+
206
+ declare class BrowserUtils {
207
+ private ctx;
208
+ private logger;
209
+ constructor(context: IScriptContext);
210
+ private checkAbort;
211
+ sleep(ms: number): Promise<void>;
212
+ waitForElement(selector: string, timeout?: number, scope?: Frame): Promise<ElementHandle | null>;
213
+ click(target: string | ElementHandle, options?: {
214
+ delay?: number;
215
+ timeout?: number;
216
+ frame?: Frame;
217
+ }): Promise<boolean>;
218
+ type(selector: string, text: string, options?: {
219
+ delay?: number;
220
+ frame?: Frame;
221
+ }): Promise<boolean>;
222
+ getText(selector: string, frame?: Frame): Promise<string | null>;
223
+ exists(selector: string, timeout?: number, frame?: Frame): Promise<boolean>;
224
+ goto(url: string, options?: {
225
+ waitUntil?: "load" | "domcontentloaded" | "networkidle0" | "networkidle2";
226
+ }): Promise<boolean>;
227
+ waitForNavigation(options?: {
228
+ timeout?: number;
229
+ waitUntil?: "load" | "domcontentloaded" | "networkidle0" | "networkidle2";
230
+ }): Promise<boolean>;
231
+ screenshot(path?: string): Promise<unknown>;
232
+ switchToPopup(matcher: string | RegExp, timeout?: number): Promise<puppeteer.Page | null>;
233
+ switchToTabIndex(index: number): Promise<puppeteer.Page | null>;
234
+ closeCurrentTab(): Promise<void>;
235
+ closeOtherTabs(): Promise<void>;
236
+ logConfig(config: Record<string, unknown>, label?: string): Promise<void>;
237
+ logProfileInput(): Promise<void>;
238
+ logGlobalInput(): Promise<void>;
239
+ private shortSelector;
240
+ private log;
241
+ private actionDelay;
242
+ private rawSleep;
243
+ }
244
+
245
+ declare class FlowLogger extends EventEmitter implements ILogger {
246
+ private _context?;
247
+ private parentPort;
248
+ private readonly isWorker;
249
+ constructor(_context?: string | undefined);
250
+ private serialize;
251
+ private dispatch;
252
+ private printToConsole;
253
+ private buildMsg;
254
+ log(message: string, context?: LogContext): void;
255
+ info(message: string, context?: LogContext): void;
256
+ success(message: string, context?: LogContext): void;
257
+ warn(message: string, context?: LogContext): void;
258
+ error(message: string, context?: LogContext): void;
259
+ debug(message: string, context?: LogContext): void;
260
+ custom(label: string, message: string, color?: string): void;
261
+ logWithProfile(level: IWorkerLogMessage["level"], message: string, profileName: string, context?: LogContext): void;
262
+ createBoundLogger(profileName: string): ILogger;
263
+ profileEvent(profileName: string, message: string, status: ProfileStatus, context?: LogContext): void;
264
+ }
265
+
266
+ export { AntidetectBaseFlow, AntidetectProvider, BaseFlow, BrowserUtils, type FlowLogLevel, FlowLogger, type FlowProfileSetting, type FlowRunParams, GpmStandaloneAdapter, type IAntidetectConfig, type IBrowserAdapter, type IBrowserWindowConfig, type IExecutionConfig, type IFlowConfig, type IFlowLogEntry, type IFlowRunParams, type IGpmListProfilesParams, type IGpmProfile, type IGpmRes, type IGpmResListProfile, type IGpmStartProfileParams, type IInputField, type ILogger, type IProfileSetting, type IResGpmStartProfileData, type IScriptContext, type InferGlobalInput, type InferProfileInput, type LogContext };