@crawlee/core 4.0.0-beta.15 → 4.0.0-beta.150

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 (287) hide show
  1. package/README.md +14 -14
  2. package/autoscaling/autoscaled_pool.d.ts +73 -177
  3. package/autoscaling/autoscaled_pool.js +182 -329
  4. package/autoscaling/concurrency_system.d.ts +268 -0
  5. package/autoscaling/concurrency_system.js +364 -0
  6. package/autoscaling/cpu_load_signal.d.ts +43 -0
  7. package/autoscaling/cpu_load_signal.js +47 -0
  8. package/autoscaling/event_loop_load_signal.d.ts +51 -0
  9. package/autoscaling/event_loop_load_signal.js +60 -0
  10. package/autoscaling/index.d.ts +6 -1
  11. package/autoscaling/index.js +6 -1
  12. package/autoscaling/load_signal.d.ts +98 -0
  13. package/autoscaling/load_signal.js +103 -0
  14. package/autoscaling/memory_load_signal.d.ts +47 -0
  15. package/autoscaling/memory_load_signal.js +106 -0
  16. package/autoscaling/snapshotter.d.ts +58 -163
  17. package/autoscaling/snapshotter.js +45 -263
  18. package/autoscaling/storage_backend_load_signal.d.ts +56 -0
  19. package/autoscaling/storage_backend_load_signal.js +73 -0
  20. package/autoscaling/system_status.d.ts +67 -89
  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 +3 -2
  29. package/cookie_utils.js +18 -7
  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 -126
  33. package/crawlers/crawler_commons.js +1 -108
  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 +187 -64
  43. package/crawlers/statistics.js +354 -165
  44. package/debug.d.ts +36 -0
  45. package/debug.js +70 -0
  46. package/enqueue_links/enqueue_links.d.ts +61 -154
  47. package/enqueue_links/enqueue_links.js +40 -232
  48. package/enqueue_links/index.d.ts +0 -1
  49. package/enqueue_links/index.js +0 -1
  50. package/enqueue_links/shared.d.ts +55 -28
  51. package/enqueue_links/shared.js +116 -69
  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 +12 -13
  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 -5
  63. package/index.js +8 -4
  64. package/iterables.d.ts +79 -0
  65. package/iterables.js +134 -0
  66. package/log.d.ts +77 -1
  67. package/log.js +104 -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 +44 -0
  73. package/memory-storage/memory-storage.js +160 -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 +106 -0
  78. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  79. package/memory-storage/resource-clients/key-value-store.js +199 -0
  80. package/memory-storage/resource-clients/request-queue.d.ts +77 -0
  81. package/memory-storage/resource-clients/request-queue.js +407 -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 +22 -128
  88. package/proxy_configuration.js +32 -144
  89. package/recoverable_state.d.ts +83 -51
  90. package/recoverable_state.js +163 -72
  91. package/request.d.ts +57 -16
  92. package/request.js +130 -69
  93. package/router.d.ts +193 -21
  94. package/router.js +188 -43
  95. package/serialization.d.ts +0 -1
  96. package/serialization.js +15 -15
  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 +39 -89
  108. package/session_pool/session.js +102 -159
  109. package/session_pool/session_pool.d.ts +67 -91
  110. package/session_pool/session_pool.js +196 -187
  111. package/storages/batched_adds.d.ts +37 -0
  112. package/storages/batched_adds.js +73 -0
  113. package/storages/dataset.d.ts +109 -56
  114. package/storages/dataset.js +284 -149
  115. package/storages/index.d.ts +9 -9
  116. package/storages/index.js +7 -9
  117. package/storages/key_value_store.d.ts +183 -48
  118. package/storages/key_value_store.js +444 -171
  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 +53 -115
  124. package/storages/request_list.js +194 -167
  125. package/storages/request_loader.d.ts +133 -0
  126. package/storages/request_loader.js +36 -0
  127. package/storages/request_manager.d.ts +109 -0
  128. package/storages/request_manager.js +1 -0
  129. package/storages/request_manager_tandem.d.ts +55 -23
  130. package/storages/request_manager_tandem.js +139 -57
  131. package/storages/request_queue.d.ts +286 -48
  132. package/storages/request_queue.js +757 -218
  133. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +43 -91
  134. package/storages/sitemap_request_loader.js +439 -0
  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 +222 -0
  140. package/storages/throttling_request_manager.js +780 -0
  141. package/storages/transaction.d.ts +252 -0
  142. package/storages/transaction.js +251 -0
  143. package/storages/utils.d.ts +59 -11
  144. package/storages/utils.js +75 -15
  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 +22 -18
  158. package/validators.js +13 -18
  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 -140
  204. package/http_clients/base-http-client.d.ts.map +0 -1
  205. package/http_clients/base-http-client.js +0 -40
  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 -20
  212. package/http_clients/got-scraping-http-client.d.ts.map +0 -1
  213. package/http_clients/got-scraping-http-client.js +0 -85
  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/recoverable_state.d.ts.map +0 -1
  226. package/recoverable_state.js.map +0 -1
  227. package/request.d.ts.map +0 -1
  228. package/request.js.map +0 -1
  229. package/router.d.ts.map +0 -1
  230. package/router.js.map +0 -1
  231. package/serialization.d.ts.map +0 -1
  232. package/serialization.js.map +0 -1
  233. package/session_pool/consts.d.ts.map +0 -1
  234. package/session_pool/consts.js.map +0 -1
  235. package/session_pool/errors.d.ts.map +0 -1
  236. package/session_pool/errors.js.map +0 -1
  237. package/session_pool/events.d.ts +0 -3
  238. package/session_pool/events.d.ts.map +0 -1
  239. package/session_pool/events.js +0 -3
  240. package/session_pool/events.js.map +0 -1
  241. package/session_pool/index.d.ts.map +0 -1
  242. package/session_pool/index.js.map +0 -1
  243. package/session_pool/session.d.ts.map +0 -1
  244. package/session_pool/session.js.map +0 -1
  245. package/session_pool/session_pool.d.ts.map +0 -1
  246. package/session_pool/session_pool.js.map +0 -1
  247. package/storages/access_checking.d.ts +0 -13
  248. package/storages/access_checking.d.ts.map +0 -1
  249. package/storages/access_checking.js +0 -14
  250. package/storages/access_checking.js.map +0 -1
  251. package/storages/dataset.d.ts.map +0 -1
  252. package/storages/dataset.js.map +0 -1
  253. package/storages/index.d.ts.map +0 -1
  254. package/storages/index.js.map +0 -1
  255. package/storages/key_value_store.d.ts.map +0 -1
  256. package/storages/key_value_store.js.map +0 -1
  257. package/storages/request_list.d.ts.map +0 -1
  258. package/storages/request_list.js.map +0 -1
  259. package/storages/request_list_adapter.d.ts +0 -58
  260. package/storages/request_list_adapter.d.ts.map +0 -1
  261. package/storages/request_list_adapter.js +0 -81
  262. package/storages/request_list_adapter.js.map +0 -1
  263. package/storages/request_manager_tandem.d.ts.map +0 -1
  264. package/storages/request_manager_tandem.js.map +0 -1
  265. package/storages/request_provider.d.ts +0 -371
  266. package/storages/request_provider.d.ts.map +0 -1
  267. package/storages/request_provider.js +0 -585
  268. package/storages/request_provider.js.map +0 -1
  269. package/storages/request_queue.d.ts.map +0 -1
  270. package/storages/request_queue.js.map +0 -1
  271. package/storages/request_queue_v2.d.ts +0 -87
  272. package/storages/request_queue_v2.d.ts.map +0 -1
  273. package/storages/request_queue_v2.js +0 -438
  274. package/storages/request_queue_v2.js.map +0 -1
  275. package/storages/sitemap_request_list.d.ts.map +0 -1
  276. package/storages/sitemap_request_list.js +0 -430
  277. package/storages/sitemap_request_list.js.map +0 -1
  278. package/storages/storage_manager.d.ts +0 -58
  279. package/storages/storage_manager.d.ts.map +0 -1
  280. package/storages/storage_manager.js +0 -105
  281. package/storages/storage_manager.js.map +0 -1
  282. package/storages/utils.d.ts.map +0 -1
  283. package/storages/utils.js.map +0 -1
  284. package/typedefs.d.ts.map +0 -1
  285. package/typedefs.js.map +0 -1
  286. package/validators.d.ts.map +0 -1
  287. package/validators.js.map +0 -1
