@kikiutils/shared 13.6.1 → 14.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/dist/classes/path.d.ts.map +1 -1
- package/dist/storages/lru/keyed-store.d.ts +22 -0
- package/dist/storages/lru/keyed-store.d.ts.map +1 -0
- package/dist/storages/lru/keyed-store.js +17 -0
- package/dist/storages/lru/keyed-store.js.map +1 -0
- package/dist/storages/redis/keyed-store.d.ts +16 -0
- package/dist/storages/redis/keyed-store.d.ts.map +1 -0
- package/dist/storages/redis/keyed-store.js +18 -0
- package/dist/storages/redis/keyed-store.js.map +1 -0
- package/dist/storages/redis/msgpack.d.ts +7 -0
- package/dist/storages/redis/msgpack.d.ts.map +1 -0
- package/dist/storages/redis/msgpack.js +23 -0
- package/dist/storages/redis/msgpack.js.map +1 -0
- package/dist/storages/redis/types.d.ts +22 -0
- package/dist/storages/redis/types.d.ts.map +1 -0
- package/dist/storages/redis/types.js +1 -0
- package/package.json +20 -45
- package/src/storages/lru/keyed-store.ts +23 -0
- package/src/storages/redis/keyed-store.ts +17 -0
- package/src/storages/redis/msgpack.ts +30 -0
- package/src/storages/redis/types.ts +19 -0
- package/dist/storage/enhanced/local/core.d.ts +0 -61
- package/dist/storage/enhanced/local/core.d.ts.map +0 -1
- package/dist/storage/enhanced/local/core.js +0 -63
- package/dist/storage/enhanced/local/core.js.map +0 -1
- package/dist/storage/enhanced/local/index.d.ts +0 -3
- package/dist/storage/enhanced/local/index.js +0 -4
- package/dist/storage/enhanced/local/keyed-store.d.ts +0 -36
- package/dist/storage/enhanced/local/keyed-store.d.ts.map +0 -1
- package/dist/storage/enhanced/local/keyed-store.js +0 -36
- package/dist/storage/enhanced/local/keyed-store.js.map +0 -1
- package/dist/storage/enhanced/redis/core.d.ts +0 -85
- package/dist/storage/enhanced/redis/core.d.ts.map +0 -1
- package/dist/storage/enhanced/redis/core.js +0 -85
- package/dist/storage/enhanced/redis/core.js.map +0 -1
- package/dist/storage/enhanced/redis/index.d.ts +0 -3
- package/dist/storage/enhanced/redis/index.js +0 -4
- package/dist/storage/enhanced/redis/keyed-store.d.ts +0 -44
- package/dist/storage/enhanced/redis/keyed-store.d.ts.map +0 -1
- package/dist/storage/enhanced/redis/keyed-store.js +0 -41
- package/dist/storage/enhanced/redis/keyed-store.js.map +0 -1
- package/dist/storage/lru/keyed-store.d.ts +0 -44
- package/dist/storage/lru/keyed-store.d.ts.map +0 -1
- package/dist/storage/lru/keyed-store.js +0 -40
- package/dist/storage/lru/keyed-store.js.map +0 -1
- package/src/storage/enhanced/local/core.ts +0 -106
- package/src/storage/enhanced/local/index.ts +0 -2
- package/src/storage/enhanced/local/keyed-store.ts +0 -36
- package/src/storage/enhanced/redis/core.ts +0 -151
- package/src/storage/enhanced/redis/index.ts +0 -2
- package/src/storage/enhanced/redis/keyed-store.ts +0 -43
- package/src/storage/lru/keyed-store.ts +0 -46
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { enhancedLocalStorage } from "./core.js";
|
|
2
|
-
|
|
3
|
-
//#region src/storage/enhanced/local/keyed-store.ts
|
|
4
|
-
/**
|
|
5
|
-
* Creates a reusable, type-safe storage interface based on `enhancedLocalStorage`
|
|
6
|
-
* and a dynamic key-generation function.
|
|
7
|
-
*
|
|
8
|
-
* This utility allows you to abstract away key construction logic and work directly
|
|
9
|
-
* with scoped key-value operations like `getItem`, `setItem`, and `removeItem`.
|
|
10
|
-
*
|
|
11
|
-
* @template D - The value type to store
|
|
12
|
-
*
|
|
13
|
-
* @returns A factory that accepts a key generator function and returns a scoped storage interface
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { createKeyedEnhancedLocalStore } from '@kikiutils/shared/storage/enhanced/local';
|
|
18
|
-
*
|
|
19
|
-
* const userStore = createKeyedEnhancedLocalStore<User>()((id: number) => `user:${id}`);
|
|
20
|
-
* userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
21
|
-
* const user = userStore.getItem(123);
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
function createKeyedEnhancedLocalStore() {
|
|
25
|
-
return (getKeyFunction) => Object.freeze({
|
|
26
|
-
getItem: (...args) => enhancedLocalStorage.getItem(getKeyFunction(...args)),
|
|
27
|
-
hasItem: (...args) => enhancedLocalStorage.hasItem(getKeyFunction(...args)),
|
|
28
|
-
removeItem: (...args) => enhancedLocalStorage.removeItem(getKeyFunction(...args)),
|
|
29
|
-
resolveKey: (...args) => getKeyFunction(...args),
|
|
30
|
-
setItem: (value, ...args) => enhancedLocalStorage.setItem(getKeyFunction(...args), value)
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
//#endregion
|
|
35
|
-
export { createKeyedEnhancedLocalStore };
|
|
36
|
-
//# sourceMappingURL=keyed-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.js","names":[],"sources":["../../../../src/storage/enhanced/local/keyed-store.ts"],"sourcesContent":["import { enhancedLocalStorage } from './core';\n\n/**\n * Creates a reusable, type-safe storage interface based on `enhancedLocalStorage`\n * and a dynamic key-generation function.\n *\n * This utility allows you to abstract away key construction logic and work directly\n * with scoped key-value operations like `getItem`, `setItem`, and `removeItem`.\n *\n * @template D - The value type to store\n *\n * @returns A factory that accepts a key generator function and returns a scoped storage interface\n *\n * @example\n * ```typescript\n * import { createKeyedEnhancedLocalStore } from '@kikiutils/shared/storage/enhanced/local';\n *\n * const userStore = createKeyedEnhancedLocalStore<User>()((id: number) => `user:${id}`);\n * userStore.setItem({ id: 123, name: 'user' }, 123);\n * const user = userStore.getItem(123);\n * ```\n */\nexport function createKeyedEnhancedLocalStore<D = unknown>() {\n return <P extends any[]>(getKeyFunction: (...args: P) => string) => Object.freeze({\n getItem: (...args: P) => enhancedLocalStorage.getItem<D>(getKeyFunction(...args)),\n hasItem: (...args: P) => enhancedLocalStorage.hasItem(getKeyFunction(...args)),\n removeItem: (...args: P) => enhancedLocalStorage.removeItem(getKeyFunction(...args)),\n /**\n * Resolves the storage key from the given arguments.\n *\n * @returns {string} The final string key used internally\n */\n resolveKey: (...args: P) => getKeyFunction(...args),\n setItem: (value: D, ...args: P) => enhancedLocalStorage.setItem(getKeyFunction(...args), value),\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAsBA,SAAgB,gCAA6C;AACzD,SAAyB,mBAA2C,OAAO,OAAO;EAC9E,UAAU,GAAG,SAAY,qBAAqB,QAAW,eAAe,GAAG,KAAK,CAAC;EACjF,UAAU,GAAG,SAAY,qBAAqB,QAAQ,eAAe,GAAG,KAAK,CAAC;EAC9E,aAAa,GAAG,SAAY,qBAAqB,WAAW,eAAe,GAAG,KAAK,CAAC;EAMpF,aAAa,GAAG,SAAY,eAAe,GAAG,KAAK;EACnD,UAAU,OAAU,GAAG,SAAY,qBAAqB,QAAQ,eAAe,GAAG,KAAK,EAAE,MAAM;EAClG,CAAC"}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { Redis } from "ioredis";
|
|
2
|
-
|
|
3
|
-
//#region src/storage/enhanced/redis/core.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates an enhanced Redis-based storage interface using SuperJSON encoding.
|
|
7
|
-
*
|
|
8
|
-
* This utility provides a typed, serializable key-value store backed by Redis,
|
|
9
|
-
* supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).
|
|
10
|
-
*
|
|
11
|
-
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string
|
|
12
|
-
*
|
|
13
|
-
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { createEnhancedRedisStorage } from '@kikiutils/shared/storage/enhanced/redis';
|
|
18
|
-
*
|
|
19
|
-
* const redisStorage = createEnhancedRedisStorage('redis://localhost');
|
|
20
|
-
* await redisStorage.setItem('user:1', { name: 'user' });
|
|
21
|
-
* const user = await redisStorage.getItem<{ name: string }>('user:1');
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
declare function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string): Readonly<{
|
|
25
|
-
/**
|
|
26
|
-
* Retrieves a value from Redis and decodes it.
|
|
27
|
-
*
|
|
28
|
-
* @template T - The expected return type
|
|
29
|
-
*
|
|
30
|
-
* @param {string} key - The Redis key
|
|
31
|
-
*
|
|
32
|
-
* @returns {Promise<null | T>} The decoded value or null if not found
|
|
33
|
-
*/
|
|
34
|
-
getItem<T = unknown>(key: string): Promise<T | null>;
|
|
35
|
-
/**
|
|
36
|
-
* Gets the remaining TTL (in seconds) for a given key.
|
|
37
|
-
*
|
|
38
|
-
* @param {string} key - The Redis key
|
|
39
|
-
*
|
|
40
|
-
* @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set
|
|
41
|
-
*/
|
|
42
|
-
getItemTtl: (key: string) => Promise<number>;
|
|
43
|
-
/**
|
|
44
|
-
* Checks whether a key exists in Redis.
|
|
45
|
-
*
|
|
46
|
-
* @param {string} key - The Redis key
|
|
47
|
-
*
|
|
48
|
-
* @returns {Promise<boolean>} True if the key exists, false otherwise
|
|
49
|
-
*/
|
|
50
|
-
hasItem: (key: string) => Promise<boolean>;
|
|
51
|
-
/**
|
|
52
|
-
* The underlying Redis instance, exposed for advanced operations.
|
|
53
|
-
* Use with caution; most use cases should rely on the wrapper methods.
|
|
54
|
-
*
|
|
55
|
-
* @returns {Redis} The underlying Redis instance
|
|
56
|
-
*/
|
|
57
|
-
readonly instance: Redis;
|
|
58
|
-
/**
|
|
59
|
-
* Removes a key from Redis.
|
|
60
|
-
*
|
|
61
|
-
* @param {string} key - The Redis key to delete
|
|
62
|
-
*
|
|
63
|
-
* @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,
|
|
64
|
-
* or `false` if it did not exist.
|
|
65
|
-
*/
|
|
66
|
-
removeItem: (key: string) => Promise<boolean>;
|
|
67
|
-
/**
|
|
68
|
-
* Stores a value in Redis without expiration.
|
|
69
|
-
*
|
|
70
|
-
* @param {string} key - The Redis key
|
|
71
|
-
* @param {any} value - The value to store. Will be serialized
|
|
72
|
-
*/
|
|
73
|
-
setItem: (key: string, value: any) => Promise<"OK">;
|
|
74
|
-
/**
|
|
75
|
-
* Stores a value in Redis with a time-to-live (TTL).
|
|
76
|
-
*
|
|
77
|
-
* @param {string} key - The Redis key
|
|
78
|
-
* @param {number} seconds - Expiration time in seconds
|
|
79
|
-
* @param {any} value - The value to store. Will be serialized
|
|
80
|
-
*/
|
|
81
|
-
setItemWithTtl(key: string, seconds: number, value: any): Promise<"OK">;
|
|
82
|
-
}>;
|
|
83
|
-
//#endregion
|
|
84
|
-
export { createEnhancedRedisStorage };
|
|
85
|
-
//# sourceMappingURL=core.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","names":[],"sources":["../../../../src/storage/enhanced/redis/core.ts"],"sourcesContent":[],"mappings":";;;;;;AAyCA;;;;;;;;;;;;;;;;;iBAAgB,0BAAA,uBAAiD,iBAAc;;;;;;;;;;qCAYjC,QAAA;;;;;;;;+BAWd;;;;;;;;4BAQG;;;;;;;;;;;;;;;;+BAkBG;;;;;;;wCAOG;;;;;;;;4DAQsB"}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
|
-
import { deserialize, serialize } from "superjson";
|
|
3
|
-
import { Redis } from "ioredis";
|
|
4
|
-
|
|
5
|
-
//#region src/storage/enhanced/redis/core.ts
|
|
6
|
-
var StorageValueEncodingType = /* @__PURE__ */ function(StorageValueEncodingType$1) {
|
|
7
|
-
StorageValueEncodingType$1[StorageValueEncodingType$1["Buffer"] = 0] = "Buffer";
|
|
8
|
-
StorageValueEncodingType$1[StorageValueEncodingType$1["Json"] = 1] = "Json";
|
|
9
|
-
StorageValueEncodingType$1[StorageValueEncodingType$1["String"] = 2] = "String";
|
|
10
|
-
return StorageValueEncodingType$1;
|
|
11
|
-
}(StorageValueEncodingType || {});
|
|
12
|
-
const customValueHeader = Buffer.of(226, 129, 160);
|
|
13
|
-
const customValueHeaderLength = customValueHeader.byteLength + 1;
|
|
14
|
-
/**
|
|
15
|
-
* Creates an enhanced Redis-based storage interface using SuperJSON encoding.
|
|
16
|
-
*
|
|
17
|
-
* This utility provides a typed, serializable key-value store backed by Redis,
|
|
18
|
-
* supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).
|
|
19
|
-
*
|
|
20
|
-
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string
|
|
21
|
-
*
|
|
22
|
-
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```typescript
|
|
26
|
-
* import { createEnhancedRedisStorage } from '@kikiutils/shared/storage/enhanced/redis';
|
|
27
|
-
*
|
|
28
|
-
* const redisStorage = createEnhancedRedisStorage('redis://localhost');
|
|
29
|
-
* await redisStorage.setItem('user:1', { name: 'user' });
|
|
30
|
-
* const user = await redisStorage.getItem<{ name: string }>('user:1');
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
function createEnhancedRedisStorage(ioRedisInstanceOrUrl) {
|
|
34
|
-
const instance = ioRedisInstanceOrUrl instanceof Redis ? ioRedisInstanceOrUrl : new Redis(ioRedisInstanceOrUrl);
|
|
35
|
-
return Object.freeze({
|
|
36
|
-
async getItem(key) {
|
|
37
|
-
const rawValue = await instance.getBuffer(key);
|
|
38
|
-
return rawValue ? decodeStorageValue(rawValue) : null;
|
|
39
|
-
},
|
|
40
|
-
getItemTtl: (key) => instance.ttl(key),
|
|
41
|
-
hasItem: async (key) => await instance.exists(key) === 1,
|
|
42
|
-
get instance() {
|
|
43
|
-
return instance;
|
|
44
|
-
},
|
|
45
|
-
removeItem: async (key) => await instance.del(key) === 1,
|
|
46
|
-
setItem: (key, value) => instance.set(key, encodeToStorageValue(value)),
|
|
47
|
-
setItemWithTtl(key, seconds, value) {
|
|
48
|
-
return instance.setex(key, seconds, encodeToStorageValue(value));
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
function decodeStorageValue(data) {
|
|
53
|
-
if (!isCustomFormat(data)) return data;
|
|
54
|
-
const payload = data.subarray(customValueHeaderLength);
|
|
55
|
-
const type = data[customValueHeader.byteLength];
|
|
56
|
-
switch (type) {
|
|
57
|
-
case StorageValueEncodingType.Buffer: return payload;
|
|
58
|
-
case StorageValueEncodingType.Json: try {
|
|
59
|
-
return deserialize(JSON.parse(payload.toString()));
|
|
60
|
-
} catch {
|
|
61
|
-
throw new Error("[RedisStorage] Failed to parse JSON payload");
|
|
62
|
-
}
|
|
63
|
-
case StorageValueEncodingType.String: return payload.toString();
|
|
64
|
-
default: throw new Error(`[RedisStorage] Unknown encoding type: ${type}`);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
function encodeToStorageValue(value) {
|
|
68
|
-
if (Buffer.isBuffer(value)) return toCustomValue(StorageValueEncodingType.Buffer, value);
|
|
69
|
-
if (typeof value === "string") return toCustomValue(StorageValueEncodingType.String, Buffer.from(value));
|
|
70
|
-
return toCustomValue(StorageValueEncodingType.Json, Buffer.from(JSON.stringify(serialize(value))));
|
|
71
|
-
}
|
|
72
|
-
function isCustomFormat(buffer) {
|
|
73
|
-
return buffer.length >= customValueHeaderLength && buffer[0] === customValueHeader[0] && buffer[1] === customValueHeader[1] && buffer[2] === customValueHeader[2];
|
|
74
|
-
}
|
|
75
|
-
function toCustomValue(type, payload) {
|
|
76
|
-
return Buffer.concat([
|
|
77
|
-
customValueHeader,
|
|
78
|
-
Buffer.of(type),
|
|
79
|
-
payload
|
|
80
|
-
]);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
//#endregion
|
|
84
|
-
export { createEnhancedRedisStorage };
|
|
85
|
-
//# sourceMappingURL=core.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","names":[],"sources":["../../../../src/storage/enhanced/redis/core.ts"],"sourcesContent":["import { Buffer } from 'node:buffer';\n\nimport { Redis } from 'ioredis';\nimport {\n deserialize,\n serialize,\n} from 'superjson';\n\nenum StorageValueEncodingType {\n Buffer = 0,\n Json = 1,\n String = 2,\n}\n\nconst customValueHeader = Buffer.of(\n 0xE2,\n 0x81,\n 0xA0,\n);\n\nconst customValueHeaderLength = customValueHeader.byteLength + 1;\n\n/**\n * Creates an enhanced Redis-based storage interface using SuperJSON encoding.\n *\n * This utility provides a typed, serializable key-value store backed by Redis,\n * supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).\n *\n * @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string\n *\n * @returns A frozen object that wraps Redis commands with typed get/set logic and encoding\n *\n * @example\n * ```typescript\n * import { createEnhancedRedisStorage } from '@kikiutils/shared/storage/enhanced/redis';\n *\n * const redisStorage = createEnhancedRedisStorage('redis://localhost');\n * await redisStorage.setItem('user:1', { name: 'user' });\n * const user = await redisStorage.getItem<{ name: string }>('user:1');\n * ```\n */\nexport function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string) {\n const instance = ioRedisInstanceOrUrl instanceof Redis ? ioRedisInstanceOrUrl : new Redis(ioRedisInstanceOrUrl);\n return Object.freeze({\n /**\n * Retrieves a value from Redis and decodes it.\n *\n * @template T - The expected return type\n *\n * @param {string} key - The Redis key\n *\n * @returns {Promise<null | T>} The decoded value or null if not found\n */\n async getItem<T = unknown>(key: string) {\n const rawValue = await instance.getBuffer(key);\n return rawValue ? decodeStorageValue(rawValue) as T : null;\n },\n /**\n * Gets the remaining TTL (in seconds) for a given key.\n *\n * @param {string} key - The Redis key\n *\n * @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set\n */\n getItemTtl: (key: string) => instance.ttl(key),\n /**\n * Checks whether a key exists in Redis.\n *\n * @param {string} key - The Redis key\n *\n * @returns {Promise<boolean>} True if the key exists, false otherwise\n */\n hasItem: async (key: string) => await instance.exists(key) === 1,\n /**\n * The underlying Redis instance, exposed for advanced operations.\n * Use with caution; most use cases should rely on the wrapper methods.\n *\n * @returns {Redis} The underlying Redis instance\n */\n get instance() {\n return instance;\n },\n /**\n * Removes a key from Redis.\n *\n * @param {string} key - The Redis key to delete\n *\n * @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,\n * or `false` if it did not exist.\n */\n removeItem: async (key: string) => await instance.del(key) === 1,\n /**\n * Stores a value in Redis without expiration.\n *\n * @param {string} key - The Redis key\n * @param {any} value - The value to store. Will be serialized\n */\n setItem: (key: string, value: any) => instance.set(key, encodeToStorageValue(value)),\n /**\n * Stores a value in Redis with a time-to-live (TTL).\n *\n * @param {string} key - The Redis key\n * @param {number} seconds - Expiration time in seconds\n * @param {any} value - The value to store. Will be serialized\n */\n setItemWithTtl(key: string, seconds: number, value: any) {\n return instance.setex(key, seconds, encodeToStorageValue(value));\n },\n });\n}\n\nfunction decodeStorageValue(data: Buffer) {\n if (!isCustomFormat(data)) return data;\n const payload = data.subarray(customValueHeaderLength);\n const type = data[customValueHeader.byteLength];\n switch (type) {\n case StorageValueEncodingType.Buffer: return payload;\n case StorageValueEncodingType.Json:\n try {\n return deserialize(JSON.parse(payload.toString()));\n } catch {\n throw new Error('[RedisStorage] Failed to parse JSON payload');\n }\n case StorageValueEncodingType.String: return payload.toString();\n default:\n throw new Error(`[RedisStorage] Unknown encoding type: ${type}`);\n }\n}\n\nfunction encodeToStorageValue(value: any) {\n if (Buffer.isBuffer(value)) return toCustomValue(StorageValueEncodingType.Buffer, value);\n if (typeof value === 'string') return toCustomValue(StorageValueEncodingType.String, Buffer.from(value));\n return toCustomValue(StorageValueEncodingType.Json, Buffer.from(JSON.stringify(serialize(value))));\n}\n\nfunction isCustomFormat(buffer: Buffer) {\n return (\n buffer.length >= customValueHeaderLength\n && buffer[0] === customValueHeader[0]\n && buffer[1] === customValueHeader[1]\n && buffer[2] === customValueHeader[2]\n );\n}\n\nfunction toCustomValue(type: StorageValueEncodingType, payload: Buffer) {\n return Buffer.concat([\n customValueHeader,\n Buffer.of(type),\n payload,\n ]);\n}\n"],"mappings":";;;;;AAQA,IAAK,gFAAL;AACI;AACA;AACA;;EAHC;AAML,MAAM,oBAAoB,OAAO,GAC7B,KACA,KACA,IACH;AAED,MAAM,0BAA0B,kBAAkB,aAAa;;;;;;;;;;;;;;;;;;;;AAqB/D,SAAgB,2BAA2B,sBAAsC;CAC7E,MAAM,WAAW,gCAAgC,QAAQ,uBAAuB,IAAI,MAAM,qBAAqB;AAC/G,QAAO,OAAO,OAAO;EAUjB,MAAM,QAAqB,KAAa;GACpC,MAAM,WAAW,MAAM,SAAS,UAAU,IAAI;AAC9C,UAAO,WAAW,mBAAmB,SAAS,GAAQ;;EAS1D,aAAa,QAAgB,SAAS,IAAI,IAAI;EAQ9C,SAAS,OAAO,QAAgB,MAAM,SAAS,OAAO,IAAI,KAAK;EAO/D,IAAI,WAAW;AACX,UAAO;;EAUX,YAAY,OAAO,QAAgB,MAAM,SAAS,IAAI,IAAI,KAAK;EAO/D,UAAU,KAAa,UAAe,SAAS,IAAI,KAAK,qBAAqB,MAAM,CAAC;EAQpF,eAAe,KAAa,SAAiB,OAAY;AACrD,UAAO,SAAS,MAAM,KAAK,SAAS,qBAAqB,MAAM,CAAC;;EAEvE,CAAC;;AAGN,SAAS,mBAAmB,MAAc;AACtC,KAAI,CAAC,eAAe,KAAK,CAAE,QAAO;CAClC,MAAM,UAAU,KAAK,SAAS,wBAAwB;CACtD,MAAM,OAAO,KAAK,kBAAkB;AACpC,SAAQ,MAAR;EACI,KAAK,yBAAyB,OAAQ,QAAO;EAC7C,KAAK,yBAAyB,KAC1B,KAAI;AACA,UAAO,YAAY,KAAK,MAAM,QAAQ,UAAU,CAAC,CAAC;UAC9C;AACJ,SAAM,IAAI,MAAM,8CAA8C;;EAEtE,KAAK,yBAAyB,OAAQ,QAAO,QAAQ,UAAU;EAC/D,QACI,OAAM,IAAI,MAAM,yCAAyC,OAAO;;;AAI5E,SAAS,qBAAqB,OAAY;AACtC,KAAI,OAAO,SAAS,MAAM,CAAE,QAAO,cAAc,yBAAyB,QAAQ,MAAM;AACxF,KAAI,OAAO,UAAU,SAAU,QAAO,cAAc,yBAAyB,QAAQ,OAAO,KAAK,MAAM,CAAC;AACxG,QAAO,cAAc,yBAAyB,MAAM,OAAO,KAAK,KAAK,UAAU,UAAU,MAAM,CAAC,CAAC,CAAC;;AAGtG,SAAS,eAAe,QAAgB;AACpC,QACI,OAAO,UAAU,2BACd,OAAO,OAAO,kBAAkB,MAChC,OAAO,OAAO,kBAAkB,MAChC,OAAO,OAAO,kBAAkB;;AAI3C,SAAS,cAAc,MAAgC,SAAiB;AACpE,QAAO,OAAO,OAAO;EACjB;EACA,OAAO,GAAG,KAAK;EACf;EACH,CAAC"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { createEnhancedRedisStorage } from "./core.js";
|
|
2
|
-
|
|
3
|
-
//#region src/storage/enhanced/redis/keyed-store.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates a reusable, type-safe Redis-backed storage interface based on `createEnhancedRedisStorage`
|
|
7
|
-
* and a dynamic key-generation function.
|
|
8
|
-
*
|
|
9
|
-
* This utility abstracts away key construction and provides high-level access
|
|
10
|
-
* to Redis operations such as `getItem`, `setItem`, `setItemWithTtl`, and `getItemTtl`.
|
|
11
|
-
* It is ideal for namespaced data, caching, and session handling.
|
|
12
|
-
*
|
|
13
|
-
* @template D - The value type to store
|
|
14
|
-
*
|
|
15
|
-
* @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance
|
|
16
|
-
*
|
|
17
|
-
* @returns A factory that accepts a key generator function and returns a scoped Redis storage interface
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* import { createKeyedEnhancedRedisStore } from '@kikiutils/shared/storage/enhanced/redis';
|
|
22
|
-
*
|
|
23
|
-
* const userStore = createKeyedEnhancedRedisStore<User>(redisStorage)((id: number) => `user:${id}`);
|
|
24
|
-
* await userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
25
|
-
* const user = await userStore.getItem(123);
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
declare function createKeyedEnhancedRedisStore<D = unknown>(storage: ReturnType<typeof createEnhancedRedisStorage>): <P extends any[]>(getKeyFunction: (...args: P) => string) => Readonly<{
|
|
29
|
-
getItem: (...args: P) => Promise<D | null>;
|
|
30
|
-
getItemTtl: (...args: P) => Promise<number>;
|
|
31
|
-
hasItem: (...args: P) => Promise<boolean>;
|
|
32
|
-
removeItem: (...args: P) => Promise<boolean>;
|
|
33
|
-
/**
|
|
34
|
-
* Resolves the storage key from the given arguments.
|
|
35
|
-
*
|
|
36
|
-
* @returns {string} The final string key used internally
|
|
37
|
-
*/
|
|
38
|
-
resolveKey: (...args: P) => string;
|
|
39
|
-
setItem: (value: D, ...args: P) => Promise<"OK">;
|
|
40
|
-
setItemWithTtl(seconds: number, value: D, ...args: P): Promise<"OK">;
|
|
41
|
-
}>;
|
|
42
|
-
//#endregion
|
|
43
|
-
export { createKeyedEnhancedRedisStore };
|
|
44
|
-
//# sourceMappingURL=keyed-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.d.ts","names":[],"sources":["../../../../src/storage/enhanced/redis/keyed-store.ts"],"sourcesContent":[],"mappings":";;;;;;AAyBA;;;;;;;;;;;;;;;;;;;;;AACmE,iBADnD,6BACmD,CAAA,IAAA,OAAA,CAAA,CAAA,OAAA,EADC,UACD,CAAA,OADmB,0BACnB,CAAA,CAAA,EAAA,CAAA,UAAA,GAAA,EAAA,CAAA,CAAA,cAAA,EAAA,CAAA,GAAA,IAAA,EAAZ,CAAY,EAAA,GAAA,MAAA,EAAA,GAAA,QAAA,CAAA;qBACxC,MAAC,QAAA;wBACE,MAAC;qBACJ,MAAC;wBACE,MAAC;;;;;;wBAMD;mBACL,YAAY,MAAC;yCACS,YAAY,IAAC"}
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
//#region src/storage/enhanced/redis/keyed-store.ts
|
|
2
|
-
/**
|
|
3
|
-
* Creates a reusable, type-safe Redis-backed storage interface based on `createEnhancedRedisStorage`
|
|
4
|
-
* and a dynamic key-generation function.
|
|
5
|
-
*
|
|
6
|
-
* This utility abstracts away key construction and provides high-level access
|
|
7
|
-
* to Redis operations such as `getItem`, `setItem`, `setItemWithTtl`, and `getItemTtl`.
|
|
8
|
-
* It is ideal for namespaced data, caching, and session handling.
|
|
9
|
-
*
|
|
10
|
-
* @template D - The value type to store
|
|
11
|
-
*
|
|
12
|
-
* @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance
|
|
13
|
-
*
|
|
14
|
-
* @returns A factory that accepts a key generator function and returns a scoped Redis storage interface
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* import { createKeyedEnhancedRedisStore } from '@kikiutils/shared/storage/enhanced/redis';
|
|
19
|
-
*
|
|
20
|
-
* const userStore = createKeyedEnhancedRedisStore<User>(redisStorage)((id: number) => `user:${id}`);
|
|
21
|
-
* await userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
22
|
-
* const user = await userStore.getItem(123);
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
function createKeyedEnhancedRedisStore(storage) {
|
|
26
|
-
return (getKeyFunction) => Object.freeze({
|
|
27
|
-
getItem: (...args) => storage.getItem(getKeyFunction(...args)),
|
|
28
|
-
getItemTtl: (...args) => storage.getItemTtl(getKeyFunction(...args)),
|
|
29
|
-
hasItem: (...args) => storage.hasItem(getKeyFunction(...args)),
|
|
30
|
-
removeItem: (...args) => storage.removeItem(getKeyFunction(...args)),
|
|
31
|
-
resolveKey: (...args) => getKeyFunction(...args),
|
|
32
|
-
setItem: (value, ...args) => storage.setItem(getKeyFunction(...args), value),
|
|
33
|
-
setItemWithTtl(seconds, value, ...args) {
|
|
34
|
-
return storage.setItemWithTtl(getKeyFunction(...args), seconds, value);
|
|
35
|
-
}
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
//#endregion
|
|
40
|
-
export { createKeyedEnhancedRedisStore };
|
|
41
|
-
//# sourceMappingURL=keyed-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.js","names":[],"sources":["../../../../src/storage/enhanced/redis/keyed-store.ts"],"sourcesContent":["import type { createEnhancedRedisStorage } from './core';\n\n/**\n * Creates a reusable, type-safe Redis-backed storage interface based on `createEnhancedRedisStorage`\n * and a dynamic key-generation function.\n *\n * This utility abstracts away key construction and provides high-level access\n * to Redis operations such as `getItem`, `setItem`, `setItemWithTtl`, and `getItemTtl`.\n * It is ideal for namespaced data, caching, and session handling.\n *\n * @template D - The value type to store\n *\n * @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance\n *\n * @returns A factory that accepts a key generator function and returns a scoped Redis storage interface\n *\n * @example\n * ```typescript\n * import { createKeyedEnhancedRedisStore } from '@kikiutils/shared/storage/enhanced/redis';\n *\n * const userStore = createKeyedEnhancedRedisStore<User>(redisStorage)((id: number) => `user:${id}`);\n * await userStore.setItem({ id: 123, name: 'user' }, 123);\n * const user = await userStore.getItem(123);\n * ```\n */\nexport function createKeyedEnhancedRedisStore<D = unknown>(storage: ReturnType<typeof createEnhancedRedisStorage>) {\n return <P extends any[]>(getKeyFunction: (...args: P) => string) => Object.freeze({\n getItem: (...args: P) => storage.getItem<D>(getKeyFunction(...args)),\n getItemTtl: (...args: P) => storage.getItemTtl(getKeyFunction(...args)),\n hasItem: (...args: P) => storage.hasItem(getKeyFunction(...args)),\n removeItem: (...args: P) => storage.removeItem(getKeyFunction(...args)),\n /**\n * Resolves the storage key from the given arguments.\n *\n * @returns {string} The final string key used internally\n */\n resolveKey: (...args: P) => getKeyFunction(...args),\n setItem: (value: D, ...args: P) => storage.setItem(getKeyFunction(...args), value),\n setItemWithTtl(seconds: number, value: D, ...args: P) {\n return storage.setItemWithTtl(getKeyFunction(...args), seconds, value);\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,8BAA2C,SAAwD;AAC/G,SAAyB,mBAA2C,OAAO,OAAO;EAC9E,UAAU,GAAG,SAAY,QAAQ,QAAW,eAAe,GAAG,KAAK,CAAC;EACpE,aAAa,GAAG,SAAY,QAAQ,WAAW,eAAe,GAAG,KAAK,CAAC;EACvE,UAAU,GAAG,SAAY,QAAQ,QAAQ,eAAe,GAAG,KAAK,CAAC;EACjE,aAAa,GAAG,SAAY,QAAQ,WAAW,eAAe,GAAG,KAAK,CAAC;EAMvE,aAAa,GAAG,SAAY,eAAe,GAAG,KAAK;EACnD,UAAU,OAAU,GAAG,SAAY,QAAQ,QAAQ,eAAe,GAAG,KAAK,EAAE,MAAM;EAClF,eAAe,SAAiB,OAAU,GAAG,MAAS;AAClD,UAAO,QAAQ,eAAe,eAAe,GAAG,KAAK,EAAE,SAAS,MAAM;;EAE7E,CAAC"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { LRUCache } from "lru-cache";
|
|
2
|
-
|
|
3
|
-
//#region src/storage/lru/keyed-store.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Creates a reusable, type-safe keyed store wrapper for an LRUCache instance.
|
|
7
|
-
*
|
|
8
|
-
* This utility allows structured access to cache entries using a dynamic key-generation function.
|
|
9
|
-
*
|
|
10
|
-
* @template D - The specific value type exposed by this store (must extend `V`)
|
|
11
|
-
*
|
|
12
|
-
* @param {LRUCache<any, any, any>} 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
|
-
* import { createKeyedLruStore } from '@kikiutils/shared/storage/lru/keyed-store';
|
|
19
|
-
*
|
|
20
|
-
* const lruCache = new LRUCache({ max: 5000 });
|
|
21
|
-
* const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);
|
|
22
|
-
* userStore.setItem({ id: 1 }, 1);
|
|
23
|
-
* const user = userStore.getItem(1);
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
declare function createKeyedLruStore<D = unknown>(lruInstance: LRUCache<any, any, any>): <P extends any[]>(getKeyFunction: (...args: P) => string) => Readonly<{
|
|
27
|
-
/**
|
|
28
|
-
* Return a value from the cache. Will update the recency of the cache entry found.
|
|
29
|
-
*
|
|
30
|
-
* If the key is not found, returns `null`.
|
|
31
|
-
*/
|
|
32
|
-
getItem(...args: P): NonNullable<D> | null;
|
|
33
|
-
getItemTtl: (...args: P) => number;
|
|
34
|
-
hasItem: (...args: P) => boolean;
|
|
35
|
-
removeItem: (...args: P) => boolean;
|
|
36
|
-
/**
|
|
37
|
-
* Resolves the full cache key from the given arguments.
|
|
38
|
-
*/
|
|
39
|
-
resolveKey: (...args: P) => string;
|
|
40
|
-
setItem(value: D, ...args: P): void;
|
|
41
|
-
}>;
|
|
42
|
-
//#endregion
|
|
43
|
-
export { createKeyedLruStore };
|
|
44
|
-
//# sourceMappingURL=keyed-store.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.d.ts","names":[],"sources":["../../../src/storage/lru/keyed-store.ts"],"sourcesContent":[],"mappings":";;;;;;AAuBA;;;;;;;;;;;;;;;;;;;iBAAgB,8CAA8C,sEACP,iBAAY;;;;;;mBAM1C,IAAC,YAAA;wBAII;qBACH;wBACG;;;;wBAIA;iBACP,YAAY"}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
//#region src/storage/lru/keyed-store.ts
|
|
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
|
-
*
|
|
9
|
-
* @param {LRUCache<any, any, any>} lruInstance - An instance of `lru-cache`
|
|
10
|
-
*
|
|
11
|
-
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import { createKeyedLruStore } from '@kikiutils/shared/storage/lru/keyed-store';
|
|
16
|
-
*
|
|
17
|
-
* const lruCache = new LRUCache({ max: 5000 });
|
|
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
|
-
function createKeyedLruStore(lruInstance) {
|
|
24
|
-
return (getKeyFunction) => Object.freeze({
|
|
25
|
-
getItem(...args) {
|
|
26
|
-
return lruInstance.get(getKeyFunction(...args)) ?? null;
|
|
27
|
-
},
|
|
28
|
-
getItemTtl: (...args) => lruInstance.getRemainingTTL(getKeyFunction(...args)),
|
|
29
|
-
hasItem: (...args) => lruInstance.has(getKeyFunction(...args)),
|
|
30
|
-
removeItem: (...args) => lruInstance.delete(getKeyFunction(...args)),
|
|
31
|
-
resolveKey: (...args) => getKeyFunction(...args),
|
|
32
|
-
setItem(value, ...args) {
|
|
33
|
-
lruInstance.set(getKeyFunction(...args), value);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//#endregion
|
|
39
|
-
export { createKeyedLruStore };
|
|
40
|
-
//# sourceMappingURL=keyed-store.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.js","names":[],"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 *\n * @param {LRUCache<any, any, any>} 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 * import { createKeyedLruStore } from '@kikiutils/shared/storage/lru/keyed-store';\n *\n * const lruCache = new LRUCache({ max: 5000 });\n * const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);\n * userStore.setItem({ id: 1 }, 1);\n * const user = userStore.getItem(1);\n * ```\n */\nexport function createKeyedLruStore<D = unknown>(lruInstance: LRUCache<any, any, any>) {\n return <P extends any[]>(getKeyFunction: (...args: P) => string) => 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 as D ?? 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) {\n lruInstance.set(getKeyFunction(...args), value);\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,oBAAiC,aAAsC;AACnF,SAAyB,mBAA2C,OAAO,OAAO;EAM9E,QAAQ,GAAG,MAAS;AAEhB,UADiB,YAAY,IAAI,eAAe,GAAG,KAAK,CAAC,IACjC;;EAE5B,aAAa,GAAG,SAAY,YAAY,gBAAgB,eAAe,GAAG,KAAK,CAAC;EAChF,UAAU,GAAG,SAAY,YAAY,IAAI,eAAe,GAAG,KAAK,CAAC;EACjE,aAAa,GAAG,SAAY,YAAY,OAAO,eAAe,GAAG,KAAK,CAAC;EAIvE,aAAa,GAAG,SAAY,eAAe,GAAG,KAAK;EACnD,QAAQ,OAAU,GAAG,MAAS;AAC1B,eAAY,IAAI,eAAe,GAAG,KAAK,EAAE,MAAM;;EAEtD,CAAC"}
|
|
@@ -1,106 +0,0 @@
|
|
|
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
|
-
* import { enhancedLocalStorage } from '@kikiutils/shared/storage/enhanced/local';
|
|
24
|
-
*
|
|
25
|
-
* enhancedLocalStorage.setItem('user', { name: 'user', createdAt: new Date() });
|
|
26
|
-
* const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
export const enhancedLocalStorage = Object.freeze({
|
|
30
|
-
/**
|
|
31
|
-
* Removes all items from localStorage.
|
|
32
|
-
*/
|
|
33
|
-
clear: () => window.localStorage.clear(),
|
|
34
|
-
/**
|
|
35
|
-
* Retrieves a value by key and decodes it using SuperJSON or raw string.
|
|
36
|
-
*
|
|
37
|
-
* @template T - The expected type of the value
|
|
38
|
-
*
|
|
39
|
-
* @param {string} key - The key of the value to retrieve
|
|
40
|
-
*
|
|
41
|
-
* @returns {null | T} The decoded value or null if not found
|
|
42
|
-
*/
|
|
43
|
-
getItem<T = unknown>(key: string) {
|
|
44
|
-
const rawValue = window.localStorage.getItem(key);
|
|
45
|
-
return rawValue ? decodeStorageValue(rawValue) as T : null;
|
|
46
|
-
},
|
|
47
|
-
/**
|
|
48
|
-
* Checks whether a key exists in localStorage.
|
|
49
|
-
*
|
|
50
|
-
* @param {string} key - The key to check
|
|
51
|
-
*
|
|
52
|
-
* @returns {boolean} True if the key exists, false otherwise
|
|
53
|
-
*/
|
|
54
|
-
hasItem: (key: string) => window.localStorage.getItem(key) !== null,
|
|
55
|
-
/**
|
|
56
|
-
* Returns the number of items stored in localStorage.
|
|
57
|
-
*
|
|
58
|
-
* @returns {number} The number of items stored in localStorage
|
|
59
|
-
*/
|
|
60
|
-
get length() {
|
|
61
|
-
return window.localStorage.length;
|
|
62
|
-
},
|
|
63
|
-
/**
|
|
64
|
-
* Removes a specific key from localStorage.
|
|
65
|
-
*
|
|
66
|
-
* @param {string} key - The key to remove
|
|
67
|
-
*/
|
|
68
|
-
removeItem: (key: string) => window.localStorage.removeItem(key),
|
|
69
|
-
/**
|
|
70
|
-
* Stores a value in localStorage with automatic serialization.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} key - The key to store the value under
|
|
73
|
-
* @param {any} value - The value to store
|
|
74
|
-
*/
|
|
75
|
-
setItem: (key: string, value: any) => window.localStorage.setItem(key, encodeToStorageValue(value)),
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
function decodeStorageValue(data: string) {
|
|
79
|
-
if (!isCustomFormat(data)) return data;
|
|
80
|
-
const payload = data.substring(customValueHeaderLength);
|
|
81
|
-
const type = data.charAt(customValueHeader.length);
|
|
82
|
-
switch (type) {
|
|
83
|
-
case StorageValueEncodingType.Json:
|
|
84
|
-
try {
|
|
85
|
-
return deserialize(JSON.parse(payload));
|
|
86
|
-
} catch {
|
|
87
|
-
throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload');
|
|
88
|
-
}
|
|
89
|
-
case StorageValueEncodingType.String: return payload;
|
|
90
|
-
default:
|
|
91
|
-
throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}`);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function encodeToStorageValue(value: any) {
|
|
96
|
-
if (typeof value === 'string') return toCustomValue(StorageValueEncodingType.String, value);
|
|
97
|
-
return toCustomValue(StorageValueEncodingType.Json, JSON.stringify(serialize(value)));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function isCustomFormat(data: string) {
|
|
101
|
-
return (
|
|
102
|
-
data.length >= customValueHeaderLength
|
|
103
|
-
&& data[0] === customValueHeader[0]
|
|
104
|
-
&& data[1] === customValueHeader[1]
|
|
105
|
-
);
|
|
106
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
* import { createKeyedEnhancedLocalStore } from '@kikiutils/shared/storage/enhanced/local';
|
|
17
|
-
*
|
|
18
|
-
* const userStore = createKeyedEnhancedLocalStore<User>()((id: number) => `user:${id}`);
|
|
19
|
-
* userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
20
|
-
* const user = userStore.getItem(123);
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export function createKeyedEnhancedLocalStore<D = unknown>() {
|
|
24
|
-
return <P extends any[]>(getKeyFunction: (...args: P) => string) => Object.freeze({
|
|
25
|
-
getItem: (...args: P) => enhancedLocalStorage.getItem<D>(getKeyFunction(...args)),
|
|
26
|
-
hasItem: (...args: P) => enhancedLocalStorage.hasItem(getKeyFunction(...args)),
|
|
27
|
-
removeItem: (...args: P) => enhancedLocalStorage.removeItem(getKeyFunction(...args)),
|
|
28
|
-
/**
|
|
29
|
-
* Resolves the storage key from the given arguments.
|
|
30
|
-
*
|
|
31
|
-
* @returns {string} The final string key used internally
|
|
32
|
-
*/
|
|
33
|
-
resolveKey: (...args: P) => getKeyFunction(...args),
|
|
34
|
-
setItem: (value: D, ...args: P) => enhancedLocalStorage.setItem(getKeyFunction(...args), value),
|
|
35
|
-
});
|
|
36
|
-
}
|