YoBatM commited on
Commit
bccbbe3
·
verified ·
1 Parent(s): f078a88

Update src/terminal.js

Browse files
Files changed (1) hide show
  1. src/terminal.js +44 -47
src/terminal.js CHANGED
@@ -1,54 +1,51 @@
1
- import http from 'http';
2
- import { WebSocketServer } from 'ws';
3
- import fs from 'fs';
4
- import path from 'path';
5
- import { fileURLToPath } from 'url';
6
- import { handleTerminalConnection, setSharedTerminalMode } from 'terminal.js';
7
-
8
- const __filename = fileURLToPath(import.meta.url);
9
- const __dirname = path.dirname(__filename);
10
-
11
- // Config
12
- setSharedTerminalMode(false); // Set this to false to allow a shared session
13
- const port = 6060;
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
- const filePath = path.join(__dirname, assetObj.file);
29
-
30
- fs.readFile(filePath, (err, data) => {
31
- if (err) {
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 wss = new WebSocketServer({ noServer: true });
 
43
 
44
- wss.on('connection', handleTerminalConnection);
 
 
 
45
 
46
- server.on('upgrade', (request, socket, head) => {
47
- wss.handleUpgrade(request, socket, head, (ws) => {
48
- wss.emit('connection', ws, request);
49
  });
50
- });
51
 
52
- server.listen(port, () => {
53
- console.log(`HTTP and WebSocket server is running on port ${port}`);
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
+ };