@crawlee/core 4.0.0-beta.13 → 4.0.0-beta.130

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 (280) hide show
  1. package/README.md +17 -13
  2. package/autoscaling/autoscaled_pool.d.ts +67 -172
  3. package/autoscaling/autoscaled_pool.js +182 -329
  4. package/autoscaling/concurrency_system.d.ts +268 -0
  5. package/autoscaling/concurrency_system.js +362 -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 +100 -0
  13. package/autoscaling/load_signal.js +105 -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 -129
  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 +187 -63
  43. package/crawlers/statistics.js +354 -164
  44. package/debug.d.ts +36 -0
  45. package/debug.js +70 -0
  46. package/enqueue_links/enqueue_links.d.ts +68 -154
  47. package/enqueue_links/enqueue_links.js +38 -210
  48. package/enqueue_links/index.d.ts +0 -1
  49. package/enqueue_links/index.js +0 -1
  50. package/enqueue_links/shared.d.ts +52 -26
  51. package/enqueue_links/shared.js +115 -66
  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 -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 +108 -0
  78. package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
  79. package/memory-storage/resource-clients/key-value-store.js +201 -0
  80. package/memory-storage/resource-clients/request-queue.d.ts +77 -0
  81. package/memory-storage/resource-clients/request-queue.js +409 -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 +14 -13
  87. package/proxy_configuration.d.ts +24 -132
  88. package/proxy_configuration.js +32 -147
  89. package/recoverable_state.d.ts +155 -0
  90. package/recoverable_state.js +231 -0
  91. package/request.d.ts +80 -16
  92. package/request.js +147 -68
  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 +35 -88
  108. package/session_pool/session.js +101 -159
  109. package/session_pool/session_pool.d.ts +73 -91
  110. package/session_pool/session_pool.js +172 -165
  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 +291 -148
  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 +445 -169
  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 -115
  124. package/storages/request_list.js +183 -152
  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 +762 -216
  133. package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +45 -89
  134. package/storages/sitemap_request_loader.js +438 -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 +239 -0
  140. package/storages/throttling_request_manager.js +646 -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 -82
  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 +0 -430
  269. package/storages/sitemap_request_list.js.map +0 -1
  270. package/storages/storage_manager.d.ts +0 -58
  271. package/storages/storage_manager.d.ts.map +0 -1
  272. package/storages/storage_manager.js +0 -105
  273. package/storages/storage_manager.js.map +0 -1
  274. package/storages/utils.d.ts.map +0 -1
  275. package/storages/utils.js.map +0 -1
  276. package/tsconfig.build.tsbuildinfo +0 -1
  277. package/typedefs.d.ts.map +0 -1
  278. package/typedefs.js.map +0 -1
  279. package/validators.d.ts.map +0 -1
  280. package/validators.js.map +0 -1
@@ -1,19 +1,63 @@
1
- import type { Dictionary } from '@crawlee/types';
2
- // @ts-ignore optional peer dependency or compatibility with es2022
3
- import type { OptionsInit } from 'got-scraping';
4
- import type { ReadonlyDeep, SetRequired } from 'type-fest';
5
- import type { Configuration } from '../configuration.js';
6
- import type { EnqueueLinksOptions } from '../enqueue_links/enqueue_links.js';
7
- import type { Log } from '../log.js';
8
- import type { ProxyInfo } from '../proxy_configuration.js';
9
- import type { Request, Source } from '../request.js';
10
- import type { Session } from '../session_pool/session.js';
1
+ import type { Dictionary, HttpRequestOptions, ISession, ProxyInfo, SendRequestOptions } from '@crawlee/types';
2
+ import type { ReadonlyDeep } from 'type-fest';
3
+ import type { EnqueueUrlsOptions } from '../enqueue_links/enqueue_links.js';
4
+ import type { CrawleeLogger } from '../log.js';
5
+ import type { Request, RequestOptions, Source } from '../request.js';
6
+ import type { StorageIdentifier } from '../storages/storage_instance_manager.js';
11
7
  import type { Dataset } from '../storages/dataset.js';
