Skip to content

Commit 2419c8a

Browse files
terminal events added
1 parent 9f16b99 commit 2419c8a

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

src/modules/terminal.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import cbws from './websocket';
2+
import { EventEmitter } from 'events';
23

4+
/**
5+
* CustomEventEmitter class that extends the Node.js EventEmitter class.
6+
*/
7+
class CustomEventEmitter extends EventEmitter {}
38
/**
49
* A module for executing commands in a terminal-like environment via WebSocket.
510
*/
611
const cbterminal = {
12+
eventEmitter: new CustomEventEmitter(),
713

814
/**
915
* Executes a given command and returns the result.
@@ -26,6 +32,81 @@ const cbterminal = {
2632
}
2733
});
2834
});
35+
},
36+
37+
/**
38+
* Executes a given command and keeps running until an error occurs.
39+
* Listens for messages from the WebSocket and resolves the promise when an error is encountered.
40+
*
41+
* @param {string} command - The command to be executed.
42+
* @returns {Promise<any>} A promise that resolves when an error occurs during command execution.
43+
*/
44+
executeCommandRunUntilError: async (command: string): Promise<any> => {
45+
return new Promise((resolve, reject) => {
46+
cbws.getWebsocket.send(JSON.stringify({
47+
"type": "executeCommandRunUntilError",
48+
"message": command,
49+
}));
50+
cbws.getWebsocket.on('message', (data: string) => {
51+
const response = JSON.parse(data);
52+
if ( response.type === "commandError") {
53+
resolve(response);
54+
}
55+
});
56+
});
57+
},
58+
59+
/**
60+
* Executes a given command and keeps running until interrupted.
61+
* Listens for messages from the WebSocket and resolves the promise when an interruption signal is received.
62+
*
63+
* @param {string} command - The command to be executed.
64+
* @returns {Promise<any>} A promise that resolves when an interruption signal is received during command execution.
65+
*/
66+
executeCommandRunUnitlIntrupt: async (command: string): Promise<any> => {
67+
return new Promise((resolve, reject) => {
68+
cbws.getWebsocket.send(JSON.stringify({
69+
"type": "executeCommandRunUnitlIntrupt",
70+
"message": command,
71+
}));
72+
cbws.getWebsocket.on('message', (data: string) => {
73+
const response = JSON.parse(data);
74+
if (response.type === "terminalIntruptResponse") {
75+
resolve(response);
76+
}
77+
});
78+
});
79+
},
80+
81+
/**
82+
* Executes a given command and streams the output.
83+
* Listens for messages from the WebSocket and streams the output data.
84+
*
85+
* @param {string} command - The command to be executed.
86+
* @returns {Promise<any>} A promise that streams the output data during command execution.
87+
*/
88+
executeCommandWithStream(command: string) {
89+
// Send the process started message
90+
cbws.getWebsocket.send(JSON.stringify({
91+
"type": "executeCommandWithStream",
92+
"message": command,
93+
}));
94+
// Register event listener for WebSocket messages
95+
cbws.getWebsocket.on('message', (data: string) => {
96+
const response = JSON.parse(data);
97+
console.log("Received message:", response);
98+
if (response.type === "commandOutput" || response.type === "commandError" || response.type === "commandFinish")
99+
// Emit a custom event based on the message type
100+
this.eventEmitter.emit("serverEvents", response.response);
101+
});
102+
103+
// Return an object that includes the event emitter and the stopProcess method
104+
return {
105+
event: this.eventEmitter
106+
};
29107
}
108+
109+
110+
30111
};
31112
export default cbterminal;

0 commit comments

Comments
 (0)