Socket
Socket
Sign inDemoInstall

node-pty

Package Overview
Dependencies
Maintainers
3
Versions
164
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-pty - npm Package Compare versions

Comparing version 0.10.0-beta1 to 0.10.0-beta10

src/tsconfig.json

3

lib/eventEmitter2.js

@@ -6,2 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.EventEmitter2 = void 0;
var EventEmitter2 = /** @class */ (function () {

@@ -32,3 +33,3 @@ function EventEmitter2() {

},
enumerable: true,
enumerable: false,
configurable: true

@@ -35,0 +36,0 @@ });

@@ -8,2 +8,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.native = exports.open = exports.createTerminal = exports.fork = exports.spawn = void 0;
var terminalCtor;

@@ -10,0 +11,0 @@ if (process.platform === 'win32') {

@@ -8,2 +8,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.Terminal = exports.DEFAULT_ROWS = exports.DEFAULT_COLS = void 0;
var events_1 = require("events");

@@ -31,10 +32,10 @@ var eventEmitter2_1 = require("./eventEmitter2");

// types go through to the C++ side it can lead to hard to diagnose exceptions.
this._checkType('name', opt.name ? opt.name : null, 'string');
this._checkType('cols', opt.cols ? opt.cols : null, 'number');
this._checkType('rows', opt.rows ? opt.rows : null, 'number');
this._checkType('cwd', opt.cwd ? opt.cwd : null, 'string');
this._checkType('env', opt.env ? opt.env : null, 'object');
this._checkType('uid', opt.uid ? opt.uid : null, 'number');
this._checkType('gid', opt.gid ? opt.gid : null, 'number');
this._checkType('encoding', opt.encoding ? opt.encoding : null, 'string');
this._checkType('name', opt.name ? opt.name : undefined, 'string');
this._checkType('cols', opt.cols ? opt.cols : undefined, 'number');
this._checkType('rows', opt.rows ? opt.rows : undefined, 'number');
this._checkType('cwd', opt.cwd ? opt.cwd : undefined, 'string');
this._checkType('env', opt.env ? opt.env : undefined, 'object');
this._checkType('uid', opt.uid ? opt.uid : undefined, 'number');
this._checkType('gid', opt.gid ? opt.gid : undefined, 'number');
this._checkType('encoding', opt.encoding ? opt.encoding : undefined, 'string');
// setup flow control handling

@@ -47,3 +48,3 @@ this.handleFlowControl = !!(opt.handleFlowControl);

get: function () { return this._onData.event; },
enumerable: true,
enumerable: false,
configurable: true

@@ -53,3 +54,3 @@ });

get: function () { return this._onExit.event; },
enumerable: true,
enumerable: false,
configurable: true

@@ -59,3 +60,3 @@ });

get: function () { return this._pid; },
enumerable: true,
enumerable: false,
configurable: true

@@ -65,3 +66,3 @@ });

get: function () { return this._cols; },
enumerable: true,
enumerable: false,
configurable: true

@@ -71,3 +72,3 @@ });

get: function () { return this._rows; },
enumerable: true,
enumerable: false,
configurable: true

