File size: 2,439 Bytes
488d96b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/** Virtual executable registry used by the Worker process launcher. */
import { basename } from '../../module-system/posix-path.ts'
import type { ShellFileSystem } from '../types.ts'
import { LANDLOCK_EXECUTABLE } from './landlock.ts'

/** Completed virtual executable invocation. */
export interface VirtualExecutableExit {
  readonly kind: 'exit'
  readonly exitCode: number
  readonly stdout: string
  readonly stderr: string
}

/** Invocation delegated to the normal Worker command runner after preparation. */
export interface VirtualExecutableDelegate {
  readonly kind: 'delegate'
  readonly argv: readonly string[]
  readonly filesystem: ShellFileSystem
  readonly missingExecutable: VirtualExecutableExit
}

/** Result of preparing an asynchronous virtual executable invocation. */
export type VirtualExecutablePreparation = VirtualExecutableExit | VirtualExecutableDelegate

/** Result available to the synchronous child-process face. */
export type VirtualExecutableSyncResult = VirtualExecutableExit | { readonly kind: 'asynchronous' }

/** One executable implemented by the Worker instead of an operating-system binary. */
export interface VirtualExecutable {
  /** Platform executable name, independent of package-manager installation path. */
  readonly name: string
  /**
   * Prepare an invocation or complete it without entering the command runner.
   * @param args - Arguments after the executable path.
   * @param context - Working directory and ambient Worker filesystem.
   * @returns The completed result or delegated command and filesystem.
   */
  prepare(
    args: readonly string[],
    context: { readonly cwd: string; readonly filesystem: ShellFileSystem },
  ): Promise<VirtualExecutablePreparation>
  /**
   * Handle the subset that can complete synchronously.
   * @param args - Arguments after the executable path.
   * @returns A completed result or the asynchronous marker.
   */
  runSync(args: readonly string[]): VirtualExecutableSyncResult
}

const EXECUTABLES: ReadonlyMap<string, VirtualExecutable> = new Map([
  [LANDLOCK_EXECUTABLE.name, LANDLOCK_EXECUTABLE],
])

/**
 * Resolve a Worker platform executable by logical name.
 * @param path - Bare name or executable path passed to `spawn`.
 * @returns Its implementation, or undefined for the normal command table.
 */
export function virtualExecutable(path: string): VirtualExecutable | undefined {
  return EXECUTABLES.get(basename(path))
}