brsh 2.8.0 → 3.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/.github/workflows/release.yml +3 -1
- package/CHANGELOG.md +32 -0
- package/bin/brsh-shell.js +16 -131
- package/bin/exportFileSystem.js +4 -0
- package/bin/zsh-server.js +261 -0
- package/dist/brsh.css +1 -1
- package/dist/shell.es.js +943 -499
- package/dist/shell.es.js.map +1 -1
- package/dist/shell.min.js +31 -16
- package/dist/shell.min.js.map +1 -1
- package/dist/terminal.cjs.js +77 -100
- package/dist/terminal.cjs.js.map +1 -1
- package/dist/terminal.es.js +1382 -603
- package/dist/terminal.es.js.map +1 -1
- package/dist/terminal.min.js +77 -100
- package/dist/terminal.min.js.map +1 -1
- package/dist/wbt.es.js +1863 -752
- package/dist/wbt.es.js.map +1 -1
- package/dist/wbt.min.js +8 -6
- package/dist/wbt.min.js.map +1 -1
- package/index.js +472 -0
- package/lib/BaseFileSystem.js +40 -0
- package/lib/BinaryContainer.js +1 -1
- package/lib/Command.js +30 -0
- package/lib/FileSystem.js +1 -38
- package/lib/InteractiveCommand.js +28 -0
- package/lib/NodeFileSystem.js +4 -2
- package/lib/ReplCommand.js +15 -0
- package/lib/Terminal.js +769 -510
- package/lib/TtyStream.js +181 -0
- package/lib/WasmCommand.js +168 -18
- package/lib/builtins/cat.js +2 -2
- package/lib/builtins/grep.js +2 -2
- package/lib/local/clear.js +3 -0
- package/lib/terminal/DomRenderer.js +325 -0
- package/lib/terminal/PtyBridge.js +169 -0
- package/lib/terminal/ScreenBuffer.js +594 -0
- package/lib/terminal/VtParser.js +728 -0
- package/lib/wbt/index.js +270 -14
- package/lib/wbt/packages/wllama.js +12 -2
- package/lib/wbt/registry.js +11 -2
- package/package.json +4 -2
- package/styles/terminal.css +33 -4
- package/test/terminal.test.js +762 -40
- package/test/wbt.test.js +24 -5
package/index.js
CHANGED
|
@@ -6,6 +6,9 @@ const ScriptCommand = require('./lib/ScriptCommand');
|
|
|
6
6
|
const WasmCommand = require('./lib/WasmCommand');
|
|
7
7
|
const Command = require('./lib/Command');
|
|
8
8
|
const InteractiveCommand = require('./lib/InteractiveCommand');
|
|
9
|
+
const { TtyStream } = require('./lib/TtyStream');
|
|
10
|
+
|
|
11
|
+
const HISTORY_FILE = '/.brsh_history';
|
|
9
12
|
|
|
10
13
|
const builtins = require('./lib/builtins');
|
|
11
14
|
const localCommands = require('./lib/local');
|
|
@@ -74,6 +77,35 @@ class Shell extends EventEmitter {
|
|
|
74
77
|
this._loadingFs = null;
|
|
75
78
|
this._queuedWhileLoading = false;
|
|
76
79
|
|
|
80
|
+
this.stdinStream = new TtyStream({ columns: 80, rows: 24 });
|
|
81
|
+
this.stdout = new TtyStream({
|
|
82
|
+
columns: 80,
|
|
83
|
+
rows: 24,
|
|
84
|
+
onWrite: (chunk) => this.emit('data', chunk)
|
|
85
|
+
});
|
|
86
|
+
this.stderr = new TtyStream({
|
|
87
|
+
columns: 80,
|
|
88
|
+
rows: 24,
|
|
89
|
+
onWrite: (chunk) => this.emit('data', chunk)
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
this._lineBuffer = [];
|
|
93
|
+
this._cursorPos = 0;
|
|
94
|
+
this._history = [];
|
|
95
|
+
this._historyPos = null;
|
|
96
|
+
this._savedInput = null;
|
|
97
|
+
this._historyLoaded = false;
|
|
98
|
+
this._working = true;
|
|
99
|
+
this._promptReady = false;
|
|
100
|
+
this._inProgressLine = false;
|
|
101
|
+
this._fromLineEditor = false;
|
|
102
|
+
this._lastPromptRowOffset = 0;
|
|
103
|
+
this._pendingCommands = [];
|
|
104
|
+
|
|
105
|
+
if (!subshell) {
|
|
106
|
+
this._setupTtyStreams();
|
|
107
|
+
}
|
|
108
|
+
|
|
77
109
|
if (useRealFilesystem) {
|
|
78
110
|
this.context.setNodeFilesystem(cwd);
|
|
79
111
|
this.context.setCwd(cwd);
|
|
@@ -186,10 +218,23 @@ class Shell extends EventEmitter {
|
|
|
186
218
|
return this._lastCode;
|
|
187
219
|
}
|
|
188
220
|
|
|
221
|
+
get stdin() {
|
|
222
|
+
return this._stdin !== undefined ? this._stdin : this.stdinStream;
|
|
223
|
+
}
|
|
224
|
+
|
|
189
225
|
set stdin(data) {
|
|
190
226
|
this._stdin = data;
|
|
191
227
|
}
|
|
192
228
|
|
|
229
|
+
write(data) {
|
|
230
|
+
this.stdinStream.pushData(data);
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
resize(cols, rows) {
|
|
235
|
+
this.setTerminalSize(cols, rows);
|
|
236
|
+
}
|
|
237
|
+
|
|
193
238
|
onInput(char) {
|
|
194
239
|
if(this.runningCommand && this.runningCommand.onInput && this.runningCommand.captureInput) {
|
|
195
240
|
this.runningCommand.onInput(char);
|
|
@@ -201,6 +246,14 @@ class Shell extends EventEmitter {
|
|
|
201
246
|
this._terminalSize = { cols, rows };
|
|
202
247
|
this.context.setVar('COLUMNS', String(cols));
|
|
203
248
|
this.context.setVar('LINES', String(rows));
|
|
249
|
+
this.stdinStream.resize(cols, rows);
|
|
250
|
+
this.stdout.resize(cols, rows);
|
|
251
|
+
this.stderr.resize(cols, rows);
|
|
252
|
+
if (this.runningCommand) {
|
|
253
|
+
if (this.runningCommand.stdinStream) this.runningCommand.stdinStream.resize(cols, rows);
|
|
254
|
+
if (this.runningCommand.stdout) this.runningCommand.stdout.resize(cols, rows);
|
|
255
|
+
if (this.runningCommand.stderr) this.runningCommand.stderr.resize(cols, rows);
|
|
256
|
+
}
|
|
204
257
|
this.emit('resize', { cols, rows });
|
|
205
258
|
}
|
|
206
259
|
|
|
@@ -339,6 +392,13 @@ class Shell extends EventEmitter {
|
|
|
339
392
|
}
|
|
340
393
|
|
|
341
394
|
onCommand(raw, events = true) {
|
|
395
|
+
if (events && !this._subshell && this._promptReady && !this._fromLineEditor) {
|
|
396
|
+
this.stdout.write(`\r${this.getPrompt()} ${raw}\x1b[K\r\n`);
|
|
397
|
+
this._lineBuffer = [];
|
|
398
|
+
this._cursorPos = 0;
|
|
399
|
+
}
|
|
400
|
+
this._fromLineEditor = false;
|
|
401
|
+
|
|
342
402
|
const segments = this._splitInput(raw);
|
|
343
403
|
const groups = this._groupPipes(segments);
|
|
344
404
|
let shouldRun = true;
|
|
@@ -477,6 +537,17 @@ class Shell extends EventEmitter {
|
|
|
477
537
|
this.runningCommand.on('flush', lines => {
|
|
478
538
|
this._emitOutput(lines);
|
|
479
539
|
});
|
|
540
|
+
this.runningCommand.on('stdout', chunk => {
|
|
541
|
+
if (redirectPath) {
|
|
542
|
+
instance.stdOut = String(chunk ?? '');
|
|
543
|
+
} else {
|
|
544
|
+
this.stdout.write(chunk);
|
|
545
|
+
this.emit('stdOutRaw', chunk);
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
this.runningCommand.on('stderr', chunk => {
|
|
549
|
+
this.stderr.write(chunk);
|
|
550
|
+
});
|
|
480
551
|
}
|
|
481
552
|
if (instance.interactive) {
|
|
482
553
|
this.emit('interactive');
|
|
@@ -540,9 +611,410 @@ class Shell extends EventEmitter {
|
|
|
540
611
|
}
|
|
541
612
|
|
|
542
613
|
clear() {
|
|
614
|
+
this.stdout.write('\x1b[2J\x1b[3J\x1b[H');
|
|
543
615
|
this.emit('clear');
|
|
544
616
|
}
|
|
545
617
|
|
|
618
|
+
_setupTtyStreams() {
|
|
619
|
+
this.on('stdOut', line => {
|
|
620
|
+
if (this.runningCommand && (this.runningCommand.interactive || this.runningCommand._inAltScreen)) {
|
|
621
|
+
this.stdout.write(String(line ?? ''));
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
if (typeof line === 'string' && line.startsWith('\r')) {
|
|
625
|
+
this._inProgressLine = true;
|
|
626
|
+
this.stdout.write('\r\x1b[K' + line.slice(1));
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
if (this._inProgressLine) {
|
|
630
|
+
this.stdout.write('\r\n');
|
|
631
|
+
this._inProgressLine = false;
|
|
632
|
+
}
|
|
633
|
+
this.stdout.write(String(line ?? '') + '\r\n');
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
this.on('stdErr', line => {
|
|
637
|
+
if (this.runningCommand && (this.runningCommand.interactive || this.runningCommand._inAltScreen)) {
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
if (this._inProgressLine) {
|
|
641
|
+
this.stdout.write('\r\n');
|
|
642
|
+
this._inProgressLine = false;
|
|
643
|
+
}
|
|
644
|
+
this.stderr.write(String(line ?? '') + '\r\n');
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
this.on('status', status => {
|
|
648
|
+
if (status === Shell.STATUS_READY) {
|
|
649
|
+
if (this._inProgressLine) {
|
|
650
|
+
this.stdout.write('\r\n');
|
|
651
|
+
this._inProgressLine = false;
|
|
652
|
+
}
|
|
653
|
+
this._working = false;
|
|
654
|
+
this._promptReady = true;
|
|
655
|
+
if (!this._historyLoaded) {
|
|
656
|
+
this._loadHistory();
|
|
657
|
+
this._historyLoaded = true;
|
|
658
|
+
}
|
|
659
|
+
this._redrawPromptLine();
|
|
660
|
+
if (this._pendingCommands.length > 0) {
|
|
661
|
+
const nextCmd = this._pendingCommands.shift();
|
|
662
|
+
if (this._pendingCommands.length === 0) {
|
|
663
|
+
const flags = this.context.getVar('-');
|
|
664
|
+
if (flags && flags.includes('c')) {
|
|
665
|
+
this.context.setVar('-', flags.replace(/c/g, ''));
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
this.sendCommand(nextCmd);
|
|
669
|
+
}
|
|
670
|
+
} else {
|
|
671
|
+
this._working = true;
|
|
672
|
+
}
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
this.stdinStream.on('data', data => this._handleStdinData(data));
|
|
676
|
+
this.stdinStream.on('end', () => {
|
|
677
|
+
if (!this.destroyed) this.destroy(0);
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
sendCommand(command) {
|
|
682
|
+
if (!this._promptReady) {
|
|
683
|
+
this.suppressMotd();
|
|
684
|
+
this._pendingCommands.push(command);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
this.onCommand(command);
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
_redrawPromptLine() {
|
|
691
|
+
if (this._working || (this.runningCommand && (this.runningCommand.interactive || this.runningCommand._inAltScreen))) {
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const prompt = `${this.getPrompt()} `;
|
|
695
|
+
const text = this._lineBuffer.join('');
|
|
696
|
+
const cols = (this._terminalSize && this._terminalSize.cols) || 80;
|
|
697
|
+
const prevRowOffset = this._lastPromptRowOffset || 0;
|
|
698
|
+
|
|
699
|
+
let seq = '';
|
|
700
|
+
if (prevRowOffset > 0) {
|
|
701
|
+
seq += `\x1b[${prevRowOffset}A`;
|
|
702
|
+
}
|
|
703
|
+
seq += `\r\x1b[J${prompt}${text}`;
|
|
704
|
+
|
|
705
|
+
const pos = Math.min(this._cursorPos, this._lineBuffer.length);
|
|
706
|
+
const endPos = prompt.length + this._lineBuffer.length;
|
|
707
|
+
const targetPos = prompt.length + pos;
|
|
708
|
+
|
|
709
|
+
const endRowOffset = Math.floor(endPos / cols);
|
|
710
|
+
const targetRowOffset = Math.floor(targetPos / cols);
|
|
711
|
+
const targetCol = targetPos % cols;
|
|
712
|
+
|
|
713
|
+
if (endRowOffset > targetRowOffset) {
|
|
714
|
+
seq += `\x1b[${endRowOffset - targetRowOffset}A\r`;
|
|
715
|
+
if (targetCol > 0) {
|
|
716
|
+
seq += `\x1b[${targetCol}C`;
|
|
717
|
+
}
|
|
718
|
+
} else if (endPos > targetPos) {
|
|
719
|
+
const diff = endPos - targetPos;
|
|
720
|
+
seq += `\x1b[${diff}D`;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
this._lastPromptRowOffset = targetRowOffset;
|
|
724
|
+
this.stdout.write(seq);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
_loadHistory() {
|
|
728
|
+
try {
|
|
729
|
+
const file = this.context.fs._getFileRaw(HISTORY_FILE);
|
|
730
|
+
if (file && typeof file === 'string' && file.length > 0) {
|
|
731
|
+
this._history = file.split('\n').filter(l => l.length > 0);
|
|
732
|
+
}
|
|
733
|
+
} catch (e) {
|
|
734
|
+
// No history file yet
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
_saveHistory() {
|
|
739
|
+
try {
|
|
740
|
+
this.context.fs._setFileRaw(HISTORY_FILE, this._history.join('\n'), true);
|
|
741
|
+
} catch (e) {
|
|
742
|
+
// Ignore persistence errors
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
_navigateHistory(direction) {
|
|
747
|
+
if (this._history.length === 0) return;
|
|
748
|
+
if (direction === -1) {
|
|
749
|
+
if (this._historyPos === null) {
|
|
750
|
+
this._savedInput = [...this._lineBuffer];
|
|
751
|
+
this._historyPos = this._history.length - 1;
|
|
752
|
+
} else if (this._historyPos > 0) {
|
|
753
|
+
this._historyPos--;
|
|
754
|
+
} else {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
this._lineBuffer = Array.from(this._history[this._historyPos]);
|
|
758
|
+
} else {
|
|
759
|
+
if (this._historyPos === null) return;
|
|
760
|
+
if (this._historyPos < this._history.length - 1) {
|
|
761
|
+
this._historyPos++;
|
|
762
|
+
this._lineBuffer = Array.from(this._history[this._historyPos]);
|
|
763
|
+
} else {
|
|
764
|
+
this._historyPos = null;
|
|
765
|
+
this._lineBuffer = this._savedInput ? [...this._savedInput] : [];
|
|
766
|
+
this._savedInput = null;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
this._cursorPos = this._lineBuffer.length;
|
|
770
|
+
this._redrawPromptLine();
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
_wordLeft() {
|
|
774
|
+
let pos = this._cursorPos;
|
|
775
|
+
while (pos > 0 && this._lineBuffer[pos - 1] === ' ') pos--;
|
|
776
|
+
while (pos > 0 && this._lineBuffer[pos - 1] !== ' ') pos--;
|
|
777
|
+
return pos;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
_wordRight() {
|
|
781
|
+
let pos = this._cursorPos;
|
|
782
|
+
const len = this._lineBuffer.length;
|
|
783
|
+
while (pos < len && this._lineBuffer[pos] === ' ') pos++;
|
|
784
|
+
while (pos < len && this._lineBuffer[pos] !== ' ') pos++;
|
|
785
|
+
return pos;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
_handleStdinData(raw) {
|
|
789
|
+
const data = typeof raw === 'string'
|
|
790
|
+
? raw
|
|
791
|
+
: (raw instanceof Uint8Array || (typeof Buffer !== 'undefined' && Buffer.isBuffer(raw)))
|
|
792
|
+
? new TextDecoder().decode(raw)
|
|
793
|
+
: String(raw ?? '');
|
|
794
|
+
|
|
795
|
+
if (this.runningCommand) {
|
|
796
|
+
const cmd = this.runningCommand;
|
|
797
|
+
const hasStreamListeners = cmd.stdinStream && cmd.stdinStream.listenerCount('data') > 1;
|
|
798
|
+
if (cmd.captureInput || cmd.interactive || cmd._inAltScreen || hasStreamListeners) {
|
|
799
|
+
cmd.stdinStream.pushData(raw);
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
if (this._working) return;
|
|
805
|
+
|
|
806
|
+
let i = 0;
|
|
807
|
+
while (i < data.length) {
|
|
808
|
+
const ch = data[i];
|
|
809
|
+
const code = data.charCodeAt(i);
|
|
810
|
+
|
|
811
|
+
if (code === 0x1b) {
|
|
812
|
+
if (data[i + 1] === 'b') {
|
|
813
|
+
this._cursorPos = this._wordLeft();
|
|
814
|
+
this._redrawPromptLine();
|
|
815
|
+
i += 2;
|
|
816
|
+
continue;
|
|
817
|
+
}
|
|
818
|
+
if (data[i + 1] === 'f') {
|
|
819
|
+
this._cursorPos = this._wordRight();
|
|
820
|
+
this._redrawPromptLine();
|
|
821
|
+
i += 2;
|
|
822
|
+
continue;
|
|
823
|
+
}
|
|
824
|
+
if (data.charCodeAt(i + 1) === 0x7f || data.charCodeAt(i + 1) === 0x08) {
|
|
825
|
+
const target = this._wordLeft();
|
|
826
|
+
this._lineBuffer.splice(target, this._cursorPos - target);
|
|
827
|
+
this._cursorPos = target;
|
|
828
|
+
this._redrawPromptLine();
|
|
829
|
+
i += 2;
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
if (data[i + 1] === 'd') {
|
|
833
|
+
const target = this._wordRight();
|
|
834
|
+
this._lineBuffer.splice(this._cursorPos, target - this._cursorPos);
|
|
835
|
+
this._redrawPromptLine();
|
|
836
|
+
i += 2;
|
|
837
|
+
continue;
|
|
838
|
+
}
|
|
839
|
+
if (data[i + 1] === '[' || data[i + 1] === 'O') {
|
|
840
|
+
let j = i + 2;
|
|
841
|
+
while (j < data.length && data.charCodeAt(j) >= 0x20 && data.charCodeAt(j) <= 0x3f) {
|
|
842
|
+
j++;
|
|
843
|
+
}
|
|
844
|
+
if (j < data.length) {
|
|
845
|
+
this._handleEscapeSeq(data.slice(i + 2, j + 1));
|
|
846
|
+
i = j + 1;
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
i++;
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
if (code === 0x04) {
|
|
855
|
+
if (this._lineBuffer.length === 0) {
|
|
856
|
+
this.stdout.write('exit\r\n');
|
|
857
|
+
this.destroy(0);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
if (this._cursorPos < this._lineBuffer.length) {
|
|
861
|
+
this._lineBuffer.splice(this._cursorPos, 1);
|
|
862
|
+
this._redrawPromptLine();
|
|
863
|
+
}
|
|
864
|
+
i++;
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
if (code === 0x0d || code === 0x0a) {
|
|
869
|
+
const command = this._lineBuffer.join('');
|
|
870
|
+
this._lineBuffer = [];
|
|
871
|
+
this._cursorPos = 0;
|
|
872
|
+
this._historyPos = null;
|
|
873
|
+
this._savedInput = null;
|
|
874
|
+
this.stdout.write('\r\n');
|
|
875
|
+
|
|
876
|
+
if (command.trim()) {
|
|
877
|
+
if (this._history[this._history.length - 1] !== command) {
|
|
878
|
+
this._history.push(command);
|
|
879
|
+
}
|
|
880
|
+
this._saveHistory();
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
this._fromLineEditor = true;
|
|
884
|
+
this.onCommand(command);
|
|
885
|
+
if (code === 0x0d && data[i + 1] === '\n') i++;
|
|
886
|
+
i++;
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
if (code === 0x7f || code === 0x08) {
|
|
891
|
+
if (this._cursorPos > 0) {
|
|
892
|
+
this._lineBuffer.splice(this._cursorPos - 1, 1);
|
|
893
|
+
this._cursorPos--;
|
|
894
|
+
this._redrawPromptLine();
|
|
895
|
+
}
|
|
896
|
+
i++;
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
if (code === 0x09) {
|
|
901
|
+
this._handleTab();
|
|
902
|
+
i++;
|
|
903
|
+
continue;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
if (code === 0x01) {
|
|
907
|
+
this._cursorPos = 0;
|
|
908
|
+
this._redrawPromptLine();
|
|
909
|
+
i++;
|
|
910
|
+
continue;
|
|
911
|
+
}
|
|
912
|
+
if (code === 0x05) {
|
|
913
|
+
this._cursorPos = this._lineBuffer.length;
|
|
914
|
+
this._redrawPromptLine();
|
|
915
|
+
i++;
|
|
916
|
+
continue;
|
|
917
|
+
}
|
|
918
|
+
if (code === 0x15) {
|
|
919
|
+
this._lineBuffer.splice(0, this._cursorPos);
|
|
920
|
+
this._cursorPos = 0;
|
|
921
|
+
this._redrawPromptLine();
|
|
922
|
+
i++;
|
|
923
|
+
continue;
|
|
924
|
+
}
|
|
925
|
+
if (code === 0x0b) {
|
|
926
|
+
this._lineBuffer.splice(this._cursorPos);
|
|
927
|
+
this._redrawPromptLine();
|
|
928
|
+
i++;
|
|
929
|
+
continue;
|
|
930
|
+
}
|
|
931
|
+
if (code === 0x17) {
|
|
932
|
+
const target = this._wordLeft();
|
|
933
|
+
this._lineBuffer.splice(target, this._cursorPos - target);
|
|
934
|
+
this._cursorPos = target;
|
|
935
|
+
this._redrawPromptLine();
|
|
936
|
+
i++;
|
|
937
|
+
continue;
|
|
938
|
+
}
|
|
939
|
+
if (code === 0x0c) {
|
|
940
|
+
this.stdout.write('\x1b[2J\x1b[H');
|
|
941
|
+
this._redrawPromptLine();
|
|
942
|
+
i++;
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
if (code === 0x03) {
|
|
946
|
+
this._lineBuffer = [];
|
|
947
|
+
this._cursorPos = 0;
|
|
948
|
+
this._historyPos = null;
|
|
949
|
+
this._savedInput = null;
|
|
950
|
+
this.stdout.write('^C\r\n');
|
|
951
|
+
this._redrawPromptLine();
|
|
952
|
+
i++;
|
|
953
|
+
continue;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
if (code >= 0x20) {
|
|
957
|
+
this._lineBuffer.splice(this._cursorPos, 0, ch);
|
|
958
|
+
this._cursorPos++;
|
|
959
|
+
this._redrawPromptLine();
|
|
960
|
+
}
|
|
961
|
+
i++;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
_handleEscapeSeq(seq) {
|
|
966
|
+
if (seq === 'A') {
|
|
967
|
+
this._navigateHistory(-1);
|
|
968
|
+
} else if (seq === 'B') {
|
|
969
|
+
this._navigateHistory(1);
|
|
970
|
+
} else if (seq === 'C') {
|
|
971
|
+
if (this._cursorPos < this._lineBuffer.length) {
|
|
972
|
+
this._cursorPos++;
|
|
973
|
+
this._redrawPromptLine();
|
|
974
|
+
}
|
|
975
|
+
} else if (seq === 'D') {
|
|
976
|
+
if (this._cursorPos > 0) {
|
|
977
|
+
this._cursorPos--;
|
|
978
|
+
this._redrawPromptLine();
|
|
979
|
+
}
|
|
980
|
+
} else if (seq === '1;3D' || seq === '1;5D') {
|
|
981
|
+
this._cursorPos = this._wordLeft();
|
|
982
|
+
this._redrawPromptLine();
|
|
983
|
+
} else if (seq === '1;3C' || seq === '1;5C') {
|
|
984
|
+
this._cursorPos = this._wordRight();
|
|
985
|
+
this._redrawPromptLine();
|
|
986
|
+
} else if (seq === 'H' || seq === '1~' || seq === '7~') {
|
|
987
|
+
this._cursorPos = 0;
|
|
988
|
+
this._redrawPromptLine();
|
|
989
|
+
} else if (seq === 'F' || seq === '4~' || seq === '8~') {
|
|
990
|
+
this._cursorPos = this._lineBuffer.length;
|
|
991
|
+
this._redrawPromptLine();
|
|
992
|
+
} else if (seq === '3~') {
|
|
993
|
+
if (this._cursorPos < this._lineBuffer.length) {
|
|
994
|
+
this._lineBuffer.splice(this._cursorPos, 1);
|
|
995
|
+
this._redrawPromptLine();
|
|
996
|
+
}
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
_handleTab() {
|
|
1001
|
+
const commandParts = this._lineBuffer.join('').split(' ');
|
|
1002
|
+
const lastPath = commandParts.pop();
|
|
1003
|
+
const autoComplete = this.tabCompletion(lastPath);
|
|
1004
|
+
if (!autoComplete) return;
|
|
1005
|
+
|
|
1006
|
+
const { options, path } = autoComplete;
|
|
1007
|
+
if (options.length === 1) {
|
|
1008
|
+
commandParts.push(`${path != null ? path.concat('/') : ''}${options[0]}`);
|
|
1009
|
+
this._lineBuffer = commandParts.join(' ').split('');
|
|
1010
|
+
this._cursorPos = this._lineBuffer.length;
|
|
1011
|
+
this._redrawPromptLine();
|
|
1012
|
+
} else if (options.length > 1) {
|
|
1013
|
+
this.stdout.write('\r\n' + options.join(' ') + '\r\n');
|
|
1014
|
+
this._redrawPromptLine();
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
|
|
546
1018
|
_splitInput(input) {
|
|
547
1019
|
const commentStripped = input.split('#')[0];
|
|
548
1020
|
return this._parseOperators(commentStripped);
|
package/lib/BaseFileSystem.js
CHANGED
|
@@ -79,4 +79,44 @@ class BaseFileSystem {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Represents a binary file stored as raw bytes in the virtual filesystem.
|
|
84
|
+
* Extends Uint8Array so binary-aware commands can access bytes directly,
|
|
85
|
+
* while text-based commands (cat, grep, etc.) get the original data URI
|
|
86
|
+
* via toString() — avoiding the 33% base64 memory overhead at runtime.
|
|
87
|
+
*/
|
|
88
|
+
class BinaryFile extends Uint8Array {
|
|
89
|
+
constructor(buffer, mimeType) {
|
|
90
|
+
super(buffer);
|
|
91
|
+
Object.defineProperty(this, 'mimeType', { value: mimeType, enumerable: false, configurable: false, writable: false });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
toString() {
|
|
95
|
+
if (typeof Buffer !== 'undefined') {
|
|
96
|
+
return `data:${this.mimeType};base64,${Buffer.from(this).toString('base64')}`;
|
|
97
|
+
}
|
|
98
|
+
// Browser path — chunked to avoid stack overflow on large files
|
|
99
|
+
let binary = '';
|
|
100
|
+
const chunk = 8192;
|
|
101
|
+
for (let i = 0; i < this.length; i += chunk) {
|
|
102
|
+
binary += String.fromCharCode(...this.subarray(i, i + chunk));
|
|
103
|
+
}
|
|
104
|
+
return `data:${this.mimeType};base64,${btoa(binary)}`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Represents a symbolic link stored in the virtual filesystem.
|
|
110
|
+
* Acts as an opaque marker: `_resolveByAbsolutePath` follows these
|
|
111
|
+
* transparently during file access, while lstat-like calls return the
|
|
112
|
+
* SymlinkRef itself so callers can detect and report symlinks.
|
|
113
|
+
*/
|
|
114
|
+
class SymlinkRef {
|
|
115
|
+
constructor(target) {
|
|
116
|
+
this.target = target;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
82
120
|
module.exports = BaseFileSystem;
|
|
121
|
+
module.exports.BinaryFile = BinaryFile;
|
|
122
|
+
module.exports.SymlinkRef = SymlinkRef;
|
package/lib/BinaryContainer.js
CHANGED
package/lib/Command.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const events = require('events');
|
|
4
4
|
const Result = require('./Result');
|
|
5
|
+
const { TtyStream, parseVtKeys } = require('./TtyStream');
|
|
5
6
|
|
|
6
7
|
const EventEmitter = events.EventEmitter || events.default || events;
|
|
7
8
|
|
|
@@ -11,6 +12,35 @@ class Command extends EventEmitter {
|
|
|
11
12
|
this.arguments = args;
|
|
12
13
|
this.result = new Result();
|
|
13
14
|
this.context = context || null;
|
|
15
|
+
|
|
16
|
+
const cols = (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('COLUMNS'), 10)) || 80;
|
|
17
|
+
const rows = (this.context && typeof this.context.getVar === 'function' && parseInt(this.context.getVar('LINES'), 10)) || 24;
|
|
18
|
+
|
|
19
|
+
this.stdinStream = new TtyStream({ columns: cols, rows: rows });
|
|
20
|
+
this.stdin = this.stdinStream;
|
|
21
|
+
this.stdout = new TtyStream({
|
|
22
|
+
columns: cols,
|
|
23
|
+
rows: rows,
|
|
24
|
+
onWrite: (chunk) => this.emit('stdout', chunk)
|
|
25
|
+
});
|
|
26
|
+
this.stderr = new TtyStream({
|
|
27
|
+
columns: cols,
|
|
28
|
+
rows: rows,
|
|
29
|
+
onWrite: (chunk) => this.emit('stderr', chunk)
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
this.stdinStream.on('data', (chunk) => {
|
|
33
|
+
if (!this.captureInput) return;
|
|
34
|
+
const keys = parseVtKeys(chunk);
|
|
35
|
+
for (const k of keys) {
|
|
36
|
+
if (!this.interactive && !this.stdinStream.isRaw) {
|
|
37
|
+
if (k === 'Enter') this.stdout.write('\r\n');
|
|
38
|
+
else if (k === 'Backspace') this.stdout.write('\b \b');
|
|
39
|
+
else if (k.length === 1) this.stdout.write(k);
|
|
40
|
+
}
|
|
41
|
+
this.onInput(k);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
14
44
|
}
|
|
15
45
|
|
|
16
46
|
get name() {
|
package/lib/FileSystem.js
CHANGED
|
@@ -1,44 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const BaseFileSystem = require('./BaseFileSystem');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Represents a binary file stored as raw bytes in the virtual filesystem.
|
|
7
|
-
* Extends Uint8Array so binary-aware commands can access bytes directly,
|
|
8
|
-
* while text-based commands (cat, grep, etc.) get the original data URI
|
|
9
|
-
* via toString() — avoiding the 33% base64 memory overhead at runtime.
|
|
10
|
-
*/
|
|
11
|
-
class BinaryFile extends Uint8Array {
|
|
12
|
-
constructor(buffer, mimeType) {
|
|
13
|
-
super(buffer);
|
|
14
|
-
Object.defineProperty(this, 'mimeType', { value: mimeType, enumerable: false, configurable: false, writable: false });
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
toString() {
|
|
18
|
-
if (typeof Buffer !== 'undefined') {
|
|
19
|
-
return `data:${this.mimeType};base64,${Buffer.from(this).toString('base64')}`;
|
|
20
|
-
}
|
|
21
|
-
// Browser path — chunked to avoid stack overflow on large files
|
|
22
|
-
let binary = '';
|
|
23
|
-
const chunk = 8192;
|
|
24
|
-
for (let i = 0; i < this.length; i += chunk) {
|
|
25
|
-
binary += String.fromCharCode(...this.subarray(i, i + chunk));
|
|
26
|
-
}
|
|
27
|
-
return `data:${this.mimeType};base64,${btoa(binary)}`;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Represents a symbolic link stored in the virtual filesystem.
|
|
33
|
-
* Acts as an opaque marker: `_resolveByAbsolutePath` follows these
|
|
34
|
-
* transparently during file access, while lstat-like calls return the
|
|
35
|
-
* SymlinkRef itself so callers can detect and report symlinks.
|
|
36
|
-
*/
|
|
37
|
-
class SymlinkRef {
|
|
38
|
-
constructor(target) {
|
|
39
|
-
this.target = target;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
4
|
+
const { BinaryFile, SymlinkRef } = BaseFileSystem;
|
|
42
5
|
|
|
43
6
|
/**
|
|
44
7
|
* Recursively converts `data:<mime>;base64,<...>` strings in a filesystem
|