1 import type { IncomingMessage
} from
'http';
3 import { Protocol
, ProtocolVersion
} from
'../../types/UIProtocol';
4 import logger from
'../../utils/Logger';
5 import Utils from
'../../utils/Utils';
7 export class UIServerUtils
{
8 private constructor() {
12 public static handleProtocols
= (
13 protocols
: Set
<string>,
14 // eslint-disable-next-line @typescript-eslint/no-unused-vars
15 request
: IncomingMessage
16 ): string | false => {
17 let protocol
: Protocol
;
18 let version
: ProtocolVersion
;
19 if (protocols
.size
=== 0) {
22 for (const fullProtocol
of protocols
) {
23 if (UIServerUtils
.isProtocolAndVersionSupported(fullProtocol
) === true) {
29 ' UI WebSocket Server |'
30 )} Unsupported protocol: ${protocol} or protocol version: ${version}`
35 public static isProtocolAndVersionSupported
= (protocolStr
: string): boolean => {
36 const [protocol
, version
] = UIServerUtils
.getProtocolAndVersion(protocolStr
);
38 Object.values(Protocol
).includes(protocol
) === true &&
39 Object.values(ProtocolVersion
).includes(version
) === true
43 public static getProtocolAndVersion
= (protocolStr
: string): [Protocol
, ProtocolVersion
] => {
44 const protocolIndex
= protocolStr
.indexOf(Protocol
.UI
);
45 const protocol
= protocolStr
.substring(
47 protocolIndex
+ Protocol
.UI
.length
49 const version
= protocolStr
.substring(protocolIndex
+ Protocol
.UI
.length
) as ProtocolVersion
;
50 return [protocol
, version
];
53 public static isLoopback(address
: string): boolean {
54 const isLoopbackRegExp
= new RegExp(
55 // eslint-disable-next-line no-useless-escape
56 /^localhost$
|^127(?:\
.\d
+){0,2}\
.\d
+$
|^(?:0*\
:)*?:?0*1$
/,
59 return isLoopbackRegExp
.test(address
);