@kikiutils/shared 9.2.0 → 9.3.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/README.md +34 -17
- package/dist/env.cjs +10 -13
- package/dist/env.cjs.map +1 -1
- package/dist/env.d.ts +9 -12
- package/dist/env.d.ts.map +1 -1
- package/dist/env.mjs +10 -13
- package/dist/env.mjs.map +1 -1
- package/dist/storage/enhanced/local/core.cjs +103 -0
- package/dist/storage/enhanced/local/core.cjs.map +1 -0
- package/dist/storage/enhanced/local/core.d.ts +56 -0
- package/dist/storage/enhanced/local/core.d.ts.map +1 -0
- package/dist/storage/enhanced/local/core.mjs +101 -0
- package/dist/storage/enhanced/local/core.mjs.map +1 -0
- package/dist/storage/enhanced/local/index.cjs +10 -0
- package/dist/storage/enhanced/local/index.cjs.map +1 -0
- package/dist/storage/enhanced/local/index.d.ts +3 -0
- package/dist/storage/enhanced/local/index.d.ts.map +1 -0
- package/dist/storage/enhanced/local/index.mjs +3 -0
- package/dist/storage/enhanced/local/index.mjs.map +1 -0
- package/dist/storage/enhanced/local/keyed-store.cjs +39 -0
- package/dist/storage/enhanced/local/keyed-store.cjs.map +1 -0
- package/dist/storage/enhanced/local/keyed-store.d.ts +31 -0
- package/dist/storage/enhanced/local/keyed-store.d.ts.map +1 -0
- package/dist/storage/enhanced/local/keyed-store.mjs +37 -0
- package/dist/storage/enhanced/local/keyed-store.mjs.map +1 -0
- package/dist/storage/enhanced/redis/core.cjs +142 -0
- package/dist/storage/enhanced/redis/core.cjs.map +1 -0
- package/dist/storage/enhanced/redis/core.d.ts +78 -0
- package/dist/storage/enhanced/redis/core.d.ts.map +1 -0
- package/dist/storage/enhanced/redis/core.mjs +140 -0
- package/dist/storage/enhanced/redis/core.mjs.map +1 -0
- package/dist/storage/enhanced/redis/index.cjs +10 -0
- package/dist/storage/enhanced/redis/index.cjs.map +1 -0
- package/dist/storage/enhanced/redis/index.d.ts +3 -0
- package/dist/storage/enhanced/redis/index.d.ts.map +1 -0
- package/dist/storage/enhanced/redis/index.mjs +3 -0
- package/dist/storage/enhanced/redis/index.mjs.map +1 -0
- package/dist/storage/enhanced/redis/keyed-store.cjs +44 -0
- package/dist/storage/enhanced/redis/keyed-store.cjs.map +1 -0
- package/dist/storage/enhanced/redis/keyed-store.d.ts +37 -0
- package/dist/storage/enhanced/redis/keyed-store.d.ts.map +1 -0
- package/dist/storage/enhanced/redis/keyed-store.mjs +42 -0
- package/dist/storage/enhanced/redis/keyed-store.mjs.map +1 -0
- package/dist/storage/lru/keyed-store.cjs +49 -0
- package/dist/storage/lru/keyed-store.cjs.map +1 -0
- package/dist/storage/lru/keyed-store.d.ts +40 -0
- package/dist/storage/lru/keyed-store.d.ts.map +1 -0
- package/dist/storage/lru/keyed-store.mjs +47 -0
- package/dist/storage/lru/keyed-store.mjs.map +1 -0
- package/package.json +33 -19
- package/src/env.ts +10 -13
- package/src/storage/enhanced/local/core.ts +104 -0
- package/src/storage/enhanced/local/index.ts +2 -0
- package/src/storage/enhanced/local/keyed-store.ts +34 -0
- package/src/storage/enhanced/redis/core.ts +149 -0
- package/src/storage/enhanced/redis/index.ts +2 -0
- package/src/storage/enhanced/redis/keyed-store.ts +41 -0
- package/src/storage/lru/keyed-store.ts +48 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a reusable, type-safe keyed store wrapper for an LRUCache instance.
|
|
5
|
+
*
|
|
6
|
+
* This utility allows structured access to cache entries using a dynamic key-generation function.
|
|
7
|
+
*
|
|
8
|
+
* @template D - The specific value type exposed by this store (must extend `V`).
|
|
9
|
+
* @template K - The type of keys used in the LRU cache (default: string).
|
|
10
|
+
* @template V - The type of values stored in the LRU cache.
|
|
11
|
+
* @template FC - The value used by `lru-cache` fetch context (if any).
|
|
12
|
+
*
|
|
13
|
+
* @param {LRUCache<K, V, FC>} lruInstance - An instance of `lru-cache`.
|
|
14
|
+
*
|
|
15
|
+
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const lruCache = new LRUCache<string, { id: number }>();
|
|
20
|
+
* const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);
|
|
21
|
+
* userStore.setItem({ id: 1 }, 1);
|
|
22
|
+
* const user = userStore.getItem(1);
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
// eslint-disable-next-line ts/no-empty-object-type
|
|
26
|
+
function createKeyedLruStore(lruInstance) {
|
|
27
|
+
return (getKeyFunction) => Object.freeze({
|
|
28
|
+
/**
|
|
29
|
+
* Return a value from the cache. Will update the recency of the cache entry found.
|
|
30
|
+
*
|
|
31
|
+
* If the key is not found, returns `null`.
|
|
32
|
+
*/
|
|
33
|
+
getItem(...args) {
|
|
34
|
+
const rawValue = lruInstance.get(getKeyFunction(...args));
|
|
35
|
+
return rawValue ?? null;
|
|
36
|
+
},
|
|
37
|
+
getItemTtl: (...args) => lruInstance.getRemainingTTL(getKeyFunction(...args)),
|
|
38
|
+
hasItem: (...args) => lruInstance.has(getKeyFunction(...args)),
|
|
39
|
+
removeItem: (...args) => lruInstance.delete(getKeyFunction(...args)),
|
|
40
|
+
/**
|
|
41
|
+
* Resolves the full cache key from the given arguments.
|
|
42
|
+
*/
|
|
43
|
+
resolveKey: (...args) => getKeyFunction(...args),
|
|
44
|
+
setItem: (value, ...args) => lruInstance.set(getKeyFunction(...args), value),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
exports.createKeyedLruStore = createKeyedLruStore;
|
|
49
|
+
//# sourceMappingURL=keyed-store.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyed-store.cjs","sources":["../../../src/storage/lru/keyed-store.ts"],"sourcesContent":["import type { LRUCache } from 'lru-cache';\n\n/**\n * Creates a reusable, type-safe keyed store wrapper for an LRUCache instance.\n *\n * This utility allows structured access to cache entries using a dynamic key-generation function.\n *\n * @template D - The specific value type exposed by this store (must extend `V`).\n * @template K - The type of keys used in the LRU cache (default: string).\n * @template V - The type of values stored in the LRU cache.\n * @template FC - The value used by `lru-cache` fetch context (if any).\n *\n * @param {LRUCache<K, V, FC>} lruInstance - An instance of `lru-cache`.\n *\n * @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store.\n *\n * @example\n * ```typescript\n * const lruCache = new LRUCache<string, { id: number }>();\n * const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);\n * userStore.setItem({ id: 1 }, 1);\n * const user = userStore.getItem(1);\n * ```\n */\n// eslint-disable-next-line ts/no-empty-object-type\nexport function createKeyedLruStore<D extends V, K extends {}, V extends {}, FC = unknown>(\n lruInstance: LRUCache<K, V, FC>,\n) {\n return <P extends any[]>(getKeyFunction: (...args: P) => K) => Object.freeze({\n /**\n * Return a value from the cache. Will update the recency of the cache entry found.\n *\n * If the key is not found, returns `null`.\n */\n getItem(...args: P) {\n const rawValue = lruInstance.get(getKeyFunction(...args));\n return rawValue ?? null;\n },\n getItemTtl: (...args: P) => lruInstance.getRemainingTTL(getKeyFunction(...args)),\n hasItem: (...args: P) => lruInstance.has(getKeyFunction(...args)),\n removeItem: (...args: P) => lruInstance.delete(getKeyFunction(...args)),\n /**\n * Resolves the full cache key from the given arguments.\n */\n resolveKey: (...args: P) => getKeyFunction(...args),\n setItem: (value: D, ...args: P) => lruInstance.set(getKeyFunction(...args), value),\n });\n}\n"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;AAqBG;AACH;AACM,SAAU,mBAAmB,CAC/B,WAA+B,EAAA;IAE/B,OAAO,CAAkB,cAAiC,KAAK,MAAM,CAAC,MAAM,CAAC;AACzE;;;;AAIG;QACH,OAAO,CAAC,GAAG,IAAO,EAAA;AACd,YAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;YACzD,OAAO,QAAQ,IAAI,IAAI;SAC1B;AACD,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,WAAW,CAAC,eAAe,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AAChF,QAAA,OAAO,EAAE,CAAC,GAAG,IAAO,KAAK,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACjE,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,WAAW,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACvE;;AAEG;QACH,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,cAAc,CAAC,GAAG,IAAI,CAAC;QACnD,OAAO,EAAE,CAAC,KAAQ,EAAE,GAAG,IAAO,KAAK,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;AACrF,KAAA,CAAC;AACN;;;;"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { LRUCache } from 'lru-cache';
|
|
2
|
+
/**
|
|
3
|
+
* Creates a reusable, type-safe keyed store wrapper for an LRUCache instance.
|
|
4
|
+
*
|
|
5
|
+
* This utility allows structured access to cache entries using a dynamic key-generation function.
|
|
6
|
+
*
|
|
7
|
+
* @template D - The specific value type exposed by this store (must extend `V`).
|
|
8
|
+
* @template K - The type of keys used in the LRU cache (default: string).
|
|
9
|
+
* @template V - The type of values stored in the LRU cache.
|
|
10
|
+
* @template FC - The value used by `lru-cache` fetch context (if any).
|
|
11
|
+
*
|
|
12
|
+
* @param {LRUCache<K, V, FC>} lruInstance - An instance of `lru-cache`.
|
|
13
|
+
*
|
|
14
|
+
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const lruCache = new LRUCache<string, { id: number }>();
|
|
19
|
+
* const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);
|
|
20
|
+
* userStore.setItem({ id: 1 }, 1);
|
|
21
|
+
* const user = userStore.getItem(1);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function createKeyedLruStore<D extends V, K extends {}, V extends {}, FC = unknown>(lruInstance: LRUCache<K, V, FC>): <P extends any[]>(getKeyFunction: (...args: P) => K) => Readonly<{
|
|
25
|
+
/**
|
|
26
|
+
* Return a value from the cache. Will update the recency of the cache entry found.
|
|
27
|
+
*
|
|
28
|
+
* If the key is not found, returns `null`.
|
|
29
|
+
*/
|
|
30
|
+
getItem(...args: P): V | null;
|
|
31
|
+
getItemTtl: (...args: P) => number;
|
|
32
|
+
hasItem: (...args: P) => boolean;
|
|
33
|
+
removeItem: (...args: P) => boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Resolves the full cache key from the given arguments.
|
|
36
|
+
*/
|
|
37
|
+
resolveKey: (...args: P) => K;
|
|
38
|
+
setItem: (value: D, ...args: P) => LRUCache<K, V, FC>;
|
|
39
|
+
}>;
|
|
40
|
+
//# sourceMappingURL=keyed-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyed-store.d.ts","sourceRoot":"","sources":["../../../src/storage/lru/keyed-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,wBAAgB,mBAAmB,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,EAAE,EAAE,EAAE,GAAG,OAAO,EACrF,WAAW,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,IAEvB,CAAC,SAAS,GAAG,EAAE,EAAE,gBAAgB,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,CAAC;IACtD;;;;OAIG;qBACc,CAAC;0BAII,CAAC;uBACJ,CAAC;0BACE,CAAC;IACvB;;OAEG;0BACmB,CAAC;qBACN,CAAC,WAAW,CAAC;GAErC"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a reusable, type-safe keyed store wrapper for an LRUCache instance.
|
|
3
|
+
*
|
|
4
|
+
* This utility allows structured access to cache entries using a dynamic key-generation function.
|
|
5
|
+
*
|
|
6
|
+
* @template D - The specific value type exposed by this store (must extend `V`).
|
|
7
|
+
* @template K - The type of keys used in the LRU cache (default: string).
|
|
8
|
+
* @template V - The type of values stored in the LRU cache.
|
|
9
|
+
* @template FC - The value used by `lru-cache` fetch context (if any).
|
|
10
|
+
*
|
|
11
|
+
* @param {LRUCache<K, V, FC>} lruInstance - An instance of `lru-cache`.
|
|
12
|
+
*
|
|
13
|
+
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const lruCache = new LRUCache<string, { id: number }>();
|
|
18
|
+
* const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);
|
|
19
|
+
* userStore.setItem({ id: 1 }, 1);
|
|
20
|
+
* const user = userStore.getItem(1);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
// eslint-disable-next-line ts/no-empty-object-type
|
|
24
|
+
function createKeyedLruStore(lruInstance) {
|
|
25
|
+
return (getKeyFunction) => Object.freeze({
|
|
26
|
+
/**
|
|
27
|
+
* Return a value from the cache. Will update the recency of the cache entry found.
|
|
28
|
+
*
|
|
29
|
+
* If the key is not found, returns `null`.
|
|
30
|
+
*/
|
|
31
|
+
getItem(...args) {
|
|
32
|
+
const rawValue = lruInstance.get(getKeyFunction(...args));
|
|
33
|
+
return rawValue ?? null;
|
|
34
|
+
},
|
|
35
|
+
getItemTtl: (...args) => lruInstance.getRemainingTTL(getKeyFunction(...args)),
|
|
36
|
+
hasItem: (...args) => lruInstance.has(getKeyFunction(...args)),
|
|
37
|
+
removeItem: (...args) => lruInstance.delete(getKeyFunction(...args)),
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the full cache key from the given arguments.
|
|
40
|
+
*/
|
|
41
|
+
resolveKey: (...args) => getKeyFunction(...args),
|
|
42
|
+
setItem: (value, ...args) => lruInstance.set(getKeyFunction(...args), value),
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { createKeyedLruStore };
|
|
47
|
+
//# sourceMappingURL=keyed-store.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyed-store.mjs","sources":["../../../src/storage/lru/keyed-store.ts"],"sourcesContent":["import type { LRUCache } from 'lru-cache';\n\n/**\n * Creates a reusable, type-safe keyed store wrapper for an LRUCache instance.\n *\n * This utility allows structured access to cache entries using a dynamic key-generation function.\n *\n * @template D - The specific value type exposed by this store (must extend `V`).\n * @template K - The type of keys used in the LRU cache (default: string).\n * @template V - The type of values stored in the LRU cache.\n * @template FC - The value used by `lru-cache` fetch context (if any).\n *\n * @param {LRUCache<K, V, FC>} lruInstance - An instance of `lru-cache`.\n *\n * @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store.\n *\n * @example\n * ```typescript\n * const lruCache = new LRUCache<string, { id: number }>();\n * const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);\n * userStore.setItem({ id: 1 }, 1);\n * const user = userStore.getItem(1);\n * ```\n */\n// eslint-disable-next-line ts/no-empty-object-type\nexport function createKeyedLruStore<D extends V, K extends {}, V extends {}, FC = unknown>(\n lruInstance: LRUCache<K, V, FC>,\n) {\n return <P extends any[]>(getKeyFunction: (...args: P) => K) => Object.freeze({\n /**\n * Return a value from the cache. Will update the recency of the cache entry found.\n *\n * If the key is not found, returns `null`.\n */\n getItem(...args: P) {\n const rawValue = lruInstance.get(getKeyFunction(...args));\n return rawValue ?? null;\n },\n getItemTtl: (...args: P) => lruInstance.getRemainingTTL(getKeyFunction(...args)),\n hasItem: (...args: P) => lruInstance.has(getKeyFunction(...args)),\n removeItem: (...args: P) => lruInstance.delete(getKeyFunction(...args)),\n /**\n * Resolves the full cache key from the given arguments.\n */\n resolveKey: (...args: P) => getKeyFunction(...args),\n setItem: (value: D, ...args: P) => lruInstance.set(getKeyFunction(...args), value),\n });\n}\n"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;AAqBG;AACH;AACM,SAAU,mBAAmB,CAC/B,WAA+B,EAAA;IAE/B,OAAO,CAAkB,cAAiC,KAAK,MAAM,CAAC,MAAM,CAAC;AACzE;;;;AAIG;QACH,OAAO,CAAC,GAAG,IAAO,EAAA;AACd,YAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;YACzD,OAAO,QAAQ,IAAI,IAAI;SAC1B;AACD,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,WAAW,CAAC,eAAe,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AAChF,QAAA,OAAO,EAAE,CAAC,GAAG,IAAO,KAAK,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACjE,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,WAAW,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACvE;;AAEG;QACH,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,cAAc,CAAC,GAAG,IAAI,CAAC;QACnD,OAAO,EAAE,CAAC,KAAQ,EAAE,GAAG,IAAO,KAAK,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;AACrF,KAAA,CAAC;AACN;;;;"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/shared",
|
|
3
|
-
"version": "9.
|
|
4
|
-
"description": "A lightweight modular utility library for JavaScript and TypeScript
|
|
3
|
+
"version": "9.3.1",
|
|
4
|
+
"description": "A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.",
|
|
5
5
|
"author": "kiki-kanri",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -9,25 +9,26 @@
|
|
|
9
9
|
"url": "git+https://github.com/kikiutils/node-shared.git"
|
|
10
10
|
},
|
|
11
11
|
"keywords": [
|
|
12
|
+
"utils",
|
|
13
|
+
"utilities",
|
|
14
|
+
"javascript",
|
|
15
|
+
"typescript",
|
|
16
|
+
"node",
|
|
12
17
|
"browser",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"datetime",
|
|
16
|
-
"enum",
|
|
18
|
+
"web",
|
|
19
|
+
"vue",
|
|
17
20
|
"env",
|
|
18
|
-
"
|
|
19
|
-
"javascript",
|
|
20
|
-
"logging",
|
|
21
|
+
"datetime",
|
|
21
22
|
"math",
|
|
22
|
-
"node",
|
|
23
|
-
"pino",
|
|
24
23
|
"string",
|
|
25
|
-
"
|
|
24
|
+
"enum",
|
|
26
25
|
"url",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
26
|
+
"crypto",
|
|
27
|
+
"hashing",
|
|
28
|
+
"storage",
|
|
29
|
+
"logging",
|
|
30
|
+
"consola",
|
|
31
|
+
"pino"
|
|
31
32
|
],
|
|
32
33
|
"sideEffects": false,
|
|
33
34
|
"exports": {
|
|
@@ -35,6 +36,16 @@
|
|
|
35
36
|
"types": "./dist/*.d.ts",
|
|
36
37
|
"import": "./dist/*.mjs",
|
|
37
38
|
"require": "./dist/*.cjs"
|
|
39
|
+
},
|
|
40
|
+
"./storage/enhanced/local": {
|
|
41
|
+
"types": "./dist/storage/enhanced/local/index.d.ts",
|
|
42
|
+
"import": "./dist/storage/enhanced/local/index.mjs",
|
|
43
|
+
"require": "./dist/storage/enhanced/local/index.cjs"
|
|
44
|
+
},
|
|
45
|
+
"./storage/enhanced/redis": {
|
|
46
|
+
"types": "./dist/storage/enhanced/redis/index.d.ts",
|
|
47
|
+
"import": "./dist/storage/enhanced/redis/index.mjs",
|
|
48
|
+
"require": "./dist/storage/enhanced/redis/index.cjs"
|
|
38
49
|
}
|
|
39
50
|
},
|
|
40
51
|
"files": [
|
|
@@ -45,7 +56,7 @@
|
|
|
45
56
|
"node": ">=18.12.1"
|
|
46
57
|
},
|
|
47
58
|
"scripts": {
|
|
48
|
-
"build": "ts-project-builder './src/**/*.ts' --clean --sourcemaps",
|
|
59
|
+
"build": "ts-project-builder './src/**/*.ts' --clean --preserve-modules --sourcemaps",
|
|
49
60
|
"bumplog": "changelogen --bump --hideAuthorEmail",
|
|
50
61
|
"lint": "eslint",
|
|
51
62
|
"lint:fix": "eslint --fix",
|
|
@@ -56,20 +67,23 @@
|
|
|
56
67
|
},
|
|
57
68
|
"devDependencies": {
|
|
58
69
|
"@kikiutils/changelogen": "^0.8.0",
|
|
59
|
-
"@kikiutils/eslint-config": "^1.0.
|
|
70
|
+
"@kikiutils/eslint-config": "^1.0.2",
|
|
60
71
|
"@kikiutils/tsconfigs": "^5.0.1",
|
|
61
72
|
"@noble/hashes": "^1.8.0",
|
|
62
73
|
"@types/jest": "^29.5.14",
|
|
63
|
-
"@types/node": "^22.15.
|
|
74
|
+
"@types/node": "^22.15.14",
|
|
64
75
|
"consola": "^3.4.2",
|
|
65
76
|
"cross-env": "^7.0.3",
|
|
66
77
|
"date-fns": "^4.1.0",
|
|
67
78
|
"decimal.js": "^10.5.0",
|
|
79
|
+
"ioredis": "^5.6.1",
|
|
68
80
|
"jest": "^29.7.0",
|
|
69
81
|
"jest-environment-jsdom": "^29.7.0",
|
|
82
|
+
"lru-cache": "^11.1.0",
|
|
70
83
|
"millify": "^6.1.0",
|
|
71
84
|
"pino": "^9.6.0",
|
|
72
85
|
"pino-pretty": "^13.0.0",
|
|
86
|
+
"superjson": "^2.2.2",
|
|
73
87
|
"ts-jest": "^29.3.2",
|
|
74
88
|
"ts-project-builder": "5.0.1",
|
|
75
89
|
"typescript": "^5.8.3",
|
package/src/env.ts
CHANGED
|
@@ -22,9 +22,12 @@ export class EnvironmentNotFoundError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* Retrieves the value of an environment variable, or throws an error if not
|
|
25
|
+
* Retrieves the value of an environment variable, or throws an error if it is not defined.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
27
|
+
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
28
|
+
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} key - The environment variable key to retrieve.
|
|
28
31
|
*
|
|
29
32
|
* @returns {string} The value of the environment variable.
|
|
30
33
|
*
|
|
@@ -32,20 +35,14 @@ export class EnvironmentNotFoundError extends Error {
|
|
|
32
35
|
*
|
|
33
36
|
* @example
|
|
34
37
|
* ```typescript
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* // When the environment variable 'API_KEY' is set:
|
|
38
|
-
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
38
|
+
* process.env.API_KEY = '';
|
|
39
|
+
* checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered "defined")
|
|
39
40
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
43
|
-
* } catch (error) {
|
|
44
|
-
* console.error(error); // Missing environment variable: API_KEY
|
|
45
|
-
* }
|
|
41
|
+
* delete process.env.API_KEY;
|
|
42
|
+
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
46
43
|
* ```
|
|
47
44
|
*/
|
|
48
45
|
export function checkAndGetEnvValue(key: string): string {
|
|
49
|
-
if (
|
|
46
|
+
if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);
|
|
50
47
|
return process.env[key];
|
|
51
48
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deserialize,
|
|
3
|
+
serialize,
|
|
4
|
+
} from 'superjson';
|
|
5
|
+
|
|
6
|
+
enum StorageValueEncodingType {
|
|
7
|
+
Json = '0',
|
|
8
|
+
String = '1',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const customValueHeader = '';
|
|
12
|
+
const customValueHeaderLength = customValueHeader.length + 1;
|
|
13
|
+
const toCustomValue = (type: StorageValueEncodingType, payload: string) => `${customValueHeader}${type}${payload}`;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* An enhanced localStorage wrapper that supports storing
|
|
17
|
+
* complex data types (e.g. Dates, Maps, Sets) using SuperJSON encoding.
|
|
18
|
+
*
|
|
19
|
+
* This utility preserves type structure when saving and retrieving values.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* enhancedLocalStorage.setItem('user', { name: 'user', createdAt: new Date() });
|
|
24
|
+
* const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export const enhancedLocalStorage = Object.freeze({
|
|
28
|
+
/**
|
|
29
|
+
* Removes all items from localStorage.
|
|
30
|
+
*/
|
|
31
|
+
clear: () => window.localStorage.clear(),
|
|
32
|
+
/**
|
|
33
|
+
* Retrieves a value by key and decodes it using SuperJSON or raw string.
|
|
34
|
+
*
|
|
35
|
+
* @template T - The expected type of the value.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} key - The key of the value to retrieve.
|
|
38
|
+
*
|
|
39
|
+
* @returns {null | T} The decoded value or null if not found.
|
|
40
|
+
*/
|
|
41
|
+
getItem<T = unknown>(key: string) {
|
|
42
|
+
const rawValue = window.localStorage.getItem(key);
|
|
43
|
+
return rawValue ? decodeStorageValue(rawValue) as T : null;
|
|
44
|
+
},
|
|
45
|
+
/**
|
|
46
|
+
* Checks whether a key exists in localStorage.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} key - The key to check.
|
|
49
|
+
*
|
|
50
|
+
* @returns {boolean} True if the key exists, false otherwise.
|
|
51
|
+
*/
|
|
52
|
+
hasItem: (key: string) => window.localStorage.getItem(key) !== null,
|
|
53
|
+
/**
|
|
54
|
+
* Returns the number of items stored in localStorage.
|
|
55
|
+
*
|
|
56
|
+
* @returns {number} The number of items stored in localStorage.
|
|
57
|
+
*/
|
|
58
|
+
get length() {
|
|
59
|
+
return window.localStorage.length;
|
|
60
|
+
},
|
|
61
|
+
/**
|
|
62
|
+
* Removes a specific key from localStorage.
|
|
63
|
+
*
|
|
64
|
+
* @param {string} key - The key to remove.
|
|
65
|
+
*/
|
|
66
|
+
removeItem: (key: string) => window.localStorage.removeItem(key),
|
|
67
|
+
/**
|
|
68
|
+
* Stores a value in localStorage with automatic serialization.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} key - The key to store the value under.
|
|
71
|
+
* @param {any} value - The value to store.
|
|
72
|
+
*/
|
|
73
|
+
setItem: (key: string, value: any) => window.localStorage.setItem(key, encodeToStorageValue(value)),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
function decodeStorageValue(data: string) {
|
|
77
|
+
if (!isCustomFormat(data)) return data;
|
|
78
|
+
const payload = data.substring(customValueHeaderLength);
|
|
79
|
+
const type = data.charAt(customValueHeader.length);
|
|
80
|
+
switch (type) {
|
|
81
|
+
case StorageValueEncodingType.Json:
|
|
82
|
+
try {
|
|
83
|
+
return deserialize(JSON.parse(payload));
|
|
84
|
+
} catch {
|
|
85
|
+
throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload.');
|
|
86
|
+
}
|
|
87
|
+
case StorageValueEncodingType.String: return payload;
|
|
88
|
+
default:
|
|
89
|
+
throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}.`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function encodeToStorageValue(value: any) {
|
|
94
|
+
if (typeof value === 'string') return toCustomValue(StorageValueEncodingType.String, value);
|
|
95
|
+
return toCustomValue(StorageValueEncodingType.Json, JSON.stringify(serialize(value)));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function isCustomFormat(data: string) {
|
|
99
|
+
return (
|
|
100
|
+
data.length >= customValueHeaderLength
|
|
101
|
+
&& data[0] === customValueHeader[0]
|
|
102
|
+
&& data[1] === customValueHeader[1]
|
|
103
|
+
);
|
|
104
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { enhancedLocalStorage } from './core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a reusable, type-safe storage interface based on `enhancedLocalStorage`
|
|
5
|
+
* and a dynamic key-generation function.
|
|
6
|
+
*
|
|
7
|
+
* This utility allows you to abstract away key construction logic and work directly
|
|
8
|
+
* with scoped key-value operations like `getItem`, `setItem`, and `removeItem`.
|
|
9
|
+
*
|
|
10
|
+
* @template D - The value type to store.
|
|
11
|
+
*
|
|
12
|
+
* @returns A factory that accepts a key generator function and returns a scoped storage interface.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const userStore = createKeyedEnhancedLocalStore<User>()((id: number) => `user:${id}`);
|
|
17
|
+
* userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
18
|
+
* const user = userStore.getItem(123);
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function createKeyedEnhancedLocalStore<D = unknown>() {
|
|
22
|
+
return <P extends any[]>(getKeyFunction: (...args: P) => string) => Object.freeze({
|
|
23
|
+
getItem: (...args: P) => enhancedLocalStorage.getItem<D>(getKeyFunction(...args)),
|
|
24
|
+
hasItem: (...args: P) => enhancedLocalStorage.hasItem(getKeyFunction(...args)),
|
|
25
|
+
removeItem: (...args: P) => enhancedLocalStorage.removeItem(getKeyFunction(...args)),
|
|
26
|
+
/**
|
|
27
|
+
* Resolves the storage key from the given arguments.
|
|
28
|
+
*
|
|
29
|
+
* @returns {string} The final string key used internally.
|
|
30
|
+
*/
|
|
31
|
+
resolveKey: (...args: P) => getKeyFunction(...args),
|
|
32
|
+
setItem: (value: D, ...args: P) => enhancedLocalStorage.setItem(getKeyFunction(...args), value),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
|
|
3
|
+
import { Redis } from 'ioredis';
|
|
4
|
+
import {
|
|
5
|
+
deserialize,
|
|
6
|
+
serialize,
|
|
7
|
+
} from 'superjson';
|
|
8
|
+
|
|
9
|
+
enum StorageValueEncodingType {
|
|
10
|
+
Buffer = 0,
|
|
11
|
+
Json = 1,
|
|
12
|
+
String = 2,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const customValueHeader = Buffer.of(
|
|
16
|
+
0xE2,
|
|
17
|
+
0x81,
|
|
18
|
+
0xA0,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
const customValueHeaderLength = customValueHeader.byteLength + 1;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates an enhanced Redis-based storage interface using SuperJSON encoding.
|
|
25
|
+
*
|
|
26
|
+
* This utility provides a typed, serializable key-value store backed by Redis,
|
|
27
|
+
* supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).
|
|
28
|
+
*
|
|
29
|
+
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string.
|
|
30
|
+
*
|
|
31
|
+
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const redisStorage = createEnhancedRedisStorage('redis://localhost');
|
|
36
|
+
* await redisStorage.setItem('user:1', { name: 'user' });
|
|
37
|
+
* const user = await redisStorage.getItem<{ name: string }>('user:1');
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string) {
|
|
41
|
+
const instance = ioRedisInstanceOrUrl instanceof Redis ? ioRedisInstanceOrUrl : new Redis(ioRedisInstanceOrUrl);
|
|
42
|
+
return Object.freeze({
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves a value from Redis and decodes it.
|
|
45
|
+
*
|
|
46
|
+
* @template T - The expected return type.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} key - The Redis key.
|
|
49
|
+
*
|
|
50
|
+
* @returns {Promise<null | T>} The decoded value or null if not found.
|
|
51
|
+
*/
|
|
52
|
+
async getItem<T = unknown>(key: string) {
|
|
53
|
+
const rawValue = await instance.getBuffer(key);
|
|
54
|
+
return rawValue ? decodeStorageValue(rawValue) as T : null;
|
|
55
|
+
},
|
|
56
|
+
/**
|
|
57
|
+
* Gets the remaining TTL (in seconds) for a given key.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} key - The Redis key.
|
|
60
|
+
*
|
|
61
|
+
* @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set.
|
|
62
|
+
*/
|
|
63
|
+
getItemTtl: (key: string) => instance.ttl(key),
|
|
64
|
+
/**
|
|
65
|
+
* Checks whether a key exists in Redis.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} key - The Redis key.
|
|
68
|
+
*
|
|
69
|
+
* @returns {Promise<boolean>} True if the key exists, false otherwise.
|
|
70
|
+
*/
|
|
71
|
+
hasItem: async (key: string) => await instance.exists(key) === 1,
|
|
72
|
+
/**
|
|
73
|
+
* The underlying Redis instance, exposed for advanced operations.
|
|
74
|
+
* Use with caution; most use cases should rely on the wrapper methods.
|
|
75
|
+
*
|
|
76
|
+
* @returns {Redis} The underlying Redis instance.
|
|
77
|
+
*/
|
|
78
|
+
get instance() {
|
|
79
|
+
return instance;
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* Removes a key from Redis.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} key - The Redis key to delete.
|
|
85
|
+
*
|
|
86
|
+
* @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,
|
|
87
|
+
* or `false` if it did not exist.
|
|
88
|
+
*/
|
|
89
|
+
removeItem: async (key: string) => await instance.del(key) === 1,
|
|
90
|
+
/**
|
|
91
|
+
* Stores a value in Redis without expiration.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} key - The Redis key.
|
|
94
|
+
* @param {any} value - The value to store. Will be serialized.
|
|
95
|
+
*/
|
|
96
|
+
setItem: (key: string, value: any) => instance.set(key, encodeToStorageValue(value)),
|
|
97
|
+
/**
|
|
98
|
+
* Stores a value in Redis with a time-to-live (TTL).
|
|
99
|
+
*
|
|
100
|
+
* @param {string} key - The Redis key.
|
|
101
|
+
* @param {number} seconds - Expiration time in seconds.
|
|
102
|
+
* @param {any} value - The value to store. Will be serialized.
|
|
103
|
+
*/
|
|
104
|
+
setItemWithTtl(key: string, seconds: number, value: any) {
|
|
105
|
+
return instance.setex(key, seconds, encodeToStorageValue(value));
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function decodeStorageValue(data: Buffer) {
|
|
111
|
+
if (!isCustomFormat(data)) return data;
|
|
112
|
+
const payload = data.subarray(customValueHeaderLength);
|
|
113
|
+
const type = data[customValueHeader.byteLength];
|
|
114
|
+
switch (type) {
|
|
115
|
+
case StorageValueEncodingType.Buffer: return payload;
|
|
116
|
+
case StorageValueEncodingType.Json:
|
|
117
|
+
try {
|
|
118
|
+
return deserialize(JSON.parse(payload.toString()));
|
|
119
|
+
} catch {
|
|
120
|
+
throw new Error('[RedisStorage] Failed to parse JSON payload.');
|
|
121
|
+
}
|
|
122
|
+
case StorageValueEncodingType.String: return payload.toString();
|
|
123
|
+
default:
|
|
124
|
+
throw new Error(`[RedisStorage] Unknown encoding type: ${type}.`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function encodeToStorageValue(value: any) {
|
|
129
|
+
if (Buffer.isBuffer(value)) return toCustomValue(StorageValueEncodingType.Buffer, value);
|
|
130
|
+
if (typeof value === 'string') return toCustomValue(StorageValueEncodingType.String, Buffer.from(value));
|
|
131
|
+
return toCustomValue(StorageValueEncodingType.Json, Buffer.from(JSON.stringify(serialize(value))));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isCustomFormat(buffer: Buffer) {
|
|
135
|
+
return (
|
|
136
|
+
buffer.length >= customValueHeaderLength
|
|
137
|
+
&& buffer[0] === customValueHeader[0]
|
|
138
|
+
&& buffer[1] === customValueHeader[1]
|
|
139
|
+
&& buffer[2] === customValueHeader[2]
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function toCustomValue(type: StorageValueEncodingType, payload: Buffer) {
|
|
144
|
+
return Buffer.concat([
|
|
145
|
+
customValueHeader,
|
|
146
|
+
Buffer.of(type),
|
|
147
|
+
payload,
|
|
148
|
+
]);
|
|
149
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { createEnhancedRedisStorage } from './core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates a reusable, type-safe Redis-backed storage interface based on `createEnhancedRedisStorage`
|
|
5
|
+
* and a dynamic key-generation function.
|
|
6
|
+
*
|
|
7
|
+
* This utility abstracts away key construction and provides high-level access
|
|
8
|
+
* to Redis operations such as `getItem`, `setItem`, `setItemWithTtl`, and `getItemTtl`.
|
|
9
|
+
* It is ideal for namespaced data, caching, and session handling.
|
|
10
|
+
*
|
|
11
|
+
* @template D - The value type to store.
|
|
12
|
+
*
|
|
13
|
+
* @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance.
|
|
14
|
+
*
|
|
15
|
+
* @returns A factory that accepts a key generator function and returns a scoped Redis storage interface.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const userStore = createKeyedEnhancedRedisStore<User>(redisStorage)((id: number) => `user:${id}`);
|
|
20
|
+
* await userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
21
|
+
* const user = await userStore.getItem(123);
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function createKeyedEnhancedRedisStore<D = unknown>(storage: ReturnType<typeof createEnhancedRedisStorage>) {
|
|
25
|
+
return <P extends any[]>(getKeyFunction: (...args: P) => string) => Object.freeze({
|
|
26
|
+
getItem: (...args: P) => storage.getItem<D>(getKeyFunction(...args)),
|
|
27
|
+
getItemTtl: (...args: P) => storage.getItemTtl(getKeyFunction(...args)),
|
|
28
|
+
hasItem: (...args: P) => storage.hasItem(getKeyFunction(...args)),
|
|
29
|
+
removeItem: (...args: P) => storage.removeItem(getKeyFunction(...args)),
|
|
30
|
+
/**
|
|
31
|
+
* Resolves the storage key from the given arguments.
|
|
32
|
+
*
|
|
33
|
+
* @returns {string} The final string key used internally.
|
|
34
|
+
*/
|
|
35
|
+
resolveKey: (...args: P) => getKeyFunction(...args),
|
|
36
|
+
setItem: (value: D, ...args: P) => storage.setItem(getKeyFunction(...args), value),
|
|
37
|
+
setItemWithTtl(seconds: number, value: D, ...args: P) {
|
|
38
|
+
return storage.setItemWithTtl(getKeyFunction(...args), seconds, value);
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|