utils.js 8.9 KB

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