@crawlee/core 4.0.0-beta.12 → 4.0.0-beta.121

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 (279) hide show
  1. package/README.md +17 -13
  2. package/autoscaling/autoscaled_pool.d.ts +67 -172
  3. package/autoscaling/autoscaled_pool.js +165 -320
  4. package/autoscaling/client_load_signal.d.ts +55 -0
  5. package/autoscaling/client_load_signal.js +73 -0
  6. package/autoscaling/concurrency_system.d.ts +268 -0
  7. package/autoscaling/concurrency_system.js +351 -0
  8. package/autoscaling/cpu_load_signal.d.ts +43 -0
  9. package/autoscaling/cpu_load_signal.js +47 -0
  10. package/autoscaling/event_loop_load_signal.d.ts +51 -0
  11. package/autoscaling/event_loop_load_signal.js +60 -0
  12. package/autoscaling/index.d.ts +6 -1
  13. package/autoscaling/index.js +6 -1
  14. package/autoscaling/load_signal.d.ts +99 -0
  15. package/autoscaling/load_signal.js +104 -0
  16. package/autoscaling/memory_load_signal.d.ts +47 -0
  17. package/autoscaling/memory_load_signal.js +106 -0
  18. package/autoscaling/snapshotter.d.ts +58 -163
  19. package/autoscaling/snapshotter.js +45 -263
  20. package/autoscaling/system_status.d.ts +62 -84
  21. package/autoscaling/system_status.js +92 -122
  22. package/autoscaling/weighted_avg.d.ts +5 -0
  23. package/autoscaling/weighted_avg.js +14 -0
  24. package/byte_utils.d.ts +17 -0
  25. package/byte_utils.js +42 -0
  26. package/configuration.d.ts +96 -223
  27. package/configuration.js +170 -222
  28. package/cookie_utils.d.ts +4 -3
  29. package/cookie_utils.js +22 -13
  30. package/crawlers/context_pipeline.d.ts +10 -1
  31. package/crawlers/context_pipeline.js +31 -8
  32. package/crawlers/crawler_commons.d.ts +90 -83
  33. package/crawlers/crawler_commons.js +1 -116
  34. package/crawlers/error_snapshotter.d.ts +2 -5
  35. package/crawlers/error_snapshotter.js +7 -8
  36. package/crawlers/error_tracker.d.ts +0 -1
  37. package/crawlers/error_tracker.js +0 -1
  38. package/crawlers/index.d.ts +1 -3
  39. package/crawlers/index.js +0 -3
  40. package/crawlers/internals/types.d.ts +0 -1
  41. package/crawlers/internals/types.js +0 -1
  42. package/crawlers/statistics.d.ts +143 -59
  43. package/crawlers/statistics.js +243 -153
  44. package/debug.d.ts +36 -0
  45. package/debug.js +70 -0
  46. package/enqueue_links/enqueue_links.d.ts +59 -68
  47. package/enqueue_links/enqueue_links.js +57 -62
  48. package/enqueue_links/index.d.ts +0 -1
  49. package/enqueue_links/index.js +0 -1
  50. package/enqueue_links/shared.d.ts +40 -27
  51. package/enqueue_links/shared.js +90 -68
  52. package/errors.d.ts +72 -4
  53. package/errors.js +89 -5
  54. package/events/event_manager.d.ts +35 -9
  55. package/events/event_manager.js +10 -12
  56. package/events/index.d.ts +0 -1
  57. package/events/index.js +0 -1
  58. package/events/local_event_manager.d.ts +15 -3
  59. package/events/local_event_manager.js +39 -13
  60. package/http.d.ts +9 -0
  61. package/http.js +28 -0
  62. package/index.d.ts +7 -4
  63. package/index.js +6 -3
  64. package/iterables.d.ts +79 -0
  65. package/iterables.js +134 -0
  66. package/log.d.ts +82 -3
  67. package/log.js +106 -1
  68. package/memory-storage/consts.d.ts +4 -0
  69. package/memory-storage/consts.js +4 -0
  70. package/memory-storage/index.d.ts +1 -0
  71. package/memory-storage/index.js +1 -0
  72. package/memory-storage/memory-storage.d.ts +38 -0
  73. package/memory-storage/memory-storage.js +130 -0
  74. package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
  75. package/memory-storage/resource-clients/common/base-client.js +6 -0
  76. package/memory-storage/resource-clients/dataset.d.ts +40 -0
  77. package/memory-storage/resource-clients/dataset.js +114 -0
  78. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  79. package/memory-storage/resource-clients/key-value-store.js +204 -0
  80. package/memory-storage/resource-clients/request-queue.d.ts +77 -0
  81. package/memory-storage/resource-clients/request-queue.js +422 -0
  82. package/memory-storage/utils.d.ts +16 -0
  83. package/memory-storage/utils.js +41 -0
  84. package/owned_or_injected.d.ts +58 -0
  85. package/owned_or_injected.js +98 -0
  86. package/package.json +13 -12
  87. package/proxy_configuration.d.ts +24 -132
  88. package/proxy_configuration.js +24 -143
  89. package/recoverable_state.d.ts +140 -0
  90. package/recoverable_state.js +212 -0
  91. package/request.d.ts +86 -17
  92. package/request.js +120 -41
  93. package/router.d.ts +193 -21
  94. package/router.js +188 -43
  95. package/serialization.d.ts +0 -1
  96. package/serialization.js +9 -11
  97. package/service_locator.d.ts +165 -0
  98. package/service_locator.js +253 -0
  99. package/session_pool/consts.d.ts +1 -2
  100. package/session_pool/consts.js +1 -2
  101. package/session_pool/errors.d.ts +0 -1
  102. package/session_pool/errors.js +0 -1
  103. package/session_pool/fingerprint.d.ts +9 -0
  104. package/session_pool/fingerprint.js +30 -0
  105. package/session_pool/index.d.ts +0 -2
  106. package/session_pool/index.js +0 -2
  107. package/session_pool/session.d.ts +35 -89
  108. package/session_pool/session.js +82 -142
  109. package/session_pool/session_pool.d.ts +69 -90
  110. package/session_pool/session_pool.js +151 -150
  111. package/storages/batched_adds.d.ts +37 -0
  112. package/storages/batched_adds.js +73 -0
  113. package/storages/dataset.d.ts +114 -54
  114. package/storages/dataset.js +285 -144
  115. package/storages/index.d.ts +10 -8
  116. package/storages/index.js +8 -8
  117. package/storages/key_value_store.d.ts +185 -42
  118. package/storages/key_value_store.js +424 -151
  119. package/storages/key_value_store_codec.d.ts +32 -0
  120. package/storages/key_value_store_codec.js +113 -0
  121. package/storages/request_dedup_cache.d.ts +22 -0
  122. package/storages/request_dedup_cache.js +48 -0
  123. package/storages/request_list.d.ts +52 -116
  124. package/storages/request_list.js +159 -133
  125. package/storages/request_loader.d.ts +101 -0
  126. package/storages/request_loader.js +1 -0
  127. package/storages/request_manager.d.ts +33 -0
  128. package/storages/request_manager.js +1 -0
  129. package/storages/request_manager_tandem.d.ts +97 -0
  130. package/storages/request_manager_tandem.js +197 -0
  131. package/storages/request_queue.d.ts +290 -47
  132. package/storages/request_queue.js +757 -216
  133. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +37 -88
  134. package/storages/{sitemap_request_list.js → sitemap_request_loader.js} +137 -143
  135. package/storages/storage_instance_manager.d.ts +87 -0
  136. package/storages/storage_instance_manager.js +256 -0
  137. package/storages/storage_stats.d.ts +48 -0
  138. package/storages/storage_stats.js +29 -0
  139. package/storages/throttling_request_manager.d.ts +216 -0
  140. package/storages/throttling_request_manager.js +453 -0
  141. package/storages/transaction.d.ts +252 -0
  142. package/storages/transaction.js +251 -0
  143. package/storages/utils.d.ts +58 -11
  144. package/storages/utils.js +64 -13
  145. package/system-info/cpu-info.d.ts +67 -0
  146. package/system-info/cpu-info.js +216 -0
  147. package/system-info/memory-info.d.ts +31 -0
  148. package/system-info/memory-info.js +115 -0
  149. package/system-info/ps-tree.d.ts +17 -0
  150. package/system-info/ps-tree.js +144 -0
  151. package/system-info/runtime.d.ts +14 -0
  152. package/system-info/runtime.js +80 -0
  153. package/typedefs.d.ts +0 -6
  154. package/typedefs.js +0 -1
  155. package/url.d.ts +9 -0
  156. package/url.js +11 -0
  157. package/validators.d.ts +8 -1
  158. package/validators.js +10 -3
  159. package/autoscaling/autoscaled_pool.d.ts.map +0 -1
  160. package/autoscaling/autoscaled_pool.js.map +0 -1
  161. package/autoscaling/index.d.ts.map +0 -1
  162. package/autoscaling/index.js.map +0 -1
  163. package/autoscaling/snapshotter.d.ts.map +0 -1
  164. package/autoscaling/snapshotter.js.map +0 -1
  165. package/autoscaling/system_status.d.ts.map +0 -1
  166. package/autoscaling/system_status.js.map +0 -1
  167. package/configuration.d.ts.map +0 -1
  168. package/configuration.js.map +0 -1
  169. package/cookie_utils.d.ts.map +0 -1
  170. package/cookie_utils.js.map +0 -1
  171. package/crawlers/context_pipeline.d.ts.map +0 -1
  172. package/crawlers/context_pipeline.js.map +0 -1
  173. package/crawlers/crawler_commons.d.ts.map +0 -1
  174. package/crawlers/crawler_commons.js.map +0 -1
  175. package/crawlers/crawler_utils.d.ts +0 -10
  176. package/crawlers/crawler_utils.d.ts.map +0 -1
  177. package/crawlers/crawler_utils.js +0 -12
  178. package/crawlers/crawler_utils.js.map +0 -1
  179. package/crawlers/error_snapshotter.d.ts.map +0 -1
  180. package/crawlers/error_snapshotter.js.map +0 -1
  181. package/crawlers/error_tracker.d.ts.map +0 -1
  182. package/crawlers/error_tracker.js.map +0 -1
  183. package/crawlers/index.d.ts.map +0 -1
  184. package/crawlers/index.js.map +0 -1
  185. package/crawlers/internals/types.d.ts.map +0 -1
  186. package/crawlers/internals/types.js.map +0 -1
  187. package/crawlers/statistics.d.ts.map +0 -1
  188. package/crawlers/statistics.js.map +0 -1
  189. package/enqueue_links/enqueue_links.d.ts.map +0 -1
  190. package/enqueue_links/enqueue_links.js.map +0 -1
  191. package/enqueue_links/index.d.ts.map +0 -1
  192. package/enqueue_links/index.js.map +0 -1
  193. package/enqueue_links/shared.d.ts.map +0 -1
  194. package/enqueue_links/shared.js.map +0 -1
  195. package/errors.d.ts.map +0 -1
  196. package/errors.js.map +0 -1
  197. package/events/event_manager.d.ts.map +0 -1
  198. package/events/event_manager.js.map +0 -1
  199. package/events/index.d.ts.map +0 -1
  200. package/events/index.js.map +0 -1
  201. package/events/local_event_manager.d.ts.map +0 -1
  202. package/events/local_event_manager.js.map +0 -1
  203. package/http_clients/base-http-client.d.ts +0 -134
  204. package/http_clients/base-http-client.d.ts.map +0 -1
  205. package/http_clients/base-http-client.js +0 -33
  206. package/http_clients/base-http-client.js.map +0 -1
  207. package/http_clients/form-data-like.d.ts +0 -67
  208. package/http_clients/form-data-like.d.ts.map +0 -1
  209. package/http_clients/form-data-like.js +0 -5
  210. package/http_clients/form-data-like.js.map +0 -1
  211. package/http_clients/got-scraping-http-client.d.ts +0 -15
  212. package/http_clients/got-scraping-http-client.d.ts.map +0 -1
  213. package/http_clients/got-scraping-http-client.js +0 -69
  214. package/http_clients/got-scraping-http-client.js.map +0 -1
  215. package/http_clients/index.d.ts +0 -3
  216. package/http_clients/index.d.ts.map +0 -1
  217. package/http_clients/index.js +0 -3
  218. package/http_clients/index.js.map +0 -1
  219. package/index.d.ts.map +0 -1
  220. package/index.js.map +0 -1
  221. package/log.d.ts.map +0 -1
  222. package/log.js.map +0 -1
  223. package/proxy_configuration.d.ts.map +0 -1
  224. package/proxy_configuration.js.map +0 -1
  225. package/request.d.ts.map +0 -1
  226. package/request.js.map +0 -1
  227. package/router.d.ts.map +0 -1
  228. package/router.js.map +0 -1
  229. package/serialization.d.ts.map +0 -1
  230. package/serialization.js.map +0 -1
  231. package/session_pool/consts.d.ts.map +0 -1
  232. package/session_pool/consts.js.map +0 -1
  233. package/session_pool/errors.d.ts.map +0 -1
  234. package/session_pool/errors.js.map +0 -1
  235. package/session_pool/events.d.ts +0 -3
  236. package/session_pool/events.d.ts.map +0 -1
  237. package/session_pool/events.js +0 -3
  238. package/session_pool/events.js.map +0 -1
  239. package/session_pool/index.d.ts.map +0 -1
  240. package/session_pool/index.js.map +0 -1
  241. package/session_pool/session.d.ts.map +0 -1
  242. package/session_pool/session.js.map +0 -1
  243. package/session_pool/session_pool.d.ts.map +0 -1
  244. package/session_pool/session_pool.js.map +0 -1
  245. package/storages/access_checking.d.ts +0 -13
  246. package/storages/access_checking.d.ts.map +0 -1
  247. package/storages/access_checking.js +0 -14
  248. package/storages/access_checking.js.map +0 -1
  249. package/storages/dataset.d.ts.map +0 -1
  250. package/storages/dataset.js.map +0 -1
  251. package/storages/index.d.ts.map +0 -1
  252. package/storages/index.js.map +0 -1
  253. package/storages/key_value_store.d.ts.map +0 -1
  254. package/storages/key_value_store.js.map +0 -1
  255. package/storages/request_list.d.ts.map +0 -1
  256. package/storages/request_list.js.map +0 -1
  257. package/storages/request_provider.d.ts +0 -308
  258. package/storages/request_provider.d.ts.map +0 -1
  259. package/storages/request_provider.js +0 -555
  260. package/storages/request_provider.js.map +0 -1
  261. package/storages/request_queue.d.ts.map +0 -1
  262. package/storages/request_queue.js.map +0 -1
  263. package/storages/request_queue_v2.d.ts +0 -87
  264. package/storages/request_queue_v2.d.ts.map +0 -1
  265. package/storages/request_queue_v2.js +0 -438
  266. package/storages/request_queue_v2.js.map +0 -1
  267. package/storages/sitemap_request_list.d.ts.map +0 -1
  268. package/storages/sitemap_request_list.js.map +0 -1
  269. package/storages/storage_manager.d.ts +0 -58
  270. package/storages/storage_manager.d.ts.map +0 -1
  271. package/storages/storage_manager.js +0 -105
  272. package/storages/storage_manager.js.map +0 -1
  273. package/storages/utils.d.ts.map +0 -1
  274. package/storages/utils.js.map +0 -1
  275. package/tsconfig.build.tsbuildinfo +0 -1
  276. package/typedefs.d.ts.map +0 -1
  277. package/typedefs.js.map +0 -1
  278. package/validators.d.ts.map +0 -1
  279. package/validators.js.map +0 -1
