Browse Source

Add some documentation and type information

dev_h
lovasoa 4 years ago
parent
commit
a76bbeced3
No account linked to committer's email address
4 changed files with 40 additions and 8 deletions
  1. 8
    8
      server/boardData.js
  2. 5
    0
      server/log.js
  3. 15
    0
      server/server.js
  4. 12
    0
      server/sockets.js

+ 8
- 8
server/boardData.js View File

@@ -32,10 +32,13 @@ var fs = require('./fs_promises.js')
32 32
 
33 33
 /**
34 34
  * Represents a board.
35
+ * @class
35 36
  * @constructor
37
+ * @param {string} name
36 38
  */
37 39
 var BoardData = function (name) {
38 40
 	this.name = name;
41
+	/** @type {{[name: string]: {[object_id:string]: any}}} */
39 42
 	this.board = {};
40 43
 	this.file = path.join(config.HISTORY_DIR, "board-" + encodeURIComponent(name) + ".json");
41 44
 	this.lastSaveDate = Date.now();
@@ -119,13 +122,6 @@ BoardData.prototype.getAll = function (id) {
119 122
 	return results;
120 123
 };
121 124
 
122
-/**
123
- * 
124
- */
125
-BoardData.prototype.addUser = function addUser(userId) {
126
-
127
-}
128
-
129 125
 /**
130 126
  * This callback is displayed as part of the BoardData class.
131 127
  * Describes a function that processes data that comes from the board
@@ -224,7 +220,7 @@ BoardData.prototype.validate = function validate(item, parent) {
224 220
 }
225 221
 
226 222
 /** Load the data in the board from a file.
227
- * @param {string} file - Path to the file where the board data will be read.
223
+ * @param {string} name - name of the board
228 224
 */
229 225
 BoardData.load = async function loadBoard(name) {
230 226
 	var boardData = new BoardData(name), data;
@@ -254,6 +250,10 @@ BoardData.load = async function loadBoard(name) {
254 250
 	return boardData;
255 251
 };
256 252
 
253
+/**
254
+ * Given a board file name, return a name to use for temporary data saving. 
255
+ * @param {string} baseName 
256
+ */
257 257
 function backupFileName(baseName) {
258 258
 	var date = new Date().toISOString().replace(/:/g, '');
259 259
 	return baseName + '.' + date + '.bak';

+ 5
- 0
server/log.js View File

@@ -1,3 +1,8 @@
1
+/**
2
+ * Add a message to the logs
3
+ * @param {string} type 
4
+ * @param {any} infos 
5
+ */
1 6
 function log(type, infos) {
2 7
     var msg = new Date().toISOString() + '\t' + type;
3 8
     if (infos) msg += '\t' + JSON.stringify(infos);

+ 15
- 0
server/server.js View File

@@ -47,6 +47,10 @@ function serveError(request, response) {
47 47
 	}
48 48
 }
49 49
 
50
+/**
51
+ * Write a request to the logs
52
+ * @param {import("http").IncomingMessage} request 
53
+ */
50 54
 function logRequest(request) {
51 55
 	log('connection', {
52 56
 		ip: request.connection.remoteAddress,
@@ -58,6 +62,9 @@ function logRequest(request) {
58 62
 	});
59 63
 }
60 64
 
65
+/**
66
+ * @type {import('http').RequestListener}
67
+ */
61 68
 function handler(request, response) {
62 69
 	try {
63 70
 		handleRequest(request, response);
@@ -71,11 +78,19 @@ function handler(request, response) {
71 78
 const boardTemplate = new templating.BoardTemplate(path.join(config.WEBROOT, 'board.html'));
72 79
 const indexTemplate = new templating.Template(path.join(config.WEBROOT, 'index.html'));
73 80
 
81
+/**
82
+ * Throws an error if the given board name is not allowed
83
+ * @param {string} boardName 
84
+ * @throws {Error}
85
+ */
74 86
 function validateBoardName(boardName) {
75 87
 	if (/^[\w%\-_~()]*$/.test(boardName)) return boardName;
76 88
 	throw new Error("Illegal board name: " + boardName);
77 89
 }
78 90
 
91
+/**
92
+ * @type {import('http').RequestListener}
93
+ */
79 94
 function handleRequest(request, response) {
80 95
 	var parsedUrl = url.parse(request.url, true);
81 96
 	var parts = parsedUrl.pathname.split('/');

+ 12
- 0
server/sockets.js View File

@@ -8,6 +8,14 @@ var iolib = require('socket.io')
8 8
 */
9 9
 var boards = {};
10 10
 
11
+/**
12
+ * Prevents a function from throwing errors.
13
+ * If the inner function throws, the outer function just returns undefined
14
+ * and logs the error.
15
+ * @template A
16
+ * @param {A} fn 
17
+ * @returns {A}
18
+ */
11 19
 function noFail(fn) {
12 20
 	return function noFailWrapped(arg) {
13 21
 		try {
@@ -43,6 +51,10 @@ function getBoard(name) {
43 51
  */
44 52
 function socketConnection(socket) {
45 53
 
54
+	/**
55
+	 * Function to call when an user joins a board
56
+	 * @param {string} name 
57
+	 */
46 58
 	async function joinBoard(name) {
47 59
 		// Default to the public board
48 60
 		if (!name) name = "anonymous";

Loading…
Cancel
Save