chore(root): migrate to biome
This commit is contained in:
@@ -1,61 +0,0 @@
|
||||
import {
|
||||
createHash as cryptoCreateHash,
|
||||
randomBytes,
|
||||
scrypt,
|
||||
timingSafeEqual,
|
||||
} from 'crypto';
|
||||
|
||||
export function generateSalt() {
|
||||
return randomBytes(16).toString('hex');
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a password or a secret with a password hashing algorithm (scrypt)
|
||||
* @param {string} password
|
||||
* @returns {string} The salt+hash
|
||||
*/
|
||||
export async function hashPassword(
|
||||
password: string,
|
||||
keyLength = 32
|
||||
): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// generate random 16 bytes long salt - recommended by NodeJS Docs
|
||||
const salt = generateSalt();
|
||||
scrypt(password, salt, keyLength, (err, derivedKey) => {
|
||||
if (err) reject(err);
|
||||
// derivedKey is of type Buffer
|
||||
resolve(`${salt}.${derivedKey.toString('hex')}`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare a plain text password with a salt+hash password
|
||||
* @param {string} password The plain text password
|
||||
* @param {string} hash The hash+salt to check against
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export async function verifyPassword(
|
||||
password: string,
|
||||
hash: string,
|
||||
keyLength = 32
|
||||
): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const [salt, hashKey] = hash.split('.');
|
||||
// we need to pass buffer values to timingSafeEqual
|
||||
const hashKeyBuff = Buffer.from(hashKey!, 'hex');
|
||||
scrypt(password, salt!, keyLength, (err, derivedKey) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
// compare the new supplied password with the hashed password using timeSafeEqual
|
||||
resolve(timingSafeEqual(hashKeyBuff, derivedKey));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function createHash(data: string, len: number) {
|
||||
return cryptoCreateHash('shake256', { outputLength: len })
|
||||
.update(data)
|
||||
.digest('hex');
|
||||
}
|
||||
@@ -84,7 +84,7 @@ export function completeSerie(
|
||||
data: ISerieDataItem[],
|
||||
_startDate: string,
|
||||
_endDate: string,
|
||||
interval: IInterval
|
||||
interval: IInterval,
|
||||
) {
|
||||
const startDate = parseISO(_startDate);
|
||||
const endDate = parseISO(_endDate);
|
||||
|
||||
@@ -6,7 +6,7 @@ import { round } from './math';
|
||||
|
||||
export function getPreviousMetric(
|
||||
current: number,
|
||||
previous: number | null | undefined
|
||||
previous: number | null | undefined,
|
||||
): PreviousValue {
|
||||
if (isNil(previous)) {
|
||||
return undefined;
|
||||
@@ -20,7 +20,7 @@ export function getPreviousMetric(
|
||||
: 0) -
|
||||
1) *
|
||||
100,
|
||||
1
|
||||
1,
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { isNumber } from 'mathjs';
|
||||
|
||||
export const round = (num: number, decimals = 2) => {
|
||||
const factor = Math.pow(10, decimals);
|
||||
const factor = 10 ** decimals;
|
||||
return Math.round((num + Number.EPSILON) * factor) / factor;
|
||||
};
|
||||
|
||||
export const average = (arr: (number | null)[]) => {
|
||||
const filtered = arr.filter(
|
||||
(n): n is number =>
|
||||
isNumber(n) && !Number.isNaN(n) && Number.isFinite(n) && n !== 0
|
||||
isNumber(n) && !Number.isNaN(n) && Number.isFinite(n) && n !== 0,
|
||||
);
|
||||
const avg = filtered.reduce((p, c) => p + c, 0) / filtered.length;
|
||||
return Number.isNaN(avg) ? 0 : avg;
|
||||
|
||||
@@ -3,7 +3,7 @@ import superjson from 'superjson';
|
||||
|
||||
export function toDots(
|
||||
obj: Record<string, unknown>,
|
||||
path = ''
|
||||
path = '',
|
||||
): Record<string, string> {
|
||||
return Object.entries(obj).reduce((acc, [key, value]) => {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
@@ -26,7 +26,7 @@ export function toDots(
|
||||
}
|
||||
|
||||
export function toObject(
|
||||
obj: Record<string, string | undefined>
|
||||
obj: Record<string, string | undefined>,
|
||||
): Record<string, unknown> {
|
||||
let result: Record<string, unknown> = {};
|
||||
Object.entries(obj).forEach(([key, value]) => {
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
import { createHash } from './crypto';
|
||||
|
||||
interface GenerateDeviceIdOptions {
|
||||
salt: string;
|
||||
ua: string;
|
||||
ip: string;
|
||||
origin: string;
|
||||
}
|
||||
|
||||
export function generateDeviceId({
|
||||
salt,
|
||||
ua,
|
||||
ip,
|
||||
origin,
|
||||
}: GenerateDeviceIdOptions) {
|
||||
return createHash(`${ua}:${ip}:${origin}:${salt}`, 16);
|
||||
}
|
||||
@@ -9,7 +9,7 @@ const slugify = (str: string) => {
|
||||
.replace('Å', 'A')
|
||||
.replace('Ä', 'A')
|
||||
.replace('Ö', 'O'),
|
||||
{ lower: true, strict: true, trim: true }
|
||||
{ lower: true, strict: true, trim: true },
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export function parseSearchParams(
|
||||
params: URLSearchParams
|
||||
params: URLSearchParams,
|
||||
): Record<string, string> | undefined {
|
||||
const result: Record<string, string> = {};
|
||||
for (const [key, value] of params.entries()) {
|
||||
@@ -39,7 +39,7 @@ export function parsePath(path?: string): {
|
||||
|
||||
export function isSameDomain(
|
||||
url1: string | undefined,
|
||||
url2: string | undefined
|
||||
url2: string | undefined,
|
||||
) {
|
||||
if (!url1 || !url2) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user