@@ -1,25 +1,29 @@
1
- import type { Awaitable, BatchAddRequestsResult, Dictionary } from '@crawlee/types';
1
+ import type { BatchAddRequestsResult, Dictionary } from '@crawlee/types';
2
2
  import { type RobotsTxtFile } from '@crawlee/utils';
3
3
  import type { SetRequired } from 'type-fest';
4
- import type { RequestProvider, RequestQueueOperationOptions } from '../storages/request_provider.js';
5
- import type { GlobInput, PseudoUrlInput, RegExpInput, RequestTransform } from './shared.js';
6
- export type SkippedRequestCallback = (args: {
7
- url: string;
8
- reason: 'robotsTxt';
9
- }) => Awaitable<void>;
4
+ import { Request } from '../request.js';
5
+ import type { IRequestManager } from '../storages/request_manager.js';
6
+ import type { AddRequestsBatchedOptions, AddRequestsBatchedResult, RequestQueueOperationOptions } from '../storages/request_queue.js';
7
+ import type { RequestTransform, SkippedRequestCallback, UrlPatternInput } from './shared.js';
10
8
  export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
11
9
  /** Limit the amount of actually enqueued URLs to this number. Useful for testing across the entire crawling scope. */
12
10
  limit?: number;
13
11
  /** An array of URLs to enqueue. */
