utils.js 8.6 KB

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