@crawlee/core 3.0.0-alpha.1 → 3.0.0-alpha.4

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 (159) hide show
  1. package/autoscaling/autoscaled_pool.d.ts +286 -0
  2. package/autoscaling/autoscaled_pool.d.ts.map +1 -0
  3. package/autoscaling/autoscaled_pool.js +624 -0
  4. package/autoscaling/autoscaled_pool.js.map +1 -0
  5. package/autoscaling/index.d.ts +4 -0
  6. package/autoscaling/index.d.ts.map +1 -0
  7. package/autoscaling/index.js +7 -0
  8. package/autoscaling/index.js.map +1 -0
  9. package/autoscaling/snapshotter.d.ts +193 -0
  10. package/autoscaling/snapshotter.d.ts.map +1 -0
  11. package/autoscaling/snapshotter.js +377 -0
  12. package/autoscaling/snapshotter.js.map +1 -0
  13. package/autoscaling/system_status.d.ts +181 -0
  14. package/autoscaling/system_status.d.ts.map +1 -0
  15. package/autoscaling/system_status.js +194 -0
  16. package/autoscaling/system_status.js.map +1 -0
  17. package/cache_container.d.ts +18 -0
  18. package/cache_container.d.ts.map +1 -0
  19. package/cache_container.js +42 -0
  20. package/cache_container.js.map +1 -0
  21. package/configuration.d.ts +150 -0
  22. package/configuration.d.ts.map +1 -0
  23. package/configuration.js +284 -0
  24. package/configuration.js.map +1 -0
  25. package/constants.d.ts +3 -0
  26. package/constants.d.ts.map +1 -0
  27. package/constants.js +6 -0
  28. package/constants.js.map +1 -0
  29. package/crawlers/crawler_commons.d.ts +23 -0
  30. package/crawlers/crawler_commons.d.ts.map +1 -0
  31. package/crawlers/crawler_commons.js +3 -0
  32. package/crawlers/crawler_commons.js.map +1 -0
  33. package/crawlers/crawler_extension.d.ts +11 -0
  34. package/crawlers/crawler_extension.d.ts.map +1 -0
  35. package/crawlers/crawler_extension.js +30 -0
  36. package/crawlers/crawler_extension.js.map +1 -0
  37. package/crawlers/crawler_utils.d.ts +25 -0
  38. package/crawlers/crawler_utils.d.ts.map +1 -0
  39. package/crawlers/crawler_utils.js +72 -0
  40. package/crawlers/crawler_utils.js.map +1 -0
  41. package/crawlers/index.d.ts +5 -0
  42. package/crawlers/index.d.ts.map +1 -0
  43. package/crawlers/index.js +8 -0
  44. package/crawlers/index.js.map +1 -0
  45. package/crawlers/statistics.d.ts +147 -0
  46. package/crawlers/statistics.d.ts.map +1 -0
  47. package/crawlers/statistics.js +360 -0
  48. package/crawlers/statistics.js.map +1 -0
  49. package/enqueue_links/enqueue_links.d.ts +138 -0
  50. package/enqueue_links/enqueue_links.d.ts.map +1 -0
  51. package/enqueue_links/enqueue_links.js +123 -0
  52. package/enqueue_links/enqueue_links.js.map +1 -0
  53. package/enqueue_links/index.d.ts +3 -0
  54. package/enqueue_links/index.d.ts.map +1 -0
  55. package/enqueue_links/index.js +6 -0
  56. package/enqueue_links/index.js.map +1 -0
  57. package/enqueue_links/shared.d.ts +60 -0
  58. package/enqueue_links/shared.d.ts.map +1 -0
  59. package/enqueue_links/shared.js +148 -0
  60. package/enqueue_links/shared.js.map +1 -0
  61. package/errors.d.ts +8 -0
  62. package/errors.d.ts.map +1 -0
  63. package/errors.js +12 -0
  64. package/errors.js.map +1 -0
  65. package/events/event_manager.d.ts +41 -0
  66. package/events/event_manager.d.ts.map +1 -0
  67. package/events/event_manager.js +103 -0
  68. package/events/event_manager.js.map +1 -0
  69. package/events/index.d.ts +3 -0
  70. package/events/index.d.ts.map +1 -0
  71. package/events/index.js +6 -0
  72. package/events/index.js.map +1 -0
  73. package/events/local_event_manager.d.ts +32 -0
  74. package/events/local_event_manager.d.ts.map +1 -0
  75. package/events/local_event_manager.js +114 -0
  76. package/events/local_event_manager.js.map +1 -0
  77. package/index.d.ts +14 -0
  78. package/index.d.ts.map +1 -0
  79. package/index.js +17 -0
  80. package/index.js.map +1 -0
  81. package/index.mjs +63 -0
  82. package/log.d.ts +3 -0
  83. package/log.d.ts.map +1 -0
  84. package/log.js +12 -0
  85. package/log.js.map +1 -0
  86. package/package.json +4 -7
  87. package/proxy_configuration.d.ts +181 -0
  88. package/proxy_configuration.d.ts.map +1 -0
  89. package/proxy_configuration.js +209 -0
  90. package/proxy_configuration.js.map +1 -0
  91. package/request.d.ts +174 -0
  92. package/request.d.ts.map +1 -0
  93. package/request.js +270 -0
  94. package/request.js.map +1 -0
  95. package/serialization.d.ts +33 -0
  96. package/serialization.d.ts.map +1 -0
  97. package/serialization.js +146 -0
  98. package/serialization.js.map +1 -0
  99. package/session_pool/errors.d.ts +8 -0
  100. package/session_pool/errors.d.ts.map +1 -0
  101. package/session_pool/errors.js +20 -0
  102. package/session_pool/errors.js.map +1 -0
  103. package/session_pool/events.d.ts +3 -0
  104. package/session_pool/events.d.ts.map +1 -0
  105. package/session_pool/events.js +6 -0
  106. package/session_pool/events.js.map +1 -0
  107. package/session_pool/index.d.ts +6 -0
  108. package/session_pool/index.d.ts.map +1 -0
  109. package/session_pool/index.js +9 -0
  110. package/session_pool/index.js.map +1 -0
  111. package/session_pool/session.d.ts +212 -0
  112. package/session_pool/session.d.ts.map +1 -0
  113. package/session_pool/session.js +360 -0
  114. package/session_pool/session.js.map +1 -0
  115. package/session_pool/session_pool.d.ts +222 -0
  116. package/session_pool/session_pool.d.ts.map +1 -0
  117. package/session_pool/session_pool.js +389 -0
  118. package/session_pool/session_pool.js.map +1 -0
  119. package/session_pool/session_utils.d.ts +9 -0
  120. package/session_pool/session_utils.d.ts.map +1 -0
  121. package/session_pool/session_utils.js +19 -0
  122. package/session_pool/session_utils.js.map +1 -0
  123. package/storages/dataset.d.ts +358 -0
  124. package/storages/dataset.d.ts.map +1 -0
  125. package/storages/dataset.js +415 -0
  126. package/storages/dataset.js.map +1 -0
  127. package/storages/index.d.ts +7 -0
  128. package/storages/index.d.ts.map +1 -0
  129. package/storages/index.js +11 -0
  130. package/storages/index.js.map +1 -0
  131. package/storages/key_value_store.d.ts +242 -0
  132. package/storages/key_value_store.d.ts.map +1 -0
  133. package/storages/key_value_store.js +319 -0
  134. package/storages/key_value_store.js.map +1 -0
  135. package/storages/request_list.d.ts +461 -0
  136. package/storages/request_list.d.ts.map +1 -0
  137. package/storages/request_list.js +715 -0
  138. package/storages/request_list.js.map +1 -0
  139. package/storages/request_queue.d.ts +312 -0
  140. package/storages/request_queue.d.ts.map +1 -0
  141. package/storages/request_queue.js +708 -0
  142. package/storages/request_queue.js.map +1 -0
  143. package/storages/storage.d.ts +260 -0
  144. package/storages/storage.d.ts.map +1 -0
  145. package/storages/storage.js +3 -0
  146. package/storages/storage.js.map +1 -0
  147. package/storages/storage_manager.d.ts +40 -0
  148. package/storages/storage_manager.d.ts.map +1 -0
  149. package/storages/storage_manager.js +119 -0
  150. package/storages/storage_manager.js.map +1 -0
  151. package/tsconfig.build.tsbuildinfo +1 -0
  152. package/typedefs.d.ts +12 -0
  153. package/typedefs.d.ts.map +1 -0
  154. package/typedefs.js +15 -0
  155. package/typedefs.js.map +1 -0
  156. package/validators.d.ts +21 -0
  157. package/validators.d.ts.map +1 -0
  158. package/validators.js +26 -0
  159. package/validators.js.map +1 -0
