@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.
Files changed (211) hide show
  1. package/README.md +1 -1
  2. package/dist/buffer.d.ts +7 -3
  3. package/dist/buffer.d.ts.map +1 -1
  4. package/dist/buffer.js +42 -0
  5. package/dist/buffer.js.map +1 -0
  6. package/dist/clipboard.d.ts +8 -6
  7. package/dist/clipboard.d.ts.map +1 -1
  8. package/dist/clipboard.js +86 -0
  9. package/dist/clipboard.js.map +1 -0
  10. package/dist/consola.d.ts +8 -2
  11. package/dist/consola.d.ts.map +1 -1
  12. package/dist/consola.js +32 -0
  13. package/dist/consola.js.map +1 -0
  14. package/dist/crypto-hash.d.ts +16 -26
  15. package/dist/crypto-hash.d.ts.map +1 -1
  16. package/dist/crypto-hash.js +37 -0
  17. package/dist/crypto-hash.js.map +1 -0
  18. package/dist/datetime.d.ts +13 -9
  19. package/dist/datetime.d.ts.map +1 -1
  20. package/dist/datetime.js +131 -0
  21. package/dist/datetime.js.map +1 -0
  22. package/dist/element-plus.d.ts +8 -4
  23. package/dist/element-plus.d.ts.map +1 -1
  24. package/dist/element-plus.js +37 -0
  25. package/dist/element-plus.js.map +1 -0
  26. package/dist/enum.d.ts +6 -3
  27. package/dist/enum.d.ts.map +1 -1
  28. package/dist/enum.js +108 -0
  29. package/dist/enum.js.map +1 -0
  30. package/dist/env.d.ts +12 -9
  31. package/dist/env.d.ts.map +1 -1
  32. package/dist/env.js +51 -0
  33. package/dist/env.js.map +1 -0
  34. package/dist/general.d.ts +5 -2
  35. package/dist/general.d.ts.map +1 -1
  36. package/dist/general.js +8 -0
  37. package/dist/general.js.map +1 -0
  38. package/dist/hash.d.ts +7 -4
  39. package/dist/hash.d.ts.map +1 -1
  40. package/dist/hash.js +20 -0
  41. package/dist/hash.js.map +1 -0
  42. package/dist/math.d.ts +20 -17
  43. package/dist/math.d.ts.map +1 -1
  44. package/dist/math.js +36 -0
  45. package/dist/math.js.map +1 -0
  46. package/dist/number.d.ts +7 -2
  47. package/dist/number.d.ts.map +1 -1
  48. package/dist/number.js +32 -0
  49. package/dist/number.js.map +1 -0
  50. package/dist/object.d.ts +4 -1
  51. package/dist/object.d.ts.map +1 -1
  52. package/dist/object.js +42 -0
  53. package/dist/object.js.map +1 -0
  54. package/dist/pino.d.ts +8 -2
  55. package/dist/pino.d.ts.map +1 -1
  56. package/dist/pino.js +40 -0
  57. package/dist/pino.js.map +1 -0
  58. package/dist/random.d.ts +4 -1
  59. package/dist/random.d.ts.map +1 -1
  60. package/dist/random.js +31 -0
  61. package/dist/random.js.map +1 -0
  62. package/dist/storage/enhanced/local/core.d.ts +45 -42
  63. package/dist/storage/enhanced/local/core.d.ts.map +1 -1
  64. package/dist/storage/enhanced/local/core.js +63 -0
  65. package/dist/storage/enhanced/local/core.js.map +1 -0
  66. package/dist/storage/enhanced/local/index.d.ts +3 -3
  67. package/dist/storage/enhanced/local/index.js +4 -0
  68. package/dist/storage/enhanced/local/keyed-store.d.ts +14 -11
  69. package/dist/storage/enhanced/local/keyed-store.d.ts.map +1 -1
  70. package/dist/storage/enhanced/local/keyed-store.js +36 -0
  71. package/dist/storage/enhanced/local/keyed-store.js.map +1 -0
  72. package/dist/storage/enhanced/redis/core.d.ts +64 -59
  73. package/dist/storage/enhanced/redis/core.d.ts.map +1 -1
  74. package/dist/storage/enhanced/redis/core.js +85 -0
  75. package/dist/storage/enhanced/redis/core.js.map +1 -0
  76. package/dist/storage/enhanced/redis/index.d.ts +3 -3
  77. package/dist/storage/enhanced/redis/index.js +4 -0
  78. package/dist/storage/enhanced/redis/keyed-store.d.ts +19 -14
  79. package/dist/storage/enhanced/redis/keyed-store.d.ts.map +1 -1
  80. package/dist/storage/enhanced/redis/keyed-store.js +41 -0
  81. package/dist/storage/enhanced/redis/keyed-store.js.map +1 -0
  82. package/dist/storage/lru/keyed-store.d.ts +21 -16
  83. package/dist/storage/lru/keyed-store.d.ts.map +1 -1
  84. package/dist/storage/lru/keyed-store.js +40 -0
  85. package/dist/storage/lru/keyed-store.js.map +1 -0
  86. package/dist/string.d.ts +5 -2
  87. package/dist/string.d.ts.map +1 -1
  88. package/dist/string.js +42 -0
  89. package/dist/string.js.map +1 -0
  90. package/dist/url.d.ts +4 -1
  91. package/dist/url.d.ts.map +1 -1
  92. package/dist/url.js +21 -0
  93. package/dist/url.js.map +1 -0
  94. package/dist/vue.d.ts +10 -5
  95. package/dist/vue.d.ts.map +1 -1
  96. package/dist/vue.js +58 -0
  97. package/dist/vue.js.map +1 -0
  98. package/dist/web.d.ts +5 -2
  99. package/dist/web.d.ts.map +1 -1
  100. package/dist/web.js +32 -0
  101. package/dist/web.js.map +1 -0
  102. package/package.json +43 -27
  103. package/src/env.ts +1 -1
  104. package/src/vue.ts +1 -1
  105. package/src/web.ts +1 -1
  106. package/dist/buffer.cjs +0 -43
  107. package/dist/buffer.cjs.map +0 -1
  108. package/dist/buffer.mjs +0 -41
  109. package/dist/buffer.mjs.map +0 -1
  110. package/dist/clipboard.cjs +0 -91
  111. package/dist/clipboard.cjs.map +0 -1
  112. package/dist/clipboard.mjs +0 -88
  113. package/dist/clipboard.mjs.map +0 -1
  114. package/dist/consola.cjs +0 -35
  115. package/dist/consola.cjs.map +0 -1
  116. package/dist/consola.mjs +0 -32
  117. package/dist/consola.mjs.map +0 -1
  118. package/dist/crypto-hash.cjs +0 -61
  119. package/dist/crypto-hash.cjs.map +0 -1
  120. package/dist/crypto-hash.mjs +0 -50
  121. package/dist/crypto-hash.mjs.map +0 -1
  122. package/dist/datetime.cjs +0 -134
  123. package/dist/datetime.cjs.map +0 -1
  124. package/dist/datetime.mjs +0 -130
  125. package/dist/datetime.mjs.map +0 -1
  126. package/dist/element-plus.cjs +0 -37
  127. package/dist/element-plus.cjs.map +0 -1
  128. package/dist/element-plus.mjs +0 -35
  129. package/dist/element-plus.mjs.map +0 -1
  130. package/dist/enum.cjs +0 -112
  131. package/dist/enum.cjs.map +0 -1
  132. package/dist/enum.mjs +0 -108
  133. package/dist/enum.mjs.map +0 -1
  134. package/dist/env.cjs +0 -53
  135. package/dist/env.cjs.map +0 -1
  136. package/dist/env.mjs +0 -50
  137. package/dist/env.mjs.map +0 -1
  138. package/dist/general.cjs +0 -10
  139. package/dist/general.cjs.map +0 -1
  140. package/dist/general.mjs +0 -8
  141. package/dist/general.mjs.map +0 -1
  142. package/dist/hash.cjs +0 -36
  143. package/dist/hash.cjs.map +0 -1
  144. package/dist/hash.mjs +0 -31
  145. package/dist/hash.mjs.map +0 -1
  146. package/dist/math.cjs +0 -38
  147. package/dist/math.cjs.map +0 -1
  148. package/dist/math.mjs +0 -36
  149. package/dist/math.mjs.map +0 -1
  150. package/dist/number.cjs +0 -32
  151. package/dist/number.cjs.map +0 -1
  152. package/dist/number.mjs +0 -30
  153. package/dist/number.mjs.map +0 -1
  154. package/dist/object.cjs +0 -55
  155. package/dist/object.cjs.map +0 -1
  156. package/dist/object.mjs +0 -53
  157. package/dist/object.mjs.map +0 -1
  158. package/dist/pino.cjs +0 -42
  159. package/dist/pino.cjs.map +0 -1
  160. package/dist/pino.mjs +0 -39
  161. package/dist/pino.mjs.map +0 -1
  162. package/dist/random.cjs +0 -31
  163. package/dist/random.cjs.map +0 -1
  164. package/dist/random.mjs +0 -29
  165. package/dist/random.mjs.map +0 -1
  166. package/dist/storage/enhanced/local/core.cjs +0 -105
  167. package/dist/storage/enhanced/local/core.cjs.map +0 -1
  168. package/dist/storage/enhanced/local/core.mjs +0 -103
  169. package/dist/storage/enhanced/local/core.mjs.map +0 -1
  170. package/dist/storage/enhanced/local/index.cjs +0 -10
  171. package/dist/storage/enhanced/local/index.cjs.map +0 -1
  172. package/dist/storage/enhanced/local/index.d.ts.map +0 -1
  173. package/dist/storage/enhanced/local/index.mjs +0 -3
  174. package/dist/storage/enhanced/local/index.mjs.map +0 -1
  175. package/dist/storage/enhanced/local/keyed-store.cjs +0 -41
  176. package/dist/storage/enhanced/local/keyed-store.cjs.map +0 -1
  177. package/dist/storage/enhanced/local/keyed-store.mjs +0 -39
  178. package/dist/storage/enhanced/local/keyed-store.mjs.map +0 -1
  179. package/dist/storage/enhanced/redis/core.cjs +0 -144
  180. package/dist/storage/enhanced/redis/core.cjs.map +0 -1
  181. package/dist/storage/enhanced/redis/core.mjs +0 -142
  182. package/dist/storage/enhanced/redis/core.mjs.map +0 -1
  183. package/dist/storage/enhanced/redis/index.cjs +0 -10
  184. package/dist/storage/enhanced/redis/index.cjs.map +0 -1
  185. package/dist/storage/enhanced/redis/index.d.ts.map +0 -1
  186. package/dist/storage/enhanced/redis/index.mjs +0 -3
  187. package/dist/storage/enhanced/redis/index.mjs.map +0 -1
  188. package/dist/storage/enhanced/redis/keyed-store.cjs +0 -46
  189. package/dist/storage/enhanced/redis/keyed-store.cjs.map +0 -1
  190. package/dist/storage/enhanced/redis/keyed-store.mjs +0 -44
  191. package/dist/storage/enhanced/redis/keyed-store.mjs.map +0 -1
  192. package/dist/storage/lru/keyed-store.cjs +0 -49
  193. package/dist/storage/lru/keyed-store.cjs.map +0 -1
  194. package/dist/storage/lru/keyed-store.mjs +0 -47
  195. package/dist/storage/lru/keyed-store.mjs.map +0 -1
  196. package/dist/string.cjs +0 -45
  197. package/dist/string.cjs.map +0 -1
  198. package/dist/string.mjs +0 -43
  199. package/dist/string.mjs.map +0 -1
  200. package/dist/url.cjs +0 -21
  201. package/dist/url.cjs.map +0 -1
  202. package/dist/url.mjs +0 -19
  203. package/dist/url.mjs.map +0 -1
  204. package/dist/vue.cjs +0 -64
  205. package/dist/vue.cjs.map +0 -1
  206. package/dist/vue.mjs +0 -59
  207. package/dist/vue.mjs.map +0 -1
  208. package/dist/web.cjs +0 -35
  209. package/dist/web.cjs.map +0 -1
  210. package/dist/web.mjs +0 -32
  211. 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