14
12
  urls?: readonly string[];
15
- /** A request queue to which the URLs will be enqueued. */
16
- requestQueue?: RequestProvider;
13
+ /** A request manager to which the URLs will be enqueued. */
14
+ requestManager?: IRequestManager;
17
15
  /** A CSS selector matching links to be enqueued. */
18
16
  selector?: string;
19
17
  /** Sets {@link Request.userData} for newly enqueued requests. */
20
18
  userData?: Dictionary;
21
- /** Sets {@link Request.label} for newly enqueued requests. */
19
+ /**
20
+ * Sets {@link Request.label} for newly enqueued requests.
21
+ *
22
+ * Can be overwritten by `transformRequestFunction`.
23
+ */
22
24
  label?: string;
25
+ /** Sets {@link Request.sessionId} for newly enqueued requests. */
26
+ sessionId?: string;
23
27
  /**
24
28
  * If set to `true`, tells the crawler to skip navigation and process the request directly.
25
29
  * @default false
@@ -31,67 +35,37 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
31
35
  */
32
36
  baseUrl?: string;
33
37
  /**
34
- * An array of glob pattern strings or plain objects
35
- * containing glob pattern strings matching the URLs to be enqueued.
38
+ * An array of URL patterns that URLs must match to be enqueued.
36
39
  *
37
- * The plain objects must include at least the `glob` property, which holds the glob pattern string.
38
- * All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
39
- *
40
- * The matching is always case-insensitive.
41
- * If you need case-sensitive matching, use `regexps` property directly.
42
- *
43
- * If `globs` is an empty array or `undefined`, and `regexps` are also not defined, then the function
44
- * enqueues the links with the same subdomain.
45
- */
46
- globs?: readonly GlobInput[];
47
- /**
48
- * An array of glob pattern strings, regexp patterns or plain objects
49
- * containing patterns matching URLs that will **never** be enqueued.
50
- *
51
- * The plain objects must include either the `glob` property or the `regexp` property.
52
- * All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
40
+ * Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
53
41
  *
54
42
  * Glob matching is always case-insensitive.
55
- * If you need case-sensitive matching, provide a regexp.
56
- */
57
- exclude?: readonly (GlobInput | RegExpInput)[];
58
- /**
59
- * An array of regular expressions or plain objects
60
- * containing regular expressions matching the URLs to be enqueued.
43
+ * If you need case-sensitive matching, use a `RegExp`.
61
44
  *
62
- * The plain objects must include at least the `regexp` property, which holds the regular expression.
63
- * All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
45
+ * The patterns are combined with the {@link EnqueueLinksOptions.strategy|`strategy`} using AND logic - a URL
46
+ * must match at least one `include` pattern **and** satisfy the strategy to be enqueued. To match URLs across
47
+ * hostnames, pass an explicit {@link EnqueueStrategy.All} strategy.
64
48
  *
65
- * If `regexps` is an empty array or `undefined`, and `globs` are also not defined, then the function
66
- * enqueues the links with the same subdomain.
49
+ * If `undefined`, the links are enqueued based on the {@link EnqueueLinksOptions.strategy|`strategy`} alone.
50
+ * Passing an empty array is not allowed.
67
51
  */
