1 import type { IncomingMessage
} from
'node:http'
3 import { Protocol
, ProtocolVersion
} from
'../../types/index.js'
4 import { logPrefix
, logger
} from
'../../utils/index.js'
6 // eslint-disable-next-line @typescript-eslint/no-extraneous-class
7 export class UIServerUtils
{
8 private constructor () {
12 public static handleProtocols
= (
13 protocols
: Set
<string>,
14 request
: IncomingMessage
15 ): string | false => {
16 let protocol
: Protocol
| undefined
17 let version
: ProtocolVersion
| undefined
18 if (protocols
.size
=== 0) {
21 for (const fullProtocol
of protocols
) {
22 if (UIServerUtils
.isProtocolAndVersionSupported(fullProtocol
)) {
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
) && Object.values(ProtocolVersion
).includes(version
)
41 public static getProtocolAndVersion
= (protocolStr
: string): [Protocol
, ProtocolVersion
] => {
42 const protocolIndex
= protocolStr
.indexOf(Protocol
.UI
)
43 const protocol
= protocolStr
.substring(
45 protocolIndex
+ Protocol
.UI
.length
47 const version
= protocolStr
.substring(protocolIndex
+ Protocol
.UI
.length
) as ProtocolVersion
48 return [protocol
, version
]
51 public static isLoopback (address
: string): boolean {
52 // eslint-disable-next-line no-useless-escape
53 return /^localhost$
|^127(?:\
.\d
+){0,2}\
.\d
+$
|^(?:0*\
:)*?:?0*1$
/i
.test(address
)