bare-tty 4.0.1 → 5.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/CMakeLists.txt CHANGED
@@ -1,8 +1,8 @@
1
1
  cmake_minimum_required(VERSION 3.25)
2
2
 
3
- project(bare_tty C)
3
+ find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
4
4
 
5
- include(bare)
5
+ project(bare_tty C)
6
6
 
7
7
  add_bare_module(bare_tty)
8
8
 
package/binding.c CHANGED
@@ -19,10 +19,13 @@ typedef struct {
19
19
  js_ref_t *on_write;
20
20
  js_ref_t *on_read;
21
21
  js_ref_t *on_close;
22
+
23
+ js_deferred_teardown_t *teardown;
24
+ bool exiting;
22
25
  } bare_tty_t;
23
26
 
24
27
  static void
25
- bare_tty__on_write (uv_write_t *req, int status) {
28
+ bare_tty__on_write(uv_write_t *req, int status) {
26
29
  int err;
27
30
 
28
31
  bare_tty_t *tty = (bare_tty_t *) req->data;
@@ -66,7 +69,7 @@ bare_tty__on_write (uv_write_t *req, int status) {
66
69
  }
67
70
 
68
71
  static void
69
- bare_tty__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
72
+ bare_tty__on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
70
73
  if (nread == UV_EOF) nread = 0;
71
74
  else if (nread == 0) return;
72
75
 
@@ -119,13 +122,15 @@ bare_tty__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
119
122
  }
120
123
 
121
124
  static void
122
- bare_tty__on_close (uv_handle_t *handle) {
125
+ bare_tty__on_close(uv_handle_t *handle) {
123
126
  int err;
124
127
 
125
128
  bare_tty_t *tty = (bare_tty_t *) handle;
126
129
 
127
130
  js_env_t *env = tty->env;
128
131
 
132
+ if (tty->exiting) goto finalize;
133
+
129
134
  js_handle_scope_t *scope;
130
135
  err = js_open_handle_scope(env, &scope);
131
136
  assert(err == 0);
@@ -140,6 +145,13 @@ bare_tty__on_close (uv_handle_t *handle) {
140
145
 
141
146
  js_call_function(env, ctx, callback, 0, NULL, NULL);
142
147
 
148
+ err = js_close_handle_scope(env, scope);
149
+ assert(err == 0);
150
+
151
+ finalize:
152
+ err = js_finish_deferred_teardown_callback(tty->teardown);
153
+ assert(err == 0);
154
+
143
155
  err = js_delete_reference(env, tty->on_write);
144
156
  assert(err == 0);
145
157
 
@@ -151,20 +163,26 @@ bare_tty__on_close (uv_handle_t *handle) {
151
163
 
152
164
  err = js_delete_reference(env, tty->ctx);
153
165
  assert(err == 0);
166
+ }
154
167
 
155
- err = js_close_handle_scope(env, scope);
156
- assert(err == 0);
168
+ static void
169
+ bare_tty__on_teardown(js_deferred_teardown_t *handle, void *data) {
170
+ bare_tty_t *tty = (bare_tty_t *) data;
171
+
172
+ tty->exiting = true;
173
+
174
+ uv_close((uv_handle_t *) &tty->handle, bare_tty__on_close);
157
175
  }
158
176
 
159
177
  static void
160
- bare_tty__on_alloc (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
178
+ bare_tty__on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
161
179
  bare_tty_t *tty = (bare_tty_t *) handle;
162
180
 
163
181
  *buf = tty->read;
164
182
  }
165
183
 
166
184
  static js_value_t *
167
- bare_tty_init (js_env_t *env, js_callback_info_t *info) {
185
+ bare_tty_init(js_env_t *env, js_callback_info_t *info) {
168
186
  int err;
169
187
 
170
188
  size_t argc = 6;
@@ -201,6 +219,7 @@ bare_tty_init (js_env_t *env, js_callback_info_t *info) {
201
219
  (void) err;
202
220
 
203
221
  tty->env = env;
222
+ tty->exiting = false;
204
223
 
205
224
  err = js_get_typedarray_info(env, argv[1], NULL, (void **) &tty->read.base, (size_t *) &tty->read.len, NULL, NULL);
206
225
  assert(err == 0);
@@ -217,11 +236,14 @@ bare_tty_init (js_env_t *env, js_callback_info_t *info) {
217
236
  err = js_create_reference(env, argv[5], 1, &tty->on_close);
218
237
  assert(err == 0);
219
238
 
239
+ err = js_add_deferred_teardown_callback(env, bare_tty__on_teardown, (void *) tty, &tty->teardown);
240
+ assert(err == 0);
241
+
220
242
  return handle;
221
243
  }
222
244
 
223
245
  static js_value_t *
224
- bare_tty_writev (js_env_t *env, js_callback_info_t *info) {
246
+ bare_tty_writev(js_env_t *env, js_callback_info_t *info) {
225
247
  int err;
226
248
 
227
249
  size_t argc = 2;
@@ -271,7 +293,7 @@ bare_tty_writev (js_env_t *env, js_callback_info_t *info) {
271
293
  }
272
294
 
273
295
  static js_value_t *
274
- bare_tty_resume (js_env_t *env, js_callback_info_t *info) {
296
+ bare_tty_resume(js_env_t *env, js_callback_info_t *info) {
275
297
  int err;
276
298
 
277
299
  size_t argc = 1;
@@ -297,7 +319,7 @@ bare_tty_resume (js_env_t *env, js_callback_info_t *info) {
297
319
  }
298
320
 
299
321
  static js_value_t *
300
- bare_tty_pause (js_env_t *env, js_callback_info_t *info) {
322
+ bare_tty_pause(js_env_t *env, js_callback_info_t *info) {
301
323
  int err;
302
324
 
303
325
  size_t argc = 1;
@@ -323,7 +345,7 @@ bare_tty_pause (js_env_t *env, js_callback_info_t *info) {
323
345
  }
324
346
 
325
347
  static js_value_t *
326
- bare_tty_close (js_env_t *env, js_callback_info_t *info) {
348
+ bare_tty_close(js_env_t *env, js_callback_info_t *info) {
327
349
  int err;
328
350
 
329
351
  size_t argc = 1;
@@ -350,7 +372,7 @@ bare_tty_close (js_env_t *env, js_callback_info_t *info) {
350
372
  }
351
373
 
352
374
  static js_value_t *
353
- bare_tty_set_mode (js_env_t *env, js_callback_info_t *info) {
375
+ bare_tty_set_mode(js_env_t *env, js_callback_info_t *info) {
354
376
  int err;
355
377
 
356
378
  size_t argc = 2;
@@ -380,7 +402,7 @@ bare_tty_set_mode (js_env_t *env, js_callback_info_t *info) {
380
402
  }
381
403
 
382
404
  static js_value_t *
383
- bare_tty_get_window_size (js_env_t *env, js_callback_info_t *info) {
405
+ bare_tty_get_window_size(js_env_t *env, js_callback_info_t *info) {
384
406
  int err;
385
407
 
386
408
  size_t argc = 1;
@@ -425,7 +447,7 @@ bare_tty_get_window_size (js_env_t *env, js_callback_info_t *info) {
425
447
  }
426
448
 
427
449
  static js_value_t *
428
- bare_tty_is_tty (js_env_t *env, js_callback_info_t *info) {
450
+ bare_tty_is_tty(js_env_t *env, js_callback_info_t *info) {
429
451
  int err;
430
452
 
431
453
  size_t argc = 1;
@@ -448,7 +470,7 @@ bare_tty_is_tty (js_env_t *env, js_callback_info_t *info) {
448
470
  }
449
471
 
450
472
  static js_value_t *
451
- bare_tty_exports (js_env_t *env, js_value_t *exports) {
473
+ bare_tty_exports(js_env_t *env, js_value_t *exports) {
452
474
  int err;
453
475
 
454
476
  #define V(name, fn) \
package/index.js CHANGED
@@ -7,13 +7,11 @@ const constants = require('./lib/constants')
7
7
  const defaultReadBufferSize = 65536
8
8
 
9
9
  exports.ReadStream = class TTYReadStream extends Readable {
10
- constructor (fd, opts = {}) {
10
+ constructor(fd, opts = {}) {
11
11
  super()
12
12
 
13
- const {
14
- readBufferSize = defaultReadBufferSize,
15
- allowHalfOpen = true
16
- } = opts
13
+ const { readBufferSize = defaultReadBufferSize, allowHalfOpen = true } =
14
+ opts
17
15
 
18
16
  this._state = 0
19
17
 
@@ -23,58 +21,57 @@ exports.ReadStream = class TTYReadStream extends Readable {
23
21
 
24
22
  this._buffer = Buffer.alloc(readBufferSize)
25
23
 
26
- this._handle = binding.init(fd, this._buffer, this,
24
+ this._handle = binding.init(
25
+ fd,
26
+ this._buffer,
27
+ this,
27
28
  noop,
28
29
  this._onread,
29
30
  this._onclose
30
31
  )
31
-
32
- TTYReadStream._streams.add(this)
33
32
  }
34
33
 
35
- get isTTY () {
34
+ get isTTY() {
36
35
  return true
37
36
  }
38
37
 
39
- setMode (mode) {
38
+ setMode(mode) {
40
39
  binding.setMode(this._handle, mode)
41
40
  return this
42
41
  }
43
42
 
44
- setRawMode (mode) {
43
+ setRawMode(mode) {
45
44
  return this.setMode(mode ? constants.mode.RAW : constants.mode.NORMAL)
46
45
  }
47
46
 
48
- _read () {
47
+ _read() {
49
48
  if ((this._state & constants.state.READING) === 0) {
50
49
  this._state |= constants.state.READING
51
50
  binding.resume(this._handle)
52
51
  }
53
52
  }
54
53
 
55
- _predestroy () {
54
+ _predestroy() {
56
55
  if (this._state & constants.state.CLOSING) return
57
56
  this._state |= constants.state.CLOSING
58
57
  binding.close(this._handle)
59
- TTYReadStream._streams.delete(this)
60
58
  }
61
59
 
62
- _destroy (err, cb) {
60
+ _destroy(err, cb) {
63
61
  if (this._state & constants.state.CLOSING) return cb(err)
64
62
  this._state |= constants.state.CLOSING
65
63
  this._pendingDestroy = cb
66
64
  binding.close(this._handle)
67
- TTYReadStream._streams.delete(this)
68
65
  }
69
66
 
70
- _continueDestroy () {
67
+ _continueDestroy() {
71
68
  if (this._pendingDestroy === null) return
72
69
  const cb = this._pendingDestroy
73
70
  this._pendingDestroy = null
74
71
  cb(null)
75
72
  }
76
73
 
77
- _onread (err, read) {
74
+ _onread(err, read) {
78
75
  if (err) {
79
76
  this.destroy(err)
80
77
  return
@@ -95,16 +92,14 @@ exports.ReadStream = class TTYReadStream extends Readable {
95
92
  }
96
93
  }
97
94
 
98
- _onclose () {
95
+ _onclose() {
99
96
  this._handle = null
100
97
  this._continueDestroy()
101
98
  }
102
-
103
- static _streams = new Set()
104
99
  }
105
100
 
106
101
  exports.WriteStream = class TTYWriteStream extends Writable {
107
- constructor (fd, opts = {}) {
102
+ constructor(fd, opts = {}) {
108
103
  super()
109
104
 
110
105
  this._state = 0
@@ -113,7 +108,10 @@ exports.WriteStream = class TTYWriteStream extends Writable {
113
108
  this._pendingWrite = null
114
109
  this._pendingDestroy = null
115
110
 
116
- this._handle = binding.init(fd, empty, this,
111
+ this._handle = binding.init(
112
+ fd,
113
+ empty,
114
+ this,
117
115
  this._onwrite,
118
116
  noop,
119
117
  this._onclose
@@ -125,28 +123,31 @@ exports.WriteStream = class TTYWriteStream extends Writable {
125
123
  TTYWriteStream._streams.add(this)
126
124
  }
127
125
 
128
- get isTTY () {
126
+ get isTTY() {
129
127
  return true
130
128
  }
131
129
 
132
- get columns () {
130
+ get columns() {
133
131
  return this._size[0]
134
132
  }
135
133
 
136
- get rows () {
134
+ get rows() {
137
135
  return this._size[1]
138
136
  }
139
137
 
140
- getWindowSize () {
138
+ getWindowSize() {
141
139
  return binding.getWindowSize(this._handle)
142
140
  }
143
141
 
144
- _writev (batch, cb) {
142
+ _writev(batch, cb) {
145
143
  this._pendingWrite = [cb, batch]
146
- binding.writev(this._handle, batch.map(({ chunk }) => chunk))
144
+ binding.writev(
145
+ this._handle,
146
+ batch.map(({ chunk }) => chunk)
147
+ )
147
148
  }
148
149
 
149
- _predestroy () {
150
+ _predestroy() {
150
151
  if (this._state & constants.state.CLOSING) return
151
152
  this._state |= constants.state.CLOSING
152
153
  binding.close(this._handle)
@@ -154,7 +155,7 @@ exports.WriteStream = class TTYWriteStream extends Writable {
154
155
  if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.stop()
155
156
  }
156
157
 
157
- _destroy (err, cb) {
158
+ _destroy(err, cb) {
158
159
  if (this._state & constants.state.CLOSING) return cb(err)
159
160
  this._state |= constants.state.CLOSING
160
161
  this._pendingDestroy = cb
@@ -163,30 +164,30 @@ exports.WriteStream = class TTYWriteStream extends Writable {
163
164
  if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.stop()
164
165
  }
165
166
 
166
- _continueWrite (err) {
167
+ _continueWrite(err) {
167
168
  if (this._pendingWrite === null) return
168
169
  const cb = this._pendingWrite[0]
169
170
  this._pendingWrite = null
170
171
  cb(err)
171
172
  }
172
173
 
173
- _continueDestroy () {
174
+ _continueDestroy() {
174
175
  if (this._pendingDestroy === null) return
175
176
  const cb = this._pendingDestroy
176
177
  this._pendingDestroy = null
177
178
  cb(null)
178
179
  }
179
180
 
180
- _onwrite (err) {
181
+ _onwrite(err) {
181
182
  this._continueWrite(err)
182
183
  }
183
184
 
184
- _onclose () {
185
+ _onclose() {
185
186
  this._handle = null
186
187
  this._continueDestroy()
187
188
  }
188
189
 
189
- _onresize () {
190
+ _onresize() {
190
191
  this._size = this.getWindowSize()
191
192
  this.emit('resize')
192
193
  }
@@ -210,17 +211,6 @@ exports.WriteStream._resize
210
211
  })
211
212
  .unref()
212
213
 
213
- Bare
214
- .on('exit', () => {
215
- for (const stream of exports.ReadStream._streams) {
216
- stream.destroy()
217
- }
218
-
219
- for (const stream of exports.WriteStream._streams) {
220
- stream.destroy()
221
- }
222
- })
223
-
224
214
  const empty = Buffer.alloc(0)
225
215
 
226
- function noop () {}
216
+ function noop() {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-tty",
3
- "version": "4.0.1",
3
+ "version": "5.0.0",
4
4
  "description": "Native TTY streams for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -17,7 +17,7 @@
17
17
  ],
18
18
  "addon": true,
19
19
  "scripts": {
20
- "test": "standard && bare test.js"
20
+ "test": "prettier . --check && bare test.js"
21
21
  },
22
22
  "repository": {
23
23
  "type": "git",
@@ -29,13 +29,18 @@
29
29
  "url": "https://github.com/holepunchto/bare-tty/issues"
30
30
  },
31
31
  "homepage": "https://github.com/holepunchto/bare-tty#readme",
32
+ "engines": {
33
+ "bare": ">=1.7.0"
34
+ },
32
35
  "dependencies": {
33
36
  "bare-events": "^2.2.0",
34
- "bare-signals": "^3.0.0",
37
+ "bare-signals": "^4.0.0",
35
38
  "bare-stream": "^2.0.0"
36
39
  },
37
40
  "devDependencies": {
38
41
  "brittle": "^3.2.1",
39
- "standard": "^17.0.0"
42
+ "cmake-bare": "^1.1.6",
43
+ "prettier": "^3.4.1",
44
+ "prettier-config-standard": "^7.0.0"
40
45
  }
41
46
  }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file