@kikiutils/shared 11.0.0 → 13.0.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/README.md +10 -1
- package/dist/buffer.d.ts +7 -3
- package/dist/buffer.d.ts.map +1 -1
- package/dist/buffer.js +42 -0
- package/dist/buffer.js.map +1 -0
- package/dist/clipboard.d.ts +8 -6
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +86 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/consola.d.ts +8 -2
- package/dist/consola.d.ts.map +1 -1
- package/dist/consola.js +32 -0
- package/dist/consola.js.map +1 -0
- package/dist/crypto-hash.d.ts +16 -26
- package/dist/crypto-hash.d.ts.map +1 -1
- package/dist/crypto-hash.js +37 -0
- package/dist/crypto-hash.js.map +1 -0
- package/dist/datetime.d.ts +13 -9
- package/dist/datetime.d.ts.map +1 -1
- package/dist/datetime.js +131 -0
- package/dist/datetime.js.map +1 -0
- package/dist/element-plus.d.ts +8 -4
- package/dist/element-plus.d.ts.map +1 -1
- package/dist/element-plus.js +37 -0
- package/dist/element-plus.js.map +1 -0
- package/dist/enum.d.ts +6 -3
- package/dist/enum.d.ts.map +1 -1
- package/dist/enum.js +108 -0
- package/dist/enum.js.map +1 -0
- package/dist/env.d.ts +12 -9
- package/dist/env.d.ts.map +1 -1
- package/dist/env.js +51 -0
- package/dist/env.js.map +1 -0
- package/dist/general.d.ts +5 -2
- package/dist/general.d.ts.map +1 -1
- package/dist/general.js +8 -0
- package/dist/general.js.map +1 -0
- package/dist/hash.d.ts +7 -4
- package/dist/hash.d.ts.map +1 -1
- package/dist/hash.js +20 -0
- package/dist/hash.js.map +1 -0
- package/dist/math.d.ts +20 -17
- package/dist/math.d.ts.map +1 -1
- package/dist/math.js +36 -0
- package/dist/math.js.map +1 -0
- package/dist/number.d.ts +7 -2
- package/dist/number.d.ts.map +1 -1
- package/dist/number.js +32 -0
- package/dist/number.js.map +1 -0
- package/dist/object.d.ts +4 -1
- package/dist/object.d.ts.map +1 -1
- package/dist/object.js +42 -0
- package/dist/object.js.map +1 -0
- package/dist/pino.d.ts +8 -2
- package/dist/pino.d.ts.map +1 -1
- package/dist/pino.js +40 -0
- package/dist/pino.js.map +1 -0
- package/dist/random.d.ts +4 -1
- package/dist/random.d.ts.map +1 -1
- package/dist/random.js +31 -0
- package/dist/random.js.map +1 -0
- package/dist/storage/enhanced/local/core.d.ts +45 -42
- package/dist/storage/enhanced/local/core.d.ts.map +1 -1
- package/dist/storage/enhanced/local/core.js +63 -0
- package/dist/storage/enhanced/local/core.js.map +1 -0
- package/dist/storage/enhanced/local/index.d.ts +3 -3
- package/dist/storage/enhanced/local/index.js +4 -0
- package/dist/storage/enhanced/local/keyed-store.d.ts +14 -11
- package/dist/storage/enhanced/local/keyed-store.d.ts.map +1 -1
- package/dist/storage/enhanced/local/keyed-store.js +36 -0
- package/dist/storage/enhanced/local/keyed-store.js.map +1 -0
- package/dist/storage/enhanced/redis/core.d.ts +64 -59
- package/dist/storage/enhanced/redis/core.d.ts.map +1 -1
- package/dist/storage/enhanced/redis/core.js +85 -0
- package/dist/storage/enhanced/redis/core.js.map +1 -0
- package/dist/storage/enhanced/redis/index.d.ts +3 -3
- package/dist/storage/enhanced/redis/index.js +4 -0
- package/dist/storage/enhanced/redis/keyed-store.d.ts +19 -14
- package/dist/storage/enhanced/redis/keyed-store.d.ts.map +1 -1
- package/dist/storage/enhanced/redis/keyed-store.js +41 -0
- package/dist/storage/enhanced/redis/keyed-store.js.map +1 -0
- package/dist/storage/lru/keyed-store.d.ts +21 -16
- package/dist/storage/lru/keyed-store.d.ts.map +1 -1
- package/dist/storage/lru/keyed-store.js +40 -0
- package/dist/storage/lru/keyed-store.js.map +1 -0
- package/dist/string.d.ts +5 -2
- package/dist/string.d.ts.map +1 -1
- package/dist/string.js +42 -0
- package/dist/string.js.map +1 -0
- package/dist/url.d.ts +4 -1
- package/dist/url.d.ts.map +1 -1
- package/dist/url.js +21 -0
- package/dist/url.js.map +1 -0
- package/dist/vue.d.ts +10 -5
- package/dist/vue.d.ts.map +1 -1
- package/dist/vue.js +58 -0
- package/dist/vue.js.map +1 -0
- package/dist/web.d.ts +5 -2
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +32 -0
- package/dist/web.js.map +1 -0
- package/package.json +108 -32
- package/src/hash.ts +19 -5
- package/src/vue.ts +1 -1
- package/src/web.ts +1 -1
- package/dist/buffer.cjs +0 -43
- package/dist/buffer.cjs.map +0 -1
- package/dist/buffer.mjs +0 -41
- package/dist/buffer.mjs.map +0 -1
- package/dist/clipboard.cjs +0 -91
- package/dist/clipboard.cjs.map +0 -1
- package/dist/clipboard.mjs +0 -88
- package/dist/clipboard.mjs.map +0 -1
- package/dist/consola.cjs +0 -35
- package/dist/consola.cjs.map +0 -1
- package/dist/consola.mjs +0 -32
- package/dist/consola.mjs.map +0 -1
- package/dist/crypto-hash.cjs +0 -61
- package/dist/crypto-hash.cjs.map +0 -1
- package/dist/crypto-hash.mjs +0 -50
- package/dist/crypto-hash.mjs.map +0 -1
- package/dist/datetime.cjs +0 -134
- package/dist/datetime.cjs.map +0 -1
- package/dist/datetime.mjs +0 -130
- package/dist/datetime.mjs.map +0 -1
- package/dist/element-plus.cjs +0 -37
- package/dist/element-plus.cjs.map +0 -1
- package/dist/element-plus.mjs +0 -35
- package/dist/element-plus.mjs.map +0 -1
- package/dist/enum.cjs +0 -112
- package/dist/enum.cjs.map +0 -1
- package/dist/enum.mjs +0 -108
- package/dist/enum.mjs.map +0 -1
- package/dist/env.cjs +0 -53
- package/dist/env.cjs.map +0 -1
- package/dist/env.mjs +0 -50
- package/dist/env.mjs.map +0 -1
- package/dist/general.cjs +0 -10
- package/dist/general.cjs.map +0 -1
- package/dist/general.mjs +0 -8
- package/dist/general.mjs.map +0 -1
- package/dist/hash.cjs +0 -28
- package/dist/hash.cjs.map +0 -1
- package/dist/hash.mjs +0 -23
- package/dist/hash.mjs.map +0 -1
- package/dist/math.cjs +0 -38
- package/dist/math.cjs.map +0 -1
- package/dist/math.mjs +0 -36
- package/dist/math.mjs.map +0 -1
- package/dist/number.cjs +0 -32
- package/dist/number.cjs.map +0 -1
- package/dist/number.mjs +0 -30
- package/dist/number.mjs.map +0 -1
- package/dist/object.cjs +0 -55
- package/dist/object.cjs.map +0 -1
- package/dist/object.mjs +0 -53
- package/dist/object.mjs.map +0 -1
- package/dist/pino.cjs +0 -42
- package/dist/pino.cjs.map +0 -1
- package/dist/pino.mjs +0 -39
- package/dist/pino.mjs.map +0 -1
- package/dist/random.cjs +0 -31
- package/dist/random.cjs.map +0 -1
- package/dist/random.mjs +0 -29
- package/dist/random.mjs.map +0 -1
- package/dist/storage/enhanced/local/core.cjs +0 -105
- package/dist/storage/enhanced/local/core.cjs.map +0 -1
- package/dist/storage/enhanced/local/core.mjs +0 -103
- package/dist/storage/enhanced/local/core.mjs.map +0 -1
- package/dist/storage/enhanced/local/index.cjs +0 -10
- package/dist/storage/enhanced/local/index.cjs.map +0 -1
- package/dist/storage/enhanced/local/index.d.ts.map +0 -1
- package/dist/storage/enhanced/local/index.mjs +0 -3
- package/dist/storage/enhanced/local/index.mjs.map +0 -1
- package/dist/storage/enhanced/local/keyed-store.cjs +0 -41
- package/dist/storage/enhanced/local/keyed-store.cjs.map +0 -1
- package/dist/storage/enhanced/local/keyed-store.mjs +0 -39
- package/dist/storage/enhanced/local/keyed-store.mjs.map +0 -1
- package/dist/storage/enhanced/redis/core.cjs +0 -144
- package/dist/storage/enhanced/redis/core.cjs.map +0 -1
- package/dist/storage/enhanced/redis/core.mjs +0 -142
- package/dist/storage/enhanced/redis/core.mjs.map +0 -1
- package/dist/storage/enhanced/redis/index.cjs +0 -10
- package/dist/storage/enhanced/redis/index.cjs.map +0 -1
- package/dist/storage/enhanced/redis/index.d.ts.map +0 -1
- package/dist/storage/enhanced/redis/index.mjs +0 -3
- package/dist/storage/enhanced/redis/index.mjs.map +0 -1
- package/dist/storage/enhanced/redis/keyed-store.cjs +0 -46
- package/dist/storage/enhanced/redis/keyed-store.cjs.map +0 -1
- package/dist/storage/enhanced/redis/keyed-store.mjs +0 -44
- package/dist/storage/enhanced/redis/keyed-store.mjs.map +0 -1
- package/dist/storage/lru/keyed-store.cjs +0 -49
- package/dist/storage/lru/keyed-store.cjs.map +0 -1
- package/dist/storage/lru/keyed-store.mjs +0 -47
- package/dist/storage/lru/keyed-store.mjs.map +0 -1
- package/dist/string.cjs +0 -45
- package/dist/string.cjs.map +0 -1
- package/dist/string.mjs +0 -43
- package/dist/string.mjs.map +0 -1
- package/dist/url.cjs +0 -21
- package/dist/url.cjs.map +0 -1
- package/dist/url.mjs +0 -19
- package/dist/url.mjs.map +0 -1
- package/dist/vue.cjs +0 -64
- package/dist/vue.cjs.map +0 -1
- package/dist/vue.mjs +0 -59
- package/dist/vue.mjs.map +0 -1
- package/dist/web.cjs +0 -35
- package/dist/web.cjs.map +0 -1
- package/dist/web.mjs +0 -32
- package/dist/web.mjs.map +0 -1
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const node_buffer = require('node:buffer');
|
|
4
|
-
const ioredis = require('ioredis');
|
|
5
|
-
const superjson = require('superjson');
|
|
6
|
-
|
|
7
|
-
var StorageValueEncodingType;
|
|
8
|
-
(function (StorageValueEncodingType) {
|
|
9
|
-
StorageValueEncodingType[StorageValueEncodingType["Buffer"] = 0] = "Buffer";
|
|
10
|
-
StorageValueEncodingType[StorageValueEncodingType["Json"] = 1] = "Json";
|
|
11
|
-
StorageValueEncodingType[StorageValueEncodingType["String"] = 2] = "String";
|
|
12
|
-
})(StorageValueEncodingType || (StorageValueEncodingType = {}));
|
|
13
|
-
const customValueHeader = node_buffer.Buffer.of(0xE2, 0x81, 0xA0);
|
|
14
|
-
const customValueHeaderLength = customValueHeader.byteLength + 1;
|
|
15
|
-
/**
|
|
16
|
-
* Creates an enhanced Redis-based storage interface using SuperJSON encoding.
|
|
17
|
-
*
|
|
18
|
-
* This utility provides a typed, serializable key-value store backed by Redis,
|
|
19
|
-
* supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).
|
|
20
|
-
*
|
|
21
|
-
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string
|
|
22
|
-
*
|
|
23
|
-
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* import { createEnhancedRedisStorage } from '@kikiutils/shared/storage/enhanced/redis';
|
|
28
|
-
*
|
|
29
|
-
* const redisStorage = createEnhancedRedisStorage('redis://localhost');
|
|
30
|
-
* await redisStorage.setItem('user:1', { name: 'user' });
|
|
31
|
-
* const user = await redisStorage.getItem<{ name: string }>('user:1');
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
function createEnhancedRedisStorage(ioRedisInstanceOrUrl) {
|
|
35
|
-
const instance = ioRedisInstanceOrUrl instanceof ioredis.Redis ? ioRedisInstanceOrUrl : new ioredis.Redis(ioRedisInstanceOrUrl);
|
|
36
|
-
return Object.freeze({
|
|
37
|
-
/**
|
|
38
|
-
* Retrieves a value from Redis and decodes it.
|
|
39
|
-
*
|
|
40
|
-
* @template T - The expected return type
|
|
41
|
-
*
|
|
42
|
-
* @param {string} key - The Redis key
|
|
43
|
-
*
|
|
44
|
-
* @returns {Promise<null | T>} The decoded value or null if not found
|
|
45
|
-
*/
|
|
46
|
-
async getItem(key) {
|
|
47
|
-
const rawValue = await instance.getBuffer(key);
|
|
48
|
-
return rawValue ? decodeStorageValue(rawValue) : null;
|
|
49
|
-
},
|
|
50
|
-
/**
|
|
51
|
-
* Gets the remaining TTL (in seconds) for a given key.
|
|
52
|
-
*
|
|
53
|
-
* @param {string} key - The Redis key
|
|
54
|
-
*
|
|
55
|
-
* @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set
|
|
56
|
-
*/
|
|
57
|
-
getItemTtl: (key) => instance.ttl(key),
|
|
58
|
-
/**
|
|
59
|
-
* Checks whether a key exists in Redis.
|
|
60
|
-
*
|
|
61
|
-
* @param {string} key - The Redis key
|
|
62
|
-
*
|
|
63
|
-
* @returns {Promise<boolean>} True if the key exists, false otherwise
|
|
64
|
-
*/
|
|
65
|
-
hasItem: async (key) => await instance.exists(key) === 1,
|
|
66
|
-
/**
|
|
67
|
-
* The underlying Redis instance, exposed for advanced operations.
|
|
68
|
-
* Use with caution; most use cases should rely on the wrapper methods.
|
|
69
|
-
*
|
|
70
|
-
* @returns {Redis} The underlying Redis instance
|
|
71
|
-
*/
|
|
72
|
-
get instance() {
|
|
73
|
-
return instance;
|
|
74
|
-
},
|
|
75
|
-
/**
|
|
76
|
-
* Removes a key from Redis.
|
|
77
|
-
*
|
|
78
|
-
* @param {string} key - The Redis key to delete
|
|
79
|
-
*
|
|
80
|
-
* @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,
|
|
81
|
-
* or `false` if it did not exist.
|
|
82
|
-
*/
|
|
83
|
-
removeItem: async (key) => await instance.del(key) === 1,
|
|
84
|
-
/**
|
|
85
|
-
* Stores a value in Redis without expiration.
|
|
86
|
-
*
|
|
87
|
-
* @param {string} key - The Redis key
|
|
88
|
-
* @param {any} value - The value to store. Will be serialized
|
|
89
|
-
*/
|
|
90
|
-
setItem: (key, value) => instance.set(key, encodeToStorageValue(value)),
|
|
91
|
-
/**
|
|
92
|
-
* Stores a value in Redis with a time-to-live (TTL).
|
|
93
|
-
*
|
|
94
|
-
* @param {string} key - The Redis key
|
|
95
|
-
* @param {number} seconds - Expiration time in seconds
|
|
96
|
-
* @param {any} value - The value to store. Will be serialized
|
|
97
|
-
*/
|
|
98
|
-
setItemWithTtl(key, seconds, value) {
|
|
99
|
-
return instance.setex(key, seconds, encodeToStorageValue(value));
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
function decodeStorageValue(data) {
|
|
104
|
-
if (!isCustomFormat(data))
|
|
105
|
-
return data;
|
|
106
|
-
const payload = data.subarray(customValueHeaderLength);
|
|
107
|
-
const type = data[customValueHeader.byteLength];
|
|
108
|
-
switch (type) {
|
|
109
|
-
case StorageValueEncodingType.Buffer: return payload;
|
|
110
|
-
case StorageValueEncodingType.Json:
|
|
111
|
-
try {
|
|
112
|
-
return superjson.deserialize(JSON.parse(payload.toString()));
|
|
113
|
-
}
|
|
114
|
-
catch {
|
|
115
|
-
throw new Error('[RedisStorage] Failed to parse JSON payload');
|
|
116
|
-
}
|
|
117
|
-
case StorageValueEncodingType.String: return payload.toString();
|
|
118
|
-
default:
|
|
119
|
-
throw new Error(`[RedisStorage] Unknown encoding type: ${type}`);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
function encodeToStorageValue(value) {
|
|
123
|
-
if (node_buffer.Buffer.isBuffer(value))
|
|
124
|
-
return toCustomValue(StorageValueEncodingType.Buffer, value);
|
|
125
|
-
if (typeof value === 'string')
|
|
126
|
-
return toCustomValue(StorageValueEncodingType.String, node_buffer.Buffer.from(value));
|
|
127
|
-
return toCustomValue(StorageValueEncodingType.Json, node_buffer.Buffer.from(JSON.stringify(superjson.serialize(value))));
|
|
128
|
-
}
|
|
129
|
-
function isCustomFormat(buffer) {
|
|
130
|
-
return (buffer.length >= customValueHeaderLength
|
|
131
|
-
&& buffer[0] === customValueHeader[0]
|
|
132
|
-
&& buffer[1] === customValueHeader[1]
|
|
133
|
-
&& buffer[2] === customValueHeader[2]);
|
|
134
|
-
}
|
|
135
|
-
function toCustomValue(type, payload) {
|
|
136
|
-
return node_buffer.Buffer.concat([
|
|
137
|
-
customValueHeader,
|
|
138
|
-
node_buffer.Buffer.of(type),
|
|
139
|
-
payload,
|
|
140
|
-
]);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
exports.createEnhancedRedisStorage = createEnhancedRedisStorage;
|
|
144
|
-
//# sourceMappingURL=core.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"core.cjs","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"],"names":["Buffer","Redis","deserialize","serialize"],"mappings":"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"}
|
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { Buffer } from 'node:buffer';
|
|
2
|
-
import { Redis } from 'ioredis';
|
|
3
|
-
import { serialize, deserialize } from 'superjson';
|
|
4
|
-
|
|
5
|
-
var StorageValueEncodingType;
|
|
6
|
-
(function (StorageValueEncodingType) {
|
|
7
|
-
StorageValueEncodingType[StorageValueEncodingType["Buffer"] = 0] = "Buffer";
|
|
8
|
-
StorageValueEncodingType[StorageValueEncodingType["Json"] = 1] = "Json";
|
|
9
|
-
StorageValueEncodingType[StorageValueEncodingType["String"] = 2] = "String";
|
|
10
|
-
})(StorageValueEncodingType || (StorageValueEncodingType = {}));
|
|
11
|
-
const customValueHeader = Buffer.of(0xE2, 0x81, 0xA0);
|
|
12
|
-
const customValueHeaderLength = customValueHeader.byteLength + 1;
|
|
13
|
-
/**
|
|
14
|
-
* Creates an enhanced Redis-based storage interface using SuperJSON encoding.
|
|
15
|
-
*
|
|
16
|
-
* This utility provides a typed, serializable key-value store backed by Redis,
|
|
17
|
-
* supporting TTL operations and safe deserialization of complex types (e.g. Date, Map).
|
|
18
|
-
*
|
|
19
|
-
* @param {Redis | string} ioRedisInstanceOrUrl - Either an existing `ioredis` instance or a Redis connection string
|
|
20
|
-
*
|
|
21
|
-
* @returns A frozen object that wraps Redis commands with typed get/set logic and encoding
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* import { createEnhancedRedisStorage } from '@kikiutils/shared/storage/enhanced/redis';
|
|
26
|
-
*
|
|
27
|
-
* const redisStorage = createEnhancedRedisStorage('redis://localhost');
|
|
28
|
-
* await redisStorage.setItem('user:1', { name: 'user' });
|
|
29
|
-
* const user = await redisStorage.getItem<{ name: string }>('user:1');
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
function createEnhancedRedisStorage(ioRedisInstanceOrUrl) {
|
|
33
|
-
const instance = ioRedisInstanceOrUrl instanceof Redis ? ioRedisInstanceOrUrl : new Redis(ioRedisInstanceOrUrl);
|
|
34
|
-
return Object.freeze({
|
|
35
|
-
/**
|
|
36
|
-
* Retrieves a value from Redis and decodes it.
|
|
37
|
-
*
|
|
38
|
-
* @template T - The expected return type
|
|
39
|
-
*
|
|
40
|
-
* @param {string} key - The Redis key
|
|
41
|
-
*
|
|
42
|
-
* @returns {Promise<null | T>} The decoded value or null if not found
|
|
43
|
-
*/
|
|
44
|
-
async getItem(key) {
|
|
45
|
-
const rawValue = await instance.getBuffer(key);
|
|
46
|
-
return rawValue ? decodeStorageValue(rawValue) : null;
|
|
47
|
-
},
|
|
48
|
-
/**
|
|
49
|
-
* Gets the remaining TTL (in seconds) for a given key.
|
|
50
|
-
*
|
|
51
|
-
* @param {string} key - The Redis key
|
|
52
|
-
*
|
|
53
|
-
* @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set
|
|
54
|
-
*/
|
|
55
|
-
getItemTtl: (key) => instance.ttl(key),
|
|
56
|
-
/**
|
|
57
|
-
* Checks whether a key exists in Redis.
|
|
58
|
-
*
|
|
59
|
-
* @param {string} key - The Redis key
|
|
60
|
-
*
|
|
61
|
-
* @returns {Promise<boolean>} True if the key exists, false otherwise
|
|
62
|
-
*/
|
|
63
|
-
hasItem: async (key) => await instance.exists(key) === 1,
|
|
64
|
-
/**
|
|
65
|
-
* The underlying Redis instance, exposed for advanced operations.
|
|
66
|
-
* Use with caution; most use cases should rely on the wrapper methods.
|
|
67
|
-
*
|
|
68
|
-
* @returns {Redis} The underlying Redis instance
|
|
69
|
-
*/
|
|
70
|
-
get instance() {
|
|
71
|
-
return instance;
|
|
72
|
-
},
|
|
73
|
-
/**
|
|
74
|
-
* Removes a key from Redis.
|
|
75
|
-
*
|
|
76
|
-
* @param {string} key - The Redis key to delete
|
|
77
|
-
*
|
|
78
|
-
* @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,
|
|
79
|
-
* or `false` if it did not exist.
|
|
80
|
-
*/
|
|
81
|
-
removeItem: async (key) => await instance.del(key) === 1,
|
|
82
|
-
/**
|
|
83
|
-
* Stores a value in Redis without expiration.
|
|
84
|
-
*
|
|
85
|
-
* @param {string} key - The Redis key
|
|
86
|
-
* @param {any} value - The value to store. Will be serialized
|
|
87
|
-
*/
|
|
88
|
-
setItem: (key, value) => instance.set(key, encodeToStorageValue(value)),
|
|
89
|
-
/**
|
|
90
|
-
* Stores a value in Redis with a time-to-live (TTL).
|
|
91
|
-
*
|
|
92
|
-
* @param {string} key - The Redis key
|
|
93
|
-
* @param {number} seconds - Expiration time in seconds
|
|
94
|
-
* @param {any} value - The value to store. Will be serialized
|
|
95
|
-
*/
|
|
96
|
-
setItemWithTtl(key, seconds, value) {
|
|
97
|
-
return instance.setex(key, seconds, encodeToStorageValue(value));
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
function decodeStorageValue(data) {
|
|
102
|
-
if (!isCustomFormat(data))
|
|
103
|
-
return data;
|
|
104
|
-
const payload = data.subarray(customValueHeaderLength);
|
|
105
|
-
const type = data[customValueHeader.byteLength];
|
|
106
|
-
switch (type) {
|
|
107
|
-
case StorageValueEncodingType.Buffer: return payload;
|
|
108
|
-
case StorageValueEncodingType.Json:
|
|
109
|
-
try {
|
|
110
|
-
return deserialize(JSON.parse(payload.toString()));
|
|
111
|
-
}
|
|
112
|
-
catch {
|
|
113
|
-
throw new Error('[RedisStorage] Failed to parse JSON payload');
|
|
114
|
-
}
|
|
115
|
-
case StorageValueEncodingType.String: return payload.toString();
|
|
116
|
-
default:
|
|
117
|
-
throw new Error(`[RedisStorage] Unknown encoding type: ${type}`);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function encodeToStorageValue(value) {
|
|
121
|
-
if (Buffer.isBuffer(value))
|
|
122
|
-
return toCustomValue(StorageValueEncodingType.Buffer, value);
|
|
123
|
-
if (typeof value === 'string')
|
|
124
|
-
return toCustomValue(StorageValueEncodingType.String, Buffer.from(value));
|
|
125
|
-
return toCustomValue(StorageValueEncodingType.Json, Buffer.from(JSON.stringify(serialize(value))));
|
|
126
|
-
}
|
|
127
|
-
function isCustomFormat(buffer) {
|
|
128
|
-
return (buffer.length >= customValueHeaderLength
|
|
129
|
-
&& buffer[0] === customValueHeader[0]
|
|
130
|
-
&& buffer[1] === customValueHeader[1]
|
|
131
|
-
&& buffer[2] === customValueHeader[2]);
|
|
132
|
-
}
|
|
133
|
-
function toCustomValue(type, payload) {
|
|
134
|
-
return Buffer.concat([
|
|
135
|
-
customValueHeader,
|
|
136
|
-
Buffer.of(type),
|
|
137
|
-
payload,
|
|
138
|
-
]);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export { createEnhancedRedisStorage };
|
|
142
|
-
//# sourceMappingURL=core.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"core.mjs","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"],"names":[],"mappings":";;;;AAQA,IAAK,wBAIJ;AAJD,CAAA,UAAK,wBAAwB,EAAA;AACzB,IAAA,wBAAA,CAAA,wBAAA,CAAA,QAAA,CAAA,GAAA,CAAA,CAAA,GAAA,QAAU;AACV,IAAA,wBAAA,CAAA,wBAAA,CAAA,MAAA,CAAA,GAAA,CAAA,CAAA,GAAA,MAAQ;AACR,IAAA,wBAAA,CAAA,wBAAA,CAAA,QAAA,CAAA,GAAA,CAAA,CAAA,GAAA,QAAU;AACd,CAAC,EAJI,wBAAwB,KAAxB,wBAAwB,GAAA,EAAA,CAAA,CAAA;AAM7B,MAAM,iBAAiB,GAAG,MAAM,CAAC,EAAE,CAC/B,IAAI,EACJ,IAAI,EACJ,IAAI,CACP;AAED,MAAM,uBAAuB,GAAG,iBAAiB,CAAC,UAAU,GAAG,CAAC;AAEhE;;;;;;;;;;;;;;;;;;AAkBG;AACG,SAAU,0BAA0B,CAAC,oBAAoC,EAAA;AAC3E,IAAA,MAAM,QAAQ,GAAG,oBAAoB,YAAY,KAAK,GAAG,oBAAoB,GAAG,IAAI,KAAK,CAAC,oBAAoB,CAAC;IAC/G,OAAO,MAAM,CAAC,MAAM,CAAC;AACjB;;;;;;;;AAQG;QACH,MAAM,OAAO,CAAc,GAAW,EAAA;YAClC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,SAAS,CAAC,GAAG,CAAC;AAC9C,YAAA,OAAO,QAAQ,GAAG,kBAAkB,CAAC,QAAQ,CAAM,GAAG,IAAI;QAC9D,CAAC;AACD;;;;;;AAMG;QACH,UAAU,EAAE,CAAC,GAAW,KAAK,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC;AAC9C;;;;;;AAMG;AACH,QAAA,OAAO,EAAE,OAAO,GAAW,KAAK,MAAM,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC;AAChE;;;;;AAKG;AACH,QAAA,IAAI,QAAQ,GAAA;AACR,YAAA,OAAO,QAAQ;QACnB,CAAC;AACD;;;;;;;AAOG;AACH,QAAA,UAAU,EAAE,OAAO,GAAW,KAAK,MAAM,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC;AAChE;;;;;AAKG;AACH,QAAA,OAAO,EAAE,CAAC,GAAW,EAAE,KAAU,KAAK,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;AACpF;;;;;;AAMG;AACH,QAAA,cAAc,CAAC,GAAW,EAAE,OAAe,EAAE,KAAU,EAAA;AACnD,YAAA,OAAO,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;QACpE,CAAC;AACJ,KAAA,CAAC;AACN;AAEA,SAAS,kBAAkB,CAAC,IAAY,EAAA;AACpC,IAAA,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC;AAAE,QAAA,OAAO,IAAI;IACtC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,uBAAuB,CAAC;IACtD,MAAM,IAAI,GAAG,IAAI,CAAC,iBAAiB,CAAC,UAAU,CAAC;IAC/C,QAAQ,IAAI;AACR,QAAA,KAAK,wBAAwB,CAAC,MAAM,EAAE,OAAO,OAAO;QACpD,KAAK,wBAAwB,CAAC,IAAI;AAC9B,YAAA,IAAI;AACA,gBAAA,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,CAAC,CAAC;YACtD;AAAE,YAAA,MAAM;AACJ,gBAAA,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC;YAClE;QACJ,KAAK,wBAAwB,CAAC,MAAM,EAAE,OAAO,OAAO,CAAC,QAAQ,EAAE;AAC/D,QAAA;AACI,YAAA,MAAM,IAAI,KAAK,CAAC,yCAAyC,IAAI,CAAA,CAAE,CAAC;;AAE5E;AAEA,SAAS,oBAAoB,CAAC,KAAU,EAAA;AACpC,IAAA,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,aAAa,CAAC,wBAAwB,CAAC,MAAM,EAAE,KAAK,CAAC;IACxF,IAAI,OAAO,KAAK,KAAK,QAAQ;AAAE,QAAA,OAAO,aAAa,CAAC,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxG,OAAO,aAAa,CAAC,wBAAwB,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACtG;AAEA,SAAS,cAAc,CAAC,MAAc,EAAA;AAClC,IAAA,QACI,MAAM,CAAC,MAAM,IAAI;AACd,WAAA,MAAM,CAAC,CAAC,CAAC,KAAK,iBAAiB,CAAC,CAAC;AACjC,WAAA,MAAM,CAAC,CAAC,CAAC,KAAK,iBAAiB,CAAC,CAAC;WACjC,MAAM,CAAC,CAAC,CAAC,KAAK,iBAAiB,CAAC,CAAC,CAAC;AAE7C;AAEA,SAAS,aAAa,CAAC,IAA8B,EAAE,OAAe,EAAA;IAClE,OAAO,MAAM,CAAC,MAAM,CAAC;QACjB,iBAAiB;AACjB,QAAA,MAAM,CAAC,EAAE,CAAC,IAAI,CAAC;QACf,OAAO;AACV,KAAA,CAAC;AACN;;;;"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const core = require('./core.cjs');
|
|
4
|
-
const keyedStore = require('./keyed-store.cjs');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.createEnhancedRedisStorage = core.createEnhancedRedisStorage;
|
|
9
|
-
exports.createKeyedEnhancedRedisStore = keyedStore.createKeyedEnhancedRedisStore;
|
|
10
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/storage/enhanced/redis/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
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
|
-
* import { createKeyedEnhancedRedisStore } from '@kikiutils/shared/storage/enhanced/redis';
|
|
20
|
-
*
|
|
21
|
-
* const userStore = createKeyedEnhancedRedisStore<User>(redisStorage)((id: number) => `user:${id}`);
|
|
22
|
-
* await userStore.setItem({ id: 123, name: 'user' }, 123);
|
|
23
|
-
* const user = await userStore.getItem(123);
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
function createKeyedEnhancedRedisStore(storage) {
|
|
27
|
-
return (getKeyFunction) => Object.freeze({
|
|
28
|
-
getItem: (...args) => storage.getItem(getKeyFunction(...args)),
|
|
29
|
-
getItemTtl: (...args) => storage.getItemTtl(getKeyFunction(...args)),
|
|
30
|
-
hasItem: (...args) => storage.hasItem(getKeyFunction(...args)),
|
|
31
|
-
removeItem: (...args) => storage.removeItem(getKeyFunction(...args)),
|
|
32
|
-
/**
|
|
33
|
-
* Resolves the storage key from the given arguments.
|
|
34
|
-
*
|
|
35
|
-
* @returns {string} The final string key used internally
|
|
36
|
-
*/
|
|
37
|
-
resolveKey: (...args) => getKeyFunction(...args),
|
|
38
|
-
setItem: (value, ...args) => storage.setItem(getKeyFunction(...args), value),
|
|
39
|
-
setItemWithTtl(seconds, value, ...args) {
|
|
40
|
-
return storage.setItemWithTtl(getKeyFunction(...args), seconds, value);
|
|
41
|
-
},
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
exports.createKeyedEnhancedRedisStore = createKeyedEnhancedRedisStore;
|
|
46
|
-
//# sourceMappingURL=keyed-store.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.cjs","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"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,6BAA6B,CAAc,OAAsD,EAAA;IAC7G,OAAO,CAAkB,cAAsC,KAAK,MAAM,CAAC,MAAM,CAAC;AAC9E,QAAA,OAAO,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,OAAO,CAAI,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACpE,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACvE,QAAA,OAAO,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACjE,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACvE;;;;AAIG;QACH,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,cAAc,CAAC,GAAG,IAAI,CAAC;QACnD,OAAO,EAAE,CAAC,KAAQ,EAAE,GAAG,IAAO,KAAK,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;AAClF,QAAA,cAAc,CAAC,OAAe,EAAE,KAAQ,EAAE,GAAG,IAAO,EAAA;AAChD,YAAA,OAAO,OAAO,CAAC,cAAc,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC;QAC1E,CAAC;AACJ,KAAA,CAAC;AACN;;;;"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Creates a reusable, type-safe Redis-backed storage interface based on `createEnhancedRedisStorage`
|
|
3
|
-
* and a dynamic key-generation function.
|
|
4
|
-
*
|
|
5
|
-
* This utility abstracts away key construction and provides high-level access
|
|
6
|
-
* to Redis operations such as `getItem`, `setItem`, `setItemWithTtl`, and `getItemTtl`.
|
|
7
|
-
* It is ideal for namespaced data, caching, and session handling.
|
|
8
|
-
*
|
|
9
|
-
* @template D - The value type to store
|
|
10
|
-
*
|
|
11
|
-
* @param {ReturnType<typeof createEnhancedRedisStorage>} storage - The enhanced Redis storage instance
|
|
12
|
-
*
|
|
13
|
-
* @returns A factory that accepts a key generator function and returns a scoped Redis storage interface
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* import { createKeyedEnhancedRedisStore } from '@kikiutils/shared/storage/enhanced/redis';
|
|
18
|
-
*
|
|
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
|
-
function createKeyedEnhancedRedisStore(storage) {
|
|
25
|
-
return (getKeyFunction) => Object.freeze({
|
|
26
|
-
getItem: (...args) => storage.getItem(getKeyFunction(...args)),
|
|
27
|
-
getItemTtl: (...args) => storage.getItemTtl(getKeyFunction(...args)),
|
|
28
|
-
hasItem: (...args) => storage.hasItem(getKeyFunction(...args)),
|
|
29
|
-
removeItem: (...args) => 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) => getKeyFunction(...args),
|
|
36
|
-
setItem: (value, ...args) => storage.setItem(getKeyFunction(...args), value),
|
|
37
|
-
setItemWithTtl(seconds, value, ...args) {
|
|
38
|
-
return storage.setItemWithTtl(getKeyFunction(...args), seconds, value);
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export { createKeyedEnhancedRedisStore };
|
|
44
|
-
//# sourceMappingURL=keyed-store.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.mjs","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"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;AAsBG;AACG,SAAU,6BAA6B,CAAc,OAAsD,EAAA;IAC7G,OAAO,CAAkB,cAAsC,KAAK,MAAM,CAAC,MAAM,CAAC;AAC9E,QAAA,OAAO,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,OAAO,CAAI,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACpE,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACvE,QAAA,OAAO,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACjE,QAAA,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,OAAO,CAAC,UAAU,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;AACvE;;;;AAIG;QACH,UAAU,EAAE,CAAC,GAAG,IAAO,KAAK,cAAc,CAAC,GAAG,IAAI,CAAC;QACnD,OAAO,EAAE,CAAC,KAAQ,EAAE,GAAG,IAAO,KAAK,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;AAClF,QAAA,cAAc,CAAC,OAAe,EAAE,KAAQ,EAAE,GAAG,IAAO,EAAA;AAChD,YAAA,OAAO,OAAO,CAAC,cAAc,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC;QAC1E,CAAC;AACJ,KAAA,CAAC;AACN;;;;"}
|
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
*
|
|
10
|
-
* @param {LRUCache<any, any, any>} lruInstance - An instance of `lru-cache`
|
|
11
|
-
*
|
|
12
|
-
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```typescript
|
|
16
|
-
* import { createKeyedLruStore } from '@kikiutils/shared/storage/lru/keyed-store';
|
|
17
|
-
*
|
|
18
|
-
* const lruCache = new LRUCache({ max: 5000 });
|
|
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
|
-
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) {
|
|
43
|
-
lruInstance.set(getKeyFunction(...args), value);
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
exports.createKeyedLruStore = createKeyedLruStore;
|
|
49
|
-
//# sourceMappingURL=keyed-store.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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 *\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"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAc,WAAoC,EAAA;IACjF,OAAO,CAAkB,cAAsC,KAAK,MAAM,CAAC,MAAM,CAAC;AAC9E;;;;AAIG;QACH,OAAO,CAAC,GAAG,IAAO,EAAA;AACd,YAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;YACzD,OAAO,QAAa,IAAI,IAAI;QAChC,CAAC;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;AACnD,QAAA,OAAO,CAAC,KAAQ,EAAE,GAAG,IAAO,EAAA;YACxB,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;QACnD,CAAC;AACJ,KAAA,CAAC;AACN;;;;"}
|
|
@@ -1,47 +0,0 @@
|
|
|
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
|
-
*
|
|
8
|
-
* @param {LRUCache<any, any, any>} lruInstance - An instance of `lru-cache`
|
|
9
|
-
*
|
|
10
|
-
* @returns A factory that accepts a key generator and returns a scoped, type-safe LRU-based store
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* import { createKeyedLruStore } from '@kikiutils/shared/storage/lru/keyed-store';
|
|
15
|
-
*
|
|
16
|
-
* const lruCache = new LRUCache({ max: 5000 });
|
|
17
|
-
* const userStore = createKeyedLruStore<User>(lruCache)((id: number) => `user:${id}`);
|
|
18
|
-
* userStore.setItem({ id: 1 }, 1);
|
|
19
|
-
* const user = userStore.getItem(1);
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
function createKeyedLruStore(lruInstance) {
|
|
23
|
-
return (getKeyFunction) => Object.freeze({
|
|
24
|
-
/**
|
|
25
|
-
* Return a value from the cache. Will update the recency of the cache entry found.
|
|
26
|
-
*
|
|
27
|
-
* If the key is not found, returns `null`.
|
|
28
|
-
*/
|
|
29
|
-
getItem(...args) {
|
|
30
|
-
const rawValue = lruInstance.get(getKeyFunction(...args));
|
|
31
|
-
return rawValue ?? null;
|
|
32
|
-
},
|
|
33
|
-
getItemTtl: (...args) => lruInstance.getRemainingTTL(getKeyFunction(...args)),
|
|
34
|
-
hasItem: (...args) => lruInstance.has(getKeyFunction(...args)),
|
|
35
|
-
removeItem: (...args) => lruInstance.delete(getKeyFunction(...args)),
|
|
36
|
-
/**
|
|
37
|
-
* Resolves the full cache key from the given arguments.
|
|
38
|
-
*/
|
|
39
|
-
resolveKey: (...args) => getKeyFunction(...args),
|
|
40
|
-
setItem(value, ...args) {
|
|
41
|
-
lruInstance.set(getKeyFunction(...args), value);
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export { createKeyedLruStore };
|
|
47
|
-
//# sourceMappingURL=keyed-store.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
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 *\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"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAc,WAAoC,EAAA;IACjF,OAAO,CAAkB,cAAsC,KAAK,MAAM,CAAC,MAAM,CAAC;AAC9E;;;;AAIG;QACH,OAAO,CAAC,GAAG,IAAO,EAAA;AACd,YAAA,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,CAAC;YACzD,OAAO,QAAa,IAAI,IAAI;QAChC,CAAC;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;AACnD,QAAA,OAAO,CAAC,KAAQ,EAAE,GAAG,IAAO,EAAA;YACxB,WAAW,CAAC,GAAG,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC,EAAE,KAAK,CAAC;QACnD,CAAC;AACJ,KAAA,CAAC;AACN;;;;"}
|
package/dist/string.cjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const DIGITS = '0123456789';
|
|
4
|
-
const LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';
|
|
5
|
-
const UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
|
6
|
-
const CHARSETS = {
|
|
7
|
-
'alphabetic': LOWERCASE + UPPERCASE,
|
|
8
|
-
'alphanumeric': DIGITS + LOWERCASE + UPPERCASE,
|
|
9
|
-
'lowercase': LOWERCASE,
|
|
10
|
-
'lowercase-numeric': DIGITS + LOWERCASE,
|
|
11
|
-
'numeric': DIGITS,
|
|
12
|
-
'uppercase': UPPERCASE,
|
|
13
|
-
'uppercase-numeric': DIGITS + UPPERCASE,
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Generates a random string of a given length using a specified character set.
|
|
17
|
-
*
|
|
18
|
-
* @param {number} length - The length of the string to generate. Must be a positive integer
|
|
19
|
-
* @param {RandomStringMode} [mode] - The character set to use
|
|
20
|
-
*
|
|
21
|
-
* @returns {string} The generated random string
|
|
22
|
-
*
|
|
23
|
-
* @throws {Error} If the length is not a positive integer or the mode is unsupported
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```typescript
|
|
27
|
-
* import { randomString } from '@kikiutils/shared/string';
|
|
28
|
-
*
|
|
29
|
-
* console.log(randomString(8)); // e.g. 'aZbXwTyQ' (alphabetic)
|
|
30
|
-
* console.log(randomString(6, 'numeric')); // e.g. '402398'
|
|
31
|
-
* console.log(randomString(10, 'alphanumeric')); // e.g. 'a9Z4pQ8xY2'
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
function randomString(length, mode = 'alphabetic') {
|
|
35
|
-
if (!Number.isInteger(length) || length <= 0) {
|
|
36
|
-
throw new Error(`Invalid length: ${length}. Must be a positive integer`);
|
|
37
|
-
}
|
|
38
|
-
const charset = CHARSETS[mode];
|
|
39
|
-
if (!charset)
|
|
40
|
-
throw new Error(`Unsupported mode: ${mode}`);
|
|
41
|
-
return Array.from({ length }, () => charset[Math.floor(Math.random() * charset.length)]).join('');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
exports.randomString = randomString;
|
|
45
|
-
//# sourceMappingURL=string.cjs.map
|
package/dist/string.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"string.cjs","sources":["../src/string.ts"],"sourcesContent":["export type RandomStringMode =\n | 'alphabetic'\n | 'alphanumeric'\n | 'lowercase'\n | 'lowercase-numeric'\n | 'numeric'\n | 'uppercase'\n | 'uppercase-numeric';\n\nconst DIGITS = '0123456789';\nconst LOWERCASE = 'abcdefghijklmnopqrstuvwxyz';\nconst UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\nconst CHARSETS: Record<RandomStringMode, string> = {\n 'alphabetic': LOWERCASE + UPPERCASE,\n 'alphanumeric': DIGITS + LOWERCASE + UPPERCASE,\n 'lowercase': LOWERCASE,\n 'lowercase-numeric': DIGITS + LOWERCASE,\n 'numeric': DIGITS,\n 'uppercase': UPPERCASE,\n 'uppercase-numeric': DIGITS + UPPERCASE,\n};\n\n/**\n * Generates a random string of a given length using a specified character set.\n *\n * @param {number} length - The length of the string to generate. Must be a positive integer\n * @param {RandomStringMode} [mode] - The character set to use\n *\n * @returns {string} The generated random string\n *\n * @throws {Error} If the length is not a positive integer or the mode is unsupported\n *\n * @example\n * ```typescript\n * import { randomString } from '@kikiutils/shared/string';\n *\n * console.log(randomString(8)); // e.g. 'aZbXwTyQ' (alphabetic)\n * console.log(randomString(6, 'numeric')); // e.g. '402398'\n * console.log(randomString(10, 'alphanumeric')); // e.g. 'a9Z4pQ8xY2'\n * ```\n */\nexport function randomString(length: number, mode: RandomStringMode = 'alphabetic') {\n if (!Number.isInteger(length) || length <= 0) {\n throw new Error(`Invalid length: ${length}. Must be a positive integer`);\n }\n\n const charset = CHARSETS[mode];\n if (!charset) throw new Error(`Unsupported mode: ${mode}`);\n return Array.from({ length }, () => charset[Math.floor(Math.random() * charset.length)]).join('');\n}\n"],"names":[],"mappings":";;AASA,MAAM,MAAM,GAAG,YAAY;AAC3B,MAAM,SAAS,GAAG,4BAA4B;AAC9C,MAAM,SAAS,GAAG,4BAA4B;AAC9C,MAAM,QAAQ,GAAqC;IAC/C,YAAY,EAAE,SAAS,GAAG,SAAS;AACnC,IAAA,cAAc,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;AAC9C,IAAA,WAAW,EAAE,SAAS;IACtB,mBAAmB,EAAE,MAAM,GAAG,SAAS;AACvC,IAAA,SAAS,EAAE,MAAM;AACjB,IAAA,WAAW,EAAE,SAAS;IACtB,mBAAmB,EAAE,MAAM,GAAG,SAAS;CAC1C;AAED;;;;;;;;;;;;;;;;;;AAkBG;SACa,YAAY,CAAC,MAAc,EAAE,OAAyB,YAAY,EAAA;AAC9E,IAAA,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,IAAI,CAAC,EAAE;AAC1C,QAAA,MAAM,IAAI,KAAK,CAAC,mBAAmB,MAAM,CAAA,4BAAA,CAA8B,CAAC;IAC5E;AAEA,IAAA,MAAM,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC;AAC9B,IAAA,IAAI,CAAC,OAAO;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,qBAAqB,IAAI,CAAA,CAAE,CAAC;AAC1D,IAAA,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;AACrG;;;;"}
|