Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConPTY Support #236

Merged
merged 56 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
c84d987
Begin prototyping with conpty support
zadjii-msft Aug 15, 2018
b10d40d
General cleanup
zadjii-msft Aug 16, 2018
671e038
Add Windows SDK install instructions
Tyriar Aug 22, 2018
1b6ab0b
Fix npm test (temporary)
Tyriar Aug 22, 2018
7e6c4a7
Close conpty on PtyKill
Tyriar Aug 22, 2018
46ea91f
Properly clean up process in fork example
Tyriar Aug 22, 2018
d6bf31f
Handle resize for conpty
Tyriar Aug 22, 2018
43fffd4
Comment out most winpty code
Tyriar Oct 8, 2018
714f84d
Refactors, move code to winpty.cc and conpty.cc
Tyriar Oct 11, 2018
1abbe94
Add useConpty option
Tyriar Oct 12, 2018
705a26e
Set pid in conpty
Tyriar Oct 12, 2018
25218d6
Set innerPid, not pid
Tyriar Oct 12, 2018
38e17cb
Generate random pipe names
Tyriar Oct 15, 2018
fda8529
Fix type narrowing warning
Tyriar Oct 15, 2018
e7f9fc3
Shuffle args around
Tyriar Oct 15, 2018
942ac3f
Pull in environment to conpty
Tyriar Oct 15, 2018
df76478
Add env to example
Tyriar Oct 15, 2018
4079cfe
Add env test
Tyriar Oct 15, 2018
290f92a
Fix environment
Tyriar Oct 17, 2018
61ed2cc
Store handles in a vector
Tyriar Oct 17, 2018
0c9f574
Use vector of handles
Tyriar Oct 17, 2018
1be58b7
Support cmdline, improve error handling
Tyriar Oct 17, 2018
ba6d466
Support cwd
Tyriar Oct 17, 2018
79ea707
Fix build for winpty
Tyriar Oct 17, 2018
ee7b2db
Build winpty as well
Tyriar Oct 17, 2018
d78711f
Use release build
Tyriar Oct 17, 2018
8ecb80c
Fix handling of size type
Tyriar Oct 17, 2018
de6a767
Allow use conpty option to work beyond first terminal
Tyriar Oct 18, 2018
dc0922c
Fix fault in conpty.kill
Tyriar Oct 18, 2018
4d18161
Remove old gyp definition
Tyriar Oct 18, 2018
34569c5
Load correct build type in unix terminal
Tyriar Oct 19, 2018
3c97754
Use release, fallback to debug
Tyriar Oct 19, 2018
bc59495
Merge remote-tracking branch 'origin/master' into dev/migrie/f/conpty
Tyriar Oct 19, 2018
21ab08d
Remove goto from conpty
Tyriar Oct 19, 2018
a0af090
Fix throwing node exceptions
Tyriar Oct 19, 2018
d9d44ea
Remove Terminal.redraw
Tyriar Dec 19, 2018
c81796e
Resolve TODO
Tyriar Dec 19, 2018
ee82095
Revert "Resolve TODO"
Tyriar Dec 19, 2018
c2c4636
Clean up
Tyriar Dec 19, 2018
b0d8f5a
Temp working callback
Tyriar Dec 20, 2018
f0183e0
Why u no work?
Tyriar Dec 20, 2018
85b7ea6
Disable DOM APIs in tsconfig.json
Tyriar Dec 21, 2018
e4c294a
Fixed all tests in conpty
Tyriar Dec 21, 2018
772de9c
Types wip
Tyriar Dec 21, 2018
1a3ce77
Type Windows native components
Tyriar Dec 21, 2018
70df397
Clean up
Tyriar Dec 21, 2018
0284d42
Call UnregisterWait
Tyriar Dec 21, 2018
57dbb1c
Remove TODO
Tyriar Dec 21, 2018
dfb319c
Fix lint
Tyriar Dec 21, 2018
4281739
Mention conpty in readme
Tyriar Dec 21, 2018
d4c69c3
Add unix native typings
Tyriar Dec 21, 2018
2d3c8de
Fix license
Tyriar Dec 21, 2018
7125d38
Elaborate on experimentalUseConpty setting
Tyriar Dec 21, 2018
6dcdf5e
Ignore vscode workspace settings
Tyriar Dec 21, 2018
6849180
Use stricter tsc options
Tyriar Dec 21, 2018
44640eb
Only allow conpty to be used on 17692+, even when set to true
Tyriar Dec 21, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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