-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnassh_stream_tty.js
190 lines (160 loc) · 4.67 KB
/
nassh_stream_tty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* The buffer for input from a terminal.
*
* This is necessary when /dev/tty and stdin can be separate streams. In that
* case, the input from the user must be buffered, and data must only be given
* to the first stream that reads it.
*
* @constructor
*/
nassh.InputBuffer = function() {
// The buffered data.
this.data_ = '';
// The queue of readers that are waiting for data. Readers are only queued
// when they attempt to read and there is no data available.
/**
* @const
* @private {!Array<{size: number, onRead: function(string):boolean}>}
*/
this.pendingReaders_ = [];
// This event is fired with the value true when there is data available to be
// read, and false when the buffer is empty. It is only fired when this
// status changes.
this.onDataAvailable = lib.Event();
};
/**
* Write data to the input buffer.
*
* This may call callbacks for pending readers.
*
* @param {string} data
*/
nassh.InputBuffer.prototype.write = function(data) {
const wasAvailable = this.data_.length != 0;
this.data_ += data;
// First, send data to the pending readers.
for (let i = 0; i < this.pendingReaders_.length; i++) {
const onRead = this.pendingReaders_[i].onRead;
let size = this.pendingReaders_[i].size;
if (size > this.data_.length) {
size = this.data_.length;
}
if (size == 0) {
break;
}
const rv = this.data_.slice(0, size);
if (onRead(rv)) {
this.data_ = this.data_.slice(size);
}
this.pendingReaders_.shift();
}
// Now, if data is still available, notify.
if (this.data_.length > 0 && !wasAvailable) {
this.onDataAvailable(true);
}
};
/**
* Read data from the input buffer.
*
* If there is no data available to be read, this read will be queued, and
* onRead will be later called when data is written to the input buffer.
*
* This only happens if there is no data available in the buffer. If there is
* not enough data available, onRead is called with all of the data in the
* buffer.
*
* @param {number} size
* @param {function(string)} onRead
*/
nassh.InputBuffer.prototype.read = function(size, onRead) {
const avail = this.data_.length;
if (avail == 0) {
// No data is available. Wait for data to be available and send it to the
// queued readers.
this.pendingReaders_.push({size: size, onRead: onRead});
return;
}
if (size > avail) {
size = avail;
}
const rv = this.data_.slice(0, size);
if (onRead(rv)) {
this.data_ = this.data_.slice(size);
}
if (this.data_.length == 0) {
this.onDataAvailable(false);
}
};
/**
* The /dev/tty stream.
*
* This stream allows reads (from an nassh.InputBuffer) and writes (to a
* hterm.Terminal.IO). It is used for /dev/tty, as well as stdin, stdout and
* stderr when they are reading from/writing to a terminal.
*
* @param {number} fd
* @param {!Object} info
* @constructor
* @extends {nassh.Stream}
*/
nassh.Stream.Tty = function(fd, info) {
nassh.Stream.apply(this, [fd]);
this.encoder_ = new TextEncoder();
};
nassh.Stream.Tty.prototype = Object.create(nassh.Stream.prototype);
/** @override */
nassh.Stream.Tty.constructor = nassh.Stream.Tty;
/**
* @param {!Object} settings
* @param {function(boolean)} onOpen
* @override
*/
nassh.Stream.Tty.prototype.asyncOpen = function(settings, onOpen) {
this.allowRead_ = settings.allowRead;
this.allowWrite_ = settings.allowWrite;
this.inputBuffer_ = settings.inputBuffer;
this.io_ = settings.io;
this.acknowledgeCount_ = 0;
setTimeout(function() { onOpen(true); }, 0);
};
/**
* @param {number} size
* @param {function(!ArrayBuffer)} onRead
* @override
*/
nassh.Stream.Tty.prototype.asyncRead = function(size, onRead) {
if (!this.open) {
throw nassh.Stream.ERR_STREAM_CLOSED;
}
if (!this.allowRead_) {
throw nassh.Stream.ERR_STREAM_CANT_READ;
}
this.inputBuffer_.read(size, (data) => {
if (!this.open) {
return false;
}
// Turn the UTF-16 JavaScript string into a UTF-8 array buffer.
const buf = this.encoder_.encode(data).buffer;
setTimeout(() => onRead(buf), 0);
return true;
});
};
/**
* @param {!ArrayBuffer} data
* @param {function(number)} onSuccess
* @override
*/
nassh.Stream.Tty.prototype.asyncWrite = function(data, onSuccess) {
if (!this.open) {
throw nassh.Stream.ERR_STREAM_CLOSED;
}
if (!this.allowWrite_) {
throw nassh.Stream.ERR_STREAM_CANT_WRITE;
}
this.acknowledgeCount_ += data.byteLength;
this.io_.writeUTF8(data);
setTimeout(() => { onSuccess(this.acknowledgeCount_); }, 0);
};