From dabd053ac563707c2598b125dadd0dbb310dcd34 Mon Sep 17 00:00:00 2001 From: dygaevskiy Date: Thu, 23 Jan 2020 19:57:46 +0300 Subject: [PATCH] fix: protect merged static and dynamic tags from being duplicated --- src/storages/base.spec.ts | 4 ++-- src/storages/base.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/storages/base.spec.ts b/src/storages/base.spec.ts index 2c542f6..986e278 100644 --- a/src/storages/base.spec.ts +++ b/src/storages/base.spec.ts @@ -100,8 +100,8 @@ describe('BaseStorage', () => { expect(value.expiresIn).toEqual(expect.any(Number)); }); - it('set sets key to storage adapter with concatenated dynamic tags and simple tags', async () => { - await storage.set('test', '123', { tags: ['tag1'], getTags: (result) => [result]}); + it('set sets key to storage adapter with uniq array of concatenated dynamic tags and simple tags', async () => { + await storage.set('test', '123', { tags: ['tag1', '123'], getTags: (result) => [result]}); const value = JSON.parse(testInterface.internalStorage['cache-test']); diff --git a/src/storages/base.ts b/src/storages/base.ts index 91ae756..af5221f 100644 --- a/src/storages/base.ts +++ b/src/storages/base.ts @@ -1,5 +1,6 @@ import { createHash } from 'crypto'; import isFunction from 'lodash/isFunction'; +import uniq from 'lodash/uniq'; import { WriteOptions, Storage, StorageRecord, StorageRecordTag, StorageRecordValue } from '../storage'; import { StorageAdapter } from '../storage-adapter'; import serialize from '../serialize'; @@ -158,7 +159,7 @@ export class BaseStorage implements Storage { throw new TypeError(`getTags should return an array of strings, got ${typeof dynamicTags}`); } - const record = createRecord(key, value, tags.concat(dynamicTags).map(createTag), options); + const record = createRecord(key, value, uniq(tags.concat(dynamicTags)).map(createTag), options); await this.adapter.set( this.createKey(key),