@crawlee/core 4.0.0-beta.105 → 4.0.0-beta.106

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 (75) hide show
  1. package/autoscaling/autoscaled_pool.d.ts +3 -21
  2. package/autoscaling/autoscaled_pool.js +85 -85
  3. package/autoscaling/client_load_signal.d.ts +1 -5
  4. package/autoscaling/client_load_signal.js +20 -20
  5. package/autoscaling/concurrency_system.d.ts +5 -20
  6. package/autoscaling/concurrency_system.js +81 -80
  7. package/autoscaling/cpu_load_signal.d.ts +1 -2
  8. package/autoscaling/cpu_load_signal.js +10 -10
  9. package/autoscaling/event_loop_load_signal.d.ts +1 -4
  10. package/autoscaling/event_loop_load_signal.js +18 -18
  11. package/autoscaling/load_signal.d.ts +1 -1
  12. package/autoscaling/load_signal.js +12 -11
  13. package/autoscaling/memory_load_signal.d.ts +3 -12
  14. package/autoscaling/memory_load_signal.js +40 -41
  15. package/autoscaling/snapshotter.d.ts +1 -4
  16. package/autoscaling/snapshotter.js +12 -12
  17. package/autoscaling/system_status.d.ts +1 -3
  18. package/autoscaling/system_status.js +11 -11
  19. package/configuration.d.ts +1 -1
  20. package/configuration.js +3 -3
  21. package/crawlers/context_pipeline.js +6 -6
  22. package/crawlers/statistics.d.ts +1 -8
  23. package/crawlers/statistics.js +45 -44
  24. package/events/event_manager.d.ts +1 -1
  25. package/events/event_manager.js +3 -3
  26. package/events/local_event_manager.d.ts +1 -1
  27. package/events/local_event_manager.js +3 -3
  28. package/log.js +5 -1
  29. package/memory-storage/memory-storage.d.ts +1 -5
  30. package/memory-storage/memory-storage.js +2 -2
  31. package/memory-storage/resource-clients/dataset.d.ts +1 -1
  32. package/memory-storage/resource-clients/dataset.js +6 -5
  33. package/memory-storage/resource-clients/key-value-store.d.ts +1 -1
  34. package/memory-storage/resource-clients/key-value-store.js +13 -12
  35. package/memory-storage/resource-clients/request-queue.d.ts +4 -23
  36. package/memory-storage/resource-clients/request-queue.js +59 -58
  37. package/owned_or_injected.d.ts +1 -3
  38. package/owned_or_injected.js +17 -17
  39. package/package.json +5 -5
  40. package/proxy_configuration.d.ts +1 -3
  41. package/proxy_configuration.js +8 -8
  42. package/recoverable_state.d.ts +1 -10
  43. package/recoverable_state.js +41 -41
  44. package/request.d.ts +1 -2
  45. package/request.js +10 -13
  46. package/router.d.ts +1 -4
  47. package/router.js +23 -23
  48. package/serialization.js +8 -9
  49. package/service_locator.d.ts +1 -10
  50. package/service_locator.js +48 -48
  51. package/session_pool/session.d.ts +1 -12
  52. package/session_pool/session.js +50 -50
  53. package/session_pool/session_pool.d.ts +2 -11
  54. package/session_pool/session_pool.js +59 -58
  55. package/storages/dataset.d.ts +1 -1
  56. package/storages/dataset.js +5 -5
  57. package/storages/key_value_store.d.ts +1 -4
  58. package/storages/key_value_store.js +21 -20
  59. package/storages/request_dedup_cache.d.ts +1 -2
  60. package/storages/request_dedup_cache.js +9 -9
  61. package/storages/request_list.d.ts +2 -22
  62. package/storages/request_list.js +74 -73
  63. package/storages/request_manager_tandem.d.ts +1 -10
  64. package/storages/request_manager_tandem.js +27 -27
  65. package/storages/request_queue.d.ts +2 -18
  66. package/storages/request_queue.js +37 -35
  67. package/storages/sitemap_request_loader.d.ts +1 -44
  68. package/storages/sitemap_request_loader.js +87 -87
  69. package/storages/storage_instance_manager.d.ts +1 -2
  70. package/storages/storage_instance_manager.js +17 -17
  71. package/storages/storage_stats.d.ts +1 -1
  72. package/storages/storage_stats.js +4 -4
  73. package/storages/transaction.d.ts +1 -3
  74. package/storages/transaction.js +17 -17
  75. package/system-info/runtime.js +7 -7
