@crawlee/core 4.0.0-beta.28 → 4.0.0-beta.280

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 (266) hide show
  1. package/README.md +15 -15
  2. package/byte-utils.d.ts +17 -0
  3. package/byte-utils.js +42 -0
  4. package/configuration.d.ts +75 -167
  5. package/configuration.js +164 -140
  6. package/debug.d.ts +36 -0
  7. package/debug.js +70 -0
  8. package/errors.d.ts +40 -19
  9. package/errors.js +52 -30
  10. package/events/{event_manager.d.ts → event-manager.d.ts} +29 -6
  11. package/events/{event_manager.js → event-manager.js} +10 -9
  12. package/events/index.d.ts +3 -3
  13. package/events/index.js +2 -3
  14. package/events/{local_event_manager.d.ts → local-event-manager.d.ts} +3 -10
  15. package/events/local-event-manager.js +101 -0
  16. package/events/system-info.d.ts +38 -0
  17. package/events/system-info.js +1 -0
  18. package/index.d.ts +9 -13
  19. package/index.js +10 -12
  20. package/internal.d.ts +8 -0
  21. package/internal.js +9 -0
  22. package/iterables.d.ts +79 -0
  23. package/iterables.js +134 -0
  24. package/log.d.ts +77 -1
  25. package/log.js +134 -1
  26. package/memory-storage/consts.d.ts +4 -0
  27. package/memory-storage/consts.js +4 -0
  28. package/memory-storage/index.d.ts +1 -0
  29. package/memory-storage/index.js +1 -0
  30. package/memory-storage/memory-storage.d.ts +43 -0
  31. package/memory-storage/memory-storage.js +158 -0
  32. package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
  33. package/memory-storage/resource-clients/common/base-client.js +6 -0
  34. package/memory-storage/resource-clients/dataset.d.ts +35 -0
  35. package/memory-storage/resource-clients/dataset.js +108 -0
  36. package/memory-storage/resource-clients/key-value-store.d.ts +54 -0
  37. package/memory-storage/resource-clients/key-value-store.js +179 -0
  38. package/memory-storage/resource-clients/request-queue.d.ts +55 -0
  39. package/memory-storage/resource-clients/request-queue.js +413 -0
  40. package/memory-storage/utils.d.ts +16 -0
  41. package/memory-storage/utils.js +41 -0
  42. package/owned-or-injected.d.ts +58 -0
  43. package/owned-or-injected.js +98 -0
  44. package/package.json +23 -23
  45. package/proxy-configuration.d.ts +118 -0
  46. package/proxy-configuration.js +141 -0
  47. package/recoverable-state.d.ts +177 -0
  48. package/recoverable-state.js +268 -0
  49. package/request.d.ts +48 -101
  50. package/request.js +139 -249
  51. package/serialization.d.ts +0 -1
  52. package/serialization.js +18 -18
  53. package/{service_locator.d.ts → service-locator.d.ts} +66 -37
  54. package/service-locator.js +255 -0
  55. package/storages/batched-adds.d.ts +39 -0
  56. package/storages/batched-adds.js +81 -0
  57. package/storages/dataset.d.ts +105 -58
  58. package/storages/dataset.js +292 -151
  59. package/storages/index.d.ts +11 -12
  60. package/storages/index.js +7 -12
  61. package/storages/key-value-store-codec.d.ts +32 -0
  62. package/storages/key-value-store-codec.js +108 -0
  63. package/storages/{key_value_store.d.ts → key-value-store.d.ts} +169 -72
  64. package/storages/key-value-store.js +767 -0
  65. package/storages/request-dedup-cache.d.ts +20 -0
  66. package/storages/request-dedup-cache.js +48 -0
  67. package/storages/{request_list.d.ts → request-list.d.ts} +32 -171
  68. package/storages/{request_list.js → request-list.js} +276 -253
  69. package/storages/request-loader.d.ts +127 -0
  70. package/storages/request-loader.js +34 -0
  71. package/storages/request-manager-tandem.d.ts +82 -0
  72. package/storages/request-manager-tandem.js +200 -0
  73. package/storages/request-manager.d.ts +119 -0
  74. package/storages/request-manager.js +1 -0
  75. package/storages/{request_provider.d.ts → request-queue.d.ts} +127 -161
  76. package/storages/request-queue.js +871 -0
  77. package/storages/storage-instance-manager.d.ts +86 -0
  78. package/storages/storage-instance-manager.js +256 -0
  79. package/storages/storage-stats.d.ts +48 -0
  80. package/storages/storage-stats.js +29 -0
  81. package/storages/transaction.d.ts +270 -0
  82. package/storages/transaction.js +296 -0
  83. package/storages/utils.d.ts +59 -11
  84. package/storages/utils.js +76 -17
  85. package/system-info/cpu-info.d.ts +67 -0
  86. package/system-info/cpu-info.js +216 -0
  87. package/system-info/memory-info.d.ts +31 -0
  88. package/system-info/memory-info.js +115 -0
  89. package/system-info/ps-tree.d.ts +17 -0
  90. package/system-info/ps-tree.js +144 -0
  91. package/system-info/runtime.d.ts +14 -0
  92. package/system-info/runtime.js +80 -0
  93. package/typedefs.d.ts +0 -6
  94. package/typedefs.js +0 -1
  95. package/url.d.ts +9 -0
  96. package/url.js +11 -0
  97. package/validators.d.ts +23 -18
  98. package/validators.js +14 -18
  99. package/autoscaling/autoscaled_pool.d.ts +0 -298
  100. package/autoscaling/autoscaled_pool.d.ts.map +0 -1
  101. package/autoscaling/autoscaled_pool.js +0 -527
  102. package/autoscaling/autoscaled_pool.js.map +0 -1
  103. package/autoscaling/index.d.ts +0 -4
  104. package/autoscaling/index.d.ts.map +0 -1
  105. package/autoscaling/index.js +0 -4
  106. package/autoscaling/index.js.map +0 -1
  107. package/autoscaling/snapshotter.d.ts +0 -179
  108. package/autoscaling/snapshotter.d.ts.map +0 -1
  109. package/autoscaling/snapshotter.js +0 -279
  110. package/autoscaling/snapshotter.js.map +0 -1
  111. package/autoscaling/system_status.d.ts +0 -178
  112. package/autoscaling/system_status.d.ts.map +0 -1
  113. package/autoscaling/system_status.js +0 -168
  114. package/autoscaling/system_status.js.map +0 -1
  115. package/configuration.d.ts.map +0 -1
  116. package/configuration.js.map +0 -1
  117. package/cookie_utils.d.ts +0 -43
  118. package/cookie_utils.d.ts.map +0 -1
  119. package/cookie_utils.js +0 -111
  120. package/cookie_utils.js.map +0 -1
  121. package/crawlers/context_pipeline.d.ts +0 -61
  122. package/crawlers/context_pipeline.d.ts.map +0 -1
  123. package/crawlers/context_pipeline.js +0 -99
  124. package/crawlers/context_pipeline.js.map +0 -1
  125. package/crawlers/crawler_commons.d.ts +0 -191
  126. package/crawlers/crawler_commons.d.ts.map +0 -1
  127. package/crawlers/crawler_commons.js +0 -108
  128. package/crawlers/crawler_commons.js.map +0 -1
  129. package/crawlers/crawler_utils.d.ts +0 -10
  130. package/crawlers/crawler_utils.d.ts.map +0 -1
  131. package/crawlers/crawler_utils.js +0 -12
  132. package/crawlers/crawler_utils.js.map +0 -1
  133. package/crawlers/error_snapshotter.d.ts +0 -60
  134. package/crawlers/error_snapshotter.d.ts.map +0 -1
  135. package/crawlers/error_snapshotter.js +0 -118
  136. package/crawlers/error_snapshotter.js.map +0 -1
  137. package/crawlers/error_tracker.d.ts +0 -55
  138. package/crawlers/error_tracker.d.ts.map +0 -1
  139. package/crawlers/error_tracker.js +0 -309
  140. package/crawlers/error_tracker.js.map +0 -1
  141. package/crawlers/index.d.ts +0 -7
  142. package/crawlers/index.d.ts.map +0 -1
  143. package/crawlers/index.js +0 -7
  144. package/crawlers/index.js.map +0 -1
  145. package/crawlers/internals/types.d.ts +0 -8
  146. package/crawlers/internals/types.d.ts.map +0 -1
  147. package/crawlers/internals/types.js +0 -2
  148. package/crawlers/internals/types.js.map +0 -1
  149. package/crawlers/statistics.d.ts +0 -204
  150. package/crawlers/statistics.d.ts.map +0 -1
  151. package/crawlers/statistics.js +0 -350
  152. package/crawlers/statistics.js.map +0 -1
  153. package/enqueue_links/enqueue_links.d.ts +0 -254
  154. package/enqueue_links/enqueue_links.d.ts.map +0 -1
  155. package/enqueue_links/enqueue_links.js +0 -268
  156. package/enqueue_links/enqueue_links.js.map +0 -1
  157. package/enqueue_links/index.d.ts +0 -3
  158. package/enqueue_links/index.d.ts.map +0 -1
  159. package/enqueue_links/index.js +0 -3
  160. package/enqueue_links/index.js.map +0 -1
  161. package/enqueue_links/shared.d.ts +0 -84
  162. package/enqueue_links/shared.d.ts.map +0 -1
  163. package/enqueue_links/shared.js +0 -219
  164. package/enqueue_links/shared.js.map +0 -1
  165. package/errors.d.ts.map +0 -1
  166. package/errors.js.map +0 -1
  167. package/events/event_manager.d.ts.map +0 -1
  168. package/events/event_manager.js.map +0 -1
  169. package/events/index.d.ts.map +0 -1
  170. package/events/index.js.map +0 -1
  171. package/events/local_event_manager.d.ts.map +0 -1
  172. package/events/local_event_manager.js +0 -93
  173. package/events/local_event_manager.js.map +0 -1
  174. package/index.d.ts.map +0 -1
  175. package/index.js.map +0 -1
  176. package/log.d.ts.map +0 -1
  177. package/log.js.map +0 -1
  178. package/proxy_configuration.d.ts +0 -169
  179. package/proxy_configuration.d.ts.map +0 -1
  180. package/proxy_configuration.js +0 -247
  181. package/proxy_configuration.js.map +0 -1
  182. package/recoverable_state.d.ts +0 -122
  183. package/recoverable_state.d.ts.map +0 -1
  184. package/recoverable_state.js +0 -138
  185. package/recoverable_state.js.map +0 -1
  186. package/request.d.ts.map +0 -1
  187. package/request.js.map +0 -1
  188. package/router.d.ts +0 -134
  189. package/router.d.ts.map +0 -1
  190. package/router.js +0 -164
  191. package/router.js.map +0 -1
  192. package/serialization.d.ts.map +0 -1
  193. package/serialization.js.map +0 -1
  194. package/service_locator.d.ts.map +0 -1
  195. package/service_locator.js +0 -252
  196. package/service_locator.js.map +0 -1
  197. package/session_pool/consts.d.ts +0 -4
  198. package/session_pool/consts.d.ts.map +0 -1
  199. package/session_pool/consts.js +0 -4
  200. package/session_pool/consts.js.map +0 -1
  201. package/session_pool/errors.d.ts +0 -8
  202. package/session_pool/errors.d.ts.map +0 -1
  203. package/session_pool/errors.js +0 -12
  204. package/session_pool/errors.js.map +0 -1
  205. package/session_pool/events.d.ts +0 -3
  206. package/session_pool/events.d.ts.map +0 -1
  207. package/session_pool/events.js +0 -3
  208. package/session_pool/events.js.map +0 -1
  209. package/session_pool/index.d.ts +0 -6
  210. package/session_pool/index.d.ts.map +0 -1
  211. package/session_pool/index.js +0 -6
  212. package/session_pool/index.js.map +0 -1
  213. package/session_pool/session.d.ts +0 -186
  214. package/session_pool/session.d.ts.map +0 -1
  215. package/session_pool/session.js +0 -278
  216. package/session_pool/session.js.map +0 -1
  217. package/session_pool/session_pool.d.ts +0 -256
  218. package/session_pool/session_pool.d.ts.map +0 -1
  219. package/session_pool/session_pool.js +0 -386
  220. package/session_pool/session_pool.js.map +0 -1
  221. package/storages/access_checking.d.ts +0 -13
  222. package/storages/access_checking.d.ts.map +0 -1
  223. package/storages/access_checking.js +0 -14
  224. package/storages/access_checking.js.map +0 -1
  225. package/storages/dataset.d.ts.map +0 -1
  226. package/storages/dataset.js.map +0 -1
  227. package/storages/index.d.ts.map +0 -1
  228. package/storages/index.js.map +0 -1
  229. package/storages/key_value_store.d.ts.map +0 -1
  230. package/storages/key_value_store.js +0 -513
  231. package/storages/key_value_store.js.map +0 -1
  232. package/storages/request_list.d.ts.map +0 -1
  233. package/storages/request_list.js.map +0 -1
  234. package/storages/request_list_adapter.d.ts +0 -58
  235. package/storages/request_list_adapter.d.ts.map +0 -1
  236. package/storages/request_list_adapter.js +0 -81
  237. package/storages/request_list_adapter.js.map +0 -1
  238. package/storages/request_manager_tandem.d.ts +0 -68
  239. package/storages/request_manager_tandem.d.ts.map +0 -1
  240. package/storages/request_manager_tandem.js +0 -124
  241. package/storages/request_manager_tandem.js.map +0 -1
  242. package/storages/request_provider.d.ts.map +0 -1
  243. package/storages/request_provider.js +0 -592
  244. package/storages/request_provider.js.map +0 -1
  245. package/storages/request_queue.d.ts +0 -128
  246. package/storages/request_queue.d.ts.map +0 -1
  247. package/storages/request_queue.js +0 -320
  248. package/storages/request_queue.js.map +0 -1
  249. package/storages/request_queue_v2.d.ts +0 -87
  250. package/storages/request_queue_v2.d.ts.map +0 -1
  251. package/storages/request_queue_v2.js +0 -437
  252. package/storages/request_queue_v2.js.map +0 -1
  253. package/storages/sitemap_request_list.d.ts +0 -245
  254. package/storages/sitemap_request_list.d.ts.map +0 -1
  255. package/storages/sitemap_request_list.js +0 -431
  256. package/storages/sitemap_request_list.js.map +0 -1
  257. package/storages/storage_manager.d.ts +0 -60
  258. package/storages/storage_manager.d.ts.map +0 -1
  259. package/storages/storage_manager.js +0 -95
  260. package/storages/storage_manager.js.map +0 -1
  261. package/storages/utils.d.ts.map +0 -1
  262. package/storages/utils.js.map +0 -1
  263. package/typedefs.d.ts.map +0 -1
  264. package/typedefs.js.map +0 -1
  265. package/validators.d.ts.map +0 -1
  266. package/validators.js.map +0 -1
