@crawlee/http-client 4.0.0-beta.99 → 4.0.0-rc.0

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.
@@ -1,5 +1,4 @@
1
- import type { BaseHttpClient as BaseHttpClientInterface, CrawleeLogger, SendRequestOptions, SessionFingerprint } from '@crawlee/types';
2
- import { CookieJar } from 'tough-cookie';
1
+ import type { BaseHttpClient as BaseHttpClientInterface, CookieJar, CrawleeLogger, SendRequestOptions, SessionFingerprint } from '@crawlee/types';
3
2
  /**
4
3
  * Per-request options handed to a concrete client's `fetch` implementation.
5
4
  */
@@ -25,6 +24,14 @@ export interface CustomFetchOptions {
25
24
  * rest on a best-effort basis. Sourced from `SendRequestOptions.session.fingerprint`.
26
25
  */
27
26
  fingerprint?: SessionFingerprint;
27
+ /**
28
+ * When `true`, TLS certificate errors should be ignored for this request.
29
+ * Set when `SendRequestOptions.ignoreTlsErrors` is passed (e.g. from the
30
+ * `ignoreTlsErrors` crawler option) or when the session's proxy is a MITM
31
+ * proxy (`session.proxyInfo.ignoreTlsErrors`). Best-effort: clients that
32
+ * cannot disable TLS verification ignore it.
33
+ */
34
+ ignoreTlsErrors?: boolean;
28
35
  }
29
36
  /**
30
37
  * Base HTTP client that provides fetch-like `sendRequest` with Crawlee-managed
@@ -32,7 +39,7 @@ export interface CustomFetchOptions {
32
39
  * implement only the low-level network call in `fetch`.
33
40
  */
34
41
  export declare abstract class BaseHttpClient implements BaseHttpClientInterface {
35
- private log?;
42
+ #private;
36
43
  constructor(options?: {
37
44
  logger?: CrawleeLogger;
38
45
  });
@@ -1,13 +1,12 @@
1
- import { CookieJar } from 'tough-cookie';
2
1
  /**
3
2
  * Base HTTP client that provides fetch-like `sendRequest` with Crawlee-managed
4
3
  * behaviors (redirect handling, proxy and cookie handling). Concrete clients
5
4
  * implement only the low-level network call in `fetch`.
6
5
  */
7
6
  export class BaseHttpClient {
8
- log;
7
+ #log;
9
8
  constructor(options) {
10
- this.log = options?.logger;
9
+ this.#log = options?.logger;
11
10
  }
12
11
  async applyCookies(request, cookieJar) {
13
12
  try {
@@ -27,13 +26,13 @@ export class BaseHttpClient {
27
26
  .split(/; */)
28
27
  .filter(Boolean)
29
28
  .map((pair) => merged.setCookie(pair, request.url)));
30
- const cookieString = merged.getCookieStringSync(request.url);
29
+ const cookieString = await merged.getCookieString(request.url);
31
30
  if (cookieString) {
32
31
  request.headers.set('cookie', cookieString);
33
32
  }
34
33
  }
35
34
  catch (e) {
36
- this.log?.warning(`Failed to get cookies for URL "${request.url}": ${e.message}`);
35
+ this.#log?.warning(`Failed to get cookies for URL "${request.url}": ${e.message}`);
37
36
  }
38
37
  return request;
39
38
  }
@@ -44,21 +43,26 @@ export class BaseHttpClient {
44
43
  await cookieJar.setCookie(header, response.url);
45
44
  }
46
45
  catch (e) {
47
- this.log?.warning(`Failed to set cookie for URL "${response.url}": ${e.message}`);
46
+ this.#log?.warning(`Failed to set cookie for URL "${response.url}": ${e.message}`);
48
47
  }
49
48
  }
50
49
  }
