Skip to content

Commit

Permalink
server & core: handle binary terminal data (koush#1149)
Browse files Browse the repository at this point in the history
* server & core: send terminal size info

* server & core: handle binary terminal data

* send all data as buffer

* add guard to not crash on mismatched core
  • Loading branch information
bjia56 authored Oct 25, 2023
1 parent 2ef482c commit d3593b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
13 changes: 7 additions & 6 deletions plugins/core/ui/src/components/builtin/ShellComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,20 @@ export default {
this.socket.send(JSON.stringify({ dim: { cols: term.cols, rows: term.rows } }));
this.socket.on("message", (data) => {
term.write(new Uint8Array(Buffer.from(JSON.parse(data).data)));
term.write(new Uint8Array(Buffer.from(data)));
});
term.onData((data) => {
this.socket.send(JSON.stringify({ data }));
this.socket.send(Buffer.from(data, 'utf8'));
});
term.on('resize', dim => {
this.socket.send(JSON.stringify({ dim }));
term.onBinary((data) => {
// https://github.com/xtermjs/xterm.js/blob/2e02c37e528c1abc200ce401f49d0d7eae330e63/typings/xterm.d.ts#L859-L868
this.socket.send(Buffer.from(data, 'binary'));
});
term.onBinary((data) => {
this.socket.send(JSON.stringify({ data }));
term.on('resize', dim => {
this.socket.send(JSON.stringify({ dim }));
});
},
destroyed() {
Expand Down
21 changes: 15 additions & 6 deletions server/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,22 @@ export class ScryptedRuntime extends PluginHttp<HttpPluginData> {
const spawn = require('node-pty-prebuilt-multiarch').spawn as typeof ptySpawn;
const cp = spawn(process.env.SHELL, [], {
});
cp.onData(data => connection.send(JSON.stringify({data})));
cp.onData(data => connection.send(data));
connection.on('message', message => {
const parsed = JSON.parse(message.toString());
if (parsed.data) {
cp.write(parsed.data);
} else if (parsed.dim) {
cp.resize(parsed.dim.cols, parsed.dim.rows);
if (Buffer.isBuffer(message)) {
cp.write(message.toString());
return;
}

try {
const parsed = JSON.parse(message.toString());
if (parsed.dim) {
cp.resize(parsed.dim.cols, parsed.dim.rows);
}
} catch (e) {
// we should only get here if an outdated core plugin
// is sending us string data instead of buffer data
console.log(e);
}
});
connection.on('close', () => cp.kill());
Expand Down

0 comments on commit d3593b9

Please sign in to comment.