@crawlee/utils 4.0.0-beta.11 → 4.0.0-beta.111

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 (71) hide show
  1. package/README.md +17 -13
  2. package/index.d.ts +4 -12
  3. package/index.js +3 -11
  4. package/internal.d.ts +7 -0
  5. package/internal.js +6 -0
  6. package/internals/blocked.d.ts +0 -1
  7. package/internals/blocked.js +0 -1
  8. package/internals/cheerio.d.ts +3 -2
  9. package/internals/cheerio.js +4 -5
  10. package/internals/extract-urls.d.ts +5 -1
  11. package/internals/extract-urls.js +8 -5
  12. package/internals/general.d.ts +0 -25
  13. package/internals/general.js +2 -110
  14. package/internals/iterables.d.ts +47 -0
  15. package/internals/iterables.js +96 -0
  16. package/internals/open_graph_parser.d.ts +2 -3
  17. package/internals/open_graph_parser.js +8 -9
  18. package/internals/robots.d.ts +14 -7
  19. package/internals/robots.js +37 -41
  20. package/internals/sitemap.d.ts +65 -8
  21. package/internals/sitemap.js +213 -73
  22. package/internals/social.d.ts +1 -2
  23. package/internals/social.js +7 -5
  24. package/internals/url.d.ts +1 -2
  25. package/internals/url.js +1 -2
  26. package/package.json +7 -6
  27. package/index.d.ts.map +0 -1
  28. package/index.js.map +0 -1
  29. package/internals/blocked.d.ts.map +0 -1
  30. package/internals/blocked.js.map +0 -1
  31. package/internals/cheerio.d.ts.map +0 -1
  32. package/internals/cheerio.js.map +0 -1
  33. package/internals/chunk.d.ts +0 -2
  34. package/internals/chunk.d.ts.map +0 -1
  35. package/internals/chunk.js +0 -40
  36. package/internals/chunk.js.map +0 -1
  37. package/internals/debug.d.ts +0 -31
  38. package/internals/debug.d.ts.map +0 -1
  39. package/internals/debug.js +0 -29
  40. package/internals/debug.js.map +0 -1
  41. package/internals/extract-urls.d.ts.map +0 -1
  42. package/internals/extract-urls.js.map +0 -1
  43. package/internals/general.d.ts.map +0 -1
  44. package/internals/general.js.map +0 -1
  45. package/internals/open_graph_parser.d.ts.map +0 -1
  46. package/internals/open_graph_parser.js.map +0 -1
  47. package/internals/robots.d.ts.map +0 -1
  48. package/internals/robots.js.map +0 -1
  49. package/internals/sitemap.d.ts.map +0 -1
  50. package/internals/sitemap.js.map +0 -1
  51. package/internals/social.d.ts.map +0 -1
  52. package/internals/social.js.map +0 -1
  53. package/internals/system-info/cpu-info.d.ts +0 -64
  54. package/internals/system-info/cpu-info.d.ts.map +0 -1
  55. package/internals/system-info/cpu-info.js +0 -211
  56. package/internals/system-info/cpu-info.js.map +0 -1
  57. package/internals/system-info/memory-info.d.ts +0 -28
  58. package/internals/system-info/memory-info.d.ts.map +0 -1
  59. package/internals/system-info/memory-info.js +0 -118
  60. package/internals/system-info/memory-info.js.map +0 -1
  61. package/internals/system-info/ps-tree.d.ts +0 -18
  62. package/internals/system-info/ps-tree.d.ts.map +0 -1
  63. package/internals/system-info/ps-tree.js +0 -145
  64. package/internals/system-info/ps-tree.js.map +0 -1
  65. package/internals/typedefs.d.ts +0 -5
  66. package/internals/typedefs.d.ts.map +0 -1
  67. package/internals/typedefs.js +0 -9
  68. package/internals/typedefs.js.map +0 -1
  69. package/internals/url.d.ts.map +0 -1
  70. package/internals/url.js.map +0 -1
  71. package/tsconfig.build.tsbuildinfo +0 -1
@@ -1,3 +1,4 @@
1
+ import type { BaseHttpClient, CrawleeLogger } from '@crawlee/types';
1
2
  import { Sitemap } from './sitemap.js';
2
3
  /**
3
4
  * Loads and queries information from a [robots.txt file](https://en.wikipedia.org/wiki/Robots.txt).
@@ -18,15 +19,23 @@ import { Sitemap } from './sitemap.js';
18
19
  * ```
19
20
  */