@@ -0,0 +1,461 @@
1
+ /// <reference types="node" />
2
+ import { Configuration } from '../configuration';
3
+ import { Request, RequestOptions } from '../request';
4
+ /** @internal */
5
+ export declare const STATE_PERSISTENCE_KEY = "REQUEST_LIST_STATE";
6
+ /** @internal */
7
+ export declare const REQUESTS_PERSISTENCE_KEY = "REQUEST_LIST_REQUESTS";
8
+ export interface RequestListOptions {
9
+ /**
10
+ * An array of sources of URLs for the {@link RequestList}. It can be either an array of strings,
11
+ * plain objects that define at least the `url` property, or an array of {@link Request} instances.
12
+ *
13
+ * **IMPORTANT:** The `sources` array will be consumed (left empty) after `RequestList` initializes.
14
+ * This is a measure to prevent memory leaks in situations when millions of sources are
15
+ * added.
16
+ *
17
+ * Additionally, the `requestsFromUrl` property may be used instead of `url`,
18
+ * which will instruct `RequestList` to download the source URLs from a given remote location.
19
+ * The URLs will be parsed from the received response.
20
+ *
21
+ * ```
22
+ * [
23
+ * // A single URL
24
+ * 'http://example.com/a/b',
25
+ *
26
+ * // Modify Request options
27
+ * { method: PUT, 'https://example.com/put, payload: { foo: 'bar' }}
28
+ *
29
+ * // Batch import of URLs from a file hosted on the web,
30
+ * // where the URLs should be requested using the HTTP POST request
31
+ * { method: 'POST', requestsFromUrl: 'http://example.com/urls.txt' },
32
+ *
33
+ * // Batch import from remote file, using a specific regular expression to extract the URLs.
34
+ * { requestsFromUrl: 'http://example.com/urls.txt', regex: /https:\/\/example.com\/.+/ },
35
+ *
36
+ * // Get list of URLs from a Google Sheets document. Just add "/gviz/tq?tqx=out:csv" to the Google Sheet URL.
37
+ * // For details, see https://help.apify.com/en/articles/2906022-scraping-a-list-of-urls-from-a-google-sheets-document
38
+ * { requestsFromUrl: 'https://docs.google.com/spreadsheets/d/1GA5sSQhQjB_REes8I5IKg31S-TuRcznWOPjcpNqtxmU/gviz/tq?tqx=out:csv' }
39
+ * ]
40
+ * ```
41
+ */
42
+ sources?: Source[];
43
+ /**
44
+ * A function that will be called to get the sources for the `RequestList`, but only if `RequestList`
45
+ * was not able to fetch their persisted version (see {@link RequestListOptions.persistRequestsKey}).
46
+ * It must return an `Array` of {@link Request} or {@link RequestOptions}.
47
+ *
48
+ * This is very useful in a scenario when getting the sources is a resource intensive or time consuming
49
+ * task, such as fetching URLs from multiple sitemaps or parsing URLs from large datasets. Using the
50
+ * `sourcesFunction` in combination with `persistStateKey` and `persistRequestsKey` will allow you to
51
+ * fetch and parse those URLs only once, saving valuable time when your actor migrates or restarts.
52
+ *
53
+ * If both {@link RequestListOptions.sources} and {@link RequestListOptions.sourcesFunction} are provided,
54
+ * the sources returned by the function will be added after the `sources`.
55
+ *
56
+ * **Example:**
57
+ * ```javascript
58
+ * // Let's say we want to scrape URLs extracted from sitemaps.
59
+ *
60
+ * const sourcesFunction = async () => {
61
+ * // With super large sitemaps, this operation could take very long
62
+ * // and big websites typically have multiple sitemaps.
63
+ * const sitemaps = await downloadHugeSitemaps();
64
+ * return parseUrlsFromSitemaps(sitemaps);
65
+ * }
66
+ *
67
+ * // Sitemaps can change in real-time, so it's important to persist
68
+ * // the URLs we collected. Otherwise we might lose our scraping
69
+ * // state in case of an actor migration / failure / time-out.
70
+ * const requestList = new RequestList({
71
+ * sourcesFunction,
72
+ * persistStateKey: 'state-key',
73
+ * persistRequestsKey: 'requests-key',
74
+ * })
75
+ *
76
+ * // The sourcesFunction is called now and the Requests are persisted.
77
+ * // If something goes wrong and we need to start again, RequestList
78
+ * // will load the persisted Requests from storage and will NOT
79
+ * // call the sourcesFunction again, saving time and resources.
80
+ * await requestList.initialize();
81
+ * ```
82
+ */
83
+ sourcesFunction?: RequestListSourcesFunction;
84
+ /**
85
+ * Identifies the key in the default key-value store under which `RequestList` periodically stores its
86
+ * state (i.e. which URLs were crawled and which not).
87
+ * If the actor is restarted, `RequestList` will read the state
88
+ * and continue where it left off.
89
+ *
90
+ * If `persistStateKey` is not set, `RequestList` will always start from the beginning,
91
+ * and all the source URLs will be crawled again.
92
+ */
93
+ persistStateKey?: string;
94
+ /**
95
+ * Identifies the key in the default key-value store under which the `RequestList` persists its
96
+ * Requests during the {@link RequestList.initialize} call.
97
+ * This is necessary if `persistStateKey` is set and the source URLs might potentially change,
98
+ * to ensure consistency of the source URLs and state object. However, it comes with some
99
+ * storage and performance overheads.
100
+ *
101
+ * If `persistRequestsKey` is not set, {@link RequestList.initialize} will always fetch the sources
102
+ * from their origin, check that they are consistent with the restored state (if any)
103
+ * and throw an error if they are not.
104
+ */
105
+ persistRequestsKey?: string;
106
+ /**
107
+ * The state object that the `RequestList` will be initialized from.
108
+ * It is in the form as returned by `RequestList.getState()`, such as follows:
109
+ *
110
+ * ```
111
+ * {
112
+ * nextIndex: 5,
113
+ * nextUniqueKey: 'unique-key-5'
114
+ * inProgress: {
115
+ * 'unique-key-1': true,
116
+ * 'unique-key-4': true,
117
+ * },
118
+ * }
119
+ * ```
120
+ *
121
+ * Note that the preferred (and simpler) way to persist the state of crawling of the `RequestList`
122
+ * is to use the `stateKeyPrefix` parameter instead.
123
+ */
124
+ state?: RequestListState;
125
+ /**
126
+ * By default, `RequestList` will deduplicate the provided URLs. Default deduplication is based
127
+ * on the `uniqueKey` property of passed source {@link Request} objects.
128
+ *
129
+ * If the property is not present, it is generated by normalizing the URL. If present, it is kept intact.
130
+ * In any case, only one request per `uniqueKey` is added to the `RequestList` resulting in removal
131
+ * of duplicate URLs / unique keys.
132
+ *
133
+ * Setting `keepDuplicateUrls` to `true` will append an additional identifier to the `uniqueKey`
134
+ * of each request that does not already include a `uniqueKey`. Therefore, duplicate
135
+ * URLs will be kept in the list. It does not protect the user from having duplicates in user set
136
+ * `uniqueKey`s however. It is the user's responsibility to ensure uniqueness of their unique keys
137
+ * if they wish to keep more than just a single copy in the `RequestList`.
138
+ * @default false
139
+ */
140
+ keepDuplicateUrls?: boolean;
141
+ /** @internal */
142
+ config?: Configuration;
143
+ }
144
+ /**
145
+ * Represents a static list of URLs to crawl.
146
+ * The URLs can be provided either in code or parsed from a text file hosted on the web.
147
+ * `RequestList` is used by {@link BasicCrawler}, {@link CheerioCrawler}, {@link PuppeteerCrawler}
148
+ * and {@link PlaywrightCrawler} as a source of URLs to crawl.
149
+ *
150
+ * Each URL is represented using an instance of the {@link Request} class.
151
+ * The list can only contain unique URLs. More precisely, it can only contain `Request` instances
152
+ * with distinct `uniqueKey` properties. By default, `uniqueKey` is generated from the URL, but it can also be overridden.
153
+ * To add a single URL to the list multiple times, corresponding {@link Request} objects will need to have different
154
+ * `uniqueKey` properties. You can use the `keepDuplicateUrls` option to do this for you when initializing the
155
+ * `RequestList` from sources.
156
+ *
157
+ * Once you create an instance of `RequestList`, you need to call the {@link RequestList.initialize} function
158
+ * before the instance can be used. After that, no more URLs can be added to the list.
159
+ * Unlike {@link RequestQueue}, `RequestList` is static but it can contain even millions of URLs.
160
+ * > Note that `RequestList` can be used together with `RequestQueue` by the same crawler.
161
+ * > In such cases, each request from `RequestList` is enqueued into `RequestQueue` first and then consumed from the latter.
162
+ * > This is necessary to avoid the same URL being processed more than once (from the list first and then possibly from the queue).
163
+ * > In practical terms, such a combination can be useful when there is a large number of initial URLs,
164
+ * > but more URLs would be added dynamically by the crawler.
165
+ *
166
+ * `RequestList` has an internal state where it stores information about which requests were already handled,
167
+ * which are in progress and which were reclaimed. The state may be automatically persisted to the default
168
+ * {@link KeyValueStore} by setting the `persistStateKey` option so that if the Node.js process is restarted,
169
+ * the crawling can continue where it left off. The automated persisting is launched upon receiving the `persistState`
170
+ * event that is periodically emitted by {@link events|Actor.events}.
171
+ *
172
+ * The internal state is closely tied to the provided sources (URLs). If the sources change on actor restart, the state will become corrupted and
173
+ * `RequestList` will raise an exception. This typically happens when the sources is a list of URLs downloaded from the web.
174
+ * In such case, use the `persistRequestsKey` option in conjunction with `persistStateKey`,
175
+ * to make the `RequestList` store the initial sources to the default key-value store and load them after restart,
176
+ * which will prevent any issues that a live list of URLs might cause.
177
+ *
178
+ * **Basic usage:**
179
+ * ```javascript
180
+ * // Use a helper function to simplify request list initialization.
181
+ * // State and sources are automatically persisted. This is a preferred usage.
182
+ * const requestList = await RequestList.open('my-request-list', [
183
+ * 'http://www.example.com/page-1',
184
+ * { url: 'http://www.example.com/page-2', method: 'POST', userData: { foo: 'bar' }},
185
+ * { requestsFromUrl: 'http://www.example.com/my-url-list.txt', userData: { isFromUrl: true } },
186
+ * ]);
187
+ * ```
188
+ *
189
+ * **Advanced usage:**
190
+ * ```javascript
191
+ * // Use the constructor to get more control over the initialization.
192
+ * const requestList = new RequestList({
193
+ * sources: [
194
+ * // Separate requests
195
+ * { url: 'http://www.example.com/page-1', method: 'GET', headers: { ... } },
196
+ * { url: 'http://www.example.com/page-2', userData: { foo: 'bar' }},
197
+ *
198
+ * // Bulk load of URLs from file `http://www.example.com/my-url-list.txt`
199
+ * // Note that all URLs must start with http:// or https://
200
+ * { requestsFromUrl: 'http://www.example.com/my-url-list.txt', userData: { isFromUrl: true } },
201
+ * ],
202
+ *
203
+ * // Persist the state to avoid re-crawling which can lead to data duplications.
204
+ * // Keep in mind that the sources have to be immutable or this will throw an error.
205
+ * persistStateKey: 'my-state',
206
+ * });
207
+ *
208
+ * await requestList.initialize();
209
+ * ```
210
+ * @category Sources
211
+ */
212
+ export declare class RequestList {
213
+ private log;
214
+ /**
215
+ * Array of all requests from all sources, in the order as they appeared in sources.
216
+ * All requests in the array have distinct uniqueKey!
217
+ * @internal
218
+ */
219
+ requests: Request[];
220
+ /** Index to the next item in requests array to fetch. All previous requests are either handled or in progress. */
221
+ private nextIndex;
222
+ /** Dictionary, key is Request.uniqueKey, value is corresponding index in the requests array. */
223
+ private uniqueKeyToIndex;
224
+ /**
225
+ * Set of `uniqueKey`s of requests that were returned by fetchNextRequest().
226
+ * @internal
227
+ */
228
+ inProgress: Set<string>;
229
+ /**
230
+ * Set of `uniqueKey`s of requests for which reclaimRequest() was called.
231
+ * @internal
232
+ */
233
+ reclaimed: Set<string>;
234
+ /**
235
+ * Starts as true because until we handle the first request, the list is effectively persisted by doing nothing.
236
+ * @internal
237
+ */
238
+ isStatePersisted: boolean;
239
+ /**
240
+ * Starts as false because we don't know yet and sources might change in the meantime (eg. download from live list).
241
+ * @internal
242
+ */
243
+ areRequestsPersisted: boolean;
244
+ private isLoading;
245
+ private isInitialized;
246
+ private persistStateKey?;
247
+ private persistRequestsKey?;
248
+ private initialState?;
249
+ private store?;
250
+ private keepDuplicateUrls;
251
+ private sources;
252
+ private sourcesFunction?;
253
+ private events;
254
+ /**
255
+ * @param options All `RequestList` configuration options
256
+ */
257
+ constructor(options?: RequestListOptions);
258
+ /**
259
+ * Loads all remote sources of URLs and potentially starts periodic state persistence.
260
+ * This function must be called before you can start using the instance in a meaningful way.
261
+ */
262
+ initialize(): Promise<void>;
263
+ /**
264
+ * Adds previously persisted Requests, as retrieved from the key-value store.
265
+ * This needs to be done in a memory efficient way. We should update the input
266
+ * to a Stream once apify-client supports streams.
267
+ */
268
+ protected _addPersistedRequests(persistedRequests: Buffer): Promise<void>;
269
+ /**
270
+ * Add Requests from both options.sources and options.sourcesFunction.
271
+ * This function is called only when persisted sources were not loaded.
272
+ * We need to avoid keeping both sources and requests in memory
273
+ * to reduce memory footprint with very large sources.
274
+ */
275
+ protected _addRequestsFromSources(): Promise<void>;
276
+ /**
277
+ * Persists the current state of the `RequestList` into the default {@link KeyValueStore}.
278
+ * The state is persisted automatically in regular intervals, but calling this method manually
279
+ * is useful in cases where you want to have the most current state available after you pause
280
+ * or stop fetching its requests. For example after you pause or abort a crawl. Or just before
281
+ * a server migration.
282
+ */
283
+ persistState(): Promise<void>;
284
+ /**
285
+ * Unlike persistState(), this is used only internally, since the sources
286
+ * are automatically persisted at RequestList initialization (if the persistRequestsKey is set),
287
+ * but there's no reason to persist it again afterwards, because RequestList is immutable.
288
+ */
289
+ protected _persistRequests(): Promise<void>;
290
+ /**
291
+ * Restores RequestList state from a state object.
292
+ */
293
+ protected _restoreState(state?: RequestListState): void;
294
+ /**
295
+ * Attempts to load state and requests using the `RequestList` configuration
296
+ * and returns a tuple of [state, requests] where each may be null if not loaded.
297
+ */
298
+ protected _loadStateAndPersistedRequests(): Promise<[RequestListState, Buffer]>;
299
+ /**
300
+ * Returns an object representing the internal state of the `RequestList` instance.
301
+ * Note that the object's fields can change in future releases.
302
+ */
303
+ getState(): RequestListState;
304
+ /**
305
+ * Resolves to `true` if the next call to {@link RequestList.fetchNextRequest} function
306
+ * would return `null`, otherwise it resolves to `false`.
307
+ * Note that even if the list is empty, there might be some pending requests currently being processed.
308
+ */
309
+ isEmpty(): Promise<boolean>;
310
+ /**
311
+ * Returns `true` if all requests were already handled and there are no more left.
312
+ */
313
+ isFinished(): Promise<boolean>;
314
+ /**
315
+ * Gets the next {@link Request} to process. First, the function gets a request previously reclaimed
316
+ * using the {@link RequestList.reclaimRequest} function, if there is any.
317
+ * Otherwise it gets the next request from sources.
318
+ *
319
+ * The function's `Promise` resolves to `null` if there are no more
320
+ * requests to process.
321
+ */
322
+ fetchNextRequest(): Promise<Request | null>;
323
+ /**
324
+ * Marks request as handled after successful processing.
325
+ */
326
+ markRequestHandled(request: Request): Promise<void>;
327
+ /**
328
+ * Reclaims request to the list if its processing failed.
329
+ * The request will become available in the next `this.fetchNextRequest()`.
330
+ */
331
+ reclaimRequest(request: Request): Promise<void>;
332
+ /**
333
+ * Adds all fetched requests from a URL from a remote resource.
334
+ */
335
+ protected _addFetchedRequests(source: InternalSource, fetchedRequests: RequestOptions[]): Promise<void>;
336
+ protected _getPersistedState<T>(key: string): Promise<T>;
337
+ /**
338
+ * Fetches URLs from requestsFromUrl and returns them in format of list of requests
339
+ */
340
+ protected _fetchRequestsFromUrl(source: InternalSource): Promise<RequestOptions[]>;
341
+ /**
342
+ * Adds given request.
343
+ * If the `source` parameter is a string or plain object and not an instance
344
+ * of a `Request`, then the function creates a `Request` instance.
345
+ */
346
+ protected _addRequest(source: Source): void;
347
+ /**
348
+ * Helper function that validates unique key.
349
+ * Throws an error if uniqueKey is not a non-empty string.
350
+ */
351
+ protected _ensureUniqueKeyValid(uniqueKey: string): void;
352
+ /**
353
+ * Checks that request is not reclaimed and throws an error if so.
354
+ */
355
+ protected _ensureInProgressAndNotReclaimed(uniqueKey: string): void;
356
+ /**
357
+ * Throws an error if request list wasn't initialized.
358
+ */
359
+ protected _ensureIsInitialized(): void;
360
+ /**
361
+ * Returns the total number of unique requests present in the `RequestList`.
362
+ */
363
+ length(): number;
364
+ /**
365
+ * Returns number of handled requests.
366
+ */
367
+ handledCount(): number;
368
+ /**
369
+ * Opens a request list and returns a promise resolving to an instance
370
+ * of the {@link RequestList} class that is already initialized.
371
+ *
372
+ * {@link RequestList} represents a list of URLs to crawl, which is always stored in memory.
373
+ * To enable picking up where left off after a process restart, the request list sources
374
+ * are persisted to the key-value store at initialization of the list. Then, while crawling,
375
+ * a small state object is regularly persisted to keep track of the crawling status.
376
+ *
377
+ * For more details and code examples, see the {@link RequestList} class.
378
+ *
379
+ * **Example usage:**
380
+ *
381
+ * ```javascript
382
+ * const sources = [
383
+ * 'https://www.example.com',
384
+ * 'https://www.google.com',
385
+ * 'https://www.bing.com'
386
+ * ];
387
+ *
388
+ * const requestList = await RequestList.open('my-name', sources);
389
+ * ```
390
+ *
391
+ * @param listName
392
+ * Name of the request list to be opened. Setting a name enables the `RequestList`'s state to be persisted
393
+ * in the key-value store. This is useful in case of a restart or migration. Since `RequestList` is only
394
+ * stored in memory, a restart or migration wipes it clean. Setting a name will enable the `RequestList`'s
395
+ * state to survive those situations and continue where it left off.
396
+ *
397
+ * The name will be used as a prefix in key-value store, producing keys such as `NAME-REQUEST_LIST_STATE`
398
+ * and `NAME-REQUEST_LIST_SOURCES`.
399
+ *
400
+ * If `null`, the list will not be persisted and will only be stored in memory. Process restart
401
+ * will then cause the list to be crawled again from the beginning. We suggest always using a name.
402
+ * @param sources
403
+ * An array of sources of URLs for the {@link RequestList}. It can be either an array of strings,
404
+ * plain objects that define at least the `url` property, or an array of {@link Request} instances.
405
+ *
406
+ * **IMPORTANT:** The `sources` array will be consumed (left empty) after {@link RequestList} initializes.
407
+ * This is a measure to prevent memory leaks in situations when millions of sources are
408
+ * added.
409
+ *
410
+ * Additionally, the `requestsFromUrl` property may be used instead of `url`,
411
+ * which will instruct {@link RequestList} to download the source URLs from a given remote location.
412
+ * The URLs will be parsed from the received response. In this case you can limit the URLs
413
+ * using `regex` parameter containing regular expression pattern for URLs to be included.
414
+ *
415
+ * For details, see the {@link RequestListOptions.sources}
416
+ * @param [options]
417
+ * The {@link RequestList} options. Note that the `listName` parameter supersedes
418
+ * the {@link RequestListOptions.persistStateKey} and {@link RequestListOptions.persistRequestsKey}
419
+ * options and the `sources` parameter supersedes the {@link RequestListOptions.sources} option.
420
+ */
421
+ static open(listName: string | null, sources: Source[], options?: RequestListOptions): Promise<RequestList>;
422
+ /**
423
+ * @internal wraps public utility for mocking purposes
424
+ */
425
+ private _downloadListOfUrls;
426
+ }
427
+ /**
428
+ * Represents state of a {@link RequestList}. It can be used to resume a {@link RequestList} which has been previously processed.
429
+ * You can obtain the state by calling {@link RequestList.getState} and receive an object with
430
+ * the following structure:
431
+ *
432
+ * ```
433
+ * {
434
+ * nextIndex: 5,
435
+ * nextUniqueKey: 'unique-key-5'
436
+ * inProgress: {
437
+ * 'unique-key-1': true,
438
+ * 'unique-key-4': true
439
+ * },
440
+ * }
441
+ * ```
442
+ */
443
+ export interface RequestListState {
444
+ /** Position of the next request to be processed. */
445
+ nextIndex: number;
446
+ /** Key of the next request to be processed. */
447
+ nextUniqueKey: string | null;
448
+ /** Array of request keys representing those that being processed at the moment. */
449
+ inProgress: string[];
450
+ }
451
+ export declare type Source = string | (Partial<RequestOptions> & {
452
+ requestsFromUrl?: string;
453
+ regex?: RegExp;
454
+ }) | Request;
455
+ declare type InternalSource = {
456
+ requestsFromUrl: string;
457
+ regex?: RegExp;
458
+ };
459
+ export declare type RequestListSourcesFunction = () => Promise<Source[]>;
460
+ export {};
461
+ //# sourceMappingURL=request_list.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"request_list.d.ts","sourceRoot":"","sources":["../../src/storages/request_list.ts"],"names":[],"mappings":";AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAKrD,gBAAgB;AAChB,eAAO,MAAM,qBAAqB,uBAAuB,CAAC;AAE1D,gBAAgB;AAChB,eAAO,MAAM,wBAAwB,0BAA0B,CAAC;AAIhE,MAAM,WAAW,kBAAkB;IAC/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAgCG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IAEnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAuCG;IACH,eAAe,CAAC,EAAE,0BAA0B,CAAC;IAE7C;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;;;;;;;;;OAUG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B;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