From 38e407db7f09a545c576ca168931a9ac3a2027e9 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Fri, 24 May 2024 18:47:15 -0700 Subject: [PATCH] Moved cacheOptions to context, added skipCache to fetchOptions --- src/lib/types.ts | 3 ++- src/skimming.ts | 14 +++++++++----- test/mod_test.ts | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/lib/types.ts b/src/lib/types.ts index 4095ea7..9b5c0fc 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -5,7 +5,7 @@ export type Context = { url: string; files: string[]; - options?: CacheOptions; + cacheOptions?: CacheOptions; }; /** @@ -57,4 +57,5 @@ export type FetchOptions = { trimContent?: boolean; previewLength?: number; regex?: boolean; + skipCache?: boolean; }; diff --git a/src/skimming.ts b/src/skimming.ts index c3a8035..c8bad1a 100644 --- a/src/skimming.ts +++ b/src/skimming.ts @@ -38,8 +38,8 @@ export class Skimming { validateContext(context); this.context = context; - if (context.options) { - this.cacheHandler = new CacheHandler(context.options); + if (context.cacheOptions) { + this.cacheHandler = new CacheHandler(context.cacheOptions); this.useCache = true; } } @@ -55,10 +55,10 @@ export class Skimming { validateQuery(query); validateContext(this.context); - const { ignoreCase, previewLength, trimContent } = options; + const { ignoreCase, previewLength, trimContent, skipCache } = options; let results: Output[] = []; - if (this.useCache) { + if (this.canUseCache(skipCache)) { results = this.cacheHandler.fetch(query, options); } @@ -86,7 +86,7 @@ export class Skimming { }; results.push(output); - if (this.useCache) { + if (this.canUseCache(skipCache)) { this.cacheHandler.store( query, output, @@ -173,4 +173,8 @@ export class Skimming { result.body?.cancel(); throw new NotContentFound(url, doc); } + + private canUseCache(skipCache: boolean = false): boolean { + return this.useCache && !skipCache; + } } diff --git a/test/mod_test.ts b/test/mod_test.ts index 8846459..1875bf4 100644 --- a/test/mod_test.ts +++ b/test/mod_test.ts @@ -186,7 +186,7 @@ test({ const context: Context = { url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/', files, - options: { size: 10, expireDuration: 10 }, + cacheOptions: { size: 10, expireDuration: 10 }, }; const skimmer = Skimming.create(context); @@ -206,6 +206,35 @@ test({ }, }); +test({ + name: 'MOD - Should NOT return value from the cache - when skip cache is enabled', + async fn(): Promise { + // given + const query = 'Skimming'; + const files = ['README.md']; + const context: Context = { + url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/', + files, + cacheOptions: { size: 10, expireDuration: 10 }, + }; + + const skimmer = Skimming.create(context); + + // test + let output = await skimmer.skim(query); + assertEquals(output.length, 1); + output.forEach((data) => { + assertEquals(data.cache, false); + }); + + output = await skimmer.skim(query, { skipCache: true }); + assertEquals(output.length, 1); + output.forEach((data) => { + assertEquals(data.cache, false); + }); + }, +}); + test({ name: 'MOD - Should return value from the cache with new preview length', async fn(): Promise { @@ -215,7 +244,7 @@ test({ const context: Context = { url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/', files, - options: { size: 10, expireDuration: 10 }, + cacheOptions: { size: 10, expireDuration: 10 }, }; const skimmer = Skimming.create(context); @@ -249,7 +278,7 @@ test({ const context: Context = { url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/', files, - options: { size: 10, expireDuration: 10 }, + cacheOptions: { size: 10, expireDuration: 10 }, }; const skimmer = Skimming.create(context); @@ -283,7 +312,7 @@ test({ const context: Context = { url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/', files, - options: { size: 10, expireDuration: 10 }, + cacheOptions: { size: 10, expireDuration: 10 }, }; const skimmer = Skimming.create(context); @@ -323,7 +352,7 @@ test({ const context: Context = { url: 'https://raw.githubusercontent.com/petruki/skimming/master/test/fixtures/', files, - options: { size: 10, expireDuration: 10 }, + cacheOptions: { size: 10, expireDuration: 10 }, }; const skimmer = Skimming.create(context);