bare-tty 1.0.6 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/binding.c +7 -4
- package/index.js +99 -27
- package/package.json +1 -1
package/README.md
CHANGED
package/binding.c
CHANGED
|
@@ -243,6 +243,13 @@ bare_tty_init (js_env_t *env, js_callback_info_t *info) {
|
|
|
243
243
|
return NULL;
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
+
err = uv_stream_set_blocking((uv_stream_t *) &tty->handle, true);
|
|
247
|
+
|
|
248
|
+
if (err < 0) {
|
|
249
|
+
js_throw_error(env, uv_err_name(err), uv_strerror(err));
|
|
250
|
+
return NULL;
|
|
251
|
+
}
|
|
252
|
+
|
|
246
253
|
tty->env = env;
|
|
247
254
|
|
|
248
255
|
err = js_get_typedarray_info(env, argv[1], NULL, (void **) &tty->read.base, &tty->read.len, NULL, NULL);
|
|
@@ -362,8 +369,6 @@ bare_tty_resume (js_env_t *env, js_callback_info_t *info) {
|
|
|
362
369
|
err = js_get_arraybuffer_info(env, argv[0], (void **) &tty, NULL);
|
|
363
370
|
assert(err == 0);
|
|
364
371
|
|
|
365
|
-
if (!uv_is_readable((uv_stream_t *) &tty->handle)) return NULL;
|
|
366
|
-
|
|
367
372
|
err = uv_read_start((uv_stream_t *) &tty->handle, on_alloc, on_read);
|
|
368
373
|
|
|
369
374
|
if (err < 0) {
|
|
@@ -390,8 +395,6 @@ bare_tty_pause (js_env_t *env, js_callback_info_t *info) {
|
|
|
390
395
|
err = js_get_arraybuffer_info(env, argv[0], (void **) &tty, NULL);
|
|
391
396
|
assert(err == 0);
|
|
392
397
|
|
|
393
|
-
if (!uv_is_readable((uv_stream_t *) &tty->handle)) return NULL;
|
|
394
|
-
|
|
395
398
|
err = uv_read_stop((uv_stream_t *) &tty->handle);
|
|
396
399
|
|
|
397
400
|
if (err < 0) {
|
package/index.js
CHANGED
|
@@ -1,73 +1,142 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { Readable, Writable } = require('streamx')
|
|
2
2
|
const binding = require('./binding')
|
|
3
3
|
|
|
4
4
|
const DEFAULT_READ_BUFFER = 65536
|
|
5
5
|
|
|
6
6
|
process.on('exit', () => binding.reset())
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
exports.ReadStream = class TTYReadStream extends Readable {
|
|
9
9
|
constructor (fd, opts = {}) {
|
|
10
|
-
super(
|
|
10
|
+
super()
|
|
11
11
|
|
|
12
12
|
const {
|
|
13
13
|
readBufferSize = DEFAULT_READ_BUFFER,
|
|
14
14
|
allowHalfOpen = true
|
|
15
15
|
} = opts
|
|
16
16
|
|
|
17
|
-
this.
|
|
18
|
-
this._finalCallback = null
|
|
19
|
-
this._destroyCallback = null
|
|
17
|
+
this._pendingDestroy = null
|
|
20
18
|
|
|
19
|
+
this._reading = false
|
|
21
20
|
this._allowHalfOpen = allowHalfOpen
|
|
22
21
|
|
|
23
22
|
this._buffer = Buffer.alloc(readBufferSize)
|
|
24
23
|
|
|
25
24
|
this._handle = binding.init(fd, this._buffer, this,
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
noop,
|
|
26
|
+
noop,
|
|
28
27
|
this._onread,
|
|
29
28
|
this._onclose
|
|
30
29
|
)
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
get isTTY () {
|
|
33
|
+
return true
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
setMode (mode) {
|
|
37
|
+
binding.setMode(this._handle, mode)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_read (cb) {
|
|
41
|
+
if (!this._reading) {
|
|
42
|
+
this._reading = true
|
|
43
|
+
binding.resume(this._handle)
|
|
44
|
+
}
|
|
35
45
|
cb(null)
|
|
36
46
|
}
|
|
37
47
|
|
|
48
|
+
_destroy (cb) {
|
|
49
|
+
this._pendingDestroy = cb
|
|
50
|
+
binding.close(this._handle)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_continueDestroy () {
|
|
54
|
+
if (this._pendingDestroy === null) return
|
|
55
|
+
const cb = this._pendingDestroy
|
|
56
|
+
this._pendingDestroy = null
|
|
57
|
+
cb(null)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_onread (err, read) {
|
|
61
|
+
if (err) {
|
|
62
|
+
this.destroy(err)
|
|
63
|
+
return
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (read === 0) {
|
|
67
|
+
this.push(null)
|
|
68
|
+
if (this._allowHalfOpen === false) this.end()
|
|
69
|
+
return
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const copy = Buffer.allocUnsafe(read)
|
|
73
|
+
copy.set(this._buffer.subarray(0, read))
|
|
74
|
+
|
|
75
|
+
if (this.push(copy) === false && this.destroying === false) {
|
|
76
|
+
this._reading = false
|
|
77
|
+
binding.pause(this._handle)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
_onclose () {
|
|
82
|
+
this._handle = null
|
|
83
|
+
this._continueDestroy()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
exports.WriteStream = class TTYWriteStream extends Writable {
|
|
88
|
+
constructor (fd, opts = {}) {
|
|
89
|
+
super({ mapWritable })
|
|
90
|
+
|
|
91
|
+
this._pendingWrite = null
|
|
92
|
+
this._pendingFinal = null
|
|
93
|
+
this._pendingDestroy = null
|
|
94
|
+
|
|
95
|
+
this._handle = binding.init(fd, empty, this,
|
|
96
|
+
this._onwrite,
|
|
97
|
+
this._onfinal,
|
|
98
|
+
noop,
|
|
99
|
+
this._onclose
|
|
100
|
+
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
get isTTY () {
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
106
|
+
|
|
38
107
|
_writev (datas, cb) {
|
|
39
|
-
this.
|
|
108
|
+
this._pendingWrite = cb
|
|
40
109
|
binding.writev(this._handle, datas)
|
|
41
110
|
}
|
|
42
111
|
|
|
43
112
|
_final (cb) {
|
|
44
|
-
this.
|
|
113
|
+
this._pendingFinal = cb
|
|
45
114
|
binding.end(this._handle)
|
|
46
115
|
}
|
|
47
116
|
|
|
48
117
|
_destroy (cb) {
|
|
49
|
-
this.
|
|
118
|
+
this._pendingDestroy = cb
|
|
50
119
|
binding.close(this._handle)
|
|
51
120
|
}
|
|
52
121
|
|
|
53
122
|
_continueWrite (err) {
|
|
54
|
-
if (this.
|
|
55
|
-
const cb = this.
|
|
56
|
-
this.
|
|
123
|
+
if (this._pendingWrite === null) return
|
|
124
|
+
const cb = this._pendingWrite
|
|
125
|
+
this._pendingWrite = null
|
|
57
126
|
cb(err)
|
|
58
127
|
}
|
|
59
128
|
|
|
60
129
|
_continueFinal (err) {
|
|
61
|
-
if (this.
|
|
62
|
-
const cb = this.
|
|
63
|
-
this.
|
|
130
|
+
if (this._pendingFinal === null) return
|
|
131
|
+
const cb = this._pendingFinal
|
|
132
|
+
this._pendingFinal = null
|
|
64
133
|
cb(err)
|
|
65
134
|
}
|
|
66
135
|
|
|
67
136
|
_continueDestroy () {
|
|
68
|
-
if (this.
|
|
69
|
-
const cb = this.
|
|
70
|
-
this.
|
|
137
|
+
if (this._pendingDestroy === null) return
|
|
138
|
+
const cb = this._pendingDestroy
|
|
139
|
+
this._pendingDestroy = null
|
|
71
140
|
cb(null)
|
|
72
141
|
}
|
|
73
142
|
|
|
@@ -90,7 +159,10 @@ module.exports = exports = class TTY extends Duplex {
|
|
|
90
159
|
const copy = Buffer.allocUnsafe(read)
|
|
91
160
|
copy.set(this._buffer.subarray(0, read))
|
|
92
161
|
|
|
93
|
-
this.push(copy)
|
|
162
|
+
if (this.push(copy) === false && this.destroying === false) {
|
|
163
|
+
this._reading = false
|
|
164
|
+
binding.pause(this._handle)
|
|
165
|
+
}
|
|
94
166
|
}
|
|
95
167
|
|
|
96
168
|
_onfinal (err) {
|
|
@@ -101,10 +173,6 @@ module.exports = exports = class TTY extends Duplex {
|
|
|
101
173
|
this._handle = null
|
|
102
174
|
this._continueDestroy()
|
|
103
175
|
}
|
|
104
|
-
|
|
105
|
-
setMode (mode) {
|
|
106
|
-
binding.setMode(this._handle, mode)
|
|
107
|
-
}
|
|
108
176
|
}
|
|
109
177
|
|
|
110
178
|
exports.constants = {
|
|
@@ -113,6 +181,10 @@ exports.constants = {
|
|
|
113
181
|
MODE_IO: binding.MODE_IO || 0
|
|
114
182
|
}
|
|
115
183
|
|
|
184
|
+
const empty = Buffer.alloc(0)
|
|
185
|
+
|
|
186
|
+
function noop () {}
|
|
187
|
+
|
|
116
188
|
function mapWritable (buf) {
|
|
117
189
|
return typeof buf === 'string' ? Buffer.from(buf) : buf
|
|
118
190
|
}
|