bare-tty 5.0.3 → 5.1.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/CMakeLists.txt CHANGED
@@ -1,4 +1,4 @@
1
- cmake_minimum_required(VERSION 3.25)
1
+ cmake_minimum_required(VERSION 4.0)
2
2
 
3
3
  find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
4
4
 
package/index.d.ts CHANGED
@@ -1,16 +1,11 @@
1
- import {
2
- Readable,
3
- ReadableOptions,
4
- Writable,
5
- WritableEvents,
6
- WritableOptions
7
- } from 'bare-stream'
1
+ import { Readable, ReadableOptions, Writable, WritableEvents, WritableOptions } from 'bare-stream'
2
+ import constants from './lib/constants'
8
3
 
9
4
  interface ReadStream extends Readable {
5
+ readonly fd: number
10
6
  readonly isTTY: true
11
7
 
12
8
  setMode(mode: number): this
13
-
14
9
  setRawMode(mode: boolean): this
15
10
  }
16
11
 
@@ -22,8 +17,8 @@ interface WriteStreamEvents extends WritableEvents {
22
17
  resize: []
23
18
  }
24
19
 
25
- interface WriteStream<M extends WriteStreamEvents = WriteStreamEvents>
26
- extends Writable<M> {
20
+ interface WriteStream<M extends WriteStreamEvents = WriteStreamEvents> extends Writable<M> {
21
+ readonly fd: number
27
22
  readonly isTTY: true
28
23
  readonly columns: number
29
24
  readonly rows: number
@@ -31,17 +26,10 @@ interface WriteStream<M extends WriteStreamEvents = WriteStreamEvents>
31
26
  getWindowSize(): [width: number, height: number]
32
27
  }
33
28
 
34
- declare class WriteStream<
35
- M extends WriteStreamEvents = WriteStreamEvents
36
- > extends Writable<M> {
29
+ declare class WriteStream<M extends WriteStreamEvents = WriteStreamEvents> extends Writable<M> {
37
30
  constructor(fd: number, opts?: WritableOptions)
38
31
  }
39
32
 
40
33
  declare function isTTY(fd: number): boolean
41
34
 
42
- declare const constants: {
43
- mode: { NORMAL: number; RAW: number; IO: number }
44
- state: { READING: number; CLOSING: number }
45
- }
46
-
47
35
  export { ReadStream, WriteStream, isTTY, isTTY as isatty, constants }
package/index.js CHANGED
@@ -1,34 +1,29 @@
1
- /* global Bare */
2
1
  const { Readable, Writable } = require('bare-stream')
3
2
  const Signal = require('bare-signals')
4
3
  const binding = require('./binding')
5
4
  const constants = require('./lib/constants')
6
5
 
7
6
  const defaultReadBufferSize = 65536
7
+ const empty = Buffer.alloc(0)
8
8
 
9
9
  exports.ReadStream = class TTYReadStream extends Readable {
10
10
  constructor(fd, opts = {}) {
11
11
  super()
12
12
 
13
- const { readBufferSize = defaultReadBufferSize, allowHalfOpen = true } =
14
- opts
13
+ const { readBufferSize = defaultReadBufferSize, allowHalfOpen = true } = opts
15
14
 
15
+ this._fd = fd
16
16
  this._state = 0
17
-
18
17
  this._allowHalfOpen = allowHalfOpen
18
+ this._buffer = Buffer.alloc(readBufferSize)
19
19
 
20
20
  this._pendingDestroy = null
21
21
 
22
- this._buffer = Buffer.alloc(readBufferSize)
22
+ this._handle = binding.init(fd, this._buffer, this, noop, this._onread, this._onclose)
23
+ }
23
24
 
24
- this._handle = binding.init(
25
- fd,
26
- this._buffer,
27
- this,
28
- noop,
29
- this._onread,
30
- this._onclose
31
- )
25
+ get fd() {
26
+ return this._fd
32
27
  }
33
28
 
34
29
  get isTTY() {
@@ -47,6 +42,7 @@ exports.ReadStream = class TTYReadStream extends Readable {
47
42
  _read() {
48
43
  if ((this._state & constants.state.READING) === 0) {
49
44
  this._state |= constants.state.READING
45
+
50
46
  binding.resume(this._handle)
51
47
  }
52
48
  }
@@ -54,13 +50,16 @@ exports.ReadStream = class TTYReadStream extends Readable {
54
50
  _predestroy() {
55
51
  if (this._state & constants.state.CLOSING) return
56
52
  this._state |= constants.state.CLOSING
53
+
57
54
  binding.close(this._handle)
58
55
  }
59
56
 
60
57
  _destroy(err, cb) {
61
58
  if (this._state & constants.state.CLOSING) return cb(err)
62
59
  this._state |= constants.state.CLOSING
60
+
63
61
  this._pendingDestroy = cb
62
+
64
63
  binding.close(this._handle)
65
64
  }
66
65
 
@@ -88,12 +87,14 @@ exports.ReadStream = class TTYReadStream extends Readable {
88
87
 
89
88
  if (this.push(copy) === false && this.destroying === false) {
90
89
  this._state &= ~constants.state.READING
90
+
91
91
  binding.pause(this._handle)
92
92
  }
93
93
  }
94
94
 
95
95
  _onclose() {
96
96
  this._handle = null
97
+
97
98
  this._continueDestroy()
98
99
  }
99
100
  }
@@ -102,27 +103,27 @@ exports.WriteStream = class TTYWriteStream extends Writable {
102
103
  constructor(fd, opts = {}) {
103
104
  super()
104
105
 
106
+ this._fd = fd
105
107
  this._state = 0
106
108
  this._size = null
107
109
 
108
110
  this._pendingWrite = null
111
+ this._pendingWriteBatch = null
109
112
  this._pendingDestroy = null
110
113
 
111
- this._handle = binding.init(
112
- fd,
113
- empty,
114
- this,
115
- this._onwrite,
116
- noop,
117
- this._onclose
118
- )
114
+ this._handle = binding.init(fd, empty, this, this._onwrite, noop, this._onclose)
119
115
 
120
116
  this._size = this.getWindowSize()
121
117
 
122
118
  if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.start()
119
+
123
120
  TTYWriteStream._streams.add(this)
124
121
  }
125
122
 
123
+ get fd() {
124
+ return this._fd
125
+ }
126
+
126
127
  get isTTY() {
127
128
  return true
128
129
  }
@@ -140,34 +141,48 @@ exports.WriteStream = class TTYWriteStream extends Writable {
140
141
  }
141
142
 
142
143
  _writev(batch, cb) {
143
- this._pendingWrite = [cb, batch]
144
- binding.writev(
145
- this._handle,
146
- batch.map(({ chunk }) => chunk)
147
- )
144
+ this._pendingWrite = cb
145
+ this._pendingWriteBatch = batch
146
+
147
+ try {
148
+ binding.writev(
149
+ this._handle,
150
+ batch.map(({ chunk }) => chunk)
151
+ )
152
+ } catch (err) {
153
+ this._continueWrite(err)
154
+ }
148
155
  }
149
156
 
150
157
  _predestroy() {
151
158
  if (this._state & constants.state.CLOSING) return
152
159
  this._state |= constants.state.CLOSING
160
+
153
161
  binding.close(this._handle)
162
+
154
163
  TTYWriteStream._streams.delete(this)
164
+
155
165
  if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.stop()
156
166
  }
157
167
 
158
168
  _destroy(err, cb) {
159
169
  if (this._state & constants.state.CLOSING) return cb(err)
160
170
  this._state |= constants.state.CLOSING
171
+
161
172
  this._pendingDestroy = cb
173
+
162
174
  binding.close(this._handle)
175
+
163
176
  TTYWriteStream._streams.delete(this)
177
+
164
178
  if (TTYWriteStream._streams.size === 0) TTYWriteStream._resize.stop()
165
179
  }
166
180
 
167
181
  _continueWrite(err) {
168
182
  if (this._pendingWrite === null) return
169
- const cb = this._pendingWrite[0]
183
+ const cb = this._pendingWrite
170
184
  this._pendingWrite = null
185
+ this._pendingWriteBatch = null
171
186
  cb(err)
172
187
  }
173
188
 
@@ -184,11 +199,13 @@ exports.WriteStream = class TTYWriteStream extends Writable {
184
199
 
185
200
  _onclose() {
186
201
  this._handle = null
202
+
187
203
  this._continueDestroy()
188
204
  }
189
205
 
190
206
  _onresize() {
191
207
  this._size = this.getWindowSize()
208
+
192
209
  this.emit('resize')
193
210
  }
194
211
 
@@ -211,6 +228,4 @@ exports.WriteStream._resize
211
228
  })
212
229
  .unref()
213
230
 
214
- const empty = Buffer.alloc(0)
215
-
216
231
  function noop() {}
@@ -0,0 +1,6 @@
1
+ declare const constants: {
2
+ mode: { NORMAL: number; RAW: number; IO: number }
3
+ state: { READING: number; CLOSING: number }
4
+ }
5
+
6
+ export = constants
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-tty",
3
- "version": "5.0.3",
3
+ "version": "5.1.1",
4
4
  "description": "Native TTY streams for JavaScript",
5
5
  "exports": {
6
6
  ".": {
@@ -21,7 +21,9 @@
21
21
  ],
22
22
  "addon": true,
23
23
  "scripts": {
24
- "test": "prettier . --check && bare test.js"
24
+ "format": "prettier --write . && lunte --fix",
25
+ "lint": "prettier --check . && lunte",
26
+ "test": "brittle-bare --coverage test.js"
25
27
  },
26
28
  "repository": {
27
29
  "type": "git",
@@ -44,7 +46,8 @@
44
46
  "devDependencies": {
45
47
  "brittle": "^3.2.1",
46
48
  "cmake-bare": "^1.1.6",
49
+ "lunte": "^1.8.0",
47
50
  "prettier": "^3.4.1",
48
- "prettier-config-standard": "^7.0.0"
51
+ "prettier-config-holepunch": "^2.0.0"
49
52
  }
50
53
  }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file