Skip to content

Commit

Permalink
Ignore setInitialCacheTimestamp if called after initial save (no error)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Dec 18, 2024
1 parent 8dc464b commit 431c585
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
33 changes: 11 additions & 22 deletions src/AssetCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class AssetCache {
#source;
#hash;
#customFilename;
#hasSaved = false;
#cache;
#cacheDirectory;
#cacheLocationDirty = false;
Expand Down Expand Up @@ -42,10 +41,6 @@ class AssetCache {

setInitialCacheTimestamp(timestamp) {
this.initialCacheTimestamp = timestamp;

if(this.#hasSaved) {
throw new Error("`setInitialCacheTimestamp` method must be called before the object is saved.");
}
}

log(message) {
Expand Down Expand Up @@ -235,21 +230,11 @@ class AssetCache {
}

async save(contents, type = "buffer", metadata = {}) {
if (this.options.dryRun) {
debug("An attempt was made to save to the file system with `dryRun: true`. Skipping.");

// Errors are still expected from this
this.#hasSaved = true;
return;
}

if(!contents) {
throw new Error("save(contents) expects contents (it was falsy)");
throw new Error("save(contents) expects contents (was falsy)");
}

if(!this.isDirEnsured) {
this.ensureDir();
}
this.ensureDir();

if (type === "json" || type === "parsed-xml") {
contents = JSON.stringify(contents);
Expand All @@ -258,18 +243,22 @@ class AssetCache {
let contentPath = this.getCachedContentsPath(type);

// the contents must exist before the cache metadata are saved below
fs.writeFileSync(contentPath, contents);

debug(`Writing ${contentPath}`);
if(!this.options.dryRun) {
fs.writeFileSync(contentPath, contents);
debug(`Writing ${contentPath}`);
} else {
debug(`Dry run writing ${contentPath}`);
}

this.cache.set(this.hash, {
cachedAt: this.initialCacheTimestamp || Date.now(),
type: type,
metadata,
});

this.cache.save();
this.#hasSaved = true;
if(!this.options.dryRun) {
this.cache.save();
}
}

async getCachedContents(type) {
Expand Down
33 changes: 24 additions & 9 deletions test/AssetCacheTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,40 @@ test("setInitialCacheTimestamp method (used by Eleventy Image to establish a con
let cache = new AssetCache("this_is_a_test", ".cache", {
dryRun: true
});
let timestamp = Date.now();
let timestamp = (new Date(2024,1,1)).getTime();
cache.setInitialCacheTimestamp(timestamp);

await cache.save("test");

t.is(cache.getCachedTimestamp(), timestamp);
});

test("setInitialCacheTimestamp method after save throws error", async (t) => {
let cache = new AssetCache("this_is_a_test", ".cache", {
test("setInitialCacheTimestamp method after save is ignored", async (t) => {
let cache = new AssetCache("this_is_a_test2", ".cache", {
dryRun: true
});
let timestamp = Date.now();

let timestamp = (new Date(2024,1,1)).getTime();

await cache.save("test");

t.throws(() => {
cache.setInitialCacheTimestamp(timestamp);
}, {
message: "`setInitialCacheTimestamp` method must be called before the object is saved."
})
cache.setInitialCacheTimestamp(timestamp);

t.not(cache.getCachedTimestamp(), timestamp);
});

test("setInitialCacheTimestamp method after second save is used", async (t) => {
let cache = new AssetCache("this_is_a_test3", ".cache", {
dryRun: true
});

let timestamp = (new Date(2024,1,1)).getTime();

await cache.save("test");

cache.setInitialCacheTimestamp(timestamp);

await cache.save("test");

t.is(cache.getCachedTimestamp(), timestamp);
});

0 comments on commit 431c585

Please sign in to comment.