@@ -95,4 +96,18 @@ });

};
Terminal.prototype._checkType = function (name, value, type) {
if (value && typeof value !== type) {
Terminal.prototype._checkType = function (name, value, type, allowArray) {
if (allowArray === void 0) { allowArray = false; }
if (value === undefined) {
return;
}
if (allowArray) {
if (Array.isArray(value)) {
value.forEach(function (v, i) {
if (typeof v !== type) {
throw new Error(name + "[" + i + "] must be a " + type + " (not a " + typeof v[i] + ")");
}
});
return;
}
}
if (typeof value !== type) {
throw new Error(name + " must be a " + type + " (not a " + typeof value + ")");

@@ -99,0 +114,0 @@ }

@@ -6,2 +6,15 @@ "use strict";

*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });

@@ -11,2 +24,3 @@ var assert = require("assert");

var unixTerminal_1 = require("./unixTerminal");
var terminal_1 = require("./terminal");
var terminalConstructor = (process.platform === 'win32') ? windowsTerminal_1.WindowsTerminal : unixTerminal_1.UnixTerminal;

@@ -21,2 +35,46 @@ var SHELL = (process.platform === 'win32') ? 'cmd.exe' : '/bin/bash';

}
var TestTerminal = /** @class */ (function (_super) {
__extends(TestTerminal, _super);
function TestTerminal() {
return _super !== null && _super.apply(this, arguments) || this;
}
TestTerminal.prototype.checkType = function (name, value, type, allowArray) {
if (allowArray === void 0) { allowArray = false; }
this._checkType(name, value, type, allowArray);
};
TestTerminal.prototype._write = function (data) {
throw new Error('Method not implemented.');
};
TestTerminal.prototype.resize = function (cols, rows) {
throw new Error('Method not implemented.');
};
TestTerminal.prototype.destroy = function () {
throw new Error('Method not implemented.');
};
TestTerminal.prototype.kill = function (signal) {
throw new Error('Method not implemented.');
};
Object.defineProperty(TestTerminal.prototype, "process", {
get: function () {
throw new Error('Method not implemented.');
},
enumerable: false,
configurable: true
});
Object.defineProperty(TestTerminal.prototype, "master", {
get: function () {
throw new Error('Method not implemented.');
},
enumerable: false,
configurable: true
});
Object.defineProperty(TestTerminal.prototype, "slave", {
get: function () {
throw new Error('Method not implemented.');
},
enumerable: false,
configurable: true
});
return TestTerminal;
}(terminal_1.Terminal));
describe('Terminal', function () {

@@ -28,2 +86,22 @@ describe('constructor', function () {

});
describe('checkType', function () {
it('should throw for the wrong type', function () {
var t = new TestTerminal();
assert.doesNotThrow(function () { return t.checkType('foo', 'test', 'string'); });
assert.doesNotThrow(function () { return t.checkType('foo', 1, 'number'); });
assert.doesNotThrow(function () { return t.checkType('foo', {}, 'object'); });
assert.throws(function () { return t.checkType('foo', 'test', 'number'); });
assert.throws(function () { return t.checkType('foo', 1, 'object'); });
assert.throws(function () { return t.checkType('foo', {}, 'string'); });
});
it('should throw for wrong types within arrays', function () {
var t = new TestTerminal();
assert.doesNotThrow(function () { return t.checkType('foo', ['test'], 'string', true); });
assert.doesNotThrow(function () { return t.checkType('foo', [1], 'number', true); });
assert.doesNotThrow(function () { return t.checkType('foo', [{}], 'object', true); });
assert.throws(function () { return t.checkType('foo', ['test'], 'number', true); });
assert.throws(function () { return t.checkType('foo', [1], 'object', true); });
assert.throws(function () { return t.checkType('foo', [{}], 'string', true); });
});
});
describe('automatic flow control', function () {

@@ -30,0 +108,0 @@ it('should respect ctor flow control options', function () {

@@ -6,2 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.pollUntil = void 0;
function pollUntil(cb, timeout, interval) {

@@ -8,0 +9,0 @@ return new Promise(function (resolve, reject) {

@@ -16,2 +16,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.UnixTerminal = void 0;
/**

@@ -63,2 +64,3 @@ * Copyright (c) 2012-2015, Christopher Jeffrey (MIT License)

var cwd = opt.cwd || process.cwd();
env.PWD = cwd;
var name = opt.name || env.TERM || DEFAULT_NAME;

@@ -149,3 +151,3 @@ env.TERM = name;

get: function () { return this._master; },
enumerable: true,
enumerable: false,
configurable: true

@@ -155,3 +157,3 @@ });

get: function () { return this._slave; },
enumerable: true,
enumerable: false,
configurable: true

@@ -231,3 +233,3 @@ });

},
enumerable: true,
enumerable: false,
configurable: true

@@ -272,5 +274,5 @@ });

var _this = this;
var _a = process.binding('pipe_wrap'), Pipe = _a.Pipe, constants = _a.constants; // tslint:disable-line
var pipeWrap = process.binding('pipe_wrap'); // tslint:disable-line
// @types/node has fd as string? https://github.com/DefinitelyTyped/DefinitelyTyped/pull/18275
var handle = new Pipe(constants.SOCKET);
var handle = new pipeWrap.Pipe(pipeWrap.constants.SOCKET);
handle.open(fd);

@@ -277,0 +279,0 @@ _this = _super.call(this, { handle: handle }) || this;

@@ -9,2 +9,3 @@ "use strict";

var assert = require("assert");
var cp = require("child_process");
var path = require("path");

@@ -78,3 +79,2 @@ var testUtils_test_1 = require("./testUtils.test");

it('should open a pty with access to a master and slave socket', function (done) {
var doneCalled = false;
term = unixTerminal_1.UnixTerminal.open({});

@@ -100,4 +100,105 @@ var slavebuf = '';

});
describe('signals in parent and child', function () {
it('SIGINT - custom in parent and child', function (done) {
// this test is cumbersome - we have to run it in a sub process to
// see behavior of SIGINT handlers
var data = "\n var pty = require('./lib/index');\n process.on('SIGINT', () => console.log('SIGINT in parent'));\n var ptyProcess = pty.spawn('node', ['-e', 'process.on(\"SIGINT\", ()=>console.log(\"SIGINT in child\"));setTimeout(() => null, 300);'], {\n name: 'xterm-color',\n cols: 80,\n rows: 30,\n cwd: process.env.HOME,\n env: process.env\n });\n ptyProcess.on('data', function (data) {\n console.log(data);\n });\n setTimeout(() => null, 500);\n console.log('ready', ptyProcess.pid);\n ";
var buffer = [];
var p = cp.spawn('node', ['-e', data]);
var sub = '';
p.stdout.on('data', function (data) {
if (!data.toString().indexOf('ready')) {
sub = data.toString().split(' ')[1].slice(0, -1);
setTimeout(function () {
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child
p.kill('SIGINT'); // SIGINT to parent
}, 200);
}
else {
buffer.push(data.toString().replace(/^\s+|\s+$/g, ''));
}
});
p.on('close', function () {
// handlers in parent and child should have been triggered
assert.equal(buffer.indexOf('SIGINT in child') !== -1, true);
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true);
done();
});
});
it('SIGINT - custom in parent, default in child', function (done) {
// this tests the original idea of the signal(...) change in pty.cc:
// to make sure the SIGINT handler of a pty child is reset to default
// and does not interfere with the handler in the parent
var data = "\n var pty = require('./lib/index');\n process.on('SIGINT', () => console.log('SIGINT in parent'));\n var ptyProcess = pty.spawn('node', ['-e', 'setTimeout(() => console.log(\"should not be printed\"), 300);'], {\n name: 'xterm-color',\n cols: 80,\n rows: 30,\n cwd: process.env.HOME,\n env: process.env\n });\n ptyProcess.on('data', function (data) {\n console.log(data);\n });\n setTimeout(() => null, 500);\n console.log('ready', ptyProcess.pid);\n ";
var buffer = [];
var p = cp.spawn('node', ['-e', data]);
var sub = '';
p.stdout.on('data', function (data) {
if (!data.toString().indexOf('ready')) {
sub = data.toString().split(' ')[1].slice(0, -1);
setTimeout(function () {
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child
p.kill('SIGINT'); // SIGINT to parent
}, 200);
}
else {
buffer.push(data.toString().replace(/^\s+|\s+$/g, ''));
}
});
p.on('close', function () {
// handlers in parent and child should have been triggered
assert.equal(buffer.indexOf('should not be printed') !== -1, false);
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true);
done();
});
});
it('SIGHUP default (child only)', function (done) {
var term = new unixTerminal_1.UnixTerminal('node', ['-e', "\n console.log('ready');\n setTimeout(()=>console.log('timeout'), 200);"
]);
var buffer = '';
term.on('data', function (data) {
if (data === 'ready\r\n') {
term.kill();
}
else {
buffer += data;
}
});
term.on('exit', function () {
// no timeout in buffer
assert.equal(buffer, '');
done();
});
});
it('SIGUSR1 - custom in parent and child', function (done) {
var pHandlerCalled = 0;
var handleSigUsr = function (h) {
return function () {
pHandlerCalled += 1;
process.removeListener('SIGUSR1', h);
};
};
process.on('SIGUSR1', handleSigUsr(handleSigUsr));
var term = new unixTerminal_1.UnixTerminal('node', ['-e', "\n process.on('SIGUSR1', () => {\n console.log('SIGUSR1 in child');\n });\n console.log('ready');\n setTimeout(()=>null, 200);"
]);
var buffer = '';
term.on('data', function (data) {
if (data === 'ready\r\n') {
process.kill(process.pid, 'SIGUSR1');
term.kill('SIGUSR1');
}
else {
buffer += data;
}
});
term.on('exit', function () {
// should have called both handlers and only once
assert.equal(pHandlerCalled, 1);
assert.equal(buffer, 'SIGUSR1 in child\r\n');
done();
});
});
});
});
}
//# sourceMappingURL=unixTerminal.test.js.map

@@ -7,2 +7,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.assign = void 0;
function assign(target) {

@@ -9,0 +10,0 @@ var sources = [];

@@ -8,2 +8,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.argsToCommandLine = exports.WindowsPtyAgent = void 0;
var os = require("os");

@@ -107,3 +108,3 @@ var path = require("path");

get: function () { return this._inSocket; },
enumerable: true,
enumerable: false,
configurable: true

@@ -113,3 +114,3 @@ });

get: function () { return this._outSocket; },
enumerable: true,
enumerable: false,
configurable: true

@@ -119,3 +120,3 @@ });

get: function () { return this._fd; },
enumerable: true,
enumerable: false,
configurable: true

@@ -125,3 +126,3 @@ });

get: function () { return this._innerPid; },
enumerable: true,
enumerable: false,
configurable: true

@@ -131,3 +132,3 @@ });

get: function () { return this._pty; },
enumerable: true,
enumerable: false,
configurable: true

@@ -206,3 +207,3 @@ });

},
enumerable: true,
enumerable: false,
configurable: true

@@ -209,0 +210,0 @@ });

@@ -21,2 +21,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.WindowsTerminal = void 0;
var terminal_1 = require("./terminal");

@@ -31,2 +32,3 @@ var windowsPtyAgent_1 = require("./windowsPtyAgent");

var _this = _super.call(this, opt) || this;
_this._checkType('args', args, 'string', true);
// Initialize arguments

@@ -167,3 +169,3 @@ args = args || [];

get: function () { return this._name; },
enumerable: true,
enumerable: false,
configurable: true

@@ -173,3 +175,3 @@ });

get: function () { throw new Error('master is not supported on Windows'); },
enumerable: true,
enumerable: false,
configurable: true

@@ -179,3 +181,3 @@ });

get: function () { throw new Error('slave is not supported on Windows'); },
enumerable: true,
enumerable: false,
configurable: true

@@ -182,0 +184,0 @@ });

@@ -7,3 +7,3 @@ {

},
"version": "0.10.0-beta1",
"version": "0.10.0-beta10",
"license": "MIT",

@@ -37,10 +37,11 @@ "main": "./lib/index.js",

"scripts": {
"tsc": "tsc",
"watch": "tsc -w",
"lint": "tslint 'src/**/*.ts'",
"build": "tsc -b ./src/tsconfig.json",
"watch": "tsc -b -w ./src/tsconfig.json",
"lint": "eslint -c .eslintrc.js --ext .ts src/",
"install": "node scripts/install.js",
"postinstall": "node scripts/post-install.js",
"test": "cross-env NODE_ENV=test mocha -R spec --exit lib/*.test.js",
"prepare": "npm run tsc",
"prepublishOnly": "npm run tsc"
"posttest": "npm run lint",
"prepare": "npm run build",
"prepublishOnly": "npm run build"
},

@@ -51,11 +52,12 @@ "dependencies": {

"devDependencies": {
"@types/mocha": "^5.0.0",
"@types/mocha": "^7.0.2",
"@types/node": "8",
"@typescript-eslint/eslint-plugin": "^2.27.0",
"@typescript-eslint/parser": "^2.27.0",
"cross-env": "^5.1.4",
"mocha": "^5.0.5",
"eslint": "^6.8.0",
"mocha": "^7.1.1",
"ps-list": "^6.0.0",
"tslint": "^5.12.1",
"tslint-consistent-codestyle": "^1.15.0",
"typescript": "3.4"
"typescript": "^3.8.3"
}
}

@@ -24,7 +24,11 @@ # node-pty

- [FreeMAN](https://github.com/matthew-matvei/freeman) file manager
- [atom-xterm](https://atom.io/packages/atom-xterm) - Atom plugin for providing terminals inside your Atom workspace.
- [Termination](https://atom.io/packages/termination) - Another Atom plugin that provides terminals inside your Atom workspace.
- [electerm](https://github.com/electerm/electerm) Terminal/ssh/sftp client(linux, mac, win).
- [terminus](https://atom.io/packages/terminus) - An Atom plugin for providing terminals inside your Atom workspace.
- [x-terminal](https://atom.io/packages/x-terminal) - Also an Atom plugin that provides terminals inside your Atom workspace.
- [Termination](https://atom.io/packages/termination) - Also an Atom plugin that provides terminals inside your Atom workspace.
- [atom-xterm](https://atom.io/packages/atom-xterm) - Also an Atom plugin that provides terminals inside your Atom workspace.
- [electerm](https://github.com/electerm/electerm) Terminal/SSH/SFTP client(Linux, macOS, Windows).
- [Extraterm](http://extraterm.org/)
- [Wetty](https://github.com/krishnasrinivas/wetty) Browser based Terminal over HTTP and HTTPS
- [nomad](https://github.com/lukebarnard1/nomad-term)
- [DockerStacks](https://github.com/sfx101/docker-stacks) Local LAMP/LEMP stack using Docker

@@ -64,3 +68,3 @@ Do you use node-pty in your application as well? Please open a [Pull Request](https://github.com/Tyriar/node-pty/pulls) to include it here. We would love to have it in our list.

# Compile TypeScript -> JavaScript
npm run tsc
npm run build
```

@@ -80,2 +84,6 @@

### macOS
Xcode is needed to compile the sources, this can be installed from the App Store.
### Windows

@@ -82,0 +90,0 @@

@@ -9,3 +9,4 @@ /**

import { UnixTerminal } from './unixTerminal';
import { pollUntil } from './testUtils.test';
import { Terminal } from './terminal';
import { Socket } from 'net';

@@ -22,2 +23,28 @@ const terminalConstructor = (process.platform === 'win32') ? WindowsTerminal : UnixTerminal;

class TestTerminal extends Terminal {
public checkType<T>(name: string, value: T, type: string, allowArray: boolean = false): void {
this._checkType(name, value, type, allowArray);
}
protected _write(data: string): void {
throw new Error('Method not implemented.');
}
public resize(cols: number, rows: number): void {
throw new Error('Method not implemented.');
}
public destroy(): void {
throw new Error('Method not implemented.');
}
public kill(signal?: string): void {
throw new Error('Method not implemented.');
}
public get process(): string {
throw new Error('Method not implemented.');
}
public get master(): Socket {
throw new Error('Method not implemented.');
}
public get slave(): Socket {
throw new Error('Method not implemented.');
}
}

@@ -34,2 +61,25 @@ describe('Terminal', () => {

describe('checkType', () => {
it('should throw for the wrong type', () => {
const t = new TestTerminal();
assert.doesNotThrow(() => t.checkType('foo', 'test', 'string'));
assert.doesNotThrow(() => t.checkType('foo', 1, 'number'));
assert.doesNotThrow(() => t.checkType('foo', {}, 'object'));
assert.throws(() => t.checkType('foo', 'test', 'number'));
assert.throws(() => t.checkType('foo', 1, 'object'));
assert.throws(() => t.checkType('foo', {}, 'string'));
});
it('should throw for wrong types within arrays', () => {
const t = new TestTerminal();
assert.doesNotThrow(() => t.checkType('foo', ['test'], 'string', true));
assert.doesNotThrow(() => t.checkType('foo', [1], 'number', true));
assert.doesNotThrow(() => t.checkType('foo', [{}], 'object', true));
assert.throws(() => t.checkType('foo', ['test'], 'number', true));
assert.throws(() => t.checkType('foo', [1], 'object', true));
assert.throws(() => t.checkType('foo', [{}], 'string', true));
});
});
describe('automatic flow control', () => {

@@ -36,0 +86,0 @@ it('should respect ctor flow control options', () => {

@@ -62,10 +62,10 @@ /**

// types go through to the C++ side it can lead to hard to diagnose exceptions.
this._checkType('name', opt.name ? opt.name : null, 'string');
this._checkType('cols', opt.cols ? opt.cols : null, 'number');
this._checkType('rows', opt.rows ? opt.rows : null, 'number');
this._checkType('cwd', opt.cwd ? opt.cwd : null, 'string');
this._checkType('env', opt.env ? opt.env : null, 'object');
this._checkType('uid', opt.uid ? opt.uid : null, 'number');
this._checkType('gid', opt.gid ? opt.gid : null, 'number');
this._checkType('encoding', opt.encoding ? opt.encoding : null, 'string');
this._checkType('name', opt.name ? opt.name : undefined, 'string');
this._checkType('cols', opt.cols ? opt.cols : undefined, 'number');
this._checkType('rows', opt.rows ? opt.rows : undefined, 'number');
this._checkType('cwd', opt.cwd ? opt.cwd : undefined, 'string');
this._checkType('env', opt.env ? opt.env : undefined, 'object');
this._checkType('uid', opt.uid ? opt.uid : undefined, 'number');
this._checkType('gid', opt.gid ? opt.gid : undefined, 'number');
this._checkType('encoding', opt.encoding ? opt.encoding : undefined, 'string');

@@ -101,4 +101,17 @@ // setup flow control handling

private _checkType(name: string, value: any, type: string): void {
if (value && typeof value !== type) {
protected _checkType<T>(name: string, value: T | undefined, type: string, allowArray: boolean = false): void {
if (value === undefined) {
return;
}
if (allowArray) {
if (Array.isArray(value)) {
value.forEach((v, i) => {
if (typeof v !== type) {
throw new Error(`${name}[${i}] must be a ${type} (not a ${typeof v[i]})`);
}
});
return;
}
}
if (typeof value !== type) {
throw new Error(`${name} must be a ${type} (not a ${typeof value})`);

@@ -130,4 +143,4 @@ }

public setEncoding(encoding: string | null): void {
if ((<any>this._socket)._decoder) {
delete (<any>this._socket)._decoder;
if ((this._socket as any)._decoder) {
delete (this._socket as any)._decoder;
}

@@ -134,0 +147,0 @@ if (encoding) {

@@ -8,2 +8,3 @@ /**

import * as assert from 'assert';
import * as cp from 'child_process';
import * as path from 'path';

@@ -19,3 +20,3 @@ import { pollUntil } from './testUtils.test';

const term = new UnixTerminal('/bin/bash', [], {});
let regExp;
let regExp: RegExp;
if (process.platform === 'linux') {

@@ -84,3 +85,2 @@ // https://linux.die.net/man/4/pts

it('should open a pty with access to a master and slave socket', (done) => {
let doneCalled = false;
term = UnixTerminal.open({});

@@ -110,3 +110,138 @@

});
describe('signals in parent and child', () => {
it('SIGINT - custom in parent and child', done => {
// this test is cumbersome - we have to run it in a sub process to
// see behavior of SIGINT handlers
const data = `
var pty = require('./lib/index');
process.on('SIGINT', () => console.log('SIGINT in parent'));
var ptyProcess = pty.spawn('node', ['-e', 'process.on("SIGINT", ()=>console.log("SIGINT in child"));setTimeout(() => null, 300);'], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.on('data', function (data) {
console.log(data);
});
setTimeout(() => null, 500);
console.log('ready', ptyProcess.pid);
`;
const buffer: string[] = [];
const p = cp.spawn('node', ['-e', data]);
let sub = '';
p.stdout.on('data', (data) => {
if (!data.toString().indexOf('ready')) {
sub = data.toString().split(' ')[1].slice(0, -1);
setTimeout(() => {
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child
p.kill('SIGINT'); // SIGINT to parent
}, 200);
} else {
buffer.push(data.toString().replace(/^\s+|\s+$/g, ''));
}
});
p.on('close', () => {
// handlers in parent and child should have been triggered
assert.equal(buffer.indexOf('SIGINT in child') !== -1, true);
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true);
done();
});
});
it('SIGINT - custom in parent, default in child', done => {
// this tests the original idea of the signal(...) change in pty.cc:
// to make sure the SIGINT handler of a pty child is reset to default
// and does not interfere with the handler in the parent
const data = `
var pty = require('./lib/index');
process.on('SIGINT', () => console.log('SIGINT in parent'));
var ptyProcess = pty.spawn('node', ['-e', 'setTimeout(() => console.log("should not be printed"), 300);'], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env
});
ptyProcess.on('data', function (data) {
console.log(data);
});
setTimeout(() => null, 500);
console.log('ready', ptyProcess.pid);
`;
const buffer: string[] = [];
const p = cp.spawn('node', ['-e', data]);
let sub = '';
p.stdout.on('data', (data) => {
if (!data.toString().indexOf('ready')) {
sub = data.toString().split(' ')[1].slice(0, -1);
setTimeout(() => {
process.kill(parseInt(sub), 'SIGINT'); // SIGINT to child
p.kill('SIGINT'); // SIGINT to parent
}, 200);
} else {
buffer.push(data.toString().replace(/^\s+|\s+$/g, ''));
}
});
p.on('close', () => {
// handlers in parent and child should have been triggered
assert.equal(buffer.indexOf('should not be printed') !== -1, false);
assert.equal(buffer.indexOf('SIGINT in parent') !== -1, true);
done();
});
});
it('SIGHUP default (child only)', done => {
const term = new UnixTerminal('node', [ '-e', `
console.log('ready');
setTimeout(()=>console.log('timeout'), 200);`
]);
let buffer = '';
term.on('data', (data) => {
if (data === 'ready\r\n') {
term.kill();
} else {
buffer += data;
}
});
term.on('exit', () => {
// no timeout in buffer
assert.equal(buffer, '');
done();
});
});
it('SIGUSR1 - custom in parent and child', done => {
let pHandlerCalled = 0;
const handleSigUsr = function(h: any): any {
return function(): void {
pHandlerCalled += 1;
process.removeListener('SIGUSR1', h);
};
};
process.on('SIGUSR1', handleSigUsr(handleSigUsr));
const term = new UnixTerminal('node', [ '-e', `
process.on('SIGUSR1', () => {
console.log('SIGUSR1 in child');
});
console.log('ready');
setTimeout(()=>null, 200);`
]);
let buffer = '';
term.on('data', (data) => {
if (data === 'ready\r\n') {
process.kill(process.pid, 'SIGUSR1');
term.kill('SIGUSR1');
} else {
buffer += data;
}
});
term.on('exit', () => {
// should have called both handlers and only once
assert.equal(pHandlerCalled, 1);
assert.equal(buffer, 'SIGUSR1 in child\r\n');
done();
});
});
});
});
}

@@ -71,2 +71,3 @@ /**

const cwd = opt.cwd || process.cwd();
env.PWD = cwd;
const name = opt.name || env.TERM || DEFAULT_NAME;

@@ -78,3 +79,3 @@ env.TERM = name;

const onexit = (code: number, signal: number) => {
const onexit = (code: number, signal: number): void => {
// XXX Sometimes a data event is emitted after exit. Wait til socket is

@@ -197,3 +198,3 @@ // destroyed.

if (encoding !== null) {
self._master.setEncoding(encoding);
self._master.setEncoding(encoding);
}

@@ -204,3 +205,3 @@ self._master.resume();

if (encoding !== null) {
self._slave.setEncoding(encoding);
self._slave.setEncoding(encoding);
}

@@ -273,16 +274,16 @@ self._slave.resume();

private _sanitizeEnv(env: IProcessEnv): void {
// Make sure we didn't start our server from inside tmux.
delete env['TMUX'];
delete env['TMUX_PANE'];
// Make sure we didn't start our server from inside tmux.
delete env['TMUX'];
delete env['TMUX_PANE'];
// Make sure we didn't start our server from inside screen.
// http://web.mit.edu/gnu/doc/html/screen_20.html
delete env['STY'];
delete env['WINDOW'];
// Make sure we didn't start our server from inside screen.
// http://web.mit.edu/gnu/doc/html/screen_20.html
delete env['STY'];
delete env['WINDOW'];
// Delete some variables that might confuse our terminal.
delete env['WINDOWID'];
delete env['TERMCAP'];
delete env['COLUMNS'];
delete env['LINES'];
// Delete some variables that might confuse our terminal.
delete env['WINDOWID'];
delete env['TERMCAP'];
delete env['COLUMNS'];
delete env['LINES'];
}

@@ -298,5 +299,5 @@ }

constructor(fd: number) {
const { Pipe, constants } = (<any>process).binding('pipe_wrap'); // tslint:disable-line
const pipeWrap = (<any>process).binding('pipe_wrap'); // tslint:disable-line
// @types/node has fd as string? https://github.com/DefinitelyTyped/DefinitelyTyped/pull/18275
const handle = new Pipe(constants.SOCKET);
const handle = new pipeWrap.Pipe(pipeWrap.constants.SOCKET);
handle.open(fd);

@@ -303,0 +304,0 @@ super(<any>{ handle });

@@ -131,4 +131,3 @@ /**

if (this._useConpty) {
const connect = (this._ptyNative as IConptyNative).connect(this._pty, commandLine, cwd, env, c => this._$onProcessExit(c)
);
const connect = (this._ptyNative as IConptyNative).connect(this._pty, commandLine, cwd, env, c => this._$onProcessExit(c));
this._innerPid = connect.pid;

@@ -135,0 +134,0 @@ }

@@ -25,2 +25,4 @@ /**

this._checkType('args', args, 'string', true);
// Initialize arguments

@@ -27,0 +29,0 @@ args = args || [];

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc