@crawlee/core 4.0.0-beta.13 → 4.0.0-beta.130
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -13
- package/autoscaling/autoscaled_pool.d.ts +67 -172
- package/autoscaling/autoscaled_pool.js +182 -329
- package/autoscaling/concurrency_system.d.ts +268 -0
- package/autoscaling/concurrency_system.js +362 -0
- package/autoscaling/cpu_load_signal.d.ts +43 -0
- package/autoscaling/cpu_load_signal.js +47 -0
- package/autoscaling/event_loop_load_signal.d.ts +51 -0
- package/autoscaling/event_loop_load_signal.js +60 -0
- package/autoscaling/index.d.ts +6 -1
- package/autoscaling/index.js +6 -1
- package/autoscaling/load_signal.d.ts +100 -0
- package/autoscaling/load_signal.js +105 -0
- package/autoscaling/memory_load_signal.d.ts +47 -0
- package/autoscaling/memory_load_signal.js +106 -0
- package/autoscaling/snapshotter.d.ts +58 -163
- package/autoscaling/snapshotter.js +45 -263
- package/autoscaling/storage_backend_load_signal.d.ts +56 -0
- package/autoscaling/storage_backend_load_signal.js +73 -0
- package/autoscaling/system_status.d.ts +67 -89
- package/autoscaling/system_status.js +92 -122
- package/autoscaling/weighted_avg.d.ts +5 -0
- package/autoscaling/weighted_avg.js +14 -0
- package/byte_utils.d.ts +17 -0
- package/byte_utils.js +42 -0
- package/configuration.d.ts +96 -223
- package/configuration.js +170 -222
- package/cookie_utils.d.ts +3 -2
- package/cookie_utils.js +18 -7
- package/crawlers/context_pipeline.d.ts +10 -1
- package/crawlers/context_pipeline.js +31 -8
- package/crawlers/crawler_commons.d.ts +90 -129
- package/crawlers/crawler_commons.js +1 -116
- package/crawlers/error_snapshotter.d.ts +2 -5
- package/crawlers/error_snapshotter.js +7 -8
- package/crawlers/error_tracker.d.ts +0 -1
- package/crawlers/error_tracker.js +0 -1
- package/crawlers/index.d.ts +1 -3
- package/crawlers/index.js +0 -3
- package/crawlers/internals/types.d.ts +0 -1
- package/crawlers/internals/types.js +0 -1
- package/crawlers/statistics.d.ts +187 -63
- package/crawlers/statistics.js +354 -164
- package/debug.d.ts +36 -0
- package/debug.js +70 -0
- package/enqueue_links/enqueue_links.d.ts +68 -154
- package/enqueue_links/enqueue_links.js +38 -210
- package/enqueue_links/index.d.ts +0 -1
- package/enqueue_links/index.js +0 -1
- package/enqueue_links/shared.d.ts +52 -26
- package/enqueue_links/shared.js +115 -66
- package/errors.d.ts +72 -4
- package/errors.js +89 -5
- package/events/event_manager.d.ts +35 -9
- package/events/event_manager.js +12 -13
- package/events/index.d.ts +0 -1
- package/events/index.js +0 -1
- package/events/local_event_manager.d.ts +15 -3
- package/events/local_event_manager.js +39 -13
- package/http.d.ts +9 -0
- package/http.js +28 -0
- package/index.d.ts +7 -4
- package/index.js +6 -3
- package/iterables.d.ts +79 -0
- package/iterables.js +134 -0
- package/log.d.ts +82 -3
- package/log.js +106 -1
- package/memory-storage/consts.d.ts +4 -0
- package/memory-storage/consts.js +4 -0
- package/memory-storage/index.d.ts +1 -0
- package/memory-storage/index.js +1 -0
- package/memory-storage/memory-storage.d.ts +38 -0
- package/memory-storage/memory-storage.js +130 -0
- package/memory-storage/resource-clients/common/base-client.d.ts +4 -0
- package/memory-storage/resource-clients/common/base-client.js +6 -0
- package/memory-storage/resource-clients/dataset.d.ts +40 -0
- package/memory-storage/resource-clients/dataset.js +108 -0
- package/memory-storage/resource-clients/key-value-store.d.ts +63 -0
- package/memory-storage/resource-clients/key-value-store.js +201 -0
- package/memory-storage/resource-clients/request-queue.d.ts +77 -0
- package/memory-storage/resource-clients/request-queue.js +409 -0
- package/memory-storage/utils.d.ts +16 -0
- package/memory-storage/utils.js +41 -0
- package/owned_or_injected.d.ts +58 -0
- package/owned_or_injected.js +98 -0
- package/package.json +14 -13
- package/proxy_configuration.d.ts +24 -132
- package/proxy_configuration.js +32 -147
- package/recoverable_state.d.ts +155 -0
- package/recoverable_state.js +231 -0
- package/request.d.ts +80 -16
- package/request.js +147 -68
- package/router.d.ts +193 -21
- package/router.js +188 -43
- package/serialization.d.ts +0 -1
- package/serialization.js +15 -15
- package/service_locator.d.ts +165 -0
- package/service_locator.js +253 -0
- package/session_pool/consts.d.ts +1 -2
- package/session_pool/consts.js +1 -2
- package/session_pool/errors.d.ts +0 -1
- package/session_pool/errors.js +0 -1
- package/session_pool/fingerprint.d.ts +9 -0
- package/session_pool/fingerprint.js +30 -0
- package/session_pool/index.d.ts +0 -2
- package/session_pool/index.js +0 -2
- package/session_pool/session.d.ts +35 -88
- package/session_pool/session.js +101 -159
- package/session_pool/session_pool.d.ts +73 -91
- package/session_pool/session_pool.js +172 -165
- package/storages/batched_adds.d.ts +37 -0
- package/storages/batched_adds.js +73 -0
- package/storages/dataset.d.ts +114 -54
- package/storages/dataset.js +291 -148
- package/storages/index.d.ts +10 -8
- package/storages/index.js +8 -8
- package/storages/key_value_store.d.ts +185 -42
- package/storages/key_value_store.js +445 -169
- package/storages/key_value_store_codec.d.ts +32 -0
- package/storages/key_value_store_codec.js +113 -0
- package/storages/request_dedup_cache.d.ts +22 -0
- package/storages/request_dedup_cache.js +48 -0
- package/storages/request_list.d.ts +52 -115
- package/storages/request_list.js +183 -152
- package/storages/request_loader.d.ts +101 -0
- package/storages/request_loader.js +1 -0
- package/storages/request_manager.d.ts +33 -0
- package/storages/request_manager.js +1 -0
- package/storages/request_manager_tandem.d.ts +97 -0
- package/storages/request_manager_tandem.js +197 -0
- package/storages/request_queue.d.ts +290 -47
- package/storages/request_queue.js +762 -216
- package/storages/{sitemap_request_list.d.ts → sitemap_request_loader.d.ts} +45 -89
- package/storages/sitemap_request_loader.js +438 -0
- package/storages/storage_instance_manager.d.ts +87 -0
- package/storages/storage_instance_manager.js +256 -0
- package/storages/storage_stats.d.ts +48 -0
- package/storages/storage_stats.js +29 -0
- package/storages/throttling_request_manager.d.ts +239 -0
- package/storages/throttling_request_manager.js +646 -0
- package/storages/transaction.d.ts +252 -0
- package/storages/transaction.js +251 -0
- package/storages/utils.d.ts +59 -11
- package/storages/utils.js +75 -15
- package/system-info/cpu-info.d.ts +67 -0
- package/system-info/cpu-info.js +216 -0
- package/system-info/memory-info.d.ts +31 -0
- package/system-info/memory-info.js +115 -0
- package/system-info/ps-tree.d.ts +17 -0
- package/system-info/ps-tree.js +144 -0
- package/system-info/runtime.d.ts +14 -0
- package/system-info/runtime.js +80 -0
- package/typedefs.d.ts +0 -6
- package/typedefs.js +0 -1
- package/url.d.ts +9 -0
- package/url.js +11 -0
- package/validators.d.ts +22 -18
- package/validators.js +13 -18
- package/autoscaling/autoscaled_pool.d.ts.map +0 -1
- package/autoscaling/autoscaled_pool.js.map +0 -1
- package/autoscaling/index.d.ts.map +0 -1
- package/autoscaling/index.js.map +0 -1
- package/autoscaling/snapshotter.d.ts.map +0 -1
- package/autoscaling/snapshotter.js.map +0 -1
- package/autoscaling/system_status.d.ts.map +0 -1
- package/autoscaling/system_status.js.map +0 -1
- package/configuration.d.ts.map +0 -1
- package/configuration.js.map +0 -1
- package/cookie_utils.d.ts.map +0 -1
- package/cookie_utils.js.map +0 -1
- package/crawlers/context_pipeline.d.ts.map +0 -1
- package/crawlers/context_pipeline.js.map +0 -1
- package/crawlers/crawler_commons.d.ts.map +0 -1
- package/crawlers/crawler_commons.js.map +0 -1
- package/crawlers/crawler_utils.d.ts +0 -10
- package/crawlers/crawler_utils.d.ts.map +0 -1
- package/crawlers/crawler_utils.js +0 -12
- package/crawlers/crawler_utils.js.map +0 -1
- package/crawlers/error_snapshotter.d.ts.map +0 -1
- package/crawlers/error_snapshotter.js.map +0 -1
- package/crawlers/error_tracker.d.ts.map +0 -1
- package/crawlers/error_tracker.js.map +0 -1
- package/crawlers/index.d.ts.map +0 -1
- package/crawlers/index.js.map +0 -1
- package/crawlers/internals/types.d.ts.map +0 -1
- package/crawlers/internals/types.js.map +0 -1
- package/crawlers/statistics.d.ts.map +0 -1
- package/crawlers/statistics.js.map +0 -1
- package/enqueue_links/enqueue_links.d.ts.map +0 -1
- package/enqueue_links/enqueue_links.js.map +0 -1
- package/enqueue_links/index.d.ts.map +0 -1
- package/enqueue_links/index.js.map +0 -1
- package/enqueue_links/shared.d.ts.map +0 -1
- package/enqueue_links/shared.js.map +0 -1
- package/errors.d.ts.map +0 -1
- package/errors.js.map +0 -1
- package/events/event_manager.d.ts.map +0 -1
- package/events/event_manager.js.map +0 -1
- package/events/index.d.ts.map +0 -1
- package/events/index.js.map +0 -1
- package/events/local_event_manager.d.ts.map +0 -1
- package/events/local_event_manager.js.map +0 -1
- package/http_clients/base-http-client.d.ts +0 -140
- package/http_clients/base-http-client.d.ts.map +0 -1
- package/http_clients/base-http-client.js +0 -40
- package/http_clients/base-http-client.js.map +0 -1
- package/http_clients/form-data-like.d.ts +0 -67
- package/http_clients/form-data-like.d.ts.map +0 -1
- package/http_clients/form-data-like.js +0 -5
- package/http_clients/form-data-like.js.map +0 -1
- package/http_clients/got-scraping-http-client.d.ts +0 -20
- package/http_clients/got-scraping-http-client.d.ts.map +0 -1
- package/http_clients/got-scraping-http-client.js +0 -82
- package/http_clients/got-scraping-http-client.js.map +0 -1
- package/http_clients/index.d.ts +0 -3
- package/http_clients/index.d.ts.map +0 -1
- package/http_clients/index.js +0 -3
- package/http_clients/index.js.map +0 -1
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/log.d.ts.map +0 -1
- package/log.js.map +0 -1
- package/proxy_configuration.d.ts.map +0 -1
- package/proxy_configuration.js.map +0 -1
- package/request.d.ts.map +0 -1
- package/request.js.map +0 -1
- package/router.d.ts.map +0 -1
- package/router.js.map +0 -1
- package/serialization.d.ts.map +0 -1
- package/serialization.js.map +0 -1
- package/session_pool/consts.d.ts.map +0 -1
- package/session_pool/consts.js.map +0 -1
- package/session_pool/errors.d.ts.map +0 -1
- package/session_pool/errors.js.map +0 -1
- package/session_pool/events.d.ts +0 -3
- package/session_pool/events.d.ts.map +0 -1
- package/session_pool/events.js +0 -3
- package/session_pool/events.js.map +0 -1
- package/session_pool/index.d.ts.map +0 -1
- package/session_pool/index.js.map +0 -1
- package/session_pool/session.d.ts.map +0 -1
- package/session_pool/session.js.map +0 -1
- package/session_pool/session_pool.d.ts.map +0 -1
- package/session_pool/session_pool.js.map +0 -1
- package/storages/access_checking.d.ts +0 -13
- package/storages/access_checking.d.ts.map +0 -1
- package/storages/access_checking.js +0 -14
- package/storages/access_checking.js.map +0 -1
- package/storages/dataset.d.ts.map +0 -1
- package/storages/dataset.js.map +0 -1
- package/storages/index.d.ts.map +0 -1
- package/storages/index.js.map +0 -1
- package/storages/key_value_store.d.ts.map +0 -1
- package/storages/key_value_store.js.map +0 -1
- package/storages/request_list.d.ts.map +0 -1
- package/storages/request_list.js.map +0 -1
- package/storages/request_provider.d.ts +0 -308
- package/storages/request_provider.d.ts.map +0 -1
- package/storages/request_provider.js +0 -555
- package/storages/request_provider.js.map +0 -1
- package/storages/request_queue.d.ts.map +0 -1
- package/storages/request_queue.js.map +0 -1
- package/storages/request_queue_v2.d.ts +0 -87
- package/storages/request_queue_v2.d.ts.map +0 -1
- package/storages/request_queue_v2.js +0 -438
- package/storages/request_queue_v2.js.map +0 -1
- package/storages/sitemap_request_list.d.ts.map +0 -1
- package/storages/sitemap_request_list.js +0 -430
- package/storages/sitemap_request_list.js.map +0 -1
- package/storages/storage_manager.d.ts +0 -58
- package/storages/storage_manager.d.ts.map +0 -1
- package/storages/storage_manager.js +0 -105
- package/storages/storage_manager.js.map +0 -1
- package/storages/utils.d.ts.map +0 -1
- package/storages/utils.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
- package/typedefs.d.ts.map +0 -1
- package/typedefs.js.map +0 -1
- package/validators.d.ts.map +0 -1
- package/validators.js.map +0 -1
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
|
+
}
|
|
@@ -1,97 +1,76 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import type { Dictionary } from '@crawlee/types';
|
|
2
|
+
import { EnqueueStrategy } from '@crawlee/utils';
|
|
3
|
+
import type { RequestQueueOperationOptions } from '../storages/request_queue.js';
|
|
4
|
+
import type { RequestTransform, SkippedRequestCallback, UrlPatternInput, UrlPatternObject } from './shared.js';
|
|
5
|
+
/**
|
|
6
|
+
* Options shared by the `extractLinks()` context helper across crawler types.
|
|
7
|
+
*/
|
|
8
|
+
export interface ExtractLinksOptions {
|
|
9
|
+
/** A CSS selector matching links to be extracted. */
|
|
10
|
+
selector?: string;
|
|
11
|
+
/**
|
|
12
|
+
* A base URL that will be used to resolve relative URLs when using Cheerio. Ignored when using Puppeteer,
|
|
13
|
+
* since the relative URL resolution is done inside the browser automatically.
|
|
14
|
+
*/
|
|
15
|
+
baseUrl?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Options accepted by the `enqueueUrls()` context helper exposed by `BasicCrawler`.
|
|
19
|
+
*/
|
|
20
|
+
export interface EnqueueUrlsOptions extends RequestQueueOperationOptions {
|
|
11
21
|
/** Limit the amount of actually enqueued URLs to this number. Useful for testing across the entire crawling scope. */
|
|
12
22
|
limit?: number;
|
|
13
|
-
/** An array of URLs to enqueue. */
|
|
14
|
-
urls?: readonly string[];
|
|
15
|
-
/** A request queue to which the URLs will be enqueued. */
|
|
16
|
-
requestQueue?: RequestProvider;
|
|
17
|
-
/** A CSS selector matching links to be enqueued. */
|
|
18
|
-
selector?: string;
|
|
19
23
|
/** Sets {@link Request.userData} for newly enqueued requests. */
|
|
20
24
|
userData?: Dictionary;
|
|
21
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* Sets {@link Request.label} for newly enqueued requests.
|
|
27
|
+
*
|
|
28
|
+
* Can be overwritten by `transformRequestFunction`.
|
|
29
|
+
*/
|
|
22
30
|
label?: string;
|
|
31
|
+
/** Sets {@link Request.sessionId} for newly enqueued requests. */
|
|
32
|
+
sessionId?: string;
|
|
23
33
|
/**
|
|
24
34
|
* If set to `true`, tells the crawler to skip navigation and process the request directly.
|
|
25
35
|
* @default false
|
|
26
36
|
*/
|
|
27
37
|
skipNavigation?: boolean;
|
|
28
38
|
/**
|
|
29
|
-
* A base URL that will be used to resolve relative URLs
|
|
30
|
-
* since the relative URL resolution is done inside the browser automatically.
|
|
39
|
+
* A base URL that will be used to resolve relative URLs.
|
|
31
40
|
*/
|
|
32
41
|
baseUrl?: string;
|
|
33
42
|
/**
|
|
34
|
-
* An array of
|
|
35
|
-
* containing glob pattern strings matching the URLs to be enqueued.
|
|
36
|
-
*
|
|
37
|
-
* The plain objects must include at least the `glob` property, which holds the glob pattern string.
|
|
38
|
-
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
39
|
-
*
|
|
40
|
-
* The matching is always case-insensitive.
|
|
41
|
-
* If you need case-sensitive matching, use `regexps` property directly.
|
|
42
|
-
*
|
|
43
|
-
* If `globs` is an empty array or `undefined`, and `regexps` are also not defined, then the function
|
|
44
|
-
* enqueues the links with the same subdomain.
|
|
45
|
-
*/
|
|
46
|
-
globs?: readonly GlobInput[];
|
|
47
|
-
/**
|
|
48
|
-
* An array of glob pattern strings, regexp patterns or plain objects
|
|
49
|
-
* containing patterns matching URLs that will **never** be enqueued.
|
|
43
|
+
* An array of URL patterns that URLs must match to be enqueued.
|
|
50
44
|
*
|
|
51
|
-
*
|
|
52
|
-
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
45
|
+
* Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
|
|
53
46
|
*
|
|
54
47
|
* Glob matching is always case-insensitive.
|
|
55
|
-
* If you need case-sensitive matching,
|
|
56
|
-
*/
|
|
57
|
-
exclude?: readonly (GlobInput | RegExpInput)[];
|
|
58
|
-
/**
|
|
59
|
-
* An array of regular expressions or plain objects
|
|
60
|
-
* containing regular expressions matching the URLs to be enqueued.
|
|
48
|
+
* If you need case-sensitive matching, use a `RegExp`.
|
|
61
49
|
*
|
|
62
|
-
* The
|
|
63
|
-
*
|
|
50
|
+
* The patterns are combined with the {@link EnqueueUrlsOptions.strategy|`strategy`} using AND logic - a URL
|
|
51
|
+
* must match at least one `include` pattern **and** satisfy the strategy to be enqueued. To match URLs across
|
|
52
|
+
* hostnames, pass an explicit {@link EnqueueStrategy.All} strategy.
|
|
64
53
|
*
|
|
65
|
-
* If `
|
|
66
|
-
*
|
|
54
|
+
* If `undefined`, the links are enqueued based on the {@link EnqueueUrlsOptions.strategy|`strategy`} alone.
|
|
55
|
+
* Passing an empty array is not allowed.
|
|
67
56
|
*/
|
|
68
|
-
|
|
57
|
+
include?: readonly UrlPatternInput[];
|
|
69
58
|
/**
|
|
70
|
-
*
|
|
71
|
-
* Please use `globs` or `regexps` instead.
|
|
72
|
-
*
|
|
73
|
-
* An array of {@link PseudoUrl} strings or plain objects
|
|
74
|
-
* containing {@link PseudoUrl} strings matching the URLs to be enqueued.
|
|
59
|
+
* An array of URL patterns. Matching URLs will **not** be enqueued.
|
|
75
60
|
*
|
|
76
|
-
*
|
|
77
|
-
* All remaining keys will be used as request options for the corresponding enqueued {@link Request} objects.
|
|
61
|
+
* Accepts glob pattern strings, `{ glob: string }` objects, `RegExp` instances, or `{ regexp: RegExp }` objects.
|
|
78
62
|
*
|
|
79
|
-
*
|
|
80
|
-
* If you need case-sensitive matching, use `
|
|
81
|
-
*
|
|
82
|
-
* If `pseudoUrls` is an empty array or `undefined`, then the function
|
|
83
|
-
* enqueues the links with the same subdomain.
|
|
84
|
-
*
|
|
85
|
-
* @deprecated prefer using `globs` or `regexps` instead
|
|
63
|
+
* Glob matching is always case-insensitive.
|
|
64
|
+
* If you need case-sensitive matching, use a `RegExp`.
|
|
86
65
|
*/
|
|
87
|
-
|
|
66
|
+
exclude?: readonly UrlPatternInput[];
|
|
88
67
|
/**
|
|
89
|
-
*
|
|
90
|
-
* to remove
|
|
68
|
+
* After request options are filtered by `include`/`exclude` patterns, this function can be used
|
|
69
|
+
* to remove them or modify their contents such as `userData`, `payload` or, most importantly `uniqueKey`. This is useful
|
|
91
70
|
* when you need to enqueue multiple `Requests` to the queue that share the same URL, but differ in methods or payloads,
|
|
92
71
|
* or to dynamically update or create `userData`.
|
|
93
72
|
*
|
|
94
|
-
* For example: by adding `keepUrlFragment: true` to the
|
|
73
|
+
* For example: by adding `keepUrlFragment: true` to the request options, URL fragments will not be removed
|
|
95
74
|
* when `uniqueKey` is computed.
|
|
96
75
|
*
|
|
97
76
|
* **Example:**
|
|
@@ -105,9 +84,13 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
|
|
|
105
84
|
* }
|
|
106
85
|
* ```
|
|
107
86
|
*
|
|
108
|
-
* Note that `transformRequestFunction` has
|
|
109
|
-
*
|
|
110
|
-
*
|
|
87
|
+
* Note that `transformRequestFunction` has the highest priority and can overwrite
|
|
88
|
+
* the global `label` option.
|
|
89
|
+
*
|
|
90
|
+
* The function receives a {@link RequestOptions} object and can return either:
|
|
91
|
+
* - The modified {@link RequestOptions} object
|
|
92
|
+
* - `'unchanged'` to keep the original options as-is
|
|
93
|
+
* - A falsy value or `'skip'` to exclude the request from the queue
|
|
111
94
|
*/
|
|
112
95
|
transformRequestFunction?: RequestTransform;
|
|
113
96
|
/**
|
|
@@ -128,99 +111,26 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
|
|
|
128
111
|
*
|
|
129
112
|
* @default EnqueueStrategy.SameHostname
|
|
130
113
|
*/
|
|
131
|
-
strategy?:
|
|
114
|
+
strategy?: EnqueueStrategyOption;
|
|
132
115
|
/**
|
|
133
116
|
* By default, only the first batch (1000) of found requests will be added to the queue before resolving the call.
|
|
134
117
|
* You can use this option to wait for adding all of them.
|
|
135
118
|
*/
|
|
136
119
|
waitForAllRequestsToBeAdded?: boolean;
|
|
137
|
-
/**
|
|
138
|
-
* RobotsTxtFile instance for the current request that triggered the `enqueueLinks`.
|
|
139
|
-
* If provided, disallowed URLs will be ignored.
|
|
140
|
-
*/
|
|
141
|
-
robotsTxtFile?: RobotsTxtFile;
|
|
142
120
|
/**
|
|
143
121
|
* When a request is skipped for some reason, you can use this callback to act on it.
|
|
144
|
-
* This is currently fired
|
|
122
|
+
* This is currently fired for requests skipped
|
|
123
|
+
* 1. based on robots.txt file,
|
|
124
|
+
* 2. because they don't match enqueueLinks filters,
|
|
125
|
+
* 3. or because the maxRequestsPerCrawl limit has been reached
|
|
145
126
|
*/
|
|
146
127
|
onSkippedRequest?: SkippedRequestCallback;
|
|
147
128
|
}
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
* ```md
|
|
154
|
-
* Protocol Domain
|
|
155
|
-
* ┌────┐ ┌─────────┐
|
|
156
|
-
* https://example.crawlee.dev/...
|
|
157
|
-
* │ └─────────────────┤
|
|
158
|
-
* │ Hostname │
|
|
159
|
-
* │ │
|
|
160
|
-
* └─────────────────────────┘
|
|
161
|
-
* Origin
|
|
162
|
-
*```
|
|
163
|
-
*
|
|
164
|
-
* - The `Protocol` is usually `http` or `https`
|
|
165
|
-
* - The `Domain` represents the path without any possible subdomains to a website. For example, `crawlee.dev` is the domain of `https://example.crawlee.dev/`
|
|
166
|
-
* - The `Hostname` is the full path to a website, including any subdomains. For example, `example.crawlee.dev` is the hostname of `https://example.crawlee.dev/`
|
|
167
|
-
* - The `Origin` is the combination of the `Protocol` and `Hostname`. For example, `https://example.crawlee.dev` is the origin of `https://example.crawlee.dev/`
|
|
168
|
-
*/
|
|
169
|
-
export declare enum EnqueueStrategy {
|
|
170
|
-
/**
|
|
171
|
-
* Matches any URLs found
|
|
172
|
-
*/
|
|
173
|
-
All = "all",
|
|
174
|
-
/**
|
|
175
|
-
* Matches any URLs that have the same hostname.
|
|
176
|
-
* For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
|
|
177
|
-
* `https://example.com/hello` will not be matched.
|
|
178
|
-
*
|
|
179
|
-
* > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
|
|
180
|
-
*/
|
|
181
|
-
SameHostname = "same-hostname",
|
|
182
|
-
/**
|
|
183
|
-
* Matches any URLs that have the same domain as the base URL.
|
|
184
|
-
* For example, `https://wow.an.example.com` and `https://example.com` will both be matched for a base url of
|
|
185
|
-
* `https://example.com`.
|
|
186
|
-
*
|
|
187
|
-
* > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
|
|
188
|
-
*/
|
|
189
|
-
SameDomain = "same-domain",
|
|
190
|
-
/**
|
|
191
|
-
* Matches any URLs that have the same hostname and protocol.
|
|
192
|
-
* For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
|
|
193
|
-
* `http://wow.example.com/hello` will not be matched.
|
|
194
|
-
*
|
|
195
|
-
* > This strategy will ensure the protocol of the base URL is the same as the protocol of the URL to be enqueued.
|
|
196
|
-
*/
|
|
197
|
-
SameOrigin = "same-origin"
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
|
|
201
|
-
* you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
|
|
202
|
-
*
|
|
203
|
-
* Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
|
|
204
|
-
* and override settings of the enqueued {@link Request} objects.
|
|
205
|
-
*
|
|
206
|
-
* **Example usage**
|
|
207
|
-
*
|
|
208
|
-
* ```javascript
|
|
209
|
-
* await enqueueLinks({
|
|
210
|
-
* urls: aListOfFoundUrls,
|
|
211
|
-
* requestQueue,
|
|
212
|
-
* selector: 'a.product-detail',
|
|
213
|
-
* globs: [
|
|
214
|
-
* 'https://www.example.com/handbags/*',
|
|
215
|
-
* 'https://www.example.com/purses/*'
|
|
216
|
-
* ],
|
|
217
|
-
* });
|
|
218
|
-
* ```
|
|
219
|
-
*
|
|
220
|
-
* @param options All `enqueueLinks()` parameters are passed via an options object.
|
|
221
|
-
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
222
|
-
*/
|
|
223
|
-
export declare function enqueueLinks(options: SetRequired<EnqueueLinksOptions, 'requestQueue' | 'urls'>): Promise<BatchAddRequestsResult>;
|
|
129
|
+
/** The combined options accepted by a crawler context's `enqueueLinks()` helper: `extractLinks()` + `enqueueUrls()`. */
|
|
130
|
+
export type EnqueueLinksOptions = ExtractLinksOptions & EnqueueUrlsOptions;
|
|
131
|
+
export { EnqueueStrategy };
|
|
132
|
+
/** The `strategy` option accepted by {@link ExtractLinksOptions} and {@link EnqueueUrlsOptions}. */
|
|
133
|
+
export type EnqueueStrategyOption = EnqueueStrategy | 'all' | 'same-domain' | 'same-hostname' | 'same-origin';
|
|
224
134
|
/**
|
|
225
135
|
* @internal
|
|
226
136
|
* This method helps resolve the baseUrl that will be used for filtering in {@link enqueueLinks}.
|
|
@@ -235,8 +145,12 @@ export declare function resolveBaseUrlForEnqueueLinksFiltering({ enqueueStrategy
|
|
|
235
145
|
*/
|
|
236
146
|
export interface ResolveBaseUrl {
|
|
237
147
|
userProvidedBaseUrl?: string;
|
|
238
|
-
enqueueStrategy?:
|
|
148
|
+
enqueueStrategy?: EnqueueStrategyOption;
|
|
239
149
|
originalRequestUrl: string;
|
|
240
150
|
finalRequestUrl?: string;
|
|
241
151
|
}
|
|
242
|
-
|
|
152
|
+
/**
|
|
153
|
+
* @internal
|
|
154
|
+
* Builds the glob patterns a URL must match to satisfy the given enqueue `strategy`, anchored at `baseUrl`.
|
|
155
|
+
*/
|
|
156
|
+
export declare function buildEnqueueStrategyPatterns(baseUrl: string, strategy: EnqueueStrategyOption): UrlPatternObject[];
|
|
@@ -1,212 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { EnqueueStrategy } from '@crawlee/utils';
|
|
2
2
|
import { getDomain } from 'tldts';
|
|
3
|
-
|
|
4
|
-
import { constructGlobObjectsFromGlobs, constructRegExpObjectsFromPseudoUrls, constructRegExpObjectsFromRegExps, createRequestOptions, createRequests, filterRequestsByPatterns, } from './shared.js';
|
|
5
|
-
/**
|
|
6
|
-
* The different enqueueing strategies available.
|
|
7
|
-
*
|
|
8
|
-
* Depending on the strategy you select, we will only check certain parts of the URLs found. Here is a diagram of each URL part and their name:
|
|
9
|
-
*
|
|
10
|
-
* ```md
|
|
11
|
-
* Protocol Domain
|
|
12
|
-
* ┌────┐ ┌─────────┐
|
|
13
|
-
* https://example.crawlee.dev/...
|
|
14
|
-
* │ └─────────────────┤
|
|
15
|
-
* │ Hostname │
|
|
16
|
-
* │ │
|
|
17
|
-
* └─────────────────────────┘
|
|
18
|
-
* Origin
|
|
19
|
-
*```
|
|
20
|
-
*
|
|
21
|
-
* - The `Protocol` is usually `http` or `https`
|
|
22
|
-
* - The `Domain` represents the path without any possible subdomains to a website. For example, `crawlee.dev` is the domain of `https://example.crawlee.dev/`
|
|
23
|
-
* - The `Hostname` is the full path to a website, including any subdomains. For example, `example.crawlee.dev` is the hostname of `https://example.crawlee.dev/`
|
|
24
|
-
* - The `Origin` is the combination of the `Protocol` and `Hostname`. For example, `https://example.crawlee.dev` is the origin of `https://example.crawlee.dev/`
|
|
25
|
-
*/
|
|
26
|
-
export var EnqueueStrategy;
|
|
27
|
-
(function (EnqueueStrategy) {
|
|
28
|
-
/**
|
|
29
|
-
* Matches any URLs found
|
|
30
|
-
*/
|
|
31
|
-
EnqueueStrategy["All"] = "all";
|
|
32
|
-
/**
|
|
33
|
-
* Matches any URLs that have the same hostname.
|
|
34
|
-
* For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
|
|
35
|
-
* `https://example.com/hello` will not be matched.
|
|
36
|
-
*
|
|
37
|
-
* > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
|
|
38
|
-
*/
|
|
39
|
-
EnqueueStrategy["SameHostname"] = "same-hostname";
|
|
40
|
-
/**
|
|
41
|
-
* Matches any URLs that have the same domain as the base URL.
|
|
42
|
-
* For example, `https://wow.an.example.com` and `https://example.com` will both be matched for a base url of
|
|
43
|
-
* `https://example.com`.
|
|
44
|
-
*
|
|
45
|
-
* > This strategy will match both `http` and `https` protocols regardless of the base URL protocol.
|
|
46
|
-
*/
|
|
47
|
-
EnqueueStrategy["SameDomain"] = "same-domain";
|
|
48
|
-
/**
|
|
49
|
-
* Matches any URLs that have the same hostname and protocol.
|
|
50
|
-
* For example, `https://wow.example.com/hello` will be matched for a base url of `https://wow.example.com/`, but
|
|
51
|
-
* `http://wow.example.com/hello` will not be matched.
|
|
52
|
-
*
|
|
53
|
-
* > This strategy will ensure the protocol of the base URL is the same as the protocol of the URL to be enqueued.
|
|
54
|
-
*/
|
|
55
|
-
EnqueueStrategy["SameOrigin"] = "same-origin";
|
|
56
|
-
})(EnqueueStrategy || (EnqueueStrategy = {}));
|
|
57
|
-
/**
|
|
58
|
-
* This function enqueues the urls provided to the {@link RequestQueue} provided. If you want to automatically find and enqueue links,
|
|
59
|
-
* you should use the context-aware `enqueueLinks` function provided on the crawler contexts.
|
|
60
|
-
*
|
|
61
|
-
* Optionally, the function allows you to filter the target links' URLs using an array of globs or regular expressions
|
|
62
|
-
* and override settings of the enqueued {@link Request} objects.
|
|
63
|
-
*
|
|
64
|
-
* **Example usage**
|
|
65
|
-
*
|
|
66
|
-
* ```javascript
|
|
67
|
-
* await enqueueLinks({
|
|
68
|
-
* urls: aListOfFoundUrls,
|
|
69
|
-
* requestQueue,
|
|
70
|
-
* selector: 'a.product-detail',
|
|
71
|
-
* globs: [
|
|
72
|
-
* 'https://www.example.com/handbags/*',
|
|
73
|
-
* 'https://www.example.com/purses/*'
|
|
74
|
-
* ],
|
|
75
|
-
* });
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* @param options All `enqueueLinks()` parameters are passed via an options object.
|
|
79
|
-
* @returns Promise that resolves to {@link BatchAddRequestsResult} object.
|
|
80
|
-
*/
|
|
81
|
-
export async function enqueueLinks(options) {
|
|
82
|
-
if (!options || Object.keys(options).length === 0) {
|
|
83
|
-
throw new RangeError([
|
|
84
|
-
'enqueueLinks() was called without the required options. You can only do that when you use the `crawlingContext.enqueueLinks()` method in request handlers.',
|
|
85
|
-
'Check out our guide on how to use enqueueLinks() here: https://crawlee.dev/js/docs/examples/crawl-relative-links',
|
|
86
|
-
].join('\n'));
|
|
87
|
-
}
|
|
88
|
-
ow(options, ow.object.exactShape({
|
|
89
|
-
urls: ow.array.ofType(ow.string),
|
|
90
|
-
requestQueue: ow.object.hasKeys('fetchNextRequest', 'addRequest'),
|
|
91
|
-
robotsTxtFile: ow.optional.object.hasKeys('isAllowed'),
|
|
92
|
-
onSkippedRequest: ow.optional.function,
|
|
93
|
-
forefront: ow.optional.boolean,
|
|
94
|
-
skipNavigation: ow.optional.boolean,
|
|
95
|
-
limit: ow.optional.number,
|
|
96
|
-
selector: ow.optional.string,
|
|
97
|
-
baseUrl: ow.optional.string,
|
|
98
|
-
userData: ow.optional.object,
|
|
99
|
-
label: ow.optional.string,
|
|
100
|
-
pseudoUrls: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('purl'))),
|
|
101
|
-
globs: ow.optional.array.ofType(ow.any(ow.string, ow.object.hasKeys('glob'))),
|
|
102
|
-
exclude: ow.optional.array.ofType(ow.any(ow.string, ow.regExp, ow.object.hasKeys('glob'), ow.object.hasKeys('regexp'))),
|
|
103
|
-
regexps: ow.optional.array.ofType(ow.any(ow.regExp, ow.object.hasKeys('regexp'))),
|
|
104
|
-
transformRequestFunction: ow.optional.function,
|
|
105
|
-
strategy: ow.optional.string.oneOf(Object.values(EnqueueStrategy)),
|
|
106
|
-
waitForAllRequestsToBeAdded: ow.optional.boolean,
|
|
107
|
-
}));
|
|
108
|
-
const { requestQueue, limit, urls, pseudoUrls, exclude, globs, regexps, transformRequestFunction, forefront, waitForAllRequestsToBeAdded, robotsTxtFile, onSkippedRequest, } = options;
|
|
109
|
-
const urlExcludePatternObjects = [];
|
|
110
|
-
const urlPatternObjects = [];
|
|
111
|
-
if (exclude?.length) {
|
|
112
|
-
for (const excl of exclude) {
|
|
113
|
-
if (typeof excl === 'string' || 'glob' in excl) {
|
|
114
|
-
urlExcludePatternObjects.push(...constructGlobObjectsFromGlobs([excl]));
|
|
115
|
-
}
|
|
116
|
-
else if (excl instanceof RegExp || 'regexp' in excl) {
|
|
117
|
-
urlExcludePatternObjects.push(...constructRegExpObjectsFromRegExps([excl]));
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
if (pseudoUrls?.length) {
|
|
122
|
-
log.deprecated('`pseudoUrls` option is deprecated, use `globs` or `regexps` instead');
|
|
123
|
-
urlPatternObjects.push(...constructRegExpObjectsFromPseudoUrls(pseudoUrls));
|
|
124
|
-
}
|
|
125
|
-
if (globs?.length) {
|
|
126
|
-
urlPatternObjects.push(...constructGlobObjectsFromGlobs(globs));
|
|
127
|
-
}
|
|
128
|
-
if (regexps?.length) {
|
|
129
|
-
urlPatternObjects.push(...constructRegExpObjectsFromRegExps(regexps));
|
|
130
|
-
}
|
|
131
|
-
if (!urlPatternObjects.length) {
|
|
132
|
-
options.strategy ??= EnqueueStrategy.SameHostname;
|
|
133
|
-
}
|
|
134
|
-
const enqueueStrategyPatterns = [];
|
|
135
|
-
if (options.baseUrl) {
|
|
136
|
-
const url = new URL(options.baseUrl);
|
|
137
|
-
switch (options.strategy) {
|
|
138
|
-
case EnqueueStrategy.SameHostname:
|
|
139
|
-
// We need to get the origin of the passed in domain in the event someone sets baseUrl
|
|
140
|
-
// to an url like https://example.com/deep/default/path and one of the found urls is an
|
|
141
|
-
// absolute relative path (/path/to/page)
|
|
142
|
-
enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin}/**`) });
|
|
143
|
-
break;
|
|
144
|
-
case EnqueueStrategy.SameDomain: {
|
|
145
|
-
// Get the actual hostname from the base url
|
|
146
|
-
const baseUrlHostname = getDomain(url.hostname, { mixedInputs: false });
|
|
147
|
-
if (baseUrlHostname) {
|
|
148
|
-
// We have a hostname, so we can use it to match all links on the page that point to it and any subdomains of it
|
|
149
|
-
url.hostname = baseUrlHostname;
|
|
150
|
-
enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin.replace(baseUrlHostname, `*.${baseUrlHostname}`)}/**`) }, { glob: ignoreHttpSchema(`${url.origin}/**`) });
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
// We don't have a hostname (can happen for ips for instance), so reproduce the same behavior
|
|
154
|
-
// as SameDomainAndSubdomain
|
|
155
|
-
enqueueStrategyPatterns.push({ glob: ignoreHttpSchema(`${url.origin}/**`) });
|
|
156
|
-
}
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
case EnqueueStrategy.SameOrigin: {
|
|
160
|
-
// The same behavior as SameHostname, but respecting the protocol of the URL
|
|
161
|
-
enqueueStrategyPatterns.push({ glob: `${url.origin}/**` });
|
|
162
|
-
break;
|
|
163
|
-
}
|
|
164
|
-
case EnqueueStrategy.All:
|
|
165
|
-
default:
|
|
166
|
-
enqueueStrategyPatterns.push({ glob: `http{s,}://**` });
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
let requestOptions = createRequestOptions(urls, options);
|
|
171
|
-
if (robotsTxtFile) {
|
|
172
|
-
const skippedRequests = [];
|
|
173
|
-
requestOptions = requestOptions.filter((request) => {
|
|
174
|
-
if (robotsTxtFile.isAllowed(request.url)) {
|
|
175
|
-
return true;
|
|
176
|
-
}
|
|
177
|
-
skippedRequests.push(request);
|
|
178
|
-
return false;
|
|
179
|
-
});
|
|
180
|
-
if (onSkippedRequest && skippedRequests.length > 0) {
|
|
181
|
-
await Promise.all(skippedRequests.map((request) => {
|
|
182
|
-
return onSkippedRequest({ url: request.url, reason: 'robotsTxt' });
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
if (transformRequestFunction) {
|
|
187
|
-
requestOptions = requestOptions
|
|
188
|
-
.map((request) => transformRequestFunction(request))
|
|
189
|
-
.filter((r) => !!r);
|
|
190
|
-
}
|
|
191
|
-
function createFilteredRequests() {
|
|
192
|
-
// No user provided patterns means we can skip an extra filtering step
|
|
193
|
-
if (urlPatternObjects.length === 0) {
|
|
194
|
-
return createRequests(requestOptions, enqueueStrategyPatterns, urlExcludePatternObjects, options.strategy);
|
|
195
|
-
}
|
|
196
|
-
// Generate requests based on the user patterns first
|
|
197
|
-
const generatedRequestsFromUserFilters = createRequests(requestOptions, urlPatternObjects, urlExcludePatternObjects, options.strategy);
|
|
198
|
-
// ...then filter them by the enqueue links strategy (making this an AND check)
|
|
199
|
-
return filterRequestsByPatterns(generatedRequestsFromUserFilters, enqueueStrategyPatterns);
|
|
200
|
-
}
|
|
201
|
-
let requests = createFilteredRequests();
|
|
202
|
-
if (limit)
|
|
203
|
-
requests = requests.slice(0, limit);
|
|
204
|
-
const { addedRequests } = await requestQueue.addRequestsBatched(requests, {
|
|
205
|
-
forefront,
|
|
206
|
-
waitForAllRequestsToBeAdded,
|
|
207
|
-
});
|
|
208
|
-
return { processedRequests: addedRequests, unprocessedRequests: [] };
|
|
209
|
-
}
|
|
3
|
+
export { EnqueueStrategy };
|
|
210
4
|
/**
|
|
211
5
|
* @internal
|
|
212
6
|
* This method helps resolve the baseUrl that will be used for filtering in {@link enqueueLinks}.
|
|
@@ -242,9 +36,43 @@ export function resolveBaseUrlForEnqueueLinksFiltering({ enqueueStrategy, finalR
|
|
|
242
36
|
return originalUrlOrigin;
|
|
243
37
|
}
|
|
244
38
|
/**
|
|
245
|
-
*
|
|
39
|
+
* @internal
|
|
40
|
+
* Builds the glob patterns a URL must match to satisfy the given enqueue `strategy`, anchored at `baseUrl`.
|
|
41
|
+
*/
|
|
42
|
+
export function buildEnqueueStrategyPatterns(baseUrl, strategy) {
|
|
43
|
+
const url = new URL(baseUrl);
|
|
44
|
+
switch (strategy) {
|
|
45
|
+
case EnqueueStrategy.SameHostname:
|
|
46
|
+
// We need to get the origin of the passed in domain in the event someone sets baseUrl
|
|
47
|
+
// to an url like https://example.com/deep/default/path and one of the found urls is an
|
|
48
|
+
// absolute relative path (/path/to/page)
|
|
49
|
+
return [{ glob: ignoreHttpSchema(`${url.origin}/**`) }];
|
|
50
|
+
case EnqueueStrategy.SameDomain: {
|
|
51
|
+
// Get the actual hostname from the base url
|
|
52
|
+
const baseUrlHostname = getDomain(url.hostname, { mixedInputs: false });
|
|
53
|
+
if (baseUrlHostname) {
|
|
54
|
+
// We have a hostname, so we can use it to match all links on the page that point to it and any subdomains of it
|
|
55
|
+
url.hostname = baseUrlHostname;
|
|
56
|
+
return [
|
|
57
|
+
{ glob: ignoreHttpSchema(`${url.origin.replace(baseUrlHostname, `*.${baseUrlHostname}`)}/**`) },
|
|
58
|
+
{ glob: ignoreHttpSchema(`${url.origin}/**`) },
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
// We don't have a hostname (can happen for ips for instance), so reproduce the same behavior
|
|
62
|
+
// as SameDomainAndSubdomain
|
|
63
|
+
return [{ glob: ignoreHttpSchema(`${url.origin}/**`) }];
|
|
64
|
+
}
|
|
65
|
+
case EnqueueStrategy.SameOrigin:
|
|
66
|
+
// The same behavior as SameHostname, but respecting the protocol of the URL
|
|
67
|
+
return [{ glob: `${url.origin}/**` }];
|
|
68
|
+
case EnqueueStrategy.All:
|
|
69
|
+
default:
|
|
70
|
+
return [{ glob: `http{s,}://**` }];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Internal function that changes the enqueue glob patterns to match both http and https
|
|
246
75
|
*/
|
|
247
76
|
function ignoreHttpSchema(pattern) {
|
|
248
77
|
return pattern.replace(/^(https?):\/\//, 'http{s,}://');
|
|
249
78
|
}
|
|
250
|
-
//# sourceMappingURL=enqueue_links.js.map
|
package/enqueue_links/index.d.ts
CHANGED
package/enqueue_links/index.js
CHANGED