apify 3.7.3-beta.96 → 4.0.0-beta.100
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 +1 -1
- package/dist/actor.d.ts +379 -139
- package/dist/actor.js +764 -500
- package/dist/apify_dataset_backend.d.ts +19 -0
- package/dist/apify_dataset_backend.js +77 -0
- package/dist/apify_file_system_storage_backend.d.ts +34 -0
- package/dist/apify_file_system_storage_backend.js +41 -0
- package/dist/apify_key_value_store_backend.d.ts +23 -0
- package/dist/apify_key_value_store_backend.js +56 -0
- package/dist/apify_request_queue_backend.d.ts +78 -0
- package/dist/apify_request_queue_backend.js +114 -0
- package/dist/apify_request_queue_shared_backend.d.ts +31 -0
- package/dist/apify_request_queue_shared_backend.js +193 -0
- package/dist/apify_request_queue_single_backend.d.ts +43 -0
- package/dist/apify_request_queue_single_backend.js +256 -0
- package/dist/apify_storage_backend.d.ts +90 -0
- package/dist/apify_storage_backend.js +210 -0
- package/dist/charging.d.ts +39 -44
- package/dist/charging.js +245 -292
- package/dist/charging_storage_backend.d.ts +53 -0
- package/dist/charging_storage_backend.js +105 -0
- package/dist/child_run_tracker.d.ts +88 -0
- package/dist/child_run_tracker.js +239 -0
- package/dist/configuration.d.ts +85 -144
- package/dist/configuration.js +138 -174
- package/dist/enqueue_links_filters.d.ts +29 -0
- package/dist/enqueue_links_filters.js +52 -0
- package/dist/errors.d.ts +31 -0
- package/dist/errors.js +22 -0
- package/dist/google_sheets.d.ts +12 -0
- package/dist/google_sheets.js +26 -0
- package/dist/index.d.ts +10 -4
- package/dist/index.js +12 -22
- package/dist/input-schemas.d.ts +1 -1
- package/dist/input-schemas.js +12 -18
- package/dist/platform_event_manager.d.ts +15 -10
- package/dist/platform_event_manager.js +193 -63
- package/dist/proxy_configuration.d.ts +52 -72
- package/dist/proxy_configuration.js +176 -221
- package/dist/smart_apify_storage_backend.d.ts +52 -0
- package/dist/smart_apify_storage_backend.js +73 -0
- package/dist/storage.d.ts +6 -24
- package/dist/storage.js +1 -79
- package/dist/utils.d.ts +30 -0
- package/dist/utils.js +81 -23
- package/package.json +38 -43
- package/dist/index.mjs +0 -19
- package/dist/key_value_store.d.ts +0 -16
- package/dist/key_value_store.js +0 -40
- package/dist/patched_apify_client.d.ts +0 -25
- package/dist/patched_apify_client.js +0 -75
|
@@ -1,11 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
+
import { EventManager, EventType } from '@crawlee/core';
|
|
3
|
+
import { WebSocket } from 'ws';
|
|
4
|
+
import { ACTOR_ENV_VARS, ACTOR_EVENT_NAMES } from '@apify/consts';
|
|
5
|
+
import { betterClearInterval } from '@apify/utilities';
|
|
6
|
+
import { Configuration } from './configuration.js';
|
|
7
|
+
/**
|
|
8
|
+
* WebSocket close codes for a permanent condition, on which the connection is not re-established.
|
|
9
|
+
*
|
|
10
|
+
* The platform sends `1008` (policy violation) for an unknown/missing run ID or an exceeded per-run connection limit.
|
|
11
|
+
* The rest are protocol, data, or mandatory-extension failures that reconnecting cannot resolve.
|
|
12
|
+
*/
|
|
13
|
+
const NON_RETRYABLE_CLOSE_CODES = new Set([1002, 1003, 1007, 1008, 1010]);
|
|
14
|
+
/** Close codes of a clean close, as opposed to a lost connection. */
|
|
15
|
+
const CLEAN_CLOSE_CODES = new Set([1000, 1001]);
|
|
16
|
+
/** How long a connection must stay open to count as healthy, after which a drop reconnects without backoff. */
|
|
17
|
+
const HEALTHY_CONNECTION_MIN_DURATION_MILLIS = 1000;
|
|
18
|
+
/**
|
|
19
|
+
* Delays between reconnection attempts, mirroring the Python `websockets` library
|
|
20
|
+
* (see RFC 6455, section 7.2.3): a random initial delay, then exponential growth up to a cap.
|
|
21
|
+
*/
|
|
22
|
+
function* backoffDelays() {
|
|
23
|
+
yield Math.random() * 5000;
|
|
24
|
+
let delay = 3100;
|
|
25
|
+
while (delay < 90_000) {
|
|
26
|
+
yield delay;
|
|
27
|
+
delay *= 1.618;
|
|
28
|
+
}
|
|
29
|
+
while (true)
|
|
30
|
+
yield 90_000;
|
|
31
|
+
}
|
|
9
32
|
/**
|
|
10
33
|
* Gets an instance of a Node.js'
|
|
11
34
|
* [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)
|
|
@@ -32,15 +55,10 @@ const configuration_js_1 = require("./configuration.js");
|
|
|
32
55
|
* You can use it to persist the state of the Actor and gracefully stop your in-progress tasks,
|
|
33
56
|
* so that they are not interrupted by the migration.
|
|
34
57
|
* For example, this is used by the {@link RequestList} class.
|
|
35
|
-
* If you pass `gracefulShutdown: true` to {@link Actor.init}, the SDK will automatically call
|
|
36
|
-
* {@link Actor.reboot} when this event is received, which speeds up the migration and lets the
|
|
37
|
-
* run continue on a new worker.
|
|
38
58
|
* - `aborting`: `void`
|
|
39
59
|
* When a user aborts an Actor run on the Apify platform, they can choose to abort gracefully to allow
|
|
40
60
|
* the Actor some time before getting killed. This graceful abort emits the `aborting` event which the SDK
|
|
41
61
|
* uses to gracefully stop running crawls and you can use it to do your own cleanup as well.
|
|
42
|
-
* If you pass `gracefulShutdown: true` to {@link Actor.init}, the SDK will automatically call
|
|
43
|
-
* {@link Actor.exit} when this event is received.
|
|
44
62
|
* - `persistState`: `{ "isMigrating": Boolean }`
|
|
45
63
|
* Emitted in regular intervals (by default 60 seconds) to notify all components of Apify SDK that it is time to persist
|
|
46
64
|
* their state, in order to avoid repeating all work when the Actor restarts.
|
|
@@ -48,70 +66,73 @@ const configuration_js_1 = require("./configuration.js");
|
|
|
48
66
|
* in which case the `isMigrating` flag is set to `true`. Otherwise the flag is `false`.
|
|
49
67
|
* Note that the `persistState` event is provided merely for user convenience,
|
|
50
68
|
* you can achieve the same effect using `setInterval()` and listening for the `migrating` event.
|
|
69
|
+
*
|
|
70
|
+
* The connection to the platform events websocket is re-established after it drops, unless it was closed
|
|
71
|
+
* with a code signalling a permanent condition (e.g. `1008` for an unknown run ID).
|
|
51
72
|
*/
|
|
52
|
-
class PlatformEventManager extends
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
73
|
+
export class PlatformEventManager extends EventManager {
|
|
74
|
+
configuration;
|
|
75
|
+
/** Websocket connection to Actor events. */
|
|
76
|
+
#eventsWs;
|
|
77
|
+
/** Aborted by `close()`, which stops any pending reconnection and marks the final close as intentional. */
|
|
78
|
+
#abortController = new AbortController();
|
|
79
|
+
/** Backoff for rapid reconnections; reset after a connection that stayed open long enough to count as healthy. */
|
|
80
|
+
#backoff;
|
|
81
|
+
#internalListeners;
|
|
82
|
+
/** Calls of the internal listeners that are still in progress. */
|
|
83
|
+
#internalListenerCalls = new Set();
|
|
84
|
+
/**
|
|
85
|
+
* @param internalListeners Listeners of the SDK itself, kept apart from the ones registered with `on()`, so that
|
|
86
|
+
* user code can neither remove them with `off()` nor see them in `listeners()`.
|
|
87
|
+
*/
|
|
88
|
+
constructor(configuration = Configuration.getGlobalConfiguration(), internalListeners = {}) {
|
|
89
|
+
super({
|
|
90
|
+
persistStateIntervalMillis: configuration.persistStateIntervalMillis,
|
|
67
91
|
});
|
|
92
|
+
this.configuration = configuration;
|
|
93
|
+
this.#internalListeners = internalListeners;
|
|
94
|
+
}
|
|
95
|
+
emit(event, ...args) {
|
|
96
|
+
super.emit(event, ...args);
|
|
97
|
+
const listener = this.#internalListeners[event];
|
|
98
|
+
if (!listener)
|
|
99
|
+
return;
|
|
100
|
+
const call = (async () => listener(...args))()
|
|
101
|
+
.catch((err) => this.log.exception(err, `Internal listener of the "${event}" event failed`))
|
|
102
|
+
.finally(() => this.#internalListenerCalls.delete(call));
|
|
103
|
+
this.#internalListenerCalls.add(call);
|
|
104
|
+
}
|
|
105
|
+
async waitForAllListenersToComplete() {
|
|
106
|
+
const [completed] = await Promise.all([super.waitForAllListenersToComplete(), ...this.#internalListenerCalls]);
|
|
107
|
+
return completed;
|
|
68
108
|
}
|
|
69
109
|
/**
|
|
70
110
|
* Initializes `Actor.events` event emitter by creating a connection to a websocket that provides them.
|
|
71
111
|
* This is an internal function that is automatically called by `Actor.main()`.
|
|
112
|
+
*
|
|
113
|
+
* Rejects if the first connection cannot be established. Later drops are handled by reconnecting.
|
|
72
114
|
*/
|
|
73
115
|
async init() {
|
|
74
116
|
if (this.initialized) {
|
|
75
117
|
return;
|
|
76
118
|
}
|
|
77
119
|
await super.init();
|
|
78
|
-
const eventsWsUrl = this.
|
|
120
|
+
const eventsWsUrl = this.configuration.actorEventsWsUrl;
|
|
79
121
|
// Locally there is no web socket to connect, so just print a log message.
|
|
80
122
|
if (!eventsWsUrl) {
|
|
81
|
-
this.log.debug(`Environment variable ${
|
|
123
|
+
this.log.debug(`Environment variable ${ACTOR_ENV_VARS.EVENTS_WEBSOCKET_URL} is not set, no events from Apify platform will be emitted.`);
|
|
82
124
|
return;
|
|
83
125
|
}
|
|
84
|
-
this
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (name === consts_1.ACTOR_EVENT_NAMES.MIGRATING) {
|
|
95
|
-
(0, utilities_1.betterClearInterval)(this.intervals.persistState); // Don't send any other persist state event.
|
|
96
|
-
this.events.emit("persistState" /* EventType.PERSIST_STATE */, {
|
|
97
|
-
isMigrating: true,
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
catch (err) {
|
|
102
|
-
this.log.exception(err, 'Cannot parse Actor event');
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
this.eventsWs.on('error', (err) => {
|
|
106
|
-
// Don't print this error as this happens in the case of very short Actor.main().
|
|
107
|
-
if (err.message === 'WebSocket was closed before the connection was established')
|
|
108
|
-
return;
|
|
109
|
-
this.log.exception(err, 'web socket connection failed');
|
|
110
|
-
});
|
|
111
|
-
this.eventsWs.on('close', () => {
|
|
112
|
-
this.log.debug('web socket has been closed');
|
|
113
|
-
this.eventsWs = undefined;
|
|
114
|
-
});
|
|
126
|
+
this.#abortController = new AbortController();
|
|
127
|
+
this.#backoff = undefined;
|
|
128
|
+
try {
|
|
129
|
+
await this.#connect(eventsWsUrl);
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
// Leave no `persistState` interval behind, so that `init()` can be retried.
|
|
133
|
+
await this.close();
|
|
134
|
+
throw new Error('Error connecting to platform events websocket!', { cause: err });
|
|
135
|
+
}
|
|
115
136
|
}
|
|
116
137
|
/**
|
|
117
138
|
* Closes websocket providing events from Actor infrastructure and also stops sending internal events
|
|
@@ -123,7 +144,116 @@ class PlatformEventManager extends core_1.EventManager {
|
|
|
123
144
|
return;
|
|
124
145
|
}
|
|
125
146
|
await super.close();
|
|
126
|
-
this.
|
|
147
|
+
this.#abortController.abort();
|
|
148
|
+
this.#eventsWs?.close();
|
|
149
|
+
}
|
|
150
|
+
/** Opens a new connection; resolves once it is established and rejects if it fails before that. */
|
|
151
|
+
async #connect(eventsWsUrl) {
|
|
152
|
+
await new Promise((resolve, reject) => {
|
|
153
|
+
// `ws` has no default handshake timeout, so without it a silent server would block `init()` forever.
|
|
154
|
+
const ws = new WebSocket(eventsWsUrl, { handshakeTimeout: 10_000 });
|
|
155
|
+
this.#eventsWs = ws;
|
|
156
|
+
let openedAt;
|
|
157
|
+
let lastError;
|
|
158
|
+
ws.on('open', () => {
|
|
159
|
+
openedAt = Date.now();
|
|
160
|
+
resolve();
|
|
161
|
+
});
|
|
162
|
+
ws.on('message', (message) => this.#handleMessage(message));
|
|
163
|
+
ws.on('error', (err) => {
|
|
164
|
+
// Before the connection is open, the error is reported through the `close` event.
|
|
165
|
+
lastError = err;
|
|
166
|
+
if (openedAt !== undefined)
|
|
167
|
+
this.log.exception(err, 'Platform events websocket error');
|
|
168
|
+
});
|
|
169
|
+
ws.on('close', (code, reason) => {
|
|
170
|
+
if (this.#eventsWs === ws)
|
|
171
|
+
this.#eventsWs = undefined;
|
|
172
|
+
if (openedAt === undefined) {
|
|
173
|
+
reject(lastError ??
|
|
174
|
+
new Error(`Web socket closed before the connection was established (code=${code})`));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
this.#handleClose(eventsWsUrl, { code, reason: reason.toString(), openedAt });
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
#handleMessage(message) {
|
|
182
|
+
if (!message)
|
|
183
|
+
return;
|
|
184
|
+
try {
|
|
185
|
+
const { name, data } = JSON.parse(String(message));
|
|
186
|
+
this.emit(name, data);
|
|
187
|
+
if (name === ACTOR_EVENT_NAMES.MIGRATING) {
|
|
188
|
+
betterClearInterval(this.intervals.persistState); // Don't send any other persist state event.
|
|
189
|
+
this.events.emit(EventType.PERSIST_STATE, {
|
|
190
|
+
isMigrating: true,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
catch (err) {
|
|
195
|
+
this.log.exception(err, 'Cannot parse Actor event');
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/** Logs the close of an established connection and schedules a reconnection, unless the close is final. */
|
|
199
|
+
#handleClose(eventsWsUrl, { code, reason, openedAt }) {
|
|
200
|
+
if (this.#abortController.signal.aborted) {
|
|
201
|
+
this.log.debug('web socket has been closed');
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
const details = `code=${code}, reason=${JSON.stringify(reason)}`;
|
|
205
|
+
if (NON_RETRYABLE_CLOSE_CODES.has(code)) {
|
|
206
|
+
this.log.error(`Connection to platform events websocket was closed with a non-retryable code (${details}); not reconnecting. ` +
|
|
207
|
+
'No further platform events will be received for the rest of this run, so migration and abort handling ' +
|
|
208
|
+
'(e.g. persisting state before a migration) is now disabled.');
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (CLEAN_CLOSE_CODES.has(code)) {
|
|
212
|
+
this.log.info(`Connection to platform events websocket was closed (${details}), reconnecting...`);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.log.warning(`Connection to platform events websocket was lost (${details}), reconnecting...`);
|
|
216
|
+
}
|
|
217
|
+
// Reconnect a healthy connection immediately; back off only on repeated rapid drops. The first rapid drop
|
|
218
|
+
// retries without delay (it just primes the backoff), each consecutive one waits for the next interval.
|
|
219
|
+
let delay = 0;
|
|
220
|
+
if (Date.now() - openedAt >= HEALTHY_CONNECTION_MIN_DURATION_MILLIS) {
|
|
221
|
+
this.#backoff = undefined;
|
|
222
|
+
}
|
|
223
|
+
else if (this.#backoff) {
|
|
224
|
+
delay = this.#backoff.next().value;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
this.#backoff = backoffDelays();
|
|
228
|
+
}
|
|
229
|
+
this.#reconnect(eventsWsUrl, delay).catch((err) => {
|
|
230
|
+
this.log.exception(err, 'Reconnecting to platform events websocket failed');
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
/** Keeps trying to connect, with backoff between failed attempts, until it succeeds or `close()` is called. */
|
|
234
|
+
async #reconnect(eventsWsUrl, initialDelayMillis) {
|
|
235
|
+
const { signal } = this.#abortController;
|
|
236
|
+
await this.#sleep(initialDelayMillis, signal);
|
|
237
|
+
while (!signal.aborted) {
|
|
238
|
+
try {
|
|
239
|
+
await this.#connect(eventsWsUrl);
|
|
240
|
+
this.log.info('Reconnected to the platform events websocket.');
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
catch (err) {
|
|
244
|
+
if (signal.aborted)
|
|
245
|
+
return;
|
|
246
|
+
this.log.warning(`Reconnecting to platform events websocket failed: ${err.message}`);
|
|
247
|
+
}
|
|
248
|
+
this.#backoff ??= backoffDelays();
|
|
249
|
+
await this.#sleep(this.#backoff.next().value, signal);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/** Sleeps for the given time, or until the signal is aborted. */
|
|
253
|
+
async #sleep(millis, signal) {
|
|
254
|
+
if (millis <= 0 || signal.aborted)
|
|
255
|
+
return;
|
|
256
|
+
// Rejects with an `AbortError` when aborted, which just means there is nothing left to wait for.
|
|
257
|
+
await sleep(millis, undefined, { signal }).catch(() => { });
|
|
127
258
|
}
|
|
128
259
|
}
|
|
129
|
-
exports.PlatformEventManager = PlatformEventManager;
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import type { ProxyConfigurationOptions as CoreProxyConfigurationOptions
|
|
1
|
+
import type { ProxyConfigurationOptions as CoreProxyConfigurationOptions } from '@crawlee/core';
|
|
2
2
|
import { ProxyConfiguration as CoreProxyConfiguration } from '@crawlee/core';
|
|
3
|
+
import type { ProxyInfo as CoreProxyInfo } from '@crawlee/types';
|
|
3
4
|
import { Configuration } from './configuration.js';
|
|
5
|
+
/** Response of the Apify Proxy status endpoint (`proxy.apify.com/?format=json`). */
|
|
6
|
+
interface ProxyStatus {
|
|
7
|
+
connected: boolean;
|
|
8
|
+
connectionError: string;
|
|
9
|
+
isManInTheMiddle: boolean;
|
|
10
|
+
}
|
|
4
11
|
export interface ProxyConfigurationOptions extends CoreProxyConfigurationOptions {
|
|
5
12
|
/**
|
|
6
13
|
* User's password for the proxy. By default, it is taken from the `APIFY_PROXY_PASSWORD`
|
|
@@ -24,32 +31,27 @@ export interface ProxyConfigurationOptions extends CoreProxyConfigurationOptions
|
|
|
24
31
|
* on the Apify cloud, or when using the [Apify CLI](https://github.com/apify/apify-cli).
|
|
25
32
|
*/
|
|
26
33
|
countryCode?: string;
|
|
34
|
+
/**
|
|
35
|
+
* If set, all proxied requests will use IP addresses geolocated to the specified subdivision (e.g. US state).
|
|
36
|
+
* Requires `countryCode` to be set. The value must follow the ISO 3166-2 subdivision code format,
|
|
37
|
+
* e.g. `'CA'` for California when `countryCode` is `'US'`.
|
|
38
|
+
*/
|
|
39
|
+
subdivisionCode?: string;
|
|
27
40
|
/**
|
|
28
41
|
* Same option as `groups` which can be used to
|
|
29
|
-
*
|
|
42
|
+
* configurate the proxy by UI input schema. You should use the `groups` option in your crawler code.
|
|
30
43
|
*/
|
|
31
44
|
apifyProxyGroups?: string[];
|
|
32
45
|
/**
|
|
33
46
|
* Same option as `countryCode` which can be used to
|
|
34
|
-
*
|
|
47
|
+
* configurate the proxy by UI input schema. You should use the `countryCode` option in your crawler code.
|
|
35
48
|
*/
|
|
36
49
|
apifyProxyCountry?: string;
|
|
37
|
-
/**
|
|
38
|
-
* If set, all proxied requests will use IP addresses geolocated to the specified subdivision (e.g. US state).
|
|
39
|
-
* Requires `countryCode` to be set. The value must follow the ISO 3166-2 subdivision code format,
|
|
40
|
-
* e.g. `'CA'` for California when `countryCode` is `'US'`.
|
|
41
|
-
*/
|
|
42
|
-
subdivisionCode?: string;
|
|
43
50
|
/**
|
|
44
51
|
* Same option as `subdivisionCode` which can be used to
|
|
45
|
-
*
|
|
52
|
+
* configurate the proxy by UI input schema. You should use the `subdivisionCode` option in your crawler code.
|
|
46
53
|
*/
|
|
47
54
|
apifyProxySubdivision?: string;
|
|
48
|
-
/**
|
|
49
|
-
* Multiple different ProxyConfigurationOptions stratified into tiers. Crawlee crawlers will switch between those tiers
|
|
50
|
-
* based on the blocked request statistics.
|
|
51
|
-
*/
|
|
52
|
-
tieredProxyConfig?: Omit<ProxyConfigurationOptions, keyof CoreProxyConfigurationOptions | 'tieredProxyConfig'>[];
|
|
53
55
|
/**
|
|
54
56
|
* As part of the init process, we verify the configuration by checking the proxy status endpoint.
|
|
55
57
|
* This can make the init slower, to opt-out of this, use `checkAccess: false` (defaults to `true`).
|
|
@@ -80,9 +82,6 @@ export interface ProxyConfigurationOptions extends CoreProxyConfigurationOptions
|
|
|
80
82
|
* requestHandler({ proxyInfo }) {
|
|
81
83
|
* // Getting used proxy URL
|
|
82
84
|
* const proxyUrl = proxyInfo.url;
|
|
83
|
-
*
|
|
84
|
-
* // Getting ID of used Session
|
|
85
|
-
* const sessionIdentifier = proxyInfo.sessionId;
|
|
86
85
|
* }
|
|
87
86
|
* })
|
|
88
87
|
*
|
|
@@ -93,7 +92,7 @@ export interface ProxyInfo extends CoreProxyInfo {
|
|
|
93
92
|
* An array of proxy groups to be used by the [Apify Proxy](https://docs.apify.com/proxy).
|
|
94
93
|
* If not provided, the proxy will select the groups automatically.
|
|
95
94
|
*/
|
|
96
|
-
groups
|
|
95
|
+
groups?: string[];
|
|
97
96
|
/**
|
|
98
97
|
* If set and relevant proxies are available in your Apify account, all proxied requests will
|
|
99
98
|
* use IP addresses that are geolocated to the specified country. For example `GB` for IPs
|
|
@@ -151,19 +150,13 @@ export interface ProxyInfo extends CoreProxyInfo {
|
|
|
151
150
|
* @category Scaling
|
|
152
151
|
*/
|
|
153
152
|
export declare class ProxyConfiguration extends CoreProxyConfiguration {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
private subdivisionCode?;
|
|
158
|
-
private password?;
|
|
159
|
-
private hostname;
|
|
160
|
-
private port?;
|
|
161
|
-
private usesApifyProxy?;
|
|
162
|
-
private checkAccess?;
|
|
153
|
+
#private;
|
|
154
|
+
readonly configuration: Configuration;
|
|
155
|
+
protected readonly log: import("@crawlee/core").Log;
|
|
163
156
|
/**
|
|
164
157
|
* @internal
|
|
165
158
|
*/
|
|
166
|
-
constructor(options?: ProxyConfigurationOptions,
|
|
159
|
+
constructor(options?: ProxyConfigurationOptions, configuration?: Configuration);
|
|
167
160
|
/**
|
|
168
161
|
* Loads proxy password if token is provided and checks access to Apify Proxy and provided proxy groups
|
|
169
162
|
* if Apify Proxy configuration is used.
|
|
@@ -171,72 +164,59 @@ export declare class ProxyConfiguration extends CoreProxyConfiguration {
|
|
|
171
164
|
*
|
|
172
165
|
* You should use the {@link createProxyConfiguration} function to create a pre-initialized
|
|
173
166
|
* `ProxyConfiguration` instance instead of calling this manually.
|
|
174
|
-
*
|
|
175
|
-
* As part of the init process, we verify the configuration by checking the proxy status endpoint.
|
|
176
|
-
* This can make the init slower, to opt-out of this, use `checkAccess: false`.
|
|
177
167
|
*/
|
|
178
168
|
initialize(options?: {
|
|
179
169
|
checkAccess?: boolean;
|
|
180
170
|
}): Promise<boolean>;
|
|
181
171
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* Use it if you want to work with a rich representation of a proxy URL.
|
|
186
|
-
* If you need the URL string only, use {@link ProxyConfiguration.newUrl}.
|
|
187
|
-
* @param [sessionId]
|
|
188
|
-
* Represents the identifier of user {@link Session} that can be managed by the {@link SessionPool} or
|
|
189
|
-
* you can use the Apify Proxy [Session](https://docs.apify.com/proxy#sessions) identifier.
|
|
190
|
-
* When the provided sessionId is a number, it's converted to a string. Property sessionId of
|
|
191
|
-
* {@link ProxyInfo} is always returned as a type string.
|
|
172
|
+
* Returns a new {@link ProxyInfo} object with a fresh proxy URL. Each call mints an
|
|
173
|
+
* independent URL; for Apify Proxy a random session id is embedded so consecutive
|
|
174
|
+
* calls resolve to different IPs.
|
|
192
175
|
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
*
|
|
201
|
-
*
|
|
202
|
-
* Represents the identifier of user {@link Session} that can be managed by the {@link SessionPool} or
|
|
203
|
-
* you can use the Apify Proxy [Session](https://docs.apify.com/proxy#sessions) identifier.
|
|
204
|
-
* When the provided sessionId is a number, it's converted to a string.
|
|
205
|
-
*
|
|
206
|
-
* All the HTTP requests going through the proxy with the same session identifier
|
|
207
|
-
* will use the same target proxy server (i.e. the same IP address).
|
|
208
|
-
* The identifier must not be longer than 50 characters and include only the following: `0-9`, `a-z`, `A-Z`, `"."`, `"_"` and `"~"`.
|
|
209
|
-
* @return A string with a proxy URL, including authentication credentials and port number.
|
|
210
|
-
* For example, `http://bob:password123@proxy.example.com:8000`
|
|
176
|
+
* A passed `proxyInfo` (e.g. one restored with a persisted session) is returned unchanged, unless it is an
|
|
177
|
+
* Apify Proxy one. Its endpoint is specific to the host, so it is moved to the current endpoint, keeping the
|
|
178
|
+
* username and with it the proxy session.
|
|
179
|
+
*/
|
|
180
|
+
newProxyInfo(proxyInfo?: ProxyInfo): Promise<ProxyInfo | undefined>;
|
|
181
|
+
/**
|
|
182
|
+
* Returns a new proxy URL. For Apify Proxy, each call generates a URL with a fresh
|
|
183
|
+
* random session id, so consecutive calls return independent URLs. For custom
|
|
184
|
+
* `proxyUrls`, the URLs are rotated round-robin.
|
|
211
185
|
*/
|
|
212
|
-
newUrl(
|
|
213
|
-
protected _generateTieredProxyUrls(tieredProxyConfig: NonNullable<ProxyConfigurationOptions['tieredProxyConfig']>, globalOptions: ProxyConfigurationOptions): string[][];
|
|
186
|
+
newUrl(): Promise<string | undefined>;
|
|
214
187
|
/**
|
|
215
188
|
* Returns proxy username.
|
|
216
189
|
*/
|
|
217
|
-
protected
|
|
218
|
-
protected composeDefaultUrl(sessionId
|
|
190
|
+
protected getUsername(sessionId: string): string;
|
|
191
|
+
protected composeDefaultUrl(sessionId: string): string;
|
|
192
|
+
private composeUrl;
|
|
219
193
|
/**
|
|
220
194
|
* Fetch & set the proxy password from Apify API if an Apify token is provided.
|
|
221
195
|
*/
|
|
222
|
-
|
|
196
|
+
private setPasswordIfToken;
|
|
223
197
|
/**
|
|
224
198
|
* Checks whether the user has access to the proxies specified in the provided ProxyConfigurationOptions.
|
|
225
199
|
* If the check can not be made, it only prints a warning and allows the program to continue. This is to
|
|
226
200
|
* prevent program crashes caused by short downtimes of Proxy.
|
|
227
201
|
*/
|
|
228
|
-
protected
|
|
202
|
+
protected checkAccess(): Promise<boolean>;
|
|
229
203
|
/**
|
|
230
204
|
* Apify Proxy can be down for a second or a minute, but this should not crash processes.
|
|
231
205
|
*/
|
|
232
|
-
protected
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
206
|
+
protected fetchStatus(): Promise<ProxyStatus | undefined>;
|
|
207
|
+
/**
|
|
208
|
+
* Fetches the Apify Proxy status endpoint once, *through* the proxy, so the
|
|
209
|
+
* response reports on this exact connection (auth + man-in-the-middle).
|
|
210
|
+
*
|
|
211
|
+
* Uses a native `node:http` forward-proxy request — an absolute request URL
|
|
212
|
+
* plus a `Proxy-Authorization` header — so no proxy-agent dependency is
|
|
213
|
+
* needed. The status endpoint (`http://proxy.apify.com`) is plain HTTP.
|
|
214
|
+
*/
|
|
215
|
+
protected requestStatus(statusUrl: string, proxyUrl: string): Promise<ProxyStatus>;
|
|
237
216
|
/**
|
|
238
217
|
* Throws cannot combine custom proxies with Apify Proxy
|
|
239
218
|
* @internal
|
|
240
219
|
*/
|
|
241
|
-
protected
|
|
220
|
+
protected throwCannotCombineCustomWithApify(): void;
|
|
242
221
|
}
|
|
222
|
+
export {};
|