mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 09:10:35 +08:00
34 lines
638 B
JavaScript
34 lines
638 B
JavaScript
// @ts-check
|
|
const tldts = require('./cached-tld-parse');
|
|
|
|
/**
|
|
* @param {string} domain
|
|
*/
|
|
module.exports.isDomainLoose = (domain) => {
|
|
const { isIcann, isPrivate, isIp } = tldts.parse(domain);
|
|
return !!(!isIp && (isIcann || isPrivate));
|
|
};
|
|
|
|
/**
|
|
* @param {string | null | undefined} domain
|
|
*/
|
|
module.exports.normalizeDomain = (domain) => {
|
|
if (!domain) {
|
|
return null;
|
|
}
|
|
|
|
const { isIcann, isPrivate, hostname, isIp } = tldts.parse(domain);
|
|
if (isIp) {
|
|
return null;
|
|
}
|
|
|
|
if (isIcann || isPrivate) {
|
|
if (hostname?.[0] === '.') {
|
|
return hostname.slice(1);
|
|
}
|
|
return hostname;
|
|
}
|
|
|
|
return null;
|
|
};
|