-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split hash cache functionality into multiple classes
- Loading branch information
Showing
8 changed files
with
110 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { HashCache } from "./hash-cache"; | ||
import fse from "fs-extra"; | ||
import { hashes as log } from "../util/log"; | ||
import { resolve } from "node:path"; | ||
|
||
/** | ||
* A hash cache that is read from and stored in a JSON file. | ||
*/ | ||
export class FileHashCache extends HashCache { | ||
private readonly cache = new Map<string, string>(); | ||
private readonly path: string; | ||
|
||
constructor(path: string) { | ||
super(); | ||
|
||
this.path = resolve(path); | ||
} | ||
|
||
async read(): Promise<void> { | ||
try { | ||
const json: Record<string, string> = await fse.readJSON(this.path); | ||
log("loaded hashes from %s", this.path); | ||
|
||
for (const [name, hash] of Object.entries(json)) { | ||
this.cache.set(name, hash); | ||
} | ||
} catch (error) { | ||
if (error.code === "ENOENT") { | ||
await fse.writeJSON(this.path, {}); | ||
log("created hashes file at %s", this.path); | ||
} else { | ||
log("failed to load hashes file at %s", this.path); | ||
} | ||
} | ||
} | ||
|
||
async write(): Promise<void> { | ||
try { | ||
await fse.writeJSON(this.path, Object.fromEntries(this.cache)); | ||
log("saved new hashes to %s", this.path); | ||
} catch { | ||
log("failed to save new hashes"); | ||
} | ||
} | ||
|
||
isCached(name: string, hash: string): boolean { | ||
if (this.cache.get(name) === hash) { | ||
log("skipping %s as its hash has not changed", name); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
add(name: string, hash: string): void { | ||
this.cache.set(name, hash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* A map representing the last hash saved for each bundle. | ||
*/ | ||
export abstract class HashCache { | ||
read(): void | Promise<void> { | ||
return; | ||
} | ||
|
||
write(): void | Promise<void> { | ||
return; | ||
} | ||
|
||
abstract isCached(name: string, hash: string): boolean; | ||
abstract add(name: string, hash: string): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { HashCache } from "./hash-cache"; | ||
import { hashes as log } from "../util/log"; | ||
|
||
/** | ||
* A hash cache that does not store or check hashes. | ||
*/ | ||
export class NeverHashCache extends HashCache { | ||
read(): void { | ||
log("not checking bundle hashes"); | ||
} | ||
|
||
isCached(): boolean { | ||
return false; | ||
} | ||
|
||
add(): void { | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters