@kisaragi-hiu/cached-fetch 0.2.0 → 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.
Files changed (3) hide show
  1. package/index.d.ts +2 -18
  2. package/index.js +21 -13
  3. package/package.json +2 -1
package/index.d.ts CHANGED
@@ -1,20 +1,4 @@
1
- /**
2
- * A cache designed to cache some text.
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, existsSync } from "node:fs";
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 async function cached(
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
- const cacheHit = existsSync(cacheFile);
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.2.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
  }