Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The node-pty package is a Node.js library that provides bindings to pseudo terminals (PTYs). It allows you to spawn and interact with terminal processes programmatically, making it useful for building terminal emulators, remote shells, and other command-line interface (CLI) tools.
Spawning a Terminal Process
This feature allows you to spawn a terminal process. The code sample demonstrates how to spawn a shell process and listen for data events to capture the terminal output.
const pty = require('node-pty');
const shell = process.env[process.platform === 'win32' ? 'COMSPEC' : 'SHELL'];
const ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.cwd(),
env: process.env
});
ptyProcess.on('data', function(data) {
console.log(data);
});
Resizing the Terminal
This feature allows you to resize the terminal window. The code sample shows how to resize the terminal to 100 columns and 40 rows.
ptyProcess.resize(100, 40);
Writing to the Terminal
This feature allows you to send input to the terminal process. The code sample demonstrates how to send the 'ls' command to the terminal.
ptyProcess.write('ls\r');
xterm is a front-end component that provides a terminal emulator in the browser. It is often used in conjunction with node-pty to create web-based terminal applications. While node-pty handles the backend terminal process, xterm handles the frontend display and interaction.
pty.js is another Node.js library for interacting with pseudo terminals. It is similar to node-pty but is less actively maintained. node-pty is generally preferred due to its better support and more recent updates.
shelljs is a portable Unix shell commands for Node.js. While it does not provide a PTY interface, it allows you to run shell commands and scripts in a more straightforward manner. It is more suitable for simple scripting tasks rather than full terminal emulation.
winpty is a Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs. The package consists of a library (libwinpty) and a tool for Cygwin and MSYS for running Windows console programs in a Cygwin/MSYS pty.
The software works by starting the winpty-agent.exe
process with a new,
hidden console window, which bridges between the console API and terminal
input/output escape codes. It polls the hidden console's screen buffer for
changes and generates a corresponding stream of output.
The Unix adapter allows running Windows console programs (e.g. CMD, PowerShell,
IronPython, etc.) under mintty
or Cygwin's sshd
with
properly-functioning input (e.g. arrow and function keys) and output (e.g. line
buffering). The library could be also useful for writing a non-Cygwin SSH
server.
winpty runs on Windows XP through Windows 10, including server versions. It can be compiled into either 32-bit or 64-bit binaries.
winpty.exe
)You need the following to build winpty:
winpty.dll
and winpty-agent.exe
winpty.exe
Winpty requires two g++ toolchains as it is split into two parts. The
winpty.dll
and winpty-agent.exe
binaries interface with the native
Windows command prompt window so they are compiled with the native MinGW
toolchain. The winpty.exe
binary interfaces with the MSYS/Cygwin terminal so
it is compiled with the MSYS/Cygwin toolchain.
MinGW appears to be split into two distributions -- MinGW (creates 32-bit binaries) and MinGW-w64 (creates both 32-bit and 64-bit binaries). Either one is generally acceptable.
The default g++ compiler for Cygwin targets Cygwin itself, but Cygwin also packages MinGW-w64 compilers. As of this writing, the necessary packages are:
mingw64-i686-gcc-g++
or mingw64-x86_64-gcc-g++
. Select the
appropriate compiler for your CPU architecture.gcc-g++
make
As of this writing (2016-01-23), only the MinGW-w64 compiler is acceptable.
The MinGW compiler (e.g. from the mingw-gcc-g++
package) is no longer
maintained and is too buggy.
For the original MSYS, use the mingw-get
tool (MinGW Installation Manager),
and select at least these components:
mingw-developer-toolkit
mingw32-base
mingw32-gcc-g++
msys-base
msys-system-builder
When running ./configure
, make sure that mingw32-g++
is in your
PATH
. It will be in the C:\MinGW\bin
directory.
For MSYS2, use pacman
and install at least these packages:
msys/gcc
mingw32/mingw-w64-i686-gcc
or mingw64/mingw-w64-x86_64-gcc
. Select
the appropriate compiler for your CPU architecture.make
MSYS2 provides three start menu shortcuts for starting MSYS2:
To build winpty, use the MinGW-w64 {Win32,Win64} shortcut of the architecture
matching MSYS2. These shortcuts will put the g++ compiler from the
{mingw32,mingw64}/mingw-w64-{i686,x86_64}-gcc
packages into the PATH
.
Alternatively, instead of installing mingw32/mingw-w64-i686-gcc
or
mingw64/mingw-w64-x86_64-gcc
, install the mingw-w64-cross-gcc
and
mingw-w64-cross-crt-git
packages. These packages install cross-compilers
into /opt/bin
, and then any of the three shortcuts will work.
In the project directory, run ./configure
, then make
, then make install
.
By default, winpty is installed into /usr/local
. Pass PREFIX=<path>
to
make install
to override this default.
To run a Windows console program in mintty
or Cygwin sshd
, prepend
winpty
to the command-line:
$ winpty powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\rprichard\proj\winpty> 10 + 20
30
PS C:\rprichard\proj\winpty> exit
See src/include/winpty.h
for the prototypes of functions exported by
winpty.dll
.
Only the winpty.exe
binary uses Cygwin; all the other binaries work without
it and can be compiled with either MinGW or MSVC. To compile using MSVC,
download gyp and run gyp -I configurations.gypi
in the src
subdirectory.
This will generate a winpty.sln
and associated project files. See the
src/winpty.gyp
and src/configurations.gypi
files for notes on dealing with
MSVC versions and different architectures.
Compiling winpty with MSVC currently requires MSVC 2013 or newer.
winpty comes with a tool for collecting timestamped debugging output. To use it:
winpty-debugserver.exe
on the same computer as winpty.WINPTY_DEBUG
environment variable to trace
for the
winpty.exe
process and/or the process using libwinpty.dll
.winpty also recognizes a WINPTY_SHOW_CONSOLE
environment variable. Set it
to 1 to prevent winpty from hiding the console window.
This project is distributed under the MIT license (see the LICENSE
file in
the project root).
By submitting a pull request for this project, you agree to license your contribution under the MIT license to this project.
FAQs
Fork pseudoterminals in Node.JS
The npm package node-pty receives a total of 109,891 weekly downloads. As such, node-pty popularity was classified as popular.
We found that node-pty demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.