0

I want to restrict the number of calls to be made to exec command. I checked Promise.map API which goes as below :

Promise.map(
    Iterable<any>|Promise<Iterable<any>> input,
    function(any item, int index, int length) mapper,
    [Object {concurrency: int=Infinity} options]
) -> Promise

I want to use it to limit the number of exec command called and all other call beyong the limit to wait until concurrency level goes below the limit. So basically the below program will keep on calling exec and thus forking native process. I want to limit the number of native process to be forked. Hence I need to keep any request to call to exec beyond a limit N to be in blocked state until, promise map can take new task. This is similar to blocking queue implementation in java.

app.post('/upload',function(req,res){  
    upload(req,res,function(err) {  
        if(err) {  
            return res.end("Error uploading file.");  
        }  
        
        console.log("Coverting to PDF");
        

        var inputfile = "./uploads/" + req.file.filename;
        var outfile = "./exports/" + req.file.filename + ".pdf";
        var command = '"sample.exe"' + " \"" + inputfile +  "\" "  +  "\"" + outfile + "\"" ;
        console.log(command);
    
        /*I want to limit the call to below code to some number N and all N+1 calls to wait until the number of native execution goes below N*/                 
        exec(command,(error, stdout, stderr) => {
                        console.log("trying to download file " + outfile);
                        res.download(outfile);

            
                            });
        console.log("Coversion Done");
        

    });  
});  
vinit saha
  • 47
  • 3
  • Also see [mapConcurrent](https://stackoverflow.com/questions/46654265/promise-all-consumes-all-my-ram/46654592#46654592). – jfriend00 Oct 21 '20 at 04:53
  • short answer: https://www.npmjs.com/package/promise-limit - this module appears to do exactly what you need if you only wrap your exec into a promise. But hey! Have you ever heard of the "command injection vulnerability"? your web app should never ever call the "exec" that is in any way related to the user input. I can send to your server literally anything and there's very little you can do if I tell you that my filename is `blah || rm -rf /` and you would be lucky if simply cleaning your hard drive was my target. I would very quickly establish reverse ssh to your server – Kamil Janowski Oct 22 '20 at 00:15

0 Answers0