| 1 | import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; |
| 2 | |
| 3 | import { IncomingMessage } from 'http'; |
| 4 | import Utils from '../../utils/Utils'; |
| 5 | import logger from '../../utils/Logger'; |
| 6 | |
| 7 | export class UIServiceUtils { |
| 8 | public static handleProtocols = ( |
| 9 | protocols: Set<string>, |
| 10 | request: IncomingMessage |
| 11 | ): string | false => { |
| 12 | let protocolIndex: number; |
| 13 | let protocol: Protocol; |
| 14 | let version: ProtocolVersion; |
| 15 | for (const fullProtocol of protocols) { |
| 16 | protocolIndex = fullProtocol.indexOf(Protocol.UI); |
| 17 | protocol = fullProtocol.substring( |
| 18 | protocolIndex, |
| 19 | protocolIndex + Protocol.UI.length |
| 20 | ) as Protocol; |
| 21 | version = fullProtocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion; |
| 22 | if ( |
| 23 | Object.values(Protocol).includes(protocol) && |
| 24 | Object.values(ProtocolVersion).includes(version) |
| 25 | ) { |
| 26 | return fullProtocol; |
| 27 | } |
| 28 | } |
| 29 | logger.error( |
| 30 | `${Utils.logPrefix( |
| 31 | ' UI WebSocket Server:' |
| 32 | )} Unsupported protocol: ${protocol} or protocol version: ${version}` |
| 33 | ); |
| 34 | return false; |
| 35 | }; |
| 36 | } |