You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

boardData.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**
  2. * WHITEBOPHIR SERVER
  3. *********************************************************
  4. * @licstart The following is the entire license notice for the
  5. * JavaScript code in this page.
  6. *
  7. * Copyright (C) 2013-2014 Ophir LOJKINE
  8. *
  9. *
  10. * The JavaScript code in this page is free software: you can
  11. * redistribute it and/or modify it under the terms of the GNU
  12. * General Public License (GNU GPL) as published by the Free Software
  13. * Foundation, either version 3 of the License, or (at your option)
  14. * any later version. The code is distributed WITHOUT ANY WARRANTY;
  15. * without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
  17. *
  18. * As additional permission under GNU GPL version 3 section 7, you
  19. * may distribute non-source (e.g., minimized or compacted) forms of
  20. * that code without the copy of the GNU GPL normally required by
  21. * section 4, provided you include this license notice and a URL
  22. * through which recipients can access the Corresponding Source.
  23. *
  24. * @licend
  25. * @module boardData
  26. */
  27. var fs = require("./fs_promises.js"),
  28. log = require("./log.js").log,
  29. path = require("path"),
  30. config = require("./configuration.js"),
  31. Mutex = require("async-mutex").Mutex;
  32. /**
  33. * Represents a board.
  34. * @typedef {{[object_id:string]: any}} BoardElem
  35. */
  36. class BoardData {
  37. /**
  38. * @param {string} name
  39. */
  40. constructor(name) {
  41. this.name = name;
  42. /** @type {{[name: string]: BoardElem}} */
  43. this.board = {};
  44. this.file = path.join(
  45. config.HISTORY_DIR,
  46. "board-" + encodeURIComponent(name) + ".json"
  47. );
  48. this.lastSaveDate = Date.now();
  49. this.users = new Set();
  50. this.saveMutex = new Mutex();
  51. }
  52. /** Adds data to the board
  53. * @param {string} id
  54. * @param {BoardElem} data
  55. */
  56. set(id, data) {
  57. //KISS
  58. data.time = Date.now();
  59. this.validate(data);
  60. this.board[id] = data;
  61. this.delaySave();
  62. }
  63. /** Adds a child to an element that is already in the board
  64. * @param {string} parentId - Identifier of the parent element.
  65. * @param {BoardElem} child - Object containing the the values to update.
  66. * @returns {boolean} - True if the child was added, else false
  67. */
  68. addChild(parentId, child) {
  69. var obj = this.board[parentId];
  70. if (typeof obj !== "object") return false;
  71. if (Array.isArray(obj._children)) obj._children.push(child);
  72. else obj._children = [child];
  73. this.validate(obj);
  74. this.delaySave();
  75. return true;
  76. }
  77. /** Update the data in the board
  78. * @param {string} id - Identifier of the data to update.
  79. * @param {BoardElem} data - Object containing the values to update.
  80. * @param {boolean} create - True if the object should be created if it's not currently in the DB.
  81. */
  82. update(id, data, create) {
  83. delete data.type;
  84. delete data.tool;
  85. var obj = this.board[id];
  86. if (typeof obj === "object") {
  87. for (var i in data) {
  88. obj[i] = data[i];
  89. }
  90. } else if (create || obj !== undefined) {
  91. this.board[id] = data;
  92. }
  93. this.delaySave();
  94. }
  95. /** Copy elements in the board
  96. * @param {string} id - Identifier of the data to copy.
  97. * @param {BoardElem} data - Object containing the id of the new copied element.
  98. */
  99. copy(id, data) {
  100. var obj = this.board[id];
  101. var newid = data.newid;
  102. if (obj) {
  103. var newobj = JSON.parse(JSON.stringify(obj));
  104. newobj.id = newid;
  105. if (newobj._children) {
  106. for (var child of newobj._children) {
  107. child.parent = newid;
  108. }
  109. }
  110. this.board[newid] = newobj;
  111. } else {
  112. log("Copied object does not exist in board.", { object: id });
  113. }
  114. this.delaySave();
  115. }
  116. /** Removes data from the board
  117. * @param {string} id - Identifier of the data to delete.
  118. */
  119. delete(id) {
  120. //KISS
  121. delete this.board[id];
  122. this.delaySave();
  123. }
  124. /** Process a batch of messages
  125. * @typedef {{
  126. * id:string,
  127. * type: "delete" | "update" | "child",
  128. * parent?: string,
  129. * _children?: BoardMessage[],
  130. * } & BoardElem } BoardMessage
  131. * @param {BoardMessage[]} children array of messages to be delegated to the other methods
  132. */
  133. processMessageBatch(children) {
  134. for (const message of children) {
  135. this.processMessage(message);
  136. }
  137. }
  138. /** Process a single message
  139. * @param {BoardMessage} message instruction to apply to the board
  140. */
  141. processMessage(message) {
  142. if (message._children) return this.processMessageBatch(message._children);
  143. let id = message.id;
  144. switch (message.type) {
  145. case "delete":
  146. if (id) this.delete(id);
  147. break;
  148. case "update":
  149. if (id) this.update(id, message);
  150. break;
  151. case "copy":
  152. if (id) this.copy(id, message);
  153. break;
  154. case "child":
  155. this.addChild(message.parent, message);
  156. break;
  157. default:
  158. //Add data
  159. if (!id) throw new Error("Invalid message: ", message);
  160. this.set(id, message);
  161. }
  162. }
  163. /** Reads data from the board
  164. * @param {string} id - Identifier of the element to get.
  165. * @returns {BoardElem} The element with the given id, or undefined if no element has this id
  166. */
  167. get(id) {
  168. return this.board[id];
  169. }
  170. /** Reads data from the board
  171. * @param {string} [id] - Identifier of the first element to get.
  172. * @returns {BoardElem[]}
  173. */
  174. getAll(id) {
  175. return Object.entries(this.board)
  176. .filter(([i]) => !id || i > id)
  177. .map(([_, elem]) => elem);
  178. }
  179. /** Delays the triggering of auto-save by SAVE_INTERVAL seconds */
  180. delaySave() {
  181. if (this.saveTimeoutId !== undefined) clearTimeout(this.saveTimeoutId);
  182. this.saveTimeoutId = setTimeout(this.save.bind(this), config.SAVE_INTERVAL);
  183. if (Date.now() - this.lastSaveDate > config.MAX_SAVE_DELAY)
  184. setTimeout(this.save.bind(this), 0);
  185. }
  186. /** Saves the data in the board to a file. */
  187. async save() {
  188. // The mutex prevents multiple save operation to happen simultaneously
  189. this.saveMutex.runExclusive(this._unsafe_save.bind(this));
  190. }
  191. /** Save the board to disk without preventing multiple simultaneaous saves. Use save() instead */
  192. async _unsafe_save() {
  193. this.lastSaveDate = Date.now();
  194. this.clean();
  195. var file = this.file;
  196. var tmp_file = backupFileName(file);
  197. var board_txt = JSON.stringify(this.board);
  198. if (board_txt === "{}") {
  199. // empty board
  200. try {
  201. await fs.promises.unlink(file);
  202. log("removed empty board", { board: this.name });
  203. } catch (err) {
  204. if (err.code !== "ENOENT") {
  205. // If the file already wasn't saved, this is not an error
  206. log("board deletion error", { err: err.toString() });
  207. }
  208. }
  209. } else {
  210. try {
  211. await fs.promises.writeFile(tmp_file, board_txt, { flag: "wx" });
  212. await fs.promises.rename(tmp_file, file);
  213. log("saved board", {
  214. board: this.name,
  215. size: board_txt.length,
  216. delay_ms: Date.now() - this.lastSaveDate,
  217. });
  218. } catch (err) {
  219. log("board saving error", {
  220. board: this.name,
  221. err: err.toString(),
  222. tmp_file: tmp_file,
  223. });
  224. return;
  225. }
  226. }
  227. }
  228. /** Remove old elements from the board */
  229. clean() {
  230. var board = this.board;
  231. var ids = Object.keys(board);
  232. if (ids.length > config.MAX_ITEM_COUNT) {
  233. var toDestroy = ids
  234. .sort(function (x, y) {
  235. return (board[x].time | 0) - (board[y].time | 0);
  236. })
  237. .slice(0, -config.MAX_ITEM_COUNT);
  238. for (var i = 0; i < toDestroy.length; i++) delete board[toDestroy[i]];
  239. log("cleaned board", { removed: toDestroy.length, board: this.name });
  240. }
  241. }
  242. /** Reformats an item if necessary in order to make it follow the boards' policy
  243. * @param {object} item The object to edit
  244. */
  245. validate(item) {
  246. if (item.hasOwnProperty("size")) {
  247. item.size = parseInt(item.size) || 1;
  248. item.size = Math.min(Math.max(item.size, 1), 50);
  249. }
  250. if (item.hasOwnProperty("x") || item.hasOwnProperty("y")) {
  251. item.x = parseFloat(item.x) || 0;
  252. item.x = Math.min(Math.max(item.x, 0), config.MAX_BOARD_SIZE);
  253. item.x = Math.round(10 * item.x) / 10;
  254. item.y = parseFloat(item.y) || 0;
  255. item.y = Math.min(Math.max(item.y, 0), config.MAX_BOARD_SIZE);
  256. item.y = Math.round(10 * item.y) / 10;
  257. }
  258. if (item.hasOwnProperty("opacity")) {
  259. item.opacity = Math.min(Math.max(item.opacity, 0.1), 1) || 1;
  260. if (item.opacity === 1) delete item.opacity;
  261. }
  262. if (item.hasOwnProperty("_children")) {
  263. if (!Array.isArray(item._children)) item._children = [];
  264. if (item._children.length > config.MAX_CHILDREN)
  265. item._children.length = config.MAX_CHILDREN;
  266. for (var i = 0; i < item._children.length; i++) {
  267. this.validate(item._children[i]);
  268. }
  269. }
  270. }
  271. /** Load the data in the board from a file.
  272. * @param {string} name - name of the board
  273. */
  274. static async load(name) {
  275. var boardData = new BoardData(name),
  276. data;
  277. try {
  278. data = await fs.promises.readFile(boardData.file);
  279. boardData.board = JSON.parse(data);
  280. for (const id in boardData.board) boardData.validate(boardData.board[id]);
  281. log("disk load", { board: boardData.name });
  282. } catch (e) {
  283. // If the file doesn't exist, this is not an error
  284. if (e.code === "ENOENT") {
  285. log("empty board creation", { board: boardData.name });
  286. } else {
  287. log("board load error", {
  288. board: name,
  289. error: e.toString(),
  290. stack: e.stack,
  291. });
  292. }
  293. boardData.board = {};
  294. if (data) {
  295. // There was an error loading the board, but some data was still read
  296. var backup = backupFileName(boardData.file);
  297. log("Writing the corrupted file to " + backup);
  298. try {
  299. await fs.promises.writeFile(backup, data);
  300. } catch (err) {
  301. log("Error writing " + backup + ": " + err);
  302. }
  303. }
  304. }
  305. return boardData;
  306. }
  307. }
  308. /**
  309. * Given a board file name, return a name to use for temporary data saving.
  310. * @param {string} baseName
  311. */
  312. function backupFileName(baseName) {
  313. var date = new Date().toISOString().replace(/:/g, "");
  314. return baseName + "." + date + ".bak";
  315. }
  316. module.exports.BoardData = BoardData;