11import http from "http" ;
2- import { WebSocketServer } from "ws" ;
3- import os from "os" ;
42import pty from "node-pty" ;
3+ import os from "os" ;
4+ import { WebSocketServer } from "ws" ;
55
66let sharedPtyProcess = null ;
77let sharedTerminalMode = false ;
@@ -25,14 +25,20 @@ const setSharedTerminalMode = (useSharedTerminal) => {
2525const handleTerminalConnection = ( ws ) => {
2626 let ptyProcess = sharedTerminalMode ? sharedPtyProcess : spawnShell ( ) ;
2727
28- ws . on ( "message" , ( command ) => {
29- const processedCommand = commandProcessor ( command ) ;
30- ptyProcess . write ( processedCommand ) ;
28+ ws . on ( "message" , ( data ) => {
29+ const dataObj = JSON . parse ( data ) ;
30+
31+ if ( dataObj . type === "input" ) {
32+ const command = dataObj . payload ;
33+ ptyProcess . write ( command ) ;
34+ } else if ( dataObj . type === "resize" ) {
35+ const { cols, rows } = dataObj . payload ;
36+ ptyProcess . resize ( cols , rows ) ;
37+ }
3138 } ) ;
3239
3340 ptyProcess . on ( "data" , ( rawOutput ) => {
34- const processedOutput = outputProcessor ( rawOutput ) ;
35- ws . send ( processedOutput ) ;
41+ ws . send ( JSON . stringify ( { type : "output" , payload : rawOutput } ) ) ;
3642 } ) ;
3743
3844 ws . on ( "close" , ( ) => {
@@ -42,16 +48,6 @@ const handleTerminalConnection = (ws) => {
4248 } ) ;
4349} ;
4450
45- // Utility function to process commands
46- const commandProcessor = ( command ) => {
47- return command ;
48- } ;
49-
50- // Utility function to process output
51- const outputProcessor = ( output ) => {
52- return output ;
53- } ;
54-
5551/* Host ws node-pty server */
5652setSharedTerminalMode ( false ) ; // Set this to false to allow a shared session
5753const port = 6060 ;
0 commit comments