bare-tty 3.2.4 → 3.3.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
@@ -3,7 +3,7 @@
3
3
  Native TTY streams for JavaScript.
4
4
 
5
5
  ```
6
- npm install bare-tty
6
+ npm i bare-tty
7
7
  ```
8
8
 
9
9
  ## Usage
package/binding.c CHANGED
@@ -22,7 +22,7 @@ typedef struct {
22
22
  } bare_tty_t;
23
23
 
24
24
  static void
25
- on_write (uv_write_t *req, int status) {
25
+ bare_tty__on_write (uv_write_t *req, int status) {
26
26
  int err;
27
27
 
28
28
  bare_tty_t *tty = (bare_tty_t *) req->data;
@@ -66,7 +66,7 @@ on_write (uv_write_t *req, int status) {
66
66
  }
67
67
 
68
68
  static void
69
- on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
69
+ bare_tty__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
70
70
  if (nread == UV_EOF) nread = 0;
71
71
  else if (nread == 0) return;
72
72
 
@@ -119,7 +119,7 @@ on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
119
119
  }
120
120
 
121
121
  static void
122
- on_close (uv_handle_t *handle) {
122
+ bare_tty__on_close (uv_handle_t *handle) {
123
123
  int err;
124
124
 
125
125
  bare_tty_t *tty = (bare_tty_t *) handle;
@@ -157,7 +157,7 @@ on_close (uv_handle_t *handle) {
157
157
  }
158
158
 
159
159
  static void
160
- on_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
160
+ bare_tty__on_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
161
161
  bare_tty_t *tty = (bare_tty_t *) handle;
162
162
 
163
163
  *buf = tty->read;
@@ -258,7 +258,7 @@ bare_tty_writev (js_env_t *env, js_callback_info_t *info) {
258
258
 
259
259
  req->data = tty;
260
260
 
261
- err = uv_write(req, (uv_stream_t *) &tty->handle, bufs, bufs_len, on_write);
261
+ err = uv_write(req, (uv_stream_t *) &tty->handle, bufs, bufs_len, bare_tty__on_write);
262
262
 
263
263
  free(bufs);
264
264
 
@@ -286,7 +286,7 @@ bare_tty_resume (js_env_t *env, js_callback_info_t *info) {
286
286
  err = js_get_arraybuffer_info(env, argv[0], (void **) &tty, NULL);
287
287
  assert(err == 0);
288
288
 
289
- err = uv_read_start((uv_stream_t *) &tty->handle, on_alloc, on_read);
289
+ err = uv_read_start((uv_stream_t *) &tty->handle, bare_tty__on_alloc, bare_tty__on_read);
290
290
 
291
291
  if (err < 0) {
292
292
  js_throw_error(env, uv_err_name(err), uv_strerror(err));
@@ -344,7 +344,7 @@ bare_tty_close (js_env_t *env, js_callback_info_t *info) {
344
344
  // close an output stream.
345
345
  (void) err;
346
346
 
347
- uv_close((uv_handle_t *) &tty->handle, on_close);
347
+ uv_close((uv_handle_t *) &tty->handle, bare_tty__on_close);
348
348
 
349
349
  return NULL;
350
350
  }
@@ -448,66 +448,45 @@ bare_tty_is_tty (js_env_t *env, js_callback_info_t *info) {
448
448
  }
449
449
 
450
450
  static js_value_t *
451
- init (js_env_t *env, js_value_t *exports) {
452
- {
453
- js_value_t *fn;
454
- js_create_function(env, "init", -1, bare_tty_init, NULL, &fn);
455
- js_set_named_property(env, exports, "init", fn);
456
- }
457
- {
458
- js_value_t *fn;
459
- js_create_function(env, "writev", -1, bare_tty_writev, NULL, &fn);
460
- js_set_named_property(env, exports, "writev", fn);
461
- }
462
- {
463
- js_value_t *fn;
464
- js_create_function(env, "resume", -1, bare_tty_resume, NULL, &fn);
465
- js_set_named_property(env, exports, "resume", fn);
466
- }
467
- {
468
- js_value_t *fn;
469
- js_create_function(env, "pause", -1, bare_tty_pause, NULL, &fn);
470
- js_set_named_property(env, exports, "pause", fn);
471
- }
472
- {
473
- js_value_t *fn;
474
- js_create_function(env, "close", -1, bare_tty_close, NULL, &fn);
475
- js_set_named_property(env, exports, "close", fn);
476
- }
477
- {
478
- js_value_t *fn;
479
- js_create_function(env, "setMode", -1, bare_tty_set_mode, NULL, &fn);
480
- js_set_named_property(env, exports, "setMode", fn);
481
- }
482
- {
483
- js_value_t *fn;
484
- js_create_function(env, "getWindowSize", -1, bare_tty_get_window_size, NULL, &fn);
485
- js_set_named_property(env, exports, "getWindowSize", fn);
486
- }
487
- {
488
- js_value_t *fn;
489
- js_create_function(env, "isTTY", -1, bare_tty_is_tty, NULL, &fn);
490
- js_set_named_property(env, exports, "isTTY", fn);
491
- }
492
- {
493
- js_value_t *val;
494
- js_create_uint32(env, UV_TTY_MODE_NORMAL, &val);
495
- js_set_named_property(env, exports, "MODE_NORMAL", val);
451
+ bare_tty_exports (js_env_t *env, js_value_t *exports) {
452
+ int err;
453
+
454
+ #define V(name, fn) \
455
+ { \
456
+ js_value_t *val; \
457
+ err = js_create_function(env, name, -1, fn, NULL, &val); \
458
+ assert(err == 0); \
459
+ err = js_set_named_property(env, exports, name, val); \
460
+ assert(err == 0); \
496
461
  }
497
- {
498
- js_value_t *val;
499
- js_create_uint32(env, UV_TTY_MODE_RAW, &val);
500
- js_set_named_property(env, exports, "MODE_RAW", val);
462
+
463
+ V("init", bare_tty_init)
464
+ V("writev", bare_tty_writev)
465
+ V("resume", bare_tty_resume)
466
+ V("pause", bare_tty_pause)
467
+ V("close", bare_tty_close)
468
+ V("setMode", bare_tty_set_mode)
469
+ V("getWindowSize", bare_tty_get_window_size)
470
+ V("isTTY", bare_tty_is_tty)
471
+ #undef V
472
+
473
+ #define V(name, n) \
474
+ { \
475
+ js_value_t *val; \
476
+ err = js_create_uint32(env, n, &val); \
477
+ assert(err == 0); \
478
+ err = js_set_named_property(env, exports, name, val); \
479
+ assert(err == 0); \
501
480
  }
481
+
482
+ V("MODE_NORMAL", UV_TTY_MODE_NORMAL)
483
+ V("MODE_RAW", UV_TTY_MODE_RAW)
502
484
  #ifndef _WIN32
503
- {
504
- js_value_t *val;
505
- js_create_uint32(env, UV_TTY_MODE_IO, &val);
506
- js_set_named_property(env, exports, "MODE_IO", val);
507
- }
485
+ V("MODE_IO", UV_TTY_MODE_IO)
508
486
  #endif
487
+ #undef V
509
488
 
510
489
  return exports;
511
490
  }
512
491
 
513
- BARE_MODULE(bare_tty, init)
492
+ BARE_MODULE(bare_tty, bare_tty_exports)
package/index.js CHANGED
@@ -1,24 +1,25 @@
1
1
  /* global Bare */
2
2
  const { Readable, Writable } = require('streamx')
3
3
  const binding = require('./binding')
4
+ const constants = require('./lib/constants')
4
5
 
5
- const DEFAULT_READ_BUFFER = 65536
6
+ const defaultReadBufferSize = 65536
6
7
 
7
8
  exports.ReadStream = class TTYReadStream extends Readable {
8
9
  constructor (fd, opts = {}) {
9
10
  super()
10
11
 
11
12
  const {
12
- readBufferSize = DEFAULT_READ_BUFFER,
13
+ readBufferSize = defaultReadBufferSize,
13
14
  allowHalfOpen = true
14
15
  } = opts
15
16
 
16
- this._pendingDestroy = null
17
+ this._state = 0
17
18
 
18
- this._reading = false
19
- this._closing = false
20
19
  this._allowHalfOpen = allowHalfOpen
21
20
 
21
+ this._pendingDestroy = null
22
+
22
23
  this._buffer = Buffer.alloc(readBufferSize)
23
24
 
24
25
  this._handle = binding.init(fd, this._buffer, this,
@@ -39,27 +40,28 @@ exports.ReadStream = class TTYReadStream extends Readable {
39
40
  }
40
41
 
41
42
  setRawMode (mode) {
42
- this.setMode(mode ? constants.MODE_RAW : constants.MODE_NORMAL)
43
+ this.setMode(mode ? constants.mode.RAW : constants.mode.NORMAL)
43
44
  }
44
45
 
45
46
  _read (cb) {
46
- if (!this._reading) {
47
- this._reading = true
47
+ if ((this._state & constants.state.READING) === 0) {
48
+ this._state |= constants.state.READING
48
49
  binding.resume(this._handle)
49
50
  }
51
+
50
52
  cb(null)
51
53
  }
52
54
 
53
55
  _predestroy () {
54
- if (this._closing) return
55
- this._closing = true
56
+ if (this._state & constants.state.CLOSING) return
57
+ this._state |= constants.state.CLOSING
56
58
  binding.close(this._handle)
57
59
  TTYReadStream._streams.delete(this)
58
60
  }
59
61
 
60
62
  _destroy (cb) {
61
- if (this._closing) return cb(null)
62
- this._closing = true
63
+ if (this._state & constants.state.CLOSING) return cb(null)
64
+ this._state |= constants.state.CLOSING
63
65
  this._pendingDestroy = cb
64
66
  binding.close(this._handle)
65
67
  TTYReadStream._streams.delete(this)
@@ -88,7 +90,7 @@ exports.ReadStream = class TTYReadStream extends Readable {
88
90
  copy.set(this._buffer.subarray(0, read))
89
91
 
90
92
  if (this.push(copy) === false && this.destroying === false) {
91
- this._reading = false
93
+ this._state &= ~constants.state.READING
92
94
  binding.pause(this._handle)
93
95
  }
94
96
  }
@@ -105,11 +107,11 @@ exports.WriteStream = class TTYWriteStream extends Writable {
105
107
  constructor (fd, opts = {}) {
106
108
  super({ mapWritable })
107
109
 
110
+ this._state = 0
111
+
108
112
  this._pendingWrite = null
109
113
  this._pendingDestroy = null
110
114
 
111
- this._closing = false
112
-
113
115
  this._handle = binding.init(fd, empty, this,
114
116
  this._onwrite,
115
117
  noop,
@@ -133,15 +135,15 @@ exports.WriteStream = class TTYWriteStream extends Writable {
133
135
  }
134
136
 
135
137
  _predestroy () {
136
- if (this._closing) return
137
- this._closing = true
138
+ if (this._state & constants.state.CLOSING) return
139
+ this._state |= constants.state.CLOSING
138
140
  binding.close(this._handle)
139
141
  TTYWriteStream._streams.delete(this)
140
142
  }
141
143
 
142
144
  _destroy (cb) {
143
- if (this._closing) return cb(null)
144
- this._closing = true
145
+ if (this._state & constants.state.CLOSING) return cb(null)
146
+ this._state |= constants.state.CLOSING
145
147
  this._pendingDestroy = cb
146
148
  binding.close(this._handle)
147
149
  TTYWriteStream._streams.delete(this)
@@ -165,27 +167,6 @@ exports.WriteStream = class TTYWriteStream extends Writable {
165
167
  this._continueWrite(err)
166
168
  }
167
169
 
168
- _onread (err, read) {
169
- if (err) {
170
- this.destroy(err)
171
- return
172
- }
173
-
174
- if (read === 0) {
175
- this.push(null)
176
- if (this._allowHalfOpen === false) this.end()
177
- return
178
- }
179
-
180
- const copy = Buffer.allocUnsafe(read)
181
- copy.set(this._buffer.subarray(0, read))
182
-
183
- if (this.push(copy) === false && this.destroying === false) {
184
- this._reading = false
185
- binding.pause(this._handle)
186
- }
187
- }
188
-
189
170
  _onclose () {
190
171
  this._handle = null
191
172
  this._continueDestroy()
@@ -194,13 +175,9 @@ exports.WriteStream = class TTYWriteStream extends Writable {
194
175
  static _streams = new Set()
195
176
  }
196
177
 
197
- exports.isTTY = binding.isTTY
178
+ exports.constants = constants
198
179
 
199
- const constants = exports.constants = {
200
- MODE_NORMAL: binding.MODE_NORMAL,
201
- MODE_RAW: binding.MODE_RAW,
202
- MODE_IO: binding.MODE_IO || 0
203
- }
180
+ exports.isTTY = binding.isTTY
204
181
 
205
182
  Bare
206
183
  .on('exit', () => {
@@ -0,0 +1,21 @@
1
+ const binding = require('../binding')
2
+
3
+ module.exports = exports = {
4
+ mode: {
5
+ NORMAL: binding.MODE_NORMAL,
6
+ RAW: binding.MODE_RAW,
7
+ IO: binding.MODE_IO || 0
8
+ },
9
+
10
+ state: {
11
+ READING: 0x1,
12
+ CLOSING: 0x2
13
+ }
14
+ }
15
+
16
+ /** @deprecated Use `mode.NORMAL` */
17
+ exports.MODE_NORMAL = exports.mode.NORMAL
18
+ /** @deprecated Use `mode.RAW` */
19
+ exports.MODE_RAW = exports.mode.RAW
20
+ /** @deprecated Use `mode.IO` */
21
+ exports.MODE_IO = exports.mode.IO
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "name": "bare-tty",
3
- "version": "3.2.4",
3
+ "version": "3.3.0",
4
4
  "description": "Native TTY streams for JavaScript",
5
- "main": "index.js",
5
+ "exports": {
6
+ ".": "./index.js",
7
+ "./package": "./package.json",
8
+ "./constants": "./lib/constants.js"
9
+ },
6
10
  "files": [
7
11
  "index.js",
8
12
  "binding.c",
9
13
  "binding.js",
10
14
  "CMakeLists.txt",
15
+ "lib",
11
16
  "prebuilds"
12
17
  ],
13
18
  "addon": true,
Binary file
Binary file
Binary file
Binary file