installed pty
[VSoRC/.git] / node_modules / node-pty / lib / terminal.js
1 "use strict";
2 /**
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).
6  */
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;
12 /**
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`.
16  */
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();
23         // for 'close'
24         this._internalee = new events_1.EventEmitter();
25         if (!opt) {
26             return;
27         }
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;
42     }
43     Object.defineProperty(Terminal.prototype, "onData", {
44         get: function () { return this._onData.event; },
45         enumerable: true,
46         configurable: true
47     });
48     Object.defineProperty(Terminal.prototype, "onExit", {
49         get: function () { return this._onExit.event; },
50         enumerable: true,
51         configurable: true
52     });
53     Object.defineProperty(Terminal.prototype, "pid", {
54         get: function () { return this._pid; },
55         enumerable: true,
56         configurable: true
57     });
58     Object.defineProperty(Terminal.prototype, "cols", {
59         get: function () { return this._cols; },
60         enumerable: true,
61         configurable: true
62     });
63     Object.defineProperty(Terminal.prototype, "rows", {
64         get: function () { return this._rows; },
65         enumerable: true,
66         configurable: true
67     });
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) {
72                 this.pause();
73                 return;
74             }
75             if (data === this._flowControlResume) {
76                 this.resume();
77                 return;
78             }
79         }
80         // everything else goes to the real pty
81         this._write(data);
82     };
83     Terminal.prototype._forwardEvents = function () {
84         var _this = this;
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 }); });
87     };
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 + ")");
91         }
92     };
93     /** See net.Socket.end */
94     Terminal.prototype.end = function (data) {
95         this._socket.end(data);
96     };
97     /** See stream.Readable.pipe */
98     Terminal.prototype.pipe = function (dest, options) {
99         return this._socket.pipe(dest, options);
100     };
101     /** See net.Socket.pause */
102     Terminal.prototype.pause = function () {
103         return this._socket.pause();
104     };
105     /** See net.Socket.resume */
106     Terminal.prototype.resume = function () {
107         return this._socket.resume();
108     };
109     /** See net.Socket.setEncoding */
110     Terminal.prototype.setEncoding = function (encoding) {
111         if (this._socket._decoder) {
112             delete this._socket._decoder;
113         }
114         if (encoding) {
115             this._socket.setEncoding(encoding);
116         }
117     };
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);
122             return;
123         }
124         this._socket.on(eventName, listener);
125     };
126     Terminal.prototype.emit = function (eventName) {
127         var args = [];
128         for (var _i = 1; _i < arguments.length; _i++) {
129             args[_i - 1] = arguments[_i];
130         }
131         if (eventName === 'close') {
132             return this._internalee.emit.apply(this._internalee, arguments);
133         }
134         return this._socket.emit.apply(this._socket, arguments);
135     };
136     Terminal.prototype.listeners = function (eventName) {
137         return this._socket.listeners(eventName);
138     };
139     Terminal.prototype.removeListener = function (eventName, listener) {
140         this._socket.removeListener(eventName, listener);
141     };
142     Terminal.prototype.removeAllListeners = function (eventName) {
143         this._socket.removeAllListeners(eventName);
144     };
145     Terminal.prototype.once = function (eventName, listener) {
146         this._socket.once(eventName, listener);
147     };
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;
155     };
156     Terminal.prototype._parseEnv = function (env) {
157         var keys = Object.keys(env || {});
158         var pairs = [];
159         for (var i = 0; i < keys.length; i++) {
160             pairs.push(keys[i] + '=' + env[keys[i]]);
161         }
162         return pairs;
163     };
164     return Terminal;
165 }());
166 exports.Terminal = Terminal;
167 //# sourceMappingURL=terminal.js.map