From bab0bb5995e2a0149558c744e58c09e6519f4ec8 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 22 Oct 2024 10:52:22 -0500 Subject: [PATCH] Make sure we destroy cache in new tests for #32 --- .editorconfig | 8 ++++++++ src/AssetCache.js | 29 ++++++++++++++++++----------- src/RemoteAssetCache.js | 7 +++++++ test/RemoteAssetCacheTest.js | 28 ++++++++++++++++++++++++---- 4 files changed, 57 insertions(+), 15 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..137c871 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,8 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 diff --git a/src/AssetCache.js b/src/AssetCache.js index 3a35c9c..0ceadc3 100644 --- a/src/AssetCache.js +++ b/src/AssetCache.js @@ -7,9 +7,14 @@ const { createHash } = require("crypto"); const debug = require("debug")("Eleventy:Fetch"); class AssetCache { + #customFilename; + constructor(url, cacheDirectory, options = {}) { let uniqueKey; - if ((typeof url === "object" && typeof url.then === "function") || (typeof url === "function" && url.constructor.name === "AsyncFunction")) { + if ( + (typeof url === "object" && typeof url.then === "function") || + (typeof url === "function" && url.constructor.name === "AsyncFunction") + ) { uniqueKey = options.formatUrlForDisplay(); } else { uniqueKey = url; @@ -21,22 +26,24 @@ class AssetCache { this.options = options; // Compute the filename only once - if (typeof this.options.cacheFilename === 'function') { - this._customFilename = this.options.cacheFilename(this.uniqueKey, this.hash); + if (typeof this.options.filenameFormat === "function") { + this.#customFilename = this.options.filenameFormat(this.uniqueKey, this.hash); - if (typeof this._customFilename !== 'string') { + if (typeof this.#customFilename !== "string") { throw new Error(`The provided cacheFilename callback function did not return a string.`); } - if (typeof this._customFilename.length === 0) { + if (typeof this.#customFilename.length === 0) { throw new Error(`The provided cacheFilename callback function returned an empty string.`); } // Ensure no illegal characters are present (Windows or Linux: forward/backslash, chevrons, colon, double-quote, pipe, question mark, asterisk) - if (this._customFilename.match(/([\/\\<>:"|?*]+?)/)) { - const sanitizedFilename = this._customFilename.replace(/[\/\\<>:"|?*]+/g, ''); - console.warn(`[AssetCache] Some illegal characters were removed from the cache filename: ${this._customFilename} will be cached as ${sanitizedFilename}.`); - this._customFilename = sanitizedFilename; + if (this.#customFilename.match(/([\/\\<>:"|?*]+?)/)) { + const sanitizedFilename = this.#customFilename.replace(/[\/\\<>:"|?*]+/g, ""); + console.warn( + `[AssetCache] Some illegal characters were removed from the cache filename: ${this.#customFilename} will be cached as ${sanitizedFilename}.`, + ); + this.#customFilename = sanitizedFilename; } } } @@ -103,8 +110,8 @@ class AssetCache { } get cacheFilename() { - if (typeof this._customFilename === 'string' && this._customFilename.length > 0) { - return this._customFilename; + if (typeof this.#customFilename === "string" && this.#customFilename.length > 0) { + return this.#customFilename; } return `eleventy-fetch-${this.hash}`; } diff --git a/src/RemoteAssetCache.js b/src/RemoteAssetCache.js index afd5134..d3f3c5c 100644 --- a/src/RemoteAssetCache.js +++ b/src/RemoteAssetCache.js @@ -8,6 +8,13 @@ class RemoteAssetCache extends AssetCache { constructor(url, cacheDirectory, options = {}) { let cleanUrl = url; if (options.removeUrlQueryParams) { + if (typeof cleanUrl !== "string") { + throw new Error( + "The `removeUrlQueryParams` option requires the cache source to be a string. Received: " + + typeof url, + ); + } + cleanUrl = RemoteAssetCache.cleanUrl(cleanUrl); } diff --git a/test/RemoteAssetCacheTest.js b/test/RemoteAssetCacheTest.js index e1e5c6a..ebdb1f8 100644 --- a/test/RemoteAssetCacheTest.js +++ b/test/RemoteAssetCacheTest.js @@ -198,6 +198,10 @@ test("Error with `cause`", async (t) => { ); t.truthy(e.cause); } + + try { + await asset.destroy(); + } catch (e) {} }); test("supports promises that resolve", async (t) => { @@ -213,6 +217,10 @@ test("supports promises that resolve", async (t) => { let actual = await asset.fetch(); t.deepEqual(actual, expected); + + try { + await asset.destroy(); + } catch (e) {} }); test("supports promises that reject", async (t) => { @@ -221,17 +229,21 @@ test("supports promises that reject", async (t) => { let promise = Promise.reject(new Error(expected, { cause })); let asset = new RemoteAssetCache(promise, undefined, { - formatUrlForDisplay() { - return "reject-promise"; - }, + formatUrlForDisplay() { + return "reject-promise"; + }, }); try { - await asset.fetch(); + await asset.fetch(); } catch (e) { t.is(e.message, expected); t.is(e.cause, cause); } + + try { + await asset.destroy(); + } catch (e) {} }); test("supports async functions that return data", async (t) => { @@ -249,6 +261,10 @@ test("supports async functions that return data", async (t) => { let actual = await asset.fetch(); t.deepEqual(actual, expected); + + try { + await asset.destroy(); + } catch (e) {} }); test("supports async functions that throw", async (t) => { @@ -270,4 +286,8 @@ test("supports async functions that throw", async (t) => { t.is(e.message, expected); t.is(e.cause, cause); } + + try { + await asset.destroy(); + } catch (e) {} });