X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2Fui-server%2FUIServerUtils.ts;h=a4f3bf51dafd7d6aa3612412617a58a4cd5dfa93;hb=ba9a56a613727d96757690a8b52af6731f3fd8a8;hp=95bbba56c8134dd93f20ebbf2dcf06986e70961d;hpb=90aceaf6e0cd749bc5feb4e50c89f2a213c53108;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIServerUtils.ts b/src/charging-station/ui-server/UIServerUtils.ts index 95bbba56..a4f3bf51 100644 --- a/src/charging-station/ui-server/UIServerUtils.ts +++ b/src/charging-station/ui-server/UIServerUtils.ts @@ -1,60 +1,55 @@ -import type { IncomingMessage } from 'node:http'; +import type { IncomingMessage } from 'node:http' -import { Protocol, ProtocolVersion } from '../../types'; -import { logPrefix, logger } from '../../utils'; +import { Protocol, ProtocolVersion } from '../../types/index.js' +import { logPrefix, logger } from '../../utils/index.js' +// eslint-disable-next-line @typescript-eslint/no-extraneous-class export class UIServerUtils { - private constructor() { + private constructor () { // This is intentional } public static handleProtocols = ( protocols: Set, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - request: IncomingMessage, + request: IncomingMessage ): string | false => { - let protocol: Protocol | undefined; - let version: ProtocolVersion | undefined; + let protocol: Protocol | undefined + let version: ProtocolVersion | undefined if (protocols.size === 0) { - return false; + return false } for (const fullProtocol of protocols) { - if (UIServerUtils.isProtocolAndVersionSupported(fullProtocol) === true) { - return fullProtocol; + if (UIServerUtils.isProtocolAndVersionSupported(fullProtocol)) { + return fullProtocol } } logger.error( `${logPrefix( - ' UI WebSocket Server |', - )} Unsupported protocol: '${protocol}' or protocol version: '${version}'`, - ); - return false; - }; + ' UI WebSocket Server |' + )} Unsupported protocol: '${protocol}' or protocol version: '${version}'` + ) + return false + } public static isProtocolAndVersionSupported = (protocolStr: string): boolean => { - const [protocol, version] = UIServerUtils.getProtocolAndVersion(protocolStr); + const [protocol, version] = UIServerUtils.getProtocolAndVersion(protocolStr) return ( - Object.values(Protocol).includes(protocol) === true && - Object.values(ProtocolVersion).includes(version) === true - ); - }; + Object.values(Protocol).includes(protocol) && Object.values(ProtocolVersion).includes(version) + ) + } public static getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => { - const protocolIndex = protocolStr.indexOf(Protocol.UI); + 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]; - }; + 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); + public static isLoopback (address: string): boolean { + // eslint-disable-next-line no-useless-escape + return /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/i.test(address) } }