{"size": 1148, "ext": "js", "max_stars_repo_path": "src/password.js", "max_stars_repo_name": "cablelabs/dev-portal", "max_stars_repo_stars_event_min_datetime": "2018-01-31T03:49:51.000Z", "max_stars_repo_stars_event_max_datetime": "2018-01-31T03:49:51.000Z", "max_issues_repo_path": "src/password.js", "max_issues_repo_name": "cablelabs/dev-portal", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/password.js", "max_forks_repo_name": "cablelabs/dev-portal", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.0769230769, "max_line_length": 82, "alphanum_fraction": 0.6297909408}


'use strict';
var fork = require('child_process').fork;

(function(module) {

	var child = fork('./bcrypt', process.argv.slice(2), {
					env: process.env
				});

	var callbacks = {
		'hash': {},
		'compare': {}
	};

	module.hash = function(rounds, password, callback) {
		sendCommand({type: 'hash', password: password, rounds: rounds}, callback);
	};

	module.compare = function(password, hash, callback) {
		sendCommand({type: 'compare', password: password, hash: hash}, callback);
	};

	function sendCommand(data, callback) {
		callbacks[data.type][data.password] = callbacks[data.type][data.password] || [];
		callbacks[data.type][data.password].push(callback);
		child.send(data);
	}

	child.on('message', function(msg) {
		var cbs = callbacks[msg.type] ? callbacks[msg.type][msg.password] : null;

		if (Array.isArray(cbs)) {
			if (msg.err) {
				var err = new Error(msg.err.message);
				cbs.forEach(function(callback) {
					callback(err);
				});
				cbs.length = 0;
				return;
			}

			cbs.forEach(function(callback) {
				callback(null, msg.type === 'hash' ? msg.hash : msg.result);
			});
			cbs.length = 0;
		}
	});

}(exports));