68
- regexps?: readonly RegExpInput[];
52
+ include?: readonly UrlPatternInput[];
69
53
  /**
70
- * *NOTE:* In future versions of SDK the options will be removed.
71
- * Please use `globs` or `regexps` instead.
72
- *
73
- * An array of {@link PseudoUrl} strings or plain objects
74
- * containing {@link PseudoUrl} strings matching the URLs to be enqueued.
75
- *
76
- * The plain objects must include at least the `purl` property, which holds the pseudo-URL string.
77
- * All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
78
- *
79
- * With a pseudo-URL string, the matching is always case-insensitive.
80
- * If you need case-sensitive matching, use `regexps` property directly.
54
+ * An array of URL patterns. Matching URLs will **not** be enqueued.
81
55
  *
82
- * If `pseudoUrls` is an empty array or `undefined`, then the function
83
- * enqueues the links with the same subdomain.
56
+ * Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
84
57
  *
85
- * @deprecated prefer using `globs` or `regexps` instead
58
+ * Glob matching is always case-insensitive.
59
+ * If you need case-sensitive matching, use a `RegExp`.
86
60
  */
87
- pseudoUrls?: readonly PseudoUrlInput[];
61
+ exclude?: readonly UrlPatternInput[];
88
62
  /**
89
- * Just before a new {@link Request} is constructed and enqueued to the {@link RequestQueue}, this function can be used
90
- * to remove it or modify its contents such as `userData`, `payload` or, most importantly `uniqueKey`. This is useful
63
+ * After request options are filtered by `include`/`exclude` patterns, this function can be used
64
+ * to remove them or modify their contents such as `userData`, `payload` or, most importantly `uniqueKey`. This is useful
91
65
  * when you need to enqueue multiple `Requests` to the queue that share the same URL, but differ in methods or payloads,
92
66
  * or to dynamically update or create `userData`.
93
67
  *
94
- * For example: by adding `keepUrlFragment: true` to the `request` object, URL fragments will not be removed
68
+ * For example: by adding `keepUrlFragment: true` to the request options, URL fragments will not be removed
95
69
  * when `uniqueKey` is computed.
96
70
  *
97
71
  * **Example:**
@@ -105,9 +79,13 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
105
79
  * }
106
80
  * ```
107
81
  *
108
- * Note that `transformRequestFunction` has a priority over request options
109
- * specified in `globs`, `regexps`, or `pseudoUrls` objects,
110
- * and thus some options could be over-written by `transformRequestFunction`.
82
+ * Note that `transformRequestFunction` has the highest priority and can overwrite
83
+ * the global `label` option.
84
+ *
85
+ * The function receives a {@link RequestOptions} object and can return either:
86
+ * - The modified {@link RequestOptions} object
87
+ * - `'unchanged'` to keep the original options as-is
88
+ * - A falsy value or `'skip'` to exclude the request from the queue
111
89
  */
