This package contains common Node.js utilities I use in my projects.
npm install @ionaru/node-utils
Returns the value of the environment variable key or defaultValue if the environment variable is not set.
If defaultValue is undefined, the function will throw an error if the environment variable is not set.
import { getEnvironmentVariable } from '@ionaru/node-utils';
// Returns the contents of process.env.MY_ENV_VAR
const envVar = getEnvironmentVariable('MY_ENV_VAR');
// Errors with "Environment variable 'MISSING_ENV_VAR' is not set!".
const envVarError = getEnvironmentVariable('MISSING_ENV_VAR');
// Returns 'default_value'.
const envVarWithDefault = getEnvironmentVariable('MISSING_ENV_VAR', 'default_value');Hashes a password using the internal Node.js scrypt and randomUUID functions.
randomUUID is used to generate a salt for the password, that is then hashed using scrypt and outputted as base64 string.
import { hashPassword } from '@ionaru/node-utils';
const password = 'my_password';
const hashedPassword = await hashPassword(password);
// hashedPassword = {
// hash: "BASE64_STRING_HASH",
// salt: "UUID_SALT",
// };Checks if a password matches a hash and salt.
The IPasswordData interface matches the output of hashPassword.
import { checkPassword } from '@ionaru/node-utils';
const password = 'my_password';
const hashedPassword = await hashPassword(password);
const passwordMatches = await checkPassword(password, hashedPassword);
// passwordMatches = true;