From f566b627abb62f1a4780098b716b9f888aff5896 Mon Sep 17 00:00:00 2001 From: ndragun92 Date: Mon, 11 Mar 2024 21:45:40 +0100 Subject: [PATCH] feat: Add generateKey function and update package version A new utility function `generateKey` has been added which enables to generate keys based on the provided hash and encoding options. The `generateKey` function is also exported in the main index file. Additionally, a minor change has been made in the caching logic with an added log method and the package version has been revised to 1.1.1. --- package.json | 2 +- src/index.ts | 3 ++- src/lib/file-sys-cache.ts | 1 + src/utils/index.util.ts | 6 ++++++ 4 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 src/utils/index.util.ts diff --git a/package.json b/package.json index 9009376..e5b7e11 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "file-sys-cache", - "version": "1.1.0", + "version": "1.1.1", "description": "A Node.js package providing efficient caching using the file system for storage.", "type": "module", "main": "./dist/file-sys-cache.cjs", diff --git a/src/index.ts b/src/index.ts index eddd721..fa1a277 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ // eslint-disable-next-line import/no-named-default import { default as FileSysCache } from './lib/file-sys-cache' +import { generateKey } from './utils/index.util.ts' import type { THashOptions } from './types/index.type' -export { type THashOptions, FileSysCache } +export { type THashOptions, FileSysCache, generateKey } diff --git a/src/lib/file-sys-cache.ts b/src/lib/file-sys-cache.ts index fa7b935..1ad1f17 100644 --- a/src/lib/file-sys-cache.ts +++ b/src/lib/file-sys-cache.ts @@ -159,6 +159,7 @@ export default class FileSysCache { } if (this.enableMonitoring) { monitoring.count.success.invalidate++ + this.log() } } catch (_) { if (this.enableMonitoring) { diff --git a/src/utils/index.util.ts b/src/utils/index.util.ts new file mode 100644 index 0000000..0bee0bc --- /dev/null +++ b/src/utils/index.util.ts @@ -0,0 +1,6 @@ +import { createHash } from 'node:crypto' +import { type TBinaryToTextEncoding, type THashOptions } from '../types/index.type.ts' + +export const generateKey = (value: string, hash: THashOptions = 'sha256', encoding: TBinaryToTextEncoding = 'hex'): string => { + return createHash(hash).update(value).digest(encoding) +}