Skip to content

Commit

Permalink
Add retry
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 29, 2023
1 parent 14b9e01 commit b4b70a5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/https-proxy-agent/package.json
Expand Up @@ -34,10 +34,12 @@
"debug": "4"
},
"devDependencies": {
"@types/async-retry": "^1.4.5",
"@types/debug": "4",
"@types/jest": "^29.5.1",
"@types/node": "^14.18.43",
"async-listen": "^2.1.0",
"async-retry": "^1.3.3",
"jest": "^29.5.0",
"proxy": "workspace:*",
"ts-jest": "^29.1.0",
Expand Down
38 changes: 29 additions & 9 deletions packages/https-proxy-agent/test/e2e.test.ts
@@ -1,3 +1,4 @@
import retry from 'async-retry';
import { req, json } from 'agent-base';
import { HttpsProxyAgent } from '../src';

Expand All @@ -10,15 +11,34 @@ interface NordVPNServer {

jest.setTimeout(30000);

async function findNordVpnServer(): Promise<NordVPNServer> {
const res = await req('https://nordvpn.com/api/server');
const body = await json(res);
const server = (body as NordVPNServer[]).find((s) => s.features.proxy_ssl);
if (!server) {
throw new Error('Could not find `https` proxy server from NordVPN');
}
return server;
}
const findNordVpnServer = () =>
retry(
async (): Promise<NordVPNServer> => {
const res = await req('https://nordvpn.com/api/server');
if (res.statusCode !== 200) {
res.socket.destroy();
throw new Error(`Status code: ${res.statusCode}`);
}
const body = await json(res);
const server = (body as NordVPNServer[]).find(
(s) => s.features.proxy_ssl
);
if (!server) {
throw new Error(
'Could not find `https` proxy server from NordVPN'
);
}
return server;
},
{
retries: 5,
onRetry(err, attempt) {
console.log(
`Failed to get NordVPN servers. Retrying… (attempt #${attempt}, ${err.message})`
);
},
}
);

async function getRealIP(): Promise<string> {
const res = await req('https://dump.n8.io');
Expand Down
27 changes: 27 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b4b70a5

Please sign in to comment.