112
90
  transformRequestFunction?: RequestTransform;
113
91
  /**
@@ -138,10 +116,21 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
138
116
  * RobotsTxtFile instance for the current request that triggered the `enqueueLinks`.
139
117
  * If provided, disallowed URLs will be ignored.
140
118
  */
141
- robotsTxtFile?: RobotsTxtFile;
119
+ robotsTxtFile?: Pick<RobotsTxtFile, 'isAllowed'>;
120
+ /**
121
+ * Mirrors {@link BasicCrawlerOptions.respectRobotsTxtFile}: pass `false` to disable filtering or
122
+ * `{ userAgent }` to evaluate rules for a specific user-agent. Defaults to `*` when
123
+ * {@link EnqueueLinksOptions.robotsTxtFile|`robotsTxtFile`} is provided.
124
+ */
125
+ respectRobotsTxtFile?: boolean | {
126
+ userAgent?: string;
127
+ };
142
128
  /**
143
129
  * When a request is skipped for some reason, you can use this callback to act on it.
144
- * This is currently fired only for requests skipped based on robots.txt file.
130
+ * This is currently fired for requests skipped
131
+ * 1. based on robots.txt file,
132
+ * 2. because they don't match enqueueLinks filters,
133
+ * 3. or because the maxRequestsPerCrawl limit has been reached
145
134
  */
146
135
  onSkippedRequest?: SkippedRequestCallback;
147
136
  }
@@ -200,17 +189,16 @@ export declare enum EnqueueStrategy {
200
189
  * This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
201
190
  * you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
202
191
  *
203
- * Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
204
- * and override settings of the enqueued {@link Request} objects.
192
+ * Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
205
193
  *
206
194
  * **Example usage**
207
195
  *
208
196
  * ```javascript
209
197
  * await enqueueLinks({
210
198
  * urls: aListOfFoundUrls,
211
- * requestQueue,
199
+ * requestManager,
212
200
  * selector: 'a.product-detail',
213
- * globs: [
201
+ * include: [
214
202
  * 'https://www.example.com/handbags/*',
215
203
  * 'https://www.example.com/purses/*'
216
204
  * ],
@@ -220,7 +208,11 @@ export declare enum EnqueueStrategy {
220
208
  * @param options All `enqueueLinks()` parameters are passed via an options object.
221
209
  * @returns Promise that resolves to {@link BatchAddRequestsResult} object.
222
210
  */
223
- export declare function enqueueLinks(options: SetRequired<EnqueueLinksOptions, 'requestQueue' | 'urls'>): Promise<BatchAddRequestsResult>;
211
+ export declare function enqueueLinks(options: SetRequired<Omit<EnqueueLinksOptions, 'requestManager'>, 'urls'> & {
212
+ requestManager: {
213
+ addRequestsBatched: (requests: Request<Dictionary>[], options: AddRequestsBatchedOptions) => Promise<AddRequestsBatchedResult>;
214
+ };
215
+ }): Promise<BatchAddRequestsResult>;
224
216
  /**
225
217
  * @internal
226
218
  * This method helps resolve the baseUrl that will be used for filtering in {@link enqueueLinks}.
@@ -239,4 +231,3 @@ export interface ResolveBaseUrl {
239
231
  originalRequestUrl: string;
240
232
  finalRequestUrl?: string;
241
233
  }
242
- //# sourceMappingURL=enqueue_links.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import ow from 'ow';
2
2
  import { getDomain } from 'tldts';
3
- import log from '@apify/log';
4
- import { constructGlobObjectsFromGlobs, constructRegExpObjectsFromPseudoUrls, constructRegExpObjectsFromRegExps, createRequestOptions, createRequests, filterRequestsByPatterns, } from './shared.js';
3
+ import { Request } from '../request.js';
4
+ import { applyRequestTransform, constructUrlPatternObjects, createRequestOptions, filterRequestOptionsByPatterns, } from './shared.js';
5
5
  /**
6
6
  * The different enqueueing strategies available.
7
7
  *
@@ -58,17 +58,16 @@ export var EnqueueStrategy;
58
58
  * This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
59
59
  * you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
60
60
  *
61
- * Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
62
- * and override settings of the enqueued {@link Request} objects.
61
+ * Optionally, the function allows you to filter the target links' URLs using an array of glob or regexp patterns.
63
62
  *
64
63
  * **Example usage**
65
64
  *
66
65
  * ```javascript
67
66
  * await enqueueLinks({
68
67
  * urls: aListOfFoundUrls,
69
- * requestQueue,
68
+ * requestManager,
70
69
  * selector: 'a.product-detail',
71
- * globs: [
70
+ * include: [
72
71
  * 'https://www.example.com/handbags/*',
73
72
  * 'https://www.example.com/purses/*'
74
73
  * ],
@@ -85,52 +84,33 @@ export async function enqueueLinks(options) {
85
84
  'Check out our guide on how to use enqueueLinks() here: https://crawlee.dev/js/docs/examples/crawl-relative-links',
86
85
  ].join('\n'));
87
86
  }
87
+ const urlPatternValidator = ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'));
88
88
  ow(options, ow.object.exactShape({
89
89
  urls: ow.array.ofType(ow.string),
90
- requestQueue: ow.object.hasKeys('fetchNextRequest', 'addRequest'),
90
+ requestManager: ow.object.hasKeys('addRequestsBatched'),
91
91
  robotsTxtFile: ow.optional.object.hasKeys('isAllowed'),
92
+ respectRobotsTxtFile: ow.optional.any(ow.boolean, ow.object.exactShape({ userAgent: ow.optional.string })),
92
93
  onSkippedRequest: ow.optional.function,
93
94
  forefront: ow.optional.boolean,
94
95
  skipNavigation: ow.optional.boolean,
96
+ sessionId: ow.optional.string,
95
97
  limit: ow.optional.number,
96
98
  selector: ow.optional.string,
97
99
  baseUrl: ow.optional.string,
98
100
  userData: ow.optional.object,
99
101
  label: ow.optional.string,
100
- pseudoUrls: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('purl'))),
101
- globs: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('glob'))),
102
- exclude: ow.optional.array.ofType(ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'))),
103
- regexps: ow.optional.array.ofType(ow.any(ow.regExp, ow.object.hasKeys('regexp'))),
102
+ include: ow.optional.array.minLength(1).ofType(urlPatternValidator),
103
+ exclude: ow.optional.array.ofType(urlPatternValidator),
104
104
  transformRequestFunction: ow.optional.function,
105
105
  strategy: ow.optional.string.oneOf(Object.values(EnqueueStrategy)),
106
106
  waitForAllRequestsToBeAdded: ow.optional.boolean,
107
107
  }));