20
21
  export declare class RobotsTxtFile {
21
- private robots;
22
- private proxyUrl?;
22
+ #private;
23
23
  private constructor();
24
24
  /**
25
25
  * Determine the location of a robots.txt file for a URL and fetch it.
26
26
  * @param url the URL to fetch robots.txt for
27
- * @param [proxyUrl] a proxy to be used for fetching the robots.txt file
27
+ * @param [options] additional options
28
+ * @param [options.signal] an AbortSignal to cancel the request
29
+ * @param [options.timeoutMillis] timeout in milliseconds for the request
30
+ * @param [options.proxyUrl] a proxy to be used for fetching the robots.txt file
28
31
  */
29
- static find(url: string, proxyUrl?: string): Promise<RobotsTxtFile>;
32
+ static find(url: string, options?: {
33
+ signal?: AbortSignal;
34
+ timeoutMillis?: number;
35
+ proxyUrl?: string;
36
+ httpClient?: BaseHttpClient;
37
+ logger?: CrawleeLogger;
38
+ }): Promise<RobotsTxtFile>;
30
39
  /**
31
40
  * Allows providing the URL and robots.txt content explicitly instead of loading it from the target site.
32
41
  * @param url the URL for robots.txt file
@@ -34,7 +43,7 @@ export declare class RobotsTxtFile {
34
43
  * @param [proxyUrl] a proxy to be used for fetching the robots.txt file
35
44
  */
36
45
  static from(url: string, content: string, proxyUrl?: string): RobotsTxtFile;
37
- protected static load(url: string, proxyUrl?: string): Promise<RobotsTxtFile>;
46
+ private static load;
38
47
  /**
39
48
  * Check if a URL should be crawled by robots.
40
49
  * @param url the URL to check against the rules in robots.txt
@@ -54,5 +63,3 @@ export declare class RobotsTxtFile {
54
63
  */
55
64
  parseUrlsFromSitemaps(): Promise<string[]>;
56
65
  }
57
- export { RobotsTxtFile as RobotsFile };
58
- //# sourceMappingURL=robots.d.ts.map
@@ -1,7 +1,6 @@
1
- import { gotScraping } from 'got-scraping';
1
+ import { FetchHttpClient } from '@crawlee/http-client';
2
2
  import robotsParser from 'robots-parser';
3
3
  import { Sitemap } from './sitemap.js';
4
- let HTTPError;
5
4
  /**
6
5
  * Loads and queries information from a [robots.txt file](https://en.wikipedia.org/wiki/Robots.txt).
7
6
  *
@@ -21,22 +20,27 @@ let HTTPError;
21
20
  * ```
22
21
  */
