3 * Copyright (c) 2012-2015, Christopher Jeffrey (MIT License)
4 * Copyright (c) 2016, Daniel Imms (MIT License).
5 * Copyright (c) 2018, Microsoft Corporation (MIT License).
7 Object.defineProperty(exports, "__esModule", { value: true });
8 var events_1 = require("events");
9 var eventEmitter2_1 = require("./eventEmitter2");
10 exports.DEFAULT_COLS = 80;
11 exports.DEFAULT_ROWS = 24;
13 * Default messages to indicate PAUSE/RESUME for automatic flow control.
14 * To avoid conflicts with rebound XON/XOFF control codes (such as on-my-zsh),
15 * the sequences can be customized in `IPtyForkOptions`.
17 var FLOW_CONTROL_PAUSE = '\x13'; // defaults to XOFF
18 var FLOW_CONTROL_RESUME = '\x11'; // defaults to XON
19 var Terminal = /** @class */ (function () {
20 function Terminal(opt) {
21 this._onData = new eventEmitter2_1.EventEmitter2();
22 this._onExit = new eventEmitter2_1.EventEmitter2();
24 this._internalee = new events_1.EventEmitter();
28 // Do basic type checks here in case node-pty is being used within JavaScript. If the wrong
29 // types go through to the C++ side it can lead to hard to diagnose exceptions.
30 this._checkType('name', opt.name ? opt.name : null, 'string');
31 this._checkType('cols', opt.cols ? opt.cols : null, 'number');
32 this._checkType('rows', opt.rows ? opt.rows : null, 'number');
33 this._checkType('cwd', opt.cwd ? opt.cwd : null, 'string');
34 this._checkType('env', opt.env ? opt.env : null, 'object');
35 this._checkType('uid', opt.uid ? opt.uid : null, 'number');
36 this._checkType('gid', opt.gid ? opt.gid : null, 'number');
37 this._checkType('encoding', opt.encoding ? opt.encoding : null, 'string');
38 // setup flow control handling
39 this.handleFlowControl = !!(opt.handleFlowControl);
40 this._flowControlPause = opt.flowControlPause || FLOW_CONTROL_PAUSE;
41 this._flowControlResume = opt.flowControlResume || FLOW_CONTROL_RESUME;
43 Object.defineProperty(Terminal.prototype, "onData", {
44 get: function () { return this._onData.event; },
48 Object.defineProperty(Terminal.prototype, "onExit", {
49 get: function () { return this._onExit.event; },
53 Object.defineProperty(Terminal.prototype, "pid", {
54 get: function () { return this._pid; },
58 Object.defineProperty(Terminal.prototype, "cols", {
59 get: function () { return this._cols; },
63 Object.defineProperty(Terminal.prototype, "rows", {
64 get: function () { return this._rows; },
68 Terminal.prototype.write = function (data) {
69 if (this.handleFlowControl) {
70 // PAUSE/RESUME messages are not forwarded to the pty
71 if (data === this._flowControlPause) {
75 if (data === this._flowControlResume) {
80 // everything else goes to the real pty
83 Terminal.prototype._forwardEvents = function () {
85 this.on('data', function (e) { return _this._onData.fire(e); });
86 this.on('exit', function (exitCode, signal) { return _this._onExit.fire({ exitCode: exitCode, signal: signal }); });
88 Terminal.prototype._checkType = function (name, value, type) {
89 if (value && typeof value !== type) {
90 throw new Error(name + " must be a " + type + " (not a " + typeof value + ")");
93 /** See net.Socket.end */
94 Terminal.prototype.end = function (data) {
95 this._socket.end(data);
97 /** See stream.Readable.pipe */
98 Terminal.prototype.pipe = function (dest, options) {
99 return this._socket.pipe(dest, options);
101 /** See net.Socket.pause */
102 Terminal.prototype.pause = function () {
103 return this._socket.pause();
105 /** See net.Socket.resume */
106 Terminal.prototype.resume = function () {
107 return this._socket.resume();
109 /** See net.Socket.setEncoding */
110 Terminal.prototype.setEncoding = function (encoding) {
111 if (this._socket._decoder) {
112 delete this._socket._decoder;
115 this._socket.setEncoding(encoding);
118 Terminal.prototype.addListener = function (eventName, listener) { this.on(eventName, listener); };
119 Terminal.prototype.on = function (eventName, listener) {
120 if (eventName === 'close') {
121 this._internalee.on('close', listener);
124 this._socket.on(eventName, listener);
126 Terminal.prototype.emit = function (eventName) {
128 for (var _i = 1; _i < arguments.length; _i++) {
129 args[_i - 1] = arguments[_i];
131 if (eventName === 'close') {
132 return this._internalee.emit.apply(this._internalee, arguments);
134 return this._socket.emit.apply(this._socket, arguments);
136 Terminal.prototype.listeners = function (eventName) {
137 return this._socket.listeners(eventName);
139 Terminal.prototype.removeListener = function (eventName, listener) {
140 this._socket.removeListener(eventName, listener);
142 Terminal.prototype.removeAllListeners = function (eventName) {
143 this._socket.removeAllListeners(eventName);
145 Terminal.prototype.once = function (eventName, listener) {
146 this._socket.once(eventName, listener);
148 Terminal.prototype._close = function () {
149 this._socket.writable = false;
150 this._socket.readable = false;
151 this.write = function () { };
152 this.end = function () { };
153 this._writable = false;
154 this._readable = false;
156 Terminal.prototype._parseEnv = function (env) {
157 var keys = Object.keys(env || {});
159 for (var i = 0; i < keys.length; i++) {
160 pairs.push(keys[i] + '=' + env[keys[i]]);
166 exports.Terminal = Terminal;
167 //# sourceMappingURL=terminal.js.map