2019-05-18 05:49:53 +00:00
|
|
|
import URL from 'url';
|
2019-05-24 15:40:39 +00:00
|
|
|
import merge from 'deepmerge';
|
2019-07-15 09:04:05 +00:00
|
|
|
import { logger } from '../logger';
|
2018-07-06 05:26:36 +00:00
|
|
|
|
2019-05-24 15:40:39 +00:00
|
|
|
export interface HostRule {
|
|
|
|
hostType?: string;
|
|
|
|
domainName?: string;
|
|
|
|
hostName?: string;
|
2019-07-17 08:14:56 +00:00
|
|
|
json?: true;
|
2019-05-24 15:40:39 +00:00
|
|
|
baseUrl?: string;
|
2019-05-20 13:08:18 +00:00
|
|
|
token?: string;
|
2019-05-24 15:40:39 +00:00
|
|
|
username?: string;
|
|
|
|
password?: string;
|
2019-07-17 08:14:56 +00:00
|
|
|
|
|
|
|
timeout?: number;
|
2019-05-18 05:49:53 +00:00
|
|
|
}
|
2019-05-24 15:40:39 +00:00
|
|
|
|
|
|
|
let hostRules: HostRule[] = [];
|
|
|
|
|
|
|
|
export function add(params: HostRule) {
|
|
|
|
if (params.domainName && params.hostName) {
|
|
|
|
throw new Error('hostRules cannot contain both a domainName and hostName');
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
2019-05-24 15:40:39 +00:00
|
|
|
if (params.domainName && params.baseUrl) {
|
|
|
|
throw new Error('hostRules cannot contain both a domainName and baseUrl');
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
2019-05-24 15:40:39 +00:00
|
|
|
if (params.hostName && params.baseUrl) {
|
|
|
|
throw new Error('hostRules cannot contain both a hostName and baseUrl');
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
2019-05-24 15:40:39 +00:00
|
|
|
hostRules.push(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface HostRuleSearch {
|
|
|
|
hostType?: string;
|
2019-07-17 08:14:56 +00:00
|
|
|
url?: string;
|
2019-05-24 15:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isEmptyRule(rule: HostRule) {
|
|
|
|
return !rule.hostType && !rule.domainName && !rule.hostName && !rule.baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isHostTypeRule(rule: HostRule) {
|
|
|
|
return rule.hostType && !rule.domainName && !rule.hostName && !rule.baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isDomainNameRule(rule: HostRule) {
|
|
|
|
return !rule.hostType && rule.domainName;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isHostNameRule(rule: HostRule) {
|
|
|
|
return !rule.hostType && rule.hostName;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isBaseUrlRule(rule: HostRule) {
|
|
|
|
return !rule.hostType && rule.baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMultiRule(rule: HostRule) {
|
|
|
|
return rule.hostType && (rule.domainName || rule.hostName || rule.baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
function matchesHostType(rule: HostRule, search: HostRuleSearch) {
|
|
|
|
return rule.hostType === search.hostType;
|
|
|
|
}
|
|
|
|
|
|
|
|
function matchesDomainName(rule: HostRule, search: HostRuleSearch) {
|
2019-07-11 14:24:45 +00:00
|
|
|
const hostname = search.url && URL.parse(search.url).hostname;
|
2019-05-24 15:40:39 +00:00
|
|
|
return (
|
|
|
|
search.url &&
|
|
|
|
rule.domainName &&
|
2019-07-11 14:24:45 +00:00
|
|
|
hostname &&
|
|
|
|
hostname.endsWith(rule.domainName)
|
2019-05-24 15:40:39 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function matchesHostName(rule: HostRule, search: HostRuleSearch) {
|
|
|
|
return (
|
|
|
|
search.url &&
|
|
|
|
rule.hostName &&
|
|
|
|
URL.parse(search.url).hostname === rule.hostName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function matchesBaseUrl(rule: HostRule, search: HostRuleSearch) {
|
|
|
|
return search.url && rule.baseUrl && search.url.startsWith(rule.baseUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function find(search: HostRuleSearch) {
|
|
|
|
if (!(search.hostType || search.url)) {
|
|
|
|
logger.warn({ search }, 'Invalid hostRules search');
|
2019-05-25 05:49:26 +00:00
|
|
|
return {};
|
2018-09-13 04:48:08 +00:00
|
|
|
}
|
2019-05-24 15:40:39 +00:00
|
|
|
let res = ({} as any) as HostRule;
|
|
|
|
// First, apply empty rule matches
|
|
|
|
hostRules
|
|
|
|
.filter(rule => isEmptyRule(rule))
|
|
|
|
.forEach(rule => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
// Next, find hostType-only matches
|
|
|
|
hostRules
|
|
|
|
.filter(rule => isHostTypeRule(rule) && matchesHostType(rule, search))
|
|
|
|
.forEach(rule => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
// Next, find domainName-only matches
|
|
|
|
hostRules
|
|
|
|
.filter(rule => isDomainNameRule(rule) && matchesDomainName(rule, search))
|
|
|
|
.forEach(rule => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
// Next, find hostName-only matches
|
|
|
|
hostRules
|
|
|
|
.filter(rule => isHostNameRule(rule) && matchesHostName(rule, search))
|
|
|
|
.forEach(rule => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
// Next, find baseUrl-only matches
|
|
|
|
hostRules
|
|
|
|
.filter(rule => isBaseUrlRule(rule) && matchesBaseUrl(rule, search))
|
|
|
|
.forEach(rule => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
// Finally, find combination matches
|
|
|
|
hostRules
|
|
|
|
.filter(
|
|
|
|
rule =>
|
|
|
|
isMultiRule(rule) &&
|
|
|
|
matchesHostType(rule, search) &&
|
|
|
|
(matchesDomainName(rule, search) ||
|
|
|
|
matchesHostName(rule, search) ||
|
|
|
|
matchesBaseUrl(rule, search))
|
|
|
|
)
|
|
|
|
.forEach(rule => {
|
|
|
|
res = merge(res, rule);
|
|
|
|
});
|
|
|
|
delete res.hostType;
|
|
|
|
delete res.domainName;
|
|
|
|
delete res.hostName;
|
|
|
|
delete res.baseUrl;
|
|
|
|
return res;
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 11:20:09 +00:00
|
|
|
export function hosts({ hostType }: { hostType: string }) {
|
2019-05-24 15:40:39 +00:00
|
|
|
return hostRules
|
|
|
|
.filter(rule => rule.hostType === hostType)
|
|
|
|
.map(rule => {
|
|
|
|
if (rule.hostName) return rule.hostName;
|
|
|
|
if (rule.baseUrl) return URL.parse(rule.baseUrl).hostname;
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.filter(Boolean);
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
export function clear() {
|
2019-05-24 15:40:39 +00:00
|
|
|
hostRules = [];
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|