@kisaragi-hiu/cached-fetch 0.2.0 → 0.3.4
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/README.md +24 -29
- package/index.d.ts +2 -18
- package/index.js +21 -13
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -17,34 +17,29 @@ const text = await cached(
|
|
|
17
17
|
|
|
18
18
|
This persists the text response in `os.tmpdir` and should stay around until the next reboot.
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
/**
|
|
24
|
-
* The key of the value.
|
|
25
|
-
* Subsequent calls with the same key will return the cached value.
|
|
26
|
-
*/
|
|
27
|
-
key: string,
|
|
28
|
-
/**
|
|
29
|
-
* A fetcher function, called when the value isn't cached.
|
|
30
|
-
*
|
|
31
|
-
* Should return a promise, which resolves to an object whose text key is a
|
|
32
|
-
* Promise<string> --- usually a Response, but the ProcessPromise from the
|
|
33
|
-
* zx library are also supported.
|
|
34
|
-
*/
|
|
35
|
-
fetcher: () => Promise<{ text: () => Promise<string> }>
|
|
36
|
-
```
|
|
20
|
+
Signature: `cached(key, fetcher)`
|
|
37
21
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
This also works:
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
import { $ } from "zx";
|
|
44
|
-
import { cached } from "@kisaragi-hiu/cached-fetch";
|
|
22
|
+
Arguments:
|
|
45
23
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
() =>
|
|
49
|
-
|
|
50
|
-
|
|
24
|
+
- `key`: the key of the value. Subsequent calls with the same key will return the cached value.
|
|
25
|
+
- `fetcher`: a function doing work that should be cached. This should return any of these:
|
|
26
|
+
- `Promise<{text: () => string | Promise<string>}`, a promise of an object whose “text” property is a function returning a string or a promise of a string.
|
|
27
|
+
|
|
28
|
+
This covers both a `Promise<Response>` from `fetch`, and also `ProcessPromise` from `zx`. So this works:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
const value = await cached("key1", () => fetch("https://example.com"))
|
|
32
|
+
|
|
33
|
+
import {$} from "zx"
|
|
34
|
+
const output = await cached("key2", () => $`sleep 1 && echo "slow process demo"`)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
- a `Promise<string>`
|
|
38
|
+
|
|
39
|
+
- a string. In this case the whole call is sync.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const output = cached("key4", () =>
|
|
43
|
+
spawnSync("ls", { stdio: "pipe", encoding: "utf-8" }).stdout,
|
|
44
|
+
);
|
|
45
|
+
```
|
package/index.d.ts
CHANGED
|
@@ -1,20 +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
|
-
* This can return a string or a Promise<string>.
|
|
14
|
-
*
|
|
15
|
-
* This can also return a Promise<{ text: () => string | Promise<string> }>.
|
|
16
|
-
* This means a Response or zx's ProcessOutput can be used directly.
|
|
17
|
-
*/
|
|
18
|
-
fetcher: () => string | Promise<string> | Promise<{
|
|
1
|
+
export declare function cached(key: string, fetcher: () => string): string;
|
|
2
|
+
export declare function cached(key: string, fetcher: () => Promise<string> | Promise<{
|
|
19
3
|
text: () => string | Promise<string>;
|
|
20
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.
|
|
@@ -21,18 +21,26 @@ key,
|
|
|
21
21
|
*/
|
|
22
22
|
fetcher) {
|
|
23
23
|
const cacheFile = join(tmpdir(), key);
|
|
24
|
-
|
|
25
|
-
let text;
|
|
26
|
-
if (cacheHit) {
|
|
27
|
-
text = readFileSync(cacheFile, { encoding: "utf-8" });
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
const result = await fetcher();
|
|
31
|
-
text = typeof result === "string" ? result : await result.text();
|
|
32
|
-
}
|
|
33
|
-
if (!cacheHit) {
|
|
24
|
+
function saveCache(text) {
|
|
34
25
|
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
35
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
|
+
}
|
|
36
45
|
}
|
|
37
|
-
return text;
|
|
38
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.4",
|
|
4
4
|
"description": "A cache in OS tmpdir useful for fetch()",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -15,10 +15,13 @@
|
|
|
15
15
|
"author": "Kisaragi Hiu <mail@kisaragi-hiu.com>",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"files": ["index.js", "index.d.ts"],
|
|
18
|
+
"main": "index.js",
|
|
19
|
+
"types": "index.d.ts",
|
|
18
20
|
"devDependencies": {
|
|
19
21
|
"@typescript/native-preview": "^7.0.0-dev.20250822.1"
|
|
20
22
|
},
|
|
21
23
|
"scripts": {
|
|
24
|
+
"test": "deno test -A",
|
|
22
25
|
"build": "tsgo index.ts --noCheck -d -t esnext -m nodenext"
|
|
23
26
|
}
|
|
24
27
|
}
|