Skip to content

Commit

Permalink
Merge pull request #146 from AthennaIO/develop
Browse files Browse the repository at this point in the history
chore(npm): update dependencies
  • Loading branch information
jlenon7 authored Jan 27, 2025
2 parents c71ec9e + a9de594 commit a2c3309
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "5.8.0",
"version": "5.9.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
18 changes: 4 additions & 14 deletions src/helpers/String.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ import * as changeCase from 'change-case'

import { crc32 } from 'crc'
import { debug } from '#src/debug'
import { Path } from '#src/helpers/Path'
import { Module } from '#src/helpers/Module'
import { Options } from '#src/helpers/Options'
import { Macroable } from '#src/helpers/Macroable'
import { createHmac, randomBytes } from 'node:crypto'
import { OrdinalNanException } from '#src/exceptions/OrdinalNanException'
import { NotFoundAthennaConfig } from '#src/exceptions/NotFoundAthennaConfig'

export class String extends Macroable {
public static config: any

/**
* Generate hash for a given value.
*
Expand All @@ -36,21 +32,15 @@ export class String extends Macroable {
value: string,
options: { key?: string; prefix?: string } = {}
) {
if (!this.config) {
const require = Module.createRequire(Path.pwd())

try {
this.config = require('@athenna/config')
} catch (_err) {
debug('@athenna/config not found to run String.hash()')
}
if (!global.Config) {
debug('@athenna/config not found running String.hash()')
}

if (!options.key && !this.config) {
if (!options.key && !global.Config) {
throw new NotFoundAthennaConfig()
}

options.key = options.key || this.config.Config.get('app.key')
options.key = options.key || global.Config.get('app.key')

const hash = createHmac('sha256', options.key).update(value).digest('hex')

Expand Down
22 changes: 21 additions & 1 deletion tests/unit/helpers/StringTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
*/

import { String } from '#src'
import { Test, type Context } from '@athenna/test'
import { AfterEach, Mock, Test, type Context } from '@athenna/test'
import { OrdinalNanException } from '#src/exceptions/OrdinalNanException'
import { NotFoundAthennaConfig } from '#src/exceptions/NotFoundAthennaConfig'

export default class StringTest {
@AfterEach()
public async afterEach() {
Mock.restoreAll()
}

@Test()
public async shouldBeAbleToHashStringValues({ assert }: Context) {
const value = 'lenon'
Expand All @@ -35,6 +40,21 @@ export default class StringTest {
)
}

@Test()
public async shouldBeAbleToHashStringValuesUsingConfigAppKey({ assert }: Context) {
const value = 'lenon'

global.Config = null

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Mock.when(global, 'Config').value({
get: () => 'secret1'
})

assert.deepEqual(String.hash(value), 'f48760e603c17c6abf2eff3dad2495b291ccbac943836518d0db3f41c3853f8b')
}

@Test()
public async shouldThrownNotFoundAthennaConfigExceptionIfNotProvidingASecretKeyForHash({ assert }: Context) {
const value = 'lenon'
Expand Down

0 comments on commit a2c3309

Please sign in to comment.