Update src/terminal.js
Browse files- src/terminal.js +44 -47
src/terminal.js
CHANGED
|
@@ -1,54 +1,51 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
const server = http.createServer((req, res) => {
|
| 16 |
-
if (req.method === 'GET') {
|
| 17 |
-
const routeName = req.url.slice(1);
|
| 18 |
-
const assetObj = {
|
| 19 |
-
'': { file: "index.html", contentType: "text/html" },
|
| 20 |
-
'client.js': { file: "client.js", contentType: "text/javascript" }
|
| 21 |
-
}[routeName];
|
| 22 |
-
|
| 23 |
-
if (!assetObj) {
|
| 24 |
-
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
| 25 |
-
return res.end('Path not found');
|
| 26 |
-
}
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
| 33 |
-
res.end('Failed to load file');
|
| 34 |
-
} else {
|
| 35 |
-
res.writeHead(200, { 'Content-Type': assetObj.contentType });
|
| 36 |
-
res.end(data);
|
| 37 |
-
}
|
| 38 |
-
});
|
| 39 |
}
|
| 40 |
-
}
|
| 41 |
|
| 42 |
-
const
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
});
|
| 50 |
-
});
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os from 'os';
|
| 2 |
+
import pty from 'node-pty';
|
| 3 |
+
|
| 4 |
+
let sharedPtyProcess = null;
|
| 5 |
+
let sharedTerminalMode = false;
|
| 6 |
+
|
| 7 |
+
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
|
| 8 |
+
|
| 9 |
+
const spawnShell = () => {
|
| 10 |
+
return pty.spawn(shell, [], {
|
| 11 |
+
name: 'xterm-color',
|
| 12 |
+
env: process.env,
|
| 13 |
+
});
|
| 14 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
export const setSharedTerminalMode = (useSharedTerminal) => {
|
| 17 |
+
sharedTerminalMode = useSharedTerminal;
|
| 18 |
+
if (sharedTerminalMode && !sharedPtyProcess) {
|
| 19 |
+
sharedPtyProcess = spawnShell();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
+
};
|
| 22 |
|
| 23 |
+
export const handleTerminalConnection = (ws) => {
|
| 24 |
+
let ptyProcess = sharedTerminalMode ? sharedPtyProcess : spawnShell();
|
| 25 |
|
| 26 |
+
ws.on('message', command => {
|
| 27 |
+
const processedCommand = commandProcessor(command);
|
| 28 |
+
ptyProcess.write(processedCommand);
|
| 29 |
+
});
|
| 30 |
|
| 31 |
+
ptyProcess.on('data', (rawOutput) => {
|
| 32 |
+
const processedOutput = outputProcessor(rawOutput);
|
| 33 |
+
ws.send(processedOutput);
|
| 34 |
});
|
|
|
|
| 35 |
|
| 36 |
+
ws.on('close', () => {
|
| 37 |
+
if (!sharedTerminalMode) {
|
| 38 |
+
ptyProcess.kill();
|
| 39 |
+
}
|
| 40 |
+
});
|
| 41 |
+
};
|
| 42 |
+
|
| 43 |
+
// Utility function to process commands
|
| 44 |
+
const commandProcessor = (command) => {
|
| 45 |
+
return command;
|
| 46 |
+
};
|
| 47 |
+
|
| 48 |
+
// Utility function to process output
|
| 49 |
+
const outputProcessor = (output) => {
|
| 50 |
+
return output;
|
| 51 |
+
};
|