Commit | Line | Data |
---|---|---|
66a7748d | 1 | import chalk from 'chalk' |
8114d10e | 2 | |
53a9f111 | 3 | import { BaseError } from '../../exception/index.js' |
a6080904 JB |
4 | import { |
5 | ApplicationProtocol, | |
6 | ApplicationProtocolVersion, | |
329eab0e | 7 | AuthenticationType, |
ae61fa2f | 8 | ConfigurationSection, |
66a7748d JB |
9 | type UIServerConfiguration |
10 | } from '../../types/index.js' | |
ae61fa2f | 11 | import { logger, logPrefix } from '../../utils/index.js' |
4c3f6c20 JB |
12 | import type { AbstractUIServer } from './AbstractUIServer.js' |
13 | import { UIHttpServer } from './UIHttpServer.js' | |
14 | import { isLoopback } from './UIServerUtils.js' | |
15 | import { UIWebSocketServer } from './UIWebSocketServer.js' | |
fe94fce0 | 16 | |
66a7748d | 17 | // eslint-disable-next-line @typescript-eslint/no-extraneous-class |
268a74bb | 18 | export class UIServerFactory { |
66a7748d | 19 | private constructor () { |
fe94fce0 JB |
20 | // This is intentional |
21 | } | |
22 | ||
66a7748d JB |
23 | public static getUIServerImplementation ( |
24 | uiServerConfiguration: UIServerConfiguration | |
a1cfaa16 | 25 | ): AbstractUIServer { |
329eab0e JB |
26 | if ( |
27 | uiServerConfiguration.authentication?.enabled === true && | |
28 | !Object.values(AuthenticationType).includes(uiServerConfiguration.authentication.type) | |
29 | ) { | |
30 | throw new BaseError( | |
ae61fa2f | 31 | `Unknown authentication type '${uiServerConfiguration.authentication.type}' in '${ConfigurationSection.uiServer}' configuration section` |
329eab0e JB |
32 | ) |
33 | } | |
34 | if ( | |
35 | uiServerConfiguration.type === ApplicationProtocol.HTTP && | |
36 | uiServerConfiguration.authentication?.enabled === true && | |
37 | uiServerConfiguration.authentication.type === AuthenticationType.PROTOCOL_BASIC_AUTH | |
38 | ) { | |
ae61fa2f JB |
39 | throw new BaseError( |
40 | `'${uiServerConfiguration.authentication.type}' authentication type with application protocol type '${uiServerConfiguration.type}' is not supported in '${ConfigurationSection.uiServer}' configuration section` | |
41 | ) | |
329eab0e | 42 | } |
b35a06e0 JB |
43 | if ( |
44 | uiServerConfiguration.authentication?.enabled !== true && | |
45 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
75adc3d8 | 46 | !isLoopback(uiServerConfiguration.options!.host!) |
b35a06e0 | 47 | ) { |
ae61fa2f JB |
48 | const logMsg = `Non loopback address in '${ConfigurationSection.uiServer}' configuration section without authentication enabled. This is not recommended` |
49 | logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) | |
50 | console.warn(chalk.yellow(logMsg)) | |
d5bd1c00 | 51 | } |
7aba23e1 JB |
52 | if ( |
53 | uiServerConfiguration.type === ApplicationProtocol.WS && | |
54 | uiServerConfiguration.version !== ApplicationProtocolVersion.VERSION_11 | |
55 | ) { | |
81c74884 | 56 | const logMsg = `Only version ${ApplicationProtocolVersion.VERSION_11} with application protocol type '${uiServerConfiguration.type}' is supported in '${ConfigurationSection.uiServer}' configuration section. Falling back to version ${ApplicationProtocolVersion.VERSION_11}` |
ae61fa2f JB |
57 | logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) |
58 | console.warn(chalk.yellow(logMsg)) | |
66a7748d | 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: | |
ae61fa2f JB |
66 | if ( |
67 | !Object.values(ApplicationProtocol).includes( | |
68 | uiServerConfiguration.type as ApplicationProtocol | |
69 | ) | |
70 | ) { | |
48847bc0 JB |
71 | const logMsg = `Unknown application protocol type '${uiServerConfiguration.type}' in '${ |
72 | ConfigurationSection.uiServer | |
73 | // eslint-disable-next-line @typescript-eslint/no-base-to-string | |
74 | }' configuration section from values '${ApplicationProtocol.toString()}', defaulting to '${ | |
ae61fa2f | 75 | ApplicationProtocol.WS |
81c74884 | 76 | }'` |
ae61fa2f JB |
77 | logger.warn(`${UIServerFactory.logPrefix()} ${logMsg}`) |
78 | console.warn(logMsg) | |
79 | } | |
a1cfaa16 | 80 | return new UIWebSocketServer(uiServerConfiguration) |
fe94fce0 JB |
81 | } |
82 | } | |
ae61fa2f JB |
83 | |
84 | private static readonly logPrefix = (modName?: string, methodName?: string): string => { | |
85 | const logMsgPrefix = 'UI Server' | |
86 | const logMsg = | |
87 | modName != null && methodName != null | |
88 | ? ` ${logMsgPrefix} | ${modName}.${methodName}:` | |
89 | : ` ${logMsgPrefix} |` | |
90 | return logPrefix(logMsg) | |
91 | } | |
fe94fce0 | 92 | } |