2016-10-09 19:14:02 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-12-09 20:46:53 +00:00
|
|
|
const cheerio = require("cheerio");
|
2019-04-15 16:19:50 +00:00
|
|
|
const got = require("got");
|
2018-04-27 13:27:26 +00:00
|
|
|
const URL = require("url").URL;
|
2017-12-30 10:46:51 +00:00
|
|
|
const mime = require("mime-types");
|
2016-12-09 20:46:53 +00:00
|
|
|
const Helper = require("../../helper");
|
2017-09-28 08:58:43 +00:00
|
|
|
const cleanIrcMessage = require("../../../client/js/libs/handlebars/ircmessageparser/cleanIrcMessage");
|
2017-06-26 09:01:55 +00:00
|
|
|
const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks");
|
2017-07-06 15:33:09 +00:00
|
|
|
const storage = require("../storage");
|
2018-06-10 17:32:52 +00:00
|
|
|
const currentFetchPromises = new Map();
|
2019-04-15 16:19:50 +00:00
|
|
|
const imageTypeRegex = /^image\/.+/;
|
2017-12-14 11:14:45 +00:00
|
|
|
const mediaTypeRegex = /^(audio|video)\/.+/;
|
|
|
|
|
2018-03-06 13:11:42 +00:00
|
|
|
// Fix ECDH curve client compatibility in Node v8/v9
|
|
|
|
// This is fixed in Node 10, but The Lounge supports LTS versions
|
|
|
|
// https://github.com/nodejs/node/issues/16196
|
|
|
|
// https://github.com/nodejs/node/pull/16853
|
2018-03-07 06:44:22 +00:00
|
|
|
// https://github.com/nodejs/node/pull/15206
|
2018-03-06 13:11:42 +00:00
|
|
|
const tls = require("tls");
|
2018-03-07 06:44:22 +00:00
|
|
|
const semver = require("semver");
|
2018-03-06 13:11:42 +00:00
|
|
|
|
2018-03-07 06:44:22 +00:00
|
|
|
if (semver.gte(process.version, "8.6.0") && tls.DEFAULT_ECDH_CURVE === "prime256v1") {
|
2018-03-06 13:11:42 +00:00
|
|
|
tls.DEFAULT_ECDH_CURVE = "auto";
|
|
|
|
}
|
|
|
|
|
2017-06-26 09:01:55 +00:00
|
|
|
module.exports = function(client, chan, msg) {
|
2016-12-09 20:46:53 +00:00
|
|
|
if (!Helper.config.prefetch) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-26 09:01:55 +00:00
|
|
|
// Remove all IRC formatting characters before searching for links
|
2017-09-28 08:58:43 +00:00
|
|
|
const cleanText = cleanIrcMessage(msg.text);
|
2017-06-26 09:01:55 +00:00
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
msg.previews = findLinks(cleanText).reduce((cleanLinks, link) => {
|
|
|
|
const url = normalizeURL(link.link);
|
2016-12-09 20:46:53 +00:00
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
// If the URL is invalid and cannot be normalized, don't fetch it
|
|
|
|
if (url === null) {
|
|
|
|
return cleanLinks;
|
|
|
|
}
|
2016-12-09 20:46:53 +00:00
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
// If there are too many urls in this message, only fetch first X valid links
|
|
|
|
if (cleanLinks.length > 4) {
|
|
|
|
return cleanLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not fetch duplicate links twice
|
|
|
|
if (cleanLinks.some((l) => l.link === link.link)) {
|
|
|
|
return cleanLinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
const preview = {
|
|
|
|
type: "loading",
|
|
|
|
head: "",
|
|
|
|
body: "",
|
|
|
|
thumb: "",
|
|
|
|
link: link.link, // Send original matched link to the client
|
|
|
|
shown: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
cleanLinks.push(preview);
|
|
|
|
|
|
|
|
fetch(url, {
|
2018-03-23 14:50:52 +00:00
|
|
|
accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
|
|
|
|
language: client.language,
|
2018-06-10 17:32:52 +00:00
|
|
|
}).then((res) => {
|
2018-07-10 11:57:11 +00:00
|
|
|
parse(msg, chan, preview, res, client);
|
2018-06-10 17:32:52 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
preview.type = "error";
|
|
|
|
preview.error = "message";
|
|
|
|
preview.message = err.message;
|
2018-07-10 11:57:11 +00:00
|
|
|
handlePreview(client, chan, msg, preview, null);
|
2017-07-06 06:16:01 +00:00
|
|
|
});
|
2018-04-27 13:27:26 +00:00
|
|
|
|
|
|
|
return cleanLinks;
|
|
|
|
}, []);
|
2014-09-27 19:17:05 +00:00
|
|
|
};
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
function parseHtml(preview, res, client) {
|
|
|
|
return new Promise((resolve) => {
|
2018-01-11 11:33:36 +00:00
|
|
|
const $ = cheerio.load(res.data);
|
2017-08-13 09:58:27 +00:00
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
return parseHtmlMedia($, preview, client)
|
2017-12-14 11:14:45 +00:00
|
|
|
.then((newRes) => resolve(newRes))
|
|
|
|
.catch(() => {
|
|
|
|
preview.type = "link";
|
|
|
|
preview.head =
|
|
|
|
$('meta[property="og:title"]').attr("content")
|
2018-06-11 08:32:32 +00:00
|
|
|
|| $("head > title, title").first().text()
|
2017-12-14 11:14:45 +00:00
|
|
|
|| "";
|
|
|
|
preview.body =
|
|
|
|
$('meta[property="og:description"]').attr("content")
|
|
|
|
|| $('meta[name="description"]').attr("content")
|
|
|
|
|| "";
|
|
|
|
preview.thumb =
|
|
|
|
$('meta[property="og:image"]').attr("content")
|
|
|
|
|| $('meta[name="twitter:image:src"]').attr("content")
|
|
|
|
|| $('link[rel="image_src"]').attr("href")
|
|
|
|
|| "";
|
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
// Make sure thumbnail is a valid and absolute url
|
2017-12-14 11:14:45 +00:00
|
|
|
if (preview.thumb.length) {
|
2018-04-27 13:27:26 +00:00
|
|
|
preview.thumb = normalizeURL(preview.thumb, preview.link) || "";
|
2017-06-22 19:32:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
// Verify that thumbnail pic exists and is under allowed size
|
|
|
|
if (preview.thumb.length) {
|
2018-06-10 17:32:52 +00:00
|
|
|
fetch(preview.thumb, {language: client.language}).then((resThumb) => {
|
2017-12-14 11:14:45 +00:00
|
|
|
if (resThumb === null
|
2019-04-15 16:19:50 +00:00
|
|
|
|| !(imageTypeRegex.test(resThumb.type))
|
2017-12-14 11:14:45 +00:00
|
|
|
|| resThumb.size > (Helper.config.prefetchMaxImageSize * 1024)) {
|
|
|
|
preview.thumb = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(resThumb);
|
2018-06-10 17:32:52 +00:00
|
|
|
}).catch(() => {
|
|
|
|
preview.thumb = "";
|
|
|
|
resolve(null);
|
2017-12-14 11:14:45 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve(res);
|
|
|
|
}
|
2017-06-22 19:32:13 +00:00
|
|
|
});
|
2017-12-14 11:14:45 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-22 19:32:13 +00:00
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
function parseHtmlMedia($, preview, client) {
|
2017-12-14 11:14:45 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let foundMedia = false;
|
|
|
|
|
|
|
|
["video", "audio"].forEach((type) => {
|
|
|
|
if (foundMedia) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$(`meta[property="og:${type}:type"]`).each(function(i) {
|
|
|
|
const mimeType = $(this).attr("content");
|
|
|
|
|
|
|
|
if (mediaTypeRegex.test(mimeType)) {
|
|
|
|
// If we match a clean video or audio tag, parse that as a preview instead
|
2018-04-27 13:27:26 +00:00
|
|
|
let mediaUrl = $($(`meta[property="og:${type}"]`).get(i)).attr("content");
|
2017-12-14 11:14:45 +00:00
|
|
|
|
|
|
|
// Make sure media is a valid url
|
2018-04-27 13:27:26 +00:00
|
|
|
mediaUrl = normalizeURL(mediaUrl, preview.link, true);
|
|
|
|
|
|
|
|
// Make sure media is a valid url
|
|
|
|
if (!mediaUrl) {
|
2017-12-14 11:14:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foundMedia = true;
|
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
fetch(mediaUrl, {
|
2018-03-23 14:50:52 +00:00
|
|
|
accept: type === "video" ?
|
|
|
|
"video/webm,video/ogg,video/*;q=0.9,application/ogg;q=0.7,audio/*;q=0.6,*/*;q=0.5" :
|
|
|
|
"audio/webm, audio/ogg, audio/wav, audio/*;q=0.9, application/ogg;q=0.7, video/*;q=0.6; */*;q=0.5",
|
|
|
|
language: client.language,
|
2018-06-10 17:32:52 +00:00
|
|
|
}).then((resMedia) => {
|
2017-12-14 11:14:45 +00:00
|
|
|
if (resMedia === null || !mediaTypeRegex.test(resMedia.type)) {
|
|
|
|
return reject();
|
|
|
|
}
|
|
|
|
|
|
|
|
preview.type = type;
|
|
|
|
preview.media = mediaUrl;
|
|
|
|
preview.mediaType = resMedia.type;
|
|
|
|
|
|
|
|
resolve(resMedia);
|
2018-06-10 17:32:52 +00:00
|
|
|
}).catch(reject);
|
2017-12-14 11:14:45 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!foundMedia) {
|
|
|
|
reject();
|
2017-06-22 19:32:13 +00:00
|
|
|
}
|
2017-12-14 11:14:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
function parse(msg, chan, preview, res, client) {
|
2017-12-14 11:14:45 +00:00
|
|
|
let promise;
|
2017-06-22 19:32:13 +00:00
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
switch (res.type) {
|
|
|
|
case "text/html":
|
|
|
|
promise = parseHtml(preview, res, client);
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "image/png":
|
|
|
|
case "image/gif":
|
|
|
|
case "image/jpg":
|
|
|
|
case "image/jpeg":
|
2017-11-27 13:50:35 +00:00
|
|
|
case "image/webp":
|
2017-07-06 15:33:09 +00:00
|
|
|
if (res.size > (Helper.config.prefetchMaxImageSize * 1024)) {
|
2017-12-28 16:28:49 +00:00
|
|
|
preview.type = "error";
|
2017-12-28 21:31:19 +00:00
|
|
|
preview.error = "image-too-big";
|
|
|
|
preview.maxSize = Helper.config.prefetchMaxImageSize * 1024;
|
2017-12-28 16:28:49 +00:00
|
|
|
} else {
|
|
|
|
preview.type = "image";
|
|
|
|
preview.thumb = preview.link;
|
2015-09-30 22:15:53 +00:00
|
|
|
}
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
break;
|
|
|
|
|
2017-12-06 22:27:35 +00:00
|
|
|
case "audio/midi":
|
|
|
|
case "audio/mpeg":
|
|
|
|
case "audio/mpeg3":
|
|
|
|
case "audio/ogg":
|
|
|
|
case "audio/wav":
|
|
|
|
case "audio/x-mid":
|
|
|
|
case "audio/x-midi":
|
|
|
|
case "audio/x-mpeg":
|
|
|
|
case "audio/x-mpeg-3":
|
|
|
|
if (!preview.link.startsWith("https://")) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-06 22:27:35 +00:00
|
|
|
preview.type = "audio";
|
2017-12-14 11:14:45 +00:00
|
|
|
preview.media = preview.link;
|
|
|
|
preview.mediaType = res.type;
|
2017-12-09 23:25:01 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "video/webm":
|
|
|
|
case "video/ogg":
|
|
|
|
case "video/mp4":
|
|
|
|
if (!preview.link.startsWith("https://")) {
|
|
|
|
break;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-12-09 23:25:01 +00:00
|
|
|
preview.type = "video";
|
2017-12-14 11:14:45 +00:00
|
|
|
preview.media = preview.link;
|
|
|
|
preview.mediaType = res.type;
|
2017-12-09 23:25:01 +00:00
|
|
|
|
2017-12-06 22:27:35 +00:00
|
|
|
break;
|
|
|
|
|
2014-09-27 19:17:05 +00:00
|
|
|
default:
|
2018-06-03 09:25:01 +00:00
|
|
|
return removePreview(msg, preview);
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
2014-09-27 23:47:04 +00:00
|
|
|
|
2017-12-14 11:14:45 +00:00
|
|
|
if (!promise) {
|
2018-07-10 11:57:11 +00:00
|
|
|
return handlePreview(client, chan, msg, preview, res);
|
2017-12-14 11:14:45 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
promise.then((newRes) => handlePreview(client, chan, msg, preview, newRes));
|
2017-07-06 15:33:09 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
function handlePreview(client, chan, msg, preview, res) {
|
2017-07-06 15:33:09 +00:00
|
|
|
if (!preview.thumb.length || !Helper.config.prefetchStorage) {
|
2018-07-10 11:57:11 +00:00
|
|
|
return emitPreview(client, chan, msg, preview);
|
2017-07-06 15:33:09 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 10:46:51 +00:00
|
|
|
// Get the correct file extension for the provided content-type
|
|
|
|
// This is done to prevent user-input being stored in the file name (extension)
|
|
|
|
const extension = mime.extension(res.type);
|
|
|
|
|
|
|
|
if (!extension) {
|
|
|
|
// For link previews, drop the thumbnail
|
|
|
|
// For other types, do not display preview at all
|
|
|
|
if (preview.type !== "link") {
|
2018-06-03 09:25:01 +00:00
|
|
|
return removePreview(msg, preview);
|
2017-12-30 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
preview.thumb = "";
|
2018-07-10 11:57:11 +00:00
|
|
|
return emitPreview(client, chan, msg, preview);
|
2017-12-30 10:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
storage.store(res.data, extension, (uri) => {
|
2017-08-13 09:58:27 +00:00
|
|
|
preview.thumb = uri;
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
emitPreview(client, chan, msg, preview);
|
2017-07-06 15:33:09 +00:00
|
|
|
});
|
2017-06-26 06:27:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
function emitPreview(client, chan, msg, preview) {
|
2017-06-26 06:27:51 +00:00
|
|
|
// If there is no title but there is preview or description, set title
|
|
|
|
// otherwise bail out and show no preview
|
2017-06-26 09:01:55 +00:00
|
|
|
if (!preview.head.length && preview.type === "link") {
|
|
|
|
if (preview.thumb.length || preview.body.length) {
|
|
|
|
preview.head = "Untitled page";
|
2017-06-26 06:27:51 +00:00
|
|
|
} else {
|
2018-06-03 09:25:01 +00:00
|
|
|
return removePreview(msg, preview);
|
2017-06-26 06:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:57:11 +00:00
|
|
|
client.emit("msg:preview", {
|
|
|
|
id: msg.id,
|
|
|
|
chan: chan.id,
|
|
|
|
preview: preview,
|
|
|
|
});
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-03 09:25:01 +00:00
|
|
|
function removePreview(msg, preview) {
|
|
|
|
// If a preview fails to load, remove the link from msg object
|
|
|
|
// So that client doesn't attempt to display an preview on page reload
|
|
|
|
const index = msg.previews.indexOf(preview);
|
|
|
|
|
|
|
|
if (index > -1) {
|
|
|
|
msg.previews.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 14:50:52 +00:00
|
|
|
function getRequestHeaders(headers) {
|
|
|
|
const formattedHeaders = {
|
2018-02-21 17:48:22 +00:00
|
|
|
"User-Agent": "Mozilla/5.0 (compatible; The Lounge IRC Client; +https://github.com/thelounge/thelounge)",
|
2018-03-24 20:53:02 +00:00
|
|
|
"Accept": headers.accept || "*/*",
|
2018-08-08 00:09:45 +00:00
|
|
|
"X-Purpose": "preview",
|
2017-12-28 13:34:49 +00:00
|
|
|
};
|
|
|
|
|
2018-03-23 14:50:52 +00:00
|
|
|
if (headers.language) {
|
|
|
|
formattedHeaders["Accept-Language"] = headers.language;
|
2017-12-28 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 14:50:52 +00:00
|
|
|
return formattedHeaders;
|
2017-12-28 13:34:49 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 17:32:52 +00:00
|
|
|
function fetch(uri, headers) {
|
|
|
|
// Stringify the object otherwise the objects won't compute to the same value
|
|
|
|
const cacheKey = JSON.stringify([uri, headers]);
|
|
|
|
let promise = currentFetchPromises.get(cacheKey);
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2018-06-10 17:32:52 +00:00
|
|
|
if (promise) {
|
|
|
|
return promise;
|
2015-01-04 02:58:12 +00:00
|
|
|
}
|
2017-11-10 20:44:14 +00:00
|
|
|
|
2018-06-10 17:32:52 +00:00
|
|
|
promise = new Promise((resolve, reject) => {
|
2019-04-15 16:19:50 +00:00
|
|
|
let buffer = Buffer.from("");
|
|
|
|
let request;
|
|
|
|
let response;
|
2018-06-10 17:32:52 +00:00
|
|
|
let limit = Helper.config.prefetchMaxImageSize * 1024;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
try {
|
|
|
|
got
|
|
|
|
.stream(uri, {
|
|
|
|
timeout: 5000,
|
|
|
|
headers: getRequestHeaders(headers),
|
|
|
|
rejectUnauthorized: false,
|
|
|
|
})
|
|
|
|
.on("request", (req) => request = req)
|
|
|
|
.on("response", function(res) {
|
|
|
|
response = res;
|
|
|
|
|
|
|
|
if (imageTypeRegex.test(res.headers["content-type"])) {
|
|
|
|
// response is an image
|
|
|
|
// if Content-Length header reports a size exceeding the prefetch limit, abort fetch
|
|
|
|
const contentLength = parseInt(res.headers["content-length"], 10) || 0;
|
|
|
|
|
|
|
|
if (contentLength > limit) {
|
|
|
|
request.abort();
|
|
|
|
}
|
|
|
|
} else if (mediaTypeRegex.test(res.headers["content-type"])) {
|
|
|
|
// We don't need to download the file any further after we received content-type header
|
|
|
|
request.abort();
|
|
|
|
} else {
|
|
|
|
// if not image, limit download to 50kb, since we need only meta tags
|
|
|
|
// twitter.com sends opengraph meta tags within ~20kb of data for individual tweets
|
|
|
|
limit = 1024 * 50;
|
2018-06-10 17:32:52 +00:00
|
|
|
}
|
2019-04-15 16:19:50 +00:00
|
|
|
})
|
|
|
|
.on("error", (e) => reject(e))
|
|
|
|
.on("data", (data) => {
|
|
|
|
buffer = Buffer.concat(
|
|
|
|
[buffer, data],
|
|
|
|
buffer.length + data.length
|
|
|
|
);
|
|
|
|
|
|
|
|
if (buffer.length >= limit) {
|
|
|
|
request.abort();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.on("end", () => {
|
|
|
|
let type = "";
|
|
|
|
let size = parseInt(response.headers["content-length"], 10) || buffer.length;
|
2017-06-21 05:51:14 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
if (size < buffer.length) {
|
|
|
|
size = buffer.length;
|
|
|
|
}
|
2017-07-06 15:33:09 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
if (response.headers["content-type"]) {
|
|
|
|
type = response.headers["content-type"].split(/ *; */).shift();
|
|
|
|
}
|
2018-06-10 17:32:52 +00:00
|
|
|
|
2019-04-15 16:19:50 +00:00
|
|
|
resolve({data: buffer, type, size});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
return reject(e);
|
|
|
|
}
|
2018-06-10 17:32:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const removeCache = () => currentFetchPromises.delete(cacheKey);
|
|
|
|
|
|
|
|
promise.then(removeCache).catch(removeCache);
|
|
|
|
|
|
|
|
currentFetchPromises.set(cacheKey, promise);
|
|
|
|
|
|
|
|
return promise;
|
2014-09-27 19:17:05 +00:00
|
|
|
}
|
2016-03-25 09:45:39 +00:00
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
function normalizeURL(link, baseLink, disallowHttp = false) {
|
2018-04-27 11:11:54 +00:00
|
|
|
try {
|
2018-04-27 13:27:26 +00:00
|
|
|
const url = new URL(link, baseLink);
|
2018-04-27 11:11:54 +00:00
|
|
|
|
|
|
|
// Only fetch http and https links
|
2018-04-27 13:27:26 +00:00
|
|
|
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disallowHttp && url.protocol === "http:") {
|
|
|
|
return null;
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not fetch links without hostname or ones that contain authorization
|
2018-04-27 13:27:26 +00:00
|
|
|
if (!url.hostname || url.username || url.password) {
|
|
|
|
return null;
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|
2018-04-27 13:27:26 +00:00
|
|
|
|
|
|
|
// Drop hash from the url, if any
|
|
|
|
url.hash = "";
|
|
|
|
|
|
|
|
return url.toString();
|
2018-04-27 11:11:54 +00:00
|
|
|
} catch (e) {
|
2018-04-27 13:27:26 +00:00
|
|
|
// if an exception was thrown, the url is not valid
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 13:27:26 +00:00
|
|
|
return null;
|
2018-04-27 11:11:54 +00:00
|
|
|
}
|