@crawlee/core 4.0.0-beta.104 → 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.
- package/autoscaling/autoscaled_pool.d.ts +3 -21
- package/autoscaling/autoscaled_pool.js +85 -85
- package/autoscaling/client_load_signal.d.ts +1 -5
- package/autoscaling/client_load_signal.js +20 -20
- package/autoscaling/concurrency_system.d.ts +5 -20
- package/autoscaling/concurrency_system.js +81 -80
- package/autoscaling/cpu_load_signal.d.ts +1 -2
- package/autoscaling/cpu_load_signal.js +10 -10
- package/autoscaling/event_loop_load_signal.d.ts +1 -4
- package/autoscaling/event_loop_load_signal.js +18 -18
- package/autoscaling/load_signal.d.ts +1 -1
- package/autoscaling/load_signal.js +12 -11
- package/autoscaling/memory_load_signal.d.ts +3 -12
- package/autoscaling/memory_load_signal.js +40 -41
- package/autoscaling/snapshotter.d.ts +1 -4
- package/autoscaling/snapshotter.js +12 -12
- package/autoscaling/system_status.d.ts +1 -3
- package/autoscaling/system_status.js +11 -11
- package/configuration.d.ts +1 -1
- package/configuration.js +3 -3
- package/crawlers/context_pipeline.js +6 -6
- package/crawlers/crawler_commons.d.ts +6 -56
- package/crawlers/crawler_commons.js +1 -107
- package/crawlers/index.d.ts +1 -1
- package/crawlers/index.js +0 -1
- package/crawlers/statistics.d.ts +1 -8
- package/crawlers/statistics.js +45 -44
- package/events/event_manager.d.ts +1 -1
- package/events/event_manager.js +3 -3
- package/events/local_event_manager.d.ts +1 -1
- package/events/local_event_manager.js +3 -3
- package/log.js +5 -1
- package/memory-storage/memory-storage.d.ts +1 -5
- package/memory-storage/memory-storage.js +2 -2
- package/memory-storage/resource-clients/dataset.d.ts +1 -1
- package/memory-storage/resource-clients/dataset.js +6 -5
- package/memory-storage/resource-clients/key-value-store.d.ts +1 -1
- package/memory-storage/resource-clients/key-value-store.js +13 -12
- package/memory-storage/resource-clients/request-queue.d.ts +4 -23
- package/memory-storage/resource-clients/request-queue.js +59 -58
- package/owned_or_injected.d.ts +1 -3
- package/owned_or_injected.js +17 -17
- package/package.json +5 -5
- package/proxy_configuration.d.ts +1 -3
- package/proxy_configuration.js +8 -8
- package/recoverable_state.d.ts +1 -10
- package/recoverable_state.js +41 -41
- package/request.d.ts +1 -2
- package/request.js +10 -13
- package/router.d.ts +1 -4
- package/router.js +23 -23
- package/serialization.js +8 -9
- package/service_locator.d.ts +1 -10
- package/service_locator.js +48 -48
- package/session_pool/session.d.ts +1 -12
- package/session_pool/session.js +50 -50
- package/session_pool/session_pool.d.ts +2 -11
- package/session_pool/session_pool.js +59 -58
- package/storages/dataset.d.ts +12 -1
- package/storages/dataset.js +121 -22
- package/storages/index.d.ts +1 -1
- package/storages/index.js +1 -1
- package/storages/key_value_store.d.ts +19 -4
- package/storages/key_value_store.js +174 -48
- package/storages/request_dedup_cache.d.ts +1 -2
- package/storages/request_dedup_cache.js +9 -9
- package/storages/request_list.d.ts +2 -22
- package/storages/request_list.js +74 -73
- package/storages/request_manager_tandem.d.ts +1 -10
- package/storages/request_manager_tandem.js +27 -27
- package/storages/request_queue.d.ts +21 -18
- package/storages/request_queue.js +256 -53
- package/storages/sitemap_request_loader.d.ts +1 -44
- package/storages/sitemap_request_loader.js +87 -87
- package/storages/storage_instance_manager.d.ts +1 -2
- package/storages/storage_instance_manager.js +17 -17
- package/storages/storage_stats.d.ts +1 -1
- package/storages/storage_stats.js +4 -4
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/system-info/runtime.js +7 -7
- package/storages/access_checking.d.ts +0 -12
- package/storages/access_checking.js +0 -17
package/recoverable_state.js
CHANGED
|
@@ -10,30 +10,30 @@ import { KeyValueStore, serviceLocator } from '@crawlee/core';
|
|
|
10
10
|
* The class automatically hooks into the event system to persist state when needed.
|
|
11
11
|
*/
|
|
12
12
|
export class RecoverableState {
|
|
13
|
-
defaultState;
|
|
14
|
-
state = null;
|
|
15
|
-
persistenceEnabled;
|
|
16
|
-
persistStateKey;
|
|
17
|
-
persistStateKvsName;
|
|
18
|
-
persistStateKvsId;
|
|
19
|
-
keyValueStore = null;
|
|
20
|
-
log;
|
|
21
|
-
serialize;
|
|
22
|
-
deserialize;
|
|
13
|
+
#defaultState;
|
|
14
|
+
#state = null;
|
|
15
|
+
#persistenceEnabled;
|
|
16
|
+
#persistStateKey;
|
|
17
|
+
#persistStateKvsName;
|
|
18
|
+
#persistStateKvsId;
|
|
19
|
+
#keyValueStore = null;
|
|
20
|
+
#log;
|
|
21
|
+
#serialize;
|
|
22
|
+
#deserialize;
|
|
23
23
|
/**
|
|
24
24
|
* Initialize a new recoverable state object.
|
|
25
25
|
*
|
|
26
26
|
* @param options Configuration options for the recoverable state
|
|
27
27
|
*/
|
|
28
28
|
constructor(options) {
|
|
29
|
-
this
|
|
30
|
-
this
|
|
31
|
-
this
|
|
32
|
-
this
|
|
33
|
-
this
|
|
34
|
-
this
|
|
35
|
-
this
|
|
36
|
-
this
|
|
29
|
+
this.#defaultState = options.defaultState;
|
|
30
|
+
this.#persistStateKey = options.persistStateKey;
|
|
31
|
+
this.#persistenceEnabled = options.persistenceEnabled ?? false;
|
|
32
|
+
this.#persistStateKvsName = options.persistStateKvsName;
|
|
33
|
+
this.#persistStateKvsId = options.persistStateKvsId;
|
|
34
|
+
this.#log = options.logger ?? serviceLocator.getLogger().child({ prefix: 'RecoverableState' });
|
|
35
|
+
this.#serialize = options.serialize ?? JSON.stringify;
|
|
36
|
+
this.#deserialize = options.deserialize ?? JSON.parse;
|
|
37
37
|
this.persistState = this.persistState.bind(this);
|
|
38
38
|
}
|
|
39
39
|
/**
|
|
@@ -45,21 +45,21 @@ export class RecoverableState {
|
|
|
45
45
|
* @returns The loaded state object
|
|
46
46
|
*/
|
|
47
47
|
async initialize() {
|
|
48
|
-
if (this
|
|
48
|
+
if (this.#state !== null && this.#state !== undefined) {
|
|
49
49
|
return this.currentValue;
|
|
50
50
|
}
|
|
51
|
-
if (!this
|
|
52
|
-
this
|
|
51
|
+
if (!this.#persistenceEnabled) {
|
|
52
|
+
this.#state = this.#deserialize(this.#serialize(this.#defaultState));
|
|
53
53
|
return this.currentValue;
|
|
54
54
|
}
|
|
55
55
|
let kvsIdentifier = null;
|
|
56
|
-
if (this
|
|
57
|
-
kvsIdentifier = { name: this
|
|
56
|
+
if (this.#persistStateKvsName) {
|
|
57
|
+
kvsIdentifier = { name: this.#persistStateKvsName };
|
|
58
58
|
}
|
|
59
|
-
else if (this
|
|
60
|
-
kvsIdentifier = { id: this
|
|
59
|
+
else if (this.#persistStateKvsId) {
|
|
60
|
+
kvsIdentifier = { id: this.#persistStateKvsId };
|
|
61
61
|
}
|
|
62
|
-
this
|
|
62
|
+
this.#keyValueStore = await KeyValueStore.open(kvsIdentifier, {
|
|
63
63
|
configuration: serviceLocator.getConfiguration(),
|
|
64
64
|
});
|
|
65
65
|
await this.loadSavedState();
|
|
@@ -75,7 +75,7 @@ export class RecoverableState {
|
|
|
75
75
|
* and persists the current state one last time.
|
|
76
76
|
*/
|
|
77
77
|
async teardown() {
|
|
78
|
-
if (!this
|
|
78
|
+
if (!this.#persistenceEnabled || !this.persistState) {
|
|
79
79
|
return;
|
|
80
80
|
}
|
|
81
81
|
const eventManager = serviceLocator.getEventManager();
|
|
@@ -86,10 +86,10 @@ export class RecoverableState {
|
|
|
86
86
|
* Get the current state.
|
|
87
87
|
*/
|
|
88
88
|
get currentValue() {
|
|
89
|
-
if (this
|
|
89
|
+
if (this.#state === null) {
|
|
90
90
|
throw new Error('Recoverable state has not yet been loaded');
|
|
91
91
|
}
|
|
92
|
-
return this
|
|
92
|
+
return this.#state;
|
|
93
93
|
}
|
|
94
94
|
/**
|
|
95
95
|
* Reset the state to the default values and clear any persisted state.
|
|
@@ -98,12 +98,12 @@ export class RecoverableState {
|
|
|
98
98
|
* clears the persisted state from the KeyValueStore.
|
|
99
99
|
*/
|
|
100
100
|
async reset() {
|
|
101
|
-
this
|
|
102
|
-
if (this
|
|
103
|
-
if (this
|
|
101
|
+
this.#state = this.#deserialize(this.#serialize(this.#defaultState));
|
|
102
|
+
if (this.#persistenceEnabled) {
|
|
103
|
+
if (this.#keyValueStore === null) {
|
|
104
104
|
throw new Error('Recoverable state has not yet been initialized');
|
|
105
105
|
}
|
|
106
|
-
await this
|
|
106
|
+
await this.#keyValueStore.setValue(this.#persistStateKey, null);
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
@@ -115,12 +115,12 @@ export class RecoverableState {
|
|
|
115
115
|
* @param eventData Optional data associated with a PERSIST_STATE event
|
|
116
116
|
*/
|
|
117
117
|
async persistState(eventData) {
|
|
118
|
-
this
|
|
119
|
-
if (this
|
|
118
|
+
this.#log.debug(`Persisting state of the RecoverableState (eventData=${JSON.stringify(eventData)}).`);
|
|
119
|
+
if (this.#keyValueStore === null || this.#state === null) {
|
|
120
120
|
throw new Error('Recoverable state has not yet been initialized');
|
|
121
121
|
}
|
|
122
|
-
if (this
|
|
123
|
-
await this
|
|
122
|
+
if (this.#persistenceEnabled) {
|
|
123
|
+
await this.#keyValueStore.setValue(this.#persistStateKey, this.#serialize(this.#state), {
|
|
124
124
|
contentType: 'text/plain', // HACK - the result is expected to be JSON, but we do this to avoid the implicit JSON.parse in `KeyValueStore.getValue`
|
|
125
125
|
});
|
|
126
126
|
}
|
|
@@ -129,15 +129,15 @@ export class RecoverableState {
|
|
|
129
129
|
* Load the saved state from the KeyValueStore
|
|
130
130
|
*/
|
|
131
131
|
async loadSavedState() {
|
|
132
|
-
if (this
|
|
132
|
+
if (this.#keyValueStore === null) {
|
|
133
133
|
throw new Error('Recoverable state has not yet been initialized');
|
|
134
134
|
}
|
|
135
|
-
const storedState = await this
|
|
135
|
+
const storedState = await this.#keyValueStore.getValue(this.#persistStateKey);
|
|
136
136
|
if (storedState === null || storedState === undefined) {
|
|
137
|
-
this
|
|
137
|
+
this.#state = this.#deserialize(this.#serialize(this.#defaultState));
|
|
138
138
|
}
|
|
139
139
|
else {
|
|
140
|
-
this
|
|
140
|
+
this.#state = this.#deserialize(storedState);
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
}
|
package/request.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ export declare enum RequestState {
|
|
|
44
44
|
* @category Sources
|
|
45
45
|
*/
|
|
46
46
|
declare class CrawleeRequest<UserData extends Dictionary = Dictionary> {
|
|
47
|
+
#private;
|
|
47
48
|
/** Request ID */
|
|
48
49
|
id?: string;
|
|
49
50
|
/** URL of the web page to crawl. */
|
|
@@ -74,8 +75,6 @@ declare class CrawleeRequest<UserData extends Dictionary = Dictionary> {
|
|
|
74
75
|
errorMessages: string[];
|
|
75
76
|
/** Object with HTTP headers. Key is header name, value is the value. */
|
|
76
77
|
headers?: Record<string, string>;
|
|
77
|
-
/** Private store for the custom user data assigned to the request. */
|
|
78
|
-
private _userData;
|
|
79
78
|
/**
|
|
80
79
|
* Custom user data assigned to the request.
|
|
81
80
|
*
|
package/request.js
CHANGED
|
@@ -101,7 +101,7 @@ class CrawleeRequest {
|
|
|
101
101
|
/** Object with HTTP headers. Key is header name, value is the value. */
|
|
102
102
|
headers;
|
|
103
103
|
/** Private store for the custom user data assigned to the request. */
|
|
104
|
-
|
|
104
|
+
#userData = {};
|
|
105
105
|
/**
|
|
106
106
|
* Custom user data assigned to the request.
|
|
107
107
|
*
|
|
@@ -167,36 +167,33 @@ class CrawleeRequest {
|
|
|
167
167
|
if (label) {
|
|
168
168
|
userData.label = label;
|
|
169
169
|
}
|
|
170
|
+
this.#userData = { __crawlee: {}, ...userData };
|
|
171
|
+
// `userData` must stay an enumerable own accessor — serialization in the storages relies on it
|
|
170
172
|
Object.defineProperties(this, {
|
|
171
|
-
_userData: {
|
|
172
|
-
value: { __crawlee: {}, ...userData },
|
|
173
|
-
enumerable: false,
|
|
174
|
-
writable: true,
|
|
175
|
-
},
|
|
176
173
|
userData: {
|
|
177
|
-
get: () => this
|
|
174
|
+
get: () => this.#userData,
|
|
178
175
|
set: (value) => {
|
|
179
176
|
Object.defineProperties(value, {
|
|
180
177
|
__crawlee: {
|
|
181
|
-
value: this.
|
|
178
|
+
value: this.#userData.__crawlee,
|
|
182
179
|
enumerable: false,
|
|
183
180
|
writable: true,
|
|
184
181
|
},
|
|
185
182
|
toJSON: {
|
|
186
183
|
value: () => {
|
|
187
|
-
if (Object.keys(this.
|
|
184
|
+
if (Object.keys(this.#userData.__crawlee).length > 0) {
|
|
188
185
|
return {
|
|
189
|
-
...this
|
|
190
|
-
__crawlee: this.
|
|
186
|
+
...this.#userData,
|
|
187
|
+
__crawlee: this.#userData.__crawlee,
|
|
191
188
|
};
|
|
192
189
|
}
|
|
193
|
-
return this
|
|
190
|
+
return this.#userData;
|
|
194
191
|
},
|
|
195
192
|
enumerable: false,
|
|
196
193
|
writable: true,
|
|
197
194
|
},
|
|
198
195
|
});
|
|
199
|
-
this
|
|
196
|
+
this.#userData = value;
|
|
200
197
|
},
|
|
201
198
|
enumerable: true,
|
|
202
199
|
},
|
package/router.d.ts
CHANGED
|
@@ -215,10 +215,7 @@ export type RouterRoutes<Context, Routes extends Record<keyof Routes, Dictionary
|
|
|
215
215
|
* ```
|
|
216
216
|
*/
|
|
217
217
|
export declare class Router<Context extends Omit<RestrictedCrawlingContext, 'enqueueLinks'>, Routes extends Record<keyof Routes, Dictionary> = Record<string, GetUserDataFromRequest<Context['request']>>> {
|
|
218
|
-
private
|
|
219
|
-
private readonly schemas;
|
|
220
|
-
private readonly timeouts;
|
|
221
|
-
private readonly middlewares;
|
|
218
|
+
#private;
|
|
222
219
|
/**
|
|
223
220
|
* use Router.create() instead!
|
|
224
221
|
* @ignore
|
package/router.js
CHANGED
|
@@ -161,10 +161,10 @@ export async function validateUserData(label, schema, userData) {
|
|
|
161
161
|
* ```
|
|
162
162
|
*/
|
|
163
163
|
export class Router {
|
|
164
|
-
routes = new Map();
|
|
165
|
-
schemas = new Map();
|
|
166
|
-
timeouts = new Map();
|
|
167
|
-
middlewares = [];
|
|
164
|
+
#routes = new Map();
|
|
165
|
+
#schemas = new Map();
|
|
166
|
+
#timeouts = new Map();
|
|
167
|
+
#middlewares = [];
|
|
168
168
|
/**
|
|
169
169
|
* use Router.create() instead!
|
|
170
170
|
* @ignore
|
|
@@ -172,9 +172,9 @@ export class Router {
|
|
|
172
172
|
constructor() { }
|
|
173
173
|
addHandler(label, handler, options = {}) {
|
|
174
174
|
this.validate(label);
|
|
175
|
-
this
|
|
175
|
+
this.#routes.set(label, handler);
|
|
176
176
|
if (options.requestHandlerTimeoutSecs !== undefined) {
|
|
177
|
-
this
|
|
177
|
+
this.#timeouts.set(label, options.requestHandlerTimeoutSecs);
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
/**
|
|
@@ -187,9 +187,9 @@ export class Router {
|
|
|
187
187
|
*/
|
|
188
188
|
addDefaultHandler(handler, options = {}) {
|
|
189
189
|
this.validate(defaultRoute);
|
|
190
|
-
this
|
|
190
|
+
this.#routes.set(defaultRoute, handler);
|
|
191
191
|
if (options.requestHandlerTimeoutSecs !== undefined) {
|
|
192
|
-
this
|
|
192
|
+
this.#timeouts.set(defaultRoute, options.requestHandlerTimeoutSecs);
|
|
193
193
|
}
|
|
194
194
|
}
|
|
195
195
|
/**
|
|
@@ -199,25 +199,25 @@ export class Router {
|
|
|
199
199
|
*/
|
|
200
200
|
getSchema(label) {
|
|
201
201
|
if (label != null) {
|
|
202
|
-
const schema = this
|
|
202
|
+
const schema = this.#schemas.get(label);
|
|
203
203
|
if (schema) {
|
|
204
204
|
return schema;
|
|
205
205
|
}
|
|
206
206
|
// A label with its own route is fully specified; don't fall back to the default-route schema.
|
|
207
|
-
if (this
|
|
207
|
+
if (this.#routes.has(label)) {
|
|
208
208
|
return undefined;
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
// Requests with no route of their own fall through to the default handler, so validate their
|
|
212
212
|
// `userData` against the default-route schema, if one was registered.
|
|
213
|
-
return this
|
|
213
|
+
return this.#schemas.get(defaultRoute);
|
|
214
214
|
}
|
|
215
215
|
/**
|
|
216
216
|
* Registers a middleware that will be fired before the matching route handler.
|
|
217
217
|
* Multiple middlewares can be registered, they will be fired in the same order.
|
|
218
218
|
*/
|
|
219
219
|
use(middleware) {
|
|
220
|
-
this
|
|
220
|
+
this.#middlewares.push(middleware);
|
|
221
221
|
}
|
|
222
222
|
/**
|
|
223
223
|
* Returns the `requestHandlerTimeoutSecs` registered for a label, or `undefined` when the route did not
|
|
@@ -226,27 +226,27 @@ export class Router {
|
|
|
226
226
|
* the default route asked for. Used by the crawler; not meant to be called directly.
|
|
227
227
|
*/
|
|
228
228
|
getTimeoutSecs(label) {
|
|
229
|
-
if (label && this
|
|
230
|
-
return this
|
|
229
|
+
if (label && this.#routes.has(label)) {
|
|
230
|
+
return this.#timeouts.get(label);
|
|
231
231
|
}
|
|
232
|
-
return this
|
|
232
|
+
return this.#timeouts.get(defaultRoute);
|
|
233
233
|
}
|
|
234
234
|
/**
|
|
235
235
|
* The longest `requestHandlerTimeoutSecs` any route asked for, or `undefined` when no route overrides it.
|
|
236
236
|
* The crawler needs an upper bound up front, before it knows which routes a run will actually hit.
|
|
237
237
|
*/
|
|
238
238
|
getMaxTimeoutSecs() {
|
|
239
|
-
return this
|
|
239
|
+
return this.#timeouts.size > 0 ? Math.max(...this.#timeouts.values()) : undefined;
|
|
240
240
|
}
|
|
241
241
|
/**
|
|
242
242
|
* Returns route handler for given label. If no label is provided, the default request handler will be returned.
|
|
243
243
|
*/
|
|
244
244
|
getHandler(label) {
|
|
245
|
-
if (label && this
|
|
246
|
-
return this
|
|
245
|
+
if (label && this.#routes.has(label)) {
|
|
246
|
+
return this.#routes.get(label);
|
|
247
247
|
}
|
|
248
|
-
if (this
|
|
249
|
-
return this
|
|
248
|
+
if (this.#routes.has(defaultRoute)) {
|
|
249
|
+
return this.#routes.get(defaultRoute);
|
|
250
250
|
}
|
|
251
251
|
throw new MissingRouteError(`Route not found for label '${String(label)}'.` +
|
|
252
252
|
' You must set up a route for this label or a default route.' +
|
|
@@ -267,7 +267,7 @@ export class Router {
|
|
|
267
267
|
* Throws when the label already exists in our registry.
|
|
268
268
|
*/
|
|
269
269
|
validate(label) {
|
|
270
|
-
if (this
|
|
270
|
+
if (this.#routes.has(label)) {
|
|
271
271
|
const message = label === defaultRoute
|
|
272
272
|
? `Default route is already defined!`
|
|
273
273
|
: `Route for label '${String(label)}' is already defined!`;
|
|
@@ -291,14 +291,14 @@ export class Router {
|
|
|
291
291
|
router.addHandler(label, value);
|
|
292
292
|
}
|
|
293
293
|
else {
|
|
294
|
-
router
|
|
294
|
+
router.#schemas.set(label, value);
|
|
295
295
|
}
|
|
296
296
|
}
|
|
297
297
|
const func = async function (context) {
|
|
298
298
|
const { url, loadedUrl, label } = context.request;
|
|
299
299
|
context.log.debug('Page opened.', { label, url: loadedUrl ?? url });
|
|
300
300
|
await router.validateRequest(context);
|
|
301
|
-
for (const middleware of router
|
|
301
|
+
for (const middleware of router.#middlewares) {
|
|
302
302
|
await middleware(context);
|
|
303
303
|
}
|
|
304
304
|
return router.getHandler(label)(context);
|
package/serialization.js
CHANGED
|
@@ -11,32 +11,31 @@ const pipeline = util.promisify(streamPipeline);
|
|
|
11
11
|
* @internal
|
|
12
12
|
*/
|
|
13
13
|
class ArrayToJson extends Readable {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
#offset = 0;
|
|
15
|
+
#batchSize;
|
|
16
|
+
#data;
|
|
17
17
|
constructor(data, options = {}) {
|
|
18
18
|
super({
|
|
19
19
|
...options,
|
|
20
20
|
autoDestroy: true,
|
|
21
21
|
emitClose: true,
|
|
22
22
|
});
|
|
23
|
-
this.data = data;
|
|
24
23
|
const { batchSize = 10000 } = options;
|
|
25
|
-
this
|
|
26
|
-
this
|
|
24
|
+
this.#batchSize = batchSize;
|
|
25
|
+
this.#data = data;
|
|
27
26
|
this.push('[');
|
|
28
27
|
}
|
|
29
28
|
_read() {
|
|
30
29
|
try {
|
|
31
|
-
const items = this
|
|
30
|
+
const items = this.#data.slice(this.#offset, this.#offset + this.#batchSize);
|
|
32
31
|
if (items.length) {
|
|
33
32
|
const json = JSON.stringify(items);
|
|
34
33
|
// Strip brackets to flatten the batch.
|
|
35
34
|
const itemString = json.substring(1, json.length - 1);
|
|
36
|
-
if (this
|
|
35
|
+
if (this.#offset > 0)
|
|
37
36
|
this.push(',', 'utf8');
|
|
38
37
|
this.push(itemString, 'utf8');
|
|
39
|
-
this
|
|
38
|
+
this.#offset += this.#batchSize;
|
|
40
39
|
}
|
|
41
40
|
else {
|
|
42
41
|
this.push(']');
|
package/service_locator.d.ts
CHANGED
|
@@ -101,16 +101,7 @@ interface ServiceLocatorInterface {
|
|
|
101
101
|
* ```
|
|
102
102
|
*/
|
|
103
103
|
export declare class ServiceLocator implements ServiceLocatorInterface {
|
|
104
|
-
private
|
|
105
|
-
private eventManager?;
|
|
106
|
-
private storageBackend?;
|
|
107
|
-
private logger?;
|
|
108
|
-
/**
|
|
109
|
-
* Unified storage instance manager for Dataset, KeyValueStore, and RequestQueue.
|
|
110
|
-
* Shared across all ServiceLocator instances (global singleton), matching crawlee-python.
|
|
111
|
-
* Per-crawler isolation is achieved via `clientCacheKey`, not separate manager instances.
|
|
112
|
-
*/
|
|
113
|
-
private static storageInstanceManager?;
|
|
104
|
+
#private;
|
|
114
105
|
/**
|
|
115
106
|
* Creates a new ServiceLocator instance.
|
|
116
107
|
*
|
package/service_locator.js
CHANGED
|
@@ -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
|
|
62
|
-
this
|
|
63
|
-
this
|
|
64
|
-
this
|
|
61
|
+
this.#configuration = configuration;
|
|
62
|
+
this.#eventManager = eventManager;
|
|
63
|
+
this.#storageBackend = storageBackend;
|
|
64
|
+
this.#logger = logger;
|
|
65
65
|
}
|
|
66
66
|
getConfiguration() {
|
|
67
|
-
if (!this
|
|
67
|
+
if (!this.#configuration) {
|
|
68
68
|
this.getLogger().debug('No configuration set, implicitly creating and using default Configuration.');
|
|
69
|
-
this
|
|
69
|
+
this.#configuration = new Configuration();
|
|
70
70
|
}
|
|
71
|
-
return this
|
|
71
|
+
return this.#configuration;
|
|
72
72
|
}
|
|
73
73
|
setConfiguration(configuration) {
|
|
74
74
|
// Same instance, no need to do anything
|
|
75
|
-
if (this
|
|
75
|
+
if (this.#configuration === configuration) {
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
78
|
// Already have a different configuration that was retrieved
|
|
79
|
-
if (this
|
|
80
|
-
throw new ServiceConflictError('Configuration', configuration, this
|
|
79
|
+
if (this.#configuration) {
|
|
80
|
+
throw new ServiceConflictError('Configuration', configuration, this.#configuration);
|
|
81
81
|
}
|
|
82
|
-
this
|
|
82
|
+
this.#configuration = configuration;
|
|
83
83
|
}
|
|
84
84
|
getEventManager() {
|
|
85
|
-
if (!this
|
|
85
|
+
if (!this.#eventManager) {
|
|
86
86
|
this.getLogger().debug('No event manager set, implicitly creating and using default LocalEventManager.');
|
|
87
|
-
if (!this
|
|
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
|
|
91
|
+
this.#eventManager = LocalEventManager.fromConfiguration(this.getConfiguration());
|
|
92
92
|
}
|
|
93
|
-
return this
|
|
93
|
+
return this.#eventManager;
|
|
94
94
|
}
|
|
95
95
|
setEventManager(eventManager) {
|
|
96
96
|
// Same instance, no need to do anything
|
|
97
|
-
if (this
|
|
97
|
+
if (this.#eventManager === eventManager) {
|
|
98
98
|
return;
|
|
99
99
|
}
|
|
100
100
|
// Already have a different event manager that was retrieved
|
|
101
|
-
if (this
|
|
102
|
-
throw new ServiceConflictError('EventManager', eventManager, this
|
|
101
|
+
if (this.#eventManager) {
|
|
102
|
+
throw new ServiceConflictError('EventManager', eventManager, this.#eventManager);
|
|
103
103
|
}
|
|
104
|
-
this
|
|
104
|
+
this.#eventManager = eventManager;
|
|
105
105
|
}
|
|
106
106
|
getStorageBackend() {
|
|
107
|
-
if (!this
|
|
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
|
|
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
|
|
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
|
|
124
|
+
return this.#storageBackend;
|
|
125
125
|
}
|
|
126
126
|
setStorageBackend(storageBackend) {
|
|
127
127
|
// Same instance, no need to do anything
|
|
128
|
-
if (this
|
|
128
|
+
if (this.#storageBackend === storageBackend) {
|
|
129
129
|
return;
|
|
130
130
|
}
|
|
131
131
|
// Already have a different storage backend that was retrieved
|
|
132
|
-
if (this
|
|
133
|
-
throw new ServiceConflictError('StorageBackend', storageBackend, this
|
|
132
|
+
if (this.#storageBackend) {
|
|
133
|
+
throw new ServiceConflictError('StorageBackend', storageBackend, this.#storageBackend);
|
|
134
134
|
}
|
|
135
|
-
this
|
|
135
|
+
this.#storageBackend = storageBackend;
|
|
136
136
|
}
|
|
137
137
|
getLogger() {
|
|
138
|
-
if (!this
|
|
139
|
-
this
|
|
138
|
+
if (!this.#logger) {
|
|
139
|
+
this.#logger = new ApifyLogAdapter(log);
|
|
140
140
|
}
|
|
141
|
-
return this
|
|
141
|
+
return this.#logger;
|
|
142
142
|
}
|
|
143
143
|
setLogger(logger) {
|
|
144
|
-
if (this
|
|
144
|
+
if (this.#logger === logger) {
|
|
145
145
|
return;
|
|
146
146
|
}
|
|
147
|
-
if (this
|
|
148
|
-
throw new ServiceConflictError('Logger', logger, this
|
|
147
|
+
if (this.#logger) {
|
|
148
|
+
throw new ServiceConflictError('Logger', logger, this.#logger);
|
|
149
149
|
}
|
|
150
|
-
this
|
|
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
|
|
157
|
-
ServiceLocator
|
|
156
|
+
if (!ServiceLocator.#storageInstanceManager) {
|
|
157
|
+
ServiceLocator.#storageInstanceManager = new StorageInstanceManager();
|
|
158
158
|
}
|
|
159
|
-
return ServiceLocator
|
|
159
|
+
return ServiceLocator.#storageInstanceManager;
|
|
160
160
|
}
|
|
161
161
|
reset() {
|
|
162
|
-
this
|
|
163
|
-
this
|
|
164
|
-
this
|
|
165
|
-
this
|
|
166
|
-
ServiceLocator
|
|
167
|
-
ServiceLocator
|
|
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;
|