From a6e43901a65581f8ac106b274cfabc661e4fed8c Mon Sep 17 00:00:00 2001 From: fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Sat, 11 Jan 2025 21:01:59 +0800 Subject: [PATCH] fix: resolve plex user mismatch due to caching issues This commit addresses an issue where cached responses for PlexTV API requests could return incorrect user data when multiple users were logged in. This was caused by a shared cache key that did not account for differences in auth tokens, i.e `X-Plex-Token`. The `serializeCacheKey` method should now include headers in the cache key generation to ensure unique cache keys. This should fix the plex user mismatch that occurred due to the same cache key being used without accounting for the difference in auth token. fix #1227 --- server/api/externalapi.ts | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/server/api/externalapi.ts b/server/api/externalapi.ts index 9d6ef8d4..741ef58e 100644 --- a/server/api/externalapi.ts +++ b/server/api/externalapi.ts @@ -16,6 +16,10 @@ interface ExternalAPIOptions { rateLimit?: RateLimitOptions; } +interface CustomRequestConfig extends RequestInit { + params?: Record; +} + class ExternalAPI { protected fetch: typeof fetch; protected params: Record; @@ -67,12 +71,11 @@ class ExternalAPI { endpoint: string, params?: Record, ttl?: number, - config?: RequestInit + config?: CustomRequestConfig ): Promise { - const cacheKey = this.serializeCacheKey(endpoint, { - ...this.params, - ...params, - }); + const headers = { ...this.defaultHeaders, ...config?.headers }; + const cacheKey = this.serializeCacheKey(endpoint, config?.params, headers); + const cachedItem = this.cache?.get(cacheKey); if (cachedItem) { return cachedItem; @@ -325,13 +328,15 @@ class ExternalAPI { private serializeCacheKey( endpoint: string, - params?: Record + params?: Record, + headers?: Record ) { - if (!params) { - return `${this.baseUrl}${endpoint}`; + const key = `${this.baseUrl}${endpoint}`; + if (!params && !headers) { + return key; } - return `${this.baseUrl}${endpoint}${JSON.stringify(params)}`; + return `${key}${JSON.stringify({ params, headers })}`; } private async getDataFromResponse(response: Response) {