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

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
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Resolves `segment` against `baseDirectory` and ensures the result stays within `baseDirectory`.
3
+ * Storage names and record keys are used as filesystem path components, so a value containing `..`
4
+ * or an absolute path could otherwise escape the intended directory.
5
+ */
6
+ export declare function resolveWithinDirectory(baseDirectory: string, segment: string): string;
7
+ /**
8
+ * Removes all properties with a null value
9
+ * from the provided object.
10
+ */
11
+ export declare function purgeNullsFromObject<T>(object: T): T;
12
+ /**
13
+ * Creates a standard request ID (same as Platform).
14
+ */
15
+ export declare function uniqueKeyToRequestId(uniqueKey: string): string;
16
+ export { isBuffer, isStream, toBuffer } from '../byte_utils.js';
@@ -0,0 +1,41 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { resolve, sep } from 'node:path';
3
+ import { REQUEST_ID_LENGTH } from './consts.js';
4
+ /**
5
+ * Resolves `segment` against `baseDirectory` and ensures the result stays within `baseDirectory`.
6
+ * Storage names and record keys are used as filesystem path components, so a value containing `..`
7
+ * or an absolute path could otherwise escape the intended directory.
8
+ */
9
+ export function resolveWithinDirectory(baseDirectory, segment) {
10
+ const base = resolve(baseDirectory);
11
+ const resolved = resolve(base, segment);
12
+ if (resolved !== base && !resolved.startsWith(`${base}${sep}`)) {
13
+ throw new Error(`"${segment}" is not allowed because it would resolve outside of the storage directory. ` +
14
+ `Storage names and record keys must not contain path traversal segments ("..") or absolute paths.`);
15
+ }
16
+ return resolved;
17
+ }
18
+ /**
19
+ * Removes all properties with a null value
20
+ * from the provided object.
21
+ */
22
+ export function purgeNullsFromObject(object) {
23
+ if (object && typeof object === 'object' && !Array.isArray(object)) {
24
+ for (const [key, value] of Object.entries(object)) {
25
+ if (value === null)
26
+ Reflect.deleteProperty(object, key);
27
+ }
28
+ }
29
+ return object;
30
+ }
31
+ /**
32
+ * Creates a standard request ID (same as Platform).
33
+ */
34
+ export function uniqueKeyToRequestId(uniqueKey) {
35
+ const str = createHash('sha256')
36
+ .update(uniqueKey)
37
+ .digest('base64')
38
+ .replace(/(\+|\/|=)/g, '');
39
+ return str.length > REQUEST_ID_LENGTH ? str.slice(0, REQUEST_ID_LENGTH) : str;
40
+ }
41
+ export { isBuffer, isStream, toBuffer } from '../byte_utils.js';
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Captures the "inject-or-default" ownership pattern for a crawler's stateful collaborators (session pool, browser
3
+ * pool, the crawler-opened request queue, ...). A collaborator is either **injected** by the user (borrowed — the
4
+ * crawler never drives its lifecycle) or **built by the crawler** as a default (owned — the crawler sequences its
5
+ * lifecycle). Owned-only lifecycle hooks are gated through a single {@link OwnedOrInjected.ifOwned|`ifOwned()`}.
6
+ *
7
+ * This is the deliberate complement to the {@link ServiceLocator}: ambient infrastructure (config/events/storage/
8
+ * logger) stays in the locator, while crawler-scoped stateful collaborators the crawler sequences go through this.
9
+ *
10
+ * @internal
11
+ */
12
+ export declare class OwnedOrInjected<Injected, Owned extends Injected = Injected> {
13
+ #private;
14
+ private constructor();
15
+ /**
16
+ * Resolves a collaborator from an optionally-injected instance. `Injected` is the public/borrowed type exposed via
17
+ * {@link OwnedOrInjected.value|`value`}; `Owned` is the concrete type the crawler builds (a subtype with extra
18
+ * lifecycle methods), which {@link OwnedOrInjected.set|`set()`} and {@link OwnedOrInjected.ifOwned|`ifOwned()`}
19
+ * deal in — so owned-only lifecycle hooks are statically typed with no casts.
20
+ *
21
+ * - `injected` provided → borrowed (present, not owned).
22
+ * - `injected` omitted → owned; `buildDefault` fills the slot eagerly if given, otherwise it stays empty until a
23
+ * later {@link OwnedOrInjected.set|`set()`} (the lazy case, e.g. a request queue opened on first use).
24
+ */
25
+ static resolve<Injected, Owned extends Injected = Injected>(injected?: Injected, buildDefault?: () => Owned): OwnedOrInjected<Injected, Owned>;
26
+ /**
27
+ * Whether the crawler owns the instance and is therefore responsible for its lifecycle. `true` only for
28
+ * crawler-built defaults, `false` for user-injected instances.
29
+ */
30
+ get isOwned(): boolean;
31
+ /**
32
+ * Whether a value is currently available. `false` for an owned slot whose default hasn't been built yet
33
+ * (e.g. a lazily-opened request queue before its first use).
34
+ */
35
+ get isPresent(): boolean;
36
+ /**
37
+ * The resolved instance, typed as the public `Injected` type. Throws if the value is not present yet — callers that
38
+ * expect a lazily-filled owned slot should read {@link OwnedOrInjected.maybeValue|`maybeValue`} instead.
39
+ */
40
+ get value(): Injected;
41
+ /**
42
+ * The resolved instance, or `undefined` when a lazily-filled owned slot hasn't been built yet. The non-throwing
43
+ * counterpart to {@link OwnedOrInjected.value|`value`} — pairs naturally with `?? fallback` so callers can read
44
+ * a possibly-empty slot without the `isPresent ? value : …` dance.
45
+ */
46
+ get maybeValue(): Injected | undefined;
47
+ /**
48
+ * Fills the (owned) slot with the crawler-built default, returning it for convenience. Only valid on an owned,
49
+ * not-yet-filled slot: borrowed instances are never replaced and an owned slot is filled exactly once (re-setting
50
+ * would silently orphan the previous instance's lifecycle).
51
+ */
52
+ set(value: Owned): Owned;
53
+ /**
54
+ * Runs an owned-only lifecycle hook, invoked (with the value typed as the concrete `Owned`) only when the crawler
55
+ * owns a present instance — a no-op for a borrowed instance or an owned-but-not-yet-built slot.
56
+ */
57
+ ifOwned<R>(fn: (value: Owned) => R | Promise<R>): Promise<R | undefined>;
58
+ }
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Captures the "inject-or-default" ownership pattern for a crawler's stateful collaborators (session pool, browser
3
+ * pool, the crawler-opened request queue, ...). A collaborator is either **injected** by the user (borrowed — the
4
+ * crawler never drives its lifecycle) or **built by the crawler** as a default (owned — the crawler sequences its
5
+ * lifecycle). Owned-only lifecycle hooks are gated through a single {@link OwnedOrInjected.ifOwned|`ifOwned()`}.
6
+ *
7
+ * This is the deliberate complement to the {@link ServiceLocator}: ambient infrastructure (config/events/storage/
8
+ * logger) stays in the locator, while crawler-scoped stateful collaborators the crawler sequences go through this.
9
+ *
10
+ * @internal
11
+ */
12
+ export class OwnedOrInjected {
13
+ #value;
14
+ #owned;
15
+ #present;
16
+ constructor(value, owned, present) {
17
+ this.#value = value;
18
+ this.#owned = owned;
19
+ this.#present = present;
20
+ }
21
+ /**
22
+ * Resolves a collaborator from an optionally-injected instance. `Injected` is the public/borrowed type exposed via
23
+ * {@link OwnedOrInjected.value|`value`}; `Owned` is the concrete type the crawler builds (a subtype with extra
24
+ * lifecycle methods), which {@link OwnedOrInjected.set|`set()`} and {@link OwnedOrInjected.ifOwned|`ifOwned()`}
25
+ * deal in — so owned-only lifecycle hooks are statically typed with no casts.
26
+ *
27
+ * - `injected` provided → borrowed (present, not owned).
28
+ * - `injected` omitted → owned; `buildDefault` fills the slot eagerly if given, otherwise it stays empty until a
29
+ * later {@link OwnedOrInjected.set|`set()`} (the lazy case, e.g. a request queue opened on first use).
30
+ */
31
+ static resolve(injected, buildDefault) {
32
+ if (injected !== undefined) {
33
+ return new OwnedOrInjected(injected, false, true);
34
+ }
35
+ if (buildDefault !== undefined) {
36
+ return new OwnedOrInjected(buildDefault(), true, true);
37
+ }
38
+ return new OwnedOrInjected(undefined, true, false);
39
+ }
40
+ /**
41
+ * Whether the crawler owns the instance and is therefore responsible for its lifecycle. `true` only for
42
+ * crawler-built defaults, `false` for user-injected instances.
43
+ */
44
+ get isOwned() {
45
+ return this.#owned;
46
+ }
47
+ /**
48
+ * Whether a value is currently available. `false` for an owned slot whose default hasn't been built yet
49
+ * (e.g. a lazily-opened request queue before its first use).
50
+ */
51
+ get isPresent() {
52
+ return this.#present;
53
+ }
54
+ /**
55
+ * The resolved instance, typed as the public `Injected` type. Throws if the value is not present yet — callers that
56
+ * expect a lazily-filled owned slot should read {@link OwnedOrInjected.maybeValue|`maybeValue`} instead.
57
+ */
58
+ get value() {
59
+ if (!this.#present) {
60
+ throw new Error('OwnedOrInjected value is not initialized yet');
61
+ }
62
+ return this.#value;
63
+ }
64
+ /**
65
+ * The resolved instance, or `undefined` when a lazily-filled owned slot hasn't been built yet. The non-throwing
66
+ * counterpart to {@link OwnedOrInjected.value|`value`} — pairs naturally with `?? fallback` so callers can read
67
+ * a possibly-empty slot without the `isPresent ? value : …` dance.
68
+ */
69
+ get maybeValue() {
70
+ return this.#present ? this.#value : undefined;
71
+ }
72
+ /**
73
+ * Fills the (owned) slot with the crawler-built default, returning it for convenience. Only valid on an owned,
74
+ * not-yet-filled slot: borrowed instances are never replaced and an owned slot is filled exactly once (re-setting
75
+ * would silently orphan the previous instance's lifecycle).
76
+ */
77
+ set(value) {
78
+ if (!this.#owned) {
79
+ throw new Error('Cannot set() a borrowed OwnedOrInjected value');
80
+ }
81
+ if (this.#present) {
82
+ throw new Error('OwnedOrInjected value is already initialized');
83
+ }
84
+ this.#value = value;
85
+ this.#present = true;
86
+ return value;
87
+ }
88
+ /**
89
+ * Runs an owned-only lifecycle hook, invoked (with the value typed as the concrete `Owned`) only when the crawler
90
+ * owns a present instance — a no-op for a borrowed instance or an owned-but-not-yet-built slot.
91
+ */
92
+ async ifOwned(fn) {
93
+ if (!this.#owned || !this.#present) {
94
+ return undefined;
95
+ }
96
+ return fn(this.#value);
97
+ }
98
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/core",
3
- "version": "4.0.0-beta.13",
3
+ "version": "4.0.0-beta.131",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
6
  "node": ">=22.0.0"
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "homepage": "https://crawlee.dev",
40
40
  "scripts": {
41
- "build": "yarn clean && yarn compile && yarn copy",
41
+ "build": "pnpm clean && pnpm compile && pnpm copy",
42
42
  "clean": "rimraf ./dist",
43
43
  "compile": "tsc -p tsconfig.build.json",
44
44
  "copy": "tsx ../../scripts/copy.ts"
@@ -50,25 +50,26 @@
50
50
  "@apify/consts": "^2.41.0",
51
51
  "@apify/datastructures": "^2.0.3",
52
52
  "@apify/log": "^2.5.18",
53
- "@apify/pseudo_url": "^2.0.59",
54
- "@apify/timeout": "^0.3.2",
53
+ "@apify/timeout": "^0.4.4",
55
54
  "@apify/utilities": "^2.15.5",
56
- "@crawlee/memory-storage": "4.0.0-beta.13",
57
- "@crawlee/types": "4.0.0-beta.13",
58
- "@crawlee/utils": "4.0.0-beta.13",
55
+ "@crawlee/fs-storage": "4.0.0-beta.131",
56
+ "@crawlee/http-client": "4.0.0-beta.131",
57
+ "@crawlee/types": "4.0.0-beta.131",
58
+ "@crawlee/utils": "4.0.0-beta.131",
59
59
  "@sapphire/async-queue": "^1.5.5",
60
+ "@standard-schema/spec": "^1.0.0",
60
61
  "@vladfrangu/async_event_emitter": "^2.4.6",
62
+ "content-type": "^1.0.5",
61
63
  "csv-stringify": "^6.5.2",
62
- "fs-extra": "^11.3.0",
63
- "got-scraping": "^4.1.1",
64
64
  "json5": "^2.2.3",
65
+ "mime-types": "^3.0.1",
65
66
  "minimatch": "^10.0.1",
66
- "ow": "^2.0.0",
67
67
  "stream-json": "^1.9.1",
68
68
  "tldts": "^7.0.6",
69
- "tough-cookie": "^5.1.2",
69
+ "tough-cookie": "^6.0.0",
70
70
  "tslib": "^2.8.1",
71
- "type-fest": "^4.41.0"
71
+ "type-fest": "^4.41.0",
72
+ "zod": "^4.4.3"
72
73
  },
73
74
  "lerna": {
74
75
  "command": {
@@ -77,5 +78,5 @@
77
78
  }
78
79
  }
79
80
  },
80
- "gitHead": "24df7c8dba6b6c9623c02ccc655d8ee748f003e3"
81
+ "gitHead": "de741c0b63d0da061e4ffcd6ce6fdc0c92401399"
81
82
  }
