Skip to main content
jg-packages

Search

    Child processes

    jg-process

    Run interactive foreground commands or capture output without a shell, with timeouts, cancellation and process-tree cleanup for captured runs.

    v1.8.0Node.jsESMMIT

    npm

    npm install jg-process

    pnpm

    pnpm add jg-process

    Usage

    Run a command to completion and read its output. The run is bounded by a timeout and an output limit, and both stop the whole process tree:

    ts
    import { runProcess } from 'jg-process';
    
    const { exitCode, stdout, stderr, durationMs } = await runProcess({
      command: 'git',
      args: ['status', '--porcelain'],
      cwd: repositoryDirectory,
      timeoutSeconds: 30,
      signal: AbortSignal.timeout(60_000),
    });

    A non-zero exit code resolves, so callers decide what counts as failure. A timeout, too much output, or a failure to start rejects with PROCESS_TIMED_OUT, PROCESS_OUTPUT_TOO_LARGE, or PROCESS_START_FAILED. A timeout that isn’t a positive number of at most 2,147,483 seconds, or an output limit that isn’t a positive safe integer, such as a computed budget that ran out, rejects with INVALID_PROCESS_OPTIONS before anything starts. With encoding: 'buffer', stdout and stderr are the bytes the process wrote, for output that may not be valid UTF-8, such as file names.

    Run an interactive command in the current terminal:

    ts
    import { runForegroundProcess } from 'jg-process';
    
    const { exitCode, signalCode } = await runForegroundProcess('npm', ['--version']);

    The child inherits stdin, stdout and stderr. The result includes a nonzero exit code or the signal that stopped the child. A launch failure rejects with PROCESS_START_FAILED. Terminal interrupts follow the current process group’s signal policy.

    Start a long-running process that you own, and stop it together with everything it started:

    ts
    import { createShutdownSignal, startProcess, stopProcessTree } from 'jg-process';
    
    const server = startProcess({ command: 'node', args: ['server.js'], cwd: 'apps/api' });
    const shutdown = createShutdownSignal(); // Ctrl+C or SIGTERM, which sets the exit code to 130 or 143
    shutdown.signal.addEventListener('abort', () => void stopProcessTree(server), { once: true });

    createShutdownSignal turns SIGINT and SIGTERM, or the signals you pass, into an AbortSignal that aborts with PROCESS_INTERRUPTED at the first one, so one signal stops a script’s or a service’s work. It sets process.exitCode to 128 plus the signal’s number, as a shell reports a process the signal stopped, unless setExitCode is false, and calls onSignal once, ignoring what it throws. It never calls process.exit, so the process exits once its work stops. A second signal is ignored, or, with forceOnRepeat, stops the process at once, for a shutdown that hangs. With disconnect: true, a child started with an IPC channel, such as by fork, also shuts down as with SIGTERM when its parent closes the channel or dies. dispose stops listening. On Windows, Ctrl+C sends SIGINT, but SIGTERM is never delivered.

    Start a server and wait until it answers, or stop everything it started when it doesn’t:

    ts
    import { startProcessAndWait } from 'jg-process';
    
    const api = await startProcessAndWait(
      { command: process.execPath, args: ['server.js'], cwd: 'apps/api' },
      { readyWhen: { url: 'http://127.0.0.1:3000/health' }, timeoutSeconds: 60 },
    );

    Run a package manager without a shell on every platform, including Windows, where its .cmd shim can’t be spawned directly:

    ts
    import { resolvePackageManagerCommand, runProcess } from 'jg-process';
    
    await runProcess({ ...resolvePackageManagerCommand(['run', 'build']), timeoutSeconds: 600 });

    Reserve a free port before handing it to a server, keeping the preferred one when it’s free:

    ts
    import { reservePort } from 'jg-process';
    
    const reservation = await reservePort({ preferredPort: 3000 }); // 3000, or another free port
    await reservation.release(); // right before the server binds it

    Write a CLI script that parses its options strictly, reads piped input, and runs only when executed:

    ts
    import { isMainModule, parseStrictArgs, readStdin } from 'jg-process';
    
    if (isMainModule(import.meta.url)) {
      const { check, path } = parseStrictArgs(process.argv.slice(2), {
        check: { type: 'boolean' },
        path: { type: 'string', required: true },
      });
      const input = await readStdin({ maxBytes: 1_048_576 }); // STDIN_TOO_LARGE past 1 MiB
    }

    Read a request body or a socket’s message within a limit, and still answer one that’s too large:

    ts
    import { createServer } from 'node:http';
    
    import { hasErrorCode } from 'jg-exception';
    import { ERRORS, readStream } from 'jg-process';
    
    createServer((request, response) => {
      void readStream(request, { maxBytes: 65_536 }).then(
        (body) => response.end(body),
        (error: unknown) => {
          response.statusCode = hasErrorCode(error, ERRORS.STREAM_TOO_LARGE) ? 413 : 400;
          response.setHeader('connection', 'close');
          response.end();
        },
      );
    });

    parseStrictArgs refuses unknown options, positional arguments, an option given twice that isn’t multiple, an empty value, and a missing required option, with INVALID_ARGUMENTS and data.option naming it. isMainModule compares real paths, so a script started through a symbolic link, such as an npm bin entry, counts. isProcessRunning(pid) tells whether a process, such as a lock’s owner, is still running.

    Generated from TSDoc

    API reference

    Every export of [email protected], 43 in all, with its signature, its errors, and an example.

    Functions

    createShutdownSignal

    function

    Turns the signals that ask a process to stop, SIGINT and SIGTERM by default, into an AbortSignal, so a script or a service passes one signal to the work it shuts down. The first signal aborts it with PROCESS_INTERRUPTED, sets process.exitCode to 128 plus the signal's number, 130 for SIGINT and 143 for SIGTERM, as a shell reports it, and calls onSignal. It never calls process.exit: the process exits once the work stops. Later signals are ignored, or, with forceOnRepeat, stop the process at once, as they would without a listener. On Windows, Ctrl+C sends SIGINT, but SIGTERM is never delivered. With disconnect, a child started with an IPC channel, such as by fork, also shuts down as with SIGTERM when its parent closes the channel or dies, at once when it's already closed. dispose stops listening.

    Signature
    createShutdownSignal(options?: IShutdownSignalOptions): IShutdownSignal

    Parameters

    options?IShutdownSignalOptions

    The signals, what runs at the first one, and how later ones behave.

    Defaults to {}.

    Returns

    IShutdownSignal

    The shutdown signal.

    Throws

    • INVALID_SIGNALThe shutdown needs a non-empty list of signals Node.js can listen for, such as SIGINT, other than SIGKILL and SIGSTOP, and an onSignal that's a function when it's given.
    Example
    import { createShutdownSignal } from 'jg-process';
    
    declare const drainQueue: (signal: AbortSignal) => Promise<void>;
    
    const shutdown = createShutdownSignal({ forceOnRepeat: true });
    await drainQueue(shutdown.signal); // stops at Ctrl+C, and the process exits with 130
    shutdown.dispose();

    isMainModule

    function

    Tells whether the module is the script Node.js was started with, such as a CLI file that should run its command only when executed, not when a test imports it. It compares real paths, so a script started through a symbolic link, such as an npm bin entry, still counts.

    Signature
    isMainModule(importMetaUrl: string): boolean

    Parameters

    importMetaUrlstring

    The module's import.meta.url.

    Returns

    boolean

    Whether the module is the entry script, or false when either path can't be resolved.

    Example
    import { isMainModule } from 'jg-process';
    
    if (isMainModule(import.meta.url)) {
      // run the command
    }

    isProcessRunning

    function

    Tells whether a process with this id is running on this machine, such as the owner a lock file names. It sends signal 0, which checks without signalling, and treats only ESRCH, no such process, as gone: a process that belongs to another user still counts as running.

    Signature
    isProcessRunning(pid: number): boolean

    Parameters

    pidnumber

    The process id.

    Returns

    boolean

    Whether the process is running.

    Throws

    • INVALID_PROCESS_IDThe process id must be a positive safe integer.
    Example
    import { isProcessRunning } from 'jg-process';
    
    isProcessRunning(process.pid); // true

    parseStrictArgs

    function

    Parses a command's arguments strictly, as util.parseArgs does with strict: true and no positionals, and also refuses what parseArgs lets through: an option given twice that isn't multiple, an empty string value, and a missing required option. A missing flag is false, a missing multiple option an empty array, and a missing optional string undefined.

    Signature
    parseStrictArgs<TOptions extends Record<string, IStrictArgOption>>(
      args: readonly string[],
      options: TOptions,
    ): IStrictArgValues<TOptions>

    Parameters

    argsreadonly string[]

    The arguments, such as process.argv.slice(2).

    optionsTOptions

    The options, by name.

    Returns

    IStrictArgValues<TOptions>

    The values, by name.

    Throws

    • INVALID_ARGUMENTSan unknown option, a positional argument, a missing or empty value, a repeated
    • Erroroption, or a missing required one, with `data.option` naming it, and Node.js's error as the cause
    • Errorwhen it found the problem
    Example
    import { parseStrictArgs } from 'jg-process';
    
    const { check, path, tag } = parseStrictArgs(['--path', 'docs', '--tag', 'a', '--tag', 'b'], {
      check: { type: 'boolean' },
      path: { type: 'string', required: true },
      tag: { type: 'string', multiple: true },
    }); // check: false, path: 'docs', tag: ['a', 'b']

    readStdin

    function

    Reads the process's stdin to its end, such as the input a CLI is piped. It refuses more than maxBytes, destroying stdin once the input passes the limit, and stops when the signal aborts.

    Signature
    readStdin(options: IReadStdinOptions): Promise<Buffer<ArrayBufferLike>>

    Parameters

    optionsIReadStdinOptions

    The byte limit, and the signal that stops the read.

    Returns

    Promise<Buffer<ArrayBufferLike>>

    A promise that resolves to the bytes stdin held.

    Throws

    • INVALID_PROCESS_OPTIONSThe stdin limit must be a positive safe integer.
    • STDIN_TOO_LARGEstdin held more than `maxBytes`
    • ErrorThe signal's reason when the signal aborts.
    Example
    import { readStdin } from 'jg-process';
    
    const input = (await readStdin({ maxBytes: 1_048_576 })).toString('utf8');

    readStream

    function

    Reads a stream to its end, such as a request body or a socket's message, into one buffer. It refuses more than maxBytes the moment the content passes the limit, and stops when the signal aborts. A refused or stopped read leaves the stream open and paused, so its owner can still answer, such as with a 413, before it closes the stream. Start reading before the stream ends.

    Signature
    readStream(
      stream: Readable,
      options: IReadStreamOptions,
    ): Promise<Buffer<ArrayBufferLike>>

    Parameters

    streamReadable

    The stream, which isn't in object mode.

    optionsIReadStreamOptions

    The byte limit, and the signal that stops the read.

    Returns

    Promise<Buffer<ArrayBufferLike>>

    A promise that resolves to the stream's bytes.

    Throws

    • INVALID_PROCESS_OPTIONSThe stream limit must be a positive safe integer.
    • STREAM_TOO_LARGEThe stream is larger than `maxBytes` bytes.
    • ErrorThe stream's own error, unchanged.
    • ErrorThe signal's reason when the signal aborts.
    Example
    import { createServer } from 'node:http';
    import { readStream } from 'jg-process';
    
    createServer(async (request, response) => {
      const body = await readStream(request, { maxBytes: 65_536 }); // STREAM_TOO_LARGE past 64 KiB
      response.end(body);
    });

    reservePort

    function

    Reserves a free TCP port by listening on it, so nothing else can take it until it's released, such as before passing it to a dev server or a test fixture. The preferred port is kept when it's free, and any free port is reserved when it's taken. Connections to the reserved port are closed at once, and the reservation doesn't keep the process running.

    A child process that needs the port itself can't bind it while it's reserved, so release it right before starting the child. Another process can take the port in between.

    Signature
    reservePort(options?: IReservePortOptions): Promise<IPortReservation>

    Parameters

    options?IReservePortOptions

    The preferred port and the address. The address defaults to 127.0.0.1.

    Defaults to {}.

    Returns

    Promise<IPortReservation>

    A promise that resolves to the reservation.

    Throws

    • INVALID_PORTThe preferred port must be an integer from 1 to 65535.
    • PORT_RESERVATION_FAILEDThe port couldn't be reserved.
    Example
    import { reservePort } from 'jg-process';
    
    const reservation = await reservePort({ preferredPort: 3000 });
    console.log(`The port is ${reservation.port}`); // 3000, or another free port when it's taken
    await reservation.release();

    resolvePackageManagerCommand

    function

    Builds the command that runs a package manager with the given arguments, without a shell, so it can be passed to runProcess or startProcess as it is on Windows, macOS, and Linux. It looks, in order, at: 1. npm_execpath, which a package manager sets for the scripts it runs, when it names the requested package manager, or any one when none is requested 2. the package manager installed in the project, resolved from cwd 3. PATH: an executable on POSIX, and on Windows an .exe, or a .cmd or .ps1 shim whose script it runs through Node.js, because Node.js refuses to spawn a .cmd file without a shell

    A JavaScript entry, such as pnpm.cjs, runs through the current Node.js. Without a requested package manager or npm_execpath, there's nothing to look for, so it throws.

    Signature
    resolvePackageManagerCommand(
      args: string[],
      options?: IResolvePackageManagerOptions,
    ): IPackageManagerCommand

    Parameters

    argsstring[]

    The arguments for the package manager, such as ['run', 'build'].

    options?IResolvePackageManagerOptions

    The package manager, the environment, and the directory to resolve from.

    Defaults to {}.

    Returns

    IPackageManagerCommand

    The command and its arguments.

    Throws

    • UNSUPPORTED_PACKAGE_MANAGERThe package manager must be pnpm, npm, yarn, or bun.
    • PACKAGE_MANAGER_NOT_FOUNDThe package manager's executable couldn't be found.
    Example
    import { resolvePackageManagerCommand, runProcess } from 'jg-process';
    
    const command = resolvePackageManagerCommand(['run', 'build'], { packageManager: 'pnpm' });
    const { exitCode } = await runProcess({ ...command, timeoutSeconds: 600 });

    runForegroundProcess

    function

    Runs a command without a shell, inheriting all three standard streams in the caller's process group. The terminal handles interrupts for the group. The caller retains its own signal policy.

    Signature
    runForegroundProcess(
      command: string,
      args: string[],
      options?: IForegroundProcessOptions,
    ): Promise<IForegroundProcessResult>

    Parameters

    commandstring

    The executable to run.

    argsstring[]

    The arguments passed to the executable.

    options?IForegroundProcessOptions

    The optional working directory and environment.

    Defaults to {}.

    Returns

    Promise<IForegroundProcessResult>

    A promise that resolves to the child's exit code and signal after it closes.

    Throws

    • PROCESS_START_FAILEDUnable to start '<command>'.
    Example
    import { runForegroundProcess } from 'jg-process';
    
    const { exitCode, signalCode } = await runForegroundProcess('npm', ['--version']);

    runProcess

    function

    Runs a command to completion and resolves with its exit code, signal and output, decoded as UTF-8, or as the bytes the process wrote with encoding: 'buffer'. It runs without a shell and, on POSIX, as the leader of its own process group.

    Every exit resolves, whatever its code: callers decide which codes are acceptable, so process output never ends up in an error message. The call rejects only when the command can't start, the timeout passes, the output exceeds maxOutputBytes, or the signal aborts; in those cases the process tree is stopped first, so the rejection can arrive up to the grace period plus two seconds after the trigger on POSIX, and up to 30 seconds after it on Windows, where the stop lists the tree, runs taskkill, and waits for every process to exit, each within 10 seconds.

    Input that the child never reads is discarded without an error. When the child exits, descendants left in its process group are stopped. If one that couldn't be reached, such as a descendant that left the group with setsid or one orphaned on Windows, keeps stdout or stderr open for longer than the grace period, the call rejects instead of hanging.

    Because the child leads its own group, a terminal's Ctrl+C reaches only the parent: CLIs should abort the signal when they receive SIGINT or SIGTERM.

    Signature
    runProcess<TEncoding extends IProcessOutputEncoding = "utf-8">(
      options: IRunProcessOptions<TEncoding>,
    ): Promise<IProcessResult<IProcessOutput<TEncoding>>>

    Parameters

    optionsIRunProcessOptions<TEncoding>

    The command, its arguments, and the limits of the run.

    Returns

    Promise<IProcessResult<IProcessOutput<TEncoding>>>

    A promise that resolves with the exit code, the signal and the output.

    Throws

    • INVALID_PROCESS_OPTIONSThe timeout must be a positive number of at most 2,147,483 seconds, and the output limit a positive safe integer.
    • PROCESS_START_FAILEDUnable to start '<command>'.
    • PROCESS_TIMED_OUTThe process didn't finish within the timeout
    • PROCESS_OUTPUT_TOO_LARGEThe process wrote more output than allowed
    • PROCESS_TREE_SURVIVEDThe process tree is still running after it was force-stopped
    • ErrorThe signal's reason when the signal aborts.
    Example
    import { runProcess } from 'jg-process';
    
    const { exitCode, stdout } = await runProcess({ command: 'git', args: ['status', '--porcelain'], timeoutSeconds: 30 });
    if (exitCode !== 0) {
      // handle the failure
    }

    startProcess

    function

    Starts a long-running child that the caller owns, without a shell and, on POSIX, as the leader of its own process group, so stopProcessTree can later stop it together with its descendants. Because the child leads its own group, a terminal's Ctrl+C reaches only the parent: CLIs should stop the child when they receive SIGINT or SIGTERM. A command that can't start emits an 'error' event on the returned child.

    Signature
    startProcess(
      command: string,
      args: string[],
      options?: IStartProcessOptions,
    ): ChildProcess

    Parameters

    commandstring

    The executable to run.

    argsstring[]

    The arguments passed to the executable.

    options?IStartProcessOptions

    The working directory, the environment and the stdio configuration ('inherit' by default).

    Returns

    ChildProcess

    The started child process.

    Starts a long-running child from a command object, the form runProcess takes, with the same behavior as the positional form.

    Signature
    startProcess(command: IStartProcessCommand): ChildProcess

    Parameters

    commandIStartProcessCommand

    The executable, its arguments, and the working directory, environment, and stdio.

    Returns

    ChildProcess

    The started child process.

    Example 1
    import { startProcess, stopProcessTree } from 'jg-process';
    
    const server = startProcess('node', ['server.js'], { stdio: 'inherit' });
    process.once('SIGINT', () => void stopProcessTree(server));
    Example 2
    import { startProcess } from 'jg-process';
    
    const server = startProcess({ command: 'node', args: ['server.js'], cwd: 'apps/api' });

    startProcessAndWait

    function

    Starts a long-running process, such as a dev server, with startProcess, and resolves with it once it's ready: its port accepts connections, its URL answers, or a probe returns a value other than null, undefined, or false. When it exits, fails to start, isn't ready in time, or the signal aborts, its whole process tree is stopped before the call rejects, so nothing is left running.

    Signature
    startProcessAndWait(
      command: IStartProcessCommand,
      options: IStartProcessAndWaitOptions,
    ): Promise<ChildProcess>

    Parameters

    commandIStartProcessCommand

    The command, its arguments, and the working directory, environment, and stdio.

    optionsIStartProcessAndWaitOptions

    What ready means, the deadline and pace, the signal, and the grace period stopProcessTree gets on failure.

    Returns

    Promise<ChildProcess>

    A promise that resolves to the ready child process, which the caller owns.

    Throws

    • INVALID_PORTThe port must be an integer from 1 to 65535.
    • INVALID_READINESS_OPTIONSThe readiness options need finite, positive seconds.
    • PROCESS_NOT_READYThe process wasn't ready in time.
    • PROCESS_EXITED_BEFORE_READYThe process exited before it was ready.
    • PROCESS_START_FAILEDUnable to start the process.
    • PROCESS_TREE_SURVIVEDThe process tree is still running after it was force-stopped.
    • ErrorThe signal's reason when the signal aborts.
    Example
    import { startProcessAndWait, stopProcessTree } from 'jg-process';
    
    const server = await startProcessAndWait(
      { command: process.execPath, args: ['server.js'], cwd: 'apps/api' },
      { readyWhen: { url: 'http://127.0.0.1:3000/health' }, timeoutSeconds: 60 },
    );
    await stopProcessTree(server);

    stopProcessTree

    function

    Stops a child started by startProcess or runProcess together with its descendants, and resolves once they are gone. It resolves immediately for a child without a pid.

    On POSIX, it sends SIGTERM to the child's process group, waits up to the grace period, then sends SIGKILL and waits up to two more seconds. It is designed for children that lead their own process group, as startProcess and runProcess start them; for any other child, it signals the child alone. Descendants that start their own session or process group, for example with setsid, aren't stopped.

    On Windows, it lists the child's descendants with PowerShell's Get-CimInstance, runs taskkill /T /F, and waits up to ten seconds until the child and each listed descendant have exited, so their working directories can be removed once it resolves. taskkill reaches only descendants of a root process that is still running: when the root has already exited, it resolves and orphaned descendants may remain. Without PowerShell, it waits for the child alone.

    Signature
    stopProcessTree(child: ChildProcess, gracePeriodSeconds?: number): Promise<void>

    Parameters

    childChildProcess

    The child process to stop.

    gracePeriodSeconds?number

    No description.

    Defaults to DEFAULT_GRACE_PERIOD_SECONDS.

    Returns

    Promise<void>

    A promise that resolves once the process tree is gone.

    Throws

    • PROCESS_TREE_SURVIVEDThe process tree is still running after it was force-stopped.
    Example
    import { startProcess, stopProcessTree } from 'jg-process';
    
    const server = startProcess('node', ['server.js']);
    await stopProcessTree(server, 10); // SIGTERM, then SIGKILL after 10 seconds

    waitForPort

    function

    Waits until a TCP port accepts connections, such as a database or a dev server that a script started, probing every intervalSeconds until timeoutSeconds pass. With a child, its exit or failure to start ends the wait at once, since it can no longer become ready.

    Signature
    waitForPort(port: number, options: IWaitForPortOptions): Promise<void>

    Parameters

    portnumber

    The port, from 1 to 65535.

    optionsIWaitForPortOptions

    The address, the deadline and pace, the signal, and the child to watch.

    Returns

    Promise<void>

    A promise that resolves once a connection succeeds.

    Throws

    • INVALID_PORTThe port must be an integer from 1 to 65535.
    • INVALID_READINESS_OPTIONSThe readiness options need finite, positive seconds.
    • PROCESS_NOT_READYThe process wasn't ready in time.
    • PROCESS_EXITED_BEFORE_READYThe process exited before it was ready.
    • PROCESS_START_FAILEDUnable to start the process.
    • ErrorThe signal's reason when the signal aborts.
    Example
    import { waitForPort } from 'jg-process';
    
    await waitForPort(5432, { timeoutSeconds: 30 }); // PostgreSQL accepts connections

    waitForUrl

    function

    Waits until a URL answers in a way that means ready, by default with a 2xx status, such as a health endpoint. Redirects aren't followed, a network error means not ready yet, and every response's body is discarded. With a child, its exit or failure to start ends the wait at once.

    Signature
    waitForUrl(url: string | URL, options: IWaitForUrlOptions): Promise<void>

    Parameters

    urlstring | URL

    The URL.

    optionsIWaitForUrlOptions

    The deadline and pace, the request timeout, the predicate, the signal, and the child to watch.

    Returns

    Promise<void>

    A promise that resolves once a response means ready.

    Throws

    • INVALID_READINESS_OPTIONSThe readiness options need finite, positive seconds.
    • PROCESS_NOT_READYThe process wasn't ready in time.
    • PROCESS_EXITED_BEFORE_READYThe process exited before it was ready.
    • PROCESS_START_FAILEDUnable to start the process.
    • ErrorThe signal's reason when the signal aborts.
    Example
    import { waitForUrl } from 'jg-process';
    
    await waitForUrl('http://127.0.0.1:3000/health', { timeoutSeconds: 60 });

    Constants

    DEFAULT_GRACE_PERIOD_SECONDS

    const

    the seconds stopProcessTree waits after SIGTERM before it sends SIGKILL

    Declaration
    const DEFAULT_GRACE_PERIOD_SECONDS: 5

    DEFAULT_MAX_OUTPUT_BYTES

    const

    the bytes stdout and stderr can hold together when runProcess gets no maxOutputBytes (1 MiB)

    Declaration
    const DEFAULT_MAX_OUTPUT_BYTES: 1048576

    DEFAULT_SHUTDOWN_SIGNALS

    const

    the signals createShutdownSignal listens for by default: Ctrl+C, and a service manager's stop

    Declaration
    const DEFAULT_SHUTDOWN_SIGNALS: readonly NodeJS.Signals[]

    ERRORS

    const

    the codes for every error thrown by the package

    Declaration
    const ERRORS: { PROCESS_START_FAILED: "PROCESS_START_FAILED"; PROCESS_TIMED_OUT: "PROCESS_TIMED_OUT"; PROCESS_OUTPUT_TOO_LARGE: "PROCESS_OUTPUT_TOO_LARGE"; PROCESS_TREE_SURVIVED: "PROCESS_TREE_SURVIVED"; INVALID_PORT: "INVALID_PORT"; PORT_RESERVATION_FAILED: "PORT_RESERVATION_FAILED"; UNSUPPORTED_PACKAGE_MANAGER: "UNSUPPORTED_PACKAGE_MANAGER"; PACKAGE_MANAGER_NOT_FOUND: "PACKAGE_MANAGER_NOT_FOUND"; INVALID_READINESS_OPTIONS: "INVALID_READINESS_OPTIONS"; PROCESS_NOT_READY: "PROCESS_NOT_READY"; PROCESS_EXITED_BEFORE_READY: "PROCESS_EXITED_BEFORE_READY"; PROCESS_INTERRUPTED: "PROCESS_INTERRUPTED"; INVALID_SIGNAL: "INVALID_SIGNAL"; INVALID_PROCESS_OPTIONS: "INVALID_PROCESS_OPTIONS"; INVALID_PROCESS_ID: "INVALID_PROCESS_ID"; INVALID_ARGUMENTS: "INVALID_ARGUMENTS"; STDIN_TOO_LARGE: "STDIN_TOO_LARGE"; STREAM_TOO_LARGE: "STREAM_TOO_LARGE" }

    Types

    IForegroundProcessOptions

    type

    the working directory and environment of a foreground command

    Declaration
    type IForegroundProcessOptions = Pick<IStartProcessOptions, 'cwd' | 'env'>;

    IForegroundProcessResult

    type

    the exit code or signal reported by the foreground command

    Declaration
    type IForegroundProcessResult = Pick<IProcessResult, 'exitCode' | 'signalCode'>;

    IPackageManager

    type

    the package managers resolvePackageManagerCommand finds

    Declaration
    type IPackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun';

    IPackageManagerCommand

    type

    a command to spawn without a shell, which runProcess and startProcess take as they are

    Declaration
    type IPackageManagerCommand = {
        command: string;
        args: string[];
    };

    Members

    • commandproperty
      string
    • argsproperty
      string[]

    IPortReservation

    type

    a port held until it's released, so nothing else can take it meanwhile

    Declaration
    type IPortReservation = {
        port: number;
        host: string;
        release: () => Promise<void>;
    };

    Members

    • portproperty
      number

      the reserved port

    • hostproperty
      string

      the address it's reserved on

    • releaseproperty
      () => Promise<void>

      stops holding the port; calling it again does nothing

    IProcessOutput

    type

    the type of the output for an encoding: a string for 'utf-8', a Buffer for 'buffer'

    Declaration
    type IProcessOutput<TEncoding extends IProcessOutputEncoding> = TEncoding extends 'buffer' ? Buffer : string;

    IProcessOutputEncoding

    type

    how runProcess returns the output: decoded as UTF-8, or as the bytes the process wrote

    Declaration
    type IProcessOutputEncoding = 'utf-8' | 'buffer';

    IProcessResult

    type

    the outcome of a process that ran to completion

    Declaration
    type IProcessResult<TOutput extends string | Buffer = string> = {
        exitCode: number | null;
        signalCode: NodeJS.Signals | null;
        stdout: TOutput;
        stderr: TOutput;
        durationMs: number;
    };

    Members

    • exitCodeproperty
      number | null

      the exit code, or null when a signal ended the process

    • signalCodeproperty
      NodeJS.Signals | null

      the signal that ended the process, or null when it exited on its own

    • stdoutproperty
      TOutput

      the output, decoded as UTF-8, or as bytes with encoding 'buffer'

    • stderrproperty
      TOutput
    • durationMsproperty
      number

      the milliseconds between the start and the end of the run

    IReadinessWaitOptions

    type

    what a readiness wait shares: its deadline, its pace, and what ends it early

    Declaration
    type IReadinessWaitOptions = {
        timeoutSeconds: number;
        intervalSeconds?: number | undefined;
        signal?: AbortSignal | undefined;
        child?: ChildProcess | undefined;
    };

    Members

    • timeoutSecondsproperty
      number

      the most seconds the whole wait can take

    • intervalSecondsproperty
      number

      the seconds between probes. Defaults to 0.25

    • signalproperty
      AbortSignal

      stops the wait and rejects with the signal's reason when it aborts

    • childproperty
      ChildProcess

      a process whose exit or start failure ends the wait, because it can no longer become ready

    IReadStdinOptions

    type

    how readStdin reads

    Declaration
    type IReadStdinOptions = {
        maxBytes: number;
        signal?: AbortSignal | undefined;
    };

    Members

    • maxBytesproperty
      number

      the most bytes stdin may hold, a positive safe integer

    • signalproperty
      AbortSignal

      stops reading and rejects with the signal's reason when it aborts

    IReadStreamOptions

    type

    how readStream reads

    Declaration
    type IReadStreamOptions = {
        maxBytes: number;
        signal?: AbortSignal | undefined;
    };

    Members

    • maxBytesproperty
      number

      the most bytes the stream may hold, a positive safe integer

    • signalproperty
      AbortSignal

      stops reading and rejects with the signal's reason when it aborts

    IReadyWhen

    type

    what startProcessAndWait waits for: a port, a URL, or a probe returning other than null, undefined, or false

    Declaration
    type IReadyWhen = {
        port: number;
        host?: string | undefined;
    } | {
        url: string | URL;
        isReady?: IWaitForUrlOptions['isReady'];
    } | {
        probe: (signal: AbortSignal) => unknown;
    };

    IReservePortOptions

    type

    the options reservePort takes

    Declaration
    type IReservePortOptions = {
        preferredPort?: number | undefined;
        host?: string | undefined;
    };

    Members

    • preferredPortproperty
      number

      the port to try first, from 1 to 65535; another free one is reserved when it's taken

    • hostproperty
      string

      the address to listen on. Defaults to 127.0.0.1

    IResolvePackageManagerOptions

    type

    the options resolvePackageManagerCommand takes

    Declaration
    type IResolvePackageManagerOptions = {
        packageManager?: IPackageManager | undefined;
        env?: NodeJS.ProcessEnv | undefined;
        cwd?: string | undefined;
    };

    Members

    • packageManagerproperty
      IPackageManager

      The package manager to run. Defaults to the one running the current package script, which npm_execpath names; without one, a package manager has to be named.

    • envproperty
      NodeJS.ProcessEnv

      the environment that holds npm_execpath and PATH. Defaults to process.env

    • cwdproperty
      string

      the directory a local install is resolved from. Defaults to the current one

    IRunProcessOptions

    type

    the options accepted by runProcess

    Declaration
    type IRunProcessOptions<TEncoding extends IProcessOutputEncoding = 'utf-8'> = {
        command: string;
        args: string[];
        cwd?: string;
        env?: NodeJS.ProcessEnv;
        input?: string | Uint8Array;
        timeoutSeconds?: number;
        maxOutputBytes?: number;
        signal?: AbortSignal;
        encoding?: TEncoding;
    };

    Members

    • commandproperty
      string

      the executable to run, without a shell

    • argsproperty
      string[]

      the arguments passed to the executable

    • cwdproperty
      string

      the working directory. Defaults to the current one

    • envproperty
      NodeJS.ProcessEnv

      the environment variables. Defaults to process.env

    • inputproperty
      string | Uint8Array

      the data written to the process's stdin before it is closed

    • timeoutSecondsproperty
      number

      the seconds after which the process tree is stopped and the call rejects, a positive number of at most 2,147,483

    • maxOutputBytesproperty
      number

      the bytes stdout and stderr can hold together, a positive safe integer. Defaults to DEFAULT_MAX_OUTPUT_BYTES

    • signalproperty
      AbortSignal

      stops the process tree and rejects with the signal's reason when it aborts

    • encodingproperty
      TEncoding

      whether stdout and stderr are decoded as UTF-8 or returned as the bytes the process wrote, such as paths that aren't valid UTF-8. Defaults to 'utf-8'

    IShutdownSignal

    type

    a signal that aborts when the process is asked to stop

    Declaration
    type IShutdownSignal = {
        readonly signal: AbortSignal;
        receivedSignal: () => NodeJS.Signals | null;
        exitCode: () => number | null;
        dispose: () => void;
    };

    Members

    • signalproperty
      AbortSignal

      aborts with PROCESS_INTERRUPTED at the first signal

    • receivedSignalproperty
      () => NodeJS.Signals | null

      the first signal received, or null

    • exitCodeproperty
      () => number | null

      128 plus the first signal's number, such as 130 for SIGINT, or null before one

    • disposeproperty
      () => void

      stops listening, so a later signal gets Node.js's default; calling it again does nothing

    IShutdownSignalOptions

    type

    what createShutdownSignal takes

    Declaration
    type IShutdownSignalOptions = {
        signals?: readonly NodeJS.Signals[] | undefined;
        onSignal?: ((signal: NodeJS.Signals) => unknown) | undefined;
        setExitCode?: boolean | undefined;
        forceOnRepeat?: boolean | undefined;
        disconnect?: boolean | undefined;
    };

    Members

    • signalsproperty
      readonly NodeJS.Signals[]

      the signals that start the shutdown. Defaults to SIGINT and SIGTERM

    • onSignalproperty
      (signal: NodeJS.Signals) => unknown

      runs once, with the first signal, after the abort; what it throws or rejects with is ignored

    • setExitCodeproperty
      boolean

      whether the first signal sets process.exitCode to 128 plus its number, as a shell reports a process the signal stopped. Defaults to true

    • forceOnRepeatproperty
      boolean

      whether a second signal stops the process at once, as Node.js does without a listener, for a shutdown that hangs. Defaults to false, which ignores later signals

    • disconnectproperty
      boolean

      whether the IPC channel to the parent closing starts the shutdown too, as SIGTERM does, such as for a child whose parent died without signalling it. Defaults to false

    IStartProcessAndWaitOptions

    type

    the options startProcessAndWait takes

    Declaration
    type IStartProcessAndWaitOptions = Omit<IReadinessWaitOptions, 'child'> & {
        readyWhen: IReadyWhen;
        gracePeriodSeconds?: number | undefined;
    };

    IStartProcessCommand

    type

    a command and its options in one object, the form runProcess takes, accepted by startProcess too

    Declaration
    type IStartProcessCommand = IStartProcessOptions & {
        command: string;
        args: string[];
    };

    IStartProcessOptions

    type

    the options accepted by startProcess

    Declaration
    type IStartProcessOptions = {
        cwd?: string;
        env?: NodeJS.ProcessEnv;
        stdio?: StdioOptions;
    };

    Members

    • cwdproperty
      string

      the working directory. Defaults to the current one

    • envproperty
      NodeJS.ProcessEnv

      the environment variables. Defaults to process.env

    • stdioproperty
      StdioOptions

      the stdio configuration. Defaults to 'inherit'

    IStrictArgOption

    type

    one option parseStrictArgs accepts: a flag, or a string that can be required or repeated

    Declaration
    type IStrictArgOption = {
        type: 'boolean';
    } | {
        type: 'string';
        multiple?: boolean | undefined;
        required?: boolean | undefined;
    };

    IStrictArgValues

    type

    the values parseStrictArgs returns for its options: false for a missing flag, an empty array for a missing repeated option, and undefined for a missing optional string

    Declaration
    type IStrictArgValues<TOptions extends Record<string, IStrictArgOption>> = {
        [TName in keyof TOptions]: TOptions[TName] extends {
            type: 'boolean';
        } ? boolean : TOptions[TName] extends {
            multiple: true;
        } ? string[] : TOptions[TName] extends {
            required: true;
        } ? string : string | undefined;
    };

    IWaitForPortOptions

    type

    the options waitForPort takes

    Declaration
    type IWaitForPortOptions = IReadinessWaitOptions & {
        host?: string | undefined;
        connectTimeoutSeconds?: number | undefined;
    };

    IWaitForUrlOptions

    type

    the options waitForUrl takes

    Declaration
    type IWaitForUrlOptions = IReadinessWaitOptions & {
        requestTimeoutSeconds?: number | undefined;
        isReady?: ((response: Response) => boolean | Promise<boolean>) | undefined;
    };