@chriscdn/file-cache 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ //#region src/index.d.ts
2
+ type DirectoryPath = string;
3
+ type FileName = string;
4
+ type FilePath = string;
5
+ type Milliseconds = number;
6
+ export type FileCacheOptions<T extends Record<string, any>> = {
7
+ cachePath: DirectoryPath;
8
+ autoCreateCachePath?: boolean;
9
+ cb: (filePath: FilePath, args: T, cache: FileCache<T>) => Promise<void>;
10
+ ext: (args: T) => string | Promise<string>;
11
+ resolveCacheKey?: (args: T) => string | Promise<string>;
12
+ ttl: Milliseconds;
13
+ cleanupInterval?: Milliseconds;
14
+ };
15
+ declare class FileCache<T extends Record<string, any>> {
16
+ private _cachePath;
17
+ private _cb;
18
+ private _ttl;
19
+ private _cleanupInterval;
20
+ private _resolveCacheKey;
21
+ private _ext;
22
+ private _intervalId;
23
+ private _semaphore;
24
+ private _cleanupGroupSemaphore;
25
+ constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheKey }: FileCacheOptions<T>);
26
+ /**
27
+ * Resolves the filename for the cache based on the provided arguments.
28
+ *
29
+ * Uses the provided `resolveFileName` function if available, or falls back to
30
+ * generating a hash from the arguments to create a unique file name.
31
+ */
32
+ private resolveFileName;
33
+ /**
34
+ * Retrieves a cached file for the given arguments.
35
+ * If the file doesn't exist, it is generated by the callback and stored.
36
+ *
37
+ * Concurrency-safe, ensuring no conflicts when multiple requests are made
38
+ * for the same file.
39
+ *
40
+ * @param args The arguments used to determine the file's identity and generation
41
+ * @returns The path to the cached or newly created file
42
+ */
43
+ getFile(args: T): Promise<FilePath>;
44
+ /**
45
+ * Resolves the full file path for the cached file based on the provided arguments.
46
+ *
47
+ * @param args The arguments used to resolve the file path
48
+ * @returns The full path to the cached file
49
+ */
50
+ resolveFilePath(args: T): Promise<FilePath>;
51
+ /**
52
+ * Stops the background cleanup process, but does not delete any cached files.
53
+ */
54
+ destroy(): void;
55
+ has(args: T): Promise<boolean>;
56
+ /**
57
+ * Runs a cleanup pass to remove files older than 1 day from the cache.
58
+ * This is called periodically, but can be triggered manually.
59
+ */
60
+ cleanup(): Promise<void>;
61
+ }
62
+ //#endregion
63
+ export { type DirectoryPath, FileCache, type FileName, type FilePath };
64
+ //# sourceMappingURL=index.d.mts.map
package/lib/index.mjs ADDED
@@ -0,0 +1,114 @@
1
+ import path from "path";
2
+ import fs from "fs";
3
+ import { pathExists, pathExistsSync } from "path-exists";
4
+ import sha1 from "sha1";
5
+ import { GroupSemaphore, Semaphore } from "@chriscdn/promise-semaphore";
6
+ import touch from "touch";
7
+ import { findNuke } from "@chriscdn/find-nuke";
8
+ import { Duration } from "@chriscdn/duration";
9
+ //#region src/index.ts
10
+ var FileCache = class {
11
+ _cachePath;
12
+ _cb;
13
+ _ttl;
14
+ _cleanupInterval;
15
+ _resolveCacheKey;
16
+ _ext;
17
+ _intervalId = null;
18
+ _semaphore = new Semaphore();
19
+ _cleanupGroupSemaphore = new GroupSemaphore();
20
+ constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheKey }) {
21
+ this._cachePath = cachePath;
22
+ this._cb = cb;
23
+ this._ext = ext;
24
+ this._ttl = ttl;
25
+ this._cleanupInterval = cleanupInterval ?? Duration.toMilliseconds({ days: 1 });
26
+ this._resolveCacheKey = resolveCacheKey ?? ((args) => JSON.stringify(args));
27
+ if (pathExistsSync(cachePath)) {} else if (autoCreateCachePath) fs.mkdirSync(cachePath, { recursive: true });
28
+ else throw new Error("💥 FileCache error: cachePath does not exist.");
29
+ this.cleanup();
30
+ this._intervalId = setInterval(this.cleanup.bind(this), this._cleanupInterval);
31
+ }
32
+ /**
33
+ * Resolves the filename for the cache based on the provided arguments.
34
+ *
35
+ * Uses the provided `resolveFileName` function if available, or falls back to
36
+ * generating a hash from the arguments to create a unique file name.
37
+ */
38
+ async resolveFileName(args) {
39
+ const [name, ext] = await Promise.all([this._resolveCacheKey(args), this._ext(args)]);
40
+ const resolvedFileName = sha1(JSON.stringify([name, ext]));
41
+ return path.format({
42
+ name: resolvedFileName,
43
+ ext
44
+ });
45
+ }
46
+ /**
47
+ * Retrieves a cached file for the given arguments.
48
+ * If the file doesn't exist, it is generated by the callback and stored.
49
+ *
50
+ * Concurrency-safe, ensuring no conflicts when multiple requests are made
51
+ * for the same file.
52
+ *
53
+ * @param args The arguments used to determine the file's identity and generation
54
+ * @returns The path to the cached or newly created file
55
+ */
56
+ async getFile(args) {
57
+ const filePath = await this.resolveFilePath(args);
58
+ try {
59
+ await Promise.all([this._cleanupGroupSemaphore.acquire("getFile"), this._semaphore.acquire(filePath)]);
60
+ if (await pathExists(filePath)) touch(filePath);
61
+ else {
62
+ await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
63
+ await this._cb(filePath, args, this);
64
+ }
65
+ } finally {
66
+ this._semaphore.release(filePath);
67
+ this._cleanupGroupSemaphore.release("getFile");
68
+ }
69
+ return filePath;
70
+ }
71
+ /**
72
+ * Resolves the full file path for the cached file based on the provided arguments.
73
+ *
74
+ * @param args The arguments used to resolve the file path
75
+ * @returns The full path to the cached file
76
+ */
77
+ async resolveFilePath(args) {
78
+ const fileName = await this.resolveFileName(args);
79
+ return path.resolve(this._cachePath, fileName[0], fileName[1], fileName[2], fileName);
80
+ }
81
+ /**
82
+ * Stops the background cleanup process, but does not delete any cached files.
83
+ */
84
+ destroy() {
85
+ if (this._intervalId) {
86
+ clearInterval(this._intervalId);
87
+ this._intervalId = null;
88
+ }
89
+ }
90
+ async has(args) {
91
+ const filePath = await this.resolveFilePath(args);
92
+ return await pathExists(filePath);
93
+ }
94
+ /**
95
+ * Runs a cleanup pass to remove files older than 1 day from the cache.
96
+ * This is called periodically, but can be triggered manually.
97
+ */
98
+ async cleanup() {
99
+ try {
100
+ await this._cleanupGroupSemaphore.acquire("cleanup");
101
+ await findNuke(this._cachePath, {
102
+ olderThan: this._ttl,
103
+ verbose: true,
104
+ deleteEmptyDirectories: true
105
+ });
106
+ } finally {
107
+ this._cleanupGroupSemaphore.release("cleanup");
108
+ }
109
+ }
110
+ };
111
+ //#endregion
112
+ export { FileCache };
113
+
114
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import path from \"path\";\nimport fs from \"fs\";\nimport { pathExists, pathExistsSync } from \"path-exists\";\nimport sha1 from \"sha1\";\nimport { GroupSemaphore, Semaphore } from \"@chriscdn/promise-semaphore\";\nimport touch from \"touch\";\nimport { findNuke } from \"@chriscdn/find-nuke\";\nimport { Duration } from \"@chriscdn/duration\";\n\ntype DirectoryPath = string;\ntype FileName = string;\ntype FilePath = string;\ntype Milliseconds = number;\n\nexport type FileCacheOptions<T extends Record<string, any>> = {\n cachePath: DirectoryPath;\n autoCreateCachePath?: boolean;\n cb: (filePath: FilePath, args: T, cache: FileCache<T>) => Promise<void>;\n ext: (args: T) => string | Promise<string>;\n resolveCacheKey?: (args: T) => string | Promise<string>;\n ttl: Milliseconds;\n cleanupInterval?: Milliseconds;\n};\n\nclass FileCache<T extends Record<string, any>> {\n private _cachePath: FileCacheOptions<T>[\"cachePath\"];\n private _cb: FileCacheOptions<T>[\"cb\"];\n private _ttl: FileCacheOptions<T>[\"ttl\"];\n private _cleanupInterval: FileCacheOptions<T>[\"cleanupInterval\"];\n private _resolveCacheKey: Required<FileCacheOptions<T>>[\"resolveCacheKey\"];\n private _ext: FileCacheOptions<T>[\"ext\"];\n private _intervalId: ReturnType<typeof setInterval> | null = null;\n private _semaphore: Semaphore = new Semaphore();\n private _cleanupGroupSemaphore: GroupSemaphore = new GroupSemaphore();\n\n constructor({\n cachePath,\n autoCreateCachePath,\n cb,\n ext,\n ttl,\n cleanupInterval,\n resolveCacheKey,\n }: FileCacheOptions<T>) {\n this._cachePath = cachePath;\n this._cb = cb;\n this._ext = ext;\n this._ttl = ttl;\n this._cleanupInterval =\n cleanupInterval ?? Duration.toMilliseconds({ days: 1 });\n\n this._resolveCacheKey =\n resolveCacheKey ?? ((args: T) => JSON.stringify(args));\n\n // Startup code\n if (pathExistsSync(cachePath)) {\n // we're good\n } else if (autoCreateCachePath) {\n fs.mkdirSync(cachePath, { recursive: true });\n } else {\n throw new Error(\"💥 FileCache error: cachePath does not exist.\");\n }\n\n // fire and forget\n this.cleanup();\n\n this._intervalId = setInterval(\n this.cleanup.bind(this),\n this._cleanupInterval,\n );\n }\n\n /**\n * Resolves the filename for the cache based on the provided arguments.\n *\n * Uses the provided `resolveFileName` function if available, or falls back to\n * generating a hash from the arguments to create a unique file name.\n */\n private async resolveFileName(args: T): Promise<FileName> {\n const [name, ext] = await Promise.all([\n this._resolveCacheKey(args),\n this._ext(args),\n ]);\n\n const resolvedFileName = sha1(JSON.stringify([name, ext]));\n\n return path.format({\n name: resolvedFileName,\n ext,\n }) as FileName;\n }\n\n /**\n * Retrieves a cached file for the given arguments.\n * If the file doesn't exist, it is generated by the callback and stored.\n *\n * Concurrency-safe, ensuring no conflicts when multiple requests are made\n * for the same file.\n *\n * @param args The arguments used to determine the file's identity and generation\n * @returns The path to the cached or newly created file\n */\n async getFile(args: T): Promise<FilePath> {\n const filePath = await this.resolveFilePath(args);\n\n try {\n await Promise.all([\n this._cleanupGroupSemaphore.acquire(\"getFile\"),\n this._semaphore.acquire(filePath),\n ]);\n\n if (await pathExists(filePath)) {\n // fire and forget\n touch(filePath);\n } else {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n await this._cb(filePath, args, this);\n }\n } finally {\n this._semaphore.release(filePath);\n this._cleanupGroupSemaphore.release(\"getFile\");\n }\n\n return filePath;\n }\n\n /**\n * Resolves the full file path for the cached file based on the provided arguments.\n *\n * @param args The arguments used to resolve the file path\n * @returns The full path to the cached file\n */\n async resolveFilePath(args: T): Promise<FilePath> {\n const fileName = await this.resolveFileName(args);\n\n const filePath = path.resolve(\n this._cachePath,\n fileName[0]!,\n fileName[1]!,\n fileName[2]!,\n fileName,\n );\n\n return filePath;\n }\n\n /**\n * Stops the background cleanup process, but does not delete any cached files.\n */\n destroy() {\n if (this._intervalId) {\n clearInterval(this._intervalId);\n this._intervalId = null;\n }\n }\n\n async has(args: T): Promise<boolean> {\n const filePath = await this.resolveFilePath(args);\n return await pathExists(filePath);\n }\n\n /**\n * Runs a cleanup pass to remove files older than 1 day from the cache.\n * This is called periodically, but can be triggered manually.\n */\n async cleanup() {\n try {\n await this._cleanupGroupSemaphore.acquire(\"cleanup\");\n await findNuke(this._cachePath, {\n olderThan: this._ttl,\n verbose: true,\n deleteEmptyDirectories: true,\n });\n } finally {\n this._cleanupGroupSemaphore.release(\"cleanup\");\n }\n }\n}\n\nexport { type DirectoryPath, FileCache, type FileName, type FilePath };\n"],"mappings":";;;;;;;;;AAwBA,IAAM,YAAN,MAA+C;CAC7C;CACA;CACA;CACA;CACA;CACA;CACA,cAA6D;CAC7D,aAAgC,IAAI,UAAU;CAC9C,yBAAiD,IAAI,eAAe;CAEpE,YAAY,EACV,WACA,qBACA,IACA,KACA,KACA,iBACA,mBACsB;EACtB,KAAK,aAAa;EAClB,KAAK,MAAM;EACX,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,mBACH,mBAAmB,SAAS,eAAe,EAAE,MAAM,EAAE,CAAC;EAExD,KAAK,mBACH,qBAAqB,SAAY,KAAK,UAAU,IAAI;EAGtD,IAAI,eAAe,SAAS,GAAG,CAE/B,OAAO,IAAI,qBACT,GAAG,UAAU,WAAW,EAAE,WAAW,KAAK,CAAC;OAE3C,MAAM,IAAI,MAAM,+CAA+C;EAIjE,KAAK,QAAQ;EAEb,KAAK,cAAc,YACjB,KAAK,QAAQ,KAAK,IAAI,GACtB,KAAK,gBACP;CACF;;;;;;;CAQA,MAAc,gBAAgB,MAA4B;EACxD,MAAM,CAAC,MAAM,OAAO,MAAM,QAAQ,IAAI,CACpC,KAAK,iBAAiB,IAAI,GAC1B,KAAK,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1,22 +1,25 @@
1
1
  {
2
2
  "name": "@chriscdn/file-cache",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "File cache",
5
- "repository": "https://github.com/chriscdn/file-cache",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/chriscdn/file-cache"
8
+ },
6
9
  "author": "Christopher Meyer <chris@schwiiz.org>",
