Too many connections cause node crash - node.js

My node program is crashed without any log.
I run the program below with node index.js.
const childProcess = require('child_process')
const util = require('util');
const exec = util.promisify(childProcess.exec);
console.time('total');
console.log("start");
const urlList = Array(500).fill("https://google.com");
const pList = urlList.map(function(url) {
return exec('curl --max-time 20 --connect-timeout 10 -iSs "' + url + '"', function (error, stdout, stderr) { });
});
Promise.all(pList).then(() => {
console.timeEnd('total');
}).catch((e) => {
console.log('error: ' + e);
});
I think it might be memory problem because of too many curl connections.
But how to figure out it?
Thank you in advance!

because util.promisify(childProcess.exec)() only receive string not string and function
So you must change it into
const pList = urlList.map(function(url) {
return exec('curl --max-time 20 --connect-timeout 10 -i "' + url + '"');
});

Related

How to display prompts and command outputs simultaneously in Nodejs?

I am trying to make a shell environment using Nodejs and encountered the following flaw in my program. I tried using the readline-sync and prompt-sync for resolving the issue and described about the problem next.
I am having the below code:
const prompt = require('prompt-sync')({sigint: true});
const { spawn } = require("child_process");
const os = require('os')
working_dir = os.homedir();
user_cmd = "";
while (user_cmd != "exit") {
user_cmd = prompt(working_dir + " >");
if (user_cmd.match("ls") != null) {
const ls = spawn("ls");
ls.stdout.on("data", data => {
console.log(`${data}`);
});
ls.stderr.on("data", data => {
console.log(`${data}`);
});
ls.on('error', (error) => {
console.log(`${error.message}`);
});
}
}
I want output in this way:
OUTPUT AFTER EACH PROMPT
hi#SanthoshSingh:/mnt/e/Atri Labs$ node shell.js
/home/hi >ls
hi.js
ls.js
node_modules
package-lock.json
package.json
shell.js
/home/hi >exit
but getting the output in this way:
AFTER ALL PROMPTS GETTING THE OUTPUT
hi#SanthoshSingh:/mnt/e/Atri Labs$ node shell.js
/home/hi >ls
/home/hi >exit
hi.js
ls.js
node_modules
package-lock.json
package.json
shell.js
Get me a solution people :-)
prompt-sync blocks the /dev/tty. Unless you exit from it you will not be able to print stdout buffer to tty(screen). You exit from it(prompt-sync) only after you exit the while loop.
following is an alternate implementation that fixes the above issue:
const prompt = require('prompt-sync')({sigint: true});
const { spawn } = require("child_process");
const os = require('os')
working_dir = os.homedir();
user_cmd = "";
function call() {
user_cmd = prompt(working_dir + " >");
const ls = spawn(user_cmd);
ls.stdout.on("data", data => {
console.log(`${data}`);
ls.kill('SIGINT')
});
ls.stderr.on("data", data => {
console.log(`${data}`);
});
ls.on('error', (error) => {
console.log(`${error.message}`);
ls.kill('SIGINT')
});
ls.on('exit', (error) => {
call()
});
}
const interval = setInterval(function () { }, 1000)
process.on('exit', () => {
clearTimeout(interval)
})
call()

NodeJs: child_process.stdout, `data` be cut off

I am using NodeJs to run JXA (JavaScript for Automation). Below is my code:
const spawn = require("child_process").spawn;
const StringToStream = require('string-to-stream');
const code = `
ObjC.import('stdlib');
console.log('inited');
`;
const child = spawn(
"osascript",
["-il", "JavaScript"],
{
maxBuffer: 1024 * 1024 * 1024
}
);
child.stdout.on('data', (data) => {
// Very strange, it didn't run to here
});
child.stderr.on('data', (data) => {
console.log(data.toString());
// run to hear. but the data be cut off
});
StringToStream(`
console.log('... Very-Long-String ...');
`).pipe(child.stdin, { end: false })
Very strange, stderr.on('data') be fired, and data be cut off. It seems that the output stream is length limited.
Does anyone know how to solve this problem ?

Piping shell script through bash using node.js

