curlnapi-node 0.1.8 → 0.1.9

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 ADDED
@@ -0,0 +1,52 @@
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
+ proxy?: string;
14
+ userAgent?: string;
15
+ referer?: string;
16
+ maxRedirects?: number;
17
+ httpVersion?: number | '2' | '3' | 'h2' | 'h3';
18
+ ipResolve?: 'v4' | 'v6' | string;
19
+ dohUrl?: string;
20
+ ignoreTlsErrors?: boolean;
21
+ headers?: Record<string, string>;
22
+ cookieJar?: {
23
+ setCookie?: (cookieStr: string, url: string) => Promise<any> | any;
24
+ getCookieString?: (url: string) => Promise<string> | string;
25
+ } | undefined;
26
+ }
27
+
28
+ export interface RequestInit {
29
+ method?: HttpMethod;
30
+ headers?: Headers | Record<string, string> | Array<[string, string]>;
31
+ body?: any;
32
+ timeout?: number;
33
+ }
34
+
35
+ export interface ImpitResponse {
36
+ status: number;
37
+ url: string;
38
+ headers: Headers | Array<[string, string]>;
39
+ text(): Promise<string>;
40
+ json(): Promise<any>;
41
+ bytes(): Promise<Uint8Array>;
42
+ body: ReadableStream<any>;
43
+ abort(): void;
44
+ }
45
+
46
+ export class Impit {
47
+ constructor(options?: ImpitOptions);
48
+ fetch(url: string, init?: RequestInit): Promise<ImpitResponse>;
49
+ }
50
+
51
+ export const ImpitWrapper: typeof Impit;
52
+ 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
- module.exports.ImpitResponse = native.ImpitResponse
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.8",
3
+ "version": "0.1.9",
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-win32-x64-msvc": "^0.1.0",
12
- "@wengo/curlnapi-linux-x64-gnu": "^0.1.0"
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
  }