Socket
Socket
Sign inDemoInstall

brsolab-process

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    brsolab-process

Nodejs Child Process wrapper with timeout and promises


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

brsolab-process

Wrapper for Nodejs Child process library Uses the ES6 to make it simple the use of subprocess in nodeJS

This library contains 3 functions: spawn, exec, and escape spawn is used to create processes. exec is used to execute commandes via the system interpreter (SH, BATCH, MS-DOS, ...) escape is used to escape arguments to exec commande

Those two functions use the some options as the original ones of nodejs. See the doc page at: https://nodejs.org/api/child_process.html

spawn

/**
 * spawn process
 * spaw(cmd, [args, [options, [processInitCallBack]]])
 * spaw(cmd, [args, [processInitCallBack]])
 * @param  {string} command The command to run
 * @param  {Array} args    List of string arguments
 * @param  {Object} options
 *         timeout{number}		: process execution timeout in ms 
 *         cwd	{String}		: Current working directory of the child process
 *         env	{Object}		: Environment key-value pairs
 *         argv0{String}		: Explicitly set the value of argv[0] sent to the child process. This will be set to command if not specified.
 *         stdio{Array | String}: Child's stdio configuration
 *         detached{boolean}	: Prepare child to run independently of its parent process.
 *         uid	{number}		: Sets the user identity of the process
 *         gid	{number}		: Sets the group identity of the process
 *         shell{boolean|string}: If true, runs command inside of a shell. Uses '/bin/sh' on UNIX, and process.env.ComSpec on Windows. A different shell can be specified as a string.
 * @return {Promise}
 */

const {spawn} = require('brsolab-process');
var list	= await spawn('ls', ['-l', '/home'], {timeout: 500});

exec

/**
 * exec process
 * exec(cmd, [options, [processInitCallBack]])
 * spaw(cmd, [processInitCallBack])
 * @param  {string} command The command to run
 * @param  {Array} args    List of string arguments
 * @param  {Object} options
 *         timeout{number}		: process execution timeout in ms 
 *         cwd	{String}		: Current working directory of the child process
 *         env	{Object}		: Environment key-value pairs
 *         argv0{String}		: Explicitly set the value of argv[0] sent to the child process. This will be set to command if not specified.
 *         stdio{Array | String}: Child's stdio configuration
 *         detached{boolean}	: Prepare child to run independently of its parent process.
 *         uid	{number}		: Sets the user identity of the process
 *         gid	{number}		: Sets the group identity of the process
 *         shell{boolean|string}: If true, runs command inside of a shell. Uses '/bin/sh' on UNIX, and process.env.ComSpec on Windows. A different shell can be specified as a string.
 * @return {Promise}
 */

const {exec, escape} = require('brsolab-process');
var list	= await exec('ls ' + escape(['-l', '/home']), {timeout: 500});
await exec('ls ' + escape(['-l', '/home']) + ' > list.txt', {timeout: 500});

examples

const { spawn, exec } = require('brsolab-process');
const fs  = require('fs');

exec('ls .').then( result => console.log(result) ).catch( error => console.error(error) );

// it's better to use "async" & "await"
(async function(){
  result  = await spawn('command', [args]);
  console.log(result);
  
  // pipe the output to a file
  await spawn('commande', [args], {stdio : ['pipe', fs.openSync('file.txt'), 'pipe']});
  
  // send messages to process
  var result = await spawn('cmd', [args], {options}, prcess => {
    // operations on prcess
    prcess.send({ foo: 'bar', baz: NaN });
  });
})();

Keywords

FAQs

Last updated on 13 Dec 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc