curlnapi-node 0.1.8 → 0.1.10
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/index.d.ts +53 -0
- package/index.wrapper.js +38 -5
- package/package.json +9 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type HttpMethod =
|
|
2
|
+
| 'GET'
|
|
3
|
+
| 'POST'
|
|
4
|
+
| 'PUT'
|
|
5
|
+
| 'DELETE'
|
|
6
|
+
| 'PATCH'
|
|
7
|
+
| 'HEAD'
|
|
8
|
+
| 'OPTIONS';
|
|
9
|
+
|
|
10
|
+
export interface ImpitOptions {
|
|
11
|
+
timeout?: number;
|
|
12
|
+
followRedirects?: boolean;
|
|
13
|
+
browser?: string;
|
|
14
|
+
proxyUrl?: string;
|
|
15
|
+
userAgent?: string;
|
|
16
|
+
referer?: string;
|
|
17
|
+
maxRedirects?: number;
|
|
18
|
+
httpVersion?: number | '2' | '3' | 'h2' | 'h3';
|
|
19
|
+
ipResolve?: 'v4' | 'v6' | string;
|
|
20
|
+
dohUrl?: string;
|
|
21
|
+
ignoreTlsErrors?: boolean;
|
|
22
|
+
headers?: Record<string, string>;
|
|
23
|
+
cookieJar?: {
|
|
24
|
+
setCookie?: (cookieStr: string, url: string) => Promise<any> | any;
|
|
25
|
+
getCookieString?: (url: string) => Promise<string> | string;
|
|
26
|
+
} | undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface RequestInit {
|
|
30
|
+
method?: HttpMethod;
|
|
31
|
+
headers?: Headers | Record<string, string> | Array<[string, string]>;
|
|
32
|
+
body?: any;
|
|
33
|
+
timeout?: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ImpitResponse {
|
|
37
|
+
status: number;
|
|
38
|
+
url: string;
|
|
39
|
+
headers: Headers | Array<[string, string]>;
|
|
40
|
+
text(): Promise<string>;
|
|
41
|
+
json(): Promise<any>;
|
|
42
|
+
bytes(): Promise<Uint8Array>;
|
|
43
|
+
body: ReadableStream<any>;
|
|
44
|
+
abort(): void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export class Impit {
|
|
48
|
+
constructor(options?: ImpitOptions);
|
|
49
|
+
fetch(url: string, init?: RequestInit): Promise<ImpitResponse>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const ImpitWrapper: typeof Impit;
|
|
53
|
+
export const ImpitResponse: any;
|
package/index.wrapper.js
CHANGED
|
@@ -50,15 +50,20 @@ class Impit extends native.Impit {
|
|
|
50
50
|
const jsCookieJar = options?.cookieJar
|
|
51
51
|
super({
|
|
52
52
|
...options,
|
|
53
|
-
cookieJar: jsCookieJar ? {
|
|
54
|
-
setCookie: async (args) => jsCookieJar.setCookie?.bind?.(jsCookieJar)(...args),
|
|
55
|
-
getCookieString: async (args) => jsCookieJar.getCookieString?.bind?.(jsCookieJar)(args),
|
|
56
|
-
} : undefined,
|
|
57
53
|
headers: headersToObject(options?.headers),
|
|
58
54
|
})
|
|
55
|
+
this._jsCookieJar = jsCookieJar
|
|
59
56
|
}
|
|
60
57
|
async fetch(resource, init) {
|
|
61
58
|
const { url, signal, ...options } = await parseFetchOptions(resource, init)
|
|
59
|
+
if (this._jsCookieJar) {
|
|
60
|
+
try {
|
|
61
|
+
const cookieStr = await this._jsCookieJar.getCookieString?.(url)
|
|
62
|
+
if (cookieStr && !options.headers.some(([k]) => String(k).toLowerCase() === 'cookie')) {
|
|
63
|
+
options.headers.push(['Cookie', cookieStr])
|
|
64
|
+
}
|
|
65
|
+
} catch {}
|
|
66
|
+
}
|
|
62
67
|
const waitForAbort = new Promise((_, reject) => {
|
|
63
68
|
signal?.throwIfAborted?.()
|
|
64
69
|
signal?.addEventListener?.('abort', () => reject(signal.reason), { once: true })
|
|
@@ -67,13 +72,41 @@ class Impit extends native.Impit {
|
|
|
67
72
|
const originalResponse = await Promise.race([response, waitForAbort])
|
|
68
73
|
signal?.throwIfAborted?.()
|
|
69
74
|
signal?.addEventListener?.('abort', () => { originalResponse.abort() })
|
|
75
|
+
const rawHeaders = originalResponse.headers
|
|
70
76
|
if (typeof Headers !== 'undefined') {
|
|
71
77
|
Object.defineProperty(originalResponse, 'headers', { value: new Headers(originalResponse.headers) })
|
|
72
78
|
}
|
|
79
|
+
if (this._jsCookieJar && Array.isArray(rawHeaders)) {
|
|
80
|
+
try {
|
|
81
|
+
for (const [k, v] of rawHeaders) {
|
|
82
|
+
if (String(k).toLowerCase() === 'set-cookie') {
|
|
83
|
+
await this._jsCookieJar.setCookie?.(v, url)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
} catch {}
|
|
87
|
+
}
|
|
73
88
|
return originalResponse
|
|
74
89
|
}
|
|
75
90
|
}
|
|
76
91
|
|
|
77
92
|
module.exports.Impit = Impit
|
|
78
93
|
module.exports.ImpitWrapper = native.ImpitWrapper
|
|
79
|
-
|
|
94
|
+
// ImpitResponse is an interface in TypeScript, not a runtime class.
|
|
95
|
+
// We export a dummy object for compatibility if needed, but it's not strictly required at runtime.
|
|
96
|
+
module.exports.ImpitResponse = class ImpitResponse {}
|
|
97
|
+
module.exports.Browser = {
|
|
98
|
+
Chrome: 'chrome',
|
|
99
|
+
Firefox: 'firefox',
|
|
100
|
+
}
|
|
101
|
+
module.exports.HttpMethod = {
|
|
102
|
+
GET: 'GET',
|
|
103
|
+
POST: 'POST',
|
|
104
|
+
PUT: 'PUT',
|
|
105
|
+
DELETE: 'DELETE',
|
|
106
|
+
HEAD: 'HEAD',
|
|
107
|
+
OPTIONS: 'OPTIONS',
|
|
108
|
+
PATCH: 'PATCH',
|
|
109
|
+
TRACE: 'TRACE',
|
|
110
|
+
CONNECT: 'CONNECT',
|
|
111
|
+
}
|
|
112
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "curlnapi-node",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Node binding for curl-impersonate with a requests-like API",
|
|
6
6
|
"main": "index.wrapper.js",
|
|
@@ -8,7 +8,13 @@
|
|
|
8
8
|
"test": "node examples/test.js"
|
|
9
9
|
},
|
|
10
10
|
"optionalDependencies": {
|
|
11
|
-
"@wengo/curlnapi-
|
|
12
|
-
"@wengo/curlnapi-
|
|
11
|
+
"@wengo/curlnapi-linux-x64-gnu": "^0.1.0",
|
|
12
|
+
"@wengo/curlnapi-win32-x64-msvc": "^0.1.0"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@apify/datastructures": "^2.0.3",
|
|
16
|
+
"@crawlee/core": "^3.15.3",
|
|
17
|
+
"tough-cookie": "^6.0.0",
|
|
18
|
+
"tsx": "^4.21.0"
|
|
13
19
|
}
|
|
14
20
|
}
|