23
22
  export class RobotsTxtFile {
24
- robots;
25
- proxyUrl;
26
- constructor(robots, proxyUrl) {
27
- this.robots = robots;
28
- this.proxyUrl = proxyUrl;
23
+ #robots;
24
+ #proxyUrl;
25
+ #logger;
26
+ constructor(robots, proxyUrl, logger) {
27
+ this.#robots = robots;
28
+ this.#proxyUrl = proxyUrl;
29
+ this.#logger = logger;
29
30
  }
30
31
  /**
31
32
  * Determine the location of a robots.txt file for a URL and fetch it.
32
33
  * @param url the URL to fetch robots.txt for
33
- * @param [proxyUrl] a proxy to be used for fetching the robots.txt file
34
+ * @param [options] additional options
35
+ * @param [options.signal] an AbortSignal to cancel the request
36
+ * @param [options.timeoutMillis] timeout in milliseconds for the request
37
+ * @param [options.proxyUrl] a proxy to be used for fetching the robots.txt file
34
38
  */
35
- static async find(url, proxyUrl) {
39
+ static async find(url, options) {
36
40
  const robotsTxtFileUrl = new URL(url);
37
41
  robotsTxtFileUrl.pathname = '/robots.txt';
38
42
  robotsTxtFileUrl.search = '';
39
- return RobotsTxtFile.load(robotsTxtFileUrl.toString(), proxyUrl);
43
+ return RobotsTxtFile.load(robotsTxtFileUrl.toString(), options);
40
44
  }
41
45
  /**
42
46
  * Allows providing the URL and robots.txt content explicitly instead of loading it from the target site.
@@ -48,33 +52,28 @@ export class RobotsTxtFile {
48
52
  // @ts-ignore
49
53
  return new RobotsTxtFile(robotsParser(url, content), proxyUrl);
50
54
  }
51
- static async load(url, proxyUrl) {
52
- if (!HTTPError) {
53
- HTTPError = (await import('got-scraping')).HTTPError;
54
- }
55
- try {
56
- const response = await gotScraping({
57
- url,
58
- proxyUrl,
59
- method: 'GET',
60
- responseType: 'text',
61
- });
62
- // @ts-ignore
63
- return new RobotsTxtFile(robotsParser(url.toString(), response.body), proxyUrl);
55
+ static async load(url, options) {
56
+ const { proxyUrl, logger, httpClient = new FetchHttpClient() } = options || {};
57
+ const response = await httpClient.sendRequest(new Request(url, { method: 'GET' }), {
58
+ proxyUrl,
59
+ timeoutMillis: options?.timeoutMillis,
60
+ signal: options?.signal,
61
+ });
62
+ if (response.status < 200 || response.status >= 300) {
63
+ throw new Error(`Failed to load robots.txt from ${url}: HTTP ${response.status}`);
64
64
  }
65
- catch (e) {
66
- if (e instanceof HTTPError && e.response.statusCode === 404) {
67
- return new RobotsTxtFile({
68
- isAllowed() {
69
- return true;
70
- },
71
- getSitemaps() {
72
- return [];
73
- },
74
- }, proxyUrl);
75
- }
76
- throw e;
65
+ if (response.status === 404) {
66
+ return new RobotsTxtFile({
67
+ isAllowed() {
68
+ return true;
69
+ },
70
+ getSitemaps() {
71
+ return [];
72
+ },
73
+ }, proxyUrl, logger);
77
74
  }
75
+ // @ts-ignore
76
+ return new RobotsTxtFile(robotsParser(url.toString(), await response.text()), proxyUrl, logger);
78
77
  }
79
78
  /**
80
79
  * Check if a URL should be crawled by robots.
@@ -82,19 +81,19 @@ export class RobotsTxtFile {
82
81
  * @param [userAgent] relevant user agent, default to `*`
83
82
  */
84
83
  isAllowed(url, userAgent = '*') {
85
- return this.robots.isAllowed(url, userAgent) ?? true; // `undefined` means that there is no explicit rule for the requested URL - assume it's allowed
84
+ return this.#robots.isAllowed(url, userAgent) ?? true; // `undefined` means that there is no explicit rule for the requested URL - assume it's allowed
86
85
  }
87
86
  /**
88
87
  * Get URLs of sitemaps referenced in the robots file.
89
88
  */
90
89
  getSitemaps() {
91
- return this.robots.getSitemaps();
90
+ return this.#robots.getSitemaps();
92
91
  }
93
92
  /**
94
93
  * Parse all the sitemaps referenced in the robots file.
95
94
  */
96
95
  async parseSitemaps() {
97
- return Sitemap.load(this.robots.getSitemaps(), this.proxyUrl);
96
+ return Sitemap.load(this.#robots.getSitemaps(), this.#proxyUrl, { logger: this.#logger });
98
97
  }
99
98
  /**
100
99
  * Get all URLs from all the sitemaps referenced in the robots file. A shorthand for `(await robots.parseSitemaps()).urls`.
@@ -103,6 +102,3 @@ export class RobotsTxtFile {
103
102
  return (await this.parseSitemaps()).urls;
104
103
  }
105
104
  }
106
- // to stay backwards compatible
107
- export { RobotsTxtFile as RobotsFile };
108
- //# sourceMappingURL=robots.js.map
@@ -1,5 +1,4 @@
1
- // @ts-ignore optional peer dependency or compatibility with es2022
2
- import type { Delays } from 'got-scraping';
1
+ import type { BaseHttpClient, CrawleeLogger } from '@crawlee/types';
3
2
  interface SitemapUrlData {
4
3
  loc: string;
5
4
  lastmod?: Date;
@@ -36,9 +35,29 @@ export interface ParseSitemapOptions {
36
35
  */
37
36
  sitemapRetries?: number;
38
37
  /**
39
- * Network timeouts for sitemap fetching. See [Got documentation](https://github.com/sindresorhus/got/blob/main/documentation/6-timeout.md) for more details.
38
+ * Timeout settings for network requests when fetching sitemaps. By default this is `30000` milliseconds (30 seconds).
40
39
  */
41
- networkTimeouts?: Delays;
40
+ timeoutMillis?: number;
41
+ /**
42
+ * If true, the parser will log a warning if it fails to fetch a sitemap due to a network error
43
+ * @default true
44
+ */
45
+ reportNetworkErrors?: boolean;
46
+ /**
47
+ * Custom HTTP client to be used for fetching sitemaps.
48
+ */
49
+ httpClient?: BaseHttpClient;
50
+ /**
51
+ * Optional filter for nested sitemap URLs discovered in sitemap index files.
52
+ * Called with the URL of each child sitemap before it is fetched.
53
+ * Return `true` to include the sitemap, `false` to skip it.
54
+ * If not provided, all nested sitemaps are followed.
55
+ */
56
+ nestedSitemapFilter?: (sitemapUrl: string) => boolean;
57
+ /**
58
+ * Optional logger for reporting warnings during sitemap parsing.
59
+ */
60
+ logger?: CrawleeLogger;
42
61
  }
43
62
  export declare function parseSitemap<T extends ParseSitemapOptions>(initialSources: SitemapSource[], proxyUrl?: string, options?: T): AsyncIterable<T['emitNestedSitemaps'] extends true ? SitemapUrl | NestedSitemap : SitemapUrl>;
44
63
  /**
@@ -62,7 +81,7 @@ export declare class Sitemap {
62
81
  * @param url The domain URL to fetch the sitemap for.
63
82
  * @param proxyUrl A proxy to be used for fetching the sitemap file.
64
83
  */
65
- static tryCommonNames(url: string, proxyUrl?: string): Promise<Sitemap>;
84
+ static tryCommonNames(url: string, proxyUrl?: string, parseSitemapOptions?: ParseSitemapOptions): Promise<Sitemap>;
66
85
  /**
67
86
  * Fetch sitemap content from given URL or URLs and return URLs of referenced pages.
68
87
  * @param urls sitemap URL(s)
@@ -74,8 +93,46 @@ export declare class Sitemap {
74
93
  * @param content XML sitemap content
75
94
  * @param proxyUrl URL of a proxy to be used for fetching sitemap contents
76
95
  */
77
- static fromXmlString(content: string, proxyUrl?: string): Promise<Sitemap>;
78
- protected static parse(sources: SitemapSource[], proxyUrl?: string, parseSitemapOptions?: ParseSitemapOptions): Promise<Sitemap>;
96
+ static fromXmlString(content: string, proxyUrl?: string, parseSitemapOptions?: ParseSitemapOptions): Promise<Sitemap>;
97
+ private static parse;
79
98
  }
99
+ /**
100
+ * Given a list of URLs, discover related sitemap files for these domains by checking the `robots.txt` file,
101
+ * the default `sitemap.xml` & `sitemap.txt` files and the URLs themselves.
102
+ * @param `urls` The list of URLs to discover sitemaps for.
103
+ * @param `options` Options for sitemap discovery
104
+ * @returns An async iterable with the discovered sitemap URLs.
105
+ */
106
+ export declare function discoverValidSitemaps(urls: string[], options?: {
107
+ /**
108
+ * Proxy URL to be used for network requests.
109
+ */
110
+ proxyUrl?: string;
111
+ /**
112
+ * Timeout in milliseconds for the entire `discoverValidSitemaps` call.
113
+ * An `AbortController` is created internally and its signal is passed to every HTTP request,
114
+ * so the whole discovery operation is cancelled once the timeout elapses.
115
+ * Defaults to `60_000` ms (60 seconds) to prevent indefinite hangs.
116
+ */
117
+ timeoutMillis?: number;
118
+ /**
119
+ * An external `AbortSignal` to cancel the entire discovery operation.
120
+ * If both `signal` and `timeout` are provided, the operation is cancelled
121
+ * when either the signal is aborted or the timeout elapses (whichever comes first).
122
+ */
123
+ signal?: AbortSignal;
124
+ /**
125
+ * Timeout in milliseconds for each individual HTTP request during discovery.
126
+ * Defaults to `20000` ms (20 seconds).
127
+ */
128
+ requestTimeoutMillis?: number;
129
+ /**
130
+ * HTTP client to be used for network requests.
131
+ */
132
+ httpClient?: BaseHttpClient;
133
+ /**
134
+ * Optional logger for reporting warnings during sitemap discovery.
135
+ */
136
+ logger?: CrawleeLogger;
137
+ }): AsyncIterable<string>;
80
138
  export {};
81
- //# sourceMappingURL=sitemap.d.ts.map