use send for child process

This commit is contained in:
barisusakli
2014-11-18 22:55:44 -05:00
parent 871cf8a3b7
commit a445e3f7b1
2 changed files with 52 additions and 46 deletions

View File

@@ -4,34 +4,28 @@
var fork = require('child_process').fork;
module.hash = function(rounds, password, callback) {
var child = fork('./bcrypt', ['hash', rounds, password], {
silent: true
}),
response = '';
child.stdout.on('data', function(chunk) {
response += chunk.toString();
});
child.stdout.on('end', function() {
callback(null, response);
});
forkChild({type: 'hash', rounds: rounds, password: password}, callback);
};
module.compare = function(password, hash, callback) {
var child = fork('./bcrypt', ['compare', password, hash], {
silent: true
}),
response = '';
child.stdout.on('data', function(chunk) {
response += chunk.toString();
});
child.stdout.on('end', function() {
callback(null, response === 'true');
});
forkChild({type: 'compare', password: password, hash: hash}, callback);
};
function forkChild(message, callback) {
var child = fork('./bcrypt', {
silent: true
});
child.on('message', function(msg) {
if (msg.err) {
return callback(new Error(msg.err));
}
callback(null, msg.result);
});
child.send(message);
}
return module;
})(exports);