refactor: cleanup eslint configuration
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIServerFactory.ts
CommitLineData
66a7748d 1import chalk from 'chalk'
8114d10e 2
53a9f111 3import { BaseError } from '../../exception/index.js'
a6080904
JB
4import {
5 ApplicationProtocol,
6 ApplicationProtocolVersion,
329eab0e 7 AuthenticationType,
66a7748d
JB
8 type UIServerConfiguration
9} from '../../types/index.js'
4c3f6c20
JB
10import type { AbstractUIServer } from './AbstractUIServer.js'
11import { UIHttpServer } from './UIHttpServer.js'
12import { isLoopback } from './UIServerUtils.js'
13import { UIWebSocketServer } from './UIWebSocketServer.js'
fe94fce0 14
66a7748d 15// eslint-disable-next-line @typescript-eslint/no-extraneous-class
268a74bb 16export class UIServerFactory {
66a7748d 17 private constructor () {
fe94fce0
JB
18 // This is intentional
19 }
20
66a7748d
JB
21 public static getUIServerImplementation (
22 uiServerConfiguration: UIServerConfiguration
a1cfaa16 23 ): AbstractUIServer {
329eab0e
JB
24 if (
25 uiServerConfiguration.authentication?.enabled === true &&
26 !Object.values(AuthenticationType).includes(uiServerConfiguration.authentication.type)
27 ) {
28 throw new BaseError(
29 `Unknown authentication type '${uiServerConfiguration.authentication.type}' for UI server`
30 )
31 }
32 if (
33 uiServerConfiguration.type === ApplicationProtocol.HTTP &&
34 uiServerConfiguration.authentication?.enabled === true &&
35 uiServerConfiguration.authentication.type === AuthenticationType.PROTOCOL_BASIC_AUTH
36 ) {
37 throw new BaseError('Protocol basic authentication is not supported for HTTP UI server')
38 }
b35a06e0
JB
39 if (
40 uiServerConfiguration.authentication?.enabled !== true &&
41 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
75adc3d8 42 !isLoopback(uiServerConfiguration.options!.host!)
b35a06e0 43 ) {
d5bd1c00 44 console.warn(
9c5d9fa4 45 chalk.yellow(
b35a06e0 46 'Non loopback address in UI server configuration without authentication enabled. This is not recommended'
66a7748d
JB
47 )
48 )
d5bd1c00 49 }
7aba23e1
JB
50 if (
51 uiServerConfiguration.type === ApplicationProtocol.WS &&
52 uiServerConfiguration.version !== ApplicationProtocolVersion.VERSION_11
53 ) {
54 console.warn(
55 chalk.yellow(
412cece8 56 `Only version ${ApplicationProtocolVersion.VERSION_11} is supported for WebSocket UI server. Falling back to version ${ApplicationProtocolVersion.VERSION_11}`
66a7748d
JB
57 )
58 )
59 uiServerConfiguration.version = ApplicationProtocolVersion.VERSION_11
7aba23e1 60 }
864e5f8d 61 switch (uiServerConfiguration.type) {
1f7fa4de 62 case ApplicationProtocol.HTTP:
66a7748d 63 return new UIHttpServer(uiServerConfiguration)
a1cfaa16
JB
64 case ApplicationProtocol.WS:
65 default:
66 return new UIWebSocketServer(uiServerConfiguration)
fe94fce0
JB
67 }
68 }
69}