@crawlee/core 4.0.0-beta.21 → 4.0.0-beta.211
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 +14 -14
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +88 -227
- package/configuration.js +162 -226
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/errors.d.ts +45 -18
- package/errors.js +58 -27
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +12 -13
- package/events/index.d.ts +1 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +39 -13
- package/events/system_info.d.ts +38 -0
- package/events/system_info.js +1 -0
- package/index.d.ts +7 -11
- package/index.js +8 -10
- package/internal.d.ts +8 -0
- package/internal.js +9 -0
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +77 -1
- package/log.js +134 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +43 -0
- package/memory-storage/memory-storage.js +158 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +106 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +56 -0
- package/memory-storage/resource-clients/key-value-store.js +180 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +414 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +19 -20
- package/proxy_configuration.d.ts +28 -77
- package/proxy_configuration.js +37 -146
- package/recoverable_state.d.ts +106 -52
- package/recoverable_state.js +200 -72
- package/request.d.ts +48 -93
- package/request.js +112 -230
- package/serialization.d.ts +0 -1
- package/serialization.js +18 -18
- package/service_locator.d.ts +168 -0
- package/service_locator.js +255 -0
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +115 -58
- package/storages/dataset.js +291 -149
- package/storages/index.d.ts +8 -9
- package/storages/index.js +4 -9
- package/storages/key_value_store.d.ts +186 -71
- package/storages/key_value_store.js +449 -194
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +50 -133
- package/storages/request_list.js +269 -248
- package/storages/request_loader.d.ts +127 -0
- package/storages/request_loader.js +36 -0
- package/storages/request_manager.d.ts +119 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +50 -24
- package/storages/request_manager_tandem.js +134 -58
- package/storages/request_queue.d.ts +293 -48
- package/storages/request_queue.js +766 -216
- package/storages/storage_instance_manager.d.ts +86 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/transaction.d.ts +271 -0
- package/storages/transaction.js +296 -0
- package/storages/utils.d.ts +59 -11
- package/storages/utils.js +75 -15
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +23 -18
- package/validators.js +14 -18
- package/autoscaling/autoscaled_pool.d.ts +0 -300
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js +0 -533
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts +0 -4
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js +0 -4
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts +0 -189
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js +0 -285
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts +0 -181
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js +0 -169
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts +0 -43
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js +0 -111
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts +0 -61
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js +0 -99
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts +0 -194
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js +0 -108
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts +0 -60
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js +0 -118
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts +0 -55
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js +0 -309
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts +0 -7
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js +0 -7
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts +0 -8
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js +0 -2
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts +0 -213
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js +0 -349
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts +0 -249
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js +0 -269
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts +0 -3
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js +0 -3
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts +0 -73
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js +0 -212
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -12
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -44
- 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 -22
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -116
- 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/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/recoverable_state.d.ts.map +0 -1
- package/recoverable_state.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts +0 -134
- package/router.d.ts.map +0 -1
- package/router.js +0 -164
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts +0 -4
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js +0 -4
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts +0 -8
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js +0 -12
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts +0 -6
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js +0 -6
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts +0 -186
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js +0 -278
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts +0 -258
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js +0 -387
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_list_adapter.d.ts +0 -58
- package/storages/request_list_adapter.d.ts.map +0 -1
- package/storages/request_list_adapter.js +0 -81
- package/storages/request_list_adapter.js.map +0 -1
- package/storages/request_manager_tandem.d.ts.map +0 -1
- package/storages/request_manager_tandem.js.map +0 -1
- package/storages/request_provider.d.ts +0 -372
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -591
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts +0 -250
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js +0 -431
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -62
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
package/session_pool/session.js
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import ow from 'ow';
|
|
3
|
-
import { CookieJar } from 'tough-cookie';
|
|
4
|
-
import { cryptoRandomObjectId } from '@apify/utilities';
|
|
5
|
-
import { browserPoolCookieToToughCookie, getCookiesFromResponse, getDefaultCookieExpirationDate, toughCookieToBrowserPoolCookie, } from '../cookie_utils.js';
|
|
6
|
-
import { log as defaultLog } from '../log.js';
|
|
7
|
-
import { EVENT_SESSION_RETIRED } from './events.js';
|
|
8
|
-
/**
|
|
9
|
-
* Sessions are used to store information such as cookies and can be used for generating fingerprints and proxy sessions.
|
|
10
|
-
* You can imagine each session as a specific user, with its own cookies, IP (via proxy) and potentially a unique browser fingerprint.
|
|
11
|
-
* Session internal state can be enriched with custom user data for example some authorization tokens and specific headers in general.
|
|
12
|
-
* @category Scaling
|
|
13
|
-
*/
|
|
14
|
-
export class Session {
|
|
15
|
-
id;
|
|
16
|
-
maxAgeSecs;
|
|
17
|
-
userData;
|
|
18
|
-
_maxErrorScore;
|
|
19
|
-
_errorScoreDecrement;
|
|
20
|
-
_createdAt;
|
|
21
|
-
_expiresAt;
|
|
22
|
-
_usageCount;
|
|
23
|
-
_maxUsageCount;
|
|
24
|
-
sessionPool;
|
|
25
|
-
_errorScore;
|
|
26
|
-
_proxyInfo;
|
|
27
|
-
_cookieJar;
|
|
28
|
-
log;
|
|
29
|
-
get errorScore() {
|
|
30
|
-
return this._errorScore;
|
|
31
|
-
}
|
|
32
|
-
get usageCount() {
|
|
33
|
-
return this._usageCount;
|
|
34
|
-
}
|
|
35
|
-
get maxErrorScore() {
|
|
36
|
-
return this._maxErrorScore;
|
|
37
|
-
}
|
|
38
|
-
get errorScoreDecrement() {
|
|
39
|
-
return this._errorScoreDecrement;
|
|
40
|
-
}
|
|
41
|
-
get expiresAt() {
|
|
42
|
-
return this._expiresAt;
|
|
43
|
-
}
|
|
44
|
-
get createdAt() {
|
|
45
|
-
return this._createdAt;
|
|
46
|
-
}
|
|
47
|
-
get maxUsageCount() {
|
|
48
|
-
return this._maxUsageCount;
|
|
49
|
-
}
|
|
50
|
-
get cookieJar() {
|
|
51
|
-
return this._cookieJar;
|
|
52
|
-
}
|
|
53
|
-
get proxyInfo() {
|
|
54
|
-
return this._proxyInfo;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Session configuration.
|
|
58
|
-
*/
|
|
59
|
-
constructor(options) {
|
|
60
|
-
ow(options, ow.object.exactShape({
|
|
61
|
-
sessionPool: ow.object.instanceOf(EventEmitter),
|
|
62
|
-
id: ow.optional.string,
|
|
63
|
-
cookieJar: ow.optional.object,
|
|
64
|
-
proxyInfo: ow.optional.object,
|
|
65
|
-
maxAgeSecs: ow.optional.number,
|
|
66
|
-
userData: ow.optional.object,
|
|
67
|
-
maxErrorScore: ow.optional.number,
|
|
68
|
-
errorScoreDecrement: ow.optional.number,
|
|
69
|
-
createdAt: ow.optional.date,
|
|
70
|
-
expiresAt: ow.optional.date,
|
|
71
|
-
usageCount: ow.optional.number,
|
|
72
|
-
errorScore: ow.optional.number,
|
|
73
|
-
maxUsageCount: ow.optional.number,
|
|
74
|
-
log: ow.optional.object,
|
|
75
|
-
}));
|
|
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;
|
|
77
|
-
const { expiresAt = getDefaultCookieExpirationDate(maxAgeSecs) } = options;
|
|
78
|
-
this.log = log.child({ prefix: 'Session' });
|
|
79
|
-
this._cookieJar = cookieJar.setCookie ? cookieJar : CookieJar.fromJSON(JSON.stringify(cookieJar));
|
|
80
|
-
this._proxyInfo = proxyInfo;
|
|
81
|
-
this.id = id;
|
|
82
|
-
this.maxAgeSecs = maxAgeSecs;
|
|
83
|
-
this.userData = userData;
|
|
84
|
-
this._maxErrorScore = maxErrorScore;
|
|
85
|
-
this._errorScoreDecrement = errorScoreDecrement;
|
|
86
|
-
// Internal
|
|
87
|
-
this._expiresAt = expiresAt;
|
|
88
|
-
this._createdAt = createdAt;
|
|
89
|
-
this._usageCount = usageCount; // indicates how many times the session has been used
|
|
90
|
-
this._errorScore = errorScore; // indicates number of markBaded request with the session
|
|
91
|
-
this._maxUsageCount = maxUsageCount;
|
|
92
|
-
this.sessionPool = sessionPool;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Indicates whether the session is blocked.
|
|
96
|
-
* Session is blocked once it reaches the `maxErrorScore`.
|
|
97
|
-
*/
|
|
98
|
-
isBlocked() {
|
|
99
|
-
return this.errorScore >= this.maxErrorScore;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Indicates whether the session is expired.
|
|
103
|
-
* Session expiration is determined by the `maxAgeSecs`.
|
|
104
|
-
* Once the session is older than `createdAt + maxAgeSecs` the session is considered expired.
|
|
105
|
-
*/
|
|
106
|
-
isExpired() {
|
|
107
|
-
return this.expiresAt <= new Date();
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Indicates whether the session is used maximum number of times.
|
|
111
|
-
* Session maximum usage count can be changed by `maxUsageCount` parameter.
|
|
112
|
-
*/
|
|
113
|
-
isMaxUsageCountReached() {
|
|
114
|
-
return this.usageCount >= this.maxUsageCount;
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Indicates whether the session can be used for next requests.
|
|
118
|
-
* Session is usable when it is not expired, not blocked and the maximum usage count has not be reached.
|
|
119
|
-
*/
|
|
120
|
-
isUsable() {
|
|
121
|
-
return !this.isBlocked() && !this.isExpired() && !this.isMaxUsageCountReached();
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* This method should be called after a successful session usage.
|
|
125
|
-
* It increases `usageCount` and potentially lowers the `errorScore` by the `errorScoreDecrement`.
|
|
126
|
-
*/
|
|
127
|
-
markGood() {
|
|
128
|
-
this._usageCount += 1;
|
|
129
|
-
if (this._errorScore > 0) {
|
|
130
|
-
this._errorScore -= this._errorScoreDecrement;
|
|
131
|
-
}
|
|
132
|
-
this._maybeSelfRetire();
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Gets session state for persistence in KeyValueStore.
|
|
136
|
-
* @returns Represents session internal state.
|
|
137
|
-
*/
|
|
138
|
-
getState() {
|
|
139
|
-
return {
|
|
140
|
-
id: this.id,
|
|
141
|
-
cookieJar: this.cookieJar.toJSON(),
|
|
142
|
-
proxyInfo: this._proxyInfo,
|
|
143
|
-
userData: this.userData,
|
|
144
|
-
maxErrorScore: this.maxErrorScore,
|
|
145
|
-
errorScoreDecrement: this.errorScoreDecrement,
|
|
146
|
-
expiresAt: this.expiresAt.toISOString(),
|
|
147
|
-
createdAt: this.createdAt.toISOString(),
|
|
148
|
-
usageCount: this.usageCount,
|
|
149
|
-
maxUsageCount: this.maxUsageCount,
|
|
150
|
-
errorScore: this.errorScore,
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Marks session as blocked and emits event on the `SessionPool`
|
|
155
|
-
* This method should be used if the session usage was unsuccessful
|
|
156
|
-
* and you are sure that it is because of the session configuration and not any external matters.
|
|
157
|
-
* For example when server returns 403 status code.
|
|
158
|
-
* If the session does not work due to some external factors as server error such as 5XX you probably want to use `markBad` method.
|
|
159
|
-
*/
|
|
160
|
-
retire() {
|
|
161
|
-
// mark it as an invalid by increasing the error score count.
|
|
162
|
-
this._errorScore += this._maxErrorScore;
|
|
163
|
-
this._usageCount += 1;
|
|
164
|
-
// emit event so we can retire browser in puppeteer pool
|
|
165
|
-
this.sessionPool.emit(EVENT_SESSION_RETIRED, this);
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Increases usage and error count.
|
|
169
|
-
* Should be used when the session has been used unsuccessfully. For example because of timeouts.
|
|
170
|
-
*/
|
|
171
|
-
markBad() {
|
|
172
|
-
this._errorScore += 1;
|
|
173
|
-
this._usageCount += 1;
|
|
174
|
-
this._maybeSelfRetire();
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* With certain status codes: `401`, `403` or `429` we can be certain
|
|
178
|
-
* that the target website is blocking us. This function helps to do this conveniently
|
|
179
|
-
* by retiring the session when such code is received. Optionally, the default status
|
|
180
|
-
* codes can be extended in the second parameter.
|
|
181
|
-
* @param statusCode HTTP status code.
|
|
182
|
-
* @returns Whether the session was retired.
|
|
183
|
-
*/
|
|
184
|
-
retireOnBlockedStatusCodes(statusCode) {
|
|
185
|
-
// eslint-disable-next-line dot-notation -- accessing private property
|
|
186
|
-
const isBlocked = this.sessionPool['blockedStatusCodes'].includes(statusCode);
|
|
187
|
-
if (isBlocked) {
|
|
188
|
-
this.retire();
|
|
189
|
-
}
|
|
190
|
-
return isBlocked;
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Saves cookies from an HTTP response to be used with the session.
|
|
194
|
-
* It expects an object with a `headers` property that's either an `Object`
|
|
195
|
-
* (typical Node.js responses) or a `Function` (Puppeteer Response).
|
|
196
|
-
*
|
|
197
|
-
* It then parses and saves the cookies from the `set-cookie` header, if available.
|
|
198
|
-
*/
|
|
199
|
-
setCookiesFromResponse(response) {
|
|
200
|
-
try {
|
|
201
|
-
const cookies = getCookiesFromResponse(response).filter((c) => c);
|
|
202
|
-
this._setCookies(cookies, response.url);
|
|
203
|
-
}
|
|
204
|
-
catch (e) {
|
|
205
|
-
const err = e;
|
|
206
|
-
// if invalid Cookie header is provided just log the exception.
|
|
207
|
-
this.log.exception(err, 'Could not get cookies from response');
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Saves an array with cookie objects to be used with the session.
|
|
212
|
-
* The objects should be in the format that
|
|
213
|
-
* [Puppeteer uses](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls),
|
|
214
|
-
* but you can also use this function to set cookies manually:
|
|
215
|
-
*
|
|
216
|
-
* ```
|
|
217
|
-
* [
|
|
218
|
-
* { name: 'cookie1', value: 'my-cookie' },
|
|
219
|
-
* { name: 'cookie2', value: 'your-cookie' }
|
|
220
|
-
* ]
|
|
221
|
-
* ```
|
|
222
|
-
*/
|
|
223
|
-
setCookies(cookies, url) {
|
|
224
|
-
const normalizedCookies = cookies.map((c) => browserPoolCookieToToughCookie(c, this.maxAgeSecs));
|
|
225
|
-
this._setCookies(normalizedCookies, url);
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Returns cookies in a format compatible with puppeteer/playwright and ready to be used with `page.setCookie`.
|
|
229
|
-
* @param url website url. Only cookies stored for this url will be returned
|
|
230
|
-
*/
|
|
231
|
-
getCookies(url) {
|
|
232
|
-
const cookies = this.cookieJar.getCookiesSync(url);
|
|
233
|
-
return cookies.map((c) => toughCookieToBrowserPoolCookie(c));
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Returns cookies saved with the session in the typical
|
|
237
|
-
* key1=value1; key2=value2 format, ready to be used in
|
|
238
|
-
* a cookie header or elsewhere.
|
|
239
|
-
* @returns Represents `Cookie` header.
|
|
240
|
-
*/
|
|
241
|
-
getCookieString(url) {
|
|
242
|
-
return this.cookieJar.getCookieStringSync(url, {});
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Sets a cookie within this session for the specific URL.
|
|
246
|
-
*/
|
|
247
|
-
setCookie(rawCookie, url) {
|
|
248
|
-
this.cookieJar.setCookieSync(rawCookie, url);
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Sets cookies.
|
|
252
|
-
*/
|
|
253
|
-
_setCookies(cookies, url) {
|
|
254
|
-
const errorMessages = [];
|
|
255
|
-
for (const cookie of cookies) {
|
|
256
|
-
try {
|
|
257
|
-
this.cookieJar.setCookieSync(cookie, url, { ignoreError: false });
|
|
258
|
-
}
|
|
259
|
-
catch (e) {
|
|
260
|
-
const err = e;
|
|
261
|
-
errorMessages.push(err.message);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
// if invalid cookies are provided just log the exception. No need to retry the request automatically.
|
|
265
|
-
if (errorMessages.length) {
|
|
266
|
-
this.log.debug('Could not set cookies.', { errorMessages });
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
/**
|
|
270
|
-
* Checks if session is not usable. if it is not retires the session.
|
|
271
|
-
*/
|
|
272
|
-
_maybeSelfRetire() {
|
|
273
|
-
if (!this.isUsable()) {
|
|
274
|
-
this.retire();
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
//# sourceMappingURL=session.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session_pool/session.ts"],"names":[],"mappings":"AAAA,OAAO,EA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O,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,8BAA8B,CAAC,CAAC,CAAC,CAAC,CAAC;IACjE,CAAC;IAED;;;;;OAKG;IACH,eAAe,CAAC,GAAW;QACvB,OAAO,IAAI,CAAC,SAAS,CAAC,mBAAmB,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IACvD,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,SAAiB,EAAE,GAAW;QACpC,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACjD,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,KAAK,CAAC,wBAAwB,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC;QAChE,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,258 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'node:events';
|
|
2
|
-
import type { Log } from '@apify/log';
|
|
3
|
-
import { Configuration } from '../configuration.js';
|
|
4
|
-
import type { PersistenceOptions } from '../crawlers/statistics.js';
|
|
5
|
-
import type { EventManager } from '../events/event_manager.js';
|
|
6
|
-
import { KeyValueStore } from '../storages/key_value_store.js';
|
|
7
|
-
import type { SessionOptions } from './session.js';
|
|
8
|
-
import { Session } from './session.js';
|
|
9
|
-
/**
|
|
10
|
-
* Factory user-function which creates customized {@link Session} instances.
|
|
11
|
-
*/
|
|
12
|
-
export interface CreateSession {
|
|
13
|
-
/**
|
|
14
|
-
* @param sessionPool Pool requesting the new session.
|
|
15
|
-
* @param options
|
|
16
|
-
*/
|
|
17
|
-
(sessionPool: SessionPool, options?: {
|
|
18
|
-
sessionOptions?: SessionOptions;
|
|
19
|
-
}): Session | Promise<Session>;
|
|
20
|
-
}
|
|
21
|
-
export interface SessionPoolOptions {
|
|
22
|
-
/**
|
|
23
|
-
* Maximum size of the pool. Indicates how many sessions are rotated.
|
|
24
|
-
* @default 1000
|
|
25
|
-
*/
|
|
26
|
-
maxPoolSize?: number;
|
|
27
|
-
/** The configuration options for {@link Session} instances. */
|
|
28
|
-
sessionOptions?: SessionOptions;
|
|
29
|
-
/** Name or Id of `KeyValueStore` where is the `SessionPool` state stored. */
|
|
30
|
-
persistStateKeyValueStoreId?: string;
|
|
31
|
-
/**
|
|
32
|
-
* Session pool persists it's state under this key in Key value store.
|
|
33
|
-
* @default SESSION_POOL_STATE
|
|
34
|
-
*/
|
|
35
|
-
persistStateKey?: string;
|
|
36
|
-
/**
|
|
37
|
-
* Custom function that should return a `Session` instance, or a promise resolving to such instance.
|
|
38
|
-
* Any error thrown from this function will terminate the process.
|
|
39
|
-
* Function receives `SessionPool` instance as a parameter
|
|
40
|
-
*/
|
|
41
|
-
createSessionFunction?: CreateSession;
|
|
42
|
-
/**
|
|
43
|
-
* Specifies which response status codes are considered as blocked.
|
|
44
|
-
* Session connected to such request will be marked as retired.
|
|
45
|
-
* @default [401, 403, 429]
|
|
46
|
-
*/
|
|
47
|
-
blockedStatusCodes?: number[];
|
|
48
|
-
/** @internal */
|
|
49
|
-
log?: Log;
|
|
50
|
-
/**
|
|
51
|
-
* Control how and when to persist the state of the session pool.
|
|
52
|
-
*/
|
|
53
|
-
persistenceOptions?: PersistenceOptions;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Handles the rotation, creation and persistence of user-like sessions.
|
|
57
|
-
* Creates a pool of {@link Session} instances, that are randomly rotated.
|
|
58
|
-
* When some session is marked as blocked, it is removed and new one is created instead (the pool never returns an unusable session).
|
|
59
|
-
* Learn more in the {@doclink guides/session-management | Session management guide}.
|
|
60
|
-
*
|
|
61
|
-
* You can create one by calling the {@link SessionPool.open} function.
|
|
62
|
-
*
|
|
63
|
-
* Session pool is already integrated into crawlers, and it can significantly improve your scraper
|
|
64
|
-
* performance with just 2 lines of code.
|
|
65
|
-
*
|
|
66
|
-
* **Example usage:**
|
|
67
|
-
*
|
|
68
|
-
* ```javascript
|
|
69
|
-
* const crawler = new CheerioCrawler({
|
|
70
|
-
* useSessionPool: true,
|
|
71
|
-
* persistCookiesPerSession: true,
|
|
72
|
-
* // ...
|
|
73
|
-
* })
|
|
74
|
-
* ```
|
|
75
|
-
*
|
|
76
|
-
* You can configure the pool with many options. See the {@link SessionPoolOptions}.
|
|
77
|
-
* Session pool is by default persisted in default {@link KeyValueStore}.
|
|
78
|
-
* If you want to have one pool for all runs you have to specify
|
|
79
|
-
* {@link SessionPoolOptions.persistStateKeyValueStoreId}.
|
|
80
|
-
*
|
|
81
|
-
* **Advanced usage:**
|
|
82
|
-
*
|
|
83
|
-
* ```javascript
|
|
84
|
-
* const sessionPool = await SessionPool.open({
|
|
85
|
-
* maxPoolSize: 25,
|
|
86
|
-
* sessionOptions:{
|
|
87
|
-
* maxAgeSecs: 10,
|
|
88
|
-
* maxUsageCount: 150, // for example when you know that the site blocks after 150 requests.
|
|
89
|
-
* },
|
|
90
|
-
* persistStateKeyValueStoreId: 'my-key-value-store-for-sessions',
|
|
91
|
-
* persistStateKey: 'my-session-pool',
|
|
92
|
-
* });
|
|
93
|
-
*
|
|
94
|
-
* // Get random session from the pool
|
|
95
|
-
* const session1 = await sessionPool.getSession();
|
|
96
|
-
* const session2 = await sessionPool.getSession();
|
|
97
|
-
* const session3 = await sessionPool.getSession();
|
|
98
|
-
*
|
|
99
|
-
* // Now you can mark the session either failed or successful
|
|
100
|
-
*
|
|
101
|
-
* // Marks session as bad after unsuccessful usage -> it increases error count (soft retire)
|
|
102
|
-
* session1.markBad()
|
|
103
|
-
*
|
|
104
|
-
* // Marks as successful.
|
|
105
|
-
* session2.markGood()
|
|
106
|
-
*
|
|
107
|
-
* // Retires session -> session is removed from the pool
|
|
108
|
-
* session3.retire()
|
|
109
|
-
*
|
|
110
|
-
* ```
|
|
111
|
-
*
|
|
112
|
-
* **Default session allocation flow:*
|
|
113
|
-
* 1. Until the `SessionPool` reaches `maxPoolSize`, new sessions are created, provided to the user and added to the pool
|
|
114
|
-
* 2. Blocked/retired sessions stay in the pool but are never provided to the user
|
|
115
|
-
* 3. Once the pool is full (live plus blocked session count reaches `maxPoolSize`), a random session from the pool is provided.
|
|
116
|
-
* 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
|
|
117
|
-
*
|
|
118
|
-
* @category Scaling
|
|
119
|
-
*/
|
|
120
|
-
export declare class SessionPool extends EventEmitter {
|
|
121
|
-
readonly config: Configuration;
|
|
122
|
-
protected log: Log;
|
|
123
|
-
protected maxPoolSize: number;
|
|
124
|
-
protected createSessionFunction: CreateSession;
|
|
125
|
-
protected keyValueStore: KeyValueStore;
|
|
126
|
-
protected sessions: Session[];
|
|
127
|
-
protected sessionMap: Map<string, Session>;
|
|
128
|
-
protected sessionOptions: SessionOptions;
|
|
129
|
-
protected persistStateKeyValueStoreId?: string;
|
|
130
|
-
protected persistStateKey: string;
|
|
131
|
-
protected _listener: () => Promise<void>;
|
|
132
|
-
protected events: EventManager;
|
|
133
|
-
protected readonly blockedStatusCodes: number[];
|
|
134
|
-
protected persistenceOptions: PersistenceOptions;
|
|
135
|
-
protected isInitialized: boolean;
|
|
136
|
-
private queue;
|
|
137
|
-
/**
|
|
138
|
-
* @internal
|
|
139
|
-
*/
|
|
140
|
-
constructor(options?: SessionPoolOptions, config?: Configuration);
|
|
141
|
-
/**
|
|
142
|
-
* Gets count of usable sessions in the pool.
|
|
143
|
-
*/
|
|
144
|
-
get usableSessionsCount(): number;
|
|
145
|
-
/**
|
|
146
|
-
* Gets count of retired sessions in the pool.
|
|
147
|
-
*/
|
|
148
|
-
get retiredSessionsCount(): number;
|
|
149
|
-
/**
|
|
150
|
-
* Starts periodic state persistence and potentially loads SessionPool state from {@link KeyValueStore}.
|
|
151
|
-
* It is called automatically by the {@link SessionPool.open} function.
|
|
152
|
-
*/
|
|
153
|
-
initialize(): Promise<void>;
|
|
154
|
-
/**
|
|
155
|
-
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
156
|
-
* but this allows you to add more sessions once the max pool size is reached.
|
|
157
|
-
* This also allows you to add session with overridden session options (e.g. with specific session id).
|
|
158
|
-
* @param [options] The configuration options for the session being added to the session pool.
|
|
159
|
-
*/
|
|
160
|
-
addSession(options?: Session | SessionOptions): Promise<void>;
|
|
161
|
-
/**
|
|
162
|
-
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
163
|
-
* but this allows you to add more sessions once the max pool size is reached.
|
|
164
|
-
* This also allows you to add session with overridden session options (e.g. with specific session id).
|
|
165
|
-
* @param [options] The configuration options for the session being added to the session pool.
|
|
166
|
-
*/
|
|
167
|
-
newSession(sessionOptions?: SessionOptions): Promise<Session>;
|
|
168
|
-
/**
|
|
169
|
-
* Gets session.
|
|
170
|
-
* If there is space for new session, it creates and returns new session.
|
|
171
|
-
* If the session pool is full, it picks a session from the pool,
|
|
172
|
-
* If the picked session is usable it is returned, otherwise it creates and returns a new one.
|
|
173
|
-
*/
|
|
174
|
-
getSession(): Promise<Session>;
|
|
175
|
-
/**
|
|
176
|
-
* Gets session based on the provided session id or `undefined.
|
|
177
|
-
*/
|
|
178
|
-
getSession(sessionId: string): Promise<Session>;
|
|
179
|
-
/**
|
|
180
|
-
* @param options - Override the persistence options provided in the constructor
|
|
181
|
-
*/
|
|
182
|
-
resetStore(options?: PersistenceOptions): Promise<void>;
|
|
183
|
-
/**
|
|
184
|
-
* Returns an object representing the internal state of the `SessionPool` instance.
|
|
185
|
-
* Note that the object's fields can change in future releases.
|
|
186
|
-
*/
|
|
187
|
-
getState(): {
|
|
188
|
-
usableSessionsCount: number;
|
|
189
|
-
retiredSessionsCount: number;
|
|
190
|
-
// @ts-ignore optional peer dependency or compatibility with es2022
|
|
191
|
-
sessions: import("@crawlee/types").SessionState[];
|
|
192
|
-
};
|
|
193
|
-
/**
|
|
194
|
-
* Persists the current state of the `SessionPool` into the default {@link KeyValueStore}.
|
|
195
|
-
* The state is persisted automatically in regular intervals.
|
|
196
|
-
* @param options - Override the persistence options provided in the constructor
|
|
197
|
-
*/
|
|
198
|
-
persistState(options?: PersistenceOptions): Promise<void>;
|
|
199
|
-
/**
|
|
200
|
-
* Removes listener from `persistState` event.
|
|
201
|
-
* This function should be called after you are done with using the `SessionPool` instance.
|
|
202
|
-
*/
|
|
203
|
-
teardown(): Promise<void>;
|
|
204
|
-
/**
|
|
205
|
-
* SessionPool should not work before initialization.
|
|
206
|
-
*/
|
|
207
|
-
protected _throwIfNotInitialized(): void;
|
|
208
|
-
/**
|
|
209
|
-
* Removes retired `Session` instances from `SessionPool`.
|
|
210
|
-
*/
|
|
211
|
-
protected _removeRetiredSessions(): void;
|
|
212
|
-
/**
|
|
213
|
-
* Adds `Session` instance to `SessionPool`.
|
|
214
|
-
* @param newSession `Session` instance to be added.
|
|
215
|
-
*/
|
|
216
|
-
protected _addSession(newSession: Session): void;
|
|
217
|
-
/**
|
|
218
|
-
* Gets random index.
|
|
219
|
-
*/
|
|
220
|
-
protected _getRandomIndex(): number;
|
|
221
|
-
/**
|
|
222
|
-
* Creates new session without any extra behavior.
|
|
223
|
-
* @param sessionPool
|
|
224
|
-
* @param [options]
|
|
225
|
-
* @param [options.sessionOptions] The configuration options for the session being created.
|
|
226
|
-
* @returns New session.
|
|
227
|
-
*/
|
|
228
|
-
protected _defaultCreateSessionFunction(sessionPool: SessionPool, options?: {
|
|
229
|
-
sessionOptions?: SessionOptions;
|
|
230
|
-
}): Promise<Session>;
|
|
231
|
-
/**
|
|
232
|
-
* Creates new session and adds it to the pool.
|
|
233
|
-
* @returns Newly created `Session` instance.
|
|
234
|
-
*/
|
|
235
|
-
protected _createSession(): Promise<Session>;
|
|
236
|
-
/**
|
|
237
|
-
* Decides whether there is enough space for creating new session.
|
|
238
|
-
*/
|
|
239
|
-
protected _hasSpaceForSession(): boolean;
|
|
240
|
-
/**
|
|
241
|
-
* Picks random session from the `SessionPool`.
|
|
242
|
-
* @returns Picked `Session`.
|
|
243
|
-
*/
|
|
244
|
-
protected _pickSession(): Session;
|
|
245
|
-
/**
|
|
246
|
-
* Potentially loads `SessionPool`.
|
|
247
|
-
* If the state was persisted it loads the `SessionPool` from the persisted state.
|
|
248
|
-
*/
|
|
249
|
-
protected _maybeLoadSessionPool(): Promise<void>;
|
|
250
|
-
/**
|
|
251
|
-
* Opens a SessionPool and returns a promise resolving to an instance
|
|
252
|
-
* of the {@link SessionPool} class that is already initialized.
|
|
253
|
-
*
|
|
254
|
-
* For more details and code examples, see the {@link SessionPool} class.
|
|
255
|
-
*/
|
|
256
|
-
static open(options?: SessionPoolOptions, config?: Configuration): Promise<SessionPool>;
|
|
257
|
-
}
|
|
258
|
-
//# sourceMappingURL=session_pool.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
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,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