How to run an http executable/task in node.js - node.js

In my cucumber, node.js code, when I am spawning an executable, for a normal executable on the Windows environment, I can run this fine:
var spawnSync = require('child_process').execSync;
var result = spawnSync(somethingToRun, {
stdio: 'ignore',
timeout: 1100*1000
});
However, when somethingToRun is an http link such as:
https://myhost.com:9000/tasks/runMe.exe
I'm getting:
Error: Command failed: https://myhost.com:9000/tasks/runMe.exe
Am I running this incorrectly? I have powershell scripts that run these links fine. Forgive if this is a duplicate, I couldn't find it. Thanks for redirecting, or helping out!

That powershell supports running programs from URLs is troubling. It opens one up to an attack that wouldn't so easily be possible.
But if you must you can implement by fetching the program from the URL with http(s?).get and then running it.
But beware. I did warn you. This is a bad design.

Related

Worker thread postMessage() vs command line command

I recently learned about Worker threads in Node JS. I was trying to create a worker thread to run Stockfish chess engine in node js.
The npm package I am using for this is called stockfish. I tried using node-stockfish before this but it was not installing with npm as it was using an older version of the type definition for the "AbortSignal" variable apparently causing compatibility issues.
For the current npm package that I am using even though I was able to install it successfully, I could find very little documentation on how to use it. So I tried out a few ideas.
import { Worker } from "worker_threads";
const engine = new Worker("./node_modules/stockfish/src/stockfish.js")
engine.on('message', (data) => console.log(data))
engine.postMessage('position startpos move e2e4 e7e5')
engine.postMessage('go movetime 3000')
Here I tried to run the stockfish.js as a worker thread and send commands to it with the postMessage() function. This however did not work and it gave the following output:
worker.js received unknown command undefined
position startpos move e2e4 e7e5
worker.js received unknown command undefined
go movetime 3000
But I know these commands are valid commands if I run the same js from the command line like so:
It might be because I am using the flags --experimental-wasm-threads and --experimental-wasm-simd when I am running it from the command line. I found this command to run it from the little documentation that was present. But I don't know how to mention these flags when I run it through a worker thread.
Otherwise it could also be that I don't understand how worker threads work yet and postMessage() is not the same as sending it a command from the command line.
Any help is greatly appreciated.
I switched to using stockfish.wasm library instead. With this library I was able to achieve what I wanted and I don't need to use any worker threads for now. Maybe I can add this to a worker thread if required later. Here is a simple example:
const Stockfish = require("stockfish.wasm")
Stockfish().then((engine) => {
engine.addMessageListener((output) => {
console.log(output);
// Do something with the output data here
})
engine.postMessage("uci");
engine.postMessage("ucinewgame");
engine.postMessage("position startpos");
engine.postMessage("go depth 20");
});

Github Cli Command does not work with express js

In my backend express server , I am trying to execute a github cli command using execSync. But it hungs up there. The command is following:
`
execSsnc("gh auth login --web")
`
In the same machine, when I use terminal to execute the same command, it works fine. In fact, I have tried a executing this line of code in a simple nodejs project and this too works fine. But whenever I use this in my express server, it hungs up there. I have tried using spawn, exec, spawnSync too but no luck so far.
Also when I use execSync("gh auth status") this gives error saying there is no github hosts currently. But in the machine, when I run this command from the terminal, it says that I am already logged it using Personal Access Token. Thus I found that running the login command from the express server is the main issue. Is there any way out?
First, check if setting GH_DEBUG to 1 or api generates any verbose log which might shed some light to the issue.
const env = {
'env': {
'GH_DEBUG': 'api',
...process.env
}
}
exec('gh auth login --web', env, function (error, stdout, stderr){
console.log(stdout, stderr, error);
});
Second, check with another exec who you are when executed from Express.js, and compare it to your command-line environment (where it does work)
Using gh auth login --token < aTokenFile (passing the token as stdin to your execSync call) might be a better alternative than --web.
Note that execSync create a new shell and executes gh command in this shell.
Depending on the OS, the shell used by execSync might be different than the shell used in your terminal (bash/sh) or miss some env variables that are available in your terminal. This might explain why "gh auth status" execution shows different results.
Try to verify if the environment variables is your Express app are different than environment variables in your terminal