- export declare const pinoLogger: import("pino").Logger<never, boolean>;
23
- export declare const logger: import("pino").Logger<never, boolean>;
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
@@ -1 +1 @@
1
- {"version":3,"file":"pino.d.ts","sourceRoot":"","sources":["../src/pino.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,UAAU,uCAAmB,CAAC;AAC3C,eAAO,MAAM,MAAM,uCAAa,CAAC"}
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
@@ -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
- export declare function generateWithNestedRandomLength<T = string>(generator: (length: number) => T, minMin: number, minMax: number, maxMin: number, maxMax: number): T;
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
@@ -1 +1 @@
1
- {"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,8BAA8B,CAAC,CAAC,GAAG,MAAM,EACrD,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,CAAC,EAChC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,KAMjB"}
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
- export declare const enhancedLocalStorage: Readonly<{
16
- /**
17
- * Removes all items from localStorage.
18
- */
19
- clear: () => void;
20
- /**
21
- * Retrieves a value by key and decodes it using SuperJSON or raw string.
22
- *
23
- * @template T - The expected type of the value
24
- *
25
- * @param {string} key - The key of the value to retrieve
26
- *
27
- * @returns {null | T} The decoded value or null if not found
28
- */
29
- getItem<T = unknown>(key: string): T | null;
30
- /**
31
- * Checks whether a key exists in localStorage.
32
- *
33
- * @param {string} key - The key to check
34
- *
35
- * @returns {boolean} True if the key exists, false otherwise
36
- */
37
- hasItem: (key: string) => boolean;
38
- /**
39
- * Returns the number of items stored in localStorage.
40
- *
41
- * @returns {number} The number of items stored in localStorage
42
- */
43
- readonly length: number;
44
- /**
45
- * Removes a specific key from localStorage.
46
- *
47
- * @param {string} key - The key to remove
48
- */
49
- removeItem: (key: string) => void;
50
- /**
51
- * Stores a value in localStorage with automatic serialization.
52
- *
53
- * @param {string} key - The key to store the value under
54
- * @param {any} value - The value to store
55
- */
56
- setItem: (key: string, value: any) => void;
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","sourceRoot":"","sources":["../../../../src/storage/enhanced/local/core.ts"],"names":[],"mappings":"AAcA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,oBAAoB;IAC7B;;OAEG;;IAEH;;;;;;;;OAQG;YACK,CAAC,iBAAiB,MAAM;IAIhC;;;;;;OAMG;mBACY,MAAM;IACrB;;;;OAIG;;IAIH;;;;OAIG;sBACe,MAAM;IACxB;;;;;OAKG;mBACY,MAAM,SAAS,GAAG;EACnC,CAAC"}
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
- export { enhancedLocalStorage } from './core';
2
- export { createKeyedEnhancedLocalStore } from './keyed-store';
3
- //# sourceMappingURL=index.d.ts.map
1
+ import { enhancedLocalStorage } from "./core.js";
2
+ import { createKeyedEnhancedLocalStore } from "./keyed-store.js";
3
+ export { createKeyedEnhancedLocalStore, enhancedLocalStorage };
@@ -0,0 +1,4 @@
1
+ import { enhancedLocalStorage } from "./core.js";
2
+ import { createKeyedEnhancedLocalStore } from "./keyed-store.js";
3
+
4
+ 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
- export declare function createKeyedEnhancedLocalStore<D = unknown>(): <P extends any[]>(getKeyFunction: (...args: P) => string) => Readonly<{
22
- getItem: (...args: P) => D | null;
23
- hasItem: (...args: P) => boolean;
24
- removeItem: (...args: P) => void;
25
- /**
26
- * Resolves the storage key from the given arguments.
27
- *
28
- * @returns {string} The final string key used internally
29
- */
30
- resolveKey: (...args: P) => string;
31
- setItem: (value: D, ...args: P) => void;
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","sourceRoot":"","sources":["../../../../src/storage/enhanced/local/keyed-store.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,6BAA6B,CAAC,CAAC,GAAG,OAAO,MAC7C,CAAC,SAAS,GAAG,EAAE,EAAE,gBAAgB,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,MAAM;uBACxC,CAAC;uBACD,CAAC;0BACE,CAAC;IACvB;;;;OAIG;0BACmB,CAAC;qBACN,CAAC,WAAW,CAAC;GAErC"}
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 'ioredis';
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
- export declare function createEnhancedRedisStorage(ioRedisInstanceOrUrl: Redis | string): Readonly<{
22
- /**
23
- * Retrieves a value from Redis and decodes it.
24
- *
25
- * @template T - The expected return type
26
- *
27
- * @param {string} key - The Redis key
28
- *
29
- * @returns {Promise<null | T>} The decoded value or null if not found
30
- */
31
- getItem<T = unknown>(key: string): Promise<T | null>;
32
- /**
33
- * Gets the remaining TTL (in seconds) for a given key.
34
- *
35
- * @param {string} key - The Redis key
36
- *
37
- * @returns {Promise<number>} The number of seconds until the key expires, or -1 if no expiration is set
38
- */
39
- getItemTtl: (key: string) => Promise<number>;
40
- /**
41
- * Checks whether a key exists in Redis.
42
- *
43
- * @param {string} key - The Redis key
44
- *
45
- * @returns {Promise<boolean>} True if the key exists, false otherwise
46
- */
47
- hasItem: (key: string) => Promise<boolean>;
48
- /**
49
- * The underlying Redis instance, exposed for advanced operations.
50
- * Use with caution; most use cases should rely on the wrapper methods.
51
- *
52
- * @returns {Redis} The underlying Redis instance
53
- */
54
- readonly instance: Redis;
55
- /**
56
- * Removes a key from Redis.
57
- *
58
- * @param {string} key - The Redis key to delete
59
- *
60
- * @returns {Promise<boolean>} A Promise that resolves to `true` if the key was removed,
61
- * or `false` if it did not exist.
62
- */
63
- removeItem: (key: string) => Promise<boolean>;
64
- /**
65
- * Stores a value in Redis without expiration.
66
- *
67
- * @param {string} key - The Redis key
68
- * @param {any} value - The value to store. Will be serialized
69
- */
70
- setItem: (key: string, value: any) => Promise<"OK">;
71
- /**
72
- * Stores a value in Redis with a time-to-live (TTL).
73
- *
74
- * @param {string} key - The Redis key
75
- * @param {number} seconds - Expiration time in seconds
76
- * @param {any} value - The value to store. Will be serialized
77
- */
78
- setItemWithTtl(key: string, seconds: number, value: any): Promise<"OK">;
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","sourceRoot":"","sources":["../../../../src/storage/enhanced/redis/core.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAoBhC;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,0BAA0B,CAAC,oBAAoB,EAAE,KAAK,GAAG,MAAM;IAGvE;;;;;;;;OAQG;YACW,CAAC,iBAAiB,MAAM;IAItC;;;;;;OAMG;sBACe,MAAM;IACxB;;;;;;OAMG;mBACkB,MAAM;IAC3B;;;;;OAKG;;IAIH;;;;;;;OAOG;sBACqB,MAAM;IAC9B;;;;;OAKG;mBACY,MAAM,SAAS,GAAG;IACjC;;;;;;OAMG;wBACiB,MAAM,WAAW,MAAM,SAAS,GAAG;GAI9D"}
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