1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
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 |
|
14 |
|
15 |
|
16 |
|
17 | var FLOW_CONTROL_PAUSE = '\x13';
|
18 | var FLOW_CONTROL_RESUME = '\x11';
|
19 | var Terminal = (function () {
|
20 | function Terminal(opt) {
|
21 | this._onData = new eventEmitter2_1.EventEmitter2();
|
22 | this._onExit = new eventEmitter2_1.EventEmitter2();
|
23 |
|
24 | this._internalee = new events_1.EventEmitter();
|
25 | if (!opt) {
|
26 | return;
|
27 | }
|
28 |
|
29 |
|
30 | this._checkType('name', opt.name ? opt.name : undefined, 'string');
|
31 | this._checkType('cols', opt.cols ? opt.cols : undefined, 'number');
|
32 | this._checkType('rows', opt.rows ? opt.rows : undefined, 'number');
|
33 | this._checkType('cwd', opt.cwd ? opt.cwd : undefined, 'string');
|
34 | this._checkType('env', opt.env ? opt.env : undefined, 'object');
|
35 | this._checkType('uid', opt.uid ? opt.uid : undefined, 'number');
|
36 | this._checkType('gid', opt.gid ? opt.gid : undefined, 'number');
|
37 | this._checkType('encoding', opt.encoding ? opt.encoding : undefined, 'string');
|
38 |
|
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 |
|
71 | if (data === this._flowControlPause) {
|
72 | this.pause();
|
73 | return;
|
74 | }
|
75 | if (data === this._flowControlResume) {
|
76 | this.resume();
|
77 | return;
|
78 | }
|
79 | }
|
80 |
|
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, allowArray) {
|
89 | if (allowArray === void 0) { allowArray = false; }
|
90 | if (value === undefined) {
|
91 | return;
|
92 | }
|
93 | if (allowArray) {
|
94 | if (Array.isArray(value)) {
|
95 | value.forEach(function (v, i) {
|
96 | if (typeof v !== type) {
|
97 | throw new Error(name + "[" + i + "] must be a " + type + " (not a " + typeof v[i] + ")");
|
98 | }
|
99 | });
|
100 | return;
|
101 | }
|
102 | }
|
103 | if (typeof value !== type) {
|
104 | throw new Error(name + " must be a " + type + " (not a " + typeof value + ")");
|
105 | }
|
106 | };
|
107 |
|
108 | Terminal.prototype.end = function (data) {
|
109 | this._socket.end(data);
|
110 | };
|
111 |
|
112 | Terminal.prototype.pipe = function (dest, options) {
|
113 | return this._socket.pipe(dest, options);
|
114 | };
|
115 |
|
116 | Terminal.prototype.pause = function () {
|
117 | return this._socket.pause();
|
118 | };
|
119 |
|
120 | Terminal.prototype.resume = function () {
|
121 | return this._socket.resume();
|
122 | };
|
123 |
|
124 | Terminal.prototype.setEncoding = function (encoding) {
|
125 | if (this._socket._decoder) {
|
126 | delete this._socket._decoder;
|
127 | }
|
128 | if (encoding) {
|
129 | this._socket.setEncoding(encoding);
|
130 | }
|
131 | };
|
132 | Terminal.prototype.addListener = function (eventName, listener) { this.on(eventName, listener); };
|
133 | Terminal.prototype.on = function (eventName, listener) {
|
134 | if (eventName === 'close') {
|
135 | this._internalee.on('close', listener);
|
136 | return;
|
137 | }
|
138 | this._socket.on(eventName, listener);
|
139 | };
|
140 | Terminal.prototype.emit = function (eventName) {
|
141 | var args = [];
|
142 | for (var _i = 1; _i < arguments.length; _i++) {
|
143 | args[_i - 1] = arguments[_i];
|
144 | }
|
145 | if (eventName === 'close') {
|
146 | return this._internalee.emit.apply(this._internalee, arguments);
|
147 | }
|
148 | return this._socket.emit.apply(this._socket, arguments);
|
149 | };
|
150 | Terminal.prototype.listeners = function (eventName) {
|
151 | return this._socket.listeners(eventName);
|
152 | };
|
153 | Terminal.prototype.removeListener = function (eventName, listener) {
|
154 | this._socket.removeListener(eventName, listener);
|
155 | };
|
156 | Terminal.prototype.removeAllListeners = function (eventName) {
|
157 | this._socket.removeAllListeners(eventName);
|
158 | };
|
159 | Terminal.prototype.once = function (eventName, listener) {
|
160 | this._socket.once(eventName, listener);
|
161 | };
|
162 | Terminal.prototype._close = function () {
|
163 | this._socket.writable = false;
|
164 | this._socket.readable = false;
|
165 | this.write = function () { };
|
166 | this.end = function () { };
|
167 | this._writable = false;
|
168 | this._readable = false;
|
169 | };
|
170 | Terminal.prototype._parseEnv = function (env) {
|
171 | var keys = Object.keys(env || {});
|
172 | var pairs = [];
|
173 | for (var i = 0; i < keys.length; i++) {
|
174 | pairs.push(keys[i] + '=' + env[keys[i]]);
|
175 | }
|
176 | return pairs;
|
177 | };
|
178 | return Terminal;
|
179 | }());
|
180 | exports.Terminal = Terminal;
|
181 |
|
\ | No newline at end of file |