( runtime: SessionRuntimeHints | undefined, )
| 9 | }; |
| 10 | |
| 11 | export function resolveRuntimeTransportHints( |
| 12 | runtime: SessionRuntimeHints | undefined, |
| 13 | ): ResolvedRuntimeTransport | undefined { |
| 14 | if (!runtime) return undefined; |
| 15 | |
| 16 | let host = trimRuntimeValue(runtime.metroHost); |
| 17 | let port = normalizePort(runtime.metroPort); |
| 18 | let scheme: 'http' | 'https' = 'http'; |
| 19 | const bundleUrl = trimRuntimeValue(runtime.bundleUrl); |
| 20 | if (bundleUrl) { |
| 21 | let parsed: URL; |
| 22 | try { |
| 23 | parsed = new URL(bundleUrl); |
| 24 | } catch (error) { |
| 25 | throw new AppError( |
| 26 | 'INVALID_ARGS', |
| 27 | `Invalid runtime bundle URL: ${bundleUrl}`, |
| 28 | {}, |
| 29 | error as Error, |
| 30 | ); |
| 31 | } |
| 32 | if (parsed.protocol === 'http:' || parsed.protocol === 'https:') { |
| 33 | host ??= trimRuntimeValue(parsed.hostname); |
| 34 | port ??= normalizePort( |
| 35 | parsed.port.length > 0 ? Number(parsed.port) : defaultPortForProtocol(parsed.protocol), |
| 36 | ); |
| 37 | scheme = parsed.protocol === 'https:' ? 'https' : 'http'; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if (!host || !port) return undefined; |
| 42 | return { host, port, scheme }; |
| 43 | } |
| 44 | |
| 45 | export function trimRuntimeValue(value: string | undefined): string | undefined { |
| 46 | const trimmed = value?.trim(); |
no test coverage detected