@@ -39,16 +39,16 @@ import { StorageInstanceManager } from './storages/storage_instance_manager.js';
39
39
  * ```
40
40
  */
41
41
  export class ServiceLocator {
42
- configuration;
43
- eventManager;
44
- storageBackend;
45
- logger;
42
+ #configuration;
43
+ #eventManager;
44
+ #storageBackend;
45
+ #logger;
46
46
  /**
47
47
  * Unified storage instance manager for Dataset, KeyValueStore, and RequestQueue.
48
48
  * Shared across all ServiceLocator instances (global singleton), matching crawlee-python.
49
49
  * Per-crawler isolation is achieved via `clientCacheKey`, not separate manager instances.
50
50
  */
51
- static storageInstanceManager;
51
+ static #storageInstanceManager;
52
52
  /**
53
53
  * Creates a new ServiceLocator instance.
54
54
  *
@@ -58,61 +58,61 @@ export class ServiceLocator {
58
58
  * @param logger Optional logger instance to use
59
59
  */
60
60
  constructor(configuration, eventManager, storageBackend, logger) {
61
- this.configuration = configuration;
62
- this.eventManager = eventManager;
63
- this.storageBackend = storageBackend;
64
- this.logger = logger;
61
+ this.#configuration = configuration;
62
+ this.#eventManager = eventManager;
63
+ this.#storageBackend = storageBackend;
64
+ this.#logger = logger;
65
65
  }
66
66
  getConfiguration() {
67
- if (!this.configuration) {
67
+ if (!this.#configuration) {
68
68
  this.getLogger().debug('No configuration set, implicitly creating and using default Configuration.');
69
- this.configuration = new Configuration();
69
+ this.#configuration = new Configuration();
70
70
  }
71
- return this.configuration;
71
+ return this.#configuration;
72
72
  }
73
73
  setConfiguration(configuration) {
74
74
  // Same instance, no need to do anything
75
- if (this.configuration === configuration) {
75
+ if (this.#configuration === configuration) {
76
76
  return;
77
77
  }
78
78
  // Already have a different configuration that was retrieved
79
- if (this.configuration) {
80
- throw new ServiceConflictError('Configuration', configuration, this.configuration);
79
+ if (this.#configuration) {
80
+ throw new ServiceConflictError('Configuration', configuration, this.#configuration);
81
81
  }
82
- this.configuration = configuration;
82
+ this.#configuration = configuration;
83
83
  }
84
84
  getEventManager() {
85
- if (!this.eventManager) {
85
+ if (!this.#eventManager) {
86
86
  this.getLogger().debug('No event manager set, implicitly creating and using default LocalEventManager.');
87
- if (!this.configuration) {
87
+ if (!this.#configuration) {
88
88
  this.getLogger().warning('Implicit creation of event manager will implicitly set configuration as side effect. ' +
89
89
  'It is advised to explicitly first set the configuration instead.');
90
90
  }
91
- this.eventManager = LocalEventManager.fromConfiguration(this.getConfiguration());
91
+ this.#eventManager = LocalEventManager.fromConfiguration(this.getConfiguration());
92
92
  }
93
- return this.eventManager;
93
+ return this.#eventManager;
94
94
  }
95
95
  setEventManager(eventManager) {
96
96
  // Same instance, no need to do anything
97
- if (this.eventManager === eventManager) {
97
+ if (this.#eventManager === eventManager) {
98
98
  return;
99
99
  }
100
100
  // Already have a different event manager that was retrieved
101
- if (this.eventManager) {
102
- throw new ServiceConflictError('EventManager', eventManager, this.eventManager);
101
+ if (this.#eventManager) {
102
+ throw new ServiceConflictError('EventManager', eventManager, this.#eventManager);
103
103
  }
104
- this.eventManager = eventManager;
104
+ this.#eventManager = eventManager;
105
105
  }
106
106
  getStorageBackend() {
107
- if (!this.storageBackend) {
107
+ if (!this.#storageBackend) {
108
108
  this.getLogger().debug('No storage backend set, implicitly creating and using the default storage backend ' +
109
109
  '(FileSystemStorageBackend when persistStorage is enabled, MemoryStorageBackend otherwise).');
110
- if (!this.configuration) {
110
+ if (!this.#configuration) {
111
111
  this.getLogger().warning('Implicit creation of storage backend will implicitly set configuration as side effect. ' +
112
112
  'It is advised to explicitly first set the configuration instead.');
113
113
  }
114
114
  const configuration = this.getConfiguration();
115
- this.storageBackend = configuration.persistStorage
115
+ this.#storageBackend = configuration.persistStorage
116
116
  ? new FileSystemStorageBackend({
117
117
  localDataDirectory: configuration.storageDir,
118
118
  logger: this.getLogger().child({ prefix: 'FileSystemStorageBackend' }),
@@ -121,50 +121,50 @@ export class ServiceLocator {
121
121
  logger: this.getLogger().child({ prefix: 'MemoryStorageBackend' }),
122
122
  });
123
123
  }
124
- return this.storageBackend;
124
+ return this.#storageBackend;
125
125
  }
126
126
  setStorageBackend(storageBackend) {
127
127
  // Same instance, no need to do anything
128
- if (this.storageBackend === storageBackend) {
128
+ if (this.#storageBackend === storageBackend) {
129
129
  return;
130
130
  }
131
131
  // Already have a different storage backend that was retrieved
132
- if (this.storageBackend) {
133
- throw new ServiceConflictError('StorageBackend', storageBackend, this.storageBackend);
132
+ if (this.#storageBackend) {
133
+ throw new ServiceConflictError('StorageBackend', storageBackend, this.#storageBackend);
134
134
  }
135
- this.storageBackend = storageBackend;
135
+ this.#storageBackend = storageBackend;
136
136
  }
137
137
  getLogger() {
138
- if (!this.logger) {
139
- this.logger = new ApifyLogAdapter(log);
138
+ if (!this.#logger) {
139
+ this.#logger = new ApifyLogAdapter(log);
140
140
  }
141
- return this.logger;
141
+ return this.#logger;
142
142
  }
143
143
  setLogger(logger) {
144
- if (this.logger === logger) {
144
+ if (this.#logger === logger) {
145
145
  return;
146
146
  }
147
- if (this.logger) {
148
- throw new ServiceConflictError('Logger', logger, this.logger);
147
+ if (this.#logger) {
148
+ throw new ServiceConflictError('Logger', logger, this.#logger);
149
149
  }
150
- this.logger = logger;
150
+ this.#logger = logger;
151
151
  }
152
152
  getChildLog(prefix) {
153
153
  return this.getLogger().child({ prefix });
154
154
  }
155
155
  getStorageInstanceManager() {
156
- if (!ServiceLocator.storageInstanceManager) {
157
- ServiceLocator.storageInstanceManager = new StorageInstanceManager();
156
+ if (!ServiceLocator.#storageInstanceManager) {
157
+ ServiceLocator.#storageInstanceManager = new StorageInstanceManager();
158
158
  }
159
- return ServiceLocator.storageInstanceManager;
159
+ return ServiceLocator.#storageInstanceManager;
160
160
  }
161
161
  reset() {
162
- this.configuration = undefined;
163
- this.eventManager = undefined;
164
- this.storageBackend = undefined;
165
- this.logger = undefined;
166
- ServiceLocator.storageInstanceManager?.clearCache();
167
- ServiceLocator.storageInstanceManager = undefined;
162
+ this.#configuration = undefined;
163
+ this.#eventManager = undefined;
164
+ this.#storageBackend = undefined;
165
+ this.#logger = undefined;
166
+ ServiceLocator.#storageInstanceManager?.clearCache();
167
+ ServiceLocator.#storageInstanceManager = undefined;
168
168
  }
169
169
  }
170
170
  /**
@@ -65,20 +65,9 @@ export interface SessionOptions {
65
65
  * @category Scaling
66
66
  */
67
67
  export declare class Session implements ISession {
68
+ #private;
68
69
  readonly id: string;
69
70
  readonly userData: Dictionary;
70
- private _maxErrorScore;
71
- private _errorScoreDecrement;
72
- private _createdAt;
73
- private _expiresAt;
74
- private _usageCount;
75
- private _maxUsageCount;
76
- private _errorScore;
77
- private _retired;
78
- private _proxyInfo?;
79
- private _cookieJar;
80
- private _fingerprint?;
81
- private log;
82
71
  get errorScore(): number;
83
72
  get usageCount(): number;
84
73
  get maxErrorScore(): number;
@@ -12,57 +12,57 @@ import { serviceLocator } from '../service_locator.js';
12
12
  export class Session {
13
13
  id;
14
14
  userData;
15
- _maxErrorScore;
16
- _errorScoreDecrement;
17
- _createdAt;
18
- _expiresAt;
19
- _usageCount;
20
- _maxUsageCount;
21
- _errorScore;
22
- _retired = false;
23
- _proxyInfo;
24
- _cookieJar;
25
- _fingerprint;
26
- log;
15
+ #maxErrorScore;
16
+ #errorScoreDecrement;
17
+ #createdAt;
18
+ #expiresAt;
19
+ #usageCount;
20
+ #maxUsageCount;
21
+ #errorScore;
22
+ #retired = false;
23
+ #proxyInfo;
24
+ #cookieJar;
25
+ #fingerprint;
26
+ #log;
27
27
  get errorScore() {
28
- return this._errorScore;
28
+ return this.#errorScore;
29
29
  }
30
30
  get usageCount() {
31
- return this._usageCount;
31
+ return this.#usageCount;
32
32
  }
33
33
  get maxErrorScore() {
34
- return this._maxErrorScore;
34
+ return this.#maxErrorScore;
35
35
  }
36
36
  get errorScoreDecrement() {
37
- return this._errorScoreDecrement;
37
+ return this.#errorScoreDecrement;
38
38
  }
39
39
  get expiresAt() {
40
- return this._expiresAt;
40
+ return this.#expiresAt;
41
41
  }
42
42
  get createdAt() {
43
- return this._createdAt;
43
+ return this.#createdAt;
44
44
  }
45
45
  get maxUsageCount() {
46
- return this._maxUsageCount;
46
+ return this.#maxUsageCount;
47
47
  }
48
48
  get cookieJar() {
49
- return this._cookieJar;
49
+ return this.#cookieJar;
50
50
  }
51
51
  get proxyInfo() {
52
- return this._proxyInfo;
52
+ return this.#proxyInfo;
53
53
  }
54
54
  get fingerprint() {
55
- return this._fingerprint;
55
+ return this.#fingerprint;
56
56
  }
57
57
  set fingerprint(fingerprint) {
58
- this._fingerprint = fingerprint;
58
+ this.#fingerprint = fingerprint;
59
59
  }
60
60
  /**
61
61
  * `true` once {@link Session.retire|`retire()`} has been called. Retirement is terminal:
62
62
  * a retired session is never picked by the pool and cannot be revived via `markGood()`.
63
63
  */
64
64
  get retired() {
65
- return this._retired;
65
+ return this.#retired;
66
66
  }
67
67
  /**
68
68
  * Session configuration.
@@ -87,21 +87,21 @@ export class Session {
87
87
  }));
88
88
  const { id = `session_${cryptoRandomObjectId(10)}`, cookieJar = new CookieJar(), proxyInfo = undefined, maxAgeSecs = 3000, userData = {}, maxErrorScore = 3, errorScoreDecrement = 0.5, createdAt = new Date(), usageCount = 0, errorScore = 0, maxUsageCount = 50, retired = false, log = serviceLocator.getLogger(), fingerprint, } = options;
89
89
  const { expiresAt = getDefaultCookieExpirationDate(maxAgeSecs) } = options;
90
- this.log = log.child({ prefix: 'Session' });
91
- this._cookieJar = cookieJar.setCookie ? cookieJar : CookieJar.fromJSON(JSON.stringify(cookieJar));
92
- this._proxyInfo = proxyInfo;
93
- this._fingerprint = fingerprint;
90
+ this.#log = log.child({ prefix: 'Session' });
91
+ this.#cookieJar = cookieJar.setCookie ? cookieJar : CookieJar.fromJSON(JSON.stringify(cookieJar));
92
+ this.#proxyInfo = proxyInfo;
93
+ this.#fingerprint = fingerprint;
94
94
  this.id = id;
95
95
  this.userData = userData;
96
- this._maxErrorScore = maxErrorScore;
97
- this._errorScoreDecrement = errorScoreDecrement;
96
+ this.#maxErrorScore = maxErrorScore;
97
+ this.#errorScoreDecrement = errorScoreDecrement;
98
98
  // Internal
99
- this._expiresAt = expiresAt;
100
- this._createdAt = createdAt;
101
- this._usageCount = usageCount; // indicates how many times the session has been used
102
- this._errorScore = errorScore; // indicates number of markBaded request with the session
103
- this._maxUsageCount = maxUsageCount;
104
- this._retired = retired;
99
+ this.#expiresAt = expiresAt;
100
+ this.#createdAt = createdAt;
101
+ this.#usageCount = usageCount; // indicates how many times the session has been used
102
+ this.#errorScore = errorScore; // indicates number of markBaded request with the session
103
+ this.#maxUsageCount = maxUsageCount;
104
+ this.#retired = retired;
105
105
  }
106
106
  /**
107
107
  * Indicates whether the session is blocked.
@@ -130,16 +130,16 @@ export class Session {
130
130
  * Session is usable when it is not retired, not expired, not blocked and the maximum usage count has not be reached.
131
131
  */
132
132
  isUsable() {
133
- return !this._retired && !this.isBlocked() && !this.isExpired() && !this.isMaxUsageCountReached();
133
+ return !this.#retired && !this.isBlocked() && !this.isExpired() && !this.isMaxUsageCountReached();
134
134
  }
135
135
  /**
136
136
  * This method should be called after a successful session usage.
137
137
  * It increases `usageCount` and potentially lowers the `errorScore` by the `errorScoreDecrement`.
138
138
  */
139
139
  markGood() {
140
- this._usageCount += 1;
141
- if (this._errorScore > 0) {
142
- this._errorScore -= this._errorScoreDecrement;
140
+ this.#usageCount += 1;
141
+ if (this.#errorScore > 0) {
142
+ this.#errorScore -= this.#errorScoreDecrement;
143
143
  }
144
144
  this.maybeSelfRetire();
145
145
  }
@@ -151,9 +151,9 @@ export class Session {
151
151
  return {
152
152
  id: this.id,
153
153
  cookieJar: this.cookieJar.toJSON(),
154
- proxyInfo: this._proxyInfo,
154
+ proxyInfo: this.#proxyInfo,
155
155
  userData: this.userData,
156
- fingerprint: this._fingerprint,
156
+ fingerprint: this.#fingerprint,
157
157
  maxErrorScore: this.maxErrorScore,
158
158
  errorScoreDecrement: this.errorScoreDecrement,
159
159
  expiresAt: this.expiresAt.toISOString(),
@@ -161,7 +161,7 @@ export class Session {
161
161
  usageCount: this.usageCount,
162
162
  maxUsageCount: this.maxUsageCount,
163
163
  errorScore: this.errorScore,
164
- retired: this._retired,
164
+ retired: this.#retired,
165
165
  };
166
166
  }
167
167
  /**
@@ -172,19 +172,19 @@ export class Session {
172
172
  * For transient external failures (such as `5XX` responses), use `markBad()` instead.
173
173
  */
174
174
  retire() {
175
- if (this._retired)
175
+ if (this.#retired)
176
176
  return;
177
- this._errorScore += this._maxErrorScore;
178
- this._usageCount += 1;
179
- this._retired = true;
177
+ this.#errorScore += this.#maxErrorScore;
178
+ this.#usageCount += 1;
179
+ this.#retired = true;
180
180
  }
181
181
  /**
182
182
  * Increases usage and error count.
183
183
  * Should be used when the session has been used unsuccessfully. For example because of timeouts.
184
184
  */
185
185
  markBad() {
186
- this._errorScore += 1;
187
- this._usageCount += 1;
186
+ this.#errorScore += 1;
187
+ this.#usageCount += 1;
188
188
  this.maybeSelfRetire();
189
189
  }
190
190
  /**
@@ -204,7 +204,7 @@ export class Session {
204
204
  this.cookieJar.setCookieSync(rawCookie, url);
205
205
  }
206
206
  catch (e) {
207
- this.log.warning('Could not set cookie.', { url, error: e.message });
207
+ this.#log.warning('Could not set cookie.', { url, error: e.message });
208
208
  }
209
209
  }
210
210
  /**
@@ -112,24 +112,15 @@ export interface SessionPoolOptions {
112
112
  * @category Scaling
113
113
  */
114
114
  export declare class SessionPool implements ISessionPool {
115
- private static nextId;
115
+ #private;
116
116
  readonly id: string;
117
- private log;
118
117
  private maxPoolSize;
119
118
  private createSessionFunction;
120
119
  private keyValueStore?;
121
- private sessions;
122
120
  private sessionMap;
123
121
  private sessionOptions;
124
122
  private persistStateKeyValueStoreId?;
125
123
  private persistStateKey;
126
- private listener?;
127
- private events;
128
- private persistenceOptions;
129
- private sessionReuseStrategy;
130
- private initPromise?;
131
- private queue;
132
- private roundRobinIndex;
133
124
  constructor(options?: SessionPoolOptions);
134
125
  /**
135
126
  * Gets count of usable sessions in the pool.
@@ -222,7 +213,7 @@ export declare class SessionPool implements ISessionPool {
222
213
  * through `maybeLoadSessionPool` naturally wins because it arrives in
223
214
  * `perCallOptions`.
224
215
  */
225
- private _invokeCreateSessionFunction;
216
+ private invokeCreateSessionFunction;
226
217
  /**
227
218
  * Creates new session and adds it to the pool.
228
219
  * @returns Newly created `Session` instance.