Skip to content

Commit 3f16290

Browse files
fix: set custom impersonate config
1 parent 1212fc5 commit 3f16290

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/Curl.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import { NODE_LIBCURL_BINDING } from './binding'
1717
import { Easy } from './Easy'
1818
import { Multi } from './Multi'
1919
import { Share } from './Share'
20-
import { type Browser, getCurlOptionsFromBrowser } from './impersonate'
20+
import {
21+
type Browser,
22+
getCurlOptionsFromBrowser,
23+
getCurlOptionsFromBrowserConfig,
24+
type ImpersonateConfig,
25+
} from './impersonate'
2126
import { mergeChunks } from './mergeChunks'
2227
import { parseHeaders, HeaderInfo } from './parseHeaders'
2328
import {
@@ -1095,10 +1100,15 @@ class Curl extends EventEmitter {
10951100
return size * nmemb
10961101
}
10971102

1098-
static impersonate(browser: Browser) {
1103+
static impersonate(browserOrImpersonateConfig: Browser | ImpersonateConfig) {
1104+
let options: CurlOptionValueType
1105+
if (typeof browserOrImpersonateConfig === 'string') {
1106+
options = getCurlOptionsFromBrowser(browserOrImpersonateConfig)
1107+
} else {
1108+
options = getCurlOptionsFromBrowserConfig(browserOrImpersonateConfig)
1109+
}
10991110
const handle = new Curl()
1100-
const curlOptions = getCurlOptionsFromBrowser(browser)
1101-
for (const [option, value] of Object.entries(curlOptions)) {
1111+
for (const [option, value] of Object.entries(options)) {
11021112
// @ts-expect-error todo make type safe
11031113
handle.setOpt(option, value)
11041114
}

lib/curly.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
type Browser,
2222
type ImpersonateConfig,
2323
getCurlOptionsFromBrowser,
24+
getCurlOptionsFromBrowserConfig,
2425
} from './impersonate'
2526

2627
/**
@@ -264,7 +265,7 @@ export interface CurlyFunction extends HttpMethodCalls {
264265
*/
265266
impersonate: (
266267
browserOrImpersonateConfig: Browser | ImpersonateConfig,
267-
defaultOptions: CurlyOptions,
268+
defaultOptions?: CurlyOptions,
268269
) => CurlyFunction
269270

270271
/**
@@ -290,7 +291,10 @@ export function impersonate(
290291
...getCurlOptionsFromBrowser(browserOrImpersonateConfig),
291292
}
292293
} else {
293-
options = { ...defaultOptions, ...browserOrImpersonateConfig }
294+
options = {
295+
...defaultOptions,
296+
...getCurlOptionsFromBrowserConfig(browserOrImpersonateConfig),
297+
}
294298
}
295299
return create(options)
296300
}

0 commit comments

Comments
 (0)