@crawlee/basic 4.0.0-beta.19 → 4.0.0-beta.190
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -14
- package/index.d.ts +11 -2
- package/index.js +11 -1
- package/internals/autoscaling/autoscaled_pool.d.ts +198 -0
- package/internals/autoscaling/autoscaled_pool.js +388 -0
- package/internals/autoscaling/concurrency_system.d.ts +275 -0
- package/internals/autoscaling/concurrency_system.js +365 -0
- package/internals/autoscaling/cpu_load_signal.d.ts +43 -0
- package/internals/autoscaling/cpu_load_signal.js +46 -0
- package/internals/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/internals/autoscaling/event_loop_load_signal.js +60 -0
- package/internals/autoscaling/index.d.ts +9 -0
- package/internals/autoscaling/index.js +9 -0
- package/internals/autoscaling/load_signal.d.ts +98 -0
- package/internals/autoscaling/load_signal.js +103 -0
- package/internals/autoscaling/memory_load_signal.d.ts +47 -0
- package/internals/autoscaling/memory_load_signal.js +104 -0
- package/internals/autoscaling/snapshotter.d.ts +84 -0
- package/internals/autoscaling/snapshotter.js +67 -0
- package/internals/autoscaling/storage_backend_load_signal.d.ts +56 -0
- package/internals/autoscaling/storage_backend_load_signal.js +73 -0
- package/internals/autoscaling/system_status.d.ts +122 -0
- package/internals/autoscaling/system_status.js +139 -0
- package/internals/autoscaling/weighted_avg.d.ts +5 -0
- package/internals/autoscaling/weighted_avg.js +14 -0
- package/internals/basic-crawler.d.ts +465 -335
- package/internals/basic-crawler.js +1402 -820
- package/internals/cookie_utils.d.ts +44 -0
- package/internals/cookie_utils.js +122 -0
- package/internals/crawler-run.d.ts +76 -0
- package/internals/crawler-run.js +142 -0
- package/internals/crawlers/context_pipeline.d.ts +71 -0
- package/internals/crawlers/context_pipeline.js +117 -0
- package/internals/crawlers/crawler_commons.d.ts +194 -0
- package/internals/crawlers/crawler_commons.js +1 -0
- package/internals/crawlers/error_snapshotter.d.ts +57 -0
- package/internals/crawlers/error_snapshotter.js +117 -0
- package/internals/crawlers/error_tracker.d.ts +54 -0
- package/internals/crawlers/error_tracker.js +308 -0
- package/internals/crawlers/index.d.ts +5 -0
- package/internals/crawlers/index.js +4 -0
- package/internals/crawlers/internals/types.d.ts +7 -0
- package/internals/crawlers/internals/types.js +1 -0
- package/internals/crawlers/statistics.d.ts +318 -0
- package/internals/crawlers/statistics.js +525 -0
- package/internals/crawling_request.d.ts +86 -0
- package/internals/crawling_request.js +127 -0
- package/internals/enqueue_links/enqueue_links.d.ts +152 -0
- package/internals/enqueue_links/enqueue_links.js +76 -0
- package/internals/enqueue_links/index.d.ts +2 -0
- package/internals/enqueue_links/index.js +2 -0
- package/internals/enqueue_links/shared.d.ts +99 -0
- package/internals/enqueue_links/shared.js +258 -0
- package/internals/errors.d.ts +55 -0
- package/internals/errors.js +67 -0
- package/internals/http.d.ts +9 -0
- package/internals/http.js +28 -0
- package/internals/request-timeout.d.ts +48 -0
- package/internals/request-timeout.js +73 -0
- package/internals/router.d.ts +306 -0
- package/internals/router.js +310 -0
- package/internals/send-request.d.ts +4 -4
- package/internals/send-request.js +22 -11
- package/internals/session_pool/consts.d.ts +3 -0
- package/internals/session_pool/consts.js +3 -0
- package/internals/session_pool/errors.d.ts +7 -0
- package/internals/session_pool/errors.js +11 -0
- package/internals/session_pool/fingerprint.d.ts +9 -0
- package/internals/session_pool/fingerprint.js +30 -0
- package/internals/session_pool/index.d.ts +4 -0
- package/internals/session_pool/index.js +4 -0
- package/internals/session_pool/session.d.ts +153 -0
- package/internals/session_pool/session.js +221 -0
- package/internals/session_pool/session_pool.d.ts +242 -0
- package/internals/session_pool/session_pool.js +429 -0
- package/internals/sitemap_request_loader.d.ts +196 -0
- package/internals/sitemap_request_loader.js +432 -0
- package/internals/throttling_request_manager.d.ts +214 -0
- package/internals/throttling_request_manager.js +825 -0
- package/package.json +19 -13
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/internals/basic-crawler.d.ts.map +0 -1
- package/internals/basic-crawler.js.map +0 -1
- package/internals/send-request.d.ts.map +0 -1
- package/internals/send-request.js.map +0 -1
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import type { Dictionary } from '@crawlee/types';
|
|
2
|
+
import type { AddRequestsBatchedOptions, AddRequestsBatchedResult, Configuration, IRequestManager, PacingSignal, RequestQueueOperationInfo, RequestQueueOperationOptions, RequestsLike, Source, StorageIdentifier, RequestSourceStatus, StorageOpenOptions } from '@crawlee/core';
|
|
3
|
+
import { Request } from '@crawlee/core';
|
|
4
|
+
/**
|
|
5
|
+
* Opens a request manager, matching the shape of storage `open` methods such as
|
|
6
|
+
* {@link RequestQueue.open|`RequestQueue.open`}.
|
|
7
|
+
*
|
|
8
|
+
* {@link ThrottlingRequestManager} calls this once per configured domain, so every per-domain queue shares the
|
|
9
|
+
* concrete type and storage backend of the manager being wrapped.
|
|
10
|
+
*/
|
|
11
|
+
export type RequestManagerOpener<T extends IRequestManager = IRequestManager> = (identifier?: string | StorageIdentifier | null, options?: StorageOpenOptions) => Promise<T>;
|
|
12
|
+
/** Options for {@link ThrottlingRequestManager}. */
|
|
13
|
+
export interface ThrottlingRequestManagerOptions<T extends IRequestManager = IRequestManager> {
|
|
14
|
+
/**
|
|
15
|
+
* The request manager to wrap, usually a {@link RequestQueue}. Requests for domains that are not throttled
|
|
16
|
+
* are stored here. May be a factory, so that the throttler can be constructed synchronously and the manager
|
|
17
|
+
* under it opened only on first use.
|
|
18
|
+
*
|
|
19
|
+
* Omitted, the default request queue is opened on first use through
|
|
20
|
+
* {@link ThrottlingRequestManagerOptions.requestManagerOpener|`requestManagerOpener`}.
|
|
21
|
+
*/
|
|
22
|
+
inner?: T | (() => T | Promise<T>);
|
|
23
|
+
/**
|
|
24
|
+
* Which domains to throttle: a list of hostnames, or `'all'` for every domain the crawl encounters.
|
|
25
|
+
*
|
|
26
|
+
* Matching a listed hostname is case-insensitive and exact - wildcards such as `*.example.com` are not
|
|
27
|
+
* supported, so list each subdomain you care about (or set
|
|
28
|
+
* {@link ThrottlingRequestManagerOptions.throttleBy|`throttleBy: 'registrableDomain'`}). An
|
|
29
|
+
* internationalized domain may be given in either its unicode or its punycode form, and an IPv6 address has
|
|
30
|
+
* to be bracketed (`[::1]`). Requests for any other domain bypass throttling entirely.
|
|
31
|
+
*
|
|
32
|
+
* `'all'` gives each domain a queue of its own the first time it is seen, so that it can be held back
|
|
33
|
+
* without its requests being repeatedly popped and re-enqueued. One request queue per domain is not free,
|
|
34
|
+
* which is what {@link ThrottlingRequestManagerOptions.maxThrottledDomains|`maxThrottledDomains`} is
|
|
35
|
+
* there to bound.
|
|
36
|
+
*/
|
|
37
|
+
domains: string[] | 'all';
|
|
38
|
+
/**
|
|
39
|
+
* A floor under the crawl delay of every throttled domain, in seconds - the proactive clock described on
|
|
40
|
+
* {@link ThrottlingRequestManager}. A domain whose robots.txt asks for a longer `Crawl-delay` gets the
|
|
41
|
+
* longer one; this is a minimum, not an override. A `minIntervalEverywhere` {@link PacingSignal} raises
|
|
42
|
+
* this floor at runtime, and never lowers it.
|
|
43
|
+
* @default 0
|
|
44
|
+
*/
|
|
45
|
+
minCrawlDelaySecs?: number;
|
|
46
|
+
/**
|
|
47
|
+
* What counts as "the same domain": the exact hostname, or the registrable domain it belongs to
|
|
48
|
+
* (`example.com` for `www.example.com`, `a.example.co.uk` and so on). Hosts with no registrable domain -
|
|
49
|
+
* IP addresses, `localhost` - are always throttled per hostname.
|
|
50
|
+
*
|
|
51
|
+
* Grouping by registrable domain gives subdomains a single pair of clocks and a single queue, which is what
|
|
52
|
+
* you want when the pacing is there to be polite to one server rather than to satisfy a specific host's
|
|
53
|
+
* rate limit.
|
|
54
|
+
* @default 'hostname'
|
|
55
|
+
*/
|
|
56
|
+
throttleBy?: 'hostname' | 'registrableDomain';
|
|
57
|
+
/**
|
|
58
|
+
* The most domains a run may throttle at once. Exceeding it throws, rather than silently letting the
|
|
59
|
+
* throttling lapse - one request queue per domain is not free, and a crawl that discovers domains without
|
|
60
|
+
* bound would drown the storage backend in them.
|
|
61
|
+
*
|
|
62
|
+
* Only domains discovered under `domains: 'all'` count against this; an explicit list is taken at face value.
|
|
63
|
+
* @default 100
|
|
64
|
+
*/
|
|
65
|
+
maxThrottledDomains?: number;
|
|
66
|
+
/**
|
|
67
|
+
* The key under which the discovered domain list is kept in the default key-value store, so that a restart
|
|
68
|
+
* with `purgeOnStart` disabled reopens their queues instead of stranding whatever they still hold. Only
|
|
69
|
+
* written under `domains: 'all'`.
|
|
70
|
+
*
|
|
71
|
+
* Give each manager its own key when running several of them against the same storage.
|
|
72
|
+
* @default 'CRAWLEE_THROTTLED_DOMAINS'
|
|
73
|
+
*/
|
|
74
|
+
persistStateKey?: string;
|
|
75
|
+
/**
|
|
76
|
+
* Opens the per-domain queues, one per throttled domain, each under the alias `throttled-<domain>`.
|
|
77
|
+
* @default RequestQueue.open
|
|
78
|
+
*/
|
|
79
|
+
requestManagerOpener?: RequestManagerOpener<T>;
|
|
80
|
+
/**
|
|
81
|
+
* The delay applied after a domain's first HTTP 429, doubled on each subsequent one.
|
|
82
|
+
* @default 2
|
|
83
|
+
*/
|
|
84
|
+
baseDelaySecs?: number;
|
|
85
|
+
/**
|
|
86
|
+
* Upper bound on the delay between requests to a rate-limited domain, applied to both the exponential
|
|
87
|
+
* backoff and a `Retry-After` value.
|
|
88
|
+
* @default 60
|
|
89
|
+
*/
|
|
90
|
+
maxDelaySecs?: number;
|
|
91
|
+
/**
|
|
92
|
+
* How long a domain may rate-limit us without a single request getting through before the crawl is
|
|
93
|
+
* abandoned with a {@link PersistentRateLimitError}.
|
|
94
|
+
*
|
|
95
|
+
* A domain that keeps answering 429 for this long is not going to be crawled by waiting longer - the
|
|
96
|
+
* concurrency is too high for it, or it has blocked us outright. Its requests are deliberately left in
|
|
97
|
+
* their queue, so re-running the crawl with `purgeOnStart` disabled picks them up once the domain recovers.
|
|
98
|
+
*
|
|
99
|
+
* A crawler running with `keepAlive` is exempt - outliving a domain that will not let us through is the
|
|
100
|
+
* whole point there.
|
|
101
|
+
* @default 900
|
|
102
|
+
*/
|
|
103
|
+
maxDomainStallSecs?: number;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* A request manager that wraps another one and paces requests per domain.
|
|
107
|
+
*
|
|
108
|
+
* Requests for a throttled domain are routed into their own queue when they are added, so each request lives in
|
|
109
|
+
* exactly one place and deduplication keeps working. Everything else goes to the wrapped manager untouched.
|
|
110
|
+
*
|
|
111
|
+
* {@link ThrottlingRequestManager.fetchNextRequest|`fetchNextRequest()`} serves the domain that has been waiting
|
|
112
|
+
* longest and skips any that are backing off, falling back to the wrapped manager. It never blocks: while every
|
|
113
|
+
* remaining request belongs to a throttled domain it returns `null` and
|
|
114
|
+
* {@link ThrottlingRequestManager.checkReadiness|`checkReadiness()`} reports `waiting` with the moment the
|
|
115
|
+
* earliest of them comes due, so the crawler idles instead of holding a concurrency slot open.
|
|
116
|
+
*
|
|
117
|
+
* Each throttled domain runs two independent clocks, and may be dispatched to once **both** have run out:
|
|
118
|
+
* - **Backoff**, set by HTTP 429 responses - honouring `Retry-After`, and otherwise doubling from `baseDelaySecs`.
|
|
119
|
+
* Reactive and temporary: it decays once the domain stops turning us away. The crawlers report the 429s
|
|
120
|
+
* themselves; a request held back this way is retried later without counting against `maxRequestRetries` and
|
|
121
|
+
* without penalising its session.
|
|
122
|
+
* - **Crawl delay**, the minimum interval between two dispatches to the domain, armed after each one. Proactive
|
|
123
|
+
* and constant: whatever the domain's robots.txt asks for, floored by
|
|
124
|
+
* {@link ThrottlingRequestManagerOptions.minCrawlDelaySecs|`minCrawlDelaySecs`}. Either may be absent, in
|
|
125
|
+
* which case the other one is the delay.
|
|
126
|
+
*
|
|
127
|
+
* Which domains get those clocks is {@link ThrottlingRequestManagerOptions.domains|`domains`} - a list, or
|
|
128
|
+
* `'all'` for every domain the crawl encounters.
|
|
129
|
+
*
|
|
130
|
+
* Pass one as a crawler's `requestManager`; the `sameDomainDelaySecs` shorthand builds one with `domains: 'all'`
|
|
131
|
+
* and `throttleBy: 'registrableDomain'`. Construct it yourself to name the domains or tune the delays - one
|
|
132
|
+
* covering every domain also makes `sameDomainDelaySecs` land on it as a floor rather than adding a second pacer.
|
|
133
|
+
*
|
|
134
|
+
* Signals - 429s, robots.txt `Crawl-delay`, that floor - arrive through
|
|
135
|
+
* {@link IRequestManager.recordPacingSignal|`recordPacingSignal`}, which wrapping managers forward, so this
|
|
136
|
+
* works wherever it sits in a composition, including inside a {@link RequestManagerTandem}.
|
|
137
|
+
*
|
|
138
|
+
* **Example usage:**
|
|
139
|
+
*
|
|
140
|
+
* ```ts
|
|
141
|
+
* const crawler = new CheerioCrawler({
|
|
142
|
+
* requestManager: new ThrottlingRequestManager({
|
|
143
|
+
* domains: ['api.example.com', 'slow-site.org'],
|
|
144
|
+
* }),
|
|
145
|
+
* requestHandler: async ({ request }) => { ... },
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @category Sources
|
|
150
|
+
*/
|
|
151
|
+
export declare class ThrottlingRequestManager<T extends IRequestManager = IRequestManager> implements IRequestManager {
|
|
152
|
+
#private;
|
|
153
|
+
private readonly config;
|
|
154
|
+
constructor(options: ThrottlingRequestManagerOptions<T>, config?: Configuration);
|
|
155
|
+
/**
|
|
156
|
+
* The wrapped manager, holding every request whose domain is not throttled. `undefined` until an `inner`
|
|
157
|
+
* passed as a factory is resolved - reading this never forces it, because a getter should not open a queue
|
|
158
|
+
* behind a caller's back.
|
|
159
|
+
*/
|
|
160
|
+
get innerManager(): T | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* Records a pacing signal: a refusal puts the URL's domain into backoff, a declared interval becomes its
|
|
163
|
+
* crawl delay, and a crawl-wide floor raises {@link recordEverywhereFloor|the floor under all of them}.
|
|
164
|
+
*
|
|
165
|
+
* @returns `false` if the domain the signal covers is not throttled, in which case this is a no-op.
|
|
166
|
+
* @throws If the signal's scope is one this manager cannot honour - see {@link assertScopeHonourable}.
|
|
167
|
+
* @inheritdoc
|
|
168
|
+
*/
|
|
169
|
+
recordPacingSignal(signal: PacingSignal): boolean;
|
|
170
|
+
addRequest(requestLike: Source, options?: RequestQueueOperationOptions): Promise<RequestQueueOperationInfo>;
|
|
171
|
+
/**
|
|
172
|
+
* Adds requests in batches, routing each one to the manager that owns its domain.
|
|
173
|
+
*
|
|
174
|
+
* Batching, validation, deduplication and `Retry-After`-free bookkeeping are all delegated to the target
|
|
175
|
+
* managers - this only decides where each request goes, one batch at a time, so a lazy or unbounded input
|
|
176
|
+
* iterable is never fully materialized.
|
|
177
|
+
*/
|
|
178
|
+
addRequestsBatched(requests: RequestsLike, options?: AddRequestsBatchedOptions): Promise<AddRequestsBatchedResult>;
|
|
179
|
+
reclaimRequest(request: Request, options?: RequestQueueOperationOptions): Promise<RequestQueueOperationInfo | null>;
|
|
180
|
+
markRequestAsHandled(request: Request): Promise<RequestQueueOperationInfo | void | null>;
|
|
181
|
+
getTotalCount(): Promise<number>;
|
|
182
|
+
getPendingCount(): Promise<number>;
|
|
183
|
+
getHandledCount(): Promise<number>;
|
|
184
|
+
/**
|
|
185
|
+
* Reports whether anything can be dispatched right now, and if not, when — or why never.
|
|
186
|
+
*
|
|
187
|
+
* One traversal of the domain clocks answers all of it: only domains whose delays have run out are probed,
|
|
188
|
+
* the rest merely contribute the moment they come due. Throttled requests count as outstanding work, so a
|
|
189
|
+
* crawler gated on this idles for the backoff instead of concluding it is done.
|
|
190
|
+
*
|
|
191
|
+
* `ready` from anywhere else outranks a stalling domain and is returned without looking at the stall clocks,
|
|
192
|
+
* so one hopeless domain never ends a crawl making progress elsewhere. It cannot outrank itself, though -
|
|
193
|
+
* see the traversal.
|
|
194
|
+
*/
|
|
195
|
+
checkReadiness(): Promise<RequestSourceStatus>;
|
|
196
|
+
/**
|
|
197
|
+
* Empties every manager and clears the accumulated backoff. A robots.txt `Crawl-delay` is a property of the
|
|
198
|
+
* site rather than of the run, so it survives.
|
|
199
|
+
*/
|
|
200
|
+
purge(): Promise<void>;
|
|
201
|
+
setExpectedRequestProcessingTimeSecs(secs: number): Promise<void>;
|
|
202
|
+
/**
|
|
203
|
+
* Returns the next request from a domain that is not backing off, or from the inner manager.
|
|
204
|
+
*
|
|
205
|
+
* Returns `null` while every remaining request belongs to a throttled domain - it never waits the backoff
|
|
206
|
+
* out, because a consumer parked in here holds a concurrency slot, which the autoscaler reads as spare
|
|
207
|
+
* capacity and answers by scaling up. Callers poll instead, and
|
|
208
|
+
* {@link ThrottlingRequestManager.checkReadiness|`checkReadiness()`} reports `waiting` meanwhile so the
|
|
209
|
+
* crawler's task loop idles rather than spins.
|
|
210
|
+
*/
|
|
211
|
+
fetchNextRequest<R extends Dictionary = Dictionary>(): Promise<Request<R> | null>;
|
|
212
|
+
[Symbol.asyncIterator](): AsyncGenerator<Request<Dictionary>, void, unknown>;
|
|
213
|
+
drop(): Promise<void>;
|
|
214
|
+
}
|