bare-tty 4.0.2 → 5.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.
- package/binding.c +49 -16
- package/index.js +38 -48
- package/package.json +8 -4
- package/prebuilds/android-arm/bare-tty.bare +0 -0
- package/prebuilds/android-arm64/bare-tty.bare +0 -0
- package/prebuilds/android-ia32/bare-tty.bare +0 -0
- package/prebuilds/android-x64/bare-tty.bare +0 -0
- package/prebuilds/darwin-arm64/bare-tty.bare +0 -0
- package/prebuilds/darwin-x64/bare-tty.bare +0 -0
- package/prebuilds/ios-arm64/bare-tty.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-tty.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-tty.bare +0 -0
- package/prebuilds/linux-arm64/bare-tty.bare +0 -0
- package/prebuilds/linux-x64/bare-tty.bare +0 -0
- package/prebuilds/win32-arm64/bare-tty.bare +0 -0
- package/prebuilds/win32-x64/bare-tty.bare +0 -0
package/binding.c
CHANGED
|
@@ -19,14 +19,21 @@ 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
|
+
bool closing;
|
|
24
|
+
bool exiting;
|
|
25
|
+
|
|
26
|
+
js_deferred_teardown_t *teardown;
|
|
22
27
|
} bare_tty_t;
|
|
23
28
|
|
|
24
29
|
static void
|
|
25
|
-
bare_tty__on_write
|
|
30
|
+
bare_tty__on_write(uv_write_t *req, int status) {
|
|
26
31
|
int err;
|
|
27
32
|
|
|
28
33
|
bare_tty_t *tty = (bare_tty_t *) req->data;
|
|
29
34
|
|
|
35
|
+
if (tty->exiting) return;
|
|
36
|
+
|
|
30
37
|
js_env_t *env = tty->env;
|
|
31
38
|
|
|
32
39
|
js_handle_scope_t *scope;
|
|
@@ -66,7 +73,7 @@ bare_tty__on_write (uv_write_t *req, int status) {
|
|
|
66
73
|
}
|
|
67
74
|
|
|
68
75
|
static void
|
|
69
|
-
bare_tty__on_read
|
|
76
|
+
bare_tty__on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
|
|
70
77
|
if (nread == UV_EOF) nread = 0;
|
|
71
78
|
else if (nread == 0) return;
|
|
72
79
|
|
|
@@ -74,6 +81,8 @@ bare_tty__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
|
|
|
74
81
|
|
|
75
82
|
bare_tty_t *tty = (bare_tty_t *) stream;
|
|
76
83
|
|
|
84
|
+
if (tty->exiting) return;
|
|
85
|
+
|
|
77
86
|
js_env_t *env = tty->env;
|
|
78
87
|
|
|
79
88
|
js_handle_scope_t *scope;
|
|
@@ -119,13 +128,15 @@ bare_tty__on_read (uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
|
|
|
119
128
|
}
|
|
120
129
|
|
|
121
130
|
static void
|
|
122
|
-
bare_tty__on_close
|
|
131
|
+
bare_tty__on_close(uv_handle_t *handle) {
|
|
123
132
|
int err;
|
|
124
133
|
|
|
125
134
|
bare_tty_t *tty = (bare_tty_t *) handle;
|
|
126
135
|
|
|
127
136
|
js_env_t *env = tty->env;
|
|
128
137
|
|
|
138
|
+
js_deferred_teardown_t *teardown = tty->teardown;
|
|
139
|
+
|
|
129
140
|
js_handle_scope_t *scope;
|
|
130
141
|
err = js_open_handle_scope(env, &scope);
|
|
131
142
|
assert(err == 0);
|
|
@@ -138,8 +149,6 @@ bare_tty__on_close (uv_handle_t *handle) {
|
|
|
138
149
|
err = js_get_reference_value(env, tty->on_close, &callback);
|
|
139
150
|
assert(err == 0);
|
|
140
151
|
|
|
141
|
-
js_call_function(env, ctx, callback, 0, NULL, NULL);
|
|
142
|
-
|
|
143
152
|
err = js_delete_reference(env, tty->on_write);
|
|
144
153
|
assert(err == 0);
|
|
145
154
|
|
|
@@ -152,19 +161,35 @@ bare_tty__on_close (uv_handle_t *handle) {
|
|
|
152
161
|
err = js_delete_reference(env, tty->ctx);
|
|
153
162
|
assert(err == 0);
|
|
154
163
|
|
|
164
|
+
if (!tty->exiting) js_call_function(env, ctx, callback, 0, NULL, NULL);
|
|
165
|
+
|
|
155
166
|
err = js_close_handle_scope(env, scope);
|
|
156
167
|
assert(err == 0);
|
|
168
|
+
|
|
169
|
+
err = js_finish_deferred_teardown_callback(teardown);
|
|
170
|
+
assert(err == 0);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static void
|
|
174
|
+
bare_tty__on_teardown(js_deferred_teardown_t *handle, void *data) {
|
|
175
|
+
bare_tty_t *tty = (bare_tty_t *) data;
|
|
176
|
+
|
|
177
|
+
tty->exiting = true;
|
|
178
|
+
|
|
179
|
+
if (tty->closing) return;
|
|
180
|
+
|
|
181
|
+
uv_close((uv_handle_t *) &tty->handle, bare_tty__on_close);
|
|
157
182
|
}
|
|
158
183
|
|
|
159
184
|
static void
|
|
160
|
-
bare_tty__on_alloc
|
|
185
|
+
bare_tty__on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
|
|
161
186
|
bare_tty_t *tty = (bare_tty_t *) handle;
|
|
162
187
|
|
|
163
188
|
*buf = tty->read;
|
|
164
189
|
}
|
|
165
190
|
|
|
166
191
|
static js_value_t *
|
|
167
|
-
bare_tty_init
|
|
192
|
+
bare_tty_init(js_env_t *env, js_callback_info_t *info) {
|
|
168
193
|
int err;
|
|
169
194
|
|
|
170
195
|
size_t argc = 6;
|
|
@@ -176,7 +201,8 @@ bare_tty_init (js_env_t *env, js_callback_info_t *info) {
|
|
|
176
201
|
assert(argc == 6);
|
|
177
202
|
|
|
178
203
|
uv_loop_t *loop;
|
|
179
|
-
js_get_env_loop(env, &loop);
|
|
204
|
+
err = js_get_env_loop(env, &loop);
|
|
205
|
+
assert(err == 0);
|
|
180
206
|
|
|
181
207
|
js_value_t *handle;
|
|
182
208
|
|
|
@@ -201,6 +227,8 @@ bare_tty_init (js_env_t *env, js_callback_info_t *info) {
|
|
|
201
227
|
(void) err;
|
|
202
228
|
|
|
203
229
|
tty->env = env;
|
|
230
|
+
tty->closing = false;
|
|
231
|
+
tty->exiting = false;
|
|
204
232
|
|
|
205
233
|
err = js_get_typedarray_info(env, argv[1], NULL, (void **) &tty->read.base, (size_t *) &tty->read.len, NULL, NULL);
|
|
206
234
|
assert(err == 0);
|
|
@@ -217,11 +245,14 @@ bare_tty_init (js_env_t *env, js_callback_info_t *info) {
|
|
|
217
245
|
err = js_create_reference(env, argv[5], 1, &tty->on_close);
|
|
218
246
|
assert(err == 0);
|
|
219
247
|
|
|
248
|
+
err = js_add_deferred_teardown_callback(env, bare_tty__on_teardown, (void *) tty, &tty->teardown);
|
|
249
|
+
assert(err == 0);
|
|
250
|
+
|
|
220
251
|
return handle;
|
|
221
252
|
}
|
|
222
253
|
|
|
223
254
|
static js_value_t *
|
|
224
|
-
bare_tty_writev
|
|
255
|
+
bare_tty_writev(js_env_t *env, js_callback_info_t *info) {
|
|
225
256
|
int err;
|
|
226
257
|
|
|
227
258
|
size_t argc = 2;
|
|
@@ -271,7 +302,7 @@ bare_tty_writev (js_env_t *env, js_callback_info_t *info) {
|
|
|
271
302
|
}
|
|
272
303
|
|
|
273
304
|
static js_value_t *
|
|
274
|
-
bare_tty_resume
|
|
305
|
+
bare_tty_resume(js_env_t *env, js_callback_info_t *info) {
|
|
275
306
|
int err;
|
|
276
307
|
|
|
277
308
|
size_t argc = 1;
|
|
@@ -297,7 +328,7 @@ bare_tty_resume (js_env_t *env, js_callback_info_t *info) {
|
|
|
297
328
|
}
|
|
298
329
|
|
|
299
330
|
static js_value_t *
|
|
300
|
-
bare_tty_pause
|
|
331
|
+
bare_tty_pause(js_env_t *env, js_callback_info_t *info) {
|
|
301
332
|
int err;
|
|
302
333
|
|
|
303
334
|
size_t argc = 1;
|
|
@@ -323,7 +354,7 @@ bare_tty_pause (js_env_t *env, js_callback_info_t *info) {
|
|
|
323
354
|
}
|
|
324
355
|
|
|
325
356
|
static js_value_t *
|
|
326
|
-
bare_tty_close
|
|
357
|
+
bare_tty_close(js_env_t *env, js_callback_info_t *info) {
|
|
327
358
|
int err;
|
|
328
359
|
|
|
329
360
|
size_t argc = 1;
|
|
@@ -344,13 +375,15 @@ bare_tty_close (js_env_t *env, js_callback_info_t *info) {
|
|
|
344
375
|
// close an output stream.
|
|
345
376
|
(void) err;
|
|
346
377
|
|
|
378
|
+
tty->closing = true;
|
|
379
|
+
|
|
347
380
|
uv_close((uv_handle_t *) &tty->handle, bare_tty__on_close);
|
|
348
381
|
|
|
349
382
|
return NULL;
|
|
350
383
|
}
|
|
351
384
|
|
|
352
385
|
static js_value_t *
|
|
353
|
-
bare_tty_set_mode
|
|
386
|
+
bare_tty_set_mode(js_env_t *env, js_callback_info_t *info) {
|
|
354
387
|
int err;
|
|
355
388
|
|
|
356
389
|
size_t argc = 2;
|
|
@@ -380,7 +413,7 @@ bare_tty_set_mode (js_env_t *env, js_callback_info_t *info) {
|
|
|
380
413
|
}
|
|
381
414
|
|
|
382
415
|
static js_value_t *
|
|
383
|
-
bare_tty_get_window_size
|
|
416
|
+
bare_tty_get_window_size(js_env_t *env, js_callback_info_t *info) {
|
|
384
417
|
int err;
|
|
385
418
|
|
|
386
419
|
size_t argc = 1;
|
|
@@ -425,7 +458,7 @@ bare_tty_get_window_size (js_env_t *env, js_callback_info_t *info) {
|
|
|
425
458
|
}
|
|
426
459
|
|
|
427
460
|
static js_value_t *
|
|
428
|
-
bare_tty_is_tty
|
|
461
|
+
bare_tty_is_tty(js_env_t *env, js_callback_info_t *info) {
|
|
429
462
|
int err;
|
|
430
463
|
|
|
431
464
|
size_t argc = 1;
|
|
@@ -448,7 +481,7 @@ bare_tty_is_tty (js_env_t *env, js_callback_info_t *info) {
|
|
|
448
481
|
}
|
|
449
482
|
|
|
450
483
|
static js_value_t *
|
|
451
|
-
bare_tty_exports
|
|
484
|
+
bare_tty_exports(js_env_t *env, js_value_t *exports) {
|
|
452
485
|
int err;
|
|
453
486
|
|
|
454
487
|
#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
|
|
10
|
+
constructor(fd, opts = {}) {
|
|
11
11
|
super()
|
|
12
12
|
|
|
13
|
-
const {
|
|
14
|
-
|
|
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(
|
|
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
|
|
38
|
+
setMode(mode) {
|
|
40
39
|
binding.setMode(this._handle, mode)
|
|
41
40
|
return this
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
setRawMode
|
|
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
|
|
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
|
|
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
|
|
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(
|
|
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
|
|
142
|
+
_writev(batch, cb) {
|
|
145
143
|
this._pendingWrite = [cb, batch]
|
|
146
|
-
binding.writev(
|
|
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
|
|
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
|
|
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
|
|
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": "
|
|
3
|
+
"version": "5.0.1",
|
|
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": "
|
|
20
|
+
"test": "prettier . --check && bare test.js"
|
|
21
21
|
},
|
|
22
22
|
"repository": {
|
|
23
23
|
"type": "git",
|
|
@@ -29,14 +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": "^
|
|
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
42
|
"cmake-bare": "^1.1.6",
|
|
40
|
-
"
|
|
43
|
+
"prettier": "^3.4.1",
|
|
44
|
+
"prettier-config-standard": "^7.0.0"
|
|
41
45
|
}
|
|
42
46
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|