@@ -1,16 +1,18 @@
1
+ import type { ProxyInfo } from '@crawlee/types';
1
2
  import type { Request } from './request.js';
2
3
  export interface ProxyConfigurationFunction {
3
4
  (options?: {
4
5
  request?: Request;
5
6
  }): string | null | Promise<string | null>;
6
7
  }
8
+ type UrlList = (string | null)[];
7
9
  export interface ProxyConfigurationOptions {
8
10
  /**
9
11
  * An array of custom proxy URLs to be rotated.
10
12
  * Custom proxies are not compatible with Apify Proxy and an attempt to use both
11
13
  * configuration options will cause an error to be thrown on initialize.
12
14
  */
13
- proxyUrls?: string[];
15
+ proxyUrls?: UrlList;
14
16
  /**
15
17
  * Custom function that allows you to generate the new proxy URL dynamically. It gets an optional parameter with the `Request` object when applicable.
16
18
  * Can return either stringified proxy URL or `null` if the proxy should not be used. Can be asynchronous.
@@ -18,111 +20,26 @@ export interface ProxyConfigurationOptions {
18
20
  * This function is used to generate the URL when {@link ProxyConfiguration.newUrl} or {@link ProxyConfiguration.newProxyInfo} is called.
19
21
  */
20
22
  newUrlFunction?: ProxyConfigurationFunction;
21
- /**
22
- * An array of custom proxy URLs to be rotated stratified in tiers.
23
- * This is a more advanced version of `proxyUrls` that allows you to define a hierarchy of proxy URLs
24
- * If everything goes well, all the requests will be sent through the first proxy URL in the list.
25
- * Whenever the crawler encounters a problem with the current proxy on the given domain, it will switch to the higher tier for this domain.
26
- * The crawler probes lower-level proxies at intervals to check if it can make the tier downshift.
27
- *
28
- * This feature is useful when you have a set of proxies with different performance characteristics (speed, price, antibot performance etc.) and you want to use the best one for each domain.
29
- *
30
- * Use `null` as a proxy URL to disable the proxy for the given tier.
31
- */
32
- tieredProxyUrls?: (string | null)[][];
33
- }
34
- export interface TieredProxy {
35
- proxyUrl: string | null;
36
- proxyTier?: number;
37
- }
38
- /**
39
- * The main purpose of the ProxyInfo object is to provide information
40
- * about the current proxy connection used by the crawler for the request.
41
- * Outside of crawlers, you can get this object by calling {@link ProxyConfiguration.newProxyInfo}.
42
- *
43
- * **Example usage:**
44
- *
45
- * ```javascript
46
- * const proxyConfiguration = new ProxyConfiguration({
47
- * proxyUrls: ['...', '...'] // List of Proxy URLs to rotate
48
- * });
49
- *
50
- * // Getting proxyInfo object by calling class method directly
51
- * const proxyInfo = await proxyConfiguration.newProxyInfo();
52
- *
53
- * // In crawler
54
- * const crawler = new CheerioCrawler({
55
- * // ...
56
- * proxyConfiguration,
57
- * requestHandler({ proxyInfo }) {
58
- * // Getting used proxy URL
59
- * const proxyUrl = proxyInfo.url;
60
- * }
61
- * })
62
- *
63
- * ```
64
- */
65
- export interface ProxyInfo {
66
- /**
67
- * The URL of the proxy.
68
- */
69
- url: string;
70
- /**
71
- * Username for the proxy.
72
- */
73
- username?: string;
74
- /**
75
- * User's password for the proxy.
76
- */
77
- password: string;
78
- /**
79
- * Hostname of your proxy.
80
- */
81
- hostname: string;
82
- /**
83
- * Proxy port.
84
- */
85
- port: number | string;
86
- /**
87
- * Proxy tier for the current proxy, if applicable (only for `tieredProxyUrls`).
88
- */
89
- proxyTier?: number;
90
- /**
91
- * When `true`, the proxy is likely intercepting HTTPS traffic and is able to view and modify its content.
92
- *
93
- * @default false
94
- */
95
- ignoreTlsErrors?: boolean;
96
23
  }
