@crawlee/impit-client 4.0.0-rc.0 → 4.0.0-rc.2
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 +1 -1
- package/index.d.ts +3 -5
- package/index.js +20 -7
- package/package.json +9 -8
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@ Crawlee is available as the [`crawlee`](https://www.npmjs.com/package/crawlee) N
|
|
|
34
34
|
|
|
35
35
|
We recommend visiting the [Introduction tutorial](https://crawlee.dev/js/docs/introduction) in Crawlee documentation for more information.
|
|
36
36
|
|
|
37
|
-
> Crawlee requires **Node.js
|
|
37
|
+
> Crawlee requires **Node.js 22.13 or higher**.
|
|
38
38
|
|
|
39
39
|
### With Crawlee CLI
|
|
40
40
|
|
package/index.d.ts
CHANGED
|
@@ -3,15 +3,14 @@ import { BaseHttpClient } from '@crawlee/http-client';
|
|
|
3
3
|
import type { CrawleeLogger } from '@crawlee/types';
|
|
4
4
|
import { type ImpitOptions } from 'impit';
|
|
5
5
|
export declare const Browser: {
|
|
6
|
-
readonly Chrome:
|
|
7
|
-
readonly Firefox:
|
|
6
|
+
readonly Chrome: 'chrome';
|
|
7
|
+
readonly Firefox: 'firefox';
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
10
|
* A HTTP client implementation based on the `impit` library.
|
|
11
11
|
*/
|
|
12
12
|
export declare class ImpitHttpClient extends BaseHttpClient {
|
|
13
13
|
#private;
|
|
14
|
-
private getClient;
|
|
15
14
|
/**
|
|
16
15
|
* @param options.cacheClients Whether to cache `impit` clients between requests. Defaults to `true`.
|
|
17
16
|
*/
|
|
@@ -22,6 +21,5 @@ export declare class ImpitHttpClient extends BaseHttpClient {
|
|
|
22
21
|
/**
|
|
23
22
|
* @inheritDoc
|
|
24
23
|
*/
|
|
25
|
-
fetch(request: Request, options?: RequestInit & CustomFetchOptions): Promise<Response>;
|
|
26
|
-
private resolveImpitBrowser;
|
|
24
|
+
protected fetch(request: Request, options?: RequestInit & CustomFetchOptions): Promise<Response>;
|
|
27
25
|
}
|
package/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { serviceLocator } from '@crawlee/core';
|
|
1
2
|
import { BaseHttpClient, ResponseWithUrl } from '@crawlee/http-client';
|
|
2
3
|
import { Impit } from 'impit';
|
|
3
4
|
import { LruCache } from '@apify/datastructures';
|
|
@@ -44,7 +45,7 @@ export class ImpitHttpClient extends BaseHttpClient {
|
|
|
44
45
|
* instead of rerolling on every call.
|
|
45
46
|
*/
|
|
46
47
|
#impitBrowserByFingerprint = new WeakMap();
|
|
47
|
-
getClient(options) {
|
|
48
|
+
#getClient(options) {
|
|
48
49
|
if (!this.#cacheClients) {
|
|
49
50
|
return new Impit(options);
|
|
50
51
|
}
|
|
@@ -63,7 +64,11 @@ export class ImpitHttpClient extends BaseHttpClient {
|
|
|
63
64
|
*/
|
|
64
65
|
constructor(options) {
|
|
65
66
|
super({ logger: options?.logger });
|
|
66
|
-
const { cacheClients = true, logger
|
|
67
|
+
const { cacheClients = true, logger, ...impitOptions } = options ?? {};
|
|
68
|
+
if (impitOptions.browser) {
|
|
69
|
+
(logger ?? serviceLocator.getLogger()).warningOnce('The `browser` option of ImpitHttpClient overrides the browser fingerprint of the session. ' +
|
|
70
|
+
'Using it with Crawlee crawlers can lead to incompatible Session setups.');
|
|
71
|
+
}
|
|
67
72
|
this.#impitOptions = impitOptions;
|
|
68
73
|
this.#cacheClients = cacheClients;
|
|
69
74
|
}
|
|
@@ -72,8 +77,8 @@ export class ImpitHttpClient extends BaseHttpClient {
|
|
|
72
77
|
*/
|
|
73
78
|
async fetch(request, options) {
|
|
74
79
|
const { proxyUrl, redirect, signal, fingerprint, ignoreTlsErrors } = options ?? {};
|
|
75
|
-
const impitBrowser = this.resolveImpitBrowser(fingerprint);
|
|
76
|
-
const impit = this
|
|
80
|
+
const impitBrowser = this.#impitOptions.browser ? undefined : this.#resolveImpitBrowser(fingerprint);
|
|
81
|
+
const impit = this.#getClient({
|
|
77
82
|
...this.#impitOptions,
|
|
78
83
|
...(impitBrowser ? { browser: impitBrowser } : {}),
|
|
79
84
|
// The per-request flag (from the crawler option or a MITM proxy session)
|
|
@@ -82,11 +87,19 @@ export class ImpitHttpClient extends BaseHttpClient {
|
|
|
82
87
|
proxyUrl,
|
|
83
88
|
followRedirects: redirect === 'follow',
|
|
84
89
|
});
|
|
85
|
-
|
|
90
|
+
// The caller's signal carries the deadline (for crawlers, the navigation window that `extendTimeout()` can
|
|
91
|
+
// push out), so impit's 30 s default must not cut the request short. Without a signal, the default stays.
|
|
92
|
+
const response = await impit.fetch(request, {
|
|
93
|
+
signal: signal ?? undefined,
|
|
94
|
+
...(signal ? { timeout: null } : {}),
|
|
95
|
+
});
|
|
96
|
+
// Once the headers are in, an aborted signal only stops impit's body reads, while the download itself keeps
|
|
97
|
+
// going. Piping the body through the signal cancels the download too.
|
|
98
|
+
const body = signal && response.body ? response.body.pipeThrough(new TransformStream(), { signal }) : response.body;
|
|
86
99
|
// todo - cast shouldn't be needed here, impit returns `Uint8Array`
|
|
87
|
-
return new ResponseWithUrl(
|
|
100
|
+
return new ResponseWithUrl(body, response);
|
|
88
101
|
}
|
|
89
|
-
resolveImpitBrowser(fingerprint) {
|
|
102
|
+
#resolveImpitBrowser(fingerprint) {
|
|
90
103
|
if (!fingerprint?.browser)
|
|
91
104
|
return undefined;
|
|
92
105
|
const cached = this.#impitBrowserByFingerprint.get(fingerprint);
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/impit-client",
|
|
3
|
-
"version": "4.0.0-rc.
|
|
3
|
+
"version": "4.0.0-rc.2",
|
|
4
4
|
"description": "impit-based HTTP client implementation for Crawlee. Impersonates browser requests to avoid bot detection.",
|
|
5
5
|
"engines": {
|
|
6
|
-
"node": ">=22.
|
|
6
|
+
"node": ">=22.13.0"
|
|
7
7
|
},
|
|
8
8
|
"type": "module",
|
|
9
9
|
"exports": {
|
|
@@ -47,11 +47,12 @@
|
|
|
47
47
|
"access": "public"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@apify/datastructures": "^
|
|
51
|
-
"@crawlee/
|
|
52
|
-
"@crawlee/
|
|
53
|
-
"
|
|
54
|
-
"
|
|
50
|
+
"@apify/datastructures": "^3.0.1",
|
|
51
|
+
"@crawlee/core": "4.0.0-rc.2",
|
|
52
|
+
"@crawlee/http-client": "4.0.0-rc.2",
|
|
53
|
+
"@crawlee/types": "4.0.0-rc.2",
|
|
54
|
+
"impit": "^0.14.8",
|
|
55
|
+
"tough-cookie": "^6.1.0"
|
|
55
56
|
},
|
|
56
57
|
"lerna": {
|
|
57
58
|
"command": {
|
|
@@ -60,5 +61,5 @@
|
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
},
|
|
63
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "a9b623229cd7d1bff5063fc82940efebaea47165"
|
|
64
65
|
}
|