@crawlee/core 4.0.0-beta.80 → 4.0.0-beta.82
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/autoscaling/autoscaled_pool.d.ts +9 -9
- package/autoscaling/autoscaled_pool.js +25 -25
- package/autoscaling/snapshotter.d.ts +3 -28
- package/autoscaling/snapshotter.js +0 -36
- package/autoscaling/system_status.d.ts +1 -1
- package/autoscaling/system_status.js +3 -3
- package/crawlers/statistics.d.ts +6 -6
- package/crawlers/statistics.js +8 -8
- package/package.json +5 -5
- package/proxy_configuration.d.ts +26 -14
- package/proxy_configuration.js +8 -11
- package/router.d.ts +1 -1
- package/session_pool/session.d.ts +2 -2
- package/session_pool/session.js +3 -3
- package/session_pool/session_pool.d.ts +23 -27
- package/session_pool/session_pool.js +29 -29
- package/storages/request_list.d.ts +16 -16
- package/storages/request_list.js +37 -37
- package/storages/request_queue.d.ts +20 -34
- package/storages/request_queue.js +13 -15
- package/storages/utils.d.ts +2 -2
- package/validators.js +2 -2
package/session_pool/session.js
CHANGED
|
@@ -141,7 +141,7 @@ export class Session {
|
|
|
141
141
|
if (this._errorScore > 0) {
|
|
142
142
|
this._errorScore -= this._errorScoreDecrement;
|
|
143
143
|
}
|
|
144
|
-
this.
|
|
144
|
+
this.maybeSelfRetire();
|
|
145
145
|
}
|
|
146
146
|
/**
|
|
147
147
|
* Gets session state for persistence in KeyValueStore.
|
|
@@ -185,7 +185,7 @@ export class Session {
|
|
|
185
185
|
markBad() {
|
|
186
186
|
this._errorScore += 1;
|
|
187
187
|
this._usageCount += 1;
|
|
188
|
-
this.
|
|
188
|
+
this.maybeSelfRetire();
|
|
189
189
|
}
|
|
190
190
|
/**
|
|
191
191
|
* Returns cookies saved with the session in the typical
|
|
@@ -210,7 +210,7 @@ export class Session {
|
|
|
210
210
|
/**
|
|
211
211
|
* Checks if session is not usable. if it is not retires the session.
|
|
212
212
|
*/
|
|
213
|
-
|
|
213
|
+
maybeSelfRetire() {
|
|
214
214
|
if (!this.isUsable()) {
|
|
215
215
|
this.retire();
|
|
216
216
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { ISessionPool } from '@crawlee/types';
|
|
2
2
|
import type { PersistenceOptions } from '../crawlers/statistics.js';
|
|
3
|
-
import type { EventManager } from '../events/event_manager.js';
|
|
4
3
|
import type { CrawleeLogger } from '../log.js';
|
|
5
|
-
import { KeyValueStore } from '../storages/key_value_store.js';
|
|
6
4
|
import type { SessionOptions } from './session.js';
|
|
7
5
|
import { Session } from './session.js';
|
|
8
6
|
declare const SESSION_REUSE_STRATEGIES: readonly ["random", "round-robin", "use-until-failure"];
|
|
@@ -116,19 +114,19 @@ export interface SessionPoolOptions {
|
|
|
116
114
|
export declare class SessionPool implements ISessionPool {
|
|
117
115
|
private static nextId;
|
|
118
116
|
readonly id: string;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
117
|
+
private log;
|
|
118
|
+
private maxPoolSize;
|
|
119
|
+
private createSessionFunction;
|
|
120
|
+
private keyValueStore?;
|
|
121
|
+
private sessions;
|
|
122
|
+
private sessionMap;
|
|
123
|
+
private sessionOptions;
|
|
124
|
+
private persistStateKeyValueStoreId?;
|
|
125
|
+
private persistStateKey;
|
|
126
|
+
private listener?;
|
|
127
|
+
private events;
|
|
128
|
+
private persistenceOptions;
|
|
129
|
+
private sessionReuseStrategy;
|
|
132
130
|
private initPromise?;
|
|
133
131
|
private queue;
|
|
134
132
|
private roundRobinIndex;
|
|
@@ -145,7 +143,7 @@ export declare class SessionPool implements ISessionPool {
|
|
|
145
143
|
* Starts periodic state persistence and potentially loads SessionPool state from {@link KeyValueStore}.
|
|
146
144
|
* Called automatically on first use of any public method.
|
|
147
145
|
*/
|
|
148
|
-
|
|
146
|
+
private ensureInitialized;
|
|
149
147
|
private setupPool;
|
|
150
148
|
/**
|
|
151
149
|
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
@@ -197,25 +195,23 @@ export declare class SessionPool implements ISessionPool {
|
|
|
197
195
|
/**
|
|
198
196
|
* Removes retired `Session` instances from `SessionPool`.
|
|
199
197
|
*/
|
|
200
|
-
|
|
198
|
+
private removeRetiredSessions;
|
|
201
199
|
/**
|
|
202
200
|
* Adds `Session` instance to `SessionPool`.
|
|
203
201
|
* @param newSession `Session` instance to be added.
|
|
204
202
|
*/
|
|
205
|
-
|
|
203
|
+
private registerSession;
|
|
206
204
|
/**
|
|
207
205
|
* Gets random index.
|
|
208
206
|
*/
|
|
209
|
-
|
|
207
|
+
private getRandomIndex;
|
|
210
208
|
/**
|
|
211
209
|
* Creates new session without any extra behavior.
|
|
212
210
|
* @param [options]
|
|
213
211
|
* @param [options.sessionOptions] The configuration options for the session being created.
|
|
214
212
|
* @returns New session.
|
|
215
213
|
*/
|
|
216
|
-
|
|
217
|
-
sessionOptions?: SessionOptions;
|
|
218
|
-
}): Promise<Session>;
|
|
214
|
+
private defaultCreateSessionFunction;
|
|
219
215
|
/**
|
|
220
216
|
* Invokes `createSessionFunction` with `sessionOptions` already merged from pool-wide defaults and
|
|
221
217
|
* the supplied per-call overrides, so custom implementations don't need to spread `pool.sessionOptions` themselves.
|
|
@@ -223,7 +219,7 @@ export declare class SessionPool implements ISessionPool {
|
|
|
223
219
|
* A default {@link SessionFingerprint} is generated up front (host OS as
|
|
224
220
|
* `platform`, a random valid `browser`/`device` for that platform). Pool-wide
|
|
225
221
|
* and per-call options override it, and a persisted fingerprint coming
|
|
226
|
-
* through `
|
|
222
|
+
* through `maybeLoadSessionPool` naturally wins because it arrives in
|
|
227
223
|
* `perCallOptions`.
|
|
228
224
|
*/
|
|
229
225
|
private _invokeCreateSessionFunction;
|
|
@@ -231,20 +227,20 @@ export declare class SessionPool implements ISessionPool {
|
|
|
231
227
|
* Creates new session and adds it to the pool.
|
|
232
228
|
* @returns Newly created `Session` instance.
|
|
233
229
|
*/
|
|
234
|
-
|
|
230
|
+
private createSession;
|
|
235
231
|
/**
|
|
236
232
|
* Decides whether there is enough space for creating new session.
|
|
237
233
|
*/
|
|
238
|
-
|
|
234
|
+
private hasSpaceForSession;
|
|
239
235
|
/**
|
|
240
236
|
* Picks a session from the `SessionPool` according to the configured `sessionReuseStrategy`.
|
|
241
237
|
* Returns `undefined` when no session should be reused and a new one should be created instead.
|
|
242
238
|
*/
|
|
243
|
-
|
|
239
|
+
private pickSession;
|
|
244
240
|
/**
|
|
245
241
|
* Potentially loads `SessionPool`.
|
|
246
242
|
* If the state was persisted it loads the `SessionPool` from the persisted state.
|
|
247
243
|
*/
|
|
248
|
-
|
|
244
|
+
private maybeLoadSessionPool;
|
|
249
245
|
}
|
|
250
246
|
export {};
|
|
@@ -71,7 +71,7 @@ export class SessionPool {
|
|
|
71
71
|
sessionOptions;
|
|
72
72
|
persistStateKeyValueStoreId;
|
|
73
73
|
persistStateKey;
|
|
74
|
-
|
|
74
|
+
listener;
|
|
75
75
|
events;
|
|
76
76
|
persistenceOptions;
|
|
77
77
|
sessionReuseStrategy;
|
|
@@ -100,7 +100,7 @@ export class SessionPool {
|
|
|
100
100
|
this.persistenceOptions = persistenceOptions;
|
|
101
101
|
// Pool Configuration
|
|
102
102
|
this.maxPoolSize = maxPoolSize;
|
|
103
|
-
this.createSessionFunction = createSessionFunction || this.
|
|
103
|
+
this.createSessionFunction = createSessionFunction || this.defaultCreateSessionFunction;
|
|
104
104
|
// Session configuration. The pool-scoped logger is merged into per-call sessionOptions inside
|
|
105
105
|
// `_invokeCreateSessionFunction`, so every Session inherits it without custom createSessionFunctions
|
|
106
106
|
// having to know about it.
|
|
@@ -147,9 +147,9 @@ export class SessionPool {
|
|
|
147
147
|
this.log.debug(`No 'persistStateKeyValueStoreId' options specified, this session pool's data has been saved in the KeyValueStore with the id: ${this.keyValueStore.id}`);
|
|
148
148
|
}
|
|
149
149
|
// in case of migration happened and SessionPool state should be restored from the keyValueStore.
|
|
150
|
-
await this.
|
|
151
|
-
this.
|
|
152
|
-
this.events.on("persistState" /* EventType.PERSIST_STATE */, this.
|
|
150
|
+
await this.maybeLoadSessionPool();
|
|
151
|
+
this.listener = this.persistState.bind(this);
|
|
152
|
+
this.events.on("persistState" /* EventType.PERSIST_STATE */, this.listener);
|
|
153
153
|
}
|
|
154
154
|
/**
|
|
155
155
|
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
@@ -166,12 +166,12 @@ export class SessionPool {
|
|
|
166
166
|
throw new Error(`Cannot add session with id '${id}' as it already exists in the pool`);
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
|
-
if (!this.
|
|
170
|
-
this.
|
|
169
|
+
if (!this.hasSpaceForSession()) {
|
|
170
|
+
this.removeRetiredSessions();
|
|
171
171
|
}
|
|
172
172
|
const newSession = options instanceof Session ? options : await this._invokeCreateSessionFunction(options);
|
|
173
173
|
this.log.debug(`Adding new Session - ${newSession.id}`);
|
|
174
|
-
this.
|
|
174
|
+
this.registerSession(newSession);
|
|
175
175
|
}
|
|
176
176
|
/**
|
|
177
177
|
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
@@ -182,7 +182,7 @@ export class SessionPool {
|
|
|
182
182
|
async newSession(sessionOptions) {
|
|
183
183
|
await this.ensureInitialized();
|
|
184
184
|
const newSession = await this._invokeCreateSessionFunction(sessionOptions);
|
|
185
|
-
this.
|
|
185
|
+
this.registerSession(newSession);
|
|
186
186
|
return newSession;
|
|
187
187
|
}
|
|
188
188
|
/**
|
|
@@ -202,14 +202,14 @@ export class SessionPool {
|
|
|
202
202
|
return session;
|
|
203
203
|
return undefined;
|
|
204
204
|
}
|
|
205
|
-
const pickedSession = this.
|
|
205
|
+
const pickedSession = this.pickSession();
|
|
206
206
|
if (pickedSession)
|
|
207
207
|
return pickedSession;
|
|
208
|
-
if (this.
|
|
209
|
-
return await this.
|
|
208
|
+
if (this.hasSpaceForSession()) {
|
|
209
|
+
return await this.createSession();
|
|
210
210
|
}
|
|
211
|
-
this.
|
|
212
|
-
return await this.
|
|
211
|
+
this.removeRetiredSessions();
|
|
212
|
+
return await this.createSession();
|
|
213
213
|
}
|
|
214
214
|
finally {
|
|
215
215
|
this.queue.shift();
|
|
@@ -263,15 +263,15 @@ export class SessionPool {
|
|
|
263
263
|
if (!this.initPromise)
|
|
264
264
|
return;
|
|
265
265
|
await this.ensureInitialized();
|
|
266
|
-
if (this.
|
|
267
|
-
this.events.off("persistState" /* EventType.PERSIST_STATE */, this.
|
|
266
|
+
if (this.listener) {
|
|
267
|
+
this.events.off("persistState" /* EventType.PERSIST_STATE */, this.listener);
|
|
268
268
|
}
|
|
269
269
|
await this.persistState();
|
|
270
270
|
}
|
|
271
271
|
/**
|
|
272
272
|
* Removes retired `Session` instances from `SessionPool`.
|
|
273
273
|
*/
|
|
274
|
-
|
|
274
|
+
removeRetiredSessions() {
|
|
275
275
|
this.sessions = this.sessions.filter((storedSession) => {
|
|
276
276
|
if (storedSession.isUsable())
|
|
277
277
|
return true;
|
|
@@ -284,14 +284,14 @@ export class SessionPool {
|
|
|
284
284
|
* Adds `Session` instance to `SessionPool`.
|
|
285
285
|
* @param newSession `Session` instance to be added.
|
|
286
286
|
*/
|
|
287
|
-
|
|
287
|
+
registerSession(newSession) {
|
|
288
288
|
this.sessions.push(newSession);
|
|
289
289
|
this.sessionMap.set(newSession.id, newSession);
|
|
290
290
|
}
|
|
291
291
|
/**
|
|
292
292
|
* Gets random index.
|
|
293
293
|
*/
|
|
294
|
-
|
|
294
|
+
getRandomIndex() {
|
|
295
295
|
return Math.floor(Math.random() * this.sessions.length);
|
|
296
296
|
}
|
|
297
297
|
/**
|
|
@@ -300,7 +300,7 @@ export class SessionPool {
|
|
|
300
300
|
* @param [options.sessionOptions] The configuration options for the session being created.
|
|
301
301
|
* @returns New session.
|
|
302
302
|
*/
|
|
303
|
-
async
|
|
303
|
+
async defaultCreateSessionFunction(options = {}) {
|
|
304
304
|
ow(options, ow.object.exactShape({ sessionOptions: ow.optional.object }));
|
|
305
305
|
const { sessionOptions = {} } = options;
|
|
306
306
|
return new Session(sessionOptions);
|
|
@@ -312,7 +312,7 @@ export class SessionPool {
|
|
|
312
312
|
* A default {@link SessionFingerprint} is generated up front (host OS as
|
|
313
313
|
* `platform`, a random valid `browser`/`device` for that platform). Pool-wide
|
|
314
314
|
* and per-call options override it, and a persisted fingerprint coming
|
|
315
|
-
* through `
|
|
315
|
+
* through `maybeLoadSessionPool` naturally wins because it arrives in
|
|
316
316
|
* `perCallOptions`.
|
|
317
317
|
*/
|
|
318
318
|
async _invokeCreateSessionFunction(perCallOptions) {
|
|
@@ -327,24 +327,24 @@ export class SessionPool {
|
|
|
327
327
|
* Creates new session and adds it to the pool.
|
|
328
328
|
* @returns Newly created `Session` instance.
|
|
329
329
|
*/
|
|
330
|
-
async
|
|
330
|
+
async createSession() {
|
|
331
331
|
const newSession = await this._invokeCreateSessionFunction();
|
|
332
|
-
this.
|
|
332
|
+
this.registerSession(newSession);
|
|
333
333
|
this.log.debug(`Created new Session - ${newSession.id}`);
|
|
334
334
|
return newSession;
|
|
335
335
|
}
|
|
336
336
|
/**
|
|
337
337
|
* Decides whether there is enough space for creating new session.
|
|
338
338
|
*/
|
|
339
|
-
|
|
339
|
+
hasSpaceForSession() {
|
|
340
340
|
return this.sessions.length < this.maxPoolSize;
|
|
341
341
|
}
|
|
342
342
|
/**
|
|
343
343
|
* Picks a session from the `SessionPool` according to the configured `sessionReuseStrategy`.
|
|
344
344
|
* Returns `undefined` when no session should be reused and a new one should be created instead.
|
|
345
345
|
*/
|
|
346
|
-
|
|
347
|
-
if (this.sessionReuseStrategy !== 'use-until-failure' && this.
|
|
346
|
+
pickSession() {
|
|
347
|
+
if (this.sessionReuseStrategy !== 'use-until-failure' && this.hasSpaceForSession())
|
|
348
348
|
return undefined;
|
|
349
349
|
if (this.sessionReuseStrategy === 'use-until-failure') {
|
|
350
350
|
return this.sessions.find((session) => session.isUsable());
|
|
@@ -356,7 +356,7 @@ export class SessionPool {
|
|
|
356
356
|
picked = this.sessions[index];
|
|
357
357
|
}
|
|
358
358
|
else {
|
|
359
|
-
picked = this.sessions[this.
|
|
359
|
+
picked = this.sessions[this.getRandomIndex()];
|
|
360
360
|
}
|
|
361
361
|
return picked.isUsable() ? picked : undefined;
|
|
362
362
|
}
|
|
@@ -364,7 +364,7 @@ export class SessionPool {
|
|
|
364
364
|
* Potentially loads `SessionPool`.
|
|
365
365
|
* If the state was persisted it loads the `SessionPool` from the persisted state.
|
|
366
366
|
*/
|
|
367
|
-
async
|
|
367
|
+
async maybeLoadSessionPool() {
|
|
368
368
|
const loadedSessionPool = await this.keyValueStore?.getValue(this.persistStateKey);
|
|
369
369
|
if (!loadedSessionPool)
|
|
370
370
|
return;
|
|
@@ -378,7 +378,7 @@ export class SessionPool {
|
|
|
378
378
|
sessionObject.expiresAt = new Date(sessionObject.expiresAt);
|
|
379
379
|
const recreatedSession = await this._invokeCreateSessionFunction(sessionObject);
|
|
380
380
|
if (recreatedSession.isUsable()) {
|
|
381
|
-
this.
|
|
381
|
+
this.registerSession(recreatedSession);
|
|
382
382
|
}
|
|
383
383
|
}
|
|
384
384
|
this.log.debug(`${this.sessions.length} active sessions loaded from KeyValueStore`);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { BaseHttpClient, Dictionary } from '@crawlee/types';
|
|
2
2
|
import type { Configuration } from '../configuration.js';
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
3
|
+
import type { IProxyConfiguration } from '../proxy_configuration.js';
|
|
4
|
+
import { Request, type RequestOptions, type Source } from '../request.js';
|
|
5
5
|
import type { IRequestLoader } from './request_loader.js';
|
|
6
6
|
import type { IRequestManager } from './request_manager.js';
|
|
7
7
|
/** @internal */
|
|
@@ -87,7 +87,7 @@ export interface RequestListOptions {
|
|
|
87
87
|
* Takes advantage of the internal address rotation and authentication process.
|
|
88
88
|
* If undefined, the `requestsFromUrl` requests will be made without proxy.
|
|
89
89
|
*/
|
|
90
|
-
proxyConfiguration?:
|
|
90
|
+
proxyConfiguration?: IProxyConfiguration;
|
|
91
91
|
/**
|
|
92
92
|
* Identifies the key in the default key-value store under which `RequestList` periodically stores its
|
|
93
93
|
* state (i.e. which URLs were crawled and which not).
|
|
@@ -222,7 +222,7 @@ export declare class RequestList implements IRequestLoader {
|
|
|
222
222
|
* All requests in the array have distinct uniqueKey!
|
|
223
223
|
* @internal
|
|
224
224
|
*/
|
|
225
|
-
requests: (Request | RequestOptions)[];
|
|
225
|
+
readonly requests: (Request | RequestOptions)[];
|
|
226
226
|
/** Index to the next item in requests array to fetch. All previous requests are either handled or in progress. */
|
|
227
227
|
private nextIndex;
|
|
228
228
|
/** Dictionary, key is Request.uniqueKey, value is corresponding index in the requests array. */
|
|
@@ -275,14 +275,14 @@ export declare class RequestList implements IRequestLoader {
|
|
|
275
275
|
* This needs to be done in a memory efficient way. We should update the input
|
|
276
276
|
* to a Stream once apify-client supports streams.
|
|
277
277
|
*/
|
|
278
|
-
|
|
278
|
+
private addPersistedRequests;
|
|
279
279
|
/**
|
|
280
280
|
* Add Requests from both options.sources and options.sourcesFunction.
|
|
281
281
|
* This function is called only when persisted sources were not loaded.
|
|
282
282
|
* We need to avoid keeping both sources and requests in memory
|
|
283
283
|
* to reduce memory footprint with very large sources.
|
|
284
284
|
*/
|
|
285
|
-
|
|
285
|
+
private addRequestsFromSources;
|
|
286
286
|
/**
|
|
287
287
|
* @inheritDoc
|
|
288
288
|
*/
|
|
@@ -298,16 +298,16 @@ export declare class RequestList implements IRequestLoader {
|
|
|
298
298
|
* are automatically persisted at RequestList initialization (if the persistRequestsKey is set),
|
|
299
299
|
* but there's no reason to persist it again afterwards, because RequestList is immutable.
|
|
300
300
|
*/
|
|
301
|
-
|
|
301
|
+
private persistRequests;
|
|
302
302
|
/**
|
|
303
303
|
* Restores RequestList state from a state object.
|
|
304
304
|
*/
|
|
305
|
-
|
|
305
|
+
private restoreState;
|
|
306
306
|
/**
|
|
307
307
|
* Attempts to load state and requests using the `RequestList` configuration
|
|
308
308
|
* and returns a tuple of [state, requests] where each may be null if not loaded.
|
|
309
309
|
*/
|
|
310
|
-
|
|
310
|
+
private loadStateAndPersistedRequests;
|
|
311
311
|
/**
|
|
312
312
|
* Returns an object representing the internal state of the `RequestList` instance.
|
|
313
313
|
* Note that the object's fields can change in future releases.
|
|
@@ -337,31 +337,31 @@ export declare class RequestList implements IRequestLoader {
|
|
|
337
337
|
/**
|
|
338
338
|
* Adds all fetched requests from a URL from a remote resource.
|
|
339
339
|
*/
|
|
340
|
-
|
|
341
|
-
|
|
340
|
+
private addFetchedRequests;
|
|
341
|
+
private getPersistedState;
|
|
342
342
|
/**
|
|
343
343
|
* Fetches URLs from requestsFromUrl and returns them in format of list of requests
|
|
344
344
|
*/
|
|
345
|
-
|
|
345
|
+
private fetchRequestsFromUrl;
|
|
346
346
|
/**
|
|
347
347
|
* Adds given request.
|
|
348
348
|
* If the `source` parameter is a string or plain object and not an instance
|
|
349
349
|
* of a `Request`, then the function creates a `Request` instance.
|
|
350
350
|
*/
|
|
351
|
-
|
|
351
|
+
private addRequest;
|
|
352
352
|
/**
|
|
353
353
|
* Helper function that validates unique key.
|
|
354
354
|
* Throws an error if uniqueKey is not a non-empty string.
|
|
355
355
|
*/
|
|
356
|
-
|
|
356
|
+
private ensureUniqueKeyValid;
|
|
357
357
|
/**
|
|
358
358
|
* Checks that a request is currently being processed and throws an error if not.
|
|
359
359
|
*/
|
|
360
|
-
|
|
360
|
+
private ensureInProgress;
|
|
361
361
|
/**
|
|
362
362
|
* Throws an error if request list wasn't initialized.
|
|
363
363
|
*/
|
|
364
|
-
|
|
364
|
+
private ensureIsInitialized;
|
|
365
365
|
/**
|
|
366
366
|
* Returns the total number of unique requests present in the `RequestList`.
|
|
367
367
|
*/
|