@alibarbar/common 1.0.8 → 1.0.10

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 (176) hide show
  1. package/dist/{chunk-O3O67R4I.js → algorithm.cjs} +0 -2
  2. package/dist/algorithm.js +130 -44
  3. package/dist/{chunk-LOAZSPGQ.js → array.cjs} +0 -2
  4. package/dist/array.js +135 -84
  5. package/dist/{chunk-Y364QIQH.js → color.cjs} +0 -2
  6. package/dist/color.js +127 -40
  7. package/dist/{chunk-DYBSRI7V.js → crypto.cjs} +0 -2
  8. package/dist/crypto.js +172 -60
  9. package/dist/{chunk-BHCRFURU.js → data-structure.cjs} +0 -2
  10. package/dist/data-structure.js +481 -32
  11. package/dist/{chunk-I3L42475.js → date.cjs} +0 -2
  12. package/dist/date.js +125 -72
  13. package/dist/{chunk-JK2SE3I2.js → dom.cjs} +0 -2
  14. package/dist/dom.js +84 -56
  15. package/dist/{chunk-7V5UQXIO.js → file.cjs} +0 -2
  16. package/dist/file.js +79 -32
  17. package/dist/{chunk-OX5PLOWB.js → i18n.cjs} +0 -2
  18. package/dist/i18n.js +78 -40
  19. package/dist/index.cjs +4132 -0
  20. package/dist/index.d.mts +3 -159
  21. package/dist/index.d.ts +3 -159
  22. package/dist/index.js +3364 -17427
  23. package/dist/{chunk-4RGXV4SJ.js → network.cjs} +0 -2
  24. package/dist/network.js +97 -28
  25. package/dist/{chunk-3FRYJPHG.js → number.cjs} +0 -2
  26. package/dist/number.js +77 -56
  27. package/dist/{chunk-27UDDVLZ.js → object.cjs} +0 -2
  28. package/dist/object.js +237 -80
  29. package/dist/{chunk-JQZBPAPO.js → performance.cjs} +0 -2
  30. package/dist/performance.js +145 -40
  31. package/dist/services.cjs +291 -0
  32. package/dist/services.d.mts +161 -0
  33. package/dist/services.d.ts +161 -0
  34. package/dist/services.js +281 -0
  35. package/dist/{chunk-WZDOPUJW.js → storage.cjs} +216 -17
  36. package/dist/storage.js +550 -31
  37. package/dist/{chunk-D7CS5EKF.js → string.cjs} +0 -2
  38. package/dist/string.js +91 -68
  39. package/dist/{chunk-56W6YECK.js → tracking.cjs} +0 -2
  40. package/dist/tracking.js +359 -52
  41. package/dist/{chunk-ZDMFMUDR.js → transform.cjs} +0 -2
  42. package/dist/transform.js +299 -32
  43. package/dist/{chunk-NJARVI6X.mjs → upload.cjs} +41 -14
  44. package/dist/upload.js +402 -20
  45. package/dist/{chunk-KGFTD255.js → url.cjs} +0 -2
  46. package/dist/url.js +91 -44
  47. package/dist/{chunk-TQN37HIN.js → validation.cjs} +0 -2
  48. package/dist/validation.js +77 -60
  49. package/package.json +12 -2
  50. package/dist/algorithm.js.map +0 -1
  51. package/dist/algorithm.mjs +0 -4
  52. package/dist/algorithm.mjs.map +0 -1
  53. package/dist/array.js.map +0 -1
  54. package/dist/array.mjs +0 -4
  55. package/dist/array.mjs.map +0 -1
  56. package/dist/chunk-27UDDVLZ.js.map +0 -1
  57. package/dist/chunk-2R2QWFJC.mjs +0 -138
  58. package/dist/chunk-2R2QWFJC.mjs.map +0 -1
  59. package/dist/chunk-3FRYJPHG.js.map +0 -1
  60. package/dist/chunk-4RGXV4SJ.js.map +0 -1
  61. package/dist/chunk-56W6YECK.js.map +0 -1
  62. package/dist/chunk-5BGSUGTI.mjs +0 -128
  63. package/dist/chunk-5BGSUGTI.mjs.map +0 -1
  64. package/dist/chunk-7E6GELHJ.mjs +0 -302
  65. package/dist/chunk-7E6GELHJ.mjs.map +0 -1
  66. package/dist/chunk-7V5UQXIO.js.map +0 -1
  67. package/dist/chunk-A4SWQXX7.mjs +0 -484
  68. package/dist/chunk-A4SWQXX7.mjs.map +0 -1
  69. package/dist/chunk-BHCRFURU.js.map +0 -1
  70. package/dist/chunk-CDSGEAOK.mjs +0 -80
  71. package/dist/chunk-CDSGEAOK.mjs.map +0 -1
  72. package/dist/chunk-D7CS5EKF.js.map +0 -1
  73. package/dist/chunk-DYBSRI7V.js.map +0 -1
  74. package/dist/chunk-FEBKPX5A.js +0 -386
  75. package/dist/chunk-FEBKPX5A.js.map +0 -1
  76. package/dist/chunk-FJ6ZGZIA.mjs +0 -39
  77. package/dist/chunk-FJ6ZGZIA.mjs.map +0 -1
  78. package/dist/chunk-HLDFI7R2.mjs +0 -175
  79. package/dist/chunk-HLDFI7R2.mjs.map +0 -1
  80. package/dist/chunk-I3L42475.js.map +0 -1
  81. package/dist/chunk-JBLX27WD.mjs +0 -240
  82. package/dist/chunk-JBLX27WD.mjs.map +0 -1
  83. package/dist/chunk-JHZ7M2MR.mjs +0 -133
  84. package/dist/chunk-JHZ7M2MR.mjs.map +0 -1
  85. package/dist/chunk-JK2SE3I2.js.map +0 -1
  86. package/dist/chunk-JQZBPAPO.js.map +0 -1
  87. package/dist/chunk-JXYGC2C5.mjs +0 -100
  88. package/dist/chunk-JXYGC2C5.mjs.map +0 -1
  89. package/dist/chunk-KGFTD255.js.map +0 -1
  90. package/dist/chunk-LBHBNPNJ.mjs +0 -148
  91. package/dist/chunk-LBHBNPNJ.mjs.map +0 -1
  92. package/dist/chunk-LF4CILQS.mjs +0 -87
  93. package/dist/chunk-LF4CILQS.mjs.map +0 -1
  94. package/dist/chunk-LOAZSPGQ.js.map +0 -1
  95. package/dist/chunk-NJARVI6X.mjs.map +0 -1
  96. package/dist/chunk-NSSDYX2U.mjs +0 -80
  97. package/dist/chunk-NSSDYX2U.mjs.map +0 -1
  98. package/dist/chunk-O3O67R4I.js.map +0 -1
  99. package/dist/chunk-OIXQ3E6W.mjs +0 -354
  100. package/dist/chunk-OIXQ3E6W.mjs.map +0 -1
  101. package/dist/chunk-OX5PLOWB.js.map +0 -1
  102. package/dist/chunk-PJ7UCTX4.mjs +0 -362
  103. package/dist/chunk-PJ7UCTX4.mjs.map +0 -1
  104. package/dist/chunk-PR4QN5HX.js +0 -44
  105. package/dist/chunk-PR4QN5HX.js.map +0 -1
  106. package/dist/chunk-QIBE7GVN.mjs +0 -81
  107. package/dist/chunk-QIBE7GVN.mjs.map +0 -1
  108. package/dist/chunk-QIOC54LQ.mjs +0 -130
  109. package/dist/chunk-QIOC54LQ.mjs.map +0 -1
  110. package/dist/chunk-TQN37HIN.js.map +0 -1
  111. package/dist/chunk-WZDOPUJW.js.map +0 -1
  112. package/dist/chunk-XJTZDXSR.mjs +0 -94
  113. package/dist/chunk-XJTZDXSR.mjs.map +0 -1
  114. package/dist/chunk-Y364QIQH.js.map +0 -1
  115. package/dist/chunk-YXM6Q4JS.mjs +0 -94
  116. package/dist/chunk-YXM6Q4JS.mjs.map +0 -1
  117. package/dist/chunk-ZDMFMUDR.js.map +0 -1
  118. package/dist/chunk-ZVJ6NQUM.mjs +0 -82
  119. package/dist/chunk-ZVJ6NQUM.mjs.map +0 -1
  120. package/dist/color.js.map +0 -1
  121. package/dist/color.mjs +0 -4
  122. package/dist/color.mjs.map +0 -1
  123. package/dist/crypto.js.map +0 -1
  124. package/dist/crypto.mjs +0 -4
  125. package/dist/crypto.mjs.map +0 -1
  126. package/dist/data-structure.js.map +0 -1
  127. package/dist/data-structure.mjs +0 -4
  128. package/dist/data-structure.mjs.map +0 -1
  129. package/dist/date.js.map +0 -1
  130. package/dist/date.mjs +0 -4
  131. package/dist/date.mjs.map +0 -1
  132. package/dist/dom.js.map +0 -1
  133. package/dist/dom.mjs +0 -4
  134. package/dist/dom.mjs.map +0 -1
  135. package/dist/file.js.map +0 -1
  136. package/dist/file.mjs +0 -4
  137. package/dist/file.mjs.map +0 -1
  138. package/dist/i18n.js.map +0 -1
  139. package/dist/i18n.mjs +0 -4
  140. package/dist/i18n.mjs.map +0 -1
  141. package/dist/index.js.map +0 -1
  142. package/dist/index.mjs +0 -17155
  143. package/dist/index.mjs.map +0 -1
  144. package/dist/network.js.map +0 -1
  145. package/dist/network.mjs +0 -4
  146. package/dist/network.mjs.map +0 -1
  147. package/dist/number.js.map +0 -1
  148. package/dist/number.mjs +0 -4
  149. package/dist/number.mjs.map +0 -1
  150. package/dist/object.js.map +0 -1
  151. package/dist/object.mjs +0 -4
  152. package/dist/object.mjs.map +0 -1
  153. package/dist/performance.js.map +0 -1
  154. package/dist/performance.mjs +0 -4
  155. package/dist/performance.mjs.map +0 -1
  156. package/dist/storage.js.map +0 -1
  157. package/dist/storage.mjs +0 -5
  158. package/dist/storage.mjs.map +0 -1
  159. package/dist/string.js.map +0 -1
  160. package/dist/string.mjs +0 -4
  161. package/dist/string.mjs.map +0 -1
  162. package/dist/tracking.js.map +0 -1
  163. package/dist/tracking.mjs +0 -4
  164. package/dist/tracking.mjs.map +0 -1
  165. package/dist/transform.js.map +0 -1
  166. package/dist/transform.mjs +0 -4
  167. package/dist/transform.mjs.map +0 -1
  168. package/dist/upload.js.map +0 -1
  169. package/dist/upload.mjs +0 -5
  170. package/dist/upload.mjs.map +0 -1
  171. package/dist/url.js.map +0 -1
  172. package/dist/url.mjs +0 -4
  173. package/dist/url.mjs.map +0 -1
  174. package/dist/validation.js.map +0 -1
  175. package/dist/validation.mjs +0 -4
  176. package/dist/validation.mjs.map +0 -1
