53 lines
7.6 KiB
JavaScript
53 lines
7.6 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
const lodash_1 = require("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 += (0, lodash_1.escapeRegExp)(rule.url).replace(wildcardRegExp, '\\..*');
|
||
|
if (domainRequiredAfterRule)
|
||
|
regExStr += '.+';
|
||
|
regExStr += '$';
|
||
|
return new RegExp(regExStr).test(sourceUrl.url) && matchByProtocols && matchByPorts;
|
||
|
}
|
||
|
function default_1(url, rules) {
|
||
|
if (!Array.isArray(rules))
|
||
|
rules = [rules];
|
||
|
return rules.some(rule => urlMatchRule(parseUrl(url), prepareRule(rule)));
|
||
|
}
|
||
|
exports.default = default_1;
|
||
|
module.exports = exports.default;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2hlY2stdXJsLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3V0aWxzL2NoZWNrLXVybC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOztBQUFBLG1DQUFrRDtBQUVsRCxNQUFNLHdCQUF3QixHQUFHLE9BQU8sQ0FBQztBQUN6QyxNQUFNLHNCQUFzQixHQUFLLE9BQU8sQ0FBQztBQUN6QyxNQUFNLHFCQUFxQixHQUFNLE9BQU8sQ0FBQztBQUN6QyxNQUFNLFVBQVUsR0FBaUIsU0FBUyxDQUFDO0FBQzNDLE1BQU0sY0FBYyxHQUFhLGFBQWEsQ0FBQztBQUMvQyxNQUFNLGNBQWMsR0FBYSxXQUFXLENBQUM7QUFFN0MsU0FBUyxRQUFRLENBQUUsR0FBRztJQUNsQixJQUFJLENBQUMsR0FBRyxJQUFJLE9BQU8sR0FBRyxLQUFLLFFBQVE7UUFDL0IsT0FBTyxJQUFJLENBQUM7SUFFaEIsSUFBSSxRQUFRLEdBQUcsR0FBRyxDQUFDLEtBQUssQ0FBQyxjQUFjLENBQUMsQ0FBQztJQUV6QyxRQUFRLEdBQUcsUUFBUSxDQUFDLENBQUMsQ0FBQyxRQUFRLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLElBQUksQ0FBQztJQUN6QyxHQUFHLEdBQVEsR0FBRyxDQUFDLE9BQU8sQ0FBQyxjQUFjLEVBQUUsRUFBRSxDQUFDLENBQUM7SUFDM0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