7
10
  "license": "MIT",
8
11
  "type": "module",
9
- "main": "./lib/index.js",
10
- "types": "./lib/index.d.ts",
11
- "exports": "./lib/index.js",
12
- "scripts": {
13
- "build": "tsup",
14
- "test": "vitest"
12
+ "types": "./lib/index.d.mts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./lib/index.d.mts",
16
+ "import": "./lib/index.mjs"
17
+ }
15
18
  },
16
19
  "dependencies": {
17
- "@chriscdn/duration": "^2.0.0",
18
- "@chriscdn/find-nuke": "^2.0.0",
19
- "@chriscdn/promise-semaphore": "^4.0.0",
20
+ "@chriscdn/duration": "^2.0.1",
21
+ "@chriscdn/find-nuke": "^2.0.1",
22
+ "@chriscdn/promise-semaphore": "^4.0.1",
20
23
  "path-exists": "^5.0.0",
21
24
  "sha1": "^1.1.1",
22
25
  "touch": "^3.1.1"
@@ -27,11 +30,15 @@
27
30
  "@types/temp": "^0.9.4",
28
31
  "@types/touch": "^3.1.5",
29
32
  "temp": "^0.9.4",
30
- "tsup": "^8.5.1",
31
- "typescript": "^5.9.3",
32
- "vitest": "^4.0.6"
33
+ "tsdown": "0.23.0",
34
+ "typescript": "^7.0.2",
35
+ "vitest": "^5.0.0"
33
36
  },
