@crawlee/basic 4.0.0-beta.18 → 4.0.0-beta.181

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.
Files changed (84) hide show
  1. package/README.md +14 -14
  2. package/index.d.ts +10 -2
  3. package/index.js +10 -1
  4. package/internals/autoscaling/autoscaled_pool.d.ts +196 -0
  5. package/internals/autoscaling/autoscaled_pool.js +385 -0
  6. package/internals/autoscaling/concurrency_system.d.ts +267 -0
  7. package/internals/autoscaling/concurrency_system.js +364 -0
  8. package/internals/autoscaling/cpu_load_signal.d.ts +43 -0
  9. package/internals/autoscaling/cpu_load_signal.js +46 -0
  10. package/internals/autoscaling/event_loop_load_signal.d.ts +51 -0
  11. package/internals/autoscaling/event_loop_load_signal.js +60 -0
  12. package/internals/autoscaling/index.d.ts +9 -0
  13. package/internals/autoscaling/index.js +9 -0
  14. package/internals/autoscaling/load_signal.d.ts +98 -0
  15. package/internals/autoscaling/load_signal.js +103 -0
  16. package/internals/autoscaling/memory_load_signal.d.ts +47 -0
  17. package/internals/autoscaling/memory_load_signal.js +104 -0
  18. package/internals/autoscaling/snapshotter.d.ts +84 -0
  19. package/internals/autoscaling/snapshotter.js +67 -0
  20. package/internals/autoscaling/storage_backend_load_signal.d.ts +56 -0
  21. package/internals/autoscaling/storage_backend_load_signal.js +73 -0
  22. package/internals/autoscaling/system_status.d.ts +122 -0
  23. package/internals/autoscaling/system_status.js +139 -0
  24. package/internals/autoscaling/weighted_avg.d.ts +5 -0
  25. package/internals/autoscaling/weighted_avg.js +14 -0
  26. package/internals/basic-crawler.d.ts +462 -332
  27. package/internals/basic-crawler.js +1378 -815
  28. package/internals/cookie_utils.d.ts +44 -0
  29. package/internals/cookie_utils.js +122 -0
  30. package/internals/crawler-run.d.ts +76 -0
  31. package/internals/crawler-run.js +142 -0
  32. package/internals/crawlers/context_pipeline.d.ts +70 -0
  33. package/internals/crawlers/context_pipeline.js +122 -0
  34. package/internals/crawlers/crawler_commons.d.ts +193 -0
  35. package/internals/crawlers/crawler_commons.js +1 -0
  36. package/internals/crawlers/error_snapshotter.d.ts +57 -0
  37. package/internals/crawlers/error_snapshotter.js +117 -0
  38. package/internals/crawlers/error_tracker.d.ts +54 -0
  39. package/internals/crawlers/error_tracker.js +308 -0
  40. package/internals/crawlers/index.d.ts +5 -0
  41. package/internals/crawlers/index.js +4 -0
  42. package/internals/crawlers/internals/types.d.ts +7 -0
  43. package/internals/crawlers/internals/types.js +1 -0
  44. package/internals/crawlers/statistics.d.ts +318 -0
  45. package/internals/crawlers/statistics.js +525 -0
  46. package/internals/enqueue_links/enqueue_links.d.ts +152 -0
  47. package/internals/enqueue_links/enqueue_links.js +76 -0
  48. package/internals/enqueue_links/index.d.ts +2 -0
  49. package/internals/enqueue_links/index.js +2 -0
  50. package/internals/enqueue_links/shared.d.ts +98 -0
  51. package/internals/enqueue_links/shared.js +259 -0
  52. package/internals/errors.d.ts +58 -0
  53. package/internals/errors.js +72 -0
  54. package/internals/http.d.ts +9 -0
  55. package/internals/http.js +28 -0
  56. package/internals/request-timeout.d.ts +48 -0
  57. package/internals/request-timeout.js +73 -0
  58. package/internals/router.d.ts +306 -0
  59. package/internals/router.js +310 -0
  60. package/internals/send-request.d.ts +4 -4
  61. package/internals/send-request.js +22 -11
  62. package/internals/session_pool/consts.d.ts +3 -0
  63. package/internals/session_pool/consts.js +3 -0
  64. package/internals/session_pool/errors.d.ts +7 -0
  65. package/internals/session_pool/errors.js +11 -0
  66. package/internals/session_pool/fingerprint.d.ts +9 -0
  67. package/internals/session_pool/fingerprint.js +30 -0
  68. package/internals/session_pool/index.d.ts +4 -0
  69. package/internals/session_pool/index.js +4 -0
  70. package/internals/session_pool/session.d.ts +153 -0
  71. package/internals/session_pool/session.js +221 -0
  72. package/internals/session_pool/session_pool.d.ts +242 -0
  73. package/internals/session_pool/session_pool.js +429 -0
  74. package/internals/sitemap_request_loader.d.ts +196 -0
  75. package/internals/sitemap_request_loader.js +432 -0
  76. package/internals/throttling_request_manager.d.ts +216 -0
  77. package/internals/throttling_request_manager.js +826 -0
  78. package/package.json +19 -13
  79. package/index.d.ts.map +0 -1
  80. package/index.js.map +0 -1
  81. package/internals/basic-crawler.d.ts.map +0 -1
  82. package/internals/basic-crawler.js.map +0 -1
  83. package/internals/send-request.d.ts.map +0 -1
  84. package/internals/send-request.js.map +0 -1
