-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathclient.ts
More file actions
394 lines (335 loc) · 10.3 KB
/
client.ts
File metadata and controls
394 lines (335 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* ReaderClient
*
* A client wrapper that manages HeroCore lifecycle and provides
* a simple interface for scraping and crawling.
*
* @example
* const reader = new ReaderClient();
*
* const result = await reader.scrape({
* urls: ['https://example.com'],
* formats: ['markdown'],
* });
*
* console.log(result.data[0].markdown);
*
* // When done (optional - auto-closes on process exit)
* await reader.close();
*/
import HeroCore from "@ulixee/hero-core";
import { TransportBridge } from "@ulixee/net";
import { ConnectionToHeroCore } from "@ulixee/hero";
import { scrape } from "./scraper";
import { crawl } from "./crawler";
import { HeroBrowserPool } from "./browser/pool";
import type { ScrapeOptions, ScrapeResult, ProxyConfig, BrowserPoolConfig } from "./types";
import type { CrawlOptions, CrawlResult } from "./crawl-types";
import { createLogger } from "./utils/logger";
const logger = createLogger("client");
/**
* Proxy rotation strategy
*/
export type ProxyRotation = "round-robin" | "random";
/**
* Configuration options for ReaderClient
*/
export interface ReaderClientOptions {
/** Enable verbose logging (default: false) */
verbose?: boolean;
/** Show Chrome browser window (default: false) */
showChrome?: boolean;
/** Browser pool configuration */
browserPool?: BrowserPoolConfig;
/** List of proxies to rotate through */
proxies?: ProxyConfig[];
/** Proxy rotation strategy (default: "round-robin") */
proxyRotation?: ProxyRotation;
/** Skip TLS/SSL certificate verification (default: true) */
skipTLSVerification?: boolean;
}
/**
* ReaderClient manages the HeroCore lifecycle and provides
* scrape/crawl methods with automatic initialization.
*/
export class ReaderClient {
private heroCore: HeroCore | null = null;
private pool: HeroBrowserPool | null = null;
private initialized = false;
private initializing: Promise<void> | null = null;
private closed = false;
private options: ReaderClientOptions;
private proxyIndex = 0;
private cleanupHandler: (() => Promise<void>) | null = null;
constructor(options: ReaderClientOptions = {}) {
this.options = options;
// Configure TLS verification
// Hero uses MITM_ALLOW_INSECURE env var to skip certificate verification
// Default is true (skip verification) for compatibility with various sites
const skipTLS = options.skipTLSVerification ?? true;
if (skipTLS) {
process.env.MITM_ALLOW_INSECURE = "true";
}
// Register cleanup on process exit
this.registerCleanup();
}
/**
* Get the next proxy from the rotation pool
*/
private getNextProxy(): ProxyConfig | undefined {
const { proxies, proxyRotation = "round-robin" } = this.options;
if (!proxies || proxies.length === 0) {
return undefined;
}
if (proxyRotation === "random") {
return proxies[Math.floor(Math.random() * proxies.length)];
}
// Round-robin (default)
const proxy = proxies[this.proxyIndex % proxies.length];
this.proxyIndex++;
return proxy;
}
/**
* Initialize HeroCore. Called automatically on first scrape/crawl.
* Can be called explicitly if you want to pre-warm the client.
*/
async start(): Promise<void> {
if (this.closed) {
throw new Error("ReaderClient has been closed. Create a new instance.");
}
if (this.initialized) {
return;
}
// Prevent concurrent initialization
if (this.initializing) {
await this.initializing;
return;
}
this.initializing = this.initializeCore();
await this.initializing;
this.initializing = null;
}
/**
* Internal initialization logic
*/
private async initializeCore(): Promise<void> {
try {
if (this.options.verbose) {
logger.info("Starting HeroCore...");
}
this.heroCore = new HeroCore();
await this.heroCore.start();
if (this.options.verbose) {
logger.info("HeroCore started successfully");
}
// Initialize browser pool
if (this.options.verbose) {
logger.info("Initializing browser pool...");
}
const browserPoolConfig = this.options.browserPool;
const poolConfig = {
size: browserPoolConfig?.size ?? 2,
retireAfterPageCount: browserPoolConfig?.retireAfterPages ?? 100,
retireAfterAgeMs: (browserPoolConfig?.retireAfterMinutes ?? 30) * 60 * 1000,
maxQueueSize: browserPoolConfig?.maxQueueSize ?? 100,
};
this.pool = new HeroBrowserPool(
poolConfig,
undefined, // proxy set per-request
this.options.showChrome,
this.createConnection(),
undefined, // userAgent
this.options.verbose
);
await this.pool.initialize();
this.initialized = true;
if (this.options.verbose) {
logger.info("Browser pool initialized successfully");
}
} catch (error: any) {
// Clean up on failure
if (this.pool) {
await this.pool.shutdown().catch(() => {});
this.pool = null;
}
if (this.heroCore) {
await this.heroCore.close().catch(() => {});
this.heroCore = null;
}
this.initialized = false;
// Provide helpful error messages
const message = error.message || String(error);
if (message.includes("EADDRINUSE")) {
throw new Error(
"Failed to start HeroCore: Port already in use. " +
"Another instance may be running. " +
"Close it or use a different port."
);
}
if (message.includes("chrome") || message.includes("Chrome")) {
throw new Error(
"Failed to start HeroCore: Chrome/Chromium not found. " +
"Please install Chrome or set CHROME_PATH environment variable."
);
}
throw new Error(`Failed to start HeroCore: ${message}`);
}
}
/**
* Create a connection to the HeroCore instance
*/
private createConnection(): ConnectionToHeroCore {
if (!this.heroCore) {
throw new Error("HeroCore not initialized. This should not happen.");
}
const bridge = new TransportBridge();
this.heroCore.addConnection(bridge.transportToClient);
return new ConnectionToHeroCore(bridge.transportToCore);
}
/**
* Ensure client is initialized before operation
*/
private async ensureInitialized(): Promise<void> {
if (this.closed) {
throw new Error("ReaderClient has been closed. Create a new instance.");
}
if (!this.initialized) {
await this.start();
}
}
/**
* Scrape one or more URLs
*
* @param options - Scrape options (urls, formats, etc.)
* @returns Scrape result with data and metadata
*
* @example
* const result = await reader.scrape({
* urls: ['https://example.com'],
* formats: ['markdown', 'html'],
* });
*/
async scrape(options: Omit<ScrapeOptions, "connectionToCore" | "pool">): Promise<ScrapeResult> {
await this.ensureInitialized();
if (!this.pool) {
throw new Error("Browser pool not initialized. This should not happen.");
}
// Use proxy rotation if proxies are configured and no specific proxy is provided
const proxy = options.proxy ?? this.getNextProxy();
return await scrape({
...options,
proxy,
showChrome: options.showChrome ?? this.options.showChrome,
verbose: options.verbose ?? this.options.verbose,
pool: this.pool,
});
}
/**
* Crawl a website to discover URLs
*
* @param options - Crawl options (url, depth, maxPages, etc.)
* @returns Crawl result with discovered URLs and optional scraped content
*
* @example
* const result = await reader.crawl({
* url: 'https://example.com',
* depth: 2,
* maxPages: 50,
* scrape: true,
* });
*/
async crawl(options: Omit<CrawlOptions, "connectionToCore" | "pool">): Promise<CrawlResult> {
await this.ensureInitialized();
if (!this.pool) {
throw new Error("Browser pool not initialized. This should not happen.");
}
// Use proxy rotation if proxies are configured and no specific proxy is provided
const proxy = options.proxy ?? this.getNextProxy();
return await crawl({
...options,
proxy,
pool: this.pool,
});
}
/**
* Check if the client is initialized and ready
*/
isReady(): boolean {
return this.initialized && !this.closed;
}
/**
* Close the client and release resources
*
* Note: This is optional - the client will auto-close on process exit.
*/
async close(): Promise<void> {
if (this.closed) {
return;
}
this.closed = true;
// Remove process event handlers to allow clean exit
this.removeCleanupHandlers();
// Shutdown pool first (closes browser instances)
if (this.pool) {
if (this.options.verbose) {
logger.info("Shutting down browser pool...");
}
try {
await this.pool.shutdown();
} catch (error: any) {
if (this.options.verbose) {
logger.warn(`Error shutting down pool: ${error.message}`);
}
}
this.pool = null;
}
// Then close HeroCore
if (this.heroCore) {
if (this.options.verbose) {
logger.info("Closing HeroCore...");
}
try {
await this.heroCore.close();
// Also call static shutdown to clean up any remaining resources
await HeroCore.shutdown();
} catch (error: any) {
// Ignore close errors
if (this.options.verbose) {
logger.warn(`Error closing HeroCore: ${error.message}`);
}
}
this.heroCore = null;
}
this.initialized = false;
if (this.options.verbose) {
logger.info("ReaderClient closed");
}
}
/**
* Register cleanup handlers for process exit
*/
private registerCleanup(): void {
this.cleanupHandler = async () => {
await this.close();
};
// Handle various exit signals
process.once("beforeExit", this.cleanupHandler);
process.once("SIGINT", async () => {
await this.cleanupHandler?.();
process.exit(0);
});
process.once("SIGTERM", async () => {
await this.cleanupHandler?.();
process.exit(0);
});
}
/**
* Remove process cleanup handlers
*/
private removeCleanupHandlers(): void {
if (this.cleanupHandler) {
process.removeListener("beforeExit", this.cleanupHandler);
this.cleanupHandler = null;
}
}
}