@finapi/web-form
Version:
Library for integrating the finAPI Web Form
44 lines • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateUrl = void 0;
var LOCALHOST = "localhost";
var FINAPI_DOMAIN = ".finapi.net";
var FINAPI_PUBLIC_DOMAIN = ".finapi.io";
var isPrivateIPv4 = function (ip) {
if (/^(10)\.(.*)\.(.*)\.(.*)$/.test(ip)) {
// 10.x.x.x
return true;
}
else if (/^(172)\.(1[6-9]|2\d|3[0-1])\.(.*)\.(.*)$/.test(ip)) {
// 172.16.x.x - 172.31.255.255
return true;
}
else {
// 192.168.x.x
return /^(192)\.(168)\.(.*)\.(.*)$/.test(ip);
}
};
var isTargetLocal = function (targetUrl) {
var hostname = new URL(targetUrl).hostname;
return (hostname === LOCALHOST ||
// the given IP address belongs to a private network (local testing)
isPrivateIPv4(hostname));
};
var isFinApiDomain = function (targetUrl) {
var hostname = new URL(targetUrl).hostname;
return (hostname.endsWith(FINAPI_DOMAIN) || hostname.endsWith(FINAPI_PUBLIC_DOMAIN));
};
var validateUrl = function (url) {
try {
// if the url is not valid, the constructor throws an error
new URL(url);
if (!isTargetLocal(url) && !isFinApiDomain(url)) {
throw Error("The URL must be local or belong to the finAPI domain.");
}
}
catch (err) {
throw new Error("Invalid 'url': " + url);
}
};
exports.validateUrl = validateUrl;
//# sourceMappingURL=url.util.js.map