@@ -1,231 +1,6 @@
1
- import ow from 'ow';
1
+ import { EnqueueStrategy } from '@crawlee/utils';
2
2
  import { getDomain } from 'tldts';
3
- import log from '@apify/log';
4
- import { constructGlobObjectsFromGlobs, constructRegExpObjectsFromPseudoUrls, constructRegExpObjectsFromRegExps, createRequestOptions, createRequests, filterRequestsByPatterns, } from './shared.js';
5
- /**
6
- * The different enqueueing strategies available.
7
- *
8
- * Depending on the strategy you select, we will only check certain parts of the URLs found. Here is a diagram of each URL part and their name:
9
- *
10
- * ```md
11
- * Protocol Domain
12
- * ┌────┐ ┌─────────┐
13
- * https://example.crawlee.dev/...
14
- * │ └─────────────────┤
15
- * │ Hostname │
16
- * │ │
17
- * └─────────────────────────┘
18
- * Origin
19
- *```
20
- *
21
- * - The `Protocol` is usually `http` or `https`
22
- * - The `Domain` represents the path without any possible subdomains to a website. For example, `crawlee.dev` is the domain of `https://example.crawlee.dev/`
23
- * - The `Hostname` is the full path to a website, including any subdomains. For example, `example.crawlee.dev` is the hostname of `https://example.crawlee.dev/`
24
- * - The `Origin` is the combination of the `Protocol` and `Hostname`. For example, `https://example.crawlee.dev` is the origin of `https://example.crawlee.dev/`
25
- */
26
- export var EnqueueStrategy;
27
- (function (EnqueueStrategy) {
28
- /**
29
- * Matches any URLs found
30
- */
31
- EnqueueStrategy["All"] = "all";
32
- /**
33
- * Matches any URLs that have the same hostname.
34
- * For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
35
- * `https://example.com/hello` will not be matched.
36
- *
37
- * > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
38
- */
39
- EnqueueStrategy["SameHostname"] = "same-hostname";
40
- /**
41
- * Matches any URLs that have the same domain as the base URL.
42
- * For example, `https://wow.an.example.com` and `https://example.com` will both be matched for a base url of
43
- * `https://example.com`.
44
- *
45
- * > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
46
- */
47
- EnqueueStrategy["SameDomain"] = "same-domain";
48
- /**
49
- * Matches any URLs that have the same hostname and protocol.
50
- * For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
51
- * `http://wow.example.com/hello` will not be matched.
52
- *
53
- * > This strategy will ensure the protocol of the base URL is the same as the protocol of the URL to be enqueued.
54
- */
55
- EnqueueStrategy["SameOrigin"] = "same-origin";
56
- })(EnqueueStrategy || (EnqueueStrategy = {}));
57
- /**
58
- * This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
59
- * you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
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.
63
- *
64
- * **Example usage**
65
- *
66
- * ```javascript
67
- * await enqueueLinks({
68
- * urls: aListOfFoundUrls,
69
- * requestQueue,
70
- * selector: 'a.product-detail',
71
- * globs: [
72
- * 'https://www.example.com/handbags/*',
73
- * 'https://www.example.com/purses/*'
74
- * ],
75
- * });
76
- * ```
77
- *
78
- * @param options All `enqueueLinks()` parameters are passed via an options object.
79
- * @returns Promise that resolves to {@link BatchAddRequestsResult} object.
80
- */
81
- export async function enqueueLinks(options) {
82
- if (!options || Object.keys(options).length === 0) {
83
- throw new RangeError([
84
- 'enqueueLinks() was called without the required options. You can only do that when you use the `crawlingContext.enqueueLinks()` method in request handlers.',
85
- 'Check out our guide on how to use enqueueLinks() here: https://crawlee.dev/js/docs/examples/crawl-relative-links',
86
- ].join('\n'));
87
- }
88
- ow(options, ow.object.exactShape({
89
- urls: ow.array.ofType(ow.string),
90
- requestQueue: ow.object.hasKeys('addRequestsBatched'),
91
- robotsTxtFile: ow.optional.object.hasKeys('isAllowed'),
92
- onSkippedRequest: ow.optional.function,
93
- forefront: ow.optional.boolean,
94
- skipNavigation: ow.optional.boolean,
95
- limit: ow.optional.number,
96
- selector: ow.optional.string,
97
- baseUrl: ow.optional.string,
98
- userData: ow.optional.object,
99
- 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'))),
104
- transformRequestFunction: ow.optional.function,
105
- strategy: ow.optional.string.oneOf(Object.values(EnqueueStrategy)),
106
- waitForAllRequestsToBeAdded: ow.optional.boolean,
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
- }
134
- const enqueueStrategyPatterns = [];
135
- if (options.baseUrl) {
136
- const url = new URL(options.baseUrl);
137
- switch (options.strategy) {
138
- case EnqueueStrategy.SameHostname:
139
- // We need to get the origin of the passed in domain in the event someone sets baseUrl
140
- // to an url like https://example.com/deep/default/path and one of the found urls is an
141
- // absolute relative path (/path/to/page)
142
- enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin}/**`) });
143
- break;
144
- case EnqueueStrategy.SameDomain: {
145
- // Get the actual hostname from the base url
146
- const baseUrlHostname = getDomain(url.hostname, { mixedInputs: false });
147
- if (baseUrlHostname) {
148
- // We have a hostname, so we can use it to match all links on the page that point to it and any subdomains of it
149
- url.hostname = baseUrlHostname;
150
- enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin.replace(baseUrlHostname, `*.${baseUrlHostname}`)}/**`) }, { glob: ignoreHttpSchema(`${url.origin}/**`) });
151
- }
152
- else {
153
- // We don't have a hostname (can happen for ips for instance), so reproduce the same behavior
154
- // as SameDomainAndSubdomain
155
- enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin}/**`) });
156
- }
157
- break;
158
- }
159
- case EnqueueStrategy.SameOrigin: {
160
- // The same behavior as SameHostname, but respecting the protocol of the URL
161
- enqueueStrategyPatterns.push({ glob: `${url.origin}/**` });
162
- break;
163
- }
164
- case EnqueueStrategy.All:
165
- default:
166
- enqueueStrategyPatterns.push({ glob: `http{s,}://**` });
167
- break;
168
- }
169
- }
170
- async function reportSkippedRequests(skippedRequests, reason) {
171
- if (onSkippedRequest && skippedRequests.length > 0) {
172
- await Promise.all(skippedRequests.map((request) => {
173
- return onSkippedRequest({
174
- url: request.url,
175
- reason: request.skippedReason ?? reason,
176
- });
177
- }));
178
- }
179
- }
180
- let requestOptions = createRequestOptions(urls, options);
181
- if (robotsTxtFile) {
182
- const skippedRequests = [];
183
- requestOptions = requestOptions.filter((request) => {
184
- if (robotsTxtFile.isAllowed(request.url)) {
185
- return true;
186
- }
187
- skippedRequests.push(request);
188
- return false;
189
- });
190
- await reportSkippedRequests(skippedRequests, 'robotsTxt');
191
- }
192
- if (transformRequestFunction) {
193
- const skippedRequests = [];
194
- requestOptions = requestOptions
195
- .map((request) => {
196
- const transformedRequest = transformRequestFunction(request);
197
- if (!transformedRequest) {
198
- skippedRequests.push(request);
199
- }
200
- return transformedRequest;
201
- })
202
- .filter((r) => Boolean(r));
203
- await reportSkippedRequests(skippedRequests, 'filters');
204
- }
205
- async function createFilteredRequests() {
206
- const skippedRequests = [];
207
- // No user provided patterns means we can skip an extra filtering step
208
- if (urlPatternObjects.length === 0) {
209
- return createRequests(requestOptions, enqueueStrategyPatterns, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
210
- }
211
- // Generate requests based on the user patterns first
212
- const generatedRequestsFromUserFilters = createRequests(requestOptions, urlPatternObjects, urlExcludePatternObjects, options.strategy, (url) => skippedRequests.push(url));
213
- // ...then filter them by the enqueue links strategy (making this an AND check)
214
- const filtered = filterRequestsByPatterns(generatedRequestsFromUserFilters, enqueueStrategyPatterns, (url) => skippedRequests.push(url));
215
- await reportSkippedRequests(skippedRequests.map((url) => ({ url })), 'filters');
216
- return filtered;
217
- }
218
- let requests = await createFilteredRequests();
219
- if (typeof limit === 'number' && limit < requests.length) {
220
- await reportSkippedRequests(requests.slice(limit), 'enqueueLimit');
221
- requests = requests.slice(0, limit);
222
- }
223
- const { addedRequests } = await requestQueue.addRequestsBatched(requests, {
224
- forefront,
225
- waitForAllRequestsToBeAdded,
226
- });
227
- return { processedRequests: addedRequests, unprocessedRequests: [] };
228
- }
3
+ export { EnqueueStrategy };
229
4
  /**
230
5
  * @internal
231
6
  * This method helps resolve the baseUrl that will be used for filtering in {@link enqueueLinks}.
@@ -246,24 +21,57 @@ export function resolveBaseUrlForEnqueueLinksFiltering({ enqueueStrategy, finalR
246
21
  return finalUrlOrigin;
247
22
  }
248
23
  // If the user wants to ensure the same domain is accessed, regardless of subdomains, we check to ensure the domains match
249
- // Returning undefined here is intentional! If the domains don't match, having no baseUrl in enqueueLinks will cause it to not enqueue anything
250
- // which is the intended behavior (since we went off domain)
24
+ // If they don't (we went off domain via a redirect), we keep filtering against the original domain - returning undefined would disable the filtering entirely
251
25
  if (enqueueStrategy === EnqueueStrategy.SameDomain) {
252
26
  const originalHostname = getDomain(originalUrlOrigin, { mixedInputs: false });
253
27
  const finalHostname = getDomain(finalUrlOrigin, { mixedInputs: false });
254
28
  if (originalHostname === finalHostname) {
255
29
  return finalUrlOrigin;
256
30
  }
257
- return undefined;
31
+ return originalUrlOrigin;
258
32
  }
259
33
  // Always enqueue urls that are from the same origin in all other cases, as the filtering happens on the original request url, even if there was a redirect
260
34
  // before actually finding the urls
261
35
  return originalUrlOrigin;
262
36
  }
263
37
  /**
264
- * Internal function that changes the enqueue globs to match both http and https
38
+ * @internal
39
+ * Builds the glob patterns a URL must match to satisfy the given enqueue `strategy`, anchored at `baseUrl`.
40
+ */
41
+ export function buildEnqueueStrategyPatterns(baseUrl, strategy) {
42
+ const url = new URL(baseUrl);
43
+ switch (strategy) {
44
+ case EnqueueStrategy.SameHostname:
45
+ // We need to get the origin of the passed in domain in the event someone sets baseUrl
46
+ // to an url like https://example.com/deep/default/path and one of the found urls is an
47
+ // absolute relative path (/path/to/page)
48
+ return [{ glob: ignoreHttpSchema(`${url.origin}/**`) }];
49
+ case EnqueueStrategy.SameDomain: {
50
+ // Get the actual hostname from the base url
51
+ const baseUrlHostname = getDomain(url.hostname, { mixedInputs: false });
52
+ if (baseUrlHostname) {
53
+ // We have a hostname, so we can use it to match all links on the page that point to it and any subdomains of it
54
+ url.hostname = baseUrlHostname;
55
+ return [
56
+ { glob: ignoreHttpSchema(`${url.origin.replace(baseUrlHostname, `*.${baseUrlHostname}`)}/**`) },
57
+ { glob: ignoreHttpSchema(`${url.origin}/**`) },
58
+ ];
59
+ }
60
+ // We don't have a hostname (can happen for ips for instance), so reproduce the same behavior
61
+ // as SameDomainAndSubdomain
62
+ return [{ glob: ignoreHttpSchema(`${url.origin}/**`) }];
63
+ }
64
+ case EnqueueStrategy.SameOrigin:
65
+ // The same behavior as SameHostname, but respecting the protocol of the URL
66
+ return [{ glob: `${url.origin}/**` }];
67
+ case EnqueueStrategy.All:
68
+ default:
69
+ return [{ glob: `http{s,}://**` }];
70
+ }
71
+ }
72
+ /**
73
+ * Internal function that changes the enqueue glob patterns to match both http and https
265
74
  */
266
75
  function ignoreHttpSchema(pattern) {
267
76
  return pattern.replace(/^(https?):\/\//, 'http{s,}://');
268
77
  }
269
- //# 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,39 +1,43 @@
1
- import type { Awaitable } from '@crawlee/types';
2
- import type { RequestOptions } from '../request.js';
1
+ import type { Awaitable, Dictionary } from '@crawlee/types';
2
+ import { z } from 'zod';
3
+ import type { RequestOptions, Source } from '../request.js';
3
4
  import { Request } from '../request.js';
4
- import type { EnqueueLinksOptions } from './enqueue_links.js';
5
- export { tryAbsoluteURL } from '@crawlee/utils';
6
- export type UrlPatternObject = {
5
+ import type { EnqueueStrategyOption } from './enqueue_links.js';
6
+ export interface UrlPatternObject {
7
7
  glob?: string;
8
8
  regexp?: RegExp;
9
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
10
- export type PseudoUrlObject = {
11
- purl: string;
12
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
13
- export type PseudoUrlInput = string | PseudoUrlObject;
14
- export type GlobObject = {
9
+ }
10
+ export interface GlobObject {
15
11
  glob: string;
16
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
12
+ }
17
13
  export type GlobInput = string | GlobObject;
18
- export type RegExpObject = {
14
+ export interface RegExpObject {
19
15
  regexp: RegExp;
20
- } & Pick<RequestOptions, 'method' | 'payload' | 'label' | 'userData' | 'headers'>;
16
+ }
21
17
  export type RegExpInput = RegExp | RegExpObject;
22
- export type SkippedRequestReason = 'robotsTxt' | 'limit' | 'enqueueLimit' | 'filters' | 'redirect' | 'depth';
18
+ /** Unified URL pattern input accepts glob strings, glob objects, RegExp instances, or regexp objects. */
19
+ export type UrlPatternInput = GlobInput | RegExpInput;
20
+ /**
21
+ * Accepts one {@link UrlPatternInput} — a glob string, a RegExp instance, or a `{ glob }` / `{ regexp }` object.
22
+ * @internal
23
+ */
24
+ export declare const urlPatternSchema: z.ZodType<UrlPatternInput>;
25
+ export type SkippedRequestReason = 'robotsTxt' | 'limit' | 'enqueueLimit' | 'filters' | 'transform' | 'redirect' | 'depth';
23
26
  export type SkippedRequestCallback = (args: {
24
- url: string;
27
+ request: Request;
25
28
  reason: SkippedRequestReason;
26
29
  }) => Awaitable<void>;
27
30
  /**
31
+ * Builds the `{ request, reason }` argument passed to a {@link SkippedRequestCallback}, constructing the
32
+ * `Request` lazily on first access to `request` (and caching it) since most skips are never observed by a
33
+ * real callback and building a `Request` isn't free.
28
34
  * @ignore
29
35
  */
30
- export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput | PseudoUrlInput, pattern: RegExpObject | GlobObject): void;
36
+ export declare function createSkippedRequestArgs(source: string | Source, reason: SkippedRequestReason): Parameters<SkippedRequestCallback>[0];
31
37
  /**
32
- * Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
33
- * to construct RegExps from PseudoUrl strings.
34
38
  * @ignore
35
39
  */
36
- export declare function constructRegExpObjectsFromPseudoUrls(pseudoUrls: readonly PseudoUrlInput[]): RegExpObject[];
40
+ export declare function updateEnqueueLinksPatternCache(item: GlobInput | RegExpInput, pattern: RegExpObject | GlobObject): void;
37
41
  /**
38
42
  * Helper factory used in the `enqueueLinks()` and enqueueLinksByClickingElements() function
39
43
  * to construct Glob objects from Glob pattern strings.
@@ -51,23 +55,46 @@ export declare function validateGlobPattern(glob: string): string;
51
55
  */
52
56
  export declare function constructRegExpObjectsFromRegExps(regexps: readonly RegExpInput[]): RegExpObject[];
53
57
  /**
58
+ * Helper factory used in the `enqueueLinks()` function to construct UrlPatternObjects
59
+ * from a mixed array of glob strings, glob objects, RegExp instances, and regexp objects.
54
60
  * @ignore
55
61
  */
56
- export declare function createRequests(requestOptions: (string | RequestOptions)[], urlPatternObjects?: UrlPatternObject[], excludePatternObjects?: UrlPatternObject[], strategy?: EnqueueLinksOptions['strategy'], onSkippedUrl?: (url: string) => void): Request[];
57
- export declare function filterRequestsByPatterns(requests: Request[], patterns?: UrlPatternObject[], onSkippedUrl?: (url: string) => void): Request[];
62
+ export declare function constructUrlPatternObjects(patterns: readonly UrlPatternInput[]): UrlPatternObject[];
58
63
  /**
64
+ * Filters request options by URL patterns.
65
+ *
66
+ * When `includePatterns` is empty/undefined, all options pass through (only exclude filtering applies).
59
67
  * @ignore
60
68
  */
61
- export declare function createRequestOptions(sources: readonly (string | Record<string, unknown>)[], options?: Pick<EnqueueLinksOptions, 'label' | 'userData' | 'baseUrl' | 'skipNavigation' | 'strategy'>): RequestOptions[];
69
+ export declare function filterRequestOptionsByPatterns(requestOptions: RequestOptions[], includePatterns: UrlPatternObject[] | undefined, excludePatterns?: UrlPatternObject[], strategy?: EnqueueStrategyOption, onSkippedRequestOptions?: (options: RequestOptions) => void): RequestOptions[];
62
70
  /**
63
- * Takes an Apify {@link RequestOptions} object and changes its attributes in a desired way. This user-function is used
64
- * {@link enqueueLinks} to modify requests before enqueuing them.
71
+ * @ignore
72
+ */
73
+ export declare function createRequestOptions(sources: readonly (string | Record<string, unknown>)[], options?: {
74
+ label?: string;
75
+ userData?: Dictionary;
76
+ baseUrl?: string;
77
+ skipNavigation?: boolean;
78
+ sessionId?: string;
79
+ strategy?: EnqueueStrategyOption;
80
+ }): RequestOptions[];
81
+ /**
82
+ * Takes a {@link RequestOptions} object and changes its attributes in a desired way. This user-function is used
83
+ * by {@link enqueueLinks} to modify request options before they are converted to {@link Request} instances.
65
84
  */
66
85
  export interface RequestTransform {
67
86
  /**
68
87
  * @param original Request options to be modified.
69
- * @returns The modified request options to enqueue.
88
+ * @returns The modified request options to enqueue, `'unchanged'` to keep the original options as-is,
89
+ * or a falsy value / `'skip'` to exclude the request from the queue.
70
90
  */
71
- (original: RequestOptions): RequestOptions | false | undefined | null;
91
+ (original: RequestOptions): RequestOptions | false | undefined | null | 'skip' | 'unchanged';
72
92
  }
73
- //# sourceMappingURL=shared.d.ts.map
93
+ /**
94
+ * Applies a {@link RequestTransform} function to a list of request options.
95
+ * Options for which the transform returns a falsy value are removed from the list.
96
+ * @param onSkipped Called with the original request options when the transform returns a falsy value (i.e. the request is skipped).
97
+ * @ignore
98
+ * @internal
99
+ */
100
+ export declare function applyRequestTransform(requestOptions: RequestOptions[], transformFn: RequestTransform, onSkipped?: (requestOptions: RequestOptions) => void): RequestOptions[];