Skip to content

Commit

Permalink
PAC file support via PERCY_PAC_FILE_URL env var (#1784)
Browse files Browse the repository at this point in the history
* PAC file support via PERCY_PAC_FILE_PATH env var

* Added pac-proxy-agent lib as dependency

* typo fixes

* PR comments addressed

* Moved pac-proxy-agent lib dependency to percy client instead of main package

* Resolved conflicts
  • Loading branch information
khushhalm authored Nov 19, 2024
1 parent 160670d commit 5067675
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"@babel/eslint-parser": "^7.14.7",
"@babel/preset-env": "^7.14.7",
"@babel/register": "^7.17.7",
"babel-plugin-transform-import-meta": "^2.2.1",
"@rollup/plugin-alias": "^5.1.1",
"@rollup/plugin-babel": "^6.0.0",
"@rollup/plugin-commonjs": "^21.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"babel-plugin-istanbul": "^6.0.0",
"babel-plugin-module-resolver": "^4.1.0",
"babel-plugin-transform-import-meta": "^2.2.1",
"cross-env": "^7.0.2",
"eslint": "^7.30.0",
"eslint-config-standard": "^16.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"dependencies": {
"@percy/env": "1.30.2",
"@percy/logger": "1.30.2",
"pac-proxy-agent": "^7.0.2",
"pako": "^2.1.0"
}
}
48 changes: 43 additions & 5 deletions packages/client/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ import http from 'http';
import https from 'https';
import logger from '@percy/logger';
import { stripQuotesAndSpaces } from '@percy/env/utils';
import { PacProxyAgent } from 'pac-proxy-agent';

const CRLF = '\r\n';
const STATUS_REG = /^HTTP\/1.[01] (\d*)/;

// function to create PAC proxy agent
function createPacAgent(pacUrl, options = {}) {
pacUrl = stripQuotesAndSpaces(pacUrl);
try {
const agent = new PacProxyAgent(pacUrl, {
keepAlive: true,
...options
});

logger('client:proxy').info(`Successfully loaded PAC file from: ${pacUrl}`);
return agent;
} catch (error) {
logger('client:proxy').error(`Failed to load PAC file, error message: ${error.message}, stack: ${error.stack}`);
throw new Error(`Failed to initialize PAC proxy: ${error.message}`);
}
}

// Returns true if the URL hostname matches any patterns
export function hostnameMatches(patterns, url) {
let subject = new URL(url);
Expand Down Expand Up @@ -219,11 +237,31 @@ export function proxyAgentFor(url, options) {
let { protocol, hostname } = new URL(url);
let cachekey = `${protocol}//${hostname}`;

if (!cache.has(cachekey)) {
cache.set(cachekey, protocol === 'https:'
? new ProxyHttpsAgent(options)
: new ProxyHttpAgent(options));
// If we already have a cached agent, return it
if (cache.has(cachekey)) {
return cache.get(cachekey);
}

return cache.get(cachekey);
try {
let agent;
const pacUrl = process.env.PERCY_PAC_FILE_URL;

// If PAC URL is provided, use PAC proxy
if (pacUrl) {
logger('client:proxy').info(`Using PAC file from: ${pacUrl}`);
agent = createPacAgent(pacUrl, options);
} else {
// Fall back to other proxy configuration
agent = protocol === 'https:'
? new ProxyHttpsAgent(options)
: new ProxyHttpAgent(options);
}

// Cache the created agent
cache.set(cachekey, agent);
return agent;
} catch (error) {
logger('client:proxy').error(`Failed to create proxy agent: ${error.message}`);
throw error;
}
}
7 changes: 3 additions & 4 deletions packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { getProxy } from '@percy/client/utils';
import Browser from './browser.js';
import Pako from 'pako';
import {
base64encode
,
base64encode,
generatePromise,
yieldAll,
yieldTo
, redactSecrets,
yieldTo,
redactSecrets,
detectSystemProxyAndLog
} from './utils.js';

Expand Down
Loading

0 comments on commit 5067675

Please sign in to comment.