A high-performance Windows ConPTY backend implemented in Delphi, exposed to Node.js through a native N-API addon (C++).
This project provides a stable, future-proof alternative to ffi-napi, fully compatible with Node.js 22+.
It allows Node.js applications to spawn and control Windows pseudo-terminals (ConPTY) with:
- real-time data streaming
- resize support
- environment variable injection
- working directory control
- exit notifications
- error callbacks
- full UTF-8 support
The backend is implemented in Delphi for maximum performance and reliability, while the Node.js side uses a modern N-API addon to ensure ABI stability across future Node versions.
- Native Windows ConPTY (CreatePseudoConsole / ResizePseudoConsole)
- Delphi DLL backend for stable Windows API interaction
- Node.js N-API addon (C++), no more ffi-napi
- Works on Node.js 18, 20, 22, 23...
- Thread-safe callbacks (data, exit, error)
- Zero-copy streaming from Delphi to Node
- Simple JavaScript API
DelphiPty.dll <- Delphi ConPTY backend
addon.cpp <- N-API C++ addon (Node.js binding)
binding.gyp <- node-gyp build config
index.js <- JS wrapper around the addon
demo.js <- Example usage
package.json
PtyCore.pas <- Delphi implementation
DelphiPty.dpr/.dproj <- Delphi project files
Batch files:
BuildAddOn.bat
CleanAddOn.bat
InstallAddOn.bat
BuildDLL.bat
RunDemo.bat
- Windows 10 or 11 (ConPTY API required)
- Delphi 10.3+ (or any version with WinAPI + ConPTY support)
- Node.js 18 or higher
- Fully compatible with Node 22+
- Visual Studio Build Tools 2022
- Python 3.x
- node-gyp (installed automatically)
Open a terminal in:
M:\Delphi-Pty\Branch\Develop\Delphi
Then run:
npm install
npx node-gyp configure build
If successful, you will get:
build/Release/delphi_pty.node
This is the compiled native addon that Node.js loads.
The backend DLL must be compiled before Node.js can load it.
- Open
DelphiPty.dprojin Delphi. - Select Build ? Build All.
- The output file will be generated as:
DelphiPty.dll
Place the DLL next to demo.js or in a directory that the addon can load via LoadLibraryA.
This repository includes:
BuildDLL.bat
Running it will:
- Invoke the Delphi command-line compiler
- Build
DelphiPty.dll - Place it in the correct output directory
Usage:
BuildDLL.bat
| File | Purpose |
|---|---|
| BuildAddOn.bat | Builds the Node.js N-API addon (delphi_pty.node) |
| CleanAddOn.bat | Removes build/ and other generated addon files |
| InstallAddOn.bat | Runs npm install and prepares the addon environment |
| BuildDLL.bat | Compiles the Delphi backend DLL (DelphiPty.dll) |
| RunDemo.bat | Runs the demo (node demo.js) |
These scripts are optional but help maintain a clean, repeatable workflow.
When you build the addon, node-gyp produces:
build/Release/delphi_pty.node
This is the compiled N-API module that Node.js loads at runtime.
If your index.js loads it like:
const addon = require('./build/Release/delphi_pty.node');…then nothing else is needed.
If you prefer a flatter structure, copy it manually or run:
InstallAddOn.bat
Then load it like:
const addon = require('./delphi_pty.node');delphi_pty.nodemust be in the correct locationDelphiPty.dllmust be in the same directory or in the system PATH- Node.js architecture must match the DLL (x64 recommended)
If anything is missing, Node will throw:
Error: The specified module could not be found.
If you see errors like:
error C1083: Cannot open include file: 'napi.h'
then perform a full clean reinstall:
rmdir /s /q node_modules
del package-lock.json
npm install
npx node-gyp rebuild
rmdir /s /q C:\Users\<YOUR_USERNAME>\AppData\Local\node-gyp
This forces node-gyp to download fresh headers.
node demo.js
Or simply:
RunDemo.bat
You should see:
- ConPTY output from cmd.exe
- Directory listings
- Exit codes
- Real-time streaming
const { createPty } = require('./index');
const pty = createPty({
command: 'cmd.exe',
args: [],
cols: 120,
rows: 40,
onData: chunk => process.stdout.write(chunk),
onExit: code => console.log("Exited:", code),
onError: (code, msg) => console.error("Error:", code, msg),
});
pty.write("dir\r\n");- Implements ConPTY using Windows API
- Manages pipes, process creation, callbacks
- Exports a clean C interface via stdcall
- Loads the DLL using LoadLibraryA
- Resolves function pointers
- Wraps everything in N-API functions
- Uses ThreadSafeFunction for async callbacks
- Exposes a simple JS API
This architecture is future-proof because N-API guarantees ABI stability across Node versions.
Because:
- ffi-napi depends on libffi
- libffi breaks on Node 22+
- ffi-napi is no longer actively maintained
- Node 22 introduced a new ABI
- N-API addons remain stable forever
This project removes all those issues.
- Add PowerShell support
- Add UTF-16 passthrough mode
- Add binary mode for raw terminal streams
- Publish as an npm package