108
- const { requestQueue, limit, urls, pseudoUrls, exclude, globs, regexps, transformRequestFunction, forefront, waitForAllRequestsToBeAdded, robotsTxtFile, onSkippedRequest, } = options;
109
- const urlExcludePatternObjects = [];
110
- const urlPatternObjects = [];
111
- if (exclude?.length) {
112
- for (const excl of exclude) {
113
- if (typeof excl === 'string' || 'glob' in excl) {
114
- urlExcludePatternObjects.push(...constructGlobObjectsFromGlobs([excl]));
115
- }
116
- else if (excl instanceof RegExp || 'regexp' in excl) {
117
- urlExcludePatternObjects.push(...constructRegExpObjectsFromRegExps([excl]));
118
- }
119
- }
120
- }
121
- if (pseudoUrls?.length) {
122
- log.deprecated('`pseudoUrls` option is deprecated, use `globs` or `regexps` instead');
123
- urlPatternObjects.push(...constructRegExpObjectsFromPseudoUrls(pseudoUrls));
124
- }
125
- if (globs?.length) {
126
- urlPatternObjects.push(...constructGlobObjectsFromGlobs(globs));
127
- }
128
- if (regexps?.length) {
129
- urlPatternObjects.push(...constructRegExpObjectsFromRegExps(regexps));
130
- }
131
- if (!urlPatternObjects.length) {
132
- options.strategy ??= EnqueueStrategy.SameHostname;
133
- }
108
+ const { requestManager, limit, urls, include, exclude, transformRequestFunction, forefront, waitForAllRequestsToBeAdded, robotsTxtFile, onSkippedRequest, } = options;
109
+ const urlExcludePatternObjects = exclude?.length ? constructUrlPatternObjects(exclude) : [];
110
+ const urlPatternObjects = include?.length ? constructUrlPatternObjects(include) : [];
111
+ // The strategy always applies, even when `include` patterns are provided - the two are AND-ed together
112
+ // (a URL must match an `include` pattern *and* satisfy the strategy). This mirrors crawlee-python.
113
+ options.strategy ??= EnqueueStrategy.SameHostname;
134
114
  const enqueueStrategyPatterns = [];
135
115
  if (options.baseUrl) {
136
116
  const url = new URL(options.baseUrl);
@@ -167,44 +147,60 @@ export async function enqueueLinks(options) {
167
147
  break;
168
148
  }
169
149
  }
150
+ async function reportSkippedRequests(skippedRequests, reason) {
151
+ if (onSkippedRequest && skippedRequests.length > 0) {
152
+ await Promise.all(skippedRequests.map((request) => {
153
+ return onSkippedRequest({
154
+ url: request.url,
155
+ reason: request.skippedReason ?? reason,
156
+ });
157
+ }));
158
+ }
159
+ }
170
160
  let requestOptions = createRequestOptions(urls, options);
