Skip to content

Commit

Permalink
v1.1.1 Quality of code
Browse files Browse the repository at this point in the history
  • Loading branch information
shuritch committed May 13, 2023
1 parent 49fc083 commit 84c2150
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 65 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const obj: {
};

export const oop: {
mixin: (target: object, source: object) => unknown;
mixin: (target: object, source: object) => object;
defineSetter: (target: object) => (name: string, callback: (value: unknown) => void) => void;
defineGetter: (target: object) => (name: string, callback: () => unknown) => void;
setDefault: <T>(target: T, defaultValue: any) => T;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "leadutils",
"description": "Leadfisher utilities",
"author": "Alexander Ivanov <sashapop101@gmail.com>",
"version": "1.1.0",
"version": "1.1.1",
"homepage": "https://leadfisher.ru",
"packageManager": "npm@9.6.4",
"type": "commonjs",
Expand Down
36 changes: 9 additions & 27 deletions src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const crypto = require('node:crypto');

const SCRYPT_PARAMS = { N: 32768, r: 8, p: 1, maxmem: 64 * 1024 * 1024 };
const SCRYPT_PREFIX = '$scrypt$N=32768,r=8,p=1,maxmem=67108864$';
const SALT_LEN = 32;
const KEY_LEN = 64;

const serializeHash = (hash, salt) => {
const saltString = salt.toString('base64').split('=')[0];
Expand All @@ -12,13 +14,9 @@ const serializeHash = (hash, salt) => {
};

const parseOptions = options => {
const values = [];
let k, v;
const items = options.split(',');
for (const item of items) {
const [key, val] = item.split('=');
values.push([key, Number(val)]);
}
return Object.fromEntries(values);
return Object.fromEntries(items.reduce((a, item) => (([k, v] = item.split('=')), a.push([k, Number(v)]), a), []));
};

const deserializeHash = phcString => {
Expand All @@ -30,36 +28,20 @@ const deserializeHash = phcString => {
return { params, salt, hash };
};

const SALT_LEN = 32;
const KEY_LEN = 64;

const hashPassword = password =>
const hashPassword = pass =>
new Promise((resolve, reject) => {
crypto.randomBytes(SALT_LEN, (err, salt) => {
if (err) {
reject(err);
return;
}
crypto.scrypt(password, salt, KEY_LEN, SCRYPT_PARAMS, (err, hash) => {
if (err) {
reject(err);
return;
}
resolve(serializeHash(hash, salt));
if (err) return reject(err);
return void crypto.scrypt(pass, salt, KEY_LEN, SCRYPT_PARAMS, (err, hash) => {
err ? reject(err) : resolve(serializeHash(hash, salt));
});
});
});

const validatePassword = (password, serHash) => {
const { params, salt, hash } = deserializeHash(serHash);
return new Promise((resolve, reject) => {
const callback = (err, hashedPassword) => {
if (err) {
reject(err);
return;
}
resolve(crypto.timingSafeEqual(hashedPassword, hash));
};
const callback = (err, hashed) => void (err ? reject(err) : resolve(crypto.timingSafeEqual(hashed, hash)));
crypto.scrypt(password, salt, hash.length, params, callback);
});
};
Expand Down
15 changes: 5 additions & 10 deletions src/net.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
'use strict';

const intIP = (ip = '127.0.0.1') => ip.split('.').reduce((acc, byte) => (acc = (acc << 8) + parseInt(byte, 10)), 0);
const parseCookie = str =>
str
.split(';')
.map(v => v.split('='))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
const urlParse = v => decodeURIComponent(v.trim());
// prettier-ignore
const parseCookie = s =>
s.split(';').map(v => v.split('=')).reduce((a, v) => ((a[urlParse(v[0])] = urlParse(v[1])), a), {});

const removePort = host => {
if (!host) return '';
const portOffset = host.indexOf(':');
if (portOffset > -1) host = host.substr(0, portOffset);
return host;
return portOffset > -1 ? host.substr(0, portOffset) : host;
};

const frame = body =>
Expand Down
16 changes: 6 additions & 10 deletions src/paradigms/fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

// prettier-ignore
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
const curry = (fn, arity = fn.length, ...args) =>
arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
// prettier-ignore
const curry = (fn, arity = fn.length, ...args) => arity <= args.length ? fn(...args) : curry.bind(null, fn, arity, ...args);
// prettier-ignore
const pipeAsync = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
// prettier-ignore
const chain = ctx => func => (...args) => (func(...args), ctx);

const debounce = (fn, ms = 0) => {
let timeoutId;
Expand Down Expand Up @@ -41,12 +45,6 @@ const once = fn => {
};
};

const chain = ctx => func =>
function (...args) {
func(...args);
return ctx;
};

const memoize = fn => {
const cache = new Map();
const cached = function (...args) {
Expand All @@ -60,8 +58,6 @@ const memoize = fn => {
cached.cache = cache;
return cached;
};
// prettier-ignore
const pipeAsync = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));

const times = (n, fn, context = undefined) => {
let i = 0;
Expand Down
20 changes: 4 additions & 16 deletions src/paradigms/oop.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ const setDefault = (target, defaultValue) =>
});

const defineGetter = target => (name, callback) =>
Object.defineProperty(target, name, {
get: callback,
enumerable: true,
configurable: false,
});
Object.defineProperty(target, name, { get: callback, enumerable: true, configurable: false });

const defineSetter = target => (name, callback) =>
Object.defineProperty(target, name, {
set: callback,
enumerable: true,
configurable: false,
});
Object.defineProperty(target, name, { set: callback, enumerable: true, configurable: false });

const mixin = (target, source) => {
const methods = Object.getOwnPropertyNames(source);
const mix = {};
for (const method of methods) {
if (target[method]) continue;
mix[method] = source[method];
}
Object.assign(target, mix);
const mix = methods.reduce((acc, method) => (target[method] ? acc : ((acc[method] = source[method]), acc)), {});
return Object.assign(target, mix);
};

module.exports = { defineSetter, defineGetter, setDefault, mixin };

0 comments on commit 84c2150

Please sign in to comment.