12
- import { KeyValueStore, type RecordOptions } from '../storages/key_value_store.js';
13
- import type { RequestQueueOperationOptions } from '../storages/request_provider.js';
8
+ import type { KeyValueStore } from '../storages/key_value_store.js';
9
+ import type { AddRequestsBatchedResult } from '../storages/request_queue.js';
14
10
  /** @internal */
15
11
  export type IsAny<T> = 0 extends 1 & T ? true : false;
16
- /** @internal */
12
+ /**
13
+ * A request input (URL string, request-options object, or {@link Request}) whose `userData` is typed
14
+ * according to its `label`, based on a router's route map.
15
+ *
16
+ * When the route map is open (the default `Record<string, ...>`), this is just the regular loose
17
+ * {@link Source} input. When the map declares concrete labels, providing a `label` requires the matching
18
+ * `userData` shape and rejects labels not present in the map; unlabeled requests keep loose `userData`.
19
+ */
20
+ export type LabeledSource<Routes extends Record<keyof Routes, Dictionary>> = string extends keyof Routes ? string | Source : string | Request | ({
21
+ requestsFromUrl?: string;
22
+ regex?: RegExp;
23
+ } & ({
24
+ [Label in keyof Routes & string]: Omit<Partial<RequestOptions<Routes[Label]>>, 'label'> & {
25
+ label: Label;
26
+ };
27
+ }[keyof Routes & string] | (Omit<Partial<RequestOptions>, 'label'> & {
28
+ label?: undefined;
29
+ })));
30
+ /**
31
+ * The iterable/array of {@link LabeledSource} inputs accepted by the label-aware `addRequests`/`run`
32
+ * methods of a crawler bound to a typed router.
33
+ * @internal
34
+ */
35
+ export type TypedRequestsLike<Routes extends Record<keyof Routes, Dictionary>> = AsyncIterable<LabeledSource<Routes>> | Iterable<LabeledSource<Routes>> | LabeledSource<Routes>[];
36
+ /**
37
+ * The label-aware `addRequests` method signature exposed on a request handler's context when the crawler is
38
+ * bound to a typed router. Mirrors {@link RestrictedCrawlingContext.addRequests} with typed sources.
39
+ */
40
+ export type TypedContextAddRequests<Routes extends Record<keyof Routes, Dictionary>> = (requestsLike: ReadonlyDeep<LabeledSource<Routes>[]>, options?: ReadonlyDeep<EnqueueUrlsOptions>) => Promise<AddRequestsBatchedResult>;
41
+ /**
42
+ * An `enqueueLinks`-options object with its `label`/`userData` retyped according to a router's route map: a
43
+ * declared `label` requires the matching `userData` shape (unknown labels are rejected), while unlabeled
44
+ * calls keep loose `userData`. Returns the options unchanged when the route map is open (the default).
45
+ */
46
+ type TypedEnqueueLinksOptions<Options, Routes extends Record<keyof Routes, Dictionary>> = string extends keyof Routes ? Options : Omit<Options, 'label' | 'userData'> & ({
47
+ [Label in keyof Routes & string]: {
48
+ label: Label;
49
+ userData?: Routes[Label];
50
+ };
51
+ }[keyof Routes & string] | {
52
+ label?: undefined;
53
+ userData?: Dictionary;
54
+ });
55
+ /**
56
+ * Transforms a context's existing `enqueueLinks` method so that the `label`/`userData` in its options follow
57
+ * the router's route map, while preserving everything else about the signature (argument optionality and
58
+ * return type, which differ between crawler types).
59
+ */
60
+ export type TypedContextEnqueueLinks<EnqueueLinks, Routes extends Record<keyof Routes, Dictionary>> = EnqueueLinks extends (options?: infer Options) => infer Result ? (options?: TypedEnqueueLinksOptions<Options, Routes>) => Result : EnqueueLinks extends (options: infer Options) => infer Result ? (options: TypedEnqueueLinksOptions<Options, Routes>) => Result : EnqueueLinks;
17
61
  export type WithRequired<T, K extends keyof T> = T & {
18
62
  [P in K]-?: T[P];
19
63
  };
@@ -24,7 +68,7 @@ export type LoadedContext<Context extends RestrictedCrawlingContext> = IsAny<Con
24
68
  } & Omit<Context, 'request'>;
