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
29 changes: 15 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@croct-tech/time": "^0.8.0",
"@croct/json": "^2.0.0",
"@croct/logging": "^0.2.2",
"object-hash": "^3.0.0"
"node-object-hash": "^3.0.0"
},
"devDependencies": {
"@croct/eslint-plugin": "^0.6.0",
Expand Down
34 changes: 29 additions & 5 deletions src/adapted.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import {JsonCompatible, JsonValue} from '@croct/json';
import * as hash from 'object-hash';
import {hasher, HasherOptions} from 'node-object-hash';
import {CacheLoader, CacheProvider} from './cacheProvider';

export type Transformer<D, S> = (value: D) => S;

export type HashAlgorithm = 'passthrough' | 'md5' | 'sha1';

const HASHER = hasher({
// Support classes and instances like `ResourceId`
coerce: true,

// Do not differentiate Objects, Sets and Maps constructed in different order
// `new Set([1,2,3])` should match `new Set([3,2,1])`
sort: {
array: false,
typedArray: false,
object: true,
set: true,
map: true,
bigint: true,
},

// Do not trim values, "foo " and "foo" should not match as the same key
trim: false,
});

type Configuration<K, V, IK, IV> = {
cache: CacheProvider<IK, IV>,
keyTransformer: Transformer<K, IK>,
Expand Down Expand Up @@ -82,12 +101,17 @@ export class AdaptedCache<K, V, IK = K, IV = V> implements CacheProvider<K, V> {
}

public static createHashSerializer(algorithm?: HashAlgorithm): Transformer<any, string> {
const options: hash.Options = {
encoding: 'base64',
algorithm: algorithm,
if (algorithm === 'passthrough') {
// Do not hash when algorithm is set to `passthrough`
return HASHER.sort.bind(HASHER);
}

const options: HasherOptions = {
enc: 'base64',
alg: algorithm,
};

return (value: any): string => hash(value, options);
return (value: any): string => HASHER.hash(value, options);
}

/**
Expand Down
29 changes: 28 additions & 1 deletion test/adapted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,33 @@ describe('A cache adapter that can transform keys and values', () => {
expect(mockCache.set).toHaveBeenCalledWith('key', 'transformed');
});

it('should transform a value into a hash-able string', () => {
const transformer = AdaptedCache.createHashSerializer('passthrough');

const value = {
some: {
deeply: {
nested: [
'value',
],
},
},
with: 1,
multiple: true,
keys: null,
andTypes: [
'string',
1,
true,
null,
],
};

const result = transformer(value);

expect(result).toBe('{andTypes:[string,1,1,],keys:,multiple:1,some:{deeply:{nested:[value]}},with:1}');
});

it('should transform a value into a hash', () => {
const transformer = AdaptedCache.createHashSerializer('md5');

Expand All @@ -138,7 +165,7 @@ describe('A cache adapter that can transform keys and values', () => {

const result = transformer(value);

expect(result).toBe('DB8pVafUNdepTqZc8eE5qw==');
expect(result).toBe('oDA+C/1fqcOT90c6vwhaWg==');
});

it('should transform a value into its JSON representation', () => {
Expand Down