@codebolt/codeboltjs 1.1.14 → 1.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,15 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ /**
4
+ * CustomEventEmitter class that extends the Node.js EventEmitter class.
5
+ */
6
+ declare class CustomEventEmitter extends EventEmitter {
7
+ }
1
8
  /**
2
9
  * A module for executing commands in a terminal-like environment via WebSocket.
3
10
  */
4
11
  declare const cbterminal: {
12
+ eventEmitter: CustomEventEmitter;
5
13
  /**
6
14
  * Executes a given command and returns the result.
7
15
  * Listens for messages from the WebSocket that indicate the output, error, or finish state
@@ -11,5 +19,37 @@ declare const cbterminal: {
11
19
  * @returns {Promise<any>} A promise that resolves with the command's output, error, or finish signal.
12
20
  */
13
21
  executeCommand: (command: string) => Promise<any>;
22
+ /**
23
+ * Executes a given command and keeps running until an error occurs.
24
+ * Listens for messages from the WebSocket and resolves the promise when an error is encountered.
25
+ *
26
+ * @param {string} command - The command to be executed.
27
+ * @returns {Promise<any>} A promise that resolves when an error occurs during command execution.
28
+ */
29
+ executeCommandRunUntilError: (command: string) => Promise<any>;
30
+ /**
31
+ * Executes a given command and keeps running until interrupted.
32
+ * Listens for messages from the WebSocket and resolves the promise when an interruption signal is received.
33
+ *
34
+ * @param {string} command - The command to be executed.
35
+ * @returns {Promise<any>} A promise that resolves when an interruption signal is received during command execution.
36
+ */
37
+ executeCommandRunUnitlInterrupt: (command: string) => Promise<any>;
38
+ /**
39
+ * Sends a manual interrupt signal to the terminal.
40
+ *
41
+ * @returns {void}
42
+ */
43
+ sendManualInterrupt(): Promise<any>;
44
+ /**
45
+ * Executes a given command and streams the output.
46
+ * Listens for messages from the WebSocket and streams the output data.
47
+ *
48
+ * @param {string} command - The command to be executed.
49
+ * @returns {Promise<any>} A promise that streams the output data during command execution.
50
+ */
51
+ executeCommandWithStream(command: string): {
52
+ event: CustomEventEmitter;
53
+ };
14
54
  };
15
55
  export default cbterminal;
@@ -4,10 +4,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const websocket_1 = __importDefault(require("./websocket"));
7
+ const events_1 = require("events");
8
+ /**
9
+ * CustomEventEmitter class that extends the Node.js EventEmitter class.
10
+ */
11
+ class CustomEventEmitter extends events_1.EventEmitter {
12
+ }
7
13
  /**
8
14
  * A module for executing commands in a terminal-like environment via WebSocket.
9
15
  */
10
16
  const cbterminal = {
17
+ eventEmitter: new CustomEventEmitter(),
11
18
  /**
12
19
  * Executes a given command and returns the result.
13
20
  * Listens for messages from the WebSocket that indicate the output, error, or finish state
@@ -29,6 +36,92 @@ const cbterminal = {
29
36
  }
30
37
  });
31
38
  });
39
+ },
40
+ /**
41
+ * Executes a given command and keeps running until an error occurs.
42
+ * Listens for messages from the WebSocket and resolves the promise when an error is encountered.
43
+ *
44
+ * @param {string} command - The command to be executed.
45
+ * @returns {Promise<any>} A promise that resolves when an error occurs during command execution.
46
+ */
47
+ executeCommandRunUntilError: async (command) => {
48
+ return new Promise((resolve, reject) => {
49
+ websocket_1.default.getWebsocket.send(JSON.stringify({
50
+ "type": "executeCommandRunUntilError",
51
+ "message": command,
52
+ }));
53
+ websocket_1.default.getWebsocket.on('message', (data) => {
54
+ const response = JSON.parse(data);
55
+ if (response.type === "commandError") {
56
+ resolve(response);
57
+ }
58
+ });
59
+ });
60
+ },
61
+ /**
62
+ * Executes a given command and keeps running until interrupted.
63
+ * Listens for messages from the WebSocket and resolves the promise when an interruption signal is received.
64
+ *
65
+ * @param {string} command - The command to be executed.
66
+ * @returns {Promise<any>} A promise that resolves when an interruption signal is received during command execution.
67
+ */
68
+ executeCommandRunUnitlInterrupt: async (command) => {
69
+ return new Promise((resolve, reject) => {
70
+ websocket_1.default.getWebsocket.send(JSON.stringify({
71
+ "type": "executeCommandRunUnitlInterrupt",
72
+ "message": command,
73
+ }));
74
+ websocket_1.default.getWebsocket.on('message', (data) => {
75
+ const response = JSON.parse(data);
76
+ if (response.type === "terminalInterruptResponse") {
77
+ resolve(response);
78
+ }
79
+ });
80
+ });
81
+ },
82
+ /**
83
+ * Sends a manual interrupt signal to the terminal.
84
+ *
85
+ * @returns {void}
86
+ */
87
+ sendManualInterrupt() {
88
+ return new Promise((resolve, reject) => {
89
+ websocket_1.default.getWebsocket.send(JSON.stringify({
90
+ "type": "sendInterruptToTerminal"
91
+ }));
92
+ websocket_1.default.getWebsocket.on('message', (data) => {
93
+ const response = JSON.parse(data);
94
+ if (response.type === "terminalInterrupted") {
95
+ resolve(response);
96
+ }
97
+ });
98
+ });
99
+ },
100
+ /**
101
+ * Executes a given command and streams the output.
102
+ * Listens for messages from the WebSocket and streams the output data.
103
+ *
104
+ * @param {string} command - The command to be executed.
105
+ * @returns {Promise<any>} A promise that streams the output data during command execution.
106
+ */
107
+ executeCommandWithStream(command) {
108
+ // Send the process started message
109
+ websocket_1.default.getWebsocket.send(JSON.stringify({
110
+ "type": "executeCommandWithStream",
111
+ "message": command,
112
+ }));
113
+ // Register event listener for WebSocket messages
114
+ websocket_1.default.getWebsocket.on('message', (data) => {
115
+ const response = JSON.parse(data);
116
+ console.log("Received message:", response);
117
+ if (response.type === "commandOutput" || response.type === "commandError" || response.type === "commandFinish")
118
+ // Emit a custom event based on the message type
119
+ this.eventEmitter.emit("serverEvents", response.response);
120
+ });
121
+ // Return an object that includes the event emitter and the stopProcess method
122
+ return {
123
+ event: this.eventEmitter
124
+ };
32
125
  }
33
126
  };
