launch.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. /** Select source or built bootstrap assets in the mounted execution world. */
  2. import { dirname, resolve } from 'node:path'
  3. import { fileURLToPath, pathToFileURL } from 'node:url'
  4. import type { FileSystem } from '@deepseek-ai/dsh-fs'
  5. /** Deployment-owned Node executable and optional preinstalled built bootstrap. */
  6. export interface LaunchConfig {
  7. /** Executable in the subprocess world; defaults to the current Node executable. */
  8. nodeExecutable?: string
  9. /** Absolute preinstalled built bootstrap in the execution world. */
  10. bootstrapPath?: string
  11. }
  12. /**
  13. * Select explicit arguments without inheriting host loader or inspector flags.
  14. * @param fs - Filesystem mapping host bootstrap assets into the process world.
  15. * @param config - Optional preinstalled built bootstrap.
  16. * @param maxMessageBytes - Validated frame and queued-write limit.
  17. * @returns Arguments following the resolved Node executable.
  18. */
  19. export function bootstrapArgs(fs: FileSystem, config: LaunchConfig, maxMessageBytes: number): string[] {
  20. if (config.bootstrapPath !== undefined) return [config.bootstrapPath, String(maxMessageBytes)]
  21. if ('pkg' in process) return [String(maxMessageBytes)]
  22. const mapped = (path: string): string => {
  23. const result = fs.processPathFromHostPath(path)
  24. if (result === undefined) throw new Error(`PTC runtime bootstrap is unavailable in the subprocess execution world: ${path}`)
  25. return result
  26. }
  27. /* v8 ignore next 3 -- built-lib.e2e.ts executes the bundled provider and sibling process.js under plain Node. */
  28. if (!new URL(import.meta.url).pathname.endsWith('.ts')) {
  29. return [mapped(fileURLToPath(new URL('./process.js', import.meta.url))), String(maxMessageBytes)]
  30. }
  31. const entry = mapped(fileURLToPath(new URL('./process.ts', import.meta.url)))
  32. const subprocess = dirname(fileURLToPath(import.meta.resolve('@deepseek-ai/dsh-subprocess/package.json')))
  33. const helper = mapped(resolve(subprocess, 'src/control.ts'))
  34. const source = `const {openInheritedControlChannel}=await import(${JSON.stringify(pathToFileURL(helper).href)});const {runNodeMain}=await import(${JSON.stringify(pathToFileURL(entry).href)});await runNodeMain(openInheritedControlChannel(),${maxMessageBytes},process);`
  35. return ['--input-type=module', '--eval', source]
  36. }