@kisaragi-hiu/cached-fetch 0.1.2 → 0.3.0
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.
- package/index.d.ts +2 -17
- package/index.js +25 -11
- package/package.json +2 -1
package/index.d.ts
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
export declare function cached(
|
|
5
|
-
/**
|
|
6
|
-
* The key of the value.
|
|
7
|
-
* Subsequent calls with the same key will return the cached value.
|
|
8
|
-
*/
|
|
9
|
-
key: string,
|
|
10
|
-
/**
|
|
11
|
-
* A fetcher function, called when the value isn't cached.
|
|
12
|
-
*
|
|
13
|
-
* Should return a Promise<{ text: () => string | Promise<string> }>. This is
|
|
14
|
-
* usually a Response, but ProcessOutput from the zx library also fits
|
|
15
|
-
* this signature.
|
|
16
|
-
*/
|
|
17
|
-
fetcher: () => Promise<{
|
|
1
|
+
export declare function cached(key: string, fetcher: () => string): string;
|
|
2
|
+
export declare function cached(key: string, fetcher: () => Promise<string> | Promise<{
|
|
18
3
|
text: () => string | Promise<string>;
|
|
19
4
|
}>): Promise<string>;
|
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// -*- lsp-disabled-clients: (ts-ls); -*-
|
|
2
|
-
import { mkdirSync, writeFileSync, readFileSync
|
|
2
|
+
import { mkdirSync, writeFileSync, readFileSync } from "node:fs";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
/**
|
|
6
6
|
* A cache designed to cache some text.
|
|
7
7
|
*/
|
|
8
|
-
export
|
|
8
|
+
export function cached(
|
|
9
9
|
/**
|
|
10
10
|
* The key of the value.
|
|
11
11
|
* Subsequent calls with the same key will return the cached value.
|
|
@@ -14,19 +14,33 @@ key,
|
|
|
14
14
|
/**
|
|
15
15
|
* A fetcher function, called when the value isn't cached.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
17
|
+
* This can return a string or a Promise<string>.
|
|
18
|
+
*
|
|
19
|
+
* This can also return a Promise<{ text: () => string | Promise<string> }>.
|
|
20
|
+
* This means a Response or zx's ProcessOutput can be used directly.
|
|
20
21
|
*/
|
|
21
22
|
fetcher) {
|
|
22
23
|
const cacheFile = join(tmpdir(), key);
|
|
23
|
-
|
|
24
|
-
const text = cacheHit
|
|
25
|
-
? readFileSync(cacheFile, { encoding: "utf-8" })
|
|
26
|
-
: await (await fetcher()).text();
|
|
27
|
-
if (!cacheHit) {
|
|
24
|
+
function saveCache(text) {
|
|
28
25
|
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
29
26
|
writeFileSync(cacheFile, text, { encoding: "utf-8" });
|
|
27
|
+
return text;
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
return readFileSync(cacheFile, { encoding: "utf-8" });
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
if (e.code !== "ENOENT") {
|
|
34
|
+
throw e;
|
|
35
|
+
}
|
|
36
|
+
const result = fetcher();
|
|
37
|
+
if (typeof result === "string") {
|
|
38
|
+
return saveCache(result);
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return result.then(async (v) => {
|
|
42
|
+
return saveCache(typeof v === "string" ? v : await v.text());
|
|
43
|
+
});
|
|
44
|
+
}
|
|
30
45
|
}
|
|
31
|
-
return text;
|
|
32
46
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kisaragi-hiu/cached-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A cache in OS tmpdir useful for fetch()",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"@typescript/native-preview": "^7.0.0-dev.20250822.1"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
|
+
"test": "deno test -A",
|
|
22
23
|
"build": "tsgo index.ts --noCheck -d -t esnext -m nodenext"
|
|
23
24
|
}
|
|
24
25
|
}
|