bare-tty 1.0.7 → 2.0.1

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.
Files changed (4) hide show
  1. package/README.md +2 -2
  2. package/binding.c +6 -18
  3. package/index.js +86 -16
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -9,9 +9,9 @@ npm install bare-tty
9
9
  ## Usage
10
10
 
11
11
  ```js
12
- const TTY = require('bare-tty')
12
+ const tty = require('bare-tty')
13
13
 
14
- const stdout = new TTY(1)
14
+ const stdout = new tty.WriteStream(1)
15
15
 
16
16
  stdout.write('Hello world!\n')
17
17
  ```
package/binding.c CHANGED
@@ -369,8 +369,6 @@ bare_tty_resume (js_env_t *env, js_callback_info_t *info) {
369
369
  err = js_get_arraybuffer_info(env, argv[0], (void **) &tty, NULL);
370
370
  assert(err == 0);
371
371
 
372
- if (!uv_is_readable((uv_stream_t *) &tty->handle)) return NULL;
373
-
374
372
  err = uv_read_start((uv_stream_t *) &tty->handle, on_alloc, on_read);
375
373
 
376
374
  if (err < 0) {
@@ -397,8 +395,6 @@ bare_tty_pause (js_env_t *env, js_callback_info_t *info) {
397
395
  err = js_get_arraybuffer_info(env, argv[0], (void **) &tty, NULL);
398
396
  assert(err == 0);
399
397
 
400
- if (!uv_is_readable((uv_stream_t *) &tty->handle)) return NULL;
401
-
402
398
  err = uv_read_stop((uv_stream_t *) &tty->handle);
403
399
 
404
400
  if (err < 0) {
@@ -425,17 +421,14 @@ bare_tty_close (js_env_t *env, js_callback_info_t *info) {
425
421
  err = js_get_arraybuffer_info(env, argv[0], (void **) &tty, NULL);
426
422
  assert(err == 0);
427
423
 
428
- uv_close((uv_handle_t *) &tty->handle, on_close);
424
+ err = uv_tty_set_mode(&tty->handle, UV_TTY_MODE_NORMAL);
429
425
 
430
- return NULL;
431
- }
432
-
433
- static js_value_t *
434
- bare_tty_reset (js_env_t *env, js_callback_info_t *info) {
435
- int err;
426
+ if (err < 0) {
427
+ js_throw_error(env, uv_err_name(err), uv_strerror(err));
428
+ return NULL;
429
+ }
436
430
 
437
- err = uv_tty_reset_mode();
438
- assert(err == 0);
431
+ uv_close((uv_handle_t *) &tty->handle, on_close);
439
432
 
440
433
  return NULL;
441
434
  }
@@ -502,11 +495,6 @@ init (js_env_t *env, js_value_t *exports) {
502
495
  js_create_function(env, "close", -1, bare_tty_close, NULL, &fn);
503
496
  js_set_named_property(env, exports, "close", fn);
504
497
  }
505
- {
506
- js_value_t *fn;
507
- js_create_function(env, "reset", -1, bare_tty_reset, NULL, &fn);
508
- js_set_named_property(env, exports, "reset", fn);
509
- }
510
498
  {
511
499
  js_value_t *fn;
512
500
  js_create_function(env, "setMode", -1, bare_tty_set_mode, NULL, &fn);
package/index.js CHANGED
@@ -1,40 +1,107 @@
1
- const { Duplex } = require('streamx')
1
+ const { Readable, Writable } = require('streamx')
2
2
  const binding = require('./binding')
3
3
 
4
4
  const DEFAULT_READ_BUFFER = 65536
5
5
 
6
- process.on('exit', () => binding.reset())
7
-
8
- module.exports = exports = class TTY extends Duplex {
6
+ exports.ReadStream = class TTYReadStream extends Readable {
9
7
  constructor (fd, opts = {}) {
10
- super({ mapWritable })
8
+ super()
11
9
 
12
10
  const {
13
11
  readBufferSize = DEFAULT_READ_BUFFER,
14
12
  allowHalfOpen = true
15
13
  } = opts
16
14
 
17
- this._pendingWrite = null
18
- this._pendingFinal = null
19
15
  this._pendingDestroy = null
20
16
 
17
+ this._reading = false
21
18
  this._allowHalfOpen = allowHalfOpen
22
19
 
23
20
  this._buffer = Buffer.alloc(readBufferSize)
24
21
 
25
22
  this._handle = binding.init(fd, this._buffer, this,
26
- this._onwrite,
27
- this._onfinal,
23
+ noop,
24
+ noop,
28
25
  this._onread,
29
26
  this._onclose
30
27
  )
31
28
  }
32
29
 
33
- _open (cb) {
34
- binding.resume(this._handle)
30
+ get isTTY () {
31
+ return true
32
+ }
33
+
34
+ setMode (mode) {
35
+ binding.setMode(this._handle, mode)
36
+ }
37
+
38
+ _read (cb) {
39
+ if (!this._reading) {
40
+ this._reading = true
41
+ binding.resume(this._handle)
42
+ }
43
+ cb(null)
44
+ }
45
+
46
+ _destroy (cb) {
47
+ this._pendingDestroy = cb
48
+ binding.close(this._handle)
49
+ }
50
+
51
+ _continueDestroy () {
52
+ if (this._pendingDestroy === null) return
53
+ const cb = this._pendingDestroy
54
+ this._pendingDestroy = null
35
55
  cb(null)
36
56
  }
37
57
 
58
+ _onread (err, read) {
59
+ if (err) {
60
+ this.destroy(err)
61
+ return
62
+ }
63
+
64
+ if (read === 0) {
65
+ this.push(null)
66
+ if (this._allowHalfOpen === false) this.end()
67
+ return
68
+ }
69
+
70
+ const copy = Buffer.allocUnsafe(read)
71
+ copy.set(this._buffer.subarray(0, read))
72
+
73
+ if (this.push(copy) === false && this.destroying === false) {
74
+ this._reading = false
75
+ binding.pause(this._handle)
76
+ }
77
+ }
78
+
79
+ _onclose () {
80
+ this._handle = null
81
+ this._continueDestroy()
82
+ }
83
+ }
84
+
85
+ exports.WriteStream = class TTYWriteStream extends Writable {
86
+ constructor (fd, opts = {}) {
87
+ super({ mapWritable })
88
+
89
+ this._pendingWrite = null
90
+ this._pendingFinal = null
91
+ this._pendingDestroy = null
92
+
93
+ this._handle = binding.init(fd, empty, this,
94
+ this._onwrite,
95
+ this._onfinal,
96
+ noop,
97
+ this._onclose
98
+ )
99
+ }
100
+
101
+ get isTTY () {
102
+ return true
103
+ }
104
+
38
105
  _writev (datas, cb) {
39
106
  this._pendingWrite = cb
40
107
  binding.writev(this._handle, datas)
@@ -90,7 +157,10 @@ module.exports = exports = class TTY extends Duplex {
90
157
  const copy = Buffer.allocUnsafe(read)
91
158
  copy.set(this._buffer.subarray(0, read))
92
159
 
93
- this.push(copy)
160
+ if (this.push(copy) === false && this.destroying === false) {
161
+ this._reading = false
162
+ binding.pause(this._handle)
163
+ }
94
164
  }
95
165
 
96
166
  _onfinal (err) {
@@ -101,10 +171,6 @@ module.exports = exports = class TTY extends Duplex {
101
171
  this._handle = null
102
172
  this._continueDestroy()
103
173
  }
104
-
105
- setMode (mode) {
106
- binding.setMode(this._handle, mode)
107
- }
108
174
  }
109
175
 
110
176
  exports.constants = {
@@ -113,6 +179,10 @@ exports.constants = {
113
179
  MODE_IO: binding.MODE_IO || 0
114
180
  }
115
181
 
182
+ const empty = Buffer.alloc(0)
183
+
184
+ function noop () {}
185
+
116
186
  function mapWritable (buf) {
117
187
  return typeof buf === 'string' ? Buffer.from(buf) : buf
118
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-tty",
3
- "version": "1.0.7",
3
+ "version": "2.0.1",
4
4
  "description": "Native TTY streams for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [