Skip to content

Commit

Permalink
BC-8451 Remove unnecessary usage of promisify (#5355)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper authored Nov 27, 2024
1 parent b4bc6f7 commit 340b59b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
9 changes: 3 additions & 6 deletions src/services/helpers/hash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const bcrypt = require('bcryptjs');
const { promisify } = require('es6-promisify');
const { BadRequest } = require('../../errors');
const { userModel } = require('../user/model');
const { getRandomInt } = require('../../utils/randomNumberGenerator');
Expand Down Expand Up @@ -43,21 +42,19 @@ const rndChar = () => {
};

module.exports = function setup() {
const genSaltAsync = promisify(bcrypt.genSalt);
const genHashAsync = promisify(bcrypt.hash);
class HashService {
constructor(options) {
this.options = options || {};
this.docs = {};
}

async create(data, params) {
async create(data) {
if (data.toHash === undefined) {
throw new BadRequest('Please set toHash key.');
}
const salt = await genSaltAsync(8);
const salt = await bcrypt.genSalt(8);

let hash = await genHashAsync(data.toHash, salt);
let hash = await bcrypt.hash(data.toHash, salt);

if (data.save === true || data.save === 'true') {
hash = hash.replace(/\/|\$|\./g, rndChar());
Expand Down
9 changes: 4 additions & 5 deletions src/utils/redis.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const { promisify } = require('util');
const Redis = require('ioredis');
const { Configuration } = require('@hpi-schul-cloud/commons');

Expand Down Expand Up @@ -32,19 +31,19 @@ function setRedisClient(client) {
}

const redisGetAsync = (...args) => {
if (redisClient) return promisify(redisClient.get).apply(redisClient, args);
if (redisClient) return redisClient.get(...args);
throw new GeneralError('No redis connection. Check for this via getRedisClient().');
};
const redisSetAsync = (...args) => {
if (redisClient) return promisify(redisClient.set).apply(redisClient, args);
if (redisClient) return redisClient.set(...args);
throw new GeneralError('No redis connection. Check for this via getRedisClient().');
};
const redisDelAsync = (...args) => {
if (redisClient) return promisify(redisClient.del).apply(redisClient, args);
if (redisClient) return redisClient.del(...args);
throw new GeneralError('No redis connection. Check for this via getRedisClient().');
};
const redisTtlAsync = (...args) => {
if (redisClient) return promisify(redisClient.ttl).apply(redisClient, args);
if (redisClient) return redisClient.ttl(...args);
throw new GeneralError('No redis connection. Check for this via getRedisClient().');
};

Expand Down
18 changes: 7 additions & 11 deletions test/utils/redis/redisMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ const valueDict = {};
const ttlDict = {};

const RedisClientMock = class {
get(key, ...args) {
get(key) {
const value = valueDict[key];
const callback = args[args.length - 1];
callback(null, value);
return Promise.resolve(value);
}

set(key, value, ...args) {
Expand All @@ -14,21 +13,18 @@ const RedisClientMock = class {
if (ex >= 0) {
ttlDict[key] = args[ex + 1];
}
const callback = args[args.length - 1];
callback(null, true);
return Promise.resolve(true);
}

del(key, ...args) {
del(key) {
delete valueDict[key];
delete ttlDict[key];
const callback = args[args.length - 1];
callback(null, true);
return Promise.resolve(true);
}

ttl(key, ...args) {
ttl(key) {
const ttl = ttlDict[key];
const callback = args[args.length - 1];
callback(null, ttl);
return Promise.resolve(ttl);
}
};

Expand Down

0 comments on commit 340b59b

Please sign in to comment.