Skip to content

Commit

Permalink
Fixes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 25, 2022
1 parent e2d03e7 commit 16bdb25
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
11 changes: 10 additions & 1 deletion eleventy-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ const globalOptions = {
type: "buffer",
directory: ".cache",
concurrency: 10,
removeUrlQueryParams: false,
fetchOptions: {},
dryRun: false, // don’t write anything to the file system

// *does* affect cache key hash
removeUrlQueryParams: false,

// runs after removeUrlQueryParams, does not affect cache key hash
// formatUrlForDisplay: function(url) {
// return url;
// },

verbose: false, // Changed in 3.0+
};

function isFullUrl(url) {
Expand Down
29 changes: 23 additions & 6 deletions src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ class RemoteAssetCache extends AssetCache {
constructor(url, cacheDirectory, options = {}) {
let cleanUrl = url;
if(options.removeUrlQueryParams) {
cleanUrl = RemoteAssetCache.cleanUrl(url);
cleanUrl = RemoteAssetCache.cleanUrl(cleanUrl);
}
super(shorthash(cleanUrl), cacheDirectory, options);
this.url = url;
this.cleanUrl = cleanUrl;
this.options = options;

// Important: runs after removeUrlQueryParams
this.displayUrl = this.formatUrlForDisplay(cleanUrl);
}

static cleanUrl(url) {
Expand All @@ -23,6 +25,21 @@ class RemoteAssetCache extends AssetCache {
return cleanUrl.toString();
}

formatUrlForDisplay(url) {
if(this.options.formatUrlForDisplay && typeof this.options.formatUrlForDisplay === "function") {
return this.options.formatUrlForDisplay(url);
}
return url;
}

log(message) {
if(this.options.verbose) {
console.log(message);
} else {
debug(message);
}
}

get url() {
return this._url;
}
Expand Down Expand Up @@ -52,20 +69,20 @@ class RemoteAssetCache extends AssetCache {
let fetchOptions = optionsOverride.fetchOptions || this.options.fetchOptions || {};
let response = await fetch(this.url, fetchOptions);
if(!response.ok) {
throw new Error(`Bad response for ${this.cleanUrl} (${response.status}): ${response.statusText}`)
throw new Error(`Bad response for ${this.displayUrl} (${response.status}): ${response.statusText}`)
}

let type = optionsOverride.type || this.options.type;
let body = await this.getResponseValue(response, type);
console.log( `[11ty/eleventy-fetch] ${isDryRun? "Fetching" : "Caching"}: ${this.displayUrl}` );
this.log( `[11ty/eleventy-fetch] ${isDryRun? "Fetching" : "Caching"}: ${this.displayUrl}` );
if(!isDryRun) {
await super.save(body, type);
}
return body;
} catch(e) {
if(this.cachedObject) {
console.log( `[11ty/eleventy-fetch] Error fetching ${this.displayUrl}. Message: ${e.message}`);
console.log( `[11ty/eleventy-fetch] Failing gracefully with an expired cache entry.` );
this.log( `[11ty/eleventy-fetch] Error fetching ${this.displayUrl}. Message: ${e.message}`);
this.log( `[11ty/eleventy-fetch] Failing gracefully with an expired cache entry.` );
return super.getCachedValue();
} else {
return Promise.reject(e);
Expand Down
6 changes: 4 additions & 2 deletions test/QueueTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ test("Double Fetch", async t => {

let forDestroyOnly = new RemoteAssetCache(pngUrl);
// file is now accessible
await t.notThrowsAsync(forDestroyOnly.destroy());
try {
await forDestroyOnly.destroy()
} catch(e) {}
});

test("Double Fetch (dry run)", async t => {
Expand All @@ -33,7 +35,7 @@ test("Double Fetch (dry run)", async t => {
await ac2;

let forTestOnly = new RemoteAssetCache(pngUrl, ".cache", {
dryRun: true
dryRun: true,
});
// file is now accessible
t.false(forTestOnly.hasCacheFiles());
Expand Down
37 changes: 31 additions & 6 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ test("Clean url", async t => {
let shortUrl = "https://example.com/207115/photos/243-0-1.jpg";
let longUrl = "https://example.com/207115/photos/243-0-1.jpg?Policy=FAKE_THING~2123ksjhd&Signature=FAKE_THING~2123ksjhd&Key-Pair-Id=FAKE_THING~2123ksjhd";
t.is((new RemoteAssetCache(longUrl, ".cache", {
removeUrlQueryParams: true
})).cleanUrl, shortUrl);
removeUrlQueryParams: true,
})).displayUrl, shortUrl);
});

test("Local hash without file extension in URL", async t => {
Expand All @@ -53,15 +53,40 @@ test("Fetching!", async t => {
let buffer = await ac.fetch();
t.is(Buffer.isBuffer(buffer), true);

await t.notThrowsAsync(ac.destroy());
try {
await ac.destroy()
} catch(e) {}
});

test("Fetching (dry run)!", async t => {
let svgUrl = "https://www.zachleat.com/img/avatar-2017-88.png";
let ac = new RemoteAssetCache(svgUrl);
let buffer = await ac.fetch({
dryRun: true,
let ac = new RemoteAssetCache(svgUrl, ".cache", {
dryRun: true
});
let buffer = await ac.fetch();
t.is(Buffer.isBuffer(buffer), true);
t.false(ac.hasCacheFiles());
});

test("formatUrlForDisplay (manual query param removal)", async t => {
let finalUrl = "https://example.com/207115/photos/243-0-1.jpg";
let longUrl = "https://example.com/207115/photos/243-0-1.jpg?Policy=FAKE_THING~2123ksjhd&Signature=FAKE_THING~2123ksjhd&Key-Pair-Id=FAKE_THING~2123ksjhd";
t.is((new RemoteAssetCache(longUrl, ".cache", {
removeUrlQueryParams: false,
formatUrlForDisplay(url) {
let [rest, queryParams] = url.split("?");
return rest;
}
})).displayUrl, finalUrl);
});

test("formatUrlForDisplay (using removeUrlQueryParams)", async t => {
let finalUrl = "https://example.com/207115/photos/243-0-1.jpg";
let longUrl = "https://example.com/207115/photos/243-0-1.jpg?Policy=FAKE_THING~2123ksjhd&Signature=FAKE_THING~2123ksjhd&Key-Pair-Id=FAKE_THING~2123ksjhd";
t.is((new RemoteAssetCache(longUrl, ".cache", {
removeUrlQueryParams: true,
formatUrlForDisplay(url) {
return url;
}
})).displayUrl, finalUrl);
});

0 comments on commit 16bdb25

Please sign in to comment.