@@ -0,0 +1,216 @@
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
+ private readonly domainStates;
155
+ private readonly log;
156
+ constructor(options: ThrottlingRequestManagerOptions<T>, config?: Configuration);
157
+ /**
158
+ * The wrapped manager, holding every request whose domain is not throttled. `undefined` until an `inner`
159
+ * passed as a factory is resolved - reading this never forces it, because a getter should not open a queue
160
+ * behind a caller's back.
161
+ */
162
+ get innerManager(): T | undefined;
163
+ /**
164
+ * Records a pacing signal: a refusal puts the URL's domain into backoff, a declared interval becomes its
165
+ * crawl delay, and a crawl-wide floor raises {@link recordEverywhereFloor|the floor under all of them}.
166
+ *
167
+ * @returns `false` if the domain the signal covers is not throttled, in which case this is a no-op.
168
+ * @throws If the signal's scope is one this manager cannot honour - see {@link assertScopeHonourable}.
169
+ * @inheritdoc
170
+ */
171
+ recordPacingSignal(signal: PacingSignal): boolean;
172
+ addRequest(requestLike: Source, options?: RequestQueueOperationOptions): Promise<RequestQueueOperationInfo>;
173
+ /**
174
+ * Adds requests in batches, routing each one to the manager that owns its domain.
175
+ *
176
+ * Batching, validation, deduplication and `Retry-After`-free bookkeeping are all delegated to the target
177
+ * managers - this only decides where each request goes, one batch at a time, so a lazy or unbounded input
178
+ * iterable is never fully materialized.
179
+ */
180
+ addRequestsBatched(requests: RequestsLike, options?: AddRequestsBatchedOptions): Promise<AddRequestsBatchedResult>;
181
+ reclaimRequest(request: Request, options?: RequestQueueOperationOptions): Promise<RequestQueueOperationInfo | null>;
182
+ markRequestAsHandled(request: Request): Promise<RequestQueueOperationInfo | void | null>;
183
+ getTotalCount(): Promise<number>;
184
+ getPendingCount(): Promise<number>;
185
+ getHandledCount(): Promise<number>;
186
+ /**
187
+ * Reports whether anything can be dispatched right now, and if not, when — or why never.
188
+ *
189
+ * One traversal of the domain clocks answers all of it: only domains whose delays have run out are probed,
190
+ * the rest merely contribute the moment they come due. Throttled requests count as outstanding work, so a
191
+ * crawler gated on this idles for the backoff instead of concluding it is done.
192
+ *
193
+ * `ready` from anywhere else outranks a stalling domain and is returned without looking at the stall clocks,
194
+ * so one hopeless domain never ends a crawl making progress elsewhere. It cannot outrank itself, though -
195
+ * see the traversal.
196
+ */
197
+ checkReadiness(): Promise<RequestSourceStatus>;
198
+ /**
199
+ * Empties every manager and clears the accumulated backoff. A robots.txt `Crawl-delay` is a property of the
200
+ * site rather than of the run, so it survives.
201
+ */
202
+ purge(): Promise<void>;
203
+ setExpectedRequestProcessingTimeSecs(secs: number): Promise<void>;
204
+ /**
205
+ * Returns the next request from a domain that is not backing off, or from the inner manager.
206
+ *
207
+ * Returns `null` while every remaining request belongs to a throttled domain - it never waits the backoff
208
+ * out, because a consumer parked in here holds a concurrency slot, which the autoscaler reads as spare
209
+ * capacity and answers by scaling up. Callers poll instead, and
210
+ * {@link ThrottlingRequestManager.checkReadiness|`checkReadiness()`} reports `waiting` meanwhile so the
211
+ * crawler's task loop idles rather than spins.
212
+ */
213
+ fetchNextRequest<R extends Dictionary = Dictionary>(): Promise<Request<R> | null>;
214
+ [Symbol.asyncIterator](): AsyncGenerator<Request<Dictionary>, void, unknown>;
215
+ drop(): Promise<void>;
216
+ }