@kisaragi-hiu/cached-fetch 0.0.3 → 0.1.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.
- package/index.js +32 -0
- package/package.json +6 -4
- package/index.ts +0 -32
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// -*- lsp-disabled-clients: (ts-ls); -*-
|
|
2
|
+
import { mkdirSync, writeFileSync, readFileSync, existsSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
/**
|
|
6
|
+
* A cache designed to cache some text.
|
|
7
|
+
*/
|
|
8
|
+
export async function cached(
|
|
9
|
+
/**
|
|
10
|
+
* The key of the value.
|
|
11
|
+
* Subsequent calls with the same key will return the cached value.
|
|
12
|
+
*/
|
|
13
|
+
key,
|
|
14
|
+
/**
|
|
15
|
+
* A fetcher function, called when the value isn't cached.
|
|
16
|
+
*
|
|
17
|
+
* Should return a Promise<{ text: () => string | Promise<string> }>. This is
|
|
18
|
+
* usually a Response, but ProcessOutput from the zx library also fits
|
|
19
|
+
* this signature.
|
|
20
|
+
*/
|
|
21
|
+
fetcher) {
|
|
22
|
+
const cacheFile = join(tmpdir(), key);
|
|
23
|
+
const cacheHit = existsSync(cacheFile);
|
|
24
|
+
const text = cacheHit
|
|
25
|
+
? readFileSync(cacheFile, { encoding: "utf-8" })
|
|
26
|
+
: await (await fetcher()).text();
|
|
27
|
+
if (!cacheHit) {
|
|
28
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
29
|
+
writeFileSync(cacheFile, text, { encoding: "utf-8" });
|
|
30
|
+
}
|
|
31
|
+
return text;
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kisaragi-hiu/cached-fetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A cache in OS tmpdir useful for fetch()",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -14,9 +14,11 @@
|
|
|
14
14
|
"homepage": "https://github.com/kisaragi-hiu/cached-fetch",
|
|
15
15
|
"author": "Kisaragi Hiu <mail@kisaragi-hiu.com>",
|
|
16
16
|
"license": "MIT",
|
|
17
|
-
"files": ["index.
|
|
18
|
-
"exports": "./index.ts",
|
|
17
|
+
"files": ["index.js"],
|
|
19
18
|
"devDependencies": {
|
|
20
|
-
"@
|
|
19
|
+
"@typescript/native-preview": "^7.0.0-dev.20250822.1"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsgo index.ts --noCheck -d -t esnext -m nodenext"
|
|
21
23
|
}
|
|
22
24
|
}
|
package/index.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { mkdirSync, writeFileSync, readFileSync, existsSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { tmpdir } from "node:os";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* A cache designed to cache some text.
|
|
7
|
-
*/
|
|
8
|
-
export async function cached(
|
|
9
|
-
/**
|
|
10
|
-
* The key of the value.
|
|
11
|
-
* Subsequent calls with the same key will return the cached value.
|
|
12
|
-
*/
|
|
13
|
-
key: string,
|
|
14
|
-
/**
|
|
15
|
-
* A fetcher function, called when the value isn't cached.
|
|
16
|
-
*
|
|
17
|
-
* Should return a Promise<{ text: () => Promise<string> }>. This is usually a
|
|
18
|
-
* Response, but the ProcessPromise from the zx library also fits this signature.
|
|
19
|
-
*/
|
|
20
|
-
fetcher: () => Promise<{ text: () => Promise<string> }>
|
|
21
|
-
): Promise<string> {
|
|
22
|
-
const cacheFile = join(tmpdir(), key);
|
|
23
|
-
const cacheHit = existsSync(cacheFile);
|
|
24
|
-
const text = cacheHit
|
|
25
|
-
? readFileSync(cacheFile, { encoding: "utf-8" })
|
|
26
|
-
: await (await fetcher()).text();
|
|
27
|
-
if (!cacheHit) {
|
|
28
|
-
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
29
|
-
writeFileSync(cacheFile, text, { encoding: "utf-8" });
|
|
30
|
-
}
|
|
31
|
-
return text;
|
|
32
|
-
}
|