Skip to content

Commit

Permalink
fix: change createCookieAgent types for http-proxy-agent v6 updates
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
renovate[bot] and 3846masa committed Jun 18, 2023
1 parent 4765948 commit e7d97dd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/http-proxy-agent/basic.mjs
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions src/http/__tests__/http-proxy-agent.spec.mts
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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}`);

Expand All @@ -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}`);

Expand All @@ -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}`);

Expand All @@ -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}`, {
Expand All @@ -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}`, {
Expand Down
24 changes: 13 additions & 11 deletions src/http/create_cookie_agent.ts
Expand Up @@ -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<BaseAgent extends http.Agent = http.Agent, Params extends unknown[] = unknown[]>(
BaseAgentClass: new (...params: Params) => BaseAgent,
) {
type WithCookieAgentOptions<T> = T extends http.AgentOptions ? T & CookieAgentOptions : T;
type ConstructorParams = {
[Index in keyof Params]: WithCookieAgentOptions<Params[Index]>;
} & { 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);
Expand Down
10 changes: 9 additions & 1 deletion yarn.lock
Expand Up @@ -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==
Expand Down

0 comments on commit e7d97dd

Please sign in to comment.