@resolid/cache-file 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present, Resolid Tech
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,10 @@
1
+ # File Cache store for @resolid/cache
2
+
3
+ ![GitHub License](https://img.shields.io/github/license/resolid/framework)
4
+ ![NPM Version](https://img.shields.io/npm/v/%40resolid/cache-file)
5
+
6
+ <b>[Documentation](https://www.resolid.tech/docs/cache)</b> | [Framework Bundle](https://github.com/resolid/framework)
7
+
8
+ ## License
9
+
10
+ MIT License (MIT). Please see [LICENSE](./LICENSE) for more information.
@@ -0,0 +1,17 @@
1
+ import { CacheStore } from "@resolid/cache/types";
2
+
3
+ //#region src/index.d.ts
4
+ declare class FileCache implements CacheStore {
5
+ private readonly _basePath;
6
+ private readonly _locks;
7
+ constructor(basePath: string);
8
+ private _resolve;
9
+ private _exists;
10
+ private _lockedRun;
11
+ get(key: string): Promise<string | undefined>;
12
+ set(key: string, value: string, ttl?: number): Promise<boolean>;
13
+ del(key: string): Promise<boolean>;
14
+ clear(): Promise<boolean>;
15
+ }
16
+ //#endregion
17
+ export { FileCache };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{access as e,mkdir as t,readFile as n,rm as r,writeFile as i}from"node:fs/promises";import{dirname as a,join as o}from"node:path";const s=/(\.\/|\.\.\/)/;var c=class{_basePath;_locks=new Map;constructor(e){this._basePath=e}_resolve(e){if(s.test(e))throw Error(`Invalid key: ${e}. Should not contain relative paths.`);return o(this._basePath,e.replaceAll(`:`,`/`))}async _exists(t){try{return await e(t),!0}catch{return!1}}async _lockedRun(e,t){let n=this._locks.get(e)??Promise.resolve(),r,i=new Promise(e=>r=e);this._locks.set(e,n.then(()=>i));try{return await n,await t()}finally{r(),this._locks.get(e)===i&&this._locks.delete(e)}}async get(e){let t=this._resolve(e);try{let e=await n(t,{encoding:`utf-8`}),[i,a]=JSON.parse(e);if(a!==-1&&a<Date.now()){await r(t,{force:!0});return}return i}catch{return}}async set(e,n,r){let o=this._resolve(e);return this._lockedRun(e,async()=>(await t(a(o),{recursive:!0}),await i(o,JSON.stringify([n,r?Date.now()+r*1e3:-1])),!0))}async del(e){let t=this._resolve(e);return await this._exists(t)?this._lockedRun(e,async()=>(await r(t),!0)):!1}async clear(){return await r(this._basePath,{recursive:!0,force:!0}),this._locks.clear(),!0}};export{c as FileCache};
package/package.json ADDED
@@ -0,0 +1,57 @@
1
+ {
2
+ "name": "@resolid/cache-file",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "File Cache store for @resolid/cache",
6
+ "keywords": [
7
+ "resolid",
8
+ "cache",
9
+ "cache manager",
10
+ "file"
11
+ ],
12
+ "homepage": "https://www.resolid.tech",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/resolid/framework.git",
16
+ "directory": "packages/cache-file"
17
+ },
18
+ "license": "MIT",
19
+ "author": {
20
+ "name": "Huijie Wei",
21
+ "email": "hello@resolid.tech"
22
+ },
23
+ "sideEffects": false,
24
+ "type": "module",
25
+ "exports": {
26
+ ".": {
27
+ "types": "./dist/index.d.mts",
28
+ "import": "./dist/index.mjs"
29
+ }
30
+ },
31
+ "main": "./dist/index.mjs",
32
+ "types": "./dist/index.d.mts",
33
+ "files": [
34
+ "dist"
35
+ ],
36
+ "scripts": {
37
+ "build": "tsdown",
38
+ "lint": "eslint .",
39
+ "test": "vitest run",
40
+ "typecheck": "tsc --noEmit"
41
+ },
42
+ "dependencies": {},
43
+ "devDependencies": {
44
+ "tsdown": "^0.16.3",
45
+ "typescript": "^5.9.3"
46
+ },
47
+ "peerDependencies": {
48
+ "@resolid/cache": "workspace:^"
49
+ },
50
+ "engines": {
51
+ "node": "^20.19.0 || ^22.13.0 || >=24"
52
+ },
53
+ "publishConfig": {
54
+ "access": "public",
55
+ "provenance": true
56
+ }
57
+ }