@kisaragi-hiu/cached-fetch 0.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.
- package/LICENSE +18 -0
- package/README.md +50 -0
- package/index.ts +32 -0
- package/package.json +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kisaragi Hiu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
|
6
|
+
associated documentation files (the "Software"), to deal in the Software without restriction, including
|
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
12
|
+
portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
15
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
16
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
18
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# @kisaragi-hiu/cached-fetch
|
|
2
|
+
|
|
3
|
+
A cacheing function designed for cacheing fetch GET responses on disk in a command line program.
|
|
4
|
+
|
|
5
|
+
I keep rewriting this logic in multiple projects, so I'm making it a package.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { cached } from "@kisaragi-hiu/cached-fetch";
|
|
11
|
+
|
|
12
|
+
const text = await cached(
|
|
13
|
+
"myCacheKey",
|
|
14
|
+
() => fetch("https://example.com")
|
|
15
|
+
)
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This persists the text response in `os.tmpdir` and should stay around until the next reboot.
|
|
19
|
+
|
|
20
|
+
Arguments:
|
|
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
|
+
```
|
|
37
|
+
|
|
38
|
+
## Bonus usage
|
|
39
|
+
|
|
40
|
+
This also works:
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { $ } from "zx";
|
|
44
|
+
import { cached } from "@kisaragi-hiu/cached-fetch";
|
|
45
|
+
|
|
46
|
+
const text = await cached(
|
|
47
|
+
"myCacheKey",
|
|
48
|
+
() => $`sleep 1 && echo "demo for a slow process"`
|
|
49
|
+
)
|
|
50
|
+
```
|
package/index.ts
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
|
+
/**
|
|
7
|
+
* A cache designed to cache some text.
|
|
8
|
+
*/
|
|
9
|
+
export async function cached(
|
|
10
|
+
/**
|
|
11
|
+
* The key of the value.
|
|
12
|
+
* Subsequent calls with the same key will return the cached value.
|
|
13
|
+
*/
|
|
14
|
+
key: string,
|
|
15
|
+
/**
|
|
16
|
+
* A fetcher function, called when the value isn't cached.
|
|
17
|
+
*
|
|
18
|
+
* Should return a Promise<{ text: () => Promise<string> }>. This is usually a
|
|
19
|
+
* Response, but the ProcessPromise from the zx library also fits this signature.
|
|
20
|
+
*/
|
|
21
|
+
fetcher: () => Promise<{ text: () => Promise<string> }>
|
|
22
|
+
) {
|
|
23
|
+
const cacheFile = join(tmpdir(), key);
|
|
24
|
+
const cacheHit = existsSync(cacheFile);
|
|
25
|
+
const text = cacheHit
|
|
26
|
+
? readFileSync(cacheFile, { encoding: "utf-8" })
|
|
27
|
+
: await (await fetcher()).text();
|
|
28
|
+
if (!cacheHit) {
|
|
29
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
30
|
+
writeFileSync(cacheFile, text, { encoding: "utf-8" });
|
|
31
|
+
}
|
|
32
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kisaragi-hiu/cached-fetch",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A cache in OS tmpdir useful for fetch()",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/kisaragi-hiu/cached-fetch.git"
|
|
9
|
+
},
|
|
10
|
+
"funding": {
|
|
11
|
+
"type": "buymeacoffee",
|
|
12
|
+
"url": "https://www.buymeacoffee.com/kisaragihiu"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/kisaragi-hiu/cached-fetch",
|
|
15
|
+
"author": "Kisaragi Hiu <mail@kisaragi-hiu.com>",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"files": ["index.ts"]
|
|
18
|
+
}
|