X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIServerUtils.ts;h=4d621172b94e7875f6c6bfd683ebd3050f68ec42;hb=1d6f2eb4f4c766283604e417bd34e16f18c8e997;hp=79f8c34a74d5eec236dc8ab84aa672617b2f0daf;hpb=e1d9a0f4d6ff1a90048e9a694fd12b7031cc6961;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 79f8c34a..4d621172 100644 --- a/src/charging-station/ui-server/UIServerUtils.ts +++ b/src/charging-station/ui-server/UIServerUtils.ts @@ -1,60 +1,57 @@ -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 = ( + public static readonly 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); + public static readonly isProtocolAndVersionSupported = (protocolStr: string): boolean => { + 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); + public static readonly 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]; - }; + 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) } }