From e7d97dd50095b6183ab70661c401c2583fb352b9 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 19 Jun 2023 03:03:23 +0900 Subject: [PATCH] fix: change createCookieAgent types for http-proxy-agent v6 updates * chore(npm): update dependency http-proxy-agent to v7 * fix: change createCookieAgent types for http-proxy-agent v6 updates In most use cases, this change has no impact. --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: 3846masa <3846masahiro+git@gmail.com> --- examples/http-proxy-agent/basic.mjs | 2 +- package.json | 2 +- src/http/__tests__/http-proxy-agent.spec.mts | 14 ++++++------ src/http/create_cookie_agent.ts | 24 +++++++++++--------- yarn.lock | 10 +++++++- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/examples/http-proxy-agent/basic.mjs b/examples/http-proxy-agent/basic.mjs index a42e621f..60f24a66 100644 --- a/examples/http-proxy-agent/basic.mjs +++ b/examples/http-proxy-agent/basic.mjs @@ -12,7 +12,7 @@ proxyServer.listen(9000); const HttpProxyCookieAgent = createCookieAgent(httpProxyAgent.HttpProxyAgent); const jar = new CookieJar(); -const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: '127.0.0.1', port: 9000 }); +const agent = new HttpProxyCookieAgent('http://127.0.0.1:9000', { cookies: { jar } }); http.get('http://httpbin.org/cookies/set/session/userid', { agent }, (_res) => { jar.getCookies('http://httpbin.org').then((cookies) => { diff --git a/package.json b/package.json index 4a79c057..59aa8033 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,7 @@ "axios": "1.4.0", "deasync": "0.1.28", "got": "12.6.1", - "http-proxy-agent": "5.0.0", + "http-proxy-agent": "7.0.0", "needle": "3.2.0", "node-fetch": "3.3.1", "npm-run-all": "4.1.5", diff --git a/src/http/__tests__/http-proxy-agent.spec.mts b/src/http/__tests__/http-proxy-agent.spec.mts index 43a49782..4092650a 100644 --- a/src/http/__tests__/http-proxy-agent.spec.mts +++ b/src/http/__tests__/http-proxy-agent.spec.mts @@ -34,7 +34,7 @@ test('should set cookies to CookieJar from Set-Cookie header', async (t) => { res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await request(`http://localhost:${port}`, { agent, @@ -57,7 +57,7 @@ test('should set cookies to CookieJar from multiple Set-Cookie headers', async ( res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await request(`http://localhost:${port}`, { agent, @@ -81,7 +81,7 @@ test('should send cookies from CookieJar', async (t) => { res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await jar.setCookie('key=value', `http://localhost:${port}`); @@ -102,7 +102,7 @@ test('should send cookies from both a request options and CookieJar', async (t) res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await jar.setCookie('key1=value1', `http://localhost:${port}`); @@ -124,7 +124,7 @@ test('should send cookies from a request options when the key is duplicated in b res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await jar.setCookie('key=notexpected', `http://localhost:${port}`); @@ -149,7 +149,7 @@ test('should emit error when CookieJar#getCookies throws error.', async (t) => { res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await t.throwsAsync(() => { return request(`http://localhost:${port}`, { @@ -173,7 +173,7 @@ test('should emit error when CookieJar#setCookie throws error.', async (t) => { res.end(); }, ]); - const agent = new HttpProxyCookieAgent({ cookies: { jar }, host: 'localhost', port: proxyPort }); + const agent = new HttpProxyCookieAgent(`http://localhost:${proxyPort}`, { cookies: { jar } }); await t.throwsAsync(() => { return request(`http://localhost:${port}`, { diff --git a/src/http/create_cookie_agent.ts b/src/http/create_cookie_agent.ts index d4df5d5e..644251a7 100644 --- a/src/http/create_cookie_agent.ts +++ b/src/http/create_cookie_agent.ts @@ -33,22 +33,24 @@ const kReimplicitHeader = Symbol('reimplicitHeader'); const kRecreateFirstChunk = Symbol('recreateFirstChunk'); const kOverrideRequest = Symbol('overrideRequest'); -export function createCookieAgent< - BaseAgent extends http.Agent = http.Agent, - BaseAgentOptions = unknown, - BaseAgentConstructorRestParams extends unknown[] = unknown[], ->(BaseAgentClass: new (options: BaseAgentOptions, ...rest: BaseAgentConstructorRestParams) => BaseAgent) { - type Options = BaseAgentOptions & CookieAgentOptions; +export function createCookieAgent( + BaseAgentClass: new (...params: Params) => BaseAgent, +) { + type WithCookieAgentOptions = T extends http.AgentOptions ? T & CookieAgentOptions : T; + type ConstructorParams = { + [Index in keyof Params]: WithCookieAgentOptions; + } & { length: Params['length'] }; // @ts-expect-error ... class CookieAgent extends BaseAgentClass { [kCookieOptions]: CookieOptions | undefined; - constructor( - { cookies: cookieOptions, ...options }: Options = {} as Options, - ...rest: BaseAgentConstructorRestParams - ) { - super(options as BaseAgentOptions, ...rest); + constructor(...params: ConstructorParams) { + const { cookies: cookieOptions } = (params.find((opt) => { + return opt != null && typeof opt === 'object' && 'cookies' in opt; + }) ?? {}) as CookieAgentOptions; + + super(...(params as Params)); if (cookieOptions) { validateCookieOptions(cookieOptions); diff --git a/yarn.lock b/yarn.lock index 246662b2..b9c97abb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4195,7 +4195,15 @@ http-cache-semantics@^4.1.0, http-cache-semantics@^4.1.1: resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.1.tgz#abe02fcb2985460bf0323be664436ec3476a6d5a" integrity sha512-er295DKPVsV82j5kw1Gjt+ADA/XYHsajl82cGNQG2eyoPkvgUhX+nDIyelzhIWbbsXP39EHcI6l5tYs2FYqYXQ== -http-proxy-agent@5.0.0, http-proxy-agent@^5.0.0: +http-proxy-agent@7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-7.0.0.tgz#e9096c5afd071a3fce56e6252bb321583c124673" + integrity sha512-+ZT+iBxVUQ1asugqnD6oWoRiS25AkjNfG085dKJGtGxkdwLQrMKU5wJr2bOOFAXzKcTuqq+7fZlTMgG3SRfIYQ== + dependencies: + agent-base "^7.1.0" + debug "^4.3.4" + +http-proxy-agent@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-5.0.0.tgz#5129800203520d434f142bc78ff3c170800f2b43" integrity sha512-n2hY8YdoRE1i7r6M0w9DIw5GgZN0G25P8zLCRQ8rjXtTU3vsNFBI/vWK/UIeE6g5MUUz6avwAPXmL6Fy9D/90w==