@crawlee/utils 4.0.0-beta.26 → 4.0.0-beta.261
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 +15 -15
- package/index.d.ts +10 -17
- package/index.js +8 -16
- package/internal.d.ts +12 -0
- package/internal.js +11 -0
- package/internals/blocked.d.ts +0 -1
- package/internals/blocked.js +0 -1
- package/internals/cheerio.d.ts +1 -3
- package/internals/cheerio.js +25 -16
- package/internals/extract-microdata.d.ts +24 -0
- package/internals/extract-microdata.js +122 -0
- package/internals/extract-urls.d.ts +1 -2
- package/internals/extract-urls.js +18 -17
- package/internals/general.d.ts +0 -25
- package/internals/general.js +2 -110
- package/internals/http-request.d.ts +5 -0
- package/internals/http-request.js +36 -0
- package/internals/iterables.d.ts +0 -80
- package/internals/iterables.js +0 -126
- package/internals/mutation-journal.d.ts +29 -0
- package/internals/mutation-journal.js +173 -0
- package/internals/{open_graph_parser.d.ts → open-graph-parser.d.ts} +2 -3
- package/internals/{open_graph_parser.js → open-graph-parser.js} +8 -9
- package/internals/robots.d.ts +35 -15
- package/internals/robots.js +66 -30
- package/internals/schemas.d.ts +114 -0
- package/internals/schemas.js +115 -0
- package/internals/sitemap.d.ts +44 -3
- package/internals/sitemap.js +177 -97
- package/internals/social.d.ts +4 -3
- package/internals/social.js +36 -11
- package/internals/url.d.ts +67 -1
- package/internals/url.js +118 -2
- package/internals/validation.d.ts +9 -0
- package/internals/validation.js +16 -0
- package/package.json +16 -15
- 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 +0 -2
- package/internals/chunk.d.ts.map +0 -1
- package/internals/chunk.js +0 -40
- package/internals/chunk.js.map +0 -1
- package/internals/debug.d.ts +0 -32
- package/internals/debug.d.ts.map +0 -1
- package/internals/debug.js +0 -41
- 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/iterables.d.ts.map +0 -1
- package/internals/iterables.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/system-info/cpu-info.d.ts +0 -64
- package/internals/system-info/cpu-info.d.ts.map +0 -1
- package/internals/system-info/cpu-info.js +0 -211
- package/internals/system-info/cpu-info.js.map +0 -1
- package/internals/system-info/memory-info.d.ts +0 -28
- package/internals/system-info/memory-info.d.ts.map +0 -1
- package/internals/system-info/memory-info.js +0 -118
- package/internals/system-info/memory-info.js.map +0 -1
- package/internals/system-info/ps-tree.d.ts +0 -18
- package/internals/system-info/ps-tree.d.ts.map +0 -1
- package/internals/system-info/ps-tree.js +0 -145
- package/internals/system-info/ps-tree.js.map +0 -1
- package/internals/typedefs.d.ts +0 -5
- package/internals/typedefs.d.ts.map +0 -1
- package/internals/typedefs.js +0 -9
- package/internals/typedefs.js.map +0 -1
- package/internals/url.d.ts.map +0 -1
- package/internals/url.js.map +0 -1
package/internals/sitemap.js
CHANGED
|
@@ -3,132 +3,134 @@ import { PassThrough, pipeline, Readable, Transform } from 'node:stream';
|
|
|
3
3
|
import { StringDecoder } from 'node:string_decoder';
|
|
4
4
|
import { createGunzip } from 'node:zlib';
|
|
5
5
|
import { FetchHttpClient } from '@crawlee/http-client';
|
|
6
|
-
import {
|
|
7
|
-
import sax from 'sax';
|
|
8
|
-
import MIMEType from 'whatwg-mimetype';
|
|
9
|
-
import log from '@apify/log';
|
|
6
|
+
import { MIMEType } from 'whatwg-mimetype';
|
|
10
7
|
import { mergeAsyncIterables } from './iterables.js';
|
|
11
|
-
import {
|
|
8
|
+
import { RobotsTxtFile } from './robots.js';
|
|
9
|
+
import { filterUrl } from './url.js';
|
|
12
10
|
class SitemapTxtParser extends Transform {
|
|
13
|
-
decoder = new StringDecoder('utf8');
|
|
14
|
-
buffer = '';
|
|
11
|
+
#decoder = new StringDecoder('utf8');
|
|
12
|
+
#buffer = '';
|
|
15
13
|
constructor() {
|
|
16
14
|
super({
|
|
17
15
|
readableObjectMode: true,
|
|
18
16
|
transform: (chunk, _encoding, callback) => {
|
|
19
|
-
this
|
|
17
|
+
this.#processBuffer(this.#decoder.write(chunk), false);
|
|
20
18
|
callback();
|
|
21
19
|
},
|
|
22
20
|
flush: (callback) => {
|
|
23
|
-
this
|
|
21
|
+
this.#processBuffer(this.#decoder.end(), true);
|
|
24
22
|
callback();
|
|
25
23
|
},
|
|
26
24
|
});
|
|
27
25
|
}
|
|
28
|
-
processBuffer(input, finalize) {
|
|
29
|
-
this
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
for (const url of parts) {
|
|
37
|
-
this.push({ type: 'url', loc: url });
|
|
38
|
-
}
|
|
39
|
-
this.buffer = '';
|
|
40
|
-
}
|
|
41
|
-
else if (parts.length > 0) {
|
|
42
|
-
for (const url of parts.slice(0, -1)) {
|
|
43
|
-
this.push({ type: 'url', loc: url });
|
|
44
|
-
}
|
|
45
|
-
this.buffer = parts.at(-1);
|
|
26
|
+
#processBuffer(input, finalize) {
|
|
27
|
+
const lines = (this.#buffer + input).split('\n');
|
|
28
|
+
// The text after the last line break may be a partial line, so it waits for the next chunk.
|
|
29
|
+
this.#buffer = finalize ? '' : lines.pop();
|
|
30
|
+
for (const line of lines) {
|
|
31
|
+
const url = line.trim();
|
|
32
|
+
if (url.length > 0) {
|
|
33
|
+
this.push({ type: 'url', loc: url });
|
|
46
34
|
}
|
|
47
35
|
}
|
|
48
36
|
}
|
|
49
37
|
}
|
|
50
38
|
class SitemapXmlParser extends Transform {
|
|
51
|
-
decoder = new StringDecoder('utf8');
|
|
52
|
-
parser
|
|
53
|
-
rootTagName;
|
|
54
|
-
currentTag = undefined;
|
|
55
|
-
url = {};
|
|
56
|
-
|
|
39
|
+
#decoder = new StringDecoder('utf8');
|
|
40
|
+
#parser;
|
|
41
|
+
#rootTagName;
|
|
42
|
+
#currentTag = undefined;
|
|
43
|
+
#url = {};
|
|
44
|
+
static async create() {
|
|
45
|
+
const { SAXParser } = (await import('sax')).default;
|
|
46
|
+
return new SitemapXmlParser(new SAXParser(true));
|
|
47
|
+
}
|
|
48
|
+
constructor(parser) {
|
|
57
49
|
super({
|
|
58
50
|
readableObjectMode: true,
|
|
59
51
|
transform: (chunk, _encoding, callback) => {
|
|
60
|
-
this
|
|
52
|
+
this.#parser.write(this.#decoder.write(chunk));
|
|
61
53
|
callback();
|
|
62
54
|
},
|
|
63
55
|
flush: (callback) => {
|
|
64
|
-
const rest = this
|
|
56
|
+
const rest = this.#decoder.end();
|
|
65
57
|
if (rest.length > 0) {
|
|
66
|
-
this
|
|
58
|
+
this.#parser.write(rest);
|
|
67
59
|
}
|
|
68
|
-
this
|
|
60
|
+
this.#parser.end();
|
|
69
61
|
callback();
|
|
70
62
|
},
|
|
71
63
|
});
|
|
72
|
-
this
|
|
73
|
-
this
|
|
74
|
-
this
|
|
75
|
-
this
|
|
76
|
-
this
|
|
64
|
+
this.#parser = parser;
|
|
65
|
+
this.#parser.onopentag = this.#onOpenTag.bind(this);
|
|
66
|
+
this.#parser.onclosetag = this.#onCloseTag.bind(this);
|
|
67
|
+
this.#parser.ontext = this.#onText.bind(this);
|
|
68
|
+
this.#parser.oncdata = this.#onText.bind(this);
|
|
69
|
+
this.#parser.onerror = this.destroy.bind(this);
|
|
77
70
|
}
|
|
78
|
-
onOpenTag(node) {
|
|
79
|
-
if (this
|
|
71
|
+
#onOpenTag(node) {
|
|
72
|
+
if (this.#rootTagName !== undefined) {
|
|
80
73
|
if (node.name === 'loc' ||
|
|
81
74
|
node.name === 'lastmod' ||
|
|
82
75
|
node.name === 'priority' ||
|
|
83
76
|
node.name === 'changefreq') {
|
|
84
|
-
this
|
|
77
|
+
this.#currentTag = node.name;
|
|
85
78
|
}
|
|
86
79
|
}
|
|
87
80
|
if (node.name === 'urlset') {
|
|
88
|
-
this
|
|
81
|
+
this.#rootTagName = 'urlset';
|
|
89
82
|
}
|
|
90
83
|
if (node.name === 'sitemapindex') {
|
|
91
|
-
this
|
|
84
|
+
this.#rootTagName = 'sitemapindex';
|
|
92
85
|
}
|
|
93
86
|
}
|
|
94
|
-
onCloseTag(name) {
|
|
87
|
+
#onCloseTag(name) {
|
|
95
88
|
if (name === 'loc' || name === 'lastmod' || name === 'priority' || name === 'changefreq') {
|
|
96
|
-
this
|
|
89
|
+
this.#currentTag = undefined;
|
|
97
90
|
}
|
|
98
|
-
if (name === 'url'
|
|
99
|
-
|
|
100
|
-
|
|
91
|
+
if (name === 'url') {
|
|
92
|
+
if (this.#url.loc !== undefined) {
|
|
93
|
+
this.push({ type: 'url', ...this.#url, loc: this.#url.loc });
|
|
94
|
+
}
|
|
95
|
+
this.#url = {};
|
|
101
96
|
}
|
|
102
97
|
}
|
|
103
|
-
onText(text) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
98
|
+
#onText(text) {
|
|
99
|
+
text = text.trim();
|
|
100
|
+
// Whitespace around a CDATA section arrives as separate text events.
|
|
101
|
+
if (text === '') {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (this.#currentTag === 'loc') {
|
|
105
|
+
if (this.#rootTagName === 'sitemapindex') {
|
|
106
|
+
this.push({ type: 'sitemapUrl', url: text });
|
|
107
107
|
}
|
|
108
|
-
if (this
|
|
109
|
-
this
|
|
110
|
-
this
|
|
108
|
+
if (this.#rootTagName === 'urlset') {
|
|
109
|
+
this.#url ??= {};
|
|
110
|
+
this.#url.loc = text;
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
if (this.#currentTag === 'lastmod') {
|
|
114
|
+
const lastmod = new Date(text);
|
|
115
|
+
if (!Number.isNaN(lastmod.getTime())) {
|
|
116
|
+
this.#url.lastmod = lastmod;
|
|
117
|
+
}
|
|
116
118
|
}
|
|
117
|
-
if (this
|
|
118
|
-
this
|
|
119
|
+
if (this.#currentTag === 'priority') {
|
|
120
|
+
this.#url.priority = Number(text);
|
|
119
121
|
}
|
|
120
|
-
if (this
|
|
122
|
+
if (this.#currentTag === 'changefreq') {
|
|
121
123
|
if (['always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never'].includes(text)) {
|
|
122
|
-
this
|
|
124
|
+
this.#url.changefreq = text;
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
129
|
export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
128
|
-
const { httpClient = new FetchHttpClient(), emitNestedSitemaps = false, maxDepth = Infinity, sitemapRetries = 3, timeoutMillis: timeout = 30000, reportNetworkErrors = true, } = options ?? {};
|
|
130
|
+
const { httpClient = new FetchHttpClient(), emitNestedSitemaps = false, maxDepth = Infinity, sitemapRetries = 3, timeoutMillis: timeout = 30000, reportNetworkErrors = true, nestedSitemapFilter, enqueueStrategy = 'same-hostname', logger, } = options ?? {};
|
|
129
131
|
const sources = [...initialSources];
|
|
130
132
|
const visitedSitemapUrls = new Set();
|
|
131
|
-
const createParser = (contentType = '', url) => {
|
|
133
|
+
const createParser = async (contentType = '', url) => {
|
|
132
134
|
let mimeType;
|
|
133
135
|
try {
|
|
134
136
|
mimeType = new MIMEType(contentType);
|
|
@@ -137,22 +139,27 @@ export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
|
137
139
|
mimeType = null;
|
|
138
140
|
}
|
|
139
141
|
if (mimeType?.isXML() || url?.pathname.endsWith('.xml')) {
|
|
140
|
-
return
|
|
142
|
+
return SitemapXmlParser.create();
|
|
141
143
|
}
|
|
142
144
|
if (mimeType?.essence === 'text/plain' || url?.pathname.endsWith('.txt')) {
|
|
143
145
|
return new SitemapTxtParser();
|
|
144
146
|
}
|
|
147
|
+
// A name like `sitemap.gz` says nothing about what was compressed. Sitemaps are XML unless named otherwise.
|
|
148
|
+
if (mimeType?.essence === 'application/gzip') {
|
|
149
|
+
return SitemapXmlParser.create();
|
|
150
|
+
}
|
|
145
151
|
throw new Error(`Unsupported sitemap content type (contentType = ${contentType}, url = ${url?.toString()})`);
|
|
146
152
|
};
|
|
147
153
|
while (sources.length > 0) {
|
|
148
154
|
const source = sources.shift();
|
|
149
155
|
if ((source?.depth ?? 0) > maxDepth) {
|
|
150
|
-
log.debug(`Skipping sitemap ${source.type === 'url' ? source.url : ''} because it reached max depth ${maxDepth}.`);
|
|
151
156
|
continue;
|
|
152
157
|
}
|
|
153
158
|
let items = null;
|
|
159
|
+
// Parent URL, parsed once and reused as the origin for the strategy checks below.
|
|
160
|
+
let sitemapUrl;
|
|
154
161
|
if (source.type === 'url') {
|
|
155
|
-
|
|
162
|
+
sitemapUrl = new URL(source.url);
|
|
156
163
|
visitedSitemapUrls.add(sitemapUrl.toString());
|
|
157
164
|
let retriesLeft = sitemapRetries + 1;
|
|
158
165
|
while (retriesLeft-- > 0) {
|
|
@@ -162,11 +169,11 @@ export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
|
162
169
|
sitemapResponse = await httpClient.sendRequest(new Request(sitemapUrl, {
|
|
163
170
|
method: 'GET',
|
|
164
171
|
headers: {
|
|
165
|
-
accept: '
|
|
172
|
+
accept: '*/*',
|
|
166
173
|
},
|
|
167
174
|
}), {
|
|
168
175
|
proxyUrl,
|
|
169
|
-
timeout,
|
|
176
|
+
timeoutMillis: timeout,
|
|
170
177
|
});
|
|
171
178
|
}
|
|
172
179
|
catch (error) {
|
|
@@ -178,6 +185,7 @@ export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
|
178
185
|
if (sitemapResponse.body === null) {
|
|
179
186
|
break;
|
|
180
187
|
}
|
|
188
|
+
const { fileTypeStream } = await import('file-type');
|
|
181
189
|
const streamWithType = await fileTypeStream(Readable.fromWeb(sitemapResponse.body));
|
|
182
190
|
if (streamWithType.fileType !== undefined) {
|
|
183
191
|
contentType = streamWithType.fileType.mime;
|
|
@@ -191,7 +199,7 @@ export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
|
191
199
|
sitemapUrl.pathname = sitemapUrl.pathname.substring(0, sitemapUrl.pathname.length - 3);
|
|
192
200
|
}
|
|
193
201
|
}
|
|
194
|
-
items = pipeline(streamWithType, isGzipped ? createGunzip() : new PassThrough(), createParser(contentType ?? undefined, sitemapUrl), (e) => {
|
|
202
|
+
items = pipeline(streamWithType, isGzipped ? createGunzip() : new PassThrough(), await createParser(contentType ?? undefined, sitemapUrl), (e) => {
|
|
195
203
|
if (e !== undefined && e !== null) {
|
|
196
204
|
error = { type: 'parser', error: e };
|
|
197
205
|
}
|
|
@@ -214,28 +222,53 @@ export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
|
214
222
|
}
|
|
215
223
|
}
|
|
216
224
|
catch (e) {
|
|
217
|
-
|
|
225
|
+
logger?.warning(`Malformed sitemap content: ${sitemapUrl}, ${retriesLeft === 0 ? 'no retries left.' : 'retrying...'} (${e})`);
|
|
218
226
|
}
|
|
219
227
|
}
|
|
220
228
|
}
|
|
221
229
|
else if (source.type === 'raw') {
|
|
222
|
-
items = pipeline(Readable.from([source.content]), createParser('text/xml'), (error) => {
|
|
230
|
+
items = pipeline(Readable.from([source.content]), await createParser('text/xml'), (error) => {
|
|
223
231
|
if (error !== undefined) {
|
|
224
|
-
|
|
232
|
+
logger?.warning(`Malformed sitemap content: ${error}`);
|
|
225
233
|
}
|
|
226
234
|
});
|
|
227
235
|
}
|
|
228
236
|
if (items === null) {
|
|
229
237
|
continue;
|
|
230
238
|
}
|
|
239
|
+
// URL entries dropped by the enqueue strategy filter, reported in one warning per sitemap after
|
|
240
|
+
// the loop (per-entry warnings could flood the log; individual drops are logged at debug level).
|
|
241
|
+
let droppedUrlEntries = 0;
|
|
231
242
|
for await (const item of items) {
|
|
232
243
|
if (item.type === 'sitemapUrl' && !visitedSitemapUrls.has(item.url)) {
|
|
244
|
+
if (nestedSitemapFilter && !nestedSitemapFilter(item.url)) {
|
|
245
|
+
logger?.debug(`Skipping sitemap ${item.url} due to nestedSitemapFilter.`);
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
// Keep only nested sitemaps matching the strategy (and using http(s)) relative to the
|
|
249
|
+
// parent. Raw string sources have no parent URL, so the check is skipped.
|
|
250
|
+
if (source.type === 'url') {
|
|
251
|
+
const { allowed, reason } = filterUrl(item.url, sitemapUrl, enqueueStrategy);
|
|
252
|
+
if (!allowed) {
|
|
253
|
+
logger?.warning(`Skipping nested sitemap ${item.url} (parent ${source.url}): ${reason}.`);
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
233
257
|
sources.push({ type: 'url', url: item.url, depth: (source.depth ?? 0) + 1 });
|
|
234
258
|
if (emitNestedSitemaps) {
|
|
235
259
|
yield { loc: item.url, originSitemapUrl: null };
|
|
236
260
|
}
|
|
237
261
|
}
|
|
238
262
|
if (item.type === 'url') {
|
|
263
|
+
// Keep only URL entries that match the enqueue strategy relative to the parent (see above).
|
|
264
|
+
if (source.type === 'url') {
|
|
265
|
+
const { allowed, reason } = filterUrl(item.loc, sitemapUrl, enqueueStrategy);
|
|
266
|
+
if (!allowed) {
|
|
267
|
+
droppedUrlEntries++;
|
|
268
|
+
logger?.debug(`Skipping sitemap URL ${item.loc} (parent ${source.url}): ${reason}.`);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
239
272
|
yield {
|
|
240
273
|
...item,
|
|
241
274
|
originSitemapUrl: source.type === 'url'
|
|
@@ -244,6 +277,9 @@ export async function* parseSitemap(initialSources, proxyUrl, options) {
|
|
|
244
277
|
};
|
|
245
278
|
}
|
|
246
279
|
}
|
|
280
|
+
if (droppedUrlEntries > 0 && source.type === 'url') {
|
|
281
|
+
logger?.warning(`Skipped ${droppedUrlEntries} URL(s) from sitemap ${source.url} not matching enqueue strategy '${enqueueStrategy}' (or using a non-http(s) scheme). Enable debug logs to see each skipped URL.`);
|
|
282
|
+
}
|
|
247
283
|
}
|
|
248
284
|
}
|
|
249
285
|
/**
|
|
@@ -285,7 +321,7 @@ export class Sitemap {
|
|
|
285
321
|
* @param proxyUrl URL of a proxy to be used for fetching sitemap contents
|
|
286
322
|
*/
|
|
287
323
|
static async load(urls, proxyUrl, parseSitemapOptions) {
|
|
288
|
-
return await this
|
|
324
|
+
return await this.#parse((Array.isArray(urls) ? urls : [urls]).map((url) => ({ type: 'url', url })), proxyUrl, parseSitemapOptions);
|
|
289
325
|
}
|
|
290
326
|
/**
|
|
291
327
|
* Parse XML sitemap content from a string and return URLs of referenced pages. If the sitemap references other sitemaps, they will be loaded via HTTP.
|
|
@@ -293,16 +329,17 @@ export class Sitemap {
|
|
|
293
329
|
* @param proxyUrl URL of a proxy to be used for fetching sitemap contents
|
|
294
330
|
*/
|
|
295
331
|
static async fromXmlString(content, proxyUrl, parseSitemapOptions) {
|
|
296
|
-
return await this
|
|
332
|
+
return await this.#parse([{ type: 'raw', content }], proxyUrl, parseSitemapOptions);
|
|
297
333
|
}
|
|
298
|
-
static async parse(sources, proxyUrl, parseSitemapOptions) {
|
|
334
|
+
static async #parse(sources, proxyUrl, parseSitemapOptions) {
|
|
299
335
|
const urls = [];
|
|
300
336
|
try {
|
|
301
337
|
for await (const item of parseSitemap(sources, proxyUrl, parseSitemapOptions)) {
|
|
302
338
|
urls.push(item.loc);
|
|
303
339
|
}
|
|
304
340
|
}
|
|
305
|
-
catch {
|
|
341
|
+
catch (e) {
|
|
342
|
+
parseSitemapOptions?.logger?.warning(`Sitemap.load: Failed to load sitemap, returning empty result. (${e})`);
|
|
306
343
|
return new Sitemap([]);
|
|
307
344
|
}
|
|
308
345
|
return new Sitemap(urls);
|
|
@@ -316,7 +353,19 @@ export class Sitemap {
|
|
|
316
353
|
* @returns An async iterable with the discovered sitemap URLs.
|
|
317
354
|
*/
|
|
318
355
|
export async function* discoverValidSitemaps(urls, options = {}) {
|
|
319
|
-
const { proxyUrl, httpClient = new FetchHttpClient() } = options;
|
|
356
|
+
const { proxyUrl, timeoutMillis = 60_000, signal: externalSignal, requestTimeoutMillis = 20_000, httpClient = new FetchHttpClient(), logger, } = options;
|
|
357
|
+
const controller = new AbortController();
|
|
358
|
+
const timeoutHandle = setTimeout(() => controller.abort(), timeoutMillis);
|
|
359
|
+
const onExternalAbort = () => controller.abort();
|
|
360
|
+
if (externalSignal) {
|
|
361
|
+
if (externalSignal.aborted) {
|
|
362
|
+
controller.abort();
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
externalSignal.addEventListener('abort', onExternalAbort, { once: true });
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const signal = controller.signal;
|
|
320
369
|
const sitemapUrls = new Set();
|
|
321
370
|
const addSitemapUrl = (url) => {
|
|
322
371
|
const sizeBefore = sitemapUrls.size;
|
|
@@ -331,7 +380,11 @@ export async function* discoverValidSitemaps(urls, options = {}) {
|
|
|
331
380
|
return false;
|
|
332
381
|
}
|
|
333
382
|
try {
|
|
334
|
-
const response = await httpClient.sendRequest(new Request(url, { method: 'HEAD' }), {
|
|
383
|
+
const response = await httpClient.sendRequest(new Request(url, { method: 'HEAD' }), {
|
|
384
|
+
proxyUrl,
|
|
385
|
+
timeoutMillis: requestTimeoutMillis,
|
|
386
|
+
signal,
|
|
387
|
+
});
|
|
335
388
|
return response.status >= 200 && response.status < 400;
|
|
336
389
|
}
|
|
337
390
|
catch {
|
|
@@ -343,17 +396,24 @@ export async function* discoverValidSitemaps(urls, options = {}) {
|
|
|
343
396
|
return;
|
|
344
397
|
}
|
|
345
398
|
try {
|
|
346
|
-
const robotsFile = await
|
|
347
|
-
|
|
399
|
+
const robotsFile = await RobotsTxtFile.find(domainUrls[0], {
|
|
400
|
+
proxyUrl,
|
|
401
|
+
timeoutMillis: requestTimeoutMillis,
|
|
402
|
+
signal,
|
|
403
|
+
httpClient,
|
|
404
|
+
logger,
|
|
405
|
+
});
|
|
406
|
+
// Surface all referenced sitemaps, including cross-host; scoping happens at load time.
|
|
407
|
+
for (const sitemapUrl of robotsFile.getSitemaps({ enqueueStrategy: 'all' })) {
|
|
348
408
|
if (addSitemapUrl(sitemapUrl)) {
|
|
349
409
|
yield sitemapUrl;
|
|
350
410
|
}
|
|
351
411
|
}
|
|
352
412
|
}
|
|
353
413
|
catch (err) {
|
|
354
|
-
|
|
414
|
+
logger?.warning(`Failed to fetch robots.txt file for ${hostname}`, { error: err });
|
|
355
415
|
}
|
|
356
|
-
const sitemapUrl = domainUrls.find((url) => /sitemap
|
|
416
|
+
const sitemapUrl = domainUrls.find((url) => /sitemap(?:_index)?\.(?:xml|txt)(?:\.gz)?$/i.test(url));
|
|
357
417
|
if (sitemapUrl !== undefined) {
|
|
358
418
|
if (addSitemapUrl(sitemapUrl)) {
|
|
359
419
|
yield sitemapUrl;
|
|
@@ -361,14 +421,29 @@ export async function* discoverValidSitemaps(urls, options = {}) {
|
|
|
361
421
|
}
|
|
362
422
|
else {
|
|
363
423
|
const firstUrl = new URL(domainUrls[0]);
|
|
424
|
+
// The guessed candidates are well-known paths on the same origin, so the query string
|
|
425
|
+
// and fragment of the input URL are not part of them - carrying them over probes (and
|
|
426
|
+
// yields) URLs like `/sitemap.xml?page=2`, which servers may well answer with a 404.
|
|
427
|
+
firstUrl.search = '';
|
|
428
|
+
firstUrl.hash = '';
|
|
364
429
|
const possibleSitemapPathnames = ['/sitemap.xml', '/sitemap.txt', '/sitemap_index.xml'];
|
|
365
|
-
|
|
430
|
+
const candidateSitemapUrls = possibleSitemapPathnames.map((pathname) => {
|
|
366
431
|
firstUrl.pathname = pathname;
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
432
|
+
return firstUrl.toString();
|
|
433
|
+
});
|
|
434
|
+
const candidateResults = await Promise.allSettled(candidateSitemapUrls.map(urlExists));
|
|
435
|
+
for (const [index, result] of candidateResults.entries()) {
|
|
436
|
+
const candidateSitemapUrl = candidateSitemapUrls[index];
|
|
437
|
+
if (result.status === 'fulfilled') {
|
|
438
|
+
if (result.value && addSitemapUrl(candidateSitemapUrl)) {
|
|
439
|
+
yield candidateSitemapUrl;
|
|
370
440
|
}
|
|
371
441
|
}
|
|
442
|
+
else {
|
|
443
|
+
logger?.debug(`Failed to check sitemap candidate ${candidateSitemapUrl} for ${hostname}`, {
|
|
444
|
+
error: result.reason,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
372
447
|
}
|
|
373
448
|
}
|
|
374
449
|
};
|
|
@@ -380,12 +455,17 @@ export async function* discoverValidSitemaps(urls, options = {}) {
|
|
|
380
455
|
}, {});
|
|
381
456
|
const iterables = Object.entries(groupedUrls).map(([hostname, domainUrls]) => discoverSitemapsForDomainUrls(hostname, domainUrls));
|
|
382
457
|
const discoveredUrls = new Set();
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
458
|
+
try {
|
|
459
|
+
for await (const url of mergeAsyncIterables(...iterables)) {
|
|
460
|
+
if (discoveredUrls.has(url)) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
discoveredUrls.add(url);
|
|
464
|
+
yield url;
|
|
386
465
|
}
|
|
387
|
-
|
|
388
|
-
|
|
466
|
+
}
|
|
467
|
+
finally {
|
|
468
|
+
clearTimeout(timeoutHandle);
|
|
469
|
+
externalSignal?.removeEventListener('abort', onExternalAbort);
|
|
389
470
|
}
|
|
390
471
|
}
|
|
391
|
-
//# sourceMappingURL=sitemap.js.map
|
package/internals/social.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export declare function emailsFromText(text: string): string[];
|
|
|
19
19
|
/**
|
|
20
20
|
* The function extracts email addresses from a list of URLs.
|
|
21
21
|
* Basically it looks for all `mailto:` URLs and returns valid email addresses from them.
|
|
22
|
+
* A `mailto:` URL can list several comma-separated, percent-encoded addresses and end with a query
|
|
23
|
+
* such as `?subject=Hello`, see [RFC 6068](https://www.rfc-editor.org/rfc/rfc6068).
|
|
22
24
|
* Note that the function preserves the order of emails and keep duplicates.
|
|
23
25
|
* @param urls Array of URLs.
|
|
24
26
|
* @return Array of emails addresses found.
|
|
@@ -225,7 +227,7 @@ export declare const TWITTER_REGEX: RegExp;
|
|
|
225
227
|
* ```
|
|
226
228
|
* import { social } from 'crawlee';
|
|
227
229
|
*
|
|
228
|
-
* const matches = text.match(social.
|
|
230
|
+
* const matches = text.match(social.TWITTER_REGEX_GLOBAL);
|
|
229
231
|
* if (matches) console.log(`${matches.length} Twitter profiles found!`);
|
|
230
232
|
* ```
|
|
231
233
|
*/
|
|
@@ -488,5 +490,4 @@ export declare const DISCORD_REGEX_GLOBAL: RegExp;
|
|
|
488
490
|
* so that the caller doesn't need to parse the HTML document again, if needed.
|
|
489
491
|
* @return An object with the social handles.
|
|
490
492
|
*/
|
|
491
|
-
export declare function parseHandlesFromHtml(html: string, data?: Record<string, unknown> | null): SocialHandles
|
|
492
|
-
//# sourceMappingURL=social.d.ts.map
|
|
493
|
+
export declare function parseHandlesFromHtml(html: string, data?: Record<string, unknown> | null): Promise<SocialHandles>;
|
package/internals/social.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import * as cheerio from 'cheerio';
|
|
2
1
|
import { htmlToText } from './cheerio.js';
|
|
3
2
|
// Regex inspired by https://zapier.com/blog/extract-links-email-phone-regex/
|
|
4
|
-
|
|
3
|
+
// The dot-atom local part and domain labels use RFC 5321 length bounds ({1,64}, {0,62})
|
|
4
|
+
// instead of unbounded quantifiers to avoid quadratic backtracking (ReDoS) on long
|
|
5
|
+
// dotted or hyphenated inputs, e.g. text scraped by parseHandlesFromHtml().
|
|
6
|
+
const EMAIL_REGEX_STRING = '(?:[a-z0-9!#$%&\'*+/=?^_`{|}~-]{1,64}(?:\\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]{1,64}){0,32}|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\\])';
|
|
5
7
|
/**
|
|
6
8
|
* Regular expression to exactly match a single email address.
|
|
7
9
|
* It has the following form: `/^...$/i`.
|
|
@@ -28,6 +30,8 @@ export function emailsFromText(text) {
|
|
|
28
30
|
/**
|
|
29
31
|
* The function extracts email addresses from a list of URLs.
|
|
30
32
|
* Basically it looks for all `mailto:` URLs and returns valid email addresses from them.
|
|
33
|
+
* A `mailto:` URL can list several comma-separated, percent-encoded addresses and end with a query
|
|
34
|
+
* such as `?subject=Hello`, see [RFC 6068](https://www.rfc-editor.org/rfc/rfc6068).
|
|
31
35
|
* Note that the function preserves the order of emails and keep duplicates.
|
|
32
36
|
* @param urls Array of URLs.
|
|
33
37
|
* @return Array of emails addresses found.
|
|
@@ -42,9 +46,20 @@ export function emailsFromUrls(urls) {
|
|
|
42
46
|
continue;
|
|
43
47
|
if (!EMAIL_URL_PREFIX_REGEX.test(url))
|
|
44
48
|
continue;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
// Characters like `?` and `,` inside an address must be percent-encoded, so it's safe
|
|
50
|
+
// to split on them before decoding.
|
|
51
|
+
const [addresses] = url.replace(EMAIL_URL_PREFIX_REGEX, '').split('?');
|
|
52
|
+
for (const address of addresses.split(',')) {
|
|
53
|
+
let email = address.trim();
|
|
54
|
+
try {
|
|
55
|
+
email = decodeURIComponent(email).trim();
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// Keep the raw value if it isn't valid percent-encoding
|
|
59
|
+
}
|
|
60
|
+
if (EMAIL_REGEX.test(email))
|
|
61
|
+
emails.push(email);
|
|
62
|
+
}
|
|
48
63
|
}
|
|
49
64
|
return emails;
|
|
50
65
|
}
|
|
@@ -136,8 +151,18 @@ export function phonesFromUrls(urls) {
|
|
|
136
151
|
continue;
|
|
137
152
|
if (!PHONE_URL_PREFIX_REGEX.test(url))
|
|
138
153
|
continue;
|
|
139
|
-
|
|
140
|
-
|
|
154
|
+
// Per RFC 3966, parameters such as `;ext=89` can follow the number, and `-`, `.`, `(` and `)`
|
|
155
|
+
// are only visual separators. They are checked without separators too, so a one-digit country
|
|
156
|
+
// code like in `tel:+1-555-123-4567` is accepted.
|
|
157
|
+
const [number] = url.replace(PHONE_URL_PREFIX_REGEX, '').split(';');
|
|
158
|
+
let phone = number.trim();
|
|
159
|
+
try {
|
|
160
|
+
phone = decodeURIComponent(phone).trim();
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// Keep the raw value if it isn't valid percent-encoding
|
|
164
|
+
}
|
|
165
|
+
if (PHONE_REGEX.test(phone) || PHONE_REGEX.test(phone.replace(/[-.() ]/g, '')))
|
|
141
166
|
phones.push(phone);
|
|
142
167
|
}
|
|
143
168
|
return phones;
|
|
@@ -324,7 +349,7 @@ export const TWITTER_REGEX = new RegExp(`^${TWITTER_REGEX_STRING}$`, 'i');
|
|
|
324
349
|
* ```
|
|
325
350
|
* import { social } from 'crawlee';
|
|
326
351
|
*
|
|
327
|
-
* const matches = text.match(social.
|
|
352
|
+
* const matches = text.match(social.TWITTER_REGEX_GLOBAL);
|
|
328
353
|
* if (matches) console.log(`${matches.length} Twitter profiles found!`);
|
|
329
354
|
* ```
|
|
330
355
|
*/
|
|
@@ -587,7 +612,8 @@ export const DISCORD_REGEX_GLOBAL = new RegExp(DISCORD_REGEX_STRING, 'ig');
|
|
|
587
612
|
* so that the caller doesn't need to parse the HTML document again, if needed.
|
|
588
613
|
* @return An object with the social handles.
|
|
589
614
|
*/
|
|
590
|
-
export function parseHandlesFromHtml(html, data = null) {
|
|
615
|
+
export async function parseHandlesFromHtml(html, data = null) {
|
|
616
|
+
const cheerio = await import('cheerio/slim');
|
|
591
617
|
const result = {
|
|
592
618
|
emails: [],
|
|
593
619
|
phones: [],
|
|
@@ -606,7 +632,7 @@ export function parseHandlesFromHtml(html, data = null) {
|
|
|
606
632
|
const $ = cheerio.load(html, { xml: { decodeEntities: true } });
|
|
607
633
|
if (data)
|
|
608
634
|
data.$ = $;
|
|
609
|
-
const text = htmlToText($);
|
|
635
|
+
const text = await htmlToText($);
|
|
610
636
|
if (data)
|
|
611
637
|
data.text = text;
|
|
612
638
|
// NOTE: we need to parse each text separately, orherwise we might concatenate unrelated texts
|
|
@@ -644,4 +670,3 @@ export function parseHandlesFromHtml(html, data = null) {
|
|
|
644
670
|
}
|
|
645
671
|
return result;
|
|
646
672
|
}
|
|
647
|
-
//# sourceMappingURL=social.js.map
|