Skip to content

Commit

Permalink
Add a decoder and new query param for playwright proxies. (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelgriffith committed Apr 26, 2021
1 parent dc2c781 commit 0903795
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/chrome-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export const convertUrlParamsToLaunchOpts = (
trackingId,
keepalive: keepaliveQuery,
dumpio: dumpioQuery,
playwrightProxy: playwrightProxyQuery,
} = urlParts.query;

const playwright = req.parsed.pathname === PLAYWRIGHT_ROUTE;
Expand All @@ -413,6 +414,20 @@ export const convertUrlParamsToLaunchOpts = (
? dumpioQuery !== 'false'
: DEFAULT_DUMPIO;

const playwrightProxy = (() => {
let res = undefined;

if (!_.isUndefined(playwrightProxyQuery)) {
try {
res = JSON.parse(decodeURIComponent(playwrightProxyQuery as string));
} catch(err) {
debug(`Error parsing playwright-proxy param to JSON: ${playwrightProxyQuery} isn't properly URL-encoded or JSON.stringified.`)
}
}

return res;
})();

const parsedKeepalive = _.parseInt(keepaliveQuery as string);
const keepalive = _.isNaN(parsedKeepalive) ? undefined : parsedKeepalive;
const parsedIgnoreDefaultArgs = parseIgnoreDefaultArgs(urlParts.query);
Expand All @@ -429,6 +444,7 @@ export const convertUrlParamsToLaunchOpts = (
keepalive,
pauseOnConnect: !_.isUndefined(pause),
playwright,
playwrightProxy,
slowMo: parseInt(slowMo as string, 10) || undefined,
trackingId: _.isArray(trackingId) ? trackingId[0] : trackingId,
userDataDir: (userDataDir as string) || DEFAULT_USER_DATA_DIR,
Expand Down Expand Up @@ -509,6 +525,7 @@ export const launchChrome = async (
? await chromium.launchServer({
...launchArgs,
headless: true,
proxy: launchArgs.playwrightProxy,
})
: launchArgs.stealth
? await pptrExtra.launch(launchArgs)
Expand Down
3 changes: 2 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChildProcess } from 'child_process';
import { EventEmitter } from 'events';
import { Response } from 'express';
import { BrowserServer } from 'playwright-core';
import { BrowserServer, LaunchOptions } from 'playwright-core';
import { IncomingMessage, ServerResponse } from 'http';
import net from 'net';
import puppeteer from 'puppeteer';
Expand Down Expand Up @@ -55,6 +55,7 @@ export interface ILaunchOptions extends puppeteer.LaunchOptions {
blockAds: boolean;
trackingId?: string;
keepalive?: number;
playwrightProxy?: LaunchOptions['proxy'];
playwright: boolean;
stealth: boolean;
}
Expand Down

0 comments on commit 0903795

Please sign in to comment.