51
- resolveRequestContext(options) {
50
+ async resolveRequestContext(options) {
52
51
  const proxyUrl = options?.proxyUrl ?? options?.session?.proxyInfo?.url;
53
- const cookieJar = options?.cookieJar ?? options?.session?.cookieJar ?? new CookieJar();
52
+ const cookieJar = options?.cookieJar ?? options?.session?.cookieJar ?? (await this.#createDefaultCookieJar());
54
53
  const signal = this.createAbortSignal(options?.signal, options?.timeoutMillis);
55
54
  return {
56
55
  proxyUrl,
57
- cookieJar: cookieJar,
56
+ cookieJar,
58
57
  signal,
59
58
  fingerprint: options?.session?.fingerprint,
59
+ ignoreTlsErrors: options?.ignoreTlsErrors || options?.session?.proxyInfo?.ignoreTlsErrors,
60
60
  };
61
61
  }
62
+ async #createDefaultCookieJar() {
63
+ const { CookieJar: ToughCookieJar } = await import('tough-cookie');
64
+ return new ToughCookieJar();
65
+ }
62
66
  createAbortSignal(signal, timeoutMillis) {
63
67
  if (signal && timeoutMillis) {
64
68
  return AbortSignal.any([signal, AbortSignal.timeout(timeoutMillis)]);
@@ -104,7 +108,7 @@ export class BaseHttpClient {
104
108
  const maxRedirects = 10;
105
109
  let currentRequest = initialRequest;
106
110
  let redirectCount = 0;
107
- const { proxyUrl, cookieJar, signal, fingerprint } = this.resolveRequestContext(options);
111
+ const { proxyUrl, cookieJar, signal, fingerprint, ignoreTlsErrors } = await this.resolveRequestContext(options);
108
112
  currentRequest = initialRequest.clone();
109
113
  while (true) {
110
114
  await this.applyCookies(currentRequest, cookieJar);
@@ -113,6 +117,7 @@ export class BaseHttpClient {
113
117
  proxyUrl,
114
118
  cookieJar,
115
119
  fingerprint,
120
+ ignoreTlsErrors,
116
121
  redirect: 'manual',
117
122
  });
118
123
  await this.setCookies(response, cookieJar);
@@ -1,3 +1,4 @@
1
+ import type { CrawleeLogger } from '@crawlee/types';
1
2
  import { BaseHttpClient, type CustomFetchOptions } from './base-http-client.js';
2
3
  /**
3
4
  * A HTTP client implementation using the native `fetch` API.
@@ -5,5 +6,9 @@ import { BaseHttpClient, type CustomFetchOptions } from './base-http-client.js';
5
6
  * This implementation does not support proxying.
6
7
  */
7
8
  export declare class FetchHttpClient extends BaseHttpClient {
9
+ #private;
10
+ constructor(options?: {
11
+ logger?: CrawleeLogger;
12
+ });
8
13
  fetch(request: Request, options?: RequestInit & CustomFetchOptions): Promise<Response>;
9
14
  }
@@ -5,7 +5,16 @@ import { BaseHttpClient } from './base-http-client.js';
5
5
  * This implementation does not support proxying.
6
6
  */
7
7
  export class FetchHttpClient extends BaseHttpClient {
8
+ #logger;
9
+ constructor(options) {
10
+ super(options);
11
+ this.#logger = options?.logger;
12
+ }
8
13
  async fetch(request, options) {
14
+ if (options?.ignoreTlsErrors) {
15
+ this.#logger?.warningOnce('FetchHttpClient cannot disable TLS certificate verification, the `ignoreTlsErrors` option is ignored. ' +
16
+ 'Install the optional @crawlee/impit-client dependency to make it work.');
17
+ }
9
18
  return fetch(request, options);
10
19
  }
11
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/http-client",
3
- "version": "4.0.0-beta.99",
3
+ "version": "4.0.0-rc.0",
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.99",
50
+ "@crawlee/types": "4.0.0-rc.0",
51
51
  "tough-cookie": "^6.0.0"
52
52
  },
53
53
  "lerna": {
@@ -57,5 +57,5 @@
57
57
  }
58
58
  }
59
59
  },
60
- "gitHead": "ad2748380941842bb10cff100f4b4caad92049e3"
60
+ "gitHead": "79ab33dacdacb83e0197e6516d145f3aceef80c7"
61
61
  }