97
- interface TieredProxyOptions {
24
+ interface NewUrlOptions {
98
25
  request?: Request;
99
- proxyTier?: number;
100
26
  }
101
27
  /**
102
- * Internal class for tracking the proxy tier history for a specific domain.
28
+ * Minimal contract that any object passed to a crawler as its `proxyConfiguration`
29
+ * option must satisfy.
30
+ *
31
+ * Implement this interface to plug a custom proxy-provisioning strategy into any Crawlee
32
+ * crawler — for example a remote proxy service or a thin wrapper around the built-in
33
+ * `ProxyConfiguration` with different rotation rules. *
103
34
  *
104
- * Predicts the best proxy tier for the next request based on the error history for different proxy tiers.
35
+ * @category Scaling
105
36
  */
106
- declare class ProxyTierTracker {
107
- private histogram;
108
- private currentTier;
109
- constructor(tieredProxyUrls: (string | null)[][]);
37
+ export interface IProxyConfiguration {
110
38
  /**
111
- * Processes a single step of the algorithm and updates the current tier prediction based on the error history.
39
+ * Creates a new {@link ProxyInfo} object describing the proxy to use for the given
40
+ * request. Returns `undefined` when no proxy should be used.
112
41
  */
113
- private processStep;
114
- /**
115
- * Increases the error score for the given proxy tier. This raises the chance of picking a different proxy tier for the subsequent requests.
116
- *
117
- * The error score is increased by 10 for the given tier. This means that this tier will be disadvantaged for the next 10 requests (every new request prediction decreases the error score by 1).
118
- * @param tier The proxy tier to mark as problematic.
119
- */
120
- addError(tier: number): void;
121
- /**
122
- * Returns the best proxy tier for the next request based on the error history for different proxy tiers.
123
- * @returns The proxy tier prediction
124
- */
125
- predictTier(): number;
42
+ newProxyInfo(options?: NewUrlOptions): Promise<ProxyInfo | undefined>;
126
43
  }
127
44
  /**
128
45
  * Configures connection to a proxy server with the provided options. Proxy servers are used to prevent target websites from blocking
@@ -152,16 +69,9 @@ declare class ProxyTierTracker {
152
69
  * ```
153
70
  * @category Scaling
154
71
  */
155
- export declare class ProxyConfiguration {
156
- isManInTheMiddle: boolean;
157
- protected nextCustomUrlIndex: number;
158
- protected proxyUrls?: string[];
159
- protected tieredProxyUrls?: (string | null)[][];
160
- protected usedProxyUrls: Map<string, string>;
161
- protected newUrlFunction?: ProxyConfigurationFunction;
162
- // @ts-ignore optional peer dependency or compatibility with es2022
163
- protected log: import("@apify/log").Log;
164
- protected domainTiers: Map<string, ProxyTierTracker>;
72
+ export declare class ProxyConfiguration implements IProxyConfiguration {
73
+ #private;
74
+ readonly isManInTheMiddle = false;
165
75
  /**
166
76
  * Creates a {@link ProxyConfiguration} instance based on the provided options. Proxy servers are used to prevent target websites from
167
77
  * blocking your crawlers based on IP address rate limits or blacklists. Setting proxy configuration in your crawlers automatically configures
@@ -192,38 +102,20 @@ export declare class ProxyConfiguration {
192
102
  *
193
103
  * @return Represents information about used proxy and its configuration.
194
104
  */
195
- newProxyInfo(options?: TieredProxyOptions): Promise<ProxyInfo | undefined>;
196
- /**
197
- * Given a request / proxy tier, this function returns a new proxy URL based on the provided configuration options.
198
- * @param options Options for the tiered proxy rotation
199
- * @returns An object with the proxy URL and the proxy tier used.
200
- */
201
- protected _handleTieredUrl(options?: TieredProxyOptions): TieredProxy;
202
- /**
203
- * Given a `Request` object, this function returns the tier of the proxy that should be used for the request.
204
- *
205
- * This returns `null` if `tieredProxyUrls` option is not set.
206
- */
207
- protected predictProxyTier(request: Request): number | null;
105
+ newProxyInfo(options?: NewUrlOptions): Promise<ProxyInfo | undefined>;
208
106
  /**
209
107
  * Returns a new proxy URL based on provided configuration options.
210
108
  *
211
109
  * @return A string with a proxy URL, including authentication credentials and port number.
212
110
  * For example, `http://bob:password123@proxy.example.com:8000`
213
111
  */
214
- newUrl(options?: TieredProxyOptions): Promise<string | undefined>;
215
- /**
216
- * Handles custom url rotation
217
- */
218
- protected _handleProxyUrlsList(): string;
112
+ newUrl(options?: NewUrlOptions): Promise<string | undefined>;
113
+ private handleProxyUrlsList;
219
114
  /**
220
115
  * Calls the custom newUrlFunction and checks format of its return value
221
116
  */
222
- protected _callNewUrlFunction(options?: {
223
- request?: Request;
224
- }): Promise<string | null>;
225
- protected _throwCannotCombineCustomMethods(): never;
226
- protected _throwNoOptionsProvided(): never;
117
+ private callNewUrlFunction;
118
+ private throwCannotCombineCustomMethods;
119
+ private throwNoOptionsProvided;
227
120
  }
228
121
  export {};
229
- //# sourceMappingURL=proxy_configuration.d.ts.map