@crawlee/core 4.0.0-beta.3 → 4.0.0-beta.30
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/README.md +5 -1
- package/autoscaling/autoscaled_pool.d.ts +1 -3
- package/autoscaling/autoscaled_pool.d.ts.map +1 -1
- package/autoscaling/autoscaled_pool.js +1 -7
- package/autoscaling/autoscaled_pool.js.map +1 -1
- package/autoscaling/snapshotter.d.ts +0 -10
- package/autoscaling/snapshotter.d.ts.map +1 -1
- package/autoscaling/snapshotter.js +15 -28
- package/autoscaling/snapshotter.js.map +1 -1
- package/autoscaling/system_status.d.ts +0 -3
- package/autoscaling/system_status.d.ts.map +1 -1
- package/autoscaling/system_status.js +2 -3
- package/autoscaling/system_status.js.map +1 -1
- package/configuration.d.ts +4 -77
- package/configuration.d.ts.map +1 -1
- package/configuration.js +5 -101
- package/configuration.js.map +1 -1
- package/cookie_utils.d.ts +1 -1
- package/cookie_utils.d.ts.map +1 -1
- package/cookie_utils.js +4 -6
- package/cookie_utils.js.map +1 -1
- package/crawlers/context_pipeline.d.ts +61 -0
- package/crawlers/context_pipeline.d.ts.map +1 -0
- package/crawlers/context_pipeline.js +99 -0
- package/crawlers/context_pipeline.js.map +1 -0
- package/crawlers/crawler_commons.d.ts +12 -20
- package/crawlers/crawler_commons.d.ts.map +1 -1
- package/crawlers/crawler_commons.js +0 -8
- package/crawlers/crawler_commons.js.map +1 -1
- package/crawlers/error_snapshotter.d.ts +3 -2
- package/crawlers/error_snapshotter.d.ts.map +1 -1
- package/crawlers/error_snapshotter.js +2 -2
- package/crawlers/error_snapshotter.js.map +1 -1
- package/crawlers/error_tracker.d.ts +2 -1
- package/crawlers/error_tracker.d.ts.map +1 -1
- package/crawlers/error_tracker.js.map +1 -1
- package/crawlers/index.d.ts +1 -1
- package/crawlers/index.d.ts.map +1 -1
- package/crawlers/index.js +1 -1
- package/crawlers/index.js.map +1 -1
- package/crawlers/internals/types.d.ts +8 -0
- package/crawlers/internals/types.d.ts.map +1 -0
- package/crawlers/internals/types.js +2 -0
- package/crawlers/internals/types.js.map +1 -0
- package/crawlers/statistics.d.ts +13 -13
- package/crawlers/statistics.d.ts.map +1 -1
- package/crawlers/statistics.js +20 -16
- package/crawlers/statistics.js.map +1 -1
- package/enqueue_links/enqueue_links.d.ts +30 -18
- package/enqueue_links/enqueue_links.d.ts.map +1 -1
- package/enqueue_links/enqueue_links.js +39 -21
- package/enqueue_links/enqueue_links.js.map +1 -1
- package/enqueue_links/shared.d.ts +24 -7
- package/enqueue_links/shared.d.ts.map +1 -1
- package/enqueue_links/shared.js +66 -37
- package/enqueue_links/shared.js.map +1 -1
- package/errors.d.ts +18 -0
- package/errors.d.ts.map +1 -1
- package/errors.js +29 -0
- package/errors.js.map +1 -1
- package/events/event_manager.d.ts +7 -4
- package/events/event_manager.d.ts.map +1 -1
- package/events/event_manager.js +5 -7
- package/events/event_manager.js.map +1 -1
- package/events/local_event_manager.d.ts +14 -4
- package/events/local_event_manager.d.ts.map +1 -1
- package/events/local_event_manager.js +26 -37
- package/events/local_event_manager.js.map +1 -1
- package/index.d.ts +2 -1
- package/index.d.ts.map +1 -1
- package/index.js +2 -1
- package/index.js.map +1 -1
- package/log.d.ts +4 -2
- package/log.d.ts.map +1 -1
- package/log.js.map +1 -1
- package/package.json +6 -7
- package/proxy_configuration.d.ts +16 -93
- package/proxy_configuration.d.ts.map +1 -1
- package/proxy_configuration.js +15 -51
- package/proxy_configuration.js.map +1 -1
- package/recoverable_state.d.ts +122 -0
- package/recoverable_state.d.ts.map +1 -0
- package/recoverable_state.js +138 -0
- package/recoverable_state.js.map +1 -0
- package/request.d.ts +39 -5
- package/request.d.ts.map +1 -1
- package/request.js +52 -12
- package/request.js.map +1 -1
- package/service_locator.d.ts +139 -0
- package/service_locator.d.ts.map +1 -0
- package/service_locator.js +252 -0
- package/service_locator.js.map +1 -0
- package/session_pool/session.d.ts +7 -20
- package/session_pool/session.d.ts.map +1 -1
- package/session_pool/session.js +16 -4
- package/session_pool/session.js.map +1 -1
- package/session_pool/session_pool.d.ts +12 -7
- package/session_pool/session_pool.d.ts.map +1 -1
- package/session_pool/session_pool.js +22 -11
- package/session_pool/session_pool.js.map +1 -1
- package/storages/dataset.d.ts +7 -0
- package/storages/dataset.d.ts.map +1 -1
- package/storages/dataset.js +14 -4
- package/storages/dataset.js.map +1 -1
- package/storages/index.d.ts +2 -0
- package/storages/index.d.ts.map +1 -1
- package/storages/index.js +2 -0
- package/storages/index.js.map +1 -1
- package/storages/key_value_store.d.ts +13 -1
- package/storages/key_value_store.d.ts.map +1 -1
- package/storages/key_value_store.js +16 -10
- package/storages/key_value_store.js.map +1 -1
- package/storages/request_list.d.ts +9 -9
- package/storages/request_list.d.ts.map +1 -1
- package/storages/request_list.js +10 -6
- package/storages/request_list.js.map +1 -1
- package/storages/request_list_adapter.d.ts +58 -0
- package/storages/request_list_adapter.d.ts.map +1 -0
- package/storages/request_list_adapter.js +81 -0
- package/storages/request_list_adapter.js.map +1 -0
- package/storages/request_manager_tandem.d.ts +68 -0
- package/storages/request_manager_tandem.d.ts.map +1 -0
- package/storages/request_manager_tandem.js +124 -0
- package/storages/request_manager_tandem.js.map +1 -0
- package/storages/request_provider.d.ts +74 -7
- package/storages/request_provider.d.ts.map +1 -1
- package/storages/request_provider.js +89 -52
- package/storages/request_provider.js.map +1 -1
- package/storages/request_queue.d.ts +1 -1
- package/storages/request_queue.d.ts.map +1 -1
- package/storages/request_queue.js +2 -2
- package/storages/request_queue.js.map +1 -1
- package/storages/request_queue_v2.d.ts +3 -3
- package/storages/request_queue_v2.d.ts.map +1 -1
- package/storages/request_queue_v2.js +4 -5
- package/storages/request_queue_v2.js.map +1 -1
- package/storages/sitemap_request_list.d.ts +5 -5
- package/storages/sitemap_request_list.d.ts.map +1 -1
- package/storages/sitemap_request_list.js +6 -5
- package/storages/sitemap_request_list.js.map +1 -1
- package/storages/storage_manager.d.ts +10 -8
- package/storages/storage_manager.d.ts.map +1 -1
- package/storages/storage_manager.js +12 -22
- package/storages/storage_manager.js.map +1 -1
- package/storages/utils.d.ts.map +1 -1
- package/storages/utils.js +4 -3
- package/storages/utils.js.map +1 -1
- package/typedefs.d.ts +1 -1
- package/typedefs.d.ts.map +1 -1
- package/crawlers/crawler_extension.d.ts +0 -12
- package/crawlers/crawler_extension.d.ts.map +0 -1
- package/crawlers/crawler_extension.js +0 -14
- package/crawlers/crawler_extension.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -134
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -33
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -15
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -69
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { MemoryStorage } from '@crawlee/memory-storage';
|
|
3
|
+
import log from '@apify/log';
|
|
4
|
+
import { Configuration } from './configuration.js';
|
|
5
|
+
import { ServiceConflictError } from './errors.js';
|
|
6
|
+
import { LocalEventManager } from './events/local_event_manager.js';
|
|
7
|
+
/**
|
|
8
|
+
* Service locator for managing the services used by Crawlee.
|
|
9
|
+
*
|
|
10
|
+
* All services are initialized to their default value lazily.
|
|
11
|
+
*
|
|
12
|
+
* There are two primary usage patterns:
|
|
13
|
+
*
|
|
14
|
+
* **1. Global service locator (for default services):**
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import { serviceLocator, BasicCrawler } from 'crawlee';
|
|
17
|
+
*
|
|
18
|
+
* // Optionally configure global services before creating crawlers
|
|
19
|
+
* serviceLocator.setStorageClient(myCustomClient);
|
|
20
|
+
*
|
|
21
|
+
* // Crawler uses global services
|
|
22
|
+
* const crawler = new BasicCrawler({ ... });
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* **2. Per-crawler services (recommended for isolation):**
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { BasicCrawler, Configuration, LocalEventManager } from 'crawlee';
|
|
28
|
+
* import { MemoryStorage } from '@crawlee/memory-storage';
|
|
29
|
+
*
|
|
30
|
+
* const crawler = new BasicCrawler({
|
|
31
|
+
* requestHandler: async ({ request }) => { ... },
|
|
32
|
+
* configuration: new Configuration({ ... }), // custom config
|
|
33
|
+
* storageClient: new MemoryStorage(), // custom storage
|
|
34
|
+
* eventManager: LocalEventManager.fromConfig(), // custom events
|
|
35
|
+
* });
|
|
36
|
+
* // Crawler has its own isolated ServiceLocator instance
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export class ServiceLocator {
|
|
40
|
+
configuration;
|
|
41
|
+
eventManager;
|
|
42
|
+
storageClient;
|
|
43
|
+
/**
|
|
44
|
+
* Storage managers for Dataset, KeyValueStore, and RequestQueue.
|
|
45
|
+
* Manages caching and lifecycle of storage instances.
|
|
46
|
+
*/
|
|
47
|
+
storageManagers = new Map();
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new ServiceLocator instance.
|
|
50
|
+
*
|
|
51
|
+
* @param configuration Optional configuration instance to use
|
|
52
|
+
* @param eventManager Optional event manager instance to use
|
|
53
|
+
* @param storageClient Optional storage client instance to use
|
|
54
|
+
*/
|
|
55
|
+
constructor(configuration, eventManager, storageClient) {
|
|
56
|
+
this.configuration = configuration;
|
|
57
|
+
this.eventManager = eventManager;
|
|
58
|
+
this.storageClient = storageClient;
|
|
59
|
+
}
|
|
60
|
+
getConfiguration() {
|
|
61
|
+
if (!this.configuration) {
|
|
62
|
+
log.debug('No configuration set, implicitly creating and using default Configuration.');
|
|
63
|
+
this.configuration = new Configuration();
|
|
64
|
+
}
|
|
65
|
+
return this.configuration;
|
|
66
|
+
}
|
|
67
|
+
setConfiguration(configuration) {
|
|
68
|
+
// Same instance, no need to do anything
|
|
69
|
+
if (this.configuration === configuration) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
// Already have a different configuration that was retrieved
|
|
73
|
+
if (this.configuration) {
|
|
74
|
+
throw new ServiceConflictError('Configuration', configuration, this.configuration);
|
|
75
|
+
}
|
|
76
|
+
this.configuration = configuration;
|
|
77
|
+
}
|
|
78
|
+
getEventManager() {
|
|
79
|
+
if (!this.eventManager) {
|
|
80
|
+
log.debug('No event manager set, implicitly creating and using default LocalEventManager.');
|
|
81
|
+
if (!this.configuration) {
|
|
82
|
+
log.warning('Implicit creation of event manager will implicitly set configuration as side effect. ' +
|
|
83
|
+
'It is advised to explicitly first set the configuration instead.');
|
|
84
|
+
}
|
|
85
|
+
this.eventManager = LocalEventManager.fromConfig(this.getConfiguration());
|
|
86
|
+
}
|
|
87
|
+
return this.eventManager;
|
|
88
|
+
}
|
|
89
|
+
setEventManager(eventManager) {
|
|
90
|
+
// Same instance, no need to do anything
|
|
91
|
+
if (this.eventManager === eventManager) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
// Already have a different event manager that was retrieved
|
|
95
|
+
if (this.eventManager) {
|
|
96
|
+
throw new ServiceConflictError('EventManager', eventManager, this.eventManager);
|
|
97
|
+
}
|
|
98
|
+
this.eventManager = eventManager;
|
|
99
|
+
}
|
|
100
|
+
getStorageClient() {
|
|
101
|
+
if (!this.storageClient) {
|
|
102
|
+
log.debug('No storage client set, implicitly creating and using default MemoryStorage.');
|
|
103
|
+
if (!this.configuration) {
|
|
104
|
+
log.warning('Implicit creation of storage client will implicitly set configuration as side effect. ' +
|
|
105
|
+
'It is advised to explicitly first set the configuration instead.');
|
|
106
|
+
}
|
|
107
|
+
const config = this.getConfiguration();
|
|
108
|
+
this.storageClient = new MemoryStorage({
|
|
109
|
+
persistStorage: config.get('persistStorage'),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
return this.storageClient;
|
|
113
|
+
}
|
|
114
|
+
setStorageClient(storageClient) {
|
|
115
|
+
// Same instance, no need to do anything
|
|
116
|
+
if (this.storageClient === storageClient) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
// Already have a different storage client that was retrieved
|
|
120
|
+
if (this.storageClient) {
|
|
121
|
+
throw new ServiceConflictError('StorageClient', storageClient, this.storageClient);
|
|
122
|
+
}
|
|
123
|
+
this.storageClient = storageClient;
|
|
124
|
+
}
|
|
125
|
+
getStorageManager(constructor) {
|
|
126
|
+
return this.storageManagers.get(constructor);
|
|
127
|
+
}
|
|
128
|
+
setStorageManager(constructor, storageManager) {
|
|
129
|
+
if (this.storageManagers.has(constructor)) {
|
|
130
|
+
throw new ServiceConflictError(`StorageManager(${constructor.name})`, storageManager, this.storageManagers.get(constructor));
|
|
131
|
+
}
|
|
132
|
+
this.storageManagers.set(constructor, storageManager);
|
|
133
|
+
}
|
|
134
|
+
clearStorageManagerCache() {
|
|
135
|
+
this.storageManagers.forEach((manager) => {
|
|
136
|
+
// KeyValueStore has a clearCache method on its instances
|
|
137
|
+
// TODO this uses fragile string matching and `any` casts into private fields - remove as part of
|
|
138
|
+
// https://github.com/apify/crawlee/issues/3075 (Storage instance management will be reworked significantly)
|
|
139
|
+
if (manager.name === 'KeyValueStore') {
|
|
140
|
+
manager.cache?.forEach((item) => {
|
|
141
|
+
item.clearCache?.();
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
this.storageManagers.clear();
|
|
146
|
+
}
|
|
147
|
+
reset() {
|
|
148
|
+
this.configuration = undefined;
|
|
149
|
+
this.eventManager = undefined;
|
|
150
|
+
this.storageClient = undefined;
|
|
151
|
+
this.clearStorageManagerCache();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Used as the default service provider when crawlers don't specify custom services.
|
|
156
|
+
*/
|
|
157
|
+
const globalServiceLocator = new ServiceLocator();
|
|
158
|
+
const serviceLocatorStorage = new AsyncLocalStorage();
|
|
159
|
+
/**
|
|
160
|
+
* Wraps all methods on `target` so that any code they invoke will see the given
|
|
161
|
+
* `serviceLocator` via `AsyncLocalStorage`, rather than the global one.
|
|
162
|
+
*
|
|
163
|
+
* Walks the prototype chain and replaces each method on the *instance* (not the prototype)
|
|
164
|
+
* with a wrapper that calls `serviceLocatorStorage.run(serviceLocator, originalMethod)`.
|
|
165
|
+
*
|
|
166
|
+
* The `AsyncLocalStorage` context propagates through the entire sync/async call tree of each
|
|
167
|
+
* wrapped method — including `super` calls, since the prototype methods execute within the
|
|
168
|
+
* context established by the instance-level wrapper.
|
|
169
|
+
*
|
|
170
|
+
* @internal
|
|
171
|
+
* @returns Scope control functions: `run` executes a callback within the scoped context,
|
|
172
|
+
* `enterScope`/`exitScope` allow entering/leaving the scope imperatively (e.g., for constructor bodies).
|
|
173
|
+
*/
|
|
174
|
+
export function bindMethodsToServiceLocator(serviceLocator, target) {
|
|
175
|
+
let proto = Object.getPrototypeOf(target);
|
|
176
|
+
while (proto !== null && proto !== Object.prototype) {
|
|
177
|
+
const propertyKeys = [...Object.getOwnPropertyNames(proto), ...Object.getOwnPropertySymbols(proto)];
|
|
178
|
+
for (const propertyKey of propertyKeys) {
|
|
179
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, propertyKey);
|
|
180
|
+
// We use property descriptors rather than accessing target[propertyKey] directly,
|
|
181
|
+
// because that would trigger getters and cause unwanted side effects.
|
|
182
|
+
// Skip getters, setters, and constructors — only wrap regular methods.
|
|
183
|
+
if (propertyKey === 'constructor' ||
|
|
184
|
+
!descriptor ||
|
|
185
|
+
descriptor.get ||
|
|
186
|
+
descriptor.set ||
|
|
187
|
+
typeof descriptor.value !== 'function')
|
|
188
|
+
continue;
|
|
189
|
+
const original = descriptor.value;
|
|
190
|
+
target[propertyKey] = (...args) => {
|
|
191
|
+
return serviceLocatorStorage.run(serviceLocator, () => {
|
|
192
|
+
return original.apply(target, args);
|
|
193
|
+
});
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
proto = Object.getPrototypeOf(proto);
|
|
197
|
+
}
|
|
198
|
+
let previousStore;
|
|
199
|
+
return {
|
|
200
|
+
run: (fn) => serviceLocatorStorage.run(serviceLocator, fn),
|
|
201
|
+
enterScope: () => {
|
|
202
|
+
previousStore = serviceLocatorStorage.getStore();
|
|
203
|
+
serviceLocatorStorage.enterWith(serviceLocator);
|
|
204
|
+
},
|
|
205
|
+
exitScope: () => {
|
|
206
|
+
serviceLocatorStorage.enterWith(previousStore); // casting to any so that `undefined` is accepted - this "unsets" the AsyncLocalStorage
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
export const serviceLocator = {
|
|
211
|
+
getConfiguration() {
|
|
212
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
213
|
+
return currentServiceLocator.getConfiguration();
|
|
214
|
+
},
|
|
215
|
+
setConfiguration(configuration) {
|
|
216
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
217
|
+
currentServiceLocator.setConfiguration(configuration);
|
|
218
|
+
},
|
|
219
|
+
getEventManager() {
|
|
220
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
221
|
+
return currentServiceLocator.getEventManager();
|
|
222
|
+
},
|
|
223
|
+
setEventManager(eventManager) {
|
|
224
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
225
|
+
currentServiceLocator.setEventManager(eventManager);
|
|
226
|
+
},
|
|
227
|
+
getStorageClient() {
|
|
228
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
229
|
+
return currentServiceLocator.getStorageClient();
|
|
230
|
+
},
|
|
231
|
+
setStorageClient(storageClient) {
|
|
232
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
233
|
+
currentServiceLocator.setStorageClient(storageClient);
|
|
234
|
+
},
|
|
235
|
+
getStorageManager(constructor) {
|
|
236
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
237
|
+
return currentServiceLocator.getStorageManager(constructor);
|
|
238
|
+
},
|
|
239
|
+
setStorageManager(constructor, storageManager) {
|
|
240
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
241
|
+
currentServiceLocator.setStorageManager(constructor, storageManager);
|
|
242
|
+
},
|
|
243
|
+
clearStorageManagerCache() {
|
|
244
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
245
|
+
currentServiceLocator.clearStorageManagerCache();
|
|
246
|
+
},
|
|
247
|
+
reset() {
|
|
248
|
+
const currentServiceLocator = serviceLocatorStorage.getStore() ?? globalServiceLocator;
|
|
249
|
+
currentServiceLocator.reset();
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
//# sourceMappingURL=service_locator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service_locator.js","sourceRoot":"","sources":["../src/service_locator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,GAAG,MAAM,YAAY,CAAC;AAE7B,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAiEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,OAAO,cAAc;IACf,aAAa,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,CAAC;IACD,iBAAiB,CAAC,WAAkC,EAAE,cAA8B;QAChF,MAAM,qBAAqB,GAAG,qBAAqB,CAAC,QAAQ,EAAE,IAAI,oBAAoB,CAAC;QACvF,qBAAqB,CAAC,iBAAiB,CAAC,WAAW,EAAE,cAAc,CAAC,CAAC;IACzE,CAAC;IACD,wBAAwB;QACpB,MAAM,qBAAqB,GAAG,qBAAqB,CAAC,QAAQ,EAAE,IAAI,oBAAoB,CAAC;QACvF,qBAAqB,CAAC,wBAAwB,EAAE,CAAC;IACrD,CAAC;IACD,KAAK;QACD,MAAM,qBAAqB,GAAG,qBAAqB,CAAC,QAAQ,EAAE,IAAI,oBAAoB,CAAC;QACvF,qBAAqB,CAAC,KAAK,EAAE,CAAC;IAClC,CAAC;CACJ,CAAC"}
|
|
@@ -1,23 +1,7 @@
|
|
|
1
|
-
import type { Cookie as CookieObject, Dictionary } from '@crawlee/types';
|
|
2
|
-
import type { Cookie
|
|
1
|
+
import type { Cookie as CookieObject, Dictionary, ISession, ProxyInfo, SessionState } from '@crawlee/types';
|
|
2
|
+
import type { Cookie } from 'tough-cookie';
|
|
3
3
|
import { CookieJar } from 'tough-cookie';
|
|
4
4
|
import type { Log } from '@apify/log';
|
|
5
|
-
import type { ResponseLike } from '../cookie_utils.js';
|
|
6
|
-
/**
|
|
7
|
-
* Persistable {@link Session} state.
|
|
8
|
-
*/
|
|
9
|
-
export interface SessionState {
|
|
10
|
-
id: string;
|
|
11
|
-
cookieJar: SerializedCookieJar;
|
|
12
|
-
userData: object;
|
|
13
|
-
errorScore: number;
|
|
14
|
-
maxErrorScore: number;
|
|
15
|
-
errorScoreDecrement: number;
|
|
16
|
-
usageCount: number;
|
|
17
|
-
maxUsageCount: number;
|
|
18
|
-
expiresAt: string;
|
|
19
|
-
createdAt: string;
|
|
20
|
-
}
|
|
21
5
|
export interface SessionOptions {
|
|
22
6
|
/** Id of session used for generating fingerprints. It is used as proxy session name. */
|
|
23
7
|
id?: string;
|
|
@@ -63,6 +47,7 @@ export interface SessionOptions {
|
|
|
63
47
|
log?: Log;
|
|
64
48
|
errorScore?: number;
|
|
65
49
|
cookieJar?: CookieJar;
|
|
50
|
+
proxyInfo?: ProxyInfo;
|
|
66
51
|
}
|
|
67
52
|
/**
|
|
68
53
|
* Sessions are used to store information such as cookies and can be used for generating fingerprints and proxy sessions.
|
|
@@ -70,7 +55,7 @@ export interface SessionOptions {
|
|
|
70
55
|
* Session internal state can be enriched with custom user data for example some authorization tokens and specific headers in general.
|
|
71
56
|
* @category Scaling
|
|
72
57
|
*/
|
|
73
|
-
export declare class Session {
|
|
58
|
+
export declare class Session implements ISession {
|
|
74
59
|
readonly id: string;
|
|
75
60
|
private maxAgeSecs;
|
|
76
61
|
userData: Dictionary;
|
|
@@ -82,6 +67,7 @@ export declare class Session {
|
|
|
82
67
|
private _maxUsageCount;
|
|
83
68
|
private sessionPool;
|
|
84
69
|
private _errorScore;
|
|
70
|
+
private _proxyInfo?;
|
|
85
71
|
private _cookieJar;
|
|
86
72
|
private log;
|
|
87
73
|
get errorScore(): number;
|
|
@@ -92,6 +78,7 @@ export declare class Session {
|
|
|
92
78
|
get createdAt(): Date;
|
|
93
79
|
get maxUsageCount(): number;
|
|
94
80
|
get cookieJar(): CookieJar;
|
|
81
|
+
get proxyInfo(): ProxyInfo | undefined;
|
|
95
82
|
/**
|
|
96
83
|
* Session configuration.
|
|
97
84
|
*/
|
|
@@ -156,7 +143,7 @@ export declare class Session {
|
|
|
156
143
|
*
|
|
157
144
|
* It then parses and saves the cookies from the `set-cookie` header, if available.
|
|
158
145
|
*/
|
|
159
|
-
setCookiesFromResponse(response:
|
|
146
|
+
setCookiesFromResponse(response: Response): void;
|
|
160
147
|
/**
|
|
161
148
|
* Saves an array with cookie objects to be used with the session.
|
|
162
149
|
* The objects should be in the format that
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/session_pool/session.ts"],"names":[],"mappings":"AAEA,OA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;;;;;OAKG;IACH,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIpC;;OAEG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAQ/C;;OAEG;IACH,SAAS,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAkB3D;;OAEG;IACH,SAAS,CAAC,gBAAgB,IAAI,IAAI;CAKrC"}
|
package/session_pool/session.js
CHANGED
|
@@ -23,6 +23,7 @@ export class Session {
|
|
|
23
23
|
_maxUsageCount;
|
|
24
24
|
sessionPool;
|
|
25
25
|
_errorScore;
|
|
26
|
+
_proxyInfo;
|
|
26
27
|
_cookieJar;
|
|
27
28
|
log;
|
|
28
29
|
get errorScore() {
|
|
@@ -49,6 +50,9 @@ export class Session {
|
|
|
49
50
|
get cookieJar() {
|
|
50
51
|
return this._cookieJar;
|
|
51
52
|
}
|
|
53
|
+
get proxyInfo() {
|
|
54
|
+
return this._proxyInfo;
|
|
55
|
+
}
|
|
52
56
|
/**
|
|
53
57
|
* Session configuration.
|
|
54
58
|
*/
|
|
@@ -57,6 +61,7 @@ export class Session {
|
|
|
57
61
|
sessionPool: ow.object.instanceOf(EventEmitter),
|
|
58
62
|
id: ow.optional.string,
|
|
59
63
|
cookieJar: ow.optional.object,
|
|
64
|
+
proxyInfo: ow.optional.object,
|
|
60
65
|
maxAgeSecs: ow.optional.number,
|
|
61
66
|
userData: ow.optional.object,
|
|
62
67
|
maxErrorScore: ow.optional.number,
|
|
@@ -68,10 +73,11 @@ export class Session {
|
|
|
68
73
|
maxUsageCount: ow.optional.number,
|
|
69
74
|
log: ow.optional.object,
|
|
70
75
|
}));
|
|
71
|
-
const { sessionPool, id = `session_${cryptoRandomObjectId(10)}`, cookieJar = new CookieJar(), maxAgeSecs = 3000, userData = {}, maxErrorScore = 3, errorScoreDecrement = 0.5, createdAt = new Date(), usageCount = 0, errorScore = 0, maxUsageCount = 50, log = defaultLog, } = options;
|
|
76
|
+
const { sessionPool, 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, log = defaultLog, } = options;
|
|
72
77
|
const { expiresAt = getDefaultCookieExpirationDate(maxAgeSecs) } = options;
|
|
73
78
|
this.log = log.child({ prefix: 'Session' });
|
|
74
79
|
this._cookieJar = cookieJar.setCookie ? cookieJar : CookieJar.fromJSON(JSON.stringify(cookieJar));
|
|
80
|
+
this._proxyInfo = proxyInfo;
|
|
75
81
|
this.id = id;
|
|
76
82
|
this.maxAgeSecs = maxAgeSecs;
|
|
77
83
|
this.userData = userData;
|
|
@@ -133,6 +139,7 @@ export class Session {
|
|
|
133
139
|
return {
|
|
134
140
|
id: this.id,
|
|
135
141
|
cookieJar: this.cookieJar.toJSON(),
|
|
142
|
+
proxyInfo: this._proxyInfo,
|
|
136
143
|
userData: this.userData,
|
|
137
144
|
maxErrorScore: this.maxErrorScore,
|
|
138
145
|
errorScoreDecrement: this.errorScoreDecrement,
|
|
@@ -192,7 +199,7 @@ export class Session {
|
|
|
192
199
|
setCookiesFromResponse(response) {
|
|
193
200
|
try {
|
|
194
201
|
const cookies = getCookiesFromResponse(response).filter((c) => c);
|
|
195
|
-
this._setCookies(cookies,
|
|
202
|
+
this._setCookies(cookies, response.url);
|
|
196
203
|
}
|
|
197
204
|
catch (e) {
|
|
198
205
|
const err = e;
|
|
@@ -238,7 +245,12 @@ export class Session {
|
|
|
238
245
|
* Sets a cookie within this session for the specific URL.
|
|
239
246
|
*/
|
|
240
247
|
setCookie(rawCookie, url) {
|
|
241
|
-
|
|
248
|
+
try {
|
|
249
|
+
this.cookieJar.setCookieSync(rawCookie, url);
|
|
250
|
+
}
|
|
251
|
+
catch (e) {
|
|
252
|
+
this.log.warning('Could not set cookie.', { url, error: e.message });
|
|
253
|
+
}
|
|
242
254
|
}
|
|
243
255
|
/**
|
|
244
256
|
* Sets cookies.
|
|
@@ -256,7 +268,7 @@ export class Session {
|
|
|
256
268
|
}
|
|
257
269
|
// if invalid cookies are provided just log the exception. No need to retry the request automatically.
|
|
258
270
|
if (errorMessages.length) {
|
|
259
|
-
this.log.
|
|
271
|
+
this.log.warning('Could not set cookies.', { errorMessages });
|
|
260
272
|
}
|
|
261
273
|
}
|
|
262
274
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session_pool/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session_pool/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAC;AAExD,OAAO,EACH,8BAA8B,EAC9B,sBAAsB,EACtB,8BAA8B,EAC9B,8BAA8B,GACjC,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,GAAG,IAAI,UAAU,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AA4DpD;;;;;GAKG;AACH,MAAM,OAAO,OAAO;IACP,EAAE,CAAS;IACZ,UAAU,CAAS;IAC3B,QAAQ,CAAa;IACb,cAAc,CAAS;IACvB,oBAAoB,CAAS;IAC7B,UAAU,CAAO;IACjB,UAAU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;IACL,CAAC;IAED;;OAEG;IACO,WAAW,CAAC,OAAiB,EAAE,GAAW;QAChD,MAAM,aAAa,GAAa,EAAE,CAAC;QAEnC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE,CAAC;YAC3B,IAAI,CAAC;gBACD,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAC;YACtE,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACT,MAAM,GAAG,GAAG,CAAU,CAAC;gBACvB,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YACpC,CAAC;QACL,CAAC;QAED,sGAAsG;QACtG,IAAI,aAAa,CAAC,MAAM,EAAE,CAAC;YACvB,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,wBAAwB,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC;QAClE,CAAC;IACL,CAAC;IAED;;OAEG;IACO,gBAAgB;QACtB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;YACnB,IAAI,CAAC,MAAM,EAAE,CAAC;QAClB,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events';
|
|
2
2
|
import type { Log } from '@apify/log';
|
|
3
|
-
import { Configuration } from '../configuration.js';
|
|
4
3
|
import type { PersistenceOptions } from '../crawlers/statistics.js';
|
|
5
4
|
import type { EventManager } from '../events/event_manager.js';
|
|
6
5
|
import { KeyValueStore } from '../storages/key_value_store.js';
|
|
@@ -34,7 +33,7 @@ export interface SessionPoolOptions {
|
|
|
34
33
|
*/
|
|
35
34
|
persistStateKey?: string;
|
|
36
35
|
/**
|
|
37
|
-
* Custom function that should return `Session` instance.
|
|
36
|
+
* Custom function that should return a `Session` instance, or a promise resolving to such instance.
|
|
38
37
|
* Any error thrown from this function will terminate the process.
|
|
39
38
|
* Function receives `SessionPool` instance as a parameter
|
|
40
39
|
*/
|
|
@@ -118,7 +117,6 @@ export interface SessionPoolOptions {
|
|
|
118
117
|
* @category Scaling
|
|
119
118
|
*/
|
|
120
119
|
export declare class SessionPool extends EventEmitter {
|
|
121
|
-
readonly config: Configuration;
|
|
122
120
|
protected log: Log;
|
|
123
121
|
protected maxPoolSize: number;
|
|
124
122
|
protected createSessionFunction: CreateSession;
|
|
@@ -137,7 +135,7 @@ export declare class SessionPool extends EventEmitter {
|
|
|
137
135
|
/**
|
|
138
136
|
* @internal
|
|
139
137
|
*/
|
|
140
|
-
constructor(options?: SessionPoolOptions
|
|
138
|
+
constructor(options?: SessionPoolOptions);
|
|
141
139
|
/**
|
|
142
140
|
* Gets count of usable sessions in the pool.
|
|
143
141
|
*/
|
|
@@ -158,6 +156,13 @@ export declare class SessionPool extends EventEmitter {
|
|
|
158
156
|
* @param [options] The configuration options for the session being added to the session pool.
|
|
159
157
|
*/
|
|
160
158
|
addSession(options?: Session | SessionOptions): Promise<void>;
|
|
159
|
+
/**
|
|
160
|
+
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
161
|
+
* but this allows you to add more sessions once the max pool size is reached.
|
|
162
|
+
* This also allows you to add session with overridden session options (e.g. with specific session id).
|
|
163
|
+
* @param [options] The configuration options for the session being added to the session pool.
|
|
164
|
+
*/
|
|
165
|
+
newSession(sessionOptions?: SessionOptions): Promise<Session>;
|
|
161
166
|
/**
|
|
162
167
|
* Gets session.
|
|
163
168
|
* If there is space for new session, it creates and returns new session.
|
|
@@ -181,7 +186,7 @@ export declare class SessionPool extends EventEmitter {
|
|
|
181
186
|
usableSessionsCount: number;
|
|
182
187
|
retiredSessionsCount: number;
|
|
183
188
|
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
184
|
-
sessions: import("
|
|
189
|
+
sessions: import("@crawlee/types").SessionState[];
|
|
185
190
|
};
|
|
186
191
|
/**
|
|
187
192
|
* Persists the current state of the `SessionPool` into the default {@link KeyValueStore}.
|
|
@@ -220,7 +225,7 @@ export declare class SessionPool extends EventEmitter {
|
|
|
220
225
|
*/
|
|
221
226
|
protected _defaultCreateSessionFunction(sessionPool: SessionPool, options?: {
|
|
222
227
|
sessionOptions?: SessionOptions;
|
|
223
|
-
}): Session
|
|
228
|
+
}): Promise<Session>;
|
|
224
229
|
/**
|
|
225
230
|
* Creates new session and adds it to the pool.
|
|
226
231
|
* @returns Newly created `Session` instance.
|
|
@@ -246,6 +251,6 @@ export declare class SessionPool extends EventEmitter {
|
|
|
246
251
|
*
|
|
247
252
|
* For more details and code examples, see the {@link SessionPool} class.
|
|
248
253
|
*/
|
|
249
|
-
static open(options?: SessionPoolOptions
|
|
254
|
+
static open(options?: SessionPoolOptions): Promise<SessionPool>;
|
|
250
255
|
}
|
|
251
256
|
//# sourceMappingURL=session_pool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session_pool.d.ts","sourceRoot":"","sources":["../../src/session_pool/session_pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAM3C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,
|
|
1
|
+
{"version":3,"file":"session_pool.d.ts","sourceRoot":"","sources":["../../src/session_pool/session_pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAM3C,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AAEtC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAI/D,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC;;GAEG;AACH,M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AAE,cAAc,CAAC,EAAE,cAAc,CAAA;KAAO,GAClD,OAAO,CAAC,OAAO,CAAC;IAWnB;;;OAGG;cACa,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;IAQlD;;OAEG;IACH,SAAS,CAAC,mBAAmB,IAAI,OAAO;IAIxC;;;OAGG;IACH,SAAS,CAAC,YAAY,IAAI,OAAO;IAIjC;;;OAGG;cACa,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC;IAyBtD;;;;;OAKG;WACU,IAAI,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,OAAO,CAAC,WAAW,CAAC;CAKxE"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { EventEmitter } from 'node:events';
|
|
2
2
|
import { AsyncQueue } from '@sapphire/async-queue';
|
|
3
3
|
import ow from 'ow';
|
|
4
|
-
import { Configuration } from '../configuration.js';
|
|
5
4
|
import { log as defaultLog } from '../log.js';
|
|
5
|
+
import { serviceLocator } from '../service_locator.js';
|
|
6
6
|
import { KeyValueStore } from '../storages/key_value_store.js';
|
|
7
7
|
import { BLOCKED_STATUS_CODES, MAX_POOL_SIZE, PERSIST_STATE_KEY } from './consts.js';
|
|
8
8
|
import { Session } from './session.js';
|
|
@@ -72,7 +72,6 @@ import { Session } from './session.js';
|
|
|
72
72
|
* @category Scaling
|
|
73
73
|
*/
|
|
74
74
|
export class SessionPool extends EventEmitter {
|
|
75
|
-
config;
|
|
76
75
|
log;
|
|
77
76
|
maxPoolSize;
|
|
78
77
|
createSessionFunction;
|
|
@@ -91,9 +90,8 @@ export class SessionPool extends EventEmitter {
|
|
|
91
90
|
/**
|
|
92
91
|
* @internal
|
|
93
92
|
*/
|
|
94
|
-
constructor(options = {}
|
|
93
|
+
constructor(options = {}) {
|
|
95
94
|
super();
|
|
96
|
-
this.config = config;
|
|
97
95
|
ow(options, ow.object.exactShape({
|
|
98
96
|
maxPoolSize: ow.optional.number,
|
|
99
97
|
persistStateKeyValueStoreId: ow.optional.string,
|
|
@@ -107,9 +105,8 @@ export class SessionPool extends EventEmitter {
|
|
|
107
105
|
const { maxPoolSize = MAX_POOL_SIZE, persistStateKeyValueStoreId, persistStateKey = PERSIST_STATE_KEY, createSessionFunction, sessionOptions = {}, blockedStatusCodes = BLOCKED_STATUS_CODES, log = defaultLog, persistenceOptions = {
|
|
108
106
|
enable: true,
|
|
109
107
|
}, } = options;
|
|
110
|
-
this.config = config;
|
|
111
108
|
this.blockedStatusCodes = blockedStatusCodes;
|
|
112
|
-
this.events =
|
|
109
|
+
this.events = serviceLocator.getEventManager();
|
|
113
110
|
this.log = log.child({ prefix: 'SessionPool' });
|
|
114
111
|
this.persistenceOptions = persistenceOptions;
|
|
115
112
|
// Pool Configuration
|
|
@@ -146,7 +143,9 @@ export class SessionPool extends EventEmitter {
|
|
|
146
143
|
if (this.isInitialized) {
|
|
147
144
|
return;
|
|
148
145
|
}
|
|
149
|
-
this.keyValueStore = await KeyValueStore.open(this.persistStateKeyValueStoreId, {
|
|
146
|
+
this.keyValueStore = await KeyValueStore.open(this.persistStateKeyValueStoreId, {
|
|
147
|
+
config: serviceLocator.getConfiguration(),
|
|
148
|
+
});
|
|
150
149
|
if (!this.persistenceOptions.enable) {
|
|
151
150
|
this.isInitialized = true;
|
|
152
151
|
return;
|
|
@@ -182,6 +181,18 @@ export class SessionPool extends EventEmitter {
|
|
|
182
181
|
this.log.debug(`Adding new Session - ${newSession.id}`);
|
|
183
182
|
this._addSession(newSession);
|
|
184
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
186
|
+
* but this allows you to add more sessions once the max pool size is reached.
|
|
187
|
+
* This also allows you to add session with overridden session options (e.g. with specific session id).
|
|
188
|
+
* @param [options] The configuration options for the session being added to the session pool.
|
|
189
|
+
*/
|
|
190
|
+
async newSession(sessionOptions) {
|
|
191
|
+
this._throwIfNotInitialized();
|
|
192
|
+
const newSession = await this.createSessionFunction(this, { sessionOptions });
|
|
193
|
+
this._addSession(newSession);
|
|
194
|
+
return newSession;
|
|
195
|
+
}
|
|
185
196
|
/**
|
|
186
197
|
* Gets session.
|
|
187
198
|
* If there is space for new session, it creates and returns new session.
|
|
@@ -247,7 +258,7 @@ export class SessionPool extends EventEmitter {
|
|
|
247
258
|
persistStateKey: this.persistStateKey,
|
|
248
259
|
});
|
|
249
260
|
// use half the interval of `persistState` to avoid race conditions
|
|
250
|
-
const persistStateIntervalMillis =
|
|
261
|
+
const persistStateIntervalMillis = serviceLocator.getConfiguration().get('persistStateIntervalMillis');
|
|
251
262
|
const timeoutSecs = persistStateIntervalMillis / 2_000;
|
|
252
263
|
await this.keyValueStore
|
|
253
264
|
.setValue(this.persistStateKey, this.getState(), {
|
|
@@ -304,7 +315,7 @@ export class SessionPool extends EventEmitter {
|
|
|
304
315
|
* @param [options.sessionOptions] The configuration options for the session being created.
|
|
305
316
|
* @returns New session.
|
|
306
317
|
*/
|
|
307
|
-
_defaultCreateSessionFunction(sessionPool, options = {}) {
|
|
318
|
+
async _defaultCreateSessionFunction(sessionPool, options = {}) {
|
|
308
319
|
ow(options, ow.object.exactShape({ sessionOptions: ow.optional.object }));
|
|
309
320
|
const { sessionOptions = {} } = options;
|
|
310
321
|
return new Session({
|
|
@@ -366,8 +377,8 @@ export class SessionPool extends EventEmitter {
|
|
|
366
377
|
*
|
|
367
378
|
* For more details and code examples, see the {@link SessionPool} class.
|
|
368
379
|
*/
|
|
369
|
-
static async open(options
|
|
370
|
-
const sessionPool = new SessionPool(options
|
|
380
|
+
static async open(options) {
|
|
381
|
+
const sessionPool = new SessionPool(options);
|
|
371
382
|
await sessionPool.initialize();
|
|
372
383
|
return sessionPool;
|
|
373
384
|
}
|