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