brsh 3.8.0 → 3.8.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/lib/Command.js CHANGED
@@ -29,7 +29,7 @@ class Command extends EventEmitter {
29
29
  onWrite: (chunk) => this.emit('stderr', chunk)
30
30
  });
31
31
 
32
- this.stdinStream.on('data', (chunk) => {
32
+ this._onStdinStreamData = (chunk) => {
33
33
  if (!this.captureInput) return;
34
34
  const keys = parseVtKeys(chunk);
35
35
  for (const k of keys) {
@@ -40,7 +40,8 @@ class Command extends EventEmitter {
40
40
  }
41
41
  this.onInput(k);
42
42
  }
43
- });
43
+ };
44
+ this.stdinStream.on('data', this._onStdinStreamData);
44
45
  }
45
46
 
46
47
  get name() {
package/lib/TtyStream.js CHANGED
@@ -103,7 +103,12 @@ class TtyStream extends EventEmitter {
103
103
  if (typeof this._onWrite === 'function') {
104
104
  this._onWrite(chunk);
105
105
  }
106
- this.emit('data', this._formatChunk(chunk));
106
+ this._isPushing = true;
107
+ try {
108
+ this.emit('data', this._formatChunk(chunk));
109
+ } finally {
110
+ this._isPushing = false;
111
+ }
107
112
  if (typeof encoding === 'function') encoding();
108
113
  else if (typeof callback === 'function') callback();
109
114
  return true;
@@ -111,7 +116,12 @@ class TtyStream extends EventEmitter {
111
116
 
112
117
  pushData(chunk) {
113
118
  if (chunk == null) return;
114
- this.emit('data', this._formatChunk(chunk));
119
+ this._isPushing = true;
120
+ try {
121
+ this.emit('data', this._formatChunk(chunk));
122
+ } finally {
123
+ this._isPushing = false;
124
+ }
115
125
  }
116
126
 
117
127
  _formatChunk(chunk) {
@@ -110,6 +110,9 @@ class WasmCommand extends ScriptCommand {
110
110
 
111
111
  onInput(key) {
112
112
  if (this._handlingStreamData) return;
113
+ if (this.stdinStream && this.stdinStream._isPushing) {
114
+ return;
115
+ }
113
116
  if (typeof this._externalKeyHandler === 'function') {
114
117
  this._externalKeyHandler(key);
115
118
  return;
@@ -316,6 +319,9 @@ class WasmCommand extends ScriptCommand {
316
319
  if (b) enqueueBytes(b);
317
320
  };
318
321
 
322
+ if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.off === 'function') {
323
+ this.stdinStream.off('data', this._onStdinStreamData);
324
+ }
319
325
  if (isTtyEnvironment && this.stdinStream && typeof this.stdinStream.on === 'function') {
320
326
  this.stdinStream.on('data', onStreamData);
321
327
  }
@@ -1287,10 +1293,13 @@ class WasmCommand extends ScriptCommand {
1287
1293
  if (isInteractive) {
1288
1294
  this._exitAltScreen();
1289
1295
  }
1290
- if (isInteractive && onStreamData) {
1296
+ if (onStreamData) {
1291
1297
  if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('data', onStreamData);
1292
1298
  if (this.stdin && typeof this.stdin.off === 'function') this.stdin.off('data', onStreamData);
1293
1299
  }
1300
+ if (this._onStdinStreamData && this.stdinStream && typeof this.stdinStream.on === 'function') {
1301
+ this.stdinStream.on('data', this._onStdinStreamData);
1302
+ }
1294
1303
  if (onResize) {
1295
1304
  if (this.stdout && typeof this.stdout.off === 'function') this.stdout.off('resize', onResize);
1296
1305
  if (this.stdinStream && typeof this.stdinStream.off === 'function') this.stdinStream.off('resize', onResize);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "brsh",
3
- "version": "3.8.0",
3
+ "version": "3.8.1",
4
4
  "description": "A unix like shell written in javascript",
5
5
  "main": "./dist/shell.min.js",
6
6
  "module": "./dist/shell.es.js",
package/test/wbt.test.js CHANGED
@@ -1459,6 +1459,45 @@ describe('WBT GNU nano WASM package (nano / nano-wasm)', () => {
1459
1459
  expect(joinedOutput).toContain('\x1b[?1049h'); // entered alt screen
1460
1460
  expect(joinedOutput).toContain('\x1b[?1049l'); // exited alt screen
1461
1461
  });
1462
+
1463
+ it('runs nano interactively via stream write (as in Terminal emulator) and does not duplicate characters', async () => {
1464
+ if (!NANO_WASM_BYTES) return;
1465
+ await run(shell, 'wbt install nano');
1466
+
1467
+ const stdoutChunks = [];
1468
+ const onData = (chunk) => stdoutChunks.push(chunk);
1469
+ shell.stdout.on('data', onData);
1470
+
1471
+ const cmdPromise = shell.onCommand('nano /stream-test.txt');
1472
+
1473
+ // Allow nano to initialize, query terminal size, enter alt-screen, and render initial UI
1474
+ await new Promise(r => setTimeout(r, 60));
1475
+
1476
+ // Type text into nano via stream write (simulating terminal emulator input)
1477
+ for (const ch of 'Hello from stream!') {
1478
+ shell.write(ch);
1479
+ await new Promise(r => setTimeout(r, 5));
1480
+ }
1481
+ await new Promise(r => setTimeout(r, 30));
1482
+
1483
+ // Save file: Ctrl+O (\x0f), then Enter (\r) to confirm filename
1484
+ shell.write('\x0f');
1485
+ await new Promise(r => setTimeout(r, 30));
1486
+ shell.write('\r');
1487
+ await new Promise(r => setTimeout(r, 30));
1488
+
1489
+ // Exit nano: Ctrl+X (\x18)
1490
+ shell.write('\x18');
1491
+
1492
+ await cmdPromise;
1493
+ shell.stdout.off('data', onData);
1494
+
1495
+ const saved = shell.context.fs.getFileByPath('/stream-test.txt');
1496
+
1497
+ expect(shell.lastCode).toBe(0);
1498
+ expect(saved).toContain('Hello from stream!');
1499
+ expect(saved).not.toContain('HHeelllloo');
1500
+ });
1462
1501
  });
1463
1502
 
1464
1503