@kikiutils/shared 12.0.0 → 13.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/README.md +1 -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 +43 -27
- package/src/env.ts +1 -1
- 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 -36
- package/dist/hash.cjs.map +0 -1
- package/dist/hash.mjs +0 -31
- 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
package/dist/pino.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import * as pino0 from "pino";
|
|
2
|
+
|
|
3
|
+
//#region src/pino.d.ts
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* A pino logger instance with the configured stream.
|
|
3
7
|
*
|
|
@@ -19,6 +23,8 @@
|
|
|
19
23
|
* logger.level = 'info';
|
|
20
24
|
* ```
|
|
21
25
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
declare const pinoLogger: pino0.Logger<never, boolean>;
|
|
27
|
+
declare const logger: pino0.Logger<never, boolean>;
|
|
28
|
+
//#endregion
|
|
29
|
+
export { logger, pinoLogger };
|
|
24
30
|
//# sourceMappingURL=pino.d.ts.map
|
package/dist/pino.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pino.d.ts","
|
|
1
|
+
{"version":3,"file":"pino.d.ts","names":[],"sources":["../src/pino.ts"],"sourcesContent":[],"mappings":";;;;;;;AAiCA;AACA;;;;;;;;;;;;;;;;;cADa,YAA6B,KAAA,CAAnB;cACV,QAAmB,KAAA,CAAb"}
|
package/dist/pino.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { pino } from "pino";
|
|
2
|
+
import { PinoPretty } from "pino-pretty";
|
|
3
|
+
|
|
4
|
+
//#region src/pino.ts
|
|
5
|
+
/**
|
|
6
|
+
* Configure pinoPretty to enhance the log output.
|
|
7
|
+
*/
|
|
8
|
+
const stream = PinoPretty({
|
|
9
|
+
colorize: true,
|
|
10
|
+
ignore: "hostname,pid",
|
|
11
|
+
translateTime: "SYS:yyyy-mm-dd HH:MM:ss.l"
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* A pino logger instance with the configured stream.
|
|
15
|
+
*
|
|
16
|
+
* The logger's level is determined based on the `PINO_LOGGER_LEVEL` and `NODE_ENV` environment variables.
|
|
17
|
+
* If `PINO_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,
|
|
18
|
+
* the level will be set to `error`.
|
|
19
|
+
*
|
|
20
|
+
* To manually change the level, assign the desired level to `logger.level`.
|
|
21
|
+
*
|
|
22
|
+
* See available levels [here](https://getpino.io/#/docs/api?id=level-string).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { logger } from '@kikiutils/shared/pino';
|
|
27
|
+
*
|
|
28
|
+
* logger.info('test'); // [2024-07-11 12:12:30.085] INFO: test
|
|
29
|
+
*
|
|
30
|
+
* // Manually change the level
|
|
31
|
+
* logger.level = 'info';
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
const pinoLogger = pino({}, stream);
|
|
35
|
+
const logger = pinoLogger;
|
|
36
|
+
pinoLogger.level = process.env.PINO_LOGGER_LEVEL || (process.env.NODE_ENV === "production" ? "error" : pinoLogger.level);
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { logger, pinoLogger };
|
|
40
|
+
//# sourceMappingURL=pino.js.map
|
package/dist/pino.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pino.js","names":[],"sources":["../src/pino.ts"],"sourcesContent":["import { pino } from 'pino';\nimport { PinoPretty } from 'pino-pretty';\n\n/**\n * Configure pinoPretty to enhance the log output.\n */\nconst stream = PinoPretty({\n colorize: true, // Enable colored output for better readability\n ignore: 'hostname,pid', // Exclude 'hostname' and 'pid' fields from the logs\n translateTime: 'SYS:yyyy-mm-dd HH:MM:ss.l', // Format the timestamp in 'yyyy-mm-dd HH:MM:ss.l' format\n});\n\n/**\n * A pino logger instance with the configured stream.\n *\n * The logger's level is determined based on the `PINO_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `PINO_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `error`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://getpino.io/#/docs/api?id=level-string).\n *\n * @example\n * ```typescript\n * import { logger } from '@kikiutils/shared/pino';\n *\n * logger.info('test'); // [2024-07-11 12:12:30.085] INFO: test\n *\n * // Manually change the level\n * logger.level = 'info';\n * ```\n */\nexport const pinoLogger = pino({}, stream);\nexport const logger = pinoLogger;\n// eslint-disable-next-line style/max-len\npinoLogger.level = process.env.PINO_LOGGER_LEVEL || (process.env.NODE_ENV === 'production' ? 'error' : pinoLogger.level);\n"],"mappings":";;;;;;;AAMA,MAAM,SAAS,WAAW;CACtB,UAAU;CACV,QAAQ;CACR,eAAe;CAClB,CAAC;;;;;;;;;;;;;;;;;;;;;;AAuBF,MAAa,aAAa,KAAK,EAAE,EAAE,OAAO;AAC1C,MAAa,SAAS;AAEtB,WAAW,QAAQ,QAAQ,IAAI,sBAAsB,QAAQ,IAAI,aAAa,eAAe,UAAU,WAAW"}
|
package/dist/random.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/random.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Generates a value using a provided generator function, where the input length
|
|
3
4
|
* is determined by two levels of nested random ranges:
|
|
@@ -18,5 +19,7 @@
|
|
|
18
19
|
*
|
|
19
20
|
* @returns {T} The result of the generator function using the computed final length
|
|
20
21
|
*/
|
|
21
|
-
|
|
22
|
+
declare function generateWithNestedRandomLength<T = string>(generator: (length: number) => T, minMin: number, minMax: number, maxMin: number, maxMax: number): T;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { generateWithNestedRandomLength };
|
|
22
25
|
//# sourceMappingURL=random.d.ts.map
|
package/dist/random.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.d.ts","
|
|
1
|
+
{"version":3,"file":"random.d.ts","names":[],"sources":["../src/random.ts"],"sourcesContent":[],"mappings":";;AAoBA;;;;;;;;;;;;;;;;;;;iBAAgB,0EACmB,oEAIjB"}
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
//#region src/random.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generates a value using a provided generator function, where the input length
|
|
4
|
+
* is determined by two levels of nested random ranges:
|
|
5
|
+
*
|
|
6
|
+
* 1. First, a random number (`innerMin`) is chosen between `minMin` and `minMax`.
|
|
7
|
+
* 2. Then, a final length is chosen between `Math.max(innerMin, maxMin)` and `maxMax`.
|
|
8
|
+
* 3. The generator is called with the final length and its result is returned.
|
|
9
|
+
*
|
|
10
|
+
* This function supports any return type by using a generic type parameter.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The return type of the generator function
|
|
13
|
+
*
|
|
14
|
+
* @param {(length: number) => T} generator - A function that accepts a length and returns a value of type T
|
|
15
|
+
* @param {number} minMin - Lower bound of the first random range
|
|
16
|
+
* @param {number} minMax - Upper bound of the first random range
|
|
17
|
+
* @param {number} maxMin - Lower bound of the second random range
|
|
18
|
+
* @param {number} maxMax - Upper bound of the second random range
|
|
19
|
+
*
|
|
20
|
+
* @returns {T} The result of the generator function using the computed final length
|
|
21
|
+
*/
|
|
22
|
+
function generateWithNestedRandomLength(generator, minMin, minMax, maxMin, maxMax) {
|
|
23
|
+
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
|
|
24
|
+
const innerMin = random(minMin, minMax);
|
|
25
|
+
const finalLength = random(Math.max(innerMin, maxMin), maxMax);
|
|
26
|
+
return generator(finalLength);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { generateWithNestedRandomLength };
|
|
31
|
+
//# sourceMappingURL=random.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.js","names":[],"sources":["../src/random.ts"],"sourcesContent":["/**\n * Generates a value using a provided generator function, where the input length\n * is determined by two levels of nested random ranges:\n *\n * 1. First, a random number (`innerMin`) is chosen between `minMin` and `minMax`.\n * 2. Then, a final length is chosen between `Math.max(innerMin, maxMin)` and `maxMax`.\n * 3. The generator is called with the final length and its result is returned.\n *\n * This function supports any return type by using a generic type parameter.\n *\n * @template T - The return type of the generator function\n *\n * @param {(length: number) => T} generator - A function that accepts a length and returns a value of type T\n * @param {number} minMin - Lower bound of the first random range\n * @param {number} minMax - Upper bound of the first random range\n * @param {number} maxMin - Lower bound of the second random range\n * @param {number} maxMax - Upper bound of the second random range\n *\n * @returns {T} The result of the generator function using the computed final length\n */\nexport function generateWithNestedRandomLength<T = string>(\n generator: (length: number) => T,\n minMin: number,\n minMax: number,\n maxMin: number,\n maxMax: number,\n) {\n const random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min;\n const innerMin = random(minMin, minMax);\n const finalLength = random(Math.max(innerMin, maxMin), maxMax);\n return generator(finalLength);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,+BACZ,WACA,QACA,QACA,QACA,QACF;CACE,MAAM,UAAU,KAAa,QAAgB,KAAK,MAAM,KAAK,QAAQ,IAAI,MAAM,MAAM,GAAG,GAAG;CAC3F,MAAM,WAAW,OAAO,QAAQ,OAAO;CACvC,MAAM,cAAc,OAAO,KAAK,IAAI,UAAU,OAAO,EAAE,OAAO;AAC9D,QAAO,UAAU,YAAY"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/storage/enhanced/local/core.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* An enhanced localStorage wrapper that supports storing
|
|
3
4
|
* complex data types (e.g. Dates, Maps, Sets) using SuperJSON encoding.
|
|
@@ -12,47 +13,49 @@
|
|
|
12
13
|
* const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');
|
|
13
14
|
* ```
|
|
14
15
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
16
|
+
declare const enhancedLocalStorage: Readonly<{
|
|
17
|
+
/**
|
|
18
|
+
* Removes all items from localStorage.
|
|
19
|
+
*/
|
|
20
|
+
clear: () => void;
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves a value by key and decodes it using SuperJSON or raw string.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The expected type of the value
|
|
25
|
+
*
|
|
26
|
+
* @param {string} key - The key of the value to retrieve
|
|
27
|
+
*
|
|
28
|
+
* @returns {null | T} The decoded value or null if not found
|
|
29
|
+
*/
|
|
30
|
+
getItem<T = unknown>(key: string): T | null;
|
|
31
|
+
/**
|
|
32
|
+
* Checks whether a key exists in localStorage.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} key - The key to check
|
|
35
|
+
*
|
|
36
|
+
* @returns {boolean} True if the key exists, false otherwise
|
|
37
|
+
*/
|
|
38
|
+
hasItem: (key: string) => boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Returns the number of items stored in localStorage.
|
|
41
|
+
*
|
|
42
|
+
* @returns {number} The number of items stored in localStorage
|
|
43
|
+
*/
|
|
44
|
+
readonly length: number;
|
|
45
|
+
/**
|
|
46
|
+
* Removes a specific key from localStorage.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} key - The key to remove
|
|
49
|
+
*/
|
|
50
|
+
removeItem: (key: string) => void;
|
|
51
|
+
/**
|
|
52
|
+
* Stores a value in localStorage with automatic serialization.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} key - The key to store the value under
|
|
55
|
+
* @param {any} value - The value to store
|
|
56
|
+
*/
|
|
57
|
+
setItem: (key: string, value: any) => void;
|
|
57
58
|
}>;
|
|
59
|
+
//#endregion
|
|
60
|
+
export { enhancedLocalStorage };
|
|
58
61
|
//# sourceMappingURL=core.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","
|
|
1
|
+
{"version":3,"file":"core.d.ts","names":[],"sources":["../../../../src/storage/enhanced/local/core.ts"],"sourcesContent":[],"mappings":";;AA4BA;;;;;;;;;;;;;cAAa,sBAAoB;;;;;;;;;;;;;;qCAcG"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { deserialize, serialize } from "superjson";
|
|
2
|
+
|
|
3
|
+
//#region src/storage/enhanced/local/core.ts
|
|
4
|
+
var StorageValueEncodingType = /* @__PURE__ */ function(StorageValueEncodingType$1) {
|
|
5
|
+
StorageValueEncodingType$1["Json"] = "0";
|
|
6
|
+
StorageValueEncodingType$1["String"] = "1";
|
|
7
|
+
return StorageValueEncodingType$1;
|
|
8
|
+
}(StorageValueEncodingType || {});
|
|
9
|
+
const customValueHeader = "";
|
|
10
|
+
const customValueHeaderLength = 3;
|
|
11
|
+
const toCustomValue = (type, payload) => `${customValueHeader}${type}${payload}`;
|
|
12
|
+
/**
|
|
13
|
+
* An enhanced localStorage wrapper that supports storing
|
|
14
|
+
* complex data types (e.g. Dates, Maps, Sets) using SuperJSON encoding.
|
|
15
|
+
*
|
|
16
|
+
* This utility preserves type structure when saving and retrieving values.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { enhancedLocalStorage } from '@kikiutils/shared/storage/enhanced/local';
|
|
21
|
+
*
|
|
22
|
+
* enhancedLocalStorage.setItem('user', { name: 'user', createdAt: new Date() });
|
|
23
|
+
* const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const enhancedLocalStorage = Object.freeze({
|
|
27
|
+
clear: () => window.localStorage.clear(),
|
|
28
|
+
getItem(key) {
|
|
29
|
+
const rawValue = window.localStorage.getItem(key);
|
|
30
|
+
return rawValue ? decodeStorageValue(rawValue) : null;
|
|
31
|
+
},
|
|
32
|
+
hasItem: (key) => window.localStorage.getItem(key) !== null,
|
|
33
|
+
get length() {
|
|
34
|
+
return window.localStorage.length;
|
|
35
|
+
},
|
|
36
|
+
removeItem: (key) => window.localStorage.removeItem(key),
|
|
37
|
+
setItem: (key, value) => window.localStorage.setItem(key, encodeToStorageValue(value))
|
|
38
|
+
});
|
|
39
|
+
function decodeStorageValue(data) {
|
|
40
|
+
if (!isCustomFormat(data)) return data;
|
|
41
|
+
const payload = data.substring(customValueHeaderLength);
|
|
42
|
+
const type = data.charAt(2);
|
|
43
|
+
switch (type) {
|
|
44
|
+
case StorageValueEncodingType.Json: try {
|
|
45
|
+
return deserialize(JSON.parse(payload));
|
|
46
|
+
} catch {
|
|
47
|
+
throw new Error("[EnhancedLocalStorage] Failed to parse JSON payload");
|
|
48
|
+
}
|
|
49
|
+
case StorageValueEncodingType.String: return payload;
|
|
50
|
+
default: throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function encodeToStorageValue(value) {
|
|
54
|
+
if (typeof value === "string") return toCustomValue(StorageValueEncodingType.String, value);
|
|
55
|
+
return toCustomValue(StorageValueEncodingType.Json, JSON.stringify(serialize(value)));
|
|
56
|
+
}
|
|
57
|
+
function isCustomFormat(data) {
|
|
58
|
+
return data.length >= customValueHeaderLength && data[0] === customValueHeader[0] && data[1] === customValueHeader[1];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { enhancedLocalStorage };
|
|
63
|
+
//# sourceMappingURL=core.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","names":[],"sources":["../../../../src/storage/enhanced/local/core.ts"],"sourcesContent":["import {\n deserialize,\n serialize,\n} from 'superjson';\n\nenum StorageValueEncodingType {\n Json = '0',\n String = '1',\n}\n\nconst customValueHeader = '';\nconst customValueHeaderLength = customValueHeader.length + 1;\nconst toCustomValue = (type: StorageValueEncodingType, payload: string) => `${customValueHeader}${type}${payload}`;\n\n/**\n * An enhanced localStorage wrapper that supports storing\n * complex data types (e.g. Dates, Maps, Sets) using SuperJSON encoding.\n *\n * This utility preserves type structure when saving and retrieving values.\n *\n * @example\n * ```typescript\n * import { enhancedLocalStorage } from '@kikiutils/shared/storage/enhanced/local';\n *\n * enhancedLocalStorage.setItem('user', { name: 'user', createdAt: new Date() });\n * const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');\n * ```\n */\nexport const enhancedLocalStorage = Object.freeze({\n /**\n * Removes all items from localStorage.\n */\n clear: () => window.localStorage.clear(),\n /**\n * Retrieves a value by key and decodes it using SuperJSON or raw string.\n *\n * @template T - The expected type of the value\n *\n * @param {string} key - The key of the value to retrieve\n *\n * @returns {null | T} The decoded value or null if not found\n */\n getItem<T = unknown>(key: string) {\n const rawValue = window.localStorage.getItem(key);\n return rawValue ? decodeStorageValue(rawValue) as T : null;\n },\n /**\n * Checks whether a key exists in localStorage.\n *\n * @param {string} key - The key to check\n *\n * @returns {boolean} True if the key exists, false otherwise\n */\n hasItem: (key: string) => window.localStorage.getItem(key) !== null,\n /**\n * Returns the number of items stored in localStorage.\n *\n * @returns {number} The number of items stored in localStorage\n */\n get length() {\n return window.localStorage.length;\n },\n /**\n * Removes a specific key from localStorage.\n *\n * @param {string} key - The key to remove\n */\n removeItem: (key: string) => window.localStorage.removeItem(key),\n /**\n * Stores a value in localStorage with automatic serialization.\n *\n * @param {string} key - The key to store the value under\n * @param {any} value - The value to store\n */\n setItem: (key: string, value: any) => window.localStorage.setItem(key, encodeToStorageValue(value)),\n});\n\nfunction decodeStorageValue(data: string) {\n if (!isCustomFormat(data)) return data;\n const payload = data.substring(customValueHeaderLength);\n const type = data.charAt(customValueHeader.length);\n switch (type) {\n case StorageValueEncodingType.Json:\n try {\n return deserialize(JSON.parse(payload));\n } catch {\n throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload');\n }\n case StorageValueEncodingType.String: return payload;\n default:\n throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}`);\n }\n}\n\nfunction encodeToStorageValue(value: any) {\n if (typeof value === 'string') return toCustomValue(StorageValueEncodingType.String, value);\n return toCustomValue(StorageValueEncodingType.Json, JSON.stringify(serialize(value)));\n}\n\nfunction isCustomFormat(data: string) {\n return (\n data.length >= customValueHeaderLength\n && data[0] === customValueHeader[0]\n && data[1] === customValueHeader[1]\n );\n}\n"],"mappings":";;;AAKA,IAAK,gFAAL;AACI;AACA;;EAFC;AAKL,MAAM,oBAAoB;AAC1B,MAAM,0BAA0B;AAChC,MAAM,iBAAiB,MAAgC,YAAoB,GAAG,oBAAoB,OAAO;;;;;;;;;;;;;;;AAgBzG,MAAa,uBAAuB,OAAO,OAAO;CAI9C,aAAa,OAAO,aAAa,OAAO;CAUxC,QAAqB,KAAa;EAC9B,MAAM,WAAW,OAAO,aAAa,QAAQ,IAAI;AACjD,SAAO,WAAW,mBAAmB,SAAS,GAAQ;;CAS1D,UAAU,QAAgB,OAAO,aAAa,QAAQ,IAAI,KAAK;CAM/D,IAAI,SAAS;AACT,SAAO,OAAO,aAAa;;CAO/B,aAAa,QAAgB,OAAO,aAAa,WAAW,IAAI;CAOhE,UAAU,KAAa,UAAe,OAAO,aAAa,QAAQ,KAAK,qBAAqB,MAAM,CAAC;CACtG,CAAC;AAEF,SAAS,mBAAmB,MAAc;AACtC,KAAI,CAAC,eAAe,KAAK,CAAE,QAAO;CAClC,MAAM,UAAU,KAAK,UAAU,wBAAwB;CACvD,MAAM,OAAO,KAAK,OAAO,EAAyB;AAClD,SAAQ,MAAR;EACI,KAAK,yBAAyB,KAC1B,KAAI;AACA,UAAO,YAAY,KAAK,MAAM,QAAQ,CAAC;UACnC;AACJ,SAAM,IAAI,MAAM,sDAAsD;;EAE9E,KAAK,yBAAyB,OAAQ,QAAO;EAC7C,QACI,OAAM,IAAI,MAAM,iDAAiD,OAAO;;;AAIpF,SAAS,qBAAqB,OAAY;AACtC,KAAI,OAAO,UAAU,SAAU,QAAO,cAAc,yBAAyB,QAAQ,MAAM;AAC3F,QAAO,cAAc,yBAAyB,MAAM,KAAK,UAAU,UAAU,MAAM,CAAC,CAAC;;AAGzF,SAAS,eAAe,MAAc;AAClC,QACI,KAAK,UAAU,2BACZ,KAAK,OAAO,kBAAkB,MAC9B,KAAK,OAAO,kBAAkB"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { enhancedLocalStorage } from "./core.js";
|
|
2
|
+
import { createKeyedEnhancedLocalStore } from "./keyed-store.js";
|
|
3
|
+
export { createKeyedEnhancedLocalStore, enhancedLocalStorage };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
//#region src/storage/enhanced/local/keyed-store.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Creates a reusable, type-safe storage interface based on `enhancedLocalStorage`
|
|
3
4
|
* and a dynamic key-generation function.
|
|
@@ -18,16 +19,18 @@
|
|
|
18
19
|
* const user = userStore.getItem(123);
|
|
19
20
|
* ```
|
|
20
21
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
22
|
+
declare function createKeyedEnhancedLocalStore<D = unknown>(): <P extends any[]>(getKeyFunction: (...args: P) => string) => Readonly<{
|
|
23
|
+
getItem: (...args: P) => D | null;
|
|
24
|
+
hasItem: (...args: P) => boolean;
|
|
25
|
+
removeItem: (...args: P) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Resolves the storage key from the given arguments.
|
|
28
|
+
*
|
|
29
|
+
* @returns {string} The final string key used internally
|
|
30
|
+
*/
|
|
31
|
+
resolveKey: (...args: P) => string;
|
|
32
|
+
setItem: (value: D, ...args: P) => void;
|
|
32
33
|
}>;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { createKeyedEnhancedLocalStore };
|
|
33
36
|
//# sourceMappingURL=keyed-store.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyed-store.d.ts","
|
|
1
|
+
{"version":3,"file":"keyed-store.d.ts","names":[],"sources":["../../../../src/storage/enhanced/local/keyed-store.ts"],"sourcesContent":[],"mappings":";;AAsBA;;;;;;;;;;;;;;;;;;;iBAAgB,0FACuC,iBAAY;qBACxC,MAAC;qBACD;wBACG;;;;;;wBAMA;mBACL,YAAY"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,4 +1,7 @@
|
|
|
1
|
-
import { Redis } from
|
|
1
|
+
import { Redis } from "ioredis";
|
|
2
|
+
|
|
3
|
+
//#region src/storage/enhanced/redis/core.d.ts
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Creates an enhanced Redis-based storage interface using SuperJSON encoding.
|
|
4
7
|
*
|
|
@@ -18,63 +21,65 @@ import { Redis } from 'ioredis';
|
|
|
18
21
|
* const user = await redisStorage.getItem<{ name: string }>('user:1');
|
|
19
22
|
* ```
|
|
20
23
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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">;
|
|
79
82
|
}>;
|
|
83
|
+
//#endregion
|
|
84
|
+
export { createEnhancedRedisStorage };
|
|
80
85
|
//# sourceMappingURL=core.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.d.ts","
|
|
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"}
|
|
@@ -0,0 +1,85 @@
|
|
|
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
|