@crawlee/utils 4.0.0-beta.8 → 4.0.0-beta.81
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/index.d.ts +4 -5
- package/index.js +3 -4
- package/internals/blocked.d.ts +0 -1
- package/internals/blocked.js +0 -1
- package/internals/cheerio.d.ts +3 -2
- package/internals/cheerio.js +4 -5
- package/internals/chunk.d.ts +0 -1
- package/internals/chunk.js +0 -1
- package/internals/debug.d.ts +11 -1
- package/internals/debug.js +34 -1
- package/internals/extract-urls.d.ts +5 -1
- package/internals/extract-urls.js +8 -5
- package/internals/general.d.ts +17 -15
- package/internals/general.js +44 -83
- package/internals/iterables.d.ts +126 -0
- package/internals/iterables.js +230 -0
- package/internals/open_graph_parser.d.ts +2 -3
- package/internals/open_graph_parser.js +8 -9
- package/internals/robots.d.ts +14 -4
- package/internals/robots.js +31 -33
- package/internals/sitemap.d.ts +65 -8
- package/internals/sitemap.js +174 -34
- package/internals/social.d.ts +1 -2
- package/internals/social.js +7 -5
- package/internals/typedefs.d.ts +0 -1
- package/internals/typedefs.js +0 -1
- package/internals/url.d.ts +1 -2
- package/internals/url.js +1 -2
- package/package.json +6 -6
- package/index.d.ts.map +0 -1
- package/index.js.map +0 -1
- package/internals/blocked.d.ts.map +0 -1
- package/internals/blocked.js.map +0 -1
- package/internals/cheerio.d.ts.map +0 -1
- package/internals/cheerio.js.map +0 -1
- package/internals/chunk.d.ts.map +0 -1
- package/internals/chunk.js.map +0 -1
- package/internals/debug.d.ts.map +0 -1
- package/internals/debug.js.map +0 -1
- package/internals/extract-urls.d.ts.map +0 -1
- package/internals/extract-urls.js.map +0 -1
- package/internals/general.d.ts.map +0 -1
- package/internals/general.js.map +0 -1
- package/internals/memory-info.d.ts +0 -26
- package/internals/memory-info.d.ts.map +0 -1
- package/internals/memory-info.js +0 -131
- package/internals/memory-info.js.map +0 -1
- package/internals/open_graph_parser.d.ts.map +0 -1
- package/internals/open_graph_parser.js.map +0 -1
- package/internals/robots.d.ts.map +0 -1
- package/internals/robots.js.map +0 -1
- package/internals/sitemap.d.ts.map +0 -1
- package/internals/sitemap.js.map +0 -1
- package/internals/social.d.ts.map +0 -1
- package/internals/social.js.map +0 -1
- package/internals/systemInfoV2/cpu-info.d.ts +0 -64
- package/internals/systemInfoV2/cpu-info.d.ts.map +0 -1
- package/internals/systemInfoV2/cpu-info.js +0 -211
- package/internals/systemInfoV2/cpu-info.js.map +0 -1
- package/internals/systemInfoV2/memory-info.d.ts +0 -28
- package/internals/systemInfoV2/memory-info.d.ts.map +0 -1
- package/internals/systemInfoV2/memory-info.js +0 -118
- package/internals/systemInfoV2/memory-info.js.map +0 -1
- package/internals/systemInfoV2/ps-tree.d.ts +0 -18
- package/internals/systemInfoV2/ps-tree.d.ts.map +0 -1
- package/internals/systemInfoV2/ps-tree.js +0 -145
- package/internals/systemInfoV2/ps-tree.js.map +0 -1
- package/internals/typedefs.d.ts.map +0 -1
- package/internals/typedefs.js.map +0 -1
- package/internals/url.d.ts.map +0 -1
- package/internals/url.js.map +0 -1
- package/tsconfig.build.tsbuildinfo +0 -1
package/README.md
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
<h1 align="center">
|
|
2
2
|
<a href="https://crawlee.dev">
|
|
3
3
|
<picture>
|
|
4
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true"
|
|
5
|
-
<img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500"
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-dark.svg?sanitize=true" />
|
|
5
|
+
<img alt="Crawlee" src="https://raw.githubusercontent.com/apify/crawlee/master/website/static/img/crawlee-light.svg?sanitize=true" width="500" />
|
|
6
6
|
</picture>
|
|
7
7
|
</a>
|
|
8
|
-
<br
|
|
8
|
+
<br />
|
|
9
9
|
<small>A web scraping and browser automation library</small>
|
|
10
10
|
</h1>
|
|
11
11
|
|
|
12
|
-
<p align=center>
|
|
13
|
-
<a href="https://
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
<p align="center">
|
|
13
|
+
<a href="https://trendshift.io/repositories/5179" target="_blank"><img src="https://trendshift.io/api/badge/repositories/5179" alt="apify%2Fcrawlee | Trendshift" width="250" height="55"/></a>
|
|
14
|
+
</p>
|
|
15
|
+
|
|
16
|
+
<p align="center">
|
|
17
|
+
<a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/v/@crawlee/core.svg" alt="NPM latest version" data-canonical-src="https://img.shields.io/npm/v/@crawlee/core/next.svg" /></a>
|
|
18
|
+
<a href="https://www.npmjs.com/package/@crawlee/core" rel="nofollow"><img src="https://img.shields.io/npm/dm/@crawlee/core.svg" alt="Downloads" data-canonical-src="https://img.shields.io/npm/dm/@crawlee/core.svg" /></a>
|
|
19
|
+
<a href="https://discord.gg/jyEM2PRvMU" rel="nofollow"><img src="https://img.shields.io/discord/801163717915574323?label=discord" alt="Chat on discord" data-canonical-src="https://img.shields.io/discord/801163717915574323?label=discord" /></a>
|
|
20
|
+
<a href="https://github.com/apify/crawlee/actions/workflows/test-ci.yml"><img src="https://github.com/apify/crawlee/actions/workflows/test-ci.yml/badge.svg?branch=master" alt="Build Status" /></a>
|
|
17
21
|
</p>
|
|
18
22
|
|
|
19
23
|
Crawlee covers your crawling and scraping end-to-end and **helps you build reliable scrapers. Fast.**
|
|
@@ -24,7 +28,7 @@ Crawlee is available as the [`crawlee`](https://www.npmjs.com/package/crawlee) N
|
|
|
24
28
|
|
|
25
29
|
> 👉 **View full documentation, guides and examples on the [Crawlee project website](https://crawlee.dev)** 👈
|
|
26
30
|
|
|
27
|
-
>
|
|
31
|
+
> Do you prefer 🐍 Python instead of JavaScript? [👉 Checkout Crawlee for Python 👈](https://github.com/apify/crawlee-python).
|
|
28
32
|
|
|
29
33
|
## Installation
|
|
30
34
|
|
|
@@ -85,7 +89,7 @@ By default, Crawlee stores data to `./storage` in the current working directory.
|
|
|
85
89
|
We provide automated beta builds for every merged code change in Crawlee. You can find them in the npm [list of releases](https://www.npmjs.com/package/crawlee?activeTab=versions). If you want to test new features or bug fixes before we release them, feel free to install a beta build like this:
|
|
86
90
|
|
|
87
91
|
```bash
|
|
88
|
-
npm install crawlee@
|
|
92
|
+
npm install crawlee@next
|
|
89
93
|
```
|
|
90
94
|
|
|
91
95
|
If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need to specify dependency overrides in your `package.json` file so that you don't end up with multiple versions of Crawlee installed:
|
|
@@ -94,9 +98,9 @@ If you also use the [Apify SDK](https://github.com/apify/apify-sdk-js), you need
|
|
|
94
98
|
{
|
|
95
99
|
"overrides": {
|
|
96
100
|
"apify": {
|
|
97
|
-
"@crawlee/core": "
|
|
98
|
-
"@crawlee/types": "
|
|
99
|
-
"@crawlee/utils": "
|
|
101
|
+
"@crawlee/core": "$crawlee",
|
|
102
|
+
"@crawlee/types": "$crawlee",
|
|
103
|
+
"@crawlee/utils": "$crawlee"
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
106
|
}
|
package/index.d.ts
CHANGED
|
@@ -3,15 +3,14 @@ export * from './internals/cheerio.js';
|
|
|
3
3
|
export * from './internals/chunk.js';
|
|
4
4
|
export * from './internals/extract-urls.js';
|
|
5
5
|
export * from './internals/general.js';
|
|
6
|
-
export * from './internals/memory-info.js';
|
|
7
6
|
export * from './internals/debug.js';
|
|
8
7
|
export * as social from './internals/social.js';
|
|
9
8
|
export * from './internals/typedefs.js';
|
|
10
9
|
export * from './internals/open_graph_parser.js';
|
|
11
10
|
export * from './internals/robots.js';
|
|
12
11
|
export * from './internals/sitemap.js';
|
|
12
|
+
export * from './internals/iterables.js';
|
|
13
|
+
export * from './internals/robots.js';
|
|
14
|
+
export * from './internals/sitemap.js';
|
|
13
15
|
export * from './internals/url.js';
|
|
14
|
-
export {
|
|
15
|
-
export { getMemoryInfoV2 } from './internals/systemInfoV2/memory-info.js';
|
|
16
|
-
export { Dictionary, Awaitable, Constructor } from '@crawlee/types';
|
|
17
|
-
//# sourceMappingURL=index.d.ts.map
|
|
16
|
+
export type { Dictionary, Awaitable, Constructor } from '@crawlee/types';
|
package/index.js
CHANGED
|
@@ -3,14 +3,13 @@ export * from './internals/cheerio.js';
|
|
|
3
3
|
export * from './internals/chunk.js';
|
|
4
4
|
export * from './internals/extract-urls.js';
|
|
5
5
|
export * from './internals/general.js';
|
|
6
|
-
export * from './internals/memory-info.js';
|
|
7
6
|
export * from './internals/debug.js';
|
|
8
7
|
export * as social from './internals/social.js';
|
|
9
8
|
export * from './internals/typedefs.js';
|
|
10
9
|
export * from './internals/open_graph_parser.js';
|
|
11
10
|
export * from './internals/robots.js';
|
|
12
11
|
export * from './internals/sitemap.js';
|
|
12
|
+
export * from './internals/iterables.js';
|
|
13
|
+
export * from './internals/robots.js';
|
|
14
|
+
export * from './internals/sitemap.js';
|
|
13
15
|
export * from './internals/url.js';
|
|
14
|
-
export { getCurrentCpuTicksV2 } from './internals/systemInfoV2/cpu-info.js';
|
|
15
|
-
export { getMemoryInfoV2 } from './internals/systemInfoV2/memory-info.js';
|
|
16
|
-
//# sourceMappingURL=index.js.map
|
package/internals/blocked.d.ts
CHANGED
package/internals/blocked.js
CHANGED
package/internals/cheerio.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { CheerioAPI } from 'cheerio';
|
|
2
2
|
export type CheerioRoot = CheerioAPI;
|
|
3
|
+
export type { CheerioAPI, Cheerio } from 'cheerio';
|
|
4
|
+
export type { Element } from 'domhandler';
|
|
3
5
|
/**
|
|
4
6
|
* The function converts a HTML document to a plain text.
|
|
5
7
|
*
|
|
@@ -27,7 +29,7 @@ export type CheerioRoot = CheerioAPI;
|
|
|
27
29
|
* @param htmlOrCheerioElement HTML text or parsed HTML represented using a [cheerio](https://www.npmjs.com/package/cheerio) function.
|
|
28
30
|
* @return Plain text
|
|
29
31
|
*/
|
|
30
|
-
export declare function htmlToText(htmlOrCheerioElement: string | CheerioRoot): string
|
|
32
|
+
export declare function htmlToText(htmlOrCheerioElement: string | CheerioRoot): Promise<string>;
|
|
31
33
|
/**
|
|
32
34
|
* Extracts URLs from a given Cheerio object.
|
|
33
35
|
*
|
|
@@ -38,4 +40,3 @@ export declare function htmlToText(htmlOrCheerioElement: string | CheerioRoot):
|
|
|
38
40
|
* @return An array of absolute URLs
|
|
39
41
|
*/
|
|
40
42
|
export declare function extractUrlsFromCheerio($: CheerioAPI, selector?: string, baseUrl?: string): string[];
|
|
41
|
-
//# sourceMappingURL=cheerio.d.ts.map
|
package/internals/cheerio.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import * as cheerio from 'cheerio';
|
|
2
1
|
import { tryAbsoluteURL } from './extract-urls.js';
|
|
3
2
|
// NOTE: We are skipping 'noscript' since it's content is evaluated as text, instead of HTML elements. That damages the results.
|
|
4
3
|
const SKIP_TAGS_REGEX = /^(script|style|canvas|svg|noscript)$/i;
|
|
@@ -30,10 +29,11 @@ const BLOCK_TAGS_REGEX = /^(p|h1|h2|h3|h4|h5|h6|ol|ul|li|pre|address|blockquote|
|
|
|
30
29
|
* @param htmlOrCheerioElement HTML text or parsed HTML represented using a [cheerio](https://www.npmjs.com/package/cheerio) function.
|
|
31
30
|
* @return Plain text
|
|
32
31
|
*/
|
|
33
|
-
export function htmlToText(htmlOrCheerioElement) {
|
|
32
|
+
export async function htmlToText(htmlOrCheerioElement) {
|
|
33
|
+
const { load } = await import('cheerio');
|
|
34
34
|
if (!htmlOrCheerioElement)
|
|
35
35
|
return '';
|
|
36
|
-
const $ = typeof htmlOrCheerioElement === 'function' ? htmlOrCheerioElement :
|
|
36
|
+
const $ = typeof htmlOrCheerioElement === 'function' ? htmlOrCheerioElement : load(htmlOrCheerioElement);
|
|
37
37
|
let text = '';
|
|
38
38
|
const process = (elems) => {
|
|
39
39
|
const len = elems ? elems.length : 0;
|
|
@@ -42,7 +42,7 @@ export function htmlToText(htmlOrCheerioElement) {
|
|
|
42
42
|
if (elem.type === 'text') {
|
|
43
43
|
// Compress spaces, unless we're inside <pre> element
|
|
44
44
|
let compr;
|
|
45
|
-
if (elem.parent
|
|
45
|
+
if (elem.parent?.tagName === 'pre')
|
|
46
46
|
compr = elem.data;
|
|
47
47
|
else
|
|
48
48
|
compr = elem.data.replace(/\s+/g, ' ');
|
|
@@ -107,4 +107,3 @@ export function extractUrlsFromCheerio($, selector = 'a', baseUrl = '') {
|
|
|
107
107
|
})
|
|
108
108
|
.filter(Boolean);
|
|
109
109
|
}
|
|
110
|
-
//# sourceMappingURL=cheerio.js.map
|
package/internals/chunk.d.ts
CHANGED
package/internals/chunk.js
CHANGED
package/internals/debug.d.ts
CHANGED
|
@@ -27,5 +27,15 @@ interface Request<UserData extends Dictionary = Dictionary> {
|
|
|
27
27
|
* @param [additionalFields] Object containing additional fields to be added.
|
|
28
28
|
*/
|
|
29
29
|
export declare function createRequestDebugInfo(request: Request, response?: IncomingMessage | Partial<BrowserResponseLike>, additionalFields?: Dictionary): Dictionary;
|
|
30
|
+
/**
|
|
31
|
+
* Returns a human-readable label for an unknown value,
|
|
32
|
+
* suitable for embedding in error messages and log output.
|
|
33
|
+
*
|
|
34
|
+
* Returns `constructor.name` when available (e.g. `"Configuration"`, `"Number"`),
|
|
35
|
+
* otherwise falls back to `util.inspect` (e.g. for `null`, `undefined`).
|
|
36
|
+
*
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export declare function inspectValue(value: unknown): string;
|
|
40
|
+
export declare function getObjectType(value: unknown): string;
|
|
30
41
|
export {};
|
|
31
|
-
//# sourceMappingURL=debug.d.ts.map
|
package/internals/debug.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { inspect } from 'node:util';
|
|
1
2
|
import ow from 'ow';
|
|
2
3
|
/**
|
|
3
4
|
* Creates a standardized debug info from request and response. This info is usually added to dataset under the hidden `#debug` field.
|
|
@@ -26,4 +27,36 @@ export function createRequestDebugInfo(request, response = {}, additionalFields
|
|
|
26
27
|
...additionalFields,
|
|
27
28
|
};
|
|
28
29
|
}
|
|
29
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Returns a human-readable label for an unknown value,
|
|
32
|
+
* suitable for embedding in error messages and log output.
|
|
33
|
+
*
|
|
34
|
+
* Returns `constructor.name` when available (e.g. `"Configuration"`, `"Number"`),
|
|
35
|
+
* otherwise falls back to `util.inspect` (e.g. for `null`, `undefined`).
|
|
36
|
+
*
|
|
37
|
+
* @internal
|
|
38
|
+
*/
|
|
39
|
+
export function inspectValue(value) {
|
|
40
|
+
if (typeof value === 'object' && value !== null && value.constructor?.name) {
|
|
41
|
+
return value.constructor.name;
|
|
42
|
+
}
|
|
43
|
+
return inspect(value, {
|
|
44
|
+
depth: 0,
|
|
45
|
+
compact: true,
|
|
46
|
+
maxStringLength: 64,
|
|
47
|
+
breakLength: Infinity,
|
|
48
|
+
colors: false,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export function getObjectType(value) {
|
|
52
|
+
const simple = typeof value;
|
|
53
|
+
if (['string', 'number', 'boolean', 'bigint'].includes(simple)) {
|
|
54
|
+
return simple;
|
|
55
|
+
}
|
|
56
|
+
const objectType = Object.prototype.toString.call(value);
|
|
57
|
+
const type = /\[object (\w+)]/.exec(objectType)[1];
|
|
58
|
+
if (type === 'Uint8Array') {
|
|
59
|
+
return 'Buffer';
|
|
60
|
+
}
|
|
61
|
+
return ['Date', 'Buffer', 'RegExp'].includes(type) ? type : type.toLowerCase();
|
|
62
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { BaseHttpClient } from '@crawlee/types';
|
|
1
2
|
export interface DownloadListOfUrlsOptions {
|
|
2
3
|
/**
|
|
3
4
|
* URL to the file
|
|
@@ -16,6 +17,10 @@ export interface DownloadListOfUrlsOptions {
|
|
|
16
17
|
urlRegExp?: RegExp;
|
|
17
18
|
/** Allows to use a proxy for the download request. */
|
|
18
19
|
proxyUrl?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Custom HTTP client to use for downloading the file.
|
|
22
|
+
*/
|
|
23
|
+
httpClient?: BaseHttpClient;
|
|
19
24
|
}
|
|
20
25
|
/**
|
|
21
26
|
* Returns a promise that resolves to an array of urls parsed from the resource available at the provided url.
|
|
@@ -41,4 +46,3 @@ export declare function extractUrls(options: ExtractUrlsOptions): string[];
|
|
|
41
46
|
* Helper function used to validate URLs used when extracting URLs from a page
|
|
42
47
|
*/
|
|
43
48
|
export declare function tryAbsoluteURL(href: string, baseUrl: string): string | undefined;
|
|
44
|
-
//# sourceMappingURL=extract-urls.d.ts.map
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FetchHttpClient } from '@crawlee/http-client';
|
|
2
2
|
import ow from 'ow';
|
|
3
3
|
import { URL_NO_COMMAS_REGEX } from './general.js';
|
|
4
4
|
/**
|
|
@@ -11,15 +11,19 @@ export async function downloadListOfUrls(options) {
|
|
|
11
11
|
encoding: ow.optional.string,
|
|
12
12
|
urlRegExp: ow.optional.regExp,
|
|
13
13
|
proxyUrl: ow.optional.string,
|
|
14
|
+
httpClient: ow.optional.object,
|
|
14
15
|
}));
|
|
15
|
-
const { url, encoding = 'utf8', urlRegExp = URL_NO_COMMAS_REGEX, proxyUrl } = options;
|
|
16
|
+
const { url, encoding = 'utf8', urlRegExp = URL_NO_COMMAS_REGEX, proxyUrl, httpClient = new FetchHttpClient(), } = options;
|
|
16
17
|
// Try to detect wrong urls and fix them. Currently, detects only sharing url instead of csv download one.
|
|
17
|
-
const match =
|
|
18
|
+
const match = /^(https:\/\/docs\.google\.com\/spreadsheets\/d\/(?:\w|-)+)\/?/.exec(url);
|
|
18
19
|
let fixedUrl = url;
|
|
19
20
|
if (match) {
|
|
20
21
|
fixedUrl = `${match[1]}/gviz/tq?tqx=out:csv`;
|
|
21
22
|
}
|
|
22
|
-
const
|
|
23
|
+
const response = await httpClient.sendRequest(new Request(fixedUrl, { method: 'GET' }), {
|
|
24
|
+
proxyUrl,
|
|
25
|
+
});
|
|
26
|
+
const string = new TextDecoder(encoding).decode(new Uint8Array(await response.arrayBuffer()));
|
|
23
27
|
return extractUrls({ string, urlRegExp });
|
|
24
28
|
}
|
|
25
29
|
/**
|
|
@@ -49,4 +53,3 @@ export function tryAbsoluteURL(href, baseUrl) {
|
|
|
49
53
|
return undefined;
|
|
50
54
|
}
|
|
51
55
|
}
|
|
52
|
-
//# sourceMappingURL=extract-urls.js.map
|
package/internals/general.d.ts
CHANGED
|
@@ -8,20 +8,6 @@ export declare const URL_NO_COMMAS_REGEX: RegExp;
|
|
|
8
8
|
* Note, however, that this may prevent parsing URLs from comma delimited lists, or the URLs may become malformed.
|
|
9
9
|
*/
|
|
10
10
|
export declare const URL_WITH_COMMAS_REGEX: RegExp;
|
|
11
|
-
/**
|
|
12
|
-
* Returns a `Promise` that resolves to true if the code is running in a Docker container.
|
|
13
|
-
*/
|
|
14
|
-
export declare function isDocker(forceReset?: boolean): Promise<boolean>;
|
|
15
|
-
/**
|
|
16
|
-
* Detects if crawlee is running in a containerized environment.
|
|
17
|
-
*/
|
|
18
|
-
export declare function isContainerized(): Promise<boolean>;
|
|
19
|
-
export declare function isLambda(): boolean;
|
|
20
|
-
/**
|
|
21
|
-
* gets the cgroup version by checking for a file at /sys/fs/cgroup/memory
|
|
22
|
-
* @returns "V1" or "V2" for the version of cgroup or null if cgroup is not found.
|
|
23
|
-
*/
|
|
24
|
-
export declare function getCgroupsVersion(forceReset?: boolean): Promise<"V1" | "V2" | null>;
|
|
25
11
|
/**
|
|
26
12
|
* Computes a weighted average of an array of numbers, complemented by an array of weights.
|
|
27
13
|
* @ignore
|
|
@@ -54,4 +40,20 @@ export declare function snakeCaseToCamelCase(snakeCaseStr: string): string;
|
|
|
54
40
|
* @ignore
|
|
55
41
|
*/
|
|
56
42
|
export declare function expandShadowRoots(document: Document): string;
|
|
57
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Checks if the given value is a Node.js Stream or a Web API ReadableStream.
|
|
45
|
+
* @ignore
|
|
46
|
+
*/
|
|
47
|
+
export declare function isStream(value: unknown): value is NodeJS.ReadableStream | ReadableStream;
|
|
48
|
+
/**
|
|
49
|
+
* Checks if the given value is a Node.js Buffer, ArrayBuffer, or TypedArray.
|
|
50
|
+
* @ignore
|
|
51
|
+
*/
|
|
52
|
+
export declare function isBuffer(value: unknown): value is Buffer | ArrayBuffer | ArrayBufferView;
|
|
53
|
+
/**
|
|
54
|
+
* Converts a byte-like value (Buffer, ArrayBuffer, or any typed-array / DataView) into a Buffer over
|
|
55
|
+
* the exact same bytes, honoring `byteOffset` / `byteLength` for views. Existing Buffers are returned
|
|
56
|
+
* as-is. Used by storage backends, which persist raw bytes regardless of the input's concrete shape.
|
|
57
|
+
* @ignore
|
|
58
|
+
*/
|
|
59
|
+
export declare function toBuffer(value: Buffer | ArrayBuffer | ArrayBufferView): Buffer;
|
package/internals/general.js
CHANGED
|
@@ -1,94 +1,14 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
1
|
import { setTimeout } from 'node:timers/promises';
|
|
3
2
|
/**
|
|
4
3
|
* Default regular expression to match URLs in a string that may be plain text, JSON, CSV or other. It supports common URL characters
|
|
5
4
|
* and does not support URLs containing commas or spaces. The URLs also may contain Unicode letters (not symbols).
|
|
6
5
|
*/
|
|
7
|
-
export const URL_NO_COMMAS_REGEX = /https?:\/\/(www\.)?([\p{L}0-9]|[\p{L}0-9][-\p{L}0-9@:%._+~#=]{0,254}[\p{L}0-9])\.[a-z]{2,63}(:\d{1,5})?(\/[-\p{L}0-9@:%_+.~#?&/=()]*)?/giu;
|
|
6
|
+
export const URL_NO_COMMAS_REGEX = /https?:\/\/(www\.)?([\p{L}0-9]|[\p{L}0-9][-\p{L}0-9@:%._+~#=]{0,254}[\p{L}0-9])\.[a-z]{2,63}(:\d{1,5})?(\/[-\p{L}0-9@:%_+.~#?&/=()'*]*)?/giu;
|
|
8
7
|
/**
|
|
9
8
|
* Regular expression that, in addition to the default regular expression `URL_NO_COMMAS_REGEX`, supports matching commas in URL path and query.
|
|
10
9
|
* Note, however, that this may prevent parsing URLs from comma delimited lists, or the URLs may become malformed.
|
|
11
10
|
*/
|
|
12
|
-
export const URL_WITH_COMMAS_REGEX = /https?:\/\/(www\.)?([\p{L}0-9]|[\p{L}0-9][-\p{L}0-9@:%._+~#=]{0,254}[\p{L}0-9])\.[a-z]{2,63}(:\d{1,5})?(\/[-\p{L}0-9@:%_+,.~#?&/=()]*)?/giu;
|
|
13
|
-
let isDockerPromiseCache;
|
|
14
|
-
async function createIsDockerPromise() {
|
|
15
|
-
const promise1 = fs
|
|
16
|
-
.stat('/.dockerenv')
|
|
17
|
-
.then(() => true)
|
|
18
|
-
.catch(() => false);
|
|
19
|
-
const promise2 = fs
|
|
20
|
-
.readFile('/proc/self/cgroup', 'utf8')
|
|
21
|
-
.then((content) => content.includes('docker'))
|
|
22
|
-
.catch(() => false);
|
|
23
|
-
const [result1, result2] = await Promise.all([promise1, promise2]);
|
|
24
|
-
return result1 || result2;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Returns a `Promise` that resolves to true if the code is running in a Docker container.
|
|
28
|
-
*/
|
|
29
|
-
export async function isDocker(forceReset) {
|
|
30
|
-
// Parameter forceReset is just internal for unit tests.
|
|
31
|
-
if (!isDockerPromiseCache || forceReset)
|
|
32
|
-
isDockerPromiseCache = createIsDockerPromise();
|
|
33
|
-
return isDockerPromiseCache;
|
|
34
|
-
}
|
|
35
|
-
let isContainerizedResult;
|
|
36
|
-
/**
|
|
37
|
-
* Detects if crawlee is running in a containerized environment.
|
|
38
|
-
*/
|
|
39
|
-
export async function isContainerized() {
|
|
40
|
-
// Value is very unlikley to change. Cache the result after the first execution.
|
|
41
|
-
if (isContainerizedResult !== undefined) {
|
|
42
|
-
return isContainerizedResult;
|
|
43
|
-
}
|
|
44
|
-
// return false if running in aws lambda
|
|
45
|
-
if (isLambda()) {
|
|
46
|
-
isContainerizedResult = false;
|
|
47
|
-
return isContainerizedResult;
|
|
48
|
-
}
|
|
49
|
-
const dockerenvCheck = fs
|
|
50
|
-
.stat('/.dockerenv')
|
|
51
|
-
.then(() => true)
|
|
52
|
-
.catch(() => false);
|
|
53
|
-
const cgroupCheck = fs
|
|
54
|
-
.readFile('/proc/self/cgroup', 'utf8')
|
|
55
|
-
.then((content) => content.includes('docker'))
|
|
56
|
-
.catch(() => false);
|
|
57
|
-
const [dockerenvResult, cgroupResult] = await Promise.all([dockerenvCheck, cgroupCheck]);
|
|
58
|
-
isContainerizedResult = dockerenvResult || cgroupResult || !!process.env.KUBERNETES_SERVICE_HOST;
|
|
59
|
-
return isContainerizedResult;
|
|
60
|
-
}
|
|
61
|
-
export function isLambda() {
|
|
62
|
-
return !!process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE;
|
|
63
|
-
}
|
|
64
|
-
let _cgroupsVersion;
|
|
65
|
-
/**
|
|
66
|
-
* gets the cgroup version by checking for a file at /sys/fs/cgroup/memory
|
|
67
|
-
* @returns "V1" or "V2" for the version of cgroup or null if cgroup is not found.
|
|
68
|
-
*/
|
|
69
|
-
export async function getCgroupsVersion(forceReset) {
|
|
70
|
-
// Parameter forceReset is just internal for unit tests.
|
|
71
|
-
if (_cgroupsVersion !== undefined && !forceReset) {
|
|
72
|
-
return _cgroupsVersion;
|
|
73
|
-
}
|
|
74
|
-
try {
|
|
75
|
-
// If this directory does not exists, cgroups are not available
|
|
76
|
-
await fs.access('/sys/fs/cgroup/');
|
|
77
|
-
}
|
|
78
|
-
catch (e) {
|
|
79
|
-
_cgroupsVersion = null;
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
_cgroupsVersion = 'V1';
|
|
83
|
-
try {
|
|
84
|
-
// If this directory does not exists, assume the container is using cgroups V2
|
|
85
|
-
await fs.access('/sys/fs/cgroup/memory/');
|
|
86
|
-
}
|
|
87
|
-
catch (e) {
|
|
88
|
-
_cgroupsVersion = 'V2';
|
|
89
|
-
}
|
|
90
|
-
return _cgroupsVersion;
|
|
91
|
-
}
|
|
11
|
+
export const URL_WITH_COMMAS_REGEX = /https?:\/\/(www\.)?([\p{L}0-9]|[\p{L}0-9][-\p{L}0-9@:%._+~#=]{0,254}[\p{L}0-9])\.[a-z]{2,63}(:\d{1,5})?(\/[-\p{L}0-9@:%_+,.~#?&/=()'*]*)?/giu;
|
|
92
12
|
/**
|
|
93
13
|
* Computes a weighted average of an array of numbers, complemented by an array of weights.
|
|
94
14
|
* @ignore
|
|
@@ -164,4 +84,45 @@ export function expandShadowRoots(document) {
|
|
|
164
84
|
replaceShadowDomsWithHtml(document.body);
|
|
165
85
|
return document.documentElement.outerHTML;
|
|
166
86
|
}
|
|
167
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Checks if the given value is a Node.js Stream or a Web API ReadableStream.
|
|
89
|
+
* @ignore
|
|
90
|
+
*/
|
|
91
|
+
export function isStream(value) {
|
|
92
|
+
if (typeof value !== 'object' || value === null) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
// A Node.js Readable is both pipeable and async-iterable; a Web ReadableStream exposes pipeTo.
|
|
96
|
+
// Requiring async-iterability for the `pipe` branch rejects plain `{ pipe }` ducks that would
|
|
97
|
+
// otherwise blow up later in the storage backends' drain loop with a cryptic TypeError.
|
|
98
|
+
const isNodeStream = typeof value.pipe === 'function' && typeof value[Symbol.asyncIterator] === 'function';
|
|
99
|
+
const isWebStream = typeof value.pipeTo === 'function';
|
|
100
|
+
return isNodeStream || isWebStream;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Checks if the given value is a Node.js Buffer, ArrayBuffer, or TypedArray.
|
|
104
|
+
* @ignore
|
|
105
|
+
*/
|
|
106
|
+
export function isBuffer(value) {
|
|
107
|
+
return (value != null &&
|
|
108
|
+
typeof value === 'object' &&
|
|
109
|
+
(Buffer.isBuffer(value) ||
|
|
110
|
+
value instanceof ArrayBuffer ||
|
|
111
|
+
ArrayBuffer.isView(value) ||
|
|
112
|
+
value.constructor?.name === 'Buffer'));
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Converts a byte-like value (Buffer, ArrayBuffer, or any typed-array / DataView) into a Buffer over
|
|
116
|
+
* the exact same bytes, honoring `byteOffset` / `byteLength` for views. Existing Buffers are returned
|
|
117
|
+
* as-is. Used by storage backends, which persist raw bytes regardless of the input's concrete shape.
|
|
118
|
+
* @ignore
|
|
119
|
+
*/
|
|
120
|
+
export function toBuffer(value) {
|
|
121
|
+
if (Buffer.isBuffer(value)) {
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
if (value instanceof ArrayBuffer) {
|
|
125
|
+
return Buffer.from(value);
|
|
126
|
+
}
|
|
127
|
+
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
|
|
128
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that checks if a value is iterable (has Symbol.iterator).
|
|
3
|
+
* @internal
|
|
4
|
+
*
|
|
5
|
+
* **Example usage:**
|
|
6
|
+
* ```ts
|
|
7
|
+
* if (isIterable(someValue)) {
|
|
8
|
+
* for (const item of someValue) {
|
|
9
|
+
* console.log(item);
|
|
10
|
+
* }
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function isIterable<T>(value: unknown): value is Iterable<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Type guard that checks if a value is async iterable (has Symbol.asyncIterator).
|
|
17
|
+
* @internal
|
|
18
|
+
*
|
|
19
|
+
* **Example usage:**
|
|
20
|
+
* ```ts
|
|
21
|
+
* if (isAsyncIterable(someValue)) {
|
|
22
|
+
* for await (const item of someValue) {
|
|
23
|
+
* console.log(item);
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function isAsyncIterable<T>(value: unknown): value is AsyncIterable<T>;
|
|
29
|
+
/**
|
|
30
|
+
* Converts any iterable or async iterable to an async iterable.
|
|
31
|
+
* @internal
|
|
32
|
+
*
|
|
33
|
+
* @yields Each item from the input iterable
|
|
34
|
+
*
|
|
35
|
+
* **Example usage:**
|
|
36
|
+
* ```ts
|
|
37
|
+
* const syncArray = [1, 2, 3];
|
|
38
|
+
* for await (const item of asyncifyIterable(syncArray)) {
|
|
39
|
+
* console.log(item); // 1, 2, 3
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function asyncifyIterable<T>(iterable: Iterable<T> | AsyncIterable<T>): AsyncIterable<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Lazily splits the input async iterable into chunks of specified size.
|
|
46
|
+
* The last chunk may contain fewer items if the total number of items
|
|
47
|
+
* is not evenly divisible by the chunk size.
|
|
48
|
+
* @internal
|
|
49
|
+
*
|
|
50
|
+
* @yields Arrays of items, each containing up to chunkSize items
|
|
51
|
+
*
|
|
52
|
+
* **Example usage:**
|
|
53
|
+
* ```ts
|
|
54
|
+
* const numbers = async function* () {
|
|
55
|
+
* for (let i = 1; i <= 10; i++) yield i;
|
|
56
|
+
* };
|
|
57
|
+
*
|
|
58
|
+
* for await (const chunk of chunkedAsyncIterable(numbers(), 3)) {
|
|
59
|
+
* console.log(chunk); // [1, 2, 3], [4, 5, 6], [7, 8, 9], [10]
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export declare function chunkedAsyncIterable<T>(iterable: AsyncIterable<T> | Iterable<T>, chunkSize: number | (() => number)): AsyncIterable<T[]>;
|
|
64
|
+
/**
|
|
65
|
+
* An async iterator that also supports peeking at the next value without consuming it.
|
|
66
|
+
* Extends both AsyncIterator and AsyncIterable interfaces.
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
export interface PeekableAsyncIterator<T> extends AsyncIterator<T>, AsyncIterable<T> {
|
|
70
|
+
/**
|
|
71
|
+
* Peeks at the next value without consuming it from the iterator.
|
|
72
|
+
* Subsequent calls to peek() will return the same value until next() is called.
|
|
73
|
+
*
|
|
74
|
+
* @returns Promise that resolves to the next value, or undefined if the iterator is exhausted
|
|
75
|
+
*/
|
|
76
|
+
peek(): Promise<T | undefined>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* An async iterable that yields peekable async iterators.
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
export interface PeekableAsyncIterable<T> extends AsyncIterable<T> {
|
|
83
|
+
[Symbol.asyncIterator](): PeekableAsyncIterator<T>;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Wraps an async iterable to provide peek functionality, allowing you to look at
|
|
87
|
+
* the next value without consuming it from the iterator.
|
|
88
|
+
* @internal
|
|
89
|
+
*
|
|
90
|
+
* @param iterable - The async iterable to make peekable
|
|
91
|
+
*
|
|
92
|
+
* **Example usage:**
|
|
93
|
+
* ```ts
|
|
94
|
+
* const numbers = async function* () {
|
|
95
|
+
* yield 1; yield 2; yield 3;
|
|
96
|
+
* };
|
|
97
|
+
*
|
|
98
|
+
* const peekable = peekableAsyncIterable(numbers());
|
|
99
|
+
* const iterator = peekable[Symbol.asyncIterator]();
|
|
100
|
+
*
|
|
101
|
+
* console.log(await iterator.peek()); // 1 (doesn't consume)
|
|
102
|
+
* console.log(await iterator.peek()); // 1 (still doesn't consume)
|
|
103
|
+
* console.log(await iterator.next()); // { value: 1, done: false } (now consumed)
|
|
104
|
+
* console.log(await iterator.peek()); // 2 (next value)
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function peekableAsyncIterable<T>(iterable: AsyncIterable<T> | Iterable<T>): PeekableAsyncIterable<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Merges multiple async iterables into a single async iterable, yielding values concurrently.
|
|
110
|
+
*
|
|
111
|
+
* **Example usage:**
|
|
112
|
+
* ```ts
|
|
113
|
+
* const asyncIterable1 = async function* () {
|
|
114
|
+
* yield 1; yield 3; yield 5;
|
|
115
|
+
* };
|
|
116
|
+
*
|
|
117
|
+
* const asyncIterable2 = async function* () {
|
|
118
|
+
* yield 2; yield 4; yield 6;
|
|
119
|
+
* };
|
|
120
|
+
*
|
|
121
|
+
* for await (const value of mergeAsyncIterables(asyncIterable1(), asyncIterable2())) {
|
|
122
|
+
* console.log(value);
|
|
123
|
+
* }
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
export declare function mergeAsyncIterables<T>(...iterables: AsyncIterable<T>[]): AsyncIterable<T>;
|