Skip to content

Commit

Permalink
Make sure we destroy cache in new tests for #32
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 22, 2024
1 parent 39cafcc commit bab0bb5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = tab
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8
29 changes: 18 additions & 11 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Expand Down Expand Up @@ -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}`;
}
Expand Down
7 changes: 7 additions & 0 deletions src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
28 changes: 24 additions & 4 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) {}
});

0 comments on commit bab0bb5

Please sign in to comment.