From 3dcf1c3753acfeddf0345398bf63788594a5e6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 16:59:28 -0300 Subject: [PATCH 01/12] feat: add async set cache --- src/HttpClient/HttpClient.ts | 5 +-- src/HttpClient/middlewares/cache.ts | 50 ++++++++++++++++------------- src/HttpClient/typings.ts | 1 + 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/HttpClient/HttpClient.ts b/src/HttpClient/HttpClient.ts index 5b3e4ab2..fd439982 100644 --- a/src/HttpClient/HttpClient.ts +++ b/src/HttpClient/HttpClient.ts @@ -51,6 +51,7 @@ export class HttpClient { memoryCache, diskCache, memoizable = true, + asyncSetCache, locale, name, metrics, @@ -114,8 +115,8 @@ export class HttpClient { cancellationToken(cancellation), singleFlightMiddleware, acceptNotFoundMiddleware, - ...memoryCache ? [cacheMiddleware({ type: CacheType.Memory, storage: memoryCache })] : [], - ...diskCache ? [cacheMiddleware({ type: CacheType.Disk, storage: diskCache })] : [], + ...memoryCache ? [cacheMiddleware({ type: CacheType.Memory, storage: memoryCache, asyncSet: asyncSetCache })] : [], + ...diskCache ? [cacheMiddleware({ type: CacheType.Disk, storage: diskCache, asyncSet: asyncSetCache })] : [], notFoundFallbackMiddleware, routerCacheMiddleware, requestMiddleware(limit), diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index b76c0407..835c7b5a 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -82,9 +82,10 @@ const CacheTypeNames = { interface CacheOptions { type: CacheType storage: CacheLayer + asyncSet?: Boolean } -export const cacheMiddleware = ({ type, storage }: CacheOptions) => { +export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { const CACHE_RESULT_TAG = type === CacheType.Disk ? CustomHttpTags.HTTP_DISK_CACHE_RESULT : CustomHttpTags.HTTP_MEMORY_CACHE_RESULT const cacheType = CacheTypeNames[type] @@ -223,29 +224,34 @@ export const cacheMiddleware = ({ type, storage }: CacheOptions) => { const cacheWriteSpan = createCacheSpan(cacheType, 'write', tracer, span) try { - await storage.set(setKey, { - etag, - expiration, - response: {data: cacheableData, headers, status}, - responseEncoding, - responseType, - }) - - span?.log({ - event: HttpLogEvents.LOCAL_CACHE_SAVED, - [HttpCacheLogFields.CACHE_TYPE]: cacheType, - [HttpCacheLogFields.KEY_SET]: setKey, - [HttpCacheLogFields.AGE]: currentAge, - [HttpCacheLogFields.ETAG]: etag, - [HttpCacheLogFields.EXPIRATION_TIME]: (expiration - Date.now())/1000, - [HttpCacheLogFields.RESPONSE_ENCONDING]: responseEncoding, - [HttpCacheLogFields.RESPONSE_TYPE]: responseType, - }) + const storageSet = () => + storage.set(setKey, { + etag, + expiration, + response: {data: cacheableData, headers, status}, + responseEncoding, + responseType, + }) + if (asyncSet) { + storageSet() + } else { + await storageSet() + span?.log({ + event: HttpLogEvents.LOCAL_CACHE_SAVED, + [HttpCacheLogFields.CACHE_TYPE]: cacheType, + [HttpCacheLogFields.KEY_SET]: setKey, + [HttpCacheLogFields.AGE]: currentAge, + [HttpCacheLogFields.ETAG]: etag, + [HttpCacheLogFields.EXPIRATION_TIME]: (expiration - Date.now())/1000, + [HttpCacheLogFields.RESPONSE_ENCONDING]: responseEncoding, + [HttpCacheLogFields.RESPONSE_TYPE]: responseType, + }) + } } catch (error) { - ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) - logger?.warn({ message: 'Error writing to the HttpClient cache', error }) + ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) + logger?.warn({ message: 'Error writing to the HttpClient cache', error }) } finally { - cacheWriteSpan?.finish() + cacheWriteSpan?.finish() } return diff --git a/src/HttpClient/typings.ts b/src/HttpClient/typings.ts index 6d19620c..54dfb73c 100644 --- a/src/HttpClient/typings.ts +++ b/src/HttpClient/typings.ts @@ -83,6 +83,7 @@ export interface InstanceOptions { timeout?: number memoryCache?: CacheLayer diskCache?: CacheLayer + asyncSetCache?: Boolean /** * Enables memoization, ephemeral within each request, for all requests of this client. From 7f6c4545a292aa8384ec881692b9745f625eb776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 17:14:24 -0300 Subject: [PATCH 02/12] fix lint --- src/HttpClient/middlewares/cache.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index 835c7b5a..30a34b78 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -248,10 +248,10 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { }) } } catch (error) { - ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) - logger?.warn({ message: 'Error writing to the HttpClient cache', error }) + ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) + logger?.warn({ message: 'Error writing to the HttpClient cache', error }) } finally { - cacheWriteSpan?.finish() + cacheWriteSpan?.finish() } return From 44639d40fca851adfedd4425ff8b4783816fc35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 17:18:51 -0300 Subject: [PATCH 03/12] fix typing --- src/HttpClient/middlewares/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index 30a34b78..93771649 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -82,7 +82,7 @@ const CacheTypeNames = { interface CacheOptions { type: CacheType storage: CacheLayer - asyncSet?: Boolean + asyncSet?: boolean } export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { From e3494eb35cb8028087d770017de8f9e3cf01bb8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 17:20:17 -0300 Subject: [PATCH 04/12] fix typing --- src/HttpClient/typings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpClient/typings.ts b/src/HttpClient/typings.ts index 54dfb73c..fb42fdb4 100644 --- a/src/HttpClient/typings.ts +++ b/src/HttpClient/typings.ts @@ -83,7 +83,7 @@ export interface InstanceOptions { timeout?: number memoryCache?: CacheLayer diskCache?: CacheLayer - asyncSetCache?: Boolean + asyncSetCache?: boolean /** * Enables memoization, ephemeral within each request, for all requests of this client. From a2569ecde37263a99ca53cf46c05d6bc086cf893 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 17:52:29 -0300 Subject: [PATCH 05/12] add error handling to async case --- src/HttpClient/middlewares/cache.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index 93771649..a63bbf57 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -224,8 +224,8 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { const cacheWriteSpan = createCacheSpan(cacheType, 'write', tracer, span) try { - const storageSet = () => - storage.set(setKey, { + const storageSet = async () => + await storage.set(setKey, { etag, expiration, response: {data: cacheableData, headers, status}, @@ -233,7 +233,10 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { responseType, }) if (asyncSet) { - storageSet() + storageSet().catch(error => { + ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan); + logger?.warn({ message: 'Error writing to the HttpClient cache asynchronously', error }); + }) } else { await storageSet() span?.log({ From 9381175fbf42f8af1e6f534c06ec9f78ac8193e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 17:55:48 -0300 Subject: [PATCH 06/12] remove unnecessary semicolon --- src/HttpClient/middlewares/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index a63bbf57..d9d0fc8d 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -235,7 +235,7 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { if (asyncSet) { storageSet().catch(error => { ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan); - logger?.warn({ message: 'Error writing to the HttpClient cache asynchronously', error }); + logger?.warn({ message: 'Error writing to the HttpClient cache asynchronously', error }) }) } else { await storageSet() From aa17d3a8a2a8dc42ad3d7b9805d76579adf44cba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Mon, 3 Jun 2024 17:57:22 -0300 Subject: [PATCH 07/12] remove unnecessary semicolon --- src/HttpClient/middlewares/cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index d9d0fc8d..cbd76eed 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -234,7 +234,7 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { }) if (asyncSet) { storageSet().catch(error => { - ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan); + ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) logger?.warn({ message: 'Error writing to the HttpClient cache asynchronously', error }) }) } else { From 4040f78f3781b5111606b7ea85625cebba4036ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Tue, 4 Jun 2024 09:44:26 -0300 Subject: [PATCH 08/12] remove unnecessary handle error --- src/HttpClient/middlewares/cache.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/HttpClient/middlewares/cache.ts b/src/HttpClient/middlewares/cache.ts index cbd76eed..93771649 100644 --- a/src/HttpClient/middlewares/cache.ts +++ b/src/HttpClient/middlewares/cache.ts @@ -224,8 +224,8 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { const cacheWriteSpan = createCacheSpan(cacheType, 'write', tracer, span) try { - const storageSet = async () => - await storage.set(setKey, { + const storageSet = () => + storage.set(setKey, { etag, expiration, response: {data: cacheableData, headers, status}, @@ -233,10 +233,7 @@ export const cacheMiddleware = ({ type, storage, asyncSet }: CacheOptions) => { responseType, }) if (asyncSet) { - storageSet().catch(error => { - ErrorReport.create({ originalError: error }).injectOnSpan(cacheWriteSpan) - logger?.warn({ message: 'Error writing to the HttpClient cache asynchronously', error }) - }) + storageSet() } else { await storageSet() span?.log({ From af7dfdd7300dfa1440d5680fbd8ab7bd0c7ad0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Tue, 4 Jun 2024 11:27:31 -0300 Subject: [PATCH 09/12] bump version --- CHANGELOG.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 951a9780..3e323780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -145,7 +145,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [6.54.5] - 2021-10-22 -### Fixed +### Fixed - Extract session cookie in a safe manner diff --git a/package.json b/package.json index 1911d8df..70b13607 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vtex/api", - "version": "6.46.1", + "version": "6.47.0-beta.0", "description": "VTEX I/O API client", "main": "lib/index.js", "typings": "lib/index.d.ts", From 22bf3f0b412582c3cc48d0a9284a63679cebe178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Tue, 4 Jun 2024 12:16:40 -0300 Subject: [PATCH 10/12] set default true --- CHANGELOG.md | 2 +- src/HttpClient/HttpClient.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e323780..951a9780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -145,7 +145,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [6.54.5] - 2021-10-22 -### Fixed +### Fixed - Extract session cookie in a safe manner diff --git a/src/HttpClient/HttpClient.ts b/src/HttpClient/HttpClient.ts index fd439982..94df78ae 100644 --- a/src/HttpClient/HttpClient.ts +++ b/src/HttpClient/HttpClient.ts @@ -51,7 +51,7 @@ export class HttpClient { memoryCache, diskCache, memoizable = true, - asyncSetCache, + asyncSetCache = true, locale, name, metrics, From 85cf29b5e67b7c69e0038ac9574c5ae8441c7241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Tue, 4 Jun 2024 12:17:45 -0300 Subject: [PATCH 11/12] release beta v6.47.0-beta.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 70b13607..3d86fda2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vtex/api", - "version": "6.47.0-beta.0", + "version": "6.47.0-beta.1", "description": "VTEX I/O API client", "main": "lib/index.js", "typings": "lib/index.d.ts", From be819d33c37ea1818d9151f7e4f6dd9243e12452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Seixas?= Date: Tue, 11 Jun 2024 15:48:40 -0300 Subject: [PATCH 12/12] release v6.47.0 --- CHANGELOG.md | 6 +++++- package.json | 2 +- src/HttpClient/HttpClient.ts | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 951a9780..0567aefb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -## Fixed +## [6.47.0] - 2024-06-11 +### Added +- Add option to save asynchronous cache + +### Fixed - Add handler name for runtime http handlers ## [6.46.1] - 2024-01-31 diff --git a/package.json b/package.json index 3d86fda2..2d2c5647 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@vtex/api", - "version": "6.47.0-beta.1", + "version": "6.47.0", "description": "VTEX I/O API client", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/src/HttpClient/HttpClient.ts b/src/HttpClient/HttpClient.ts index 94df78ae..fd439982 100644 --- a/src/HttpClient/HttpClient.ts +++ b/src/HttpClient/HttpClient.ts @@ -51,7 +51,7 @@ export class HttpClient { memoryCache, diskCache, memoizable = true, - asyncSetCache = true, + asyncSetCache, locale, name, metrics,