@crawlee/basic 4.0.0-beta.99 → 4.0.0-rc.1

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.
Files changed (80) hide show
  1. package/README.md +1 -1
  2. package/index.d.ts +11 -1
  3. package/index.js +11 -0
  4. package/internals/autoscaling/autoscaled_pool.d.ts +178 -0
  5. package/internals/autoscaling/autoscaled_pool.js +387 -0
  6. package/internals/autoscaling/concurrency_system.d.ts +235 -0
  7. package/internals/autoscaling/concurrency_system.js +357 -0
  8. package/internals/autoscaling/cpu_load_signal.d.ts +43 -0
  9. package/internals/autoscaling/cpu_load_signal.js +46 -0
  10. package/internals/autoscaling/event_loop_load_signal.d.ts +51 -0
  11. package/internals/autoscaling/event_loop_load_signal.js +60 -0
  12. package/internals/autoscaling/index.d.ts +9 -0
  13. package/internals/autoscaling/index.js +9 -0
  14. package/internals/autoscaling/load_signal.d.ts +98 -0
  15. package/internals/autoscaling/load_signal.js +103 -0
  16. package/internals/autoscaling/memory_load_signal.d.ts +45 -0
  17. package/internals/autoscaling/memory_load_signal.js +104 -0
  18. package/internals/autoscaling/snapshotter.d.ts +84 -0
  19. package/internals/autoscaling/snapshotter.js +67 -0
  20. package/internals/autoscaling/storage_backend_load_signal.d.ts +56 -0
  21. package/internals/autoscaling/storage_backend_load_signal.js +73 -0
  22. package/internals/autoscaling/system_status.d.ts +112 -0
  23. package/internals/autoscaling/system_status.js +139 -0
  24. package/internals/autoscaling/weighted_avg.d.ts +5 -0
  25. package/internals/autoscaling/weighted_avg.js +14 -0
  26. package/internals/basic-crawler.d.ts +185 -389
  27. package/internals/basic-crawler.js +982 -684
  28. package/internals/cookie_utils.d.ts +40 -0
  29. package/internals/cookie_utils.js +122 -0
  30. package/internals/crawler-run.d.ts +76 -0
  31. package/internals/crawler-run.js +142 -0
  32. package/internals/crawlers/context_pipeline.d.ts +75 -0
  33. package/internals/crawlers/context_pipeline.js +109 -0
  34. package/internals/crawlers/crawler_commons.d.ts +216 -0
  35. package/internals/crawlers/crawler_commons.js +1 -0
  36. package/internals/crawlers/error_snapshotter.d.ts +29 -0
  37. package/internals/crawlers/error_snapshotter.js +116 -0
  38. package/internals/crawlers/error_tracker.d.ts +51 -0
  39. package/internals/crawlers/error_tracker.js +316 -0
  40. package/internals/crawlers/index.d.ts +4 -0
  41. package/internals/crawlers/index.js +3 -0
  42. package/internals/crawlers/internals/types.d.ts +7 -0
  43. package/internals/crawlers/internals/types.js +1 -0
  44. package/internals/crawlers/statistics.d.ts +316 -0
  45. package/internals/crawlers/statistics.js +525 -0
  46. package/internals/crawling_request.d.ts +86 -0
  47. package/internals/crawling_request.js +127 -0
  48. package/internals/enqueue_links/enqueue_links.d.ts +152 -0
  49. package/internals/enqueue_links/enqueue_links.js +76 -0
  50. package/internals/enqueue_links/index.d.ts +3 -0
  51. package/internals/enqueue_links/index.js +3 -0
  52. package/internals/enqueue_links/shared.d.ts +106 -0
  53. package/internals/enqueue_links/shared.js +258 -0
  54. package/internals/errors.d.ts +68 -0
  55. package/internals/errors.js +83 -0
  56. package/internals/http.d.ts +9 -0
  57. package/internals/http.js +28 -0
  58. package/internals/request-timeout.d.ts +6 -0
  59. package/internals/request-timeout.js +27 -2
  60. package/internals/router.d.ts +299 -0
  61. package/internals/router.js +312 -0
  62. package/internals/send-request.d.ts +2 -1
  63. package/internals/send-request.js +1 -0
  64. package/internals/session_pool/consts.d.ts +1 -0
  65. package/internals/session_pool/consts.js +1 -0
  66. package/internals/session_pool/errors.d.ts +7 -0
  67. package/internals/session_pool/errors.js +11 -0
  68. package/internals/session_pool/fingerprint.d.ts +9 -0
  69. package/internals/session_pool/fingerprint.js +30 -0
  70. package/internals/session_pool/index.d.ts +4 -0
  71. package/internals/session_pool/index.js +4 -0
  72. package/internals/session_pool/session.d.ts +151 -0
  73. package/internals/session_pool/session.js +225 -0
  74. package/internals/session_pool/session_pool.d.ts +193 -0
  75. package/internals/session_pool/session_pool.js +453 -0
  76. package/internals/sitemap_request_loader.d.ts +158 -0
  77. package/internals/sitemap_request_loader.js +432 -0
  78. package/internals/throttling_request_manager.d.ts +229 -0
  79. package/internals/throttling_request_manager.js +843 -0
  80. package/package.json +17 -14
@@ -0,0 +1,453 @@
1
+ import { KeyValueStore, RecoverableState, serviceLocator, validators } from '@crawlee/core';
2
+ import { parseArgument, schemas } from '@crawlee/utils/internal';
3
+ import { AsyncQueue } from '@sapphire/async-queue';
4
+ import { z } from 'zod';
5
+ import { createDefaultSessionFingerprint } from './fingerprint.js';
6
+ import { Session } from './session.js';
7
+ /** Default upper bound on the number of sessions the pool keeps around. */
8
+ const MAX_POOL_SIZE = 1000;
9
+ /** Prefix of the default key the pool persists its state under; the pool id is appended to it. */
10
+ const PERSIST_STATE_KEY = 'CRAWLEE_SESSION_POOL_STATE';
11
+ // Runtime mirror of `SessionReuseStrategy` for the zod schema below. Declared this way round - and not as
12
+ // `typeof SESSION_REUSE_STRATEGIES[number]` - so the public type is a plain literal union that does not drag
13
+ // the array into the API surface. `satisfies` keeps the array from naming a strategy the type does not have.
14
+ const SESSION_REUSE_STRATEGIES = [
15
+ 'random',
16
+ 'round-robin',
17
+ 'use-until-failure',
18
+ ];
19
+ // `schemas.anyObject` passes values through by reference (object schemas return a pruned plain
20
+ // copy), so class instances like loggers keep their prototype.
21
+ const sessionPoolOptionsSchema = z.strictObject({
22
+ id: z.union([schemas.anyNumber, z.string()]).optional(),
23
+ maxPoolSize: schemas.anyNumber.default(MAX_POOL_SIZE),
24
+ persistStateKeyValueStoreId: z.string().optional(),
25
+ persistStateKey: z.string().optional(),
26
+ createSessionFunction: schemas.anyFunction.optional(),
27
+ sessionOptions: schemas.anyObject.default(() => ({})),
28
+ log: validators.logger.default(() => serviceLocator.getLogger()),
29
+ persistenceOptions: schemas.anyObject.default(() => ({ enable: true })),
30
+ sessionReuseStrategy: z.enum(SESSION_REUSE_STRATEGIES).default('random'),
31
+ });
32
+ const createSessionOptionsSchema = z.strictObject({
33
+ sessionOptions: schemas.anyObject.default(() => ({})),
34
+ });
35
+ // Only what the recreation reads is pinned down - the rest of a session's state is the session's own business.
36
+ const persistedSessionState = z.custom((value) => typeof value === 'object' &&
37
+ value !== null &&
38
+ typeof value.createdAt === 'string' &&
39
+ typeof value.expiresAt === 'string', 'Expected a persisted session with `createdAt` and `expiresAt` timestamps');
40
+ const persistedSessionPoolState = z.object({
41
+ usableSessionsCount: z.number(),
42
+ retiredSessionsCount: z.number(),
43
+ sessions: z.array(persistedSessionState),
44
+ });
45
+ const sessionPoolInternalState = z.object({
46
+ sessions: z.array(z.instanceof(Session)),
47
+ });
48
+ /**
49
+ * The conversion between the pool's sessions and its persisted record. Decoding recreates the sessions through
50
+ * the pool's factory and keeps only the usable ones, which is why the codec is built per pool.
51
+ */
52
+ function buildSessionPoolStateCodec(pool) {
53
+ return z.codec(persistedSessionPoolState, sessionPoolInternalState, {
54
+ decode: async (record) => {
55
+ const sessions = [];
56
+ for (const sessionState of record.sessions) {
57
+ const session = await pool.recreateSession({
58
+ ...sessionState,
59
+ createdAt: new Date(sessionState.createdAt),
60
+ expiresAt: new Date(sessionState.expiresAt),
61
+ });
62
+ if (session.isUsable()) {
63
+ sessions.push(session);
64
+ }
65
+ }
66
+ pool.log.debug(`${sessions.length} active sessions loaded from KeyValueStore`);
67
+ return { sessions };
68
+ },
69
+ encode: ({ sessions }) => pool.toRecord(sessions),
70
+ });
71
+ }
72
+ /**
73
+ * Handles the rotation, creation and persistence of user-like sessions.
74
+ * Creates a pool of {@link Session} instances, that are randomly rotated.
75
+ * When some session is marked as blocked, it is removed and new one is created instead (the pool never returns an unusable session).
76
+ * Learn more in the {@doclink guides/session-management | Session management guide}.
77
+ *
78
+ * Session pool is already integrated into crawlers and is always active.
79
+ * All public methods are lazy-initialized — the pool initializes itself on first use.
80
+ *
81
+ * You can configure the pool with many options. See the {@link SessionPoolOptions}.
82
+ * Session pool is by default persisted in default {@link KeyValueStore}.
83
+ * If you want to have one pool for all runs you have to specify
84
+ * {@link SessionPoolOptions.persistStateKeyValueStoreId}.
85
+ *
86
+ * **Advanced usage:**
87
+ *
88
+ * ```javascript
89
+ * const sessionPool = new SessionPool({
90
+ * maxPoolSize: 25,
91
+ * sessionOptions:{
92
+ * maxAgeSecs: 10,
93
+ * maxUsageCount: 150, // for example when you know that the site blocks after 150 requests.
94
+ * },
95
+ * persistStateKeyValueStoreId: 'my-key-value-store-for-sessions',
96
+ * persistStateKey: 'my-session-pool',
97
+ * });
98
+ *
99
+ * // Get random session from the pool
100
+ * const session1 = await sessionPool.getSession();
101
+ * const session2 = await sessionPool.getSession();
102
+ * const session3 = await sessionPool.getSession();
103
+ *
104
+ * // Now you can mark the session either failed or successful
105
+ *
106
+ * // Marks session as bad after unsuccessful usage -> it increases error count (soft retire)
107
+ * session1.markBad()
108
+ *
109
+ * // Marks as successful.
110
+ * session2.markGood()
111
+ *
112
+ * // Retires session -> session is removed from the pool
113
+ * session3.retire()
114
+ *
115
+ * ```
116
+ *
117
+ * **Default session allocation flow:*
118
+ * 1. Until the `SessionPool` reaches `maxPoolSize`, new sessions are created, provided to the user and added to the pool
119
+ * 2. Blocked/retired sessions stay in the pool but are never provided to the user
120
+ * 3. Once the pool is full (live plus blocked session count reaches `maxPoolSize`), a random session from the pool is provided.
121
+ * 4. If a blocked session would be picked, instead all blocked sessions are evicted from the pool and a new session is created and provided
122
+ *
123
+ * @category Scaling
124
+ */
125
+ export class SessionPool {
126
+ static #nextId = 0;
127
+ id;
128
+ #log;
129
+ #maxPoolSize;
130
+ #createSessionFunction;
131
+ #sessionMap = new Map();
132
+ #sessionOptions;
133
+ #persistStateKeyValueStoreId;
134
+ #persistStateKey;
135
+ #persistenceOptions;
136
+ #sessionReuseStrategy;
137
+ /**
138
+ * The sessions live in a {@link RecoverableState} that handles the loading, recreation and periodic
139
+ * persistence of the record under `persistStateKey`.
140
+ */
141
+ #state;
142
+ #stateCodec;
143
+ #initPromise;
144
+ #queue = new AsyncQueue();
145
+ #roundRobinIndex = 0;
146
+ constructor(options = {}) {
147
+ const { id, maxPoolSize, persistStateKeyValueStoreId, persistStateKey, createSessionFunction, sessionOptions, log, persistenceOptions, sessionReuseStrategy, } = parseArgument(options, sessionPoolOptionsSchema);
148
+ this.id = id != null ? String(id) : String(SessionPool.#nextId++);
149
+ this.#sessionReuseStrategy = sessionReuseStrategy;
150
+ this.#log = log.child({ prefix: 'SessionPool' });
151
+ this.#persistenceOptions = persistenceOptions;
152
+ // Pool Configuration
153
+ this.#maxPoolSize = maxPoolSize;
154
+ this.#createSessionFunction = createSessionFunction || this.#defaultCreateSessionFunction;
155
+ // Session configuration. The pool-scoped logger is merged into per-call sessionOptions inside
156
+ // `invokeCreateSessionFunction`, so every Session inherits it without custom createSessionFunctions
157
+ // having to know about it.
158
+ this.#sessionOptions = {
159
+ ...sessionOptions,
160
+ log: this.#log,
161
+ };
162
+ // Session keyValueStore
163
+ this.#persistStateKeyValueStoreId = persistStateKeyValueStoreId;
164
+ this.#persistStateKey = persistStateKey ?? `${PERSIST_STATE_KEY}_${this.id}`;
165
+ this.#stateCodec = buildSessionPoolStateCodec({
166
+ recreateSession: (sessionState) => this.#invokeCreateSessionFunction(sessionState),
167
+ toRecord: (sessions) => this.#buildPersistedState(sessions),
168
+ log: this.#log,
169
+ });
170
+ this.#state = new RecoverableState({
171
+ defaultState: () => ({ sessions: [] }),
172
+ persistStateKey: this.#persistStateKey,
173
+ persistenceEnabled: !!this.#persistenceOptions.enable,
174
+ logger: this.#log,
175
+ keyValueStore: this.#persistenceOptions.enable && persistStateKeyValueStoreId
176
+ ? KeyValueStore.open({ id: persistStateKeyValueStoreId }, { configuration: serviceLocator.getConfiguration() })
177
+ : undefined,
178
+ // The codec validates in the decode direction, so it is a Standard Schema as-is; encoding needs a call.
179
+ deserialize: this.#stateCodec,
180
+ serialize: (state) => this.#stateCodec.encode(state),
181
+ });
182
+ this.#state.reset();
183
+ }
184
+ /**
185
+ * Gets count of usable sessions in the pool.
186
+ */
187
+ async usableSessionsCount() {
188
+ await this.#ensureInitialized();
189
+ return this.#state.currentValue.sessions.filter((session) => session.isUsable()).length;
190
+ }
191
+ /**
192
+ * Gets count of retired sessions in the pool.
193
+ */
194
+ async retiredSessionsCount() {
195
+ await this.#ensureInitialized();
196
+ return this.#state.currentValue.sessions.filter((session) => !session.isUsable()).length;
197
+ }
198
+ /**
199
+ * Starts periodic state persistence and potentially loads SessionPool state from {@link KeyValueStore}.
200
+ * Called automatically on first use of any public method.
201
+ */
202
+ async #ensureInitialized() {
203
+ if (!this.#initPromise) {
204
+ this.#initPromise = this.#setupPool();
205
+ }
206
+ return this.#initPromise;
207
+ }
208
+ async #setupPool() {
209
+ await this.#state.initialize();
210
+ for (const session of this.#state.currentValue.sessions) {
211
+ this.#sessionMap.set(session.id, session);
212
+ }
213
+ }
214
+ /**
215
+ * Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
216
+ * but this allows you to add more sessions once the max pool size is reached.
217
+ * This also allows you to add session with overridden session options (e.g. with specific session id).
218
+ *
219
+ * If the pool already contains a session with the same id, it is replaced when it is no longer usable
220
+ * (e.g. retired), and an error is thrown otherwise.
221
+ * @param [options] The configuration options for the session being added to the session pool.
222
+ */
223
+ async addSession(options = {}) {
224
+ await this.#ensureInitialized();
225
+ const { id } = options;
226
+ if (id) {
227
+ const existingSession = this.#sessionMap.get(id);
228
+ if (existingSession) {
229
+ if (existingSession.isUsable()) {
230
+ throw new Error(`Cannot add session with id '${id}' as it already exists in the pool`);
231
+ }
232
+ this.#removeSession(existingSession);
233
+ }
234
+ }
235
+ if (!this.#hasSpaceForSession()) {
236
+ this.#removeRetiredSessions();
237
+ }
238
+ const newSession = options instanceof Session ? options : await this.#invokeCreateSessionFunction(options);
239
+ this.#log.debug(`Adding new Session - ${newSession.id}`);
240
+ this.#registerSession(newSession);
241
+ }
242
+ /**
243
+ * Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
244
+ * but this allows you to add more sessions once the max pool size is reached.
245
+ * This also allows you to add session with overridden session options (e.g. with specific session id).
246
+ * @param [options] The configuration options for the session being added to the session pool.
247
+ */
248
+ async newSession(sessionOptions) {
249
+ await this.#ensureInitialized();
250
+ const newSession = await this.#invokeCreateSessionFunction(sessionOptions);
251
+ this.#registerSession(newSession);
252
+ return newSession;
253
+ }
254
+ /**
255
+ * Gets session.
256
+ * If there is space for new session, it creates and returns new session.
257
+ * If the session pool is full, it picks a session from the pool,
258
+ * If the picked session is usable it is returned, otherwise it creates and returns a new one.
259
+ * @param [sessionId] If provided, it returns the usable session with this id, `undefined` otherwise.
260
+ */
261
+ async getSession(sessionId) {
262
+ await this.#ensureInitialized();
263
+ await this.#queue.wait();
264
+ try {
265
+ if (sessionId) {
266
+ const session = this.#sessionMap.get(sessionId);
267
+ if (session?.isUsable())
268
+ return session;
269
+ return undefined;
270
+ }
271
+ const pickedSession = this.#pickSession();
272
+ if (pickedSession)
273
+ return pickedSession;
274
+ if (this.#hasSpaceForSession()) {
275
+ return await this.#createSession();
276
+ }
277
+ this.#removeRetiredSessions();
278
+ return await this.#createSession();
279
+ }
280
+ finally {
281
+ this.#queue.shift();
282
+ }
283
+ }
284
+ /**
285
+ * Discards all sessions in the pool, both usable and retired, resetting it to a blank state.
286
+ *
287
+ * The persisted record is left alone - use {@link SessionPool.resetStore} to clear that as well.
288
+ */
289
+ reset() {
290
+ this.#state.reset();
291
+ this.#sessionMap.clear();
292
+ this.#roundRobinIndex = 0;
293
+ }
294
+ /**
295
+ * Clears the persisted pool record, leaving the in-memory sessions alone.
296
+ *
297
+ * Throws while the state is still being persisted periodically - the next PERSIST_STATE event would write
298
+ * the record straight back. Call {@link SessionPool.teardown} first, or use {@link SessionPool.reset}
299
+ * to discard the sessions themselves. A no-op if persistence is disabled.
300
+ */
301
+ async resetStore() {
302
+ await this.#state.resetStore();
303
+ }
304
+ /**
305
+ * Returns an object representing the internal state of the `SessionPool` instance.
306
+ * @internal
307
+ */
308
+ async getState() {
309
+ await this.#ensureInitialized();
310
+ return this.#buildPersistedState(this.#state.currentValue.sessions);
311
+ }
312
+ /**
313
+ * Persists the current state of the `SessionPool` into the default {@link KeyValueStore}.
314
+ * The state is persisted automatically in regular intervals.
315
+ */
316
+ async persistState() {
317
+ if (!this.#persistenceOptions.enable) {
318
+ return;
319
+ }
320
+ await this.#ensureInitialized();
321
+ this.#log.debug('Persisting state', {
322
+ persistStateKeyValueStoreId: this.#persistStateKeyValueStoreId,
323
+ persistStateKey: this.#persistStateKey,
324
+ });
325
+ await this.#state
326
+ .persistState()
327
+ .catch((error) => this.#log.warning(`Failed to persist the session pool stats to ${this.#persistStateKey}`, { error }));
328
+ }
329
+ async [Symbol.asyncDispose]() {
330
+ await this.teardown();
331
+ }
332
+ /**
333
+ * Stops the periodic state persistence and persists the state one last time.
334
+ * This function should be called after you are done with using the `SessionPool` instance.
335
+ * Using the pool again afterwards initializes it anew.
336
+ */
337
+ async teardown() {
338
+ if (!this.#initPromise)
339
+ return;
340
+ await this.#ensureInitialized();
341
+ await this.#state.teardown();
342
+ // The next use initializes the pool again, restoring the record and resuming the periodic persistence -
343
+ // a crawler that is run twice gets a persisting pool both times.
344
+ this.#initPromise = undefined;
345
+ }
346
+ /**
347
+ * Removes retired `Session` instances from `SessionPool`.
348
+ */
349
+ #removeRetiredSessions() {
350
+ this.#state.currentValue.sessions = this.#state.currentValue.sessions.filter((storedSession) => {
351
+ if (storedSession.isUsable())
352
+ return true;
353
+ this.#sessionMap.delete(storedSession.id);
354
+ this.#log.debug(`Removed Session - ${storedSession.id}`);
355
+ return false;
356
+ });
357
+ }
358
+ /**
359
+ * Removes a `Session` instance from `SessionPool`.
360
+ */
361
+ #removeSession(session) {
362
+ this.#state.currentValue.sessions = this.#state.currentValue.sessions.filter((storedSession) => storedSession !== session);
363
+ this.#sessionMap.delete(session.id);
364
+ this.#log.debug(`Removed Session - ${session.id}`);
365
+ }
366
+ /**
367
+ * Adds `Session` instance to `SessionPool`.
368
+ * @param newSession `Session` instance to be added.
369
+ */
370
+ #registerSession(newSession) {
371
+ this.#state.currentValue.sessions.push(newSession);
372
+ this.#sessionMap.set(newSession.id, newSession);
373
+ }
374
+ /**
375
+ * Gets random index.
376
+ */
377
+ #getRandomIndex() {
378
+ return Math.floor(Math.random() * this.#state.currentValue.sessions.length);
379
+ }
380
+ /**
381
+ * Creates new session without any extra behavior.
382
+ * @param [options]
383
+ * @param [options.sessionOptions] The configuration options for the session being created.
384
+ * @returns New session.
385
+ */
386
+ async #defaultCreateSessionFunction(options = {}) {
387
+ const { sessionOptions } = parseArgument(options, createSessionOptionsSchema);
388
+ return new Session(sessionOptions);
389
+ }
390
+ /**
391
+ * Invokes `createSessionFunction` with `sessionOptions` already merged from pool-wide defaults and
392
+ * the supplied per-call overrides, so custom implementations don't need to spread `pool.sessionOptions` themselves.
393
+ *
394
+ * A default {@link SessionFingerprint} is generated up front (host OS as
395
+ * `platform`, a random valid `browser`/`device` for that platform). Pool-wide
396
+ * and per-call options override it, and a persisted fingerprint coming
397
+ * through `maybeLoadSessionPool` naturally wins because it arrives in
398
+ * `perCallOptions`.
399
+ */
400
+ async #invokeCreateSessionFunction(perCallOptions) {
401
+ const sessionOptions = {
402
+ fingerprint: createDefaultSessionFingerprint(),
403
+ ...this.#sessionOptions,
404
+ ...perCallOptions,
405
+ };
406
+ return this.#createSessionFunction({ sessionOptions });
407
+ }
408
+ /**
409
+ * Creates new session and adds it to the pool.
410
+ * @returns Newly created `Session` instance.
411
+ */
412
+ async #createSession() {
413
+ const newSession = await this.#invokeCreateSessionFunction();
414
+ this.#registerSession(newSession);
415
+ this.#log.debug(`Created new Session - ${newSession.id}`);
416
+ return newSession;
417
+ }
418
+ /**
419
+ * Decides whether there is enough space for creating new session.
420
+ */
421
+ #hasSpaceForSession() {
422
+ return this.#state.currentValue.sessions.length < this.#maxPoolSize;
423
+ }
424
+ /**
425
+ * Picks a session from the `SessionPool` according to the configured `sessionReuseStrategy`.
426
+ * Returns `undefined` when no session should be reused and a new one should be created instead.
427
+ */
428
+ #pickSession() {
429
+ if (this.#sessionReuseStrategy !== 'use-until-failure' && this.#hasSpaceForSession())
430
+ return undefined;
431
+ if (this.#sessionReuseStrategy === 'use-until-failure') {
432
+ return this.#state.currentValue.sessions.find((session) => session.isUsable());
433
+ }
434
+ let picked;
435
+ if (this.#sessionReuseStrategy === 'round-robin') {
436
+ const index = this.#roundRobinIndex % this.#state.currentValue.sessions.length;
437
+ this.#roundRobinIndex = index + 1;
438
+ picked = this.#state.currentValue.sessions[index];
439
+ }
440
+ else {
441
+ picked = this.#state.currentValue.sessions[this.#getRandomIndex()];
442
+ }
443
+ return picked.isUsable() ? picked : undefined;
444
+ }
445
+ /** The persisted form of the pool, which is also what {@link SessionPool.getState} returns. */
446
+ #buildPersistedState(sessions) {
447
+ return {
448
+ usableSessionsCount: sessions.filter((session) => session.isUsable()).length,
449
+ retiredSessionsCount: sessions.filter((session) => !session.isUsable()).length,
450
+ sessions: sessions.map((session) => session.getState()),
451
+ };
452
+ }
453
+ }
@@ -0,0 +1,158 @@
1
+ import type { BaseHttpClient } from '@crawlee/http-client';
2
+ import type { ParseSitemapOptions } from '@crawlee/utils';
3
+ import { EnqueueStrategy } from '@crawlee/utils';
4
+ import type { IRequestLoader, IRequestManager, RequestLoaderStatus } from '@crawlee/core';
5
+ import { Request } from '@crawlee/core';
6
+ import type { UrlPatternInput } from './enqueue_links/index.js';
7
+ interface UrlConstraints {
8
+ /**
9
+ * An array of URL patterns that URLs must match to be included.
10
+ *
11
+ * Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
12
+ *
13
+ * Glob matching is always case-insensitive.
14
+ * If you need case-sensitive matching, use a `RegExp`.
15
+ *
16
+ * If `include` is an empty array or `undefined`, then the `SitemapRequestLoader`
17
+ * includes all the URLs from the sitemap.
18
+ */
19
+ include?: readonly UrlPatternInput[];
20
+ /**
21
+ * An array of URL patterns. Matching URLs will **not** be included.
22
+ *
23
+ * Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
24
+ *
25
+ * Glob matching is always case-insensitive.
26
+ * If you need case-sensitive matching, use a `RegExp`.
27
+ */
28
+ exclude?: readonly UrlPatternInput[];
29
+ }
30
+ export interface SitemapRequestLoaderOptions extends UrlConstraints {
31
+ /**
32
+ * List of sitemap URLs to parse.
33
+ */
34
+ sitemapUrls: string[];
35
+ /**
36
+ * Proxy URL to be used for sitemap loading.
37
+ */
38
+ proxyUrl?: string;
39
+ /**
40
+ * Key for persisting the state of the request list in the `KeyValueStore`.
41
+ */
42
+ persistStateKey?: string;
43
+ /**
44
+ * Persistence-related options to control how and when crawler's data gets persisted.
45
+ */
46
+ persistenceOptions?: {
47
+ /**
48
+ * Use this flag to disable or enable periodic persistence to key value store.
49
+ * @default true
50
+ */
51
+ enable?: boolean;
52
+ };
53
+ /**
54
+ * Abort signal to be used for sitemap loading.
55
+ */
56
+ signal?: AbortSignal;
57
+ /**
58
+ * Timeout for sitemap loading in milliseconds. If both `signal` and `timeoutMillis` are provided, either of them can abort the loading.
59
+ */
60
+ timeoutMillis?: number;
61
+ /**
62
+ * Maximum number of buffered URLs for the sitemap loading stream.
63
+ * If the buffer is full, the stream will pause until the buffer is drained.
64
+ *
65
+ * @default 200
66
+ */
67
+ maxBufferSize?: number;
68
+ /**
69
+ * Keep only sitemap-derived URLs matching this strategy relative to the parent sitemap URL; non-`http(s)`
70
+ * schemes are always dropped. The filtering stays enforced after navigation (e.g. across redirects).
71
+ * Pass `'all'` to disable host filtering.
72
+ * @default EnqueueStrategy.SameHostname
73
+ */
74
+ enqueueStrategy?: EnqueueStrategy | `${EnqueueStrategy}`;
75
+ /**
76
+ * Advanced options for the underlying `parseSitemap` call.
77
+ */
78
+ parseSitemapOptions?: Omit<ParseSitemapOptions, 'emitNestedSitemaps' | 'maxDepth'>;
79
+ /**
80
+ * Custom HTTP client to be used for sitemap loading.
81
+ */
82
+ httpClient?: BaseHttpClient;
83
+ }
84
+ /**
85
+ * A list of URLs to crawl parsed from a sitemap.
86
+ *
87
+ * The loading of the sitemap is performed in the background so that crawling can start before the sitemap is fully loaded.
88
+ */
89
+ export declare class SitemapRequestLoader implements IRequestLoader {
90
+ #private;
91
+ /**
92
+ * Set of URLs that were returned by `fetchNextRequest()` and not marked as handled yet.
93
+ * @internal
94
+ */
95
+ inProgress: Set<string>;
96
+ /** @internal */
97
+ private constructor();
98
+ /**
99
+ * Indicates whether the background processing of sitemap contents has successfully finished.
100
+ *
101
+ * If this is `false`, the background processing is either still in progress or was aborted.
102
+ */
103
+ isSitemapFullyLoaded(): boolean;
104
+ /**
105
+ * Open a sitemap and start processing it.
106
+ *
107
+ * Resolves to a new instance of `SitemapRequestLoader`, which **might not be fully loaded yet** - i.e. the sitemap might still be loading in the background.
108
+ *
109
+ * Track the loading progress using the `isSitemapFullyLoaded` property.
110
+ */
111
+ static open(options: SitemapRequestLoaderOptions): Promise<SitemapRequestLoader>;
112
+ /**
113
+ * @inheritDoc
114
+ */
115
+ getTotalCount(): Promise<number>;
116
+ /**
117
+ * @inheritDoc
118
+ */
119
+ getPendingCount(): Promise<number>;
120
+ /**
121
+ * Combines this list with a request manager (a {@link RequestQueue} by default) into a
122
+ * {@link RequestManagerTandem}, allowing requests to be added and reclaimed while still
123
+ * being read from this list first.
124
+ */
125
+ toTandem(requestManager?: IRequestManager): Promise<IRequestManager>;
126
+ /**
127
+ * @inheritDoc
128
+ */
129
+ checkReadiness(): Promise<RequestLoaderStatus>;
130
+ /**
131
+ * @inheritDoc
132
+ */
133
+ getHandledCount(): Promise<number>;
134
+ /**
135
+ * @inheritDoc
136
+ */
137
+ persistState(): Promise<void>;
138
+ /**
139
+ * @inheritDoc
140
+ */
141
+ fetchNextRequest(): Promise<Request | null>;
142
+ /**
143
+ * @inheritDoc
144
+ */
145
+ // @ts-ignore optional peer dependency or compatibility with es2022
146
+ [Symbol.asyncIterator](): AsyncGenerator<Request<import("@crawlee/types").Dictionary>, void, unknown>;
147
+ /**
148
+ * Aborts the internal sitemap loading, stops the processing of the sitemap contents and drops all the pending URLs.
149
+ *
150
+ * Calling `fetchNextRequest()` after this method will always return `null`.
151
+ */
152
+ teardown(): Promise<void>;
153
+ /**
154
+ * @inheritDoc
155
+ */
156
+ markRequestAsHandled(request: Request): Promise<void>;
157
+ }
158
+ export {};