Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'proxy-chain' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
// Parse and validate proxy URL
const parsedProxyUrl = parseUrl(opts.proxyUrl);
if (!parsedProxyUrl.host || !parsedProxyUrl.port) {
throw new Error('Invalid "proxyUrl" option: both hostname and port must be provided.');
}
if (!/^(http|https|socks4|socks5)$/.test(parsedProxyUrl.scheme)) {
throw new Error(`Invalid "proxyUrl" option: Unsupported scheme (${parsedProxyUrl.scheme}).`);
}
// Anonymize proxy URL if it has username or password
let anonymizedProxyUrl = null;
if (parsedProxyUrl.username || parsedProxyUrl.password) {
if (parsedProxyUrl.scheme !== 'http') {
throw new Error('Invalid "proxyUrl" option: authentication is only supported for HTTP proxy type.');
}
anonymizedProxyUrl = await anonymizeProxy(opts.proxyUrl);
}
opts.args.push(`--proxy-server=${anonymizedProxyUrl || opts.proxyUrl}`);
const optsForLog = _.omit(opts, LAUNCH_PUPPETEER_LOG_OMIT_OPTS);
optsForLog.proxyUrl = redactUrl(opts.proxyUrl);
optsForLog.args = opts.args.slice(0, opts.args.length - 1);
log.info('Launching Puppeteer', optsForLog);
const browser = await puppeteer.launch(opts);
// Close anonymization proxy server when Puppeteer finishes
if (anonymizedProxyUrl) {
const cleanUp = () => {
// Don't wait for finish, only log errors
closeAnonymizedProxy(anonymizedProxyUrl, true)
.catch(err => log.exception(err, 'closeAnonymizedProxy() failed.'));
_initialize() {
let promise = null;
// Applies options.proxyUrl setting to the WebDriver's Capabilities and Chrome Options.
// For proxy servers with authentication, this class starts a local proxy server
// NOTE: to view effective proxy settings in Chrome, open chrome://net-internals/#proxy
if (this.options.proxyUrl) {
// NOTE: call anonymizeProxy() outside of promise, so that errors in proxyUrl are thrown!
promise = anonymizeProxy(this.options.proxyUrl)
.then((result) => {
this.anonymizedProxyUrl = result;
if (/^chrome$/i.test(this.options.browserName)) {
// In Chrome, Capabilities.setProxy() has no effect,
// so we setup the proxy manually
this.chromeOptions.addArguments(`--proxy-server=${this.anonymizedProxyUrl}`);
} else {
const proxyConfig = {
proxyType: 'MANUAL',
httpProxy: this.anonymizedProxyUrl,
sslProxy: this.anonymizedProxyUrl,
ftpProxy: this.anonymizedProxyUrl,
};
this.capabilities.setProxy(proxyConfig);
}
const launchPuppeteerWithProxy = async (puppeteer, opts) => {
// Parse and validate proxy URL
const parsedProxyUrl = parseUrl(opts.proxyUrl);
if (!parsedProxyUrl.host || !parsedProxyUrl.port) {
throw new Error('Invalid "proxyUrl" option: both hostname and port must be provided.');
}
if (!/^(http|https|socks4|socks5)$/.test(parsedProxyUrl.scheme)) {
throw new Error(`Invalid "proxyUrl" option: Unsupported scheme (${parsedProxyUrl.scheme}).`);
}
// Anonymize proxy URL if it has username or password
let anonymizedProxyUrl = null;
if (parsedProxyUrl.username || parsedProxyUrl.password) {
if (parsedProxyUrl.scheme !== 'http') {
throw new Error('Invalid "proxyUrl" option: authentication is only supported for HTTP proxy type.');
}
anonymizedProxyUrl = await anonymizeProxy(opts.proxyUrl);
}
if (!/^(http|https|socks4|socks5)$/.test(parsedProxyUrl.scheme)) {
throw new Error(`Invalid "proxyUrl" option: Unsupported scheme (${parsedProxyUrl.scheme}).`);
}
// Anonymize proxy URL if it has username or password
let anonymizedProxyUrl = null;
if (parsedProxyUrl.username || parsedProxyUrl.password) {
if (parsedProxyUrl.scheme !== 'http') {
throw new Error('Invalid "proxyUrl" option: authentication is only supported for HTTP proxy type.');
}
anonymizedProxyUrl = await anonymizeProxy(opts.proxyUrl);
}
opts.args.push(`--proxy-server=${anonymizedProxyUrl || opts.proxyUrl}`);
const optsForLog = _.omit(opts, LAUNCH_PUPPETEER_LOG_OMIT_OPTS);
optsForLog.proxyUrl = redactUrl(opts.proxyUrl);
optsForLog.args = opts.args.slice(0, opts.args.length - 1);
log.info('Launching Puppeteer', optsForLog);
const browser = await puppeteer.launch(opts);
// Close anonymization proxy server when Puppeteer finishes
if (anonymizedProxyUrl) {
const cleanUp = () => {
// Don't wait for finish, only log errors
closeAnonymizedProxy(anonymizedProxyUrl, true)
.catch(err => log.exception(err, 'closeAnonymizedProxy() failed.'));
};
browser.on('disconnected', cleanUp);
const prevClose = browser.close.bind(browser);
const cleanUp = () => {
// Don't wait for finish, only log errors
closeAnonymizedProxy(anonymizedProxyUrl, true)
.catch(err => log.exception(err, 'closeAnonymizedProxy() failed.'));
};
.then(() => {
if (this.anonymizedProxyUrl) {
return closeAnonymizedProxy(this.anonymizedProxyUrl, true);
}
})
.then(() => {