@alibarbar/common 1.1.1 → 1.1.3

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.
@@ -1,153 +1,83 @@
1
- import { RSAKeyPair } from './crypto.mjs';
2
-
3
- /**
4
- * 存储工具函数
5
- */
6
-
7
- /**
8
- * 存储配置选项
9
- */
10
- interface StorageOptions {
11
- /** RSA公钥(用于加密) */
12
- publicKey?: CryptoKey;
13
- /** RSA私钥(用于解密) */
14
- privateKey?: CryptoKey;
15
- /** 是否启用加密,默认为 true */
16
- encrypt?: boolean;
17
- }
18
- /**
19
- * 存储初始化配置
20
- */
21
- interface StorageInitOptions {
22
- /** 是否自动生成密钥对(如果未设置),默认为 true */
23
- autoGenerateKeys?: boolean;
24
- /** 是否持久化密钥到 localStorage,默认为 false */
25
- persistKeys?: boolean;
26
- /** 密钥存储键名(用于持久化),如果未提供将自动生成随机键名 */
27
- keyStorageKey?: string;
28
- /** 用于加密持久化私钥的密码(如果提供,将使用 PBKDF2 + AES-GCM 加密) */
29
- keyEncryptionPassword?: string;
30
- /** PBKDF2 迭代次数,默认为 100000 */
31
- pbkdf2Iterations?: number;
32
- /** RSA密钥长度,默认为 2048 */
33
- keyModulusLength?: number;
34
- /** 是否启用HMAC数据完整性校验,默认为 true */
35
- enableHMAC?: boolean;
36
- /** 是否启用时间戳验证(防重放攻击),默认为 true */
37
- enableTimestampValidation?: boolean;
38
- /** 时间戳有效期(毫秒),默认 7 天,0 表示不验证 */
39
- timestampMaxAge?: number;
40
- /** 是否为生产环境(影响日志输出),默认为 false */
41
- isProduction?: boolean;
42
- /** 是否强制重新初始化(即使已经初始化过),默认为 false */
43
- forceReinitialize?: boolean;
1
+ interface SecureStorageOptions {
2
+ /** 存储后端:localStorage 或 sessionStorage */
3
+ storage?: Storage;
4
+ /** 命名空间前缀 */
5
+ namespace?: string;
6
+ /** 默认过期时间(毫秒) */
7
+ defaultExpire?: number | null;
44
8
  }
45
9
  /**
46
- * 初始化存储密钥对
47
- * @param options - 初始化选项
48
- * @returns Promise<void>
49
- */
50
- declare function initializeStorageKeys(options?: StorageInitOptions): Promise<void>;
51
- /**
52
- * 设置全局RSA密钥对
53
- * @param keyPair - RSA密钥对
54
- */
55
- declare function setStorageKeyPair(keyPair: RSAKeyPair): void;
56
- /**
57
- * 获取全局密钥对
58
- * @returns 全局密钥对或null
59
- */
60
- declare function getStorageKeyPair(): RSAKeyPair | null;
61
- /**
62
- * 清除持久化的密钥(如果存在)
63
- */
64
- declare function clearPersistedKeys(): void;
65
- /**
66
- * 获取密钥使用计数
67
- */
68
- declare function getKeyUsageCount(): number;
69
- /**
70
- * 重置密钥使用计数
71
- */
72
- declare function resetKeyUsageCount(): void;
73
- /**
74
- * Cookie操作封装
10
+ * RSA 混合加密存储类(单例模式)
75
11
  */
76
- declare const cookie: {
12
+ declare class SecureStorage {
13
+ private static _instances;
14
+ static get instances(): Map<string, SecureStorage>;
15
+ private readonly namespace;
16
+ private readonly storage;
17
+ private readonly defaultExpire;
18
+ private readonly instanceKey;
19
+ constructor(options: SecureStorageOptions);
77
20
  /**
78
- * 设置Cookie
79
- * @param key - 键
80
- * @param value - 值
81
- * @param options - Cookie选项
21
+ * 获取单例实例(静态方法)
82
22
  */
83
- set(key: string, value: string, options?: {
84
- expires?: number | Date;
85
- path?: string;
86
- domain?: string;
87
- secure?: boolean;
88
- sameSite?: "strict" | "lax" | "none";
89
- }): void;
23
+ static getInstance(options: SecureStorageOptions): SecureStorage;
90
24
  /**
91
- * 获取Cookie
92
- * @param key - 键
93
- * @returns Cookie值
25
+ * 清除所有单例实例(用于测试或重置)
94
26
  */
95
- get(key: string): string | undefined;
27
+ static clearInstances(): void;
96
28
  /**
97
- * 移除Cookie
98
- * @param key - 键
99
- * @param options - Cookie选项
29
+ * 获取当前所有实例的数量(用于调试)
100
30
  */
101
- remove(key: string, options?: {
102
- path?: string;
103
- domain?: string;
104
- }): void;
31
+ static getInstanceCount(): number;
105
32
  /**
106
- * 获取所有Cookie
107
- * @returns Cookie对象
33
+ * 获取完整键名(带命名空间)
108
34
  */
109
- getAll(): Record<string, string>;
110
- };
111
- /**
112
- * 统一安全存储接口(自动选择存储方式,支持RSA加密)
113
- * 这是推荐的主要导出接口
114
- */
115
- declare const secureStorage: {
35
+ private _getFullKey;
116
36
  /**
117
- * 设置值(优先使用localStorage,失败则使用sessionStorage)
118
- * @param key - 键
119
- * @param value - 值
120
- * @param options - 选项(过期时间、公钥等)
121
- * @returns Promise<void>
37
+ * 检查数据是否过期
122
38
  */
123
- set<T>(key: string, value: T, options?: {
124
- expiry?: number;
125
- publicKey?: CryptoKey;
126
- }): Promise<void>;
39
+ private _isExpired;
40
+ /**
41
+ * 设置值
42
+ */
43
+ set<T>(key: string, value: T, expire?: number | null): Promise<boolean>;
127
44
  /**
128
45
  * 获取值
129
- * @param key - 键
130
- * @param options - 选项(默认值、私钥等)
131
- * @returns Promise<T | undefined> 值或默认值
132
46
  */
133
- get<T>(key: string, options?: {
134
- defaultValue?: T;
135
- privateKey?: CryptoKey;
136
- }): Promise<T | undefined>;
47
+ get<T = unknown>(key: string, defaultValue?: T | null): Promise<T | null>;
48
+ /**
49
+ * 删除值
50
+ */
51
+ remove(key: string): boolean;
137
52
  /**
138
- * 移除值
139
- * @param key - 键
53
+ * 检查键是否存在
140
54
  */
141
- remove(key: string): void;
55
+ has(key: string): Promise<boolean>;
142
56
  /**
143
- * 清空所有值
57
+ * 清空所有数据
144
58
  */
145
- clear(): void;
59
+ clear(): number;
146
60
  /**
147
61
  * 获取所有键名
148
- * @returns 键名数组
149
62
  */
150
63
  keys(): string[];
151
- };
64
+ /**
65
+ * 获取数据条数
66
+ */
67
+ size(): number;
68
+ /**
69
+ * 清理过期数据
70
+ */
71
+ clearExpired(): Promise<number>;
72
+ /**
73
+ * 获取实例的唯一标识
74
+ */
75
+ getInstanceKey(): string;
76
+ /**
77
+ * 检查是否为单例实例
78
+ */
79
+ isSingleton(): boolean;
80
+ }
81
+ declare const secureStorage: SecureStorage;
152
82
 
153
- export { type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
83
+ export { SecureStorage, type SecureStorageOptions, SecureStorage as default, secureStorage };
package/dist/storage.d.ts CHANGED
@@ -1,153 +1,83 @@
1
- import { RSAKeyPair } from './crypto.js';
2
-
3
- /**
4
- * 存储工具函数
5
- */
6
-
7
- /**
8
- * 存储配置选项
9
- */
10
- interface StorageOptions {
11
- /** RSA公钥(用于加密) */
12
- publicKey?: CryptoKey;
13
- /** RSA私钥(用于解密) */
14
- privateKey?: CryptoKey;
15
- /** 是否启用加密,默认为 true */
16
- encrypt?: boolean;
17
- }
18
- /**
19
- * 存储初始化配置
20
- */
21
- interface StorageInitOptions {
22
- /** 是否自动生成密钥对(如果未设置),默认为 true */
23
- autoGenerateKeys?: boolean;
24
- /** 是否持久化密钥到 localStorage,默认为 false */
25
- persistKeys?: boolean;
26
- /** 密钥存储键名(用于持久化),如果未提供将自动生成随机键名 */
27
- keyStorageKey?: string;
28
- /** 用于加密持久化私钥的密码(如果提供,将使用 PBKDF2 + AES-GCM 加密) */
29
- keyEncryptionPassword?: string;
30
- /** PBKDF2 迭代次数,默认为 100000 */
31
- pbkdf2Iterations?: number;
32
- /** RSA密钥长度,默认为 2048 */
33
- keyModulusLength?: number;
34
- /** 是否启用HMAC数据完整性校验,默认为 true */
35
- enableHMAC?: boolean;
36
- /** 是否启用时间戳验证(防重放攻击),默认为 true */
37
- enableTimestampValidation?: boolean;
38
- /** 时间戳有效期(毫秒),默认 7 天,0 表示不验证 */
39
- timestampMaxAge?: number;
40
- /** 是否为生产环境(影响日志输出),默认为 false */
41
- isProduction?: boolean;
42
- /** 是否强制重新初始化(即使已经初始化过),默认为 false */
43
- forceReinitialize?: boolean;
1
+ interface SecureStorageOptions {
2
+ /** 存储后端:localStorage 或 sessionStorage */
3
+ storage?: Storage;
4
+ /** 命名空间前缀 */
5
+ namespace?: string;
6
+ /** 默认过期时间(毫秒) */
7
+ defaultExpire?: number | null;
44
8
  }
45
9
  /**
46
- * 初始化存储密钥对
47
- * @param options - 初始化选项
48
- * @returns Promise<void>
49
- */
50
- declare function initializeStorageKeys(options?: StorageInitOptions): Promise<void>;
51
- /**
52
- * 设置全局RSA密钥对
53
- * @param keyPair - RSA密钥对
54
- */
55
- declare function setStorageKeyPair(keyPair: RSAKeyPair): void;
56
- /**
57
- * 获取全局密钥对
58
- * @returns 全局密钥对或null
59
- */
60
- declare function getStorageKeyPair(): RSAKeyPair | null;
61
- /**
62
- * 清除持久化的密钥(如果存在)
63
- */
64
- declare function clearPersistedKeys(): void;
65
- /**
66
- * 获取密钥使用计数
67
- */
68
- declare function getKeyUsageCount(): number;
69
- /**
70
- * 重置密钥使用计数
71
- */
72
- declare function resetKeyUsageCount(): void;
73
- /**
74
- * Cookie操作封装
10
+ * RSA 混合加密存储类(单例模式)
75
11
  */
76
- declare const cookie: {
12
+ declare class SecureStorage {
13
+ private static _instances;
14
+ static get instances(): Map<string, SecureStorage>;
15
+ private readonly namespace;
16
+ private readonly storage;
17
+ private readonly defaultExpire;
18
+ private readonly instanceKey;
19
+ constructor(options: SecureStorageOptions);
77
20
  /**
78
- * 设置Cookie
79
- * @param key - 键
80
- * @param value - 值
81
- * @param options - Cookie选项
21
+ * 获取单例实例(静态方法)
82
22
  */
83
- set(key: string, value: string, options?: {
84
- expires?: number | Date;
85
- path?: string;
86
- domain?: string;
87
- secure?: boolean;
88
- sameSite?: "strict" | "lax" | "none";
89
- }): void;
23
+ static getInstance(options: SecureStorageOptions): SecureStorage;
90
24
  /**
91
- * 获取Cookie
92
- * @param key - 键
93
- * @returns Cookie值
25
+ * 清除所有单例实例(用于测试或重置)
94
26
  */
95
- get(key: string): string | undefined;
27
+ static clearInstances(): void;
96
28
  /**
97
- * 移除Cookie
98
- * @param key - 键
99
- * @param options - Cookie选项
29
+ * 获取当前所有实例的数量(用于调试)
100
30
  */
101
- remove(key: string, options?: {
102
- path?: string;
103
- domain?: string;
104
- }): void;
31
+ static getInstanceCount(): number;
105
32
  /**
106
- * 获取所有Cookie
107
- * @returns Cookie对象
33
+ * 获取完整键名(带命名空间)
108
34
  */
109
- getAll(): Record<string, string>;
110
- };
111
- /**
112
- * 统一安全存储接口(自动选择存储方式,支持RSA加密)
113
- * 这是推荐的主要导出接口
114
- */
115
- declare const secureStorage: {
35
+ private _getFullKey;
116
36
  /**
117
- * 设置值(优先使用localStorage,失败则使用sessionStorage)
118
- * @param key - 键
119
- * @param value - 值
120
- * @param options - 选项(过期时间、公钥等)
121
- * @returns Promise<void>
37
+ * 检查数据是否过期
122
38
  */
123
- set<T>(key: string, value: T, options?: {
124
- expiry?: number;
125
- publicKey?: CryptoKey;
126
- }): Promise<void>;
39
+ private _isExpired;
40
+ /**
41
+ * 设置值
42
+ */
43
+ set<T>(key: string, value: T, expire?: number | null): Promise<boolean>;
127
44
  /**
128
45
  * 获取值
129
- * @param key - 键
130
- * @param options - 选项(默认值、私钥等)
131
- * @returns Promise<T | undefined> 值或默认值
132
46
  */
133
- get<T>(key: string, options?: {
134
- defaultValue?: T;
135
- privateKey?: CryptoKey;
136
- }): Promise<T | undefined>;
47
+ get<T = unknown>(key: string, defaultValue?: T | null): Promise<T | null>;
48
+ /**
49
+ * 删除值
50
+ */
51
+ remove(key: string): boolean;
137
52
  /**
138
- * 移除值
139
- * @param key - 键
53
+ * 检查键是否存在
140
54
  */
141
- remove(key: string): void;
55
+ has(key: string): Promise<boolean>;
142
56
  /**
143
- * 清空所有值
57
+ * 清空所有数据
144
58
  */
145
- clear(): void;
59
+ clear(): number;
146
60
  /**
147
61
  * 获取所有键名
148
- * @returns 键名数组
149
62
  */
150
63
  keys(): string[];
151
- };
64
+ /**
65
+ * 获取数据条数
66
+ */
67
+ size(): number;
68
+ /**
69
+ * 清理过期数据
70
+ */
71
+ clearExpired(): Promise<number>;
72
+ /**
73
+ * 获取实例的唯一标识
74
+ */
75
+ getInstanceKey(): string;
76
+ /**
77
+ * 检查是否为单例实例
78
+ */
79
+ isSingleton(): boolean;
80
+ }
81
+ declare const secureStorage: SecureStorage;
152
82
 
153
- export { type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
83
+ export { SecureStorage, type SecureStorageOptions, SecureStorage as default, secureStorage };