171
- if (robotsTxtFile) {
161
+ if (robotsTxtFile && options.respectRobotsTxtFile !== false) {
162
+ const robotsUserAgent = typeof options.respectRobotsTxtFile === 'object' ? (options.respectRobotsTxtFile.userAgent ?? '*') : '*';
172
163
  const skippedRequests = [];
173
164
  requestOptions = requestOptions.filter((request) => {
174
- if (robotsTxtFile.isAllowed(request.url)) {
165
+ if (robotsTxtFile.isAllowed(request.url, robotsUserAgent)) {
175
166
  return true;
176
167
  }
177
168
  skippedRequests.push(request);
178
169
  return false;
179
170
  });
180
- if (onSkippedRequest && skippedRequests.length > 0) {
181
- await Promise.all(skippedRequests.map((request) => {
182
- return onSkippedRequest({ url: request.url, reason: 'robotsTxt' });
183
- }));
184
- }
171
+ await reportSkippedRequests(skippedRequests, 'robotsTxt');
185
172
  }
186
- if (transformRequestFunction) {
187
- requestOptions = requestOptions
188
- .map((request) => transformRequestFunction(request))
189
- .filter((r) => !!r);
190
- }
191
- function createFilteredRequests() {
192
- // No user provided patterns means we can skip an extra filtering step
173
+ async function createFilteredRequests() {
174
+ const skippedRequests = [];
175
+ // Step 1: Filter request options by exclude patterns, user include patterns, and strategy patterns.
176
+ let filteredOptions;
193
177
  if (urlPatternObjects.length === 0) {
194
- return createRequests(requestOptions, enqueueStrategyPatterns, urlExcludePatternObjects, options.strategy);
178
+ filteredOptions = filterRequestOptionsByPatterns(requestOptions, enqueueStrategyPatterns.length > 0 ? enqueueStrategyPatterns : undefined, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
179
+ }
180
+ else {
181
+ // Filter by user patterns first (with exclude)
182
+ const afterUserPatterns = filterRequestOptionsByPatterns(requestOptions, urlPatternObjects, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
183
+ // ...then filter by the enqueue links strategy (making this an AND check)
184
+ filteredOptions = filterRequestOptionsByPatterns(afterUserPatterns, enqueueStrategyPatterns.length > 0 ? enqueueStrategyPatterns : undefined, [], options.strategy, (url) => skippedRequests.push(url));
195
185
  }
196
- // Generate requests based on the user patterns first
197
- const generatedRequestsFromUserFilters = createRequests(requestOptions, urlPatternObjects, urlExcludePatternObjects, options.strategy);
198
- // ...then filter them by the enqueue links strategy (making this an AND check)
199
- return filterRequestsByPatterns(generatedRequestsFromUserFilters, enqueueStrategyPatterns);
186
+ await reportSkippedRequests(skippedRequests.map((url) => ({ url })), 'filters');
187
+ // Step 2: Apply transformRequestFunction on request options - it has the highest priority
188
+ if (transformRequestFunction) {
189
+ const skippedByTransform = [];
190
+ filteredOptions = applyRequestTransform(filteredOptions, transformRequestFunction, (r) => skippedByTransform.push(r));
191
+ await reportSkippedRequests(skippedByTransform, 'transform');
192
+ }
193
+ // Step 3: Create Request instances from the final request options
194
+ return filteredOptions.map((opts) => new Request(opts));
200
195
  }
201
- let requests = createFilteredRequests();
202
- if (limit)
203
- requests = requests.slice(0, limit);
204
- const { addedRequests } = await requestQueue.addRequestsBatched(requests, {
196
+ const { addedRequests, requestsOverLimit } = await requestManager.addRequestsBatched(await createFilteredRequests(), {
205
197
  forefront,
206
198
  waitForAllRequestsToBeAdded,
199
+ maxNewRequests: limit,
207
200
  });
201
+ if (requestsOverLimit?.length !== undefined && requestsOverLimit.length > 0) {
202
+ await reportSkippedRequests(requestsOverLimit.map((r) => ({ url: typeof r === 'string' ? r : r.url })), 'enqueueLimit');
203
+ }
208
204
  return { processedRequests: addedRequests, unprocessedRequests: [] };
209
205
  }
210
206
  /**
@@ -242,9 +238,8 @@ export function resolveBaseUrlForEnqueueLinksFiltering({ enqueueStrategy, finalR
242
238
  return originalUrlOrigin;
243
239
  }
244
240
  /**
245
- * Internal function that changes the enqueue globs to match both http and https
241
+ * Internal function that changes the enqueue glob patterns to match both http and https
246
242
  */
247
243
  function ignoreHttpSchema(pattern) {
248
244
  return pattern.replace(/^(https?):\/\//, 'http{s,}://');
249
245
  }
250
- //# sourceMappingURL=enqueue_links.js.map
@@ -1,3 +1,2 @@
1
1
  export * from './enqueue_links.js';
2
2
  export * from './shared.js';
3
- //# sourceMappingURL=index.d.ts.map
@@ -1,3 +1,2 @@
1
1
  export * from './enqueue_links.js';
2
2
  export * from './shared.js';
3
- //# sourceMappingURL=index.js.map
@@ -1,33 +1,30 @@
1
+ import type { Awaitable } from '@crawlee/types';
1
2
  import type { RequestOptions } from '../request.js';
2
- import { Request } from '../request.js';
3
3
  import type { EnqueueLinksOptions } from './enqueue_links.js';
4
- export { tryAbsoluteURL } from '@crawlee/utils';
5
- export type UrlPatternObject = {
4
+ export { tryAbsoluteURL } from '@crawlee/utils/internal';
5
+ export interface UrlPatternObject {
6
6
  glob?: string;
7
7
  regexp?: RegExp;
8
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
9
- export type PseudoUrlObject = {
10
- purl: string;
11
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
12
- export type PseudoUrlInput = string | PseudoUrlObject;
13
- export type GlobObject = {
8
+ }
9
+ export interface GlobObject {
14
10
  glob: string;
15
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
11
+ }
16
12
  export type GlobInput = string | GlobObject;
17
- export type RegExpObject = {
13
+ export interface RegExpObject {
18
14
  regexp: RegExp;
19
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
15
+ }
20
16
  export type RegExpInput = RegExp | RegExpObject;
17
+ /** Unified URL pattern input — accepts glob strings, glob objects, RegExp instances, or regexp objects. */
18
+ export type UrlPatternInput = GlobInput | RegExpInput;
19
+ export type SkippedRequestReason = 'robotsTxt' | 'limit' | 'enqueueLimit' | 'filters' | 'transform' | 'redirect' | 'depth';
20
+ export type SkippedRequestCallback = (args: {
21
+ url: string;
22
+ reason: SkippedRequestReason;
23
+ }) => Awaitable<void>;
21
24
  /**
22
25
  * @ignore
23
26
  */
24
- export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput | PseudoUrlInput, pattern: RegExpObject | GlobObject): void;
25
- /**
26
- * Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
27
- * to construct RegExps from PseudoUrl strings.
28
- * @ignore
29
- */
30
- export declare function constructRegExpObjectsFromPseudoUrls(pseudoUrls: readonly PseudoUrlInput[]): RegExpObject[];
27
+ export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput, pattern: RegExpObject | GlobObject): void;
31
28
  /**
32
29
  * Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
33
30
  * to construct Glob objects from Glob pattern strings.
@@ -45,23 +42,39 @@ export declare function validateGlobPattern(glob: string): string;
45
42
  */
46
43
  export declare function constructRegExpObjectsFromRegExps(regexps: readonly RegExpInput[]): RegExpObject[];
47
44
  /**
45
+ * Helper factory used in the `enqueueLinks()` function to construct UrlPatternObjects
46
+ * from a mixed array of glob strings, glob objects, RegExp instances, and regexp objects.
48
47
  * @ignore
49
48
  */
50
- export declare function createRequests(requestOptions: (string | RequestOptions)[], urlPatternObjects?: UrlPatternObject[], excludePatternObjects?: UrlPatternObject[], strategy?: EnqueueLinksOptions['strategy']): Request[];
51
- export declare function filterRequestsByPatterns(requests: Request[], patterns?: UrlPatternObject[]): Request[];
49
+ export declare function constructUrlPatternObjects(patterns: readonly UrlPatternInput[]): UrlPatternObject[];
52
50
  /**
51
+ * Filters request options by URL patterns.
52
+ *
53
+ * When `includePatterns` is empty/undefined, all options pass through (only exclude filtering applies).
53
54
  * @ignore
54
55
  */
55
- export declare function createRequestOptions(sources: readonly (string | Record<string, unknown>)[], options?: Pick<EnqueueLinksOptions, 'label' | 'userData' | 'baseUrl' | 'skipNavigation' | 'strategy'>): RequestOptions[];
56
+ export declare function filterRequestOptionsByPatterns(requestOptions: RequestOptions[], includePatterns: UrlPatternObject[] | undefined, excludePatterns?: UrlPatternObject[], strategy?: EnqueueLinksOptions['strategy'], onSkippedUrl?: (url: string) => void): RequestOptions[];
56
57
  /**
57
- * Takes an Apify {@link RequestOptions} object and changes its attributes in a desired way. This user-function is used
58
- * {@link enqueueLinks} to modify requests before enqueuing them.
58
+ * @ignore
59
+ */
60
+ export declare function createRequestOptions(sources: readonly (string | Record<string, unknown>)[], options?: Pick<EnqueueLinksOptions, 'label' | 'userData' | 'baseUrl' | 'skipNavigation' | 'sessionId' | 'strategy'>): RequestOptions[];
61
+ /**
62
+ * Takes a {@link RequestOptions} object and changes its attributes in a desired way. This user-function is used
63
+ * by {@link enqueueLinks} to modify request options before they are converted to {@link Request} instances.
59
64
  */
60
65
  export interface RequestTransform {
61
66
  /**
62
67
  * @param original Request options to be modified.
63
- * @returns The modified request options to enqueue.
68
+ * @returns The modified request options to enqueue, `'unchanged'` to keep the original options as-is,
69
+ * or a falsy value / `'skip'` to exclude the request from the queue.
64
70
  */
65
- (original: RequestOptions): RequestOptions | false | undefined | null;
71
+ (original: RequestOptions): RequestOptions | false | undefined | null | 'skip' | 'unchanged';
66
72
  }
67
- //# sourceMappingURL=shared.d.ts.map
73
+ /**
74
+ * Applies a {@link RequestTransform} function to a list of request options.
75
+ * Options for which the transform returns a falsy value are removed from the list.
76
+ * @param onSkipped Called with the original request options when the transform returns a falsy value (i.e. the request is skipped).
77
+ * @ignore
78
+ * @internal
79
+ */
80
+ export declare function applyRequestTransform(requestOptions: RequestOptions[], transformFn: RequestTransform, onSkipped?: (requestOptions: RequestOptions) => void): RequestOptions[];