deepspider 0.2.11 → 0.2.12

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.
@@ -4,13 +4,15 @@
4
4
  */
5
5
 
6
6
  import { chromium } from 'patchright';
7
+ import { EventEmitter } from 'events';
7
8
  import { getDefaultHookScript } from './defaultHooks.js';
8
9
  import { NetworkInterceptor } from './interceptors/NetworkInterceptor.js';
9
10
  import { ScriptInterceptor } from './interceptors/ScriptInterceptor.js';
10
11
  import { getDataStore } from '../store/DataStore.js';
11
12
 
12
- export class BrowserClient {
13
+ export class BrowserClient extends EventEmitter {
13
14
  constructor() {
15
+ super();
14
16
  this.browser = null;
15
17
  this.context = null;
16
18
  this.page = null;
@@ -20,6 +22,7 @@ export class BrowserClient {
20
22
  this.scriptInterceptor = null;
21
23
  this.hookScript = null;
22
24
  this.onMessage = null;
25
+ this._isCleaningUp = false;
23
26
  }
24
27
 
25
28
  /**
@@ -51,6 +54,8 @@ export class BrowserClient {
51
54
  }
52
55
 
53
56
  this.browser = await chromium.launch(launchOptions);
57
+ this.emit('launched', { headless });
58
+
54
59
  this.context = await this.browser.newContext({
55
60
  ignoreHTTPSErrors: true,
56
61
  });
@@ -183,15 +188,47 @@ export class BrowserClient {
183
188
  * 关闭浏览器
184
189
  */
185
190
  async close() {
186
- if (this.cdpSession) {
187
- await this.cdpSession.detach().catch(() => {});
188
- this.cdpSession = null;
189
- }
190
- if (this.browser) {
191
- await this.browser.close();
192
- this.browser = null;
193
- this.context = null;
194
- this.page = null;
191
+ await this.cleanup();
192
+ }
193
+
194
+ /**
195
+ * 清理所有资源
196
+ */
197
+ async cleanup() {
198
+ if (this._isCleaningUp) return;
199
+ this._isCleaningUp = true;
200
+
201
+ try {
202
+ // 停止拦截器
203
+ if (this.networkInterceptor) {
204
+ await this.networkInterceptor.stop?.().catch(() => {});
205
+ this.networkInterceptor = null;
206
+ }
207
+ if (this.scriptInterceptor) {
208
+ await this.scriptInterceptor.stop?.().catch(() => {});
209
+ this.scriptInterceptor = null;
210
+ }
211
+
212
+ // 分离 CDP session
213
+ if (this.cdpSession) {
214
+ await this.cdpSession.detach().catch(() => {});
215
+ this.cdpSession = null;
216
+ }
217
+
218
+ // 关闭浏览器
219
+ if (this.browser) {
220
+ await this.browser.close();
221
+ this.browser = null;
222
+ this.context = null;
223
+ this.page = null;
224
+ this.pages = [];
225
+ }
226
+
227
+ this.emit('closed');
228
+ } catch (e) {
229
+ this.emit('error', e);
230
+ } finally {
231
+ this._isCleaningUp = false;
195
232
  }
196
233
  }
197
234
  }