forked from DevExpress/testcafe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proxy-bypass option to prevent using proxy depending on rule (Dev…
- Loading branch information
1 parent
07e9649
commit 37145f7
Showing
10 changed files
with
340 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { escapeRegExp as escapeRe } from 'lodash'; | ||
|
||
const startsWithWildcardRegExp = /^\*\./; | ||
const endsWithWildcardRegExp = /\.\*$/; | ||
const trailingSlashesRegExp = /\/.*$/; | ||
const portRegExp = /:(\d+)$/; | ||
const protocolRegExp = /^(\w+):\/\//; | ||
const wildcardRegExp = /\\\.\\\*/g; | ||
|
||
function parseUrl (url) { | ||
if (!url || typeof url !== 'string') | ||
return null; | ||
|
||
let protocol = url.match(protocolRegExp); | ||
|
||
protocol = protocol ? protocol[1] : null; | ||
url = url.replace(protocolRegExp, ''); | ||
url = url.replace(trailingSlashesRegExp, ''); | ||
|
||
let port = url.match(portRegExp); | ||
|
||
port = port ? parseInt(port[1], 10) : null; | ||
url = url.replace(portRegExp, ''); | ||
|
||
return { protocol, url, port }; | ||
} | ||
|
||
function prepareRule (url) { | ||
const rule = parseUrl(url); | ||
|
||
if (rule) { | ||
rule.url = rule.url.replace(startsWithWildcardRegExp, '.'); | ||
rule.url = rule.url.replace(endsWithWildcardRegExp, '.'); | ||
} | ||
|
||
return rule; | ||
} | ||
|
||
function urlMatchRule (sourceUrl, rule) { | ||
if (!sourceUrl || !rule) | ||
return false; | ||
|
||
const matchByProtocols = !rule.protocol || !sourceUrl.protocol || rule.protocol === sourceUrl.protocol; | ||
const matchByPorts = !rule.port || sourceUrl.port === rule.port; | ||
const domainRequiredBeforeRule = rule.url.startsWith('.'); | ||
const domainRequiredAfterRule = rule.url.endsWith('.'); | ||
|
||
let regExStr = '^'; | ||
|
||
if (domainRequiredBeforeRule) | ||
regExStr += '.+'; | ||
|
||
regExStr += escapeRe(rule.url).replace(wildcardRegExp, '\\..*'); | ||
|
||
if (domainRequiredAfterRule) | ||
regExStr += '.+'; | ||
|
||
regExStr += '$'; | ||
|
||
return new RegExp(regExStr).test(sourceUrl.url) && matchByProtocols && matchByPorts; | ||
} | ||
|
||
export default function (url, rules) { | ||
if (!Array.isArray(rules)) | ||
rules = [rules]; | ||
|
||
return rules.some(rule => urlMatchRule(parseUrl(url), prepareRule(rule))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.