web.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * `dsh web` — thin bin over the config-tree boot: run AppCLIEntry with the
  3. * already-parsed host/port/dev, print the URL line, wire signals. All
  4. * composition lives in cordis.yml; all boot glue lives in AppCLIEntry. Host and
  5. * port are unvalidated pass-through overrides — the `dsh-host-webserver` schema
  6. * gates them at boot.
  7. */
  8. import { fileURLToPath } from 'node:url'
  9. import { AppCLIEntry } from './app-cli-entry.ts'
  10. import { registerLiveSessions } from './register-session.ts'
  11. const CONFIG_PATH = fileURLToPath(new URL('../cordis.yml', import.meta.url))
  12. // Display-only mirror of the webserver schema's loopback host: the address the
  13. // local URL always prints. Not a source of truth — the schema is.
  14. const LOOPBACK_HOST = '127.0.0.1'
  15. /**
  16. * Serve the browser UI from the shipped config tree. `host`/`port` are passed
  17. * through only when the flag was given; absent, the `cordis.yml` value stands.
  18. * @param host - the bind host, or `undefined` to keep the config default.
  19. * @param port - the listen port (`0` requests an OS-assigned port), or `undefined` to keep the config default.
  20. * @param dev - mount the client HMR driver and watch plugin bundles for rebuilds.
  21. * @param workspaceRoot - parent directory for name-created workspaces, or `undefined` for the gateway's cwd fallback.
  22. * @param trustedHosts - extra authorities for the /api browser-trust fence, or `undefined` for the derived LAN literals alone.
  23. */
  24. export async function runWeb(
  25. host: string | undefined,
  26. port: number | undefined,
  27. dev: boolean,
  28. workspaceRoot: string | undefined,
  29. trustedHosts: string[] | undefined,
  30. ): Promise<void> {
  31. const entry = new AppCLIEntry({
  32. configPath: CONFIG_PATH,
  33. dev,
  34. ...host !== undefined && { host },
  35. ...port !== undefined && { port },
  36. ...workspaceRoot !== undefined && { workspaceRoot },
  37. ...trustedHosts !== undefined && { trustedHosts },
  38. })
  39. const { ctx, port: boundPort } = await entry.run()
  40. await registerLiveSessions(ctx)
  41. let exiting = false
  42. const shutdown = (code: number): void => {
  43. if (exiting) return
  44. exiting = true
  45. void Promise.resolve(ctx.fiber.dispose()).finally(() => { process.exit(code) })
  46. }
  47. // The entry's boot-time snapshot, not a fresh sample: the printed LAN URL
  48. // must name an address the /api trust fence was configured with.
  49. const lanCandidate = entry.lanAddresses[0]
  50. const localUrl = `http://${LOOPBACK_HOST}:${boundPort}`
  51. console.log(`dsh web: ${localUrl}${lanCandidate === undefined ? '' : ` (LAN: http://${lanCandidate}:${boundPort})`}`)
  52. process.on('SIGTERM', () => { shutdown(0) })
  53. process.on('SIGINT', () => { shutdown(130) })
  54. }