Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions packages/cacheable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,23 +904,17 @@ export class Cacheable extends Hookified {
* @param {string} algorithm the hash algorithm to use. The default is 'sha256'
* @returns {string} the hash of the object
*/
// biome-ignore lint/suspicious/noExplicitAny: type format
public hash(object: any, algorithm = "sha256"): string {
// Convert string algorithm to HashAlgorithm enum value if needed
let hashAlgorithm: HashAlgorithm | undefined;
if (algorithm === "sha256" || algorithm === HashAlgorithm.SHA256) {
hashAlgorithm = HashAlgorithm.SHA256;
} else if (algorithm === "sha512" || algorithm === HashAlgorithm.SHA512) {
hashAlgorithm = HashAlgorithm.SHA512;
} else if (algorithm === "md5" || algorithm === HashAlgorithm.MD5) {
hashAlgorithm = HashAlgorithm.MD5;
} else if (algorithm === "djb2" || algorithm === HashAlgorithm.DJB2) {
hashAlgorithm = HashAlgorithm.DJB2;
} else {
// Default to SHA256 for unknown algorithms
hashAlgorithm = HashAlgorithm.SHA256;
}
return hash(object, hashAlgorithm);
public hash(
// biome-ignore lint/suspicious/noExplicitAny: type format
object: any,
algorithm: HashAlgorithm = HashAlgorithm.SHA256,
): string {
// Check if algorithm is a valid HashAlgorithm value, default to SHA256 if not
const validAlgorithm = Object.values(HashAlgorithm).includes(algorithm)
? algorithm
: HashAlgorithm.SHA256;

return hash(object, validAlgorithm);
}

private async getSecondaryRawResults<T>(
Expand Down Expand Up @@ -1012,6 +1006,10 @@ export {
} from "@cacheable/memory";
export {
type CacheableItem,
calculateTtlFromExpiration,
getCascadingTtl,
HashAlgorithm,
hash,
Stats as CacheableStats,
shorthandToMilliseconds,
shorthandToTime,
Expand Down
9 changes: 5 additions & 4 deletions packages/cacheable/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Cacheable,
CacheableEvents,
CacheableHooks,
HashAlgorithm,
KeyvCacheableMemory,
} from "../src/index.js";

Expand Down Expand Up @@ -926,23 +927,23 @@ describe("cacheable hash method", () => {
test("should hash with sha512 algorithm", () => {
const cacheable = new Cacheable();
const object = { foo: "bar" };
const result = cacheable.hash(object, "sha512");
const result = cacheable.hash(object, HashAlgorithm.SHA512);
expect(result).toBeDefined();
expect(result).toHaveLength(128); // SHA512 produces 128 hex characters
});

test("should hash with md5 algorithm", () => {
const cacheable = new Cacheable();
const object = { foo: "bar" };
const result = cacheable.hash(object, "md5");
const result = cacheable.hash(object, HashAlgorithm.MD5);
expect(result).toBeDefined();
expect(result).toHaveLength(32); // MD5 produces 32 hex characters
});

test("should hash with djb2 algorithm", () => {
const cacheable = new Cacheable();
const object = { foo: "bar" };
const result = cacheable.hash(object, "djb2");
const result = cacheable.hash(object, HashAlgorithm.DJB2);
expect(result).toBeDefined();
expect(typeof result).toBe("string");
});
Expand All @@ -955,7 +956,7 @@ describe("cacheable hash method", () => {
// biome-ignore lint/suspicious/noExplicitAny: testing unknown algorithm
"unknown-algorithm" as any,
);
const sha256Result = cacheable.hash(object, "sha256");
const sha256Result = cacheable.hash(object, HashAlgorithm.SHA256);
expect(unknownAlgorithmResult).toBe(sha256Result);
});

Expand Down