2017-10-14 20:24:18 +00:00
|
|
|
/*jslint es6: true*/
|
|
|
|
/*global window*/
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
const defaultKey = "(default)";
|
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
// From https://www.npmjs.com/package/escape-string-regexp
|
|
|
|
const matchOperatorsRe = /[|\\{}()[\]^$+*?.]/g;
|
|
|
|
|
|
|
|
const escapeStringRegexp = function (aString) {
|
|
|
|
if (typeof aString !== 'string') {
|
|
|
|
throw new TypeError('Expected a string');
|
|
|
|
}
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
return aString.replace(matchOperatorsRe, '\\$&');
|
|
|
|
};
|
|
|
|
|
|
|
|
// From https://www.npmjs.com/package/matcher
|
|
|
|
const reCache = new Map();
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
const makeRe = function (pattern, shouldNegate) {
|
|
|
|
|
|
|
|
const cacheKey = pattern + shouldNegate;
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
if (reCache.has(cacheKey)) {
|
|
|
|
return reCache.get(cacheKey);
|
|
|
|
}
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
const negated = pattern[0] === '!';
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
if (negated) {
|
|
|
|
pattern = pattern.slice(1);
|
|
|
|
}
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '.*');
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
if (negated && shouldNegate) {
|
|
|
|
pattern = `(?!${pattern})`;
|
|
|
|
}
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
const re = new RegExp(`^${pattern}$`, 'i');
|
|
|
|
re.negated = negated;
|
|
|
|
reCache.set(cacheKey, re);
|
2017-10-22 05:40:39 +00:00
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
return re;
|
|
|
|
};
|
|
|
|
|
2017-10-14 20:24:18 +00:00
|
|
|
const extractHostNameFromUrl = function (url) {
|
|
|
|
const uri = window.URI(url);
|
|
|
|
return uri.hostname();
|
|
|
|
};
|
|
|
|
|
|
|
|
const matchingUrlReduceFunction = function (hostName, prev, next) {
|
2017-10-14 22:26:02 +00:00
|
|
|
|
2017-10-14 20:24:18 +00:00
|
|
|
if (prev) {
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
2017-10-14 22:26:02 +00:00
|
|
|
if (makeRe(next, true).test(hostName)) {
|
2017-10-14 20:24:18 +00:00
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prev;
|
|
|
|
};
|
|
|
|
|
|
|
|
const matchHostName = function (domainRegExes, hostName) {
|
|
|
|
// of the URL being requested.
|
|
|
|
const matchingUrlReduceFunctionBound = matchingUrlReduceFunction.bind(undefined, hostName);
|
|
|
|
const matchingPattern = domainRegExes
|
|
|
|
.filter((aRule) => aRule !== defaultKey)
|
|
|
|
.sort()
|
|
|
|
.reduce(matchingUrlReduceFunctionBound, undefined);
|
|
|
|
|
|
|
|
return matchingPattern || undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
const matchUrl = function (domainRegExes, url) {
|
|
|
|
const hostName = extractHostNameFromUrl(url);
|
|
|
|
return matchHostName(domainRegExes, hostName);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.WEB_API_MANAGER.domainMatcherLib = {
|
|
|
|
matchHostName,
|
|
|
|
matchUrl
|
|
|
|
};
|
|
|
|
}());
|