@crawlee/core 3.0.0-alpha.2 → 3.0.0-alpha.5
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 +296 -0
- package/autoscaling/autoscaled_pool.d.ts.map +1 -0
- package/autoscaling/autoscaled_pool.js +673 -0
- package/autoscaling/autoscaled_pool.js.map +1 -0
- package/autoscaling/index.d.ts +4 -0
- package/autoscaling/index.d.ts.map +1 -0
- package/autoscaling/index.js +7 -0
- package/autoscaling/index.js.map +1 -0
- package/autoscaling/snapshotter.d.ts +193 -0
- package/autoscaling/snapshotter.d.ts.map +1 -0
- package/autoscaling/snapshotter.js +377 -0
- package/autoscaling/snapshotter.js.map +1 -0
- package/autoscaling/system_status.d.ts +181 -0
- package/autoscaling/system_status.d.ts.map +1 -0
- package/autoscaling/system_status.js +194 -0
- package/autoscaling/system_status.js.map +1 -0
- package/cache_container.d.ts +18 -0
- package/cache_container.d.ts.map +1 -0
- package/cache_container.js +42 -0
- package/cache_container.js.map +1 -0
- package/configuration.d.ts +150 -0
- package/configuration.d.ts.map +1 -0
- package/configuration.js +284 -0
- package/configuration.js.map +1 -0
- package/constants.d.ts +3 -0
- package/constants.d.ts.map +1 -0
- package/constants.js +6 -0
- package/constants.js.map +1 -0
- package/crawlers/crawler_commons.d.ts +25 -0
- package/crawlers/crawler_commons.d.ts.map +1 -0
- package/crawlers/crawler_commons.js +3 -0
- package/crawlers/crawler_commons.js.map +1 -0
- package/crawlers/crawler_extension.d.ts +11 -0
- package/crawlers/crawler_extension.d.ts.map +1 -0
- package/crawlers/crawler_extension.js +30 -0
- package/crawlers/crawler_extension.js.map +1 -0
- package/crawlers/crawler_utils.d.ts +25 -0
- package/crawlers/crawler_utils.d.ts.map +1 -0
- package/crawlers/crawler_utils.js +72 -0
- package/crawlers/crawler_utils.js.map +1 -0
- package/crawlers/index.d.ts +5 -0
- package/crawlers/index.d.ts.map +1 -0
- package/crawlers/index.js +8 -0
- package/crawlers/index.js.map +1 -0
- package/crawlers/statistics.d.ts +147 -0
- package/crawlers/statistics.d.ts.map +1 -0
- package/crawlers/statistics.js +360 -0
- package/crawlers/statistics.js.map +1 -0
- package/enqueue_links/enqueue_links.d.ts +151 -0
- package/enqueue_links/enqueue_links.d.ts.map +1 -0
- package/enqueue_links/enqueue_links.js +152 -0
- package/enqueue_links/enqueue_links.js.map +1 -0
- package/enqueue_links/index.d.ts +3 -0
- package/enqueue_links/index.d.ts.map +1 -0
- package/enqueue_links/index.js +6 -0
- package/enqueue_links/index.js.map +1 -0
- package/enqueue_links/shared.d.ts +60 -0
- package/enqueue_links/shared.d.ts.map +1 -0
- package/enqueue_links/shared.js +148 -0
- package/enqueue_links/shared.js.map +1 -0
- package/errors.d.ts +8 -0
- package/errors.d.ts.map +1 -0
- package/errors.js +12 -0
- package/errors.js.map +1 -0
- package/events/event_manager.d.ts +41 -0
- package/events/event_manager.d.ts.map +1 -0
- package/events/event_manager.js +103 -0
- package/events/event_manager.js.map +1 -0
- package/events/index.d.ts +3 -0
- package/events/index.d.ts.map +1 -0
- package/events/index.js +6 -0
- package/events/index.js.map +1 -0
- package/events/local_event_manager.d.ts +32 -0
- package/events/local_event_manager.d.ts.map +1 -0
- package/events/local_event_manager.js +114 -0
- package/events/local_event_manager.js.map +1 -0
- package/index.d.ts +14 -0
- package/index.d.ts.map +1 -0
- package/index.js +17 -0
- package/index.js.map +1 -0
- package/index.mjs +64 -0
- package/log.d.ts +3 -0
- package/log.d.ts.map +1 -0
- package/log.js +12 -0
- package/log.js.map +1 -0
- package/package.json +4 -7
- package/proxy_configuration.d.ts +181 -0
- package/proxy_configuration.d.ts.map +1 -0
- package/proxy_configuration.js +209 -0
- package/proxy_configuration.js.map +1 -0
- package/request.d.ts +174 -0
- package/request.d.ts.map +1 -0
- package/request.js +270 -0
- package/request.js.map +1 -0
- package/serialization.d.ts +33 -0
- package/serialization.d.ts.map +1 -0
- package/serialization.js +146 -0
- package/serialization.js.map +1 -0
- package/session_pool/errors.d.ts +8 -0
- package/session_pool/errors.d.ts.map +1 -0
- package/session_pool/errors.js +20 -0
- package/session_pool/errors.js.map +1 -0
- package/session_pool/events.d.ts +3 -0
- package/session_pool/events.d.ts.map +1 -0
- package/session_pool/events.js +6 -0
- package/session_pool/events.js.map +1 -0
- package/session_pool/index.d.ts +6 -0
- package/session_pool/index.d.ts.map +1 -0
- package/session_pool/index.js +9 -0
- package/session_pool/index.js.map +1 -0
- package/session_pool/session.d.ts +212 -0
- package/session_pool/session.d.ts.map +1 -0
- package/session_pool/session.js +360 -0
- package/session_pool/session.js.map +1 -0
- package/session_pool/session_pool.d.ts +222 -0
- package/session_pool/session_pool.d.ts.map +1 -0
- package/session_pool/session_pool.js +389 -0
- package/session_pool/session_pool.js.map +1 -0
- package/session_pool/session_utils.d.ts +9 -0
- package/session_pool/session_utils.d.ts.map +1 -0
- package/session_pool/session_utils.js +19 -0
- package/session_pool/session_utils.js.map +1 -0
- package/storages/dataset.d.ts +358 -0
- package/storages/dataset.d.ts.map +1 -0
- package/storages/dataset.js +415 -0
- package/storages/dataset.js.map +1 -0
- package/storages/index.d.ts +7 -0
- package/storages/index.d.ts.map +1 -0
- package/storages/index.js +11 -0
- package/storages/index.js.map +1 -0
- package/storages/key_value_store.d.ts +242 -0
- package/storages/key_value_store.d.ts.map +1 -0
- package/storages/key_value_store.js +320 -0
- package/storages/key_value_store.js.map +1 -0
- package/storages/request_list.d.ts +469 -0
- package/storages/request_list.d.ts.map +1 -0
- package/storages/request_list.js +724 -0
- package/storages/request_list.js.map +1 -0
- package/storages/request_queue.d.ts +315 -0
- package/storages/request_queue.d.ts.map +1 -0
- package/storages/request_queue.js +742 -0
- package/storages/request_queue.js.map +1 -0
- package/storages/storage.d.ts +260 -0
- package/storages/storage.d.ts.map +1 -0
- package/storages/storage.js +3 -0
- package/storages/storage.js.map +1 -0
- package/storages/storage_manager.d.ts +40 -0
- package/storages/storage_manager.d.ts.map +1 -0
- package/storages/storage_manager.js +119 -0
- package/storages/storage_manager.js.map +1 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/typedefs.d.ts +12 -0
- package/typedefs.d.ts.map +1 -0
- package/typedefs.js +15 -0
- package/typedefs.js.map +1 -0
- package/validators.d.ts +21 -0
- package/validators.d.ts.map +1 -0
- package/validators.js +26 -0
- package/validators.js.map +1 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Session = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const utilities_1 = require("@apify/utilities");
|
|
6
|
+
const ow_1 = tslib_1.__importDefault(require("ow"));
|
|
7
|
+
const tough_cookie_1 = require("tough-cookie");
|
|
8
|
+
const constants_1 = require("../constants");
|
|
9
|
+
const log_1 = require("../log");
|
|
10
|
+
const events_1 = require("./events");
|
|
11
|
+
const session_pool_1 = require("./session_pool");
|
|
12
|
+
const session_utils_1 = require("./session_utils");
|
|
13
|
+
// CONSTANTS
|
|
14
|
+
const DEFAULT_SESSION_MAX_AGE_SECS = 3000;
|
|
15
|
+
/**
|
|
16
|
+
* Sessions are used to store information such as cookies and can be used for generating fingerprints and proxy sessions.
|
|
17
|
+
* You can imagine each session as a specific user, with its own cookies, IP (via proxy) and potentially a unique browser fingerprint.
|
|
18
|
+
* Session internal state can be enriched with custom user data for example some authorization tokens and specific headers in general.
|
|
19
|
+
* @category Scaling
|
|
20
|
+
*/
|
|
21
|
+
class Session {
|
|
22
|
+
/**
|
|
23
|
+
* Session configuration.
|
|
24
|
+
*/
|
|
25
|
+
constructor(options) {
|
|
26
|
+
Object.defineProperty(this, "id", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "maxAgeSecs", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "userData", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "maxErrorScore", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "errorScoreDecrement", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "createdAt", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "expiresAt", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(this, "usageCount", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(this, "maxUsageCount", {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
value: void 0
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(this, "sessionPool", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
configurable: true,
|
|
83
|
+
writable: true,
|
|
84
|
+
value: void 0
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(this, "errorScore", {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
configurable: true,
|
|
89
|
+
writable: true,
|
|
90
|
+
value: void 0
|
|
91
|
+
});
|
|
92
|
+
Object.defineProperty(this, "cookieJar", {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
writable: true,
|
|
96
|
+
value: void 0
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(this, "log", {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
writable: true,
|
|
102
|
+
value: void 0
|
|
103
|
+
});
|
|
104
|
+
(0, ow_1.default)(options, ow_1.default.object.exactShape({
|
|
105
|
+
sessionPool: ow_1.default.object.instanceOf(session_pool_1.SessionPool),
|
|
106
|
+
id: ow_1.default.optional.string,
|
|
107
|
+
cookieJar: ow_1.default.optional.object,
|
|
108
|
+
maxAgeSecs: ow_1.default.optional.number,
|
|
109
|
+
userData: ow_1.default.optional.object,
|
|
110
|
+
maxErrorScore: ow_1.default.optional.number,
|
|
111
|
+
errorScoreDecrement: ow_1.default.optional.number,
|
|
112
|
+
createdAt: ow_1.default.optional.date,
|
|
113
|
+
expiresAt: ow_1.default.optional.date,
|
|
114
|
+
usageCount: ow_1.default.optional.number,
|
|
115
|
+
errorScore: ow_1.default.optional.number,
|
|
116
|
+
maxUsageCount: ow_1.default.optional.number,
|
|
117
|
+
log: ow_1.default.optional.object,
|
|
118
|
+
}));
|
|
119
|
+
const { sessionPool, id = `session_${(0, utilities_1.cryptoRandomObjectId)(10)}`, cookieJar = new tough_cookie_1.CookieJar(), maxAgeSecs = DEFAULT_SESSION_MAX_AGE_SECS, userData = {}, maxErrorScore = 3, errorScoreDecrement = 0.5, createdAt = new Date(), usageCount = 0, errorScore = 0, maxUsageCount = 50, log = log_1.log, } = options;
|
|
120
|
+
const { expiresAt = this._getDefaultCookieExpirationDate(maxAgeSecs) } = options;
|
|
121
|
+
this.log = log.child({ prefix: 'Session' });
|
|
122
|
+
this.cookieJar = cookieJar.setCookie ? cookieJar : tough_cookie_1.CookieJar.fromJSON(JSON.stringify(cookieJar));
|
|
123
|
+
this.id = id;
|
|
124
|
+
this.maxAgeSecs = maxAgeSecs;
|
|
125
|
+
this.userData = userData;
|
|
126
|
+
this.maxErrorScore = maxErrorScore;
|
|
127
|
+
this.errorScoreDecrement = errorScoreDecrement;
|
|
128
|
+
// Internal
|
|
129
|
+
this.expiresAt = expiresAt;
|
|
130
|
+
this.createdAt = createdAt;
|
|
131
|
+
this.usageCount = usageCount; // indicates how many times the session has been used
|
|
132
|
+
this.errorScore = errorScore; // indicates number of markBaded request with the session
|
|
133
|
+
this.maxUsageCount = maxUsageCount;
|
|
134
|
+
this.sessionPool = sessionPool;
|
|
135
|
+
this._puppeteerCookieToTough = this._puppeteerCookieToTough.bind(this);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Indicates whether the session is blocked.
|
|
139
|
+
* Session is blocked once it reaches the `maxErrorScore`.
|
|
140
|
+
*/
|
|
141
|
+
isBlocked() {
|
|
142
|
+
return this.errorScore >= this.maxErrorScore;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Indicates whether the session is expired.
|
|
146
|
+
* Session expiration is determined by the `maxAgeSecs`.
|
|
147
|
+
* Once the session is older than `createdAt + maxAgeSecs` the session is considered expired.
|
|
148
|
+
*/
|
|
149
|
+
isExpired() {
|
|
150
|
+
return this.expiresAt <= new Date();
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Indicates whether the session is used maximum number of times.
|
|
154
|
+
* Session maximum usage count can be changed by `maxUsageCount` parameter.
|
|
155
|
+
*/
|
|
156
|
+
isMaxUsageCountReached() {
|
|
157
|
+
return this.usageCount >= this.maxUsageCount;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Indicates whether the session can be used for next requests.
|
|
161
|
+
* Session is usable when it is not expired, not blocked and the maximum usage count has not be reached.
|
|
162
|
+
*/
|
|
163
|
+
isUsable() {
|
|
164
|
+
return !this.isBlocked() && !this.isExpired() && !this.isMaxUsageCountReached();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* This method should be called after a successful session usage.
|
|
168
|
+
* It increases `usageCount` and potentially lowers the `errorScore` by the `errorScoreDecrement`.
|
|
169
|
+
*/
|
|
170
|
+
markGood() {
|
|
171
|
+
this.usageCount += 1;
|
|
172
|
+
if (this.errorScore > 0) {
|
|
173
|
+
this.errorScore -= this.errorScoreDecrement;
|
|
174
|
+
}
|
|
175
|
+
this._maybeSelfRetire();
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Gets session state for persistence in KeyValueStore.
|
|
179
|
+
* @returns Represents session internal state.
|
|
180
|
+
*/
|
|
181
|
+
getState() {
|
|
182
|
+
return {
|
|
183
|
+
id: this.id,
|
|
184
|
+
cookieJar: this.cookieJar.toJSON(),
|
|
185
|
+
userData: this.userData,
|
|
186
|
+
maxErrorScore: this.maxErrorScore,
|
|
187
|
+
errorScoreDecrement: this.errorScoreDecrement,
|
|
188
|
+
expiresAt: this.expiresAt.toISOString(),
|
|
189
|
+
createdAt: this.createdAt.toISOString(),
|
|
190
|
+
usageCount: this.usageCount,
|
|
191
|
+
maxUsageCount: this.maxUsageCount,
|
|
192
|
+
errorScore: this.errorScore,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Marks session as blocked and emits event on the `SessionPool`
|
|
197
|
+
* This method should be used if the session usage was unsuccessful
|
|
198
|
+
* and you are sure that it is because of the session configuration and not any external matters.
|
|
199
|
+
* For example when server returns 403 status code.
|
|
200
|
+
* If the session does not work due to some external factors as server error such as 5XX you probably want to use `markBad` method.
|
|
201
|
+
*/
|
|
202
|
+
retire() {
|
|
203
|
+
// mark it as an invalid by increasing the error score count.
|
|
204
|
+
this.errorScore += this.maxErrorScore;
|
|
205
|
+
this.usageCount += 1;
|
|
206
|
+
// emit event so we can retire browser in puppeteer pool
|
|
207
|
+
this.sessionPool.emit(events_1.EVENT_SESSION_RETIRED, this);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Increases usage and error count.
|
|
211
|
+
* Should be used when the session has been used unsuccessfully. For example because of timeouts.
|
|
212
|
+
*/
|
|
213
|
+
markBad() {
|
|
214
|
+
this.errorScore += 1;
|
|
215
|
+
this.usageCount += 1;
|
|
216
|
+
this._maybeSelfRetire();
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* With certain status codes: `401`, `403` or `429` we can be certain
|
|
220
|
+
* that the target website is blocking us. This function helps to do this conveniently
|
|
221
|
+
* by retiring the session when such code is received. Optionally the default status
|
|
222
|
+
* codes can be extended in the second parameter.
|
|
223
|
+
* @param statusCode HTTP status code.
|
|
224
|
+
* @param [blockedStatusCodes] Custom HTTP status codes that means blocking on particular website.
|
|
225
|
+
* @returns Whether the session was retired.
|
|
226
|
+
*/
|
|
227
|
+
retireOnBlockedStatusCodes(statusCode, blockedStatusCodes = []) {
|
|
228
|
+
const isBlocked = constants_1.STATUS_CODES_BLOCKED.concat(blockedStatusCodes).includes(statusCode);
|
|
229
|
+
if (isBlocked) {
|
|
230
|
+
this.retire();
|
|
231
|
+
}
|
|
232
|
+
return isBlocked;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Saves cookies from an HTTP response to be used with the session.
|
|
236
|
+
* It expects an object with a `headers` property that's either an `Object`
|
|
237
|
+
* (typical Node.js responses) or a `Function` (Puppeteer Response).
|
|
238
|
+
*
|
|
239
|
+
* It then parses and saves the cookies from the `set-cookie` header, if available.
|
|
240
|
+
*/
|
|
241
|
+
setCookiesFromResponse(response) {
|
|
242
|
+
try {
|
|
243
|
+
const cookies = (0, session_utils_1.getCookiesFromResponse)(response).filter((c) => c);
|
|
244
|
+
this._setCookies(cookies, typeof response.url === 'function' ? response.url() : response.url);
|
|
245
|
+
}
|
|
246
|
+
catch (e) {
|
|
247
|
+
const err = e;
|
|
248
|
+
// if invalid Cookie header is provided just log the exception.
|
|
249
|
+
this.log.exception(err, 'Could not get cookies from response');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Saves an array with cookie objects to be used with the session.
|
|
254
|
+
* The objects should be in the format that
|
|
255
|
+
* [Puppeteer uses](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls),
|
|
256
|
+
* but you can also use this function to set cookies manually:
|
|
257
|
+
*
|
|
258
|
+
* ```
|
|
259
|
+
* [
|
|
260
|
+
* { name: 'cookie1', value: 'my-cookie' },
|
|
261
|
+
* { name: 'cookie2', value: 'your-cookie' }
|
|
262
|
+
* ]
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
setPuppeteerCookies(cookies, url) {
|
|
266
|
+
const normalizedCookies = cookies.map(this._puppeteerCookieToTough);
|
|
267
|
+
this._setCookies(normalizedCookies, url);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Returns cookies in a format compatible with puppeteer and ready to be used with `page.setCookie`.
|
|
271
|
+
* @param url website url. Only cookies stored for this url will be returned
|
|
272
|
+
*/
|
|
273
|
+
getPuppeteerCookies(url) {
|
|
274
|
+
const cookies = this.cookieJar.getCookiesSync(url);
|
|
275
|
+
return cookies.map(this._toughCookieToPuppeteer);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Returns cookies saved with the session in the typical
|
|
279
|
+
* key1=value1; key2=value2 format, ready to be used in
|
|
280
|
+
* a cookie header or elsewhere.
|
|
281
|
+
* @returns Represents `Cookie` header.
|
|
282
|
+
*/
|
|
283
|
+
getCookieString(url) {
|
|
284
|
+
return this.cookieJar.getCookieStringSync(url, {});
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Transforms puppeteer cookie to tough-cookie.
|
|
288
|
+
* @param puppeteerCookie Cookie from puppeteer `page.cookies` method.
|
|
289
|
+
*/
|
|
290
|
+
_puppeteerCookieToTough(puppeteerCookie) {
|
|
291
|
+
const isExpiresValid = puppeteerCookie.expires && typeof puppeteerCookie.expires === 'number' && puppeteerCookie.expires > 0;
|
|
292
|
+
const expires = isExpiresValid ? new Date(puppeteerCookie.expires * 1000) : this._getDefaultCookieExpirationDate(this.maxAgeSecs);
|
|
293
|
+
const domain = typeof puppeteerCookie.domain === 'string' && puppeteerCookie.domain.startsWith('.')
|
|
294
|
+
? puppeteerCookie.domain.slice(1)
|
|
295
|
+
: puppeteerCookie.domain;
|
|
296
|
+
return new tough_cookie_1.Cookie({
|
|
297
|
+
key: puppeteerCookie.name,
|
|
298
|
+
value: puppeteerCookie.value,
|
|
299
|
+
expires,
|
|
300
|
+
domain,
|
|
301
|
+
path: puppeteerCookie.path,
|
|
302
|
+
secure: puppeteerCookie.secure,
|
|
303
|
+
httpOnly: puppeteerCookie.httpOnly,
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Transforms tough-cookie to puppeteer cookie.
|
|
308
|
+
* @param toughCookie Cookie from CookieJar
|
|
309
|
+
* @return Cookie from Puppeteer
|
|
310
|
+
*/
|
|
311
|
+
_toughCookieToPuppeteer(toughCookie) {
|
|
312
|
+
return {
|
|
313
|
+
name: toughCookie.key,
|
|
314
|
+
value: toughCookie.value,
|
|
315
|
+
// Puppeteer and Playwright expect 'expires' to be 'Unix time in seconds', not ms
|
|
316
|
+
expires: new Date(toughCookie.expires).getTime() / 1000,
|
|
317
|
+
domain: toughCookie.domain,
|
|
318
|
+
path: toughCookie.path,
|
|
319
|
+
secure: toughCookie.secure,
|
|
320
|
+
httpOnly: toughCookie.httpOnly,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Sets cookies.
|
|
325
|
+
*/
|
|
326
|
+
_setCookies(cookies, url) {
|
|
327
|
+
const errorMessages = [];
|
|
328
|
+
for (const cookie of cookies) {
|
|
329
|
+
try {
|
|
330
|
+
this.cookieJar.setCookieSync(cookie, url, { ignoreError: false });
|
|
331
|
+
}
|
|
332
|
+
catch (e) {
|
|
333
|
+
const err = e;
|
|
334
|
+
errorMessages.push(err.message);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
// if invalid cookies are provided just log the exception. No need to retry the request automatically.
|
|
338
|
+
if (errorMessages.length) {
|
|
339
|
+
this.log.debug('Could not set cookies.', { errorMessages });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Calculate cookie expiration date
|
|
344
|
+
* @param maxAgeSecs
|
|
345
|
+
* @returns Calculated date by session max age seconds.
|
|
346
|
+
*/
|
|
347
|
+
_getDefaultCookieExpirationDate(maxAgeSecs) {
|
|
348
|
+
return new Date(Date.now() + (maxAgeSecs * 1000));
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Checks if session is not usable. if it is not retires the session.
|
|
352
|
+
*/
|
|
353
|
+
_maybeSelfRetire() {
|
|
354
|
+
if (!this.isUsable()) {
|
|
355
|
+
this.retire();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
exports.Session = Session;
|
|
360
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session_pool/session.ts"],"names":[],"mappings":";;;;AACA,gDAAwD;AAGxD,oDAAoB;AAEpB,+CAAiD;AACjD,4CAAoD;AACpD,gCAA2C;AAE3C,qCAAiD;AACjD,iDAA6C;AAC7C,mDAAyD;AAIzD,YAAY;AACZ,MAAM,4BAA4B,GAAG,IAAI,CAAC;AA6E1C;;;;;GAKG;AACH,MAAa,OAAO;IAehB;;OAEG;IACH,YAAY,OAAuB;QAjBnC;;;;;WAAoB;QACpB;;;;;WAA2B;QAC3B;;;;;WAAqB;QACrB;;;;;WAA8B;QAC9B;;;;;WAAoC;QACpC;;;;;WAAwB;QACxB;;;;;WAAwB;QACxB;;;;;WAA2B;QAC3B;;;;;WAA8B;QAC9B;;;;;WAAiC;QACjC;;;;;WAA2B;QAC3B;;;;;WAA6B;QAC7B;;;;;WAAiB;QAMb,IAAA,YAAE,EAAC,OAAO,EAAE,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AW,CAAC,MAAM;YAC1B,QAAQ,EAAE,WAAW,CAAC,QAAQ;SACd,CAAC;IACzB,CAAC;IAED;;OAEG;IACO,WAAW,CAAC,OAAiB,EAAE,GAAW;QAChD,MAAM,aAAa,GAAa,EAAE,CAAC;QAEnC,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;YAC1B,IAAI;gBACA,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAC;aACrE;YAAC,OAAO,CAAC,EAAE;gBACR,MAAM,GAAG,GAAG,CAAU,CAAC;gBACvB,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;aACnC;SACJ;QAED,sGAAsG;QACtG,IAAI,aAAa,CAAC,MAAM,EAAE;YACtB,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,wBAAwB,EAAE,EAAE,aAAa,EAAE,CAAC,CAAC;SAC/D;IACL,CAAC;IAED;;;;OAIG;IACO,+BAA+B,CAAC,UAAkB;QACxD,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC;IACtD,CAAC;IAED;;OAEG;IACO,gBAAgB;QACtB,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE;YAClB,IAAI,CAAC,MAAM,EAAE,CAAC;SACjB;IACL,CAAC;CACJ;AA3TD,0BA2TC"}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Log } from '@apify/log';
|
|
3
|
+
import { EventEmitter } from 'events';
|
|
4
|
+
import { Configuration } from '../configuration';
|
|
5
|
+
import { KeyValueStore } from '../storages/key_value_store';
|
|
6
|
+
import { Session, SessionOptions } from './session';
|
|
7
|
+
/**
|
|
8
|
+
* Factory user-function which creates customized {@link Session} instances.
|
|
9
|
+
*/
|
|
10
|
+
export interface CreateSession {
|
|
11
|
+
/**
|
|
12
|
+
* @param sessionPool Pool requesting the new session.
|
|
13
|
+
* @param options
|
|
14
|
+
*/
|
|
15
|
+
(sessionPool: SessionPool, options?: {
|
|
16
|
+
sessionOptions?: SessionOptions;
|
|
17
|
+
}): Session | Promise<Session>;
|
|
18
|
+
}
|
|
19
|
+
export interface SessionPoolOptions {
|
|
20
|
+
/**
|
|
21
|
+
* Maximum size of the pool. Indicates how many sessions are rotated.
|
|
22
|
+
* @default 1000
|
|
23
|
+
*/
|
|
24
|
+
maxPoolSize?: number;
|
|
25
|
+
/** The configuration options for {@link Session} instances. */
|
|
26
|
+
sessionOptions?: SessionOptions;
|
|
27
|
+
/** Name or Id of `KeyValueStore` where is the `SessionPool` state stored. */
|
|
28
|
+
persistStateKeyValueStoreId?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Session pool persists it's state under this key in Key value store.
|
|
31
|
+
* @default SESSION_POOL_STATE
|
|
32
|
+
*/
|
|
33
|
+
persistStateKey?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Custom function that should return `Session` instance.
|
|
36
|
+
* Any error thrown from this function will terminate the process.
|
|
37
|
+
* Function receives `SessionPool` instance as a parameter
|
|
38
|
+
*/
|
|
39
|
+
createSessionFunction?: CreateSession;
|
|
40
|
+
/** @internal */
|
|
41
|
+
log?: Log;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Handles the rotation, creation and persistence of user-like sessions.
|
|
45
|
+
* Creates a pool of {@link Session} instances, that are randomly rotated.
|
|
46
|
+
* When some session is marked as blocked, it is removed and new one is created instead (the pool never returns an unusable session).
|
|
47
|
+
* Learn more in the [Session management guide](../../../docs/guides/session-management).
|
|
48
|
+
*
|
|
49
|
+
* You can create one by calling the {@link SessionPool.open} function.
|
|
50
|
+
*
|
|
51
|
+
* Session pool is already integrated into crawlers, and it can significantly improve your scraper
|
|
52
|
+
* performance with just 2 lines of code.
|
|
53
|
+
*
|
|
54
|
+
* **Example usage:**
|
|
55
|
+
*
|
|
56
|
+
* ```javascript
|
|
57
|
+
* const crawler = new CheerioCrawler({
|
|
58
|
+
* useSessionPool: true,
|
|
59
|
+
* persistCookiesPerSession: true,
|
|
60
|
+
* // ...
|
|
61
|
+
* })
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* You can configure the pool with many options. See the {@link SessionPoolOptions}.
|
|
65
|
+
* Session pool is by default persisted in default {@link KeyValueStore}.
|
|
66
|
+
* If you want to have one pool for all runs you have to specify
|
|
67
|
+
* {@link SessionPoolOptions.persistStateKeyValueStoreId}.
|
|
68
|
+
*
|
|
69
|
+
* **Advanced usage:**
|
|
70
|
+
*
|
|
71
|
+
* ```javascript
|
|
72
|
+
* const sessionPool = await SessionPool.open({
|
|
73
|
+
* maxPoolSize: 25,
|
|
74
|
+
* sessionOptions:{
|
|
75
|
+
* maxAgeSecs: 10,
|
|
76
|
+
* maxUsageCount: 150, // for example when you know that the site blocks after 150 requests.
|
|
77
|
+
* },
|
|
78
|
+
* persistStateKeyValueStoreId: 'my-key-value-store-for-sessions',
|
|
79
|
+
* persistStateKey: 'my-session-pool',
|
|
80
|
+
* });
|
|
81
|
+
*
|
|
82
|
+
* // Get random session from the pool
|
|
83
|
+
* const session1 = await sessionPool.getSession();
|
|
84
|
+
* const session2 = await sessionPool.getSession();
|
|
85
|
+
* const session3 = await sessionPool.getSession();
|
|
86
|
+
*
|
|
87
|
+
* // Now you can mark the session either failed or successful
|
|
88
|
+
*
|
|
89
|
+
* // Marks session as bad after unsuccessful usage -> it increases error count (soft retire)
|
|
90
|
+
* session1.markBad()
|
|
91
|
+
*
|
|
92
|
+
* // Marks as successful.
|
|
93
|
+
* session2.markGood()
|
|
94
|
+
*
|
|
95
|
+
* // Retires session -> session is removed from the pool
|
|
96
|
+
* session3.retire()
|
|
97
|
+
*
|
|
98
|
+
* ```
|
|
99
|
+
* @category Scaling
|
|
100
|
+
*/
|
|
101
|
+
export declare class SessionPool extends EventEmitter {
|
|
102
|
+
readonly config: Configuration;
|
|
103
|
+
log: Log;
|
|
104
|
+
maxPoolSize: number;
|
|
105
|
+
createSessionFunction: CreateSession;
|
|
106
|
+
keyValueStore: KeyValueStore;
|
|
107
|
+
sessions: Session[];
|
|
108
|
+
sessionMap: Map<string, Session>;
|
|
109
|
+
sessionOptions: SessionOptions;
|
|
110
|
+
persistStateKeyValueStoreId?: string;
|
|
111
|
+
persistStateKey: string;
|
|
112
|
+
private _listener;
|
|
113
|
+
private events;
|
|
114
|
+
/**
|
|
115
|
+
* @internal
|
|
116
|
+
*/
|
|
117
|
+
constructor(options?: SessionPoolOptions, config?: Configuration);
|
|
118
|
+
/**
|
|
119
|
+
* Gets count of usable sessions in the pool.
|
|
120
|
+
*/
|
|
121
|
+
get usableSessionsCount(): number;
|
|
122
|
+
/**
|
|
123
|
+
* Gets count of retired sessions in the pool.
|
|
124
|
+
*/
|
|
125
|
+
get retiredSessionsCount(): number;
|
|
126
|
+
/**
|
|
127
|
+
* Starts periodic state persistence and potentially loads SessionPool state from {@link KeyValueStore}.
|
|
128
|
+
* It is called automatically by the {@link SessionPool.open} function.
|
|
129
|
+
*/
|
|
130
|
+
initialize(): Promise<void>;
|
|
131
|
+
/**
|
|
132
|
+
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
133
|
+
* but this allows you to add more sessions once the max pool size is reached.
|
|
134
|
+
* This also allows you to add session with overridden session options (e.g. with specific session id).
|
|
135
|
+
* @param [options] The configuration options for the session being added to the session pool.
|
|
136
|
+
*/
|
|
137
|
+
addSession(options?: Session | SessionOptions): Promise<void>;
|
|
138
|
+
/**
|
|
139
|
+
* Gets session.
|
|
140
|
+
* If there is space for new session, it creates and returns new session.
|
|
141
|
+
* If the session pool is full, it picks a session from the pool,
|
|
142
|
+
* If the picked session is usable it is returned, otherwise it creates and returns a new one.
|
|
143
|
+
*/
|
|
144
|
+
getSession(): Promise<Session>;
|
|
145
|
+
/**
|
|
146
|
+
* Gets session based on the provided session id or `undefined.
|
|
147
|
+
*/
|
|
148
|
+
getSession(sessionId: string): Promise<Session>;
|
|
149
|
+
/**
|
|
150
|
+
* Returns an object representing the internal state of the `SessionPool` instance.
|
|
151
|
+
* Note that the object's fields can change in future releases.
|
|
152
|
+
*/
|
|
153
|
+
getState(): {
|
|
154
|
+
usableSessionsCount: number;
|
|
155
|
+
retiredSessionsCount: number;
|
|
156
|
+
sessions: import("./session").SessionState[];
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Persists the current state of the `SessionPool` into the default {@link KeyValueStore}.
|
|
160
|
+
* The state is persisted automatically in regular intervals.
|
|
161
|
+
*/
|
|
162
|
+
persistState(): Promise<void>;
|
|
163
|
+
/**
|
|
164
|
+
* Removes listener from `persistState` event.
|
|
165
|
+
* This function should be called after you are done with using the `SessionPool` instance.
|
|
166
|
+
*/
|
|
167
|
+
teardown(): Promise<void>;
|
|
168
|
+
/**
|
|
169
|
+
* SessionPool should not work before initialization.
|
|
170
|
+
*/
|
|
171
|
+
protected _throwIfNotInitialized(): void;
|
|
172
|
+
/**
|
|
173
|
+
* Removes retired `Session` instances from `SessionPool`.
|
|
174
|
+
*/
|
|
175
|
+
protected _removeRetiredSessions(): void;
|
|
176
|
+
/**
|
|
177
|
+
* Adds `Session` instance to `SessionPool`.
|
|
178
|
+
* @param newSession `Session` instance to be added.
|
|
179
|
+
*/
|
|
180
|
+
protected _addSession(newSession: Session): void;
|
|
181
|
+
/**
|
|
182
|
+
* Gets random index.
|
|
183
|
+
*/
|
|
184
|
+
protected _getRandomIndex(): number;
|
|
185
|
+
/**
|
|
186
|
+
* Creates new session without any extra behavior.
|
|
187
|
+
* @param sessionPool
|
|
188
|
+
* @param [options]
|
|
189
|
+
* @param [options.sessionOptions] The configuration options for the session being created.
|
|
190
|
+
* @returns New session.
|
|
191
|
+
*/
|
|
192
|
+
protected _defaultCreateSessionFunction(sessionPool: SessionPool, options?: {
|
|
193
|
+
sessionOptions?: SessionOptions;
|
|
194
|
+
}): Session;
|
|
195
|
+
/**
|
|
196
|
+
* Creates new session and adds it to the pool.
|
|
197
|
+
* @returns Newly created `Session` instance.
|
|
198
|
+
*/
|
|
199
|
+
protected _createSession(): Promise<Session>;
|
|
200
|
+
/**
|
|
201
|
+
* Decides whether there is enough space for creating new session.
|
|
202
|
+
*/
|
|
203
|
+
protected _hasSpaceForSession(): boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Picks random session from the `SessionPool`.
|
|
206
|
+
* @returns Picked `Session`.
|
|
207
|
+
*/
|
|
208
|
+
protected _pickSession(): Session;
|
|
209
|
+
/**
|
|
210
|
+
* Potentially loads `SessionPool`.
|
|
211
|
+
* If the state was persisted it loads the `SessionPool` from the persisted state.
|
|
212
|
+
*/
|
|
213
|
+
protected _maybeLoadSessionPool(): Promise<void>;
|
|
214
|
+
/**
|
|
215
|
+
* Opens a SessionPool and returns a promise resolving to an instance
|
|
216
|
+
* of the {@link SessionPool} class that is already initialized.
|
|
217
|
+
*
|
|
218
|
+
* For more details and code examples, see the {@link SessionPool} class.
|
|
219
|
+
*/
|
|
220
|
+
static open(options?: SessionPoolOptions): Promise<SessionPool>;
|
|
221
|
+
}
|
|
222
|
+
//# sourceMappingURL=session_pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session_pool.d.ts","sourceRoot":"","sources":["../../src/session_pool/session_pool.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGpD;;GAEG;AACH,MAAM,WAAW,aAAa;IAC1B;;;OAGG;IACH,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,E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