Skip to content

Commit

Permalink
add support for interactive sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
EDDYMENS committed Jan 13, 2024
1 parent c68834f commit 6a517bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 45 deletions.
24 changes: 19 additions & 5 deletions backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@ var ptyProcess = pty.spawn(shell, [], {
});
wss.on('connection', ws => {
console.log("new session")

// Catch incoming request
ws.on('message', command => {
ptyProcess.write(command);
var processedCommand = commandProcessor(command)
// console.log(processedCommand, "incoming command");
ptyProcess.write(processedCommand);
})

ptyProcess.on('data', function (data) {
ws.send(data)
console.log(data);
// Output: Sent to the frontend
ptyProcess.on('data', function (rawOutput) {
var processedOutput = outputProcessor(rawOutput);
ws.send(processedOutput);
console.log(processedOutput);

});
})
})

const commandProcessor = function(command) {
return command;
}

const outputProcessor = function(output) {
return output;
}
45 changes: 5 additions & 40 deletions frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,22 @@ function init() {
};
prompt(term);

term.onData(e => {
switch (e) {
case '\u0003': // Ctrl+C
term.write('^C');
prompt(term);
break;
case '\r': // Enter
runCommand(term, command);
command = '';
break;
case '\u007F': // Backspace (DEL)
// Do not delete the prompt
if (term._core.buffer.x > 2) {
term.write('\b \b');
if (command.length > 0) {
command = command.substr(0, command.length - 1);
}
}
break;
case '\u0009':
console.log('tabbed', output, ["dd", "ls"]);
break;
default:
if (e >= String.fromCharCode(0x20) && e <= String.fromCharCode(0x7E) || e >= '\u00a0') {
command += e;
term.write(e);
}
}
term.onKey(key => {
runCommand(term, key.key);
});
}

function clearInput(command) {
var inputLengh = command.length;
for (var i = 0; i < inputLengh; i++) {
term.write('\b \b');
}
}
function prompt(term) {
command = '';
term.write('\r\n$ ');
}
socket.onmessage = (event) => {
term.write(event.data);

}

function runCommand(term, command) {
if (command.length > 0) {
clearInput(command);
socket.send(command + '\n');
return;
}
socket.send(command);

}

init();

0 comments on commit 6a517bb

Please sign in to comment.