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