Skip to content

Commit 8d8b1cb

Browse files
committed
fix: TypeError: Agent is not a constructor. This happens in the tests due to new https-proxy-agent which changed its default export from a constructor to an object containing a named export
1 parent 884bd27 commit 8d8b1cb

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

packages/teeny-request/src/agents.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,16 @@ export function getAgent(
8383

8484
if (proxy && shouldUseProxy) {
8585
// tslint:disable-next-line variable-name
86-
const Agent = isHttp
86+
let Agent = isHttp
8787
? require('http-proxy-agent')
8888
: require('https-proxy-agent');
8989

90+
if (Agent.HttpsProxyAgent) {
91+
Agent = Agent.HttpsProxyAgent;
92+
} else if (Agent.HttpProxyAgent) {
93+
Agent = Agent.HttpProxyAgent;
94+
}
95+
9096
const proxyOpts = {...parse(proxy), ...poolOptions};
9197
return new Agent(proxyOpts);
9298
}

packages/teeny-request/test/agents.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ import * as sinon from 'sinon';
2323
import {getAgent, pool} from '../src/agents';
2424

2525
// eslint-disable-next-line @typescript-eslint/no-var-requires
26-
const HttpProxyAgent = require('http-proxy-agent');
26+
let HttpProxyAgent = require('http-proxy-agent');
2727
// eslint-disable-next-line @typescript-eslint/no-var-requires
28-
const HttpsProxyAgent = require('https-proxy-agent');
28+
let HttpsProxyAgent = require('https-proxy-agent');
29+
30+
if (HttpProxyAgent.HttpProxyAgent) {
31+
HttpProxyAgent = HttpProxyAgent.HttpProxyAgent;
32+
}
33+
if (HttpsProxyAgent.HttpsProxyAgent) {
34+
HttpsProxyAgent = HttpsProxyAgent.HttpsProxyAgent;
35+
}
2936

3037
describe('agents', () => {
3138
const httpUri = 'http://example.com';
@@ -91,7 +98,7 @@ describe('agents', () => {
9198
const proxy = 'https://hello.there:8080';
9299
const proxyExpected = {
93100
hostname: 'hello.there',
94-
port: 8080,
101+
port: '8080',
95102
protocol: 'https:',
96103
};
97104

0 commit comments

Comments
 (0)