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=fc37ddc6cd89e82be004f7ff6deecf0d13425f3f;hpb=ed3d28080b6597ba2f728d625e34ce05aea49d06;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 fc37ddc6..a4f3bf51 100644 --- a/src/charging-station/ui-server/UIServerUtils.ts +++ b/src/charging-station/ui-server/UIServerUtils.ts @@ -1,61 +1,55 @@ -import type { IncomingMessage } from 'http'; +import type { IncomingMessage } from 'node:http' -import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; -import logger from '../../utils/Logger'; -import Utils from '../../utils/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 ): string | false => { - let protocol: Protocol; - let version: ProtocolVersion; + 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( - `${Utils.logPrefix( + `${logPrefix( ' UI WebSocket Server |' - )} Unsupported protocol: ${protocol} or protocol version: ${version}` - ); - return false; - }; + )} 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]; - }; + ) 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) } }