utils.js 8.6 KB

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