Parcourir la source

- Add basic comments

Parad0x il y a 7 ans
Parent
commit
992cd9beb9
3 fichiers modifiés avec 26 ajouts et 4 suppressions
  1. 7 0
      src/connectionHandler.js
  2. 9 2
      src/utils/countAndSlice.js
  3. 10 2
      src/utils/socketPath.js

+ 7 - 0
src/connectionHandler.js

@@ -4,8 +4,10 @@ import type { Socket } from "net";
 
 const countAndSlice = require("./utils/countAndSlice");
 
+// All connections stored by id
 const Connections = {};
 
+// List of operations should be executed
 const secureOp = ["ping", "close"];
 
 class Connection {
@@ -50,12 +52,15 @@ class Connection {
 
   execute(op: string) {
     if (secureOp.includes(op)) {
+      // $FlowFixMe
       this[op]();
     } else {
       this.write("Unkown operator\n\r");
     }
   }
 
+  // Session`s Operations
+
   ping() {
     this.write("pong\n\r");
   }
@@ -65,6 +70,8 @@ class Connection {
     this.socket.end();
   }
 
+  // Connection methods
+
   write(input: string | Buffer) {
     this.socket.write(input);
   }

+ 9 - 2
src/utils/countAndSlice.js

@@ -1,11 +1,18 @@
 //@flow
 
-function countAndSlice(input: string, delimeter: string = ";"): [string[], number] {
+/**
+ * Split string to array by separator and count occurance of it`s
+ *
+ * @param {string} input Input string
+ * @param {string} [separator=";"] Separator to split and count it`s occurance 
+ * @returns {[string[], number]}
+ */
+function countAndSlice(input: string, separator: string = ";"): [string[], number] {
   let buff: string = "";
   let arr: string[] = [];
   let counter: number = 0;
   for (let char of input) {
-    if (char == delimeter) {
+    if (char == separator) {
       counter++;
       arr.push(buff);
       buff = "";

+ 10 - 2
src/utils/socketPath.js

@@ -2,7 +2,13 @@
 
 let path = require("path");
 
-module.exports = (instanceName?: string): string => {
+/**
+ * Build path of socket
+ *
+ * @param {string} [instanceName] Instance`s name
+ * @returns {string}
+ */
+function socketPath(instanceName?: string): string {
   let socketPathArgs = ["/tmp"];
   if (process.platform == "win32") {
     socketPathArgs = ['\\\\?\\pipe'];
@@ -15,4 +21,6 @@ module.exports = (instanceName?: string): string => {
 
   const socketPath = path.join.apply(path, socketPathArgs);
   return socketPath;
-}
+}
+
+module.exports = socketPath;