@alibarbar/common 1.1.2 → 1.1.4

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,141 +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;
1
+ interface SecureStorageOptions {
2
+ /** 存储后端:localStorage 或 sessionStorage */
3
+ storage?: Storage;
4
+ /** 命名空间前缀 */
5
+ namespace?: string;
6
+ /** 默认过期时间(毫秒) */
7
+ defaultExpire?: number | null;
17
8
  }
18
9
  /**
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;
44
- }
45
- /**
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
10
+ * RSA 混合加密存储类(单例模式)
59
11
  */
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操作封装
75
- */
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
+ * 获取完整键名(带命名空间)
34
+ */
35
+ private _getFullKey;
36
+ /**
37
+ * 检查数据是否过期
38
+ */
39
+ private _isExpired;
40
+ /**
41
+ * 设置值
42
+ */
43
+ set<T>(key: string, value: T, expire?: number | null): Promise<boolean>;
44
+ /**
45
+ * 获取值
46
+ */
47
+ get<T = unknown>(key: string, defaultValue?: T | null): Promise<T | null>;
48
+ /**
49
+ * 删除值
50
+ */
51
+ remove(key: string): boolean;
52
+ /**
53
+ * 检查键是否存在
54
+ */
55
+ has(key: string): Promise<boolean>;
56
+ /**
57
+ * 清空所有数据
58
+ */
59
+ clear(): number;
60
+ /**
61
+ * 获取所有键名
108
62
  */
109
- getAll(): Record<string, string>;
110
- };
111
- /**
112
- * 统一安全存储接口(通过后端适配器封装 localStorage / sessionStorage / Cookie)
113
- */
114
- type SecureStorageBackendType = 'local' | 'session' | 'cookie';
115
- interface SecureStorageInstance {
116
- set<T>(key: string, value: T, options?: {
117
- expiry?: number;
118
- publicKey?: CryptoKey;
119
- privateKey?: CryptoKey;
120
- defaultValue?: T;
121
- }): Promise<void>;
122
- get<T>(key: string, options?: {
123
- defaultValue?: T;
124
- privateKey?: CryptoKey;
125
- }): Promise<T | undefined>;
126
- remove(key: string): void;
127
- clear(): void;
128
63
  keys(): string[];
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;
129
80
  }
130
- /**
131
- * 工厂函数:根据后端类型创建 SecureStorage 实例
132
- * 使用方可在应用启动时自行决定使用 local / session / cookie
133
- */
134
- declare function createSecureStorage(type?: SecureStorageBackendType): SecureStorageInstance;
135
- /**
136
- * 默认导出的 SecureStorage:使用 localStorage 作为后端
137
- * 与之前 `secureStorage` 的语义保持一致
138
- */
139
- declare const secureStorage: SecureStorageInstance;
81
+ declare const secureStorage: SecureStorage;
140
82
 
141
- export { type SecureStorageInstance, type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
83
+ export { SecureStorage, type SecureStorageOptions, SecureStorage as default, secureStorage };
package/dist/storage.d.ts CHANGED
@@ -1,141 +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;
1
+ interface SecureStorageOptions {
2
+ /** 存储后端:localStorage 或 sessionStorage */
3
+ storage?: Storage;
4
+ /** 命名空间前缀 */
5
+ namespace?: string;
6
+ /** 默认过期时间(毫秒) */
7
+ defaultExpire?: number | null;
17
8
  }
18
9
  /**
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;
44
- }
45
- /**
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
10
+ * RSA 混合加密存储类(单例模式)
59
11
  */
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操作封装
75
- */
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
+ * 获取完整键名(带命名空间)
34
+ */
35
+ private _getFullKey;
36
+ /**
37
+ * 检查数据是否过期
38
+ */
39
+ private _isExpired;
40
+ /**
41
+ * 设置值
42
+ */
43
+ set<T>(key: string, value: T, expire?: number | null): Promise<boolean>;
44
+ /**
45
+ * 获取值
46
+ */
47
+ get<T = unknown>(key: string, defaultValue?: T | null): Promise<T | null>;
48
+ /**
49
+ * 删除值
50
+ */
51
+ remove(key: string): boolean;
52
+ /**
53
+ * 检查键是否存在
54
+ */
55
+ has(key: string): Promise<boolean>;
56
+ /**
57
+ * 清空所有数据
58
+ */
59
+ clear(): number;
60
+ /**
61
+ * 获取所有键名
108
62
  */
109
- getAll(): Record<string, string>;
110
- };
111
- /**
112
- * 统一安全存储接口(通过后端适配器封装 localStorage / sessionStorage / Cookie)
113
- */
114
- type SecureStorageBackendType = 'local' | 'session' | 'cookie';
115
- interface SecureStorageInstance {
116
- set<T>(key: string, value: T, options?: {
117
- expiry?: number;
118
- publicKey?: CryptoKey;
119
- privateKey?: CryptoKey;
120
- defaultValue?: T;
121
- }): Promise<void>;
122
- get<T>(key: string, options?: {
123
- defaultValue?: T;
124
- privateKey?: CryptoKey;
125
- }): Promise<T | undefined>;
126
- remove(key: string): void;
127
- clear(): void;
128
63
  keys(): string[];
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;
129
80
  }
130
- /**
131
- * 工厂函数:根据后端类型创建 SecureStorage 实例
132
- * 使用方可在应用启动时自行决定使用 local / session / cookie
133
- */
134
- declare function createSecureStorage(type?: SecureStorageBackendType): SecureStorageInstance;
135
- /**
136
- * 默认导出的 SecureStorage:使用 localStorage 作为后端
137
- * 与之前 `secureStorage` 的语义保持一致
138
- */
139
- declare const secureStorage: SecureStorageInstance;
81
+ declare const secureStorage: SecureStorage;
140
82
 
141
- export { type SecureStorageInstance, type StorageInitOptions, type StorageOptions, clearPersistedKeys, cookie, createSecureStorage, getKeyUsageCount, getStorageKeyPair, initializeStorageKeys, resetKeyUsageCount, secureStorage, setStorageKeyPair };
83
+ export { SecureStorage, type SecureStorageOptions, SecureStorage as default, secureStorage };