package/configuration.js CHANGED
@@ -1,13 +1,73 @@
1
- import { EventEmitter } from 'node:events';
2
1
  import { readFileSync } from 'node:fs';
3
2
  import { join } from 'node:path';
4
- import { pathExistsSync } from 'fs-extra/esm';
5
- import log, { LogLevel } from '@apify/log';
6
- import { serviceLocator } from './service_locator.js';
7
- import { entries } from './typedefs.js';
3
+ import JSON5 from 'json5';
4
+ import { z } from 'zod';
5
+ import { log, LogLevel } from './log.js';
6
+ import { serviceLocator } from './service-locator.js';
7
+ export function field(schema, envVar) {
8
+ return { schema, envVar };
9
+ }
10
+ // --- Zod preprocessors ---
11
+ /** Zod preprocessor treating `'0'` and `'false'` as falsy. */
12
+ export const coerceBoolean = z.preprocess((val) => {
13
+ if (typeof val === 'string') {
14
+ return !['0', 'false'].includes(val.toLowerCase());
15
+ }
16
+ return val;
17
+ }, z.boolean());
18
+ export const coerceNumber = z.preprocess((val) => {
19
+ if (typeof val === 'string')
20
+ return Number(val);
21
+ return val;
22
+ }, z.number());
23
+ /** Zod schema accepting both LogLevel enum values and string names (case-insensitive). */
24
+ const logLevelSchema = z.preprocess((val) => {
25
+ if (val == null)
26
+ return val;
27
+ const s = String(val);
28
+ if (Number.isFinite(+s))
29
+ return +s;
30
+ const key = s.toUpperCase();
31
+ if (key in LogLevel)
32
+ return LogLevel[key];
33
+ return val;
34
+ }, z.enum(LogLevel));
35
+ // --- Crawlee config field definitions ---
36
+ export const crawleeConfigFields = {
37
+ /** @default true */
38
+ purgeOnStart: field(coerceBoolean.default(true), 'CRAWLEE_PURGE_ON_START'),
39
+ /** @default 0.95 */
40
+ maxUsedCpuRatio: field(coerceNumber.default(0.95)),
41
+ /** @default 0.25 */
42
+ availableMemoryRatio: field(coerceNumber.default(0.25), 'CRAWLEE_AVAILABLE_MEMORY_RATIO'),
43
+ memoryMbytes: field(coerceNumber.optional(), 'CRAWLEE_MEMORY_MBYTES'),
44
+ /** @default 60_000 */
45
+ persistStateIntervalMillis: field(coerceNumber.default(60_000), 'CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS'),
46
+ /**
47
+ * Internal safety-net timeout for a single request, in milliseconds. When unset the crawler derives it from
48
+ * the request handler timeout (twice it, and never below 5 minutes).
49
+ */
50
+ internalTimeoutMillis: field(coerceNumber.optional(), 'CRAWLEE_INTERNAL_TIMEOUT'),
51
+ /** @default 1_000 */
52
+ systemInfoIntervalMillis: field(coerceNumber.default(1_000)),
53
+ /** @default true */
54
+ headless: field(coerceBoolean.default(true), 'CRAWLEE_HEADLESS'),
55
+ /** @default false */
56
+ xvfb: field(coerceBoolean.default(false), 'CRAWLEE_XVFB'),
57
+ chromeExecutablePath: field(z.string().optional(), 'CRAWLEE_CHROME_EXECUTABLE_PATH'),
58
+ defaultBrowserPath: field(z.string().optional(), 'CRAWLEE_DEFAULT_BROWSER_PATH'),
59
+ /** @default false */
60
+ disableBrowserSandbox: field(coerceBoolean.default(false), 'CRAWLEE_DISABLE_BROWSER_SANDBOX'),
61
+ logLevel: field(logLevelSchema.optional(), 'CRAWLEE_LOG_LEVEL'),
62
+ /** @default true */
63
+ persistStorage: field(coerceBoolean.default(true), 'CRAWLEE_PERSIST_STORAGE'),
64
+ /** @default './storage' */
65
+ storageDir: field(z.string().default('./storage'), 'CRAWLEE_STORAGE_DIR'),
66
+ containerized: field(coerceBoolean.optional(), 'CRAWLEE_CONTAINERIZED'),
67
+ };
8
68
  /**
9
69
  * `Configuration` is a value object holding Crawlee configuration. By default, there is a
10
- * global singleton instance of this class available via `Configuration.getGlobalConfig()`.
70
+ * global singleton instance of this class available via `Configuration.getGlobalConfiguration()`.
11
71
  * Places that depend on a configurable behaviour depend on this class, as they have the global
12
72
  * instance as the default value.
13
73
  *
@@ -16,14 +76,10 @@ import { entries } from './typedefs.js';
16
76
  * import { BasicCrawler, Configuration } from 'crawlee';
17
77
  *
18
78
  * // Get the global configuration
19
- * const config = Configuration.getGlobalConfig();
20
- * // Set the 'persistStateIntervalMillis' option
21
- * // of global configuration to 10 seconds
22
- * config.set('persistStateIntervalMillis', 10_000);
23
- *
24
- * // No need to pass the configuration to the crawler,
25
- * // as it's using the global configuration by default
26
- * const crawler = new BasicCrawler();
79
+ * const config = Configuration.getGlobalConfiguration();
80
+ * // Access configuration values directly as properties
81
+ * console.log(config.headless);
82
+ * console.log(config.persistStateIntervalMillis);
27
83
  * ```
28
84
  *
29
85
  * *Using custom configuration:*
@@ -33,15 +89,14 @@ import { entries } from './typedefs.js';
33
89
  * // Create a new configuration
34
90
  * const config = new Configuration({ persistStateIntervalMillis: 30_000 });
35
91
  * // Pass the configuration to the crawler
36
- * const crawler = new BasicCrawler({ configuration: config, ... });
92
+ * const crawler = new BasicCrawler({ configuration: config });
37
93
  * ```
38
94
  *
39
- * The configuration provided via environment variables always takes precedence. We can also
40
- * define the `crawlee.json` file in the project root directory which will serve as a baseline,
41
- * so the options provided in constructor will override those. In other words, the precedence is:
95
+ * Configuration is immutable — values are set via the constructor and cannot be changed afterwards.
96
+ * The priority order for resolving values is (highest to lowest):
42
97
  *
43
98
  * ```text
44
- * crawlee.json < constructor options < environment variables
99
+ * constructor options > environment variables > crawlee.json > schema defaults
45
100
  * ```
46
101
  *
47
102
  * ## Supported Configuration Options
@@ -51,160 +106,129 @@ import { entries } from './typedefs.js';
51
106
  * `memoryMbytes` | `CRAWLEE_MEMORY_MBYTES` | -
52
107
  * `logLevel` | `CRAWLEE_LOG_LEVEL` | -
53
108
  * `headless` | `CRAWLEE_HEADLESS` | `true`
54
- * `defaultDatasetId` | `CRAWLEE_DEFAULT_DATASET_ID` | `'default'`
55
- * `defaultKeyValueStoreId` | `CRAWLEE_DEFAULT_KEY_VALUE_STORE_ID` | `'default'`
56
- * `defaultRequestQueueId` | `CRAWLEE_DEFAULT_REQUEST_QUEUE_ID` | `'default'`
57
109
  * `persistStateIntervalMillis` | `CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS` | `60_000`
110
+ * `internalTimeoutMillis` | `CRAWLEE_INTERNAL_TIMEOUT` | -
58
111
  * `purgeOnStart` | `CRAWLEE_PURGE_ON_START` | `true`
59
112
  * `persistStorage` | `CRAWLEE_PERSIST_STORAGE` | `true`
113
+ * `storageDir` | `CRAWLEE_STORAGE_DIR` | `'./storage'`
60
114
  *
61
115
  * ## Advanced Configuration Options
62
116
  *
63
117
  * Key | Environment Variable | Default Value
64
118
  * ---|---|---
65
- * `inputKey` | `CRAWLEE_INPUT_KEY` | `'INPUT'`
66
- * `xvfb` | `CRAWLEE_XVFB` | -
119
+ * `xvfb` | `CRAWLEE_XVFB` | `false`
67
120
  * `chromeExecutablePath` | `CRAWLEE_CHROME_EXECUTABLE_PATH` | -
68
121
  * `defaultBrowserPath` | `CRAWLEE_DEFAULT_BROWSER_PATH` | -
69
122
  * `disableBrowserSandbox` | `CRAWLEE_DISABLE_BROWSER_SANDBOX` | -
70
123
  * `availableMemoryRatio` | `CRAWLEE_AVAILABLE_MEMORY_RATIO` | `0.25`
71
- * `containerized | `CRAWLEE_CONTAINERIZED | -
124
+ * `containerized` | `CRAWLEE_CONTAINERIZED` | -
72
125
  */