34
127
  exports.default = cbterminal;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebolt/codeboltjs",
3
- "version": "1.1.14",
3
+ "version": "1.1.17",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "",
@@ -45,6 +45,7 @@ const taskplaner = {
45
45
  });
46
46
  });
47
47
  },
48
+
48
49
  /**
49
50
  * Updates an existing task using a WebSocket message.
50
51
  * @param {string} task - The updated task information.
@@ -1,9 +1,15 @@
1
1
  import cbws from './websocket';
2
+ import { EventEmitter } from 'events';
2
3
 
4
+ /**
5
+ * CustomEventEmitter class that extends the Node.js EventEmitter class.
6
+ */
7
+ class CustomEventEmitter extends EventEmitter {}
3
8
  /**
4
9
  * A module for executing commands in a terminal-like environment via WebSocket.
5
10
  */
6
11
  const cbterminal = {
12
+ eventEmitter: new CustomEventEmitter(),
7
13
 
8
14
  /**
9
15
  * Executes a given command and returns the result.
@@ -26,6 +32,100 @@ const cbterminal = {
26
32
  }
27
33
  });
28
34
  });
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
+ executeCommandRunUnitlInterrupt: async (command: string): Promise<any> => {
67
+ return new Promise((resolve, reject) => {
68
+ cbws.getWebsocket.send(JSON.stringify({
69
+ "type": "executeCommandRunUnitlInterrupt",
70
+ "message": command,
71
+ }));
72
+ cbws.getWebsocket.on('message', (data: string) => {
73
+ const response = JSON.parse(data);
74
+ if (response.type === "terminalInterruptResponse") {
75
+ resolve(response);
76
+ }
77
+ });
78
+ });
79
+ },
80
+ /**
81
+ * Sends a manual interrupt signal to the terminal.
82
+ *
83
+ * @returns {void}
84
+ */
85
+ sendManualInterrupt(): Promise<any> {
86
+
87
+ return new Promise((resolve, reject) => {
88
+ cbws.getWebsocket.send(JSON.stringify({
89
+ "type": "sendInterruptToTerminal"
90
+ }));
91
+ cbws.getWebsocket.on('message', (data: string) => {
92
+ const response = JSON.parse(data);
93
+ if (response.type === "terminalInterrupted") {
94
+ resolve(response);
95
+ }
96
+ });
97
+ });
98
+ },
99
+
100
+ /**
101
+ * Executes a given command and streams the output.
102
+ * Listens for messages from the WebSocket and streams the output data.
103
+ *
104
+ * @param {string} command - The command to be executed.
105
+ * @returns {Promise<any>} A promise that streams the output data during command execution.
106
+ */
107
+ executeCommandWithStream(command: string) {
108
+ // Send the process started message
109
+ cbws.getWebsocket.send(JSON.stringify({
110
+ "type": "executeCommandWithStream",
111
+ "message": command,
112
+ }));
113
+ // Register event listener for WebSocket messages
114
+ cbws.getWebsocket.on('message', (data: string) => {
115
+ const response = JSON.parse(data);
116
+ console.log("Received message:", response);
117
+ if (response.type === "commandOutput" || response.type === "commandError" || response.type === "commandFinish")
118
+ // Emit a custom event based on the message type
119
+ this.eventEmitter.emit("serverEvents", response.response);
120
+ });
121
+
122
+ // Return an object that includes the event emitter and the stopProcess method
123
+ return {
124
+ event: this.eventEmitter
125
+ };
29
126
  }
127
+
128
+
129
+
30
130
  };
31
131
  export default cbterminal;