feat: add workerpool for password, closes #10326 (#12038)

This commit is contained in:
Barış Soner Uşaklı
2023-09-26 10:48:58 -04:00
committed by GitHub
parent f1a80d48cc
commit 8b25aff79a
3 changed files with 27 additions and 53 deletions

View File

@@ -146,6 +146,7 @@
"webpack": "5.88.2", "webpack": "5.88.2",
"webpack-merge": "5.9.0", "webpack-merge": "5.9.0",
"winston": "3.10.0", "winston": "3.10.0",
"workerpool": "6.5.0",
"xml": "1.0.1", "xml": "1.0.1",
"xregexp": "5.1.1", "xregexp": "5.1.1",
"yargs": "17.7.2", "yargs": "17.7.2",

View File

@@ -2,31 +2,17 @@
const path = require('path'); const path = require('path');
const crypto = require('crypto'); const crypto = require('crypto');
const util = require('util'); const workerpool = require('workerpool');
const bcrypt = require('bcryptjs'); const pool = workerpool.pool(
path.join(__dirname, '/password_worker.js'), {
const fork = require('./meta/debugFork'); minWorkers: 1,
}
function forkChild(message, callback) { );
const child = fork(path.join(__dirname, 'password'));
child.on('message', (msg) => {
callback(msg.err ? new Error(msg.err) : null, msg.result);
});
child.on('error', (err) => {
console.error(err.stack);
callback(err);
});
child.send(message);
}
const forkChildAsync = util.promisify(forkChild);
exports.hash = async function (rounds, password) { exports.hash = async function (rounds, password) {
password = crypto.createHash('sha512').update(password).digest('hex'); password = crypto.createHash('sha512').update(password).digest('hex');
return await forkChildAsync({ type: 'hash', rounds: rounds, password: password }); return await pool.exec('hash', [password, rounds]);
}; };
exports.compare = async function (password, hash, shaWrapped) { exports.compare = async function (password, hash, shaWrapped) {
@@ -35,8 +21,7 @@ exports.compare = async function (password, hash, shaWrapped) {
if (shaWrapped) { if (shaWrapped) {
password = crypto.createHash('sha512').update(password).digest('hex'); password = crypto.createHash('sha512').update(password).digest('hex');
} }
return await pool.exec('compare', [password, hash || fakeHash]);
return await forkChildAsync({ type: 'compare', password: password, hash: hash || fakeHash });
}; };
let fakeHashCache; let fakeHashCache;
@@ -48,34 +33,4 @@ async function getFakeHash() {
return fakeHashCache; return fakeHashCache;
} }
// child process
process.on('message', (msg) => {
if (msg.type === 'hash') {
tryMethod(hashPassword, msg);
} else if (msg.type === 'compare') {
tryMethod(compare, msg);
}
});
async function tryMethod(method, msg) {
try {
const result = await method(msg);
process.send({ result: result });
} catch (err) {
process.send({ err: err.message });
} finally {
process.disconnect();
}
}
async function hashPassword(msg) {
const salt = await bcrypt.genSalt(parseInt(msg.rounds, 10));
const hash = await bcrypt.hash(msg.password, salt);
return hash;
}
async function compare(msg) {
return await bcrypt.compare(String(msg.password || ''), String(msg.hash || ''));
}
require('./promisify')(exports); require('./promisify')(exports);

18
src/password_worker.js Normal file
View File

@@ -0,0 +1,18 @@
'use strict';
const workerpool = require('workerpool');
const bcrypt = require('bcryptjs');
async function hash(password, rounds) {
const salt = await bcrypt.genSalt(parseInt(rounds, 10));
return await bcrypt.hash(password, salt);
}
async function compare(password, hash) {
return await bcrypt.compare(String(password || ''), String(hash || ''));
}
workerpool.worker({
hash: hash,
compare: compare,
});