package/dist/storage.js CHANGED
@@ -1,34 +1,553 @@
1
- 'use strict';
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropNames = Object.getOwnPropertyNames;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
+ };
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
2
10
 
3
- var chunkWZDOPUJW_js = require('./chunk-WZDOPUJW.js');
4
- require('./chunk-DYBSRI7V.js');
5
- require('./chunk-PR4QN5HX.js');
6
-
7
-
8
-
9
- Object.defineProperty(exports, "cookie", {
10
- enumerable: true,
11
- get: function () { return chunkWZDOPUJW_js.cookie; }
12
- });
13
- Object.defineProperty(exports, "getStorageKeyPair", {
14
- enumerable: true,
15
- get: function () { return chunkWZDOPUJW_js.getStorageKeyPair; }
16
- });
17
- Object.defineProperty(exports, "localStorage", {
18
- enumerable: true,
19
- get: function () { return chunkWZDOPUJW_js.localStorage; }
11
+ // src/helper/crypto.ts
12
+ var crypto_exports = {};
13
+ __export(crypto_exports, {
14
+ base64Decode: () => base64Decode,
15
+ base64Encode: () => base64Encode,
16
+ exportPrivateKey: () => exportPrivateKey,
17
+ exportPublicKey: () => exportPublicKey,
18
+ generateRSAKeyPair: () => generateRSAKeyPair,
19
+ generateRandomString: () => generateRandomString,
20
+ generateUUID: () => generateUUID,
21
+ hash: () => hash,
22
+ importPrivateKey: () => importPrivateKey,
23
+ importPublicKey: () => importPublicKey,
24
+ rsaDecrypt: () => rsaDecrypt,
25
+ rsaEncrypt: () => rsaEncrypt,
26
+ sha256: () => sha256
20
27
  });
21
- Object.defineProperty(exports, "sessionStorage", {
22
- enumerable: true,
23
- get: function () { return chunkWZDOPUJW_js.sessionStorage; }
28
+ async function sha256(data) {
29
+ const buffer = typeof data === "string" ? new TextEncoder().encode(data) : data;
30
+ const hashBuffer = await crypto.subtle.digest("SHA-256", buffer);
31
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
32
+ return hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
33
+ }
34
+ function base64Encode(data) {
35
+ if (typeof data === "string") {
36
+ return btoa(unescape(encodeURIComponent(data)));
37
+ }
38
+ const bytes = new Uint8Array(data);
39
+ let binary = "";
40
+ for (let i = 0; i < bytes.length; i++) {
41
+ binary += String.fromCharCode(bytes[i]);
42
+ }
43
+ return btoa(binary);
44
+ }
45
+ function base64Decode(data) {
46
+ try {
47
+ return decodeURIComponent(escape(atob(data)));
48
+ } catch {
49
+ throw new Error("Invalid Base64 string");
50
+ }
51
+ }
52
+ function generateUUID() {
53
+ if (typeof crypto !== "undefined" && crypto.randomUUID) {
54
+ return crypto.randomUUID();
55
+ }
56
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
57
+ const r = Math.random() * 16 | 0;
58
+ const v = c === "x" ? r : r & 3 | 8;
59
+ return v.toString(16);
60
+ });
61
+ }
62
+ function generateRandomString(length, charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
63
+ let result = "";
64
+ for (let i = 0; i < length; i++) {
65
+ result += charset.charAt(Math.floor(Math.random() * charset.length));
66
+ }
67
+ return result;
68
+ }
69
+ function hash(data) {
70
+ let hashValue = 0;
71
+ for (let i = 0; i < data.length; i++) {
72
+ const char = data.charCodeAt(i);
73
+ hashValue = (hashValue << 5) - hashValue + char;
74
+ hashValue = hashValue & hashValue;
75
+ }
76
+ return Math.abs(hashValue);
77
+ }
78
+ async function generateRSAKeyPair(modulusLength = 2048) {
79
+ if (typeof crypto === "undefined" || !crypto.subtle) {
80
+ throw new Error("Web Crypto API is not available");
81
+ }
82
+ const keyPair = await crypto.subtle.generateKey(
83
+ {
84
+ name: "RSA-OAEP",
85
+ modulusLength,
86
+ publicExponent: new Uint8Array([1, 0, 1]),
87
+ hash: "SHA-256"
88
+ },
89
+ true,
90
+ ["encrypt", "decrypt"]
91
+ );
92
+ return {
93
+ publicKey: keyPair.publicKey,
94
+ privateKey: keyPair.privateKey
95
+ };
96
+ }
97
+ async function rsaEncrypt(data, publicKey) {
98
+ if (typeof crypto === "undefined" || !crypto.subtle) {
99
+ throw new Error("Web Crypto API is not available");
100
+ }
101
+ const encoder = new TextEncoder();
102
+ const dataBuffer = encoder.encode(data);
103
+ const maxChunkSize = 245;
104
+ const chunks = [];
105
+ for (let i = 0; i < dataBuffer.length; i += maxChunkSize) {
106
+ const chunk = dataBuffer.slice(i, i + maxChunkSize);
107
+ const encrypted = await crypto.subtle.encrypt(
108
+ {
109
+ name: "RSA-OAEP"
110
+ },
111
+ publicKey,
112
+ chunk
113
+ );
114
+ chunks.push(encrypted);
115
+ }
116
+ const totalLength = chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0);
117
+ const merged = new Uint8Array(totalLength);
118
+ let offset = 0;
119
+ for (const chunk of chunks) {
120
+ merged.set(new Uint8Array(chunk), offset);
121
+ offset += chunk.byteLength;
122
+ }
123
+ return base64Encode(merged.buffer);
124
+ }
125
+ async function rsaDecrypt(encryptedData, privateKey) {
126
+ if (typeof crypto === "undefined" || !crypto.subtle) {
127
+ throw new Error("Web Crypto API is not available");
128
+ }
129
+ const binaryString = atob(encryptedData);
130
+ const encryptedArray = new Uint8Array(binaryString.length);
131
+ for (let i = 0; i < binaryString.length; i++) {
132
+ encryptedArray[i] = binaryString.charCodeAt(i);
133
+ }
134
+ const chunkSize = 256;
135
+ const chunks = [];
136
+ for (let i = 0; i < encryptedArray.length; i += chunkSize) {
137
+ const chunk = encryptedArray.slice(i, i + chunkSize);
138
+ const decrypted = await crypto.subtle.decrypt(
139
+ {
140
+ name: "RSA-OAEP"
141
+ },
142
+ privateKey,
143
+ chunk
144
+ );
145
+ const decoder = new TextDecoder();
146
+ chunks.push(decoder.decode(decrypted));
147
+ }
148
+ return chunks.join("");
149
+ }
150
+ async function exportPublicKey(publicKey) {
151
+ if (typeof crypto === "undefined" || !crypto.subtle) {
152
+ throw new Error("Web Crypto API is not available");
153
+ }
154
+ const exported = await crypto.subtle.exportKey("spki", publicKey);
155
+ return base64Encode(exported);
156
+ }
157
+ async function exportPrivateKey(privateKey) {
158
+ if (typeof crypto === "undefined" || !crypto.subtle) {
159
+ throw new Error("Web Crypto API is not available");
160
+ }
161
+ const exported = await crypto.subtle.exportKey("pkcs8", privateKey);
162
+ return base64Encode(exported);
163
+ }
164
+ async function importPublicKey(keyData) {
165
+ if (typeof crypto === "undefined" || !crypto.subtle) {
166
+ throw new Error("Web Crypto API is not available");
167
+ }
168
+ const keyBuffer = base64Decode(keyData);
169
+ const keyArray = new Uint8Array(keyBuffer.split("").map((char) => char.charCodeAt(0)));
170
+ return crypto.subtle.importKey(
171
+ "spki",
172
+ keyArray.buffer,
173
+ {
174
+ name: "RSA-OAEP",
175
+ hash: "SHA-256"
176
+ },
177
+ true,
178
+ ["encrypt"]
179
+ );
180
+ }
181
+ async function importPrivateKey(keyData) {
182
+ if (typeof crypto === "undefined" || !crypto.subtle) {
183
+ throw new Error("Web Crypto API is not available");
184
+ }
185
+ const keyBuffer = base64Decode(keyData);
186
+ const keyArray = new Uint8Array(keyBuffer.split("").map((char) => char.charCodeAt(0)));
187
+ return crypto.subtle.importKey(
188
+ "pkcs8",
189
+ keyArray.buffer,
190
+ {
191
+ name: "RSA-OAEP",
192
+ hash: "SHA-256"
193
+ },
194
+ true,
195
+ ["decrypt"]
196
+ );
197
+ }
198
+ var init_crypto = __esm({
199
+ "src/helper/crypto.ts"() {
200
+ }
24
201
  });
25
- Object.defineProperty(exports, "setStorageKeyPair", {
26
- enumerable: true,
27
- get: function () { return chunkWZDOPUJW_js.setStorageKeyPair; }
28
- });
29
- Object.defineProperty(exports, "storage", {
30
- enumerable: true,
31
- get: function () { return chunkWZDOPUJW_js.storage; }
32
- });
33
- //# sourceMappingURL=storage.js.map
34
- //# sourceMappingURL=storage.js.map
202
+
203
+ // src/browser/storage.ts
204
+ init_crypto();
205
+ var globalKeyPair = null;
206
+ function setStorageKeyPair(keyPair) {
207
+ globalKeyPair = keyPair;
208
+ }
209
+ function getStorageKeyPair() {
210
+ return globalKeyPair;
211
+ }
212
+ async function encryptValue(value, publicKey) {
213
+ return rsaEncrypt(value, publicKey);
214
+ }
215
+ async function decryptValue(encryptedValue, privateKey) {
216
+ try {
217
+ return await rsaDecrypt(encryptedValue, privateKey);
218
+ } catch {
219
+ try {
220
+ JSON.parse(encryptedValue);
221
+ return encryptedValue;
222
+ } catch {
223
+ throw new Error("Failed to decrypt storage value");
224
+ }
225
+ }
226
+ }
227
+ var localStorage = {
228
+ /**
229
+ * 设置值
230
+ * @param key - 键
231
+ * @param value - 值
232
+ * @param options - 选项(过期时间、密钥等)
233
+ * @returns Promise<void>
234
+ */
235
+ async set(key, value, options = {}) {
236
+ if (typeof window === "undefined" || !window.localStorage) {
237
+ throw new Error("localStorage is not available");
238
+ }
239
+ const { expiry, publicKey } = options;
240
+ const item = {
241
+ value,
242
+ expiry: expiry ? Date.now() + expiry : void 0
243
+ };
244
+ try {
245
+ const jsonStr = JSON.stringify(item);
246
+ const keyToUse = publicKey || globalKeyPair?.publicKey;
247
+ if (keyToUse) {
248
+ const encrypted = await encryptValue(jsonStr, keyToUse);
249
+ window.localStorage.setItem(key, encrypted);
250
+ } else {
251
+ const { base64Encode: base64Encode2 } = await Promise.resolve().then(() => (init_crypto(), crypto_exports));
252
+ const encoded = base64Encode2(jsonStr);
253
+ window.localStorage.setItem(key, encoded);
254
+ }
255
+ } catch (error) {
256
+ console.error("localStorage.set error:", error);
257
+ throw error;
258
+ }
259
+ },
260
+ /**
261
+ * 获取值
262
+ * @param key - 键
263
+ * @param options - 选项(默认值、私钥等)
264
+ * @returns Promise<T | undefined> 值或默认值
265
+ */
266
+ async get(key, options = {}) {
267
+ if (typeof window === "undefined" || !window.localStorage) {
268
+ return options.defaultValue;
269
+ }
270
+ const { defaultValue, privateKey } = options;
271
+ try {
272
+ const encryptedStr = window.localStorage.getItem(key);
273
+ if (!encryptedStr) return defaultValue;
274
+ let decryptedStr;
275
+ const keyToUse = privateKey || globalKeyPair?.privateKey;
276
+ if (keyToUse) {
277
+ try {
278
+ decryptedStr = await decryptValue(encryptedStr, keyToUse);
279
+ } catch {
280
+ const { base64Decode: base64Decode2 } = await Promise.resolve().then(() => (init_crypto(), crypto_exports));
281
+ try {
282
+ decryptedStr = base64Decode2(encryptedStr);
283
+ } catch {
284
+ return defaultValue;
285
+ }
286
+ }
287
+ } else {
288
+ const { base64Decode: base64Decode2 } = await Promise.resolve().then(() => (init_crypto(), crypto_exports));
289
+ try {
290
+ decryptedStr = base64Decode2(encryptedStr);
291
+ } catch {
292
+ return defaultValue;
293
+ }
294
+ }
295
+ const item = JSON.parse(decryptedStr);
296
+ if (item.expiry && Date.now() > item.expiry) {
297
+ window.localStorage.removeItem(key);
298
+ return defaultValue;
299
+ }
300
+ return item.value;
301
+ } catch {
302
+ return defaultValue;
303
+ }
304
+ },
305
+ /**
306
+ * 移除值
307
+ * @param key - 键
308
+ */
309
+ remove(key) {
310
+ if (typeof window === "undefined" || !window.localStorage) return;
311
+ try {
312
+ window.localStorage.removeItem(key);
313
+ } catch (error) {
314
+ console.error("localStorage.remove error:", error);
315
+ }
316
+ },
317
+ /**
318
+ * 清空所有值
319
+ */
320
+ clear() {
321
+ if (typeof window === "undefined" || !window.localStorage) return;
322
+ try {
323
+ window.localStorage.clear();
324
+ } catch (error) {
325
+ console.error("localStorage.clear error:", error);
326
+ }
327
+ },
328
+ /**
329
+ * 获取所有键
330
+ * @returns 键数组
331
+ */
332
+ keys() {
333
+ if (typeof window === "undefined" || !window.localStorage) return [];
334
+ const keys = [];
335
+ try {
336
+ for (let i = 0; i < window.localStorage.length; i++) {
337
+ const key = window.localStorage.key(i);
338
+ if (key) keys.push(key);
339
+ }
340
+ } catch (error) {
341
+ console.error("localStorage.keys error:", error);
342
+ }
343
+ return keys;
344
+ }
345
+ };
346
+ var sessionStorage = {
347
+ /**
348
+ * 设置值
349
+ * @param key - 键
350
+ * @param value - 值
351
+ * @param options - 选项(公钥等)
352
+ * @returns Promise<void>
353
+ */
354
+ async set(key, value, options = {}) {
355
+ if (typeof window === "undefined" || !window.sessionStorage) {
356
+ throw new Error("sessionStorage is not available");
357
+ }
358
+ const { publicKey } = options;
359
+ try {
360
+ const jsonStr = JSON.stringify(value);
361
+ const keyToUse = publicKey || globalKeyPair?.publicKey;
362
+ if (keyToUse) {
363
+ const encrypted = await encryptValue(jsonStr, keyToUse);
364
+ window.sessionStorage.setItem(key, encrypted);
365
+ } else {
366
+ const { base64Encode: base64Encode2 } = await Promise.resolve().then(() => (init_crypto(), crypto_exports));
367
+ const encoded = base64Encode2(jsonStr);
368
+ window.sessionStorage.setItem(key, encoded);
369
+ }
370
+ } catch (error) {
371
+ console.error("sessionStorage.set error:", error);
372
+ throw error;
373
+ }
374
+ },
375
+ /**
376
+ * 获取值
377
+ * @param key - 键
378
+ * @param options - 选项(默认值、私钥等)
379
+ * @returns Promise<T | undefined> 值或默认值
380
+ */
381
+ async get(key, options = {}) {
382
+ if (typeof window === "undefined" || !window.sessionStorage) {
383
+ return options.defaultValue;
384
+ }
385
+ const { defaultValue, privateKey } = options;
386
+ try {
387
+ const encryptedStr = window.sessionStorage.getItem(key);
388
+ if (!encryptedStr) return defaultValue;
389
+ let decryptedStr;
390
+ const keyToUse = privateKey || globalKeyPair?.privateKey;
391
+ if (keyToUse) {
392
+ try {
393
+ decryptedStr = await decryptValue(encryptedStr, keyToUse);
394
+ } catch {
395
+ const { base64Decode: base64Decode2 } = await Promise.resolve().then(() => (init_crypto(), crypto_exports));
396
+ try {
397
+ decryptedStr = base64Decode2(encryptedStr);
398
+ } catch {
399
+ return defaultValue;
400
+ }
401
+ }
402
+ } else {
403
+ const { base64Decode: base64Decode2 } = await Promise.resolve().then(() => (init_crypto(), crypto_exports));
404
+ try {
405
+ decryptedStr = base64Decode2(encryptedStr);
406
+ } catch {
407
+ return defaultValue;
408
+ }
409
+ }
410
+ return JSON.parse(decryptedStr);
411
+ } catch {
412
+ return defaultValue;
413
+ }
414
+ },
415
+ /**
416
+ * 移除值
417
+ * @param key - 键
418
+ */
419
+ remove(key) {
420
+ if (typeof window === "undefined" || !window.sessionStorage) return;
421
+ try {
422
+ window.sessionStorage.removeItem(key);
423
+ } catch (error) {
424
+ console.error("sessionStorage.remove error:", error);
425
+ }
426
+ },
427
+ /**
428
+ * 清空所有值
429
+ */
430
+ clear() {
431
+ if (typeof window === "undefined" || !window.sessionStorage) return;
432
+ try {
433
+ window.sessionStorage.clear();
434
+ } catch (error) {
435
+ console.error("sessionStorage.clear error:", error);
436
+ }
437
+ }
438
+ };
439
+ var cookie = {
440
+ /**
441
+ * 设置Cookie
442
+ * @param key - 键
443
+ * @param value - 值
444
+ * @param options - Cookie选项
445
+ */
446
+ set(key, value, options = {}) {
447
+ if (typeof document === "undefined") {
448
+ throw new Error("document is not available");
449
+ }
450
+ let cookieStr = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
451
+ if (options.expires) {
452
+ const expiresDate = options.expires instanceof Date ? options.expires : new Date(Date.now() + options.expires * 24 * 60 * 60 * 1e3);
453
+ cookieStr += `; expires=${expiresDate.toUTCString()}`;
454
+ }
455
+ if (options.path) cookieStr += `; path=${options.path}`;
456
+ if (options.domain) cookieStr += `; domain=${options.domain}`;
457
+ if (options.secure) cookieStr += "; secure";
458
+ if (options.sameSite) cookieStr += `; sameSite=${options.sameSite}`;
459
+ document.cookie = cookieStr;
460
+ },
461
+ /**
462
+ * 获取Cookie
463
+ * @param key - 键
464
+ * @returns Cookie值
465
+ */
466
+ get(key) {
467
+ if (typeof document === "undefined") return void 0;
468
+ const name = encodeURIComponent(key);
469
+ const cookies = document.cookie.split(";");
470
+ for (const cookie2 of cookies) {
471
+ const [cookieKey, cookieValue] = cookie2.trim().split("=");
472
+ if (cookieKey === name) {
473
+ return decodeURIComponent(cookieValue);
474
+ }
475
+ }
476
+ return void 0;
477
+ },
478
+ /**
479
+ * 移除Cookie
480
+ * @param key - 键
481
+ * @param options - Cookie选项
482
+ */
483
+ remove(key, options = {}) {
484
+ cookie.set(key, "", {
485
+ ...options,
486
+ expires: /* @__PURE__ */ new Date(0)
487
+ });
488
+ },
489
+ /**
490
+ * 获取所有Cookie
491
+ * @returns Cookie对象
492
+ */
493
+ getAll() {
494
+ if (typeof document === "undefined") return {};
495
+ const cookies = {};
496
+ document.cookie.split(";").forEach((cookieStr) => {
497
+ const [key, value] = cookieStr.trim().split("=");
498
+ if (key && value) {
499
+ cookies[decodeURIComponent(key)] = decodeURIComponent(value);
500
+ }
501
+ });
502
+ return cookies;
503
+ }
504
+ };
505
+ var storage = {
506
+ /**
507
+ * 设置值(优先使用localStorage,失败则使用sessionStorage)
508
+ * @param key - 键
509
+ * @param value - 值
510
+ * @param options - 选项(过期时间、公钥等)
511
+ * @returns Promise<void>
512
+ */
513
+ async set(key, value, options = {}) {
514
+ try {
515
+ await localStorage.set(key, value, options);
516
+ } catch {
517
+ try {
518
+ await sessionStorage.set(key, value, { publicKey: options.publicKey });
519
+ } catch {
520
+ console.warn("Both localStorage and sessionStorage are not available");
521
+ }
522
+ }
523
+ },
524
+ /**
525
+ * 获取值
526
+ * @param key - 键
527
+ * @param options - 选项(默认值、私钥等)
528
+ * @returns Promise<T | undefined> 值或默认值
529
+ */
530
+ async get(key, options = {}) {
531
+ const localValue = await localStorage.get(key, options);
532
+ if (localValue !== void 0) return localValue;
533
+ const sessionValue = await sessionStorage.get(key, options);
534
+ return sessionValue !== void 0 ? sessionValue : options.defaultValue;
535
+ },
536
+ /**
537
+ * 移除值
538
+ * @param key - 键
539
+ */
540
+ remove(key) {
541
+ localStorage.remove(key);
542
+ sessionStorage.remove(key);
543
+ },
544
+ /**
545
+ * 清空所有值
546
+ */
547
+ clear() {
548
+ localStorage.clear();
549
+ sessionStorage.clear();
550
+ }
551
+ };
552
+
553
+ export { cookie, getStorageKeyPair, localStorage, sessionStorage, setStorageKeyPair, storage };
@@ -106,5 +106,3 @@ exports.snakeCase = snakeCase;
106
106
  exports.template = template;
107
107
  exports.truncate = truncate;
108
108
  exports.unescapeHtml = unescapeHtml;
109
- //# sourceMappingURL=chunk-D7CS5EKF.js.map
110
- //# sourceMappingURL=chunk-D7CS5EKF.js.map