@crawlee/http-client 4.0.0-beta.199 → 4.0.0-beta.201
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/base-http-client.d.ts +0 -6
- package/base-http-client.js +12 -12
- package/fetch-http-client.d.ts +1 -1
- package/index.d.ts +1 -1
- package/package.json +3 -3
- package/response.d.ts +2 -5
package/base-http-client.d.ts
CHANGED
|
@@ -48,12 +48,6 @@ export declare abstract class BaseHttpClient implements BaseHttpClientInterface
|
|
|
48
48
|
* automatic redirect following or special error handling.
|
|
49
49
|
*/
|
|
50
50
|
protected abstract fetch(input: Request, init?: RequestInit & CustomFetchOptions): Promise<Response>;
|
|
51
|
-
private applyCookies;
|
|
52
|
-
private setCookies;
|
|
53
|
-
private resolveRequestContext;
|
|
54
|
-
private createAbortSignal;
|
|
55
|
-
private isRedirect;
|
|
56
|
-
private buildRedirectRequest;
|
|
57
51
|
/**
|
|
58
52
|
* Public fetch-like method that handles redirects and uses provided proxy and cookie jar.
|
|
59
53
|
*/
|
package/base-http-client.js
CHANGED
|
@@ -8,7 +8,7 @@ export class BaseHttpClient {
|
|
|
8
8
|
constructor(options) {
|
|
9
9
|
this.#log = options?.logger;
|
|
10
10
|
}
|
|
11
|
-
async applyCookies(request, cookieJar) {
|
|
11
|
+
async #applyCookies(request, cookieJar) {
|
|
12
12
|
try {
|
|
13
13
|
const requestCookies = request.headers.get('cookie') ?? '';
|
|
14
14
|
if (!requestCookies) {
|
|
@@ -36,7 +36,7 @@ export class BaseHttpClient {
|
|
|
36
36
|
}
|
|
37
37
|
return request;
|
|
38
38
|
}
|
|
39
|
-
async setCookies(response, cookieJar) {
|
|
39
|
+
async #setCookies(response, cookieJar) {
|
|
40
40
|
const setCookieHeaders = response.headers.getSetCookie();
|
|
41
41
|
for (const header of setCookieHeaders) {
|
|
42
42
|
try {
|
|
@@ -47,10 +47,10 @@ export class BaseHttpClient {
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
async resolveRequestContext(options) {
|
|
50
|
+
async #resolveRequestContext(options) {
|
|
51
51
|
const proxyUrl = options?.proxyUrl ?? options?.session?.proxyInfo?.url;
|
|
52
52
|
const cookieJar = options?.cookieJar ?? options?.session?.cookieJar ?? (await this.#createDefaultCookieJar());
|
|
53
|
-
const signal = this
|
|
53
|
+
const signal = this.#createAbortSignal(options?.signal, options?.timeoutMillis);
|
|
54
54
|
return {
|
|
55
55
|
proxyUrl,
|
|
56
56
|
cookieJar,
|
|
@@ -63,7 +63,7 @@ export class BaseHttpClient {
|
|
|
63
63
|
const { CookieJar: ToughCookieJar } = await import('tough-cookie');
|
|
64
64
|
return new ToughCookieJar();
|
|
65
65
|
}
|
|
66
|
-
createAbortSignal(signal, timeoutMillis) {
|
|
66
|
+
#createAbortSignal(signal, timeoutMillis) {
|
|
67
67
|
if (signal && timeoutMillis) {
|
|
68
68
|
return AbortSignal.any([signal, AbortSignal.timeout(timeoutMillis)]);
|
|
69
69
|
}
|
|
@@ -72,11 +72,11 @@ export class BaseHttpClient {
|
|
|
72
72
|
}
|
|
73
73
|
return timeoutMillis ? AbortSignal.timeout(timeoutMillis) : undefined;
|
|
74
74
|
}
|
|
75
|
-
isRedirect(response) {
|
|
75
|
+
#isRedirect(response) {
|
|
76
76
|
const status = response.status;
|
|
77
77
|
return status >= 300 && status < 400 && !!response.headers.get('location');
|
|
78
78
|
}
|
|
79
|
-
buildRedirectRequest(currentRequest, response, initialRequest) {
|
|
79
|
+
#buildRedirectRequest(currentRequest, response, initialRequest) {
|
|
80
80
|
const location = response.headers.get('location');
|
|
81
81
|
const nextUrl = new URL(location, response.url ?? currentRequest.url);
|
|
82
82
|
const prevMethod = (currentRequest.method ?? 'GET').toUpperCase();
|
|
@@ -116,11 +116,11 @@ export class BaseHttpClient {
|
|
|
116
116
|
const maxRedirects = 10;
|
|
117
117
|
let currentRequest = initialRequest;
|
|
118
118
|
let redirectCount = 0;
|
|
119
|
-
const { proxyUrl, cookieJar, signal, fingerprint, ignoreTlsErrors } = await this
|
|
119
|
+
const { proxyUrl, cookieJar, signal, fingerprint, ignoreTlsErrors } = await this.#resolveRequestContext(options);
|
|
120
120
|
currentRequest = initialRequest.clone();
|
|
121
121
|
while (true) {
|
|
122
122
|
// Like `fetch`, set the jar cookies on a copy, so that the next redirect hop does not inherit them
|
|
123
|
-
const response = await this.fetch(await this
|
|
123
|
+
const response = await this.fetch(await this.#applyCookies(new Request(currentRequest), cookieJar), {
|
|
124
124
|
signal,
|
|
125
125
|
proxyUrl,
|
|
126
126
|
cookieJar,
|
|
@@ -128,12 +128,12 @@ export class BaseHttpClient {
|
|
|
128
128
|
ignoreTlsErrors,
|
|
129
129
|
redirect: 'manual',
|
|
130
130
|
});
|
|
131
|
-
await this
|
|
132
|
-
if (this
|
|
131
|
+
await this.#setCookies(response, cookieJar);
|
|
132
|
+
if (this.#isRedirect(response)) {
|
|
133
133
|
if (redirectCount++ >= maxRedirects) {
|
|
134
134
|
throw new Error(`Too many redirects (${maxRedirects}) while requesting ${currentRequest.url}`);
|
|
135
135
|
}
|
|
136
|
-
currentRequest = this
|
|
136
|
+
currentRequest = this.#buildRedirectRequest(currentRequest, response, initialRequest);
|
|
137
137
|
continue;
|
|
138
138
|
}
|
|
139
139
|
return response;
|
package/fetch-http-client.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ export declare class FetchHttpClient extends BaseHttpClient {
|
|
|
10
10
|
constructor(options?: {
|
|
11
11
|
logger?: CrawleeLogger;
|
|
12
12
|
});
|
|
13
|
-
fetch(request: Request, options?: RequestInit & CustomFetchOptions): Promise<Response>;
|
|
13
|
+
protected fetch(request: Request, options?: RequestInit & CustomFetchOptions): Promise<Response>;
|
|
14
14
|
}
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/http-client",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.201",
|
|
4
4
|
"description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@crawlee/types": "4.0.0-beta.
|
|
50
|
+
"@crawlee/types": "4.0.0-beta.201",
|
|
51
51
|
"tough-cookie": "^6.0.0"
|
|
52
52
|
},
|
|
53
53
|
"lerna": {
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "5b29925251e92eb69252045f4980153844f58462"
|
|
61
61
|
}
|
package/response.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
export interface IResponseWithUrl extends Response {
|
|
2
|
-
url: string;
|
|
3
|
-
}
|
|
4
1
|
/**
|
|
5
2
|
* A Response class that includes the original request URL.
|
|
6
3
|
*
|
|
7
4
|
* This class extends `Response` from `fetch` API and is fully compatible with this.
|
|
8
5
|
*/
|
|
9
|
-
export declare class ResponseWithUrl extends Response
|
|
10
|
-
url: string;
|
|
6
|
+
export declare class ResponseWithUrl extends Response {
|
|
7
|
+
readonly url: string;
|
|
11
8
|
constructor(body: BodyInit | null, init: ResponseInit & {
|
|
12
9
|
url?: string;
|
|
13
10
|
});
|