25
69
  export interface RestrictedCrawlingContext<UserData extends Dictionary = Dictionary> {
26
70
  id: string;
27
- session?: Session;
71
+ session: ISession;
28
72
  /**
29
73
  * An object with information about currently used proxy by the crawler
30
74
  * and configured by the {@link ProxyConfiguration} class.
@@ -41,39 +85,17 @@ export interface RestrictedCrawlingContext<UserData extends Dictionary = Diction
41
85
  *
42
86
  * @param [data] Data to be pushed to the default dataset.
43
87
  */
44
- pushData(data: ReadonlyDeep<Parameters<Dataset['pushData']>[0]>, datasetIdOrName?: string): Promise<void>;
88
+ pushData(data: ReadonlyDeep<Parameters<Dataset['pushData']>[0]>, datasetIdentifier?: string | StorageIdentifier): Promise<void>;
45
89
  /**
46
- * This function automatically finds and enqueues links from the current page, adding them to the {@link RequestQueue}
47
- * currently used by the crawler.
48
- *
49
- * Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
50
- * and override settings of the enqueued {@link Request} objects.
90
+ * Add requests directly to the request queue currently used by the crawler.
51
91
  *
52
- * Check out the [Crawl a website with relative links](https://crawlee.dev/js/docs/examples/crawl-relative-links) example
53
- * for more details regarding its usage.
54
- *
55
- * **Example usage**
56
- *
57
- * ```ts
58
- * async requestHandler({ enqueueLinks }) {
59
- * await enqueueLinks({
60
- * globs: [
61
- * 'https://www.example.com/handbags/*',
62
- * ],
63
- * });
64
- * },
65
- * ```
66
- *
67
- * @param [options] All `enqueueLinks()` parameters are passed via an options object.
68
- */
69
- enqueueLinks: (options: ReadonlyDeep<Omit<SetRequired<EnqueueLinksOptions, 'urls'>, 'requestQueue' | 'robotsTxtFile'>>) => Promise<unknown>;
70
- /**
71
- * Add requests directly to the request queue.
92
+ * Optionally, the function allows you to filter the target URLs using an array of glob or regexp patterns,
93
+ * the same way {@link CrawlingContext.enqueueLinks|`enqueueLinks`} does for extracted links.
72
94
  *
73
95
  * @param requests The requests to add
74
96
  * @param options Options for the request queue
75
97
  */
76
- addRequests: (requestsLike: ReadonlyDeep<(string | Source)[]>, options?: ReadonlyDeep<RequestQueueOperationOptions>) => Promise<void>;
98
+ addRequests: (requestsLike: ReadonlyDeep<(string | Source)[]>, options?: ReadonlyDeep<EnqueueUrlsOptions>) => Promise<AddRequestsBatchedResult>;
77
99
  /**
78
100
  * Returns the state - a piece of mutable persistent data shared across all the request handler runs.
79
101
  */
@@ -81,42 +103,15 @@ export interface RestrictedCrawlingContext<UserData extends Dictionary = Diction
81
103
  /**
82
104
  * Get a key-value store with given name or id, or the default one for the crawler.
83
105
  */
84
- getKeyValueStore: (idOrName?: string) => Promise<Pick<KeyValueStore, 'id' | 'name' | 'getValue' | 'getAutoSavedValue' | 'setValue' | 'getPublicUrl'>>;
106
+ getKeyValueStore: (identifier?: string | StorageIdentifier) => Promise<Pick<KeyValueStore, 'id' | 'name' | 'getValue' | 'getAutoSavedValue' | 'setValue' | 'getPublicUrl'>>;
85
107
  /**
86
108
  * A preconfigured logger for the request handler.
87
109
  */
88
- log: Log;
110
+ log: CrawleeLogger;
89
111
  }
90
112
  export interface CrawlingContext<UserData extends Dictionary = Dictionary> extends RestrictedCrawlingContext<UserData> {
91
113
  /**
92
- * This function automatically finds and enqueues links from the current page, adding them to the {@link RequestQueue}
93
- * currently used by the crawler.
94
- *
95
- * Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
96
- * and override settings of the enqueued {@link Request} objects.
97
- *
98
- * Check out the [Crawl a website with relative links](https://crawlee.dev/js/docs/examples/crawl-relative-links) example
99
- * for more details regarding its usage.
100
- *
101
- * **Example usage**
102
- *
103
- * ```ts
104
- * async requestHandler({ enqueueLinks }) {
105
- * await enqueueLinks({
106
- * globs: [
107
- * 'https://www.example.com/handbags/*',
108
- * ],
109
- * });
110
- * },
111
- * ```
112
- *
113
- * @param [options] All `enqueueLinks()` parameters are passed via an options object.
114
- * @returns Promise that resolves to {@link BatchAddRequestsResult} object.
115
- */
116
- enqueueLinks(options: ReadonlyDeep<Omit<SetRequired<EnqueueLinksOptions, 'urls'>, 'requestQueue' | 'robotsTxtFile'>> & Pick<EnqueueLinksOptions, 'requestQueue' | 'robotsTxtFile'>): Promise<unknown>;
117
- /**
118
- * Fires HTTP request via [`got-scraping`](https://crawlee.dev/js/docs/guides/got-scraping), allowing to override the request
119
- * options on the fly.
114
+ * Fires HTTP request via the internal HTTP client, allowing to override the request options on the fly.
120
115
  *
121
116
  * This is handy when you work with a browser crawler but want to execute some requests outside it (e.g. API requests).
122
117
  * Check the [Skipping navigations for certain requests](https://crawlee.dev/js/docs/examples/skip-navigation) example for
@@ -131,68 +126,34 @@ export interface CrawlingContext<UserData extends Dictionary = Dictionary> exten
131
126
  * },
132
127
  * ```
133
128
  */
134
- sendRequest(overrideOptions?: Partial<OptionsInit>): Promise<Response>;
129
+ sendRequest: (requestOverrides?: Partial<HttpRequestOptions>, optionsOverrides?: SendRequestOptions) => Promise<Response>;
135
130
  /**
136
131
  * Register a function to be called at the very end of the request handling process. This is useful for resources that should be accessible to error handlers, for instance.
132
+ *
133
+ * The callback runs *outside* the request's storage transaction, so storage writes made here are
134
+ * applied immediately and are **not** rolled back when the request fails. In
135
+ * {@link AdaptivePlaywrightCrawler} it also runs once per request handler attempt, so a write
136
+ * here can land more than once for a single request. Push results from the request handler itself.
137
137
  */
138
138
  registerDeferredCleanup(cleanup: () => Promise<unknown>): void;
139
- }
140
- /**
141
- * A partial implementation of {@link RestrictedCrawlingContext} that stores parameters of calls to context methods for later inspection.
142
- *
143
- * @experimental
144
- */
145
- export declare class RequestHandlerResult {
146
- private config;
147
- private crawleeStateKey;
148
- private _keyValueStoreChanges;
149
- private pushDataCalls;
150
- private addRequestsCalls;
151
- private enqueueLinksCalls;
152
- constructor(config: Configuration, crawleeStateKey: string);
153
139
  /**
154
- * A record of calls to {@link RestrictedCrawlingContext.pushData}, {@link RestrictedCrawlingContext.addRequests}, {@link RestrictedCrawlingContext.enqueueLinks} made by a request handler.
155
- */
156
- get calls(): ReadonlyDeep<{
157
- pushData: Parameters<RestrictedCrawlingContext['pushData']>[];
158
- addRequests: Parameters<RestrictedCrawlingContext['addRequests']>[];
159
- enqueueLinks: Parameters<RestrictedCrawlingContext['enqueueLinks']>[];
160
- }>;
161
- /**
162
- * A record of changes made to key-value stores by a request handler.
163
- */
164
- get keyValueStoreChanges(): ReadonlyDeep<Record<string, Record<string, {
165
- changedValue: unknown;
166
- options?: RecordOptions;
167
- }>>>;
168
- /**
169
- * Items added to datasets by a request handler.
170
- */
171
- get datasetItems(): ReadonlyDeep<{
172
- item: Dictionary;
173
- datasetIdOrName?: string;
174
- }[]>;
175
- /**
176
- * URLs enqueued to the request queue by a request handler, either via {@link RestrictedCrawlingContext.addRequests} or {@link RestrictedCrawlingContext.enqueueLinks}
177
- */
178
- get enqueuedUrls(): ReadonlyDeep<{
179
- url: string;
180
- label?: string;
181
- }[]>;
182
- /**
183
- * URL lists enqueued to the request queue by a request handler via {@link RestrictedCrawlingContext.addRequests} using the `requestsFromUrl` option.
140
+ * Gives the current request `secs` more seconds to finish, for when how long it needs is only apparent
141
+ * once it is already running - a listing page that turns out to have far more to scroll through than
142
+ * usual, say. Prefer `requestHandlerTimeoutSecs`, or a per-route override via
143
+ * {@link Router.addHandler|`router.addHandler`}, whenever the time needed is known up front.
144
+ *
145
+ * ```ts
146
+ * router.addHandler('LIST', async ({ extendTimeout, page }) => {
147
+ * const pageCount = await countPages(page);
148
+ * extendTimeout(pageCount * 10);
149
+ * await scrapeAllPages(page);
150
+ * });
151
+ * ```
152
+ *
153
+ * Extends the request handler's own timeout and the crawler's internal one together, so the extension
154
+ * is not immediately undone by the latter. Calling it from a handler that has already timed out does
155
+ * nothing.
184
156
  */
185
- get enqueuedUrlLists(): ReadonlyDeep<{
186
- listUrl: string;
187
- label?: string;
188
- }[]>;
189
- pushData: RestrictedCrawlingContext['pushData'];
190
- enqueueLinks: RestrictedCrawlingContext['enqueueLinks'];
191
- addRequests: RestrictedCrawlingContext['addRequests'];
192
- useState: RestrictedCrawlingContext['useState'];
193
- getKeyValueStore: RestrictedCrawlingContext['getKeyValueStore'];
194
- private idOrDefault;
195
- private getKeyValueStoreChangedValue;
196
- private setKeyValueStoreChangedValue;
157
+ extendTimeout(secs: number): void;
197
158
  }
