| 1 | import chalk from 'chalk'; |
| 2 | |
| 3 | import { ApplicationProtocol, type UIServerConfiguration } from '../../types'; |
| 4 | import { Configuration } from '../../utils'; |
| 5 | import { type AbstractUIServer, UIHttpServer, UIServerUtils, UIWebSocketServer } from '../internal'; |
| 6 | |
| 7 | export class UIServerFactory { |
| 8 | private constructor() { |
| 9 | // This is intentional |
| 10 | } |
| 11 | |
| 12 | public static getUIServerImplementation( |
| 13 | uiServerConfiguration?: UIServerConfiguration |
| 14 | ): AbstractUIServer | null { |
| 15 | if (UIServerUtils.isLoopback(uiServerConfiguration.options?.host) === false) { |
| 16 | console.warn( |
| 17 | chalk.yellow( |
| 18 | 'Loopback address not detected in UI server configuration. This is not recommended.' |
| 19 | ) |
| 20 | ); |
| 21 | } |
| 22 | switch (uiServerConfiguration?.type ?? Configuration.getUIServer().type) { |
| 23 | case ApplicationProtocol.WS: |
| 24 | return new UIWebSocketServer(uiServerConfiguration ?? Configuration.getUIServer()); |
| 25 | case ApplicationProtocol.HTTP: |
| 26 | return new UIHttpServer(uiServerConfiguration ?? Configuration.getUIServer()); |
| 27 | default: |
| 28 | return null; |
| 29 | } |
| 30 | } |
| 31 | } |