Skip to content

Commit

Permalink
Changed code style to match overall style.
Browse files Browse the repository at this point in the history
  • Loading branch information
diosney committed Sep 26, 2014
1 parent 12f5dbe commit 1e2e06f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 34 deletions.
28 changes: 17 additions & 11 deletions docs/README-iptables.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,20 @@ The wrapper error codes matches (unsurprisingly) the same as iptables. They are:

#### Dump

iptables.dump({
table: 'nat', // default: null == all
}, function(err, dump) {
for (var table_name in dump) {
var table_dump = dump[table];
for (var chain_name in table_dump.chains) {
var chain_dump = table_dump.chains[chain_name];
console.log(table_name, chain_name, chain_dump);
}
}
});
iptables.dump({
table: 'nat', // default: null == all
}, function(error, dump) {
if (error) {
console.log(error);
return;
}

for (var table_name in dump) {
var table_dump = dump[table];

for (var chain_name in table_dump.chains) {
var chain_dump = table_dump.chains[chain_name];
console.log(table_name, chain_name, chain_dump);
}
}
});
67 changes: 44 additions & 23 deletions lib/child_utils.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
var processQueue = require('process-queue');
var process_queue = require('process-queue');

var default_queue = processQueue.createQueue({concurrency:1});
var default_queue = process_queue.createQueue({
concurrency: 1
});

exports.exec = function (line, opts, cb) {
/**
* Wrapper around native `child_process.exec()` method to add queue capabilities.
*
* @param {String} line Command line to be executed.
* @param {Object} options Options to be passed to the underlying method, f.e.: options.queue.
* @param {Function} cb Callback function to be executed after command execution is completed.
*/
exports.exec = function (line, options, cb) {
var stdout, stdout_length, stderr, stderr_length;
var child;
var queue = options.queue || default_queue;

// avoid allocation
if (cb) {
// Avoid allocation.
stdout = [];
stdout_length = 0;
stderr = [];
stderr_length = 0;
}

var child;
var queue = opts.queue || default_queue;
if (cb) {
queue = queue.wrap({
child: function (spawned_child, next) {
child = spawned_child;

if (cb) {
child.stdout.on('data', function (data) {
if (!Buffer.isBuffer(data)) data = new Buffer(data);
if (!Buffer.isBuffer(data)) {
data = new Buffer(data);
}
stdout_length += data.length;
stdout.push(data);
});

child.stderr.on('data', function (data) {
if (!Buffer.isBuffer(data)) data = new Buffer(data);
if (!Buffer.isBuffer(data)) {
data = new Buffer(data);
}
stderr_length += data.length;
stderr.push(data);
});
Expand All @@ -35,27 +48,35 @@ exports.exec = function (line, opts, cb) {
}
});
}

var cmd = {
spawnOptions: ['sh', ['-c', 'exec ' + line]]
};
function onFinish(err) {
if (!cb) return;

function onFinish(error) {
if (!cb) {
return;
}

if (!child) {
cb(new Error('there was a problem spawning the child process'));
cb(new Error('There was a problem spawning the child process.'));
return;
}
if (err) {
err.killed = child.killed;
err.code = child.exitCode;
err.signal = child.signalCode;
}
cb(err,
stdout_length ? String(Buffer.concat(stdout, stdout_length)) : '',
stderr_length ? String(Buffer.concat(stderr, stderr_length)) : ''

if (error) {
error.killed = child.killed;
error.code = child.exitCode;
error.signal = child.signalCode;
}

cb(error,
stdout_length ? String(Buffer.concat(stdout, stdout_length)) : '',
stderr_length ? String(Buffer.concat(stderr, stderr_length)) : ''
);
}

queue.push(cmd, onFinish);

// cleanup for gc
// Cleanup for GC (Garbage Collector).
cmd = null;
}
};

0 comments on commit 1e2e06f

Please sign in to comment.