198
- //# sourceMappingURL=crawler_commons.d.ts.map
159
+ export {};
@@ -1,116 +1 @@
1
- import { KeyValueStore } from '../storages/key_value_store.js';
2
- /**
3
- * A partial implementation of {@link RestrictedCrawlingContext} that stores parameters of calls to context methods for later inspection.
4
- *
5
- * @experimental
6
- */
7
- export class RequestHandlerResult {
8
- config;
9
- crawleeStateKey;
10
- _keyValueStoreChanges = {};
11
- pushDataCalls = [];
12
- addRequestsCalls = [];
13
- enqueueLinksCalls = [];
14
- constructor(config, crawleeStateKey) {
15
- this.config = config;
16
- this.crawleeStateKey = crawleeStateKey;
17
- }
18
- /**
19
- * A record of calls to {@link RestrictedCrawlingContext.pushData}, {@link RestrictedCrawlingContext.addRequests}, {@link RestrictedCrawlingContext.enqueueLinks} made by a request handler.
20
- */
21
- get calls() {
22
- return {
23
- pushData: this.pushDataCalls,
24
- addRequests: this.addRequestsCalls,
25
- enqueueLinks: this.enqueueLinksCalls,
26
- };
27
- }
28
- /**
29
- * A record of changes made to key-value stores by a request handler.
30
- */
31
- get keyValueStoreChanges() {
32
- return this._keyValueStoreChanges;
33
- }
34
- /**
35
- * Items added to datasets by a request handler.
36
- */
37
- get datasetItems() {
38
- return this.pushDataCalls.flatMap(([data, datasetIdOrName]) => (Array.isArray(data) ? data : [data]).map((item) => ({ item, datasetIdOrName })));
39
- }
40
- /**
41
- * URLs enqueued to the request queue by a request handler, either via {@link RestrictedCrawlingContext.addRequests} or {@link RestrictedCrawlingContext.enqueueLinks}
42
- */
43
- get enqueuedUrls() {
44
- const result = [];
45
- for (const [options] of this.enqueueLinksCalls) {
46
- result.push(...(options?.urls?.map((url) => ({ url, label: options?.label })) ?? []));
47
- }
48
- for (const [requests] of this.addRequestsCalls) {
49
- for (const request of requests) {
50
- if (typeof request === 'object' &&
51
- (!('requestsFromUrl' in request) || request.requestsFromUrl !== undefined) &&
52
- request.url !== undefined) {
53
- result.push({ url: request.url, label: request.label });
54
- }
55
- else if (typeof request === 'string') {
56
- result.push({ url: request });
57
- }
58
- }
59
- }
60
- return result;
61
- }
62
- /**
63
- * URL lists enqueued to the request queue by a request handler via {@link RestrictedCrawlingContext.addRequests} using the `requestsFromUrl` option.
64
- */
65
- get enqueuedUrlLists() {
66
- const result = [];
67
- for (const [requests] of this.addRequestsCalls) {
68
- for (const request of requests) {
69
- if (typeof request === 'object' &&
70
- 'requestsFromUrl' in request &&
71
- request.requestsFromUrl !== undefined) {
72
- result.push({ listUrl: request.requestsFromUrl, label: request.label });
73
- }
74
- }
75
- }
76
- return result;
77
- }
78
- pushData = async (data, datasetIdOrName) => {
79
- this.pushDataCalls.push([data, datasetIdOrName]);
80
- };
81
- enqueueLinks = async (options) => {
82
- this.enqueueLinksCalls.push([options]);
83
- };
84
- addRequests = async (requests, options = {}) => {
85
- this.addRequestsCalls.push([requests, options]);
86
- };
87
- useState = async (defaultValue) => {
88
- const store = await this.getKeyValueStore(undefined);
89
- return await store.getAutoSavedValue(this.crawleeStateKey, defaultValue);
90
- };
91
- getKeyValueStore = async (idOrName) => {
92
- const store = await KeyValueStore.open(idOrName, { config: this.config });
93
- return {
94
- id: this.idOrDefault(idOrName),
95
- name: idOrName,
96
- getValue: async (key) => this.getKeyValueStoreChangedValue(idOrName, key) ?? (await store.getValue(key)),
97
- setValue: async (key, value, options) => {
98
- this.setKeyValueStoreChangedValue(idOrName, key, value, options);
99
- },
100
- getAutoSavedValue: store.getAutoSavedValue.bind(store),
101
- getPublicUrl: store.getPublicUrl.bind(store),
102
- };
103
- };
104
- idOrDefault = (idOrName) => idOrName ?? this.config.get('defaultKeyValueStoreId');
105
- getKeyValueStoreChangedValue = (idOrName, key) => {
106
- const id = this.idOrDefault(idOrName);
107
- this._keyValueStoreChanges[id] ??= {};
108
- return this.keyValueStoreChanges[id][key]?.changedValue ?? null;
109
- };
110
- setKeyValueStoreChangedValue = (idOrName, key, changedValue, options) => {
111
- const id = this.idOrDefault(idOrName);
112
- this._keyValueStoreChanges[id] ??= {};
113
- this._keyValueStoreChanges[id][key] = { changedValue, options };
114
- };
115
- }
116
- //# sourceMappingURL=crawler_commons.js.map
1
+ export {};
@@ -25,9 +25,7 @@ interface ErrorSnapshot {
25
25
  * ```ts
26
26
  * const crawler = new BasicCrawler({
27
27
  * // ...
28
- * statisticsOptions: {
29
- * saveErrorSnapshots: true,
30
- * },
28
+ * statistics: new Statistics({ saveErrorSnapshots: true }),
31
29
  * });
32
30
  * ```
33
31
  */
@@ -48,7 +46,7 @@ export declare class ErrorSnapshotter {
48
46
  */
49
47
  contextCaptureSnapshot(context: BrowserCrawlingContext, fileName: string): Promise<SnapshotResult | undefined>;
50
48
  /**
51
- * Save the HTML snapshot of the page, and return the fileName with the extension.
49
+ * Save the HTML snapshot of the page, and return the key it was stored under.
52
50
  */
53
51
  saveHTMLSnapshot(html: string, keyValueStore: Pick<KeyValueStore, 'setValue'>, fileName: string): Promise<string | undefined>;
54
52
  /**
@@ -57,4 +55,3 @@ export declare class ErrorSnapshotter {
57
55
  generateFilename(error: ErrnoException): string;
58
56
  }
59
57
  export {};
60
- //# sourceMappingURL=error_snapshotter.d.ts.map
@@ -7,9 +7,7 @@ import crypto from 'node:crypto';
7
7
  * ```ts
8
8
  * const crawler = new BasicCrawler({
9
9
  * // ...
10
- * statisticsOptions: {
11
- * saveErrorSnapshots: true,
12
- * },
10
+ * statistics: new Statistics({ saveErrorSnapshots: true }),
13
11
  * });
14
12
  * ```
15
13
  */
@@ -52,9 +50,9 @@ export class ErrorSnapshotter {
52
50
  }
53
51
  return {
54
52
  screenshotFileName,
55
- screenshotFileUrl: screenshotFileName && keyValueStore.getPublicUrl(screenshotFileName),
53
+ screenshotFileUrl: screenshotFileName && (await keyValueStore.getPublicUrl(screenshotFileName)),
56
54
  htmlFileName,
57
- htmlFileUrl: htmlFileName && keyValueStore.getPublicUrl(htmlFileName),
55
+ htmlFileUrl: htmlFileName && (await keyValueStore.getPublicUrl(htmlFileName)),
58
56
  };
59
57
  }
60
58
  catch {
@@ -79,12 +77,14 @@ export class ErrorSnapshotter {
79
77
  }
80
78
  }
81
79
  /**
82
- * Save the HTML snapshot of the page, and return the fileName with the extension.
80
+ * Save the HTML snapshot of the page, and return the key it was stored under.
83
81
  */
84
82
  async saveHTMLSnapshot(html, keyValueStore, fileName) {
85
83
  try {
86
84
  await keyValueStore.setValue(fileName, html, { contentType: 'text/html' });
87
- return `${fileName}.html`;
85
+ // The record key is `fileName` - returning it with an `.html` suffix (as v3 did,
86
+ // where local storage put the extension in the key) would break `getPublicUrl`.
87
+ return fileName;
88
88
  }
89
89
  catch {
90
90
  return undefined;
@@ -115,4 +115,3 @@ export class ErrorSnapshotter {
115
115
  return fileName;
116
116
  }
117
117
  }
118
- //# sourceMappingURL=error_snapshotter.js.map
@@ -52,4 +52,3 @@ export declare class ErrorTracker {
52
52
  captureSnapshot(storage: Record<string, unknown>, error: ErrnoException, context: CrawlingContext & SnapshottableProperties): Promise<void>;
53
53
  reset(): void;
54
54
  }
55
- //# sourceMappingURL=error_tracker.d.ts.map
@@ -306,4 +306,3 @@ export class ErrorTracker {
306
306
  }
307
307
  }
308
308
  }
309
- //# sourceMappingURL=error_tracker.js.map
@@ -1,7 +1,5 @@
1
1
  export * from './context_pipeline.js';
2
- export * from './crawler_commons.js';
3
- export * from './crawler_utils.js';
2
+ export type * from './crawler_commons.js';
4
3
  export * from './statistics.js';
5
4
  export * from './error_tracker.js';
6
5
  export * from './error_snapshotter.js';
7
- //# sourceMappingURL=index.d.ts.map
package/crawlers/index.js CHANGED
@@ -1,7 +1,4 @@
1
1
  export * from './context_pipeline.js';
2
- export * from './crawler_commons.js';
3
- export * from './crawler_utils.js';
4
2
  export * from './statistics.js';
5
3
  export * from './error_tracker.js';
6
4
  export * from './error_snapshotter.js';
7
- //# sourceMappingURL=index.js.map
@@ -5,4 +5,3 @@ export interface SnapshottableProperties {
5
5
  body?: unknown;
6
6
  page?: BrowserPage;
7
7
  }
8
- //# sourceMappingURL=types.d.ts.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=types.js.map