apify 2.3.1-beta.4 → 3.0.0-alpha.0
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 +6 -5
- package/package.json +69 -128
- package/build/actor.d.ts +0 -113
- package/build/actor.d.ts.map +0 -1
- package/build/actor.js +0 -582
- package/build/actor.js.map +0 -1
- package/build/apify.d.ts +0 -752
- package/build/apify.d.ts.map +0 -1
- package/build/apify.js +0 -877
- package/build/apify.js.map +0 -1
- package/build/autoscaling/autoscaled_pool.d.ts +0 -384
- package/build/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/build/autoscaling/autoscaled_pool.js +0 -557
- package/build/autoscaling/autoscaled_pool.js.map +0 -1
- package/build/autoscaling/snapshotter.d.ts +0 -278
- package/build/autoscaling/snapshotter.d.ts.map +0 -1
- package/build/autoscaling/snapshotter.js +0 -447
- package/build/autoscaling/snapshotter.js.map +0 -1
- package/build/autoscaling/system_status.d.ts +0 -224
- package/build/autoscaling/system_status.d.ts.map +0 -1
- package/build/autoscaling/system_status.js +0 -228
- package/build/autoscaling/system_status.js.map +0 -1
- package/build/browser_launchers/browser_launcher.d.ts +0 -154
- package/build/browser_launchers/browser_launcher.d.ts.map +0 -1
- package/build/browser_launchers/browser_launcher.js +0 -160
- package/build/browser_launchers/browser_launcher.js.map +0 -1
- package/build/browser_launchers/browser_plugin.d.ts +0 -23
- package/build/browser_launchers/browser_plugin.d.ts.map +0 -1
- package/build/browser_launchers/browser_plugin.js +0 -25
- package/build/browser_launchers/browser_plugin.js.map +0 -1
- package/build/browser_launchers/playwright_launcher.d.ts +0 -131
- package/build/browser_launchers/playwright_launcher.d.ts.map +0 -1
- package/build/browser_launchers/playwright_launcher.js +0 -150
- package/build/browser_launchers/playwright_launcher.js.map +0 -1
- package/build/browser_launchers/puppeteer_launcher.d.ts +0 -153
- package/build/browser_launchers/puppeteer_launcher.d.ts.map +0 -1
- package/build/browser_launchers/puppeteer_launcher.js +0 -197
- package/build/browser_launchers/puppeteer_launcher.js.map +0 -1
- package/build/cache_container.d.ts +0 -31
- package/build/cache_container.d.ts.map +0 -1
- package/build/cache_container.js +0 -48
- package/build/cache_container.js.map +0 -1
- package/build/configuration.d.ts +0 -226
- package/build/configuration.d.ts.map +0 -1
- package/build/configuration.js +0 -325
- package/build/configuration.js.map +0 -1
- package/build/constants.d.ts +0 -37
- package/build/constants.d.ts.map +0 -1
- package/build/constants.js +0 -41
- package/build/constants.js.map +0 -1
- package/build/crawlers/basic_crawler.d.ts +0 -443
- package/build/crawlers/basic_crawler.d.ts.map +0 -1
- package/build/crawlers/basic_crawler.js +0 -664
- package/build/crawlers/basic_crawler.js.map +0 -1
- package/build/crawlers/browser_crawler.d.ts +0 -512
- package/build/crawlers/browser_crawler.d.ts.map +0 -1
- package/build/crawlers/browser_crawler.js +0 -540
- package/build/crawlers/browser_crawler.js.map +0 -1
- package/build/crawlers/cheerio_crawler.d.ts +0 -931
- package/build/crawlers/cheerio_crawler.d.ts.map +0 -1
- package/build/crawlers/cheerio_crawler.js +0 -913
- package/build/crawlers/cheerio_crawler.js.map +0 -1
- package/build/crawlers/crawler_extension.d.ts +0 -10
- package/build/crawlers/crawler_extension.d.ts.map +0 -1
- package/build/crawlers/crawler_extension.js +0 -19
- package/build/crawlers/crawler_extension.js.map +0 -1
- package/build/crawlers/crawler_utils.d.ts +0 -34
- package/build/crawlers/crawler_utils.d.ts.map +0 -1
- package/build/crawlers/crawler_utils.js +0 -87
- package/build/crawlers/crawler_utils.js.map +0 -1
- package/build/crawlers/playwright_crawler.d.ts +0 -448
- package/build/crawlers/playwright_crawler.d.ts.map +0 -1
- package/build/crawlers/playwright_crawler.js +0 -299
- package/build/crawlers/playwright_crawler.js.map +0 -1
- package/build/crawlers/puppeteer_crawler.d.ts +0 -425
- package/build/crawlers/puppeteer_crawler.d.ts.map +0 -1
- package/build/crawlers/puppeteer_crawler.js +0 -299
- package/build/crawlers/puppeteer_crawler.js.map +0 -1
- package/build/crawlers/statistics.d.ts +0 -185
- package/build/crawlers/statistics.d.ts.map +0 -1
- package/build/crawlers/statistics.js +0 -331
- package/build/crawlers/statistics.js.map +0 -1
- package/build/enqueue_links/click_elements.d.ts +0 -179
- package/build/enqueue_links/click_elements.d.ts.map +0 -1
- package/build/enqueue_links/click_elements.js +0 -434
- package/build/enqueue_links/click_elements.js.map +0 -1
- package/build/enqueue_links/enqueue_links.d.ts +0 -117
- package/build/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/build/enqueue_links/enqueue_links.js +0 -163
- package/build/enqueue_links/enqueue_links.js.map +0 -1
- package/build/enqueue_links/shared.d.ts +0 -42
- package/build/enqueue_links/shared.d.ts.map +0 -1
- package/build/enqueue_links/shared.js +0 -121
- package/build/enqueue_links/shared.js.map +0 -1
- package/build/errors.d.ts +0 -29
- package/build/errors.d.ts.map +0 -1
- package/build/errors.js +0 -38
- package/build/errors.js.map +0 -1
- package/build/events.d.ts +0 -11
- package/build/events.d.ts.map +0 -1
- package/build/events.js +0 -147
- package/build/events.js.map +0 -1
- package/build/index.d.ts +0 -4
- package/build/index.d.ts.map +0 -1
- package/build/index.js +0 -7
- package/build/index.js.map +0 -1
- package/build/main.d.ts +0 -179
- package/build/main.d.ts.map +0 -1
- package/build/main.js +0 -81
- package/build/main.js.map +0 -1
- package/build/playwright_utils.d.ts +0 -9
- package/build/playwright_utils.d.ts.map +0 -1
- package/build/playwright_utils.js +0 -90
- package/build/playwright_utils.js.map +0 -1
- package/build/proxy_configuration.d.ts +0 -411
- package/build/proxy_configuration.d.ts.map +0 -1
- package/build/proxy_configuration.js +0 -517
- package/build/proxy_configuration.js.map +0 -1
- package/build/pseudo_url.d.ts +0 -86
- package/build/pseudo_url.d.ts.map +0 -1
- package/build/pseudo_url.js +0 -153
- package/build/pseudo_url.js.map +0 -1
- package/build/puppeteer_request_interception.d.ts +0 -8
- package/build/puppeteer_request_interception.d.ts.map +0 -1
- package/build/puppeteer_request_interception.js +0 -235
- package/build/puppeteer_request_interception.js.map +0 -1
- package/build/puppeteer_utils.d.ts +0 -250
- package/build/puppeteer_utils.d.ts.map +0 -1
- package/build/puppeteer_utils.js +0 -551
- package/build/puppeteer_utils.js.map +0 -1
- package/build/request.d.ts +0 -180
- package/build/request.d.ts.map +0 -1
- package/build/request.js +0 -261
- package/build/request.js.map +0 -1
- package/build/request_list.d.ts +0 -581
- package/build/request_list.d.ts.map +0 -1
- package/build/request_list.js +0 -826
- package/build/request_list.js.map +0 -1
- package/build/serialization.d.ts +0 -5
- package/build/serialization.d.ts.map +0 -1
- package/build/serialization.js +0 -139
- package/build/serialization.js.map +0 -1
- package/build/session_pool/errors.d.ts +0 -11
- package/build/session_pool/errors.d.ts.map +0 -1
- package/build/session_pool/errors.js +0 -18
- package/build/session_pool/errors.js.map +0 -1
- package/build/session_pool/events.d.ts +0 -5
- package/build/session_pool/events.d.ts.map +0 -1
- package/build/session_pool/events.js +0 -6
- package/build/session_pool/events.js.map +0 -1
- package/build/session_pool/session.d.ts +0 -286
- package/build/session_pool/session.d.ts.map +0 -1
- package/build/session_pool/session.js +0 -355
- package/build/session_pool/session.js.map +0 -1
- package/build/session_pool/session_pool.d.ts +0 -280
- package/build/session_pool/session_pool.d.ts.map +0 -1
- package/build/session_pool/session_pool.js +0 -393
- package/build/session_pool/session_pool.js.map +0 -1
- package/build/session_pool/session_utils.d.ts +0 -4
- package/build/session_pool/session_utils.d.ts.map +0 -1
- package/build/session_pool/session_utils.js +0 -24
- package/build/session_pool/session_utils.js.map +0 -1
- package/build/stealth/hiding_tricks.d.ts +0 -22
- package/build/stealth/hiding_tricks.d.ts.map +0 -1
- package/build/stealth/hiding_tricks.js +0 -308
- package/build/stealth/hiding_tricks.js.map +0 -1
- package/build/stealth/stealth.d.ts +0 -56
- package/build/stealth/stealth.d.ts.map +0 -1
- package/build/stealth/stealth.js +0 -125
- package/build/stealth/stealth.js.map +0 -1
- package/build/storages/dataset.d.ts +0 -288
- package/build/storages/dataset.d.ts.map +0 -1
- package/build/storages/dataset.js +0 -480
- package/build/storages/dataset.js.map +0 -1
- package/build/storages/key_value_store.d.ts +0 -243
- package/build/storages/key_value_store.d.ts.map +0 -1
- package/build/storages/key_value_store.js +0 -462
- package/build/storages/key_value_store.js.map +0 -1
- package/build/storages/request_queue.d.ts +0 -318
- package/build/storages/request_queue.d.ts.map +0 -1
- package/build/storages/request_queue.js +0 -636
- package/build/storages/request_queue.js.map +0 -1
- package/build/storages/storage_manager.d.ts +0 -87
- package/build/storages/storage_manager.d.ts.map +0 -1
- package/build/storages/storage_manager.js +0 -150
- package/build/storages/storage_manager.js.map +0 -1
- package/build/tsconfig.tsbuildinfo +0 -1
- package/build/typedefs.d.ts +0 -146
- package/build/typedefs.d.ts.map +0 -1
- package/build/typedefs.js +0 -88
- package/build/typedefs.js.map +0 -1
- package/build/utils.d.ts +0 -175
- package/build/utils.d.ts.map +0 -1
- package/build/utils.js +0 -731
- package/build/utils.js.map +0 -1
- package/build/utils_log.d.ts +0 -41
- package/build/utils_log.d.ts.map +0 -1
- package/build/utils_log.js +0 -192
- package/build/utils_log.js.map +0 -1
- package/build/utils_request.d.ts +0 -77
- package/build/utils_request.d.ts.map +0 -1
- package/build/utils_request.js +0 -385
- package/build/utils_request.js.map +0 -1
- package/build/utils_social.d.ts +0 -210
- package/build/utils_social.d.ts.map +0 -1
- package/build/utils_social.js +0 -787
- package/build/utils_social.js.map +0 -1
- package/build/validators.d.ts +0 -23
- package/build/validators.d.ts.map +0 -1
- package/build/validators.js +0 -29
- package/build/validators.js.map +0 -1
|
@@ -1,355 +0,0 @@
|
|
|
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 = (0, tslib_1.__importDefault)(require("ow"));
|
|
7
|
-
const tough_cookie_1 = require("tough-cookie");
|
|
8
|
-
const events_1 = (0, tslib_1.__importDefault)(require("./events"));
|
|
9
|
-
const constants_1 = require("../constants");
|
|
10
|
-
const session_utils_1 = require("./session_utils");
|
|
11
|
-
const utils_log_1 = (0, tslib_1.__importDefault)(require("../utils_log"));
|
|
12
|
-
// TYPE IMPORTS
|
|
13
|
-
/* eslint-disable no-unused-vars,import/named,import/no-duplicates,import/order,import/no-cycle */
|
|
14
|
-
const session_pool_1 = require("./session_pool");
|
|
15
|
-
/* eslint-enable no-unused-vars,import/named,import/no-duplicates,import/order,import/no-cycle */
|
|
16
|
-
/**
|
|
17
|
-
* @typedef {object} PuppeteerCookie
|
|
18
|
-
*/
|
|
19
|
-
// CONSTANTS
|
|
20
|
-
const DEFAULT_SESSION_MAX_AGE_SECS = 3000;
|
|
21
|
-
/**
|
|
22
|
-
* Persistable {@link Session} state.
|
|
23
|
-
* @typedef SessionState
|
|
24
|
-
* @property {string} id
|
|
25
|
-
* @property {CookieJar} cookieJar
|
|
26
|
-
* @property {object} userData
|
|
27
|
-
* @property {number} errorScore
|
|
28
|
-
* @property {number} maxErrorScore
|
|
29
|
-
* @property {number} errorScoreDecrement
|
|
30
|
-
* @property {number} usageCount
|
|
31
|
-
* @property {string} expiresAt
|
|
32
|
-
* @property {string} createdAt
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
|
-
* @typedef SessionOptions
|
|
36
|
-
* @property {string} [id] - Id of session used for generating fingerprints. It is used as proxy session name.
|
|
37
|
-
* @property {number} [maxAgeSecs=3000] - Number of seconds after which the session is considered as expired.
|
|
38
|
-
* @property {Object<string,*>} [userData] - Object where custom user data can be stored. For example custom headers.
|
|
39
|
-
* @property {number} [maxErrorScore=3] - Maximum number of marking session as blocked usage.
|
|
40
|
-
* If the `errorScore` reaches the `maxErrorScore` session is marked as block and it is thrown away.
|
|
41
|
-
* It starts at 0. Calling the `markBad` function increases the `errorScore` by 1.
|
|
42
|
-
* Calling the `markGood` will decrease the `errorScore` by `errorScoreDecrement`
|
|
43
|
-
* @property {number} [errorScoreDecrement=0.5] - It is used for healing the session.
|
|
44
|
-
* For example: if your session is marked bad two times, but it is successful on the third attempt it's errorScore is decremented by this
|
|
45
|
-
* number.
|
|
46
|
-
* @property {Date} [createdAt] - Date of creation.
|
|
47
|
-
* @property {Date} [expiresAt] - Date of expiration.
|
|
48
|
-
* @property {number} [usageCount=0] - Indicates how many times the session has been used.
|
|
49
|
-
* @property {number} [errorCount=0] - Indicates how many times the session is marked bad.
|
|
50
|
-
* @property {number} [maxUsageCount=50] - Session should be used only a limited amount of times.
|
|
51
|
-
* This number indicates how many times the session is going to be used, before it is thrown away.
|
|
52
|
-
* @property {SessionPool} sessionPool - SessionPool instance. Session will emit the `sessionRetired` event on this instance.
|
|
53
|
-
*/
|
|
54
|
-
/**
|
|
55
|
-
* Sessions are used to store information such as cookies and can be used for generating fingerprints and proxy sessions.
|
|
56
|
-
* You can imagine each session as a specific user, with its own cookies, IP (via proxy) and potentially a unique browser fingerprint.
|
|
57
|
-
* Session internal state can be enriched with custom user data for example some authorization tokens and specific headers in general.
|
|
58
|
-
*
|
|
59
|
-
*/
|
|
60
|
-
class Session {
|
|
61
|
-
/**
|
|
62
|
-
* Session configuration.
|
|
63
|
-
*
|
|
64
|
-
* @param {SessionOptions} options
|
|
65
|
-
*/
|
|
66
|
-
constructor(options) {
|
|
67
|
-
(0, ow_1.default)(options, ow_1.default.object.exactShape({
|
|
68
|
-
sessionPool: ow_1.default.object.instanceOf(session_pool_1.SessionPool),
|
|
69
|
-
id: ow_1.default.optional.string,
|
|
70
|
-
cookieJar: ow_1.default.optional.object,
|
|
71
|
-
maxAgeSecs: ow_1.default.optional.number,
|
|
72
|
-
userData: ow_1.default.optional.object,
|
|
73
|
-
maxErrorScore: ow_1.default.optional.number,
|
|
74
|
-
errorScoreDecrement: ow_1.default.optional.number,
|
|
75
|
-
createdAt: ow_1.default.optional.date,
|
|
76
|
-
expiresAt: ow_1.default.optional.date,
|
|
77
|
-
usageCount: ow_1.default.optional.number,
|
|
78
|
-
errorScore: ow_1.default.optional.number,
|
|
79
|
-
maxUsageCount: ow_1.default.optional.number,
|
|
80
|
-
log: ow_1.default.optional.object,
|
|
81
|
-
}));
|
|
82
|
-
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 = utils_log_1.default, } = options;
|
|
83
|
-
const { expiresAt = this._getDefaultCookieExpirationDate(maxAgeSecs) } = options;
|
|
84
|
-
this.log = log.child({ prefix: 'Session' });
|
|
85
|
-
/**
|
|
86
|
-
* @type {CookieJar}
|
|
87
|
-
* @private
|
|
88
|
-
*/
|
|
89
|
-
this.cookieJar = cookieJar.setCookie ? cookieJar : tough_cookie_1.CookieJar.fromJSON(JSON.stringify(cookieJar));
|
|
90
|
-
this.id = id;
|
|
91
|
-
this.maxAgeSecs = maxAgeSecs;
|
|
92
|
-
/** @type {Object<string,*>} */
|
|
93
|
-
this.userData = userData;
|
|
94
|
-
this.maxErrorScore = maxErrorScore;
|
|
95
|
-
this.errorScoreDecrement = errorScoreDecrement;
|
|
96
|
-
// Internal
|
|
97
|
-
this.expiresAt = expiresAt;
|
|
98
|
-
this.createdAt = createdAt;
|
|
99
|
-
this.usageCount = usageCount; // indicates how many times the session has been used
|
|
100
|
-
this.errorScore = errorScore; // indicates number of markBaded request with the session
|
|
101
|
-
this.maxUsageCount = maxUsageCount;
|
|
102
|
-
this.sessionPool = sessionPool;
|
|
103
|
-
this._puppeteerCookieToTough = this._puppeteerCookieToTough.bind(this);
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* indicates whether the session is blocked.
|
|
107
|
-
* Session is blocked once it reaches the `maxErrorScore`.
|
|
108
|
-
* @return {boolean}
|
|
109
|
-
*/
|
|
110
|
-
isBlocked() {
|
|
111
|
-
return this.errorScore >= this.maxErrorScore;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Indicates whether the session is expired.
|
|
115
|
-
* Session expiration is determined by the `maxAgeSecs`.
|
|
116
|
-
* Once the session is older than `createdAt + maxAgeSecs` the session is considered expired.
|
|
117
|
-
* @return {boolean}
|
|
118
|
-
*/
|
|
119
|
-
isExpired() {
|
|
120
|
-
return this.expiresAt <= new Date();
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Indicates whether the session is used maximum number of times.
|
|
124
|
-
* Session maximum usage count can be changed by `maxUsageCount` parameter.
|
|
125
|
-
* @return {boolean}
|
|
126
|
-
*/
|
|
127
|
-
isMaxUsageCountReached() {
|
|
128
|
-
return this.usageCount >= this.maxUsageCount;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Indicates whether the session can be used for next requests.
|
|
132
|
-
* Session is usable when it is not expired, not blocked and the maximum usage count has not be reached.
|
|
133
|
-
* @return {boolean}
|
|
134
|
-
*/
|
|
135
|
-
isUsable() {
|
|
136
|
-
return !this.isBlocked() && !this.isExpired() && !this.isMaxUsageCountReached();
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* This method should be called after a successful session usage.
|
|
140
|
-
* It increases `usageCount` and potentially lowers the `errorScore` by the `errorScoreDecrement`.
|
|
141
|
-
*/
|
|
142
|
-
markGood() {
|
|
143
|
-
this.usageCount += 1;
|
|
144
|
-
if (this.errorScore > 0) {
|
|
145
|
-
this.errorScore -= this.errorScoreDecrement;
|
|
146
|
-
}
|
|
147
|
-
this._maybeSelfRetire();
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Gets session state for persistence in KeyValueStore.
|
|
151
|
-
* @return {SessionState} represents session internal state.
|
|
152
|
-
*/
|
|
153
|
-
getState() {
|
|
154
|
-
return {
|
|
155
|
-
id: this.id,
|
|
156
|
-
cookieJar: this.cookieJar.toJSON(),
|
|
157
|
-
userData: this.userData,
|
|
158
|
-
maxErrorScore: this.maxErrorScore,
|
|
159
|
-
errorScoreDecrement: this.errorScoreDecrement,
|
|
160
|
-
expiresAt: this.expiresAt.toISOString(),
|
|
161
|
-
createdAt: this.createdAt.toISOString(),
|
|
162
|
-
usageCount: this.usageCount,
|
|
163
|
-
maxUsageCount: this.maxUsageCount,
|
|
164
|
-
errorScore: this.errorScore,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Marks session as blocked and emits event on the `SessionPool`
|
|
169
|
-
* This method should be used if the session usage was unsuccessful
|
|
170
|
-
* and you are sure that it is because of the session configuration and not any external matters.
|
|
171
|
-
* For example when server returns 403 status code.
|
|
172
|
-
* If the session does not work due to some external factors as server error such as 5XX you probably want to use `markBad` method.
|
|
173
|
-
*/
|
|
174
|
-
retire() {
|
|
175
|
-
// mark it as an invalid by increasing the error score count.
|
|
176
|
-
this.errorScore += this.maxErrorScore;
|
|
177
|
-
this.usageCount += 1;
|
|
178
|
-
// emit event so we can retire browser in puppeteer pool
|
|
179
|
-
this.sessionPool.emit(events_1.default.SESSION_RETIRED, this);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Increases usage and error count.
|
|
183
|
-
* Should be used when the session has been used unsuccessfully. For example because of timeouts.
|
|
184
|
-
*/
|
|
185
|
-
markBad() {
|
|
186
|
-
this.errorScore += 1;
|
|
187
|
-
this.usageCount += 1;
|
|
188
|
-
this._maybeSelfRetire();
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* With certain status codes: `401`, `403` or `429` we can be certain
|
|
192
|
-
* that the target website is blocking us. This function helps to do this conveniently
|
|
193
|
-
* by retiring the session when such code is received. Optionally the default status
|
|
194
|
-
* codes can be extended in the second parameter.
|
|
195
|
-
* @param statusCode {number} - HTTP status code
|
|
196
|
-
* @param [blockedStatusCodes] {number[]} - Custom HTTP status codes that means blocking on particular website.
|
|
197
|
-
* @return {boolean} whether the session was retired.
|
|
198
|
-
*/
|
|
199
|
-
retireOnBlockedStatusCodes(statusCode, blockedStatusCodes = []) {
|
|
200
|
-
const isBlocked = constants_1.STATUS_CODES_BLOCKED.concat(blockedStatusCodes).includes(statusCode);
|
|
201
|
-
if (isBlocked) {
|
|
202
|
-
this.retire();
|
|
203
|
-
}
|
|
204
|
-
return isBlocked;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Saves cookies from an HTTP response to be used with the session.
|
|
208
|
-
* It expects an object with a `headers` property that's either an `Object`
|
|
209
|
-
* (typical Node.js responses) or a `Function` (Puppeteer Response).
|
|
210
|
-
*
|
|
211
|
-
* It then parses and saves the cookies from the `set-cookie` header, if available.
|
|
212
|
-
|
|
213
|
-
* @param {(PuppeteerResponse|IncomingMessage)} response
|
|
214
|
-
*/
|
|
215
|
-
setCookiesFromResponse(response) {
|
|
216
|
-
try {
|
|
217
|
-
const cookies = (0, session_utils_1.getCookiesFromResponse)(response).filter((c) => c);
|
|
218
|
-
this._setCookies(cookies, response.url);
|
|
219
|
-
}
|
|
220
|
-
catch (e) {
|
|
221
|
-
// if invalid Cookie header is provided just log the exception.
|
|
222
|
-
this.log.exception(e, 'Could not get cookies from response');
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Saves an array with cookie objects to be used with the session.
|
|
227
|
-
* The objects should be in the format that
|
|
228
|
-
* [Puppeteer uses](https://pptr.dev/#?product=Puppeteer&version=v2.0.0&show=api-pagecookiesurls),
|
|
229
|
-
* but you can also use this function to set cookies manually:
|
|
230
|
-
*
|
|
231
|
-
* ```
|
|
232
|
-
* [
|
|
233
|
-
* { name: 'cookie1', value: 'my-cookie' },
|
|
234
|
-
* { name: 'cookie2', value: 'your-cookie' }
|
|
235
|
-
* ]
|
|
236
|
-
* ```
|
|
237
|
-
*
|
|
238
|
-
* @param {PuppeteerCookie[]} cookies
|
|
239
|
-
* @param {string} url
|
|
240
|
-
*/
|
|
241
|
-
setPuppeteerCookies(cookies, url) {
|
|
242
|
-
const normalizedCookies = cookies.map(this._puppeteerCookieToTough);
|
|
243
|
-
this._setCookies(normalizedCookies, url);
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* Returns cookies in a format compatible with puppeteer and ready to be used with `page.setCookie`.
|
|
247
|
-
* @param {string} url website url. Only cookies stored for this url will be returned
|
|
248
|
-
* @return {PuppeteerCookie[]}
|
|
249
|
-
*/
|
|
250
|
-
getPuppeteerCookies(url) {
|
|
251
|
-
const cookies = this.cookieJar.getCookiesSync(url);
|
|
252
|
-
return cookies.map(this._toughCookieToPuppeteer);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Returns cookies saved with the session in the typical
|
|
256
|
-
* key1=value1; key2=value2 format, ready to be used in
|
|
257
|
-
* a cookie header or elsewhere.
|
|
258
|
-
* @param {string} url
|
|
259
|
-
* @return {string} represents `Cookie` header.
|
|
260
|
-
*/
|
|
261
|
-
getCookieString(url) {
|
|
262
|
-
return this.cookieJar.getCookieStringSync(url, {});
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Transforms puppeteer cookie to tough-cookie.
|
|
266
|
-
* @param {PuppeteerCookie} puppeteerCookie Cookie from puppeteer `page.cookies method.
|
|
267
|
-
* @return {Cookie}
|
|
268
|
-
* @ignore
|
|
269
|
-
* @protected
|
|
270
|
-
* @internal
|
|
271
|
-
*/
|
|
272
|
-
_puppeteerCookieToTough(puppeteerCookie) {
|
|
273
|
-
const isExpiresValid = puppeteerCookie.expires && typeof puppeteerCookie.expires === 'number' && puppeteerCookie.expires > 0;
|
|
274
|
-
const expires = isExpiresValid ? new Date(puppeteerCookie.expires * 1000) : this._getDefaultCookieExpirationDate(this.maxAgeSecs);
|
|
275
|
-
const domain = typeof puppeteerCookie.domain === 'string' && puppeteerCookie.domain.startsWith('.')
|
|
276
|
-
? puppeteerCookie.domain.slice(1)
|
|
277
|
-
: puppeteerCookie.domain;
|
|
278
|
-
return new tough_cookie_1.Cookie({
|
|
279
|
-
key: puppeteerCookie.name,
|
|
280
|
-
value: puppeteerCookie.value,
|
|
281
|
-
expires,
|
|
282
|
-
domain,
|
|
283
|
-
path: puppeteerCookie.path,
|
|
284
|
-
secure: puppeteerCookie.secure,
|
|
285
|
-
httpOnly: puppeteerCookie.httpOnly,
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Transforms tough-cookie to puppeteer cookie .
|
|
290
|
-
* @param {Cookie} toughCookie - Cookie from CookieJar
|
|
291
|
-
* @return {PuppeteerCookie} - Cookie from Puppeteer
|
|
292
|
-
* @ignore
|
|
293
|
-
* @protected
|
|
294
|
-
* @internal
|
|
295
|
-
*/
|
|
296
|
-
_toughCookieToPuppeteer(toughCookie) {
|
|
297
|
-
return {
|
|
298
|
-
name: toughCookie.key,
|
|
299
|
-
value: toughCookie.value,
|
|
300
|
-
// Puppeteer and Playwright expect 'expires' to be 'Unix time in seconds', not ms
|
|
301
|
-
expires: new Date(toughCookie.expires).getTime() / 1000,
|
|
302
|
-
domain: toughCookie.domain,
|
|
303
|
-
path: toughCookie.path,
|
|
304
|
-
secure: toughCookie.secure,
|
|
305
|
-
httpOnly: toughCookie.httpOnly,
|
|
306
|
-
};
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Sets cookies.
|
|
310
|
-
* @param {Cookie[]} cookies
|
|
311
|
-
* @param {string} url
|
|
312
|
-
* @ignore
|
|
313
|
-
* @protected
|
|
314
|
-
* @internal
|
|
315
|
-
*/
|
|
316
|
-
_setCookies(cookies, url) {
|
|
317
|
-
const errorMessages = [];
|
|
318
|
-
for (const cookie of cookies) {
|
|
319
|
-
try {
|
|
320
|
-
this.cookieJar.setCookieSync(cookie, url, { ignoreError: false });
|
|
321
|
-
}
|
|
322
|
-
catch (e) {
|
|
323
|
-
errorMessages.push(e.message);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
// if invalid cookies are provided just log the exception. No need to retry the request automatically.
|
|
327
|
-
if (errorMessages.length) {
|
|
328
|
-
this.log.debug('Could not set cookies.', { errorMessages });
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* Calculate cookie expiration date
|
|
333
|
-
* @param {number} maxAgeSecs
|
|
334
|
-
* @return {Date} - calculated date by session max age seconds.
|
|
335
|
-
* @ignore
|
|
336
|
-
* @protected
|
|
337
|
-
* @internal
|
|
338
|
-
*/
|
|
339
|
-
_getDefaultCookieExpirationDate(maxAgeSecs) {
|
|
340
|
-
return new Date(Date.now() + (maxAgeSecs * 1000));
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Checks if session is not usable. if it is not retires the session.
|
|
344
|
-
* @ignore
|
|
345
|
-
* @protected
|
|
346
|
-
* @internal
|
|
347
|
-
*/
|
|
348
|
-
_maybeSelfRetire() {
|
|
349
|
-
if (!this.isUsable()) {
|
|
350
|
-
this.retire();
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
exports.Session = Session;
|
|
355
|
-
//# sourceMappingURL=session.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session.js","sourceRoot":"","sources":["../../src/session_pool/session.js"],"names":[],"mappings":";;;;AAAA,gDAAwD;AACxD,yDAAoB;AACpB,+CAAiD;AACjD,mEAA8B;AAC9B,4CAAoD;AACpD,mDAAyD;AACzD,0EAAsC;AAEtC,eAAe;AACf,kGAAkG;AAClG,iDAA6C;AAG7C,iGAAiG;AAEjG;;GAEG;AAEH,YAAY;AACZ,MAAM,4BAA4B,GAAG,IAAI,CAAC;AAE1C;;;;;;;;;;;;GAYG;AAEH;;;;;;;;;;;;;;;;;;;GAmBG;AAEH;;;;;GAKG;AACH,MAAa,OAAO;IAChB;;;;OAIG;IACH,YAAY,OAAO;QACf,IAAA,YAAE,EAAC,OAAO,EAAE,YAAE,CAAC,MAAM,CAAC,UAAU,CAAC;YAC7B,WAAW,EAAE,YAAE,CAAC,MAAM,CAAC,UAAU,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AE,CAAC;QACzB,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,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;aACjC;SACJ;QACD,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;;;;;;;OAOG;IACH,+BAA+B,CAAC,UAAU;QACtC,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,GAAG,IAAI,CAAC,CAAC,CAAC;IACtD,CAAC;IAED;;;;;OAKG;IACH,gBAAgB;QACZ,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,EAAE;YAClB,IAAI,CAAC,MAAM,EAAE,CAAC;SACjB;IACL,CAAC;CACJ;AA9UD,0BA8UC"}
|
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Factory user-function which creates customized {@link Session} instances.
|
|
3
|
-
* @callback CreateSession
|
|
4
|
-
* @param {SessionPool} sessionPool Pool requesting the new session.
|
|
5
|
-
* @returns {Promise<Session>}
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* @typedef SessionPoolOptions
|
|
9
|
-
* @property {number} [maxPoolSize=1000] - Maximum size of the pool.
|
|
10
|
-
* Indicates how many sessions are rotated.
|
|
11
|
-
* @property {SessionOptions} [sessionOptions] The configuration options for {@link Session} instances.
|
|
12
|
-
* @property {string} [persistStateKeyValueStoreId] - Name or Id of `KeyValueStore` where is the `SessionPool` state stored.
|
|
13
|
-
* @property {string} [persistStateKey="SESSION_POOL_STATE"] - Session pool persists it's state under this key in Key value store.
|
|
14
|
-
* @property {CreateSession} [createSessionFunction] - Custom function that should return `Session` instance.
|
|
15
|
-
* Any error thrown from this function will terminate the process.
|
|
16
|
-
* Function receives `SessionPool` instance as a parameter
|
|
17
|
-
* @property {boolean} [forceCloud=false] If set to `true` then the function uses cloud storage usage even if the `APIFY_LOCAL_STORAGE_DIR`
|
|
18
|
-
* environment variable is set. This way it is possible to combine local and cloud storage.
|
|
19
|
-
*
|
|
20
|
-
* **Note:** If you use `forceCloud`, it is recommended to also set the `persistStateKeyValueStoreId` option, as otherwise the
|
|
21
|
-
* `KeyValueStore` will be unnamed!
|
|
22
|
-
*/
|
|
23
|
-
/**
|
|
24
|
-
* Handles the rotation, creation and persistence of user-like sessions.
|
|
25
|
-
* Creates a pool of {@link Session} instances, that are randomly rotated.
|
|
26
|
-
* When some session is marked as blocked, it is removed and new one is created instead (the pool never returns an unusable session).
|
|
27
|
-
* Learn more in the [`Session management guide`](../guides/session-management).
|
|
28
|
-
*
|
|
29
|
-
* You can create one by calling the {@link Apify.openSessionPool} function.
|
|
30
|
-
*
|
|
31
|
-
* Session pool is already integrated into crawlers, and it can significantly improve your scraper
|
|
32
|
-
* performance with just 2 lines of code.
|
|
33
|
-
*
|
|
34
|
-
* **Example usage:**
|
|
35
|
-
*
|
|
36
|
-
* ```javascript
|
|
37
|
-
* const crawler = new Apify.CheerioCrawler({
|
|
38
|
-
* useSessionPool: true,
|
|
39
|
-
* persistCookiesPerSession: true,
|
|
40
|
-
* // ...
|
|
41
|
-
* })
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* You can configure the pool with many options. See the {@link SessionPoolOptions}.
|
|
45
|
-
* Session pool is by default persisted in default {@link KeyValueStore}.
|
|
46
|
-
* If you want to have one pool for all runs you have to specify
|
|
47
|
-
* {@link SessionPoolOptions.persistStateKeyValueStoreId}.
|
|
48
|
-
*
|
|
49
|
-
* **Advanced usage:**
|
|
50
|
-
*
|
|
51
|
-
* ```javascript
|
|
52
|
-
* const sessionPool = await Apify.openSessionPool({
|
|
53
|
-
* maxPoolSize: 25,
|
|
54
|
-
* sessionOptions:{
|
|
55
|
-
* maxAgeSecs: 10,
|
|
56
|
-
* maxUsageCount: 150, // for example when you know that the site blocks after 150 requests.
|
|
57
|
-
* },
|
|
58
|
-
* persistStateKeyValueStoreId: 'my-key-value-store-for-sessions',
|
|
59
|
-
* persistStateKey: 'my-session-pool',
|
|
60
|
-
* });
|
|
61
|
-
*
|
|
62
|
-
* // Get random session from the pool
|
|
63
|
-
* const session1 = await sessionPool.getSession();
|
|
64
|
-
* const session2 = await sessionPool.getSession();
|
|
65
|
-
* const session3 = await sessionPool.getSession();
|
|
66
|
-
*
|
|
67
|
-
* // Now you can mark the session either failed or successful
|
|
68
|
-
*
|
|
69
|
-
* // Marks session as bad after unsuccessful usage -> it increases error count (soft retire)
|
|
70
|
-
* session1.markBad()
|
|
71
|
-
*
|
|
72
|
-
* // Marks as successful.
|
|
73
|
-
* session2.markGood()
|
|
74
|
-
*
|
|
75
|
-
* // Retires session -> session is removed from the pool
|
|
76
|
-
* session3.retire()
|
|
77
|
-
*
|
|
78
|
-
* ```
|
|
79
|
-
* @hideconstructor
|
|
80
|
-
*/
|
|
81
|
-
export class SessionPool extends EventEmitter {
|
|
82
|
-
/**
|
|
83
|
-
* Session pool configuration.
|
|
84
|
-
* @param {SessionPoolOptions} [options] All `SessionPool` configuration options.
|
|
85
|
-
* @param {Configuration} [config]
|
|
86
|
-
*/
|
|
87
|
-
constructor(options?: SessionPoolOptions | undefined, config?: Configuration | undefined);
|
|
88
|
-
config: Configuration;
|
|
89
|
-
log: any;
|
|
90
|
-
maxPoolSize: number;
|
|
91
|
-
createSessionFunction: (CreateSession & Function) | ((sessionPool: SessionPool, options?: {
|
|
92
|
-
sessionOptions?: SessionOptions | undefined;
|
|
93
|
-
} | undefined) => Session);
|
|
94
|
-
sessionOptions: {
|
|
95
|
-
log: any;
|
|
96
|
-
};
|
|
97
|
-
persistStateKeyValueStoreId: string | undefined;
|
|
98
|
-
persistStateKey: string;
|
|
99
|
-
keyValueStore: import("../storages/key_value_store").KeyValueStore | null;
|
|
100
|
-
/** @type {Session[]} */
|
|
101
|
-
sessions: Session[];
|
|
102
|
-
sessionMap: Map<any, any>;
|
|
103
|
-
/** @type {boolean} */
|
|
104
|
-
forceCloud: boolean;
|
|
105
|
-
/**
|
|
106
|
-
* Gets count of usable sessions in the pool.
|
|
107
|
-
* @return {number}
|
|
108
|
-
*/
|
|
109
|
-
get usableSessionsCount(): number;
|
|
110
|
-
/**
|
|
111
|
-
* Gets count of retired sessions in the pool.
|
|
112
|
-
* @return {number}
|
|
113
|
-
*/
|
|
114
|
-
get retiredSessionsCount(): number;
|
|
115
|
-
/**
|
|
116
|
-
* Starts periodic state persistence and potentially loads SessionPool state from {@link KeyValueStore}.
|
|
117
|
-
* It is called automatically by the {@link Apify.openSessionPool} function.
|
|
118
|
-
*
|
|
119
|
-
* @return {Promise<void>}
|
|
120
|
-
*/
|
|
121
|
-
initialize(): Promise<void>;
|
|
122
|
-
_listener: (() => Promise<void>) | undefined;
|
|
123
|
-
/**
|
|
124
|
-
* Adds a new session to the session pool. The pool automatically creates sessions up to the maximum size of the pool,
|
|
125
|
-
* but this allows you to add more sessions once the max pool size is reached.
|
|
126
|
-
* This also allows you to add session with overridden session options (e.g. with specific session id).
|
|
127
|
-
* @param {Session|SessionOptions} [options] - The configuration options for the session being added to the session pool.
|
|
128
|
-
*/
|
|
129
|
-
addSession(options?: Session | SessionOptions | undefined): Promise<void>;
|
|
130
|
-
/**
|
|
131
|
-
* Gets session.
|
|
132
|
-
* If there is space for new session, it creates and returns new session.
|
|
133
|
-
* If the session pool is full, it picks a session from the pool,
|
|
134
|
-
* If the picked session is usable it is returned, otherwise it creates and returns a new one.
|
|
135
|
-
*
|
|
136
|
-
* @param {String} [sessionId] - If provided, it returns the usable session with this id, `undefined` otherwise.
|
|
137
|
-
* @return {Promise<Session>}
|
|
138
|
-
*/
|
|
139
|
-
getSession(sessionId?: string | undefined): Promise<Session>;
|
|
140
|
-
/**
|
|
141
|
-
* Returns an object representing the internal state of the `SessionPool` instance.
|
|
142
|
-
* Note that the object's fields can change in future releases.
|
|
143
|
-
*/
|
|
144
|
-
getState(): {
|
|
145
|
-
usableSessionsCount: number;
|
|
146
|
-
retiredSessionsCount: number;
|
|
147
|
-
sessions: import("./session").SessionState[];
|
|
148
|
-
};
|
|
149
|
-
/**
|
|
150
|
-
* Persists the current state of the `SessionPool` into the default {@link KeyValueStore}.
|
|
151
|
-
* The state is persisted automatically in regular intervals.
|
|
152
|
-
*
|
|
153
|
-
* @return {Promise<void>}
|
|
154
|
-
*/
|
|
155
|
-
persistState(): Promise<void>;
|
|
156
|
-
/**
|
|
157
|
-
* Removes listener from `persistState` event.
|
|
158
|
-
* This function should be called after you are done with using the `SessionPool` instance.
|
|
159
|
-
*/
|
|
160
|
-
teardown(): Promise<void>;
|
|
161
|
-
/**
|
|
162
|
-
* SessionPool should not work before initialization.
|
|
163
|
-
* @ignore
|
|
164
|
-
* @protected
|
|
165
|
-
* @internal
|
|
166
|
-
*/
|
|
167
|
-
protected _throwIfNotInitialized(): void;
|
|
168
|
-
/**
|
|
169
|
-
* Removes retired `Session` instances from `SessionPool`.
|
|
170
|
-
* @ignore
|
|
171
|
-
* @protected
|
|
172
|
-
* @internal
|
|
173
|
-
*/
|
|
174
|
-
protected _removeRetiredSessions(): void;
|
|
175
|
-
/**
|
|
176
|
-
* Adds `Session` instance to `SessionPool`.
|
|
177
|
-
* @param {Session} newSession `Session` instance to be added.
|
|
178
|
-
* @ignore
|
|
179
|
-
* @protected
|
|
180
|
-
* @internal
|
|
181
|
-
*/
|
|
182
|
-
protected _addSession(newSession: Session): void;
|
|
183
|
-
/**
|
|
184
|
-
* Gets random index.
|
|
185
|
-
* @return {number}
|
|
186
|
-
* @ignore
|
|
187
|
-
* @protected
|
|
188
|
-
* @internal
|
|
189
|
-
*/
|
|
190
|
-
protected _getRandomIndex(): number;
|
|
191
|
-
/**
|
|
192
|
-
* Creates new session without any extra behavior.
|
|
193
|
-
* @param {SessionPool} sessionPool
|
|
194
|
-
* @param {Object} [options]
|
|
195
|
-
* @param {SessionOptions} [options.sessionOptions] - The configuration options for the session being created
|
|
196
|
-
* @return {Session} - New session.
|
|
197
|
-
* @ignore
|
|
198
|
-
* @protected
|
|
199
|
-
* @internal
|
|
200
|
-
*/
|
|
201
|
-
protected _defaultCreateSessionFunction(sessionPool: SessionPool, options?: {
|
|
202
|
-
sessionOptions?: SessionOptions | undefined;
|
|
203
|
-
} | undefined): Session;
|
|
204
|
-
/**
|
|
205
|
-
* Creates new session and adds it to the pool.
|
|
206
|
-
* @return {Promise<Session>} - Newly created `Session` instance.
|
|
207
|
-
* @ignore
|
|
208
|
-
* @protected
|
|
209
|
-
* @internal
|
|
210
|
-
*/
|
|
211
|
-
protected _createSession(): Promise<Session>;
|
|
212
|
-
/**
|
|
213
|
-
* Decides whether there is enough space for creating new session.
|
|
214
|
-
* @return {boolean}
|
|
215
|
-
* @ignore
|
|
216
|
-
* @protected
|
|
217
|
-
* @internal
|
|
218
|
-
*/
|
|
219
|
-
protected _hasSpaceForSession(): boolean;
|
|
220
|
-
/**
|
|
221
|
-
* Picks random session from the `SessionPool`.
|
|
222
|
-
* @return {Session} - Picked `Session`
|
|
223
|
-
* @ignore
|
|
224
|
-
* @protected
|
|
225
|
-
* @internal
|
|
226
|
-
*/
|
|
227
|
-
protected _pickSession(): Session;
|
|
228
|
-
/**
|
|
229
|
-
* Potentially loads `SessionPool`.
|
|
230
|
-
* If the state was persisted it loads the `SessionPool` from the persisted state.
|
|
231
|
-
* @return {Promise<void>}
|
|
232
|
-
* @ignore
|
|
233
|
-
* @protected
|
|
234
|
-
* @internal
|
|
235
|
-
*/
|
|
236
|
-
protected _maybeLoadSessionPool(): Promise<void>;
|
|
237
|
-
}
|
|
238
|
-
export function openSessionPool(sessionPoolOptions: SessionPoolOptions): Promise<SessionPool>;
|
|
239
|
-
/**
|
|
240
|
-
* Factory user-function which creates customized {@link Session } instances.
|
|
241
|
-
*/
|
|
242
|
-
export type CreateSession = (sessionPool: SessionPool) => Promise<Session>;
|
|
243
|
-
export type SessionPoolOptions = {
|
|
244
|
-
/**
|
|
245
|
-
* - Maximum size of the pool.
|
|
246
|
-
* Indicates how many sessions are rotated.
|
|
247
|
-
*/
|
|
248
|
-
maxPoolSize?: number | undefined;
|
|
249
|
-
/**
|
|
250
|
-
* The configuration options for {@link Session } instances.
|
|
251
|
-
*/
|
|
252
|
-
sessionOptions?: SessionOptions | undefined;
|
|
253
|
-
/**
|
|
254
|
-
* - Name or Id of `KeyValueStore` where is the `SessionPool` state stored.
|
|
255
|
-
*/
|
|
256
|
-
persistStateKeyValueStoreId?: string | undefined;
|
|
257
|
-
/**
|
|
258
|
-
* - Session pool persists it's state under this key in Key value store.
|
|
259
|
-
*/
|
|
260
|
-
persistStateKey?: string | undefined;
|
|
261
|
-
/**
|
|
262
|
-
* - Custom function that should return `Session` instance.
|
|
263
|
-
* Any error thrown from this function will terminate the process.
|
|
264
|
-
* Function receives `SessionPool` instance as a parameter
|
|
265
|
-
*/
|
|
266
|
-
createSessionFunction?: CreateSession | undefined;
|
|
267
|
-
/**
|
|
268
|
-
* If set to `true` then the function uses cloud storage usage even if the `APIFY_LOCAL_STORAGE_DIR`
|
|
269
|
-
* environment variable is set. This way it is possible to combine local and cloud storage.
|
|
270
|
-
*
|
|
271
|
-
* **Note:** If you use `forceCloud`, it is recommended to also set the `persistStateKeyValueStoreId` option, as otherwise the
|
|
272
|
-
* `KeyValueStore` will be unnamed!
|
|
273
|
-
*/
|
|
274
|
-
forceCloud?: boolean | undefined;
|
|
275
|
-
};
|
|
276
|
-
import { EventEmitter } from "events";
|
|
277
|
-
import { Configuration } from "../configuration";
|
|
278
|
-
import { SessionOptions } from "./session";
|
|
279
|
-
import { Session } from "./session";
|
|
280
|
-
//# sourceMappingURL=session_pool.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"session_pool.d.ts","sourceRoot":"","sources":["../../src/session_pool/session_pool.js"],"names":[],"mappings":"AASA;;;;;GAKG;AAEH;;;;;;;;;;;;;;;GAeG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH;IACI;;;;OAIG;IACH,0FAqDC;IA1BG,sBAAoB;IACpB,SAA+C;IAG/C,oBAA8B;IAC9B,mEA6MO,WAAW;;sBAGV,OAAO,EAhNyE;IAGxF;;MAKC;IAGD,gDAA8D;IAC9D,wBAAsC;IAGtC,0EAAyB;IACzB,wBAAwB;IACxB,UADW,OAAO,EAAE,CACF;IAClB,0BAA2B;IAC3B,sBAAsB;IACtB,YADW,OAAO,CACU;IAGhC;;;OAGG;IACH,kCAEC;IAED;;;OAGG;IACH,mCAEC;IAED;;;;;OAKG;IACH,cAFY,QAAQ,IAAI,CAAC,CAgBxB;IAHG,kBA+EQ,QAAQ,IAAI,CAAC,cA/EwB;IAKjD;;;;;OAKG;IACH,0EAoBC;IAED;;;;;;;;OAQG;IACH,4CAFY,QAAQ,OAAO,CAAC,CAqB3B;IAED;;;OAGG;IACH;;;;MAMC;IAED;;;;;OAKG;IACH,gBAFY,QAAQ,IAAI,CAAC,CASxB;IAED;;;OAGG;IACH,0BAGC;IAED;;;;;OAKG;IACH,yCAEC;IAED;;;;;OAKG;IACH,yCAOC;IAED;;;;;;OAMG;IACH,kCALW,OAAO,QAQjB;IAED;;;;;;OAMG;IACH,6BALY,MAAM,CAOjB;IAED;;;;;;;;;OASG;IACH,qDARW,WAAW;;oBAGV,OAAO,CAalB;IAED;;;;;;OAMG;IACH,4BALY,QAAQ,OAAO,CAAC,CAY3B;IAED;;;;;;OAMG;IACH,iCALY,OAAO,CAOlB;IAED;;;;;;OAMG;IACH,0BALY,OAAO,CAOlB;IAED;;;;;;;OAOG;IACH,mCALY,QAAQ,IAAI,CAAC,CA2BxB;C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