Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-8451 Remove unnecessary usage of promisify #5355

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading