Cleanup shared helpers structure
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIServerUtils.ts
diff --git a/src/charging-station/ui-server/UIServerUtils.ts b/src/charging-station/ui-server/UIServerUtils.ts
new file mode 100644 (file)
index 0000000..fc37ddc
--- /dev/null
@@ -0,0 +1,61 @@
+import type { IncomingMessage } from 'http';
+
+import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
+import logger from '../../utils/Logger';
+import Utils from '../../utils/Utils';
+
+export class UIServerUtils {
+  private constructor() {
+    // This is intentional
+  }
+
+  public static handleProtocols = (
+    protocols: Set<string>,
+    // eslint-disable-next-line @typescript-eslint/no-unused-vars
+    request: IncomingMessage
+  ): string | false => {
+    let protocol: Protocol;
+    let version: ProtocolVersion;
+    if (protocols.size === 0) {
+      return false;
+    }
+    for (const fullProtocol of protocols) {
+      if (UIServerUtils.isProtocolAndVersionSupported(fullProtocol) === true) {
+        return fullProtocol;
+      }
+    }
+    logger.error(
+      `${Utils.logPrefix(
+        ' UI WebSocket Server |'
+      )} Unsupported protocol: ${protocol} or protocol version: ${version}`
+    );
+    return false;
+  };
+
+  public static isProtocolAndVersionSupported = (protocolStr: string): boolean => {
+    const [protocol, version] = UIServerUtils.getProtocolAndVersion(protocolStr);
+    return (
+      Object.values(Protocol).includes(protocol) === true &&
+      Object.values(ProtocolVersion).includes(version) === true
+    );
+  };
+
+  public static getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => {
+    const protocolIndex = protocolStr.indexOf(Protocol.UI);
+    const protocol = protocolStr.substring(
+      protocolIndex,
+      protocolIndex + Protocol.UI.length
+    ) as Protocol;
+    const version = protocolStr.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
+    return [protocol, version];
+  };
+
+  public static isLoopback(address: string): boolean {
+    const isLoopbackRegExp = new RegExp(
+      // eslint-disable-next-line no-useless-escape
+      /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/,
+      'i'
+    );
+    return isLoopbackRegExp.test(address);
+  }
+}