Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling child_process.spawn() parameter "windowsVerbatimArguments" #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
53 changes: 27 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name":"stateful-process-command-proxy",
"version":"1.0.1",
"description":"Module for executing commands in shells (bash or powershell etc) against a pool of stateful child processes such as bash or powershell via stdout and stderr streams",
"main":"statefulProcessCommandProxy.js",
"directories":{
"test":"test"
"name": "stateful-process-command-proxy",
"version": "1.0.1",
"description": "Module for executing commands in shells (bash or powershell etc) against a pool of stateful child processes such as bash or powershell via stdout and stderr streams",
"main": "statefulProcessCommandProxy.js",
"directories": {
"test": "test"
},
"scripts":{
"test":"mocha test/all.js"
"scripts": {
"test": "mocha test/all.js"
},
"keywords":[
"keywords": [
"command",
"process",
"dos",
Expand All @@ -22,28 +22,29 @@
"execute",
"exec"
],
"dependencies":{
"buffer-builder":"latest",
"fifo":"latest",
"promise":"latest",
"generic-pool":"2.4.4"
"dependencies": {
"buffer-builder": "latest",
"fifo": "latest",
"generic-pool": "2.4.4",
"mobx": "^5.15.2",
"promise": "latest"
},
"devDependencies":{
"mocha":"latest"
"devDependencies": {
"mocha": "latest"
},
"contributors":[
"contributors": [
{
"name":"bitsofinfo",
"url":"http://bitsofinfo.wordpress.com"
"name": "bitsofinfo",
"url": "http://bitsofinfo.wordpress.com"
}
],
"license":"ISC",
"repository":{
"type":"git",
"url":"https://github.com/bitsofinfo/stateful-process-command-proxy.git"
"license": "ISC",
"repository": {
"type": "git",
"url": "https://github.com/bitsofinfo/stateful-process-command-proxy.git"
},
"bugs":{
"url":"https://github.com/bitsofinfo/stateful-process-command-proxy/issues"
"bugs": {
"url": "https://github.com/bitsofinfo/stateful-process-command-proxy/issues"
},
"homepage":"https://github.com/bitsofinfo/stateful-process-command-proxy"
"homepage": "https://github.com/bitsofinfo/stateful-process-command-proxy"
}
19 changes: 14 additions & 5 deletions processProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var fifo = require('fifo');
var Command = require('./command');
var spawn = require('child_process').spawn;
var Promise = require('promise');
const { observable, action } = require('mobx');

var MARKER_DONE = '__done__';

Expand Down Expand Up @@ -114,15 +115,18 @@ fifo.prototype.toArray = function () {
* ]
* }
*
*
* @param windowsVerbatimArguments : optional boolean which, on win32 only, will prevent or allow parameter quoting (as defined in
* child_process.spawn() method)
* By default, this setting has value true (no escaping)
*
*/
function ProcessProxy(processToSpawn, args,
retainMaxCmdHistory, invalidateOnRegex,
cwd, envMap, uid, gid, logFunction,
processCmdBlacklistRegex,
processCmdWhitelistRegex,
autoInvalidationConfig) {
autoInvalidationConfig,
windowsVerbatimArguments) {

this._createdAt = new Date();
this._processPid = null;
Expand All @@ -131,7 +135,7 @@ function ProcessProxy(processToSpawn, args,
this._logFunction = logFunction;


this._commandHistory = [];
this._commandHistory = observable([]);
if(typeof(retainMaxCmdHistory)==='undefined') {
this._retainMaxCmdHistory = 0;

Expand Down Expand Up @@ -187,6 +191,7 @@ function ProcessProxy(processToSpawn, args,
if (gid) {
this._processOptions['gid'] = gid;
}
this._processOptions['windowsVerbatimArguments'] = !(windowsVerbatimArguments === false);

this._commandStack = new fifo();

Expand Down Expand Up @@ -365,7 +370,8 @@ ProcessProxy.prototype.isValid = function() {
* internal method that analyzes a just finish()ed command and
* evaluates all process invalidation regexes against it
**/
ProcessProxy.prototype._handleCommandFinished = function(command) {

ProcessProxy.prototype._handleCommandFinished = action(function(command) {
if (command && command.isCompleted()) {

// store command history...
Expand Down Expand Up @@ -423,7 +429,7 @@ ProcessProxy.prototype._handleCommandFinished = function(command) {
}
}
}
}
})

/**
* _commandIsBlacklisted(command)
Expand Down Expand Up @@ -613,11 +619,14 @@ ProcessProxy.prototype.initialize = function(initCommands) {

// register stdout stream handler
self._process.stdout.on('data', function(data) {
console.log(`[${self._process.pid}] [out] ${data}`);
self.onData('stdout', data);
});

// register stderr stream handler
self._process.stderr.on('data', function(data) {
console.log(`[${self._process.pid}] [err] ${data}`);

self.onData('stderr', data);
});

Expand Down
19 changes: 12 additions & 7 deletions statefulProcessCommandProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = StatefulProcessCommandProxy;
var poolModule = require('generic-pool');
var ProcessProxy = require('./processProxy');
var Promise = require('promise');
const { observable, action } = require('mobx');

/**
* StatefulProcessCommandProxy is the gateway for executing commands
Expand Down Expand Up @@ -111,6 +112,9 @@ var Promise = require('promise');
},...
]
}
windowsVerbatimArguments : optional boolean which, on win32 only, will prevent or allow parameter quoting (as defined in
child_process.spawn() method)
By default, this setting has value true (no escaping)

*
**/
Expand All @@ -121,7 +125,7 @@ function StatefulProcessCommandProxy(config) {
this._logFunction = config.logFunction;

// map of all process PIDs -> ProcessProxies
this._pid2processMap = new Object();
this._pid2processMap = observable(new Object());

var self = this;

Expand All @@ -145,18 +149,19 @@ function StatefulProcessCommandProxy(config) {
config.logFunction,
config.processCmdBlacklistRegex,
config.processCmdWhitelistRegex,
config.autoInvalidationConfig);
config.autoInvalidationConfig,
config.windowsVerbatimArguments);


// initialize
processProxy.initialize(config.initCommands)

.then(function(cmdResults) {
self._log('info',"new process ready, initialization commands completed.");
.then(action('initStatefulProcessCommandProxy',function(cmdResults) {
self._log('info',"new process ready, initialization commands completed. ("+processProxy.getPid()+')');
self._pid2processMap[processProxy.getPid()] = processProxy; // register in our process map
callback(null, processProxy);

}).catch(function(exception) {
})).catch(function(exception) {
self._log('error',"new process initialize threw error: " + exception + ' ' + exception.stack);
});

Expand All @@ -176,7 +181,7 @@ function StatefulProcessCommandProxy(config) {
},


destroy: function(processProxy) {
destroy: action('destroyPid2ProcessMap', function(processProxy) {

try {
processProxy.shutdown(config.preDestroyCommands)
Expand Down Expand Up @@ -207,7 +212,7 @@ function StatefulProcessCommandProxy(config) {
config.preDestroyCommands + "] exception: " + e);
}

},
}),

// maximum number in the pool
max: config.max,
Expand Down