UNPKG

@nasriya/dns

Version:
68 lines (67 loc) 2.92 kB
class Helpers { /** * Print something on the debug level * @param {string|any} message * @returns {void} */ printConsole(message) { if (process.env.HYPERCLOUD_SERVER_VERBOSE === 'TRUE') { console.debug(message); } } validate = Object.freeze({ /** * Validate an IPv4 or IPv6 address * @example * // Example usage: * console.log(validate.ipAddress('192.168.0.1')); // true * console.log(validate.ipAddress('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); // true * console.log(validate.ipAddress('invalid')); // false * @param {string} ip The IP address to validate * @returns {boolean} */ ipAddress: (ip) => { const ipPattern = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$|^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,7}:$|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})$|:^:$/; return ipPattern.test(ip); }, /** * Pass domain(s) to check whether they're valid to be used for the SSL certificate * @param {string|string[]} toCheck The domain(s) to check * @returns {boolean} */ domains: (toCheck) => { const regex = /^(\*\.)?([\w-]+\.)+[\w-]+$/; if (typeof toCheck === 'string') { return regex.test(toCheck); } else if (Array.isArray(toCheck)) { const invalidDomains = []; for (const domain of toCheck) { if (!regex.test(domain)) { invalidDomains.push(domain); } } if (invalidDomains.length === 0) { return true; } else { this.printConsole(`You have used invalid domains for the SSL certificate, the domains are: ${invalidDomains.toString()}.`); return false; } } else { throw new Error(`The value that was passed on the "validate.domains()" method is invalid. Expected a string or an array of strings but instead got ${typeof toCheck}`); } }, /** * Check the syntax validity of an email address * @param {string} email The email address to check * @returns {boolean} */ email: (email) => { const regex = /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/; return regex.test(email); } }); } export default new Helpers();