From 5dd6f5a67a0be19f2ff37a6da549f0ba316358f3 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 7 Aug 2024 17:50:28 +0200 Subject: [PATCH 1/3] chore(commons): extract lru-cache into commons --- packages/commons/package.json | 10 +++++++++- .../src/persistence => commons/src}/LRUCache.ts | 2 +- .../{idempotency => commons}/src/types/LRUCache.ts | 0 .../tests/unit}/LRUCache.test.ts | 4 ++-- .../src/persistence/BasePersistenceLayer.ts | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) rename packages/{idempotency/src/persistence => commons/src}/LRUCache.ts (99%) rename packages/{idempotency => commons}/src/types/LRUCache.ts (100%) rename packages/{idempotency/tests/unit/persistence => commons/tests/unit}/LRUCache.test.ts (96%) diff --git a/packages/commons/package.json b/packages/commons/package.json index 3713b9853b..ea0a09ac38 100644 --- a/packages/commons/package.json +++ b/packages/commons/package.json @@ -45,6 +45,10 @@ "import": "./lib/esm/fromBase64.js", "require": "./lib/cjs/fromBase64.js" }, + "./utils/lru-cache": { + "import": "./lib/esm/LRUCache.js", + "require": "./lib/cjs/LRUCache.js" + }, "./types": { "import": "./lib/esm/types/index.js", "require": "./lib/cjs/types/index.js" @@ -60,6 +64,10 @@ "lib/cjs/fromBase64.d.ts", "lib/esm/fromBase64.d.ts" ], + "utils/lru-cache": [ + "lib/cjs/LRUCache.d.ts", + "lib/esm/LRUCache.d.ts" + ], "types": [ "lib/cjs/types/index.d.ts", "lib/esm/types/index.d.ts" @@ -88,4 +96,4 @@ "devDependencies": { "@aws-lambda-powertools/testing-utils": "file:../testing" } -} +} \ No newline at end of file diff --git a/packages/idempotency/src/persistence/LRUCache.ts b/packages/commons/src/LRUCache.ts similarity index 99% rename from packages/idempotency/src/persistence/LRUCache.ts rename to packages/commons/src/LRUCache.ts index c41f07e1a6..ed69798c68 100644 --- a/packages/idempotency/src/persistence/LRUCache.ts +++ b/packages/commons/src/LRUCache.ts @@ -1,4 +1,4 @@ -import type { LRUCacheOptions } from '../types/LRUCache.js'; +import type { LRUCacheOptions } from './types/LRUCache.js'; const DEFAULT_MAX_SIZE = 100; const NEWER = Symbol('newer'); diff --git a/packages/idempotency/src/types/LRUCache.ts b/packages/commons/src/types/LRUCache.ts similarity index 100% rename from packages/idempotency/src/types/LRUCache.ts rename to packages/commons/src/types/LRUCache.ts diff --git a/packages/idempotency/tests/unit/persistence/LRUCache.test.ts b/packages/commons/tests/unit/LRUCache.test.ts similarity index 96% rename from packages/idempotency/tests/unit/persistence/LRUCache.test.ts rename to packages/commons/tests/unit/LRUCache.test.ts index 5732c31a45..cec778ac4c 100644 --- a/packages/idempotency/tests/unit/persistence/LRUCache.test.ts +++ b/packages/commons/tests/unit/LRUCache.test.ts @@ -1,9 +1,9 @@ /** * Test LRUCache class * - * @group unit/idempotency/persistence/lru-cache + * @group unit/commons/lru-cache */ -import { LRUCache } from '../../../src/persistence/LRUCache.js'; +import { LRUCache } from '../../src/LRUCache.js'; describe('Class: LRUMap', () => { describe('Method: add', () => { diff --git a/packages/idempotency/src/persistence/BasePersistenceLayer.ts b/packages/idempotency/src/persistence/BasePersistenceLayer.ts index 83c730c72f..cd883c4218 100644 --- a/packages/idempotency/src/persistence/BasePersistenceLayer.ts +++ b/packages/idempotency/src/persistence/BasePersistenceLayer.ts @@ -1,5 +1,6 @@ import { type Hash, createHash } from 'node:crypto'; import type { JSONValue } from '@aws-lambda-powertools/commons/types'; +import { LRUCache } from '@aws-lambda-powertools/commons/utils/lru-cache'; import { search } from '@aws-lambda-powertools/jmespath'; import type { JMESPathParsingOptions } from '@aws-lambda-powertools/jmespath/types'; import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js'; @@ -15,7 +16,6 @@ import type { BasePersistenceLayerOptions, } from '../types/BasePersistenceLayer.js'; import { IdempotencyRecord } from './IdempotencyRecord.js'; -import { LRUCache } from './LRUCache.js'; /** * Base class for all persistence layers. This class provides the basic functionality for From 0331a1ad5b42af2d1c99eafb9230b6d801fc2c93 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 7 Aug 2024 18:00:17 +0200 Subject: [PATCH 2/3] chore(commons): add utils to API docs --- packages/commons/typedoc.json | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/commons/typedoc.json b/packages/commons/typedoc.json index 60dfaa41cd..fb15e9b68e 100644 --- a/packages/commons/typedoc.json +++ b/packages/commons/typedoc.json @@ -1,5 +1,13 @@ { - "extends": ["../../typedoc.base.json"], - "entryPoints": ["./src/index.ts", "./src/types/index.ts"], + "extends": [ + "../../typedoc.base.json" + ], + "entryPoints": [ + "./src/index.ts", + "./src/types/index.ts", + "./src/typeUtils.ts", + "./src/fromBase64.ts", + "./src/LRUCache.ts" + ], "readme": "./README.md" -} +} \ No newline at end of file From fe34182904cb48027e37966a92695a1d8d876fae Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 7 Aug 2024 18:23:13 +0200 Subject: [PATCH 3/3] chore(commons): remove todo --- packages/commons/src/LRUCache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/commons/src/LRUCache.ts b/packages/commons/src/LRUCache.ts index ed69798c68..76d2c6efc2 100644 --- a/packages/commons/src/LRUCache.ts +++ b/packages/commons/src/LRUCache.ts @@ -213,7 +213,7 @@ class LRUCache { */ private trackItemUse(item: Item): void { // If the item is already the newest, we don't need to do anything - if (this.mostRecentlyUsed === item) return; // TODO: check this + if (this.mostRecentlyUsed === item) return; // If the item is not the newest, we need to mark it as the newest if (item[NEWER]) {