utils.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import crypto from "crypto";
  2. import CoreClass from "../core";
  3. let UtilsModule;
  4. class _UtilsModule extends CoreClass {
  5. // eslint-disable-next-line require-jsdoc
  6. constructor() {
  7. super("utils");
  8. UtilsModule = this;
  9. }
  10. /**
  11. * Initialises the utils module
  12. *
  13. * @returns {Promise} - returns promise (reject, resolve)
  14. */
  15. initialize() {
  16. return new Promise(resolve => {
  17. resolve();
  18. });
  19. }
  20. /**
  21. * Parses the cookie into a readable object
  22. *
  23. * @param {object} payload - object that contains the payload
  24. * @param {string} payload.cookieString - the cookie string
  25. * @returns {Promise} - returns promise (reject, resolve)
  26. */
  27. PARSE_COOKIES(payload) {
  28. return new Promise((resolve, reject) => {
  29. const cookies = {};
  30. if (typeof payload.cookieString !== "string") {
  31. reject(new Error("Cookie string is not a string"));
  32. return;
  33. }
  34. // eslint-disable-next-line array-callback-return
  35. payload.cookieString.split("; ").map(cookie => {
  36. cookies[cookie.substring(0, cookie.indexOf("="))] = cookie.substring(
  37. cookie.indexOf("=") + 1,
  38. cookie.length
  39. );
  40. });
  41. resolve(cookies);
  42. });
  43. }
  44. // COOKIES_TO_STRING() {//cookies
  45. // return new Promise((resolve, reject) => {
  46. // let newCookie = [];
  47. // for (let prop in cookie) {
  48. // newCookie.push(prop + "=" + cookie[prop]);
  49. // }
  50. // return newCookie.join("; ");
  51. // });
  52. // }
  53. /**
  54. * Removes a cookie by name
  55. *
  56. * @param {object} payload - object that contains the payload
  57. * @param {object} payload.cookieString - the cookie string
  58. * @param {string} payload.cookieName - the unique name of the cookie
  59. * @returns {Promise} - returns promise (reject, resolve)
  60. */
  61. REMOVE_COOKIE(payload) {
  62. return new Promise((resolve, reject) => {
  63. let cookies;
  64. try {
  65. cookies = UtilsModule.runJob(
  66. "PARSE_COOKIES",
  67. {
  68. cookieString: payload.cookieString
  69. },
  70. this
  71. );
  72. } catch (err) {
  73. reject(err);
  74. return;
  75. }
  76. delete cookies[payload.cookieName];
  77. resolve();
  78. });
  79. }
  80. /**
  81. * Replaces any html reserved characters in a string with html entities
  82. *
  83. * @param {object} payload - object that contains the payload
  84. * @param {string} payload.str - the string to replace characters with html entities
  85. * @returns {Promise} - returns promise (reject, resolve)
  86. */
  87. HTML_ENTITIES(payload) {
  88. return new Promise(resolve => {
  89. resolve(
  90. String(payload.str)
  91. .replace(/&/g, "&")
  92. .replace(/</g, "&lt;")
  93. .replace(/>/g, "&gt;")
  94. .replace(/"/g, "&quot;")
  95. );
  96. });
  97. }
  98. /**
  99. * Generates a random string of a specified length
  100. *
  101. * @param {object} payload - object that contains the payload
  102. * @param {number} payload.length - the length the random string should be
  103. * @returns {Promise} - returns promise (reject, resolve)
  104. */
  105. async GENERATE_RANDOM_STRING(payload) {
  106. const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  107. const promises = [];
  108. for (let i = 0; i < payload.length; i += 1) {
  109. promises.push(
  110. UtilsModule.runJob(
  111. "GET_RANDOM_NUMBER",
  112. {
  113. min: 0,
  114. max: chars.length - 1
  115. },
  116. this
  117. )
  118. );
  119. }
  120. const randomNums = await Promise.all(promises);
  121. const randomChars = [];
  122. for (let i = 0; i < payload.length; i += 1) {
  123. randomChars.push(chars[randomNums[i]]);
  124. }
  125. return new Promise(resolve => {
  126. resolve(randomChars.join(""));
  127. });
  128. }
  129. /**
  130. * Creates a random number within a range
  131. *
  132. * @param {object} payload - object that contains the payload
  133. * @param {number} payload.min - the minimum number the result should be
  134. * @param {number} payload.max - the maximum number the result should be
  135. * @returns {Promise} - returns promise (reject, resolve)
  136. */
  137. GET_RANDOM_NUMBER(payload) {
  138. // min, max
  139. return new Promise(resolve => {
  140. resolve(Math.floor(Math.random() * (payload.max - payload.min + 1)) + payload.min);
  141. });
  142. }
  143. /**
  144. * Converts ISO8601 time format (YouTube API) to HH:MM:SS
  145. *
  146. * @param {object} payload - object contaiing the payload
  147. * @param {string} payload.duration - string in the format of ISO8601
  148. * @returns {Promise} - returns a promise (resolve, reject)
  149. */
  150. CONVERT_TIME(payload) {
  151. // duration
  152. return new Promise(resolve => {
  153. let { duration } = payload;
  154. let a = duration.match(/\d+/g);
  155. if (duration.indexOf("M") >= 0 && duration.indexOf("H") === -1 && duration.indexOf("S") === -1) {
  156. a = [0, a[0], 0];
  157. }
  158. if (duration.indexOf("H") >= 0 && duration.indexOf("M") === -1) {
  159. a = [a[0], 0, a[1]];
  160. }
  161. if (duration.indexOf("H") >= 0 && duration.indexOf("M") === -1 && duration.indexOf("S") === -1) {
  162. a = [a[0], 0, 0];
  163. }
  164. duration = 0;
  165. if (a.length === 3) {
  166. duration += parseInt(a[0]) * 3600;
  167. duration += parseInt(a[1]) * 60;
  168. duration += parseInt(a[2]);
  169. }
  170. if (a.length === 2) {
  171. duration += parseInt(a[0]) * 60;
  172. duration += parseInt(a[1]);
  173. }
  174. if (a.length === 1) {
  175. duration += parseInt(a[0]);
  176. }
  177. const hours = Math.floor(duration / 3600);
  178. const minutes = Math.floor((duration % 3600) / 60);
  179. const seconds = Math.floor((duration % 3600) % 60);
  180. resolve(
  181. (hours < 10 ? `0${hours}:` : `${hours}:`) +
  182. (minutes < 10 ? `0${minutes}:` : `${minutes}:`) +
  183. (seconds < 10 ? `0${seconds}` : seconds)
  184. );
  185. });
  186. }
  187. /**
  188. * Creates a random identifier for e.g. sessionId
  189. *
  190. * @returns {Promise} - returns promise (reject, resolve)
  191. */
  192. GUID() {
  193. return new Promise(resolve => {
  194. resolve(
  195. [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1]
  196. .map(b =>
  197. b
  198. ? Math.floor((1 + Math.random()) * 0x10000)
  199. .toString(16)
  200. .substring(1)
  201. : "-"
  202. )
  203. .join("")
  204. );
  205. });
  206. }
  207. /**
  208. * Shuffles an array
  209. *
  210. * @param {object} payload - object that contains the payload
  211. * @param {object} payload.array - an array of songs that should be shuffled
  212. * @returns {Promise} - returns promise (reject, resolve)
  213. */
  214. SHUFFLE(payload) {
  215. // array
  216. return new Promise(resolve => {
  217. const { array } = payload;
  218. // sort the positions array
  219. let currentIndex = array.length;
  220. let temporaryValue;
  221. let randomIndex;
  222. // While there remain elements to shuffle...
  223. while (currentIndex !== 0) {
  224. // Pick a remaining element...
  225. randomIndex = Math.floor(Math.random() * currentIndex);
  226. currentIndex -= 1;
  227. // And swap it with the current element.
  228. temporaryValue = array[currentIndex];
  229. array[currentIndex] = array[randomIndex];
  230. array[randomIndex] = temporaryValue;
  231. }
  232. resolve({ array });
  233. });
  234. }
  235. /**
  236. * Shuffles an array of songs by their position property
  237. *
  238. * @param {object} payload - object that contains the payload
  239. * @param {object} payload.array - an array of songs that should be shuffled
  240. * @returns {Promise} - returns promise (reject, resolve)
  241. */
  242. SHUFFLE_SONG_POSITIONS(payload) {
  243. // array
  244. return new Promise(resolve => {
  245. const { array } = payload;
  246. // get array of positions
  247. const positions = [];
  248. array.forEach(song => positions.push(song.position));
  249. // sort the positions array
  250. let currentIndex = positions.length;
  251. let temporaryValue;
  252. let randomIndex;
  253. // While there remain elements to shuffle...
  254. while (currentIndex !== 0) {
  255. // Pick a remaining element...
  256. randomIndex = Math.floor(Math.random() * currentIndex);
  257. currentIndex -= 1;
  258. // And swap it with the current element.
  259. temporaryValue = positions[currentIndex];
  260. positions[currentIndex] = positions[randomIndex];
  261. positions[randomIndex] = temporaryValue;
  262. }
  263. // assign new positions
  264. array.forEach((song, index) => {
  265. song.position = positions[index];
  266. });
  267. resolve({ array });
  268. });
  269. }
  270. /**
  271. * Creates an error
  272. *
  273. * @param {object} payload - object that contains the payload
  274. * @param {object} payload.error - object that contains the error
  275. * @param {string} payload.message - possible error message
  276. * @param {object} payload.errors - possible object that contains multiple errors
  277. * @returns {Promise} - returns promise (reject, resolve)
  278. */
  279. GET_ERROR(payload) {
  280. return new Promise(resolve => {
  281. let error = "An error occurred.";
  282. if (typeof payload.error === "string") error = payload.error;
  283. else if (payload.error.message) {
  284. if (payload.error.message !== "Validation failed") error = payload.error.message;
  285. else error = payload.error.errors[Object.keys(payload.error.errors)].message;
  286. }
  287. resolve(error);
  288. });
  289. }
  290. /**
  291. * Creates the gravatar url for a specified email address
  292. *
  293. * @param {object} payload - object that contains the payload
  294. * @param {string} payload.email - the email address
  295. * @returns {Promise} - returns promise (reject, resolve)
  296. */
  297. CREATE_GRAVATAR(payload) {
  298. return new Promise(resolve => {
  299. const hash = crypto.createHash("md5").update(payload.email).digest("hex");
  300. resolve(`https://www.gravatar.com/avatar/${hash}`);
  301. });
  302. }
  303. /**
  304. * @returns {Promise} - returns promise (reject, resolve)
  305. */
  306. DEBUG() {
  307. return new Promise(resolve => {
  308. resolve();
  309. });
  310. }
  311. }
  312. export default new _UtilsModule();