Say I have this:
const cp = require('child_process');
fs.createReadStream(__dirname + '/dummy.sh')
.pipe(cp.spawn('bash').stdin).on('data', d => {
console.log('data:', d);
});
and dummy.sh constains just echo "this is dummy", for whatever reason no data comes through in the on data callback. Does anyone know why that might be? I would expect this output data: this is dummy
You are not listening for the output of your bash command.
The on('data') event handler doesn't trigger your bash script output.
Instead, you should listen to the process stdout:
const fs = require('fs');
const cp = require('child_process');
let cmd = cp.spawn('bash');
fs.createReadStream(__dirname + '/dummy.sh')
.pipe(cmd.stdin).pipe(cmd.stdout).on('data', d => {
console.log('data:', d.toString()); // data: this is dummy
});

Stop nodejs child_process with browser api call

I have vue (axios) making a get call to an express route which triggers a child_process of ffmpeg in an infinite loop. ffmpeg streams one file over udp , on close it re calls itself and streams another file.
I'd like to be able to kill this process from a button on a web page, but can't seem to work it out.
This is my express route code
router.get('/test', function(req, res) {
const childProcess = require('child_process');
const fs = require('fs')
const path = require('path')
//Grabs a random index between 0 and length
function randomIndex(length) {
return Math.floor(Math.random() * (length));
}
function Stream () {
const FILE_SRC = '/path/to/file'
//Read the directory and get the files
const dirs = fs.readdirSync(FILE_SRC)
.map(file => {
return path.join(FILE_SRC, file);
});
const srcs_dup = [];
const hashCheck = {}; //used to check if the file was already added to srcs_dup
var numberOfFiles = dirs.length - 1; //OR whatever # you want
console.log(numberOfFiles)
//While we haven't got the number of files we want. Loop.
while (srcs_dup.length < numberOfFiles) {
var fileIndex = randomIndex(dirs.length-1);
//Check if the file was already added to the array
if (hashCheck[fileIndex] == true) {
continue; //Already have that file. Skip it
}
//Add the file to the array and object
srcs_dup.push(dirs[fileIndex]);
hashCheck[fileIndex] = true;
}
var chosen = "'" + srcs_dup[0] + "'"
var call = "ffmpeg -re -i " + chosen + " -content_type audio/mpeg -f mp3 udp://224.1.2.3:1234"
const stop = childProcess.exec(call, { shell: true });
stop.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
stop.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
stop.on('close', (code) => {
console.log ('child exited with code ' + code)
Stream();
});
stop.on('error', function(err) {
console.log('sh error' + err)
});
}

Curl install binary to users path from nodejs

Hello fellow nerds and nerdettes,
I just started building a little app that uses an external command line interface. The app first checks if the binary is installed in the users path and if not offers to install it for them. The external cli bin is the digitalocean cli and requires to curl, pipe to tar, and then move the bin into the users path. I have built the check if installed functionality and have been reading the child-process api but have been having a hard time figuring out how to console out the status of the curl command. My current incantation shows no console output. My question is this. How do i pipe the output of cURL to the console to confirm its working? How might i go about testing success then moving on?
Thanks y'all
const exec = require('child_process').exec
const curlScriptOSX = 'curl -L https://github.com/digitalocean/doctl/releases/download/v1.6.0/doctl-1.6.0-darwin-10.6-amd64.tar.gz | tar xz'
exec(curlScriptOSX, function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if(error !== null) {
console.log('exec error: ' + error);
}
})
UPDATE: i am looking at the request library as well. is it possible to
request(url).pipe(fs.createWriteStream('binary.tar.gz')).then(exec(extracting)).catch(err => console.error(err))
i wonder... ima try this now
Using the request library:
const fs = require('fs')
const os = require('os')
const request = require('request')
const url = 'https://github.com/digitalocean/doctl/releases/download/v1.6.0/doctl-1.6.0-darwin-10.6-amd64.tar.gz'
platform = os.platform()
function getInstallerFile (url) {
console.log("Getting tar")
// Variable to save downloading progress
var received_bytes = 0;
var total_bytes = 0;
const output = fs.createWriteStream('doctl.tar.gz')
request
.get(url)
.on('error', function(err) {
console.log(err);
})
.on('response', function(data) {
total_bytes = parseInt(data.headers['content-length']);
})
.on('data', function(chunk) {
received_bytes += chunk.length;
showDownloadingProgress(received_bytes, total_bytes);
})
.pipe(output);
};
function showDownloadingProgress(received, total) {
var percentage = ((received * 100) / total).toFixed(2);
process.stdout.write((platform == 'win32') ? "\033[0G": "\r");
process.stdout.write(percentage + "% | " + received + " bytes of " + total + " bytes.");
}
getInstallerFile(url)

Resources