@crawlee/utils 3.0.0-alpha.0 → 3.0.0-alpha.3

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.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2018 Apify Technologies s.r.o.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
File without changes
@@ -0,0 +1,7 @@
1
+ export * from './internals/cheerio';
2
+ export * from './internals/extract-urls';
3
+ export * from './internals/general';
4
+ export * from './internals/memory-info';
5
+ export * as social from './internals/social';
6
+ export * from './internals/typedefs';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,yBAAyB,CAAC;AACxC,OAAO,KAAK,MAAM,MAAM,oBAAoB,CAAC;AAC7C,cAAc,sBAAsB,CAAC"}
File without changes
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;AAAA,8DAAoC;AACpC,mEAAyC;AACzC,8DAAoC;AACpC,kEAAwC;AACxC,qEAA6C;AAC7C,+DAAqC"}
package/dist/index.mjs ADDED
@@ -0,0 +1,18 @@
1
+ import mod from "./index.js";
2
+
3
+ export default mod;
4
+ export const URL_NO_COMMAS_REGEX = mod.URL_NO_COMMAS_REGEX;
5
+ export const URL_WITH_COMMAS_REGEX = mod.URL_WITH_COMMAS_REGEX;
6
+ export const downloadListOfUrls = mod.downloadListOfUrls;
7
+ export const entries = mod.entries;
8
+ export const extractUrls = mod.extractUrls;
9
+ export const getMemoryInfo = mod.getMemoryInfo;
10
+ export const htmlToText = mod.htmlToText;
11
+ export const isDocker = mod.isDocker;
12
+ export const keys = mod.keys;
13
+ export const parseContentTypeFromResponse = mod.parseContentTypeFromResponse;
14
+ export const purgeLocalStorage = mod.purgeLocalStorage;
15
+ export const sleep = mod.sleep;
16
+ export const snakeCaseToCamelCase = mod.snakeCaseToCamelCase;
17
+ export const social = mod.social;
18
+ export const weightedAvg = mod.weightedAvg;
@@ -0,0 +1,32 @@
1
+ import { load } from 'cheerio';
2
+ export declare type CheerioRoot = ReturnType<typeof load>;
3
+ /**
4
+ * The function converts a HTML document to a plain text.
5
+ *
6
+ * The plain text generated by the function is similar to a text captured
7
+ * by pressing Ctrl+A and Ctrl+C on a page when loaded in a web browser.
8
+ * The function doesn't aspire to preserve the formatting or to be perfectly correct with respect to HTML specifications.
9
+ * However, it attempts to generate newlines and whitespaces in and around HTML elements
10
+ * to avoid merging distinct parts of text and thus enable extraction of data from the text (e.g. phone numbers).
11
+ *
12
+ * **Example usage**
13
+ * ```javascript
14
+ * const text = htmlToText('<html><body>Some text</body></html>');
15
+ * console.log(text);
16
+ * ```
17
+ *
18
+ * Note that the function uses [cheerio](https://www.npmjs.com/package/cheerio) to parse the HTML.
19
+ * Optionally, to avoid duplicate parsing of HTML and thus improve performance, you can pass
20
+ * an existing Cheerio object to the function instead of the HTML text. The HTML should be parsed
21
+ * with the `decodeEntities` option set to `true`. For example:
22
+ *
23
+ * ```javascript
24
+ * const cheerio = require('cheerio');
25
+ * const html = '<html><body>Some text</body></html>';
26
+ * const text = htmlToText(cheerio.load(html, { decodeEntities: true }));
27
+ * ```
28
+ * @param htmlOrCheerioElement HTML text or parsed HTML represented using a [cheerio](https://www.npmjs.com/package/cheerio) function.
29
+ * @return Plain text
30
+ */
31
+ export declare function htmlToText(htmlOrCheerioElement: string | CheerioRoot): string;
32
+ //# sourceMappingURL=cheerio.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cheerio.d.ts","sourceRoot":"","sources":["../../src/internals/cheerio.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAExC,oBAAY,WAAW,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AAMlD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,UAAU,CAAC,oBAAoB,EAAE,MAAM,GAAG,WAAW,GAAG,MAAM,CAiD7E"}
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.htmlToText = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const cheerio_1 = tslib_1.__importDefault(require("cheerio"));
6
+ // NOTE: We are skipping 'noscript' since it's content is evaluated as text, instead of HTML elements. That damages the results.
7
+ const SKIP_TAGS_REGEX = /^(script|style|canvas|svg|noscript)$/i;
8
+ const BLOCK_TAGS_REGEX = /^(p|h1|h2|h3|h4|h5|h6|ol|ul|li|pre|address|blockquote|dl|div|fieldset|form|table|tr|select|option)$/i;
9
+ /**
10
+ * The function converts a HTML document to a plain text.
11
+ *
12
+ * The plain text generated by the function is similar to a text captured
13
+ * by pressing Ctrl+A and Ctrl+C on a page when loaded in a web browser.
14
+ * The function doesn't aspire to preserve the formatting or to be perfectly correct with respect to HTML specifications.
15
+ * However, it attempts to generate newlines and whitespaces in and around HTML elements
16
+ * to avoid merging distinct parts of text and thus enable extraction of data from the text (e.g. phone numbers).
17
+ *
18
+ * **Example usage**
19
+ * ```javascript
20
+ * const text = htmlToText('<html><body>Some text</body></html>');
21
+ * console.log(text);
22
+ * ```
23
+ *
24
+ * Note that the function uses [cheerio](https://www.npmjs.com/package/cheerio) to parse the HTML.
25
+ * Optionally, to avoid duplicate parsing of HTML and thus improve performance, you can pass
26
+ * an existing Cheerio object to the function instead of the HTML text. The HTML should be parsed
27
+ * with the `decodeEntities` option set to `true`. For example:
28
+ *
29
+ * ```javascript
30
+ * const cheerio = require('cheerio');
31
+ * const html = '<html><body>Some text</body></html>';
32
+ * const text = htmlToText(cheerio.load(html, { decodeEntities: true }));
33
+ * ```
34
+ * @param htmlOrCheerioElement HTML text or parsed HTML represented using a [cheerio](https://www.npmjs.com/package/cheerio) function.
35
+ * @return Plain text
36
+ */
37
+ function htmlToText(htmlOrCheerioElement) {
38
+ if (!htmlOrCheerioElement)
39
+ return '';
40
+ // TODO: Add support for "html" being a Cheerio element, otherwise the only way
41
+ // to use it is e.g. htmlToText($('p').html())) which is inefficient
42
+ // Also, it seems this doesn't work well in CheerioScraper, e.g. htmlToText($)
43
+ // produces really text with a lot of HTML elements in it. Let's just deprecate this sort of usage,
44
+ // and make the parameter "htmlOrCheerioElement"
45
+ const $ = typeof htmlOrCheerioElement === 'function' ? htmlOrCheerioElement : cheerio_1.default.load(htmlOrCheerioElement, { decodeEntities: true });
46
+ let text = '';
47
+ // TODO: the type for elems is very annoying to work with.
48
+ // The correct type is Node[] from cheerio but it needs a lot more casting in each branch, or alternatively,
49
+ // use the is* methods from domhandler (isText, isTag, isComment, etc.)
50
+ // @ts-expect-error
51
+ const process = (elems) => {
52
+ const len = elems ? elems.length : 0;
53
+ for (let i = 0; i < len; i++) {
54
+ const elem = elems[i];
55
+ if (elem.type === 'text') {
56
+ // Compress spaces, unless we're inside <pre> element
57
+ let compr;
58
+ if (elem.parent && elem.parent.tagName === 'pre')
59
+ compr = elem.data;
60
+ else
61
+ compr = elem.data.replace(/\s+/g, ' ');
62
+ // If text is empty or ends with a whitespace, don't add the leading whitepsace
63
+ if (compr.startsWith(' ') && /(^|\s)$/.test(text))
64
+ compr = compr.substr(1);
65
+ text += compr;
66
+ }
67
+ else if (elem.type === 'comment' || SKIP_TAGS_REGEX.test(elem.tagName)) {
68
+ // Skip comments and special elements
69
+ }
70
+ else if (elem.tagName === 'br') {
71
+ text += '\n';
72
+ }
73
+ else if (elem.tagName === 'td') {
74
+ process(elem.children);
75
+ text += '\t';
76
+ }
77
+ else {
78
+ // Block elements must be surrounded by newlines (unless beginning of text)
79
+ const isBlockTag = BLOCK_TAGS_REGEX.test(elem.tagName);
80
+ if (isBlockTag && !/(^|\n)$/.test(text))
81
+ text += '\n';
82
+ process(elem.children);
83
+ if (isBlockTag && !text.endsWith('\n'))
84
+ text += '\n';
85
+ }
86
+ }
87
+ };
88
+ // If HTML document has body, only convert that, otherwise convert the entire HTML
89
+ const $body = $('body');
90
+ process($body.length > 0 ? $body : $.root());
91
+ return text.trim();
92
+ }
93
+ exports.htmlToText = htmlToText;
94
+ //# sourceMappingURL=cheerio.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cheerio.js","sourceRoot":"","sources":["../../src/internals/cheerio.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,39 @@
1
+ /// <reference types="node" />
2
+ export interface DownloadListOfUrlsOptions {
3
+ /**
4
+ * URL to the file
5
+ */
6
+ url: string;
7
+ /**
8
+ * The encoding of the file.
9
+ * @default 'utf8'
10
+ */
11
+ encoding?: BufferEncoding;
12
+ /**
13
+ * Custom regular expression to identify the URLs in the file to extract.
14
+ * The regular expression should be case-insensitive and have global flag set (i.e. `/something/gi`).
15
+ * @default Actor.utils.URL_NO_COMMAS_REGEX
16
+ */
17
+ urlRegExp?: RegExp;
18
+ }
19
+ /**
20
+ * Returns a promise that resolves to an array of urls parsed from the resource available at the provided url.
21
+ * Optionally, custom regular expression and encoding may be provided.
22
+ */
23
+ export declare function downloadListOfUrls(options: DownloadListOfUrlsOptions): Promise<string[]>;
24
+ export interface ExtractUrlsOptions {
25
+ /**
26
+ * The string to extract URLs from.
27
+ */
28
+ string: string;
29
+ /**
30
+ * Custom regular expression
31
+ * @default URL_NO_COMMAS_REGEX
32
+ */
33
+ urlRegExp?: RegExp;
34
+ }
35
+ /**
36
+ * Collects all URLs in an arbitrary string to an array, optionally using a custom regular expression.
37
+ */
38
+ export declare function extractUrls(options: ExtractUrlsOptions): string[];
39
+ //# sourceMappingURL=extract-urls.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-urls.d.ts","sourceRoot":"","sources":["../../src/internals/extract-urls.ts"],"names":[],"mappings":";AAIA,MAAM,WAAW,yBAAyB;IACtC;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IAEZ;;;OAGG;IACH,QAAQ,CAAC,EAAE,cAAc,CAAC;IAE1B;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAkB9F;AAED,MAAM,WAAW,kBAAkB;IAC/B;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,MAAM,EAAE,CAOjE"}
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractUrls = exports.downloadListOfUrls = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const ow_1 = tslib_1.__importDefault(require("ow"));
6
+ const got_scraping_1 = require("got-scraping");
7
+ const general_1 = require("./general");
8
+ /**
9
+ * Returns a promise that resolves to an array of urls parsed from the resource available at the provided url.
10
+ * Optionally, custom regular expression and encoding may be provided.
11
+ */
12
+ async function downloadListOfUrls(options) {
13
+ (0, ow_1.default)(options, ow_1.default.object.exactShape({
14
+ url: ow_1.default.string.url,
15
+ encoding: ow_1.default.optional.string,
16
+ urlRegExp: ow_1.default.optional.regExp,
17
+ }));
18
+ const { url, encoding = 'utf8', urlRegExp = general_1.URL_NO_COMMAS_REGEX } = options;
19
+ // Try to detect wrong urls and fix them. Currently, detects only sharing url instead of csv download one.
20
+ const match = url.match(/^(https:\/\/docs\.google\.com\/spreadsheets\/d\/(?:\w|-)+)\/?/);
21
+ let fixedUrl = url;
22
+ if (match) {
23
+ fixedUrl = `${match[1]}/gviz/tq?tqx=out:csv`;
24
+ }
25
+ const { body: string } = await (0, got_scraping_1.gotScraping)({ url: fixedUrl, encoding });
26
+ return extractUrls({ string, urlRegExp });
27
+ }
28
+ exports.downloadListOfUrls = downloadListOfUrls;
29
+ /**
30
+ * Collects all URLs in an arbitrary string to an array, optionally using a custom regular expression.
31
+ */
32
+ function extractUrls(options) {
33
+ (0, ow_1.default)(options, ow_1.default.object.exactShape({
34
+ string: ow_1.default.string,
35
+ urlRegExp: ow_1.default.optional.regExp,
36
+ }));
37
+ const { string, urlRegExp = general_1.URL_NO_COMMAS_REGEX } = options;
38
+ return string.match(urlRegExp) || [];
39
+ }
40
+ exports.extractUrls = extractUrls;
41
+ //# sourceMappingURL=extract-urls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extract-urls.js","sourceRoot":"","sources":["../../src/internals/extract-urls.ts"],"names":[],"mappings":";;;;AAAA,oDAAoB;AACpB,+CAA2C;AAC3C,uCAAgD;AAsBhD;;;GAGG;AACI,KAAK,UAAU,kBAAkB,CAAC,OAAkC;IACvE,IAAA,YAAE,EAAC,OAAO,EAAE,YAAE,CAAC,MAAM,CAAC,UAAU,CAAC;QAC7B,GAAG,EAAE,YAAE,CAAC,MAAM,CAAC,GAAG;QAClB,QAAQ,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM;QAC5B,SAAS,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM;KAChC,CAAC,CAAC,CAAC;IACJ,MAAM,EAAE,GAAG,EAAE,QAAQ,GAAG,MAAM,EAAE,SAAS,GAAG,6BAAmB,EAAE,GAAG,OAAO,CAAC;IAE5E,0GAA0G;IAC1G,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,+DAA+D,CAAC,CAAC;IACzF,IAAI,QAAQ,GAAG,GAAG,CAAC;IAEnB,IAAI,KAAK,EAAE;QACP,QAAQ,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,sBAAsB,CAAC;KAChD;IAED,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,IAAA,0BAAW,EAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;IACxE,OAAO,WAAW,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;AAC9C,CAAC;AAlBD,gDAkBC;AAeD;;GAEG;AACH,SAAgB,WAAW,CAAC,OAA2B;IACnD,IAAA,YAAE,EAAC,OAAO,EAAE,YAAE,CAAC,MAAM,CAAC,UAAU,CAAC;QAC7B,MAAM,EAAE,YAAE,CAAC,MAAM;QACjB,SAAS,EAAE,YAAE,CAAC,QAAQ,CAAC,MAAM;KAChC,CAAC,CAAC,CAAC;IACJ,MAAM,EAAE,MAAM,EAAE,SAAS,GAAG,6BAAmB,EAAE,GAAG,OAAO,CAAC;IAC5D,OAAO,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;AACzC,CAAC;AAPD,kCAOC"}
@@ -0,0 +1,62 @@
1
+ /// <reference types="node" />
2
+ import type { IncomingMessage } from 'node:http';
3
+ /**
4
+ * Default regular expression to match URLs in a string that may be plain text, JSON, CSV or other. It supports common URL characters
5
+ * and does not support URLs containing commas or spaces. The URLs also may contain Unicode letters (not symbols).
6
+ */
7
+ export declare const URL_NO_COMMAS_REGEX: RegExp;
8
+ /**
9
+ * Regular expression that, in addition to the default regular expression `URL_NO_COMMAS_REGEX`, supports matching commas in URL path and query.
10
+ * Note, however, that this may prevent parsing URLs from comma delimited lists, or the URLs may become malformed.
11
+ */
12
+ export declare const URL_WITH_COMMAS_REGEX: RegExp;
13
+ /**
14
+ * Returns a `Promise` that resolves to true if the code is running in a Docker container.
15
+ */
16
+ export declare function isDocker(forceReset?: boolean): Promise<boolean>;
17
+ /**
18
+ * Computes a weighted average of an array of numbers, complemented by an array of weights.
19
+ * @ignore
20
+ */
21
+ export declare function weightedAvg(arrValues: number[], arrWeights: number[]): number;
22
+ /**
23
+ * Returns a `Promise` that resolves after a specific period of time. This is useful to implement waiting
24
+ * in your code, e.g. to prevent overloading of target website or to avoid bot detection.
25
+ *
26
+ * **Example usage:**
27
+ *
28
+ * ```
29
+ * const { sleep } = require('@crawlee/utils');
30
+ *
31
+ * ...
32
+ *
33
+ * // Sleep 1.5 seconds
34
+ * await sleep(1500);
35
+ * ```
36
+ * @param millis Period of time to sleep, in milliseconds. If not a positive number, the returned promise resolves immediately.
37
+ */
38
+ export declare function sleep(millis?: number): Promise<void>;
39
+ /**
40
+ * Converts SNAKE_CASE to camelCase.
41
+ * @ignore
42
+ */
43
+ export declare function snakeCaseToCamelCase(snakeCaseStr: string): string;
44
+ /**
45
+ * Cleans up the local storage folder created when testing locally.
46
+ * This is useful in the event you are debugging your code locally.
47
+ *
48
+ * Be careful as this will remove the folder you provide and everything in it!
49
+ *
50
+ * @param [folder] The folder to clean up
51
+ */
52
+ export declare function purgeLocalStorage(folder?: string): Promise<void>;
53
+ /**
54
+ * Gets parsed content type from response object
55
+ * @param response HTTP response object
56
+ * @ignore
57
+ */
58
+ export declare function parseContentTypeFromResponse(response: IncomingMessage): {
59
+ type: string;
60
+ charset: BufferEncoding;
61
+ };
62
+ //# sourceMappingURL=general.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../../src/internals/general.ts"],"names":[],"mappings":";AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAMjD;;;GAGG;AACH,eAAO,MAAM,mBAAmB,QAA0J,CAAC;AAE3L;;;GAGG;AACH,eAAO,MAAM,qBAAqB,QAA2J,CAAC;AAkB9L;;GAEG;AACH,wBAAgB,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAK/D;AAED;;;GAGG;AACH,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,MAAM,CAS7E;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEpD;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAUjE;AAED;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAQtE;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,eAAe,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,cAAc,CAAA;CAAE,CA6BjH"}