2016-02-20 03:39:51 +01:00
|
|
|
import url from 'url';
|
|
|
|
import validator from 'validator';
|
|
|
|
|
2017-04-25 17:04:57 +02:00
|
|
|
function appendProtocol(testUrl) {
|
2017-04-29 16:52:12 +02:00
|
|
|
const parsed = url.parse(testUrl);
|
|
|
|
if (!parsed.protocol) {
|
|
|
|
return `http://${testUrl}`;
|
|
|
|
}
|
|
|
|
return testUrl;
|
2017-04-25 17:04:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeUrl(testUrl) {
|
2017-04-29 16:52:12 +02:00
|
|
|
const urlWithProtocol = appendProtocol(testUrl);
|
2017-04-18 23:30:00 +02:00
|
|
|
|
2017-04-29 16:52:12 +02:00
|
|
|
const validatorOptions = {
|
|
|
|
require_protocol: true,
|
|
|
|
require_tld: false,
|
|
|
|
allow_trailing_dot: true, // mDNS addresses, https://github.com/jiahaog/nativefier/issues/308
|
|
|
|
};
|
|
|
|
if (!validator.isURL(urlWithProtocol, validatorOptions)) {
|
|
|
|
throw new Error(`Your Url: "${urlWithProtocol}" is invalid!`);
|
|
|
|
}
|
|
|
|
return urlWithProtocol;
|
2016-02-20 03:39:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default normalizeUrl;
|