From d9f3a73bd9b52ae4a115fd4c0fd1750af3f6f2a9 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Mon, 13 May 2024 09:27:41 -0500 Subject: [PATCH] Support for array of keys to assetcache hashing. --- src/AssetCache.js | 13 +++++++++++-- src/RemoteAssetCache.js | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/AssetCache.js b/src/AssetCache.js index adcc4a9..7cdd892 100644 --- a/src/AssetCache.js +++ b/src/AssetCache.js @@ -24,9 +24,18 @@ class AssetCache { } // Defult hashLength also set in global options, duplicated here for tests - static getHash(url, hashLength = 30) { + // v5.0+ key can be Array or literal + static getHash(key, hashLength = 30) { let hash = createHash("sha256"); - hash.update(url); + + if (!Array.isArray(key)) { + key = [key]; + } + + for (let k of key) { + hash.update(k); + } + return ("" + hash.digest("hex")).slice(0, hashLength); } diff --git a/src/RemoteAssetCache.js b/src/RemoteAssetCache.js index 15a90db..f93d148 100644 --- a/src/RemoteAssetCache.js +++ b/src/RemoteAssetCache.js @@ -10,7 +10,9 @@ class RemoteAssetCache extends AssetCache { if (options.removeUrlQueryParams) { cleanUrl = RemoteAssetCache.cleanUrl(cleanUrl); } + let cacheKey = [cleanUrl]; + if (options.fetchOptions) { if (options.fetchOptions.method && options.fetchOptions.method !== "GET") { cacheKey.push(options.fetchOptions.method); @@ -19,7 +21,7 @@ class RemoteAssetCache extends AssetCache { cacheKey.push(options.fetchOptions.body); } } - cacheKey = cacheKey.length > 1 ? JSON.stringify(cacheKey) : cleanUrl; + super(cacheKey, cacheDirectory, options); this.url = url;