126
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
73
127
  export class Configuration {
74
128
  /**
75
- * Maps environment variables to config keys (e.g. `CRAWLEE_MEMORY_MBYTES` to `memoryMbytes`)
129
+ * Field definitions for this configuration class.
130
+ * Subclasses override this to register additional fields.
76
131
  */
77
- static ENV_MAP = {
78
- CRAWLEE_AVAILABLE_MEMORY_RATIO: 'availableMemoryRatio',
79
- CRAWLEE_PURGE_ON_START: 'purgeOnStart',
80
- CRAWLEE_MEMORY_MBYTES: 'memoryMbytes',
81
- CRAWLEE_DEFAULT_DATASET_ID: 'defaultDatasetId',
82
- CRAWLEE_DEFAULT_KEY_VALUE_STORE_ID: 'defaultKeyValueStoreId',
83
- CRAWLEE_DEFAULT_REQUEST_QUEUE_ID: 'defaultRequestQueueId',
84
- CRAWLEE_INPUT_KEY: 'inputKey',
85
- CRAWLEE_PERSIST_STATE_INTERVAL_MILLIS: 'persistStateIntervalMillis',
86
- CRAWLEE_HEADLESS: 'headless',
87
- CRAWLEE_XVFB: 'xvfb',
88
- CRAWLEE_CHROME_EXECUTABLE_PATH: 'chromeExecutablePath',
89
- CRAWLEE_DEFAULT_BROWSER_PATH: 'defaultBrowserPath',
90
- CRAWLEE_DISABLE_BROWSER_SANDBOX: 'disableBrowserSandbox',
91
- CRAWLEE_LOG_LEVEL: 'logLevel',
92
- CRAWLEE_PERSIST_STORAGE: 'persistStorage',
93
- CRAWLEE_CONTAINERIZED: 'containerized',
94
- };
95
- static BOOLEAN_VARS = [
96
- 'purgeOnStart',
97
- 'headless',
98
- 'xvfb',
99
- 'disableBrowserSandbox',
100
- 'persistStorage',
101
- 'containerized',
102
- ];
103
- static INTEGER_VARS = ['memoryMbytes', 'persistStateIntervalMillis', 'systemInfoIntervalMillis'];
104
- static COMMA_SEPARATED_LIST_VARS = [];
105
- static DEFAULTS = {
106
- defaultKeyValueStoreId: 'default',
107
- defaultDatasetId: 'default',
108
- defaultRequestQueueId: 'default',
109
- inputKey: 'INPUT',
110
- maxUsedCpuRatio: 0.95,
111
- availableMemoryRatio: 0.25,
112
- storageClientOptions: {},
113
- purgeOnStart: true,
114
- headless: true,
115
- persistStateIntervalMillis: 60_000,
116
- systemInfoIntervalMillis: 1_000,
117
- persistStorage: true,
118
- };
119
- options;
132
+ static fields = crawleeConfigFields;
133
+ #resolvedValues;
120
134
  /**
121
- * Creates new `Configuration` instance with provided options. Env vars will have precedence over those.
135
+ * Creates new `Configuration` instance with provided options.
136
+ * Constructor options take precedence over environment variables, which take precedence
137
+ * over crawlee.json values, which take precedence over schema defaults.
122
138
  */
123
139
  constructor(options = {}) {
124
- this.buildOptions(options);
125
- // Increase the global limit for event emitter memory leak warnings.
126
- EventEmitter.defaultMaxListeners = 50;
127
- // set the log level to support CRAWLEE_ prefixed env var too
128
- const logLevel = this.get('logLevel');
129
- if (logLevel) {
130
- const level = Number.isFinite(+logLevel)
131
- ? +logLevel
132
- : LogLevel[String(logLevel).toUpperCase()];
133
- log.setLevel(level);
140
+ const fields = this.constructor.fields;
141
+ const fileOptions = Configuration.#loadFileOptions();
142
+ this.#resolvedValues = Configuration.#resolveAll(fields, options, fileOptions);
143
+ this.#registerAccessors();
144
+ // Set the log level
145
+ const logLevel = this.logLevel;
146
+ if (logLevel != null) {
147
+ log.setLevel(logLevel);
134
148
  }
135
149
  }
136
150
  /**
137
- * Returns configured value. First checks the environment variables, then provided configuration,
138
- * fallbacks to the `defaultValue` argument if provided, otherwise uses the default value as described
139
- * in the above section.
151
+ * Returns the global configuration instance. It will respect the environment variables.
152
+ *
153
+ * Delegates to the global ServiceLocator, making it the single source of truth for service management.
140
154
  */
141
- get(key, defaultValue) {
142
- // prefer env vars, always iterate through the whole map as there might be duplicate env vars for the same option
143
- let envValue;
144
- for (const [k, v] of entries(Configuration.ENV_MAP)) {
145
- if (key === v) {
146
- envValue = process.env[k];
147
- if (envValue) {
148
- break;
149
- }
155
+ static getGlobalConfiguration() {
156
+ return serviceLocator.getConfiguration();
157
+ }
158
+ /**
159
+ * Resolves all field values once using the priority chain:
160
+ * constructor options > env vars > crawlee.json > schema defaults.
161
+ */
162
+ static #resolveAll(fields, userOptions, fileOptions) {
163
+ const values = {};
164
+ for (const [key, fieldDef] of Object.entries(fields)) {
165
+ // 1. Constructor options (highest priority)
166
+ if (key in userOptions && userOptions[key] !== undefined) {
167
+ values[key] = fieldDef.schema.parse(userOptions[key]);
168
+ continue;
150
169
  }
170
+ // 2. Environment variables
171
+ const envValue = Configuration.#readEnvVar(fieldDef);
172
+ if (envValue != null) {
173
+ values[key] = fieldDef.schema.parse(envValue);
174
+ continue;
175
+ }
176
+ // 3. crawlee.json file options
177
+ if (key in fileOptions && fileOptions[key] !== undefined) {
178
+ values[key] = fieldDef.schema.parse(fileOptions[key]);
179
+ continue;
180
+ }
181
+ // 4. Schema default (by parsing undefined through the schema)
182
+ const parsed = fieldDef.schema.safeParse(undefined);
183
+ values[key] = parsed.success ? parsed.data : undefined;
151
184
  }
152
- if (envValue != null) {
153
- return this._castEnvValue(key, envValue);
154
- }
155
- // check instance level options
156
- if (this.options.has(key)) {
157
- return this.options.get(key);
158
- }
159
- // fallback to defaults
160
- return (defaultValue ?? Configuration.DEFAULTS[key] ?? envValue);
185
+ return values;
161
186
  }
162
- _castEnvValue(key, value) {
163
- if (Configuration.INTEGER_VARS.includes(key)) {
164
- return +value;
165
- }
166
- if (Configuration.BOOLEAN_VARS.includes(key)) {
167
- // 0, false and empty string are considered falsy values
168
- return !['0', 'false', ''].includes(String(value).toLowerCase());
169
- }
170
- if (Configuration.COMMA_SEPARATED_LIST_VARS.includes(key)) {
171
- if (!value)
172
- return [];
173
- return String(value)
174
- .split(',')
175
- .map((v) => v.trim());
187
+ /**
188
+ * Registers getters (and throwing setters) on the instance for each field.
189
+ */
190
+ #registerAccessors() {
191
+ const fields = this.constructor.fields;
192
+ const descriptors = {};
193
+ for (const key of Object.keys(fields)) {
194
+ descriptors[key] = {
195
+ get: () => this.#resolvedValues[key],
196
+ set() {
197
+ throw new TypeError('Configuration is immutable. Pass options via the constructor instead.');
198
+ },
199
+ enumerable: true,
200
+ configurable: false,
201
+ };
176
202
  }
177
- return value;
203
+ Object.defineProperties(this, descriptors);
178
204
  }
179
205
  /**
180
- * Sets value for given option. Only affects this `Configuration` instance, the value will not be propagated down to the env var.
181
- * To reset a value, we can omit the `value` argument or pass `undefined` there.
206
+ * Reads the first defined env var value for a field definition.
207
+ * Empty strings are treated as unset, falling through to crawlee.json or schema defaults.
208
+ * (Crawlee v3 coerced `''` to `false`/`0`/`''` per type — v4 drops that for consistency.)
182
209
  */
183
- set(key, value) {
184
- this.options.set(key, value);
210
+ static #readEnvVar(fieldDef) {
211
+ if (!fieldDef.envVar)
212
+ return undefined;
213
+ const envVars = Array.isArray(fieldDef.envVar) ? fieldDef.envVar : [fieldDef.envVar];
214
+ for (const envVar of envVars) {
215
+ const value = process.env[envVar];
216
+ if (value != null && value !== '')
217
+ return value;
218
+ }
219
+ return undefined;
185
220
  }
186
221
  /**
187
- * Returns the global configuration instance. It will respect the environment variables.
188
- *
189
- * Delegates to the global ServiceLocator, making it the single source of truth for service management.
222
+ * Loads config options from crawlee.json in the current working directory.
190
223
  */
191
- static getGlobalConfig() {
192
- return serviceLocator.getConfiguration();
193
- }
194
- buildOptions(options) {
195
- // try to load configuration from crawlee.json as the baseline
196
- const path = join(process.cwd(), 'crawlee.json');
197
- if (pathExistsSync(path)) {
198
- try {
199
- const file = readFileSync(path);
200
- const optionsFromFileConfig = JSON.parse(file.toString());
201
- Object.assign(options, optionsFromFileConfig);
202
- }
203
- catch {
204
- // ignore
205
- }
224
+ static #loadFileOptions() {
225
+ try {
226
+ const file = readFileSync(join(process.cwd(), 'crawlee.json'), 'utf8');
227
+ // JSON5 also handles a leading UTF-8 byte order mark, which some Windows tools write
228
+ return JSON5.parse(file);
229
+ }
230
+ catch {
231
+ return {};
206
232
  }
207
- this.options = new Map(entries(options));
208
233
  }
209
234
  }
210
- //# sourceMappingURL=configuration.js.map
package/debug.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ import type { IncomingMessage } from 'node:http';
2
+ import type { Dictionary } from '@crawlee/types';
3
+ import type { Request } from './request.js';
4
+ interface BrowserResponseLike {
5
+ status(): number;
6
+ }
7
+ /**
8
+ * Creates a standardized debug info from request and response. This info is usually added to dataset under the hidden `#debug` field.
9
+ *
10
+ * @param request [Request](https://sdk.apify.com/docs/api/request) object.
11
+ * @param [response]
12
+ * Puppeteer [`Response`](https://pptr.dev/#?product=Puppeteer&version=v1.11.0&show=api-class-response)
13
+ * or NodeJS [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_serverresponse).
14
+ * @param [additionalFields] Object containing additional fields to be added.
15
+ *
16
+ * @internal
17
+ */
18
+ export declare function createRequestDebugInfo(request: Request, response?: IncomingMessage | Partial<BrowserResponseLike>, additionalFields?: Dictionary): Dictionary;
19
+ /**
20
+ * Returns a human-readable label for an unknown value,
21
+ * suitable for embedding in error messages and log output.
22
+ *
23
+ * Returns `constructor.name` when available (e.g. `"Configuration"`, `"Number"`),
24
+ * otherwise falls back to `util.inspect` (e.g. for `null`, `undefined`).
25
+ *
26
+ * @internal
27
+ */
28
+ export declare function inspectValue(value: unknown): string;
29
+ /**
30
+ * Returns the type of a value as a lowercase string, with `Date`, `Buffer` and `RegExp` reported
31
+ * by their constructor name. Used for building validation error messages.
32
+ *
33
+ * @internal
34
+ */
35
+ export declare function getObjectType(value: unknown): string;
36
+ export {};
package/debug.js ADDED
@@ -0,0 +1,70 @@
1
+ import { inspect } from 'node:util';
2
+ import { parseArgument, schemas } from './validators.js';
3
+ /**
4
+ * Creates a standardized debug info from request and response. This info is usually added to dataset under the hidden `#debug` field.
5
+ *
6
+ * @param request [Request](https://sdk.apify.com/docs/api/request) object.
7
+ * @param [response]
8
+ * Puppeteer [`Response`](https://pptr.dev/#?product=Puppeteer&version=v1.11.0&show=api-class-response)
9
+ * or NodeJS [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_class_http_serverresponse).
10
+ * @param [additionalFields] Object containing additional fields to be added.
11
+ *
12
+ * @internal
13
+ */
14
+ export function createRequestDebugInfo(request, response = {}, additionalFields = {}) {
15
+ parseArgument(request, schemas.anyObject);
16
+ parseArgument(response, schemas.anyObject);
17
+ parseArgument(additionalFields, schemas.anyObject);
18
+ return {
19
+ requestId: request.id,
20
+ url: request.url,
21
+ loadedUrl: request.loadedUrl,
22
+ method: request.method,
23
+ retryCount: request.retryCount,
24
+ errorMessages: request.errorMessages,
25
+ // Puppeteer response has .status() function and NodeJS response, statusCode property.
26
+ statusCode: 'status' in response && response.status instanceof Function
27
+ ? response.status()
28
+ : response.statusCode,
29
+ ...additionalFields,
30
+ };
31
+ }
32
+ /**
33
+ * Returns a human-readable label for an unknown value,
34
+ * suitable for embedding in error messages and log output.
35
+ *
36
+ * Returns `constructor.name` when available (e.g. `"Configuration"`, `"Number"`),
37
+ * otherwise falls back to `util.inspect` (e.g. for `null`, `undefined`).
38
+ *
39
+ * @internal
40
+ */
41
+ export function inspectValue(value) {
42
+ if (typeof value === 'object' && value !== null && value.constructor?.name) {
43
+ return value.constructor.name;
44
+ }
45
+ return inspect(value, {
46
+ depth: 0,
47
+ compact: true,
48
+ maxStringLength: 64,
49
+ breakLength: Infinity,
50
+ colors: false,
51
+ });
52
+ }
53
+ /**
54
+ * Returns the type of a value as a lowercase string, with `Date`, `Buffer` and `RegExp` reported
55
+ * by their constructor name. Used for building validation error messages.
56
+ *
57
+ * @internal
58
+ */
59
+ export function getObjectType(value) {
60
+ const simple = typeof value;
61
+ if (['string', 'number', 'boolean', 'bigint'].includes(simple)) {
62
+ return simple;
63
+ }
64
+ const objectType = Object.prototype.toString.call(value);
65
+ const type = /\[object (\w+)]/.exec(objectType)[1];
66
+ if (type === 'Uint8Array') {
67
+ return 'Buffer';
68
+ }
69
+ return ['Date', 'Buffer', 'RegExp'].includes(type) ? type : type.toLowerCase();
70
+ }
package/errors.d.ts CHANGED
@@ -10,37 +10,59 @@ export declare class NonRetryableError extends Error {
10
10
  export declare class CriticalError extends NonRetryableError {
11
11
  }
12
12
  /**
13
- * @ignore
13
+ * A schema validation issue, structurally compatible with `StandardSchemaV1.Issue`. Declared here so that
14
+ * error types do not have to depend on `@standard-schema/spec`.
14
15
  */
15
- export declare class MissingRouteError extends CriticalError {
16
+ export interface SchemaIssue {
17
+ readonly message: string;
18
+ readonly path?: readonly (PropertyKey | {
19
+ key: PropertyKey;
20
+ })[];
16
21
  }
17
22
  /**
18
- * Errors of `RetryRequestError` type will always be retried by the crawler.
23
+ * Thrown when a request's `userData` does not match the {@link RouteSchemas|Standard Schema} registered for its label.
19
24
  *
20
- * *This error overrides the `maxRequestRetries` option, i.e. the request can be retried indefinitely until it succeeds.*
25
+ * As the `userData` does not change between attempts, this error is non-retryable.
21
26
  */
22
- export declare class RetryRequestError extends Error {
23
- constructor(message?: string);
27
+ export declare class RequestValidationError extends NonRetryableError {
28
+ readonly label: string | symbol;
29
+ readonly issues: readonly SchemaIssue[];
30
+ constructor(label: string | symbol, issues: readonly SchemaIssue[]);
24
31
  }
25
32
  /**
26
- * Errors of `SessionError` type will trigger a session rotation.
33
+ * Thrown by {@link RecoverableState} when a persisted state record does not match its `stateSchema`.
27
34
  *
28
- * This error doesn't respect the `maxRequestRetries` option and has a separate limit of `maxSessionRotations`.
35
+ * Whether a corrupt record should abort the run or be discarded in favour of the defaults depends on what the
36
+ * state is for, so {@link RecoverableState.initialize} always throws and leaves the choice to the caller.
29
37
  */
30
- export declare class SessionError extends RetryRequestError {
31
- constructor(message?: string);
38
+ export declare class StateValidationError extends Error {
39
+ readonly persistStateKey: string;
40
+ readonly issues: readonly SchemaIssue[];
41
+ constructor(persistStateKey: string, issues: readonly SchemaIssue[]);
32
42
  }
33
- export declare class ContextPipelineInterruptedError extends Error {
43
+ /**
44
+ * Errors of `SessionError` type retire the session associated with the request and trigger a regular retry.
45
+ *
46
+ * The retry counts towards the `maxRequestRetries` limit, just like any other error.
47
+ */
48
+ export declare class SessionError extends Error {
34
49
  constructor(message?: string);
35
50
  }
36
- export declare class ContextPipelineInitializationError extends Error {
37
- constructor(error: unknown, options?: ErrorOptions);
38
- }
39
- export declare class ContextPipelineCleanupError extends CriticalError {
40
- constructor(error: unknown, options?: ErrorOptions);
51
+ /**
52
+ * A {@link SessionError} for a session that finished without being blocked, e.g. by reaching its
53
+ * `maxUsageCount` or `maxAgeSecs`. A plain `SessionError` means the session was blocked.
54
+ */
55
+ export declare class SessionRetiredError extends SessionError {
41
56
  }
42
- export declare class RequestHandlerError extends Error {
43
- constructor(error: unknown, options?: ErrorOptions);
57
+ /**
58
+ * Wraps the failure of a callback registered with {@link StorageTransaction.afterCommit|`afterCommit`}
59
+ * that ran after the request's writes had already been committed.
60
+ *
61
+ * Non-retryable by nature: the writes are durable, so re-running the request handler would duplicate
62
+ * them. A callback that throws a `NonRetryableError` of its own is left alone.
63
+ */
64
+ export declare class AfterCommitError extends NonRetryableError {
65
+ constructor(cause: unknown);
44
66
  }
45
67
  /**
46
68
  * Thrown when attempting to set a different service instance after one has already been retrieved.
@@ -48,4 +70,3 @@ export declare class RequestHandlerError extends Error {
48
70
  export declare class ServiceConflictError extends Error {
49
71
  constructor(serviceName: string, newValue: unknown, existingValue: unknown);
50
72
  }
51
- //# sourceMappingURL=errors.d.ts.map