Socket
Socket
Sign inDemoInstall

node-pty

Package Overview
Dependencies
Maintainers
1
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.6.10 to 0.7.0

lib/terminal.test.d.ts

3

lib/index.js

@@ -7,5 +7,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var os = require("os");
var Terminal;
if (os.platform() === 'win32') {
if (process.platform === 'win32') {
Terminal = require('./windowsTerminal').WindowsTerminal;

@@ -12,0 +11,0 @@ }

@@ -0,4 +1,6 @@

/// <reference types="node" />
/**
* Copyright (c) 2016, Daniel Imms (MIT License).
*/
import * as net from 'net';
export declare type ProcessEnv = {

@@ -13,2 +15,15 @@ [key: string]: string;

/**
* Gets the process ID.
*/
pid: number;
/**
* The socket for the master file descriptor. This is not supported on
* Windows.
*/
master: net.Socket;
/**
* The socket for the slave file descriptor. This is not supported on Windows.
*/
slave: net.Socket;
/**
* Writes data to the socket.

@@ -15,0 +30,0 @@ * @param data The data to write.

@@ -5,16 +5,17 @@ /// <reference types="node" />

import { ITerminal, IPtyForkOptions } from './interfaces';
export declare const DEFAULT_COLS: number;
export declare const DEFAULT_ROWS: number;
export declare abstract class Terminal implements ITerminal {
protected static readonly DEFAULT_COLS: number;
protected static readonly DEFAULT_ROWS: number;
protected socket: Socket;
protected pid: number;
protected fd: number;
protected pty: any;
protected file: string;
protected name: string;
protected cols: number;
protected rows: number;
protected readable: boolean;
protected writable: boolean;
protected _socket: Socket;
protected _pid: number;
protected _fd: number;
protected _pty: any;
protected _file: string;
protected _name: string;
protected _cols: number;
protected _rows: number;
protected _readable: boolean;
protected _writable: boolean;
protected _internalee: EventEmitter;
readonly pid: number;
constructor(opt?: IPtyForkOptions);

@@ -43,6 +44,5 @@ private _checkType(name, value, type);

abstract kill(signal?: string): void;
/**
* Gets the name of the process.
*/
readonly abstract process: string;
readonly abstract master: Socket;
readonly abstract slave: Socket;
redraw(): void;

@@ -49,0 +49,0 @@ protected _close(): void;

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

var events_1 = require("events");
exports.DEFAULT_COLS = 80;
exports.DEFAULT_ROWS = 24;
var Terminal = (function () {

@@ -27,2 +29,7 @@ function Terminal(opt) {

}
Object.defineProperty(Terminal.prototype, "pid", {
get: function () { return this._pid; },
enumerable: true,
configurable: true
});
Terminal.prototype._checkType = function (name, value, type) {

@@ -35,24 +42,23 @@ if (value && typeof value !== type) {

Terminal.prototype.end = function (data) {
this.socket.end(data);
this._socket.end(data);
};
/** See stream.Readable.pipe */
Terminal.prototype.pipe = function (dest, options) {
return this.socket.pipe(dest, options);
return this._socket.pipe(dest, options);
};
/** See net.Socket.pause */
Terminal.prototype.pause = function () {
return this.socket.pause();
return this._socket.pause();
};
/** See net.Socket.resume */
Terminal.prototype.resume = function () {
// TODO: Type with Socket
return this.socket.resume();
return this._socket.resume();
};
/** See net.Socket.setEncoding */
Terminal.prototype.setEncoding = function (encoding) {
if (this.socket._decoder) {
delete this.socket._decoder;
if (this._socket._decoder) {
delete this._socket._decoder;
}
if (encoding) {
this.socket.setEncoding(encoding);
this._socket.setEncoding(encoding);
}

@@ -66,3 +72,3 @@ };

}
this.socket.on(eventName, listener);
this._socket.on(eventName, listener);
};

@@ -77,15 +83,15 @@ Terminal.prototype.emit = function (eventName) {

}
return this.socket.emit.apply(this.socket, arguments);
return this._socket.emit.apply(this._socket, arguments);
};
Terminal.prototype.listeners = function (eventName) {
return this.socket.listeners(eventName);
return this._socket.listeners(eventName);
};
Terminal.prototype.removeListener = function (eventName, listener) {
this.socket.removeListener(eventName, listener);
this._socket.removeListener(eventName, listener);
};
Terminal.prototype.removeAllListeners = function (eventName) {
this.socket.removeAllListeners(eventName);
this._socket.removeAllListeners(eventName);
};
Terminal.prototype.once = function (eventName, listener) {
this.socket.once(eventName, listener);
this._socket.once(eventName, listener);
};

@@ -95,4 +101,4 @@ // TODO: Should this be in the API?

var _this = this;
var cols = this.cols;
var rows = this.rows;
var cols = this._cols;
var rows = this._rows;
// We could just send SIGWINCH, but most programs will ignore it if the

@@ -104,8 +110,8 @@ // size hasn't actually changed.

Terminal.prototype._close = function () {
this.socket.writable = false;
this.socket.readable = false;
this._socket.writable = false;
this._socket.readable = false;
this.write = function () { };
this.end = function () { };
this.writable = false;
this.readable = false;
this._writable = false;
this._readable = false;
};

@@ -120,4 +126,2 @@ Terminal.prototype._parseEnv = function (env) {

};
Terminal.DEFAULT_COLS = 80;
Terminal.DEFAULT_ROWS = 24;
return Terminal;

@@ -124,0 +128,0 @@ }());

@@ -0,1 +1,7 @@

/// <reference types="node" />
/**
* Copyright (c) 2012-2015, Christopher Jeffrey (MIT License)
* Copyright (c) 2016, Daniel Imms (MIT License).
*/
import * as net from 'net';
import { Terminal } from './terminal';

@@ -5,13 +11,14 @@ import { IPtyForkOptions, IPtyOpenOptions } from './interfaces';

export declare class UnixTerminal extends Terminal {
protected pid: number;
protected fd: number;
protected pty: any;
protected file: string;
protected name: string;
protected readable: boolean;
protected writable: boolean;
protected _fd: number;
protected _pty: string;
protected _file: string;
protected _name: string;
protected _readable: boolean;
protected _writable: boolean;
private _boundClose;
private _emittedClose;
private master;
private slave;
private _master;
private _slave;
readonly master: net.Socket;
readonly slave: net.Socket;
constructor(file?: string, args?: ArgvOrCommandLine, opt?: IPtyForkOptions);

@@ -18,0 +25,0 @@ /**

@@ -36,4 +36,4 @@ "use strict";

opt.env = opt.env || process.env;
var cols = opt.cols || terminal_1.Terminal.DEFAULT_COLS;
var rows = opt.rows || terminal_1.Terminal.DEFAULT_ROWS;
var cols = opt.cols || terminal_1.DEFAULT_COLS;
var rows = opt.rows || terminal_1.DEFAULT_ROWS;
var uid = opt.uid || -1;

@@ -64,8 +64,8 @@ var gid = opt.gid || -1;

var term = pty.fork(file, args, parsedEnv, cwd, cols, rows, uid, gid, (encoding === 'utf8'), onexit);
_this.socket = new PipeSocket(term.fd);
_this._socket = new PipeSocket(term.fd);
if (encoding !== null) {
_this.socket.setEncoding(encoding);
_this._socket.setEncoding(encoding);
}
// setup
_this.socket.on('error', function (err) {
_this._socket.on('error', function (err) {
// NOTE: fs.ReadStream gets EAGAIN twice at first:

@@ -98,10 +98,10 @@ if (err.code) {

});
_this.pid = term.pid;
_this.fd = term.fd;
_this.pty = term.pty;
_this.file = file;
_this.name = name;
_this.readable = true;
_this.writable = true;
_this.socket.on('close', function () {
_this._pid = term.pid;
_this._fd = term.fd;
_this._pty = term.pty;
_this._file = file;
_this._name = name;
_this._readable = true;
_this._writable = true;
_this._socket.on('close', function () {
if (_this._emittedClose) {

@@ -116,2 +116,12 @@ return;

}
Object.defineProperty(UnixTerminal.prototype, "master", {
get: function () { return this._master; },
enumerable: true,
configurable: true
});
Object.defineProperty(UnixTerminal.prototype, "slave", {
get: function () { return this._slave; },
enumerable: true,
configurable: true
});
/**

@@ -129,22 +139,22 @@ * openpty

}
var cols = opt.cols || terminal_1.Terminal.DEFAULT_COLS;
var rows = opt.rows || terminal_1.Terminal.DEFAULT_ROWS;
var cols = opt.cols || terminal_1.DEFAULT_COLS;
var rows = opt.rows || terminal_1.DEFAULT_ROWS;
var encoding = opt.encoding ? 'utf8' : opt.encoding;
// open
var term = pty.open(cols, rows);
self.master = new PipeSocket(term.master);
self.master.setEncoding(encoding);
self.master.resume();
self.slave = new PipeSocket(term.slave);
self.slave.setEncoding(encoding);
self.slave.resume();
self.socket = self.master;
self.pid = null;
self.fd = term.master;
self.pty = term.pty;
self.file = process.argv[0] || 'node';
self.name = process.env.TERM || '';
self.readable = true;
self.writable = true;
self.socket.on('error', function (err) {
self._master = new PipeSocket(term.master);
self._master.setEncoding(encoding);
self._master.resume();
self._slave = new PipeSocket(term.slave);
self._slave.setEncoding(encoding);
self._slave.resume();
self._socket = self._master;
self._pid = null;
self._fd = term.master;
self._pty = term.pty;
self._file = process.argv[0] || 'node';
self._name = process.env.TERM || '';
self._readable = true;
self._writable = true;
self._socket.on('error', function (err) {
self._close();

@@ -155,3 +165,3 @@ if (self.listeners('error').length < 2) {

});
self.socket.on('close', function () {
self._socket.on('close', function () {
self._close();

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

UnixTerminal.prototype.write = function (data) {
this.socket.write(data);
this._socket.write(data);
};

@@ -171,6 +181,6 @@ UnixTerminal.prototype.destroy = function () {

// descriptor. Then we can safely SIGHUP the shell.
this.socket.once('close', function () {
this._socket.once('close', function () {
_this.kill('SIGHUP');
});
this.socket.destroy();
this._socket.destroy();
};

@@ -188,3 +198,3 @@ UnixTerminal.prototype.kill = function (signal) {

get: function () {
return pty.process(this.fd, this.pty) || this.file;
return pty.process(this._fd, this._pty) || this._file;
},

@@ -198,3 +208,3 @@ enumerable: true,

UnixTerminal.prototype.resize = function (cols, rows) {
pty.resize(this.fd, cols, rows);
pty.resize(this._fd, cols, rows);
};

@@ -230,2 +240,3 @@ UnixTerminal.prototype._sanitizeEnv = function (env) {

tty.guessHandleType = function () { return 'PIPE'; };
// @types/node has fd as string? https://github.com/DefinitelyTyped/DefinitelyTyped/pull/18275
_this = _super.call(this, { fd: fd }) || this;

@@ -232,0 +243,0 @@ tty.guessHandleType = guessHandleType;

/// <reference types="node" />
/**
* Copyright (c) 2012-2015, Christopher Jeffrey, Peter Sunde (MIT License)
* Copyright (c) 2016, Daniel Imms (MIT License).
*/
import * as net from 'net';
import { Socket } from 'net';
import { ArgvOrCommandLine } from './types';

@@ -23,4 +19,4 @@ /**

private _pty;
readonly inSocket: net.Socket;
readonly outSocket: net.Socket;
readonly inSocket: Socket;
readonly outSocket: Socket;
readonly fd: any;

@@ -32,3 +28,4 @@ readonly innerPid: number;

kill(): void;
getExitCode(): number;
}
export declare function argsToCommandLine(file: string, args: ArgvOrCommandLine): string;

@@ -7,4 +7,4 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
var net = require("net");
var path = require("path");
var net_1 = require("net");
var pty = require(path.join('..', 'build', 'Release', 'pty.node'));

@@ -37,3 +37,3 @@ /**

// Create terminal pipe IPC channel and forward to a local unix socket.
this._outSocket = new net.Socket();
this._outSocket = new net_1.Socket();
this._outSocket.setEncoding('utf8');

@@ -45,3 +45,3 @@ this._outSocket.connect(term.conout, function () {

});
this._inSocket = new net.Socket();
this._inSocket = new net_1.Socket();
this._inSocket.setEncoding('utf8');

@@ -86,2 +86,5 @@ this._inSocket.connect(term.conin);

};
WindowsPtyAgent.prototype.getExitCode = function () {
return pty.getExitCode(this._innerPidHandle);
};
return WindowsPtyAgent;

@@ -88,0 +91,0 @@ }());

@@ -0,1 +1,3 @@

/// <reference types="node" />
import { Socket } from 'net';
import { Terminal } from './terminal';

@@ -5,5 +7,5 @@ import { IPtyForkOptions, IPtyOpenOptions } from './interfaces';

export declare class WindowsTerminal extends Terminal {
private isReady;
private deferreds;
private agent;
private _isReady;
private _deferreds;
private _agent;
constructor(file?: string, args?: ArgvOrCommandLine, opt?: IPtyForkOptions);

@@ -25,6 +27,5 @@ /**

private _defer(deferredFn);
/**
* Gets the name of the process.
*/
readonly process: string;
readonly master: Socket;
readonly slave: Socket;
}

@@ -26,5 +26,2 @@ "use strict";

var _this = _super.call(this, opt) || this;
if (opt.encoding) {
console.warn('Setting encoding on Windows is not supported');
}
// Initialize arguments

@@ -35,5 +32,8 @@ args = args || [];

opt.env = opt.env || process.env;
if (opt.encoding) {
console.warn('Setting encoding on Windows is not supported');
}
var env = utils_1.assign({}, opt.env);
var cols = opt.cols || terminal_1.Terminal.DEFAULT_COLS;
var rows = opt.rows || terminal_1.Terminal.DEFAULT_ROWS;
var cols = opt.cols || terminal_1.DEFAULT_COLS;
var rows = opt.rows || terminal_1.DEFAULT_ROWS;
var cwd = opt.cwd || process.cwd();

@@ -43,25 +43,25 @@ var name = opt.name || env.TERM || DEFAULT_NAME;

// If the terminal is ready
_this.isReady = false;
_this._isReady = false;
// Functions that need to run after `ready` event is emitted.
_this.deferreds = [];
_this._deferreds = [];
// Create new termal.
_this.agent = new windowsPtyAgent_1.WindowsPtyAgent(file, args, parsedEnv, cwd, cols, rows, false);
_this.socket = _this.agent.outSocket;
_this._agent = new windowsPtyAgent_1.WindowsPtyAgent(file, args, parsedEnv, cwd, cols, rows, false);
_this._socket = _this._agent.outSocket;
// Not available until `ready` event emitted.
_this.pid = _this.agent.innerPid;
_this.fd = _this.agent.fd;
_this.pty = _this.agent.pty;
_this._pid = _this._agent.innerPid;
_this._fd = _this._agent.fd;
_this._pty = _this._agent.pty;
// The forked windows terminal is not available until `ready` event is
// emitted.
_this.socket.on('ready_datapipe', function () {
_this._socket.on('ready_datapipe', function () {
// These events needs to be forwarded.
['connect', 'data', 'end', 'timeout', 'drain'].forEach(function (event) {
_this.socket.on(event, function (data) {
_this._socket.on(event, function (data) {
// Wait until the first data event is fired then we can run deferreds.
if (!_this.isReady && event === 'data') {
if (!_this._isReady && event === 'data') {
// Terminal is now ready and we can avoid having to defer method
// calls.
_this.isReady = true;
_this._isReady = true;
// Execute all deferred methods
_this.deferreds.forEach(function (fn) {
_this._deferreds.forEach(function (fn) {
// NB! In order to ensure that `this` has all its references

@@ -74,3 +74,3 @@ // updated any variable that need to be available in `this` before

// Reset
_this.deferreds = [];
_this._deferreds = [];
}

@@ -80,3 +80,3 @@ });

// Shutdown if `error` event is emitted.
_this.socket.on('error', function (err) {
_this._socket.on('error', function (err) {
// Close terminal session.

@@ -98,11 +98,11 @@ _this._close();

// Cleanup after the socket is closed.
_this.socket.on('close', function () {
_this.emit('exit', null);
_this._socket.on('close', function () {
_this.emit('exit', _this._agent.getExitCode());
_this._close();
});
});
_this.file = file;
_this.name = name;
_this.readable = true;
_this.writable = true;
_this._file = file;
_this._name = name;
_this._readable = true;
_this._writable = true;
return _this;

@@ -122,3 +122,3 @@ }

this._defer(function () {
_this.agent.inSocket.write(data);
_this._agent.inSocket.write(data);
});

@@ -131,4 +131,7 @@ };

var _this = this;
if (cols <= 0 || rows <= 0) {
throw new Error('resizing must be done using positive cols and rows');
}
this._defer(function () {
_this.agent.resize(cols, rows);
_this._agent.resize(cols, rows);
});

@@ -149,3 +152,3 @@ };

_this._close();
_this.agent.kill();
_this._agent.kill();
});

@@ -160,3 +163,3 @@ };

// If the terminal is ready, execute.
if (this.isReady) {
if (this._isReady) {
deferredFn.apply(this, null);

@@ -166,3 +169,3 @@ return;

// Queue until terminal is ready.
this.deferreds.push({
this._deferreds.push({
run: function () { return deferredFn.apply(_this, null); }

@@ -172,9 +175,16 @@ });

Object.defineProperty(WindowsTerminal.prototype, "process", {
/**
* Gets the name of the process.
*/
get: function () { return this.name; },
get: function () { return this._name; },
enumerable: true,
configurable: true
});
Object.defineProperty(WindowsTerminal.prototype, "master", {
get: function () { throw new Error('master is not supported on Windows'); },
enumerable: true,
configurable: true
});
Object.defineProperty(WindowsTerminal.prototype, "slave", {
get: function () { throw new Error('slave is not supported on Windows'); },
enumerable: true,
configurable: true
});
return WindowsTerminal;

@@ -181,0 +191,0 @@ }(terminal_1.Terminal));

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

},
"version": "0.6.10",
"version": "0.7.0",
"license": "MIT",

@@ -40,12 +40,14 @@ "main": "./lib/index.js",

"postinstall": "node scripts/post-install.js",
"test": "cross-env NODE_ENV=test mocha -R spec test",
"test": "cross-env NODE_ENV=test mocha -R spec lib/*.test.js",
"prepublish": "npm run tsc"
},
"dependencies": {
"nan": "2.5.0"
"nan": "^2.6.2"
},
"devDependencies": {
"@types/mocha": "^2.2.41",
"@types/node": "^6.0.58",
"cross-env": "^3.1.3",
"cross-env": "^3.2.4",
"mocha": "^3.1.2",
"pollUntil": "^1.0.3",
"tslint": "^4.3.1",

@@ -52,0 +54,0 @@ "typescript": "^2.1.4"

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

import * as os from 'os';
import { Terminal as BaseTerminal } from './terminal';

@@ -13,3 +12,3 @@ import { ITerminal, IPtyOpenOptions, IPtyForkOptions } from './interfaces';

let Terminal: any;
if (os.platform() === 'win32') {
if (process.platform === 'win32') {
Terminal = require('./windowsTerminal').WindowsTerminal;

@@ -16,0 +15,0 @@ } else {

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

import * as net from 'net';
export type ProcessEnv = {[key: string]: string};

@@ -15,2 +17,18 @@

/**
* Gets the process ID.
*/
pid: number;
/**
* The socket for the master file descriptor. This is not supported on
* Windows.
*/
master: net.Socket;
/**
* The socket for the slave file descriptor. This is not supported on Windows.
*/
slave: net.Socket;
/**
* Writes data to the socket.

@@ -17,0 +35,0 @@ * @param data The data to write.

@@ -11,21 +11,23 @@ /**

export const DEFAULT_COLS: number = 80;
export const DEFAULT_ROWS: number = 24;
export abstract class Terminal implements ITerminal {
protected static readonly DEFAULT_COLS: number = 80;
protected static readonly DEFAULT_ROWS: number = 24;
protected _socket: Socket;
protected _pid: number;
protected _fd: number;
protected _pty: any;
protected socket: Socket;
protected pid: number;
protected fd: number;
protected pty: any;
protected _file: string;
protected _name: string;
protected _cols: number;
protected _rows: number;
protected file: string;
protected name: string;
protected cols: number;
protected rows: number;
protected _readable: boolean;
protected _writable: boolean;
protected readable: boolean;
protected writable: boolean;
protected _internalee: EventEmitter;
public get pid(): number { return this._pid; }
constructor(opt?: IPtyForkOptions) {

@@ -59,3 +61,3 @@ // for 'close'

public end(data: string): void{
this.socket.end(data);
this._socket.end(data);
}

@@ -65,3 +67,3 @@

public pipe(dest: any, options: any): any {
return this.socket.pipe(dest, options);
return this._socket.pipe(dest, options);
}

@@ -71,3 +73,3 @@

public pause(): Socket {
return this.socket.pause();
return this._socket.pause();
}

@@ -77,4 +79,3 @@

public resume(): Socket {
// TODO: Type with Socket
return this.socket.resume();
return this._socket.resume();
}

@@ -84,7 +85,7 @@

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

@@ -99,3 +100,3 @@ }

}
this.socket.on(eventName, listener);
this._socket.on(eventName, listener);
}

@@ -107,19 +108,19 @@

}
return this.socket.emit.apply(this.socket, arguments);
return this._socket.emit.apply(this._socket, arguments);
}
public listeners(eventName: string): Function[] {
return this.socket.listeners(eventName);
return this._socket.listeners(eventName);
}
public removeListener(eventName: string, listener: (...args: any[]) => any): void {
this.socket.removeListener(eventName, listener);
this._socket.removeListener(eventName, listener);
}
public removeAllListeners(eventName: string): void {
this.socket.removeAllListeners(eventName);
this._socket.removeAllListeners(eventName);
}
public once(eventName: string, listener: (...args: any[]) => any): void {
this.socket.once(eventName, listener);
this._socket.once(eventName, listener);
}

@@ -132,11 +133,10 @@

/**
* Gets the name of the process.
*/
public abstract get process(): string;
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;
let cols = this._cols;
let rows = this._rows;

@@ -152,8 +152,8 @@ // We could just send SIGWINCH, but most programs will ignore it if the

protected _close(): void {
this.socket.writable = false;
this.socket.readable = false;
this._socket.writable = false;
this._socket.readable = false;
this.write = () => {};
this.end = () => {};
this.writable = false;
this.readable = false;
this._writable = false;
this._readable = false;
}

@@ -160,0 +160,0 @@

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

import * as tty from 'tty';
import { Terminal } from './terminal';
import { Terminal, DEFAULT_COLS, DEFAULT_ROWS } from './terminal';
import { ProcessEnv, IPtyForkOptions, IPtyOpenOptions } from './interfaces';

@@ -15,2 +15,8 @@ import { ArgvOrCommandLine } from './types';

declare type NativePty = {
master: number;
slave: number;
pty: string;
};
const pty = require(path.join('..', 'build', 'Release', 'pty.node'));

@@ -22,17 +28,19 @@

export class UnixTerminal extends Terminal {
protected pid: number;
protected fd: number;
protected pty: any;
protected _fd: number;
protected _pty: string;
protected file: string;
protected name: string;
protected _file: string;
protected _name: string;
protected readable: boolean;
protected writable: boolean;
protected _readable: boolean;
protected _writable: boolean;
private _boundClose: boolean;
private _emittedClose: boolean;
private master: any;
private slave: any;
private _master: net.Socket;
private _slave: net.Socket;
public get master(): net.Socket { return this._master; }
public get slave(): net.Socket { return this._slave; }
constructor(file?: string, args?: ArgvOrCommandLine, opt?: IPtyForkOptions) {

@@ -51,4 +59,4 @@ super(opt);

const cols = opt.cols || Terminal.DEFAULT_COLS;
const rows = opt.rows || Terminal.DEFAULT_ROWS;
const cols = opt.cols || DEFAULT_COLS;
const rows = opt.rows || DEFAULT_ROWS;
const uid = opt.uid || -1;

@@ -84,9 +92,9 @@ const gid = opt.gid || -1;

this.socket = new PipeSocket(term.fd);
this._socket = new PipeSocket(term.fd);
if (encoding !== null) {
this.socket.setEncoding(encoding);
this._socket.setEncoding(encoding);
}
// setup
this.socket.on('error', (err: any) => {
this._socket.on('error', (err: any) => {
// NOTE: fs.ReadStream gets EAGAIN twice at first:

@@ -123,13 +131,13 @@ if (err.code) {

this.pid = term.pid;
this.fd = term.fd;
this.pty = term.pty;
this._pid = term.pid;
this._fd = term.fd;
this._pty = term.pty;
this.file = file;
this.name = name;
this._file = file;
this._name = name;
this.readable = true;
this.writable = true;
this._readable = true;
this._writable = true;
this.socket.on('close', () => {
this._socket.on('close', () => {
if (this._emittedClose) {

@@ -149,3 +157,3 @@ return;

public static open(opt: IPtyOpenOptions): UnixTerminal {
const self = Object.create(UnixTerminal.prototype);
const self: UnixTerminal = Object.create(UnixTerminal.prototype);
opt = opt || {};

@@ -160,29 +168,29 @@

const cols = opt.cols || Terminal.DEFAULT_COLS;
const rows = opt.rows || Terminal.DEFAULT_ROWS;
const cols = opt.cols || DEFAULT_COLS;
const rows = opt.rows || DEFAULT_ROWS;
const encoding = opt.encoding ? 'utf8' : opt.encoding;
// open
const term = pty.open(cols, rows);
const term: NativePty = pty.open(cols, rows);
self.master = new PipeSocket(term.master);
self.master.setEncoding(encoding);
self.master.resume();
self._master = new PipeSocket(<number>term.master);
self._master.setEncoding(encoding);
self._master.resume();
self.slave = new PipeSocket(term.slave);
self.slave.setEncoding(encoding);
self.slave.resume();
self._slave = new PipeSocket(term.slave);
self._slave.setEncoding(encoding);
self._slave.resume();
self.socket = self.master;
self.pid = null;
self.fd = term.master;
self.pty = term.pty;
self._socket = self._master;
self._pid = null;
self._fd = term.master;
self._pty = term.pty;
self.file = process.argv[0] || 'node';
self.name = process.env.TERM || '';
self._file = process.argv[0] || 'node';
self._name = process.env.TERM || '';
self.readable = true;
self.writable = true;
self._readable = true;
self._writable = true;
self.socket.on('error', err => {
self._socket.on('error', err => {
self._close();

@@ -194,3 +202,3 @@ if (self.listeners('error').length < 2) {

self.socket.on('close', () => {
self._socket.on('close', () => {
self._close();

@@ -203,3 +211,3 @@ });

public write(data: string): void {
this.socket.write(data);
this._socket.write(data);
}

@@ -212,7 +220,7 @@

// descriptor. Then we can safely SIGHUP the shell.
this.socket.once('close', () => {
this._socket.once('close', () => {
this.kill('SIGHUP');
});
this.socket.destroy();
this._socket.destroy();
}

@@ -230,3 +238,3 @@

public get process(): string {
return pty.process(this.fd, this.pty) || this.file;
return pty.process(this._fd, this._pty) || this._file;
}

@@ -239,3 +247,3 @@

public resize(cols: number, rows: number): void {
pty.resize(this.fd, cols, rows);
pty.resize(this._fd, cols, rows);
}

@@ -267,9 +275,10 @@

class PipeSocket extends net.Socket {
constructor(fd: any) {
constructor(fd: number) {
const tty = (<any>process).binding('tty_wrap');
const guessHandleType = tty.guessHandleType;
tty.guessHandleType = () => 'PIPE';
super({ fd });
// @types/node has fd as string? https://github.com/DefinitelyTyped/DefinitelyTyped/pull/18275
super({ fd: <any>fd });
tty.guessHandleType = guessHandleType;
}
}

@@ -6,4 +6,4 @@ /**

import * as net from 'net';
import * as path from 'path';
import { Socket } from 'net';
import { ArgvOrCommandLine } from './types';

@@ -22,4 +22,4 @@

export class WindowsPtyAgent {
private _inSocket: net.Socket;
private _outSocket: net.Socket;
private _inSocket: Socket;
private _outSocket: Socket;
private _pid: number;

@@ -32,4 +32,4 @@ private _innerPid: number;

public get inSocket(): net.Socket { return this._inSocket; }
public get outSocket(): net.Socket { return this._outSocket; }
public get inSocket(): Socket { return this._inSocket; }
public get outSocket(): Socket { return this._outSocket; }
public get fd(): any { return this._fd; }

@@ -70,3 +70,3 @@ public get innerPid(): number { return this._innerPid; }

// Create terminal pipe IPC channel and forward to a local unix socket.
this._outSocket = new net.Socket();
this._outSocket = new Socket();
this._outSocket.setEncoding('utf8');

@@ -80,3 +80,3 @@ this._outSocket.connect(term.conout, () => {

this._inSocket = new net.Socket();
this._inSocket = new Socket();
this._inSocket.setEncoding('utf8');

@@ -98,2 +98,6 @@ this._inSocket.connect(term.conin);

}
public getExitCode(): number {
return pty.getExitCode(this._innerPidHandle);
}
}

@@ -100,0 +104,0 @@

@@ -6,6 +6,6 @@ /**

import * as net from 'net';
import * as path from 'path';
import { Socket } from 'net';
import { inherits } from 'util';
import { Terminal } from './terminal';
import { Terminal, DEFAULT_COLS, DEFAULT_ROWS } from './terminal';
import { WindowsPtyAgent } from './windowsPtyAgent';

@@ -20,5 +20,5 @@ import { IPtyForkOptions, IPtyOpenOptions } from './interfaces';

export class WindowsTerminal extends Terminal {
private isReady: boolean;
private deferreds: any[];
private agent: WindowsPtyAgent;
private _isReady: boolean;
private _deferreds: any[];
private _agent: WindowsPtyAgent;

@@ -28,6 +28,2 @@ constructor(file?: string, args?: ArgvOrCommandLine, opt?: IPtyForkOptions) {

if (opt.encoding) {
console.warn('Setting encoding on Windows is not supported');
}
// Initialize arguments

@@ -39,5 +35,9 @@ args = args || [];

if (opt.encoding) {
console.warn('Setting encoding on Windows is not supported');
}
const env = assign({}, opt.env);
const cols = opt.cols || Terminal.DEFAULT_COLS;
const rows = opt.rows || Terminal.DEFAULT_ROWS;
const cols = opt.cols || DEFAULT_COLS;
const rows = opt.rows || DEFAULT_ROWS;
const cwd = opt.cwd || process.cwd();

@@ -48,33 +48,33 @@ const name = opt.name || env.TERM || DEFAULT_NAME;

// If the terminal is ready
this.isReady = false;
this._isReady = false;
// Functions that need to run after `ready` event is emitted.
this.deferreds = [];
this._deferreds = [];
// Create new termal.
this.agent = new WindowsPtyAgent(file, args, parsedEnv, cwd, cols, rows, false);
this.socket = this.agent.outSocket;
this._agent = new WindowsPtyAgent(file, args, parsedEnv, cwd, cols, rows, false);
this._socket = this._agent.outSocket;
// Not available until `ready` event emitted.
this.pid = this.agent.innerPid;
this.fd = this.agent.fd;
this.pty = this.agent.pty;
this._pid = this._agent.innerPid;
this._fd = this._agent.fd;
this._pty = this._agent.pty;
// The forked windows terminal is not available until `ready` event is
// emitted.
this.socket.on('ready_datapipe', () => {
this._socket.on('ready_datapipe', () => {
// These events needs to be forwarded.
['connect', 'data', 'end', 'timeout', 'drain'].forEach(event => {
this.socket.on(event, data => {
this._socket.on(event, data => {
// Wait until the first data event is fired then we can run deferreds.
if (!this.isReady && event === 'data') {
if (!this._isReady && event === 'data') {
// Terminal is now ready and we can avoid having to defer method
// calls.
this.isReady = true;
this._isReady = true;
// Execute all deferred methods
this.deferreds.forEach(fn => {
this._deferreds.forEach(fn => {
// NB! In order to ensure that `this` has all its references

@@ -88,3 +88,3 @@ // updated any variable that need to be available in `this` before

// Reset
this.deferreds = [];
this._deferreds = [];

@@ -96,3 +96,3 @@ }

// Shutdown if `error` event is emitted.
this.socket.on('error', err => {
this._socket.on('error', err => {
// Close terminal session.

@@ -116,4 +116,4 @@ this._close();

// Cleanup after the socket is closed.
this.socket.on('close', () => {
this.emit('exit', null);
this._socket.on('close', () => {
this.emit('exit', this._agent.getExitCode());
this._close();

@@ -124,7 +124,7 @@ });

this.file = file;
this.name = name;
this._file = file;
this._name = name;
this.readable = true;
this.writable = true;
this._readable = true;
this._writable = true;
}

@@ -146,3 +146,3 @@

this._defer(() => {
this.agent.inSocket.write(data);
this._agent.inSocket.write(data);
});

@@ -156,4 +156,7 @@ }

public resize(cols: number, rows: number): void {
if (cols <= 0 || rows <= 0) {
throw new Error('resizing must be done using positive cols and rows');
}
this._defer(() => {
this.agent.resize(cols, rows);
this._agent.resize(cols, rows);
});

@@ -174,3 +177,3 @@ }

this._close();
this.agent.kill();
this._agent.kill();
});

@@ -187,3 +190,3 @@ }

// If the terminal is ready, execute.
if (this.isReady) {
if (this._isReady) {
deferredFn.apply(this, null);

@@ -194,3 +197,3 @@ return;

// Queue until terminal is ready.
this.deferreds.push({
this._deferreds.push({
run: () => deferredFn.apply(this, null)

@@ -200,6 +203,5 @@ });

/**
* Gets the name of the process.
*/
public get process(): string { return this.name; }
public get process(): string { return this._name; }
public get master(): Socket { throw new Error('master is not supported on Windows'); }
public get slave(): Socket { throw new Error('slave is not supported on Windows'); }
}

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