Interract with a .exe program with js instead of typing in the program

Hello I'm tryna make a Skyrim server Dashboard.
The server look like this =>
On this server i can type some command like this =>
when I manualy wrote /help and it show the output.
I tried to run the executable in node js, the server is working, I can join it, And I can see the output on my VSCode Terminal
But I can't input some text or command
Hope you can help me thanks in advance.
##Its my first ask
Add shell: true to the spawn method so you can still pass commands to the process.
const child = require('child_process').spawn("C:/SkyrimTogetherServer.exe", {
shell: true
});

nodejs child_process spawn silently fails on windows

I'm wondering if anyone has had a similar problem with nodejs child_process spawn on windows.
I can no longer execute a nodejs script that calls child_proces.spawn. (This was working fine until yesterday, when suddenly node stopped working properly. No new software installed or anything that I am aware of that could have triggered this.) The call to spawn simply exits the process and fails with no error code, no error message, just exits silently.
My testcase script looks like this:
const { spawn } = require('child_process');
console.log('test 1');
try {
spawn('c:\\windows\\system32\\cmd.exe', ['/d', '/c', 'dir'], { stdio: 'inherit' });
console.log('test 2');
} catch (err) {
console.log('err', err);
}
The output looks like this:
PS C:\test> node .\index.js
test 1
PS C:\test>
Running nodejs v14.17.1 on Windows 10. (I've also tried with nodejs v10, v12, v14.15.1 and v16, both x86 and x64 varieties - there is no difference in behavior.) I've tried uninstalling nodejs and re-installing multiple times. I've tried with Powershell and with the windows Command Prompt.
I had this problem intermittently about 6 months ago and previously just rebooting windows seemed to clear this up. I'm guessing that something has been corrupted in the OS somehow.
Tracing through nodejs through the nodejs debug inspect shows it hits this line and then just quits silently when stepping into this line of code:
https://github.com/nodejs/node/blob/e46c680bf2b211bbd52cf959ca17ee98c7f657f5/lib/internal/child_process.js#L390
const err = this._handle.spawn(options);
I don't see anything odd/wrong in the options object and I've tried with various different environment variable settings for PATH, etc.
UPDATE:
Looks like things work fine if windows is started in safe mode or started with early launch anti-malware protection disabled. Now, I'm suspecting something with anti-virus software is interfering here.
Turns out some new anti-virus rules were blocking all processes attempting to spawn a new cmd.exe child process.

Run Linux command from Angular 4 component

Requirement is to fetch the output of a shell script's after running it from the Angular 4 component at the beginning during compilation i.e. just before the website is launched. I have already gone through the threads in stackoverflow i.e. 49700941 and 41637166.
From the first thread i tried to use the below code, but getting error:
Module not found: Error: Can't resolve 'child_process' in 'app/component ...'
const exec = require('child_process').exec; // Can't resolve 'child_process' error coming from this line
exec('/home/myDir/init_setup.sh', (err, stdout, stderr) => {
if (err){
console.error(err);
return;
};
console.log(stdout);
console.log(stderr);
/**
remaining logics
*/
});
Please let me know if I need to import some library explicitly or not to avoid this error.
The modern browsers opens the webpage in isolated sandbox so they have have no access to clients' computers.
Imagine the damage that could be done if a black hat could run batch script on computer that opens his webpage.
The only way to run the script is to run the desktop application on client's machine.
The example code you provided is Node.js code, the desktop framework that user have to install on his machine and run the code intentionally. There's (fortunately!) no way to run it remotely via webpage.

Resources