* Adds requests to the queue in batches of 25. This method will wait till all the requests are added * to the queue before resolving. You should prefer using `queue.addRequestsBatched()` or `crawler.addRequests()` * if you don't want to block the processing, as those methods will only wait
(
requestsLike: RequestsLike,
options: RequestQueueOperationOptions = {},
)
| 273 | * @param [options] Request queue operation options. |
| 274 | */ |
| 275 | async addRequests( |
| 276 | requestsLike: RequestsLike, |
| 277 | options: RequestQueueOperationOptions = {}, |
| 278 | ): Promise<BatchAddRequestsResult> { |
| 279 | checkStorageAccess(); |
| 280 | |
| 281 | this.lastActivity = new Date(); |
| 282 | |
| 283 | ow( |
| 284 | requestsLike, |
| 285 | ow.object |
| 286 | .is((value: unknown) => isIterable(value) || isAsyncIterable(value)) |
| 287 | .message((value) => `Expected an iterable or async iterable, got ${getObjectType(value)}`), |
| 288 | ); |
| 289 | ow( |
| 290 | options, |
| 291 | ow.object.exactShape({ |
| 292 | forefront: ow.optional.boolean, |
| 293 | cache: ow.optional.boolean, |
| 294 | }), |
| 295 | ); |
| 296 | |
| 297 | const { forefront = false, cache = true } = options; |
| 298 | |
| 299 | const uniqueKeyToCacheKey = new Map<string, string>(); |
| 300 | const getCachedRequestId = (uniqueKey: string) => { |
| 301 | const cached = uniqueKeyToCacheKey.get(uniqueKey); |
| 302 | |
| 303 | if (cached) return cached; |
| 304 | |
| 305 | const newCacheKey = getRequestId(uniqueKey); |
| 306 | uniqueKeyToCacheKey.set(uniqueKey, newCacheKey); |
| 307 | |
| 308 | return newCacheKey; |
| 309 | }; |
| 310 | |
| 311 | const results: BatchAddRequestsResult = { |
| 312 | processedRequests: [], |
| 313 | unprocessedRequests: [], |
| 314 | }; |
| 315 | |
| 316 | const requests: Request<Dictionary>[] = []; |
| 317 | |
| 318 | for await (const requestLike of requestsLike) { |
| 319 | if (typeof requestLike === 'string') { |
| 320 | requests.push(new Request({ url: requestLike })); |
| 321 | } else if ('requestsFromUrl' in requestLike) { |
| 322 | const fetchedRequests = await this._fetchRequestsFromUrl(requestLike as InternalSource); |
| 323 | await this._addFetchedRequests(requestLike as InternalSource, fetchedRequests, options); |
| 324 | } else { |
| 325 | requests.push( |
| 326 | requestLike instanceof Request ? requestLike : new Request(requestLike as RequestOptions), |
| 327 | ); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | const requestsToAdd = new Map<string, Request>(); |
| 332 |
searching dependent graphs…