@extra/proxy-router
Version:
A plugin for playwright & puppeteer to route proxies dynamically.
86 lines • 2.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtraPluginProxyRouter = void 0;
const puppeteer_extra_plugin_1 = require("puppeteer-extra-plugin");
const router_1 = require("./router");
class ExtraPluginProxyRouter extends puppeteer_extra_plugin_1.PuppeteerExtraPlugin {
constructor(opts) {
super(opts);
/** The name of the automation framework used */
this.framework = null;
// Disable the puppeteer compat shim when used with playwright-extra
this.noPuppeteerShim = true;
this.debug('Initialized', this.opts);
this.router = new router_1.ProxyRouter(this.opts);
}
get name() {
return 'proxy-router';
}
get defaults() {
return {
collectStats: true,
proxyServerOpts: {
port: 2800,
},
};
}
// Make accessing router methods shorter
/** Get or set proxies at runtime */
get proxies() {
return this.router.proxies;
}
set proxies(proxies) {
this.router.proxies = proxies;
}
/** Retrieve traffic statistics */
get stats() {
return this.router.stats;
}
/** Get or set the `routeByHost` function at runtime */
get routeByHost() {
return this.router.routeByHost;
}
set routeByHost(fn) {
this.router.routeByHost = fn;
}
get proxyBypassListString() {
return (this.opts.proxyBypassList || []).join(',') || undefined;
}
async onPluginRegistered(args) {
this.framework =
(args === null || args === void 0 ? void 0 : args.framework) === 'playwright' ? 'playwright' : 'puppeteer';
this.debug('plugin registered', this.framework);
}
async beforeLaunch(options = {}) {
this.debug('beforeLaunch - before', options);
await this.router.listen();
const proxyUrl = this.router.proxyServerUrl;
if (!proxyUrl) {
throw new Error('No local proxy server available');
}
if (this.framework === 'playwright') {
const pwOptions = options;
pwOptions.proxy = {
server: proxyUrl,
bypass: this.proxyBypassListString,
};
}
else if (this.framework === 'puppeteer') {
const pptrOptions = options;
pptrOptions.args = pptrOptions.args || [];
pptrOptions.args.push(`--proxy-server=${proxyUrl}`);
if (this.proxyBypassListString) {
pptrOptions.args.push(`--proxy-bypass-list=${this.proxyBypassListString}`);
}
}
else {
this.debug('Unsupported framework, not setting proxy');
}
this.debug('beforeLaunch - after', options);
}
async onDisconnected() {
await this.router.close().catch(this.debug);
}
}
exports.ExtraPluginProxyRouter = ExtraPluginProxyRouter;
//# sourceMappingURL=plugin.js.map