utils.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. const ms = require("ms");
  2. const path = require("path");
  3. const nanoid = require("nanoid/generate");
  4. const JWT = require("jsonwebtoken");
  5. const { differenceInDays, differenceInHours, differenceInMonths, differenceInMilliseconds, addDays, subHours, subDays, subMonths, subYears } = require("date-fns");
  6. const hbs = require("hbs");
  7. const knexUtils = require('./knex')
  8. const env = require("../env");
  9. class CustomError extends Error {
  10. constructor(message, statusCode, data) {
  11. super(message);
  12. this.name = this.constructor.name;
  13. this.statusCode = statusCode ?? 500;
  14. this.data = data;
  15. }
  16. }
  17. const urlRegex = /^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\u00a1-\uffff][a-z0-9\u00a1-\uffff_-]{0,62})?[a-z0-9\u00a1-\uffff]\.)+(?:[a-z\u00a1-\uffff]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$/i;
  18. function isAdmin(email) {
  19. return env.ADMIN_EMAILS.split(",")
  20. .map((e) => e.trim())
  21. .includes(email)
  22. }
  23. function signToken(user) {
  24. return JWT.sign(
  25. {
  26. iss: "ApiAuth",
  27. sub: user.email,
  28. domain: user.domain || "",
  29. iat: parseInt((new Date().getTime() / 1000).toFixed(0)),
  30. exp: parseInt((addDays(new Date(), 7).getTime() / 1000).toFixed(0))
  31. },
  32. env.JWT_SECRET
  33. )
  34. }
  35. async function generateId(query, domain_id) {
  36. const address = nanoid(
  37. "abcdefghkmnpqrstuvwxyzABCDEFGHKLMNPQRSTUVWXYZ23456789",
  38. env.LINK_LENGTH
  39. );
  40. const link = await query.link.find({ address, domain_id });
  41. if (!link) return address;
  42. return generateId(domain_id);
  43. }
  44. function addProtocol(url) {
  45. const hasProtocol = /^(\w+:|\/\/)/.test(url);
  46. return hasProtocol ? url : "http://" + url;
  47. }
  48. function getShortURL(address, domain) {
  49. const protocol = (env.CUSTOM_DOMAIN_USE_HTTPS || !domain) && !env.isDev ? "https://" : "http://";
  50. const link = `${domain || env.DEFAULT_DOMAIN}/${address}`;
  51. const url = `${protocol}${link}`;
  52. return { link, url };
  53. }
  54. const getRedisKey = {
  55. // TODO: remove user id and make domain id required
  56. link: (address, domain_id, user_id) => `${address}-${domain_id || ""}-${user_id || ""}`,
  57. domain: (address) => `d-${address}`,
  58. host: (address) => `h-${address}`,
  59. user: (emailOrKey) => `u-${emailOrKey}`
  60. };
  61. function getStatsLimit() {
  62. return env.DEFAULT_MAX_STATS_PER_LINK || 100000000;
  63. };
  64. function getStatsCacheTime(total) {
  65. return (total > 50000 ? ms("5 minutes") : ms("1 minutes")) / 1000
  66. };
  67. function statsObjectToArray(obj) {
  68. const objToArr = (key) =>
  69. Array.from(Object.keys(obj[key]))
  70. .map((name) => ({
  71. name,
  72. value: obj[key][name]
  73. }))
  74. .sort((a, b) => b.value - a.value);
  75. return {
  76. browser: objToArr("browser"),
  77. os: objToArr("os"),
  78. country: objToArr("country"),
  79. referrer: objToArr("referrer")
  80. };
  81. }
  82. function getDifferenceFunction(type) {
  83. if (type === "lastDay") return differenceInHours;
  84. if (type === "lastWeek") return differenceInDays;
  85. if (type === "lastMonth") return differenceInDays;
  86. if (type === "lastYear") return differenceInMonths;
  87. throw new Error("Unknown type.");
  88. }
  89. function getUTCDate(dateString) {
  90. const date = new Date(dateString || Date.now());
  91. return new Date(
  92. date.getUTCFullYear(),
  93. date.getUTCMonth(),
  94. date.getUTCDate(),
  95. date.getUTCHours()
  96. );
  97. }
  98. function getStatsPeriods(now) {
  99. return [
  100. ["lastDay", subHours(now, 24)],
  101. ["lastWeek", subDays(now, 7)],
  102. ["lastMonth", subDays(now, 30)],
  103. ["lastYear", subMonths(now, 12)],
  104. ]
  105. }
  106. const preservedURLs = [
  107. "login",
  108. "logout",
  109. "signup",
  110. "reset-password",
  111. "resetpassword",
  112. "url-password",
  113. "url-info",
  114. "settings",
  115. "stats",
  116. "verify",
  117. "api",
  118. "404",
  119. "static",
  120. "images",
  121. "banned",
  122. "terms",
  123. "privacy",
  124. "protected",
  125. "report",
  126. "pricing"
  127. ];
  128. function getInitStats() {
  129. return Object.create({
  130. browser: {
  131. chrome: 0,
  132. edge: 0,
  133. firefox: 0,
  134. ie: 0,
  135. opera: 0,
  136. other: 0,
  137. safari: 0
  138. },
  139. os: {
  140. android: 0,
  141. ios: 0,
  142. linux: 0,
  143. macos: 0,
  144. other: 0,
  145. windows: 0
  146. },
  147. country: {},
  148. referrer: {}
  149. });
  150. }
  151. // format date to relative date
  152. const MINUTE = 60,
  153. HOUR = MINUTE * 60,
  154. DAY = HOUR * 24,
  155. WEEK = DAY * 7,
  156. MONTH = DAY * 30,
  157. YEAR = DAY * 365;
  158. function getTimeAgo(date) {
  159. const secondsAgo = Math.round((Date.now() - Number(date)) / 1000);
  160. if (secondsAgo < MINUTE) {
  161. return `${secondsAgo} second${secondsAgo !== 1 ? "s" : ""} ago`;
  162. }
  163. let divisor;
  164. let unit = "";
  165. if (secondsAgo < HOUR) {
  166. [divisor, unit] = [MINUTE, "minute"];
  167. } else if (secondsAgo < DAY) {
  168. [divisor, unit] = [HOUR, "hour"];
  169. } else if (secondsAgo < WEEK) {
  170. [divisor, unit] = [DAY, "day"];
  171. } else if (secondsAgo < MONTH) {
  172. [divisor, unit] = [WEEK, "week"];
  173. } else if (secondsAgo < YEAR) {
  174. [divisor, unit] = [MONTH, "month"];
  175. } else {
  176. [divisor, unit] = [YEAR, "year"];
  177. }
  178. const count = Math.floor(secondsAgo / divisor);
  179. return `${count} ${unit}${count > 1 ? "s" : ""} ago`;
  180. }
  181. const sanitize = {
  182. domain: domain => ({
  183. ...domain,
  184. id: domain.uuid,
  185. uuid: undefined,
  186. user_id: undefined,
  187. banned_by_id: undefined
  188. }),
  189. link: link => ({
  190. ...link,
  191. banned_by_id: undefined,
  192. domain_id: undefined,
  193. user_id: undefined,
  194. uuid: undefined,
  195. id: link.uuid,
  196. relative_created_at: getTimeAgo(link.created_at),
  197. relative_expire_in: link.expire_in && ms(differenceInMilliseconds(new Date(link.expire_in), new Date()), { long: true }),
  198. password: !!link.password,
  199. link: getShortURL(link.address, link.domain)
  200. })
  201. };
  202. function sleep(ms) {
  203. return new Promise(resolve => setTimeout(resolve, ms));
  204. }
  205. function removeWww(host) {
  206. return host.replace("www.", "");
  207. };
  208. function registerHandlebarsHelpers() {
  209. hbs.registerHelper("ifEquals", function(arg1, arg2, options) {
  210. return (arg1 === arg2) ? options.fn(this) : options.inverse(this);
  211. });
  212. hbs.registerHelper("json", function(context) {
  213. return JSON.stringify(context);
  214. });
  215. const blocks = {};
  216. hbs.registerHelper("extend", function(name, context) {
  217. let block = blocks[name];
  218. if (!block) {
  219. block = blocks[name] = [];
  220. }
  221. block.push(context.fn(this));
  222. });
  223. hbs.registerHelper("block", function(name) {
  224. const val = (blocks[name] || []).join('\n');
  225. blocks[name] = [];
  226. return val;
  227. });
  228. hbs.registerPartials(path.join(__dirname, "../views/partials"), function (err) {});
  229. }
  230. module.exports = {
  231. addProtocol,
  232. CustomError,
  233. generateId,
  234. getDifferenceFunction,
  235. getInitStats,
  236. getRedisKey,
  237. getShortURL,
  238. getStatsCacheTime,
  239. getStatsLimit,
  240. getStatsPeriods,
  241. getUTCDate,
  242. isAdmin,
  243. preservedURLs,
  244. registerHandlebarsHelpers,
  245. removeWww,
  246. sanitize,
  247. signToken,
  248. sleep,
  249. statsObjectToArray,
  250. urlRegex,
  251. ...knexUtils,
  252. }