34
37
  "files": [
35
38
  "lib"
36
- ]
37
- }
39
+ ],
40
+ "scripts": {
41
+ "build": "tsdown",
42
+ "test": "vitest"
43
+ }
44
+ }
package/lib/index.d.ts DELETED
@@ -1,62 +0,0 @@
1
- type DirectoryPath = string;
2
- type FileName = string;
3
- type FilePath = string;
4
- type Milliseconds = number;
5
- type FileCacheOptions<T extends Record<string, any>> = {
6
- cachePath: DirectoryPath;
7
- autoCreateCachePath?: boolean;
8
- cb: (filePath: FilePath, args: T, cache: FileCache<T>) => Promise<void>;
9
- ext: (args: T) => string | Promise<string>;
10
- resolveCacheKey?: (args: T) => string | Promise<string>;
11
- ttl: Milliseconds;
12
- cleanupInterval?: Milliseconds;
13
- };
14
- declare class FileCache<T extends Record<string, any>> {
15
- private _cachePath;
16
- private _cb;
17
- private _ttl;
18
- private _cleanupInterval;
19
- private _resolveCacheKey;
20
- private _ext;
21
- private _intervalId;
22
- private _semaphore;
23
- private _cleanupGroupSemaphore;
24
- constructor({ cachePath, autoCreateCachePath, cb, ext, ttl, cleanupInterval, resolveCacheKey, }: FileCacheOptions<T>);
25
- /**
26
- * Resolves the filename for the cache based on the provided arguments.
27
- *
28
- * Uses the provided `resolveFileName` function if available, or falls back to
29
- * generating a hash from the arguments to create a unique file name.
30
- */
31
- private resolveFileName;
32
- /**
33
- * Retrieves a cached file for the given arguments.
34
- * If the file doesn't exist, it is generated by the callback and stored.
35
- *
36
- * Concurrency-safe, ensuring no conflicts when multiple requests are made
37
- * for the same file.
38
- *
39
- * @param args The arguments used to determine the file's identity and generation
40
- * @returns The path to the cached or newly created file
41
- */
42
- getFile(args: T): Promise<FilePath>;
43
- /**
44
- * Resolves the full file path for the cached file based on the provided arguments.
45
- *
46
- * @param args The arguments used to resolve the file path
47
- * @returns The full path to the cached file
48
- */
49
- resolveFilePath(args: T): Promise<FilePath>;
50
- /**
51
- * Stops the background cleanup process, but does not delete any cached files.
52
- */
53
- destroy(): void;
54
- has(args: T): Promise<boolean>;
55
- /**
56
- * Runs a cleanup pass to remove files older than 1 day from the cache.
57
- * This is called periodically, but can be triggered manually.
58
- */
59
- cleanup(): Promise<void>;
60
- }
61
-
62
- export { type DirectoryPath, FileCache, type FileCacheOptions, type FileName, type FilePath };
package/lib/index.js DELETED
@@ -1,143 +0,0 @@
1
- // src/index.ts
2
- import path from "path";
3
- import fs from "fs";
4
- import { pathExists, pathExistsSync } from "path-exists";
5
- import sha1 from "sha1";
6
- import { GroupSemaphore, Semaphore } from "@chriscdn/promise-semaphore";
7
- import touch from "touch";
8
- import { findNuke } from "@chriscdn/find-nuke";
9
- import { Duration } from "@chriscdn/duration";
10
- var FileCache = class {
11
- _cachePath;
12
- _cb;
13
- _ttl;
14
- _cleanupInterval;
15
- _resolveCacheKey;
16
- _ext;
17
- _intervalId = null;
18
- _semaphore = new Semaphore();
19
- _cleanupGroupSemaphore = new GroupSemaphore();
20
- constructor({
21
- cachePath,
22
- autoCreateCachePath,
23
- cb,
24
- ext,
25
- ttl,
26
- cleanupInterval,
27
- resolveCacheKey
28
- }) {
29
- this._cachePath = cachePath;
30
- this._cb = cb;
31
- this._ext = ext;
32
- this._ttl = ttl;
33
- this._cleanupInterval = cleanupInterval ?? Duration.toMilliseconds({ days: 1 });
34
- this._resolveCacheKey = resolveCacheKey ?? ((args) => JSON.stringify(args));
35
- if (pathExistsSync(cachePath)) {
36
- } else if (autoCreateCachePath) {
37
- fs.mkdirSync(cachePath, { recursive: true });
38
- } else {
39
- throw new Error("\u{1F4A5} FileCache error: cachePath does not exist.");
40
- }
41
- this.cleanup();
42
- this._intervalId = setInterval(
43
- this.cleanup.bind(this),
44
- this._cleanupInterval
45
- );
46
- }
47
- /**
48
- * Resolves the filename for the cache based on the provided arguments.
49
- *
50
- * Uses the provided `resolveFileName` function if available, or falls back to
51
- * generating a hash from the arguments to create a unique file name.
52
- */
53
- async resolveFileName(args) {
54
- const [name, ext] = await Promise.all([
55
- this._resolveCacheKey(args),
56
- this._ext(args)
57
- ]);
58
- const resolvedFileName = sha1(JSON.stringify([name, ext]));
59
- return path.format({
60
- name: resolvedFileName,
61
- ext
62
- });
63
- }
64
- /**
65
- * Retrieves a cached file for the given arguments.
66
- * If the file doesn't exist, it is generated by the callback and stored.
67
- *
68
- * Concurrency-safe, ensuring no conflicts when multiple requests are made
69
- * for the same file.
70
- *
71
- * @param args The arguments used to determine the file's identity and generation
72
- * @returns The path to the cached or newly created file
73
- */
74
- async getFile(args) {
75
- const filePath = await this.resolveFilePath(args);
76
- try {
77
- await Promise.all([
78
- this._cleanupGroupSemaphore.acquire("getFile"),
79
- this._semaphore.acquire(filePath)
80
- ]);
81
- if (await pathExists(filePath)) {
82
- touch(filePath);
83
- } else {
84
- await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
85
- await this._cb(filePath, args, this);
86
- }
87
- } finally {
88
- this._semaphore.release(filePath);
89
- this._cleanupGroupSemaphore.release("getFile");
90
- }
91
- return filePath;
92
- }
93
- /**
94
- * Resolves the full file path for the cached file based on the provided arguments.
95
- *
96
- * @param args The arguments used to resolve the file path
97
- * @returns The full path to the cached file
98
- */
99
- async resolveFilePath(args) {
100
- const fileName = await this.resolveFileName(args);
101
- const filePath = path.resolve(
102
- this._cachePath,
103
- fileName[0],
104
- fileName[1],
105
- fileName[2],
106
- fileName
107
- );
108
- return filePath;
109
- }
110
- /**
111
- * Stops the background cleanup process, but does not delete any cached files.
112
- */
113
- destroy() {
114
- if (this._intervalId) {
115
- clearInterval(this._intervalId);
116
- this._intervalId = null;
117
- }
118
- }
119
- async has(args) {
120
- const filePath = await this.resolveFilePath(args);
121
- return await pathExists(filePath);
122
- }
123
- /**
124
- * Runs a cleanup pass to remove files older than 1 day from the cache.
125
- * This is called periodically, but can be triggered manually.
126
- */
127
- async cleanup() {
128
- try {
129
- await this._cleanupGroupSemaphore.acquire("cleanup");
130
- await findNuke(this._cachePath, {
131
- olderThan: this._ttl,
132
- verbose: true,
133
- deleteEmptyDirectories: true
134
- });
135
- } finally {
136
- this._cleanupGroupSemaphore.release("cleanup");
137
- }
138
- }
139
- };
140
- export {
141
- FileCache
142
- };
143
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import path from \"path\";\nimport fs from \"fs\";\nimport { pathExists, pathExistsSync } from \"path-exists\";\nimport sha1 from \"sha1\";\nimport { GroupSemaphore, Semaphore } from \"@chriscdn/promise-semaphore\";\nimport touch from \"touch\";\nimport { findNuke } from \"@chriscdn/find-nuke\";\nimport { Duration } from \"@chriscdn/duration\";\n\ntype DirectoryPath = string;\ntype FileName = string;\ntype FilePath = string;\ntype Milliseconds = number;\n\nexport type FileCacheOptions<T extends Record<string, any>> = {\n cachePath: DirectoryPath;\n autoCreateCachePath?: boolean;\n cb: (filePath: FilePath, args: T, cache: FileCache<T>) => Promise<void>;\n ext: (args: T) => string | Promise<string>;\n resolveCacheKey?: (args: T) => string | Promise<string>;\n ttl: Milliseconds;\n cleanupInterval?: Milliseconds;\n};\n\nclass FileCache<T extends Record<string, any>> {\n private _cachePath: FileCacheOptions<T>[\"cachePath\"];\n private _cb: FileCacheOptions<T>[\"cb\"];\n private _ttl: FileCacheOptions<T>[\"ttl\"];\n private _cleanupInterval: FileCacheOptions<T>[\"cleanupInterval\"];\n private _resolveCacheKey: Required<\n FileCacheOptions<T>\n >[\"resolveCacheKey\"];\n private _ext: FileCacheOptions<T>[\"ext\"];\n private _intervalId: ReturnType<typeof setInterval> | null = null;\n private _semaphore: Semaphore = new Semaphore();\n private _cleanupGroupSemaphore: GroupSemaphore = new GroupSemaphore();\n\n constructor(\n {\n cachePath,\n autoCreateCachePath,\n cb,\n ext,\n ttl,\n cleanupInterval,\n resolveCacheKey,\n }: FileCacheOptions<T>,\n ) {\n this._cachePath = cachePath;\n this._cb = cb;\n this._ext = ext;\n this._ttl = ttl;\n this._cleanupInterval = cleanupInterval ??\n Duration.toMilliseconds({ days: 1 });\n\n this._resolveCacheKey = resolveCacheKey ??\n ((args: T) => (JSON.stringify(args)));\n\n // Startup code\n if ((pathExistsSync(cachePath))) {\n // we're good\n } else if (autoCreateCachePath) {\n fs.mkdirSync(cachePath, { recursive: true });\n } else {\n throw new Error(\"💥 FileCache error: cachePath does not exist.\");\n }\n\n // fire and forget\n this.cleanup();\n\n this._intervalId = setInterval(\n this.cleanup.bind(this),\n this._cleanupInterval,\n );\n }\n\n /**\n * Resolves the filename for the cache based on the provided arguments.\n *\n * Uses the provided `resolveFileName` function if available, or falls back to\n * generating a hash from the arguments to create a unique file name.\n */\n private async resolveFileName(args: T): Promise<FileName> {\n const [name, ext] = await Promise.all([\n this._resolveCacheKey(args),\n this._ext(args),\n ]);\n\n const resolvedFileName = sha1(JSON.stringify([name, ext]));\n\n return path.format({\n name: resolvedFileName,\n ext,\n }) as FileName;\n }\n\n /**\n * Retrieves a cached file for the given arguments.\n * If the file doesn't exist, it is generated by the callback and stored.\n *\n * Concurrency-safe, ensuring no conflicts when multiple requests are made\n * for the same file.\n *\n * @param args The arguments used to determine the file's identity and generation\n * @returns The path to the cached or newly created file\n */\n async getFile(args: T): Promise<FilePath> {\n const filePath = await this.resolveFilePath(args);\n\n try {\n await Promise.all([\n this._cleanupGroupSemaphore.acquire(\"getFile\"),\n this._semaphore.acquire(filePath),\n ]);\n\n if (await pathExists(filePath)) {\n // fire and forget\n touch(filePath);\n } else {\n await fs.promises.mkdir(path.dirname(filePath), { recursive: true });\n await this._cb(filePath, args, this);\n }\n } finally {\n this._semaphore.release(filePath);\n this._cleanupGroupSemaphore.release(\"getFile\");\n }\n\n return filePath;\n }\n\n /**\n * Resolves the full file path for the cached file based on the provided arguments.\n *\n * @param args The arguments used to resolve the file path\n * @returns The full path to the cached file\n */\n async resolveFilePath(args: T): Promise<FilePath> {\n const fileName = await this.resolveFileName(args);\n\n const filePath = path.resolve(\n this._cachePath,\n fileName[0]!,\n fileName[1]!,\n fileName[2]!,\n fileName,\n );\n\n return filePath;\n }\n\n /**\n * Stops the background cleanup process, but does not delete any cached files.\n */\n destroy() {\n if (this._intervalId) {\n clearInterval(this._intervalId);\n this._intervalId = null;\n }\n }\n\n async has(args: T): Promise<boolean> {\n const filePath = await this.resolveFilePath(args);\n return await pathExists(filePath);\n }\n\n /**\n * Runs a cleanup pass to remove files older than 1 day from the cache.\n * This is called periodically, but can be triggered manually.\n */\n async cleanup() {\n try {\n await this._cleanupGroupSemaphore.acquire(\"cleanup\");\n await findNuke(this._cachePath, {\n olderThan: this._ttl,\n verbose: true,\n deleteEmptyDirectories: true,\n });\n } finally {\n this._cleanupGroupSemaphore.release(\"cleanup\");\n }\n }\n}\n\nexport { type DirectoryPath, FileCache, type FileName, type FilePath };\n"],"mappings":";AAAA,OAAO,UAAU;AACjB,OAAO,QAAQ;AACf,SAAS,YAAY,sBAAsB;AAC3C,OAAO,UAAU;AACjB,SAAS,gBAAgB,iBAAiB;AAC1C,OAAO,WAAW;AAClB,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AAiBzB,IAAM,YAAN,MAA+C;AAAA,EACrC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAGA;AAAA,EACA,cAAqD;AAAA,EACrD,aAAwB,IAAI,UAAU;AAAA,EACtC,yBAAyC,IAAI,eAAe;AAAA,EAEpE,YACE;AAAA,IACE;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GACA;AACA,SAAK,aAAa;AAClB,SAAK,MAAM;AACX,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,mBAAmB,mBACtB,SAAS,eAAe,EAAE,MAAM,EAAE,CAAC;AAErC,SAAK,mBAAmB,oBACrB,CAAC,SAAa,KAAK,UAAU,IAAI;AAGpC,QAAK,eAAe,SAAS,GAAI;AAAA,IAEjC,WAAW,qBAAqB;AAC9B,SAAG,UAAU,WAAW,EAAE,WAAW,KAAK,CAAC;AAAA,IAC7C,OAAO;AACL,YAAM,IAAI,MAAM,sDAA+C;AAAA,IACjE;AAGA,SAAK,QAAQ;AAEb,SAAK,cAAc;AAAA,MACjB,KAAK,QAAQ,KAAK,IAAI;AAAA,MACtB,KAAK;AAAA,IACP;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAc,gBAAgB,MAA4B;AACxD,UAAM,CAAC,MAAM,GAAG,IAAI,MAAM,QAAQ,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