bare-tty 1.0.7 → 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 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) {
package/index.js CHANGED
@@ -1,40 +1,109 @@
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
6
  process.on('exit', () => binding.reset())
7
7
 
8
- module.exports = exports = class TTY extends Duplex {
8
+ exports.ReadStream = class TTYReadStream extends Readable {
9
9
  constructor (fd, opts = {}) {
10
- super({ mapWritable })
10
+ super()
11
11
 
12
12
  const {
13
13
  readBufferSize = DEFAULT_READ_BUFFER,
14
14
  allowHalfOpen = true
15
15
  } = opts
16
16
 
17
- this._pendingWrite = null
18
- this._pendingFinal = null
19
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
- this._onwrite,
27
- this._onfinal,
25
+ noop,
26
+ noop,
28
27
  this._onread,
29
28
  this._onclose
30
29
  )
31
30
  }
32
31
 
33
- _open (cb) {
34
- binding.resume(this._handle)
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
108
  this._pendingWrite = cb
40
109
  binding.writev(this._handle, datas)
@@ -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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-tty",
3
- "version": "1.0.7",
3
+ "version": "2.0.0",
4
4
  "description": "Native TTY streams for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [