Skip to content

Commit

Permalink
Merge pull request #63 from tictactrip/fix/http-agent
Browse files Browse the repository at this point in the history
feat: agents updated
  • Loading branch information
rimiti authored Mar 26, 2024
2 parents 08f564b + 27da30f commit 16a5936
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 179 deletions.
44 changes: 24 additions & 20 deletions __tests__/unit/bright-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ describe('BrightData', () => {
expect(agent).toBeInstanceOf(BrightData);
expect(typeof agent.sessionId).toEqual('number');
expect(agent.country).toMatch(new RegExp(regexPatternAllCountries));
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe(22225);
expect(agent.axios.defaults.httpsAgent.proxy.rejectUnauthorized).toBe(false);
expect(agent.axios.defaults.httpsAgent.proxy.auth).toMatch(
new RegExp(`${proxy.username}-session-([0-9])*-country-(${regexPatternAllCountries}):${proxy.password}`),
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io:22225');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe('22225');
expect(agent.axios.defaults.httpsAgent.proxy.href).toMatch(
new RegExp(
`https://${proxy.username}-session-([0-9])*-country-(${regexPatternAllCountries}):${proxy.password}@zproxy.lum-superproxy.io:22225/`,
),
);
});

Expand All @@ -107,11 +108,12 @@ describe('BrightData', () => {
expect(agent).toBeInstanceOf(BrightData);
expect(typeof agent.sessionId).toEqual('number');
expect(agent.country).toMatch(/fr/);
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe(22225);
expect(agent.axios.defaults.httpsAgent.proxy.rejectUnauthorized).toBe(false);
expect(agent.axios.defaults.httpsAgent.proxy.auth).toMatch(
new RegExp(`${proxy.username}-session-([0-9])*-country-fr:${proxy.password}`),
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io:22225');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe('22225');
expect(agent.axios.defaults.httpsAgent.proxy.href).toMatch(
new RegExp(
`https://${proxy.username}-session-([0-9])*-country-fr:${proxy.password}@zproxy.lum-superproxy.io:22225/`,
),
);
});

Expand All @@ -123,11 +125,12 @@ describe('BrightData', () => {
expect(agent).toBeInstanceOf(BrightData);
expect(agent.sessionId).toEqual(123456789);
expect(agent.country).toMatch(/fr/);
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe(22225);
expect(agent.axios.defaults.httpsAgent.proxy.rejectUnauthorized).toBe(false);
expect(agent.axios.defaults.httpsAgent.proxy.auth).toMatch(
new RegExp(`${proxy.username}-session-${sessionId}-country-fr:${proxy.password}`),
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io:22225');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe('22225');
expect(agent.axios.defaults.httpsAgent.proxy.href).toMatch(
new RegExp(
`https://${proxy.username}-session-${sessionId}-country-fr:${proxy.password}@zproxy.lum-superproxy.io:22225/`,
),
);
});

Expand All @@ -139,11 +142,12 @@ describe('BrightData', () => {
expect(agent).toBeInstanceOf(BrightData);
expect(agent.sessionId).toEqual(123456789);
expect(agent.country).toMatch(new RegExp(regexPatternAllCountries));
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe(22225);
expect(agent.axios.defaults.httpsAgent.proxy.rejectUnauthorized).toBe(false);
expect(agent.axios.defaults.httpsAgent.proxy.auth).toMatch(
new RegExp(`${proxy.username}-session-${sessionId}-country-(${regexPatternAllCountries}):${proxy.password}`),
expect(agent.axios.defaults.httpsAgent.proxy.host).toBe('zproxy.lum-superproxy.io:22225');
expect(agent.axios.defaults.httpsAgent.proxy.port).toBe('22225');
expect(agent.axios.defaults.httpsAgent.proxy.href).toMatch(
new RegExp(
`https://${proxy.username}-session-${sessionId}-country-(${regexPatternAllCountries}):${proxy.password}@zproxy.lum-superproxy.io:22225/`,
),
);
});

Expand Down
32 changes: 10 additions & 22 deletions __tests__/unit/shifter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,53 +62,41 @@ describe('Shifter', () => {
it('creates a properly formed proxy agent', () => {
const agent: Shifter = shifter.setIp();

const expectedProxyAgent = {
host: proxy.host,
port: expect.any(Number),
rejectUnauthorized: false,
};

expect(agent.axios.defaults.httpsAgent.proxy).toMatchObject(expectedProxyAgent);
expect(agent.axios.defaults.httpAgent.proxy).toMatchObject(expectedProxyAgent);
expect(agent.axios.defaults.httpsAgent.proxy.toString()).toMatch(new RegExp(`https://${proxy.host}:[0-9]+`));
expect(agent.axios.defaults.httpAgent.proxy.toString()).toMatch(new RegExp(`http://${proxy.host}:[0-9]+`));
});

it('creates a proxy agent having a random port among the given mapping', () => {
const agent: Shifter = shifter.setIp();

expect(getMappingPorts(mapping)).toEqual(expect.arrayContaining([agent.axios.defaults.httpsAgent.proxy.port]));
expect(getMappingPorts(mapping)).toEqual(expect.arrayContaining([agent.axios.defaults.httpAgent.proxy.port]));
expect(getMappingPorts(mapping).includes(Number(agent.axios.defaults.httpsAgent.proxy.port))).toBeTruthy();
expect(getMappingPorts(mapping).includes(Number(agent.axios.defaults.httpAgent.proxy.port))).toBeTruthy();
});
});

describe('when given one or more specific countries to target', () => {
it('creates a properly formed proxy agent', () => {
const agent: Shifter = shifter.setIp();

const expectedProxyAgent = {
host: proxy.host,
port: expect.any(Number),
rejectUnauthorized: false,
};

expect(agent.axios.defaults.httpsAgent.proxy).toMatchObject(expectedProxyAgent);
expect(agent.axios.defaults.httpAgent.proxy).toMatchObject(expectedProxyAgent);
expect(agent.axios.defaults.httpsAgent.proxy.toString()).toMatch(new RegExp(`https://${proxy.host}:[0-9]+`));
expect(agent.axios.defaults.httpAgent.proxy.toString()).toMatch(new RegExp(`http://${proxy.host}:[0-9]+`));
});

it('create a proxy agent having a port matching one of the options in the mapping', () => {
const country: EShifterCountry = EShifterCountry.FRANCE;
const agent: Shifter = shifter.setIp({ countries: [country] });

expect(mapping[country]).toEqual(expect.arrayContaining([agent.axios.defaults.httpsAgent.proxy.port]));
expect(mapping[country]).toEqual(expect.arrayContaining([agent.axios.defaults.httpAgent.proxy.port]));
expect(mapping[country].includes(Number(agent.axios.defaults.httpsAgent.proxy.port))).toBeTruthy();
expect(mapping[country].includes(Number(agent.axios.defaults.httpAgent.proxy.port))).toBeTruthy();
});

it('create a proxy agent having a port matching several of the options in the mapping', () => {
const countries: EShifterCountry[] = [EShifterCountry.FRANCE, EShifterCountry.BELGIUM];
const mappedPorts: number[] = countries.map((c) => mapping[c]).reduce((acc, c) => [...acc, ...c], []);
const agent: Shifter = shifter.setIp({ countries: countries });

expect(mappedPorts).toEqual(expect.arrayContaining([agent.axios.defaults.httpsAgent.proxy.port]));
expect(mappedPorts).toEqual(expect.arrayContaining([agent.axios.defaults.httpAgent.proxy.port]));
expect(mappedPorts.includes(Number(agent.axios.defaults.httpsAgent.proxy.port))).toBeTruthy();
expect(mappedPorts.includes(Number(agent.axios.defaults.httpAgent.proxy.port))).toBeTruthy();
});
});
});
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
{
"name": "@tictactrip/luminator",
"version": "4.0.0",
"version": "5.0.2-0",
"description": "Axios proxy provider agent.",
"author": "Tictactrip <[email protected]>",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"license": "GPL-3.0",
"dependencies": {
"@rimiti/stimmy": "1.11.0",
"axios": "1.6.7",
"http-proxy-agent": "4.0.1",
"https-proxy-agent": "2.2.4"
"axios": "1.6.8",
"http-proxy-agent": "7.0.2",
"https-proxy-agent": "7.0.4"
},
"devDependencies": {
"@types/jest": "29.5.12",
"@typescript-eslint/eslint-plugin": "7.2.0",
"@typescript-eslint/parser": "7.2.0",
"@typescript-eslint/eslint-plugin": "7.3.1",
"@typescript-eslint/parser": "7.3.1",
"eslint": "8.57.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.1.3",
"jest": "29.7.0",
"nock": "13.5.4",
"prettier": "3.2.5",
"ts-jest": "29.1.2",
"typescript": "5.4.2"
"typescript": "5.4.3"
},
"scripts": {
"clean": "rm -rf dist coverage",
Expand Down
6 changes: 3 additions & 3 deletions src/classes/providers/base/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as HttpsProxyAgent from 'https-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import { AxiosRequestConfig } from 'axios';

Expand All @@ -12,8 +12,8 @@ interface IBaseConfig {
}

interface ICreateProxyConfig {
httpsAgent: HttpsProxyAgent;
httpAgent: HttpProxyAgent;
httpsAgent: HttpsProxyAgent<string>;
httpAgent: HttpProxyAgent<string>;
}

interface IProviderConfig {
Expand Down
8 changes: 5 additions & 3 deletions src/classes/providers/bright-data/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosInstance, AxiosPromise, AxiosRequestConfig } from 'axios';
import * as HttpsProxyAgent from 'https-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import { ICreateProxyConfig, EStrategyMode } from '../base/types';
import { replacer } from '../../../utils/replacer';
Expand Down Expand Up @@ -170,8 +170,10 @@ export class BrightData extends Base {
this.country = country;

return {
httpsAgent: new HttpsProxyAgent({ ...proxy, rejectUnauthorized: false }),
httpAgent: new HttpProxyAgent({ ...proxy, rejectUnauthorized: false }),
httpsAgent: new HttpsProxyAgent(`https://${proxy.auth}@${proxy.host}:${proxy.port}`, {
rejectUnauthorized: false,
}),
httpAgent: new HttpProxyAgent(`http://${proxy.auth}@${proxy.host}:${proxy.port}`),
};
}
}
8 changes: 5 additions & 3 deletions src/classes/providers/proxyrack/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosPromise, AxiosRequestConfig } from 'axios';
import * as HttpsProxyAgent from 'https-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import { EStrategyMode, ICreateProxyConfig } from '../base/types';
import { replacer } from '../../../utils/replacer';
Expand Down Expand Up @@ -81,8 +81,10 @@ export class Proxyrack extends Base {
};

return {
httpsAgent: new HttpsProxyAgent({ ...proxy, rejectUnauthorized: false }),
httpAgent: new HttpProxyAgent({ ...proxy, rejectUnauthorized: false }),
httpsAgent: new HttpsProxyAgent(`https://${proxy.auth}@${proxy.host}:${proxy.port}`, {
rejectUnauthorized: false,
}),
httpAgent: new HttpProxyAgent(`http://${proxy.auth}@${proxy.host}:${proxy.port}`),
};
}
}
8 changes: 5 additions & 3 deletions src/classes/providers/shifter/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AxiosInstance, AxiosPromise, AxiosRequestConfig } from 'axios';
import * as HttpsProxyAgent from 'https-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import { ICreateProxyConfig, EStrategyMode, IProviderConfig } from '../base/types';
import { Base } from '../base';
Expand Down Expand Up @@ -127,8 +127,10 @@ export class Shifter extends Base {
};

return {
httpsAgent: new HttpsProxyAgent({ ...proxy, rejectUnauthorized: false }),
httpAgent: new HttpProxyAgent({ ...proxy, rejectUnauthorized: false }),
httpsAgent: new HttpsProxyAgent(`https://${proxy.host}:${proxy.port}`, {
rejectUnauthorized: false,
}),
httpAgent: new HttpProxyAgent(`http://${proxy.host}:${proxy.port}`),
};
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"sourceMap": true,
"resolveJsonModule": true,
"declaration": true,
"noUnusedLocals": true
"noUnusedLocals": true,
"skipLibCheck": true
},
"include": ["src/**/*.ts"]
}
Loading

0 comments on commit 16a5936

Please sign in to comment.