node-pty

repository·main·Indexed 24 days ago

https://github.com/microsoft/node-pty

A Node.js library providing forkpty(3) bindings to spawn processes with pseudoterminal file descriptors. It is primarily used for building terminal emulators and running programs that require a TTY environment. Key features include the pty.spawn() method for creating ITerminal instances, support for XON/XOFF flow control, and platform-specific configurations for Unix and Windows (ConPTY). Version 1.1.0.

Tokens
3K
Snippets
8
Records
25
Agent score
84%

What's inside node-pty

  1. Security and Thread Safety considerations

    main

    Security

    All processes launched via node-pty run at the same permission level as the parent process. If running in a server environment accessible via the internet, it is highly recommended to launch the pty inside a container to protect the host machine.

    Thread Safety

    node-pty is not thread safe. Avoid running it across multiple worker threads in Node.js to prevent issues.

  2. How flow control works in node-pty

    main

    Flow control allows you to pause and resume the child process using XON/XOFF control codes. This can be enabled by setting handleFlowControl: true in the pty.spawn options or by setting ptyProcess.handleFlowControl = true on the instance.

    By default, it uses:

    • PAUSE: \x13 (XOFF)
    • RESUME: \x11 (XON)

    When flow control is enabled, these codes are not passed to the underlying pseudoterminal. You can customize these sequences using flowControlPause and flowControlResume in the constructor options to avoid conflicts with other environments.

    const PAUSE = '\x13';   // XOFF
    const RESUME = '\x11';  // XON
    
    const ptyProcess = pty.spawn(shell, [], {handleFlowControl: true});
    
    // flow control in action
    ptyProcess.write(PAUSE);  // pty will block and pause the child program
    // ...
    ptyProcess.write(RESUME); // pty will enter flow mode and resume the child program
    
    // temporarily disable/re-enable flow control
    ptyProcess.handleFlowControl = false;
    // ...
    ptyProcess.handleFlowControl = true;
  3. Install and build node-pty

    main

    To use node-pty, you must install its dependencies and compile the C++ bindings.

    Prerequisites:

    • Node.js 16+ or Electron 19+
    • Windows 10 version 1809+ (for Windows support via ConPTY)

    Build Commands:

    # Install dependencies and build C++
    npm install
    
    # Compile TypeScript -> JavaScript
    npm run build
    # Install dependencies and build C++
    npm install
    # Compile TypeScript -> JavaScript
    npm run build
  4. Configure system dependencies for building node-pty

    main

    Building node-pty requires specific system-level tools depending on your platform.

    Linux (apt)

    Install make, python, and build-essential:

    sudo apt install -y make python build-essential

    macOS

    Install Xcode from the App Store to compile sources.

    Windows

    1. Run PowerShell as Administrator and install build tools:
      npm install --global --production windows-build-tools
    2. Install the Windows SDK (specifically the "Desktop C++ Apps" component).
    3. Install Spectre-mitigated libraries via the Visual Studio Installer (e.g., MSVC v143 - VS 2022 C++ x64/x86 Spectre-mitigated libs (Latest)).
  5. Run the Electron terminal example

    main

    This example demonstrates how to implement a terminal in Electron by using xterm.js in the renderer process and node-pty in the main process, communicating via IPC.

    To run the example, follow these steps:

    1. Install dependencies using the platform-specific script.
    2. Launch the application using npm start.
    # Install dependencies (Windows)
    ./npm-install.bat
    
    # Install dependencies (non-Windows)
    ./npm-install.sh
    
    # Launch the app
    npm start
  6. Configure flow control in Terminal

    main

    The Terminal class supports automatic flow control to manage data transmission. When handleFlowControl is enabled in IPtyForkOptions, the terminal intercepts specific pause and resume sequences to prevent them from being forwarded to the underlying PTY. This is useful for avoiding conflicts with shell-level XON/XOFF control codes.

    By default, the sequences used are:

    • Pause: \x13 (XOFF)
    • Resume: \x11 (XON)

    You can customize these sequences by providing flowControlPause and flowControlResume in the IPtyForkOptions object.

  7. Troubleshoot PowerShell error 8009001d

    main
    If you encounter the error Internal Windows PowerShell error. Loading managed Windows PowerShell failed with error 8009001d, it is typically caused by PowerShell being launched without the SystemRoot environment variable present.
  8. Run the deep tree process kill test

    main

    This manual test verifies that deeply nested process trees are correctly killed on Windows. When executed, it launches a Notepad window and a Webpack dev server. After 10 seconds, the Webpack dev server should be killed, while the Notepad instance should remain running. You can verify the kill was successful by checking Process Explorer or re-running the test.

    npm i
    node index.js
  9. Spawn a terminal process with pty.spawn()

    main

    Use pty.spawn() to fork a process with a pseudoterminal file descriptor. This returns a terminal object that supports reading data, writing data, and resizing. This is the primary way to create a terminal emulator or run programs that require a TTY environment.

    Common options for the spawn configuration include:

    • name: The terminal type (e.g., 'xterm-color').
    • cols: Number of columns.
    • rows: Number of rows.
    • cwd: Current working directory.
    • env: Environment variables.
    • handleFlowControl: Boolean to enable automatic XON/XOFF flow control.
    import * as os from 'node:os';
    import * as pty from 'node-pty';
    
    const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
    
    const ptyProcess = pty.spawn(shell, [], {
      name: 'xterm-color',
      cols: 80,
      rows: 30,
      cwd: process.env.HOME,
      env: process.env
    });
    
    ptyProcess.onData((data) => {
      process.stdout.write(data);
    });
    
    ptyProcess.write('ls\r');
    ptyProcess.resize(100, 40);
    ptyProcess.write('ls\r');
  10. Configure IWindowsPtyForkOptions

    main

    On Windows, you can provide IWindowsPtyForkOptions to control specific ConPTY behaviors.

    Options

    • useConpty: Whether to use the ConPTY system. (Note: This option is deprecated and will be removed in a future version).
    • useConptyDll: Use the ConPTY DLL.
    • conptyInheritCursor: Whether the cursor is inherited in ConPTY.