'Error listening onData while executing a program using node-pty
Hello I have been trying to write a remote compiler using node-pty. Basic logic is if my client socket gets an event with language and source code from front end application. First I check for the language and then create a directory in current working directory. Then create a file and append source code in it, save it using random name. Try to compile using node-pty and send the result back.
Here is source,
const { io } = require('socket.io-client');
const terminal = require('node-pty');
const os = require('os');
const fs = require('fs')
const socket = io('http://192.168.1.13:4805');
socket.on(`${os.hostname()}_compile`, (data) => {
if (data.language === "py") {
try {
// Generate random string of length 10
const randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
// get current working directory to store the file
const cwd = process.cwd();
// create directory named 'py' if not exists
if (!fs.existsSync(`${cwd}/py`)) {
fs.mkdirSync(`${cwd}/py`);
}
fs.writeFileSync(`${cwd}/py/${randomString}.py`, data.code);
// execute the file
const pty = terminal.spawn('powershell.exe', [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: `${cwd}/py`,
env: process.env
});
pty.write(`python ${cwd}/py/${randomString}.py \r`);
pty.onData('data', (out) => {
console.log('Must be problem with only out');
//console.log(out);
/*socket.emit(`compile_result`, {
out: out,
language: data.language,
});*/
//pty.kill();
});
pty.onExit((code) => {
fs.unlinkSync(`${cwd}/py/${randomString}.py`);
});
pty.write('ls\r')
} catch (error) {
console.log("Throwing : " + error);
const errorEmit = {
'source': os.hostname(),
'error': 'Error in writing file'
};
socket.emit('error', errorEmit);
}
}
else if (data.language === "c") {}
else if (data.language === "cpp") {}
else if (data.language === "java") {}
});
I am getting error exactly on
pty.onData('data', (out) => {
console.log(out);
/*socket.emit(`compile_result`, {
out: out,
language: data.language,
});*/
//pty.kill();
});
error log as follows
D:\Website\Dunix\containers\compiler\compiler-node\node_modules\node-pty\lib\eventEmitter2.js:40
queue[i].call(undefined, data);
^
TypeError: queue[i].call is not a function
at EventEmitter2.fire (D:\Website\Dunix\containers\compiler\compiler-node\node_modules\node-pty\lib\eventEmitter2.js
:40:22)
at Socket.<anonymous> (D:\Website\Dunix\containers\compiler\compiler-node\node_modules\node-pty\lib\terminal.js:85:6
1)
at Socket.emit (node:events:532:35)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:285:11)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)
[nodemon] app crashed - waiting for file changes before starting...
is there anything I am doing wrong ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
