Skip to content

Commit

Permalink
Combine solutions for both #7 and #14
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 25, 2022
1 parent 8038130 commit 5279c3f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
2 changes: 2 additions & 0 deletions eleventy-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const globalOptions = {
// },

verbose: false, // Changed in 3.0+

hashLength: 30,
};

function isFullUrl(url) {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"ava": "^4.0.1"
},
"dependencies": {
"@sindresorhus/slugify": "^1.1.2",
"debug": "^4.3.3",
"flat-cache": "^3.0.4",
"node-fetch": "^2.6.7",
Expand Down
17 changes: 10 additions & 7 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ const fs = require("fs");
const fsp = fs.promises; // Node 10+
const path = require("path");
const flatCache = require("flat-cache");
const slugify = require("@sindresorhus/slugify");
const { createHash } = require("crypto");

const debug = require("debug")("EleventyCacheAssets");

class AssetCache {
constructor(uniqueKey, cacheDirectory, options = {}) {
this.hash = uniqueKey;
this.hash = AssetCache.getHash(uniqueKey, options.hashLength);
this.cacheDirectory = cacheDirectory || ".cache";
this.defaultDuration = "1d";
this.options = options;
}

// Defult hashLength also set in global options, duplicated here for tests
static getHash(url, hashLength = 30) {
let hash = createHash("sha256");
hash.update(url);
return (""+hash.digest('hex')).substr(0, hashLength);
}

get source() {
return this._source;
}
Expand All @@ -26,11 +34,6 @@ class AssetCache {
}

set hash(value) {
value = slugify(value, {
preserveLeadingUnderscore: true,
preserveTrailingDash: true,
});

if(value !== this._hash) {
this._cacheLocationDirty = true;
}
Expand Down
10 changes: 1 addition & 9 deletions src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require("fs");
const fsp = fs.promises; // Node 10+
const { createHash } = require("crypto");

const fetch = require("node-fetch");
const AssetCache = require("./AssetCache");
Expand All @@ -12,8 +11,7 @@ class RemoteAssetCache extends AssetCache {
if(options.removeUrlQueryParams) {
cleanUrl = RemoteAssetCache.cleanUrl(cleanUrl);
}
let hash = RemoteAssetCache.getHash(cleanUrl);
super(hash, cacheDirectory, options);
super(cleanUrl, cacheDirectory, options);

this.url = url;
this.options = options;
Expand All @@ -22,12 +20,6 @@ class RemoteAssetCache extends AssetCache {
this.displayUrl = this.formatUrlForDisplay(cleanUrl);
}

static getHash(url) {
let hash = createHash("sha256");
hash.update(url);
return hash.digest('hex');
}

static cleanUrl(url) {
let cleanUrl = new URL(url);
cleanUrl.search = new URLSearchParams([]);
Expand Down
12 changes: 6 additions & 6 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ test("Absolute path cache directory", t => {
let cache = new AssetCache("lksdjflkjsdf", "/tmp/.cache");
let cachePath = normalizePath(cache.cachePath);

t.is(cachePath, "/tmp/.cache/eleventy-fetch-lksdjflkjsdf");
t.is(cachePath, "/tmp/.cache/eleventy-fetch-73015bafd152bccf9929e0f4dcbe36");
});

test("Relative path cache directory", t => {
let cache = new AssetCache("lksdjflkjsdf", ".cache");
let cachePath = normalizePath(cache.cachePath);

t.not(cachePath, ".cache/eleventy-fetch-lksdjflkjsdf");
t.true(cachePath.endsWith(".cache/eleventy-fetch-lksdjflkjsdf"));
t.not(cachePath, ".cache/eleventy-fetch-73015bafd152bccf9929e0f4dcbe36");
t.true(cachePath.endsWith(".cache/eleventy-fetch-73015bafd152bccf9929e0f4dcbe36"));
});

test("AWS Lambda root directory resolves correctly", t => {
Expand All @@ -36,7 +36,7 @@ test("AWS Lambda root directory resolves correctly", t => {
let cache = new AssetCache("lksdjflkjsdf", ".cache");
let cachePath = normalizePath(cache.cachePath);

t.is(cachePath, `${cwd}/.cache/eleventy-fetch-lksdjflkjsdf`);
t.is(cachePath, `${cwd}/.cache/eleventy-fetch-73015bafd152bccf9929e0f4dcbe36`);
delete "ELEVENTY_ROOT" in process.env;
delete "LAMBDA_TASK_ROOT" in process.env;
});
Expand All @@ -54,9 +54,9 @@ test("Test a save", async t => {
fs.unlinkSync(jsonCachePath);
});

test("Cache path should be slugified, issue #14", t => {
test("Cache path should handle slashes without creating directories, issue #14", t => {
let cache = new AssetCache("lksdjflk/jsdf", "/tmp/.cache");
let cachePath = normalizePath(cache.cachePath);

t.is(cachePath, "/tmp/.cache/eleventy-fetch-lksdjflk-jsdf");
t.is(cachePath, "/tmp/.cache/eleventy-fetch-135797dbf5ab1187e5003c49162602");
});
9 changes: 5 additions & 4 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require("ava");
const path = require("path");
const { Util } = require("../");
const AssetCache = require("../src/AssetCache");
const RemoteAssetCache = require("../src/RemoteAssetCache");

test("getDurationMs", t => {
Expand All @@ -22,13 +23,13 @@ test("getDurationMs", t => {

test("Local hash file names", async t => {
let pngUrl = "https://www.zachleat.com/img/avatar-2017-big.png";
t.is((new RemoteAssetCache(pngUrl)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${RemoteAssetCache.getHash(pngUrl)}`));
t.is((new RemoteAssetCache(pngUrl)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${AssetCache.getHash(pngUrl)}`));

let fontUrl = "https://www.zachleat.com/font.woff";
t.is((new RemoteAssetCache(fontUrl)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${RemoteAssetCache.getHash(fontUrl)}`));
t.is((new RemoteAssetCache(fontUrl)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${AssetCache.getHash(fontUrl)}`));

let fontUrl2 = "https://www.zachleat.com/font.woff2";
t.is((new RemoteAssetCache(fontUrl2)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${RemoteAssetCache.getHash(fontUrl2)}`));
t.is((new RemoteAssetCache(fontUrl2)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${AssetCache.getHash(fontUrl2)}`));
});

test("Clean url", async t => {
Expand All @@ -41,7 +42,7 @@ test("Clean url", async t => {

test("Local hash without file extension in URL", async t => {
let noExt = "https://twitter.com/zachleat/profile_image?size=bigger";
t.is((new RemoteAssetCache(noExt)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${RemoteAssetCache.getHash(noExt)}`));
t.is((new RemoteAssetCache(noExt)).cachePath, path.resolve(".", `.cache/eleventy-fetch-${AssetCache.getHash(noExt)}`));
});

test("Unique hashes for URLs", async t => {
Expand Down

0 comments on commit 5279c3f

Please sign in to comment.