Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace shorthash with sha1 #8

Merged
merged 2 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"flat-cache": "^2.0.1",
"node-fetch": "^2.6.0",
"p-queue": "^6.3.0",
"short-hash": "^1.0.0"
"sha1": "^1.1.1"
},
"ava": {
"failFast": true,
Expand Down
4 changes: 2 additions & 2 deletions src/RemoteAssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const fs = require("fs");
const fsp = fs.promises; // Node 10+
const path = require("path");
const fetch = require("node-fetch");
const shorthash = require("short-hash");
const sha1 = require("sha1");
const flatCache = require("flat-cache");
const AssetCache = require("./AssetCache");
const debug = require("debug")("EleventyCacheAssets");

class RemoteAssetCache extends AssetCache {
constructor(url, cacheDirectory) {
super(shorthash(url), cacheDirectory);
super(sha1(url), cacheDirectory);
this.url = url;
}

Expand Down
18 changes: 13 additions & 5 deletions test/RemoteAssetCacheTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require("ava");
const path = require("path");
const fs = require("fs");
const shorthash = require("short-hash");
const sha1 = require("sha1");
const RemoteAssetCache = require("../src/RemoteAssetCache");

let fsp = fs.promises;
Expand All @@ -25,18 +25,26 @@ 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-cache-assets-${shorthash(pngUrl)}`));
t.is((new RemoteAssetCache(pngUrl)).cachePath, path.resolve(".", `.cache/eleventy-cache-assets-${sha1(pngUrl)}`));

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

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

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-cache-assets-${shorthash(noExt)}`));
t.is((new RemoteAssetCache(noExt)).cachePath, path.resolve(".", `.cache/eleventy-cache-assets-${sha1(noExt)}`));
});

test("Unique hashes for URLs", async t => {
let apiURL1 = 'https://api.zooniverse.org/projects/illustratedlife/talk/subjects/ASC0000qu3';
let apiURL2 = 'https://api.zooniverse.org/projects/illustratedlife/talk/subjects/ASC0000q71';
let cachePath1 = new RemoteAssetCache(apiURL1).cachePath;
let cachePath2 = new RemoteAssetCache(apiURL2).cachePath;
t.not(cachePath1, cachePath2);
});

test("Fetching!", async t => {
Expand Down