| 1 | import chalk from 'chalk'; |
| 2 | |
| 3 | import type { AbstractUIServer } from './AbstractUIServer'; |
| 4 | import { UIHttpServer } from './UIHttpServer'; |
| 5 | import { UIServerUtils } from './UIServerUtils'; |
| 6 | import { UIWebSocketServer } from './UIWebSocketServer'; |
| 7 | import { ApplicationProtocol, ConfigurationSection, type UIServerConfiguration } from '../../types'; |
| 8 | import { Configuration } from '../../utils'; |
| 9 | |
| 10 | export class UIServerFactory { |
| 11 | private constructor() { |
| 12 | // This is intentional |
| 13 | } |
| 14 | |
| 15 | public static getUIServerImplementation( |
| 16 | uiServerConfiguration?: UIServerConfiguration, |
| 17 | ): AbstractUIServer | null { |
| 18 | if (UIServerUtils.isLoopback(uiServerConfiguration!.options!.host!) === false) { |
| 19 | console.warn( |
| 20 | chalk.yellow( |
| 21 | 'Loopback address not detected in UI server configuration. This is not recommended.', |
| 22 | ), |
| 23 | ); |
| 24 | } |
| 25 | switch ( |
| 26 | uiServerConfiguration?.type ?? |
| 27 | Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer) |
| 28 | .type |
| 29 | ) { |
| 30 | case ApplicationProtocol.WS: |
| 31 | return new UIWebSocketServer( |
| 32 | uiServerConfiguration ?? |
| 33 | Configuration.getConfigurationSection<UIServerConfiguration>( |
| 34 | ConfigurationSection.uiServer, |
| 35 | ), |
| 36 | ); |
| 37 | case ApplicationProtocol.HTTP: |
| 38 | return new UIHttpServer( |
| 39 | uiServerConfiguration ?? |
| 40 | Configuration.getConfigurationSection<UIServerConfiguration>( |
| 41 | ConfigurationSection.uiServer, |
| 42 | ), |
| 43 | ); |
| 44 | default: |
| 45 | return null; |
| 46 | } |
| 47 | } |
| 48 | } |