open.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { spawn } from 'node:child_process'
  2. import { join } from 'node:path'
  3. const handoffProbe = `
  4. const { writeFileSync } = require('node:fs')
  5. const marker = process.argv[1]
  6. const helperPid = Number(process.argv[2])
  7. setTimeout(() => {
  8. let helperAlive = true
  9. if (process.platform === 'win32') {
  10. try {
  11. process.kill(helperPid, 0)
  12. } catch {
  13. helperAlive = false
  14. }
  15. }
  16. if (helperAlive) writeFileSync(marker, '')
  17. }, 50)
  18. `
  19. export default async function open(url) {
  20. if (process.env.BROWSER_OPEN_TEST_FAILURE !== undefined) {
  21. throw new Error(process.env.BROWSER_OPEN_TEST_FAILURE)
  22. }
  23. const response = await fetch(url)
  24. const html = await response.text()
  25. console.log(`dsh browser-open: ${JSON.stringify({
  26. url,
  27. status: response.status,
  28. bootManifest: html.includes('__DSH_BOOT__'),
  29. apiKeyPresent: process.env.DEEPSEEK_API_KEY !== undefined,
  30. dshHomePresent: process.env.DSH_HOME !== undefined,
  31. })}`)
  32. // The Windows launcher writes the server-exit marker only while its helper
  33. // remains alive, so the assembled test detects an early helper exit.
  34. const launcher = spawn(process.execPath, [
  35. '--eval', handoffProbe,
  36. '--', join(process.cwd(), `.dsh-browser-open-${process.ppid}`), String(process.pid),
  37. ], { stdio: 'ignore' })
  38. launcher.unref()
  39. return launcher
  40. }