Skip to content

Commit

Permalink
Merge pull request #236 from Microsoft/dev/migrie/f/conpty
Browse files Browse the repository at this point in the history
ConPTY Support
  • Loading branch information
Tyriar authored Dec 21, 2018
2 parents 53f28c6 + 44640eb commit cce934f
Show file tree
Hide file tree
Showing 27 changed files with 795 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ builderror.log
lib/
npm-debug.log
package-lock.json
fixtures/space folder/
fixtures/space folder/
.vscode/settings.json
28 changes: 27 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,30 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.



MIT License

Copyright (c) 2018 - present Microsoft Corporation

All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is useful for:
- Writing a terminal emulator (eg. via [xterm.js](https://github.com/sourcelair/xterm.js)).
- Getting certain programs to *think* you're a terminal, such as when you need a program to send you control sequences.

`node-pty` supports Linux, macOS and Windows. Windows support is possible by utilizing the [winpty](https://github.com/rprichard/winpty) library.
`node-pty` supports Linux, macOS and Windows. Windows support is possible by utilizing the [Windows conpty API](https://blogs.msdn.microsoft.com/commandline/2018/08/02/windows-command-line-introducing-the-windows-pseudo-console-conpty/) on Windows 1809+ and the [winpty](https://github.com/rprichard/winpty) library in older version.

## Real-world Uses

Expand Down Expand Up @@ -67,6 +67,8 @@ npm run tsc
npm install --global --production windows-build-tools
```

The Windows SDK is also needed which can be [downloaded here](https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk). Only the "Desktop C++ Apps" components are needed to be installed.

## Debugging

[The wiki](https://github.com/Microsoft/node-pty/wiki/Debugging) contains instructions for debugging node-pty.
Expand Down Expand Up @@ -94,4 +96,5 @@ This project is forked from [chjj/pty.js](https://github.com/chjj/pty.js) with t
## License

Copyright (c) 2012-2015, Christopher Jeffrey (MIT License).<br>
Copyright (c) 2016, Daniel Imms (MIT License).
Copyright (c) 2016, Daniel Imms (MIT License).<br>
Copyright (c) 2018, Microsoft Corporation (MIT License).
107 changes: 64 additions & 43 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
{
'targets': [{
'target_name': 'pty',
'include_dirs' : [
'<!(node -e "require(\'nan\')")'
],
'conditions': [
['OS=="win"', {
# "I disabled those warnings because of winpty" - @peters (GH-40)
'msvs_disabled_warnings': [ 4506, 4530 ],
'conditions': [
['OS=="win"', {
'targets': [
{
'target_name': 'conpty',
'include_dirs' : [
'<!(node -e "require(\'nan\')")'
],
'sources' : [
'src/win/conpty.cc',
'src/win/path_util.cc'
],
'libraries': [
'shlwapi.lib'
]
},
{
'target_name': 'pty',
'include_dirs' : [
'<!(node -e "require(\'nan\')")',
'deps/winpty/src/include',
],
# Disabled due to winpty
'msvs_disabled_warnings': [ 4506, 4530 ],
'dependencies' : [
'deps/winpty/src/winpty.gyp:winpty-agent',
'deps/winpty/src/winpty.gyp:winpty',
],
'sources' : [
'src/win/winpty.cc',
'src/win/path_util.cc'
],
'libraries': [
'shlwapi.lib'
],
}
]
}, { # OS!="win"
'targets': [{
'target_name': 'pty',
'include_dirs' : [
'deps/winpty/src/include',
'<!(node -e "require(\'nan\')")'
],
'dependencies' : [
'deps/winpty/src/winpty.gyp:winpty-agent',
'deps/winpty/src/winpty.gyp:winpty',
],
'sources' : [
'src/win/pty.cc',
'src/win/path_util.cc'
],
'libraries': [
'shlwapi.lib'
],
}, { # OS!="win"
'sources': [
'src/unix/pty.cc'
],
Expand All @@ -31,27 +50,29 @@
'-L/usr/lib',
'-L/usr/local/lib'
],
}],
# http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html
# One some systems (at least including Cygwin, Interix,
# OSF/1 4 and 5, and Mac OS X) linking with -lutil is not required.
['OS=="mac" or OS=="solaris"', {
'libraries!': [
'-lutil'
'conditions': [
# http://www.gnu.org/software/gnulib/manual/html_node/forkpty.html
# One some systems (at least including Cygwin, Interix,
# OSF/1 4 and 5, and Mac OS X) linking with -lutil is not required.
['OS=="mac" or OS=="solaris"', {
'libraries!': [
'-lutil'
]
}],
['OS=="mac"', {
"xcode_settings": {
"OTHER_CPLUSPLUSFLAGS": [
"-std=c++11",
"-stdlib=libc++"
],
"OTHER_LDFLAGS": [
"-stdlib=libc++"
],
"MACOSX_DEPLOYMENT_TARGET":"10.7"
}
}]
]
}],
['OS=="mac"', {
"xcode_settings": {
"OTHER_CPLUSPLUSFLAGS": [
"-std=c++11",
"-stdlib=libc++"
],
"OTHER_LDFLAGS": [
"-stdlib=libc++"
],
"MACOSX_DEPLOYMENT_TARGET":"10.7"
}
}]
]
}]
}]
]
}
30 changes: 20 additions & 10 deletions examples/fork/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ var pty = require('../..');
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';

var ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
name: 'xterm-256color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
rows: 26,
cwd: os.platform() === 'win32' ? process.env.USERPROFILE : process.env.HOME,
env: Object.assign({ TEST: "abc" }, process.env),
experimentalUseConpty: true
});

ptyProcess.on('data', function(data) {
console.log(data);
// console.log(data);
process.stdout.write(data);
});

ptyProcess.write('ls\r');
ptyProcess.resize(100, 40);
ptyProcess.write('ls\r');
ptyProcess.write('dir\r');
// ptyProcess.write('ls\r');

setTimeout(() => {
ptyProcess.kill()
}, 5000);
ptyProcess.resize(30, 19);
ptyProcess.write(shell === 'powershell.exe' ? '$Env:TEST\r' : 'echo %TEST%\r');
}, 2000);

process.on('exit', () => {
ptyProcess.kill();
});

setTimeout(() => {
process.exit();
}, 4000);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "node-pty",
"description": "Fork pseudoterminals in Node.JS",
"author": {
"name": "Daniel Imms"
"name": "Microsoft Corporation"
},
"version": "0.7.8",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion scripts/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const os = require('os');
const path = require('path');
const spawn = require('child_process').spawn;

const p = spawn(os.platform() === 'win32' ? 'node-gyp.cmd' : 'node-gyp', ['rebuild'], {
const args = ['rebuild'];
if (process.env.NODE_PTY_DEBUG) {
args.push('--debug');
}
const p = spawn(os.platform() === 'win32' ? 'node-gyp.cmd' : 'node-gyp', args, {
cwd: path.join(__dirname, '..'),
stdio: 'inherit'
});
Expand Down
2 changes: 2 additions & 0 deletions scripts/post-install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var path = require('path');

var RELEASE_DIR = path.join(__dirname, '..', 'build', 'Release');
var BUILD_FILES = [
path.join(RELEASE_DIR, 'conpty.node'),
path.join(RELEASE_DIR, 'conpty.pdb'),
path.join(RELEASE_DIR, 'pty.node'),
path.join(RELEASE_DIR, 'pty.pdb'),
path.join(RELEASE_DIR, 'winpty-agent.exe'),
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright (c) 2012-2015, Christopher Jeffrey, Peter Sunde (MIT License)
* Copyright (c) 2016, Daniel Imms (MIT License).
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

import * as path from 'path';
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2016, Daniel Imms (MIT License).
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

import * as net from 'net';
Expand Down Expand Up @@ -115,6 +116,7 @@ export interface IPtyForkOptions {
uid?: number;
gid?: number;
encoding?: string;
experimentalUseConpty?: boolean | undefined;
}

export interface IPtyOpenOptions {
Expand Down
54 changes: 54 additions & 0 deletions src/native.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

interface IConptyNative {
startProcess(file: string, cols: number, rows: number, debug: boolean, pipeName: string): IConptyProcess;
connect(ptyId: number, commandLine: string, cwd: string, env: string[], onExitCallback: (exitCode: number) => void): { pid: number };
resize(ptyId: number, cols: number, rows: number): void;
kill(ptyId: number): void;
}

interface IWinptyNative {
startProcess(file: string, commandLine: string, env: string[], cwd: string, cols: number, rows: number, debug: boolean): IWinptyProcess;
resize(processHandle: number, cols: number, rows: number): void;
kill(pid: number, innerPidHandle: number): void;
getProcessList(pid: number): number[];
getExitCode(innerPidHandle: number): number;
}

interface IUnixNative {
fork(file: string, args: string[], parsedEnv: string[], cwd: string, cols: number, rows: number, uid: number, gid: number, useUtf8: boolean, onExitCallback: (code: number, signal: number) => void): IUnixProcess;
open(cols: number, rows: number): IUnixOpenProcess;
process(fd: number, pty: string): string;
resize(fd: number, cols: number, rows: number): void;
}

interface IConptyProcess {
pty: number;
fd: number;
conin: string;
conout: string;
}

interface IWinptyProcess {
pty: number;
fd: number;
conin: string;
conout: string;
pid: number;
innerPid: number;
innerPidHandle: number;
}

interface IUnixProcess {
fd: number;
pid: number;
pty: string;
}

interface IUnixOpenProcess {
master: number;
slave: number;
pty: string;
}
1 change: 1 addition & 0 deletions src/terminal.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2017, Daniel Imms (MIT License).
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

import * as assert from 'assert';
Expand Down
14 changes: 1 addition & 13 deletions src/terminal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Copyright (c) 2012-2015, Christopher Jeffrey (MIT License)
* Copyright (c) 2016, Daniel Imms (MIT License).
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

import * as path from 'path';
Expand Down Expand Up @@ -126,19 +127,6 @@ export abstract class Terminal implements ITerminal {
public abstract get master(): Socket;
public abstract get slave(): Socket;

// TODO: Should this be in the API?
public redraw(): void {
let cols = this._cols;
let rows = this._rows;

// We could just send SIGWINCH, but most programs will ignore it if the
// size hasn't actually changed.

this.resize(cols + 1, rows + 1);

setTimeout(() => this.resize(cols, rows), 30);
}

protected _close(): void {
this._socket.writable = false;
this._socket.readable = false;
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2017, Daniel Imms (MIT License).
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

export type ArgvOrCommandLine = string[] | string;
1 change: 1 addition & 0 deletions src/unixTerminal.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* Copyright (c) 2017, Daniel Imms (MIT License).
* Copyright (c) 2018, Microsoft Corporation (MIT License).
*/

import { UnixTerminal } from './unixTerminal';
Expand Down
Loading

0 comments on commit cce934f

Please sign in to comment.