Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should allow to use Apify proxy', async () => {
process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
process.env[ENV_VARS.PROXY_HOSTNAME] = 'my.host.com';
process.env[ENV_VARS.PROXY_PORT] = 123;
const mock = sinon.mock(actor);
mock.expects('getApifyProxyUrl')
.once()
.withArgs({
session: 'xxx',
groups: ['yyy'],
groupsParamName: 'options.apifyProxyGroups',
sessionParamName: 'options.apifyProxySession',
})
.returns(null); // Return null so that it doesn't start proxy-chain
try {
await Apify
.launchPuppeteer({
it('should work', async () => {
process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
process.env[ENV_VARS.PROXY_HOSTNAME] = 'my.host.com';
process.env[ENV_VARS.PROXY_PORT] = 123;
const pool = new Apify.PuppeteerPool({
maxOpenPagesPerInstance: 3,
retireInstanceAfterRequestCount: 5,
});
const browsers = [];
// Open 6 pages 3 in both browsers.
browsers.push(pool.newPage());
browsers.push(pool.newPage());
browsers.push(pool.newPage());
browsers.push(pool.newPage());
browsers.push(pool.newPage());
browsers.push(pool.newPage());
await Promise.all(browsers);
.returns(null); // Return null so that it doesn't start proxy-chain
try {
await Apify
.launchPuppeteer({
useApifyProxy: true,
apifyProxySession: 'xxx',
apifyProxyGroups: ['yyy'],
headless: true,
})
.then(browser => browser.close());
} finally {
mock.verify();
mock.restore();
delete process.env[ENV_VARS.PROXY_PASSWORD];
delete process.env[ENV_VARS.PROXY_HOSTNAME];
delete process.env[ENV_VARS.PROXY_PORT];
}
});
expect(pool.activeInstances[2].totalPages).to.be.eql(1);
expect(pool.retiredInstances[0].activePages).to.be.eql(3);
expect(pool.retiredInstances[0].totalPages).to.be.eql(5);
// Kill the remaining 3 pages from the 1st browser to see that it gets closed.
await (await browsers[2]).close();
await (await browsers[6]).close();
await (await browsers[7]).close();
await shortSleep(2000);
expect(_.values(pool.retiredInstances).length).to.be.eql(0);
// Cleanup everything.
await pool.destroy();
delete process.env[ENV_VARS.PROXY_PASSWORD];
delete process.env[ENV_VARS.PROXY_HOSTNAME];
delete process.env[ENV_VARS.PROXY_PORT];
});
// TODO: remove this when we release v1.0.0
if (!options.groups && options.apifyProxyGroups) {
log.warning('Parameter `apifyProxyGroups` of Apify.getApifyProxyUrl() is deprecated!!! Use `groups` instead!');
options.groups = options.apifyProxyGroups;
}
if (!options.session && options.apifyProxySession) {
log.warning('Parameter `apifyProxySession` of Apify.getApifyProxyUrl() is deprecated!!! Use `session` instead!');
options.session = options.apifyProxySession;
}
const {
groups,
session,
country,
password = process.env[ENV_VARS.PROXY_PASSWORD],
hostname = process.env[ENV_VARS.PROXY_HOSTNAME] || LOCAL_ENV_VARS[ENV_VARS.PROXY_HOSTNAME],
port = parseInt(process.env[ENV_VARS.PROXY_PORT] || LOCAL_ENV_VARS[ENV_VARS.PROXY_PORT], 10),
// This is used only internaly. Some other function calling this function use different naming for groups and session
// parameters so we need to override this in error messages.
groupsParamName = 'opts.groups',
sessionParamName = 'opts.session',
countryParamName = 'opts.country',
} = options;
const getMissingParamErrorMgs = (param, env) => `Apify Proxy ${param} must be provided as parameter or "${env}" environment variable!`;
const throwInvalidProxyValueError = (param) => {
throw new Error(`The "${param}" option can only contain the following characters: 0-9, a-z, A-Z, ".", "_" and "~"`);
};
const throwInvalidCountryCode = (code) => {
throw new Error(`The "${code}" option must be a valid two letter country code according to ISO 3166-1 alpha-2`);
};
it('should work', () => {
process.env[ENV_VARS.PROXY_PASSWORD] = 'abc123';
process.env[ENV_VARS.PROXY_HOSTNAME] = 'my.host.com';
process.env[ENV_VARS.PROXY_PORT] = 123;
expect(Apify.getApifyProxyUrl({
session: 'XYZ',
groups: ['g1', 'g2', 'g3'],
country: 'US',
})).to.be.eql('http://groups-g1+g2+g3,session-XYZ,country-US:abc123@my.host.com:123');
expect(Apify.getApifyProxyUrl({
session: 'XYZ',
groups: ['g1', 'g2', 'g3'],
})).to.be.eql('http://groups-g1+g2+g3,session-XYZ:abc123@my.host.com:123');
expect(Apify.getApifyProxyUrl({
groups: ['g1', 'g2', 'g3'],
})).to.be.eql('http://groups-g1+g2+g3:abc123@my.host.com:123');
},
},
};
exports.ACTS_TEMPLATE_LIST = Object.keys(exports.ACTS_TEMPLATES);
exports.DEFAULT_ACT_TEMPLATE = 'basic';
exports.DEFAULT_LOCAL_STORES_ID = 'default';
exports.LOCAL_ENV_VARS = {
[ENV_VARS.LOCAL_EMULATION_DIR]: DEFAULT_LOCAL_EMULATION_DIR,
[ENV_VARS.DEFAULT_KEY_VALUE_STORE_ID]: exports.DEFAULT_LOCAL_STORES_ID,
[ENV_VARS.DEFAULT_DATASET_ID]: exports.DEFAULT_LOCAL_STORES_ID,
[ENV_VARS.DEFAULT_REQUEST_QUEUE_ID]: exports.DEFAULT_LOCAL_STORES_ID,
[ENV_VARS.PROXY_HOSTNAME]: DEFAULT_PROXY_HOSTNAME,
[ENV_VARS.PROXY_PORT]: DEFAULT_PROXY_PORT.toString(),
};
exports.EMPTY_LOCAL_CONFIG = {
name: null,
actId: null,
version: {
versionNumber: '0.1',
buildTag: 'latest',
envVars: [],
sourceType: 'TARBALL',
tarballUrl: null,
},
};
exports.GLOBAL_CONFIGS_FOLDER = path.join(os.homedir(), '.apify');