@crawlee/utils 4.0.0-beta.256 → 4.0.0-beta.258
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/internal.d.ts +1 -0
- package/internal.js +1 -0
- package/internals/http-request.d.ts +5 -0
- package/internals/http-request.js +36 -0
- package/internals/sitemap.js +7 -17
- package/package.json +4 -4
package/internal.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { CheerioAPI, Cheerio, Element } from './internals/cheerio.js';
|
|
|
3
3
|
export { extractUrlsFromCheerio } from './internals/cheerio.js';
|
|
4
4
|
export { tryAbsoluteURL } from './internals/extract-urls.js';
|
|
5
5
|
export { expandShadowRoots, URL_NO_COMMAS_REGEX, URL_WITH_COMMAS_REGEX } from './internals/general.js';
|
|
6
|
+
export { processHttpRequestOptions } from './internals/http-request.js';
|
|
6
7
|
export * from './internals/iterables.js';
|
|
7
8
|
export { MutationJournal } from './internals/mutation-journal.js';
|
|
8
9
|
export * from './internals/url.js';
|
package/internal.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './internals/blocked.js';
|
|
|
2
2
|
export { extractUrlsFromCheerio } from './internals/cheerio.js';
|
|
3
3
|
export { tryAbsoluteURL } from './internals/extract-urls.js';
|
|
4
4
|
export { expandShadowRoots, URL_NO_COMMAS_REGEX, URL_WITH_COMMAS_REGEX } from './internals/general.js';
|
|
5
|
+
export { processHttpRequestOptions } from './internals/http-request.js';
|
|
5
6
|
export * from './internals/iterables.js';
|
|
6
7
|
export { MutationJournal } from './internals/mutation-journal.js';
|
|
7
8
|
export * from './internals/url.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { HttpRequest, HttpRequestOptions } from '@crawlee/types';
|
|
2
|
+
/**
|
|
3
|
+
* Converts {@link HttpRequestOptions} to a {@link HttpRequest}.
|
|
4
|
+
*/
|
|
5
|
+
export declare function processHttpRequestOptions({ searchParams, form, json, username, password, ...request }: HttpRequestOptions): HttpRequest;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { applySearchParams } from './url.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts {@link HttpRequestOptions} to a {@link HttpRequest}.
|
|
5
|
+
*/
|
|
6
|
+
export function processHttpRequestOptions({ searchParams, form, json, username, password, ...request }) {
|
|
7
|
+
const url = new URL(request.url);
|
|
8
|
+
const headers = new Headers(request.headers);
|
|
9
|
+
applySearchParams(url, searchParams);
|
|
10
|
+
if ([request.body, form, json].filter((value) => value !== undefined).length > 1) {
|
|
11
|
+
throw new Error('At most one of `body`, `form` and `json` may be specified in sendRequest arguments');
|
|
12
|
+
}
|
|
13
|
+
const body = (() => {
|
|
14
|
+
if (form !== undefined) {
|
|
15
|
+
return Readable.from(new URLSearchParams(form).toString());
|
|
16
|
+
}
|
|
17
|
+
if (json !== undefined) {
|
|
18
|
+
return Readable.from(JSON.stringify(json));
|
|
19
|
+
}
|
|
20
|
+
if (request.body !== undefined) {
|
|
21
|
+
return Readable.from(request.body);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
})();
|
|
25
|
+
if (form !== undefined && !headers.has('content-type')) {
|
|
26
|
+
headers.set('content-type', 'application/x-www-form-urlencoded');
|
|
27
|
+
}
|
|
28
|
+
if (json !== undefined && !headers.has('content-type')) {
|
|
29
|
+
headers.set('content-type', 'application/json');
|
|
30
|
+
}
|
|
31
|
+
if (username !== undefined || password !== undefined) {
|
|
32
|
+
const encodedAuth = Buffer.from(`${username ?? ''}:${password ?? ''}`).toString('base64');
|
|
33
|
+
headers.set('authorization', `Basic ${encodedAuth}`);
|
|
34
|
+
}
|
|
35
|
+
return { ...request, body, url, headers };
|
|
36
|
+
}
|
package/internals/sitemap.js
CHANGED
|
@@ -24,23 +24,13 @@ class SitemapTxtParser extends Transform {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
#processBuffer(input, finalize) {
|
|
27
|
-
this.#buffer
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (const url of parts) {
|
|
35
|
-
this.push({ type: 'url', loc: url });
|
|
36
|
-
}
|
|
37
|
-
this.#buffer = '';
|
|
38
|
-
}
|
|
39
|
-
else if (parts.length > 0) {
|
|
40
|
-
for (const url of parts.slice(0, -1)) {
|
|
41
|
-
this.push({ type: 'url', loc: url });
|
|
42
|
-
}
|
|
43
|
-
this.#buffer = parts.at(-1);
|
|
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 });
|
|
44
34
|
}
|
|
45
35
|
}
|
|
46
36
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/utils",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.258",
|
|
4
4
|
"description": "A set of shared utilities that can be used by crawlers",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.13.0"
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@apify/validations": "^1.1.2",
|
|
46
|
-
"@crawlee/http-client": "4.0.0-beta.
|
|
47
|
-
"@crawlee/types": "4.0.0-beta.
|
|
46
|
+
"@crawlee/http-client": "4.0.0-beta.258",
|
|
47
|
+
"@crawlee/types": "4.0.0-beta.258",
|
|
48
48
|
"@types/sax": "^1.2.7",
|
|
49
49
|
"cheerio": "^1.2.0",
|
|
50
50
|
"domhandler": "^6.0.1",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "a285256b541becf78e187043be064db917c643ea"
|
|
67
67
|
}
|