@melledijkstra/storage 1.0.0 → 1.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/dist/index.cjs +114 -4
- package/dist/index.d.cts +24 -8
- package/dist/index.d.mts +24 -8
- package/dist/index.mjs +84 -5
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
1
28
|
let _melledijkstra_toolbox = require("@melledijkstra/toolbox");
|
|
29
|
+
let node_fs = require("node:fs");
|
|
30
|
+
node_fs = __toESM(node_fs);
|
|
31
|
+
let node_path = require("node:path");
|
|
32
|
+
node_path = __toESM(node_path);
|
|
33
|
+
let node_os = require("node:os");
|
|
34
|
+
node_os = __toESM(node_os);
|
|
2
35
|
|
|
3
36
|
//#region src/memory.ts
|
|
4
37
|
const logger = new _melledijkstra_toolbox.Logger("cache");
|
|
@@ -44,7 +77,7 @@ function withCache(originalFunc, options = {}) {
|
|
|
44
77
|
var MemoryCache = class {
|
|
45
78
|
logger = new _melledijkstra_toolbox.Logger("MemoryCache");
|
|
46
79
|
_cache = {};
|
|
47
|
-
get(key) {
|
|
80
|
+
async get(key) {
|
|
48
81
|
const cachedItem = this._cache[key];
|
|
49
82
|
if (!cachedItem) return;
|
|
50
83
|
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) delete this._cache[key];
|
|
@@ -72,7 +105,7 @@ var MemoryCache = class {
|
|
|
72
105
|
}
|
|
73
106
|
return false;
|
|
74
107
|
}
|
|
75
|
-
has(key) {
|
|
108
|
+
async has(key) {
|
|
76
109
|
const cachedItem = this._cache[key];
|
|
77
110
|
if (!cachedItem) return false;
|
|
78
111
|
if (this.isExpired(key)) return false;
|
|
@@ -82,15 +115,92 @@ var MemoryCache = class {
|
|
|
82
115
|
}
|
|
83
116
|
return true;
|
|
84
117
|
}
|
|
85
|
-
keys() {
|
|
118
|
+
async keys() {
|
|
86
119
|
return Object.keys(this._cache);
|
|
87
120
|
}
|
|
88
|
-
size() {
|
|
121
|
+
async size() {
|
|
89
122
|
return Object.keys(this._cache).length;
|
|
90
123
|
}
|
|
91
124
|
};
|
|
92
125
|
|
|
93
126
|
//#endregion
|
|
127
|
+
//#region src/file.ts
|
|
128
|
+
var FileStorage = class {
|
|
129
|
+
filePath;
|
|
130
|
+
constructor(filePath) {
|
|
131
|
+
if (filePath) this.filePath = filePath.startsWith("~") ? node_path.join(node_os.homedir(), filePath.slice(1)) : node_path.resolve(filePath);
|
|
132
|
+
else this.filePath = node_path.join(node_os.homedir(), ".toolbox-storage.json");
|
|
133
|
+
}
|
|
134
|
+
readStore() {
|
|
135
|
+
if (!node_fs.existsSync(this.filePath)) return {};
|
|
136
|
+
try {
|
|
137
|
+
const data = node_fs.readFileSync(this.filePath, "utf-8");
|
|
138
|
+
return JSON.parse(data);
|
|
139
|
+
} catch {
|
|
140
|
+
return {};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
writeStore(store) {
|
|
144
|
+
const dir = node_path.dirname(this.filePath);
|
|
145
|
+
if (!node_fs.existsSync(dir)) node_fs.mkdirSync(dir, { recursive: true });
|
|
146
|
+
const fileExists = node_fs.existsSync(this.filePath);
|
|
147
|
+
node_fs.writeFileSync(this.filePath, JSON.stringify(store, null, 2), "utf-8");
|
|
148
|
+
if (!fileExists && node_os.platform() !== "win32") try {
|
|
149
|
+
node_fs.chmodSync(this.filePath, 384);
|
|
150
|
+
} catch {}
|
|
151
|
+
}
|
|
152
|
+
isExpired(item) {
|
|
153
|
+
if (item.ttl === Infinity || item.ttl === null || item.ttl === void 0 || String(item.ttl) === "Infinity") return false;
|
|
154
|
+
return Date.now() - item.timestamp > item.ttl;
|
|
155
|
+
}
|
|
156
|
+
async get(key) {
|
|
157
|
+
const item = this.readStore()[key];
|
|
158
|
+
if (!item) return;
|
|
159
|
+
if (this.isExpired(item)) {
|
|
160
|
+
await this.delete(key);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
return item.data;
|
|
164
|
+
}
|
|
165
|
+
async set(key, value, ttl = Infinity) {
|
|
166
|
+
const store = this.readStore();
|
|
167
|
+
store[key] = {
|
|
168
|
+
data: value,
|
|
169
|
+
timestamp: Date.now(),
|
|
170
|
+
ttl
|
|
171
|
+
};
|
|
172
|
+
this.writeStore(store);
|
|
173
|
+
}
|
|
174
|
+
async delete(key) {
|
|
175
|
+
const store = this.readStore();
|
|
176
|
+
if (key in store) {
|
|
177
|
+
delete store[key];
|
|
178
|
+
this.writeStore(store);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
async clear() {
|
|
182
|
+
this.writeStore({});
|
|
183
|
+
}
|
|
184
|
+
async has(key) {
|
|
185
|
+
return await this.get(key) !== void 0;
|
|
186
|
+
}
|
|
187
|
+
async keys() {
|
|
188
|
+
const store = this.readStore();
|
|
189
|
+
const activeKeys = [];
|
|
190
|
+
for (const key of Object.keys(store)) {
|
|
191
|
+
const item = store[key];
|
|
192
|
+
if (item && !this.isExpired(item)) activeKeys.push(key);
|
|
193
|
+
else if (item) await this.delete(key);
|
|
194
|
+
}
|
|
195
|
+
return activeKeys;
|
|
196
|
+
}
|
|
197
|
+
async size() {
|
|
198
|
+
return (await this.keys()).length;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
//#endregion
|
|
203
|
+
exports.FileStorage = FileStorage;
|
|
94
204
|
exports.MIN_1 = MIN_1;
|
|
95
205
|
exports.MIN_10 = MIN_10;
|
|
96
206
|
exports.MIN_15 = MIN_15;
|
package/dist/index.d.cts
CHANGED
|
@@ -36,17 +36,17 @@ interface IStorage {
|
|
|
36
36
|
* @param key - Storage key
|
|
37
37
|
* @returns True if key exists, false otherwise
|
|
38
38
|
*/
|
|
39
|
-
has(key: string): boolean
|
|
39
|
+
has(key: string): Promise<boolean>;
|
|
40
40
|
/**
|
|
41
41
|
* Get all keys stored in storage
|
|
42
42
|
* @returns Array of all keys
|
|
43
43
|
*/
|
|
44
|
-
keys(): string[]
|
|
44
|
+
keys(): Promise<string[]>;
|
|
45
45
|
/**
|
|
46
46
|
* Get the number of items in storage
|
|
47
47
|
* @returns Number of stored items
|
|
48
48
|
*/
|
|
49
|
-
size(): number
|
|
49
|
+
size(): Promise<number>;
|
|
50
50
|
}
|
|
51
51
|
//#endregion
|
|
52
52
|
//#region src/memory.d.ts
|
|
@@ -70,14 +70,30 @@ declare function withCache<T, A extends unknown[]>(originalFunc: (...args: A) =>
|
|
|
70
70
|
declare class MemoryCache implements IStorage {
|
|
71
71
|
logger: Logger;
|
|
72
72
|
private _cache;
|
|
73
|
-
get<T>(key: string): T | undefined
|
|
73
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
74
74
|
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
75
75
|
delete(key: string): Promise<void>;
|
|
76
76
|
clear(): Promise<void>;
|
|
77
77
|
isExpired(key: string): boolean;
|
|
78
|
-
has(key: string): boolean
|
|
79
|
-
keys(): string[]
|
|
80
|
-
size(): number
|
|
78
|
+
has(key: string): Promise<boolean>;
|
|
79
|
+
keys(): Promise<string[]>;
|
|
80
|
+
size(): Promise<number>;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/file.d.ts
|
|
84
|
+
declare class FileStorage implements IStorage {
|
|
85
|
+
private filePath;
|
|
86
|
+
constructor(filePath?: string);
|
|
87
|
+
private readStore;
|
|
88
|
+
private writeStore;
|
|
89
|
+
private isExpired;
|
|
90
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
91
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
92
|
+
delete(key: string): Promise<void>;
|
|
93
|
+
clear(): Promise<void>;
|
|
94
|
+
has(key: string): Promise<boolean>;
|
|
95
|
+
keys(): Promise<string[]>;
|
|
96
|
+
size(): Promise<number>;
|
|
81
97
|
}
|
|
82
98
|
//#endregion
|
|
83
|
-
export { IStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
|
99
|
+
export { FileStorage, IStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
package/dist/index.d.mts
CHANGED
|
@@ -36,17 +36,17 @@ interface IStorage {
|
|
|
36
36
|
* @param key - Storage key
|
|
37
37
|
* @returns True if key exists, false otherwise
|
|
38
38
|
*/
|
|
39
|
-
has(key: string): boolean
|
|
39
|
+
has(key: string): Promise<boolean>;
|
|
40
40
|
/**
|
|
41
41
|
* Get all keys stored in storage
|
|
42
42
|
* @returns Array of all keys
|
|
43
43
|
*/
|
|
44
|
-
keys(): string[]
|
|
44
|
+
keys(): Promise<string[]>;
|
|
45
45
|
/**
|
|
46
46
|
* Get the number of items in storage
|
|
47
47
|
* @returns Number of stored items
|
|
48
48
|
*/
|
|
49
|
-
size(): number
|
|
49
|
+
size(): Promise<number>;
|
|
50
50
|
}
|
|
51
51
|
//#endregion
|
|
52
52
|
//#region src/memory.d.ts
|
|
@@ -70,14 +70,30 @@ declare function withCache<T, A extends unknown[]>(originalFunc: (...args: A) =>
|
|
|
70
70
|
declare class MemoryCache implements IStorage {
|
|
71
71
|
logger: Logger;
|
|
72
72
|
private _cache;
|
|
73
|
-
get<T>(key: string): T | undefined
|
|
73
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
74
74
|
set(key: string, value: unknown, ttl?: number): Promise<void>;
|
|
75
75
|
delete(key: string): Promise<void>;
|
|
76
76
|
clear(): Promise<void>;
|
|
77
77
|
isExpired(key: string): boolean;
|
|
78
|
-
has(key: string): boolean
|
|
79
|
-
keys(): string[]
|
|
80
|
-
size(): number
|
|
78
|
+
has(key: string): Promise<boolean>;
|
|
79
|
+
keys(): Promise<string[]>;
|
|
80
|
+
size(): Promise<number>;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/file.d.ts
|
|
84
|
+
declare class FileStorage implements IStorage {
|
|
85
|
+
private filePath;
|
|
86
|
+
constructor(filePath?: string);
|
|
87
|
+
private readStore;
|
|
88
|
+
private writeStore;
|
|
89
|
+
private isExpired;
|
|
90
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
91
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
92
|
+
delete(key: string): Promise<void>;
|
|
93
|
+
clear(): Promise<void>;
|
|
94
|
+
has(key: string): Promise<boolean>;
|
|
95
|
+
keys(): Promise<string[]>;
|
|
96
|
+
size(): Promise<number>;
|
|
81
97
|
}
|
|
82
98
|
//#endregion
|
|
83
|
-
export { IStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
|
99
|
+
export { FileStorage, IStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Logger } from "@melledijkstra/toolbox";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import * as os from "node:os";
|
|
2
5
|
|
|
3
6
|
//#region src/memory.ts
|
|
4
7
|
const logger = new Logger("cache");
|
|
@@ -44,7 +47,7 @@ function withCache(originalFunc, options = {}) {
|
|
|
44
47
|
var MemoryCache = class {
|
|
45
48
|
logger = new Logger("MemoryCache");
|
|
46
49
|
_cache = {};
|
|
47
|
-
get(key) {
|
|
50
|
+
async get(key) {
|
|
48
51
|
const cachedItem = this._cache[key];
|
|
49
52
|
if (!cachedItem) return;
|
|
50
53
|
if (Date.now() - cachedItem.timestamp > cachedItem.ttl) delete this._cache[key];
|
|
@@ -72,7 +75,7 @@ var MemoryCache = class {
|
|
|
72
75
|
}
|
|
73
76
|
return false;
|
|
74
77
|
}
|
|
75
|
-
has(key) {
|
|
78
|
+
async has(key) {
|
|
76
79
|
const cachedItem = this._cache[key];
|
|
77
80
|
if (!cachedItem) return false;
|
|
78
81
|
if (this.isExpired(key)) return false;
|
|
@@ -82,13 +85,89 @@ var MemoryCache = class {
|
|
|
82
85
|
}
|
|
83
86
|
return true;
|
|
84
87
|
}
|
|
85
|
-
keys() {
|
|
88
|
+
async keys() {
|
|
86
89
|
return Object.keys(this._cache);
|
|
87
90
|
}
|
|
88
|
-
size() {
|
|
91
|
+
async size() {
|
|
89
92
|
return Object.keys(this._cache).length;
|
|
90
93
|
}
|
|
91
94
|
};
|
|
92
95
|
|
|
93
96
|
//#endregion
|
|
94
|
-
|
|
97
|
+
//#region src/file.ts
|
|
98
|
+
var FileStorage = class {
|
|
99
|
+
filePath;
|
|
100
|
+
constructor(filePath) {
|
|
101
|
+
if (filePath) this.filePath = filePath.startsWith("~") ? path.join(os.homedir(), filePath.slice(1)) : path.resolve(filePath);
|
|
102
|
+
else this.filePath = path.join(os.homedir(), ".toolbox-storage.json");
|
|
103
|
+
}
|
|
104
|
+
readStore() {
|
|
105
|
+
if (!fs.existsSync(this.filePath)) return {};
|
|
106
|
+
try {
|
|
107
|
+
const data = fs.readFileSync(this.filePath, "utf-8");
|
|
108
|
+
return JSON.parse(data);
|
|
109
|
+
} catch {
|
|
110
|
+
return {};
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
writeStore(store) {
|
|
114
|
+
const dir = path.dirname(this.filePath);
|
|
115
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
116
|
+
const fileExists = fs.existsSync(this.filePath);
|
|
117
|
+
fs.writeFileSync(this.filePath, JSON.stringify(store, null, 2), "utf-8");
|
|
118
|
+
if (!fileExists && os.platform() !== "win32") try {
|
|
119
|
+
fs.chmodSync(this.filePath, 384);
|
|
120
|
+
} catch {}
|
|
121
|
+
}
|
|
122
|
+
isExpired(item) {
|
|
123
|
+
if (item.ttl === Infinity || item.ttl === null || item.ttl === void 0 || String(item.ttl) === "Infinity") return false;
|
|
124
|
+
return Date.now() - item.timestamp > item.ttl;
|
|
125
|
+
}
|
|
126
|
+
async get(key) {
|
|
127
|
+
const item = this.readStore()[key];
|
|
128
|
+
if (!item) return;
|
|
129
|
+
if (this.isExpired(item)) {
|
|
130
|
+
await this.delete(key);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
return item.data;
|
|
134
|
+
}
|
|
135
|
+
async set(key, value, ttl = Infinity) {
|
|
136
|
+
const store = this.readStore();
|
|
137
|
+
store[key] = {
|
|
138
|
+
data: value,
|
|
139
|
+
timestamp: Date.now(),
|
|
140
|
+
ttl
|
|
141
|
+
};
|
|
142
|
+
this.writeStore(store);
|
|
143
|
+
}
|
|
144
|
+
async delete(key) {
|
|
145
|
+
const store = this.readStore();
|
|
146
|
+
if (key in store) {
|
|
147
|
+
delete store[key];
|
|
148
|
+
this.writeStore(store);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async clear() {
|
|
152
|
+
this.writeStore({});
|
|
153
|
+
}
|
|
154
|
+
async has(key) {
|
|
155
|
+
return await this.get(key) !== void 0;
|
|
156
|
+
}
|
|
157
|
+
async keys() {
|
|
158
|
+
const store = this.readStore();
|
|
159
|
+
const activeKeys = [];
|
|
160
|
+
for (const key of Object.keys(store)) {
|
|
161
|
+
const item = store[key];
|
|
162
|
+
if (item && !this.isExpired(item)) activeKeys.push(key);
|
|
163
|
+
else if (item) await this.delete(key);
|
|
164
|
+
}
|
|
165
|
+
return activeKeys;
|
|
166
|
+
}
|
|
167
|
+
async size() {
|
|
168
|
+
return (await this.keys()).length;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
export { FileStorage, MIN_1, MIN_10, MIN_15, MIN_3, MIN_5, MemoryCache, SEC_30, get, set, withCache };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@melledijkstra/storage",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"author": "Melle Dijkstra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
],
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
|
-
"types": "./dist/index.d.
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
15
|
"import": "./dist/index.mjs",
|
|
16
16
|
"require": "./dist/index.cjs"
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@melledijkstra/toolbox": "1.
|
|
20
|
+
"@melledijkstra/toolbox": "1.1.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@melledijkstra/config": "1.0.0"
|