mockttp
Version:
Mock HTTP server for testing HTTP clients and stubbing webservices
28 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLocalhostAddress = exports.isIP = exports.isIPv6Address = exports.isIPv4Address = void 0;
exports.normalizeIP = normalizeIP;
// These are rough tests for IPs: they exclude valid domain names,
// but they don't strictly check IP formatting (that's fine - invalid
// IPs will fail elsewhere - this is for intended-format checks).
const IPv4_REGEX = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
const IPv6_REGEX = /^(?=.*[0-9a-fA-F])(?=.*:)[0-9a-fA-F:]{2,39}$/;
const isIPv4Address = (ip) => IPv4_REGEX.test(ip);
exports.isIPv4Address = isIPv4Address;
const isIPv6Address = (ip) => IPv6_REGEX.test(ip);
exports.isIPv6Address = isIPv6Address;
const isIP = (ip) => (0, exports.isIPv4Address)(ip) || (0, exports.isIPv6Address)(ip);
exports.isIP = isIP;
function normalizeIP(ip) {
return (ip && ip.startsWith('::ffff:'))
? ip.slice('::ffff:'.length)
: ip;
}
const isLocalhostAddress = (host) => !!host && ( // Null/undef are something else weird, but not localhost
host === 'localhost' || // Most common
host.endsWith('.localhost') ||
host === '::1' || // IPv6
normalizeIP(host).match(/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) // 127.0.0.0/8 range
);
exports.isLocalhostAddress = isLocalhostAddress;
//# sourceMappingURL=ip-utils.js.map